blob: c3bf5b0a2564f59012af458c6c9e8600d960f42b [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 Moolenaar5dd95a12006-05-13 12:09:24 +00002111 spell_cat_line(buf + STRLEN(buf),
2112 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002113
2114 p = buf + skip;
2115 endp = buf + len;
2116 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002117 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002118 /* When searching backward don't search after the cursor. Unless
2119 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002120 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00002121 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002122 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00002123 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002124 break;
2125
2126 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002127 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002128 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002129
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002130 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002131 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002132 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002133 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002134 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002135 /* When searching forward only accept a bad word after
2136 * the cursor. */
2137 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002138 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00002139 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002140 && (wrapped
2141 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00002142 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002143 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002144 {
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002145# ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00002146 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002147 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002148 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002149 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar51485f02005-06-04 21:55:20 +00002150 FALSE, &can_spell);
Bram Moolenaard68071d2006-05-02 22:08:30 +00002151 if (!can_spell)
2152 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002153 }
2154 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002155#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002156 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002157
Bram Moolenaar51485f02005-06-04 21:55:20 +00002158 if (can_spell)
2159 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00002160 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002161 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002162 found_pos.col = (int)(p - buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002163#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00002164 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002165#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002166 if (dir == FORWARD)
2167 {
2168 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00002169 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002170 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002171 if (attrp != NULL)
2172 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002173 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002174 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002175 else if (curline)
2176 /* Insert mode completion: put cursor after
2177 * the bad word. */
2178 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002179 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002180 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002181 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00002182 else
2183 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002184 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002185 }
2186
Bram Moolenaar51485f02005-06-04 21:55:20 +00002187 /* advance to character after the word */
2188 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002189 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002190 }
2191
Bram Moolenaar5195e452005-08-19 20:32:47 +00002192 if (dir == BACKWARD && found_pos.lnum != 0)
2193 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002194 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00002195 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002196 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002197 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002198 }
2199
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002200 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002201 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002202
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002203 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002204 if (dir == BACKWARD)
2205 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002206 /* If we are back at the starting line and searched it again there
2207 * is no match, give up. */
2208 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002209 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002210
2211 if (lnum > 1)
2212 --lnum;
2213 else if (!p_ws)
2214 break; /* at first line and 'nowrapscan' */
2215 else
2216 {
2217 /* Wrap around to the end of the buffer. May search the
2218 * starting line again and accept the last match. */
2219 lnum = wp->w_buffer->b_ml.ml_line_count;
2220 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002221 if (!shortmess(SHM_SEARCH))
2222 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002223 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002224 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002225 }
2226 else
2227 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002228 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2229 ++lnum;
2230 else if (!p_ws)
2231 break; /* at first line and 'nowrapscan' */
2232 else
2233 {
2234 /* Wrap around to the start of the buffer. May search the
2235 * starting line again and accept the first match. */
2236 lnum = 1;
2237 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002238 if (!shortmess(SHM_SEARCH))
2239 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002240 }
2241
2242 /* If we are back at the starting line and there is no match then
2243 * give up. */
2244 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002245 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002246
2247 /* Skip the characters at the start of the next line that were
2248 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002249 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002250 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002251 else
2252 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002253
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002254 /* Capcol skips over the inserted space. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002255 --capcol;
2256
2257 /* But after empty line check first word in next line */
2258 if (*skipwhite(line) == NUL)
2259 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002260 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002261
2262 line_breakcheck();
2263 }
2264
Bram Moolenaar0c405862005-06-22 22:26:26 +00002265 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002266 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002267}
2268
2269/*
2270 * For spell checking: concatenate the start of the following line "line" into
2271 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
2272 */
2273 void
2274spell_cat_line(buf, line, maxlen)
2275 char_u *buf;
2276 char_u *line;
2277 int maxlen;
2278{
2279 char_u *p;
2280 int n;
2281
2282 p = skipwhite(line);
2283 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2284 p = skipwhite(p + 1);
2285
2286 if (*p != NUL)
2287 {
2288 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002289 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002290 n = (int)(p - line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002291 if (n >= maxlen)
2292 n = maxlen - 1;
2293 vim_memset(buf + 1, ' ', n);
2294 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002295}
2296
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002297/*
2298 * Structure used for the cookie argument of do_in_runtimepath().
2299 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002300typedef struct spelload_S
2301{
2302 char_u sl_lang[MAXWLEN + 1]; /* language name */
2303 slang_T *sl_slang; /* resulting slang_T struct */
2304 int sl_nobreak; /* NOBREAK language found */
2305} spelload_T;
2306
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002307/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002308 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002309 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002310 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002311 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002312spell_load_lang(lang)
2313 char_u *lang;
2314{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002315 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002316 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002317 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002318#ifdef FEAT_AUTOCMD
2319 int round;
2320#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002321
Bram Moolenaarb765d632005-06-07 21:00:02 +00002322 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002323 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002324 STRCPY(sl.sl_lang, lang);
2325 sl.sl_slang = NULL;
2326 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002327
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002328#ifdef FEAT_AUTOCMD
2329 /* We may retry when no spell file is found for the language, an
2330 * autocommand may load it then. */
2331 for (round = 1; round <= 2; ++round)
2332#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002333 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002334 /*
2335 * Find the first spell file for "lang" in 'runtimepath' and load it.
2336 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002337 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002338 "spell/%s.%s.spl", lang, spell_enc());
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002339 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002340
2341 if (r == FAIL && *sl.sl_lang != NUL)
2342 {
2343 /* Try loading the ASCII version. */
2344 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
2345 "spell/%s.ascii.spl", lang);
2346 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
2347
2348#ifdef FEAT_AUTOCMD
2349 if (r == FAIL && *sl.sl_lang != NUL && round == 1
2350 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
2351 curbuf->b_fname, FALSE, curbuf))
2352 continue;
2353 break;
2354#endif
2355 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002356#ifdef FEAT_AUTOCMD
2357 break;
2358#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002359 }
2360
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002361 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002362 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002363 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2364 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002365 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002366 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002367 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002368 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002369 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002370 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002371 }
2372}
2373
2374/*
2375 * Return the encoding used for spell checking: Use 'encoding', except that we
2376 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2377 */
2378 static char_u *
2379spell_enc()
2380{
2381
2382#ifdef FEAT_MBYTE
2383 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2384 return p_enc;
2385#endif
2386 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002387}
2388
2389/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002390 * Get the name of the .spl file for the internal wordlist into
2391 * "fname[MAXPATHL]".
2392 */
2393 static void
2394int_wordlist_spl(fname)
2395 char_u *fname;
2396{
2397 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
2398 int_wordlist, spell_enc());
2399}
2400
2401/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002402 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002403 * Caller must fill "sl_next".
2404 */
2405 static slang_T *
2406slang_alloc(lang)
2407 char_u *lang;
2408{
2409 slang_T *lp;
2410
Bram Moolenaar51485f02005-06-04 21:55:20 +00002411 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002412 if (lp != NULL)
2413 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002414 if (lang != NULL)
2415 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002416 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002417 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002418 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002419 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002420 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002421 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002422
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002423 return lp;
2424}
2425
2426/*
2427 * Free the contents of an slang_T and the structure itself.
2428 */
2429 static void
2430slang_free(lp)
2431 slang_T *lp;
2432{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002433 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002434 vim_free(lp->sl_fname);
2435 slang_clear(lp);
2436 vim_free(lp);
2437}
2438
2439/*
2440 * Clear an slang_T so that the file can be reloaded.
2441 */
2442 static void
2443slang_clear(lp)
2444 slang_T *lp;
2445{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002446 garray_T *gap;
2447 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002448 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002449 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002450 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002451
Bram Moolenaar51485f02005-06-04 21:55:20 +00002452 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002453 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002454 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002455 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002456 vim_free(lp->sl_pbyts);
2457 lp->sl_pbyts = NULL;
2458
Bram Moolenaar51485f02005-06-04 21:55:20 +00002459 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002460 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002461 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002462 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002463 vim_free(lp->sl_pidxs);
2464 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002465
Bram Moolenaar4770d092006-01-12 23:22:24 +00002466 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002467 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002468 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
2469 while (gap->ga_len > 0)
2470 {
2471 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2472 vim_free(ftp->ft_from);
2473 vim_free(ftp->ft_to);
2474 }
2475 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002476 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002477
2478 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002479 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002480 {
2481 /* "ga_len" is set to 1 without adding an item for latin1 */
2482 if (gap->ga_data != NULL)
2483 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2484 for (i = 0; i < gap->ga_len; ++i)
2485 vim_free(((int **)gap->ga_data)[i]);
2486 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002487 else
2488 /* SAL items: free salitem_T items */
2489 while (gap->ga_len > 0)
2490 {
2491 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2492 vim_free(smp->sm_lead);
2493 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2494 vim_free(smp->sm_to);
2495#ifdef FEAT_MBYTE
2496 vim_free(smp->sm_lead_w);
2497 vim_free(smp->sm_oneof_w);
2498 vim_free(smp->sm_to_w);
2499#endif
2500 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002501 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002502
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002503 for (i = 0; i < lp->sl_prefixcnt; ++i)
2504 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002505 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002506 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002507 lp->sl_prefprog = NULL;
2508
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002509 vim_free(lp->sl_info);
2510 lp->sl_info = NULL;
2511
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002512 vim_free(lp->sl_midword);
2513 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002514
Bram Moolenaar5195e452005-08-19 20:32:47 +00002515 vim_free(lp->sl_compprog);
2516 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002517 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002518 lp->sl_compprog = NULL;
2519 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002520 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002521
2522 vim_free(lp->sl_syllable);
2523 lp->sl_syllable = NULL;
2524 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002525
Bram Moolenaar899dddf2006-03-26 21:06:50 +00002526 ga_clear_strings(&lp->sl_comppat);
2527
Bram Moolenaar4770d092006-01-12 23:22:24 +00002528 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2529 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002530
Bram Moolenaar4770d092006-01-12 23:22:24 +00002531#ifdef FEAT_MBYTE
2532 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaarea424162005-06-16 21:51:00 +00002533#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002534
Bram Moolenaar4770d092006-01-12 23:22:24 +00002535 /* Clear info from .sug file. */
2536 slang_clear_sug(lp);
2537
Bram Moolenaar5195e452005-08-19 20:32:47 +00002538 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002539 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002540 lp->sl_compsylmax = MAXWLEN;
2541 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002542}
2543
2544/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002545 * Clear the info from the .sug file in "lp".
2546 */
2547 static void
2548slang_clear_sug(lp)
2549 slang_T *lp;
2550{
2551 vim_free(lp->sl_sbyts);
2552 lp->sl_sbyts = NULL;
2553 vim_free(lp->sl_sidxs);
2554 lp->sl_sidxs = NULL;
2555 close_spellbuf(lp->sl_sugbuf);
2556 lp->sl_sugbuf = NULL;
2557 lp->sl_sugloaded = FALSE;
2558 lp->sl_sugtime = 0;
2559}
2560
2561/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002562 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002563 * Invoked through do_in_runtimepath().
2564 */
2565 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002566spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002567 char_u *fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002568 void *cookie;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002569{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002570 spelload_T *slp = (spelload_T *)cookie;
2571 slang_T *slang;
2572
2573 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2574 if (slang != NULL)
2575 {
2576 /* When a previously loaded file has NOBREAK also use it for the
2577 * ".add" files. */
2578 if (slp->sl_nobreak && slang->sl_add)
2579 slang->sl_nobreak = TRUE;
2580 else if (slang->sl_nobreak)
2581 slp->sl_nobreak = TRUE;
2582
2583 slp->sl_slang = slang;
2584 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002585}
2586
2587/*
2588 * Load one spell file and store the info into a slang_T.
2589 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00002590 * This is invoked in three ways:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002591 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2592 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2593 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2594 * points to the existing slang_T.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002595 * - Just after writing a .spl file; it's read back to produce the .sug file.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002596 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
2597 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002598 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002599 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002600 static slang_T *
2601spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002602 char_u *fname;
2603 char_u *lang;
2604 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002605 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002606{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002607 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002608 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002609 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002610 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002611 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002612 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002613 char_u *save_sourcing_name = sourcing_name;
2614 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002615 slang_T *lp = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002616 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002617 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002618
Bram Moolenaarb765d632005-06-07 21:00:02 +00002619 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002620 if (fd == NULL)
2621 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002622 if (!silent)
2623 EMSG2(_(e_notopen), fname);
2624 else if (p_verbose > 2)
2625 {
2626 verbose_enter();
2627 smsg((char_u *)e_notopen, fname);
2628 verbose_leave();
2629 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002630 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002631 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002632 if (p_verbose > 2)
2633 {
2634 verbose_enter();
2635 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2636 verbose_leave();
2637 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002638
Bram Moolenaarb765d632005-06-07 21:00:02 +00002639 if (old_lp == NULL)
2640 {
2641 lp = slang_alloc(lang);
2642 if (lp == NULL)
2643 goto endFAIL;
2644
2645 /* Remember the file name, used to reload the file when it's updated. */
2646 lp->sl_fname = vim_strsave(fname);
2647 if (lp->sl_fname == NULL)
2648 goto endFAIL;
2649
2650 /* Check for .add.spl. */
2651 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2652 }
2653 else
2654 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002655
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002656 /* Set sourcing_name, so that error messages mention the file name. */
2657 sourcing_name = fname;
2658 sourcing_lnum = 0;
2659
Bram Moolenaar4770d092006-01-12 23:22:24 +00002660 /*
2661 * <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002662 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002663 for (i = 0; i < VIMSPELLMAGICL; ++i)
2664 buf[i] = getc(fd); /* <fileID> */
2665 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2666 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002667 EMSG(_("E757: This does not look like a spell file"));
2668 goto endFAIL;
2669 }
2670 c = getc(fd); /* <versionnr> */
2671 if (c < VIMSPELLVERSION)
2672 {
2673 EMSG(_("E771: Old spell file, needs to be updated"));
2674 goto endFAIL;
2675 }
2676 else if (c > VIMSPELLVERSION)
2677 {
2678 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002679 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002680 }
2681
Bram Moolenaar5195e452005-08-19 20:32:47 +00002682
2683 /*
2684 * <SECTIONS>: <section> ... <sectionend>
2685 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2686 */
2687 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002688 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002689 n = getc(fd); /* <sectionID> or <sectionend> */
2690 if (n == SN_END)
2691 break;
2692 c = getc(fd); /* <sectionflags> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002693 len = get4c(fd); /* <sectionlen> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002694 if (len < 0)
2695 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002696
Bram Moolenaar5195e452005-08-19 20:32:47 +00002697 res = 0;
2698 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002699 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002700 case SN_INFO:
2701 lp->sl_info = read_string(fd, len); /* <infotext> */
2702 if (lp->sl_info == NULL)
2703 goto endFAIL;
2704 break;
2705
Bram Moolenaar5195e452005-08-19 20:32:47 +00002706 case SN_REGION:
2707 res = read_region_section(fd, lp, len);
2708 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002709
Bram Moolenaar5195e452005-08-19 20:32:47 +00002710 case SN_CHARFLAGS:
2711 res = read_charflags_section(fd);
2712 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002713
Bram Moolenaar5195e452005-08-19 20:32:47 +00002714 case SN_MIDWORD:
2715 lp->sl_midword = read_string(fd, len); /* <midword> */
2716 if (lp->sl_midword == NULL)
2717 goto endFAIL;
2718 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002719
Bram Moolenaar5195e452005-08-19 20:32:47 +00002720 case SN_PREFCOND:
2721 res = read_prefcond_section(fd, lp);
2722 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002723
Bram Moolenaar5195e452005-08-19 20:32:47 +00002724 case SN_REP:
Bram Moolenaar4770d092006-01-12 23:22:24 +00002725 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
2726 break;
2727
2728 case SN_REPSAL:
2729 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002730 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002731
Bram Moolenaar5195e452005-08-19 20:32:47 +00002732 case SN_SAL:
2733 res = read_sal_section(fd, lp);
2734 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002735
Bram Moolenaar5195e452005-08-19 20:32:47 +00002736 case SN_SOFO:
2737 res = read_sofo_section(fd, lp);
2738 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002739
Bram Moolenaar5195e452005-08-19 20:32:47 +00002740 case SN_MAP:
2741 p = read_string(fd, len); /* <mapstr> */
2742 if (p == NULL)
2743 goto endFAIL;
2744 set_map_str(lp, p);
2745 vim_free(p);
2746 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002747
Bram Moolenaar4770d092006-01-12 23:22:24 +00002748 case SN_WORDS:
2749 res = read_words_section(fd, lp, len);
2750 break;
2751
2752 case SN_SUGFILE:
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002753 lp->sl_sugtime = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002754 break;
2755
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002756 case SN_NOSPLITSUGS:
2757 lp->sl_nosplitsugs = TRUE; /* <timestamp> */
2758 break;
2759
Bram Moolenaar5195e452005-08-19 20:32:47 +00002760 case SN_COMPOUND:
2761 res = read_compound(fd, lp, len);
2762 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002763
Bram Moolenaar78622822005-08-23 21:00:13 +00002764 case SN_NOBREAK:
2765 lp->sl_nobreak = TRUE;
2766 break;
2767
Bram Moolenaar5195e452005-08-19 20:32:47 +00002768 case SN_SYLLABLE:
2769 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2770 if (lp->sl_syllable == NULL)
2771 goto endFAIL;
2772 if (init_syl_tab(lp) == FAIL)
2773 goto endFAIL;
2774 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002775
Bram Moolenaar5195e452005-08-19 20:32:47 +00002776 default:
2777 /* Unsupported section. When it's required give an error
2778 * message. When it's not required skip the contents. */
2779 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002780 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002781 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002782 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002783 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002784 while (--len >= 0)
2785 if (getc(fd) < 0)
2786 goto truncerr;
2787 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002788 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002789someerror:
Bram Moolenaar5195e452005-08-19 20:32:47 +00002790 if (res == SP_FORMERROR)
2791 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002792 EMSG(_(e_format));
2793 goto endFAIL;
2794 }
2795 if (res == SP_TRUNCERROR)
2796 {
2797truncerr:
2798 EMSG(_(e_spell_trunc));
2799 goto endFAIL;
2800 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002801 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002802 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002803 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002804
Bram Moolenaar4770d092006-01-12 23:22:24 +00002805 /* <LWORDTREE> */
2806 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
2807 if (res != 0)
2808 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002809
Bram Moolenaar4770d092006-01-12 23:22:24 +00002810 /* <KWORDTREE> */
2811 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
2812 if (res != 0)
2813 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002814
Bram Moolenaar4770d092006-01-12 23:22:24 +00002815 /* <PREFIXTREE> */
2816 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
2817 lp->sl_prefixcnt);
2818 if (res != 0)
2819 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002820
Bram Moolenaarb765d632005-06-07 21:00:02 +00002821 /* For a new file link it in the list of spell files. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002822 if (old_lp == NULL && lang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002823 {
2824 lp->sl_next = first_lang;
2825 first_lang = lp;
2826 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002827
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002828 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002829
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002830endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002831 if (lang != NULL)
2832 /* truncating the name signals the error to spell_load_lang() */
2833 *lang = NUL;
2834 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002835 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002836 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002837
2838endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002839 if (fd != NULL)
2840 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002841 sourcing_name = save_sourcing_name;
2842 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002843
2844 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002845}
2846
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002847/*
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002848 * Read 2 bytes from "fd" and turn them into an int, MSB first.
2849 */
2850 static int
2851get2c(fd)
2852 FILE *fd;
2853{
2854 long n;
2855
2856 n = getc(fd);
2857 n = (n << 8) + getc(fd);
2858 return n;
2859}
2860
2861/*
2862 * Read 3 bytes from "fd" and turn them into an int, MSB first.
2863 */
2864 static int
2865get3c(fd)
2866 FILE *fd;
2867{
2868 long n;
2869
2870 n = getc(fd);
2871 n = (n << 8) + getc(fd);
2872 n = (n << 8) + getc(fd);
2873 return n;
2874}
2875
2876/*
2877 * Read 4 bytes from "fd" and turn them into an int, MSB first.
2878 */
2879 static int
2880get4c(fd)
2881 FILE *fd;
2882{
2883 long n;
2884
2885 n = getc(fd);
2886 n = (n << 8) + getc(fd);
2887 n = (n << 8) + getc(fd);
2888 n = (n << 8) + getc(fd);
2889 return n;
2890}
2891
2892/*
2893 * Read 8 bytes from "fd" and turn them into a time_t, MSB first.
2894 */
2895 static time_t
2896get8c(fd)
2897 FILE *fd;
2898{
2899 time_t n = 0;
2900 int i;
2901
2902 for (i = 0; i < 8; ++i)
2903 n = (n << 8) + getc(fd);
2904 return n;
2905}
2906
2907/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002908 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002909 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002910 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002911 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2912 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002913 */
2914 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002915read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002916 FILE *fd;
2917 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002918 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002919{
2920 int cnt = 0;
2921 int i;
2922 char_u *str;
2923
2924 /* read the length bytes, MSB first */
2925 for (i = 0; i < cnt_bytes; ++i)
2926 cnt = (cnt << 8) + getc(fd);
2927 if (cnt < 0)
2928 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002929 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002930 return NULL;
2931 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002932 *cntp = cnt;
2933 if (cnt == 0)
2934 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002935
Bram Moolenaar5195e452005-08-19 20:32:47 +00002936 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002937 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002938 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002939 return str;
2940}
2941
Bram Moolenaar7887d882005-07-01 22:33:52 +00002942/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00002943 * Read a string of length "cnt" from "fd" into allocated memory.
2944 * Returns NULL when out of memory.
2945 */
2946 static char_u *
2947read_string(fd, cnt)
2948 FILE *fd;
2949 int cnt;
2950{
2951 char_u *str;
2952 int i;
2953
2954 /* allocate memory */
2955 str = alloc((unsigned)cnt + 1);
2956 if (str != NULL)
2957 {
2958 /* Read the string. Doesn't check for truncated file. */
2959 for (i = 0; i < cnt; ++i)
2960 str[i] = getc(fd);
2961 str[i] = NUL;
2962 }
2963 return str;
2964}
2965
2966/*
2967 * Read SN_REGION: <regionname> ...
2968 * Return SP_*ERROR flags.
2969 */
2970 static int
2971read_region_section(fd, lp, len)
2972 FILE *fd;
2973 slang_T *lp;
2974 int len;
2975{
2976 int i;
2977
2978 if (len > 16)
2979 return SP_FORMERROR;
2980 for (i = 0; i < len; ++i)
2981 lp->sl_regions[i] = getc(fd); /* <regionname> */
2982 lp->sl_regions[len] = NUL;
2983 return 0;
2984}
2985
2986/*
2987 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
2988 * <folcharslen> <folchars>
2989 * Return SP_*ERROR flags.
2990 */
2991 static int
2992read_charflags_section(fd)
2993 FILE *fd;
2994{
2995 char_u *flags;
2996 char_u *fol;
2997 int flagslen, follen;
2998
2999 /* <charflagslen> <charflags> */
3000 flags = read_cnt_string(fd, 1, &flagslen);
3001 if (flagslen < 0)
3002 return flagslen;
3003
3004 /* <folcharslen> <folchars> */
3005 fol = read_cnt_string(fd, 2, &follen);
3006 if (follen < 0)
3007 {
3008 vim_free(flags);
3009 return follen;
3010 }
3011
3012 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
3013 if (flags != NULL && fol != NULL)
3014 set_spell_charflags(flags, flagslen, fol);
3015
3016 vim_free(flags);
3017 vim_free(fol);
3018
3019 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
3020 if ((flags == NULL) != (fol == NULL))
3021 return SP_FORMERROR;
3022 return 0;
3023}
3024
3025/*
3026 * Read SN_PREFCOND section.
3027 * Return SP_*ERROR flags.
3028 */
3029 static int
3030read_prefcond_section(fd, lp)
3031 FILE *fd;
3032 slang_T *lp;
3033{
3034 int cnt;
3035 int i;
3036 int n;
3037 char_u *p;
3038 char_u buf[MAXWLEN + 1];
3039
3040 /* <prefcondcnt> <prefcond> ... */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003041 cnt = get2c(fd); /* <prefcondcnt> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003042 if (cnt <= 0)
3043 return SP_FORMERROR;
3044
3045 lp->sl_prefprog = (regprog_T **)alloc_clear(
3046 (unsigned)sizeof(regprog_T *) * cnt);
3047 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003048 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003049 lp->sl_prefixcnt = cnt;
3050
3051 for (i = 0; i < cnt; ++i)
3052 {
3053 /* <prefcond> : <condlen> <condstr> */
3054 n = getc(fd); /* <condlen> */
3055 if (n < 0 || n >= MAXWLEN)
3056 return SP_FORMERROR;
3057
3058 /* When <condlen> is zero we have an empty condition. Otherwise
3059 * compile the regexp program used to check for the condition. */
3060 if (n > 0)
3061 {
3062 buf[0] = '^'; /* always match at one position only */
3063 p = buf + 1;
3064 while (n-- > 0)
3065 *p++ = getc(fd); /* <condstr> */
3066 *p = NUL;
3067 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
3068 }
3069 }
3070 return 0;
3071}
3072
3073/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003074 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
Bram Moolenaar5195e452005-08-19 20:32:47 +00003075 * Return SP_*ERROR flags.
3076 */
3077 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00003078read_rep_section(fd, gap, first)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003079 FILE *fd;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003080 garray_T *gap;
3081 short *first;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003082{
3083 int cnt;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003084 fromto_T *ftp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003085 int i;
3086
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003087 cnt = get2c(fd); /* <repcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003088 if (cnt < 0)
3089 return SP_TRUNCERROR;
3090
Bram Moolenaar5195e452005-08-19 20:32:47 +00003091 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003092 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003093
3094 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
3095 for (; gap->ga_len < cnt; ++gap->ga_len)
3096 {
3097 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
3098 ftp->ft_from = read_cnt_string(fd, 1, &i);
3099 if (i < 0)
3100 return i;
3101 if (i == 0)
3102 return SP_FORMERROR;
3103 ftp->ft_to = read_cnt_string(fd, 1, &i);
3104 if (i <= 0)
3105 {
3106 vim_free(ftp->ft_from);
3107 if (i < 0)
3108 return i;
3109 return SP_FORMERROR;
3110 }
3111 }
3112
3113 /* Fill the first-index table. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003114 for (i = 0; i < 256; ++i)
3115 first[i] = -1;
3116 for (i = 0; i < gap->ga_len; ++i)
3117 {
3118 ftp = &((fromto_T *)gap->ga_data)[i];
3119 if (first[*ftp->ft_from] == -1)
3120 first[*ftp->ft_from] = i;
3121 }
3122 return 0;
3123}
3124
3125/*
3126 * Read SN_SAL section: <salflags> <salcount> <sal> ...
3127 * Return SP_*ERROR flags.
3128 */
3129 static int
3130read_sal_section(fd, slang)
3131 FILE *fd;
3132 slang_T *slang;
3133{
3134 int i;
3135 int cnt;
3136 garray_T *gap;
3137 salitem_T *smp;
3138 int ccnt;
3139 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003140 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003141
3142 slang->sl_sofo = FALSE;
3143
3144 i = getc(fd); /* <salflags> */
3145 if (i & SAL_F0LLOWUP)
3146 slang->sl_followup = TRUE;
3147 if (i & SAL_COLLAPSE)
3148 slang->sl_collapse = TRUE;
3149 if (i & SAL_REM_ACCENTS)
3150 slang->sl_rem_accents = TRUE;
3151
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003152 cnt = get2c(fd); /* <salcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003153 if (cnt < 0)
3154 return SP_TRUNCERROR;
3155
3156 gap = &slang->sl_sal;
3157 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003158 if (ga_grow(gap, cnt + 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003159 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003160
3161 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
3162 for (; gap->ga_len < cnt; ++gap->ga_len)
3163 {
3164 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3165 ccnt = getc(fd); /* <salfromlen> */
3166 if (ccnt < 0)
3167 return SP_TRUNCERROR;
3168 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003169 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003170 smp->sm_lead = p;
3171
3172 /* Read up to the first special char into sm_lead. */
3173 for (i = 0; i < ccnt; ++i)
3174 {
3175 c = getc(fd); /* <salfrom> */
3176 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
3177 break;
3178 *p++ = c;
3179 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003180 smp->sm_leadlen = (int)(p - smp->sm_lead);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003181 *p++ = NUL;
3182
3183 /* Put (abc) chars in sm_oneof, if any. */
3184 if (c == '(')
3185 {
3186 smp->sm_oneof = p;
3187 for (++i; i < ccnt; ++i)
3188 {
3189 c = getc(fd); /* <salfrom> */
3190 if (c == ')')
3191 break;
3192 *p++ = c;
3193 }
3194 *p++ = NUL;
3195 if (++i < ccnt)
3196 c = getc(fd);
3197 }
3198 else
3199 smp->sm_oneof = NULL;
3200
3201 /* Any following chars go in sm_rules. */
3202 smp->sm_rules = p;
3203 if (i < ccnt)
3204 /* store the char we got while checking for end of sm_lead */
3205 *p++ = c;
3206 for (++i; i < ccnt; ++i)
3207 *p++ = getc(fd); /* <salfrom> */
3208 *p++ = NUL;
3209
3210 /* <saltolen> <salto> */
3211 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
3212 if (ccnt < 0)
3213 {
3214 vim_free(smp->sm_lead);
3215 return ccnt;
3216 }
3217
3218#ifdef FEAT_MBYTE
3219 if (has_mbyte)
3220 {
3221 /* convert the multi-byte strings to wide char strings */
3222 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3223 smp->sm_leadlen = mb_charlen(smp->sm_lead);
3224 if (smp->sm_oneof == NULL)
3225 smp->sm_oneof_w = NULL;
3226 else
3227 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
3228 if (smp->sm_to == NULL)
3229 smp->sm_to_w = NULL;
3230 else
3231 smp->sm_to_w = mb_str2wide(smp->sm_to);
3232 if (smp->sm_lead_w == NULL
3233 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
3234 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
3235 {
3236 vim_free(smp->sm_lead);
3237 vim_free(smp->sm_to);
3238 vim_free(smp->sm_lead_w);
3239 vim_free(smp->sm_oneof_w);
3240 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003241 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003242 }
3243 }
3244#endif
3245 }
3246
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003247 if (gap->ga_len > 0)
3248 {
3249 /* Add one extra entry to mark the end with an empty sm_lead. Avoids
3250 * that we need to check the index every time. */
3251 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3252 if ((p = alloc(1)) == NULL)
3253 return SP_OTHERERROR;
3254 p[0] = NUL;
3255 smp->sm_lead = p;
3256 smp->sm_leadlen = 0;
3257 smp->sm_oneof = NULL;
3258 smp->sm_rules = p;
3259 smp->sm_to = NULL;
3260#ifdef FEAT_MBYTE
3261 if (has_mbyte)
3262 {
3263 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3264 smp->sm_leadlen = 0;
3265 smp->sm_oneof_w = NULL;
3266 smp->sm_to_w = NULL;
3267 }
3268#endif
3269 ++gap->ga_len;
3270 }
3271
Bram Moolenaar5195e452005-08-19 20:32:47 +00003272 /* Fill the first-index table. */
3273 set_sal_first(slang);
3274
3275 return 0;
3276}
3277
3278/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003279 * Read SN_WORDS: <word> ...
3280 * Return SP_*ERROR flags.
3281 */
3282 static int
3283read_words_section(fd, lp, len)
3284 FILE *fd;
3285 slang_T *lp;
3286 int len;
3287{
3288 int done = 0;
3289 int i;
3290 char_u word[MAXWLEN];
3291
3292 while (done < len)
3293 {
3294 /* Read one word at a time. */
3295 for (i = 0; ; ++i)
3296 {
3297 word[i] = getc(fd);
3298 if (word[i] == NUL)
3299 break;
3300 if (i == MAXWLEN - 1)
3301 return SP_FORMERROR;
3302 }
3303
3304 /* Init the count to 10. */
3305 count_common_word(lp, word, -1, 10);
3306 done += i + 1;
3307 }
3308 return 0;
3309}
3310
3311/*
3312 * Add a word to the hashtable of common words.
3313 * If it's already there then the counter is increased.
3314 */
3315 static void
3316count_common_word(lp, word, len, count)
3317 slang_T *lp;
3318 char_u *word;
3319 int len; /* word length, -1 for upto NUL */
3320 int count; /* 1 to count once, 10 to init */
3321{
3322 hash_T hash;
3323 hashitem_T *hi;
3324 wordcount_T *wc;
3325 char_u buf[MAXWLEN];
3326 char_u *p;
3327
3328 if (len == -1)
3329 p = word;
3330 else
3331 {
3332 vim_strncpy(buf, word, len);
3333 p = buf;
3334 }
3335
3336 hash = hash_hash(p);
3337 hi = hash_lookup(&lp->sl_wordcount, p, hash);
3338 if (HASHITEM_EMPTY(hi))
3339 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003340 wc = (wordcount_T *)alloc((unsigned)(sizeof(wordcount_T) + STRLEN(p)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003341 if (wc == NULL)
3342 return;
3343 STRCPY(wc->wc_word, p);
3344 wc->wc_count = count;
3345 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
3346 }
3347 else
3348 {
3349 wc = HI2WC(hi);
3350 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
3351 wc->wc_count = MAXWORDCOUNT;
3352 }
3353}
3354
3355/*
3356 * Adjust the score of common words.
3357 */
3358 static int
3359score_wordcount_adj(slang, score, word, split)
3360 slang_T *slang;
3361 int score;
3362 char_u *word;
3363 int split; /* word was split, less bonus */
3364{
3365 hashitem_T *hi;
3366 wordcount_T *wc;
3367 int bonus;
3368 int newscore;
3369
3370 hi = hash_find(&slang->sl_wordcount, word);
3371 if (!HASHITEM_EMPTY(hi))
3372 {
3373 wc = HI2WC(hi);
3374 if (wc->wc_count < SCORE_THRES2)
3375 bonus = SCORE_COMMON1;
3376 else if (wc->wc_count < SCORE_THRES3)
3377 bonus = SCORE_COMMON2;
3378 else
3379 bonus = SCORE_COMMON3;
3380 if (split)
3381 newscore = score - bonus / 2;
3382 else
3383 newscore = score - bonus;
3384 if (newscore < 0)
3385 return 0;
3386 return newscore;
3387 }
3388 return score;
3389}
3390
3391/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003392 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
3393 * Return SP_*ERROR flags.
3394 */
3395 static int
3396read_sofo_section(fd, slang)
3397 FILE *fd;
3398 slang_T *slang;
3399{
3400 int cnt;
3401 char_u *from, *to;
3402 int res;
3403
3404 slang->sl_sofo = TRUE;
3405
3406 /* <sofofromlen> <sofofrom> */
3407 from = read_cnt_string(fd, 2, &cnt);
3408 if (cnt < 0)
3409 return cnt;
3410
3411 /* <sofotolen> <sofoto> */
3412 to = read_cnt_string(fd, 2, &cnt);
3413 if (cnt < 0)
3414 {
3415 vim_free(from);
3416 return cnt;
3417 }
3418
3419 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
3420 if (from != NULL && to != NULL)
3421 res = set_sofo(slang, from, to);
3422 else if (from != NULL || to != NULL)
3423 res = SP_FORMERROR; /* only one of two strings is an error */
3424 else
3425 res = 0;
3426
3427 vim_free(from);
3428 vim_free(to);
3429 return res;
3430}
3431
3432/*
3433 * Read the compound section from the .spl file:
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003434 * <compmax> <compminlen> <compsylmax> <compoptions> <compflags>
Bram Moolenaar5195e452005-08-19 20:32:47 +00003435 * Returns SP_*ERROR flags.
3436 */
3437 static int
3438read_compound(fd, slang, len)
3439 FILE *fd;
3440 slang_T *slang;
3441 int len;
3442{
3443 int todo = len;
3444 int c;
3445 int atstart;
3446 char_u *pat;
3447 char_u *pp;
3448 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003449 char_u *ap;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003450 int cnt;
3451 garray_T *gap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003452
3453 if (todo < 2)
3454 return SP_FORMERROR; /* need at least two bytes */
3455
3456 --todo;
3457 c = getc(fd); /* <compmax> */
3458 if (c < 2)
3459 c = MAXWLEN;
3460 slang->sl_compmax = c;
3461
3462 --todo;
3463 c = getc(fd); /* <compminlen> */
3464 if (c < 1)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003465 c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003466 slang->sl_compminlen = c;
3467
3468 --todo;
3469 c = getc(fd); /* <compsylmax> */
3470 if (c < 1)
3471 c = MAXWLEN;
3472 slang->sl_compsylmax = c;
3473
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003474 c = getc(fd); /* <compoptions> */
3475 if (c != 0)
3476 ungetc(c, fd); /* be backwards compatible with Vim 7.0b */
3477 else
3478 {
3479 --todo;
3480 c = getc(fd); /* only use the lower byte for now */
3481 --todo;
3482 slang->sl_compoptions = c;
3483
3484 gap = &slang->sl_comppat;
3485 c = get2c(fd); /* <comppatcount> */
3486 todo -= 2;
3487 ga_init2(gap, sizeof(char_u *), c);
3488 if (ga_grow(gap, c) == OK)
3489 while (--c >= 0)
3490 {
3491 ((char_u **)(gap->ga_data))[gap->ga_len++] =
3492 read_cnt_string(fd, 1, &cnt);
3493 /* <comppatlen> <comppattext> */
3494 if (cnt < 0)
3495 return cnt;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003496 todo -= cnt + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003497 }
3498 }
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003499 if (todo < 0)
3500 return SP_FORMERROR;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003501
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003502 /* Turn the COMPOUNDRULE items into a regexp pattern:
Bram Moolenaar5195e452005-08-19 20:32:47 +00003503 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003504 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3505 * Conversion to utf-8 may double the size. */
3506 c = todo * 2 + 7;
3507#ifdef FEAT_MBYTE
3508 if (enc_utf8)
3509 c += todo * 2;
3510#endif
3511 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003512 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003513 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003514
Bram Moolenaard12a1322005-08-21 22:08:24 +00003515 /* We also need a list of all flags that can appear at the start and one
3516 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003517 cp = alloc(todo + 1);
3518 if (cp == NULL)
3519 {
3520 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003521 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003522 }
3523 slang->sl_compstartflags = cp;
3524 *cp = NUL;
3525
Bram Moolenaard12a1322005-08-21 22:08:24 +00003526 ap = alloc(todo + 1);
3527 if (ap == NULL)
3528 {
3529 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003530 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003531 }
3532 slang->sl_compallflags = ap;
3533 *ap = NUL;
3534
Bram Moolenaar5195e452005-08-19 20:32:47 +00003535 pp = pat;
3536 *pp++ = '^';
3537 *pp++ = '\\';
3538 *pp++ = '(';
3539
3540 atstart = 1;
3541 while (todo-- > 0)
3542 {
3543 c = getc(fd); /* <compflags> */
Bram Moolenaard12a1322005-08-21 22:08:24 +00003544
3545 /* Add all flags to "sl_compallflags". */
3546 if (vim_strchr((char_u *)"+*[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00003547 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003548 {
3549 *ap++ = c;
3550 *ap = NUL;
3551 }
3552
Bram Moolenaar5195e452005-08-19 20:32:47 +00003553 if (atstart != 0)
3554 {
3555 /* At start of item: copy flags to "sl_compstartflags". For a
3556 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3557 if (c == '[')
3558 atstart = 2;
3559 else if (c == ']')
3560 atstart = 0;
3561 else
3562 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003563 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003564 {
3565 *cp++ = c;
3566 *cp = NUL;
3567 }
3568 if (atstart == 1)
3569 atstart = 0;
3570 }
3571 }
3572 if (c == '/') /* slash separates two items */
3573 {
3574 *pp++ = '\\';
3575 *pp++ = '|';
3576 atstart = 1;
3577 }
3578 else /* normal char, "[abc]" and '*' are copied as-is */
3579 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003580 if (c == '+' || c == '~')
Bram Moolenaar5195e452005-08-19 20:32:47 +00003581 *pp++ = '\\'; /* "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003582#ifdef FEAT_MBYTE
3583 if (enc_utf8)
3584 pp += mb_char2bytes(c, pp);
3585 else
3586#endif
3587 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003588 }
3589 }
3590
3591 *pp++ = '\\';
3592 *pp++ = ')';
3593 *pp++ = '$';
3594 *pp = NUL;
3595
3596 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3597 vim_free(pat);
3598 if (slang->sl_compprog == NULL)
3599 return SP_FORMERROR;
3600
3601 return 0;
3602}
3603
Bram Moolenaar6de68532005-08-24 22:08:48 +00003604/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003605 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003606 * Like strchr() but independent of locale.
3607 */
3608 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003609byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003610 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003611 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003612{
3613 char_u *p;
3614
3615 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003616 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003617 return TRUE;
3618 return FALSE;
3619}
3620
Bram Moolenaar5195e452005-08-19 20:32:47 +00003621#define SY_MAXLEN 30
3622typedef struct syl_item_S
3623{
3624 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3625 int sy_len;
3626} syl_item_T;
3627
3628/*
3629 * Truncate "slang->sl_syllable" at the first slash and put the following items
3630 * in "slang->sl_syl_items".
3631 */
3632 static int
3633init_syl_tab(slang)
3634 slang_T *slang;
3635{
3636 char_u *p;
3637 char_u *s;
3638 int l;
3639 syl_item_T *syl;
3640
3641 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3642 p = vim_strchr(slang->sl_syllable, '/');
3643 while (p != NULL)
3644 {
3645 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003646 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003647 break;
3648 s = p;
3649 p = vim_strchr(p, '/');
3650 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003651 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003652 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003653 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003654 if (l >= SY_MAXLEN)
3655 return SP_FORMERROR;
3656 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003657 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003658 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3659 + slang->sl_syl_items.ga_len++;
3660 vim_strncpy(syl->sy_chars, s, l);
3661 syl->sy_len = l;
3662 }
3663 return OK;
3664}
3665
3666/*
3667 * Count the number of syllables in "word".
3668 * When "word" contains spaces the syllables after the last space are counted.
3669 * Returns zero if syllables are not defines.
3670 */
3671 static int
3672count_syllables(slang, word)
3673 slang_T *slang;
3674 char_u *word;
3675{
3676 int cnt = 0;
3677 int skip = FALSE;
3678 char_u *p;
3679 int len;
3680 int i;
3681 syl_item_T *syl;
3682 int c;
3683
3684 if (slang->sl_syllable == NULL)
3685 return 0;
3686
3687 for (p = word; *p != NUL; p += len)
3688 {
3689 /* When running into a space reset counter. */
3690 if (*p == ' ')
3691 {
3692 len = 1;
3693 cnt = 0;
3694 continue;
3695 }
3696
3697 /* Find longest match of syllable items. */
3698 len = 0;
3699 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3700 {
3701 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3702 if (syl->sy_len > len
3703 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3704 len = syl->sy_len;
3705 }
3706 if (len != 0) /* found a match, count syllable */
3707 {
3708 ++cnt;
3709 skip = FALSE;
3710 }
3711 else
3712 {
3713 /* No recognized syllable item, at least a syllable char then? */
3714#ifdef FEAT_MBYTE
3715 c = mb_ptr2char(p);
3716 len = (*mb_ptr2len)(p);
3717#else
3718 c = *p;
3719 len = 1;
3720#endif
3721 if (vim_strchr(slang->sl_syllable, c) == NULL)
3722 skip = FALSE; /* No, search for next syllable */
3723 else if (!skip)
3724 {
3725 ++cnt; /* Yes, count it */
3726 skip = TRUE; /* don't count following syllable chars */
3727 }
3728 }
3729 }
3730 return cnt;
3731}
3732
3733/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003734 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003735 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003736 */
3737 static int
3738set_sofo(lp, from, to)
3739 slang_T *lp;
3740 char_u *from;
3741 char_u *to;
3742{
3743 int i;
3744
3745#ifdef FEAT_MBYTE
3746 garray_T *gap;
3747 char_u *s;
3748 char_u *p;
3749 int c;
3750 int *inp;
3751
3752 if (has_mbyte)
3753 {
3754 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3755 * characters. The index is the low byte of the character.
3756 * The list contains from-to pairs with a terminating NUL.
3757 * sl_sal_first[] is used for latin1 "from" characters. */
3758 gap = &lp->sl_sal;
3759 ga_init2(gap, sizeof(int *), 1);
3760 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003761 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003762 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3763 gap->ga_len = 256;
3764
3765 /* First count the number of items for each list. Temporarily use
3766 * sl_sal_first[] for this. */
3767 for (p = from, s = to; *p != NUL && *s != NUL; )
3768 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003769 c = mb_cptr2char_adv(&p);
3770 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003771 if (c >= 256)
3772 ++lp->sl_sal_first[c & 0xff];
3773 }
3774 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003775 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003776
3777 /* Allocate the lists. */
3778 for (i = 0; i < 256; ++i)
3779 if (lp->sl_sal_first[i] > 0)
3780 {
3781 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3782 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003783 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003784 ((int **)gap->ga_data)[i] = (int *)p;
3785 *(int *)p = 0;
3786 }
3787
3788 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3789 * list. */
3790 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3791 for (p = from, s = to; *p != NUL && *s != NUL; )
3792 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003793 c = mb_cptr2char_adv(&p);
3794 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003795 if (c >= 256)
3796 {
3797 /* Append the from-to chars at the end of the list with
3798 * the low byte. */
3799 inp = ((int **)gap->ga_data)[c & 0xff];
3800 while (*inp != 0)
3801 ++inp;
3802 *inp++ = c; /* from char */
3803 *inp++ = i; /* to char */
3804 *inp++ = NUL; /* NUL at the end */
3805 }
3806 else
3807 /* mapping byte to char is done in sl_sal_first[] */
3808 lp->sl_sal_first[c] = i;
3809 }
3810 }
3811 else
3812#endif
3813 {
3814 /* mapping bytes to bytes is done in sl_sal_first[] */
3815 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003816 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003817
3818 for (i = 0; to[i] != NUL; ++i)
3819 lp->sl_sal_first[from[i]] = to[i];
3820 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3821 }
3822
Bram Moolenaar5195e452005-08-19 20:32:47 +00003823 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003824}
3825
3826/*
3827 * Fill the first-index table for "lp".
3828 */
3829 static void
3830set_sal_first(lp)
3831 slang_T *lp;
3832{
3833 salfirst_T *sfirst;
3834 int i;
3835 salitem_T *smp;
3836 int c;
3837 garray_T *gap = &lp->sl_sal;
3838
3839 sfirst = lp->sl_sal_first;
3840 for (i = 0; i < 256; ++i)
3841 sfirst[i] = -1;
3842 smp = (salitem_T *)gap->ga_data;
3843 for (i = 0; i < gap->ga_len; ++i)
3844 {
3845#ifdef FEAT_MBYTE
3846 if (has_mbyte)
3847 /* Use the lowest byte of the first character. For latin1 it's
3848 * the character, for other encodings it should differ for most
3849 * characters. */
3850 c = *smp[i].sm_lead_w & 0xff;
3851 else
3852#endif
3853 c = *smp[i].sm_lead;
3854 if (sfirst[c] == -1)
3855 {
3856 sfirst[c] = i;
3857#ifdef FEAT_MBYTE
3858 if (has_mbyte)
3859 {
3860 int n;
3861
3862 /* Make sure all entries with this byte are following each
3863 * other. Move the ones that are in the wrong position. Do
3864 * keep the same ordering! */
3865 while (i + 1 < gap->ga_len
3866 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3867 /* Skip over entry with same index byte. */
3868 ++i;
3869
3870 for (n = 1; i + n < gap->ga_len; ++n)
3871 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3872 {
3873 salitem_T tsal;
3874
3875 /* Move entry with same index byte after the entries
3876 * we already found. */
3877 ++i;
3878 --n;
3879 tsal = smp[i + n];
3880 mch_memmove(smp + i + 1, smp + i,
3881 sizeof(salitem_T) * n);
3882 smp[i] = tsal;
3883 }
3884 }
3885#endif
3886 }
3887 }
3888}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003889
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003890#ifdef FEAT_MBYTE
3891/*
3892 * Turn a multi-byte string into a wide character string.
3893 * Return it in allocated memory (NULL for out-of-memory)
3894 */
3895 static int *
3896mb_str2wide(s)
3897 char_u *s;
3898{
3899 int *res;
3900 char_u *p;
3901 int i = 0;
3902
3903 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3904 if (res != NULL)
3905 {
3906 for (p = s; *p != NUL; )
3907 res[i++] = mb_ptr2char_adv(&p);
3908 res[i] = NUL;
3909 }
3910 return res;
3911}
3912#endif
3913
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003914/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003915 * Read a tree from the .spl or .sug file.
3916 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
3917 * This is skipped when the tree has zero length.
3918 * Returns zero when OK, SP_ value for an error.
3919 */
3920 static int
3921spell_read_tree(fd, bytsp, idxsp, prefixtree, prefixcnt)
3922 FILE *fd;
3923 char_u **bytsp;
3924 idx_T **idxsp;
3925 int prefixtree; /* TRUE for the prefix tree */
3926 int prefixcnt; /* when "prefixtree" is TRUE: prefix count */
3927{
3928 int len;
3929 int idx;
3930 char_u *bp;
3931 idx_T *ip;
3932
3933 /* The tree size was computed when writing the file, so that we can
3934 * allocate it as one long block. <nodecount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003935 len = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003936 if (len < 0)
3937 return SP_TRUNCERROR;
3938 if (len > 0)
3939 {
3940 /* Allocate the byte array. */
3941 bp = lalloc((long_u)len, TRUE);
3942 if (bp == NULL)
3943 return SP_OTHERERROR;
3944 *bytsp = bp;
3945
3946 /* Allocate the index array. */
3947 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
3948 if (ip == NULL)
3949 return SP_OTHERERROR;
3950 *idxsp = ip;
3951
3952 /* Recursively read the tree and store it in the array. */
3953 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
3954 if (idx < 0)
3955 return idx;
3956 }
3957 return 0;
3958}
3959
3960/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003961 * Read one row of siblings from the spell file and store it in the byte array
3962 * "byts" and index array "idxs". Recursively read the children.
3963 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003964 * NOTE: The code here must match put_node()!
Bram Moolenaar51485f02005-06-04 21:55:20 +00003965 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003966 * Returns the index (>= 0) following the siblings.
3967 * Returns SP_TRUNCERROR if the file is shorter than expected.
3968 * Returns SP_FORMERROR if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003969 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003970 static idx_T
Bram Moolenaar4770d092006-01-12 23:22:24 +00003971read_tree_node(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003972 FILE *fd;
3973 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003974 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003975 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003976 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003977 int prefixtree; /* TRUE for reading PREFIXTREE */
3978 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003979{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003980 int len;
3981 int i;
3982 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003983 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003984 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003985 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003986#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003987
Bram Moolenaar51485f02005-06-04 21:55:20 +00003988 len = getc(fd); /* <siblingcount> */
3989 if (len <= 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003990 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003991
3992 if (startidx + len >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003993 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003994 byts[idx++] = len;
3995
3996 /* Read the byte values, flag/region bytes and shared indexes. */
3997 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003998 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003999 c = getc(fd); /* <byte> */
4000 if (c < 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004001 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004002 if (c <= BY_SPECIAL)
4003 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004004 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004005 {
4006 /* No flags, all regions. */
4007 idxs[idx] = 0;
4008 c = 0;
4009 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004010 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004011 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004012 if (prefixtree)
4013 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004014 /* Read the optional pflags byte, the prefix ID and the
4015 * condition nr. In idxs[] store the prefix ID in the low
4016 * byte, the condition index shifted up 8 bits, the flags
4017 * shifted up 24 bits. */
4018 if (c == BY_FLAGS)
4019 c = getc(fd) << 24; /* <pflags> */
4020 else
4021 c = 0;
4022
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004023 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004024
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004025 n = get2c(fd); /* <prefcondnr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004026 if (n >= maxprefcondnr)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004027 return SP_FORMERROR;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004028 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004029 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004030 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004031 {
4032 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004033 * idxs[] the flags go in the low two bytes, region above
4034 * that and prefix ID above the region. */
4035 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004036 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004037 if (c2 == BY_FLAGS2)
4038 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004039 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004040 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004041 if (c & WF_AFX)
4042 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004043 }
4044
Bram Moolenaar51485f02005-06-04 21:55:20 +00004045 idxs[idx] = c;
4046 c = 0;
4047 }
4048 else /* c == BY_INDEX */
4049 {
4050 /* <nodeidx> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004051 n = get3c(fd);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004052 if (n < 0 || n >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004053 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004054 idxs[idx] = n + SHARED_MASK;
4055 c = getc(fd); /* <xbyte> */
4056 }
4057 }
4058 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004059 }
4060
Bram Moolenaar51485f02005-06-04 21:55:20 +00004061 /* Recursively read the children for non-shared siblings.
4062 * Skip the end-of-word ones (zero byte value) and the shared ones (and
4063 * remove SHARED_MASK) */
4064 for (i = 1; i <= len; ++i)
4065 if (byts[startidx + i] != 0)
4066 {
4067 if (idxs[startidx + i] & SHARED_MASK)
4068 idxs[startidx + i] &= ~SHARED_MASK;
4069 else
4070 {
4071 idxs[startidx + i] = idx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004072 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004073 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004074 if (idx < 0)
4075 break;
4076 }
4077 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004078
Bram Moolenaar51485f02005-06-04 21:55:20 +00004079 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004080}
4081
4082/*
4083 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004084 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004085 */
4086 char_u *
4087did_set_spelllang(buf)
4088 buf_T *buf;
4089{
4090 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004091 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004092 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00004093 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004094 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004095 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004096 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004097 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004098 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004099 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004100 int len;
4101 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004102 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004103 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004104 char_u *use_region = NULL;
4105 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004106 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004107 int i, j;
4108 langp_T *lp, *lp2;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004109
4110 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004111 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004112
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004113 /* loop over comma separated language names. */
4114 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004115 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004116 /* Get one language name. */
4117 copy_option_part(&splp, lang, MAXWLEN, ",");
4118
Bram Moolenaar5482f332005-04-17 20:18:43 +00004119 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004120 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004121
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004122 /* If the name ends in ".spl" use it as the name of the spell file.
4123 * If there is a region name let "region" point to it and remove it
4124 * from the name. */
4125 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
4126 {
4127 filename = TRUE;
4128
Bram Moolenaarb6356332005-07-18 21:40:44 +00004129 /* Locate a region and remove it from the file name. */
4130 p = vim_strchr(gettail(lang), '_');
4131 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
4132 && !ASCII_ISALPHA(p[3]))
4133 {
4134 vim_strncpy(region_cp, p + 1, 2);
4135 mch_memmove(p, p + 3, len - (p - lang) - 2);
4136 len -= 3;
4137 region = region_cp;
4138 }
4139 else
4140 dont_use_region = TRUE;
4141
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004142 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004143 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4144 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004145 break;
4146 }
4147 else
4148 {
4149 filename = FALSE;
4150 if (len > 3 && lang[len - 3] == '_')
4151 {
4152 region = lang + len - 2;
4153 len -= 3;
4154 lang[len] = NUL;
4155 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004156 else
4157 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004158
4159 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004160 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4161 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004162 break;
4163 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004164
Bram Moolenaarb6356332005-07-18 21:40:44 +00004165 if (region != NULL)
4166 {
4167 /* If the region differs from what was used before then don't
4168 * use it for 'spellfile'. */
4169 if (use_region != NULL && STRCMP(region, use_region) != 0)
4170 dont_use_region = TRUE;
4171 use_region = region;
4172 }
4173
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004174 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004175 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004176 {
4177 if (filename)
4178 (void)spell_load_file(lang, lang, NULL, FALSE);
4179 else
4180 spell_load_lang(lang);
4181 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004182
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004183 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004184 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004185 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004186 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4187 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
4188 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004189 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004190 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004191 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004192 {
4193 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004194 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004195 if (c == REGION_ALL)
4196 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004197 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004198 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004199 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004200 /* This addition file is for other regions. */
4201 region_mask = 0;
4202 }
4203 else
4204 /* This is probably an error. Give a warning and
4205 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004206 smsg((char_u *)
4207 _("Warning: region %s not supported"),
4208 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004209 }
4210 else
4211 region_mask = 1 << c;
4212 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004213
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004214 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004215 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004216 if (ga_grow(&ga, 1) == FAIL)
4217 {
4218 ga_clear(&ga);
4219 return e_outofmem;
4220 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004221 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004222 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4223 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004224 use_midword(slang, buf);
4225 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004226 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004227 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004228 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004229 }
4230
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004231 /* round 0: load int_wordlist, if possible.
4232 * round 1: load first name in 'spellfile'.
4233 * round 2: load second name in 'spellfile.
4234 * etc. */
4235 spf = curbuf->b_p_spf;
4236 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004237 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004238 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004239 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004240 /* Internal wordlist, if there is one. */
4241 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004242 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004243 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004244 }
4245 else
4246 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004247 /* One entry in 'spellfile'. */
4248 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
4249 STRCAT(spf_name, ".spl");
4250
4251 /* If it was already found above then skip it. */
4252 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004253 {
4254 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
4255 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004256 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004257 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004258 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004259 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004260 }
4261
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004262 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004263 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4264 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004265 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004266 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004267 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004268 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004269 * region name, the region is ignored otherwise. for int_wordlist
4270 * use an arbitrary name. */
4271 if (round == 0)
4272 STRCPY(lang, "internal wordlist");
4273 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00004274 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004275 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004276 p = vim_strchr(lang, '.');
4277 if (p != NULL)
4278 *p = NUL; /* truncate at ".encoding.add" */
4279 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004280 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004281
4282 /* If one of the languages has NOBREAK we assume the addition
4283 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004284 if (slang != NULL && nobreak)
4285 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004286 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004287 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004288 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004289 region_mask = REGION_ALL;
4290 if (use_region != NULL && !dont_use_region)
4291 {
4292 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004293 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004294 if (c != REGION_ALL)
4295 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004296 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004297 /* This spell file is for other regions. */
4298 region_mask = 0;
4299 }
4300
4301 if (region_mask != 0)
4302 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004303 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
4304 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
4305 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004306 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4307 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004308 use_midword(slang, buf);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004309 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004310 }
4311 }
4312
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004313 /* Everything is fine, store the new b_langp value. */
4314 ga_clear(&buf->b_langp);
4315 buf->b_langp = ga;
4316
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004317 /* For each language figure out what language to use for sound folding and
4318 * REP items. If the language doesn't support it itself use another one
4319 * with the same name. E.g. for "en-math" use "en". */
4320 for (i = 0; i < ga.ga_len; ++i)
4321 {
4322 lp = LANGP_ENTRY(ga, i);
4323
4324 /* sound folding */
4325 if (lp->lp_slang->sl_sal.ga_len > 0)
4326 /* language does sound folding itself */
4327 lp->lp_sallang = lp->lp_slang;
4328 else
4329 /* find first similar language that does sound folding */
4330 for (j = 0; j < ga.ga_len; ++j)
4331 {
4332 lp2 = LANGP_ENTRY(ga, j);
4333 if (lp2->lp_slang->sl_sal.ga_len > 0
4334 && STRNCMP(lp->lp_slang->sl_name,
4335 lp2->lp_slang->sl_name, 2) == 0)
4336 {
4337 lp->lp_sallang = lp2->lp_slang;
4338 break;
4339 }
4340 }
4341
4342 /* REP items */
4343 if (lp->lp_slang->sl_rep.ga_len > 0)
4344 /* language has REP items itself */
4345 lp->lp_replang = lp->lp_slang;
4346 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004347 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004348 for (j = 0; j < ga.ga_len; ++j)
4349 {
4350 lp2 = LANGP_ENTRY(ga, j);
4351 if (lp2->lp_slang->sl_rep.ga_len > 0
4352 && STRNCMP(lp->lp_slang->sl_name,
4353 lp2->lp_slang->sl_name, 2) == 0)
4354 {
4355 lp->lp_replang = lp2->lp_slang;
4356 break;
4357 }
4358 }
4359 }
4360
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004361 return NULL;
4362}
4363
4364/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004365 * Clear the midword characters for buffer "buf".
4366 */
4367 static void
4368clear_midword(buf)
4369 buf_T *buf;
4370{
4371 vim_memset(buf->b_spell_ismw, 0, 256);
4372#ifdef FEAT_MBYTE
4373 vim_free(buf->b_spell_ismw_mb);
4374 buf->b_spell_ismw_mb = NULL;
4375#endif
4376}
4377
4378/*
4379 * Use the "sl_midword" field of language "lp" for buffer "buf".
4380 * They add up to any currently used midword characters.
4381 */
4382 static void
4383use_midword(lp, buf)
4384 slang_T *lp;
4385 buf_T *buf;
4386{
4387 char_u *p;
4388
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004389 if (lp->sl_midword == NULL) /* there aren't any */
4390 return;
4391
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004392 for (p = lp->sl_midword; *p != NUL; )
4393#ifdef FEAT_MBYTE
4394 if (has_mbyte)
4395 {
4396 int c, l, n;
4397 char_u *bp;
4398
4399 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004400 l = (*mb_ptr2len)(p);
4401 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004402 buf->b_spell_ismw[c] = TRUE;
4403 else if (buf->b_spell_ismw_mb == NULL)
4404 /* First multi-byte char in "b_spell_ismw_mb". */
4405 buf->b_spell_ismw_mb = vim_strnsave(p, l);
4406 else
4407 {
4408 /* Append multi-byte chars to "b_spell_ismw_mb". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004409 n = (int)STRLEN(buf->b_spell_ismw_mb);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004410 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
4411 if (bp != NULL)
4412 {
4413 vim_free(buf->b_spell_ismw_mb);
4414 buf->b_spell_ismw_mb = bp;
4415 vim_strncpy(bp + n, p, l);
4416 }
4417 }
4418 p += l;
4419 }
4420 else
4421#endif
4422 buf->b_spell_ismw[*p++] = TRUE;
4423}
4424
4425/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004426 * Find the region "region[2]" in "rp" (points to "sl_regions").
4427 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00004428 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004429 */
4430 static int
4431find_region(rp, region)
4432 char_u *rp;
4433 char_u *region;
4434{
4435 int i;
4436
4437 for (i = 0; ; i += 2)
4438 {
4439 if (rp[i] == NUL)
4440 return REGION_ALL;
4441 if (rp[i] == region[0] && rp[i + 1] == region[1])
4442 break;
4443 }
4444 return i / 2;
4445}
4446
4447/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004448 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004449 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00004450 * Word WF_ONECAP
4451 * W WORD WF_ALLCAP
4452 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004453 */
4454 static int
4455captype(word, end)
4456 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004457 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004458{
4459 char_u *p;
4460 int c;
4461 int firstcap;
4462 int allcap;
4463 int past_second = FALSE; /* past second word char */
4464
4465 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004466 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004467 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004468 return 0; /* only non-word characters, illegal word */
4469#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00004470 if (has_mbyte)
4471 c = mb_ptr2char_adv(&p);
4472 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004473#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00004474 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004475 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004476
4477 /*
4478 * Need to check all letters to find a word with mixed upper/lower.
4479 * But a word with an upper char only at start is a ONECAP.
4480 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004481 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004482 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004483 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004484 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004485 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004486 {
4487 /* UUl -> KEEPCAP */
4488 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004489 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004490 allcap = FALSE;
4491 }
4492 else if (!allcap)
4493 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004494 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004495 past_second = TRUE;
4496 }
4497
4498 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004499 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004500 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004501 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004502 return 0;
4503}
4504
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004505/*
4506 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
4507 * capital. So that make_case_word() can turn WOrd into Word.
4508 * Add ALLCAP for "WOrD".
4509 */
4510 static int
4511badword_captype(word, end)
4512 char_u *word;
4513 char_u *end;
4514{
4515 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004516 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004517 int l, u;
4518 int first;
4519 char_u *p;
4520
4521 if (flags & WF_KEEPCAP)
4522 {
4523 /* Count the number of UPPER and lower case letters. */
4524 l = u = 0;
4525 first = FALSE;
4526 for (p = word; p < end; mb_ptr_adv(p))
4527 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004528 c = PTR2CHAR(p);
4529 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004530 {
4531 ++u;
4532 if (p == word)
4533 first = TRUE;
4534 }
4535 else
4536 ++l;
4537 }
4538
4539 /* If there are more UPPER than lower case letters suggest an
4540 * ALLCAP word. Otherwise, if the first letter is UPPER then
4541 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4542 * require three upper case letters. */
4543 if (u > l && u > 2)
4544 flags |= WF_ALLCAP;
4545 else if (first)
4546 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004547
4548 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
4549 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004550 }
4551 return flags;
4552}
4553
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004554# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
4555/*
4556 * Free all languages.
4557 */
4558 void
4559spell_free_all()
4560{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004561 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004562 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004563 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004564
4565 /* Go through all buffers and handle 'spelllang'. */
4566 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4567 ga_clear(&buf->b_langp);
4568
4569 while (first_lang != NULL)
4570 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004571 slang = first_lang;
4572 first_lang = slang->sl_next;
4573 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004574 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004575
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004576 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004577 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004578 /* Delete the internal wordlist and its .spl file */
4579 mch_remove(int_wordlist);
4580 int_wordlist_spl(fname);
4581 mch_remove(fname);
4582 vim_free(int_wordlist);
4583 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004584 }
4585
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004586 init_spell_chartab();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004587
4588 vim_free(repl_to);
4589 repl_to = NULL;
4590 vim_free(repl_from);
4591 repl_from = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004592}
4593# endif
4594
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004595# if defined(FEAT_MBYTE) || defined(PROTO)
4596/*
4597 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004598 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004599 */
4600 void
4601spell_reload()
4602{
4603 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004604 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004605
Bram Moolenaarea408852005-06-25 22:49:46 +00004606 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004607 init_spell_chartab();
4608
4609 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004610 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004611
4612 /* Go through all buffers and handle 'spelllang'. */
4613 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4614 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004615 /* Only load the wordlists when 'spelllang' is set and there is a
4616 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004617 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004618 {
4619 FOR_ALL_WINDOWS(wp)
4620 if (wp->w_buffer == buf && wp->w_p_spell)
4621 {
4622 (void)did_set_spelllang(buf);
4623# ifdef FEAT_WINDOWS
4624 break;
4625# endif
4626 }
4627 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004628 }
4629}
4630# endif
4631
Bram Moolenaarb765d632005-06-07 21:00:02 +00004632/*
4633 * Reload the spell file "fname" if it's loaded.
4634 */
4635 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004636spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004637 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004638 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004639{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004640 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004641 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004642
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004643 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004644 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004645 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004646 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004647 slang_clear(slang);
4648 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004649 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004650 slang_clear(slang);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004651 redraw_all_later(SOME_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004652 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004653 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004654 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004655
4656 /* When "zg" was used and the file wasn't loaded yet, should redo
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004657 * 'spelllang' to load it now. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004658 if (added_word && !didit)
4659 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004660}
4661
4662
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004663/*
4664 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004665 */
4666
Bram Moolenaar51485f02005-06-04 21:55:20 +00004667#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004668 and .dic file. */
4669/*
4670 * Main structure to store the contents of a ".aff" file.
4671 */
4672typedef struct afffile_S
4673{
4674 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004675 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaar371baa92005-12-29 22:43:53 +00004676 unsigned af_rare; /* RARE ID for rare word */
4677 unsigned af_keepcase; /* KEEPCASE ID for keep-case word */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004678 unsigned af_bad; /* BAD ID for banned word */
4679 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004680 unsigned af_circumfix; /* CIRCUMFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004681 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004682 unsigned af_comproot; /* COMPOUNDROOT ID */
4683 unsigned af_compforbid; /* COMPOUNDFORBIDFLAG ID */
4684 unsigned af_comppermit; /* COMPOUNDPERMITFLAG ID */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004685 unsigned af_nosuggest; /* NOSUGGEST ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004686 int af_pfxpostpone; /* postpone prefixes without chop string and
4687 without flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004688 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4689 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004690 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004691} afffile_T;
4692
Bram Moolenaar6de68532005-08-24 22:08:48 +00004693#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004694#define AFT_LONG 1 /* flags are two characters */
4695#define AFT_CAPLONG 2 /* flags are one or two characters */
4696#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004697
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004698typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004699/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4700struct affentry_S
4701{
4702 affentry_T *ae_next; /* next affix with same name/number */
4703 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4704 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004705 char_u *ae_flags; /* flags on the affix (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004706 char_u *ae_cond; /* condition (NULL for ".") */
4707 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004708 char ae_compforbid; /* COMPOUNDFORBIDFLAG found */
4709 char ae_comppermit; /* COMPOUNDPERMITFLAG found */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004710};
4711
Bram Moolenaar6de68532005-08-24 22:08:48 +00004712#ifdef FEAT_MBYTE
4713# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4714#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004715# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004716#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004717
Bram Moolenaar51485f02005-06-04 21:55:20 +00004718/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4719typedef struct affheader_S
4720{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004721 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4722 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4723 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004724 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004725 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004726 affentry_T *ah_first; /* first affix entry */
4727} affheader_T;
4728
4729#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4730
Bram Moolenaar6de68532005-08-24 22:08:48 +00004731/* Flag used in compound items. */
4732typedef struct compitem_S
4733{
4734 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4735 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4736 int ci_newID; /* affix ID after renumbering. */
4737} compitem_T;
4738
4739#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4740
Bram Moolenaar51485f02005-06-04 21:55:20 +00004741/*
4742 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004743 * the need to keep track of each allocated thing, everything is freed all at
4744 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004745 */
4746#define SBLOCKSIZE 16000 /* size of sb_data */
4747typedef struct sblock_S sblock_T;
4748struct sblock_S
4749{
4750 sblock_T *sb_next; /* next block in list */
4751 int sb_used; /* nr of bytes already in use */
4752 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004753};
4754
4755/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004756 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004757 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004758typedef struct wordnode_S wordnode_T;
4759struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004760{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004761 union /* shared to save space */
4762 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004763 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004764 int index; /* index in written nodes (valid after first
4765 round) */
4766 } wn_u1;
4767 union /* shared to save space */
4768 {
4769 wordnode_T *next; /* next node with same hash key */
4770 wordnode_T *wnode; /* parent node that will write this node */
4771 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004772 wordnode_T *wn_child; /* child (next byte in word) */
4773 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4774 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004775 int wn_refs; /* Nr. of references to this node. Only
4776 relevant for first node in a list of
4777 siblings, in following siblings it is
4778 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004779 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004780
4781 /* Info for when "wn_byte" is NUL.
4782 * In PREFIXTREE "wn_region" is used for the prefcondnr.
4783 * In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
4784 * "wn_region" the LSW of the wordnr. */
4785 char_u wn_affixID; /* supported/required prefix ID or 0 */
4786 short_u wn_flags; /* WF_ flags */
4787 short wn_region; /* region mask */
4788
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004789#ifdef SPELL_PRINTTREE
4790 int wn_nr; /* sequence nr for printing */
4791#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004792};
4793
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004794#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4795
Bram Moolenaar51485f02005-06-04 21:55:20 +00004796#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004797
Bram Moolenaar51485f02005-06-04 21:55:20 +00004798/*
4799 * Info used while reading the spell files.
4800 */
4801typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004802{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004803 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004804 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004805
Bram Moolenaar51485f02005-06-04 21:55:20 +00004806 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004807 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004808
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004809 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004810
Bram Moolenaar4770d092006-01-12 23:22:24 +00004811 long si_sugtree; /* creating the soundfolding trie */
4812
Bram Moolenaar51485f02005-06-04 21:55:20 +00004813 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004814 long si_blocks_cnt; /* memory blocks allocated */
4815 long si_compress_cnt; /* words to add before lowering
4816 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004817 wordnode_T *si_first_free; /* List of nodes that have been freed during
4818 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004819 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004820#ifdef SPELL_PRINTTREE
4821 int si_wordnode_nr; /* sequence nr for nodes */
4822#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004823 buf_T *si_spellbuf; /* buffer used to store soundfold word table */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004824
Bram Moolenaar51485f02005-06-04 21:55:20 +00004825 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004826 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004827 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004828 int si_region; /* region mask */
4829 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004830 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004831 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004832 int si_msg_count; /* number of words added since last message */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004833 char_u *si_info; /* info text chars or NULL */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004834 int si_region_count; /* number of regions supported (1 when there
4835 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004836 char_u si_region_name[16]; /* region names; used only if
4837 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004838
4839 garray_T si_rep; /* list of fromto_T entries from REP lines */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004840 garray_T si_repsal; /* list of fromto_T entries from REPSAL lines */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004841 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004842 char_u *si_sofofr; /* SOFOFROM text */
4843 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004844 int si_nosugfile; /* NOSUGFILE item found */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004845 int si_nosplitsugs; /* NOSPLITSUGS item found */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004846 int si_followup; /* soundsalike: ? */
4847 int si_collapse; /* soundsalike: ? */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004848 hashtab_T si_commonwords; /* hashtable for common words */
4849 time_t si_sugtime; /* timestamp for .sug file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004850 int si_rem_accents; /* soundsalike: remove accents */
4851 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004852 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004853 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004854 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004855 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004856 int si_compoptions; /* COMP_ flags */
4857 garray_T si_comppat; /* CHECKCOMPOUNDPATTERN items, each stored as
4858 a string */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004859 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004860 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004861 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004862 garray_T si_prefcond; /* table with conditions for postponed
4863 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004864 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004865 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004866} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004867
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004868static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004869static void aff_process_flags __ARGS((afffile_T *affile, affentry_T *entry));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004870static int spell_info_item __ARGS((char_u *s));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004871static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
4872static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
4873static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004874static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004875static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
4876static void aff_check_number __ARGS((int spinval, int affval, char *name));
4877static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004878static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004879static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
4880static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00004881static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004882static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004883static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004884static int get_affix_flags __ARGS((afffile_T *affile, char_u *afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004885static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004886static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004887static 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 +00004888static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
4889static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
4890static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004891static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004892static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004893static 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 +00004894static 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 +00004895static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004896static int deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004897static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
4898static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
4899static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004900static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004901static void put_sugtime __ARGS((spellinfo_T *spin, FILE *fd));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004902static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00004903static void clear_node __ARGS((wordnode_T *node));
4904static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004905static void spell_make_sugfile __ARGS((spellinfo_T *spin, char_u *wfname));
4906static int sug_filltree __ARGS((spellinfo_T *spin, slang_T *slang));
4907static int sug_maketable __ARGS((spellinfo_T *spin));
4908static int sug_filltable __ARGS((spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap));
4909static int offset2bytes __ARGS((int nr, char_u *buf));
4910static int bytes2offset __ARGS((char_u **pp));
4911static void sug_write __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004912static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004913static void spell_message __ARGS((spellinfo_T *spin, char_u *str));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004914static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004915
Bram Moolenaar53805d12005-08-01 07:08:33 +00004916/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
4917 * but it must be negative to indicate the prefix tree to tree_add_word().
4918 * Use a negative number with the lower 8 bits zero. */
4919#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004920
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004921/* flags for "condit" argument of store_aff_word() */
4922#define CONDIT_COMB 1 /* affix must combine */
4923#define CONDIT_CFIX 2 /* affix must have CIRCUMFIX flag */
4924#define CONDIT_SUF 4 /* add a suffix for matching flags */
4925#define CONDIT_AFF 8 /* word already has an affix */
4926
Bram Moolenaar5195e452005-08-19 20:32:47 +00004927/*
4928 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
4929 */
4930static long compress_start = 30000; /* memory / SBLOCKSIZE */
4931static long compress_inc = 100; /* memory / SBLOCKSIZE */
4932static long compress_added = 500000; /* word count */
4933
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004934#ifdef SPELL_PRINTTREE
4935/*
4936 * For debugging the tree code: print the current tree in a (more or less)
4937 * readable format, so that we can see what happens when adding a word and/or
4938 * compressing the tree.
4939 * Based on code from Olaf Seibert.
4940 */
4941#define PRINTLINESIZE 1000
4942#define PRINTWIDTH 6
4943
4944#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
4945 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
4946
4947static char line1[PRINTLINESIZE];
4948static char line2[PRINTLINESIZE];
4949static char line3[PRINTLINESIZE];
4950
4951 static void
4952spell_clear_flags(wordnode_T *node)
4953{
4954 wordnode_T *np;
4955
4956 for (np = node; np != NULL; np = np->wn_sibling)
4957 {
4958 np->wn_u1.index = FALSE;
4959 spell_clear_flags(np->wn_child);
4960 }
4961}
4962
4963 static void
4964spell_print_node(wordnode_T *node, int depth)
4965{
4966 if (node->wn_u1.index)
4967 {
4968 /* Done this node before, print the reference. */
4969 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
4970 PRINTSOME(line2, depth, " ", 0, 0);
4971 PRINTSOME(line3, depth, " ", 0, 0);
4972 msg(line1);
4973 msg(line2);
4974 msg(line3);
4975 }
4976 else
4977 {
4978 node->wn_u1.index = TRUE;
4979
4980 if (node->wn_byte != NUL)
4981 {
4982 if (node->wn_child != NULL)
4983 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
4984 else
4985 /* Cannot happen? */
4986 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
4987 }
4988 else
4989 PRINTSOME(line1, depth, " $ ", 0, 0);
4990
4991 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
4992
4993 if (node->wn_sibling != NULL)
4994 PRINTSOME(line3, depth, " | ", 0, 0);
4995 else
4996 PRINTSOME(line3, depth, " ", 0, 0);
4997
4998 if (node->wn_byte == NUL)
4999 {
5000 msg(line1);
5001 msg(line2);
5002 msg(line3);
5003 }
5004
5005 /* do the children */
5006 if (node->wn_byte != NUL && node->wn_child != NULL)
5007 spell_print_node(node->wn_child, depth + 1);
5008
5009 /* do the siblings */
5010 if (node->wn_sibling != NULL)
5011 {
5012 /* get rid of all parent details except | */
5013 STRCPY(line1, line3);
5014 STRCPY(line2, line3);
5015 spell_print_node(node->wn_sibling, depth);
5016 }
5017 }
5018}
5019
5020 static void
5021spell_print_tree(wordnode_T *root)
5022{
5023 if (root != NULL)
5024 {
5025 /* Clear the "wn_u1.index" fields, used to remember what has been
5026 * done. */
5027 spell_clear_flags(root);
5028
5029 /* Recursively print the tree. */
5030 spell_print_node(root, 0);
5031 }
5032}
5033#endif /* SPELL_PRINTTREE */
5034
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005035/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005036 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00005037 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005038 */
5039 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005040spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005041 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005042 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005043{
5044 FILE *fd;
5045 afffile_T *aff;
5046 char_u rline[MAXLINELEN];
5047 char_u *line;
5048 char_u *pc = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005049#define MAXITEMCNT 30
Bram Moolenaar8db73182005-06-17 21:51:16 +00005050 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005051 int itemcnt;
5052 char_u *p;
5053 int lnum = 0;
5054 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005055 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005056 int aff_todo = 0;
5057 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005058 char_u *low = NULL;
5059 char_u *fol = NULL;
5060 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005061 int do_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005062 int do_repsal;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005063 int do_sal;
5064 int do_map;
5065 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005066 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005067 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005068 int compminlen = 0; /* COMPOUNDMIN value */
5069 int compsylmax = 0; /* COMPOUNDSYLMAX value */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005070 int compoptions = 0; /* COMP_ flags */
5071 int compmax = 0; /* COMPOUNDWORDMAX value */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005072 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDRULE
Bram Moolenaar6de68532005-08-24 22:08:48 +00005073 concatenated */
5074 char_u *midword = NULL; /* MIDWORD value */
5075 char_u *syllable = NULL; /* SYLLABLE value */
5076 char_u *sofofrom = NULL; /* SOFOFROM value */
5077 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005078
Bram Moolenaar51485f02005-06-04 21:55:20 +00005079 /*
5080 * Open the file.
5081 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005082 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005083 if (fd == NULL)
5084 {
5085 EMSG2(_(e_notopen), fname);
5086 return NULL;
5087 }
5088
Bram Moolenaar4770d092006-01-12 23:22:24 +00005089 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s ..."), fname);
5090 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005091
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005092 /* Only do REP lines when not done in another .aff file already. */
5093 do_rep = spin->si_rep.ga_len == 0;
5094
Bram Moolenaar4770d092006-01-12 23:22:24 +00005095 /* Only do REPSAL lines when not done in another .aff file already. */
5096 do_repsal = spin->si_repsal.ga_len == 0;
5097
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005098 /* Only do SAL lines when not done in another .aff file already. */
5099 do_sal = spin->si_sal.ga_len == 0;
5100
5101 /* Only do MAP lines when not done in another .aff file already. */
5102 do_map = spin->si_map.ga_len == 0;
5103
Bram Moolenaar51485f02005-06-04 21:55:20 +00005104 /*
5105 * Allocate and init the afffile_T structure.
5106 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005107 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005108 if (aff == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005109 {
5110 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005111 return NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005112 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005113 hash_init(&aff->af_pref);
5114 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005115 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005116
5117 /*
5118 * Read all the lines in the file one by one.
5119 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005120 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005121 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005122 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005123 ++lnum;
5124
5125 /* Skip comment lines. */
5126 if (*rline == '#')
5127 continue;
5128
5129 /* Convert from "SET" to 'encoding' when needed. */
5130 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005131#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005132 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005133 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005134 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005135 if (pc == NULL)
5136 {
5137 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5138 fname, lnum, rline);
5139 continue;
5140 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005141 line = pc;
5142 }
5143 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005144#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005145 {
5146 pc = NULL;
5147 line = rline;
5148 }
5149
5150 /* Split the line up in white separated items. Put a NUL after each
5151 * item. */
5152 itemcnt = 0;
5153 for (p = line; ; )
5154 {
5155 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
5156 ++p;
5157 if (*p == NUL)
5158 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00005159 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005160 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005161 items[itemcnt++] = p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005162 /* A few items have arbitrary text argument, don't split them. */
5163 if (itemcnt == 2 && spell_info_item(items[0]))
5164 while (*p >= ' ' || *p == TAB) /* skip until CR/NL */
5165 ++p;
5166 else
5167 while (*p > ' ') /* skip until white space or CR/NL */
5168 ++p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005169 if (*p == NUL)
5170 break;
5171 *p++ = NUL;
5172 }
5173
5174 /* Handle non-empty lines. */
5175 if (itemcnt > 0)
5176 {
5177 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
5178 && aff->af_enc == NULL)
5179 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005180#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005181 /* Setup for conversion from "ENC" to 'encoding'. */
5182 aff->af_enc = enc_canonize(items[1]);
5183 if (aff->af_enc != NULL && !spin->si_ascii
5184 && convert_setup(&spin->si_conv, aff->af_enc,
5185 p_enc) == FAIL)
5186 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
5187 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005188 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005189#else
5190 smsg((char_u *)_("Conversion in %s not supported"), fname);
5191#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005192 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005193 else if (STRCMP(items[0], "FLAG") == 0 && itemcnt == 2
5194 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005195 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005196 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005197 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005198 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005199 aff->af_flagtype = AFT_NUM;
5200 else if (STRCMP(items[1], "caplong") == 0)
5201 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005202 else
5203 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
5204 fname, lnum, items[1]);
Bram Moolenaar371baa92005-12-29 22:43:53 +00005205 if (aff->af_rare != 0
5206 || aff->af_keepcase != 0
5207 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005208 || aff->af_needaffix != 0
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005209 || aff->af_circumfix != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005210 || aff->af_needcomp != 0
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005211 || aff->af_comproot != 0
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005212 || aff->af_nosuggest != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005213 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00005214 || aff->af_suff.ht_used > 0
5215 || aff->af_pref.ht_used > 0)
5216 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
5217 fname, lnum, items[1]);
5218 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005219 else if (spell_info_item(items[0]))
5220 {
5221 p = (char_u *)getroom(spin,
5222 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
5223 + STRLEN(items[0])
5224 + STRLEN(items[1]) + 3, FALSE);
5225 if (p != NULL)
5226 {
5227 if (spin->si_info != NULL)
5228 {
5229 STRCPY(p, spin->si_info);
5230 STRCAT(p, "\n");
5231 }
5232 STRCAT(p, items[0]);
5233 STRCAT(p, " ");
5234 STRCAT(p, items[1]);
5235 spin->si_info = p;
5236 }
5237 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005238 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2
5239 && midword == NULL)
5240 {
5241 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005242 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005243 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005245 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005246 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005247 /* TODO: remove "RAR" later */
5248 else if ((STRCMP(items[0], "RAR") == 0
5249 || STRCMP(items[0], "RARE") == 0) && itemcnt == 2
5250 && aff->af_rare == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005251 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005252 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005253 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005254 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005255 /* TODO: remove "KEP" later */
5256 else if ((STRCMP(items[0], "KEP") == 0
5257 || STRCMP(items[0], "KEEPCASE") == 0) && itemcnt == 2
5258 && aff->af_keepcase == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005259 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005260 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005261 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005262 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00005263 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
5264 && aff->af_bad == 0)
5265 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005266 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
5267 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005268 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005269 else if (STRCMP(items[0], "NEEDAFFIX") == 0 && itemcnt == 2
5270 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005271 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005272 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
5273 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005274 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005275 else if (STRCMP(items[0], "CIRCUMFIX") == 0 && itemcnt == 2
5276 && aff->af_circumfix == 0)
5277 {
5278 aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
5279 fname, lnum);
5280 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005281 else if (STRCMP(items[0], "NOSUGGEST") == 0 && itemcnt == 2
5282 && aff->af_nosuggest == 0)
5283 {
5284 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
5285 fname, lnum);
5286 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005287 else if (STRCMP(items[0], "NEEDCOMPOUND") == 0 && itemcnt == 2
5288 && aff->af_needcomp == 0)
5289 {
5290 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
5291 fname, lnum);
5292 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005293 else if (STRCMP(items[0], "COMPOUNDROOT") == 0 && itemcnt == 2
5294 && aff->af_comproot == 0)
5295 {
5296 aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
5297 fname, lnum);
5298 }
5299 else if (STRCMP(items[0], "COMPOUNDFORBIDFLAG") == 0
5300 && itemcnt == 2 && aff->af_compforbid == 0)
5301 {
5302 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
5303 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005304 if (aff->af_pref.ht_used > 0)
5305 smsg((char_u *)_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
5306 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005307 }
5308 else if (STRCMP(items[0], "COMPOUNDPERMITFLAG") == 0
5309 && itemcnt == 2 && aff->af_comppermit == 0)
5310 {
5311 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
5312 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005313 if (aff->af_pref.ht_used > 0)
5314 smsg((char_u *)_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
5315 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005316 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005317 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005318 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005319 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005320 /* Turn flag "c" into COMPOUNDRULE compatible string "c+",
Bram Moolenaar6de68532005-08-24 22:08:48 +00005321 * "Na" into "Na+", "1234" into "1234+". */
5322 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005323 if (p != NULL)
5324 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005325 STRCPY(p, items[1]);
5326 STRCAT(p, "+");
5327 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005328 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005329 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005330 else if (STRCMP(items[0], "COMPOUNDRULE") == 0 && itemcnt == 2)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005331 {
5332 /* Concatenate this string to previously defined ones, using a
5333 * slash to separate them. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005334 l = (int)STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005335 if (compflags != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005336 l += (int)STRLEN(compflags) + 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005337 p = getroom(spin, l, FALSE);
5338 if (p != NULL)
5339 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005340 if (compflags != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005341 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005342 STRCPY(p, compflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005343 STRCAT(p, "/");
5344 }
5345 STRCAT(p, items[1]);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005346 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005347 }
5348 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005349 else if (STRCMP(items[0], "COMPOUNDWORDMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005350 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005351 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005352 compmax = atoi((char *)items[1]);
5353 if (compmax == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005354 smsg((char_u *)_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
Bram Moolenaar5195e452005-08-19 20:32:47 +00005355 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005356 }
5357 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005358 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005359 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005360 compminlen = atoi((char *)items[1]);
5361 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005362 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
5363 fname, lnum, items[1]);
5364 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005365 else if (STRCMP(items[0], "COMPOUNDSYLMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005366 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005367 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005368 compsylmax = atoi((char *)items[1]);
5369 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005370 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
5371 fname, lnum, items[1]);
5372 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005373 else if (STRCMP(items[0], "CHECKCOMPOUNDDUP") == 0 && itemcnt == 1)
5374 {
5375 compoptions |= COMP_CHECKDUP;
5376 }
5377 else if (STRCMP(items[0], "CHECKCOMPOUNDREP") == 0 && itemcnt == 1)
5378 {
5379 compoptions |= COMP_CHECKREP;
5380 }
5381 else if (STRCMP(items[0], "CHECKCOMPOUNDCASE") == 0 && itemcnt == 1)
5382 {
5383 compoptions |= COMP_CHECKCASE;
5384 }
5385 else if (STRCMP(items[0], "CHECKCOMPOUNDTRIPLE") == 0
5386 && itemcnt == 1)
5387 {
5388 compoptions |= COMP_CHECKTRIPLE;
5389 }
5390 else if (STRCMP(items[0], "CHECKCOMPOUNDPATTERN") == 0
5391 && itemcnt == 2)
5392 {
5393 if (atoi((char *)items[1]) == 0)
5394 smsg((char_u *)_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
5395 fname, lnum, items[1]);
5396 }
5397 else if (STRCMP(items[0], "CHECKCOMPOUNDPATTERN") == 0
5398 && itemcnt == 3)
5399 {
5400 garray_T *gap = &spin->si_comppat;
5401 int i;
5402
5403 /* Only add the couple if it isn't already there. */
5404 for (i = 0; i < gap->ga_len - 1; i += 2)
5405 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
5406 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
5407 items[2]) == 0)
5408 break;
5409 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
5410 {
5411 ((char_u **)(gap->ga_data))[gap->ga_len++]
5412 = getroom_save(spin, items[1]);
5413 ((char_u **)(gap->ga_data))[gap->ga_len++]
5414 = getroom_save(spin, items[2]);
5415 }
5416 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005417 else if (STRCMP(items[0], "SYLLABLE") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005418 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005419 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005420 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005421 }
Bram Moolenaar78622822005-08-23 21:00:13 +00005422 else if (STRCMP(items[0], "NOBREAK") == 0 && itemcnt == 1)
5423 {
5424 spin->si_nobreak = TRUE;
5425 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005426 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
5427 {
5428 spin->si_nosplitsugs = TRUE;
5429 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005430 else if (STRCMP(items[0], "NOSUGFILE") == 0 && itemcnt == 1)
5431 {
5432 spin->si_nosugfile = TRUE;
5433 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005434 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
5435 {
5436 aff->af_pfxpostpone = TRUE;
5437 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005438 else if ((STRCMP(items[0], "PFX") == 0
5439 || STRCMP(items[0], "SFX") == 0)
5440 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005441 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005442 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005443 int lasti = 4;
5444 char_u key[AH_KEY_LEN];
5445
5446 if (*items[0] == 'P')
5447 tp = &aff->af_pref;
5448 else
5449 tp = &aff->af_suff;
5450
5451 /* Myspell allows the same affix name to be used multiple
5452 * times. The affix files that do this have an undocumented
5453 * "S" flag on all but the last block, thus we check for that
5454 * and store it in ah_follows. */
5455 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
5456 hi = hash_find(tp, key);
5457 if (!HASHITEM_EMPTY(hi))
5458 {
5459 cur_aff = HI2AH(hi);
5460 if (cur_aff->ah_combine != (*items[2] == 'Y'))
5461 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
5462 fname, lnum, items[1]);
5463 if (!cur_aff->ah_follows)
5464 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
5465 fname, lnum, items[1]);
5466 }
5467 else
5468 {
5469 /* New affix letter. */
5470 cur_aff = (affheader_T *)getroom(spin,
5471 sizeof(affheader_T), TRUE);
5472 if (cur_aff == NULL)
5473 break;
5474 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
5475 fname, lnum);
5476 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
5477 break;
5478 if (cur_aff->ah_flag == aff->af_bad
Bram Moolenaar371baa92005-12-29 22:43:53 +00005479 || cur_aff->ah_flag == aff->af_rare
5480 || cur_aff->ah_flag == aff->af_keepcase
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005481 || cur_aff->ah_flag == aff->af_needaffix
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005482 || cur_aff->ah_flag == aff->af_circumfix
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005483 || cur_aff->ah_flag == aff->af_nosuggest
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005484 || cur_aff->ah_flag == aff->af_needcomp
5485 || cur_aff->ah_flag == aff->af_comproot)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005486 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 +00005487 fname, lnum, items[1]);
5488 STRCPY(cur_aff->ah_key, items[1]);
5489 hash_add(tp, cur_aff->ah_key);
5490
5491 cur_aff->ah_combine = (*items[2] == 'Y');
5492 }
5493
5494 /* Check for the "S" flag, which apparently means that another
5495 * block with the same affix name is following. */
5496 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
5497 {
5498 ++lasti;
5499 cur_aff->ah_follows = TRUE;
5500 }
5501 else
5502 cur_aff->ah_follows = FALSE;
5503
Bram Moolenaar8db73182005-06-17 21:51:16 +00005504 /* Myspell allows extra text after the item, but that might
5505 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00005506 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005507 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005508
Bram Moolenaar95529562005-08-25 21:21:38 +00005509 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005510 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
5511 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005512
Bram Moolenaar95529562005-08-25 21:21:38 +00005513 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005514 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005515 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005516 {
5517 /* Use a new number in the .spl file later, to be able
5518 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005519 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005520 cur_aff->ah_newID = ++spin->si_newprefID;
5521
5522 /* We only really use ah_newID if the prefix is
5523 * postponed. We know that only after handling all
5524 * the items. */
5525 did_postpone_prefix = FALSE;
5526 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005527 else
5528 /* Did use the ID in a previous block. */
5529 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005530 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005531
Bram Moolenaar51485f02005-06-04 21:55:20 +00005532 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005533 }
5534 else if ((STRCMP(items[0], "PFX") == 0
5535 || STRCMP(items[0], "SFX") == 0)
5536 && aff_todo > 0
5537 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005538 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005539 {
5540 affentry_T *aff_entry;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005541 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005542 int lasti = 5;
5543
Bram Moolenaar8db73182005-06-17 21:51:16 +00005544 /* Myspell allows extra text after the item, but that might
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005545 * mean mistakes go unnoticed. Require a comment-starter.
5546 * Hunspell uses a "-" item. */
5547 if (itemcnt > lasti && *items[lasti] != '#'
5548 && (STRCMP(items[lasti], "-") != 0
5549 || itemcnt != lasti + 1))
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005550 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005551
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005552 /* New item for an affix letter. */
5553 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005554 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005555 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005556 if (aff_entry == NULL)
5557 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005558
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005559 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005560 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005561 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005562 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005563 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005564
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005565 /* Recognize flags on the affix: abcd/XYZ */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005566 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
5567 if (aff_entry->ae_flags != NULL)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005568 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005569 *aff_entry->ae_flags++ = NUL;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005570 aff_process_flags(aff, aff_entry);
5571 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005572 }
5573
Bram Moolenaar51485f02005-06-04 21:55:20 +00005574 /* Don't use an affix entry with non-ASCII characters when
5575 * "spin->si_ascii" is TRUE. */
5576 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005577 || has_non_ascii(aff_entry->ae_add)))
5578 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00005579 aff_entry->ae_next = cur_aff->ah_first;
5580 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005581
5582 if (STRCMP(items[4], ".") != 0)
5583 {
5584 char_u buf[MAXLINELEN];
5585
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005586 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005587 if (*items[0] == 'P')
5588 sprintf((char *)buf, "^%s", items[4]);
5589 else
5590 sprintf((char *)buf, "%s$", items[4]);
5591 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005592 RE_MAGIC + RE_STRING + RE_STRICT);
5593 if (aff_entry->ae_prog == NULL)
5594 smsg((char_u *)_("Broken condition in %s line %d: %s"),
5595 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005596 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005597
5598 /* For postponed prefixes we need an entry in si_prefcond
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005599 * for the condition. Use an existing one if possible.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005600 * Can't be done for an affix with flags, ignoring
5601 * COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005602 if (*items[0] == 'P' && aff->af_pfxpostpone
5603 && aff_entry->ae_flags == NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005604 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005605 /* When the chop string is one lower-case letter and
5606 * the add string ends in the upper-case letter we set
5607 * the "upper" flag, clear "ae_chop" and remove the
5608 * letters from "ae_add". The condition must either
5609 * be empty or start with the same letter. */
5610 if (aff_entry->ae_chop != NULL
5611 && aff_entry->ae_add != NULL
5612#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005613 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00005614 aff_entry->ae_chop)] == NUL
5615#else
5616 && aff_entry->ae_chop[1] == NUL
5617#endif
5618 )
5619 {
5620 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005621
Bram Moolenaar53805d12005-08-01 07:08:33 +00005622 c = PTR2CHAR(aff_entry->ae_chop);
5623 c_up = SPELL_TOUPPER(c);
5624 if (c_up != c
5625 && (aff_entry->ae_cond == NULL
5626 || PTR2CHAR(aff_entry->ae_cond) == c))
5627 {
5628 p = aff_entry->ae_add
5629 + STRLEN(aff_entry->ae_add);
5630 mb_ptr_back(aff_entry->ae_add, p);
5631 if (PTR2CHAR(p) == c_up)
5632 {
5633 upper = TRUE;
5634 aff_entry->ae_chop = NULL;
5635 *p = NUL;
5636
5637 /* The condition is matched with the
5638 * actual word, thus must check for the
5639 * upper-case letter. */
5640 if (aff_entry->ae_cond != NULL)
5641 {
5642 char_u buf[MAXLINELEN];
5643#ifdef FEAT_MBYTE
5644 if (has_mbyte)
5645 {
5646 onecap_copy(items[4], buf, TRUE);
5647 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005648 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005649 }
5650 else
5651#endif
5652 *aff_entry->ae_cond = c_up;
5653 if (aff_entry->ae_cond != NULL)
5654 {
5655 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005656 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005657 vim_free(aff_entry->ae_prog);
5658 aff_entry->ae_prog = vim_regcomp(
5659 buf, RE_MAGIC + RE_STRING);
5660 }
5661 }
5662 }
5663 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005664 }
5665
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005666 if (aff_entry->ae_chop == NULL
5667 && aff_entry->ae_flags == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005668 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005669 int idx;
5670 char_u **pp;
5671 int n;
5672
Bram Moolenaar6de68532005-08-24 22:08:48 +00005673 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005674 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
5675 --idx)
5676 {
5677 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
5678 if (str_equal(p, aff_entry->ae_cond))
5679 break;
5680 }
5681 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5682 {
5683 /* Not found, add a new condition. */
5684 idx = spin->si_prefcond.ga_len++;
5685 pp = ((char_u **)spin->si_prefcond.ga_data)
5686 + idx;
5687 if (aff_entry->ae_cond == NULL)
5688 *pp = NULL;
5689 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005690 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00005691 aff_entry->ae_cond);
5692 }
5693
5694 /* Add the prefix to the prefix tree. */
5695 if (aff_entry->ae_add == NULL)
5696 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005697 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005698 p = aff_entry->ae_add;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005699
Bram Moolenaar53805d12005-08-01 07:08:33 +00005700 /* PFX_FLAGS is a negative number, so that
5701 * tree_add_word() knows this is the prefix tree. */
5702 n = PFX_FLAGS;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005703 if (!cur_aff->ah_combine)
5704 n |= WFP_NC;
5705 if (upper)
5706 n |= WFP_UP;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005707 if (aff_entry->ae_comppermit)
5708 n |= WFP_COMPPERMIT;
5709 if (aff_entry->ae_compforbid)
5710 n |= WFP_COMPFORBID;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005711 tree_add_word(spin, p, spin->si_prefroot, n,
5712 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005713 did_postpone_prefix = TRUE;
5714 }
5715
5716 /* Didn't actually use ah_newID, backup si_newprefID. */
5717 if (aff_todo == 0 && !did_postpone_prefix)
5718 {
5719 --spin->si_newprefID;
5720 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005721 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005722 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005723 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005724 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005725 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2
5726 && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005727 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005728 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005729 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005730 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2
5731 && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005732 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005733 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005734 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005735 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2
5736 && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005737 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005738 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005739 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005740 else if ((STRCMP(items[0], "REP") == 0
5741 || STRCMP(items[0], "REPSAL") == 0)
5742 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005743 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005744 /* Ignore REP/REPSAL count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005745 if (!isdigit(*items[1]))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005746 smsg((char_u *)_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005747 fname, lnum);
5748 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005749 else if ((STRCMP(items[0], "REP") == 0
5750 || STRCMP(items[0], "REPSAL") == 0)
5751 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005752 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005753 /* REP/REPSAL item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005754 /* Myspell ignores extra arguments, we require it starts with
5755 * # to detect mistakes. */
5756 if (itemcnt > 3 && items[3][0] != '#')
5757 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005758 if (items[0][3] == 'S' ? do_repsal : do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005759 {
5760 /* Replace underscore with space (can't include a space
5761 * directly). */
5762 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5763 if (*p == '_')
5764 *p = ' ';
5765 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5766 if (*p == '_')
5767 *p = ' ';
Bram Moolenaar4770d092006-01-12 23:22:24 +00005768 add_fromto(spin, items[0][3] == 'S'
5769 ? &spin->si_repsal
5770 : &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005771 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005772 }
5773 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
5774 {
5775 /* MAP item or count */
5776 if (!found_map)
5777 {
5778 /* First line contains the count. */
5779 found_map = TRUE;
5780 if (!isdigit(*items[1]))
5781 smsg((char_u *)_("Expected MAP count in %s line %d"),
5782 fname, lnum);
5783 }
5784 else if (do_map)
5785 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005786 int c;
5787
5788 /* Check that every character appears only once. */
5789 for (p = items[1]; *p != NUL; )
5790 {
5791#ifdef FEAT_MBYTE
5792 c = mb_ptr2char_adv(&p);
5793#else
5794 c = *p++;
5795#endif
5796 if ((spin->si_map.ga_len > 0
5797 && vim_strchr(spin->si_map.ga_data, c)
5798 != NULL)
5799 || vim_strchr(p, c) != NULL)
5800 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5801 fname, lnum);
5802 }
5803
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005804 /* We simply concatenate all the MAP strings, separated by
5805 * slashes. */
5806 ga_concat(&spin->si_map, items[1]);
5807 ga_append(&spin->si_map, '/');
5808 }
5809 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005810 /* Accept "SAL from to" and "SAL from to # comment". */
5811 else if (STRCMP(items[0], "SAL") == 0
5812 && (itemcnt == 3 || (itemcnt > 3 && items[3][0] == '#')))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005813 {
5814 if (do_sal)
5815 {
5816 /* SAL item (sounds-a-like)
5817 * Either one of the known keys or a from-to pair. */
5818 if (STRCMP(items[1], "followup") == 0)
5819 spin->si_followup = sal_to_bool(items[2]);
5820 else if (STRCMP(items[1], "collapse_result") == 0)
5821 spin->si_collapse = sal_to_bool(items[2]);
5822 else if (STRCMP(items[1], "remove_accents") == 0)
5823 spin->si_rem_accents = sal_to_bool(items[2]);
5824 else
5825 /* when "to" is "_" it means empty */
5826 add_fromto(spin, &spin->si_sal, items[1],
5827 STRCMP(items[2], "_") == 0 ? (char_u *)""
5828 : items[2]);
5829 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005830 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005831 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005832 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005833 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005834 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005835 }
5836 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005837 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005838 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005839 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005840 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005841 else if (STRCMP(items[0], "COMMON") == 0)
5842 {
5843 int i;
5844
5845 for (i = 1; i < itemcnt; ++i)
5846 {
5847 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
5848 items[i])))
5849 {
5850 p = vim_strsave(items[i]);
5851 if (p == NULL)
5852 break;
5853 hash_add(&spin->si_commonwords, p);
5854 }
5855 }
5856 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005857 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005858 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005859 fname, lnum, items[0]);
5860 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005861 }
5862
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005863 if (fol != NULL || low != NULL || upp != NULL)
5864 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005865 if (spin->si_clear_chartab)
5866 {
5867 /* Clear the char type tables, don't want to use any of the
5868 * currently used spell properties. */
5869 init_spell_chartab();
5870 spin->si_clear_chartab = FALSE;
5871 }
5872
Bram Moolenaar3982c542005-06-08 21:56:31 +00005873 /*
5874 * Don't write a word table for an ASCII file, so that we don't check
5875 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005876 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00005877 * mb_get_class(), the list of chars in the file will be incomplete.
5878 */
5879 if (!spin->si_ascii
5880#ifdef FEAT_MBYTE
5881 && !enc_utf8
5882#endif
5883 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005884 {
5885 if (fol == NULL || low == NULL || upp == NULL)
5886 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
5887 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00005888 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005889 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005890
5891 vim_free(fol);
5892 vim_free(low);
5893 vim_free(upp);
5894 }
5895
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005896 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005897 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005898 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005899 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
Bram Moolenaar6de68532005-08-24 22:08:48 +00005900 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005901 }
5902
Bram Moolenaar6de68532005-08-24 22:08:48 +00005903 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005904 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005905 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
5906 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005907 }
5908
Bram Moolenaar6de68532005-08-24 22:08:48 +00005909 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005910 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005911 if (syllable == NULL)
5912 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
5913 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
5914 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005915 }
5916
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005917 if (compoptions != 0)
5918 {
5919 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
5920 spin->si_compoptions |= compoptions;
5921 }
5922
Bram Moolenaar6de68532005-08-24 22:08:48 +00005923 if (compflags != NULL)
5924 process_compflags(spin, aff, compflags);
5925
5926 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005927 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005928 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005929 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005930 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005931 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005932 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005933 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005934 MSG(_("Too many posponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005935 }
5936
Bram Moolenaar6de68532005-08-24 22:08:48 +00005937 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005938 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005939 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
5940 spin->si_syllable = syllable;
5941 }
5942
5943 if (sofofrom != NULL || sofoto != NULL)
5944 {
5945 if (sofofrom == NULL || sofoto == NULL)
5946 smsg((char_u *)_("Missing SOFO%s line in %s"),
5947 sofofrom == NULL ? "FROM" : "TO", fname);
5948 else if (spin->si_sal.ga_len > 0)
5949 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005950 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005951 {
5952 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
5953 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
5954 spin->si_sofofr = sofofrom;
5955 spin->si_sofoto = sofoto;
5956 }
5957 }
5958
5959 if (midword != NULL)
5960 {
5961 aff_check_string(spin->si_midword, midword, "MIDWORD");
5962 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005963 }
5964
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005965 vim_free(pc);
5966 fclose(fd);
5967 return aff;
5968}
5969
5970/*
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005971 * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from
5972 * ae_flags to ae_comppermit and ae_compforbid.
5973 */
5974 static void
5975aff_process_flags(affile, entry)
5976 afffile_T *affile;
5977 affentry_T *entry;
5978{
5979 char_u *p;
5980 char_u *prevp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005981 unsigned flag;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005982
5983 if (entry->ae_flags != NULL
5984 && (affile->af_compforbid != 0 || affile->af_comppermit != 0))
5985 {
5986 for (p = entry->ae_flags; *p != NUL; )
5987 {
5988 prevp = p;
5989 flag = get_affitem(affile->af_flagtype, &p);
5990 if (flag == affile->af_comppermit || flag == affile->af_compforbid)
5991 {
5992 mch_memmove(prevp, p, STRLEN(p) + 1);
5993 p = prevp;
5994 if (flag == affile->af_comppermit)
5995 entry->ae_comppermit = TRUE;
5996 else
5997 entry->ae_compforbid = TRUE;
5998 }
5999 if (affile->af_flagtype == AFT_NUM && *p == ',')
6000 ++p;
6001 }
6002 if (*entry->ae_flags == NUL)
6003 entry->ae_flags = NULL; /* nothing left */
6004 }
6005}
6006
6007/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006008 * Return TRUE if "s" is the name of an info item in the affix file.
6009 */
6010 static int
6011spell_info_item(s)
6012 char_u *s;
6013{
6014 return STRCMP(s, "NAME") == 0
6015 || STRCMP(s, "HOME") == 0
6016 || STRCMP(s, "VERSION") == 0
6017 || STRCMP(s, "AUTHOR") == 0
6018 || STRCMP(s, "EMAIL") == 0
6019 || STRCMP(s, "COPYRIGHT") == 0;
6020}
6021
6022/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006023 * Turn an affix flag name into a number, according to the FLAG type.
6024 * returns zero for failure.
6025 */
6026 static unsigned
6027affitem2flag(flagtype, item, fname, lnum)
6028 int flagtype;
6029 char_u *item;
6030 char_u *fname;
6031 int lnum;
6032{
6033 unsigned res;
6034 char_u *p = item;
6035
6036 res = get_affitem(flagtype, &p);
6037 if (res == 0)
6038 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006039 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006040 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
6041 fname, lnum, item);
6042 else
6043 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
6044 fname, lnum, item);
6045 }
6046 if (*p != NUL)
6047 {
6048 smsg((char_u *)_(e_affname), fname, lnum, item);
6049 return 0;
6050 }
6051
6052 return res;
6053}
6054
6055/*
6056 * Get one affix name from "*pp" and advance the pointer.
6057 * Returns zero for an error, still advances the pointer then.
6058 */
6059 static unsigned
6060get_affitem(flagtype, pp)
6061 int flagtype;
6062 char_u **pp;
6063{
6064 int res;
6065
Bram Moolenaar95529562005-08-25 21:21:38 +00006066 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006067 {
6068 if (!VIM_ISDIGIT(**pp))
6069 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006070 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006071 return 0;
6072 }
6073 res = getdigits(pp);
6074 }
6075 else
6076 {
6077#ifdef FEAT_MBYTE
6078 res = mb_ptr2char_adv(pp);
6079#else
6080 res = *(*pp)++;
6081#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006082 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00006083 && res >= 'A' && res <= 'Z'))
6084 {
6085 if (**pp == NUL)
6086 return 0;
6087#ifdef FEAT_MBYTE
6088 res = mb_ptr2char_adv(pp) + (res << 16);
6089#else
6090 res = *(*pp)++ + (res << 16);
6091#endif
6092 }
6093 }
6094 return res;
6095}
6096
6097/*
6098 * Process the "compflags" string used in an affix file and append it to
6099 * spin->si_compflags.
6100 * The processing involves changing the affix names to ID numbers, so that
6101 * they fit in one byte.
6102 */
6103 static void
6104process_compflags(spin, aff, compflags)
6105 spellinfo_T *spin;
6106 afffile_T *aff;
6107 char_u *compflags;
6108{
6109 char_u *p;
6110 char_u *prevp;
6111 unsigned flag;
6112 compitem_T *ci;
6113 int id;
6114 int len;
6115 char_u *tp;
6116 char_u key[AH_KEY_LEN];
6117 hashitem_T *hi;
6118
6119 /* Make room for the old and the new compflags, concatenated with a / in
6120 * between. Processing it makes it shorter, but we don't know by how
6121 * much, thus allocate the maximum. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006122 len = (int)STRLEN(compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006123 if (spin->si_compflags != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006124 len += (int)STRLEN(spin->si_compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006125 p = getroom(spin, len, FALSE);
6126 if (p == NULL)
6127 return;
6128 if (spin->si_compflags != NULL)
6129 {
6130 STRCPY(p, spin->si_compflags);
6131 STRCAT(p, "/");
6132 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00006133 spin->si_compflags = p;
6134 tp = p + STRLEN(p);
6135
6136 for (p = compflags; *p != NUL; )
6137 {
6138 if (vim_strchr((char_u *)"/*+[]", *p) != NULL)
6139 /* Copy non-flag characters directly. */
6140 *tp++ = *p++;
6141 else
6142 {
6143 /* First get the flag number, also checks validity. */
6144 prevp = p;
6145 flag = get_affitem(aff->af_flagtype, &p);
6146 if (flag != 0)
6147 {
6148 /* Find the flag in the hashtable. If it was used before, use
6149 * the existing ID. Otherwise add a new entry. */
6150 vim_strncpy(key, prevp, p - prevp);
6151 hi = hash_find(&aff->af_comp, key);
6152 if (!HASHITEM_EMPTY(hi))
6153 id = HI2CI(hi)->ci_newID;
6154 else
6155 {
6156 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
6157 if (ci == NULL)
6158 break;
6159 STRCPY(ci->ci_key, key);
6160 ci->ci_flag = flag;
6161 /* Avoid using a flag ID that has a special meaning in a
6162 * regexp (also inside []). */
6163 do
6164 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006165 check_renumber(spin);
6166 id = spin->si_newcompID--;
6167 } while (vim_strchr((char_u *)"/+*[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006168 ci->ci_newID = id;
6169 hash_add(&aff->af_comp, ci->ci_key);
6170 }
6171 *tp++ = id;
6172 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006173 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006174 ++p;
6175 }
6176 }
6177
6178 *tp = NUL;
6179}
6180
6181/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006182 * Check that the new IDs for postponed affixes and compounding don't overrun
6183 * each other. We have almost 255 available, but start at 0-127 to avoid
6184 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
6185 * When that is used up an error message is given.
6186 */
6187 static void
6188check_renumber(spin)
6189 spellinfo_T *spin;
6190{
6191 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
6192 {
6193 spin->si_newprefID = 127;
6194 spin->si_newcompID = 255;
6195 }
6196}
6197
6198/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006199 * Return TRUE if flag "flag" appears in affix list "afflist".
6200 */
6201 static int
6202flag_in_afflist(flagtype, afflist, flag)
6203 int flagtype;
6204 char_u *afflist;
6205 unsigned flag;
6206{
6207 char_u *p;
6208 unsigned n;
6209
6210 switch (flagtype)
6211 {
6212 case AFT_CHAR:
6213 return vim_strchr(afflist, flag) != NULL;
6214
Bram Moolenaar95529562005-08-25 21:21:38 +00006215 case AFT_CAPLONG:
6216 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006217 for (p = afflist; *p != NUL; )
6218 {
6219#ifdef FEAT_MBYTE
6220 n = mb_ptr2char_adv(&p);
6221#else
6222 n = *p++;
6223#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006224 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00006225 && *p != NUL)
6226#ifdef FEAT_MBYTE
6227 n = mb_ptr2char_adv(&p) + (n << 16);
6228#else
6229 n = *p++ + (n << 16);
6230#endif
6231 if (n == flag)
6232 return TRUE;
6233 }
6234 break;
6235
Bram Moolenaar95529562005-08-25 21:21:38 +00006236 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006237 for (p = afflist; *p != NUL; )
6238 {
6239 n = getdigits(&p);
6240 if (n == flag)
6241 return TRUE;
6242 if (*p != NUL) /* skip over comma */
6243 ++p;
6244 }
6245 break;
6246 }
6247 return FALSE;
6248}
6249
6250/*
6251 * Give a warning when "spinval" and "affval" numbers are set and not the same.
6252 */
6253 static void
6254aff_check_number(spinval, affval, name)
6255 int spinval;
6256 int affval;
6257 char *name;
6258{
6259 if (spinval != 0 && spinval != affval)
6260 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6261}
6262
6263/*
6264 * Give a warning when "spinval" and "affval" strings are set and not the same.
6265 */
6266 static void
6267aff_check_string(spinval, affval, name)
6268 char_u *spinval;
6269 char_u *affval;
6270 char *name;
6271{
6272 if (spinval != NULL && STRCMP(spinval, affval) != 0)
6273 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6274}
6275
6276/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006277 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
6278 * NULL as equal.
6279 */
6280 static int
6281str_equal(s1, s2)
6282 char_u *s1;
6283 char_u *s2;
6284{
6285 if (s1 == NULL || s2 == NULL)
6286 return s1 == s2;
6287 return STRCMP(s1, s2) == 0;
6288}
6289
6290/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006291 * Add a from-to item to "gap". Used for REP and SAL items.
6292 * They are stored case-folded.
6293 */
6294 static void
6295add_fromto(spin, gap, from, to)
6296 spellinfo_T *spin;
6297 garray_T *gap;
6298 char_u *from;
6299 char_u *to;
6300{
6301 fromto_T *ftp;
6302 char_u word[MAXWLEN];
6303
6304 if (ga_grow(gap, 1) == OK)
6305 {
6306 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006307 (void)spell_casefold(from, (int)STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006308 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006309 (void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006310 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006311 ++gap->ga_len;
6312 }
6313}
6314
6315/*
6316 * Convert a boolean argument in a SAL line to TRUE or FALSE;
6317 */
6318 static int
6319sal_to_bool(s)
6320 char_u *s;
6321{
6322 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
6323}
6324
6325/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00006326 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6327 * When "s" is NULL FALSE is returned.
6328 */
6329 static int
6330has_non_ascii(s)
6331 char_u *s;
6332{
6333 char_u *p;
6334
6335 if (s != NULL)
6336 for (p = s; *p != NUL; ++p)
6337 if (*p >= 128)
6338 return TRUE;
6339 return FALSE;
6340}
6341
6342/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006343 * Free the structure filled by spell_read_aff().
6344 */
6345 static void
6346spell_free_aff(aff)
6347 afffile_T *aff;
6348{
6349 hashtab_T *ht;
6350 hashitem_T *hi;
6351 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006352 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006353 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006354
6355 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006356
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006357 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006358 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
6359 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006360 todo = (int)ht->ht_used;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006361 for (hi = ht->ht_array; todo > 0; ++hi)
6362 {
6363 if (!HASHITEM_EMPTY(hi))
6364 {
6365 --todo;
6366 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006367 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
6368 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006369 }
6370 }
6371 if (ht == &aff->af_suff)
6372 break;
6373 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006374
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006375 hash_clear(&aff->af_pref);
6376 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006377 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006378}
6379
6380/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006381 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006382 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006383 */
6384 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006385spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006386 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006387 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006388 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006389{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006390 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006391 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006392 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006393 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006394 char_u store_afflist[MAXWLEN];
6395 int pfxlen;
6396 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006397 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006398 char_u *pc;
6399 char_u *w;
6400 int l;
6401 hash_T hash;
6402 hashitem_T *hi;
6403 FILE *fd;
6404 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006405 int non_ascii = 0;
6406 int retval = OK;
6407 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006408 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006409 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006410
Bram Moolenaar51485f02005-06-04 21:55:20 +00006411 /*
6412 * Open the file.
6413 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006414 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006415 if (fd == NULL)
6416 {
6417 EMSG2(_(e_notopen), fname);
6418 return FAIL;
6419 }
6420
Bram Moolenaar51485f02005-06-04 21:55:20 +00006421 /* The hashtable is only used to detect duplicated words. */
6422 hash_init(&ht);
6423
Bram Moolenaar4770d092006-01-12 23:22:24 +00006424 vim_snprintf((char *)IObuff, IOSIZE,
6425 _("Reading dictionary file %s ..."), fname);
6426 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006427
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006428 /* start with a message for the first line */
6429 spin->si_msg_count = 999999;
6430
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006431 /* Read and ignore the first line: word count. */
6432 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006433 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006434 EMSG2(_("E760: No word count in %s"), fname);
6435
6436 /*
6437 * Read all the lines in the file one by one.
6438 * The words are converted to 'encoding' here, before being added to
6439 * the hashtable.
6440 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006441 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006442 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006443 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006444 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00006445 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006446 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006447
Bram Moolenaar51485f02005-06-04 21:55:20 +00006448 /* Remove CR, LF and white space from the end. White space halfway
6449 * the word is kept to allow e.g., "et al.". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006450 l = (int)STRLEN(line);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006451 while (l > 0 && line[l - 1] <= ' ')
6452 --l;
6453 if (l == 0)
6454 continue; /* empty line */
6455 line[l] = NUL;
6456
Bram Moolenaarb765d632005-06-07 21:00:02 +00006457#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006458 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006459 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006460 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006461 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006462 if (pc == NULL)
6463 {
6464 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6465 fname, lnum, line);
6466 continue;
6467 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006468 w = pc;
6469 }
6470 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006471#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006472 {
6473 pc = NULL;
6474 w = line;
6475 }
6476
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006477 /* Truncate the word at the "/", set "afflist" to what follows.
6478 * Replace "\/" by "/" and "\\" by "\". */
6479 afflist = NULL;
6480 for (p = w; *p != NUL; mb_ptr_adv(p))
6481 {
6482 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
6483 mch_memmove(p, p + 1, STRLEN(p));
6484 else if (*p == '/')
6485 {
6486 *p = NUL;
6487 afflist = p + 1;
6488 break;
6489 }
6490 }
6491
6492 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6493 if (spin->si_ascii && has_non_ascii(w))
6494 {
6495 ++non_ascii;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006496 vim_free(pc);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006497 continue;
6498 }
6499
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006500 /* This takes time, print a message every 10000 words. */
6501 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006502 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006503 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006504 vim_snprintf((char *)message, sizeof(message),
6505 _("line %6d, word %6d - %s"),
6506 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
6507 msg_start();
6508 msg_puts_long_attr(message, 0);
6509 msg_clr_eos();
6510 msg_didout = FALSE;
6511 msg_col = 0;
6512 out_flush();
6513 }
6514
Bram Moolenaar51485f02005-06-04 21:55:20 +00006515 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006516 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006517 if (dw == NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006518 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006519 retval = FAIL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006520 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006521 break;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006522 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006523
Bram Moolenaar51485f02005-06-04 21:55:20 +00006524 hash = hash_hash(dw);
6525 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006526 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006527 {
6528 if (p_verbose > 0)
6529 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006530 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006531 else if (duplicate == 0)
6532 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
6533 fname, lnum, dw);
6534 ++duplicate;
6535 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006536 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006537 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006538
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006539 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006540 store_afflist[0] = NUL;
6541 pfxlen = 0;
6542 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006543 if (afflist != NULL)
6544 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006545 /* Extract flags from the affix list. */
6546 flags |= get_affix_flags(affile, afflist);
6547
Bram Moolenaar6de68532005-08-24 22:08:48 +00006548 if (affile->af_needaffix != 0 && flag_in_afflist(
6549 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00006550 need_affix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006551
6552 if (affile->af_pfxpostpone)
6553 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006554 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006555
Bram Moolenaar5195e452005-08-19 20:32:47 +00006556 if (spin->si_compflags != NULL)
6557 /* Need to store the list of compound flags with the word.
6558 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006559 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006560 }
6561
Bram Moolenaar51485f02005-06-04 21:55:20 +00006562 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006563 if (store_word(spin, dw, flags, spin->si_region,
6564 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006565 retval = FAIL;
6566
6567 if (afflist != NULL)
6568 {
6569 /* Find all matching suffixes and add the resulting words.
6570 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006571 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006572 &affile->af_suff, &affile->af_pref,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006573 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006574 retval = FAIL;
6575
6576 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006577 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006578 &affile->af_pref, NULL,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006579 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006580 retval = FAIL;
6581 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006582
6583 vim_free(pc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006584 }
6585
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006586 if (duplicate > 0)
6587 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006588 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006589 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
6590 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006591 hash_clear(&ht);
6592
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006593 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006594 return retval;
6595}
6596
6597/*
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006598 * Check for affix flags in "afflist" that are turned into word flags.
6599 * Return WF_ flags.
6600 */
6601 static int
6602get_affix_flags(affile, afflist)
6603 afffile_T *affile;
6604 char_u *afflist;
6605{
6606 int flags = 0;
6607
6608 if (affile->af_keepcase != 0 && flag_in_afflist(
6609 affile->af_flagtype, afflist, affile->af_keepcase))
6610 flags |= WF_KEEPCAP | WF_FIXCAP;
6611 if (affile->af_rare != 0 && flag_in_afflist(
6612 affile->af_flagtype, afflist, affile->af_rare))
6613 flags |= WF_RARE;
6614 if (affile->af_bad != 0 && flag_in_afflist(
6615 affile->af_flagtype, afflist, affile->af_bad))
6616 flags |= WF_BANNED;
6617 if (affile->af_needcomp != 0 && flag_in_afflist(
6618 affile->af_flagtype, afflist, affile->af_needcomp))
6619 flags |= WF_NEEDCOMP;
6620 if (affile->af_comproot != 0 && flag_in_afflist(
6621 affile->af_flagtype, afflist, affile->af_comproot))
6622 flags |= WF_COMPROOT;
6623 if (affile->af_nosuggest != 0 && flag_in_afflist(
6624 affile->af_flagtype, afflist, affile->af_nosuggest))
6625 flags |= WF_NOSUGGEST;
6626 return flags;
6627}
6628
6629/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006630 * Get the list of prefix IDs from the affix list "afflist".
6631 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006632 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
6633 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006634 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006635 static int
6636get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006637 afffile_T *affile;
6638 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006639 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006640{
6641 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006642 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006643 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006644 int id;
6645 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006646 hashitem_T *hi;
6647
Bram Moolenaar6de68532005-08-24 22:08:48 +00006648 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006649 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006650 prevp = p;
6651 if (get_affitem(affile->af_flagtype, &p) != 0)
6652 {
6653 /* A flag is a postponed prefix flag if it appears in "af_pref"
6654 * and it's ID is not zero. */
6655 vim_strncpy(key, prevp, p - prevp);
6656 hi = hash_find(&affile->af_pref, key);
6657 if (!HASHITEM_EMPTY(hi))
6658 {
6659 id = HI2AH(hi)->ah_newID;
6660 if (id != 0)
6661 store_afflist[cnt++] = id;
6662 }
6663 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006664 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006665 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006666 }
6667
Bram Moolenaar5195e452005-08-19 20:32:47 +00006668 store_afflist[cnt] = NUL;
6669 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006670}
6671
6672/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006673 * Get the list of compound IDs from the affix list "afflist" that are used
6674 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006675 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006676 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006677 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00006678get_compflags(affile, afflist, store_afflist)
6679 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006680 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006681 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006682{
6683 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006684 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006685 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006686 char_u key[AH_KEY_LEN];
6687 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006688
Bram Moolenaar6de68532005-08-24 22:08:48 +00006689 for (p = afflist; *p != NUL; )
6690 {
6691 prevp = p;
6692 if (get_affitem(affile->af_flagtype, &p) != 0)
6693 {
6694 /* A flag is a compound flag if it appears in "af_comp". */
6695 vim_strncpy(key, prevp, p - prevp);
6696 hi = hash_find(&affile->af_comp, key);
6697 if (!HASHITEM_EMPTY(hi))
6698 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
6699 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006700 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006701 ++p;
6702 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006703
Bram Moolenaar5195e452005-08-19 20:32:47 +00006704 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006705}
6706
6707/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006708 * Apply affixes to a word and store the resulting words.
6709 * "ht" is the hashtable with affentry_T that need to be applied, either
6710 * prefixes or suffixes.
6711 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
6712 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006713 *
6714 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006715 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006716 static int
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006717store_aff_word(spin, word, afflist, affile, ht, xht, condit, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006718 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006719 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006720 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006721 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006722 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006723 hashtab_T *ht;
6724 hashtab_T *xht;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006725 int condit; /* CONDIT_SUF et al. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006726 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006727 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006728 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
6729 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006730{
6731 int todo;
6732 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006733 affheader_T *ah;
6734 affentry_T *ae;
6735 regmatch_T regmatch;
6736 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006737 int retval = OK;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006738 int i, j;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006739 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006740 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006741 char_u *use_pfxlist;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006742 int use_pfxlen;
6743 int need_affix;
6744 char_u store_afflist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006745 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006746 size_t wordlen = STRLEN(word);
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006747 int use_condit;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006748
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006749 todo = (int)ht->ht_used;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006750 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006751 {
6752 if (!HASHITEM_EMPTY(hi))
6753 {
6754 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006755 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00006756
Bram Moolenaar51485f02005-06-04 21:55:20 +00006757 /* Check that the affix combines, if required, and that the word
6758 * supports this affix. */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006759 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
6760 && flag_in_afflist(affile->af_flagtype, afflist,
6761 ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00006762 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006763 /* Loop over all affix entries with this name. */
6764 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006765 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006766 /* Check the condition. It's not logical to match case
6767 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006768 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006769 * Another requirement from Myspell is that the chop
6770 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006771 * For prefixes, when "PFXPOSTPONE" was used, only do
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006772 * prefixes with a chop string and/or flags.
6773 * When a previously added affix had CIRCUMFIX this one
6774 * must have it too, if it had not then this one must not
6775 * have one either. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006776 regmatch.regprog = ae->ae_prog;
6777 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006778 if ((xht != NULL || !affile->af_pfxpostpone
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006779 || ae->ae_chop != NULL
6780 || ae->ae_flags != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006781 && (ae->ae_chop == NULL
6782 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006783 && (ae->ae_prog == NULL
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006784 || vim_regexec(&regmatch, word, (colnr_T)0))
6785 && (((condit & CONDIT_CFIX) == 0)
6786 == ((condit & CONDIT_AFF) == 0
6787 || ae->ae_flags == NULL
6788 || !flag_in_afflist(affile->af_flagtype,
6789 ae->ae_flags, affile->af_circumfix))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006790 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006791 /* Match. Remove the chop and add the affix. */
6792 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006793 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006794 /* prefix: chop/add at the start of the word */
6795 if (ae->ae_add == NULL)
6796 *newword = NUL;
6797 else
6798 STRCPY(newword, ae->ae_add);
6799 p = word;
6800 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006801 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006802 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006803#ifdef FEAT_MBYTE
6804 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006805 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006806 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006807 for ( ; i > 0; --i)
6808 mb_ptr_adv(p);
6809 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006810 else
6811#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006812 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006813 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006814 STRCAT(newword, p);
6815 }
6816 else
6817 {
6818 /* suffix: chop/add at the end of the word */
6819 STRCPY(newword, word);
6820 if (ae->ae_chop != NULL)
6821 {
6822 /* Remove chop string. */
6823 p = newword + STRLEN(newword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006824 i = (int)MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006825 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006826 mb_ptr_back(newword, p);
6827 *p = NUL;
6828 }
6829 if (ae->ae_add != NULL)
6830 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006831 }
6832
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006833 use_flags = flags;
6834 use_pfxlist = pfxlist;
6835 use_pfxlen = pfxlen;
6836 need_affix = FALSE;
6837 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
6838 if (ae->ae_flags != NULL)
6839 {
6840 /* Extract flags from the affix list. */
6841 use_flags |= get_affix_flags(affile, ae->ae_flags);
6842
6843 if (affile->af_needaffix != 0 && flag_in_afflist(
6844 affile->af_flagtype, ae->ae_flags,
6845 affile->af_needaffix))
6846 need_affix = TRUE;
6847
6848 /* When there is a CIRCUMFIX flag the other affix
6849 * must also have it and we don't add the word
6850 * with one affix. */
6851 if (affile->af_circumfix != 0 && flag_in_afflist(
6852 affile->af_flagtype, ae->ae_flags,
6853 affile->af_circumfix))
6854 {
6855 use_condit |= CONDIT_CFIX;
6856 if ((condit & CONDIT_CFIX) == 0)
6857 need_affix = TRUE;
6858 }
6859
6860 if (affile->af_pfxpostpone
6861 || spin->si_compflags != NULL)
6862 {
6863 if (affile->af_pfxpostpone)
6864 /* Get prefix IDS from the affix list. */
6865 use_pfxlen = get_pfxlist(affile,
6866 ae->ae_flags, store_afflist);
6867 else
6868 use_pfxlen = 0;
6869 use_pfxlist = store_afflist;
6870
6871 /* Combine the prefix IDs. Avoid adding the
6872 * same ID twice. */
6873 for (i = 0; i < pfxlen; ++i)
6874 {
6875 for (j = 0; j < use_pfxlen; ++j)
6876 if (pfxlist[i] == use_pfxlist[j])
6877 break;
6878 if (j == use_pfxlen)
6879 use_pfxlist[use_pfxlen++] = pfxlist[i];
6880 }
6881
6882 if (spin->si_compflags != NULL)
6883 /* Get compound IDS from the affix list. */
6884 get_compflags(affile, ae->ae_flags,
6885 use_pfxlist + use_pfxlen);
6886
6887 /* Combine the list of compound flags.
6888 * Concatenate them to the prefix IDs list.
6889 * Avoid adding the same ID twice. */
6890 for (i = pfxlen; pfxlist[i] != NUL; ++i)
6891 {
6892 for (j = use_pfxlen;
6893 use_pfxlist[j] != NUL; ++j)
6894 if (pfxlist[i] == use_pfxlist[j])
6895 break;
6896 if (use_pfxlist[j] == NUL)
6897 {
6898 use_pfxlist[j++] = pfxlist[i];
6899 use_pfxlist[j] = NUL;
6900 }
6901 }
6902 }
6903 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00006904
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006905 /* Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006906 * use the compound flags. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006907 if (use_pfxlist != NULL && ae->ae_compforbid)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006908 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006909 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006910 use_pfxlist = pfx_pfxlist;
6911 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006912
6913 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00006914 if (spin->si_prefroot != NULL
6915 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006916 {
6917 /* ... add a flag to indicate an affix was used. */
6918 use_flags |= WF_HAS_AFF;
6919
6920 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00006921 * affixes is not allowed. But do use the
6922 * compound flags after them. */
Bram Moolenaar18144c82006-04-12 21:52:12 +00006923 if (!ah->ah_combine && use_pfxlist != NULL)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006924 use_pfxlist += use_pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006925 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006926
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006927 /* When compounding is supported and there is no
6928 * "COMPOUNDPERMITFLAG" then forbid compounding on the
6929 * side where the affix is applied. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006930 if (spin->si_compflags != NULL && !ae->ae_comppermit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006931 {
6932 if (xht != NULL)
6933 use_flags |= WF_NOCOMPAFT;
6934 else
6935 use_flags |= WF_NOCOMPBEF;
6936 }
6937
Bram Moolenaar51485f02005-06-04 21:55:20 +00006938 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006939 if (store_word(spin, newword, use_flags,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006940 spin->si_region, use_pfxlist,
6941 need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006942 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006943
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006944 /* When added a prefix or a first suffix and the affix
6945 * has flags may add a(nother) suffix. RECURSIVE! */
6946 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
6947 if (store_aff_word(spin, newword, ae->ae_flags,
6948 affile, &affile->af_suff, xht,
6949 use_condit & (xht == NULL
6950 ? ~0 : ~CONDIT_SUF),
Bram Moolenaar5195e452005-08-19 20:32:47 +00006951 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006952 retval = FAIL;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006953
6954 /* When added a suffix and combining is allowed also
6955 * try adding a prefix additionally. Both for the
6956 * word flags and for the affix flags. RECURSIVE! */
6957 if (xht != NULL && ah->ah_combine)
6958 {
6959 if (store_aff_word(spin, newword,
6960 afflist, affile,
6961 xht, NULL, use_condit,
6962 use_flags, use_pfxlist,
6963 pfxlen) == FAIL
6964 || (ae->ae_flags != NULL
6965 && store_aff_word(spin, newword,
6966 ae->ae_flags, affile,
6967 xht, NULL, use_condit,
6968 use_flags, use_pfxlist,
6969 pfxlen) == FAIL))
6970 retval = FAIL;
6971 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006972 }
6973 }
6974 }
6975 }
6976 }
6977
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006978 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006979}
6980
6981/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006982 * Read a file with a list of words.
6983 */
6984 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006985spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006986 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006987 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006988{
6989 FILE *fd;
6990 long lnum = 0;
6991 char_u rline[MAXLINELEN];
6992 char_u *line;
6993 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006994 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006995 int l;
6996 int retval = OK;
6997 int did_word = FALSE;
6998 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006999 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007000 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007001
7002 /*
7003 * Open the file.
7004 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007005 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007006 if (fd == NULL)
7007 {
7008 EMSG2(_(e_notopen), fname);
7009 return FAIL;
7010 }
7011
Bram Moolenaar4770d092006-01-12 23:22:24 +00007012 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname);
7013 spell_message(spin, IObuff);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007014
7015 /*
7016 * Read all the lines in the file one by one.
7017 */
7018 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
7019 {
7020 line_breakcheck();
7021 ++lnum;
7022
7023 /* Skip comment lines. */
7024 if (*rline == '#')
7025 continue;
7026
7027 /* Remove CR, LF and white space from the end. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007028 l = (int)STRLEN(rline);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007029 while (l > 0 && rline[l - 1] <= ' ')
7030 --l;
7031 if (l == 0)
7032 continue; /* empty or blank line */
7033 rline[l] = NUL;
7034
Bram Moolenaar9c102382006-05-03 21:26:49 +00007035 /* Convert from "/encoding={encoding}" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007036 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007037#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00007038 if (spin->si_conv.vc_type != CONV_NONE)
7039 {
7040 pc = string_convert(&spin->si_conv, rline, NULL);
7041 if (pc == NULL)
7042 {
7043 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
7044 fname, lnum, rline);
7045 continue;
7046 }
7047 line = pc;
7048 }
7049 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00007050#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007051 {
7052 pc = NULL;
7053 line = rline;
7054 }
7055
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007056 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00007057 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007058 ++line;
7059 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007060 {
7061 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007062 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
7063 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007064 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007065 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
7066 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007067 else
7068 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007069#ifdef FEAT_MBYTE
7070 char_u *enc;
7071
Bram Moolenaar51485f02005-06-04 21:55:20 +00007072 /* Setup for conversion to 'encoding'. */
Bram Moolenaar9c102382006-05-03 21:26:49 +00007073 line += 9;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007074 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007075 if (enc != NULL && !spin->si_ascii
7076 && convert_setup(&spin->si_conv, enc,
7077 p_enc) == FAIL)
7078 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00007079 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007080 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007081 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007082#else
7083 smsg((char_u *)_("Conversion in %s not supported"), fname);
7084#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007085 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007086 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007087 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007088
Bram Moolenaar3982c542005-06-08 21:56:31 +00007089 if (STRNCMP(line, "regions=", 8) == 0)
7090 {
7091 if (spin->si_region_count > 1)
7092 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
7093 fname, lnum, line);
7094 else
7095 {
7096 line += 8;
7097 if (STRLEN(line) > 16)
7098 smsg((char_u *)_("Too many regions in %s line %d: %s"),
7099 fname, lnum, line);
7100 else
7101 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007102 spin->si_region_count = (int)STRLEN(line) / 2;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007103 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007104
7105 /* Adjust the mask for a word valid in all regions. */
7106 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007107 }
7108 }
7109 continue;
7110 }
7111
Bram Moolenaar7887d882005-07-01 22:33:52 +00007112 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
7113 fname, lnum, line - 1);
7114 continue;
7115 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007116
Bram Moolenaar7887d882005-07-01 22:33:52 +00007117 flags = 0;
7118 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007119
Bram Moolenaar7887d882005-07-01 22:33:52 +00007120 /* Check for flags and region after a slash. */
7121 p = vim_strchr(line, '/');
7122 if (p != NULL)
7123 {
7124 *p++ = NUL;
7125 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007126 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007127 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007128 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007129 else if (*p == '!') /* Bad, bad, wicked word. */
7130 flags |= WF_BANNED;
7131 else if (*p == '?') /* Rare word. */
7132 flags |= WF_RARE;
7133 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007134 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007135 if ((flags & WF_REGION) == 0) /* first one */
7136 regionmask = 0;
7137 flags |= WF_REGION;
7138
7139 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00007140 if (l > spin->si_region_count)
7141 {
7142 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00007143 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007144 break;
7145 }
7146 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007147 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007148 else
7149 {
7150 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
7151 fname, lnum, p);
7152 break;
7153 }
7154 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007155 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007156 }
7157
7158 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
7159 if (spin->si_ascii && has_non_ascii(line))
7160 {
7161 ++non_ascii;
7162 continue;
7163 }
7164
7165 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007166 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007167 {
7168 retval = FAIL;
7169 break;
7170 }
7171 did_word = TRUE;
7172 }
7173
7174 vim_free(pc);
7175 fclose(fd);
7176
Bram Moolenaar4770d092006-01-12 23:22:24 +00007177 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007178 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007179 vim_snprintf((char *)IObuff, IOSIZE,
7180 _("Ignored %d words with non-ASCII characters"), non_ascii);
7181 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007182 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007183
Bram Moolenaar51485f02005-06-04 21:55:20 +00007184 return retval;
7185}
7186
7187/*
7188 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007189 * This avoids calling free() for every little struct we use (and keeping
7190 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00007191 * The memory is cleared to all zeros.
7192 * Returns NULL when out of memory.
7193 */
7194 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007195getroom(spin, len, align)
7196 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007197 size_t len; /* length needed */
7198 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007199{
7200 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007201 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007202
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007203 if (align && bl != NULL)
7204 /* Round size up for alignment. On some systems structures need to be
7205 * aligned to the size of a pointer (e.g., SPARC). */
7206 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
7207 & ~(sizeof(char *) - 1);
7208
Bram Moolenaar51485f02005-06-04 21:55:20 +00007209 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
7210 {
7211 /* Allocate a block of memory. This is not freed until much later. */
7212 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
7213 if (bl == NULL)
7214 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007215 bl->sb_next = spin->si_blocks;
7216 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007217 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007218 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007219 }
7220
7221 p = bl->sb_data + bl->sb_used;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007222 bl->sb_used += (int)len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007223
7224 return p;
7225}
7226
7227/*
7228 * Make a copy of a string into memory allocated with getroom().
7229 */
7230 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007231getroom_save(spin, s)
7232 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007233 char_u *s;
7234{
7235 char_u *sc;
7236
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007237 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007238 if (sc != NULL)
7239 STRCPY(sc, s);
7240 return sc;
7241}
7242
7243
7244/*
7245 * Free the list of allocated sblock_T.
7246 */
7247 static void
7248free_blocks(bl)
7249 sblock_T *bl;
7250{
7251 sblock_T *next;
7252
7253 while (bl != NULL)
7254 {
7255 next = bl->sb_next;
7256 vim_free(bl);
7257 bl = next;
7258 }
7259}
7260
7261/*
7262 * Allocate the root of a word tree.
7263 */
7264 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007265wordtree_alloc(spin)
7266 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007267{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007268 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007269}
7270
7271/*
7272 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007273 * Always store it in the case-folded tree. For a keep-case word this is
7274 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
7275 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007276 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007277 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
7278 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007279 */
7280 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00007281store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007282 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007283 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007284 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007285 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007286 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007287 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007288{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007289 int len = (int)STRLEN(word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007290 int ct = captype(word, word + len);
7291 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007292 int res = OK;
7293 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007294
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007295 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007296 for (p = pfxlist; res == OK; ++p)
7297 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007298 if (!need_affix || (p != NULL && *p != NUL))
7299 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007300 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007301 if (p == NULL || *p == NUL)
7302 break;
7303 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007304 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007305
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007306 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00007307 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007308 for (p = pfxlist; res == OK; ++p)
7309 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007310 if (!need_affix || (p != NULL && *p != NUL))
7311 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007312 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007313 if (p == NULL || *p == NUL)
7314 break;
7315 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007316 ++spin->si_keepwcount;
7317 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007318 return res;
7319}
7320
7321/*
7322 * Add word "word" to a word tree at "root".
Bram Moolenaar4770d092006-01-12 23:22:24 +00007323 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007324 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007325 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007326 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007327 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007328tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007329 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007330 char_u *word;
7331 wordnode_T *root;
7332 int flags;
7333 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007334 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007335{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007336 wordnode_T *node = root;
7337 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007338 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007339 wordnode_T **prev = NULL;
7340 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007341
Bram Moolenaar51485f02005-06-04 21:55:20 +00007342 /* Add each byte of the word to the tree, including the NUL at the end. */
7343 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007344 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007345 /* When there is more than one reference to this node we need to make
7346 * a copy, so that we can modify it. Copy the whole list of siblings
7347 * (we don't optimize for a partly shared list of siblings). */
7348 if (node != NULL && node->wn_refs > 1)
7349 {
7350 --node->wn_refs;
7351 copyprev = prev;
7352 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
7353 {
7354 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007355 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007356 if (np == NULL)
7357 return FAIL;
7358 np->wn_child = copyp->wn_child;
7359 if (np->wn_child != NULL)
7360 ++np->wn_child->wn_refs; /* child gets extra ref */
7361 np->wn_byte = copyp->wn_byte;
7362 if (np->wn_byte == NUL)
7363 {
7364 np->wn_flags = copyp->wn_flags;
7365 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007366 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007367 }
7368
7369 /* Link the new node in the list, there will be one ref. */
7370 np->wn_refs = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007371 if (copyprev != NULL)
7372 *copyprev = np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007373 copyprev = &np->wn_sibling;
7374
7375 /* Let "node" point to the head of the copied list. */
7376 if (copyp == node)
7377 node = np;
7378 }
7379 }
7380
Bram Moolenaar51485f02005-06-04 21:55:20 +00007381 /* Look for the sibling that has the same character. They are sorted
7382 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007383 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007384 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007385 while (node != NULL
7386 && (node->wn_byte < word[i]
7387 || (node->wn_byte == NUL
7388 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007389 ? node->wn_affixID < (unsigned)affixID
7390 : (node->wn_flags < (unsigned)(flags & WN_MASK)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007391 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007392 && (spin->si_sugtree
7393 ? (node->wn_region & 0xffff) < region
7394 : node->wn_affixID
7395 < (unsigned)affixID)))))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007396 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007397 prev = &node->wn_sibling;
7398 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007399 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007400 if (node == NULL
7401 || node->wn_byte != word[i]
7402 || (word[i] == NUL
7403 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007404 || spin->si_sugtree
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007405 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007406 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007407 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007408 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007409 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007410 if (np == NULL)
7411 return FAIL;
7412 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007413
7414 /* If "node" is NULL this is a new child or the end of the sibling
7415 * list: ref count is one. Otherwise use ref count of sibling and
7416 * make ref count of sibling one (matters when inserting in front
7417 * of the list of siblings). */
7418 if (node == NULL)
7419 np->wn_refs = 1;
7420 else
7421 {
7422 np->wn_refs = node->wn_refs;
7423 node->wn_refs = 1;
7424 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007425 *prev = np;
7426 np->wn_sibling = node;
7427 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007428 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007429
Bram Moolenaar51485f02005-06-04 21:55:20 +00007430 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007431 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007432 node->wn_flags = flags;
7433 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007434 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007435 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00007436 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007437 prev = &node->wn_child;
7438 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007439 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007440#ifdef SPELL_PRINTTREE
7441 smsg("Added \"%s\"", word);
7442 spell_print_tree(root->wn_sibling);
7443#endif
7444
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007445 /* count nr of words added since last message */
7446 ++spin->si_msg_count;
7447
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007448 if (spin->si_compress_cnt > 1)
7449 {
7450 if (--spin->si_compress_cnt == 1)
7451 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007452 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007453 }
7454
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007455 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007456 * When we have allocated lots of memory we need to compress the word tree
7457 * to free up some room. But compression is slow, and we might actually
7458 * need that room, thus only compress in the following situations:
7459 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00007460 * "compress_start" blocks.
7461 * 2. When compressed before and used "compress_inc" blocks before
7462 * adding "compress_added" words (si_compress_cnt > 1).
7463 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007464 * (si_compress_cnt == 1) and the number of free nodes drops below the
7465 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007466 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007467#ifndef SPELL_PRINTTREE
7468 if (spin->si_compress_cnt == 1
7469 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00007470 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007471#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007472 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007473 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00007474 * when the freed up room has been used and another "compress_inc"
7475 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007476 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007477 spin->si_blocks_cnt -= compress_inc;
7478 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007479
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007480 if (spin->si_verbose)
7481 {
7482 msg_start();
7483 msg_puts((char_u *)_(msg_compressing));
7484 msg_clr_eos();
7485 msg_didout = FALSE;
7486 msg_col = 0;
7487 out_flush();
7488 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007489
7490 /* Compress both trees. Either they both have many nodes, which makes
7491 * compression useful, or one of them is small, which means
Bram Moolenaar4770d092006-01-12 23:22:24 +00007492 * compression goes fast. But when filling the souldfold word tree
7493 * there is no keep-case tree. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007494 wordtree_compress(spin, spin->si_foldroot);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007495 if (affixID >= 0)
7496 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007497 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007498
7499 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007500}
7501
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007502/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007503 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
7504 * Sets "sps_flags".
7505 */
7506 int
7507spell_check_msm()
7508{
7509 char_u *p = p_msm;
7510 long start = 0;
7511 long inc = 0;
7512 long added = 0;
7513
7514 if (!VIM_ISDIGIT(*p))
7515 return FAIL;
7516 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
7517 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
7518 if (*p != ',')
7519 return FAIL;
7520 ++p;
7521 if (!VIM_ISDIGIT(*p))
7522 return FAIL;
7523 inc = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
7524 if (*p != ',')
7525 return FAIL;
7526 ++p;
7527 if (!VIM_ISDIGIT(*p))
7528 return FAIL;
7529 added = getdigits(&p) * 1024;
7530 if (*p != NUL)
7531 return FAIL;
7532
7533 if (start == 0 || inc == 0 || added == 0 || inc > start)
7534 return FAIL;
7535
7536 compress_start = start;
7537 compress_inc = inc;
7538 compress_added = added;
7539 return OK;
7540}
7541
7542
7543/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007544 * Get a wordnode_T, either from the list of previously freed nodes or
7545 * allocate a new one.
7546 */
7547 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007548get_wordnode(spin)
7549 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007550{
7551 wordnode_T *n;
7552
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007553 if (spin->si_first_free == NULL)
7554 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007555 else
7556 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007557 n = spin->si_first_free;
7558 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007559 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007560 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007561 }
7562#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007563 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007564#endif
7565 return n;
7566}
7567
7568/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007569 * Decrement the reference count on a node (which is the head of a list of
7570 * siblings). If the reference count becomes zero free the node and its
7571 * siblings.
Bram Moolenaar4770d092006-01-12 23:22:24 +00007572 * Returns the number of nodes actually freed.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007573 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007574 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007575deref_wordnode(spin, node)
7576 spellinfo_T *spin;
7577 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007578{
Bram Moolenaar4770d092006-01-12 23:22:24 +00007579 wordnode_T *np;
7580 int cnt = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007581
7582 if (--node->wn_refs == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007583 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007584 for (np = node; np != NULL; np = np->wn_sibling)
7585 {
7586 if (np->wn_child != NULL)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007587 cnt += deref_wordnode(spin, np->wn_child);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007588 free_wordnode(spin, np);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007589 ++cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007590 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007591 ++cnt; /* length field */
7592 }
7593 return cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007594}
7595
7596/*
7597 * Free a wordnode_T for re-use later.
7598 * Only the "wn_child" field becomes invalid.
7599 */
7600 static void
7601free_wordnode(spin, n)
7602 spellinfo_T *spin;
7603 wordnode_T *n;
7604{
7605 n->wn_child = spin->si_first_free;
7606 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007607 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007608}
7609
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007610/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007611 * Compress a tree: find tails that are identical and can be shared.
7612 */
7613 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007614wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007615 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007616 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007617{
7618 hashtab_T ht;
7619 int n;
7620 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007621 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007622
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007623 /* Skip the root itself, it's not actually used. The first sibling is the
7624 * start of the tree. */
7625 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007626 {
7627 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007628 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007629
7630#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00007631 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007632#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00007633 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007634 if (tot > 1000000)
7635 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007636 else if (tot == 0)
7637 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007638 else
7639 perc = (tot - n) * 100 / tot;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007640 vim_snprintf((char *)IObuff, IOSIZE,
7641 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
7642 n, tot, tot - n, perc);
7643 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007644 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007645#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007646 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007647#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007648 hash_clear(&ht);
7649 }
7650}
7651
7652/*
7653 * Compress a node, its siblings and its children, depth first.
7654 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007655 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007656 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007657node_compress(spin, node, ht, tot)
7658 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007659 wordnode_T *node;
7660 hashtab_T *ht;
7661 int *tot; /* total count of nodes before compressing,
7662 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007663{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007664 wordnode_T *np;
7665 wordnode_T *tp;
7666 wordnode_T *child;
7667 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007668 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007669 int len = 0;
7670 unsigned nr, n;
7671 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007672
Bram Moolenaar51485f02005-06-04 21:55:20 +00007673 /*
7674 * Go through the list of siblings. Compress each child and then try
7675 * finding an identical child to replace it.
7676 * Note that with "child" we mean not just the node that is pointed to,
Bram Moolenaar4770d092006-01-12 23:22:24 +00007677 * but the whole list of siblings of which the child node is the first.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007678 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007679 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007680 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007681 ++len;
7682 if ((child = np->wn_child) != NULL)
7683 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007684 /* Compress the child first. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007685 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007686
7687 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007688 hash = hash_hash(child->wn_u1.hashkey);
7689 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007690 if (!HASHITEM_EMPTY(hi))
7691 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007692 /* There are children we encountered before with a hash value
7693 * identical to the current child. Now check if there is one
7694 * that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007695 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007696 if (node_equal(child, tp))
7697 {
7698 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007699 * current one. This means the current child and all
7700 * its siblings is unlinked from the tree. */
7701 ++tp->wn_refs;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007702 compressed += deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007703 np->wn_child = tp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007704 break;
7705 }
7706 if (tp == NULL)
7707 {
7708 /* No other child with this hash value equals the child of
7709 * the node, add it to the linked list after the first
7710 * item. */
7711 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007712 child->wn_u2.next = tp->wn_u2.next;
7713 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007714 }
7715 }
7716 else
7717 /* No other child has this hash value, add it to the
7718 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007719 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007720 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007721 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007722 *tot += len + 1; /* add one for the node that stores the length */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007723
7724 /*
7725 * Make a hash key for the node and its siblings, so that we can quickly
7726 * find a lookalike node. This must be done after compressing the sibling
7727 * list, otherwise the hash key would become invalid by the compression.
7728 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007729 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007730 nr = 0;
7731 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007732 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007733 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007734 /* end node: use wn_flags, wn_region and wn_affixID */
7735 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007736 else
7737 /* byte node: use the byte value and the child pointer */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007738 n = (unsigned)(np->wn_byte + ((long_u)np->wn_child << 8));
Bram Moolenaar51485f02005-06-04 21:55:20 +00007739 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007740 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007741
7742 /* Avoid NUL bytes, it terminates the hash key. */
7743 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007744 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007745 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007746 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007747 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007748 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007749 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007750 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
7751 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007752
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007753 /* Check for CTRL-C pressed now and then. */
7754 fast_breakcheck();
7755
Bram Moolenaar51485f02005-06-04 21:55:20 +00007756 return compressed;
7757}
7758
7759/*
7760 * Return TRUE when two nodes have identical siblings and children.
7761 */
7762 static int
7763node_equal(n1, n2)
7764 wordnode_T *n1;
7765 wordnode_T *n2;
7766{
7767 wordnode_T *p1;
7768 wordnode_T *p2;
7769
7770 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
7771 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
7772 if (p1->wn_byte != p2->wn_byte
7773 || (p1->wn_byte == NUL
7774 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007775 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007776 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007777 : (p1->wn_child != p2->wn_child)))
7778 break;
7779
7780 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007781}
7782
7783/*
7784 * Write a number to file "fd", MSB first, in "len" bytes.
7785 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007786 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007787put_bytes(fd, nr, len)
7788 FILE *fd;
7789 long_u nr;
7790 int len;
7791{
7792 int i;
7793
7794 for (i = len - 1; i >= 0; --i)
7795 putc((int)(nr >> (i * 8)), fd);
7796}
7797
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007798#ifdef _MSC_VER
7799# if (_MSC_VER <= 1200)
7800/* This line is required for VC6 without the service pack. Also see the
7801 * matching #pragma below. */
7802/* # pragma optimize("", off) */
7803# endif
7804#endif
7805
Bram Moolenaar4770d092006-01-12 23:22:24 +00007806/*
7807 * Write spin->si_sugtime to file "fd".
7808 */
7809 static void
7810put_sugtime(spin, fd)
7811 spellinfo_T *spin;
7812 FILE *fd;
7813{
7814 int c;
7815 int i;
7816
7817 /* time_t can be up to 8 bytes in size, more than long_u, thus we
7818 * can't use put_bytes() here. */
7819 for (i = 7; i >= 0; --i)
7820 if (i + 1 > sizeof(time_t))
7821 /* ">>" doesn't work well when shifting more bits than avail */
7822 putc(0, fd);
7823 else
7824 {
7825 c = (unsigned)spin->si_sugtime >> (i * 8);
7826 putc(c, fd);
7827 }
7828}
7829
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007830#ifdef _MSC_VER
7831# if (_MSC_VER <= 1200)
7832/* # pragma optimize("", on) */
7833# endif
7834#endif
7835
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007836static int
7837#ifdef __BORLANDC__
7838_RTLENTRYF
7839#endif
7840rep_compare __ARGS((const void *s1, const void *s2));
7841
7842/*
7843 * Function given to qsort() to sort the REP items on "from" string.
7844 */
7845 static int
7846#ifdef __BORLANDC__
7847_RTLENTRYF
7848#endif
7849rep_compare(s1, s2)
7850 const void *s1;
7851 const void *s2;
7852{
7853 fromto_T *p1 = (fromto_T *)s1;
7854 fromto_T *p2 = (fromto_T *)s2;
7855
7856 return STRCMP(p1->ft_from, p2->ft_from);
7857}
7858
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007859/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007860 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007861 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007862 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007863 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007864write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007865 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007866 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007867{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007868 FILE *fd;
7869 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007870 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007871 wordnode_T *tree;
7872 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007873 int i;
7874 int l;
7875 garray_T *gap;
7876 fromto_T *ftp;
7877 char_u *p;
7878 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007879 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007880
Bram Moolenaarb765d632005-06-07 21:00:02 +00007881 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007882 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007883 {
7884 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007885 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007886 }
7887
Bram Moolenaar5195e452005-08-19 20:32:47 +00007888 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007889 /* <fileID> */
7890 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007891 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007892 EMSG(_(e_write));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007893 retval = FAIL;
7894 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007895 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007896
Bram Moolenaar5195e452005-08-19 20:32:47 +00007897 /*
7898 * <SECTIONS>: <section> ... <sectionend>
7899 */
7900
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007901 /* SN_INFO: <infotext> */
7902 if (spin->si_info != NULL)
7903 {
7904 putc(SN_INFO, fd); /* <sectionID> */
7905 putc(0, fd); /* <sectionflags> */
7906
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007907 i = (int)STRLEN(spin->si_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007908 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
7909 fwrite(spin->si_info, (size_t)i, (size_t)1, fd); /* <infotext> */
7910 }
7911
Bram Moolenaar5195e452005-08-19 20:32:47 +00007912 /* SN_REGION: <regionname> ...
7913 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007914 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007915 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007916 putc(SN_REGION, fd); /* <sectionID> */
7917 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7918 l = spin->si_region_count * 2;
7919 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7920 fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
7921 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007922 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007923 }
7924 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00007925 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007926
Bram Moolenaar5195e452005-08-19 20:32:47 +00007927 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
7928 *
7929 * The table with character flags and the table for case folding.
7930 * This makes sure the same characters are recognized as word characters
7931 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007932 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007933 * 'encoding'.
7934 * Also skip this for an .add.spl file, the main spell file must contain
7935 * the table (avoids that it conflicts). File is shorter too.
7936 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007937 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007938 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007939 char_u folchars[128 * 8];
7940 int flags;
7941
Bram Moolenaard12a1322005-08-21 22:08:24 +00007942 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007943 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7944
7945 /* Form the <folchars> string first, we need to know its length. */
7946 l = 0;
7947 for (i = 128; i < 256; ++i)
7948 {
7949#ifdef FEAT_MBYTE
7950 if (has_mbyte)
7951 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
7952 else
7953#endif
7954 folchars[l++] = spelltab.st_fold[i];
7955 }
7956 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
7957
7958 fputc(128, fd); /* <charflagslen> */
7959 for (i = 128; i < 256; ++i)
7960 {
7961 flags = 0;
7962 if (spelltab.st_isw[i])
7963 flags |= CF_WORD;
7964 if (spelltab.st_isu[i])
7965 flags |= CF_UPPER;
7966 fputc(flags, fd); /* <charflags> */
7967 }
7968
7969 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
7970 fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007971 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007972
Bram Moolenaar5195e452005-08-19 20:32:47 +00007973 /* SN_MIDWORD: <midword> */
7974 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007975 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007976 putc(SN_MIDWORD, fd); /* <sectionID> */
7977 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7978
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007979 i = (int)STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007980 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007981 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
7982 }
7983
Bram Moolenaar5195e452005-08-19 20:32:47 +00007984 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
7985 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007986 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007987 putc(SN_PREFCOND, fd); /* <sectionID> */
7988 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7989
7990 l = write_spell_prefcond(NULL, &spin->si_prefcond);
7991 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7992
7993 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007994 }
7995
Bram Moolenaar5195e452005-08-19 20:32:47 +00007996 /* SN_REP: <repcount> <rep> ...
Bram Moolenaar4770d092006-01-12 23:22:24 +00007997 * SN_SAL: <salflags> <salcount> <sal> ...
7998 * SN_REPSAL: <repcount> <rep> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007999
Bram Moolenaar5195e452005-08-19 20:32:47 +00008000 /* round 1: SN_REP section
Bram Moolenaar4770d092006-01-12 23:22:24 +00008001 * round 2: SN_SAL section (unless SN_SOFO is used)
8002 * round 3: SN_REPSAL section */
8003 for (round = 1; round <= 3; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008004 {
8005 if (round == 1)
8006 gap = &spin->si_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008007 else if (round == 2)
8008 {
8009 /* Don't write SN_SAL when using a SN_SOFO section */
8010 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8011 continue;
8012 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008013 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008014 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00008015 gap = &spin->si_repsal;
8016
8017 /* Don't write the section if there are no items. */
8018 if (gap->ga_len == 0)
8019 continue;
8020
8021 /* Sort the REP/REPSAL items. */
8022 if (round != 2)
8023 qsort(gap->ga_data, (size_t)gap->ga_len,
8024 sizeof(fromto_T), rep_compare);
8025
8026 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
8027 putc(i, fd); /* <sectionID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008028
Bram Moolenaar5195e452005-08-19 20:32:47 +00008029 /* This is for making suggestions, section is not required. */
8030 putc(0, fd); /* <sectionflags> */
8031
8032 /* Compute the length of what follows. */
8033 l = 2; /* count <repcount> or <salcount> */
8034 for (i = 0; i < gap->ga_len; ++i)
8035 {
8036 ftp = &((fromto_T *)gap->ga_data)[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008037 l += 1 + (int)STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
8038 l += 1 + (int)STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008039 }
8040 if (round == 2)
8041 ++l; /* count <salflags> */
8042 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8043
8044 if (round == 2)
8045 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008046 i = 0;
8047 if (spin->si_followup)
8048 i |= SAL_F0LLOWUP;
8049 if (spin->si_collapse)
8050 i |= SAL_COLLAPSE;
8051 if (spin->si_rem_accents)
8052 i |= SAL_REM_ACCENTS;
8053 putc(i, fd); /* <salflags> */
8054 }
8055
8056 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
8057 for (i = 0; i < gap->ga_len; ++i)
8058 {
8059 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
8060 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
8061 ftp = &((fromto_T *)gap->ga_data)[i];
8062 for (rr = 1; rr <= 2; ++rr)
8063 {
8064 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008065 l = (int)STRLEN(p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008066 putc(l, fd);
8067 fwrite(p, l, (size_t)1, fd);
8068 }
8069 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008070
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008071 }
8072
Bram Moolenaar5195e452005-08-19 20:32:47 +00008073 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
8074 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008075 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8076 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008077 putc(SN_SOFO, fd); /* <sectionID> */
8078 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008079
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008080 l = (int)STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008081 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
8082 /* <sectionlen> */
8083
8084 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
8085 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008086
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008087 l = (int)STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008088 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
8089 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008090 }
8091
Bram Moolenaar4770d092006-01-12 23:22:24 +00008092 /* SN_WORDS: <word> ...
8093 * This is for making suggestions, section is not required. */
8094 if (spin->si_commonwords.ht_used > 0)
8095 {
8096 putc(SN_WORDS, fd); /* <sectionID> */
8097 putc(0, fd); /* <sectionflags> */
8098
8099 /* round 1: count the bytes
8100 * round 2: write the bytes */
8101 for (round = 1; round <= 2; ++round)
8102 {
8103 int todo;
8104 int len = 0;
8105 hashitem_T *hi;
8106
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008107 todo = (int)spin->si_commonwords.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008108 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
8109 if (!HASHITEM_EMPTY(hi))
8110 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008111 l = (int)STRLEN(hi->hi_key) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008112 len += l;
8113 if (round == 2) /* <word> */
8114 fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
8115 --todo;
8116 }
8117 if (round == 1)
8118 put_bytes(fd, (long_u)len, 4); /* <sectionlen> */
8119 }
8120 }
8121
Bram Moolenaar5195e452005-08-19 20:32:47 +00008122 /* SN_MAP: <mapstr>
8123 * This is for making suggestions, section is not required. */
8124 if (spin->si_map.ga_len > 0)
8125 {
8126 putc(SN_MAP, fd); /* <sectionID> */
8127 putc(0, fd); /* <sectionflags> */
8128 l = spin->si_map.ga_len;
8129 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8130 fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
8131 /* <mapstr> */
8132 }
8133
Bram Moolenaar4770d092006-01-12 23:22:24 +00008134 /* SN_SUGFILE: <timestamp>
8135 * This is used to notify that a .sug file may be available and at the
8136 * same time allows for checking that a .sug file that is found matches
8137 * with this .spl file. That's because the word numbers must be exactly
8138 * right. */
8139 if (!spin->si_nosugfile
8140 && (spin->si_sal.ga_len > 0
8141 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
8142 {
8143 putc(SN_SUGFILE, fd); /* <sectionID> */
8144 putc(0, fd); /* <sectionflags> */
8145 put_bytes(fd, (long_u)8, 4); /* <sectionlen> */
8146
8147 /* Set si_sugtime and write it to the file. */
8148 spin->si_sugtime = time(NULL);
8149 put_sugtime(spin, fd); /* <timestamp> */
8150 }
8151
Bram Moolenaare1438bb2006-03-01 22:01:55 +00008152 /* SN_NOSPLITSUGS: nothing
8153 * This is used to notify that no suggestions with word splits are to be
8154 * made. */
8155 if (spin->si_nosplitsugs)
8156 {
8157 putc(SN_NOSPLITSUGS, fd); /* <sectionID> */
8158 putc(0, fd); /* <sectionflags> */
8159 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8160 }
8161
Bram Moolenaar5195e452005-08-19 20:32:47 +00008162 /* SN_COMPOUND: compound info.
8163 * We don't mark it required, when not supported all compound words will
8164 * be bad words. */
8165 if (spin->si_compflags != NULL)
8166 {
8167 putc(SN_COMPOUND, fd); /* <sectionID> */
8168 putc(0, fd); /* <sectionflags> */
8169
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008170 l = (int)STRLEN(spin->si_compflags);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008171 for (i = 0; i < spin->si_comppat.ga_len; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008172 l += (int)STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008173 put_bytes(fd, (long_u)(l + 7), 4); /* <sectionlen> */
8174
Bram Moolenaar5195e452005-08-19 20:32:47 +00008175 putc(spin->si_compmax, fd); /* <compmax> */
8176 putc(spin->si_compminlen, fd); /* <compminlen> */
8177 putc(spin->si_compsylmax, fd); /* <compsylmax> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008178 putc(0, fd); /* for Vim 7.0b compatibility */
8179 putc(spin->si_compoptions, fd); /* <compoptions> */
8180 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
8181 /* <comppatcount> */
8182 for (i = 0; i < spin->si_comppat.ga_len; ++i)
8183 {
8184 p = ((char_u **)(spin->si_comppat.ga_data))[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008185 putc((int)STRLEN(p), fd); /* <comppatlen> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008186 fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);/* <comppattext> */
8187 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008188 /* <compflags> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008189 fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
8190 (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008191 }
8192
Bram Moolenaar78622822005-08-23 21:00:13 +00008193 /* SN_NOBREAK: NOBREAK flag */
8194 if (spin->si_nobreak)
8195 {
8196 putc(SN_NOBREAK, fd); /* <sectionID> */
8197 putc(0, fd); /* <sectionflags> */
8198
8199 /* It's empty, the precense of the section flags the feature. */
8200 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8201 }
8202
Bram Moolenaar5195e452005-08-19 20:32:47 +00008203 /* SN_SYLLABLE: syllable info.
8204 * We don't mark it required, when not supported syllables will not be
8205 * counted. */
8206 if (spin->si_syllable != NULL)
8207 {
8208 putc(SN_SYLLABLE, fd); /* <sectionID> */
8209 putc(0, fd); /* <sectionflags> */
8210
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008211 l = (int)STRLEN(spin->si_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008212 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8213 fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd); /* <syllable> */
8214 }
8215
8216 /* end of <SECTIONS> */
8217 putc(SN_END, fd); /* <sectionend> */
8218
Bram Moolenaar50cde822005-06-05 21:54:54 +00008219
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008220 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008221 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008222 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008223 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008224 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008225 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008226 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008227 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008228 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008229 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008230 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008231 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008232
Bram Moolenaar0c405862005-06-22 22:26:26 +00008233 /* Clear the index and wnode fields in the tree. */
8234 clear_node(tree);
8235
Bram Moolenaar51485f02005-06-04 21:55:20 +00008236 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00008237 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00008238 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008239 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008240
Bram Moolenaar51485f02005-06-04 21:55:20 +00008241 /* number of nodes in 4 bytes */
8242 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00008243 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008244
Bram Moolenaar51485f02005-06-04 21:55:20 +00008245 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008246 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008247 }
8248
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008249 /* Write another byte to check for errors. */
8250 if (putc(0, fd) == EOF)
8251 retval = FAIL;
8252
8253 if (fclose(fd) == EOF)
8254 retval = FAIL;
8255
8256 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008257}
8258
8259/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008260 * Clear the index and wnode fields of "node", it siblings and its
8261 * children. This is needed because they are a union with other items to save
8262 * space.
8263 */
8264 static void
8265clear_node(node)
8266 wordnode_T *node;
8267{
8268 wordnode_T *np;
8269
8270 if (node != NULL)
8271 for (np = node; np != NULL; np = np->wn_sibling)
8272 {
8273 np->wn_u1.index = 0;
8274 np->wn_u2.wnode = NULL;
8275
8276 if (np->wn_byte != NUL)
8277 clear_node(np->wn_child);
8278 }
8279}
8280
8281
8282/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00008283 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008284 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00008285 * This first writes the list of possible bytes (siblings). Then for each
8286 * byte recursively write the children.
8287 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00008288 * NOTE: The code here must match the code in read_tree_node(), since
8289 * assumptions are made about the indexes (so that we don't have to write them
8290 * in the file).
Bram Moolenaar51485f02005-06-04 21:55:20 +00008291 *
8292 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008293 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008294 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00008295put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008296 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008297 wordnode_T *node;
8298 int index;
8299 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008300 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008301{
Bram Moolenaar51485f02005-06-04 21:55:20 +00008302 int newindex = index;
8303 int siblingcount = 0;
8304 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008305 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008306
Bram Moolenaar51485f02005-06-04 21:55:20 +00008307 /* If "node" is zero the tree is empty. */
8308 if (node == NULL)
8309 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008310
Bram Moolenaar51485f02005-06-04 21:55:20 +00008311 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008312 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008313
8314 /* Count the number of siblings. */
8315 for (np = node; np != NULL; np = np->wn_sibling)
8316 ++siblingcount;
8317
8318 /* Write the sibling count. */
8319 if (fd != NULL)
8320 putc(siblingcount, fd); /* <siblingcount> */
8321
8322 /* Write each sibling byte and optionally extra info. */
8323 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008324 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008325 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008326 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008327 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008328 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008329 /* For a NUL byte (end of word) write the flags etc. */
8330 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008331 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008332 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008333 * associated condition nr (stored in wn_region). The
8334 * byte value is misused to store the "rare" and "not
8335 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008336 if (np->wn_flags == (short_u)PFX_FLAGS)
8337 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008338 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00008339 {
8340 putc(BY_FLAGS, fd); /* <byte> */
8341 putc(np->wn_flags, fd); /* <pflags> */
8342 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008343 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008344 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008345 }
8346 else
8347 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008348 /* For word trees we write the flag/region items. */
8349 flags = np->wn_flags;
8350 if (regionmask != 0 && np->wn_region != regionmask)
8351 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008352 if (np->wn_affixID != 0)
8353 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008354 if (flags == 0)
8355 {
8356 /* word without flags or region */
8357 putc(BY_NOFLAGS, fd); /* <byte> */
8358 }
8359 else
8360 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008361 if (np->wn_flags >= 0x100)
8362 {
8363 putc(BY_FLAGS2, fd); /* <byte> */
8364 putc(flags, fd); /* <flags> */
8365 putc((unsigned)flags >> 8, fd); /* <flags2> */
8366 }
8367 else
8368 {
8369 putc(BY_FLAGS, fd); /* <byte> */
8370 putc(flags, fd); /* <flags> */
8371 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008372 if (flags & WF_REGION)
8373 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008374 if (flags & WF_AFX)
8375 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008376 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008377 }
8378 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008379 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008380 else
8381 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008382 if (np->wn_child->wn_u1.index != 0
8383 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008384 {
8385 /* The child is written elsewhere, write the reference. */
8386 if (fd != NULL)
8387 {
8388 putc(BY_INDEX, fd); /* <byte> */
8389 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008390 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008391 }
8392 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008393 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008394 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008395 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008396
Bram Moolenaar51485f02005-06-04 21:55:20 +00008397 if (fd != NULL)
8398 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
8399 {
8400 EMSG(_(e_write));
8401 return 0;
8402 }
8403 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008404 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008405
8406 /* Space used in the array when reading: one for each sibling and one for
8407 * the count. */
8408 newindex += siblingcount + 1;
8409
8410 /* Recursively dump the children of each sibling. */
8411 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008412 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
8413 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008414 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008415
8416 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008417}
8418
8419
8420/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008421 * ":mkspell [-ascii] outfile infile ..."
8422 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008423 */
8424 void
8425ex_mkspell(eap)
8426 exarg_T *eap;
8427{
8428 int fcount;
8429 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008430 char_u *arg = eap->arg;
8431 int ascii = FALSE;
8432
8433 if (STRNCMP(arg, "-ascii", 6) == 0)
8434 {
8435 ascii = TRUE;
8436 arg = skipwhite(arg + 6);
8437 }
8438
8439 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
8440 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
8441 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008442 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008443 FreeWild(fcount, fnames);
8444 }
8445}
8446
8447/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00008448 * Create the .sug file.
8449 * Uses the soundfold info in "spin".
8450 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
8451 */
8452 static void
8453spell_make_sugfile(spin, wfname)
8454 spellinfo_T *spin;
8455 char_u *wfname;
8456{
8457 char_u fname[MAXPATHL];
8458 int len;
8459 slang_T *slang;
8460 int free_slang = FALSE;
8461
8462 /*
8463 * Read back the .spl file that was written. This fills the required
8464 * info for soundfolding. This also uses less memory than the
8465 * pointer-linked version of the trie. And it avoids having two versions
8466 * of the code for the soundfolding stuff.
8467 * It might have been done already by spell_reload_one().
8468 */
8469 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
8470 if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME)
8471 break;
8472 if (slang == NULL)
8473 {
8474 spell_message(spin, (char_u *)_("Reading back spell file..."));
8475 slang = spell_load_file(wfname, NULL, NULL, FALSE);
8476 if (slang == NULL)
8477 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008478 free_slang = TRUE;
8479 }
8480
8481 /*
8482 * Clear the info in "spin" that is used.
8483 */
8484 spin->si_blocks = NULL;
8485 spin->si_blocks_cnt = 0;
8486 spin->si_compress_cnt = 0; /* will stay at 0 all the time*/
8487 spin->si_free_count = 0;
8488 spin->si_first_free = NULL;
8489 spin->si_foldwcount = 0;
8490
8491 /*
8492 * Go through the trie of good words, soundfold each word and add it to
8493 * the soundfold trie.
8494 */
8495 spell_message(spin, (char_u *)_("Performing soundfolding..."));
8496 if (sug_filltree(spin, slang) == FAIL)
8497 goto theend;
8498
8499 /*
8500 * Create the table which links each soundfold word with a list of the
8501 * good words it may come from. Creates buffer "spin->si_spellbuf".
8502 * This also removes the wordnr from the NUL byte entries to make
8503 * compression possible.
8504 */
8505 if (sug_maketable(spin) == FAIL)
8506 goto theend;
8507
8508 smsg((char_u *)_("Number of words after soundfolding: %ld"),
8509 (long)spin->si_spellbuf->b_ml.ml_line_count);
8510
8511 /*
8512 * Compress the soundfold trie.
8513 */
8514 spell_message(spin, (char_u *)_(msg_compressing));
8515 wordtree_compress(spin, spin->si_foldroot);
8516
8517 /*
8518 * Write the .sug file.
8519 * Make the file name by changing ".spl" to ".sug".
8520 */
8521 STRCPY(fname, wfname);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008522 len = (int)STRLEN(fname);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008523 fname[len - 2] = 'u';
8524 fname[len - 1] = 'g';
8525 sug_write(spin, fname);
8526
8527theend:
8528 if (free_slang)
8529 slang_free(slang);
8530 free_blocks(spin->si_blocks);
8531 close_spellbuf(spin->si_spellbuf);
8532}
8533
8534/*
8535 * Build the soundfold trie for language "slang".
8536 */
8537 static int
8538sug_filltree(spin, slang)
8539 spellinfo_T *spin;
8540 slang_T *slang;
8541{
8542 char_u *byts;
8543 idx_T *idxs;
8544 int depth;
8545 idx_T arridx[MAXWLEN];
8546 int curi[MAXWLEN];
8547 char_u tword[MAXWLEN];
8548 char_u tsalword[MAXWLEN];
8549 int c;
8550 idx_T n;
8551 unsigned words_done = 0;
8552 int wordcount[MAXWLEN];
8553
8554 /* We use si_foldroot for the souldfolded trie. */
8555 spin->si_foldroot = wordtree_alloc(spin);
8556 if (spin->si_foldroot == NULL)
8557 return FAIL;
8558
8559 /* let tree_add_word() know we're adding to the soundfolded tree */
8560 spin->si_sugtree = TRUE;
8561
8562 /*
8563 * Go through the whole case-folded tree, soundfold each word and put it
8564 * in the trie.
8565 */
8566 byts = slang->sl_fbyts;
8567 idxs = slang->sl_fidxs;
8568
8569 arridx[0] = 0;
8570 curi[0] = 1;
8571 wordcount[0] = 0;
8572
8573 depth = 0;
8574 while (depth >= 0 && !got_int)
8575 {
8576 if (curi[depth] > byts[arridx[depth]])
8577 {
8578 /* Done all bytes at this node, go up one level. */
8579 idxs[arridx[depth]] = wordcount[depth];
8580 if (depth > 0)
8581 wordcount[depth - 1] += wordcount[depth];
8582
8583 --depth;
8584 line_breakcheck();
8585 }
8586 else
8587 {
8588
8589 /* Do one more byte at this node. */
8590 n = arridx[depth] + curi[depth];
8591 ++curi[depth];
8592
8593 c = byts[n];
8594 if (c == 0)
8595 {
8596 /* Sound-fold the word. */
8597 tword[depth] = NUL;
8598 spell_soundfold(slang, tword, TRUE, tsalword);
8599
8600 /* We use the "flags" field for the MSB of the wordnr,
8601 * "region" for the LSB of the wordnr. */
8602 if (tree_add_word(spin, tsalword, spin->si_foldroot,
8603 words_done >> 16, words_done & 0xffff,
8604 0) == FAIL)
8605 return FAIL;
8606
8607 ++words_done;
8608 ++wordcount[depth];
8609
8610 /* Reset the block count each time to avoid compression
8611 * kicking in. */
8612 spin->si_blocks_cnt = 0;
8613
8614 /* Skip over any other NUL bytes (same word with different
8615 * flags). */
8616 while (byts[n + 1] == 0)
8617 {
8618 ++n;
8619 ++curi[depth];
8620 }
8621 }
8622 else
8623 {
8624 /* Normal char, go one level deeper. */
8625 tword[depth++] = c;
8626 arridx[depth] = idxs[n];
8627 curi[depth] = 1;
8628 wordcount[depth] = 0;
8629 }
8630 }
8631 }
8632
8633 smsg((char_u *)_("Total number of words: %d"), words_done);
8634
8635 return OK;
8636}
8637
8638/*
8639 * Make the table that links each word in the soundfold trie to the words it
8640 * can be produced from.
8641 * This is not unlike lines in a file, thus use a memfile to be able to access
8642 * the table efficiently.
8643 * Returns FAIL when out of memory.
8644 */
8645 static int
8646sug_maketable(spin)
8647 spellinfo_T *spin;
8648{
8649 garray_T ga;
8650 int res = OK;
8651
8652 /* Allocate a buffer, open a memline for it and create the swap file
8653 * (uses a temp file, not a .swp file). */
8654 spin->si_spellbuf = open_spellbuf();
8655 if (spin->si_spellbuf == NULL)
8656 return FAIL;
8657
8658 /* Use a buffer to store the line info, avoids allocating many small
8659 * pieces of memory. */
8660 ga_init2(&ga, 1, 100);
8661
8662 /* recursively go through the tree */
8663 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
8664 res = FAIL;
8665
8666 ga_clear(&ga);
8667 return res;
8668}
8669
8670/*
8671 * Fill the table for one node and its children.
8672 * Returns the wordnr at the start of the node.
8673 * Returns -1 when out of memory.
8674 */
8675 static int
8676sug_filltable(spin, node, startwordnr, gap)
8677 spellinfo_T *spin;
8678 wordnode_T *node;
8679 int startwordnr;
8680 garray_T *gap; /* place to store line of numbers */
8681{
8682 wordnode_T *p, *np;
8683 int wordnr = startwordnr;
8684 int nr;
8685 int prev_nr;
8686
8687 for (p = node; p != NULL; p = p->wn_sibling)
8688 {
8689 if (p->wn_byte == NUL)
8690 {
8691 gap->ga_len = 0;
8692 prev_nr = 0;
8693 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
8694 {
8695 if (ga_grow(gap, 10) == FAIL)
8696 return -1;
8697
8698 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
8699 /* Compute the offset from the previous nr and store the
8700 * offset in a way that it takes a minimum number of bytes.
8701 * It's a bit like utf-8, but without the need to mark
8702 * following bytes. */
8703 nr -= prev_nr;
8704 prev_nr += nr;
8705 gap->ga_len += offset2bytes(nr,
8706 (char_u *)gap->ga_data + gap->ga_len);
8707 }
8708
8709 /* add the NUL byte */
8710 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
8711
8712 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
8713 gap->ga_data, gap->ga_len, TRUE) == FAIL)
8714 return -1;
8715 ++wordnr;
8716
8717 /* Remove extra NUL entries, we no longer need them. We don't
8718 * bother freeing the nodes, the won't be reused anyway. */
8719 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
8720 p->wn_sibling = p->wn_sibling->wn_sibling;
8721
8722 /* Clear the flags on the remaining NUL node, so that compression
8723 * works a lot better. */
8724 p->wn_flags = 0;
8725 p->wn_region = 0;
8726 }
8727 else
8728 {
8729 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
8730 if (wordnr == -1)
8731 return -1;
8732 }
8733 }
8734 return wordnr;
8735}
8736
8737/*
8738 * Convert an offset into a minimal number of bytes.
8739 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
8740 * bytes.
8741 */
8742 static int
8743offset2bytes(nr, buf)
8744 int nr;
8745 char_u *buf;
8746{
8747 int rem;
8748 int b1, b2, b3, b4;
8749
8750 /* Split the number in parts of base 255. We need to avoid NUL bytes. */
8751 b1 = nr % 255 + 1;
8752 rem = nr / 255;
8753 b2 = rem % 255 + 1;
8754 rem = rem / 255;
8755 b3 = rem % 255 + 1;
8756 b4 = rem / 255 + 1;
8757
8758 if (b4 > 1 || b3 > 0x1f) /* 4 bytes */
8759 {
8760 buf[0] = 0xe0 + b4;
8761 buf[1] = b3;
8762 buf[2] = b2;
8763 buf[3] = b1;
8764 return 4;
8765 }
8766 if (b3 > 1 || b2 > 0x3f ) /* 3 bytes */
8767 {
8768 buf[0] = 0xc0 + b3;
8769 buf[1] = b2;
8770 buf[2] = b1;
8771 return 3;
8772 }
8773 if (b2 > 1 || b1 > 0x7f ) /* 2 bytes */
8774 {
8775 buf[0] = 0x80 + b2;
8776 buf[1] = b1;
8777 return 2;
8778 }
8779 /* 1 byte */
8780 buf[0] = b1;
8781 return 1;
8782}
8783
8784/*
8785 * Opposite of offset2bytes().
8786 * "pp" points to the bytes and is advanced over it.
8787 * Returns the offset.
8788 */
8789 static int
8790bytes2offset(pp)
8791 char_u **pp;
8792{
8793 char_u *p = *pp;
8794 int nr;
8795 int c;
8796
8797 c = *p++;
8798 if ((c & 0x80) == 0x00) /* 1 byte */
8799 {
8800 nr = c - 1;
8801 }
8802 else if ((c & 0xc0) == 0x80) /* 2 bytes */
8803 {
8804 nr = (c & 0x3f) - 1;
8805 nr = nr * 255 + (*p++ - 1);
8806 }
8807 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
8808 {
8809 nr = (c & 0x1f) - 1;
8810 nr = nr * 255 + (*p++ - 1);
8811 nr = nr * 255 + (*p++ - 1);
8812 }
8813 else /* 4 bytes */
8814 {
8815 nr = (c & 0x0f) - 1;
8816 nr = nr * 255 + (*p++ - 1);
8817 nr = nr * 255 + (*p++ - 1);
8818 nr = nr * 255 + (*p++ - 1);
8819 }
8820
8821 *pp = p;
8822 return nr;
8823}
8824
8825/*
8826 * Write the .sug file in "fname".
8827 */
8828 static void
8829sug_write(spin, fname)
8830 spellinfo_T *spin;
8831 char_u *fname;
8832{
8833 FILE *fd;
8834 wordnode_T *tree;
8835 int nodecount;
8836 int wcount;
8837 char_u *line;
8838 linenr_T lnum;
8839 int len;
8840
8841 /* Create the file. Note that an existing file is silently overwritten! */
8842 fd = mch_fopen((char *)fname, "w");
8843 if (fd == NULL)
8844 {
8845 EMSG2(_(e_notopen), fname);
8846 return;
8847 }
8848
8849 vim_snprintf((char *)IObuff, IOSIZE,
8850 _("Writing suggestion file %s ..."), fname);
8851 spell_message(spin, IObuff);
8852
8853 /*
8854 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
8855 */
8856 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) /* <fileID> */
8857 {
8858 EMSG(_(e_write));
8859 goto theend;
8860 }
8861 putc(VIMSUGVERSION, fd); /* <versionnr> */
8862
8863 /* Write si_sugtime to the file. */
8864 put_sugtime(spin, fd); /* <timestamp> */
8865
8866 /*
8867 * <SUGWORDTREE>
8868 */
8869 spin->si_memtot = 0;
8870 tree = spin->si_foldroot->wn_sibling;
8871
8872 /* Clear the index and wnode fields in the tree. */
8873 clear_node(tree);
8874
8875 /* Count the number of nodes. Needed to be able to allocate the
8876 * memory when reading the nodes. Also fills in index for shared
8877 * nodes. */
8878 nodecount = put_node(NULL, tree, 0, 0, FALSE);
8879
8880 /* number of nodes in 4 bytes */
8881 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
8882 spin->si_memtot += nodecount + nodecount * sizeof(int);
8883
8884 /* Write the nodes. */
8885 (void)put_node(fd, tree, 0, 0, FALSE);
8886
8887 /*
8888 * <SUGTABLE>: <sugwcount> <sugline> ...
8889 */
8890 wcount = spin->si_spellbuf->b_ml.ml_line_count;
8891 put_bytes(fd, (long_u)wcount, 4); /* <sugwcount> */
8892
8893 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
8894 {
8895 /* <sugline>: <sugnr> ... NUL */
8896 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008897 len = (int)STRLEN(line) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008898 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
8899 {
8900 EMSG(_(e_write));
8901 goto theend;
8902 }
8903 spin->si_memtot += len;
8904 }
8905
8906 /* Write another byte to check for errors. */
8907 if (putc(0, fd) == EOF)
8908 EMSG(_(e_write));
8909
8910 vim_snprintf((char *)IObuff, IOSIZE,
8911 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
8912 spell_message(spin, IObuff);
8913
8914theend:
8915 /* close the file */
8916 fclose(fd);
8917}
8918
8919/*
8920 * Open a spell buffer. This is a nameless buffer that is not in the buffer
8921 * list and only contains text lines. Can use a swapfile to reduce memory
8922 * use.
8923 * Most other fields are invalid! Esp. watch out for string options being
8924 * NULL and there is no undo info.
8925 * Returns NULL when out of memory.
8926 */
8927 static buf_T *
8928open_spellbuf()
8929{
8930 buf_T *buf;
8931
8932 buf = (buf_T *)alloc_clear(sizeof(buf_T));
8933 if (buf != NULL)
8934 {
8935 buf->b_spell = TRUE;
8936 buf->b_p_swf = TRUE; /* may create a swap file */
8937 ml_open(buf);
8938 ml_open_file(buf); /* create swap file now */
8939 }
8940 return buf;
8941}
8942
8943/*
8944 * Close the buffer used for spell info.
8945 */
8946 static void
8947close_spellbuf(buf)
8948 buf_T *buf;
8949{
8950 if (buf != NULL)
8951 {
8952 ml_close(buf, TRUE);
8953 vim_free(buf);
8954 }
8955}
8956
8957
8958/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008959 * Create a Vim spell file from one or more word lists.
8960 * "fnames[0]" is the output file name.
8961 * "fnames[fcount - 1]" is the last input file name.
8962 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
8963 * and ".spl" is appended to make the output file name.
8964 */
8965 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008966mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00008967 int fcount;
8968 char_u **fnames;
8969 int ascii; /* -ascii argument given */
8970 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008971 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008972{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008973 char_u fname[MAXPATHL];
8974 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00008975 char_u **innames;
8976 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008977 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008978 int i;
8979 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008980 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008981 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008982 spellinfo_T spin;
8983
8984 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008985 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008986 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008987 spin.si_followup = TRUE;
8988 spin.si_rem_accents = TRUE;
8989 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008990 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008991 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
8992 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008993 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008994 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008995 hash_init(&spin.si_commonwords);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008996 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008997
Bram Moolenaarb765d632005-06-07 21:00:02 +00008998 /* default: fnames[0] is output file, following are input files */
8999 innames = &fnames[1];
9000 incount = fcount - 1;
9001
9002 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00009003 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009004 len = (int)STRLEN(fnames[0]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009005 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
9006 {
9007 /* For ":mkspell path/en.latin1.add" output file is
9008 * "path/en.latin1.add.spl". */
9009 innames = &fnames[0];
9010 incount = 1;
9011 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
9012 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009013 else if (fcount == 1)
9014 {
9015 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
9016 innames = &fnames[0];
9017 incount = 1;
9018 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
9019 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
9020 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009021 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
9022 {
9023 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009024 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009025 }
9026 else
9027 /* Name should be language, make the file name from it. */
9028 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
9029 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
9030
9031 /* Check for .ascii.spl. */
9032 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
9033 spin.si_ascii = TRUE;
9034
9035 /* Check for .add.spl. */
9036 if (strstr((char *)gettail(wfname), ".add.") != NULL)
9037 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00009038 }
9039
Bram Moolenaarb765d632005-06-07 21:00:02 +00009040 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009041 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009042 else if (vim_strchr(gettail(wfname), '_') != NULL)
9043 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009044 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009045 EMSG(_("E754: Only up to 8 regions supported"));
9046 else
9047 {
9048 /* Check for overwriting before doing things that may take a lot of
9049 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009050 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009051 {
9052 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009053 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009054 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009055 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009056 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009057 EMSG2(_(e_isadir2), wfname);
9058 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009059 }
9060
9061 /*
9062 * Init the aff and dic pointers.
9063 * Get the region names if there are more than 2 arguments.
9064 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009065 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009066 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009067 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009068
Bram Moolenaar3982c542005-06-08 21:56:31 +00009069 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009070 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009071 len = (int)STRLEN(innames[i]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009072 if (STRLEN(gettail(innames[i])) < 5
9073 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009074 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009075 EMSG2(_("E755: Invalid region in %s"), innames[i]);
9076 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009077 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009078 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
9079 spin.si_region_name[i * 2 + 1] =
9080 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009081 }
9082 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009083 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009084
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009085 spin.si_foldroot = wordtree_alloc(&spin);
9086 spin.si_keeproot = wordtree_alloc(&spin);
9087 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009088 if (spin.si_foldroot == NULL
9089 || spin.si_keeproot == NULL
9090 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009091 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00009092 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009093 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009094 }
9095
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009096 /* When not producing a .add.spl file clear the character table when
9097 * we encounter one in the .aff file. This means we dump the current
9098 * one in the .spl file if the .aff file doesn't define one. That's
9099 * better than guessing the contents, the table will match a
9100 * previously loaded spell file. */
9101 if (!spin.si_add)
9102 spin.si_clear_chartab = TRUE;
9103
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009104 /*
9105 * Read all the .aff and .dic files.
9106 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00009107 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009108 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009109 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009110 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009111 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009112 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009113
Bram Moolenaarb765d632005-06-07 21:00:02 +00009114 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009115 if (mch_stat((char *)fname, &st) >= 0)
9116 {
9117 /* Read the .aff file. Will init "spin->si_conv" based on the
9118 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009119 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009120 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009121 error = TRUE;
9122 else
9123 {
9124 /* Read the .dic file and store the words in the trees. */
9125 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00009126 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009127 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009128 error = TRUE;
9129 }
9130 }
9131 else
9132 {
9133 /* No .aff file, try reading the file as a word list. Store
9134 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009135 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009136 error = TRUE;
9137 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009138
Bram Moolenaarb765d632005-06-07 21:00:02 +00009139#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009140 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00009141 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009142#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009143 }
9144
Bram Moolenaar78622822005-08-23 21:00:13 +00009145 if (spin.si_compflags != NULL && spin.si_nobreak)
9146 MSG(_("Warning: both compounding and NOBREAK specified"));
9147
Bram Moolenaar4770d092006-01-12 23:22:24 +00009148 if (!error && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009149 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009150 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00009151 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009152 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009153 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009154 wordtree_compress(&spin, spin.si_foldroot);
9155 wordtree_compress(&spin, spin.si_keeproot);
9156 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009157 }
9158
Bram Moolenaar4770d092006-01-12 23:22:24 +00009159 if (!error && !got_int)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009160 {
9161 /*
9162 * Write the info in the spell file.
9163 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009164 vim_snprintf((char *)IObuff, IOSIZE,
9165 _("Writing spell file %s ..."), wfname);
9166 spell_message(&spin, IObuff);
Bram Moolenaar50cde822005-06-05 21:54:54 +00009167
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009168 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009169
Bram Moolenaar4770d092006-01-12 23:22:24 +00009170 spell_message(&spin, (char_u *)_("Done!"));
9171 vim_snprintf((char *)IObuff, IOSIZE,
9172 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
9173 spell_message(&spin, IObuff);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009174
Bram Moolenaar4770d092006-01-12 23:22:24 +00009175 /*
9176 * If the file is loaded need to reload it.
9177 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009178 if (!error)
9179 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009180 }
9181
9182 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009183 ga_clear(&spin.si_rep);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009184 ga_clear(&spin.si_repsal);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009185 ga_clear(&spin.si_sal);
9186 ga_clear(&spin.si_map);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009187 ga_clear(&spin.si_comppat);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009188 ga_clear(&spin.si_prefcond);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009189 hash_clear_all(&spin.si_commonwords, 0);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009190
9191 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009192 for (i = 0; i < incount; ++i)
9193 if (afile[i] != NULL)
9194 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009195
9196 /* Free all the bits and pieces at once. */
9197 free_blocks(spin.si_blocks);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009198
9199 /*
9200 * If there is soundfolding info and no NOSUGFILE item create the
9201 * .sug file with the soundfolded word trie.
9202 */
9203 if (spin.si_sugtime != 0 && !error && !got_int)
9204 spell_make_sugfile(&spin, wfname);
9205
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009206 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009207}
9208
Bram Moolenaar4770d092006-01-12 23:22:24 +00009209/*
9210 * Display a message for spell file processing when 'verbose' is set or using
9211 * ":mkspell". "str" can be IObuff.
9212 */
9213 static void
9214spell_message(spin, str)
9215 spellinfo_T *spin;
9216 char_u *str;
9217{
9218 if (spin->si_verbose || p_verbose > 2)
9219 {
9220 if (!spin->si_verbose)
9221 verbose_enter();
9222 MSG(str);
9223 out_flush();
9224 if (!spin->si_verbose)
9225 verbose_leave();
9226 }
9227}
Bram Moolenaarb765d632005-06-07 21:00:02 +00009228
9229/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009230 * ":[count]spellgood {word}"
9231 * ":[count]spellwrong {word}"
Bram Moolenaard0131a82006-03-04 21:46:13 +00009232 * ":[count]spellundo {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00009233 */
9234 void
9235ex_spell(eap)
9236 exarg_T *eap;
9237{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009238 spell_add_word(eap->arg, (int)STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaard0131a82006-03-04 21:46:13 +00009239 eap->forceit ? 0 : (int)eap->line2,
9240 eap->cmdidx == CMD_spellundo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009241}
9242
9243/*
9244 * Add "word[len]" to 'spellfile' as a good or bad word.
9245 */
9246 void
Bram Moolenaard0131a82006-03-04 21:46:13 +00009247spell_add_word(word, len, bad, index, undo)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009248 char_u *word;
9249 int len;
9250 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009251 int index; /* "zG" and "zW": zero, otherwise index in
9252 'spellfile' */
Bram Moolenaard0131a82006-03-04 21:46:13 +00009253 int undo; /* TRUE for "zug", "zuG", "zuw" and "zuW" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009254{
9255 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009256 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009257 int new_spf = FALSE;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009258 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009259 char_u fnamebuf[MAXPATHL];
9260 char_u line[MAXWLEN * 2];
9261 long fpos, fpos_next = 0;
9262 int i;
9263 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009264
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009265 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009266 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009267 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009268 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009269 int_wordlist = vim_tempname('s');
9270 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009271 return;
9272 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009273 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009274 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009275 else
9276 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009277 /* If 'spellfile' isn't set figure out a good default value. */
9278 if (*curbuf->b_p_spf == NUL)
9279 {
9280 init_spellfile();
9281 new_spf = TRUE;
9282 }
9283
9284 if (*curbuf->b_p_spf == NUL)
9285 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00009286 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009287 return;
9288 }
9289
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009290 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
9291 {
9292 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
9293 if (i == index)
9294 break;
9295 if (*spf == NUL)
9296 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00009297 EMSGN(_("E765: 'spellfile' does not have %ld entries"), index);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009298 return;
9299 }
9300 }
9301
Bram Moolenaarb765d632005-06-07 21:00:02 +00009302 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009303 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009304 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
9305 buf = NULL;
9306 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00009307 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009308 EMSG(_(e_bufloaded));
9309 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009310 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009311
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009312 fname = fnamebuf;
9313 }
9314
Bram Moolenaard0131a82006-03-04 21:46:13 +00009315 if (bad || undo)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009316 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009317 /* When the word appears as good word we need to remove that one,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009318 * since its flags sort before the one with WF_BANNED. */
9319 fd = mch_fopen((char *)fname, "r");
9320 if (fd != NULL)
9321 {
9322 while (!vim_fgets(line, MAXWLEN * 2, fd))
9323 {
9324 fpos = fpos_next;
9325 fpos_next = ftell(fd);
9326 if (STRNCMP(word, line, len) == 0
9327 && (line[len] == '/' || line[len] < ' '))
9328 {
9329 /* Found duplicate word. Remove it by writing a '#' at
9330 * the start of the line. Mixing reading and writing
9331 * doesn't work for all systems, close the file first. */
9332 fclose(fd);
9333 fd = mch_fopen((char *)fname, "r+");
9334 if (fd == NULL)
9335 break;
9336 if (fseek(fd, fpos, SEEK_SET) == 0)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009337 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009338 fputc('#', fd);
Bram Moolenaard0131a82006-03-04 21:46:13 +00009339 if (undo)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009340 smsg((char_u *)_("Word removed from %s"), NameBuff);
Bram Moolenaard0131a82006-03-04 21:46:13 +00009341 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009342 fseek(fd, fpos_next, SEEK_SET);
9343 }
9344 }
9345 fclose(fd);
9346 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009347 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009348 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00009349 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009350 fd = mch_fopen((char *)fname, "a");
9351 if (fd == NULL && new_spf)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009352 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009353 /* We just initialized the 'spellfile' option and can't open the
9354 * file. We may need to create the "spell" directory first. We
9355 * already checked the runtime directory is writable in
9356 * init_spellfile(). */
9357 if (!dir_of_file_exists(fname))
9358 {
9359 /* The directory doesn't exist. Try creating it and opening
9360 * the file again. */
9361 vim_mkdir(NameBuff, 0755);
9362 fd = mch_fopen((char *)fname, "a");
9363 }
9364 }
9365
9366 if (fd == NULL)
9367 EMSG2(_(e_notopen), fname);
9368 else
9369 {
9370 if (bad)
9371 fprintf(fd, "%.*s/!\n", len, word);
9372 else
9373 fprintf(fd, "%.*s\n", len, word);
9374 fclose(fd);
9375
9376 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
9377 smsg((char_u *)_("Word added to %s"), NameBuff);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009378 }
9379 }
9380
Bram Moolenaard0131a82006-03-04 21:46:13 +00009381 if (fd != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009382 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009383 /* Update the .add.spl file. */
9384 mkspell(1, &fname, FALSE, TRUE, TRUE);
9385
9386 /* If the .add file is edited somewhere, reload it. */
9387 if (buf != NULL)
Bram Moolenaarea8bd732006-01-14 21:15:59 +00009388 buf_reload(buf, buf->b_orig_mode);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009389
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009390 redraw_all_later(SOME_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009391 }
9392}
9393
9394/*
9395 * Initialize 'spellfile' for the current buffer.
9396 */
9397 static void
9398init_spellfile()
9399{
9400 char_u buf[MAXPATHL];
9401 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009402 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009403 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009404 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009405 int aspath = FALSE;
9406 char_u *lstart = curbuf->b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009407
9408 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
9409 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009410 /* Find the end of the language name. Exclude the region. If there
9411 * is a path separator remember the start of the tail. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009412 for (lend = curbuf->b_p_spl; *lend != NUL
9413 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009414 if (vim_ispathsep(*lend))
9415 {
9416 aspath = TRUE;
9417 lstart = lend + 1;
9418 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009419
9420 /* Loop over all entries in 'runtimepath'. Use the first one where we
9421 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009422 rtp = p_rtp;
9423 while (*rtp != NUL)
9424 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009425 if (aspath)
9426 /* Use directory of an entry with path, e.g., for
9427 * "/dir/lg.utf-8.spl" use "/dir". */
9428 vim_strncpy(buf, curbuf->b_p_spl, lstart - curbuf->b_p_spl - 1);
9429 else
9430 /* Copy the path from 'runtimepath' to buf[]. */
9431 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00009432 if (filewritable(buf) == 2)
9433 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00009434 /* Use the first language name from 'spelllang' and the
9435 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009436 if (aspath)
9437 vim_strncpy(buf, curbuf->b_p_spl, lend - curbuf->b_p_spl);
9438 else
9439 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009440 /* Create the "spell" directory if it doesn't exist yet. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009441 l = (int)STRLEN(buf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009442 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
9443 if (!filewritable(buf) != 2)
9444 vim_mkdir(buf, 0755);
9445
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009446 l = (int)STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009447 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009448 "/%.*s", (int)(lend - lstart), lstart);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009449 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009450 l = (int)STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009451 fname = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang->sl_fname;
9452 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
9453 fname != NULL
9454 && strstr((char *)gettail(fname), ".ascii.") != NULL
9455 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009456 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
9457 break;
9458 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009459 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009460 }
9461 }
9462}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009463
Bram Moolenaar51485f02005-06-04 21:55:20 +00009464
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009465/*
9466 * Init the chartab used for spelling for ASCII.
9467 * EBCDIC is not supported!
9468 */
9469 static void
9470clear_spell_chartab(sp)
9471 spelltab_T *sp;
9472{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009473 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009474
9475 /* Init everything to FALSE. */
9476 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
9477 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
9478 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009479 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009480 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009481 sp->st_upper[i] = i;
9482 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009483
9484 /* We include digits. A word shouldn't start with a digit, but handling
9485 * that is done separately. */
9486 for (i = '0'; i <= '9'; ++i)
9487 sp->st_isw[i] = TRUE;
9488 for (i = 'A'; i <= 'Z'; ++i)
9489 {
9490 sp->st_isw[i] = TRUE;
9491 sp->st_isu[i] = TRUE;
9492 sp->st_fold[i] = i + 0x20;
9493 }
9494 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009495 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009496 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009497 sp->st_upper[i] = i - 0x20;
9498 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009499}
9500
9501/*
9502 * Init the chartab used for spelling. Only depends on 'encoding'.
9503 * Called once while starting up and when 'encoding' changes.
9504 * The default is to use isalpha(), but the spell file should define the word
9505 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009506 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009507 */
9508 void
9509init_spell_chartab()
9510{
9511 int i;
9512
9513 did_set_spelltab = FALSE;
9514 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009515#ifdef FEAT_MBYTE
9516 if (enc_dbcs)
9517 {
9518 /* DBCS: assume double-wide characters are word characters. */
9519 for (i = 128; i <= 255; ++i)
9520 if (MB_BYTE2LEN(i) == 2)
9521 spelltab.st_isw[i] = TRUE;
9522 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009523 else if (enc_utf8)
9524 {
9525 for (i = 128; i < 256; ++i)
9526 {
9527 spelltab.st_isu[i] = utf_isupper(i);
9528 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
9529 spelltab.st_fold[i] = utf_fold(i);
9530 spelltab.st_upper[i] = utf_toupper(i);
9531 }
9532 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009533 else
9534#endif
9535 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009536 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009537 for (i = 128; i < 256; ++i)
9538 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009539 if (MB_ISUPPER(i))
9540 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009541 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009542 spelltab.st_isu[i] = TRUE;
9543 spelltab.st_fold[i] = MB_TOLOWER(i);
9544 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009545 else if (MB_ISLOWER(i))
9546 {
9547 spelltab.st_isw[i] = TRUE;
9548 spelltab.st_upper[i] = MB_TOUPPER(i);
9549 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009550 }
9551 }
9552}
9553
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009554/*
9555 * Set the spell character tables from strings in the affix file.
9556 */
9557 static int
9558set_spell_chartab(fol, low, upp)
9559 char_u *fol;
9560 char_u *low;
9561 char_u *upp;
9562{
9563 /* We build the new tables here first, so that we can compare with the
9564 * previous one. */
9565 spelltab_T new_st;
9566 char_u *pf = fol, *pl = low, *pu = upp;
9567 int f, l, u;
9568
9569 clear_spell_chartab(&new_st);
9570
9571 while (*pf != NUL)
9572 {
9573 if (*pl == NUL || *pu == NUL)
9574 {
9575 EMSG(_(e_affform));
9576 return FAIL;
9577 }
9578#ifdef FEAT_MBYTE
9579 f = mb_ptr2char_adv(&pf);
9580 l = mb_ptr2char_adv(&pl);
9581 u = mb_ptr2char_adv(&pu);
9582#else
9583 f = *pf++;
9584 l = *pl++;
9585 u = *pu++;
9586#endif
9587 /* Every character that appears is a word character. */
9588 if (f < 256)
9589 new_st.st_isw[f] = TRUE;
9590 if (l < 256)
9591 new_st.st_isw[l] = TRUE;
9592 if (u < 256)
9593 new_st.st_isw[u] = TRUE;
9594
9595 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9596 * case-folding */
9597 if (l < 256 && l != f)
9598 {
9599 if (f >= 256)
9600 {
9601 EMSG(_(e_affrange));
9602 return FAIL;
9603 }
9604 new_st.st_fold[l] = f;
9605 }
9606
9607 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009608 * case-folding, it's upper case and the "UPP" is the upper case of
9609 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009610 if (u < 256 && u != f)
9611 {
9612 if (f >= 256)
9613 {
9614 EMSG(_(e_affrange));
9615 return FAIL;
9616 }
9617 new_st.st_fold[u] = f;
9618 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009619 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009620 }
9621 }
9622
9623 if (*pl != NUL || *pu != NUL)
9624 {
9625 EMSG(_(e_affform));
9626 return FAIL;
9627 }
9628
9629 return set_spell_finish(&new_st);
9630}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009631
9632/*
9633 * Set the spell character tables from strings in the .spl file.
9634 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009635 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009636set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009637 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009638 int cnt; /* length of "flags" */
9639 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009640{
9641 /* We build the new tables here first, so that we can compare with the
9642 * previous one. */
9643 spelltab_T new_st;
9644 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009645 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009646 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009647
9648 clear_spell_chartab(&new_st);
9649
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009650 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009651 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009652 if (i < cnt)
9653 {
9654 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
9655 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
9656 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009657
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009658 if (*p != NUL)
9659 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009660#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009661 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009662#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009663 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009664#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009665 new_st.st_fold[i + 128] = c;
9666 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
9667 new_st.st_upper[c] = i + 128;
9668 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009669 }
9670
Bram Moolenaar5195e452005-08-19 20:32:47 +00009671 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009672}
9673
9674 static int
9675set_spell_finish(new_st)
9676 spelltab_T *new_st;
9677{
9678 int i;
9679
9680 if (did_set_spelltab)
9681 {
9682 /* check that it's the same table */
9683 for (i = 0; i < 256; ++i)
9684 {
9685 if (spelltab.st_isw[i] != new_st->st_isw[i]
9686 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009687 || spelltab.st_fold[i] != new_st->st_fold[i]
9688 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009689 {
9690 EMSG(_("E763: Word characters differ between spell files"));
9691 return FAIL;
9692 }
9693 }
9694 }
9695 else
9696 {
9697 /* copy the new spelltab into the one being used */
9698 spelltab = *new_st;
9699 did_set_spelltab = TRUE;
9700 }
9701
9702 return OK;
9703}
9704
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009705/*
Bram Moolenaarea408852005-06-25 22:49:46 +00009706 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009707 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00009708 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009709 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00009710 */
9711 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009712spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00009713 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009714 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00009715{
Bram Moolenaarea408852005-06-25 22:49:46 +00009716#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009717 char_u *s;
9718 int l;
9719 int c;
9720
9721 if (has_mbyte)
9722 {
9723 l = MB_BYTE2LEN(*p);
9724 s = p;
9725 if (l == 1)
9726 {
9727 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009728 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009729 {
9730 s = p + 1; /* skip a mid-word character */
9731 l = MB_BYTE2LEN(*s);
9732 }
9733 }
9734 else
9735 {
9736 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009737 if (c < 256 ? buf->b_spell_ismw[c]
9738 : (buf->b_spell_ismw_mb != NULL
9739 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009740 {
9741 s = p + l;
9742 l = MB_BYTE2LEN(*s);
9743 }
9744 }
9745
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009746 c = mb_ptr2char(s);
9747 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009748 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009749 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009750 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009751#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009752
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009753 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
9754}
9755
9756/*
9757 * Return TRUE if "p" points to a word character.
9758 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9759 */
9760 static int
9761spell_iswordp_nmw(p)
9762 char_u *p;
9763{
9764#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009765 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009766
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009767 if (has_mbyte)
9768 {
9769 c = mb_ptr2char(p);
9770 if (c > 255)
9771 return mb_get_class(p) >= 2;
9772 return spelltab.st_isw[c];
9773 }
9774#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009775 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00009776}
9777
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009778#ifdef FEAT_MBYTE
9779/*
9780 * Return TRUE if "p" points to a word character.
9781 * Wide version of spell_iswordp().
9782 */
9783 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009784spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009785 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009786 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009787{
9788 int *s;
9789
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009790 if (*p < 256 ? buf->b_spell_ismw[*p]
9791 : (buf->b_spell_ismw_mb != NULL
9792 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009793 s = p + 1;
9794 else
9795 s = p;
9796
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009797 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009798 {
9799 if (enc_utf8)
9800 return utf_class(*s) >= 2;
9801 if (enc_dbcs)
9802 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
9803 return 0;
9804 }
9805 return spelltab.st_isw[*s];
9806}
9807#endif
9808
Bram Moolenaarea408852005-06-25 22:49:46 +00009809/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009810 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009811 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009812 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009813 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009814write_spell_prefcond(fd, gap)
9815 FILE *fd;
9816 garray_T *gap;
9817{
9818 int i;
9819 char_u *p;
9820 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009821 int totlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009822
Bram Moolenaar5195e452005-08-19 20:32:47 +00009823 if (fd != NULL)
9824 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
9825
9826 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009827
9828 for (i = 0; i < gap->ga_len; ++i)
9829 {
9830 /* <prefcond> : <condlen> <condstr> */
9831 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009832 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009833 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009834 len = (int)STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009835 if (fd != NULL)
9836 {
9837 fputc(len, fd);
9838 fwrite(p, (size_t)len, (size_t)1, fd);
9839 }
9840 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009841 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00009842 else if (fd != NULL)
9843 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009844 }
9845
Bram Moolenaar5195e452005-08-19 20:32:47 +00009846 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009847}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009848
9849/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009850 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
9851 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009852 * When using a multi-byte 'encoding' the length may change!
9853 * Returns FAIL when something wrong.
9854 */
9855 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009856spell_casefold(str, len, buf, buflen)
9857 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009858 int len;
9859 char_u *buf;
9860 int buflen;
9861{
9862 int i;
9863
9864 if (len >= buflen)
9865 {
9866 buf[0] = NUL;
9867 return FAIL; /* result will not fit */
9868 }
9869
9870#ifdef FEAT_MBYTE
9871 if (has_mbyte)
9872 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009873 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009874 char_u *p;
9875 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009876
9877 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009878 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009879 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009880 if (outi + MB_MAXBYTES > buflen)
9881 {
9882 buf[outi] = NUL;
9883 return FAIL;
9884 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009885 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009886 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009887 }
9888 buf[outi] = NUL;
9889 }
9890 else
9891#endif
9892 {
9893 /* Be quick for non-multibyte encodings. */
9894 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009895 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009896 buf[i] = NUL;
9897 }
9898
9899 return OK;
9900}
9901
Bram Moolenaar4770d092006-01-12 23:22:24 +00009902/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009903#define SPS_BEST 1
9904#define SPS_FAST 2
9905#define SPS_DOUBLE 4
9906
Bram Moolenaar4770d092006-01-12 23:22:24 +00009907static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
9908static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009909
9910/*
9911 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009912 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009913 */
9914 int
9915spell_check_sps()
9916{
9917 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009918 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009919 char_u buf[MAXPATHL];
9920 int f;
9921
9922 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009923 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009924
9925 for (p = p_sps; *p != NUL; )
9926 {
9927 copy_option_part(&p, buf, MAXPATHL, ",");
9928
9929 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009930 if (VIM_ISDIGIT(*buf))
9931 {
9932 s = buf;
9933 sps_limit = getdigits(&s);
9934 if (*s != NUL && !VIM_ISDIGIT(*s))
9935 f = -1;
9936 }
9937 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009938 f = SPS_BEST;
9939 else if (STRCMP(buf, "fast") == 0)
9940 f = SPS_FAST;
9941 else if (STRCMP(buf, "double") == 0)
9942 f = SPS_DOUBLE;
9943 else if (STRNCMP(buf, "expr:", 5) != 0
9944 && STRNCMP(buf, "file:", 5) != 0)
9945 f = -1;
9946
9947 if (f == -1 || (sps_flags != 0 && f != 0))
9948 {
9949 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009950 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009951 return FAIL;
9952 }
9953 if (f != 0)
9954 sps_flags = f;
9955 }
9956
9957 if (sps_flags == 0)
9958 sps_flags = SPS_BEST;
9959
9960 return OK;
9961}
9962
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009963/*
9964 * "z?": Find badly spelled word under or after the cursor.
9965 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009966 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00009967 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009968 */
9969 void
Bram Moolenaard12a1322005-08-21 22:08:24 +00009970spell_suggest(count)
9971 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009972{
9973 char_u *line;
9974 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009975 char_u wcopy[MAXWLEN + 2];
9976 char_u *p;
9977 int i;
9978 int c;
9979 suginfo_T sug;
9980 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009981 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009982 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009983 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009984 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009985 int badlen = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009986
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009987 if (no_spell_checking(curwin))
9988 return;
9989
9990#ifdef FEAT_VISUAL
9991 if (VIsual_active)
9992 {
9993 /* Use the Visually selected text as the bad word. But reject
9994 * a multi-line selection. */
9995 if (curwin->w_cursor.lnum != VIsual.lnum)
9996 {
9997 vim_beep();
9998 return;
9999 }
10000 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
10001 if (badlen < 0)
10002 badlen = -badlen;
10003 else
10004 curwin->w_cursor.col = VIsual.col;
10005 ++badlen;
10006 end_visual_mode();
10007 }
10008 else
10009#endif
10010 /* Find the start of the badly spelled word. */
10011 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +000010012 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010013 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010014 /* No bad word or it starts after the cursor: use the word under the
10015 * cursor. */
10016 curwin->w_cursor = prev_cursor;
10017 line = ml_get_curline();
10018 p = line + curwin->w_cursor.col;
10019 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010020 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010021 mb_ptr_back(line, p);
10022 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010023 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010024 mb_ptr_adv(p);
10025
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010026 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010027 {
10028 beep_flush();
10029 return;
10030 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010031 curwin->w_cursor.col = (colnr_T)(p - line);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010032 }
10033
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010034 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010035
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010036 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010037 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010038
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010039 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010040
Bram Moolenaar5195e452005-08-19 20:32:47 +000010041 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
10042 * 'spellsuggest', whatever is smaller. */
10043 if (sps_limit > (int)Rows - 2)
10044 limit = (int)Rows - 2;
10045 else
10046 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010047 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010048 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010049
10050 if (sug.su_ga.ga_len == 0)
10051 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +000010052 else if (count > 0)
10053 {
10054 if (count > sug.su_ga.ga_len)
10055 smsg((char_u *)_("Sorry, only %ld suggestions"),
10056 (long)sug.su_ga.ga_len);
10057 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010058 else
10059 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010060 vim_free(repl_from);
10061 repl_from = NULL;
10062 vim_free(repl_to);
10063 repl_to = NULL;
10064
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010065#ifdef FEAT_RIGHTLEFT
10066 /* When 'rightleft' is set the list is drawn right-left. */
10067 cmdmsg_rl = curwin->w_p_rl;
10068 if (cmdmsg_rl)
10069 msg_col = Columns - 1;
10070#endif
10071
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010072 /* List the suggestions. */
10073 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010074 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010075 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
10076 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010077#ifdef FEAT_RIGHTLEFT
10078 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
10079 {
10080 /* And now the rabbit from the high hat: Avoid showing the
10081 * untranslated message rightleft. */
10082 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
10083 sug.su_badlen, sug.su_badptr);
10084 }
10085#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010086 msg_puts(IObuff);
10087 msg_clr_eos();
10088 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +000010089
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010090 msg_scroll = TRUE;
10091 for (i = 0; i < sug.su_ga.ga_len; ++i)
10092 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010093 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010094
10095 /* The suggested word may replace only part of the bad word, add
10096 * the not replaced part. */
10097 STRCPY(wcopy, stp->st_word);
10098 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010099 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010100 sug.su_badptr + stp->st_orglen,
10101 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010102 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
10103#ifdef FEAT_RIGHTLEFT
10104 if (cmdmsg_rl)
10105 rl_mirror(IObuff);
10106#endif
10107 msg_puts(IObuff);
10108
10109 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010110 msg_puts(IObuff);
10111
10112 /* The word may replace more than "su_badlen". */
10113 if (sug.su_badlen < stp->st_orglen)
10114 {
10115 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
10116 stp->st_orglen, sug.su_badptr);
10117 msg_puts(IObuff);
10118 }
10119
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010120 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010121 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010122 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010123 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010124 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010125 stp->st_salscore ? "s " : "",
10126 stp->st_score, stp->st_altscore);
10127 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010128 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +000010129 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010130#ifdef FEAT_RIGHTLEFT
10131 if (cmdmsg_rl)
10132 /* Mirror the numbers, but keep the leading space. */
10133 rl_mirror(IObuff + 1);
10134#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +000010135 msg_advance(30);
10136 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010137 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010138 msg_putchar('\n');
10139 }
10140
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010141#ifdef FEAT_RIGHTLEFT
10142 cmdmsg_rl = FALSE;
10143 msg_col = 0;
10144#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010145 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010146 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010147 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +000010148 selected -= lines_left;
Bram Moolenaar0fd92892006-03-09 22:27:48 +000010149 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010150 }
10151
Bram Moolenaard12a1322005-08-21 22:08:24 +000010152 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
10153 {
10154 /* Save the from and to text for :spellrepall. */
10155 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000010156 if (sug.su_badlen > stp->st_orglen)
10157 {
10158 /* Replacing less than "su_badlen", append the remainder to
10159 * repl_to. */
10160 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
10161 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
10162 sug.su_badlen - stp->st_orglen,
10163 sug.su_badptr + stp->st_orglen);
10164 repl_to = vim_strsave(IObuff);
10165 }
10166 else
10167 {
10168 /* Replacing su_badlen or more, use the whole word. */
10169 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
10170 repl_to = vim_strsave(stp->st_word);
10171 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000010172
10173 /* Replace the word. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010174 p = alloc((unsigned)STRLEN(line) - stp->st_orglen + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010175 if (p != NULL)
10176 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010177 c = (int)(sug.su_badptr - line);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010178 mch_memmove(p, line, c);
10179 STRCPY(p + c, stp->st_word);
10180 STRCAT(p, sug.su_badptr + stp->st_orglen);
10181 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10182 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010183
10184 /* For redo we use a change-word command. */
10185 ResetRedobuff();
10186 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +000010187 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010188 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010189 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010190
10191 /* After this "p" may be invalid. */
10192 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010193 }
10194 }
10195 else
10196 curwin->w_cursor = prev_cursor;
10197
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010198 spell_find_cleanup(&sug);
10199}
10200
10201/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010202 * Check if the word at line "lnum" column "col" is required to start with a
10203 * capital. This uses 'spellcapcheck' of the current buffer.
10204 */
10205 static int
10206check_need_cap(lnum, col)
10207 linenr_T lnum;
10208 colnr_T col;
10209{
10210 int need_cap = FALSE;
10211 char_u *line;
10212 char_u *line_copy = NULL;
10213 char_u *p;
10214 colnr_T endcol;
10215 regmatch_T regmatch;
10216
10217 if (curbuf->b_cap_prog == NULL)
10218 return FALSE;
10219
10220 line = ml_get_curline();
10221 endcol = 0;
10222 if ((int)(skipwhite(line) - line) >= (int)col)
10223 {
10224 /* At start of line, check if previous line is empty or sentence
10225 * ends there. */
10226 if (lnum == 1)
10227 need_cap = TRUE;
10228 else
10229 {
10230 line = ml_get(lnum - 1);
10231 if (*skipwhite(line) == NUL)
10232 need_cap = TRUE;
10233 else
10234 {
10235 /* Append a space in place of the line break. */
10236 line_copy = concat_str(line, (char_u *)" ");
10237 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010238 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010239 }
10240 }
10241 }
10242 else
10243 endcol = col;
10244
10245 if (endcol > 0)
10246 {
10247 /* Check if sentence ends before the bad word. */
10248 regmatch.regprog = curbuf->b_cap_prog;
10249 regmatch.rm_ic = FALSE;
10250 p = line + endcol;
10251 for (;;)
10252 {
10253 mb_ptr_back(line, p);
10254 if (p == line || spell_iswordp_nmw(p))
10255 break;
10256 if (vim_regexec(&regmatch, p, 0)
10257 && regmatch.endp[0] == line + endcol)
10258 {
10259 need_cap = TRUE;
10260 break;
10261 }
10262 }
10263 }
10264
10265 vim_free(line_copy);
10266
10267 return need_cap;
10268}
10269
10270
10271/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010272 * ":spellrepall"
10273 */
10274/*ARGSUSED*/
10275 void
10276ex_spellrepall(eap)
10277 exarg_T *eap;
10278{
10279 pos_T pos = curwin->w_cursor;
10280 char_u *frompat;
10281 int addlen;
10282 char_u *line;
10283 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010284 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010285 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010286
10287 if (repl_from == NULL || repl_to == NULL)
10288 {
10289 EMSG(_("E752: No previous spell replacement"));
10290 return;
10291 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010292 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010293
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010294 frompat = alloc((unsigned)STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010295 if (frompat == NULL)
10296 return;
10297 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
10298 p_ws = FALSE;
10299
Bram Moolenaar5195e452005-08-19 20:32:47 +000010300 sub_nsubs = 0;
10301 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010302 curwin->w_cursor.lnum = 0;
10303 while (!got_int)
10304 {
10305 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
10306 || u_save_cursor() == FAIL)
10307 break;
10308
10309 /* Only replace when the right word isn't there yet. This happens
10310 * when changing "etc" to "etc.". */
10311 line = ml_get_curline();
10312 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
10313 repl_to, STRLEN(repl_to)) != 0)
10314 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010315 p = alloc((unsigned)STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010316 if (p == NULL)
10317 break;
10318 mch_memmove(p, line, curwin->w_cursor.col);
10319 STRCPY(p + curwin->w_cursor.col, repl_to);
10320 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
10321 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10322 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010323
10324 if (curwin->w_cursor.lnum != prev_lnum)
10325 {
10326 ++sub_nlines;
10327 prev_lnum = curwin->w_cursor.lnum;
10328 }
10329 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010330 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010331 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010332 }
10333
10334 p_ws = save_ws;
10335 curwin->w_cursor = pos;
10336 vim_free(frompat);
10337
Bram Moolenaar5195e452005-08-19 20:32:47 +000010338 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010339 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010340 else
10341 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010342}
10343
10344/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010345 * Find spell suggestions for "word". Return them in the growarray "*gap" as
10346 * a list of allocated strings.
10347 */
10348 void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010349spell_suggest_list(gap, word, maxcount, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010350 garray_T *gap;
10351 char_u *word;
10352 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010353 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010354 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010355{
10356 suginfo_T sug;
10357 int i;
10358 suggest_T *stp;
10359 char_u *wcopy;
10360
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010361 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010362
10363 /* Make room in "gap". */
10364 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010365 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010366 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010367 for (i = 0; i < sug.su_ga.ga_len; ++i)
10368 {
10369 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010370
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010371 /* The suggested word may replace only part of "word", add the not
10372 * replaced part. */
10373 wcopy = alloc(stp->st_wordlen
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010374 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010375 if (wcopy == NULL)
10376 break;
10377 STRCPY(wcopy, stp->st_word);
10378 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
10379 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
10380 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010381 }
10382
10383 spell_find_cleanup(&sug);
10384}
10385
10386/*
10387 * Find spell suggestions for the word at the start of "badptr".
10388 * Return the suggestions in "su->su_ga".
10389 * The maximum number of suggestions is "maxcount".
10390 * Note: does use info for the current window.
10391 * This is based on the mechanisms of Aspell, but completely reimplemented.
10392 */
10393 static void
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010394spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010395 char_u *badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010396 int badlen; /* length of bad word or 0 if unknown */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010397 suginfo_T *su;
10398 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +000010399 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010400 int need_cap; /* word should start with capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010401 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010402{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010403 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010404 char_u buf[MAXPATHL];
10405 char_u *p;
10406 int do_combine = FALSE;
10407 char_u *sps_copy;
10408#ifdef FEAT_EVAL
10409 static int expr_busy = FALSE;
10410#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010411 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010412 int i;
10413 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010414
10415 /*
10416 * Set the info in "*su".
10417 */
10418 vim_memset(su, 0, sizeof(suginfo_T));
10419 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
10420 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010421 if (*badptr == NUL)
10422 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010423 hash_init(&su->su_banned);
10424
10425 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010426 if (badlen != 0)
10427 su->su_badlen = badlen;
10428 else
10429 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010430 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010431 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010432
10433 if (su->su_badlen >= MAXWLEN)
10434 su->su_badlen = MAXWLEN - 1; /* just in case */
10435 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
10436 (void)spell_casefold(su->su_badptr, su->su_badlen,
10437 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010438 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010439 su->su_badflags = badword_captype(su->su_badptr,
10440 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010441 if (need_cap)
10442 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010443
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010444 /* Find the default language for sound folding. We simply use the first
10445 * one in 'spelllang' that supports sound folding. That's good for when
10446 * using multiple files for one language, it's not that bad when mixing
10447 * languages (e.g., "pl,en"). */
10448 for (i = 0; i < curbuf->b_langp.ga_len; ++i)
10449 {
10450 lp = LANGP_ENTRY(curbuf->b_langp, i);
10451 if (lp->lp_sallang != NULL)
10452 {
10453 su->su_sallang = lp->lp_sallang;
10454 break;
10455 }
10456 }
10457
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010458 /* Soundfold the bad word with the default sound folding, so that we don't
10459 * have to do this many times. */
10460 if (su->su_sallang != NULL)
10461 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
10462 su->su_sal_badword);
10463
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010464 /* If the word is not capitalised and spell_check() doesn't consider the
10465 * word to be bad then it might need to be capitalised. Add a suggestion
10466 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010467 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010468 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010469 {
10470 make_case_word(su->su_badword, buf, WF_ONECAP);
10471 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010472 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010473 }
10474
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010475 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +000010476 if (banbadword)
10477 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010478
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010479 /* Make a copy of 'spellsuggest', because the expression may change it. */
10480 sps_copy = vim_strsave(p_sps);
10481 if (sps_copy == NULL)
10482 return;
10483
10484 /* Loop over the items in 'spellsuggest'. */
10485 for (p = sps_copy; *p != NUL; )
10486 {
10487 copy_option_part(&p, buf, MAXPATHL, ",");
10488
10489 if (STRNCMP(buf, "expr:", 5) == 0)
10490 {
10491#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010492 /* Evaluate an expression. Skip this when called recursively,
10493 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010494 if (!expr_busy)
10495 {
10496 expr_busy = TRUE;
10497 spell_suggest_expr(su, buf + 5);
10498 expr_busy = FALSE;
10499 }
10500#endif
10501 }
10502 else if (STRNCMP(buf, "file:", 5) == 0)
10503 /* Use list of suggestions in a file. */
10504 spell_suggest_file(su, buf + 5);
10505 else
10506 {
10507 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010508 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010509 if (sps_flags & SPS_DOUBLE)
10510 do_combine = TRUE;
10511 }
10512 }
10513
10514 vim_free(sps_copy);
10515
10516 if (do_combine)
10517 /* Combine the two list of suggestions. This must be done last,
10518 * because sorting changes the order again. */
10519 score_combine(su);
10520}
10521
10522#ifdef FEAT_EVAL
10523/*
10524 * Find suggestions by evaluating expression "expr".
10525 */
10526 static void
10527spell_suggest_expr(su, expr)
10528 suginfo_T *su;
10529 char_u *expr;
10530{
10531 list_T *list;
10532 listitem_T *li;
10533 int score;
10534 char_u *p;
10535
10536 /* The work is split up in a few parts to avoid having to export
10537 * suginfo_T.
10538 * First evaluate the expression and get the resulting list. */
10539 list = eval_spell_expr(su->su_badword, expr);
10540 if (list != NULL)
10541 {
10542 /* Loop over the items in the list. */
10543 for (li = list->lv_first; li != NULL; li = li->li_next)
10544 if (li->li_tv.v_type == VAR_LIST)
10545 {
10546 /* Get the word and the score from the items. */
10547 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010548 if (score >= 0 && score <= su->su_maxscore)
10549 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10550 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010551 }
10552 list_unref(list);
10553 }
10554
Bram Moolenaar4770d092006-01-12 23:22:24 +000010555 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10556 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010557 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10558}
10559#endif
10560
10561/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010562 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010563 */
10564 static void
10565spell_suggest_file(su, fname)
10566 suginfo_T *su;
10567 char_u *fname;
10568{
10569 FILE *fd;
10570 char_u line[MAXWLEN * 2];
10571 char_u *p;
10572 int len;
10573 char_u cword[MAXWLEN];
10574
10575 /* Open the file. */
10576 fd = mch_fopen((char *)fname, "r");
10577 if (fd == NULL)
10578 {
10579 EMSG2(_(e_notopen), fname);
10580 return;
10581 }
10582
10583 /* Read it line by line. */
10584 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
10585 {
10586 line_breakcheck();
10587
10588 p = vim_strchr(line, '/');
10589 if (p == NULL)
10590 continue; /* No Tab found, just skip the line. */
10591 *p++ = NUL;
10592 if (STRICMP(su->su_badword, line) == 0)
10593 {
10594 /* Match! Isolate the good word, until CR or NL. */
10595 for (len = 0; p[len] >= ' '; ++len)
10596 ;
10597 p[len] = NUL;
10598
10599 /* If the suggestion doesn't have specific case duplicate the case
10600 * of the bad word. */
10601 if (captype(p, NULL) == 0)
10602 {
10603 make_case_word(p, cword, su->su_badflags);
10604 p = cword;
10605 }
10606
10607 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010608 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010609 }
10610 }
10611
10612 fclose(fd);
10613
Bram Moolenaar4770d092006-01-12 23:22:24 +000010614 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10615 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010616 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10617}
10618
10619/*
10620 * Find suggestions for the internal method indicated by "sps_flags".
10621 */
10622 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010623spell_suggest_intern(su, interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010624 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010625 int interactive;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010626{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010627 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010628 * Load the .sug file(s) that are available and not done yet.
10629 */
10630 suggest_load_files();
10631
10632 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010633 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010634 *
10635 * Set a maximum score to limit the combination of operations that is
10636 * tried.
10637 */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010638 suggest_try_special(su);
10639
10640 /*
10641 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10642 * from the .aff file and inserting a space (split the word).
10643 */
10644 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010645
10646 /* For the resulting top-scorers compute the sound-a-like score. */
10647 if (sps_flags & SPS_DOUBLE)
10648 score_comp_sal(su);
10649
10650 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010651 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010652 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010653 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010654 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010655 if (sps_flags & SPS_BEST)
10656 /* Adjust the word score for the suggestions found so far for how
10657 * they sounds like. */
10658 rescore_suggestions(su);
10659
10660 /*
10661 * While going throught the soundfold tree "su_maxscore" is the score
10662 * for the soundfold word, limits the changes that are being tried,
10663 * and "su_sfmaxscore" the rescored score, which is set by
10664 * cleanup_suggestions().
10665 * First find words with a small edit distance, because this is much
10666 * faster and often already finds the top-N suggestions. If we didn't
10667 * find many suggestions try again with a higher edit distance.
10668 * "sl_sounddone" is used to avoid doing the same word twice.
10669 */
10670 suggest_try_soundalike_prep();
10671 su->su_maxscore = SCORE_SFMAX1;
10672 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010673 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010674 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10675 {
10676 /* We didn't find enough matches, try again, allowing more
10677 * changes to the soundfold word. */
10678 su->su_maxscore = SCORE_SFMAX2;
10679 suggest_try_soundalike(su);
10680 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10681 {
10682 /* Still didn't find enough matches, try again, allowing even
10683 * more changes to the soundfold word. */
10684 su->su_maxscore = SCORE_SFMAX3;
10685 suggest_try_soundalike(su);
10686 }
10687 }
10688 su->su_maxscore = su->su_sfmaxscore;
10689 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010690 }
10691
Bram Moolenaar4770d092006-01-12 23:22:24 +000010692 /* When CTRL-C was hit while searching do show the results. Only clear
10693 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010694 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +000010695 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010696 {
10697 (void)vgetc();
10698 got_int = FALSE;
10699 }
10700
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010701 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010702 {
10703 if (sps_flags & SPS_BEST)
10704 /* Adjust the word score for how it sounds like. */
10705 rescore_suggestions(su);
10706
Bram Moolenaar4770d092006-01-12 23:22:24 +000010707 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10708 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010709 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010710 }
10711}
10712
10713/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010714 * Load the .sug files for languages that have one and weren't loaded yet.
10715 */
10716 static void
10717suggest_load_files()
10718{
10719 langp_T *lp;
10720 int lpi;
10721 slang_T *slang;
10722 char_u *dotp;
10723 FILE *fd;
10724 char_u buf[MAXWLEN];
10725 int i;
10726 time_t timestamp;
10727 int wcount;
10728 int wordnr;
10729 garray_T ga;
10730 int c;
10731
10732 /* Do this for all languages that support sound folding. */
10733 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
10734 {
10735 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
10736 slang = lp->lp_slang;
10737 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10738 {
10739 /* Change ".spl" to ".sug" and open the file. When the file isn't
10740 * found silently skip it. Do set "sl_sugloaded" so that we
10741 * don't try again and again. */
10742 slang->sl_sugloaded = TRUE;
10743
10744 dotp = vim_strrchr(slang->sl_fname, '.');
10745 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10746 continue;
10747 STRCPY(dotp, ".sug");
Bram Moolenaar5555acc2006-04-07 21:33:12 +000010748 fd = mch_fopen((char *)slang->sl_fname, "r");
Bram Moolenaar4770d092006-01-12 23:22:24 +000010749 if (fd == NULL)
10750 goto nextone;
10751
10752 /*
10753 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10754 */
10755 for (i = 0; i < VIMSUGMAGICL; ++i)
10756 buf[i] = getc(fd); /* <fileID> */
10757 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10758 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010759 EMSG2(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010760 slang->sl_fname);
10761 goto nextone;
10762 }
10763 c = getc(fd); /* <versionnr> */
10764 if (c < VIMSUGVERSION)
10765 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010766 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010767 slang->sl_fname);
10768 goto nextone;
10769 }
10770 else if (c > VIMSUGVERSION)
10771 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010772 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010773 slang->sl_fname);
10774 goto nextone;
10775 }
10776
10777 /* Check the timestamp, it must be exactly the same as the one in
10778 * the .spl file. Otherwise the word numbers won't match. */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010779 timestamp = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010780 if (timestamp != slang->sl_sugtime)
10781 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010782 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010783 slang->sl_fname);
10784 goto nextone;
10785 }
10786
10787 /*
10788 * <SUGWORDTREE>: <wordtree>
10789 * Read the trie with the soundfolded words.
10790 */
10791 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
10792 FALSE, 0) != 0)
10793 {
10794someerror:
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010795 EMSG2(_("E782: error while reading .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010796 slang->sl_fname);
10797 slang_clear_sug(slang);
10798 goto nextone;
10799 }
10800
10801 /*
10802 * <SUGTABLE>: <sugwcount> <sugline> ...
10803 *
10804 * Read the table with word numbers. We use a file buffer for
10805 * this, because it's so much like a file with lines. Makes it
10806 * possible to swap the info and save on memory use.
10807 */
10808 slang->sl_sugbuf = open_spellbuf();
10809 if (slang->sl_sugbuf == NULL)
10810 goto someerror;
10811 /* <sugwcount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010812 wcount = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010813 if (wcount < 0)
10814 goto someerror;
10815
10816 /* Read all the wordnr lists into the buffer, one NUL terminated
10817 * list per line. */
10818 ga_init2(&ga, 1, 100);
10819 for (wordnr = 0; wordnr < wcount; ++wordnr)
10820 {
10821 ga.ga_len = 0;
10822 for (;;)
10823 {
10824 c = getc(fd); /* <sugline> */
10825 if (c < 0 || ga_grow(&ga, 1) == FAIL)
10826 goto someerror;
10827 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
10828 if (c == NUL)
10829 break;
10830 }
10831 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
10832 ga.ga_data, ga.ga_len, TRUE) == FAIL)
10833 goto someerror;
10834 }
10835 ga_clear(&ga);
10836
10837 /*
10838 * Need to put word counts in the word tries, so that we can find
10839 * a word by its number.
10840 */
10841 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
10842 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
10843
10844nextone:
10845 if (fd != NULL)
10846 fclose(fd);
10847 STRCPY(dotp, ".spl");
10848 }
10849 }
10850}
10851
10852
10853/*
10854 * Fill in the wordcount fields for a trie.
10855 * Returns the total number of words.
10856 */
10857 static void
10858tree_count_words(byts, idxs)
10859 char_u *byts;
10860 idx_T *idxs;
10861{
10862 int depth;
10863 idx_T arridx[MAXWLEN];
10864 int curi[MAXWLEN];
10865 int c;
10866 idx_T n;
10867 int wordcount[MAXWLEN];
10868
10869 arridx[0] = 0;
10870 curi[0] = 1;
10871 wordcount[0] = 0;
10872 depth = 0;
10873 while (depth >= 0 && !got_int)
10874 {
10875 if (curi[depth] > byts[arridx[depth]])
10876 {
10877 /* Done all bytes at this node, go up one level. */
10878 idxs[arridx[depth]] = wordcount[depth];
10879 if (depth > 0)
10880 wordcount[depth - 1] += wordcount[depth];
10881
10882 --depth;
10883 fast_breakcheck();
10884 }
10885 else
10886 {
10887 /* Do one more byte at this node. */
10888 n = arridx[depth] + curi[depth];
10889 ++curi[depth];
10890
10891 c = byts[n];
10892 if (c == 0)
10893 {
10894 /* End of word, count it. */
10895 ++wordcount[depth];
10896
10897 /* Skip over any other NUL bytes (same word with different
10898 * flags). */
10899 while (byts[n + 1] == 0)
10900 {
10901 ++n;
10902 ++curi[depth];
10903 }
10904 }
10905 else
10906 {
10907 /* Normal char, go one level deeper to count the words. */
10908 ++depth;
10909 arridx[depth] = idxs[n];
10910 curi[depth] = 1;
10911 wordcount[depth] = 0;
10912 }
10913 }
10914 }
10915}
10916
10917/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010918 * Free the info put in "*su" by spell_find_suggest().
10919 */
10920 static void
10921spell_find_cleanup(su)
10922 suginfo_T *su;
10923{
10924 int i;
10925
10926 /* Free the suggestions. */
10927 for (i = 0; i < su->su_ga.ga_len; ++i)
10928 vim_free(SUG(su->su_ga, i).st_word);
10929 ga_clear(&su->su_ga);
10930 for (i = 0; i < su->su_sga.ga_len; ++i)
10931 vim_free(SUG(su->su_sga, i).st_word);
10932 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010933
10934 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010935 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010936}
10937
10938/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010939 * Make a copy of "word", with the first letter upper or lower cased, to
10940 * "wcopy[MAXWLEN]". "word" must not be empty.
10941 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010942 */
10943 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010944onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010945 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010946 char_u *wcopy;
10947 int upper; /* TRUE: first letter made upper case */
10948{
10949 char_u *p;
10950 int c;
10951 int l;
10952
10953 p = word;
10954#ifdef FEAT_MBYTE
10955 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010956 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010957 else
10958#endif
10959 c = *p++;
10960 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010961 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010962 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010963 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010964#ifdef FEAT_MBYTE
10965 if (has_mbyte)
10966 l = mb_char2bytes(c, wcopy);
10967 else
10968#endif
10969 {
10970 l = 1;
10971 wcopy[0] = c;
10972 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010973 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010974}
10975
10976/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010977 * Make a copy of "word" with all the letters upper cased into
10978 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010979 */
10980 static void
10981allcap_copy(word, wcopy)
10982 char_u *word;
10983 char_u *wcopy;
10984{
10985 char_u *s;
10986 char_u *d;
10987 int c;
10988
10989 d = wcopy;
10990 for (s = word; *s != NUL; )
10991 {
10992#ifdef FEAT_MBYTE
10993 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010994 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010995 else
10996#endif
10997 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +000010998
10999#ifdef FEAT_MBYTE
11000 /* We only change ß to SS when we are certain latin1 is used. It
11001 * would cause weird errors in other 8-bit encodings. */
11002 if (enc_latin1like && c == 0xdf)
11003 {
11004 c = 'S';
11005 if (d - wcopy >= MAXWLEN - 1)
11006 break;
11007 *d++ = c;
11008 }
11009 else
11010#endif
11011 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011012
11013#ifdef FEAT_MBYTE
11014 if (has_mbyte)
11015 {
11016 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
11017 break;
11018 d += mb_char2bytes(c, d);
11019 }
11020 else
11021#endif
11022 {
11023 if (d - wcopy >= MAXWLEN - 1)
11024 break;
11025 *d++ = c;
11026 }
11027 }
11028 *d = NUL;
11029}
11030
11031/*
Bram Moolenaar0c405862005-06-22 22:26:26 +000011032 * Try finding suggestions by recognizing specific situations.
11033 */
11034 static void
11035suggest_try_special(su)
11036 suginfo_T *su;
11037{
11038 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011039 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011040 int c;
11041 char_u word[MAXWLEN];
11042
11043 /*
11044 * Recognize a word that is repeated: "the the".
11045 */
11046 p = skiptowhite(su->su_fbadword);
11047 len = p - su->su_fbadword;
11048 p = skipwhite(p);
11049 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
11050 {
11051 /* Include badflags: if the badword is onecap or allcap
11052 * use that for the goodword too: "The the" -> "The". */
11053 c = su->su_fbadword[len];
11054 su->su_fbadword[len] = NUL;
11055 make_case_word(su->su_fbadword, word, su->su_badflags);
11056 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011057
11058 /* Give a soundalike score of 0, compute the score as if deleting one
11059 * character. */
11060 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011061 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011062 }
11063}
11064
11065/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011066 * Try finding suggestions by adding/removing/swapping letters.
11067 */
11068 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000011069suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011070 suginfo_T *su;
11071{
11072 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011073 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011074 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011075 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011076 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011077
11078 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +000011079 * to find matches (esp. REP items). Append some more text, changing
11080 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011081 STRCPY(fword, su->su_fbadword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011082 n = (int)STRLEN(fword);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011083 p = su->su_badptr + su->su_badlen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011084 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011085
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011086 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011087 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011088 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011089
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011090 /* If reloading a spell file fails it's still in the list but
11091 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011092 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011093 continue;
11094
Bram Moolenaar4770d092006-01-12 23:22:24 +000011095 /* Try it for this language. Will add possible suggestions. */
11096 suggest_trie_walk(su, lp, fword, FALSE);
11097 }
11098}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011099
Bram Moolenaar4770d092006-01-12 23:22:24 +000011100/* Check the maximum score, if we go over it we won't try this change. */
11101#define TRY_DEEPER(su, stack, depth, add) \
11102 (stack[depth].ts_score + (add) < su->su_maxscore)
11103
11104/*
11105 * Try finding suggestions by adding/removing/swapping letters.
11106 *
11107 * This uses a state machine. At each node in the tree we try various
11108 * operations. When trying if an operation works "depth" is increased and the
11109 * stack[] is used to store info. This allows combinations, thus insert one
11110 * character, replace one and delete another. The number of changes is
11111 * limited by su->su_maxscore.
11112 *
11113 * After implementing this I noticed an article by Kemal Oflazer that
11114 * describes something similar: "Error-tolerant Finite State Recognition with
11115 * Applications to Morphological Analysis and Spelling Correction" (1996).
11116 * The implementation in the article is simplified and requires a stack of
11117 * unknown depth. The implementation here only needs a stack depth equal to
11118 * the length of the word.
11119 *
11120 * This is also used for the sound-folded word, "soundfold" is TRUE then.
11121 * The mechanism is the same, but we find a match with a sound-folded word
11122 * that comes from one or more original words. Each of these words may be
11123 * added, this is done by add_sound_suggest().
11124 * Don't use:
11125 * the prefix tree or the keep-case tree
11126 * "su->su_badlen"
11127 * anything to do with upper and lower case
11128 * anything to do with word or non-word characters ("spell_iswordp()")
11129 * banned words
11130 * word flags (rare, region, compounding)
11131 * word splitting for now
11132 * "similar_chars()"
11133 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
11134 */
11135 static void
11136suggest_trie_walk(su, lp, fword, soundfold)
11137 suginfo_T *su;
11138 langp_T *lp;
11139 char_u *fword;
11140 int soundfold;
11141{
11142 char_u tword[MAXWLEN]; /* good word collected so far */
11143 trystate_T stack[MAXWLEN];
11144 char_u preword[MAXWLEN * 3]; /* word found with proper case;
11145 * concatanation of prefix compound
11146 * words and split word. NUL terminated
11147 * when going deeper but not when coming
11148 * back. */
11149 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
11150 trystate_T *sp;
11151 int newscore;
11152 int score;
11153 char_u *byts, *fbyts, *pbyts;
11154 idx_T *idxs, *fidxs, *pidxs;
11155 int depth;
11156 int c, c2, c3;
11157 int n = 0;
11158 int flags;
11159 garray_T *gap;
11160 idx_T arridx;
11161 int len;
11162 char_u *p;
11163 fromto_T *ftp;
11164 int fl = 0, tl;
11165 int repextra = 0; /* extra bytes in fword[] from REP item */
11166 slang_T *slang = lp->lp_slang;
11167 int fword_ends;
11168 int goodword_ends;
11169#ifdef DEBUG_TRIEWALK
11170 /* Stores the name of the change made at each level. */
11171 char_u changename[MAXWLEN][80];
11172#endif
11173 int breakcheckcount = 1000;
11174 int compound_ok;
11175
11176 /*
11177 * Go through the whole case-fold tree, try changes at each node.
11178 * "tword[]" contains the word collected from nodes in the tree.
11179 * "fword[]" the word we are trying to match with (initially the bad
11180 * word).
11181 */
11182 depth = 0;
11183 sp = &stack[0];
11184 vim_memset(sp, 0, sizeof(trystate_T));
11185 sp->ts_curi = 1;
11186
11187 if (soundfold)
11188 {
11189 /* Going through the soundfold tree. */
11190 byts = fbyts = slang->sl_sbyts;
11191 idxs = fidxs = slang->sl_sidxs;
11192 pbyts = NULL;
11193 pidxs = NULL;
11194 sp->ts_prefixdepth = PFD_NOPREFIX;
11195 sp->ts_state = STATE_START;
11196 }
11197 else
11198 {
Bram Moolenaarea424162005-06-16 21:51:00 +000011199 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011200 * When there are postponed prefixes we need to use these first. At
11201 * the end of the prefix we continue in the case-fold tree.
11202 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011203 fbyts = slang->sl_fbyts;
11204 fidxs = slang->sl_fidxs;
11205 pbyts = slang->sl_pbyts;
11206 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011207 if (pbyts != NULL)
11208 {
11209 byts = pbyts;
11210 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011211 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011212 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
11213 }
11214 else
11215 {
11216 byts = fbyts;
11217 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011218 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011219 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011220 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011221 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011222
Bram Moolenaar4770d092006-01-12 23:22:24 +000011223 /*
11224 * Loop to find all suggestions. At each round we either:
11225 * - For the current state try one operation, advance "ts_curi",
11226 * increase "depth".
11227 * - When a state is done go to the next, set "ts_state".
11228 * - When all states are tried decrease "depth".
11229 */
11230 while (depth >= 0 && !got_int)
11231 {
11232 sp = &stack[depth];
11233 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011234 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011235 case STATE_START:
11236 case STATE_NOPREFIX:
11237 /*
11238 * Start of node: Deal with NUL bytes, which means
11239 * tword[] may end here.
11240 */
11241 arridx = sp->ts_arridx; /* current node in the tree */
11242 len = byts[arridx]; /* bytes in this node */
11243 arridx += sp->ts_curi; /* index of current byte */
11244
11245 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011246 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011247 /* Skip over the NUL bytes, we use them later. */
11248 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
11249 ;
11250 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011251
Bram Moolenaar4770d092006-01-12 23:22:24 +000011252 /* Always past NUL bytes now. */
11253 n = (int)sp->ts_state;
11254 sp->ts_state = STATE_ENDNUL;
11255 sp->ts_save_badflags = su->su_badflags;
11256
11257 /* At end of a prefix or at start of prefixtree: check for
11258 * following word. */
11259 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011260 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011261 /* Set su->su_badflags to the caps type at this position.
11262 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000011263#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011264 if (has_mbyte)
11265 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
11266 else
Bram Moolenaar53805d12005-08-01 07:08:33 +000011267#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011268 n = sp->ts_fidx;
11269 flags = badword_captype(su->su_badptr, su->su_badptr + n);
11270 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011271 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011272#ifdef DEBUG_TRIEWALK
11273 sprintf(changename[depth], "prefix");
11274#endif
11275 go_deeper(stack, depth, 0);
11276 ++depth;
11277 sp = &stack[depth];
11278 sp->ts_prefixdepth = depth - 1;
11279 byts = fbyts;
11280 idxs = fidxs;
11281 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011282
Bram Moolenaar4770d092006-01-12 23:22:24 +000011283 /* Move the prefix to preword[] with the right case
11284 * and make find_keepcap_word() works. */
11285 tword[sp->ts_twordlen] = NUL;
11286 make_case_word(tword + sp->ts_splitoff,
11287 preword + sp->ts_prewordlen, flags);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011288 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011289 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011290 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011291 break;
11292 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011293
Bram Moolenaar4770d092006-01-12 23:22:24 +000011294 if (sp->ts_curi > len || byts[arridx] != 0)
11295 {
11296 /* Past bytes in node and/or past NUL bytes. */
11297 sp->ts_state = STATE_ENDNUL;
11298 sp->ts_save_badflags = su->su_badflags;
11299 break;
11300 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011301
Bram Moolenaar4770d092006-01-12 23:22:24 +000011302 /*
11303 * End of word in tree.
11304 */
11305 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011306
Bram Moolenaar4770d092006-01-12 23:22:24 +000011307 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011308
11309 /* Skip words with the NOSUGGEST flag. */
11310 if (flags & WF_NOSUGGEST)
11311 break;
11312
Bram Moolenaar4770d092006-01-12 23:22:24 +000011313 fword_ends = (fword[sp->ts_fidx] == NUL
11314 || (soundfold
11315 ? vim_iswhite(fword[sp->ts_fidx])
11316 : !spell_iswordp(fword + sp->ts_fidx, curbuf)));
11317 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011318
Bram Moolenaar4770d092006-01-12 23:22:24 +000011319 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +000011320 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011321 {
11322 /* There was a prefix before the word. Check that the prefix
11323 * can be used with this word. */
11324 /* Count the length of the NULs in the prefix. If there are
11325 * none this must be the first try without a prefix. */
11326 n = stack[sp->ts_prefixdepth].ts_arridx;
11327 len = pbyts[n++];
11328 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
11329 ;
11330 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011331 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011332 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011333 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011334 if (c == 0)
11335 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011336
Bram Moolenaar4770d092006-01-12 23:22:24 +000011337 /* Use the WF_RARE flag for a rare prefix. */
11338 if (c & WF_RAREPFX)
11339 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011340
Bram Moolenaar4770d092006-01-12 23:22:24 +000011341 /* Tricky: when checking for both prefix and compounding
11342 * we run into the prefix flag first.
11343 * Remember that it's OK, so that we accept the prefix
11344 * when arriving at a compound flag. */
11345 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011346 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011347 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011348
Bram Moolenaar4770d092006-01-12 23:22:24 +000011349 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
11350 * appending another compound word below. */
11351 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011352 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011353 goodword_ends = FALSE;
11354 else
11355 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011356
Bram Moolenaar4770d092006-01-12 23:22:24 +000011357 p = NULL;
11358 compound_ok = TRUE;
11359 if (sp->ts_complen > sp->ts_compsplit)
11360 {
11361 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +000011362 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011363 /* There was a word before this word. When there was no
11364 * change in this word (it was correct) add the first word
11365 * as a suggestion. If this word was corrected too, we
11366 * need to check if a correct word follows. */
11367 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +000011368 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +000011369 && STRNCMP(fword + sp->ts_splitfidx,
11370 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +000011371 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011372 {
11373 preword[sp->ts_prewordlen] = NUL;
11374 newscore = score_wordcount_adj(slang, sp->ts_score,
11375 preword + sp->ts_prewordlen,
11376 sp->ts_prewordlen > 0);
11377 /* Add the suggestion if the score isn't too bad. */
11378 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +000011379 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011380 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011381 newscore, 0, FALSE,
11382 lp->lp_sallang, FALSE);
11383 break;
Bram Moolenaar78622822005-08-23 21:00:13 +000011384 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011385 }
Bram Moolenaare52325c2005-08-22 22:54:29 +000011386 else
Bram Moolenaar0c405862005-06-22 22:26:26 +000011387 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011388 /* There was a compound word before this word. If this
11389 * word does not support compounding then give up
11390 * (splitting is tried for the word without compound
11391 * flag). */
11392 if (((unsigned)flags >> 24) == 0
11393 || sp->ts_twordlen - sp->ts_splitoff
11394 < slang->sl_compminlen)
11395 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011396#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011397 /* For multi-byte chars check character length against
11398 * COMPOUNDMIN. */
11399 if (has_mbyte
11400 && slang->sl_compminlen > 0
11401 && mb_charlen(tword + sp->ts_splitoff)
11402 < slang->sl_compminlen)
11403 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011404#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +000011405
Bram Moolenaar4770d092006-01-12 23:22:24 +000011406 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11407 compflags[sp->ts_complen + 1] = NUL;
11408 vim_strncpy(preword + sp->ts_prewordlen,
11409 tword + sp->ts_splitoff,
11410 sp->ts_twordlen - sp->ts_splitoff);
11411 p = preword;
11412 while (*skiptowhite(p) != NUL)
11413 p = skipwhite(skiptowhite(p));
11414 if (fword_ends && !can_compound(slang, p,
11415 compflags + sp->ts_compsplit))
11416 /* Compound is not allowed. But it may still be
11417 * possible if we add another (short) word. */
11418 compound_ok = FALSE;
11419
11420 /* Get pointer to last char of previous word. */
11421 p = preword + sp->ts_prewordlen;
11422 mb_ptr_back(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011423 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011424 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011425
Bram Moolenaar4770d092006-01-12 23:22:24 +000011426 /*
11427 * Form the word with proper case in preword.
11428 * If there is a word from a previous split, append.
11429 * For the soundfold tree don't change the case, simply append.
11430 */
11431 if (soundfold)
11432 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
11433 else if (flags & WF_KEEPCAP)
11434 /* Must find the word in the keep-case tree. */
11435 find_keepcap_word(slang, tword + sp->ts_splitoff,
11436 preword + sp->ts_prewordlen);
11437 else
11438 {
11439 /* Include badflags: If the badword is onecap or allcap
11440 * use that for the goodword too. But if the badword is
11441 * allcap and it's only one char long use onecap. */
11442 c = su->su_badflags;
11443 if ((c & WF_ALLCAP)
11444#ifdef FEAT_MBYTE
11445 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
11446#else
11447 && su->su_badlen == 1
11448#endif
11449 )
11450 c = WF_ONECAP;
11451 c |= flags;
11452
11453 /* When appending a compound word after a word character don't
11454 * use Onecap. */
11455 if (p != NULL && spell_iswordp_nmw(p))
11456 c &= ~WF_ONECAP;
11457 make_case_word(tword + sp->ts_splitoff,
11458 preword + sp->ts_prewordlen, c);
11459 }
11460
11461 if (!soundfold)
11462 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011463 /* Don't use a banned word. It may appear again as a good
11464 * word, thus remember it. */
11465 if (flags & WF_BANNED)
11466 {
Bram Moolenaar5195e452005-08-19 20:32:47 +000011467 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011468 break;
11469 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011470 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +000011471 && WAS_BANNED(su, preword + sp->ts_prewordlen))
11472 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011473 {
11474 if (slang->sl_compprog == NULL)
11475 break;
11476 /* the word so far was banned but we may try compounding */
11477 goodword_ends = FALSE;
11478 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011479 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011480
Bram Moolenaar4770d092006-01-12 23:22:24 +000011481 newscore = 0;
11482 if (!soundfold) /* soundfold words don't have flags */
11483 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011484 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011485 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011486 newscore += SCORE_REGION;
11487 if (flags & WF_RARE)
11488 newscore += SCORE_RARE;
11489
Bram Moolenaar0c405862005-06-22 22:26:26 +000011490 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011491 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011492 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011493 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011494
Bram Moolenaar4770d092006-01-12 23:22:24 +000011495 /* TODO: how about splitting in the soundfold tree? */
11496 if (fword_ends
11497 && goodword_ends
11498 && sp->ts_fidx >= sp->ts_fidxtry
11499 && compound_ok)
11500 {
11501 /* The badword also ends: add suggestions. */
11502#ifdef DEBUG_TRIEWALK
11503 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011504 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011505 int j;
11506
11507 /* print the stack of changes that brought us here */
11508 smsg("------ %s -------", fword);
11509 for (j = 0; j < depth; ++j)
11510 smsg("%s", changename[j]);
11511 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011512#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011513 if (soundfold)
11514 {
11515 /* For soundfolded words we need to find the original
11516 * words, the edit distrance and then add them. */
11517 add_sound_suggest(su, preword, sp->ts_score, lp);
11518 }
11519 else
11520 {
11521 /* Give a penalty when changing non-word char to word
11522 * char, e.g., "thes," -> "these". */
11523 p = fword + sp->ts_fidx;
11524 mb_ptr_back(fword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011525 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011526 {
11527 p = preword + STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011528 mb_ptr_back(preword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011529 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011530 newscore += SCORE_NONWORD;
11531 }
11532
Bram Moolenaar4770d092006-01-12 23:22:24 +000011533 /* Give a bonus to words seen before. */
11534 score = score_wordcount_adj(slang,
11535 sp->ts_score + newscore,
11536 preword + sp->ts_prewordlen,
11537 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011538
Bram Moolenaar4770d092006-01-12 23:22:24 +000011539 /* Add the suggestion if the score isn't too bad. */
11540 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011541 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011542 add_suggestion(su, &su->su_ga, preword,
11543 sp->ts_fidx - repextra,
11544 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011545
11546 if (su->su_badflags & WF_MIXCAP)
11547 {
11548 /* We really don't know if the word should be
11549 * upper or lower case, add both. */
11550 c = captype(preword, NULL);
11551 if (c == 0 || c == WF_ALLCAP)
11552 {
11553 make_case_word(tword + sp->ts_splitoff,
11554 preword + sp->ts_prewordlen,
11555 c == 0 ? WF_ALLCAP : 0);
11556
11557 add_suggestion(su, &su->su_ga, preword,
11558 sp->ts_fidx - repextra,
11559 score + SCORE_ICASE, 0, FALSE,
11560 lp->lp_sallang, FALSE);
11561 }
11562 }
11563 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011564 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011565 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011566
Bram Moolenaar4770d092006-01-12 23:22:24 +000011567 /*
11568 * Try word split and/or compounding.
11569 */
11570 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +000011571#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011572 /* Don't split halfway a character. */
11573 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011574#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011575 )
11576 {
11577 int try_compound;
11578 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011579
Bram Moolenaar4770d092006-01-12 23:22:24 +000011580 /* If past the end of the bad word don't try a split.
11581 * Otherwise try changing the next word. E.g., find
11582 * suggestions for "the the" where the second "the" is
11583 * different. It's done like a split.
11584 * TODO: word split for soundfold words */
11585 try_split = (sp->ts_fidx - repextra < su->su_badlen)
11586 && !soundfold;
11587
11588 /* Get here in several situations:
11589 * 1. The word in the tree ends:
11590 * If the word allows compounding try that. Otherwise try
11591 * a split by inserting a space. For both check that a
11592 * valid words starts at fword[sp->ts_fidx].
11593 * For NOBREAK do like compounding to be able to check if
11594 * the next word is valid.
11595 * 2. The badword does end, but it was due to a change (e.g.,
11596 * a swap). No need to split, but do check that the
11597 * following word is valid.
11598 * 3. The badword and the word in the tree end. It may still
11599 * be possible to compound another (short) word.
11600 */
11601 try_compound = FALSE;
11602 if (!soundfold
11603 && slang->sl_compprog != NULL
11604 && ((unsigned)flags >> 24) != 0
11605 && sp->ts_twordlen - sp->ts_splitoff
11606 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011607#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011608 && (!has_mbyte
11609 || slang->sl_compminlen == 0
11610 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011611 >= slang->sl_compminlen)
11612#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011613 && (slang->sl_compsylmax < MAXWLEN
11614 || sp->ts_complen + 1 - sp->ts_compsplit
11615 < slang->sl_compmax)
11616 && (byte_in_str(sp->ts_complen == sp->ts_compsplit
11617 ? slang->sl_compstartflags
11618 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +000011619 ((unsigned)flags >> 24))))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011620 {
11621 try_compound = TRUE;
11622 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11623 compflags[sp->ts_complen + 1] = NUL;
11624 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011625
Bram Moolenaar4770d092006-01-12 23:22:24 +000011626 /* For NOBREAK we never try splitting, it won't make any word
11627 * valid. */
11628 if (slang->sl_nobreak)
11629 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +000011630
Bram Moolenaar4770d092006-01-12 23:22:24 +000011631 /* If we could add a compound word, and it's also possible to
11632 * split at this point, do the split first and set
11633 * TSF_DIDSPLIT to avoid doing it again. */
11634 else if (!fword_ends
11635 && try_compound
11636 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11637 {
11638 try_compound = FALSE;
11639 sp->ts_flags |= TSF_DIDSPLIT;
11640 --sp->ts_curi; /* do the same NUL again */
11641 compflags[sp->ts_complen] = NUL;
11642 }
11643 else
11644 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011645
Bram Moolenaar4770d092006-01-12 23:22:24 +000011646 if (try_split || try_compound)
11647 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011648 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +000011649 {
11650 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011651 * words so far are valid for compounding. If there
11652 * is only one word it must not have the NEEDCOMPOUND
11653 * flag. */
11654 if (sp->ts_complen == sp->ts_compsplit
11655 && (flags & WF_NEEDCOMP))
11656 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +000011657 p = preword;
11658 while (*skiptowhite(p) != NUL)
11659 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +000011660 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +000011661 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +000011662 compflags + sp->ts_compsplit))
11663 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011664
11665 if (slang->sl_nosplitsugs)
11666 newscore += SCORE_SPLIT_NO;
11667 else
11668 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011669
11670 /* Give a bonus to words seen before. */
11671 newscore = score_wordcount_adj(slang, newscore,
11672 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011673 }
11674
Bram Moolenaar4770d092006-01-12 23:22:24 +000011675 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011676 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011677 go_deeper(stack, depth, newscore);
11678#ifdef DEBUG_TRIEWALK
11679 if (!try_compound && !fword_ends)
11680 sprintf(changename[depth], "%.*s-%s: split",
11681 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11682 else
11683 sprintf(changename[depth], "%.*s-%s: compound",
11684 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11685#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011686 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000011687 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011688 sp->ts_state = STATE_SPLITUNDO;
11689
11690 ++depth;
11691 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011692
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011693 /* Append a space to preword when splitting. */
11694 if (!try_compound && !fword_ends)
11695 STRCAT(preword, " ");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011696 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011697 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +000011698 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011699
11700 /* If the badword has a non-word character at this
11701 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011702 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011703 * character when the word ends. But only when the
11704 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011705 if (((!try_compound && !spell_iswordp_nmw(fword
11706 + sp->ts_fidx))
11707 || fword_ends)
11708 && fword[sp->ts_fidx] != NUL
11709 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011710 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011711 int l;
11712
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011713#ifdef FEAT_MBYTE
11714 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011715 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011716 else
11717#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011718 l = 1;
11719 if (fword_ends)
11720 {
11721 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +000011722 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011723 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011724 sp->ts_prewordlen += l;
11725 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011726 }
11727 else
11728 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
11729 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011730 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011731
Bram Moolenaard12a1322005-08-21 22:08:24 +000011732 /* When compounding include compound flag in
11733 * compflags[] (already set above). When splitting we
11734 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011735 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +000011736 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011737 else
Bram Moolenaard12a1322005-08-21 22:08:24 +000011738 sp->ts_compsplit = sp->ts_complen;
11739 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011740
Bram Moolenaar53805d12005-08-01 07:08:33 +000011741 /* set su->su_badflags to the caps type at this
11742 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011743#ifdef FEAT_MBYTE
11744 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +000011745 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011746 else
11747#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +000011748 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011749 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011750 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011751
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011752 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011753 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011754
11755 /* If there are postponed prefixes, try these too. */
11756 if (pbyts != NULL)
11757 {
11758 byts = pbyts;
11759 idxs = pidxs;
11760 sp->ts_prefixdepth = PFD_PREFIXTREE;
11761 sp->ts_state = STATE_NOPREFIX;
11762 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011763 }
11764 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011765 }
11766 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011767
Bram Moolenaar4770d092006-01-12 23:22:24 +000011768 case STATE_SPLITUNDO:
11769 /* Undo the changes done for word split or compound word. */
11770 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011771
Bram Moolenaar4770d092006-01-12 23:22:24 +000011772 /* Continue looking for NUL bytes. */
11773 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011774
Bram Moolenaar4770d092006-01-12 23:22:24 +000011775 /* In case we went into the prefix tree. */
11776 byts = fbyts;
11777 idxs = fidxs;
11778 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011779
Bram Moolenaar4770d092006-01-12 23:22:24 +000011780 case STATE_ENDNUL:
11781 /* Past the NUL bytes in the node. */
11782 su->su_badflags = sp->ts_save_badflags;
11783 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011784#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011785 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011786#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011787 )
11788 {
11789 /* The badword ends, can't use STATE_PLAIN. */
11790 sp->ts_state = STATE_DEL;
11791 break;
11792 }
11793 sp->ts_state = STATE_PLAIN;
11794 /*FALLTHROUGH*/
11795
11796 case STATE_PLAIN:
11797 /*
11798 * Go over all possible bytes at this node, add each to tword[]
11799 * and use child node. "ts_curi" is the index.
11800 */
11801 arridx = sp->ts_arridx;
11802 if (sp->ts_curi > byts[arridx])
11803 {
11804 /* Done all bytes at this node, do next state. When still at
11805 * already changed bytes skip the other tricks. */
11806 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011807 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011808 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011809 sp->ts_state = STATE_FINAL;
11810 }
11811 else
11812 {
11813 arridx += sp->ts_curi++;
11814 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011815
Bram Moolenaar4770d092006-01-12 23:22:24 +000011816 /* Normal byte, go one level deeper. If it's not equal to the
11817 * byte in the bad word adjust the score. But don't even try
11818 * when the byte was already changed. And don't try when we
11819 * just deleted this byte, accepting it is always cheaper then
11820 * delete + substitute. */
11821 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +000011822#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011823 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011824#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011825 )
11826 newscore = 0;
11827 else
11828 newscore = SCORE_SUBST;
11829 if ((newscore == 0
11830 || (sp->ts_fidx >= sp->ts_fidxtry
11831 && ((sp->ts_flags & TSF_DIDDEL) == 0
11832 || c != fword[sp->ts_delidx])))
11833 && TRY_DEEPER(su, stack, depth, newscore))
11834 {
11835 go_deeper(stack, depth, newscore);
11836#ifdef DEBUG_TRIEWALK
11837 if (newscore > 0)
11838 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
11839 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11840 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011841 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011842 sprintf(changename[depth], "%.*s-%s: accept %c",
11843 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11844 fword[sp->ts_fidx]);
11845#endif
11846 ++depth;
11847 sp = &stack[depth];
11848 ++sp->ts_fidx;
11849 tword[sp->ts_twordlen++] = c;
11850 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +000011851#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011852 if (newscore == SCORE_SUBST)
11853 sp->ts_isdiff = DIFF_YES;
11854 if (has_mbyte)
11855 {
11856 /* Multi-byte characters are a bit complicated to
11857 * handle: They differ when any of the bytes differ
11858 * and then their length may also differ. */
11859 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011860 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011861 /* First byte. */
11862 sp->ts_tcharidx = 0;
11863 sp->ts_tcharlen = MB_BYTE2LEN(c);
11864 sp->ts_fcharstart = sp->ts_fidx - 1;
11865 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011866 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011867 }
11868 else if (sp->ts_isdiff == DIFF_INSERT)
11869 /* When inserting trail bytes don't advance in the
11870 * bad word. */
11871 --sp->ts_fidx;
11872 if (++sp->ts_tcharidx == sp->ts_tcharlen)
11873 {
11874 /* Last byte of character. */
11875 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +000011876 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011877 /* Correct ts_fidx for the byte length of the
11878 * character (we didn't check that before). */
11879 sp->ts_fidx = sp->ts_fcharstart
11880 + MB_BYTE2LEN(
Bram Moolenaarea424162005-06-16 21:51:00 +000011881 fword[sp->ts_fcharstart]);
11882
Bram Moolenaar4770d092006-01-12 23:22:24 +000011883 /* For changing a composing character adjust
11884 * the score from SCORE_SUBST to
11885 * SCORE_SUBCOMP. */
11886 if (enc_utf8
11887 && utf_iscomposing(
11888 mb_ptr2char(tword
11889 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011890 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011891 && utf_iscomposing(
11892 mb_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011893 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011894 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011895 SCORE_SUBST - SCORE_SUBCOMP;
11896
Bram Moolenaar4770d092006-01-12 23:22:24 +000011897 /* For a similar character adjust score from
11898 * SCORE_SUBST to SCORE_SIMILAR. */
11899 else if (!soundfold
11900 && slang->sl_has_map
11901 && similar_chars(slang,
11902 mb_ptr2char(tword
11903 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +000011904 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +000011905 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +000011906 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011907 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +000011908 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +000011909 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011910 else if (sp->ts_isdiff == DIFF_INSERT
11911 && sp->ts_twordlen > sp->ts_tcharlen)
11912 {
11913 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
11914 c = mb_ptr2char(p);
11915 if (enc_utf8 && utf_iscomposing(c))
11916 {
11917 /* Inserting a composing char doesn't
11918 * count that much. */
11919 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
11920 }
11921 else
11922 {
11923 /* If the previous character was the same,
11924 * thus doubling a character, give a bonus
11925 * to the score. Also for the soundfold
11926 * tree (might seem illogical but does
11927 * give better scores). */
11928 mb_ptr_back(tword, p);
11929 if (c == mb_ptr2char(p))
11930 sp->ts_score -= SCORE_INS
11931 - SCORE_INSDUP;
11932 }
11933 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011934
Bram Moolenaar4770d092006-01-12 23:22:24 +000011935 /* Starting a new char, reset the length. */
11936 sp->ts_tcharlen = 0;
11937 }
Bram Moolenaarea408852005-06-25 22:49:46 +000011938 }
Bram Moolenaarea424162005-06-16 21:51:00 +000011939 else
11940#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000011941 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011942 /* If we found a similar char adjust the score.
11943 * We do this after calling go_deeper() because
11944 * it's slow. */
11945 if (newscore != 0
11946 && !soundfold
11947 && slang->sl_has_map
11948 && similar_chars(slang,
11949 c, fword[sp->ts_fidx - 1]))
11950 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +000011951 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011952 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011953 }
11954 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011955
Bram Moolenaar4770d092006-01-12 23:22:24 +000011956 case STATE_DEL:
11957#ifdef FEAT_MBYTE
11958 /* When past the first byte of a multi-byte char don't try
11959 * delete/insert/swap a character. */
11960 if (has_mbyte && sp->ts_tcharlen > 0)
11961 {
11962 sp->ts_state = STATE_FINAL;
11963 break;
11964 }
11965#endif
11966 /*
11967 * Try skipping one character in the bad word (delete it).
11968 */
11969 sp->ts_state = STATE_INS_PREP;
11970 sp->ts_curi = 1;
11971 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
11972 /* Deleting a vowel at the start of a word counts less, see
11973 * soundalike_score(). */
11974 newscore = 2 * SCORE_DEL / 3;
11975 else
11976 newscore = SCORE_DEL;
11977 if (fword[sp->ts_fidx] != NUL
11978 && TRY_DEEPER(su, stack, depth, newscore))
11979 {
11980 go_deeper(stack, depth, newscore);
11981#ifdef DEBUG_TRIEWALK
11982 sprintf(changename[depth], "%.*s-%s: delete %c",
11983 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11984 fword[sp->ts_fidx]);
11985#endif
11986 ++depth;
11987
11988 /* Remember what character we deleted, so that we can avoid
11989 * inserting it again. */
11990 stack[depth].ts_flags |= TSF_DIDDEL;
11991 stack[depth].ts_delidx = sp->ts_fidx;
11992
11993 /* Advance over the character in fword[]. Give a bonus to the
11994 * score if the same character is following "nn" -> "n". It's
11995 * a bit illogical for soundfold tree but it does give better
11996 * results. */
11997#ifdef FEAT_MBYTE
11998 if (has_mbyte)
11999 {
12000 c = mb_ptr2char(fword + sp->ts_fidx);
12001 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
12002 if (enc_utf8 && utf_iscomposing(c))
12003 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
12004 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
12005 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12006 }
12007 else
12008#endif
12009 {
12010 ++stack[depth].ts_fidx;
12011 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
12012 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12013 }
12014 break;
12015 }
12016 /*FALLTHROUGH*/
12017
12018 case STATE_INS_PREP:
12019 if (sp->ts_flags & TSF_DIDDEL)
12020 {
12021 /* If we just deleted a byte then inserting won't make sense,
12022 * a substitute is always cheaper. */
12023 sp->ts_state = STATE_SWAP;
12024 break;
12025 }
12026
12027 /* skip over NUL bytes */
12028 n = sp->ts_arridx;
12029 for (;;)
12030 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012031 if (sp->ts_curi > byts[n])
12032 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012033 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012034 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012035 break;
12036 }
12037 if (byts[n + sp->ts_curi] != NUL)
12038 {
12039 /* Found a byte to insert. */
12040 sp->ts_state = STATE_INS;
12041 break;
12042 }
12043 ++sp->ts_curi;
12044 }
12045 break;
12046
12047 /*FALLTHROUGH*/
12048
12049 case STATE_INS:
12050 /* Insert one byte. Repeat this for each possible byte at this
12051 * node. */
12052 n = sp->ts_arridx;
12053 if (sp->ts_curi > byts[n])
12054 {
12055 /* Done all bytes at this node, go to next state. */
12056 sp->ts_state = STATE_SWAP;
12057 break;
12058 }
12059
12060 /* Do one more byte at this node, but:
12061 * - Skip NUL bytes.
12062 * - Skip the byte if it's equal to the byte in the word,
12063 * accepting that byte is always better.
12064 */
12065 n += sp->ts_curi++;
12066 c = byts[n];
12067 if (soundfold && sp->ts_twordlen == 0 && c == '*')
12068 /* Inserting a vowel at the start of a word counts less,
12069 * see soundalike_score(). */
12070 newscore = 2 * SCORE_INS / 3;
12071 else
12072 newscore = SCORE_INS;
12073 if (c != fword[sp->ts_fidx]
12074 && TRY_DEEPER(su, stack, depth, newscore))
12075 {
12076 go_deeper(stack, depth, newscore);
12077#ifdef DEBUG_TRIEWALK
12078 sprintf(changename[depth], "%.*s-%s: insert %c",
12079 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12080 c);
12081#endif
12082 ++depth;
12083 sp = &stack[depth];
12084 tword[sp->ts_twordlen++] = c;
12085 sp->ts_arridx = idxs[n];
12086#ifdef FEAT_MBYTE
12087 if (has_mbyte)
12088 {
12089 fl = MB_BYTE2LEN(c);
12090 if (fl > 1)
12091 {
12092 /* There are following bytes for the same character.
12093 * We must find all bytes before trying
12094 * delete/insert/swap/etc. */
12095 sp->ts_tcharlen = fl;
12096 sp->ts_tcharidx = 1;
12097 sp->ts_isdiff = DIFF_INSERT;
12098 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012099 }
12100 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012101 fl = 1;
12102 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000012103#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012104 {
12105 /* If the previous character was the same, thus doubling a
12106 * character, give a bonus to the score. Also for
12107 * soundfold words (illogical but does give a better
12108 * score). */
12109 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +000012110 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012111 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012112 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012113 }
12114 break;
12115
12116 case STATE_SWAP:
12117 /*
12118 * Swap two bytes in the bad word: "12" -> "21".
12119 * We change "fword" here, it's changed back afterwards at
12120 * STATE_UNSWAP.
12121 */
12122 p = fword + sp->ts_fidx;
12123 c = *p;
12124 if (c == NUL)
12125 {
12126 /* End of word, can't swap or replace. */
12127 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012128 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012129 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012130
Bram Moolenaar4770d092006-01-12 23:22:24 +000012131 /* Don't swap if the first character is not a word character.
12132 * SWAP3 etc. also don't make sense then. */
12133 if (!soundfold && !spell_iswordp(p, curbuf))
12134 {
12135 sp->ts_state = STATE_REP_INI;
12136 break;
12137 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012138
Bram Moolenaar4770d092006-01-12 23:22:24 +000012139#ifdef FEAT_MBYTE
12140 if (has_mbyte)
12141 {
12142 n = mb_cptr2len(p);
12143 c = mb_ptr2char(p);
12144 if (!soundfold && !spell_iswordp(p + n, curbuf))
12145 c2 = c; /* don't swap non-word char */
12146 else
12147 c2 = mb_ptr2char(p + n);
12148 }
12149 else
12150#endif
12151 {
12152 if (!soundfold && !spell_iswordp(p + 1, curbuf))
12153 c2 = c; /* don't swap non-word char */
12154 else
12155 c2 = p[1];
12156 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012157
Bram Moolenaar4770d092006-01-12 23:22:24 +000012158 /* When characters are identical, swap won't do anything.
12159 * Also get here if the second char is not a word character. */
12160 if (c == c2)
12161 {
12162 sp->ts_state = STATE_SWAP3;
12163 break;
12164 }
12165 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
12166 {
12167 go_deeper(stack, depth, SCORE_SWAP);
12168#ifdef DEBUG_TRIEWALK
12169 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
12170 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12171 c, c2);
12172#endif
12173 sp->ts_state = STATE_UNSWAP;
12174 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012175#ifdef FEAT_MBYTE
12176 if (has_mbyte)
12177 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012178 fl = mb_char2len(c2);
12179 mch_memmove(p, p + n, fl);
12180 mb_char2bytes(c, p + fl);
12181 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012182 }
12183 else
12184#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012185 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012186 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012187 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012188 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012189 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012190 }
12191 else
12192 /* If this swap doesn't work then SWAP3 won't either. */
12193 sp->ts_state = STATE_REP_INI;
12194 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000012195
Bram Moolenaar4770d092006-01-12 23:22:24 +000012196 case STATE_UNSWAP:
12197 /* Undo the STATE_SWAP swap: "21" -> "12". */
12198 p = fword + sp->ts_fidx;
12199#ifdef FEAT_MBYTE
12200 if (has_mbyte)
12201 {
12202 n = MB_BYTE2LEN(*p);
12203 c = mb_ptr2char(p + n);
12204 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
12205 mb_char2bytes(c, p);
12206 }
12207 else
12208#endif
12209 {
12210 c = *p;
12211 *p = p[1];
12212 p[1] = c;
12213 }
12214 /*FALLTHROUGH*/
12215
12216 case STATE_SWAP3:
12217 /* Swap two bytes, skipping one: "123" -> "321". We change
12218 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
12219 p = fword + sp->ts_fidx;
12220#ifdef FEAT_MBYTE
12221 if (has_mbyte)
12222 {
12223 n = mb_cptr2len(p);
12224 c = mb_ptr2char(p);
12225 fl = mb_cptr2len(p + n);
12226 c2 = mb_ptr2char(p + n);
12227 if (!soundfold && !spell_iswordp(p + n + fl, curbuf))
12228 c3 = c; /* don't swap non-word char */
12229 else
12230 c3 = mb_ptr2char(p + n + fl);
12231 }
12232 else
12233#endif
12234 {
12235 c = *p;
12236 c2 = p[1];
12237 if (!soundfold && !spell_iswordp(p + 2, curbuf))
12238 c3 = c; /* don't swap non-word char */
12239 else
12240 c3 = p[2];
12241 }
12242
12243 /* When characters are identical: "121" then SWAP3 result is
12244 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
12245 * same as SWAP on next char: "112". Thus skip all swapping.
12246 * Also skip when c3 is NUL.
12247 * Also get here when the third character is not a word character.
12248 * Second character may any char: "a.b" -> "b.a" */
12249 if (c == c3 || c3 == NUL)
12250 {
12251 sp->ts_state = STATE_REP_INI;
12252 break;
12253 }
12254 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12255 {
12256 go_deeper(stack, depth, SCORE_SWAP3);
12257#ifdef DEBUG_TRIEWALK
12258 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
12259 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12260 c, c3);
12261#endif
12262 sp->ts_state = STATE_UNSWAP3;
12263 ++depth;
12264#ifdef FEAT_MBYTE
12265 if (has_mbyte)
12266 {
12267 tl = mb_char2len(c3);
12268 mch_memmove(p, p + n + fl, tl);
12269 mb_char2bytes(c2, p + tl);
12270 mb_char2bytes(c, p + fl + tl);
12271 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
12272 }
12273 else
12274#endif
12275 {
12276 p[0] = p[2];
12277 p[2] = c;
12278 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12279 }
12280 }
12281 else
12282 sp->ts_state = STATE_REP_INI;
12283 break;
12284
12285 case STATE_UNSWAP3:
12286 /* Undo STATE_SWAP3: "321" -> "123" */
12287 p = fword + sp->ts_fidx;
12288#ifdef FEAT_MBYTE
12289 if (has_mbyte)
12290 {
12291 n = MB_BYTE2LEN(*p);
12292 c2 = mb_ptr2char(p + n);
12293 fl = MB_BYTE2LEN(p[n]);
12294 c = mb_ptr2char(p + n + fl);
12295 tl = MB_BYTE2LEN(p[n + fl]);
12296 mch_memmove(p + fl + tl, p, n);
12297 mb_char2bytes(c, p);
12298 mb_char2bytes(c2, p + tl);
12299 p = p + tl;
12300 }
12301 else
12302#endif
12303 {
12304 c = *p;
12305 *p = p[2];
12306 p[2] = c;
12307 ++p;
12308 }
12309
12310 if (!soundfold && !spell_iswordp(p, curbuf))
12311 {
12312 /* Middle char is not a word char, skip the rotate. First and
12313 * third char were already checked at swap and swap3. */
12314 sp->ts_state = STATE_REP_INI;
12315 break;
12316 }
12317
12318 /* Rotate three characters left: "123" -> "231". We change
12319 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
12320 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12321 {
12322 go_deeper(stack, depth, SCORE_SWAP3);
12323#ifdef DEBUG_TRIEWALK
12324 p = fword + sp->ts_fidx;
12325 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
12326 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12327 p[0], p[1], p[2]);
12328#endif
12329 sp->ts_state = STATE_UNROT3L;
12330 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012331 p = fword + sp->ts_fidx;
12332#ifdef FEAT_MBYTE
12333 if (has_mbyte)
12334 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012335 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000012336 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012337 fl = mb_cptr2len(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012338 fl += mb_cptr2len(p + n + fl);
12339 mch_memmove(p, p + n, fl);
12340 mb_char2bytes(c, p + fl);
12341 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012342 }
12343 else
12344#endif
12345 {
12346 c = *p;
12347 *p = p[1];
12348 p[1] = p[2];
12349 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012350 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +000012351 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012352 }
12353 else
12354 sp->ts_state = STATE_REP_INI;
12355 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012356
Bram Moolenaar4770d092006-01-12 23:22:24 +000012357 case STATE_UNROT3L:
12358 /* Undo ROT3L: "231" -> "123" */
12359 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +000012360#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012361 if (has_mbyte)
12362 {
12363 n = MB_BYTE2LEN(*p);
12364 n += MB_BYTE2LEN(p[n]);
12365 c = mb_ptr2char(p + n);
12366 tl = MB_BYTE2LEN(p[n]);
12367 mch_memmove(p + tl, p, n);
12368 mb_char2bytes(c, p);
12369 }
12370 else
Bram Moolenaarea424162005-06-16 21:51:00 +000012371#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012372 {
12373 c = p[2];
12374 p[2] = p[1];
12375 p[1] = *p;
12376 *p = c;
12377 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012378
Bram Moolenaar4770d092006-01-12 23:22:24 +000012379 /* Rotate three bytes right: "123" -> "312". We change "fword"
12380 * here, it's changed back afterwards at STATE_UNROT3R. */
12381 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12382 {
12383 go_deeper(stack, depth, SCORE_SWAP3);
12384#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012385 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012386 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
12387 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12388 p[0], p[1], p[2]);
12389#endif
12390 sp->ts_state = STATE_UNROT3R;
12391 ++depth;
12392 p = fword + sp->ts_fidx;
12393#ifdef FEAT_MBYTE
12394 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000012395 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012396 n = mb_cptr2len(p);
12397 n += mb_cptr2len(p + n);
12398 c = mb_ptr2char(p + n);
12399 tl = mb_cptr2len(p + n);
12400 mch_memmove(p + tl, p, n);
12401 mb_char2bytes(c, p);
12402 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +000012403 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012404 else
12405#endif
12406 {
12407 c = p[2];
12408 p[2] = p[1];
12409 p[1] = *p;
12410 *p = c;
12411 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12412 }
12413 }
12414 else
12415 sp->ts_state = STATE_REP_INI;
12416 break;
12417
12418 case STATE_UNROT3R:
12419 /* Undo ROT3R: "312" -> "123" */
12420 p = fword + sp->ts_fidx;
12421#ifdef FEAT_MBYTE
12422 if (has_mbyte)
12423 {
12424 c = mb_ptr2char(p);
12425 tl = MB_BYTE2LEN(*p);
12426 n = MB_BYTE2LEN(p[tl]);
12427 n += MB_BYTE2LEN(p[tl + n]);
12428 mch_memmove(p, p + tl, n);
12429 mb_char2bytes(c, p + n);
12430 }
12431 else
12432#endif
12433 {
12434 c = *p;
12435 *p = p[1];
12436 p[1] = p[2];
12437 p[2] = c;
12438 }
12439 /*FALLTHROUGH*/
12440
12441 case STATE_REP_INI:
12442 /* Check if matching with REP items from the .aff file would work.
12443 * Quickly skip if:
12444 * - there are no REP items and we are not in the soundfold trie
12445 * - the score is going to be too high anyway
12446 * - already applied a REP item or swapped here */
12447 if ((lp->lp_replang == NULL && !soundfold)
12448 || sp->ts_score + SCORE_REP >= su->su_maxscore
12449 || sp->ts_fidx < sp->ts_fidxtry)
12450 {
12451 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012452 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012453 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012454
Bram Moolenaar4770d092006-01-12 23:22:24 +000012455 /* Use the first byte to quickly find the first entry that may
12456 * match. If the index is -1 there is none. */
12457 if (soundfold)
12458 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
12459 else
12460 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012461
Bram Moolenaar4770d092006-01-12 23:22:24 +000012462 if (sp->ts_curi < 0)
12463 {
12464 sp->ts_state = STATE_FINAL;
12465 break;
12466 }
12467
12468 sp->ts_state = STATE_REP;
12469 /*FALLTHROUGH*/
12470
12471 case STATE_REP:
12472 /* Try matching with REP items from the .aff file. For each match
12473 * replace the characters and check if the resulting word is
12474 * valid. */
12475 p = fword + sp->ts_fidx;
12476
12477 if (soundfold)
12478 gap = &slang->sl_repsal;
12479 else
12480 gap = &lp->lp_replang->sl_rep;
12481 while (sp->ts_curi < gap->ga_len)
12482 {
12483 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
12484 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012485 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012486 /* past possible matching entries */
12487 sp->ts_curi = gap->ga_len;
12488 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012489 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012490 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
12491 && TRY_DEEPER(su, stack, depth, SCORE_REP))
12492 {
12493 go_deeper(stack, depth, SCORE_REP);
12494#ifdef DEBUG_TRIEWALK
12495 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
12496 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12497 ftp->ft_from, ftp->ft_to);
12498#endif
12499 /* Need to undo this afterwards. */
12500 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012501
Bram Moolenaar4770d092006-01-12 23:22:24 +000012502 /* Change the "from" to the "to" string. */
12503 ++depth;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012504 fl = (int)STRLEN(ftp->ft_from);
12505 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012506 if (fl != tl)
12507 {
12508 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
12509 repextra += tl - fl;
12510 }
12511 mch_memmove(p, ftp->ft_to, tl);
12512 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
12513#ifdef FEAT_MBYTE
12514 stack[depth].ts_tcharlen = 0;
12515#endif
12516 break;
12517 }
12518 }
12519
12520 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
12521 /* No (more) matches. */
12522 sp->ts_state = STATE_FINAL;
12523
12524 break;
12525
12526 case STATE_REP_UNDO:
12527 /* Undo a REP replacement and continue with the next one. */
12528 if (soundfold)
12529 gap = &slang->sl_repsal;
12530 else
12531 gap = &lp->lp_replang->sl_rep;
12532 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012533 fl = (int)STRLEN(ftp->ft_from);
12534 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012535 p = fword + sp->ts_fidx;
12536 if (fl != tl)
12537 {
12538 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
12539 repextra -= tl - fl;
12540 }
12541 mch_memmove(p, ftp->ft_from, fl);
12542 sp->ts_state = STATE_REP;
12543 break;
12544
12545 default:
12546 /* Did all possible states at this level, go up one level. */
12547 --depth;
12548
12549 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
12550 {
12551 /* Continue in or go back to the prefix tree. */
12552 byts = pbyts;
12553 idxs = pidxs;
12554 }
12555
12556 /* Don't check for CTRL-C too often, it takes time. */
12557 if (--breakcheckcount == 0)
12558 {
12559 ui_breakcheck();
12560 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012561 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012562 }
12563 }
12564}
12565
Bram Moolenaar4770d092006-01-12 23:22:24 +000012566
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012567/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012568 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012569 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012570 static void
12571go_deeper(stack, depth, score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012572 trystate_T *stack;
12573 int depth;
12574 int score_add;
12575{
Bram Moolenaarea424162005-06-16 21:51:00 +000012576 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012577 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012578 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012579 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000012580 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012581}
12582
Bram Moolenaar53805d12005-08-01 07:08:33 +000012583#ifdef FEAT_MBYTE
12584/*
12585 * Case-folding may change the number of bytes: Count nr of chars in
12586 * fword[flen] and return the byte length of that many chars in "word".
12587 */
12588 static int
12589nofold_len(fword, flen, word)
12590 char_u *fword;
12591 int flen;
12592 char_u *word;
12593{
12594 char_u *p;
12595 int i = 0;
12596
12597 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12598 ++i;
12599 for (p = word; i > 0; mb_ptr_adv(p))
12600 --i;
12601 return (int)(p - word);
12602}
12603#endif
12604
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012605/*
12606 * "fword" is a good word with case folded. Find the matching keep-case
12607 * words and put it in "kword".
12608 * Theoretically there could be several keep-case words that result in the
12609 * same case-folded word, but we only find one...
12610 */
12611 static void
12612find_keepcap_word(slang, fword, kword)
12613 slang_T *slang;
12614 char_u *fword;
12615 char_u *kword;
12616{
12617 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12618 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012619 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012620
12621 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012622 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012623 int round[MAXWLEN];
12624 int fwordidx[MAXWLEN];
12625 int uwordidx[MAXWLEN];
12626 int kwordlen[MAXWLEN];
12627
12628 int flen, ulen;
12629 int l;
12630 int len;
12631 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012632 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012633 char_u *p;
12634 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012635 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012636
12637 if (byts == NULL)
12638 {
12639 /* array is empty: "cannot happen" */
12640 *kword = NUL;
12641 return;
12642 }
12643
12644 /* Make an all-cap version of "fword". */
12645 allcap_copy(fword, uword);
12646
12647 /*
12648 * Each character needs to be tried both case-folded and upper-case.
12649 * All this gets very complicated if we keep in mind that changing case
12650 * may change the byte length of a multi-byte character...
12651 */
12652 depth = 0;
12653 arridx[0] = 0;
12654 round[0] = 0;
12655 fwordidx[0] = 0;
12656 uwordidx[0] = 0;
12657 kwordlen[0] = 0;
12658 while (depth >= 0)
12659 {
12660 if (fword[fwordidx[depth]] == NUL)
12661 {
12662 /* We are at the end of "fword". If the tree allows a word to end
12663 * here we have found a match. */
12664 if (byts[arridx[depth] + 1] == 0)
12665 {
12666 kword[kwordlen[depth]] = NUL;
12667 return;
12668 }
12669
12670 /* kword is getting too long, continue one level up */
12671 --depth;
12672 }
12673 else if (++round[depth] > 2)
12674 {
12675 /* tried both fold-case and upper-case character, continue one
12676 * level up */
12677 --depth;
12678 }
12679 else
12680 {
12681 /*
12682 * round[depth] == 1: Try using the folded-case character.
12683 * round[depth] == 2: Try using the upper-case character.
12684 */
12685#ifdef FEAT_MBYTE
12686 if (has_mbyte)
12687 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012688 flen = mb_cptr2len(fword + fwordidx[depth]);
12689 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012690 }
12691 else
12692#endif
12693 ulen = flen = 1;
12694 if (round[depth] == 1)
12695 {
12696 p = fword + fwordidx[depth];
12697 l = flen;
12698 }
12699 else
12700 {
12701 p = uword + uwordidx[depth];
12702 l = ulen;
12703 }
12704
12705 for (tryidx = arridx[depth]; l > 0; --l)
12706 {
12707 /* Perform a binary search in the list of accepted bytes. */
12708 len = byts[tryidx++];
12709 c = *p++;
12710 lo = tryidx;
12711 hi = tryidx + len - 1;
12712 while (lo < hi)
12713 {
12714 m = (lo + hi) / 2;
12715 if (byts[m] > c)
12716 hi = m - 1;
12717 else if (byts[m] < c)
12718 lo = m + 1;
12719 else
12720 {
12721 lo = hi = m;
12722 break;
12723 }
12724 }
12725
12726 /* Stop if there is no matching byte. */
12727 if (hi < lo || byts[lo] != c)
12728 break;
12729
12730 /* Continue at the child (if there is one). */
12731 tryidx = idxs[lo];
12732 }
12733
12734 if (l == 0)
12735 {
12736 /*
12737 * Found the matching char. Copy it to "kword" and go a
12738 * level deeper.
12739 */
12740 if (round[depth] == 1)
12741 {
12742 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
12743 flen);
12744 kwordlen[depth + 1] = kwordlen[depth] + flen;
12745 }
12746 else
12747 {
12748 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
12749 ulen);
12750 kwordlen[depth + 1] = kwordlen[depth] + ulen;
12751 }
12752 fwordidx[depth + 1] = fwordidx[depth] + flen;
12753 uwordidx[depth + 1] = uwordidx[depth] + ulen;
12754
12755 ++depth;
12756 arridx[depth] = tryidx;
12757 round[depth] = 0;
12758 }
12759 }
12760 }
12761
12762 /* Didn't find it: "cannot happen". */
12763 *kword = NUL;
12764}
12765
12766/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012767 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
12768 * su->su_sga.
12769 */
12770 static void
12771score_comp_sal(su)
12772 suginfo_T *su;
12773{
12774 langp_T *lp;
12775 char_u badsound[MAXWLEN];
12776 int i;
12777 suggest_T *stp;
12778 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012779 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012780 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012781
12782 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
12783 return;
12784
12785 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012786 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012787 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012788 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012789 if (lp->lp_slang->sl_sal.ga_len > 0)
12790 {
12791 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012792 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012793
12794 for (i = 0; i < su->su_ga.ga_len; ++i)
12795 {
12796 stp = &SUG(su->su_ga, i);
12797
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012798 /* Case-fold the suggested word, sound-fold it and compute the
12799 * sound-a-like score. */
12800 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012801 if (score < SCORE_MAXMAX)
12802 {
12803 /* Add the suggestion. */
12804 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
12805 sstp->st_word = vim_strsave(stp->st_word);
12806 if (sstp->st_word != NULL)
12807 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012808 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012809 sstp->st_score = score;
12810 sstp->st_altscore = 0;
12811 sstp->st_orglen = stp->st_orglen;
12812 ++su->su_sga.ga_len;
12813 }
12814 }
12815 }
12816 break;
12817 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012818 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012819}
12820
12821/*
12822 * Combine the list of suggestions in su->su_ga and su->su_sga.
12823 * They are intwined.
12824 */
12825 static void
12826score_combine(su)
12827 suginfo_T *su;
12828{
12829 int i;
12830 int j;
12831 garray_T ga;
12832 garray_T *gap;
12833 langp_T *lp;
12834 suggest_T *stp;
12835 char_u *p;
12836 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012837 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012838 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012839 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012840
12841 /* Add the alternate score to su_ga. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012842 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012843 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012844 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012845 if (lp->lp_slang->sl_sal.ga_len > 0)
12846 {
12847 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012848 slang = lp->lp_slang;
12849 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012850
12851 for (i = 0; i < su->su_ga.ga_len; ++i)
12852 {
12853 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012854 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012855 if (stp->st_altscore == SCORE_MAXMAX)
12856 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
12857 else
12858 stp->st_score = (stp->st_score * 3
12859 + stp->st_altscore) / 4;
12860 stp->st_salscore = FALSE;
12861 }
12862 break;
12863 }
12864 }
12865
Bram Moolenaarf193fff2006-04-27 00:02:13 +000012866 if (slang == NULL) /* Using "double" without sound folding. */
12867 {
12868 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
12869 su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012870 return;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000012871 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012872
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012873 /* Add the alternate score to su_sga. */
12874 for (i = 0; i < su->su_sga.ga_len; ++i)
12875 {
12876 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012877 stp->st_altscore = spell_edit_score(slang,
12878 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012879 if (stp->st_score == SCORE_MAXMAX)
12880 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
12881 else
12882 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
12883 stp->st_salscore = TRUE;
12884 }
12885
Bram Moolenaar4770d092006-01-12 23:22:24 +000012886 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
12887 * for both lists. */
12888 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012889 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012890 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012891 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
12892
12893 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
12894 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
12895 return;
12896
12897 stp = &SUG(ga, 0);
12898 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
12899 {
12900 /* round 1: get a suggestion from su_ga
12901 * round 2: get a suggestion from su_sga */
12902 for (round = 1; round <= 2; ++round)
12903 {
12904 gap = round == 1 ? &su->su_ga : &su->su_sga;
12905 if (i < gap->ga_len)
12906 {
12907 /* Don't add a word if it's already there. */
12908 p = SUG(*gap, i).st_word;
12909 for (j = 0; j < ga.ga_len; ++j)
12910 if (STRCMP(stp[j].st_word, p) == 0)
12911 break;
12912 if (j == ga.ga_len)
12913 stp[ga.ga_len++] = SUG(*gap, i);
12914 else
12915 vim_free(p);
12916 }
12917 }
12918 }
12919
12920 ga_clear(&su->su_ga);
12921 ga_clear(&su->su_sga);
12922
12923 /* Truncate the list to the number of suggestions that will be displayed. */
12924 if (ga.ga_len > su->su_maxcount)
12925 {
12926 for (i = su->su_maxcount; i < ga.ga_len; ++i)
12927 vim_free(stp[i].st_word);
12928 ga.ga_len = su->su_maxcount;
12929 }
12930
12931 su->su_ga = ga;
12932}
12933
12934/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012935 * For the goodword in "stp" compute the soundalike score compared to the
12936 * badword.
12937 */
12938 static int
12939stp_sal_score(stp, su, slang, badsound)
12940 suggest_T *stp;
12941 suginfo_T *su;
12942 slang_T *slang;
12943 char_u *badsound; /* sound-folded badword */
12944{
12945 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012946 char_u *pbad;
12947 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012948 char_u badsound2[MAXWLEN];
12949 char_u fword[MAXWLEN];
12950 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012951 char_u goodword[MAXWLEN];
12952 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012953
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012954 lendiff = (int)(su->su_badlen - stp->st_orglen);
12955 if (lendiff >= 0)
12956 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012957 else
12958 {
12959 /* soundfold the bad word with more characters following */
12960 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
12961
12962 /* When joining two words the sound often changes a lot. E.g., "t he"
12963 * sounds like "t h" while "the" sounds like "@". Avoid that by
12964 * removing the space. Don't do it when the good word also contains a
12965 * space. */
12966 if (vim_iswhite(su->su_badptr[su->su_badlen])
12967 && *skiptowhite(stp->st_word) == NUL)
12968 for (p = fword; *(p = skiptowhite(p)) != NUL; )
12969 mch_memmove(p, p + 1, STRLEN(p));
12970
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012971 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012972 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012973 }
12974
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012975 if (lendiff > 0)
12976 {
12977 /* Add part of the bad word to the good word, so that we soundfold
12978 * what replaces the bad word. */
12979 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012980 vim_strncpy(goodword + stp->st_wordlen,
12981 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012982 pgood = goodword;
12983 }
12984 else
12985 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012986
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012987 /* Sound-fold the word and compute the score for the difference. */
12988 spell_soundfold(slang, pgood, FALSE, goodsound);
12989
12990 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012991}
12992
Bram Moolenaar4770d092006-01-12 23:22:24 +000012993/* structure used to store soundfolded words that add_sound_suggest() has
12994 * handled already. */
12995typedef struct
12996{
12997 short sft_score; /* lowest score used */
12998 char_u sft_word[1]; /* soundfolded word, actually longer */
12999} sftword_T;
13000
13001static sftword_T dumsft;
13002#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
13003#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
13004
13005/*
13006 * Prepare for calling suggest_try_soundalike().
13007 */
13008 static void
13009suggest_try_soundalike_prep()
13010{
13011 langp_T *lp;
13012 int lpi;
13013 slang_T *slang;
13014
13015 /* Do this for all languages that support sound folding and for which a
13016 * .sug file has been loaded. */
13017 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
13018 {
13019 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
13020 slang = lp->lp_slang;
13021 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13022 /* prepare the hashtable used by add_sound_suggest() */
13023 hash_init(&slang->sl_sounddone);
13024 }
13025}
13026
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013027/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013028 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013029 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013030 */
13031 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000013032suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013033 suginfo_T *su;
13034{
13035 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013036 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013037 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013038 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013039
Bram Moolenaar4770d092006-01-12 23:22:24 +000013040 /* Do this for all languages that support sound folding and for which a
13041 * .sug file has been loaded. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013042 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013043 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013044 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
13045 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013046 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013047 {
13048 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013049 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013050
Bram Moolenaar4770d092006-01-12 23:22:24 +000013051 /* try all kinds of inserts/deletes/swaps/etc. */
13052 /* TODO: also soundfold the next words, so that we can try joining
13053 * and splitting */
13054 suggest_trie_walk(su, lp, salword, TRUE);
13055 }
13056 }
13057}
13058
13059/*
13060 * Finish up after calling suggest_try_soundalike().
13061 */
13062 static void
13063suggest_try_soundalike_finish()
13064{
13065 langp_T *lp;
13066 int lpi;
13067 slang_T *slang;
13068 int todo;
13069 hashitem_T *hi;
13070
13071 /* Do this for all languages that support sound folding and for which a
13072 * .sug file has been loaded. */
13073 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
13074 {
13075 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
13076 slang = lp->lp_slang;
13077 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13078 {
13079 /* Free the info about handled words. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013080 todo = (int)slang->sl_sounddone.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013081 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
13082 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013083 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013084 vim_free(HI2SFT(hi));
13085 --todo;
13086 }
13087 hash_clear(&slang->sl_sounddone);
13088 }
13089 }
13090}
13091
13092/*
13093 * A match with a soundfolded word is found. Add the good word(s) that
13094 * produce this soundfolded word.
13095 */
13096 static void
13097add_sound_suggest(su, goodword, score, lp)
13098 suginfo_T *su;
13099 char_u *goodword;
13100 int score; /* soundfold score */
13101 langp_T *lp;
13102{
13103 slang_T *slang = lp->lp_slang; /* language for sound folding */
13104 int sfwordnr;
13105 char_u *nrline;
13106 int orgnr;
13107 char_u theword[MAXWLEN];
13108 int i;
13109 int wlen;
13110 char_u *byts;
13111 idx_T *idxs;
13112 int n;
13113 int wordcount;
13114 int wc;
13115 int goodscore;
13116 hash_T hash;
13117 hashitem_T *hi;
13118 sftword_T *sft;
13119 int bc, gc;
13120 int limit;
13121
13122 /*
13123 * It's very well possible that the same soundfold word is found several
13124 * times with different scores. Since the following is quite slow only do
13125 * the words that have a better score than before. Use a hashtable to
13126 * remember the words that have been done.
13127 */
13128 hash = hash_hash(goodword);
13129 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
13130 if (HASHITEM_EMPTY(hi))
13131 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013132 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T)
13133 + STRLEN(goodword)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000013134 if (sft != NULL)
13135 {
13136 sft->sft_score = score;
13137 STRCPY(sft->sft_word, goodword);
13138 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
13139 }
13140 }
13141 else
13142 {
13143 sft = HI2SFT(hi);
13144 if (score >= sft->sft_score)
13145 return;
13146 sft->sft_score = score;
13147 }
13148
13149 /*
13150 * Find the word nr in the soundfold tree.
13151 */
13152 sfwordnr = soundfold_find(slang, goodword);
13153 if (sfwordnr < 0)
13154 {
13155 EMSG2(_(e_intern2), "add_sound_suggest()");
13156 return;
13157 }
13158
13159 /*
13160 * go over the list of good words that produce this soundfold word
13161 */
13162 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
13163 orgnr = 0;
13164 while (*nrline != NUL)
13165 {
13166 /* The wordnr was stored in a minimal nr of bytes as an offset to the
13167 * previous wordnr. */
13168 orgnr += bytes2offset(&nrline);
13169
13170 byts = slang->sl_fbyts;
13171 idxs = slang->sl_fidxs;
13172
13173 /* Lookup the word "orgnr" one of the two tries. */
13174 n = 0;
13175 wlen = 0;
13176 wordcount = 0;
13177 for (;;)
13178 {
13179 i = 1;
13180 if (wordcount == orgnr && byts[n + 1] == NUL)
13181 break; /* found end of word */
13182
13183 if (byts[n + 1] == NUL)
13184 ++wordcount;
13185
13186 /* skip over the NUL bytes */
13187 for ( ; byts[n + i] == NUL; ++i)
13188 if (i > byts[n]) /* safety check */
13189 {
13190 STRCPY(theword + wlen, "BAD");
13191 goto badword;
13192 }
13193
13194 /* One of the siblings must have the word. */
13195 for ( ; i < byts[n]; ++i)
13196 {
13197 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
13198 if (wordcount + wc > orgnr)
13199 break;
13200 wordcount += wc;
13201 }
13202
13203 theword[wlen++] = byts[n + i];
13204 n = idxs[n + i];
13205 }
13206badword:
13207 theword[wlen] = NUL;
13208
13209 /* Go over the possible flags and regions. */
13210 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
13211 {
13212 char_u cword[MAXWLEN];
13213 char_u *p;
13214 int flags = (int)idxs[n + i];
13215
Bram Moolenaare1438bb2006-03-01 22:01:55 +000013216 /* Skip words with the NOSUGGEST flag */
13217 if (flags & WF_NOSUGGEST)
13218 continue;
13219
Bram Moolenaar4770d092006-01-12 23:22:24 +000013220 if (flags & WF_KEEPCAP)
13221 {
13222 /* Must find the word in the keep-case tree. */
13223 find_keepcap_word(slang, theword, cword);
13224 p = cword;
13225 }
13226 else
13227 {
13228 flags |= su->su_badflags;
13229 if ((flags & WF_CAPMASK) != 0)
13230 {
13231 /* Need to fix case according to "flags". */
13232 make_case_word(theword, cword, flags);
13233 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013234 }
13235 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013236 p = theword;
13237 }
13238
13239 /* Add the suggestion. */
13240 if (sps_flags & SPS_DOUBLE)
13241 {
13242 /* Add the suggestion if the score isn't too bad. */
13243 if (score <= su->su_maxscore)
13244 add_suggestion(su, &su->su_sga, p, su->su_badlen,
13245 score, 0, FALSE, slang, FALSE);
13246 }
13247 else
13248 {
13249 /* Add a penalty for words in another region. */
13250 if ((flags & WF_REGION)
13251 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
13252 goodscore = SCORE_REGION;
13253 else
13254 goodscore = 0;
13255
13256 /* Add a small penalty for changing the first letter from
13257 * lower to upper case. Helps for "tath" -> "Kath", which is
13258 * less common thatn "tath" -> "path". Don't do it when the
13259 * letter is the same, that has already been counted. */
13260 gc = PTR2CHAR(p);
13261 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013262 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013263 bc = PTR2CHAR(su->su_badword);
13264 if (!SPELL_ISUPPER(bc)
13265 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
13266 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013267 }
13268
Bram Moolenaar4770d092006-01-12 23:22:24 +000013269 /* Compute the score for the good word. This only does letter
13270 * insert/delete/swap/replace. REP items are not considered,
13271 * which may make the score a bit higher.
13272 * Use a limit for the score to make it work faster. Use
13273 * MAXSCORE(), because RESCORE() will change the score.
13274 * If the limit is very high then the iterative method is
13275 * inefficient, using an array is quicker. */
13276 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
13277 if (limit > SCORE_LIMITMAX)
13278 goodscore += spell_edit_score(slang, su->su_badword, p);
13279 else
13280 goodscore += spell_edit_score_limit(slang, su->su_badword,
13281 p, limit);
13282
13283 /* When going over the limit don't bother to do the rest. */
13284 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013285 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013286 /* Give a bonus to words seen before. */
13287 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013288
Bram Moolenaar4770d092006-01-12 23:22:24 +000013289 /* Add the suggestion if the score isn't too bad. */
13290 goodscore = RESCORE(goodscore, score);
13291 if (goodscore <= su->su_sfmaxscore)
13292 add_suggestion(su, &su->su_ga, p, su->su_badlen,
13293 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013294 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013295 }
13296 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013297 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013298 }
13299}
13300
13301/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013302 * Find word "word" in fold-case tree for "slang" and return the word number.
13303 */
13304 static int
13305soundfold_find(slang, word)
13306 slang_T *slang;
13307 char_u *word;
13308{
13309 idx_T arridx = 0;
13310 int len;
13311 int wlen = 0;
13312 int c;
13313 char_u *ptr = word;
13314 char_u *byts;
13315 idx_T *idxs;
13316 int wordnr = 0;
13317
13318 byts = slang->sl_sbyts;
13319 idxs = slang->sl_sidxs;
13320
13321 for (;;)
13322 {
13323 /* First byte is the number of possible bytes. */
13324 len = byts[arridx++];
13325
13326 /* If the first possible byte is a zero the word could end here.
13327 * If the word ends we found the word. If not skip the NUL bytes. */
13328 c = ptr[wlen];
13329 if (byts[arridx] == NUL)
13330 {
13331 if (c == NUL)
13332 break;
13333
13334 /* Skip over the zeros, there can be several. */
13335 while (len > 0 && byts[arridx] == NUL)
13336 {
13337 ++arridx;
13338 --len;
13339 }
13340 if (len == 0)
13341 return -1; /* no children, word should have ended here */
13342 ++wordnr;
13343 }
13344
13345 /* If the word ends we didn't find it. */
13346 if (c == NUL)
13347 return -1;
13348
13349 /* Perform a binary search in the list of accepted bytes. */
13350 if (c == TAB) /* <Tab> is handled like <Space> */
13351 c = ' ';
13352 while (byts[arridx] < c)
13353 {
13354 /* The word count is in the first idxs[] entry of the child. */
13355 wordnr += idxs[idxs[arridx]];
13356 ++arridx;
13357 if (--len == 0) /* end of the bytes, didn't find it */
13358 return -1;
13359 }
13360 if (byts[arridx] != c) /* didn't find the byte */
13361 return -1;
13362
13363 /* Continue at the child (if there is one). */
13364 arridx = idxs[arridx];
13365 ++wlen;
13366
13367 /* One space in the good word may stand for several spaces in the
13368 * checked word. */
13369 if (c == ' ')
13370 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
13371 ++wlen;
13372 }
13373
13374 return wordnr;
13375}
13376
13377/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013378 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013379 */
13380 static void
13381make_case_word(fword, cword, flags)
13382 char_u *fword;
13383 char_u *cword;
13384 int flags;
13385{
13386 if (flags & WF_ALLCAP)
13387 /* Make it all upper-case */
13388 allcap_copy(fword, cword);
13389 else if (flags & WF_ONECAP)
13390 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013391 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013392 else
13393 /* Use goodword as-is. */
13394 STRCPY(cword, fword);
13395}
13396
Bram Moolenaarea424162005-06-16 21:51:00 +000013397/*
13398 * Use map string "map" for languages "lp".
13399 */
13400 static void
13401set_map_str(lp, map)
13402 slang_T *lp;
13403 char_u *map;
13404{
13405 char_u *p;
13406 int headc = 0;
13407 int c;
13408 int i;
13409
13410 if (*map == NUL)
13411 {
13412 lp->sl_has_map = FALSE;
13413 return;
13414 }
13415 lp->sl_has_map = TRUE;
13416
Bram Moolenaar4770d092006-01-12 23:22:24 +000013417 /* Init the array and hash tables empty. */
Bram Moolenaarea424162005-06-16 21:51:00 +000013418 for (i = 0; i < 256; ++i)
13419 lp->sl_map_array[i] = 0;
13420#ifdef FEAT_MBYTE
13421 hash_init(&lp->sl_map_hash);
13422#endif
13423
13424 /*
13425 * The similar characters are stored separated with slashes:
13426 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
13427 * before the same slash. For characters above 255 sl_map_hash is used.
13428 */
13429 for (p = map; *p != NUL; )
13430 {
13431#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013432 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000013433#else
13434 c = *p++;
13435#endif
13436 if (c == '/')
13437 headc = 0;
13438 else
13439 {
13440 if (headc == 0)
13441 headc = c;
13442
13443#ifdef FEAT_MBYTE
13444 /* Characters above 255 don't fit in sl_map_array[], put them in
13445 * the hash table. Each entry is the char, a NUL the headchar and
13446 * a NUL. */
13447 if (c >= 256)
13448 {
13449 int cl = mb_char2len(c);
13450 int headcl = mb_char2len(headc);
13451 char_u *b;
13452 hash_T hash;
13453 hashitem_T *hi;
13454
13455 b = alloc((unsigned)(cl + headcl + 2));
13456 if (b == NULL)
13457 return;
13458 mb_char2bytes(c, b);
13459 b[cl] = NUL;
13460 mb_char2bytes(headc, b + cl + 1);
13461 b[cl + 1 + headcl] = NUL;
13462 hash = hash_hash(b);
13463 hi = hash_lookup(&lp->sl_map_hash, b, hash);
13464 if (HASHITEM_EMPTY(hi))
13465 hash_add_item(&lp->sl_map_hash, hi, b, hash);
13466 else
13467 {
13468 /* This should have been checked when generating the .spl
13469 * file. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013470 EMSG(_("E783: duplicate char in MAP entry"));
Bram Moolenaarea424162005-06-16 21:51:00 +000013471 vim_free(b);
13472 }
13473 }
13474 else
13475#endif
13476 lp->sl_map_array[c] = headc;
13477 }
13478 }
13479}
13480
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013481/*
13482 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
13483 * lines in the .aff file.
13484 */
13485 static int
13486similar_chars(slang, c1, c2)
13487 slang_T *slang;
13488 int c1;
13489 int c2;
13490{
Bram Moolenaarea424162005-06-16 21:51:00 +000013491 int m1, m2;
13492#ifdef FEAT_MBYTE
13493 char_u buf[MB_MAXBYTES];
13494 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013495
Bram Moolenaarea424162005-06-16 21:51:00 +000013496 if (c1 >= 256)
13497 {
13498 buf[mb_char2bytes(c1, buf)] = 0;
13499 hi = hash_find(&slang->sl_map_hash, buf);
13500 if (HASHITEM_EMPTY(hi))
13501 m1 = 0;
13502 else
13503 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13504 }
13505 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013506#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000013507 m1 = slang->sl_map_array[c1];
13508 if (m1 == 0)
13509 return FALSE;
13510
13511
13512#ifdef FEAT_MBYTE
13513 if (c2 >= 256)
13514 {
13515 buf[mb_char2bytes(c2, buf)] = 0;
13516 hi = hash_find(&slang->sl_map_hash, buf);
13517 if (HASHITEM_EMPTY(hi))
13518 m2 = 0;
13519 else
13520 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13521 }
13522 else
13523#endif
13524 m2 = slang->sl_map_array[c2];
13525
13526 return m1 == m2;
13527}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013528
13529/*
13530 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +000013531 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013532 */
13533 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000013534add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus,
13535 slang, maxsf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013536 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013537 garray_T *gap; /* either su_ga or su_sga */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013538 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013539 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013540 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013541 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013542 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013543 slang_T *slang; /* language for sound folding */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013544 int maxsf; /* su_maxscore applies to soundfold score,
13545 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013546{
Bram Moolenaar4770d092006-01-12 23:22:24 +000013547 int goodlen; /* len of goodword changed */
13548 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013549 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013550 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013551 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013552 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013553
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013554 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13555 * "thee the" is added next to changing the first "the" the "thee". */
13556 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013557 pbad = su->su_badptr + badlenarg;
13558 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013559 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013560 goodlen = (int)(pgood - goodword);
13561 badlen = (int)(pbad - su->su_badptr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013562 if (goodlen <= 0 || badlen <= 0)
13563 break;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013564 mb_ptr_back(goodword, pgood);
13565 mb_ptr_back(su->su_badptr, pbad);
13566#ifdef FEAT_MBYTE
13567 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013568 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013569 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
13570 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013571 }
13572 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013573#endif
13574 if (*pgood != *pbad)
13575 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013576 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013577
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013578 if (badlen == 0 && goodlen == 0)
13579 /* goodword doesn't change anything; may happen for "the the" changing
13580 * the first "the" to itself. */
13581 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013582
Bram Moolenaar4770d092006-01-12 23:22:24 +000013583 /* Check if the word is already there. Also check the length that is
13584 * being replaced "thes," -> "these" is a different suggestion from
13585 * "thes" -> "these". */
13586 stp = &SUG(*gap, 0);
13587 for (i = gap->ga_len; --i >= 0; ++stp)
13588 if (stp->st_wordlen == goodlen
13589 && stp->st_orglen == badlen
13590 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
13591 {
13592 /*
13593 * Found it. Remember the word with the lowest score.
13594 */
13595 if (stp->st_slang == NULL)
13596 stp->st_slang = slang;
13597
13598 new_sug.st_score = score;
13599 new_sug.st_altscore = altscore;
13600 new_sug.st_had_bonus = had_bonus;
13601
13602 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013603 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013604 /* Only one of the two had the soundalike score computed.
13605 * Need to do that for the other one now, otherwise the
13606 * scores can't be compared. This happens because
13607 * suggest_try_change() doesn't compute the soundalike
13608 * word to keep it fast, while some special methods set
13609 * the soundalike score to zero. */
13610 if (had_bonus)
13611 rescore_one(su, stp);
13612 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013613 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013614 new_sug.st_word = stp->st_word;
13615 new_sug.st_wordlen = stp->st_wordlen;
13616 new_sug.st_slang = stp->st_slang;
13617 new_sug.st_orglen = badlen;
13618 rescore_one(su, &new_sug);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013619 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013620 }
13621
Bram Moolenaar4770d092006-01-12 23:22:24 +000013622 if (stp->st_score > new_sug.st_score)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013623 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013624 stp->st_score = new_sug.st_score;
13625 stp->st_altscore = new_sug.st_altscore;
13626 stp->st_had_bonus = new_sug.st_had_bonus;
13627 }
13628 break;
13629 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013630
Bram Moolenaar4770d092006-01-12 23:22:24 +000013631 if (i < 0 && ga_grow(gap, 1) == OK)
13632 {
13633 /* Add a suggestion. */
13634 stp = &SUG(*gap, gap->ga_len);
13635 stp->st_word = vim_strnsave(goodword, goodlen);
13636 if (stp->st_word != NULL)
13637 {
13638 stp->st_wordlen = goodlen;
13639 stp->st_score = score;
13640 stp->st_altscore = altscore;
13641 stp->st_had_bonus = had_bonus;
13642 stp->st_orglen = badlen;
13643 stp->st_slang = slang;
13644 ++gap->ga_len;
13645
13646 /* If we have too many suggestions now, sort the list and keep
13647 * the best suggestions. */
13648 if (gap->ga_len > SUG_MAX_COUNT(su))
13649 {
13650 if (maxsf)
13651 su->su_sfmaxscore = cleanup_suggestions(gap,
13652 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
13653 else
13654 {
13655 i = su->su_maxscore;
13656 su->su_maxscore = cleanup_suggestions(gap,
13657 su->su_maxscore, SUG_CLEAN_COUNT(su));
13658 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013659 }
13660 }
13661 }
13662}
13663
13664/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013665 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13666 * for split words, such as "the the". Remove these from the list here.
13667 */
13668 static void
13669check_suggestions(su, gap)
13670 suginfo_T *su;
13671 garray_T *gap; /* either su_ga or su_sga */
13672{
13673 suggest_T *stp;
13674 int i;
13675 char_u longword[MAXWLEN + 1];
13676 int len;
13677 hlf_T attr;
13678
13679 stp = &SUG(*gap, 0);
13680 for (i = gap->ga_len - 1; i >= 0; --i)
13681 {
13682 /* Need to append what follows to check for "the the". */
13683 STRCPY(longword, stp[i].st_word);
13684 len = stp[i].st_wordlen;
13685 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
13686 MAXWLEN - len);
13687 attr = HLF_COUNT;
13688 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
13689 if (attr != HLF_COUNT)
13690 {
13691 /* Remove this entry. */
13692 vim_free(stp[i].st_word);
13693 --gap->ga_len;
13694 if (i < gap->ga_len)
13695 mch_memmove(stp + i, stp + i + 1,
13696 sizeof(suggest_T) * (gap->ga_len - i));
13697 }
13698 }
13699}
13700
13701
13702/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013703 * Add a word to be banned.
13704 */
13705 static void
13706add_banned(su, word)
13707 suginfo_T *su;
13708 char_u *word;
13709{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013710 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013711 hash_T hash;
13712 hashitem_T *hi;
13713
Bram Moolenaar4770d092006-01-12 23:22:24 +000013714 hash = hash_hash(word);
13715 hi = hash_lookup(&su->su_banned, word, hash);
13716 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013717 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013718 s = vim_strsave(word);
13719 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013720 hash_add_item(&su->su_banned, hi, s, hash);
13721 }
13722}
13723
13724/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013725 * Recompute the score for all suggestions if sound-folding is possible. This
13726 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013727 */
13728 static void
13729rescore_suggestions(su)
13730 suginfo_T *su;
13731{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013732 int i;
13733
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013734 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013735 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013736 rescore_one(su, &SUG(su->su_ga, i));
13737}
13738
13739/*
13740 * Recompute the score for one suggestion if sound-folding is possible.
13741 */
13742 static void
13743rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013744 suginfo_T *su;
13745 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013746{
13747 slang_T *slang = stp->st_slang;
13748 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000013749 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013750
13751 /* Only rescore suggestions that have no sal score yet and do have a
13752 * language. */
13753 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
13754 {
13755 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013756 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013757 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013758 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013759 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000013760 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013761 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000013762
13763 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013764 if (stp->st_altscore == SCORE_MAXMAX)
13765 stp->st_altscore = SCORE_BIG;
13766 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
13767 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013768 }
13769}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013770
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013771static int
13772#ifdef __BORLANDC__
13773_RTLENTRYF
13774#endif
13775sug_compare __ARGS((const void *s1, const void *s2));
13776
13777/*
13778 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013779 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013780 */
13781 static int
13782#ifdef __BORLANDC__
13783_RTLENTRYF
13784#endif
13785sug_compare(s1, s2)
13786 const void *s1;
13787 const void *s2;
13788{
13789 suggest_T *p1 = (suggest_T *)s1;
13790 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013791 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013792
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013793 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013794 {
13795 n = p1->st_altscore - p2->st_altscore;
13796 if (n == 0)
13797 n = STRICMP(p1->st_word, p2->st_word);
13798 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013799 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013800}
13801
13802/*
13803 * Cleanup the suggestions:
13804 * - Sort on score.
13805 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013806 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013807 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013808 static int
13809cleanup_suggestions(gap, maxscore, keep)
13810 garray_T *gap;
13811 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013812 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013813{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013814 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013815 int i;
13816
13817 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013818 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013819
13820 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013821 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013822 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013823 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013824 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013825 gap->ga_len = keep;
13826 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013827 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013828 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013829}
13830
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013831#if defined(FEAT_EVAL) || defined(PROTO)
13832/*
13833 * Soundfold a string, for soundfold().
13834 * Result is in allocated memory, NULL for an error.
13835 */
13836 char_u *
13837eval_soundfold(word)
13838 char_u *word;
13839{
13840 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013841 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013842 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013843
13844 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13845 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013846 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013847 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013848 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013849 if (lp->lp_slang->sl_sal.ga_len > 0)
13850 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013851 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013852 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013853 return vim_strsave(sound);
13854 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013855 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013856
13857 /* No language with sound folding, return word as-is. */
13858 return vim_strsave(word);
13859}
13860#endif
13861
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013862/*
13863 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000013864 *
13865 * There are many ways to turn a word into a sound-a-like representation. The
13866 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
13867 * swedish name matching - survey and test of different algorithms" by Klas
13868 * Erikson.
13869 *
13870 * We support two methods:
13871 * 1. SOFOFROM/SOFOTO do a simple character mapping.
13872 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013873 */
13874 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013875spell_soundfold(slang, inword, folded, res)
13876 slang_T *slang;
13877 char_u *inword;
13878 int folded; /* "inword" is already case-folded */
13879 char_u *res;
13880{
13881 char_u fword[MAXWLEN];
13882 char_u *word;
13883
13884 if (slang->sl_sofo)
13885 /* SOFOFROM and SOFOTO used */
13886 spell_soundfold_sofo(slang, inword, res);
13887 else
13888 {
13889 /* SAL items used. Requires the word to be case-folded. */
13890 if (folded)
13891 word = inword;
13892 else
13893 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013894 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013895 word = fword;
13896 }
13897
13898#ifdef FEAT_MBYTE
13899 if (has_mbyte)
13900 spell_soundfold_wsal(slang, word, res);
13901 else
13902#endif
13903 spell_soundfold_sal(slang, word, res);
13904 }
13905}
13906
13907/*
13908 * Perform sound folding of "inword" into "res" according to SOFOFROM and
13909 * SOFOTO lines.
13910 */
13911 static void
13912spell_soundfold_sofo(slang, inword, res)
13913 slang_T *slang;
13914 char_u *inword;
13915 char_u *res;
13916{
13917 char_u *s;
13918 int ri = 0;
13919 int c;
13920
13921#ifdef FEAT_MBYTE
13922 if (has_mbyte)
13923 {
13924 int prevc = 0;
13925 int *ip;
13926
13927 /* The sl_sal_first[] table contains the translation for chars up to
13928 * 255, sl_sal the rest. */
13929 for (s = inword; *s != NUL; )
13930 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013931 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013932 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
13933 c = ' ';
13934 else if (c < 256)
13935 c = slang->sl_sal_first[c];
13936 else
13937 {
13938 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
13939 if (ip == NULL) /* empty list, can't match */
13940 c = NUL;
13941 else
13942 for (;;) /* find "c" in the list */
13943 {
13944 if (*ip == 0) /* not found */
13945 {
13946 c = NUL;
13947 break;
13948 }
13949 if (*ip == c) /* match! */
13950 {
13951 c = ip[1];
13952 break;
13953 }
13954 ip += 2;
13955 }
13956 }
13957
13958 if (c != NUL && c != prevc)
13959 {
13960 ri += mb_char2bytes(c, res + ri);
13961 if (ri + MB_MAXBYTES > MAXWLEN)
13962 break;
13963 prevc = c;
13964 }
13965 }
13966 }
13967 else
13968#endif
13969 {
13970 /* The sl_sal_first[] table contains the translation. */
13971 for (s = inword; (c = *s) != NUL; ++s)
13972 {
13973 if (vim_iswhite(c))
13974 c = ' ';
13975 else
13976 c = slang->sl_sal_first[c];
13977 if (c != NUL && (ri == 0 || res[ri - 1] != c))
13978 res[ri++] = c;
13979 }
13980 }
13981
13982 res[ri] = NUL;
13983}
13984
13985 static void
13986spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013987 slang_T *slang;
13988 char_u *inword;
13989 char_u *res;
13990{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013991 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013992 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013993 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013994 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013995 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013996 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013997 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013998 int n, k = 0;
13999 int z0;
14000 int k0;
14001 int n0;
14002 int c;
14003 int pri;
14004 int p0 = -333;
14005 int c0;
14006
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014007 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014008 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014009 if (slang->sl_rem_accents)
14010 {
14011 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014012 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014013 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014014 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014015 {
14016 *t++ = ' ';
14017 s = skipwhite(s);
14018 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014019 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014020 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014021 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014022 *t++ = *s;
14023 ++s;
14024 }
14025 }
14026 *t = NUL;
14027 }
14028 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014029 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014030
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014031 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014032
14033 /*
14034 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014035 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014036 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014037 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014038 while ((c = word[i]) != NUL)
14039 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014040 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014041 n = slang->sl_sal_first[c];
14042 z0 = 0;
14043
14044 if (n >= 0)
14045 {
14046 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014047 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014048 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014049 /* Quickly skip entries that don't match the word. Most
14050 * entries are less then three chars, optimize for that. */
14051 k = smp[n].sm_leadlen;
14052 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014053 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014054 if (word[i + 1] != s[1])
14055 continue;
14056 if (k > 2)
14057 {
14058 for (j = 2; j < k; ++j)
14059 if (word[i + j] != s[j])
14060 break;
14061 if (j < k)
14062 continue;
14063 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014064 }
14065
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014066 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014067 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014068 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014069 while (*pf != NUL && *pf != word[i + k])
14070 ++pf;
14071 if (*pf == NUL)
14072 continue;
14073 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014074 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014075 s = smp[n].sm_rules;
14076 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014077
14078 p0 = *s;
14079 k0 = k;
14080 while (*s == '-' && k > 1)
14081 {
14082 k--;
14083 s++;
14084 }
14085 if (*s == '<')
14086 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014087 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014088 {
14089 /* determine priority */
14090 pri = *s - '0';
14091 s++;
14092 }
14093 if (*s == '^' && *(s + 1) == '^')
14094 s++;
14095
14096 if (*s == NUL
14097 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014098 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014099 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014100 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014101 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014102 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014103 && spell_iswordp(word + i - 1, curbuf)
14104 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014105 {
14106 /* search for followup rules, if: */
14107 /* followup and k > 1 and NO '-' in searchstring */
14108 c0 = word[i + k - 1];
14109 n0 = slang->sl_sal_first[c0];
14110
14111 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014112 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014113 {
14114 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014115 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014116 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014117 /* Quickly skip entries that don't match the word.
14118 * */
14119 k0 = smp[n0].sm_leadlen;
14120 if (k0 > 1)
14121 {
14122 if (word[i + k] != s[1])
14123 continue;
14124 if (k0 > 2)
14125 {
14126 pf = word + i + k + 1;
14127 for (j = 2; j < k0; ++j)
14128 if (*pf++ != s[j])
14129 break;
14130 if (j < k0)
14131 continue;
14132 }
14133 }
14134 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014135
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014136 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014137 {
14138 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014139 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014140 while (*pf != NUL && *pf != word[i + k0])
14141 ++pf;
14142 if (*pf == NUL)
14143 continue;
14144 ++k0;
14145 }
14146
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014147 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014148 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014149 while (*s == '-')
14150 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014151 /* "k0" gets NOT reduced because
14152 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014153 s++;
14154 }
14155 if (*s == '<')
14156 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014157 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014158 {
14159 p0 = *s - '0';
14160 s++;
14161 }
14162
14163 if (*s == NUL
14164 /* *s == '^' cuts */
14165 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014166 && !spell_iswordp(word + i + k0,
14167 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014168 {
14169 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014170 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014171 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014172
14173 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014174 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014175 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014176 /* rule fits; stop search */
14177 break;
14178 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014179 }
14180
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014181 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014182 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014183 }
14184
14185 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014186 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014187 if (s == NULL)
14188 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014189 pf = smp[n].sm_rules;
14190 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014191 if (p0 == 1 && z == 0)
14192 {
14193 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014194 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
14195 || res[reslen - 1] == *s))
14196 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014197 z0 = 1;
14198 z = 1;
14199 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014200 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014201 {
14202 word[i + k0] = *s;
14203 k0++;
14204 s++;
14205 }
14206 if (k > k0)
14207 mch_memmove(word + i + k0, word + i + k,
14208 STRLEN(word + i + k) + 1);
14209
14210 /* new "actual letter" */
14211 c = word[i];
14212 }
14213 else
14214 {
14215 /* no '<' rule used */
14216 i += k - 1;
14217 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014218 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014219 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014220 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014221 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014222 s++;
14223 }
14224 /* new "actual letter" */
14225 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014226 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014227 {
14228 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014229 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014230 mch_memmove(word, word + i + 1,
14231 STRLEN(word + i + 1) + 1);
14232 i = 0;
14233 z0 = 1;
14234 }
14235 }
14236 break;
14237 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014238 }
14239 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014240 else if (vim_iswhite(c))
14241 {
14242 c = ' ';
14243 k = 1;
14244 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014245
14246 if (z0 == 0)
14247 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014248 if (k && !p0 && reslen < MAXWLEN && c != NUL
14249 && (!slang->sl_collapse || reslen == 0
14250 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014251 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014252 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014253
14254 i++;
14255 z = 0;
14256 k = 0;
14257 }
14258 }
14259
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014260 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014261}
14262
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014263#ifdef FEAT_MBYTE
14264/*
14265 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
14266 * Multi-byte version of spell_soundfold().
14267 */
14268 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014269spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014270 slang_T *slang;
14271 char_u *inword;
14272 char_u *res;
14273{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014274 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014275 int word[MAXWLEN];
14276 int wres[MAXWLEN];
14277 int l;
14278 char_u *s;
14279 int *ws;
14280 char_u *t;
14281 int *pf;
14282 int i, j, z;
14283 int reslen;
14284 int n, k = 0;
14285 int z0;
14286 int k0;
14287 int n0;
14288 int c;
14289 int pri;
14290 int p0 = -333;
14291 int c0;
14292 int did_white = FALSE;
14293
14294 /*
14295 * Convert the multi-byte string to a wide-character string.
14296 * Remove accents, if wanted. We actually remove all non-word characters.
14297 * But keep white space.
14298 */
14299 n = 0;
14300 for (s = inword; *s != NUL; )
14301 {
14302 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014303 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014304 if (slang->sl_rem_accents)
14305 {
14306 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14307 {
14308 if (did_white)
14309 continue;
14310 c = ' ';
14311 did_white = TRUE;
14312 }
14313 else
14314 {
14315 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014316 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014317 continue;
14318 }
14319 }
14320 word[n++] = c;
14321 }
14322 word[n] = NUL;
14323
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014324 /*
14325 * This comes from Aspell phonet.cpp.
14326 * Converted from C++ to C. Added support for multi-byte chars.
14327 * Changed to keep spaces.
14328 */
14329 i = reslen = z = 0;
14330 while ((c = word[i]) != NUL)
14331 {
14332 /* Start with the first rule that has the character in the word. */
14333 n = slang->sl_sal_first[c & 0xff];
14334 z0 = 0;
14335
14336 if (n >= 0)
14337 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014338 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014339 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
14340 {
14341 /* Quickly skip entries that don't match the word. Most
14342 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014343 if (c != ws[0])
14344 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014345 k = smp[n].sm_leadlen;
14346 if (k > 1)
14347 {
14348 if (word[i + 1] != ws[1])
14349 continue;
14350 if (k > 2)
14351 {
14352 for (j = 2; j < k; ++j)
14353 if (word[i + j] != ws[j])
14354 break;
14355 if (j < k)
14356 continue;
14357 }
14358 }
14359
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014360 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014361 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014362 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014363 while (*pf != NUL && *pf != word[i + k])
14364 ++pf;
14365 if (*pf == NUL)
14366 continue;
14367 ++k;
14368 }
14369 s = smp[n].sm_rules;
14370 pri = 5; /* default priority */
14371
14372 p0 = *s;
14373 k0 = k;
14374 while (*s == '-' && k > 1)
14375 {
14376 k--;
14377 s++;
14378 }
14379 if (*s == '<')
14380 s++;
14381 if (VIM_ISDIGIT(*s))
14382 {
14383 /* determine priority */
14384 pri = *s - '0';
14385 s++;
14386 }
14387 if (*s == '^' && *(s + 1) == '^')
14388 s++;
14389
14390 if (*s == NUL
14391 || (*s == '^'
14392 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014393 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014394 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014395 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014396 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014397 && spell_iswordp_w(word + i - 1, curbuf)
14398 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014399 {
14400 /* search for followup rules, if: */
14401 /* followup and k > 1 and NO '-' in searchstring */
14402 c0 = word[i + k - 1];
14403 n0 = slang->sl_sal_first[c0 & 0xff];
14404
14405 if (slang->sl_followup && k > 1 && n0 >= 0
14406 && p0 != '-' && word[i + k] != NUL)
14407 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014408 /* Test follow-up rule for "word[i + k]"; loop over
14409 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014410 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
14411 == (c0 & 0xff); ++n0)
14412 {
14413 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014414 */
14415 if (c0 != ws[0])
14416 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014417 k0 = smp[n0].sm_leadlen;
14418 if (k0 > 1)
14419 {
14420 if (word[i + k] != ws[1])
14421 continue;
14422 if (k0 > 2)
14423 {
14424 pf = word + i + k + 1;
14425 for (j = 2; j < k0; ++j)
14426 if (*pf++ != ws[j])
14427 break;
14428 if (j < k0)
14429 continue;
14430 }
14431 }
14432 k0 += k - 1;
14433
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014434 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014435 {
14436 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014437 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014438 while (*pf != NUL && *pf != word[i + k0])
14439 ++pf;
14440 if (*pf == NUL)
14441 continue;
14442 ++k0;
14443 }
14444
14445 p0 = 5;
14446 s = smp[n0].sm_rules;
14447 while (*s == '-')
14448 {
14449 /* "k0" gets NOT reduced because
14450 * "if (k0 == k)" */
14451 s++;
14452 }
14453 if (*s == '<')
14454 s++;
14455 if (VIM_ISDIGIT(*s))
14456 {
14457 p0 = *s - '0';
14458 s++;
14459 }
14460
14461 if (*s == NUL
14462 /* *s == '^' cuts */
14463 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014464 && !spell_iswordp_w(word + i + k0,
14465 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014466 {
14467 if (k0 == k)
14468 /* this is just a piece of the string */
14469 continue;
14470
14471 if (p0 < pri)
14472 /* priority too low */
14473 continue;
14474 /* rule fits; stop search */
14475 break;
14476 }
14477 }
14478
14479 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
14480 == (c0 & 0xff))
14481 continue;
14482 }
14483
14484 /* replace string */
14485 ws = smp[n].sm_to_w;
14486 s = smp[n].sm_rules;
14487 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
14488 if (p0 == 1 && z == 0)
14489 {
14490 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014491 if (reslen > 0 && ws != NULL && *ws != NUL
14492 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014493 || wres[reslen - 1] == *ws))
14494 reslen--;
14495 z0 = 1;
14496 z = 1;
14497 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014498 if (ws != NULL)
14499 while (*ws != NUL && word[i + k0] != NUL)
14500 {
14501 word[i + k0] = *ws;
14502 k0++;
14503 ws++;
14504 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014505 if (k > k0)
14506 mch_memmove(word + i + k0, word + i + k,
14507 sizeof(int) * (STRLEN(word + i + k) + 1));
14508
14509 /* new "actual letter" */
14510 c = word[i];
14511 }
14512 else
14513 {
14514 /* no '<' rule used */
14515 i += k - 1;
14516 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014517 if (ws != NULL)
14518 while (*ws != NUL && ws[1] != NUL
14519 && reslen < MAXWLEN)
14520 {
14521 if (reslen == 0 || wres[reslen - 1] != *ws)
14522 wres[reslen++] = *ws;
14523 ws++;
14524 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014525 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014526 if (ws == NULL)
14527 c = NUL;
14528 else
14529 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014530 if (strstr((char *)s, "^^") != NULL)
14531 {
14532 if (c != NUL)
14533 wres[reslen++] = c;
14534 mch_memmove(word, word + i + 1,
14535 sizeof(int) * (STRLEN(word + i + 1) + 1));
14536 i = 0;
14537 z0 = 1;
14538 }
14539 }
14540 break;
14541 }
14542 }
14543 }
14544 else if (vim_iswhite(c))
14545 {
14546 c = ' ';
14547 k = 1;
14548 }
14549
14550 if (z0 == 0)
14551 {
14552 if (k && !p0 && reslen < MAXWLEN && c != NUL
14553 && (!slang->sl_collapse || reslen == 0
14554 || wres[reslen - 1] != c))
14555 /* condense only double letters */
14556 wres[reslen++] = c;
14557
14558 i++;
14559 z = 0;
14560 k = 0;
14561 }
14562 }
14563
14564 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14565 l = 0;
14566 for (n = 0; n < reslen; ++n)
14567 {
14568 l += mb_char2bytes(wres[n], res + l);
14569 if (l + MB_MAXBYTES > MAXWLEN)
14570 break;
14571 }
14572 res[l] = NUL;
14573}
14574#endif
14575
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014576/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014577 * Compute a score for two sound-a-like words.
14578 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14579 * Instead of a generic loop we write out the code. That keeps it fast by
14580 * avoiding checks that will not be possible.
14581 */
14582 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014583soundalike_score(goodstart, badstart)
14584 char_u *goodstart; /* sound-folded good word */
14585 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014586{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014587 char_u *goodsound = goodstart;
14588 char_u *badsound = badstart;
14589 int goodlen;
14590 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014591 int n;
14592 char_u *pl, *ps;
14593 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014594 int score = 0;
14595
14596 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
14597 * counted so much, vowels halfway the word aren't counted at all. */
14598 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
14599 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014600 if (badsound[1] == goodsound[1]
14601 || (badsound[1] != NUL
14602 && goodsound[1] != NUL
14603 && badsound[2] == goodsound[2]))
14604 {
14605 /* handle like a substitute */
14606 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014607 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014608 {
14609 score = 2 * SCORE_DEL / 3;
14610 if (*badsound == '*')
14611 ++badsound;
14612 else
14613 ++goodsound;
14614 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014615 }
14616
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014617 goodlen = (int)STRLEN(goodsound);
14618 badlen = (int)STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014619
14620 /* Return quickly if the lenghts are too different to be fixed by two
14621 * changes. */
14622 n = goodlen - badlen;
14623 if (n < -2 || n > 2)
14624 return SCORE_MAXMAX;
14625
14626 if (n > 0)
14627 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014628 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014629 ps = badsound;
14630 }
14631 else
14632 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014633 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014634 ps = goodsound;
14635 }
14636
14637 /* Skip over the identical part. */
14638 while (*pl == *ps && *pl != NUL)
14639 {
14640 ++pl;
14641 ++ps;
14642 }
14643
14644 switch (n)
14645 {
14646 case -2:
14647 case 2:
14648 /*
14649 * Must delete two characters from "pl".
14650 */
14651 ++pl; /* first delete */
14652 while (*pl == *ps)
14653 {
14654 ++pl;
14655 ++ps;
14656 }
14657 /* strings must be equal after second delete */
14658 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014659 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014660
14661 /* Failed to compare. */
14662 break;
14663
14664 case -1:
14665 case 1:
14666 /*
14667 * Minimal one delete from "pl" required.
14668 */
14669
14670 /* 1: delete */
14671 pl2 = pl + 1;
14672 ps2 = ps;
14673 while (*pl2 == *ps2)
14674 {
14675 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014676 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014677 ++pl2;
14678 ++ps2;
14679 }
14680
14681 /* 2: delete then swap, then rest must be equal */
14682 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14683 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014684 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014685
14686 /* 3: delete then substitute, then the rest must be equal */
14687 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014688 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014689
14690 /* 4: first swap then delete */
14691 if (pl[0] == ps[1] && pl[1] == ps[0])
14692 {
14693 pl2 = pl + 2; /* swap, skip two chars */
14694 ps2 = ps + 2;
14695 while (*pl2 == *ps2)
14696 {
14697 ++pl2;
14698 ++ps2;
14699 }
14700 /* delete a char and then strings must be equal */
14701 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014702 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014703 }
14704
14705 /* 5: first substitute then delete */
14706 pl2 = pl + 1; /* substitute, skip one char */
14707 ps2 = ps + 1;
14708 while (*pl2 == *ps2)
14709 {
14710 ++pl2;
14711 ++ps2;
14712 }
14713 /* delete a char and then strings must be equal */
14714 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014715 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014716
14717 /* Failed to compare. */
14718 break;
14719
14720 case 0:
14721 /*
14722 * Lenghts are equal, thus changes must result in same length: An
14723 * insert is only possible in combination with a delete.
14724 * 1: check if for identical strings
14725 */
14726 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014727 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014728
14729 /* 2: swap */
14730 if (pl[0] == ps[1] && pl[1] == ps[0])
14731 {
14732 pl2 = pl + 2; /* swap, skip two chars */
14733 ps2 = ps + 2;
14734 while (*pl2 == *ps2)
14735 {
14736 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014737 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014738 ++pl2;
14739 ++ps2;
14740 }
14741 /* 3: swap and swap again */
14742 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14743 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014744 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014745
14746 /* 4: swap and substitute */
14747 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014748 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014749 }
14750
14751 /* 5: substitute */
14752 pl2 = pl + 1;
14753 ps2 = ps + 1;
14754 while (*pl2 == *ps2)
14755 {
14756 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014757 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014758 ++pl2;
14759 ++ps2;
14760 }
14761
14762 /* 6: substitute and swap */
14763 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14764 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014765 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014766
14767 /* 7: substitute and substitute */
14768 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014769 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014770
14771 /* 8: insert then delete */
14772 pl2 = pl;
14773 ps2 = ps + 1;
14774 while (*pl2 == *ps2)
14775 {
14776 ++pl2;
14777 ++ps2;
14778 }
14779 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014780 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014781
14782 /* 9: delete then insert */
14783 pl2 = pl + 1;
14784 ps2 = ps;
14785 while (*pl2 == *ps2)
14786 {
14787 ++pl2;
14788 ++ps2;
14789 }
14790 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014791 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014792
14793 /* Failed to compare. */
14794 break;
14795 }
14796
14797 return SCORE_MAXMAX;
14798}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014799
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014800/*
14801 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014802 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014803 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000014804 * The algorithm is described by Du and Chang, 1992.
14805 * The implementation of the algorithm comes from Aspell editdist.cpp,
14806 * edit_distance(). It has been converted from C++ to C and modified to
14807 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014808 */
14809 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +000014810spell_edit_score(slang, badword, goodword)
14811 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014812 char_u *badword;
14813 char_u *goodword;
14814{
14815 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014816 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014817 int j, i;
14818 int t;
14819 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014820 int pbc, pgc;
14821#ifdef FEAT_MBYTE
14822 char_u *p;
14823 int wbadword[MAXWLEN];
14824 int wgoodword[MAXWLEN];
14825
14826 if (has_mbyte)
14827 {
14828 /* Get the characters from the multi-byte strings and put them in an
14829 * int array for easy access. */
14830 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014831 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014832 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014833 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014834 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014835 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014836 }
14837 else
14838#endif
14839 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014840 badlen = (int)STRLEN(badword) + 1;
14841 goodlen = (int)STRLEN(goodword) + 1;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014842 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014843
14844 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
14845#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014846 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
14847 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014848 if (cnt == NULL)
14849 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014850
14851 CNT(0, 0) = 0;
14852 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +000014853 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014854
14855 for (i = 1; i <= badlen; ++i)
14856 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014857 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014858 for (j = 1; j <= goodlen; ++j)
14859 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014860#ifdef FEAT_MBYTE
14861 if (has_mbyte)
14862 {
14863 bc = wbadword[i - 1];
14864 gc = wgoodword[j - 1];
14865 }
14866 else
14867#endif
14868 {
14869 bc = badword[i - 1];
14870 gc = goodword[j - 1];
14871 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014872 if (bc == gc)
14873 CNT(i, j) = CNT(i - 1, j - 1);
14874 else
14875 {
14876 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014877 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014878 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
14879 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014880 {
14881 /* For a similar character use SCORE_SIMILAR. */
14882 if (slang != NULL
14883 && slang->sl_has_map
14884 && similar_chars(slang, gc, bc))
14885 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
14886 else
14887 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
14888 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014889
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014890 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014891 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014892#ifdef FEAT_MBYTE
14893 if (has_mbyte)
14894 {
14895 pbc = wbadword[i - 2];
14896 pgc = wgoodword[j - 2];
14897 }
14898 else
14899#endif
14900 {
14901 pbc = badword[i - 2];
14902 pgc = goodword[j - 2];
14903 }
14904 if (bc == pgc && pbc == gc)
14905 {
14906 t = SCORE_SWAP + CNT(i - 2, j - 2);
14907 if (t < CNT(i, j))
14908 CNT(i, j) = t;
14909 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014910 }
14911 t = SCORE_DEL + CNT(i - 1, j);
14912 if (t < CNT(i, j))
14913 CNT(i, j) = t;
14914 t = SCORE_INS + CNT(i, j - 1);
14915 if (t < CNT(i, j))
14916 CNT(i, j) = t;
14917 }
14918 }
14919 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014920
14921 i = CNT(badlen - 1, goodlen - 1);
14922 vim_free(cnt);
14923 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014924}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000014925
Bram Moolenaar4770d092006-01-12 23:22:24 +000014926typedef struct
14927{
14928 int badi;
14929 int goodi;
14930 int score;
14931} limitscore_T;
14932
14933/*
14934 * Like spell_edit_score(), but with a limit on the score to make it faster.
14935 * May return SCORE_MAXMAX when the score is higher than "limit".
14936 *
14937 * This uses a stack for the edits still to be tried.
14938 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
14939 * for multi-byte characters.
14940 */
14941 static int
14942spell_edit_score_limit(slang, badword, goodword, limit)
14943 slang_T *slang;
14944 char_u *badword;
14945 char_u *goodword;
14946 int limit;
14947{
14948 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
14949 int stackidx;
14950 int bi, gi;
14951 int bi2, gi2;
14952 int bc, gc;
14953 int score;
14954 int score_off;
14955 int minscore;
14956 int round;
14957
14958#ifdef FEAT_MBYTE
14959 /* Multi-byte characters require a bit more work, use a different function
14960 * to avoid testing "has_mbyte" quite often. */
14961 if (has_mbyte)
14962 return spell_edit_score_limit_w(slang, badword, goodword, limit);
14963#endif
14964
14965 /*
14966 * The idea is to go from start to end over the words. So long as
14967 * characters are equal just continue, this always gives the lowest score.
14968 * When there is a difference try several alternatives. Each alternative
14969 * increases "score" for the edit distance. Some of the alternatives are
14970 * pushed unto a stack and tried later, some are tried right away. At the
14971 * end of the word the score for one alternative is known. The lowest
14972 * possible score is stored in "minscore".
14973 */
14974 stackidx = 0;
14975 bi = 0;
14976 gi = 0;
14977 score = 0;
14978 minscore = limit + 1;
14979
14980 for (;;)
14981 {
14982 /* Skip over an equal part, score remains the same. */
14983 for (;;)
14984 {
14985 bc = badword[bi];
14986 gc = goodword[gi];
14987 if (bc != gc) /* stop at a char that's different */
14988 break;
14989 if (bc == NUL) /* both words end */
14990 {
14991 if (score < minscore)
14992 minscore = score;
14993 goto pop; /* do next alternative */
14994 }
14995 ++bi;
14996 ++gi;
14997 }
14998
14999 if (gc == NUL) /* goodword ends, delete badword chars */
15000 {
15001 do
15002 {
15003 if ((score += SCORE_DEL) >= minscore)
15004 goto pop; /* do next alternative */
15005 } while (badword[++bi] != NUL);
15006 minscore = score;
15007 }
15008 else if (bc == NUL) /* badword ends, insert badword chars */
15009 {
15010 do
15011 {
15012 if ((score += SCORE_INS) >= minscore)
15013 goto pop; /* do next alternative */
15014 } while (goodword[++gi] != NUL);
15015 minscore = score;
15016 }
15017 else /* both words continue */
15018 {
15019 /* If not close to the limit, perform a change. Only try changes
15020 * that may lead to a lower score than "minscore".
15021 * round 0: try deleting a char from badword
15022 * round 1: try inserting a char in badword */
15023 for (round = 0; round <= 1; ++round)
15024 {
15025 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15026 if (score_off < minscore)
15027 {
15028 if (score_off + SCORE_EDIT_MIN >= minscore)
15029 {
15030 /* Near the limit, rest of the words must match. We
15031 * can check that right now, no need to push an item
15032 * onto the stack. */
15033 bi2 = bi + 1 - round;
15034 gi2 = gi + round;
15035 while (goodword[gi2] == badword[bi2])
15036 {
15037 if (goodword[gi2] == NUL)
15038 {
15039 minscore = score_off;
15040 break;
15041 }
15042 ++bi2;
15043 ++gi2;
15044 }
15045 }
15046 else
15047 {
15048 /* try deleting/inserting a character later */
15049 stack[stackidx].badi = bi + 1 - round;
15050 stack[stackidx].goodi = gi + round;
15051 stack[stackidx].score = score_off;
15052 ++stackidx;
15053 }
15054 }
15055 }
15056
15057 if (score + SCORE_SWAP < minscore)
15058 {
15059 /* If swapping two characters makes a match then the
15060 * substitution is more expensive, thus there is no need to
15061 * try both. */
15062 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
15063 {
15064 /* Swap two characters, that is: skip them. */
15065 gi += 2;
15066 bi += 2;
15067 score += SCORE_SWAP;
15068 continue;
15069 }
15070 }
15071
15072 /* Substitute one character for another which is the same
15073 * thing as deleting a character from both goodword and badword.
15074 * Use a better score when there is only a case difference. */
15075 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15076 score += SCORE_ICASE;
15077 else
15078 {
15079 /* For a similar character use SCORE_SIMILAR. */
15080 if (slang != NULL
15081 && slang->sl_has_map
15082 && similar_chars(slang, gc, bc))
15083 score += SCORE_SIMILAR;
15084 else
15085 score += SCORE_SUBST;
15086 }
15087
15088 if (score < minscore)
15089 {
15090 /* Do the substitution. */
15091 ++gi;
15092 ++bi;
15093 continue;
15094 }
15095 }
15096pop:
15097 /*
15098 * Get here to try the next alternative, pop it from the stack.
15099 */
15100 if (stackidx == 0) /* stack is empty, finished */
15101 break;
15102
15103 /* pop an item from the stack */
15104 --stackidx;
15105 gi = stack[stackidx].goodi;
15106 bi = stack[stackidx].badi;
15107 score = stack[stackidx].score;
15108 }
15109
15110 /* When the score goes over "limit" it may actually be much higher.
15111 * Return a very large number to avoid going below the limit when giving a
15112 * bonus. */
15113 if (minscore > limit)
15114 return SCORE_MAXMAX;
15115 return minscore;
15116}
15117
15118#ifdef FEAT_MBYTE
15119/*
15120 * Multi-byte version of spell_edit_score_limit().
15121 * Keep it in sync with the above!
15122 */
15123 static int
15124spell_edit_score_limit_w(slang, badword, goodword, limit)
15125 slang_T *slang;
15126 char_u *badword;
15127 char_u *goodword;
15128 int limit;
15129{
15130 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15131 int stackidx;
15132 int bi, gi;
15133 int bi2, gi2;
15134 int bc, gc;
15135 int score;
15136 int score_off;
15137 int minscore;
15138 int round;
15139 char_u *p;
15140 int wbadword[MAXWLEN];
15141 int wgoodword[MAXWLEN];
15142
15143 /* Get the characters from the multi-byte strings and put them in an
15144 * int array for easy access. */
15145 bi = 0;
15146 for (p = badword; *p != NUL; )
15147 wbadword[bi++] = mb_cptr2char_adv(&p);
15148 wbadword[bi++] = 0;
15149 gi = 0;
15150 for (p = goodword; *p != NUL; )
15151 wgoodword[gi++] = mb_cptr2char_adv(&p);
15152 wgoodword[gi++] = 0;
15153
15154 /*
15155 * The idea is to go from start to end over the words. So long as
15156 * characters are equal just continue, this always gives the lowest score.
15157 * When there is a difference try several alternatives. Each alternative
15158 * increases "score" for the edit distance. Some of the alternatives are
15159 * pushed unto a stack and tried later, some are tried right away. At the
15160 * end of the word the score for one alternative is known. The lowest
15161 * possible score is stored in "minscore".
15162 */
15163 stackidx = 0;
15164 bi = 0;
15165 gi = 0;
15166 score = 0;
15167 minscore = limit + 1;
15168
15169 for (;;)
15170 {
15171 /* Skip over an equal part, score remains the same. */
15172 for (;;)
15173 {
15174 bc = wbadword[bi];
15175 gc = wgoodword[gi];
15176
15177 if (bc != gc) /* stop at a char that's different */
15178 break;
15179 if (bc == NUL) /* both words end */
15180 {
15181 if (score < minscore)
15182 minscore = score;
15183 goto pop; /* do next alternative */
15184 }
15185 ++bi;
15186 ++gi;
15187 }
15188
15189 if (gc == NUL) /* goodword ends, delete badword chars */
15190 {
15191 do
15192 {
15193 if ((score += SCORE_DEL) >= minscore)
15194 goto pop; /* do next alternative */
15195 } while (wbadword[++bi] != NUL);
15196 minscore = score;
15197 }
15198 else if (bc == NUL) /* badword ends, insert badword chars */
15199 {
15200 do
15201 {
15202 if ((score += SCORE_INS) >= minscore)
15203 goto pop; /* do next alternative */
15204 } while (wgoodword[++gi] != NUL);
15205 minscore = score;
15206 }
15207 else /* both words continue */
15208 {
15209 /* If not close to the limit, perform a change. Only try changes
15210 * that may lead to a lower score than "minscore".
15211 * round 0: try deleting a char from badword
15212 * round 1: try inserting a char in badword */
15213 for (round = 0; round <= 1; ++round)
15214 {
15215 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15216 if (score_off < minscore)
15217 {
15218 if (score_off + SCORE_EDIT_MIN >= minscore)
15219 {
15220 /* Near the limit, rest of the words must match. We
15221 * can check that right now, no need to push an item
15222 * onto the stack. */
15223 bi2 = bi + 1 - round;
15224 gi2 = gi + round;
15225 while (wgoodword[gi2] == wbadword[bi2])
15226 {
15227 if (wgoodword[gi2] == NUL)
15228 {
15229 minscore = score_off;
15230 break;
15231 }
15232 ++bi2;
15233 ++gi2;
15234 }
15235 }
15236 else
15237 {
15238 /* try deleting a character from badword later */
15239 stack[stackidx].badi = bi + 1 - round;
15240 stack[stackidx].goodi = gi + round;
15241 stack[stackidx].score = score_off;
15242 ++stackidx;
15243 }
15244 }
15245 }
15246
15247 if (score + SCORE_SWAP < minscore)
15248 {
15249 /* If swapping two characters makes a match then the
15250 * substitution is more expensive, thus there is no need to
15251 * try both. */
15252 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
15253 {
15254 /* Swap two characters, that is: skip them. */
15255 gi += 2;
15256 bi += 2;
15257 score += SCORE_SWAP;
15258 continue;
15259 }
15260 }
15261
15262 /* Substitute one character for another which is the same
15263 * thing as deleting a character from both goodword and badword.
15264 * Use a better score when there is only a case difference. */
15265 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15266 score += SCORE_ICASE;
15267 else
15268 {
15269 /* For a similar character use SCORE_SIMILAR. */
15270 if (slang != NULL
15271 && slang->sl_has_map
15272 && similar_chars(slang, gc, bc))
15273 score += SCORE_SIMILAR;
15274 else
15275 score += SCORE_SUBST;
15276 }
15277
15278 if (score < minscore)
15279 {
15280 /* Do the substitution. */
15281 ++gi;
15282 ++bi;
15283 continue;
15284 }
15285 }
15286pop:
15287 /*
15288 * Get here to try the next alternative, pop it from the stack.
15289 */
15290 if (stackidx == 0) /* stack is empty, finished */
15291 break;
15292
15293 /* pop an item from the stack */
15294 --stackidx;
15295 gi = stack[stackidx].goodi;
15296 bi = stack[stackidx].badi;
15297 score = stack[stackidx].score;
15298 }
15299
15300 /* When the score goes over "limit" it may actually be much higher.
15301 * Return a very large number to avoid going below the limit when giving a
15302 * bonus. */
15303 if (minscore > limit)
15304 return SCORE_MAXMAX;
15305 return minscore;
15306}
15307#endif
15308
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015309/*
15310 * ":spellinfo"
15311 */
15312/*ARGSUSED*/
15313 void
15314ex_spellinfo(eap)
15315 exarg_T *eap;
15316{
15317 int lpi;
15318 langp_T *lp;
15319 char_u *p;
15320
15321 if (no_spell_checking(curwin))
15322 return;
15323
15324 msg_start();
15325 for (lpi = 0; lpi < curbuf->b_langp.ga_len && !got_int; ++lpi)
15326 {
15327 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
15328 msg_puts((char_u *)"file: ");
15329 msg_puts(lp->lp_slang->sl_fname);
15330 msg_putchar('\n');
15331 p = lp->lp_slang->sl_info;
15332 if (p != NULL)
15333 {
15334 msg_puts(p);
15335 msg_putchar('\n');
15336 }
15337 }
15338 msg_end();
15339}
15340
Bram Moolenaar4770d092006-01-12 23:22:24 +000015341#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
15342#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015343#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +000015344#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
15345#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +000015346
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015347/*
15348 * ":spelldump"
15349 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015350 void
15351ex_spelldump(eap)
15352 exarg_T *eap;
15353{
15354 buf_T *buf = curbuf;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015355
15356 if (no_spell_checking(curwin))
15357 return;
15358
15359 /* Create a new empty buffer by splitting the window. */
15360 do_cmdline_cmd((char_u *)"new");
15361 if (!bufempty() || !buf_valid(buf))
15362 return;
15363
15364 spell_dump_compl(buf, NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
15365
15366 /* Delete the empty line that we started with. */
15367 if (curbuf->b_ml.ml_line_count > 1)
15368 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
15369
15370 redraw_later(NOT_VALID);
15371}
15372
15373/*
15374 * Go through all possible words and:
15375 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
15376 * "ic" and "dir" are not used.
15377 * 2. When "pat" is not NULL: add matching words to insert mode completion.
15378 */
15379 void
15380spell_dump_compl(buf, pat, ic, dir, dumpflags_arg)
15381 buf_T *buf; /* buffer with spell checking */
15382 char_u *pat; /* leading part of the word */
15383 int ic; /* ignore case */
15384 int *dir; /* direction for adding matches */
15385 int dumpflags_arg; /* DUMPFLAG_* */
15386{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015387 langp_T *lp;
15388 slang_T *slang;
15389 idx_T arridx[MAXWLEN];
15390 int curi[MAXWLEN];
15391 char_u word[MAXWLEN];
15392 int c;
15393 char_u *byts;
15394 idx_T *idxs;
15395 linenr_T lnum = 0;
15396 int round;
15397 int depth;
15398 int n;
15399 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015400 char_u *region_names = NULL; /* region names being used */
15401 int do_region = TRUE; /* dump region names and numbers */
15402 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015403 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015404 int dumpflags = dumpflags_arg;
15405 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015406
Bram Moolenaard0131a82006-03-04 21:46:13 +000015407 /* When ignoring case or when the pattern starts with capital pass this on
15408 * to dump_word(). */
15409 if (pat != NULL)
15410 {
15411 if (ic)
15412 dumpflags |= DUMPFLAG_ICASE;
15413 else
15414 {
15415 n = captype(pat, NULL);
15416 if (n == WF_ONECAP)
15417 dumpflags |= DUMPFLAG_ONECAP;
15418 else if (n == WF_ALLCAP
15419#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015420 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015421#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015422 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +000015423#endif
15424 )
15425 dumpflags |= DUMPFLAG_ALLCAP;
15426 }
15427 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015428
Bram Moolenaar7887d882005-07-01 22:33:52 +000015429 /* Find out if we can support regions: All languages must support the same
15430 * regions or none at all. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015431 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000015432 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015433 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000015434 p = lp->lp_slang->sl_regions;
15435 if (p[0] != 0)
15436 {
15437 if (region_names == NULL) /* first language with regions */
15438 region_names = p;
15439 else if (STRCMP(region_names, p) != 0)
15440 {
15441 do_region = FALSE; /* region names are different */
15442 break;
15443 }
15444 }
15445 }
15446
15447 if (do_region && region_names != NULL)
15448 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015449 if (pat == NULL)
15450 {
15451 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
15452 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15453 }
Bram Moolenaar7887d882005-07-01 22:33:52 +000015454 }
15455 else
15456 do_region = FALSE;
15457
15458 /*
15459 * Loop over all files loaded for the entries in 'spelllang'.
15460 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015461 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015462 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015463 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015464 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015465 if (slang->sl_fbyts == NULL) /* reloading failed */
15466 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015467
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015468 if (pat == NULL)
15469 {
15470 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
15471 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15472 }
15473
15474 /* When matching with a pattern and there are no prefixes only use
15475 * parts of the tree that match "pat". */
15476 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015477 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015478 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015479 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015480
15481 /* round 1: case-folded tree
15482 * round 2: keep-case tree */
15483 for (round = 1; round <= 2; ++round)
15484 {
15485 if (round == 1)
15486 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015487 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015488 byts = slang->sl_fbyts;
15489 idxs = slang->sl_fidxs;
15490 }
15491 else
15492 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015493 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015494 byts = slang->sl_kbyts;
15495 idxs = slang->sl_kidxs;
15496 }
15497 if (byts == NULL)
15498 continue; /* array is empty */
15499
15500 depth = 0;
15501 arridx[0] = 0;
15502 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015503 while (depth >= 0 && !got_int
15504 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015505 {
15506 if (curi[depth] > byts[arridx[depth]])
15507 {
15508 /* Done all bytes at this node, go up one level. */
15509 --depth;
15510 line_breakcheck();
Bram Moolenaara2031822006-03-07 22:29:51 +000015511 ins_compl_check_keys(50);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015512 }
15513 else
15514 {
15515 /* Do one more byte at this node. */
15516 n = arridx[depth] + curi[depth];
15517 ++curi[depth];
15518 c = byts[n];
15519 if (c == 0)
15520 {
15521 /* End of word, deal with the word.
15522 * Don't use keep-case words in the fold-case tree,
15523 * they will appear in the keep-case tree.
15524 * Only use the word when the region matches. */
15525 flags = (int)idxs[n];
15526 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015527 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000015528 && (do_region
15529 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015530 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015531 & lp->lp_region) != 0))
15532 {
15533 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015534 if (!do_region)
15535 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015536
15537 /* Dump the basic word if there is no prefix or
15538 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015539 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015540 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015541 {
15542 dump_word(slang, word, pat, dir,
15543 dumpflags, flags, lnum);
15544 if (pat == NULL)
15545 ++lnum;
15546 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015547
15548 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015549 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015550 lnum = dump_prefixes(slang, word, pat, dir,
15551 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015552 }
15553 }
15554 else
15555 {
15556 /* Normal char, go one level deeper. */
15557 word[depth++] = c;
15558 arridx[depth] = idxs[n];
15559 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015560
15561 /* Check if this characters matches with the pattern.
15562 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +000015563 * Always ignore case here, dump_word() will check
15564 * proper case later. This isn't exactly right when
15565 * length changes for multi-byte characters with
15566 * ignore case... */
15567 if (depth <= patlen
15568 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015569 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015570 }
15571 }
15572 }
15573 }
15574 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015575}
15576
15577/*
15578 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015579 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015580 */
15581 static void
Bram Moolenaard0131a82006-03-04 21:46:13 +000015582dump_word(slang, word, pat, dir, dumpflags, wordflags, lnum)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015583 slang_T *slang;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015584 char_u *word;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015585 char_u *pat;
15586 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015587 int dumpflags;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015588 int wordflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015589 linenr_T lnum;
15590{
15591 int keepcap = FALSE;
15592 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015593 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015594 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000015595 char_u badword[MAXWLEN + 10];
15596 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015597 int flags = wordflags;
15598
15599 if (dumpflags & DUMPFLAG_ONECAP)
15600 flags |= WF_ONECAP;
15601 if (dumpflags & DUMPFLAG_ALLCAP)
15602 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015603
Bram Moolenaar4770d092006-01-12 23:22:24 +000015604 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015605 {
15606 /* Need to fix case according to "flags". */
15607 make_case_word(word, cword, flags);
15608 p = cword;
15609 }
15610 else
15611 {
15612 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015613 if ((dumpflags & DUMPFLAG_KEEPCASE)
15614 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000015615 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015616 keepcap = TRUE;
15617 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015618 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015619
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015620 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015621 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015622 /* Add flags and regions after a slash. */
15623 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015624 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015625 STRCPY(badword, p);
15626 STRCAT(badword, "/");
15627 if (keepcap)
15628 STRCAT(badword, "=");
15629 if (flags & WF_BANNED)
15630 STRCAT(badword, "!");
15631 else if (flags & WF_RARE)
15632 STRCAT(badword, "?");
15633 if (flags & WF_REGION)
15634 for (i = 0; i < 7; ++i)
15635 if (flags & (0x10000 << i))
15636 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
15637 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015638 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015639
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015640 if (dumpflags & DUMPFLAG_COUNT)
15641 {
15642 hashitem_T *hi;
15643
15644 /* Include the word count for ":spelldump!". */
15645 hi = hash_find(&slang->sl_wordcount, tw);
15646 if (!HASHITEM_EMPTY(hi))
15647 {
15648 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
15649 tw, HI2WC(hi)->wc_count);
15650 p = IObuff;
15651 }
15652 }
15653
15654 ml_append(lnum, p, (colnr_T)0, FALSE);
15655 }
Bram Moolenaard0131a82006-03-04 21:46:13 +000015656 else if (((dumpflags & DUMPFLAG_ICASE)
15657 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
15658 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015659 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaarf3a67882006-05-05 21:09:41 +000015660 FALSE, NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015661 /* if dir was BACKWARD then honor it just once */
15662 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015663}
15664
15665/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000015666 * For ":spelldump": Find matching prefixes for "word". Prepend each to
15667 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015668 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015669 * Return the updated line number.
15670 */
15671 static linenr_T
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015672dump_prefixes(slang, word, pat, dir, dumpflags, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015673 slang_T *slang;
15674 char_u *word; /* case-folded word */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015675 char_u *pat;
15676 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015677 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015678 int flags; /* flags with prefix ID */
15679 linenr_T startlnum;
15680{
15681 idx_T arridx[MAXWLEN];
15682 int curi[MAXWLEN];
15683 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000015684 char_u word_up[MAXWLEN];
15685 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015686 int c;
15687 char_u *byts;
15688 idx_T *idxs;
15689 linenr_T lnum = startlnum;
15690 int depth;
15691 int n;
15692 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015693 int i;
15694
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015695 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +000015696 * upper-case letter in word_up[]. */
15697 c = PTR2CHAR(word);
15698 if (SPELL_TOUPPER(c) != c)
15699 {
15700 onecap_copy(word, word_up, TRUE);
15701 has_word_up = TRUE;
15702 }
15703
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015704 byts = slang->sl_pbyts;
15705 idxs = slang->sl_pidxs;
15706 if (byts != NULL) /* array not is empty */
15707 {
15708 /*
15709 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015710 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015711 */
15712 depth = 0;
15713 arridx[0] = 0;
15714 curi[0] = 1;
15715 while (depth >= 0 && !got_int)
15716 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015717 n = arridx[depth];
15718 len = byts[n];
15719 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015720 {
15721 /* Done all bytes at this node, go up one level. */
15722 --depth;
15723 line_breakcheck();
15724 }
15725 else
15726 {
15727 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015728 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015729 ++curi[depth];
15730 c = byts[n];
15731 if (c == 0)
15732 {
15733 /* End of prefix, find out how many IDs there are. */
15734 for (i = 1; i < len; ++i)
15735 if (byts[n + i] != 0)
15736 break;
15737 curi[depth] += i - 1;
15738
Bram Moolenaar53805d12005-08-01 07:08:33 +000015739 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
15740 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015741 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000015742 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015743 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015744 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015745 : flags, lnum);
15746 if (lnum != 0)
15747 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015748 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000015749
15750 /* Check for prefix that matches the word when the
15751 * first letter is upper-case, but only if the prefix has
15752 * a condition. */
15753 if (has_word_up)
15754 {
15755 c = valid_word_prefix(i, n, flags, word_up, slang,
15756 TRUE);
15757 if (c != 0)
15758 {
15759 vim_strncpy(prefix + depth, word_up,
15760 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015761 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015762 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015763 : flags, lnum);
15764 if (lnum != 0)
15765 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +000015766 }
15767 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015768 }
15769 else
15770 {
15771 /* Normal char, go one level deeper. */
15772 prefix[depth++] = c;
15773 arridx[depth] = idxs[n];
15774 curi[depth] = 1;
15775 }
15776 }
15777 }
15778 }
15779
15780 return lnum;
15781}
15782
Bram Moolenaar95529562005-08-25 21:21:38 +000015783/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015784 * Move "p" to the end of word "start".
15785 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +000015786 */
15787 char_u *
15788spell_to_word_end(start, buf)
15789 char_u *start;
15790 buf_T *buf;
15791{
15792 char_u *p = start;
15793
15794 while (*p != NUL && spell_iswordp(p, buf))
15795 mb_ptr_adv(p);
15796 return p;
15797}
15798
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015799#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015800/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015801 * For Insert mode completion CTRL-X s:
15802 * Find start of the word in front of column "startcol".
15803 * We don't check if it is badly spelled, with completion we can only change
15804 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015805 * Returns the column number of the word.
15806 */
15807 int
15808spell_word_start(startcol)
15809 int startcol;
15810{
15811 char_u *line;
15812 char_u *p;
15813 int col = 0;
15814
Bram Moolenaar95529562005-08-25 21:21:38 +000015815 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015816 return startcol;
15817
15818 /* Find a word character before "startcol". */
15819 line = ml_get_curline();
15820 for (p = line + startcol; p > line; )
15821 {
15822 mb_ptr_back(line, p);
15823 if (spell_iswordp_nmw(p))
15824 break;
15825 }
15826
15827 /* Go back to start of the word. */
15828 while (p > line)
15829 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015830 col = (int)(p - line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015831 mb_ptr_back(line, p);
15832 if (!spell_iswordp(p, curbuf))
15833 break;
15834 col = 0;
15835 }
15836
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015837 return col;
15838}
15839
15840/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000015841 * Need to check for 'spellcapcheck' now, the word is removed before
15842 * expand_spelling() is called. Therefore the ugly global variable.
15843 */
15844static int spell_expand_need_cap;
15845
15846 void
15847spell_expand_check_cap(col)
15848 colnr_T col;
15849{
15850 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
15851}
15852
15853/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015854 * Get list of spelling suggestions.
15855 * Used for Insert mode completion CTRL-X ?.
15856 * Returns the number of matches. The matches are in "matchp[]", array of
15857 * allocated strings.
15858 */
15859/*ARGSUSED*/
15860 int
15861expand_spelling(lnum, col, pat, matchp)
15862 linenr_T lnum;
15863 int col;
15864 char_u *pat;
15865 char_u ***matchp;
15866{
15867 garray_T ga;
15868
Bram Moolenaar4770d092006-01-12 23:22:24 +000015869 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015870 *matchp = ga.ga_data;
15871 return ga.ga_len;
15872}
15873#endif
15874
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000015875#endif /* FEAT_SPELL */