blob: 036f60c1842f76aa730c42c90800b5480624e77a [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 Moolenaar5195e452005-08-19 20:32:47 +0000188 * sectionID == SN_COMPOUND: <compmax> <compminlen> <compsylmax> <compflags>
189 * <compmax> 1 byte Maximum nr of words in compound word.
190 * <compminlen> 1 byte Minimal word length for compounding.
191 * <compsylmax> 1 byte Maximum nr of syllables in compound word.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000192 * <compflags> N bytes Flags from COMPOUNDRULE items, separated by
Bram Moolenaar5195e452005-08-19 20:32:47 +0000193 * slashes.
194 *
Bram Moolenaar78622822005-08-23 21:00:13 +0000195 * sectionID == SN_NOBREAK: (empty, its presence is enough)
196 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000197 * sectionID == SN_SYLLABLE: <syllable>
198 * <syllable> N bytes String from SYLLABLE item.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000199 *
200 * <LWORDTREE>: <wordtree>
201 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000202 * <KWORDTREE>: <wordtree>
203 *
204 * <PREFIXTREE>: <wordtree>
205 *
206 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000207 * <wordtree>: <nodecount> <nodedata> ...
208 *
209 * <nodecount> 4 bytes Number of nodes following. MSB first.
210 *
211 * <nodedata>: <siblingcount> <sibling> ...
212 *
213 * <siblingcount> 1 byte Number of siblings in this node. The siblings
214 * follow in sorted order.
215 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000216 * <sibling>: <byte> [ <nodeidx> <xbyte>
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000217 * | <flags> [<flags2>] [<region>] [<affixID>]
218 * | [<pflags>] <affixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000219 *
220 * <byte> 1 byte Byte value of the sibling. Special cases:
221 * BY_NOFLAGS: End of word without flags and for all
222 * regions.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000223 * For PREFIXTREE <affixID> and
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000224 * <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000225 * BY_FLAGS: End of word, <flags> follow.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000226 * For PREFIXTREE <pflags>, <affixID>
Bram Moolenaar53805d12005-08-01 07:08:33 +0000227 * and <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000228 * BY_FLAGS2: End of word, <flags> and <flags2>
229 * follow. Not used in PREFIXTREE.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000230 * BY_INDEX: Child of sibling is shared, <nodeidx>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000231 * and <xbyte> follow.
232 *
233 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
234 *
235 * <xbyte> 1 byte byte value of the sibling.
236 *
237 * <flags> 1 byte bitmask of:
238 * WF_ALLCAP word must have only capitals
239 * WF_ONECAP first char of word must be capital
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000240 * WF_KEEPCAP keep-case word
241 * WF_FIXCAP keep-case word, all caps not allowed
Bram Moolenaar51485f02005-06-04 21:55:20 +0000242 * WF_RARE rare word
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000243 * WF_BANNED bad word
Bram Moolenaar51485f02005-06-04 21:55:20 +0000244 * WF_REGION <region> follows
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000245 * WF_AFX <affixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000246 *
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000247 * <flags2> 1 byte Bitmask of:
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000248 * WF_HAS_AFF >> 8 word includes affix
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000249 * WF_NEEDCOMP >> 8 word only valid in compound
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000250 * WF_NOSUGGEST >> 8 word not used for suggestions
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000251 *
Bram Moolenaar53805d12005-08-01 07:08:33 +0000252 * <pflags> 1 byte bitmask of:
253 * WFP_RARE rare prefix
254 * WFP_NC non-combining prefix
255 * WFP_UP letter after prefix made upper case
256 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000257 * <region> 1 byte Bitmask for regions in which word is valid. When
258 * omitted it's valid in all regions.
259 * Lowest bit is for region 1.
260 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000261 * <affixID> 1 byte ID of affix that can be used with this word. In
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000262 * PREFIXTREE used for the required prefix ID.
263 *
264 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
265 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000266 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000267 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000268 */
269
Bram Moolenaar4770d092006-01-12 23:22:24 +0000270/*
271 * Vim .sug file format: <SUGHEADER>
272 * <SUGWORDTREE>
273 * <SUGTABLE>
274 *
275 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
276 *
277 * <fileID> 6 bytes "VIMsug"
278 * <versionnr> 1 byte VIMSUGVERSION
279 * <timestamp> 8 bytes timestamp that must match with .spl file
280 *
281 *
282 * <SUGWORDTREE>: <wordtree> (see above, no flags or region used)
283 *
284 *
285 * <SUGTABLE>: <sugwcount> <sugline> ...
286 *
287 * <sugwcount> 4 bytes number of <sugline> following
288 *
289 * <sugline>: <sugnr> ... NUL
290 *
291 * <sugnr>: X bytes word number that results in this soundfolded word,
292 * stored as an offset to the previous number in as
293 * few bytes as possible, see offset2bytes())
294 */
295
Bram Moolenaare19defe2005-03-21 08:23:33 +0000296#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000297# include "vimio.h" /* for lseek(), must be before vim.h */
Bram Moolenaare19defe2005-03-21 08:23:33 +0000298#endif
299
300#include "vim.h"
301
302#if defined(FEAT_SYN_HL) || defined(PROTO)
303
304#ifdef HAVE_FCNTL_H
305# include <fcntl.h>
306#endif
307
Bram Moolenaar4770d092006-01-12 23:22:24 +0000308#ifndef UNIX /* it's in os_unix.h for Unix */
309# include <time.h> /* for time_t */
310#endif
311
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000312#define MAXWLEN 250 /* Assume max. word len is this many bytes.
313 Some places assume a word length fits in a
314 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000315
Bram Moolenaare52325c2005-08-22 22:54:29 +0000316/* Type used for indexes in the word tree need to be at least 4 bytes. If int
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000317 * is 8 bytes we could use something smaller, but what? */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000318#if SIZEOF_INT > 3
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000319typedef int idx_T;
320#else
321typedef long idx_T;
322#endif
323
324/* Flags used for a word. Only the lowest byte can be used, the region byte
325 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000326#define WF_REGION 0x01 /* region byte follows */
327#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
328#define WF_ALLCAP 0x04 /* word must be all capitals */
329#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000330#define WF_BANNED 0x10 /* bad word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000331#define WF_AFX 0x20 /* affix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000332#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000333#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000334
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000335/* for <flags2>, shifted up one byte to be used in wn_flags */
336#define WF_HAS_AFF 0x0100 /* word includes affix */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000337#define WF_NEEDCOMP 0x0200 /* word only valid in compound */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000338#define WF_NOSUGGEST 0x0400 /* word not to be suggested */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000339
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000340/* only used for su_badflags */
341#define WF_MIXCAP 0x20 /* mix of upper and lower case: macaRONI */
342
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000343#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000344
Bram Moolenaar53805d12005-08-01 07:08:33 +0000345/* flags for <pflags> */
346#define WFP_RARE 0x01 /* rare prefix */
347#define WFP_NC 0x02 /* prefix is not combining */
348#define WFP_UP 0x04 /* to-upper prefix */
349
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000350/* Flags for postponed prefixes. Must be above affixID (one byte)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000351 * and prefcondnr (two bytes). */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000352#define WF_RAREPFX (WFP_RARE << 24) /* in sl_pidxs: flag for rare
353 * postponed prefix */
354#define WF_PFX_NC (WFP_NC << 24) /* in sl_pidxs: flag for non-combining
355 * postponed prefix */
356#define WF_PFX_UP (WFP_UP << 24) /* in sl_pidxs: flag for to-upper
357 * postponed prefix */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000358
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000359/* Special byte values for <byte>. Some are only used in the tree for
360 * postponed prefixes, some only in the other trees. This is a bit messy... */
361#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000362 * postponed prefix: no <pflags> */
363#define BY_INDEX 1 /* child is shared, index follows */
364#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
365 * postponed prefix: <pflags> follows */
366#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
367 * follow; never used in prefix tree */
368#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000369
Bram Moolenaar4770d092006-01-12 23:22:24 +0000370/* Info from "REP", "REPSAL" and "SAL" entries in ".aff" file used in si_rep,
371 * si_repsal, sl_rep, and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000372 * One replacement: from "ft_from" to "ft_to". */
373typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000374{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000375 char_u *ft_from;
376 char_u *ft_to;
377} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000378
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000379/* Info from "SAL" entries in ".aff" file used in sl_sal.
380 * The info is split for quick processing by spell_soundfold().
381 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
382typedef struct salitem_S
383{
384 char_u *sm_lead; /* leading letters */
385 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000386 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000387 char_u *sm_rules; /* rules like ^, $, priority */
388 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000389#ifdef FEAT_MBYTE
390 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000391 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000392 int *sm_to_w; /* wide character copy of "sm_to" */
393#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000394} salitem_T;
395
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000396#ifdef FEAT_MBYTE
397typedef int salfirst_T;
398#else
399typedef short salfirst_T;
400#endif
401
Bram Moolenaar5195e452005-08-19 20:32:47 +0000402/* Values for SP_*ERROR are negative, positive values are used by
403 * read_cnt_string(). */
404#define SP_TRUNCERROR -1 /* spell file truncated error */
405#define SP_FORMERROR -2 /* format error in spell file */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000406#define SP_OTHERERROR -3 /* other error while reading spell file */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000407
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000408/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000409 * Structure used to store words and other info for one language, loaded from
410 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000411 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
412 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
413 *
414 * The "byts" array stores the possible bytes in each tree node, preceded by
415 * the number of possible bytes, sorted on byte value:
416 * <len> <byte1> <byte2> ...
417 * The "idxs" array stores the index of the child node corresponding to the
418 * byte in "byts".
419 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000420 * the flags, region mask and affixID for the word. There may be several
421 * zeros in sequence for alternative flag/region/affixID combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000422 */
423typedef struct slang_S slang_T;
424struct slang_S
425{
426 slang_T *sl_next; /* next language */
427 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000428 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000429 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000430
Bram Moolenaar51485f02005-06-04 21:55:20 +0000431 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000432 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000433 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000434 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000435 char_u *sl_pbyts; /* prefix tree word bytes */
436 idx_T *sl_pidxs; /* prefix tree word indexes */
437
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000438 char_u *sl_info; /* infotext string or NULL */
439
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000440 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000441
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000442 char_u *sl_midword; /* MIDWORD string or NULL */
443
Bram Moolenaar4770d092006-01-12 23:22:24 +0000444 hashtab_T sl_wordcount; /* hashtable with word count, wordcount_T */
445
Bram Moolenaar5195e452005-08-19 20:32:47 +0000446 int sl_compmax; /* COMPOUNDMAX (default: MAXWLEN) */
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000447 int sl_compminlen; /* COMPOUNDMIN (default: 0) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000448 int sl_compsylmax; /* COMPOUNDSYLMAX (default: MAXWLEN) */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000449 regprog_T *sl_compprog; /* COMPOUNDRULE turned into a regexp progrm
Bram Moolenaar5195e452005-08-19 20:32:47 +0000450 * (NULL when no compounding) */
451 char_u *sl_compstartflags; /* flags for first compound word */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000452 char_u *sl_compallflags; /* all flags for compound words */
Bram Moolenaar78622822005-08-23 21:00:13 +0000453 char_u sl_nobreak; /* When TRUE: no spaces between words */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000454 char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */
455 garray_T sl_syl_items; /* syllable items */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000456
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000457 int sl_prefixcnt; /* number of items in "sl_prefprog" */
458 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
459
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000460 garray_T sl_rep; /* list of fromto_T entries from REP lines */
461 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
462 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000463 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000464 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000465 there is none */
466 int sl_followup; /* SAL followup */
467 int sl_collapse; /* SAL collapse_result */
468 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000469 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
470 * "sl_sal_first" maps chars, when has_mbyte
471 * "sl_sal" is a list of wide char lists. */
472 garray_T sl_repsal; /* list of fromto_T entries from REPSAL lines */
473 short sl_repsal_first[256]; /* sl_rep_first for REPSAL lines */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000474 int sl_nosplitsugs; /* don't suggest splitting a word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000475
476 /* Info from the .sug file. Loaded on demand. */
477 time_t sl_sugtime; /* timestamp for .sug file */
478 char_u *sl_sbyts; /* soundfolded word bytes */
479 idx_T *sl_sidxs; /* soundfolded word indexes */
480 buf_T *sl_sugbuf; /* buffer with word number table */
481 int sl_sugloaded; /* TRUE when .sug file was loaded or failed to
482 load */
483
Bram Moolenaarea424162005-06-16 21:51:00 +0000484 int sl_has_map; /* TRUE if there is a MAP line */
485#ifdef FEAT_MBYTE
486 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
487 int sl_map_array[256]; /* MAP for first 256 chars */
488#else
489 char_u sl_map_array[256]; /* MAP for first 256 chars */
490#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000491 hashtab_T sl_sounddone; /* table with soundfolded words that have
492 handled, see add_sound_suggest() */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000493};
494
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000495/* First language that is loaded, start of the linked list of loaded
496 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000497static slang_T *first_lang = NULL;
498
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000499/* Flags used in .spl file for soundsalike flags. */
500#define SAL_F0LLOWUP 1
501#define SAL_COLLAPSE 2
502#define SAL_REM_ACCENTS 4
503
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000504/*
505 * Structure used in "b_langp", filled from 'spelllang'.
506 */
507typedef struct langp_S
508{
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000509 slang_T *lp_slang; /* info for this language */
510 slang_T *lp_sallang; /* language used for sound folding or NULL */
511 slang_T *lp_replang; /* language used for REP items or NULL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000512 int lp_region; /* bitmask for region or REGION_ALL */
513} langp_T;
514
515#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
516
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000517#define REGION_ALL 0xff /* word valid in all regions */
518
Bram Moolenaar5195e452005-08-19 20:32:47 +0000519#define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
520#define VIMSPELLMAGICL 8
521#define VIMSPELLVERSION 50
522
Bram Moolenaar4770d092006-01-12 23:22:24 +0000523#define VIMSUGMAGIC "VIMsug" /* string at start of Vim .sug file */
524#define VIMSUGMAGICL 6
525#define VIMSUGVERSION 1
526
Bram Moolenaar5195e452005-08-19 20:32:47 +0000527/* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
528#define SN_REGION 0 /* <regionname> section */
529#define SN_CHARFLAGS 1 /* charflags section */
530#define SN_MIDWORD 2 /* <midword> section */
531#define SN_PREFCOND 3 /* <prefcond> section */
532#define SN_REP 4 /* REP items section */
533#define SN_SAL 5 /* SAL items section */
534#define SN_SOFO 6 /* soundfolding section */
535#define SN_MAP 7 /* MAP items section */
536#define SN_COMPOUND 8 /* compound words section */
537#define SN_SYLLABLE 9 /* syllable section */
Bram Moolenaar78622822005-08-23 21:00:13 +0000538#define SN_NOBREAK 10 /* NOBREAK section */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000539#define SN_SUGFILE 11 /* timestamp for .sug file */
540#define SN_REPSAL 12 /* REPSAL items section */
541#define SN_WORDS 13 /* common words */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000542#define SN_NOSPLITSUGS 14 /* don't split word for suggestions */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000543#define SN_INFO 15 /* info section */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000544#define SN_END 255 /* end of sections */
545
546#define SNF_REQUIRED 1 /* <sectionflags>: required section */
547
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000548/* Result values. Lower number is accepted over higher one. */
549#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000550#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000551#define SP_RARE 1
552#define SP_LOCAL 2
553#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000554
Bram Moolenaar7887d882005-07-01 22:33:52 +0000555/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000556static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000557
Bram Moolenaar4770d092006-01-12 23:22:24 +0000558typedef struct wordcount_S
559{
560 short_u wc_count; /* nr of times word was seen */
561 char_u wc_word[1]; /* word, actually longer */
562} wordcount_T;
563
564static wordcount_T dumwc;
565#define WC_KEY_OFF (dumwc.wc_word - (char_u *)&dumwc)
566#define HI2WC(hi) ((wordcount_T *)((hi)->hi_key - WC_KEY_OFF))
567#define MAXWORDCOUNT 0xffff
568
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000569/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000570 * Information used when looking for suggestions.
571 */
572typedef struct suginfo_S
573{
574 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000575 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000576 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000577 int su_sfmaxscore; /* idem, for when doing soundfold words */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000578 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000579 char_u *su_badptr; /* start of bad word in line */
580 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000581 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000582 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
583 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000584 char_u su_sal_badword[MAXWLEN]; /* su_badword soundfolded */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000585 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000586 slang_T *su_sallang; /* default language for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000587} suginfo_T;
588
589/* One word suggestion. Used in "si_ga". */
590typedef struct suggest_S
591{
592 char_u *st_word; /* suggested word, allocated string */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000593 int st_wordlen; /* STRLEN(st_word) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000594 int st_orglen; /* length of replaced text */
595 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000596 int st_altscore; /* used when st_score compares equal */
597 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000598 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000599 slang_T *st_slang; /* language used for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000600} suggest_T;
601
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000602#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000603
Bram Moolenaar4770d092006-01-12 23:22:24 +0000604/* TRUE if a word appears in the list of banned words. */
605#define WAS_BANNED(su, word) (!HASHITEM_EMPTY(hash_find(&su->su_banned, word)))
606
607/* Number of suggestions kept when cleaning up. we need to keep more than
608 * what is displayed, because when rescore_suggestions() is called the score
609 * may change and wrong suggestions may be removed later. */
610#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 130 ? 150 : (su)->su_maxcount + 20)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000611
612/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
613 * of suggestions that are not going to be displayed. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000614#define SUG_MAX_COUNT(su) (SUG_CLEAN_COUNT(su) + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000615
616/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000617#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000618#define SCORE_SPLIT_NO 249 /* split bad word with NOSPLITSUGS */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000619#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000620#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000621#define SCORE_RARE 180 /* rare word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000622#define SCORE_SWAP 75 /* swap two characters */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000623#define SCORE_SWAP3 110 /* swap two characters in three */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000624#define SCORE_REP 65 /* REP replacement */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000625#define SCORE_SUBST 93 /* substitute a character */
626#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000627#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000628#define SCORE_DEL 94 /* delete a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000629#define SCORE_DELDUP 66 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000630#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000631#define SCORE_INS 96 /* insert a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000632#define SCORE_INSDUP 67 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000633#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000634#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000635
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000636#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000637#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
638 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000639
Bram Moolenaar4770d092006-01-12 23:22:24 +0000640#define SCORE_COMMON1 30 /* subtracted for words seen before */
641#define SCORE_COMMON2 40 /* subtracted for words often seen */
642#define SCORE_COMMON3 50 /* subtracted for words very often seen */
643#define SCORE_THRES2 10 /* word count threshold for COMMON2 */
644#define SCORE_THRES3 100 /* word count threshold for COMMON3 */
645
646/* When trying changed soundfold words it becomes slow when trying more than
647 * two changes. With less then two changes it's slightly faster but we miss a
648 * few good suggestions. In rare cases we need to try three of four changes.
649 */
650#define SCORE_SFMAX1 200 /* maximum score for first try */
651#define SCORE_SFMAX2 300 /* maximum score for second try */
652#define SCORE_SFMAX3 400 /* maximum score for third try */
653
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000654#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000655#define SCORE_MAXMAX 999999 /* accept any score */
656#define SCORE_LIMITMAX 350 /* for spell_edit_score_limit() */
657
658/* for spell_edit_score_limit() we need to know the minimum value of
659 * SCORE_ICASE, SCORE_SWAP, SCORE_DEL, SCORE_SIMILAR and SCORE_INS */
660#define SCORE_EDIT_MIN SCORE_SIMILAR
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000661
662/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000663 * Structure to store info for word matching.
664 */
665typedef struct matchinf_S
666{
667 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000668
669 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000670 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000671 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000672 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000673 char_u *mi_cend; /* char after what was used for
674 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000675
676 /* case-folded text */
677 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000678 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000679
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000680 /* for when checking word after a prefix */
681 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000682 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000683 int mi_prefcnt; /* number of entries at mi_prefarridx */
684 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000685#ifdef FEAT_MBYTE
686 int mi_cprefixlen; /* byte length of prefix in original
687 case */
688#else
689# define mi_cprefixlen mi_prefixlen /* it's the same value */
690#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000691
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000692 /* for when checking a compound word */
693 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000694 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
695 int mi_complen; /* nr of compound words used */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000696
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000697 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000698 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000699 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000700 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000701
702 /* for NOBREAK */
703 int mi_result2; /* "mi_resul" without following word */
704 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000705} matchinf_T;
706
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000707/*
708 * The tables used for recognizing word characters according to spelling.
709 * These are only used for the first 256 characters of 'encoding'.
710 */
711typedef struct spelltab_S
712{
713 char_u st_isw[256]; /* flags: is word char */
714 char_u st_isu[256]; /* flags: is uppercase char */
715 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000716 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000717} spelltab_T;
718
719static spelltab_T spelltab;
720static int did_set_spelltab;
721
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000722#define CF_WORD 0x01
723#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000724
725static void clear_spell_chartab __ARGS((spelltab_T *sp));
726static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000727static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
728static int spell_iswordp_nmw __ARGS((char_u *p));
729#ifdef FEAT_MBYTE
730static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
731#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000732static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000733
734/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000735 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000736 */
737typedef enum
738{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000739 STATE_START = 0, /* At start of node check for NUL bytes (goodword
740 * ends); if badword ends there is a match, otherwise
741 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000742 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000743 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000744 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
745 STATE_PLAIN, /* Use each byte of the node. */
746 STATE_DEL, /* Delete a byte from the bad word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000747 STATE_INS_PREP, /* Prepare for inserting bytes. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000748 STATE_INS, /* Insert a byte in the bad word. */
749 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000750 STATE_UNSWAP, /* Undo swap two characters. */
751 STATE_SWAP3, /* Swap two characters over three. */
752 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000753 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000754 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000755 STATE_REP_INI, /* Prepare for using REP items. */
756 STATE_REP, /* Use matching REP items from the .aff file. */
757 STATE_REP_UNDO, /* Undo a REP item replacement. */
758 STATE_FINAL /* End of this node. */
759} state_T;
760
761/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000762 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000763 */
764typedef struct trystate_S
765{
Bram Moolenaarea424162005-06-16 21:51:00 +0000766 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000767 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000768 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000769 short ts_curi; /* index in list of child nodes */
770 char_u ts_fidx; /* index in fword[], case-folded bad word */
771 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
772 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000773 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000774 * PFD_PREFIXTREE or PFD_NOPREFIX */
775 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000776#ifdef FEAT_MBYTE
777 char_u ts_tcharlen; /* number of bytes in tword character */
778 char_u ts_tcharidx; /* current byte index in tword character */
779 char_u ts_isdiff; /* DIFF_ values */
780 char_u ts_fcharstart; /* index in fword where badword char started */
781#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000782 char_u ts_prewordlen; /* length of word in "preword[]" */
783 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000784 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000785 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000786 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000787 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000788 char_u ts_delidx; /* index in fword for char that was deleted,
789 valid when "ts_flags" has TSF_DIDDEL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000790} trystate_T;
791
Bram Moolenaarea424162005-06-16 21:51:00 +0000792/* values for ts_isdiff */
793#define DIFF_NONE 0 /* no different byte (yet) */
794#define DIFF_YES 1 /* different byte found */
795#define DIFF_INSERT 2 /* inserting character */
796
Bram Moolenaard12a1322005-08-21 22:08:24 +0000797/* values for ts_flags */
798#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
799#define TSF_DIDSPLIT 2 /* tried split at this point */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000800#define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000801
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000802/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000803#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000804#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000805#define PFD_NOTSPECIAL 0xfd /* highest value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000806
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000807/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000808#define FIND_FOLDWORD 0 /* find word case-folded */
809#define FIND_KEEPWORD 1 /* find keep-case word */
810#define FIND_PREFIX 2 /* find word after prefix */
811#define FIND_COMPOUND 3 /* find case-folded compound word */
812#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000813
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000814static slang_T *slang_alloc __ARGS((char_u *lang));
815static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000816static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000817static void slang_clear_sug __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000818static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000819static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000820static 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 +0000821static void find_prefix __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000822static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000823static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaar95529562005-08-25 21:21:38 +0000824static int no_spell_checking __ARGS((win_T *wp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000825static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000826static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000827static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000828static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000829static 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 +0000830static int get2c __ARGS((FILE *fd));
831static int get3c __ARGS((FILE *fd));
832static int get4c __ARGS((FILE *fd));
833static time_t get8c __ARGS((FILE *fd));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000834static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000835static char_u *read_string __ARGS((FILE *fd, int cnt));
836static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
837static int read_charflags_section __ARGS((FILE *fd));
838static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000839static int read_rep_section __ARGS((FILE *fd, garray_T *gap, short *first));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000840static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000841static int read_words_section __ARGS((FILE *fd, slang_T *lp, int len));
842static void count_common_word __ARGS((slang_T *lp, char_u *word, int len, int count));
843static int score_wordcount_adj __ARGS((slang_T *slang, int score, char_u *word, int split));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000844static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
845static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
Bram Moolenaar6de68532005-08-24 22:08:48 +0000846static int byte_in_str __ARGS((char_u *str, int byte));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000847static int init_syl_tab __ARGS((slang_T *slang));
848static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000849static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
850static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000851#ifdef FEAT_MBYTE
852static int *mb_str2wide __ARGS((char_u *s));
853#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000854static int spell_read_tree __ARGS((FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt));
855static 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 +0000856static void clear_midword __ARGS((buf_T *buf));
857static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000858static int find_region __ARGS((char_u *rp, char_u *region));
859static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000860static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000861static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000862static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000863static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000864static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000865static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar66fa2712006-01-22 23:22:22 +0000866static 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 +0000867#ifdef FEAT_EVAL
868static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
869#endif
870static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000871static void spell_suggest_intern __ARGS((suginfo_T *su, int interactive));
872static void suggest_load_files __ARGS((void));
873static void tree_count_words __ARGS((char_u *byts, idx_T *idxs));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000874static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000875static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000876static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000877static void suggest_try_special __ARGS((suginfo_T *su));
878static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000879static void suggest_trie_walk __ARGS((suginfo_T *su, langp_T *lp, char_u *fword, int soundfold));
880static void go_deeper __ARGS((trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000881#ifdef FEAT_MBYTE
882static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
883#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000884static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000885static void score_comp_sal __ARGS((suginfo_T *su));
886static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000887static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000888static void suggest_try_soundalike_prep __ARGS((void));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000889static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000890static void suggest_try_soundalike_finish __ARGS((void));
891static void add_sound_suggest __ARGS((suginfo_T *su, char_u *goodword, int score, langp_T *lp));
892static int soundfold_find __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000893static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000894static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000895static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000896static 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));
897static void check_suggestions __ARGS((suginfo_T *su, garray_T *gap));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000898static void add_banned __ARGS((suginfo_T *su, char_u *word));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000899static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000900static void rescore_one __ARGS((suginfo_T *su, suggest_T *stp));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000901static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000902static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
903static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
904static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000905#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000906static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000907#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000908static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000909static int spell_edit_score __ARGS((slang_T *slang, char_u *badword, char_u *goodword));
910static int spell_edit_score_limit __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
911#ifdef FEAT_MBYTE
912static int spell_edit_score_limit_w __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
913#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +0000914static void dump_word __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum));
915static 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 +0000916static buf_T *open_spellbuf __ARGS((void));
917static void close_spellbuf __ARGS((buf_T *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000918
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000919/*
920 * Use our own character-case definitions, because the current locale may
921 * differ from what the .spl file uses.
922 * These must not be called with negative number!
923 */
924#ifndef FEAT_MBYTE
925/* Non-multi-byte implementation. */
926# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
927# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
928# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
929#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000930# if defined(HAVE_WCHAR_H)
931# include <wchar.h> /* for towupper() and towlower() */
932# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000933/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
934 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
935 * the "w" library function for characters above 255 if available. */
936# ifdef HAVE_TOWLOWER
937# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
938 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
939# else
940# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
941 : (c) < 256 ? spelltab.st_fold[c] : (c))
942# endif
943
944# ifdef HAVE_TOWUPPER
945# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
946 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
947# else
948# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
949 : (c) < 256 ? spelltab.st_upper[c] : (c))
950# endif
951
952# ifdef HAVE_ISWUPPER
953# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
954 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
955# else
956# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000957 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000958# endif
959#endif
960
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000961
962static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000963static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000964static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar6de68532005-08-24 22:08:48 +0000965static char *e_affname = N_("Affix name too long in %s line %d: %s");
966static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
967static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000968static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000969
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000970/* Remember what "z?" replaced. */
971static char_u *repl_from = NULL;
972static char_u *repl_to = NULL;
973
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000974/*
975 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000976 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000977 * "*attrp" is set to the highlight index for a badly spelled word. For a
978 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000979 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000980 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000981 * "capcol" is used to check for a Capitalised word after the end of a
982 * sentence. If it's zero then perform the check. Return the column where to
983 * check next, or -1 when no sentence end was found. If it's NULL then don't
984 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000985 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000986 * Returns the length of the word in bytes, also when it's OK, so that the
987 * caller can skip over the word.
988 */
989 int
Bram Moolenaar4770d092006-01-12 23:22:24 +0000990spell_check(wp, ptr, attrp, capcol, docount)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000991 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000992 char_u *ptr;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000993 hlf_T *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000994 int *capcol; /* column to check for Capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000995 int docount; /* count good words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000996{
997 matchinf_T mi; /* Most things are put in "mi" so that it can
998 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000999 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001000 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001001 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001002 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001003 int count_word = docount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001004
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001005 /* A word never starts at a space or a control character. Return quickly
1006 * then, skipping over the character. */
1007 if (*ptr <= ' ')
1008 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001009
1010 /* Return here when loading language files failed. */
1011 if (wp->w_buffer->b_langp.ga_len == 0)
1012 return 1;
1013
Bram Moolenaar5195e452005-08-19 20:32:47 +00001014 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001015
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001016 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +00001017 * 0X99FF. But always do check spelling to find "3GPP" and "11
1018 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001019 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +00001020 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00001021 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
1022 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001023 else
1024 mi.mi_end = skipdigits(ptr);
Bram Moolenaar43abc522005-12-10 20:15:02 +00001025 nrlen = mi.mi_end - ptr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001026 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001027
Bram Moolenaar0c405862005-06-22 22:26:26 +00001028 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001029 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +00001030 mi.mi_fend = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001031 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001032 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001033 do
Bram Moolenaar51485f02005-06-04 21:55:20 +00001034 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001035 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001036 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001037
1038 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
1039 {
1040 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00001041 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001042 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001043 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001044 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001045 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001046 if (capcol != NULL)
1047 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001048
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001049 /* We always use the characters up to the next non-word character,
1050 * also for bad words. */
1051 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001052
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001053 /* Check caps type later. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001054 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001055
Bram Moolenaar5195e452005-08-19 20:32:47 +00001056 /* case-fold the word with one non-word character, so that we can check
1057 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001058 if (*mi.mi_fend != NUL)
1059 mb_ptr_adv(mi.mi_fend);
1060
1061 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
1062 MAXWLEN + 1);
1063 mi.mi_fwordlen = STRLEN(mi.mi_fword);
1064
1065 /* The word is bad unless we recognize it. */
1066 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +00001067 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001068
1069 /*
1070 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001071 * We check them all, because a word may be matched longer in another
1072 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001073 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001074 for (lpi = 0; lpi < wp->w_buffer->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001075 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001076 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, lpi);
1077
1078 /* If reloading fails the language is still in the list but everything
1079 * has been cleared. */
1080 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
1081 continue;
1082
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001083 /* Check for a matching word in case-folded words. */
1084 find_word(&mi, FIND_FOLDWORD);
1085
1086 /* Check for a matching word in keep-case words. */
1087 find_word(&mi, FIND_KEEPWORD);
1088
1089 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001090 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +00001091
1092 /* For a NOBREAK language, may want to use a word without a following
1093 * word as a backup. */
1094 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
1095 && mi.mi_result2 != SP_BAD)
1096 {
1097 mi.mi_result = mi.mi_result2;
1098 mi.mi_end = mi.mi_end2;
1099 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001100
1101 /* Count the word in the first language where it's found to be OK. */
1102 if (count_word && mi.mi_result == SP_OK)
1103 {
1104 count_common_word(mi.mi_lp->lp_slang, ptr,
1105 (int)(mi.mi_end - ptr), 1);
1106 count_word = FALSE;
1107 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001108 }
1109
1110 if (mi.mi_result != SP_OK)
1111 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001112 /* If we found a number skip over it. Allows for "42nd". Do flag
1113 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001114 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001115 {
1116 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
1117 return nrlen;
1118 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001119
1120 /* When we are at a non-word character there is no error, just
1121 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001122 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001123 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001124 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
1125 {
1126 regmatch_T regmatch;
1127
1128 /* Check for end of sentence. */
1129 regmatch.regprog = wp->w_buffer->b_cap_prog;
1130 regmatch.rm_ic = FALSE;
1131 if (vim_regexec(&regmatch, ptr, 0))
1132 *capcol = (int)(regmatch.endp[0] - ptr);
1133 }
1134
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001135#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001136 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001137 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001138#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001139 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001140 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001141 else if (mi.mi_end == ptr)
1142 /* Always include at least one character. Required for when there
1143 * is a mixup in "midword". */
1144 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +00001145 else if (mi.mi_result == SP_BAD
1146 && LANGP_ENTRY(wp->w_buffer->b_langp, 0)->lp_slang->sl_nobreak)
1147 {
1148 char_u *p, *fp;
1149 int save_result = mi.mi_result;
1150
1151 /* First language in 'spelllang' is NOBREAK. Find first position
1152 * at which any word would be valid. */
1153 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001154 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +00001155 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001156 p = mi.mi_word;
1157 fp = mi.mi_fword;
1158 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +00001159 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001160 mb_ptr_adv(p);
1161 mb_ptr_adv(fp);
1162 if (p >= mi.mi_end)
1163 break;
1164 mi.mi_compoff = fp - mi.mi_fword;
1165 find_word(&mi, FIND_COMPOUND);
1166 if (mi.mi_result != SP_BAD)
1167 {
1168 mi.mi_end = p;
1169 break;
1170 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001171 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001172 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +00001173 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001174 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001175
1176 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001177 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001178 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001179 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001180 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001181 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001182 }
1183
Bram Moolenaar5195e452005-08-19 20:32:47 +00001184 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1185 {
1186 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001187 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001188 return wrongcaplen;
1189 }
1190
Bram Moolenaar51485f02005-06-04 21:55:20 +00001191 return (int)(mi.mi_end - ptr);
1192}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001193
Bram Moolenaar51485f02005-06-04 21:55:20 +00001194/*
1195 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001196 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1197 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1198 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1199 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001200 *
1201 * For a match mip->mi_result is updated.
1202 */
1203 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001204find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001205 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001206 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001207{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001208 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001209 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001210 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001211 int endidxcnt = 0;
1212 int len;
1213 int wlen = 0;
1214 int flen;
1215 int c;
1216 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001217 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001218#ifdef FEAT_MBYTE
1219 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001220#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001221 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001222 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001223 slang_T *slang = mip->mi_lp->lp_slang;
1224 unsigned flags;
1225 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001226 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001227 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001228 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001229 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001230
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001231 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001232 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001233 /* Check for word with matching case in keep-case tree. */
1234 ptr = mip->mi_word;
1235 flen = 9999; /* no case folding, always enough bytes */
1236 byts = slang->sl_kbyts;
1237 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001238
1239 if (mode == FIND_KEEPCOMPOUND)
1240 /* Skip over the previously found word(s). */
1241 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001242 }
1243 else
1244 {
1245 /* Check for case-folded in case-folded tree. */
1246 ptr = mip->mi_fword;
1247 flen = mip->mi_fwordlen; /* available case-folded bytes */
1248 byts = slang->sl_fbyts;
1249 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001250
1251 if (mode == FIND_PREFIX)
1252 {
1253 /* Skip over the prefix. */
1254 wlen = mip->mi_prefixlen;
1255 flen -= mip->mi_prefixlen;
1256 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001257 else if (mode == FIND_COMPOUND)
1258 {
1259 /* Skip over the previously found word(s). */
1260 wlen = mip->mi_compoff;
1261 flen -= mip->mi_compoff;
1262 }
1263
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001264 }
1265
Bram Moolenaar51485f02005-06-04 21:55:20 +00001266 if (byts == NULL)
1267 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001268
Bram Moolenaar51485f02005-06-04 21:55:20 +00001269 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001270 * Repeat advancing in the tree until:
1271 * - there is a byte that doesn't match,
1272 * - we reach the end of the tree,
1273 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001274 */
1275 for (;;)
1276 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001277 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001278 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001279
1280 len = byts[arridx++];
1281
1282 /* If the first possible byte is a zero the word could end here.
1283 * Remember this index, we first check for the longest word. */
1284 if (byts[arridx] == 0)
1285 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001286 if (endidxcnt == MAXWLEN)
1287 {
1288 /* Must be a corrupted spell file. */
1289 EMSG(_(e_format));
1290 return;
1291 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001292 endlen[endidxcnt] = wlen;
1293 endidx[endidxcnt++] = arridx++;
1294 --len;
1295
1296 /* Skip over the zeros, there can be several flag/region
1297 * combinations. */
1298 while (len > 0 && byts[arridx] == 0)
1299 {
1300 ++arridx;
1301 --len;
1302 }
1303 if (len == 0)
1304 break; /* no children, word must end here */
1305 }
1306
1307 /* Stop looking at end of the line. */
1308 if (ptr[wlen] == NUL)
1309 break;
1310
1311 /* Perform a binary search in the list of accepted bytes. */
1312 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001313 if (c == TAB) /* <Tab> is handled like <Space> */
1314 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001315 lo = arridx;
1316 hi = arridx + len - 1;
1317 while (lo < hi)
1318 {
1319 m = (lo + hi) / 2;
1320 if (byts[m] > c)
1321 hi = m - 1;
1322 else if (byts[m] < c)
1323 lo = m + 1;
1324 else
1325 {
1326 lo = hi = m;
1327 break;
1328 }
1329 }
1330
1331 /* Stop if there is no matching byte. */
1332 if (hi < lo || byts[lo] != c)
1333 break;
1334
1335 /* Continue at the child (if there is one). */
1336 arridx = idxs[lo];
1337 ++wlen;
1338 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001339
1340 /* One space in the good word may stand for several spaces in the
1341 * checked word. */
1342 if (c == ' ')
1343 {
1344 for (;;)
1345 {
1346 if (flen <= 0 && *mip->mi_fend != NUL)
1347 flen = fold_more(mip);
1348 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1349 break;
1350 ++wlen;
1351 --flen;
1352 }
1353 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001354 }
1355
1356 /*
1357 * Verify that one of the possible endings is valid. Try the longest
1358 * first.
1359 */
1360 while (endidxcnt > 0)
1361 {
1362 --endidxcnt;
1363 arridx = endidx[endidxcnt];
1364 wlen = endlen[endidxcnt];
1365
1366#ifdef FEAT_MBYTE
1367 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1368 continue; /* not at first byte of character */
1369#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001370 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001371 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001372 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001373 continue; /* next char is a word character */
1374 word_ends = FALSE;
1375 }
1376 else
1377 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001378 /* The prefix flag is before compound flags. Once a valid prefix flag
1379 * has been found we try compound flags. */
1380 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001381
1382#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001383 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001384 {
1385 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001386 * when folding case. This can be slow, take a shortcut when the
1387 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001388 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001389 if (STRNCMP(ptr, p, wlen) != 0)
1390 {
1391 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1392 mb_ptr_adv(p);
1393 wlen = p - mip->mi_word;
1394 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001395 }
1396#endif
1397
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001398 /* Check flags and region. For FIND_PREFIX check the condition and
1399 * prefix ID.
1400 * Repeat this if there are more flags/region alternatives until there
1401 * is a match. */
1402 res = SP_BAD;
1403 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1404 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001405 {
1406 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001407
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001408 /* For the fold-case tree check that the case of the checked word
1409 * matches with what the word in the tree requires.
1410 * For keep-case tree the case is always right. For prefixes we
1411 * don't bother to check. */
1412 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001413 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001414 if (mip->mi_cend != mip->mi_word + wlen)
1415 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001416 /* mi_capflags was set for a different word length, need
1417 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001418 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001419 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001420 }
1421
Bram Moolenaar0c405862005-06-22 22:26:26 +00001422 if (mip->mi_capflags == WF_KEEPCAP
1423 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001424 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001425 }
1426
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001427 /* When mode is FIND_PREFIX the word must support the prefix:
1428 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001429 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001430 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001431 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001432 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001433 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001434 mip->mi_word + mip->mi_cprefixlen, slang,
1435 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001436 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001437 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001438
1439 /* Use the WF_RARE flag for a rare prefix. */
1440 if (c & WF_RAREPFX)
1441 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001442 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001443 }
1444
Bram Moolenaar78622822005-08-23 21:00:13 +00001445 if (slang->sl_nobreak)
1446 {
1447 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1448 && (flags & WF_BANNED) == 0)
1449 {
1450 /* NOBREAK: found a valid following word. That's all we
1451 * need to know, so return. */
1452 mip->mi_result = SP_OK;
1453 break;
1454 }
1455 }
1456
1457 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1458 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001459 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00001460 /* If there is no flag or the word is shorter than
1461 * COMPOUNDMIN reject it quickly.
1462 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001463 * that's too short... Myspell compatibility requires this
1464 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001465 if (((unsigned)flags >> 24) == 0
1466 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001467 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001468#ifdef FEAT_MBYTE
1469 /* For multi-byte chars check character length against
1470 * COMPOUNDMIN. */
1471 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001472 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001473 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
1474 wlen - mip->mi_compoff) < slang->sl_compminlen)
1475 continue;
1476#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001477
Bram Moolenaare52325c2005-08-22 22:54:29 +00001478 /* Limit the number of compound words to COMPOUNDMAX if no
1479 * maximum for syllables is specified. */
1480 if (!word_ends && mip->mi_complen + 2 > slang->sl_compmax
1481 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001482 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001483
Bram Moolenaard12a1322005-08-21 22:08:24 +00001484 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001485 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001486 ? slang->sl_compstartflags
1487 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001488 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001489 continue;
1490
Bram Moolenaare52325c2005-08-22 22:54:29 +00001491 if (mode == FIND_COMPOUND)
1492 {
1493 int capflags;
1494
1495 /* Need to check the caps type of the appended compound
1496 * word. */
1497#ifdef FEAT_MBYTE
1498 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1499 mip->mi_compoff) != 0)
1500 {
1501 /* case folding may have changed the length */
1502 p = mip->mi_word;
1503 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1504 mb_ptr_adv(p);
1505 }
1506 else
1507#endif
1508 p = mip->mi_word + mip->mi_compoff;
1509 capflags = captype(p, mip->mi_word + wlen);
1510 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1511 && (flags & WF_FIXCAP) != 0))
1512 continue;
1513
1514 if (capflags != WF_ALLCAP)
1515 {
1516 /* When the character before the word is a word
1517 * character we do not accept a Onecap word. We do
1518 * accept a no-caps word, even when the dictionary
1519 * word specifies ONECAP. */
1520 mb_ptr_back(mip->mi_word, p);
1521 if (spell_iswordp_nmw(p)
1522 ? capflags == WF_ONECAP
1523 : (flags & WF_ONECAP) != 0
1524 && capflags != WF_ONECAP)
1525 continue;
1526 }
1527 }
1528
Bram Moolenaar5195e452005-08-19 20:32:47 +00001529 /* If the word ends the sequence of compound flags of the
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001530 * words must match with one of the COMPOUNDRULE items and
Bram Moolenaar5195e452005-08-19 20:32:47 +00001531 * the number of syllables must not be too large. */
1532 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1533 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1534 if (word_ends)
1535 {
1536 char_u fword[MAXWLEN];
1537
1538 if (slang->sl_compsylmax < MAXWLEN)
1539 {
1540 /* "fword" is only needed for checking syllables. */
1541 if (ptr == mip->mi_word)
1542 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1543 else
1544 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1545 }
1546 if (!can_compound(slang, fword, mip->mi_compflags))
1547 continue;
1548 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001549 }
1550
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001551 /* Check NEEDCOMPOUND: can't use word without compounding. */
1552 else if (flags & WF_NEEDCOMP)
1553 continue;
1554
Bram Moolenaar78622822005-08-23 21:00:13 +00001555 nobreak_result = SP_OK;
1556
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001557 if (!word_ends)
1558 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001559 int save_result = mip->mi_result;
1560 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001561 langp_T *save_lp = mip->mi_lp;
1562 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +00001563
1564 /* Check that a valid word follows. If there is one and we
1565 * are compounding, it will set "mi_result", thus we are
1566 * always finished here. For NOBREAK we only check that a
1567 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001568 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001569 if (slang->sl_nobreak)
1570 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001571
1572 /* Find following word in case-folded tree. */
1573 mip->mi_compoff = endlen[endidxcnt];
1574#ifdef FEAT_MBYTE
1575 if (has_mbyte && mode == FIND_KEEPWORD)
1576 {
1577 /* Compute byte length in case-folded word from "wlen":
1578 * byte length in keep-case word. Length may change when
1579 * folding case. This can be slow, take a shortcut when
1580 * the case-folded word is equal to the keep-case word. */
1581 p = mip->mi_fword;
1582 if (STRNCMP(ptr, p, wlen) != 0)
1583 {
1584 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1585 mb_ptr_adv(p);
1586 mip->mi_compoff = p - mip->mi_fword;
1587 }
1588 }
1589#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001590 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001591 ++mip->mi_complen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001592
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001593 /* For NOBREAK we need to try all NOBREAK languages, at least
1594 * to find the ".add" file(s). */
1595 for (lpi = 0; lpi < mip->mi_buf->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001596 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001597 if (slang->sl_nobreak)
1598 {
1599 mip->mi_lp = LANGP_ENTRY(mip->mi_buf->b_langp, lpi);
1600 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1601 || !mip->mi_lp->lp_slang->sl_nobreak)
1602 continue;
1603 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001604
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001605 find_word(mip, FIND_COMPOUND);
1606
1607 /* When NOBREAK any word that matches is OK. Otherwise we
1608 * need to find the longest match, thus try with keep-case
1609 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001610 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1611 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001612 /* Find following word in keep-case tree. */
1613 mip->mi_compoff = wlen;
1614 find_word(mip, FIND_KEEPCOMPOUND);
1615
1616 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1617 {
1618 /* Check for following word with prefix. */
1619 mip->mi_compoff = c;
1620 find_prefix(mip, FIND_COMPOUND);
1621 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001622 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001623
1624 if (!slang->sl_nobreak)
1625 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001626 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001627 --mip->mi_complen;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001628 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001629
Bram Moolenaar78622822005-08-23 21:00:13 +00001630 if (slang->sl_nobreak)
1631 {
1632 nobreak_result = mip->mi_result;
1633 mip->mi_result = save_result;
1634 mip->mi_end = save_end;
1635 }
1636 else
1637 {
1638 if (mip->mi_result == SP_OK)
1639 break;
1640 continue;
1641 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001642 }
1643
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001644 if (flags & WF_BANNED)
1645 res = SP_BANNED;
1646 else if (flags & WF_REGION)
1647 {
1648 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001649 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001650 res = SP_OK;
1651 else
1652 res = SP_LOCAL;
1653 }
1654 else if (flags & WF_RARE)
1655 res = SP_RARE;
1656 else
1657 res = SP_OK;
1658
Bram Moolenaar78622822005-08-23 21:00:13 +00001659 /* Always use the longest match and the best result. For NOBREAK
1660 * we separately keep the longest match without a following good
1661 * word as a fall-back. */
1662 if (nobreak_result == SP_BAD)
1663 {
1664 if (mip->mi_result2 > res)
1665 {
1666 mip->mi_result2 = res;
1667 mip->mi_end2 = mip->mi_word + wlen;
1668 }
1669 else if (mip->mi_result2 == res
1670 && mip->mi_end2 < mip->mi_word + wlen)
1671 mip->mi_end2 = mip->mi_word + wlen;
1672 }
1673 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001674 {
1675 mip->mi_result = res;
1676 mip->mi_end = mip->mi_word + wlen;
1677 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001678 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001679 mip->mi_end = mip->mi_word + wlen;
1680
Bram Moolenaar78622822005-08-23 21:00:13 +00001681 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001682 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001683 }
1684
Bram Moolenaar78622822005-08-23 21:00:13 +00001685 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001686 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001687 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001688}
1689
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001690/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001691 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1692 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001693 */
1694 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001695can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001696 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001697 char_u *word;
1698 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001699{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001700 regmatch_T regmatch;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001701#ifdef FEAT_MBYTE
1702 char_u uflags[MAXWLEN * 2];
1703 int i;
1704#endif
1705 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001706
1707 if (slang->sl_compprog == NULL)
1708 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001709#ifdef FEAT_MBYTE
1710 if (enc_utf8)
1711 {
1712 /* Need to convert the single byte flags to utf8 characters. */
1713 p = uflags;
1714 for (i = 0; flags[i] != NUL; ++i)
1715 p += mb_char2bytes(flags[i], p);
1716 *p = NUL;
1717 p = uflags;
1718 }
1719 else
1720#endif
1721 p = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001722 regmatch.regprog = slang->sl_compprog;
1723 regmatch.rm_ic = FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001724 if (!vim_regexec(&regmatch, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001725 return FALSE;
1726
Bram Moolenaare52325c2005-08-22 22:54:29 +00001727 /* Count the number of syllables. This may be slow, do it last. If there
1728 * are too many syllables AND the number of compound words is above
1729 * COMPOUNDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001730 if (slang->sl_compsylmax < MAXWLEN
1731 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001732 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001733 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001734}
1735
1736/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001737 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1738 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001739 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001740 */
1741 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001742valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001743 int totprefcnt; /* nr of prefix IDs */
1744 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001745 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001746 char_u *word;
1747 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001748 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001749{
1750 int prefcnt;
1751 int pidx;
1752 regprog_T *rp;
1753 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001754 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001755
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001756 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001757 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1758 {
1759 pidx = slang->sl_pidxs[arridx + prefcnt];
1760
1761 /* Check the prefix ID. */
1762 if (prefid != (pidx & 0xff))
1763 continue;
1764
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001765 /* Check if the prefix doesn't combine and the word already has a
1766 * suffix. */
1767 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1768 continue;
1769
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001770 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001771 * stored in the two bytes above the prefix ID byte. */
1772 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001773 if (rp != NULL)
1774 {
1775 regmatch.regprog = rp;
1776 regmatch.rm_ic = FALSE;
1777 if (!vim_regexec(&regmatch, word, 0))
1778 continue;
1779 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001780 else if (cond_req)
1781 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001782
Bram Moolenaar53805d12005-08-01 07:08:33 +00001783 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001784 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001785 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001786 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001787}
1788
1789/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001790 * Check if the word at "mip->mi_word" has a matching prefix.
1791 * If it does, then check the following word.
1792 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001793 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1794 * prefix in a compound word.
1795 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001796 * For a match mip->mi_result is updated.
1797 */
1798 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001799find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001800 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001801 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001802{
1803 idx_T arridx = 0;
1804 int len;
1805 int wlen = 0;
1806 int flen;
1807 int c;
1808 char_u *ptr;
1809 idx_T lo, hi, m;
1810 slang_T *slang = mip->mi_lp->lp_slang;
1811 char_u *byts;
1812 idx_T *idxs;
1813
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001814 byts = slang->sl_pbyts;
1815 if (byts == NULL)
1816 return; /* array is empty */
1817
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001818 /* We use the case-folded word here, since prefixes are always
1819 * case-folded. */
1820 ptr = mip->mi_fword;
1821 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001822 if (mode == FIND_COMPOUND)
1823 {
1824 /* Skip over the previously found word(s). */
1825 ptr += mip->mi_compoff;
1826 flen -= mip->mi_compoff;
1827 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001828 idxs = slang->sl_pidxs;
1829
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001830 /*
1831 * Repeat advancing in the tree until:
1832 * - there is a byte that doesn't match,
1833 * - we reach the end of the tree,
1834 * - or we reach the end of the line.
1835 */
1836 for (;;)
1837 {
1838 if (flen == 0 && *mip->mi_fend != NUL)
1839 flen = fold_more(mip);
1840
1841 len = byts[arridx++];
1842
1843 /* If the first possible byte is a zero the prefix could end here.
1844 * Check if the following word matches and supports the prefix. */
1845 if (byts[arridx] == 0)
1846 {
1847 /* There can be several prefixes with different conditions. We
1848 * try them all, since we don't know which one will give the
1849 * longest match. The word is the same each time, pass the list
1850 * of possible prefixes to find_word(). */
1851 mip->mi_prefarridx = arridx;
1852 mip->mi_prefcnt = len;
1853 while (len > 0 && byts[arridx] == 0)
1854 {
1855 ++arridx;
1856 --len;
1857 }
1858 mip->mi_prefcnt -= len;
1859
1860 /* Find the word that comes after the prefix. */
1861 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001862 if (mode == FIND_COMPOUND)
1863 /* Skip over the previously found word(s). */
1864 mip->mi_prefixlen += mip->mi_compoff;
1865
Bram Moolenaar53805d12005-08-01 07:08:33 +00001866#ifdef FEAT_MBYTE
1867 if (has_mbyte)
1868 {
1869 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001870 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1871 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001872 }
1873 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001874 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001875#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001876 find_word(mip, FIND_PREFIX);
1877
1878
1879 if (len == 0)
1880 break; /* no children, word must end here */
1881 }
1882
1883 /* Stop looking at end of the line. */
1884 if (ptr[wlen] == NUL)
1885 break;
1886
1887 /* Perform a binary search in the list of accepted bytes. */
1888 c = ptr[wlen];
1889 lo = arridx;
1890 hi = arridx + len - 1;
1891 while (lo < hi)
1892 {
1893 m = (lo + hi) / 2;
1894 if (byts[m] > c)
1895 hi = m - 1;
1896 else if (byts[m] < c)
1897 lo = m + 1;
1898 else
1899 {
1900 lo = hi = m;
1901 break;
1902 }
1903 }
1904
1905 /* Stop if there is no matching byte. */
1906 if (hi < lo || byts[lo] != c)
1907 break;
1908
1909 /* Continue at the child (if there is one). */
1910 arridx = idxs[lo];
1911 ++wlen;
1912 --flen;
1913 }
1914}
1915
1916/*
1917 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001918 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001919 * Return the length of the folded chars in bytes.
1920 */
1921 static int
1922fold_more(mip)
1923 matchinf_T *mip;
1924{
1925 int flen;
1926 char_u *p;
1927
1928 p = mip->mi_fend;
1929 do
1930 {
1931 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001932 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001933
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001934 /* Include the non-word character so that we can check for the word end. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001935 if (*mip->mi_fend != NUL)
1936 mb_ptr_adv(mip->mi_fend);
1937
1938 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1939 mip->mi_fword + mip->mi_fwordlen,
1940 MAXWLEN - mip->mi_fwordlen);
1941 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1942 mip->mi_fwordlen += flen;
1943 return flen;
1944}
1945
1946/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001947 * Check case flags for a word. Return TRUE if the word has the requested
1948 * case.
1949 */
1950 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001951spell_valid_case(wordflags, treeflags)
1952 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001953 int treeflags; /* flags for the word in the spell tree */
1954{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001955 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001956 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001957 && ((treeflags & WF_ONECAP) == 0
1958 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001959}
1960
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001961/*
1962 * Return TRUE if spell checking is not enabled.
1963 */
1964 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00001965no_spell_checking(wp)
1966 win_T *wp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001967{
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001968 if (!wp->w_p_spell || *wp->w_buffer->b_p_spl == NUL
1969 || wp->w_buffer->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001970 {
1971 EMSG(_("E756: Spell checking is not enabled"));
1972 return TRUE;
1973 }
1974 return FALSE;
1975}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001976
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001977/*
1978 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001979 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
1980 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001981 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1982 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00001983 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001984 */
1985 int
Bram Moolenaar95529562005-08-25 21:21:38 +00001986spell_move_to(wp, dir, allwords, curline, attrp)
1987 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001988 int dir; /* FORWARD or BACKWARD */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001989 int allwords; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001990 int curline;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001991 hlf_T *attrp; /* return: attributes of bad word or NULL
1992 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001993{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001994 linenr_T lnum;
1995 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001996 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001997 char_u *line;
1998 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001999 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002000 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002001 int len;
Bram Moolenaar95529562005-08-25 21:21:38 +00002002 int has_syntax = syntax_present(wp->w_buffer);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002003 int col;
2004 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002005 char_u *buf = NULL;
2006 int buflen = 0;
2007 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002008 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002009 int found_one = FALSE;
2010 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002011
Bram Moolenaar95529562005-08-25 21:21:38 +00002012 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00002013 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002014
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002015 /*
2016 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00002017 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002018 *
2019 * When searching backwards, we continue in the line to find the last
2020 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00002021 *
2022 * We concatenate the start of the next line, so that wrapped words work
2023 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
2024 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002025 */
Bram Moolenaar95529562005-08-25 21:21:38 +00002026 lnum = wp->w_cursor.lnum;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002027 clearpos(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002028
2029 while (!got_int)
2030 {
Bram Moolenaar95529562005-08-25 21:21:38 +00002031 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002032
Bram Moolenaar0c405862005-06-22 22:26:26 +00002033 len = STRLEN(line);
2034 if (buflen < len + MAXWLEN + 2)
2035 {
2036 vim_free(buf);
2037 buflen = len + MAXWLEN + 2;
2038 buf = alloc(buflen);
2039 if (buf == NULL)
2040 break;
2041 }
2042
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002043 /* In first line check first word for Capital. */
2044 if (lnum == 1)
2045 capcol = 0;
2046
2047 /* For checking first word with a capital skip white space. */
2048 if (capcol == 0)
2049 capcol = skipwhite(line) - line;
2050
Bram Moolenaar0c405862005-06-22 22:26:26 +00002051 /* Copy the line into "buf" and append the start of the next line if
2052 * possible. */
2053 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00002054 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002055 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
2056
2057 p = buf + skip;
2058 endp = buf + len;
2059 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002060 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002061 /* When searching backward don't search after the cursor. Unless
2062 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002063 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00002064 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002065 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00002066 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002067 break;
2068
2069 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002070 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002071 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002072
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002073 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002074 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002075 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002076 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002077 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002078 found_one = TRUE;
2079
Bram Moolenaar51485f02005-06-04 21:55:20 +00002080 /* When searching forward only accept a bad word after
2081 * the cursor. */
2082 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002083 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00002084 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002085 && (wrapped
2086 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00002087 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002088 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002089 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002090 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002091 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00002092 col = p - buf;
Bram Moolenaar95529562005-08-25 21:21:38 +00002093 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar51485f02005-06-04 21:55:20 +00002094 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002095 }
2096 else
2097 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002098
Bram Moolenaar51485f02005-06-04 21:55:20 +00002099 if (can_spell)
2100 {
2101 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002102 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002103#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00002104 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002105#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002106 if (dir == FORWARD)
2107 {
2108 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00002109 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002110 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002111 if (attrp != NULL)
2112 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002113 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002114 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002115 else if (curline)
2116 /* Insert mode completion: put cursor after
2117 * the bad word. */
2118 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002119 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002120 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002121 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002122 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002123 }
2124
Bram Moolenaar51485f02005-06-04 21:55:20 +00002125 /* advance to character after the word */
2126 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002127 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002128 }
2129
Bram Moolenaar5195e452005-08-19 20:32:47 +00002130 if (dir == BACKWARD && found_pos.lnum != 0)
2131 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002132 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00002133 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002134 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002135 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002136 }
2137
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002138 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002139 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002140
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002141 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002142 if (dir == BACKWARD)
2143 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002144 /* If we are back at the starting line and searched it again there
2145 * is no match, give up. */
2146 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002147 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002148
2149 if (lnum > 1)
2150 --lnum;
2151 else if (!p_ws)
2152 break; /* at first line and 'nowrapscan' */
2153 else
2154 {
2155 /* Wrap around to the end of the buffer. May search the
2156 * starting line again and accept the last match. */
2157 lnum = wp->w_buffer->b_ml.ml_line_count;
2158 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002159 if (!shortmess(SHM_SEARCH))
2160 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002161 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002162 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002163 }
2164 else
2165 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002166 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2167 ++lnum;
2168 else if (!p_ws)
2169 break; /* at first line and 'nowrapscan' */
2170 else
2171 {
2172 /* Wrap around to the start of the buffer. May search the
2173 * starting line again and accept the first match. */
2174 lnum = 1;
2175 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002176 if (!shortmess(SHM_SEARCH))
2177 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002178 }
2179
2180 /* If we are back at the starting line and there is no match then
2181 * give up. */
2182 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002183 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002184
2185 /* Skip the characters at the start of the next line that were
2186 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002187 if (attr == HLF_COUNT)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002188 skip = p - endp;
2189 else
2190 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002191
2192 /* Capscol skips over the inserted space. */
2193 --capcol;
2194
2195 /* But after empty line check first word in next line */
2196 if (*skipwhite(line) == NUL)
2197 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002198 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002199
2200 line_breakcheck();
2201 }
2202
Bram Moolenaar0c405862005-06-22 22:26:26 +00002203 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002204 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002205}
2206
2207/*
2208 * For spell checking: concatenate the start of the following line "line" into
2209 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
2210 */
2211 void
2212spell_cat_line(buf, line, maxlen)
2213 char_u *buf;
2214 char_u *line;
2215 int maxlen;
2216{
2217 char_u *p;
2218 int n;
2219
2220 p = skipwhite(line);
2221 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2222 p = skipwhite(p + 1);
2223
2224 if (*p != NUL)
2225 {
2226 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002227 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002228 n = p - line;
2229 if (n >= maxlen)
2230 n = maxlen - 1;
2231 vim_memset(buf + 1, ' ', n);
2232 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002233}
2234
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002235/*
2236 * Structure used for the cookie argument of do_in_runtimepath().
2237 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002238typedef struct spelload_S
2239{
2240 char_u sl_lang[MAXWLEN + 1]; /* language name */
2241 slang_T *sl_slang; /* resulting slang_T struct */
2242 int sl_nobreak; /* NOBREAK language found */
2243} spelload_T;
2244
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002245/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002246 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002247 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002248 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002249 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002250spell_load_lang(lang)
2251 char_u *lang;
2252{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002253 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002254 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002255 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002256#ifdef FEAT_AUTOCMD
2257 int round;
2258#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002259
Bram Moolenaarb765d632005-06-07 21:00:02 +00002260 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002261 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002262 STRCPY(sl.sl_lang, lang);
2263 sl.sl_slang = NULL;
2264 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002265
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002266#ifdef FEAT_AUTOCMD
2267 /* We may retry when no spell file is found for the language, an
2268 * autocommand may load it then. */
2269 for (round = 1; round <= 2; ++round)
2270#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002271 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002272 /*
2273 * Find the first spell file for "lang" in 'runtimepath' and load it.
2274 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002275 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002276 "spell/%s.%s.spl", lang, spell_enc());
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002277 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002278
2279 if (r == FAIL && *sl.sl_lang != NUL)
2280 {
2281 /* Try loading the ASCII version. */
2282 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
2283 "spell/%s.ascii.spl", lang);
2284 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
2285
2286#ifdef FEAT_AUTOCMD
2287 if (r == FAIL && *sl.sl_lang != NUL && round == 1
2288 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
2289 curbuf->b_fname, FALSE, curbuf))
2290 continue;
2291 break;
2292#endif
2293 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002294#ifdef FEAT_AUTOCMD
2295 break;
2296#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002297 }
2298
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002299 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002300 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002301 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2302 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002303 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002304 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002305 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002306 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002307 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002308 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002309 }
2310}
2311
2312/*
2313 * Return the encoding used for spell checking: Use 'encoding', except that we
2314 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2315 */
2316 static char_u *
2317spell_enc()
2318{
2319
2320#ifdef FEAT_MBYTE
2321 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2322 return p_enc;
2323#endif
2324 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002325}
2326
2327/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002328 * Get the name of the .spl file for the internal wordlist into
2329 * "fname[MAXPATHL]".
2330 */
2331 static void
2332int_wordlist_spl(fname)
2333 char_u *fname;
2334{
2335 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
2336 int_wordlist, spell_enc());
2337}
2338
2339/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002340 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002341 * Caller must fill "sl_next".
2342 */
2343 static slang_T *
2344slang_alloc(lang)
2345 char_u *lang;
2346{
2347 slang_T *lp;
2348
Bram Moolenaar51485f02005-06-04 21:55:20 +00002349 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002350 if (lp != NULL)
2351 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002352 if (lang != NULL)
2353 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002354 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002355 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002356 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002357 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002358 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002359 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002360
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002361 return lp;
2362}
2363
2364/*
2365 * Free the contents of an slang_T and the structure itself.
2366 */
2367 static void
2368slang_free(lp)
2369 slang_T *lp;
2370{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002371 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002372 vim_free(lp->sl_fname);
2373 slang_clear(lp);
2374 vim_free(lp);
2375}
2376
2377/*
2378 * Clear an slang_T so that the file can be reloaded.
2379 */
2380 static void
2381slang_clear(lp)
2382 slang_T *lp;
2383{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002384 garray_T *gap;
2385 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002386 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002387 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002388 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002389
Bram Moolenaar51485f02005-06-04 21:55:20 +00002390 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002391 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002392 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002393 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002394 vim_free(lp->sl_pbyts);
2395 lp->sl_pbyts = NULL;
2396
Bram Moolenaar51485f02005-06-04 21:55:20 +00002397 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002398 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002399 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002400 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002401 vim_free(lp->sl_pidxs);
2402 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002403
Bram Moolenaar4770d092006-01-12 23:22:24 +00002404 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002405 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002406 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
2407 while (gap->ga_len > 0)
2408 {
2409 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2410 vim_free(ftp->ft_from);
2411 vim_free(ftp->ft_to);
2412 }
2413 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002414 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002415
2416 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002417 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002418 {
2419 /* "ga_len" is set to 1 without adding an item for latin1 */
2420 if (gap->ga_data != NULL)
2421 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2422 for (i = 0; i < gap->ga_len; ++i)
2423 vim_free(((int **)gap->ga_data)[i]);
2424 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002425 else
2426 /* SAL items: free salitem_T items */
2427 while (gap->ga_len > 0)
2428 {
2429 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2430 vim_free(smp->sm_lead);
2431 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2432 vim_free(smp->sm_to);
2433#ifdef FEAT_MBYTE
2434 vim_free(smp->sm_lead_w);
2435 vim_free(smp->sm_oneof_w);
2436 vim_free(smp->sm_to_w);
2437#endif
2438 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002439 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002440
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002441 for (i = 0; i < lp->sl_prefixcnt; ++i)
2442 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002443 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002444 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002445 lp->sl_prefprog = NULL;
2446
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002447 vim_free(lp->sl_info);
2448 lp->sl_info = NULL;
2449
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002450 vim_free(lp->sl_midword);
2451 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002452
Bram Moolenaar5195e452005-08-19 20:32:47 +00002453 vim_free(lp->sl_compprog);
2454 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002455 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002456 lp->sl_compprog = NULL;
2457 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002458 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002459
2460 vim_free(lp->sl_syllable);
2461 lp->sl_syllable = NULL;
2462 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002463
Bram Moolenaar4770d092006-01-12 23:22:24 +00002464 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2465 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002466
Bram Moolenaar4770d092006-01-12 23:22:24 +00002467#ifdef FEAT_MBYTE
2468 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaarea424162005-06-16 21:51:00 +00002469#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002470
Bram Moolenaar4770d092006-01-12 23:22:24 +00002471 /* Clear info from .sug file. */
2472 slang_clear_sug(lp);
2473
Bram Moolenaar5195e452005-08-19 20:32:47 +00002474 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002475 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002476 lp->sl_compsylmax = MAXWLEN;
2477 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002478}
2479
2480/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002481 * Clear the info from the .sug file in "lp".
2482 */
2483 static void
2484slang_clear_sug(lp)
2485 slang_T *lp;
2486{
2487 vim_free(lp->sl_sbyts);
2488 lp->sl_sbyts = NULL;
2489 vim_free(lp->sl_sidxs);
2490 lp->sl_sidxs = NULL;
2491 close_spellbuf(lp->sl_sugbuf);
2492 lp->sl_sugbuf = NULL;
2493 lp->sl_sugloaded = FALSE;
2494 lp->sl_sugtime = 0;
2495}
2496
2497/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002498 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002499 * Invoked through do_in_runtimepath().
2500 */
2501 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002502spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002503 char_u *fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002504 void *cookie;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002505{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002506 spelload_T *slp = (spelload_T *)cookie;
2507 slang_T *slang;
2508
2509 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2510 if (slang != NULL)
2511 {
2512 /* When a previously loaded file has NOBREAK also use it for the
2513 * ".add" files. */
2514 if (slp->sl_nobreak && slang->sl_add)
2515 slang->sl_nobreak = TRUE;
2516 else if (slang->sl_nobreak)
2517 slp->sl_nobreak = TRUE;
2518
2519 slp->sl_slang = slang;
2520 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002521}
2522
2523/*
2524 * Load one spell file and store the info into a slang_T.
2525 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00002526 * This is invoked in three ways:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002527 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2528 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2529 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2530 * points to the existing slang_T.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002531 * - Just after writing a .spl file; it's read back to produce the .sug file.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002532 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
2533 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002534 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002535 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002536 static slang_T *
2537spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002538 char_u *fname;
2539 char_u *lang;
2540 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002541 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002542{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002543 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002544 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002545 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002546 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002547 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002548 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002549 char_u *save_sourcing_name = sourcing_name;
2550 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002551 slang_T *lp = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002552 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002553 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002554
Bram Moolenaarb765d632005-06-07 21:00:02 +00002555 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002556 if (fd == NULL)
2557 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002558 if (!silent)
2559 EMSG2(_(e_notopen), fname);
2560 else if (p_verbose > 2)
2561 {
2562 verbose_enter();
2563 smsg((char_u *)e_notopen, fname);
2564 verbose_leave();
2565 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002566 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002567 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002568 if (p_verbose > 2)
2569 {
2570 verbose_enter();
2571 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2572 verbose_leave();
2573 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002574
Bram Moolenaarb765d632005-06-07 21:00:02 +00002575 if (old_lp == NULL)
2576 {
2577 lp = slang_alloc(lang);
2578 if (lp == NULL)
2579 goto endFAIL;
2580
2581 /* Remember the file name, used to reload the file when it's updated. */
2582 lp->sl_fname = vim_strsave(fname);
2583 if (lp->sl_fname == NULL)
2584 goto endFAIL;
2585
2586 /* Check for .add.spl. */
2587 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2588 }
2589 else
2590 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002591
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002592 /* Set sourcing_name, so that error messages mention the file name. */
2593 sourcing_name = fname;
2594 sourcing_lnum = 0;
2595
Bram Moolenaar4770d092006-01-12 23:22:24 +00002596 /*
2597 * <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002598 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002599 for (i = 0; i < VIMSPELLMAGICL; ++i)
2600 buf[i] = getc(fd); /* <fileID> */
2601 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2602 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002603 EMSG(_("E757: This does not look like a spell file"));
2604 goto endFAIL;
2605 }
2606 c = getc(fd); /* <versionnr> */
2607 if (c < VIMSPELLVERSION)
2608 {
2609 EMSG(_("E771: Old spell file, needs to be updated"));
2610 goto endFAIL;
2611 }
2612 else if (c > VIMSPELLVERSION)
2613 {
2614 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002615 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002616 }
2617
Bram Moolenaar5195e452005-08-19 20:32:47 +00002618
2619 /*
2620 * <SECTIONS>: <section> ... <sectionend>
2621 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2622 */
2623 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002624 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002625 n = getc(fd); /* <sectionID> or <sectionend> */
2626 if (n == SN_END)
2627 break;
2628 c = getc(fd); /* <sectionflags> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002629 len = get4c(fd); /* <sectionlen> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002630 if (len < 0)
2631 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002632
Bram Moolenaar5195e452005-08-19 20:32:47 +00002633 res = 0;
2634 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002635 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002636 case SN_INFO:
2637 lp->sl_info = read_string(fd, len); /* <infotext> */
2638 if (lp->sl_info == NULL)
2639 goto endFAIL;
2640 break;
2641
Bram Moolenaar5195e452005-08-19 20:32:47 +00002642 case SN_REGION:
2643 res = read_region_section(fd, lp, len);
2644 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002645
Bram Moolenaar5195e452005-08-19 20:32:47 +00002646 case SN_CHARFLAGS:
2647 res = read_charflags_section(fd);
2648 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002649
Bram Moolenaar5195e452005-08-19 20:32:47 +00002650 case SN_MIDWORD:
2651 lp->sl_midword = read_string(fd, len); /* <midword> */
2652 if (lp->sl_midword == NULL)
2653 goto endFAIL;
2654 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002655
Bram Moolenaar5195e452005-08-19 20:32:47 +00002656 case SN_PREFCOND:
2657 res = read_prefcond_section(fd, lp);
2658 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002659
Bram Moolenaar5195e452005-08-19 20:32:47 +00002660 case SN_REP:
Bram Moolenaar4770d092006-01-12 23:22:24 +00002661 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
2662 break;
2663
2664 case SN_REPSAL:
2665 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002666 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002667
Bram Moolenaar5195e452005-08-19 20:32:47 +00002668 case SN_SAL:
2669 res = read_sal_section(fd, lp);
2670 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002671
Bram Moolenaar5195e452005-08-19 20:32:47 +00002672 case SN_SOFO:
2673 res = read_sofo_section(fd, lp);
2674 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002675
Bram Moolenaar5195e452005-08-19 20:32:47 +00002676 case SN_MAP:
2677 p = read_string(fd, len); /* <mapstr> */
2678 if (p == NULL)
2679 goto endFAIL;
2680 set_map_str(lp, p);
2681 vim_free(p);
2682 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002683
Bram Moolenaar4770d092006-01-12 23:22:24 +00002684 case SN_WORDS:
2685 res = read_words_section(fd, lp, len);
2686 break;
2687
2688 case SN_SUGFILE:
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002689 lp->sl_sugtime = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002690 break;
2691
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002692 case SN_NOSPLITSUGS:
2693 lp->sl_nosplitsugs = TRUE; /* <timestamp> */
2694 break;
2695
Bram Moolenaar5195e452005-08-19 20:32:47 +00002696 case SN_COMPOUND:
2697 res = read_compound(fd, lp, len);
2698 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002699
Bram Moolenaar78622822005-08-23 21:00:13 +00002700 case SN_NOBREAK:
2701 lp->sl_nobreak = TRUE;
2702 break;
2703
Bram Moolenaar5195e452005-08-19 20:32:47 +00002704 case SN_SYLLABLE:
2705 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2706 if (lp->sl_syllable == NULL)
2707 goto endFAIL;
2708 if (init_syl_tab(lp) == FAIL)
2709 goto endFAIL;
2710 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002711
Bram Moolenaar5195e452005-08-19 20:32:47 +00002712 default:
2713 /* Unsupported section. When it's required give an error
2714 * message. When it's not required skip the contents. */
2715 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002716 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002717 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002718 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002719 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002720 while (--len >= 0)
2721 if (getc(fd) < 0)
2722 goto truncerr;
2723 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002724 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002725someerror:
Bram Moolenaar5195e452005-08-19 20:32:47 +00002726 if (res == SP_FORMERROR)
2727 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002728 EMSG(_(e_format));
2729 goto endFAIL;
2730 }
2731 if (res == SP_TRUNCERROR)
2732 {
2733truncerr:
2734 EMSG(_(e_spell_trunc));
2735 goto endFAIL;
2736 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002737 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002738 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002739 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002740
Bram Moolenaar4770d092006-01-12 23:22:24 +00002741 /* <LWORDTREE> */
2742 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
2743 if (res != 0)
2744 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002745
Bram Moolenaar4770d092006-01-12 23:22:24 +00002746 /* <KWORDTREE> */
2747 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
2748 if (res != 0)
2749 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002750
Bram Moolenaar4770d092006-01-12 23:22:24 +00002751 /* <PREFIXTREE> */
2752 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
2753 lp->sl_prefixcnt);
2754 if (res != 0)
2755 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002756
Bram Moolenaarb765d632005-06-07 21:00:02 +00002757 /* For a new file link it in the list of spell files. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002758 if (old_lp == NULL && lang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002759 {
2760 lp->sl_next = first_lang;
2761 first_lang = lp;
2762 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002763
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002764 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002765
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002766endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002767 if (lang != NULL)
2768 /* truncating the name signals the error to spell_load_lang() */
2769 *lang = NUL;
2770 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002771 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002772 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002773
2774endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002775 if (fd != NULL)
2776 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002777 sourcing_name = save_sourcing_name;
2778 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002779
2780 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002781}
2782
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002783/*
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002784 * Read 2 bytes from "fd" and turn them into an int, MSB first.
2785 */
2786 static int
2787get2c(fd)
2788 FILE *fd;
2789{
2790 long n;
2791
2792 n = getc(fd);
2793 n = (n << 8) + getc(fd);
2794 return n;
2795}
2796
2797/*
2798 * Read 3 bytes from "fd" and turn them into an int, MSB first.
2799 */
2800 static int
2801get3c(fd)
2802 FILE *fd;
2803{
2804 long n;
2805
2806 n = getc(fd);
2807 n = (n << 8) + getc(fd);
2808 n = (n << 8) + getc(fd);
2809 return n;
2810}
2811
2812/*
2813 * Read 4 bytes from "fd" and turn them into an int, MSB first.
2814 */
2815 static int
2816get4c(fd)
2817 FILE *fd;
2818{
2819 long n;
2820
2821 n = getc(fd);
2822 n = (n << 8) + getc(fd);
2823 n = (n << 8) + getc(fd);
2824 n = (n << 8) + getc(fd);
2825 return n;
2826}
2827
2828/*
2829 * Read 8 bytes from "fd" and turn them into a time_t, MSB first.
2830 */
2831 static time_t
2832get8c(fd)
2833 FILE *fd;
2834{
2835 time_t n = 0;
2836 int i;
2837
2838 for (i = 0; i < 8; ++i)
2839 n = (n << 8) + getc(fd);
2840 return n;
2841}
2842
2843/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002844 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002845 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002846 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002847 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2848 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002849 */
2850 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002851read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002852 FILE *fd;
2853 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002854 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002855{
2856 int cnt = 0;
2857 int i;
2858 char_u *str;
2859
2860 /* read the length bytes, MSB first */
2861 for (i = 0; i < cnt_bytes; ++i)
2862 cnt = (cnt << 8) + getc(fd);
2863 if (cnt < 0)
2864 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002865 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002866 return NULL;
2867 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002868 *cntp = cnt;
2869 if (cnt == 0)
2870 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002871
Bram Moolenaar5195e452005-08-19 20:32:47 +00002872 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002873 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002874 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002875 return str;
2876}
2877
Bram Moolenaar7887d882005-07-01 22:33:52 +00002878/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00002879 * Read a string of length "cnt" from "fd" into allocated memory.
2880 * Returns NULL when out of memory.
2881 */
2882 static char_u *
2883read_string(fd, cnt)
2884 FILE *fd;
2885 int cnt;
2886{
2887 char_u *str;
2888 int i;
2889
2890 /* allocate memory */
2891 str = alloc((unsigned)cnt + 1);
2892 if (str != NULL)
2893 {
2894 /* Read the string. Doesn't check for truncated file. */
2895 for (i = 0; i < cnt; ++i)
2896 str[i] = getc(fd);
2897 str[i] = NUL;
2898 }
2899 return str;
2900}
2901
2902/*
2903 * Read SN_REGION: <regionname> ...
2904 * Return SP_*ERROR flags.
2905 */
2906 static int
2907read_region_section(fd, lp, len)
2908 FILE *fd;
2909 slang_T *lp;
2910 int len;
2911{
2912 int i;
2913
2914 if (len > 16)
2915 return SP_FORMERROR;
2916 for (i = 0; i < len; ++i)
2917 lp->sl_regions[i] = getc(fd); /* <regionname> */
2918 lp->sl_regions[len] = NUL;
2919 return 0;
2920}
2921
2922/*
2923 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
2924 * <folcharslen> <folchars>
2925 * Return SP_*ERROR flags.
2926 */
2927 static int
2928read_charflags_section(fd)
2929 FILE *fd;
2930{
2931 char_u *flags;
2932 char_u *fol;
2933 int flagslen, follen;
2934
2935 /* <charflagslen> <charflags> */
2936 flags = read_cnt_string(fd, 1, &flagslen);
2937 if (flagslen < 0)
2938 return flagslen;
2939
2940 /* <folcharslen> <folchars> */
2941 fol = read_cnt_string(fd, 2, &follen);
2942 if (follen < 0)
2943 {
2944 vim_free(flags);
2945 return follen;
2946 }
2947
2948 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
2949 if (flags != NULL && fol != NULL)
2950 set_spell_charflags(flags, flagslen, fol);
2951
2952 vim_free(flags);
2953 vim_free(fol);
2954
2955 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
2956 if ((flags == NULL) != (fol == NULL))
2957 return SP_FORMERROR;
2958 return 0;
2959}
2960
2961/*
2962 * Read SN_PREFCOND section.
2963 * Return SP_*ERROR flags.
2964 */
2965 static int
2966read_prefcond_section(fd, lp)
2967 FILE *fd;
2968 slang_T *lp;
2969{
2970 int cnt;
2971 int i;
2972 int n;
2973 char_u *p;
2974 char_u buf[MAXWLEN + 1];
2975
2976 /* <prefcondcnt> <prefcond> ... */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002977 cnt = get2c(fd); /* <prefcondcnt> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002978 if (cnt <= 0)
2979 return SP_FORMERROR;
2980
2981 lp->sl_prefprog = (regprog_T **)alloc_clear(
2982 (unsigned)sizeof(regprog_T *) * cnt);
2983 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002984 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002985 lp->sl_prefixcnt = cnt;
2986
2987 for (i = 0; i < cnt; ++i)
2988 {
2989 /* <prefcond> : <condlen> <condstr> */
2990 n = getc(fd); /* <condlen> */
2991 if (n < 0 || n >= MAXWLEN)
2992 return SP_FORMERROR;
2993
2994 /* When <condlen> is zero we have an empty condition. Otherwise
2995 * compile the regexp program used to check for the condition. */
2996 if (n > 0)
2997 {
2998 buf[0] = '^'; /* always match at one position only */
2999 p = buf + 1;
3000 while (n-- > 0)
3001 *p++ = getc(fd); /* <condstr> */
3002 *p = NUL;
3003 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
3004 }
3005 }
3006 return 0;
3007}
3008
3009/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003010 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
Bram Moolenaar5195e452005-08-19 20:32:47 +00003011 * Return SP_*ERROR flags.
3012 */
3013 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00003014read_rep_section(fd, gap, first)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003015 FILE *fd;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003016 garray_T *gap;
3017 short *first;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003018{
3019 int cnt;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003020 fromto_T *ftp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003021 int i;
3022
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003023 cnt = get2c(fd); /* <repcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003024 if (cnt < 0)
3025 return SP_TRUNCERROR;
3026
Bram Moolenaar5195e452005-08-19 20:32:47 +00003027 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003028 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003029
3030 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
3031 for (; gap->ga_len < cnt; ++gap->ga_len)
3032 {
3033 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
3034 ftp->ft_from = read_cnt_string(fd, 1, &i);
3035 if (i < 0)
3036 return i;
3037 if (i == 0)
3038 return SP_FORMERROR;
3039 ftp->ft_to = read_cnt_string(fd, 1, &i);
3040 if (i <= 0)
3041 {
3042 vim_free(ftp->ft_from);
3043 if (i < 0)
3044 return i;
3045 return SP_FORMERROR;
3046 }
3047 }
3048
3049 /* Fill the first-index table. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003050 for (i = 0; i < 256; ++i)
3051 first[i] = -1;
3052 for (i = 0; i < gap->ga_len; ++i)
3053 {
3054 ftp = &((fromto_T *)gap->ga_data)[i];
3055 if (first[*ftp->ft_from] == -1)
3056 first[*ftp->ft_from] = i;
3057 }
3058 return 0;
3059}
3060
3061/*
3062 * Read SN_SAL section: <salflags> <salcount> <sal> ...
3063 * Return SP_*ERROR flags.
3064 */
3065 static int
3066read_sal_section(fd, slang)
3067 FILE *fd;
3068 slang_T *slang;
3069{
3070 int i;
3071 int cnt;
3072 garray_T *gap;
3073 salitem_T *smp;
3074 int ccnt;
3075 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003076 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003077
3078 slang->sl_sofo = FALSE;
3079
3080 i = getc(fd); /* <salflags> */
3081 if (i & SAL_F0LLOWUP)
3082 slang->sl_followup = TRUE;
3083 if (i & SAL_COLLAPSE)
3084 slang->sl_collapse = TRUE;
3085 if (i & SAL_REM_ACCENTS)
3086 slang->sl_rem_accents = TRUE;
3087
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003088 cnt = get2c(fd); /* <salcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003089 if (cnt < 0)
3090 return SP_TRUNCERROR;
3091
3092 gap = &slang->sl_sal;
3093 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003094 if (ga_grow(gap, cnt + 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003095 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003096
3097 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
3098 for (; gap->ga_len < cnt; ++gap->ga_len)
3099 {
3100 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3101 ccnt = getc(fd); /* <salfromlen> */
3102 if (ccnt < 0)
3103 return SP_TRUNCERROR;
3104 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003105 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003106 smp->sm_lead = p;
3107
3108 /* Read up to the first special char into sm_lead. */
3109 for (i = 0; i < ccnt; ++i)
3110 {
3111 c = getc(fd); /* <salfrom> */
3112 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
3113 break;
3114 *p++ = c;
3115 }
3116 smp->sm_leadlen = p - smp->sm_lead;
3117 *p++ = NUL;
3118
3119 /* Put (abc) chars in sm_oneof, if any. */
3120 if (c == '(')
3121 {
3122 smp->sm_oneof = p;
3123 for (++i; i < ccnt; ++i)
3124 {
3125 c = getc(fd); /* <salfrom> */
3126 if (c == ')')
3127 break;
3128 *p++ = c;
3129 }
3130 *p++ = NUL;
3131 if (++i < ccnt)
3132 c = getc(fd);
3133 }
3134 else
3135 smp->sm_oneof = NULL;
3136
3137 /* Any following chars go in sm_rules. */
3138 smp->sm_rules = p;
3139 if (i < ccnt)
3140 /* store the char we got while checking for end of sm_lead */
3141 *p++ = c;
3142 for (++i; i < ccnt; ++i)
3143 *p++ = getc(fd); /* <salfrom> */
3144 *p++ = NUL;
3145
3146 /* <saltolen> <salto> */
3147 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
3148 if (ccnt < 0)
3149 {
3150 vim_free(smp->sm_lead);
3151 return ccnt;
3152 }
3153
3154#ifdef FEAT_MBYTE
3155 if (has_mbyte)
3156 {
3157 /* convert the multi-byte strings to wide char strings */
3158 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3159 smp->sm_leadlen = mb_charlen(smp->sm_lead);
3160 if (smp->sm_oneof == NULL)
3161 smp->sm_oneof_w = NULL;
3162 else
3163 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
3164 if (smp->sm_to == NULL)
3165 smp->sm_to_w = NULL;
3166 else
3167 smp->sm_to_w = mb_str2wide(smp->sm_to);
3168 if (smp->sm_lead_w == NULL
3169 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
3170 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
3171 {
3172 vim_free(smp->sm_lead);
3173 vim_free(smp->sm_to);
3174 vim_free(smp->sm_lead_w);
3175 vim_free(smp->sm_oneof_w);
3176 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003177 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003178 }
3179 }
3180#endif
3181 }
3182
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003183 if (gap->ga_len > 0)
3184 {
3185 /* Add one extra entry to mark the end with an empty sm_lead. Avoids
3186 * that we need to check the index every time. */
3187 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3188 if ((p = alloc(1)) == NULL)
3189 return SP_OTHERERROR;
3190 p[0] = NUL;
3191 smp->sm_lead = p;
3192 smp->sm_leadlen = 0;
3193 smp->sm_oneof = NULL;
3194 smp->sm_rules = p;
3195 smp->sm_to = NULL;
3196#ifdef FEAT_MBYTE
3197 if (has_mbyte)
3198 {
3199 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3200 smp->sm_leadlen = 0;
3201 smp->sm_oneof_w = NULL;
3202 smp->sm_to_w = NULL;
3203 }
3204#endif
3205 ++gap->ga_len;
3206 }
3207
Bram Moolenaar5195e452005-08-19 20:32:47 +00003208 /* Fill the first-index table. */
3209 set_sal_first(slang);
3210
3211 return 0;
3212}
3213
3214/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003215 * Read SN_WORDS: <word> ...
3216 * Return SP_*ERROR flags.
3217 */
3218 static int
3219read_words_section(fd, lp, len)
3220 FILE *fd;
3221 slang_T *lp;
3222 int len;
3223{
3224 int done = 0;
3225 int i;
3226 char_u word[MAXWLEN];
3227
3228 while (done < len)
3229 {
3230 /* Read one word at a time. */
3231 for (i = 0; ; ++i)
3232 {
3233 word[i] = getc(fd);
3234 if (word[i] == NUL)
3235 break;
3236 if (i == MAXWLEN - 1)
3237 return SP_FORMERROR;
3238 }
3239
3240 /* Init the count to 10. */
3241 count_common_word(lp, word, -1, 10);
3242 done += i + 1;
3243 }
3244 return 0;
3245}
3246
3247/*
3248 * Add a word to the hashtable of common words.
3249 * If it's already there then the counter is increased.
3250 */
3251 static void
3252count_common_word(lp, word, len, count)
3253 slang_T *lp;
3254 char_u *word;
3255 int len; /* word length, -1 for upto NUL */
3256 int count; /* 1 to count once, 10 to init */
3257{
3258 hash_T hash;
3259 hashitem_T *hi;
3260 wordcount_T *wc;
3261 char_u buf[MAXWLEN];
3262 char_u *p;
3263
3264 if (len == -1)
3265 p = word;
3266 else
3267 {
3268 vim_strncpy(buf, word, len);
3269 p = buf;
3270 }
3271
3272 hash = hash_hash(p);
3273 hi = hash_lookup(&lp->sl_wordcount, p, hash);
3274 if (HASHITEM_EMPTY(hi))
3275 {
3276 wc = (wordcount_T *)alloc(sizeof(wordcount_T) + STRLEN(p));
3277 if (wc == NULL)
3278 return;
3279 STRCPY(wc->wc_word, p);
3280 wc->wc_count = count;
3281 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
3282 }
3283 else
3284 {
3285 wc = HI2WC(hi);
3286 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
3287 wc->wc_count = MAXWORDCOUNT;
3288 }
3289}
3290
3291/*
3292 * Adjust the score of common words.
3293 */
3294 static int
3295score_wordcount_adj(slang, score, word, split)
3296 slang_T *slang;
3297 int score;
3298 char_u *word;
3299 int split; /* word was split, less bonus */
3300{
3301 hashitem_T *hi;
3302 wordcount_T *wc;
3303 int bonus;
3304 int newscore;
3305
3306 hi = hash_find(&slang->sl_wordcount, word);
3307 if (!HASHITEM_EMPTY(hi))
3308 {
3309 wc = HI2WC(hi);
3310 if (wc->wc_count < SCORE_THRES2)
3311 bonus = SCORE_COMMON1;
3312 else if (wc->wc_count < SCORE_THRES3)
3313 bonus = SCORE_COMMON2;
3314 else
3315 bonus = SCORE_COMMON3;
3316 if (split)
3317 newscore = score - bonus / 2;
3318 else
3319 newscore = score - bonus;
3320 if (newscore < 0)
3321 return 0;
3322 return newscore;
3323 }
3324 return score;
3325}
3326
3327/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003328 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
3329 * Return SP_*ERROR flags.
3330 */
3331 static int
3332read_sofo_section(fd, slang)
3333 FILE *fd;
3334 slang_T *slang;
3335{
3336 int cnt;
3337 char_u *from, *to;
3338 int res;
3339
3340 slang->sl_sofo = TRUE;
3341
3342 /* <sofofromlen> <sofofrom> */
3343 from = read_cnt_string(fd, 2, &cnt);
3344 if (cnt < 0)
3345 return cnt;
3346
3347 /* <sofotolen> <sofoto> */
3348 to = read_cnt_string(fd, 2, &cnt);
3349 if (cnt < 0)
3350 {
3351 vim_free(from);
3352 return cnt;
3353 }
3354
3355 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
3356 if (from != NULL && to != NULL)
3357 res = set_sofo(slang, from, to);
3358 else if (from != NULL || to != NULL)
3359 res = SP_FORMERROR; /* only one of two strings is an error */
3360 else
3361 res = 0;
3362
3363 vim_free(from);
3364 vim_free(to);
3365 return res;
3366}
3367
3368/*
3369 * Read the compound section from the .spl file:
3370 * <compmax> <compminlen> <compsylmax> <compflags>
3371 * Returns SP_*ERROR flags.
3372 */
3373 static int
3374read_compound(fd, slang, len)
3375 FILE *fd;
3376 slang_T *slang;
3377 int len;
3378{
3379 int todo = len;
3380 int c;
3381 int atstart;
3382 char_u *pat;
3383 char_u *pp;
3384 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003385 char_u *ap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003386
3387 if (todo < 2)
3388 return SP_FORMERROR; /* need at least two bytes */
3389
3390 --todo;
3391 c = getc(fd); /* <compmax> */
3392 if (c < 2)
3393 c = MAXWLEN;
3394 slang->sl_compmax = c;
3395
3396 --todo;
3397 c = getc(fd); /* <compminlen> */
3398 if (c < 1)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003399 c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003400 slang->sl_compminlen = c;
3401
3402 --todo;
3403 c = getc(fd); /* <compsylmax> */
3404 if (c < 1)
3405 c = MAXWLEN;
3406 slang->sl_compsylmax = c;
3407
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003408 /* Turn the COMPOUNDRULE items into a regexp pattern:
Bram Moolenaar5195e452005-08-19 20:32:47 +00003409 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003410 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3411 * Conversion to utf-8 may double the size. */
3412 c = todo * 2 + 7;
3413#ifdef FEAT_MBYTE
3414 if (enc_utf8)
3415 c += todo * 2;
3416#endif
3417 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003418 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003419 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003420
Bram Moolenaard12a1322005-08-21 22:08:24 +00003421 /* We also need a list of all flags that can appear at the start and one
3422 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003423 cp = alloc(todo + 1);
3424 if (cp == NULL)
3425 {
3426 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003427 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003428 }
3429 slang->sl_compstartflags = cp;
3430 *cp = NUL;
3431
Bram Moolenaard12a1322005-08-21 22:08:24 +00003432 ap = alloc(todo + 1);
3433 if (ap == NULL)
3434 {
3435 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003436 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003437 }
3438 slang->sl_compallflags = ap;
3439 *ap = NUL;
3440
Bram Moolenaar5195e452005-08-19 20:32:47 +00003441 pp = pat;
3442 *pp++ = '^';
3443 *pp++ = '\\';
3444 *pp++ = '(';
3445
3446 atstart = 1;
3447 while (todo-- > 0)
3448 {
3449 c = getc(fd); /* <compflags> */
Bram Moolenaard12a1322005-08-21 22:08:24 +00003450
3451 /* Add all flags to "sl_compallflags". */
3452 if (vim_strchr((char_u *)"+*[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00003453 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003454 {
3455 *ap++ = c;
3456 *ap = NUL;
3457 }
3458
Bram Moolenaar5195e452005-08-19 20:32:47 +00003459 if (atstart != 0)
3460 {
3461 /* At start of item: copy flags to "sl_compstartflags". For a
3462 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3463 if (c == '[')
3464 atstart = 2;
3465 else if (c == ']')
3466 atstart = 0;
3467 else
3468 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003469 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003470 {
3471 *cp++ = c;
3472 *cp = NUL;
3473 }
3474 if (atstart == 1)
3475 atstart = 0;
3476 }
3477 }
3478 if (c == '/') /* slash separates two items */
3479 {
3480 *pp++ = '\\';
3481 *pp++ = '|';
3482 atstart = 1;
3483 }
3484 else /* normal char, "[abc]" and '*' are copied as-is */
3485 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003486 if (c == '+' || c == '~')
Bram Moolenaar5195e452005-08-19 20:32:47 +00003487 *pp++ = '\\'; /* "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003488#ifdef FEAT_MBYTE
3489 if (enc_utf8)
3490 pp += mb_char2bytes(c, pp);
3491 else
3492#endif
3493 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003494 }
3495 }
3496
3497 *pp++ = '\\';
3498 *pp++ = ')';
3499 *pp++ = '$';
3500 *pp = NUL;
3501
3502 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3503 vim_free(pat);
3504 if (slang->sl_compprog == NULL)
3505 return SP_FORMERROR;
3506
3507 return 0;
3508}
3509
Bram Moolenaar6de68532005-08-24 22:08:48 +00003510/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003511 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003512 * Like strchr() but independent of locale.
3513 */
3514 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003515byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003516 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003517 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003518{
3519 char_u *p;
3520
3521 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003522 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003523 return TRUE;
3524 return FALSE;
3525}
3526
Bram Moolenaar5195e452005-08-19 20:32:47 +00003527#define SY_MAXLEN 30
3528typedef struct syl_item_S
3529{
3530 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3531 int sy_len;
3532} syl_item_T;
3533
3534/*
3535 * Truncate "slang->sl_syllable" at the first slash and put the following items
3536 * in "slang->sl_syl_items".
3537 */
3538 static int
3539init_syl_tab(slang)
3540 slang_T *slang;
3541{
3542 char_u *p;
3543 char_u *s;
3544 int l;
3545 syl_item_T *syl;
3546
3547 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3548 p = vim_strchr(slang->sl_syllable, '/');
3549 while (p != NULL)
3550 {
3551 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003552 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003553 break;
3554 s = p;
3555 p = vim_strchr(p, '/');
3556 if (p == NULL)
3557 l = STRLEN(s);
3558 else
3559 l = p - s;
3560 if (l >= SY_MAXLEN)
3561 return SP_FORMERROR;
3562 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003563 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003564 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3565 + slang->sl_syl_items.ga_len++;
3566 vim_strncpy(syl->sy_chars, s, l);
3567 syl->sy_len = l;
3568 }
3569 return OK;
3570}
3571
3572/*
3573 * Count the number of syllables in "word".
3574 * When "word" contains spaces the syllables after the last space are counted.
3575 * Returns zero if syllables are not defines.
3576 */
3577 static int
3578count_syllables(slang, word)
3579 slang_T *slang;
3580 char_u *word;
3581{
3582 int cnt = 0;
3583 int skip = FALSE;
3584 char_u *p;
3585 int len;
3586 int i;
3587 syl_item_T *syl;
3588 int c;
3589
3590 if (slang->sl_syllable == NULL)
3591 return 0;
3592
3593 for (p = word; *p != NUL; p += len)
3594 {
3595 /* When running into a space reset counter. */
3596 if (*p == ' ')
3597 {
3598 len = 1;
3599 cnt = 0;
3600 continue;
3601 }
3602
3603 /* Find longest match of syllable items. */
3604 len = 0;
3605 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3606 {
3607 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3608 if (syl->sy_len > len
3609 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3610 len = syl->sy_len;
3611 }
3612 if (len != 0) /* found a match, count syllable */
3613 {
3614 ++cnt;
3615 skip = FALSE;
3616 }
3617 else
3618 {
3619 /* No recognized syllable item, at least a syllable char then? */
3620#ifdef FEAT_MBYTE
3621 c = mb_ptr2char(p);
3622 len = (*mb_ptr2len)(p);
3623#else
3624 c = *p;
3625 len = 1;
3626#endif
3627 if (vim_strchr(slang->sl_syllable, c) == NULL)
3628 skip = FALSE; /* No, search for next syllable */
3629 else if (!skip)
3630 {
3631 ++cnt; /* Yes, count it */
3632 skip = TRUE; /* don't count following syllable chars */
3633 }
3634 }
3635 }
3636 return cnt;
3637}
3638
3639/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003640 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003641 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003642 */
3643 static int
3644set_sofo(lp, from, to)
3645 slang_T *lp;
3646 char_u *from;
3647 char_u *to;
3648{
3649 int i;
3650
3651#ifdef FEAT_MBYTE
3652 garray_T *gap;
3653 char_u *s;
3654 char_u *p;
3655 int c;
3656 int *inp;
3657
3658 if (has_mbyte)
3659 {
3660 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3661 * characters. The index is the low byte of the character.
3662 * The list contains from-to pairs with a terminating NUL.
3663 * sl_sal_first[] is used for latin1 "from" characters. */
3664 gap = &lp->sl_sal;
3665 ga_init2(gap, sizeof(int *), 1);
3666 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003667 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003668 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3669 gap->ga_len = 256;
3670
3671 /* First count the number of items for each list. Temporarily use
3672 * sl_sal_first[] for this. */
3673 for (p = from, s = to; *p != NUL && *s != NUL; )
3674 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003675 c = mb_cptr2char_adv(&p);
3676 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003677 if (c >= 256)
3678 ++lp->sl_sal_first[c & 0xff];
3679 }
3680 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003681 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003682
3683 /* Allocate the lists. */
3684 for (i = 0; i < 256; ++i)
3685 if (lp->sl_sal_first[i] > 0)
3686 {
3687 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3688 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003689 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003690 ((int **)gap->ga_data)[i] = (int *)p;
3691 *(int *)p = 0;
3692 }
3693
3694 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3695 * list. */
3696 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3697 for (p = from, s = to; *p != NUL && *s != NUL; )
3698 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003699 c = mb_cptr2char_adv(&p);
3700 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003701 if (c >= 256)
3702 {
3703 /* Append the from-to chars at the end of the list with
3704 * the low byte. */
3705 inp = ((int **)gap->ga_data)[c & 0xff];
3706 while (*inp != 0)
3707 ++inp;
3708 *inp++ = c; /* from char */
3709 *inp++ = i; /* to char */
3710 *inp++ = NUL; /* NUL at the end */
3711 }
3712 else
3713 /* mapping byte to char is done in sl_sal_first[] */
3714 lp->sl_sal_first[c] = i;
3715 }
3716 }
3717 else
3718#endif
3719 {
3720 /* mapping bytes to bytes is done in sl_sal_first[] */
3721 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003722 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003723
3724 for (i = 0; to[i] != NUL; ++i)
3725 lp->sl_sal_first[from[i]] = to[i];
3726 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3727 }
3728
Bram Moolenaar5195e452005-08-19 20:32:47 +00003729 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003730}
3731
3732/*
3733 * Fill the first-index table for "lp".
3734 */
3735 static void
3736set_sal_first(lp)
3737 slang_T *lp;
3738{
3739 salfirst_T *sfirst;
3740 int i;
3741 salitem_T *smp;
3742 int c;
3743 garray_T *gap = &lp->sl_sal;
3744
3745 sfirst = lp->sl_sal_first;
3746 for (i = 0; i < 256; ++i)
3747 sfirst[i] = -1;
3748 smp = (salitem_T *)gap->ga_data;
3749 for (i = 0; i < gap->ga_len; ++i)
3750 {
3751#ifdef FEAT_MBYTE
3752 if (has_mbyte)
3753 /* Use the lowest byte of the first character. For latin1 it's
3754 * the character, for other encodings it should differ for most
3755 * characters. */
3756 c = *smp[i].sm_lead_w & 0xff;
3757 else
3758#endif
3759 c = *smp[i].sm_lead;
3760 if (sfirst[c] == -1)
3761 {
3762 sfirst[c] = i;
3763#ifdef FEAT_MBYTE
3764 if (has_mbyte)
3765 {
3766 int n;
3767
3768 /* Make sure all entries with this byte are following each
3769 * other. Move the ones that are in the wrong position. Do
3770 * keep the same ordering! */
3771 while (i + 1 < gap->ga_len
3772 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3773 /* Skip over entry with same index byte. */
3774 ++i;
3775
3776 for (n = 1; i + n < gap->ga_len; ++n)
3777 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3778 {
3779 salitem_T tsal;
3780
3781 /* Move entry with same index byte after the entries
3782 * we already found. */
3783 ++i;
3784 --n;
3785 tsal = smp[i + n];
3786 mch_memmove(smp + i + 1, smp + i,
3787 sizeof(salitem_T) * n);
3788 smp[i] = tsal;
3789 }
3790 }
3791#endif
3792 }
3793 }
3794}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003795
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003796#ifdef FEAT_MBYTE
3797/*
3798 * Turn a multi-byte string into a wide character string.
3799 * Return it in allocated memory (NULL for out-of-memory)
3800 */
3801 static int *
3802mb_str2wide(s)
3803 char_u *s;
3804{
3805 int *res;
3806 char_u *p;
3807 int i = 0;
3808
3809 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3810 if (res != NULL)
3811 {
3812 for (p = s; *p != NUL; )
3813 res[i++] = mb_ptr2char_adv(&p);
3814 res[i] = NUL;
3815 }
3816 return res;
3817}
3818#endif
3819
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003820/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003821 * Read a tree from the .spl or .sug file.
3822 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
3823 * This is skipped when the tree has zero length.
3824 * Returns zero when OK, SP_ value for an error.
3825 */
3826 static int
3827spell_read_tree(fd, bytsp, idxsp, prefixtree, prefixcnt)
3828 FILE *fd;
3829 char_u **bytsp;
3830 idx_T **idxsp;
3831 int prefixtree; /* TRUE for the prefix tree */
3832 int prefixcnt; /* when "prefixtree" is TRUE: prefix count */
3833{
3834 int len;
3835 int idx;
3836 char_u *bp;
3837 idx_T *ip;
3838
3839 /* The tree size was computed when writing the file, so that we can
3840 * allocate it as one long block. <nodecount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003841 len = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003842 if (len < 0)
3843 return SP_TRUNCERROR;
3844 if (len > 0)
3845 {
3846 /* Allocate the byte array. */
3847 bp = lalloc((long_u)len, TRUE);
3848 if (bp == NULL)
3849 return SP_OTHERERROR;
3850 *bytsp = bp;
3851
3852 /* Allocate the index array. */
3853 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
3854 if (ip == NULL)
3855 return SP_OTHERERROR;
3856 *idxsp = ip;
3857
3858 /* Recursively read the tree and store it in the array. */
3859 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
3860 if (idx < 0)
3861 return idx;
3862 }
3863 return 0;
3864}
3865
3866/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003867 * Read one row of siblings from the spell file and store it in the byte array
3868 * "byts" and index array "idxs". Recursively read the children.
3869 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003870 * NOTE: The code here must match put_node()!
Bram Moolenaar51485f02005-06-04 21:55:20 +00003871 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003872 * Returns the index (>= 0) following the siblings.
3873 * Returns SP_TRUNCERROR if the file is shorter than expected.
3874 * Returns SP_FORMERROR if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003875 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003876 static idx_T
Bram Moolenaar4770d092006-01-12 23:22:24 +00003877read_tree_node(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003878 FILE *fd;
3879 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003880 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003881 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003882 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003883 int prefixtree; /* TRUE for reading PREFIXTREE */
3884 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003885{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003886 int len;
3887 int i;
3888 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003889 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003890 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003891 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003892#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003893
Bram Moolenaar51485f02005-06-04 21:55:20 +00003894 len = getc(fd); /* <siblingcount> */
3895 if (len <= 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003896 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003897
3898 if (startidx + len >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003899 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003900 byts[idx++] = len;
3901
3902 /* Read the byte values, flag/region bytes and shared indexes. */
3903 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003904 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003905 c = getc(fd); /* <byte> */
3906 if (c < 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003907 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003908 if (c <= BY_SPECIAL)
3909 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003910 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003911 {
3912 /* No flags, all regions. */
3913 idxs[idx] = 0;
3914 c = 0;
3915 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003916 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003917 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003918 if (prefixtree)
3919 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003920 /* Read the optional pflags byte, the prefix ID and the
3921 * condition nr. In idxs[] store the prefix ID in the low
3922 * byte, the condition index shifted up 8 bits, the flags
3923 * shifted up 24 bits. */
3924 if (c == BY_FLAGS)
3925 c = getc(fd) << 24; /* <pflags> */
3926 else
3927 c = 0;
3928
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003929 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003930
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003931 n = get2c(fd); /* <prefcondnr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003932 if (n >= maxprefcondnr)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003933 return SP_FORMERROR;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003934 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003935 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003936 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003937 {
3938 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003939 * idxs[] the flags go in the low two bytes, region above
3940 * that and prefix ID above the region. */
3941 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003942 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003943 if (c2 == BY_FLAGS2)
3944 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003945 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003946 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003947 if (c & WF_AFX)
3948 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003949 }
3950
Bram Moolenaar51485f02005-06-04 21:55:20 +00003951 idxs[idx] = c;
3952 c = 0;
3953 }
3954 else /* c == BY_INDEX */
3955 {
3956 /* <nodeidx> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003957 n = get3c(fd);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003958 if (n < 0 || n >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003959 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003960 idxs[idx] = n + SHARED_MASK;
3961 c = getc(fd); /* <xbyte> */
3962 }
3963 }
3964 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003965 }
3966
Bram Moolenaar51485f02005-06-04 21:55:20 +00003967 /* Recursively read the children for non-shared siblings.
3968 * Skip the end-of-word ones (zero byte value) and the shared ones (and
3969 * remove SHARED_MASK) */
3970 for (i = 1; i <= len; ++i)
3971 if (byts[startidx + i] != 0)
3972 {
3973 if (idxs[startidx + i] & SHARED_MASK)
3974 idxs[startidx + i] &= ~SHARED_MASK;
3975 else
3976 {
3977 idxs[startidx + i] = idx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003978 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003979 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003980 if (idx < 0)
3981 break;
3982 }
3983 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003984
Bram Moolenaar51485f02005-06-04 21:55:20 +00003985 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003986}
3987
3988/*
3989 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003990 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003991 */
3992 char_u *
3993did_set_spelllang(buf)
3994 buf_T *buf;
3995{
3996 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003997 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003998 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00003999 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004000 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004001 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004002 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004003 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004004 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004005 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004006 int len;
4007 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004008 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004009 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004010 char_u *use_region = NULL;
4011 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004012 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004013 int i, j;
4014 langp_T *lp, *lp2;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004015
4016 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004017 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004018
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004019 /* loop over comma separated language names. */
4020 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004021 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004022 /* Get one language name. */
4023 copy_option_part(&splp, lang, MAXWLEN, ",");
4024
Bram Moolenaar5482f332005-04-17 20:18:43 +00004025 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004026 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004027
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004028 /* If the name ends in ".spl" use it as the name of the spell file.
4029 * If there is a region name let "region" point to it and remove it
4030 * from the name. */
4031 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
4032 {
4033 filename = TRUE;
4034
Bram Moolenaarb6356332005-07-18 21:40:44 +00004035 /* Locate a region and remove it from the file name. */
4036 p = vim_strchr(gettail(lang), '_');
4037 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
4038 && !ASCII_ISALPHA(p[3]))
4039 {
4040 vim_strncpy(region_cp, p + 1, 2);
4041 mch_memmove(p, p + 3, len - (p - lang) - 2);
4042 len -= 3;
4043 region = region_cp;
4044 }
4045 else
4046 dont_use_region = TRUE;
4047
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004048 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004049 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4050 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004051 break;
4052 }
4053 else
4054 {
4055 filename = FALSE;
4056 if (len > 3 && lang[len - 3] == '_')
4057 {
4058 region = lang + len - 2;
4059 len -= 3;
4060 lang[len] = NUL;
4061 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004062 else
4063 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004064
4065 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004066 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4067 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004068 break;
4069 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004070
Bram Moolenaarb6356332005-07-18 21:40:44 +00004071 if (region != NULL)
4072 {
4073 /* If the region differs from what was used before then don't
4074 * use it for 'spellfile'. */
4075 if (use_region != NULL && STRCMP(region, use_region) != 0)
4076 dont_use_region = TRUE;
4077 use_region = region;
4078 }
4079
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004080 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004081 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004082 {
4083 if (filename)
4084 (void)spell_load_file(lang, lang, NULL, FALSE);
4085 else
4086 spell_load_lang(lang);
4087 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004088
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004089 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004090 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004091 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004092 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4093 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
4094 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004095 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004096 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004097 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004098 {
4099 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004100 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004101 if (c == REGION_ALL)
4102 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004103 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004104 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004105 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004106 /* This addition file is for other regions. */
4107 region_mask = 0;
4108 }
4109 else
4110 /* This is probably an error. Give a warning and
4111 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004112 smsg((char_u *)
4113 _("Warning: region %s not supported"),
4114 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004115 }
4116 else
4117 region_mask = 1 << c;
4118 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004119
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004120 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004121 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004122 if (ga_grow(&ga, 1) == FAIL)
4123 {
4124 ga_clear(&ga);
4125 return e_outofmem;
4126 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004127 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004128 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4129 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004130 use_midword(slang, buf);
4131 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004132 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004133 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004134 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004135 }
4136
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004137 /* round 0: load int_wordlist, if possible.
4138 * round 1: load first name in 'spellfile'.
4139 * round 2: load second name in 'spellfile.
4140 * etc. */
4141 spf = curbuf->b_p_spf;
4142 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004143 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004144 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004145 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004146 /* Internal wordlist, if there is one. */
4147 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004148 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004149 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004150 }
4151 else
4152 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004153 /* One entry in 'spellfile'. */
4154 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
4155 STRCAT(spf_name, ".spl");
4156
4157 /* If it was already found above then skip it. */
4158 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004159 {
4160 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
4161 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004162 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004163 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004164 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004165 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004166 }
4167
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004168 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004169 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4170 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004171 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004172 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004173 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004174 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004175 * region name, the region is ignored otherwise. for int_wordlist
4176 * use an arbitrary name. */
4177 if (round == 0)
4178 STRCPY(lang, "internal wordlist");
4179 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00004180 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004181 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004182 p = vim_strchr(lang, '.');
4183 if (p != NULL)
4184 *p = NUL; /* truncate at ".encoding.add" */
4185 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004186 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004187
4188 /* If one of the languages has NOBREAK we assume the addition
4189 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004190 if (slang != NULL && nobreak)
4191 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004192 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004193 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004194 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004195 region_mask = REGION_ALL;
4196 if (use_region != NULL && !dont_use_region)
4197 {
4198 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004199 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004200 if (c != REGION_ALL)
4201 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004202 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004203 /* This spell file is for other regions. */
4204 region_mask = 0;
4205 }
4206
4207 if (region_mask != 0)
4208 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004209 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
4210 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
4211 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004212 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4213 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004214 use_midword(slang, buf);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004215 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004216 }
4217 }
4218
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004219 /* Everything is fine, store the new b_langp value. */
4220 ga_clear(&buf->b_langp);
4221 buf->b_langp = ga;
4222
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004223 /* For each language figure out what language to use for sound folding and
4224 * REP items. If the language doesn't support it itself use another one
4225 * with the same name. E.g. for "en-math" use "en". */
4226 for (i = 0; i < ga.ga_len; ++i)
4227 {
4228 lp = LANGP_ENTRY(ga, i);
4229
4230 /* sound folding */
4231 if (lp->lp_slang->sl_sal.ga_len > 0)
4232 /* language does sound folding itself */
4233 lp->lp_sallang = lp->lp_slang;
4234 else
4235 /* find first similar language that does sound folding */
4236 for (j = 0; j < ga.ga_len; ++j)
4237 {
4238 lp2 = LANGP_ENTRY(ga, j);
4239 if (lp2->lp_slang->sl_sal.ga_len > 0
4240 && STRNCMP(lp->lp_slang->sl_name,
4241 lp2->lp_slang->sl_name, 2) == 0)
4242 {
4243 lp->lp_sallang = lp2->lp_slang;
4244 break;
4245 }
4246 }
4247
4248 /* REP items */
4249 if (lp->lp_slang->sl_rep.ga_len > 0)
4250 /* language has REP items itself */
4251 lp->lp_replang = lp->lp_slang;
4252 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004253 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004254 for (j = 0; j < ga.ga_len; ++j)
4255 {
4256 lp2 = LANGP_ENTRY(ga, j);
4257 if (lp2->lp_slang->sl_rep.ga_len > 0
4258 && STRNCMP(lp->lp_slang->sl_name,
4259 lp2->lp_slang->sl_name, 2) == 0)
4260 {
4261 lp->lp_replang = lp2->lp_slang;
4262 break;
4263 }
4264 }
4265 }
4266
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004267 return NULL;
4268}
4269
4270/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004271 * Clear the midword characters for buffer "buf".
4272 */
4273 static void
4274clear_midword(buf)
4275 buf_T *buf;
4276{
4277 vim_memset(buf->b_spell_ismw, 0, 256);
4278#ifdef FEAT_MBYTE
4279 vim_free(buf->b_spell_ismw_mb);
4280 buf->b_spell_ismw_mb = NULL;
4281#endif
4282}
4283
4284/*
4285 * Use the "sl_midword" field of language "lp" for buffer "buf".
4286 * They add up to any currently used midword characters.
4287 */
4288 static void
4289use_midword(lp, buf)
4290 slang_T *lp;
4291 buf_T *buf;
4292{
4293 char_u *p;
4294
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004295 if (lp->sl_midword == NULL) /* there aren't any */
4296 return;
4297
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004298 for (p = lp->sl_midword; *p != NUL; )
4299#ifdef FEAT_MBYTE
4300 if (has_mbyte)
4301 {
4302 int c, l, n;
4303 char_u *bp;
4304
4305 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004306 l = (*mb_ptr2len)(p);
4307 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004308 buf->b_spell_ismw[c] = TRUE;
4309 else if (buf->b_spell_ismw_mb == NULL)
4310 /* First multi-byte char in "b_spell_ismw_mb". */
4311 buf->b_spell_ismw_mb = vim_strnsave(p, l);
4312 else
4313 {
4314 /* Append multi-byte chars to "b_spell_ismw_mb". */
4315 n = STRLEN(buf->b_spell_ismw_mb);
4316 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
4317 if (bp != NULL)
4318 {
4319 vim_free(buf->b_spell_ismw_mb);
4320 buf->b_spell_ismw_mb = bp;
4321 vim_strncpy(bp + n, p, l);
4322 }
4323 }
4324 p += l;
4325 }
4326 else
4327#endif
4328 buf->b_spell_ismw[*p++] = TRUE;
4329}
4330
4331/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004332 * Find the region "region[2]" in "rp" (points to "sl_regions").
4333 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00004334 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004335 */
4336 static int
4337find_region(rp, region)
4338 char_u *rp;
4339 char_u *region;
4340{
4341 int i;
4342
4343 for (i = 0; ; i += 2)
4344 {
4345 if (rp[i] == NUL)
4346 return REGION_ALL;
4347 if (rp[i] == region[0] && rp[i + 1] == region[1])
4348 break;
4349 }
4350 return i / 2;
4351}
4352
4353/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004354 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004355 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00004356 * Word WF_ONECAP
4357 * W WORD WF_ALLCAP
4358 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004359 */
4360 static int
4361captype(word, end)
4362 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004363 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004364{
4365 char_u *p;
4366 int c;
4367 int firstcap;
4368 int allcap;
4369 int past_second = FALSE; /* past second word char */
4370
4371 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004372 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004373 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004374 return 0; /* only non-word characters, illegal word */
4375#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00004376 if (has_mbyte)
4377 c = mb_ptr2char_adv(&p);
4378 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004379#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00004380 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004381 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004382
4383 /*
4384 * Need to check all letters to find a word with mixed upper/lower.
4385 * But a word with an upper char only at start is a ONECAP.
4386 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004387 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004388 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004389 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004390 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004391 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004392 {
4393 /* UUl -> KEEPCAP */
4394 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004395 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004396 allcap = FALSE;
4397 }
4398 else if (!allcap)
4399 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004400 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004401 past_second = TRUE;
4402 }
4403
4404 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004405 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004406 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004407 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004408 return 0;
4409}
4410
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004411/*
4412 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
4413 * capital. So that make_case_word() can turn WOrd into Word.
4414 * Add ALLCAP for "WOrD".
4415 */
4416 static int
4417badword_captype(word, end)
4418 char_u *word;
4419 char_u *end;
4420{
4421 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004422 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004423 int l, u;
4424 int first;
4425 char_u *p;
4426
4427 if (flags & WF_KEEPCAP)
4428 {
4429 /* Count the number of UPPER and lower case letters. */
4430 l = u = 0;
4431 first = FALSE;
4432 for (p = word; p < end; mb_ptr_adv(p))
4433 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004434 c = PTR2CHAR(p);
4435 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004436 {
4437 ++u;
4438 if (p == word)
4439 first = TRUE;
4440 }
4441 else
4442 ++l;
4443 }
4444
4445 /* If there are more UPPER than lower case letters suggest an
4446 * ALLCAP word. Otherwise, if the first letter is UPPER then
4447 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4448 * require three upper case letters. */
4449 if (u > l && u > 2)
4450 flags |= WF_ALLCAP;
4451 else if (first)
4452 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004453
4454 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
4455 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004456 }
4457 return flags;
4458}
4459
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004460# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
4461/*
4462 * Free all languages.
4463 */
4464 void
4465spell_free_all()
4466{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004467 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004468 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004469 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004470
4471 /* Go through all buffers and handle 'spelllang'. */
4472 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4473 ga_clear(&buf->b_langp);
4474
4475 while (first_lang != NULL)
4476 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004477 slang = first_lang;
4478 first_lang = slang->sl_next;
4479 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004480 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004481
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004482 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004483 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004484 /* Delete the internal wordlist and its .spl file */
4485 mch_remove(int_wordlist);
4486 int_wordlist_spl(fname);
4487 mch_remove(fname);
4488 vim_free(int_wordlist);
4489 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004490 }
4491
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004492 init_spell_chartab();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004493
4494 vim_free(repl_to);
4495 repl_to = NULL;
4496 vim_free(repl_from);
4497 repl_from = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004498}
4499# endif
4500
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004501# if defined(FEAT_MBYTE) || defined(PROTO)
4502/*
4503 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004504 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004505 */
4506 void
4507spell_reload()
4508{
4509 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004510 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004511
Bram Moolenaarea408852005-06-25 22:49:46 +00004512 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004513 init_spell_chartab();
4514
4515 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004516 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004517
4518 /* Go through all buffers and handle 'spelllang'. */
4519 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4520 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004521 /* Only load the wordlists when 'spelllang' is set and there is a
4522 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004523 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004524 {
4525 FOR_ALL_WINDOWS(wp)
4526 if (wp->w_buffer == buf && wp->w_p_spell)
4527 {
4528 (void)did_set_spelllang(buf);
4529# ifdef FEAT_WINDOWS
4530 break;
4531# endif
4532 }
4533 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004534 }
4535}
4536# endif
4537
Bram Moolenaarb765d632005-06-07 21:00:02 +00004538/*
4539 * Reload the spell file "fname" if it's loaded.
4540 */
4541 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004542spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004543 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004544 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004545{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004546 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004547 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004548
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004549 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004550 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004551 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004552 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004553 slang_clear(slang);
4554 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004555 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004556 slang_clear(slang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004557 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004558 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004559 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004560 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004561
4562 /* When "zg" was used and the file wasn't loaded yet, should redo
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004563 * 'spelllang' to load it now. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004564 if (added_word && !didit)
4565 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004566}
4567
4568
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004569/*
4570 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004571 */
4572
Bram Moolenaar51485f02005-06-04 21:55:20 +00004573#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004574 and .dic file. */
4575/*
4576 * Main structure to store the contents of a ".aff" file.
4577 */
4578typedef struct afffile_S
4579{
4580 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004581 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaar371baa92005-12-29 22:43:53 +00004582 unsigned af_rare; /* RARE ID for rare word */
4583 unsigned af_keepcase; /* KEEPCASE ID for keep-case word */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004584 unsigned af_bad; /* BAD ID for banned word */
4585 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004586 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004587 unsigned af_nosuggest; /* NOSUGGEST ID */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004588 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004589 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4590 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004591 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004592} afffile_T;
4593
Bram Moolenaar6de68532005-08-24 22:08:48 +00004594#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004595#define AFT_LONG 1 /* flags are two characters */
4596#define AFT_CAPLONG 2 /* flags are one or two characters */
4597#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004598
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004599typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004600/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4601struct affentry_S
4602{
4603 affentry_T *ae_next; /* next affix with same name/number */
4604 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4605 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004606 char_u *ae_cond; /* condition (NULL for ".") */
4607 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004608 char_u ae_rare; /* rare affix */
4609 char_u ae_nocomp; /* word with affix not compoundable */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004610};
4611
Bram Moolenaar6de68532005-08-24 22:08:48 +00004612#ifdef FEAT_MBYTE
4613# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4614#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004615# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004616#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004617
Bram Moolenaar51485f02005-06-04 21:55:20 +00004618/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4619typedef struct affheader_S
4620{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004621 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4622 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4623 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004624 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004625 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004626 affentry_T *ah_first; /* first affix entry */
4627} affheader_T;
4628
4629#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4630
Bram Moolenaar6de68532005-08-24 22:08:48 +00004631/* Flag used in compound items. */
4632typedef struct compitem_S
4633{
4634 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4635 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4636 int ci_newID; /* affix ID after renumbering. */
4637} compitem_T;
4638
4639#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4640
Bram Moolenaar51485f02005-06-04 21:55:20 +00004641/*
4642 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004643 * the need to keep track of each allocated thing, everything is freed all at
4644 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004645 */
4646#define SBLOCKSIZE 16000 /* size of sb_data */
4647typedef struct sblock_S sblock_T;
4648struct sblock_S
4649{
4650 sblock_T *sb_next; /* next block in list */
4651 int sb_used; /* nr of bytes already in use */
4652 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004653};
4654
4655/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004656 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004657 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004658typedef struct wordnode_S wordnode_T;
4659struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004660{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004661 union /* shared to save space */
4662 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004663 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004664 int index; /* index in written nodes (valid after first
4665 round) */
4666 } wn_u1;
4667 union /* shared to save space */
4668 {
4669 wordnode_T *next; /* next node with same hash key */
4670 wordnode_T *wnode; /* parent node that will write this node */
4671 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004672 wordnode_T *wn_child; /* child (next byte in word) */
4673 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4674 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004675 int wn_refs; /* Nr. of references to this node. Only
4676 relevant for first node in a list of
4677 siblings, in following siblings it is
4678 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004679 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004680
4681 /* Info for when "wn_byte" is NUL.
4682 * In PREFIXTREE "wn_region" is used for the prefcondnr.
4683 * In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
4684 * "wn_region" the LSW of the wordnr. */
4685 char_u wn_affixID; /* supported/required prefix ID or 0 */
4686 short_u wn_flags; /* WF_ flags */
4687 short wn_region; /* region mask */
4688
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004689#ifdef SPELL_PRINTTREE
4690 int wn_nr; /* sequence nr for printing */
4691#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004692};
4693
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004694#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4695
Bram Moolenaar51485f02005-06-04 21:55:20 +00004696#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004697
Bram Moolenaar51485f02005-06-04 21:55:20 +00004698/*
4699 * Info used while reading the spell files.
4700 */
4701typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004702{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004703 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004704 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004705
Bram Moolenaar51485f02005-06-04 21:55:20 +00004706 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004707 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004708
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004709 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004710
Bram Moolenaar4770d092006-01-12 23:22:24 +00004711 long si_sugtree; /* creating the soundfolding trie */
4712
Bram Moolenaar51485f02005-06-04 21:55:20 +00004713 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004714 long si_blocks_cnt; /* memory blocks allocated */
4715 long si_compress_cnt; /* words to add before lowering
4716 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004717 wordnode_T *si_first_free; /* List of nodes that have been freed during
4718 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004719 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004720#ifdef SPELL_PRINTTREE
4721 int si_wordnode_nr; /* sequence nr for nodes */
4722#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004723 buf_T *si_spellbuf; /* buffer used to store soundfold word table */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004724
Bram Moolenaar51485f02005-06-04 21:55:20 +00004725 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004726 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004727 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004728 int si_region; /* region mask */
4729 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004730 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004731 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004732 int si_msg_count; /* number of words added since last message */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004733 char_u *si_info; /* info text chars or NULL */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004734 int si_region_count; /* number of regions supported (1 when there
4735 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004736 char_u si_region_name[16]; /* region names; used only if
4737 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004738
4739 garray_T si_rep; /* list of fromto_T entries from REP lines */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004740 garray_T si_repsal; /* list of fromto_T entries from REPSAL lines */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004741 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004742 char_u *si_sofofr; /* SOFOFROM text */
4743 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004744 int si_nosugfile; /* NOSUGFILE item found */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004745 int si_nosplitsugs; /* NOSPLITSUGS item found */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004746 int si_followup; /* soundsalike: ? */
4747 int si_collapse; /* soundsalike: ? */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004748 hashtab_T si_commonwords; /* hashtable for common words */
4749 time_t si_sugtime; /* timestamp for .sug file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004750 int si_rem_accents; /* soundsalike: remove accents */
4751 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004752 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004753 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004754 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004755 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004756 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004757 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004758 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004759 garray_T si_prefcond; /* table with conditions for postponed
4760 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004761 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004762 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004763} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004764
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004765static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004766static int spell_info_item __ARGS((char_u *s));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004767static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
4768static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
4769static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004770static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004771static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
4772static void aff_check_number __ARGS((int spinval, int affval, char *name));
4773static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004774static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004775static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
4776static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00004777static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004778static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004779static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004780static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004781static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004782static int store_aff_word __ARGS((spellinfo_T *spin, char_u *word, char_u *afflist, afffile_T *affile, hashtab_T *ht, hashtab_T *xht, int comb, int flags, char_u *pfxlist, int pfxlen));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004783static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
4784static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
4785static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004786static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004787static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004788static 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 +00004789static 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 +00004790static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004791static int deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004792static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
4793static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
4794static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004795static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004796static void put_sugtime __ARGS((spellinfo_T *spin, FILE *fd));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004797static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00004798static void clear_node __ARGS((wordnode_T *node));
4799static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004800static void spell_make_sugfile __ARGS((spellinfo_T *spin, char_u *wfname));
4801static int sug_filltree __ARGS((spellinfo_T *spin, slang_T *slang));
4802static int sug_maketable __ARGS((spellinfo_T *spin));
4803static int sug_filltable __ARGS((spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap));
4804static int offset2bytes __ARGS((int nr, char_u *buf));
4805static int bytes2offset __ARGS((char_u **pp));
4806static void sug_write __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004807static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004808static void spell_message __ARGS((spellinfo_T *spin, char_u *str));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004809static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004810
Bram Moolenaar53805d12005-08-01 07:08:33 +00004811/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
4812 * but it must be negative to indicate the prefix tree to tree_add_word().
4813 * Use a negative number with the lower 8 bits zero. */
4814#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004815
Bram Moolenaar5195e452005-08-19 20:32:47 +00004816/*
4817 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
4818 */
4819static long compress_start = 30000; /* memory / SBLOCKSIZE */
4820static long compress_inc = 100; /* memory / SBLOCKSIZE */
4821static long compress_added = 500000; /* word count */
4822
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004823#ifdef SPELL_PRINTTREE
4824/*
4825 * For debugging the tree code: print the current tree in a (more or less)
4826 * readable format, so that we can see what happens when adding a word and/or
4827 * compressing the tree.
4828 * Based on code from Olaf Seibert.
4829 */
4830#define PRINTLINESIZE 1000
4831#define PRINTWIDTH 6
4832
4833#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
4834 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
4835
4836static char line1[PRINTLINESIZE];
4837static char line2[PRINTLINESIZE];
4838static char line3[PRINTLINESIZE];
4839
4840 static void
4841spell_clear_flags(wordnode_T *node)
4842{
4843 wordnode_T *np;
4844
4845 for (np = node; np != NULL; np = np->wn_sibling)
4846 {
4847 np->wn_u1.index = FALSE;
4848 spell_clear_flags(np->wn_child);
4849 }
4850}
4851
4852 static void
4853spell_print_node(wordnode_T *node, int depth)
4854{
4855 if (node->wn_u1.index)
4856 {
4857 /* Done this node before, print the reference. */
4858 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
4859 PRINTSOME(line2, depth, " ", 0, 0);
4860 PRINTSOME(line3, depth, " ", 0, 0);
4861 msg(line1);
4862 msg(line2);
4863 msg(line3);
4864 }
4865 else
4866 {
4867 node->wn_u1.index = TRUE;
4868
4869 if (node->wn_byte != NUL)
4870 {
4871 if (node->wn_child != NULL)
4872 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
4873 else
4874 /* Cannot happen? */
4875 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
4876 }
4877 else
4878 PRINTSOME(line1, depth, " $ ", 0, 0);
4879
4880 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
4881
4882 if (node->wn_sibling != NULL)
4883 PRINTSOME(line3, depth, " | ", 0, 0);
4884 else
4885 PRINTSOME(line3, depth, " ", 0, 0);
4886
4887 if (node->wn_byte == NUL)
4888 {
4889 msg(line1);
4890 msg(line2);
4891 msg(line3);
4892 }
4893
4894 /* do the children */
4895 if (node->wn_byte != NUL && node->wn_child != NULL)
4896 spell_print_node(node->wn_child, depth + 1);
4897
4898 /* do the siblings */
4899 if (node->wn_sibling != NULL)
4900 {
4901 /* get rid of all parent details except | */
4902 STRCPY(line1, line3);
4903 STRCPY(line2, line3);
4904 spell_print_node(node->wn_sibling, depth);
4905 }
4906 }
4907}
4908
4909 static void
4910spell_print_tree(wordnode_T *root)
4911{
4912 if (root != NULL)
4913 {
4914 /* Clear the "wn_u1.index" fields, used to remember what has been
4915 * done. */
4916 spell_clear_flags(root);
4917
4918 /* Recursively print the tree. */
4919 spell_print_node(root, 0);
4920 }
4921}
4922#endif /* SPELL_PRINTTREE */
4923
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004924/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004925 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00004926 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004927 */
4928 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004929spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004930 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004931 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004932{
4933 FILE *fd;
4934 afffile_T *aff;
4935 char_u rline[MAXLINELEN];
4936 char_u *line;
4937 char_u *pc = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004938#define MAXITEMCNT 30
Bram Moolenaar8db73182005-06-17 21:51:16 +00004939 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004940 int itemcnt;
4941 char_u *p;
4942 int lnum = 0;
4943 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004944 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004945 int aff_todo = 0;
4946 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004947 char_u *low = NULL;
4948 char_u *fol = NULL;
4949 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004950 int do_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004951 int do_repsal;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004952 int do_sal;
4953 int do_map;
4954 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004955 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004956 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004957 int compminlen = 0; /* COMPOUNDMIN value */
4958 int compsylmax = 0; /* COMPOUNDSYLMAX value */
4959 int compmax = 0; /* COMPOUNDMAX value */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004960 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDRULE
Bram Moolenaar6de68532005-08-24 22:08:48 +00004961 concatenated */
4962 char_u *midword = NULL; /* MIDWORD value */
4963 char_u *syllable = NULL; /* SYLLABLE value */
4964 char_u *sofofrom = NULL; /* SOFOFROM value */
4965 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004966
Bram Moolenaar51485f02005-06-04 21:55:20 +00004967 /*
4968 * Open the file.
4969 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004970 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004971 if (fd == NULL)
4972 {
4973 EMSG2(_(e_notopen), fname);
4974 return NULL;
4975 }
4976
Bram Moolenaar4770d092006-01-12 23:22:24 +00004977 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s ..."), fname);
4978 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004979
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004980 /* Only do REP lines when not done in another .aff file already. */
4981 do_rep = spin->si_rep.ga_len == 0;
4982
Bram Moolenaar4770d092006-01-12 23:22:24 +00004983 /* Only do REPSAL lines when not done in another .aff file already. */
4984 do_repsal = spin->si_repsal.ga_len == 0;
4985
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004986 /* Only do SAL lines when not done in another .aff file already. */
4987 do_sal = spin->si_sal.ga_len == 0;
4988
4989 /* Only do MAP lines when not done in another .aff file already. */
4990 do_map = spin->si_map.ga_len == 0;
4991
Bram Moolenaar51485f02005-06-04 21:55:20 +00004992 /*
4993 * Allocate and init the afffile_T structure.
4994 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004995 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004996 if (aff == NULL)
4997 return NULL;
4998 hash_init(&aff->af_pref);
4999 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005000 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005001
5002 /*
5003 * Read all the lines in the file one by one.
5004 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005005 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005006 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005007 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005008 ++lnum;
5009
5010 /* Skip comment lines. */
5011 if (*rline == '#')
5012 continue;
5013
5014 /* Convert from "SET" to 'encoding' when needed. */
5015 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005016#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005017 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005018 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005019 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005020 if (pc == NULL)
5021 {
5022 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5023 fname, lnum, rline);
5024 continue;
5025 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005026 line = pc;
5027 }
5028 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005029#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005030 {
5031 pc = NULL;
5032 line = rline;
5033 }
5034
5035 /* Split the line up in white separated items. Put a NUL after each
5036 * item. */
5037 itemcnt = 0;
5038 for (p = line; ; )
5039 {
5040 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
5041 ++p;
5042 if (*p == NUL)
5043 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00005044 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005045 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005046 items[itemcnt++] = p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005047 /* A few items have arbitrary text argument, don't split them. */
5048 if (itemcnt == 2 && spell_info_item(items[0]))
5049 while (*p >= ' ' || *p == TAB) /* skip until CR/NL */
5050 ++p;
5051 else
5052 while (*p > ' ') /* skip until white space or CR/NL */
5053 ++p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005054 if (*p == NUL)
5055 break;
5056 *p++ = NUL;
5057 }
5058
5059 /* Handle non-empty lines. */
5060 if (itemcnt > 0)
5061 {
5062 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
5063 && aff->af_enc == NULL)
5064 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005065#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005066 /* Setup for conversion from "ENC" to 'encoding'. */
5067 aff->af_enc = enc_canonize(items[1]);
5068 if (aff->af_enc != NULL && !spin->si_ascii
5069 && convert_setup(&spin->si_conv, aff->af_enc,
5070 p_enc) == FAIL)
5071 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
5072 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005073 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005074#else
5075 smsg((char_u *)_("Conversion in %s not supported"), fname);
5076#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005077 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005078 else if (STRCMP(items[0], "FLAG") == 0 && itemcnt == 2
5079 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005080 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005081 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005082 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005083 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005084 aff->af_flagtype = AFT_NUM;
5085 else if (STRCMP(items[1], "caplong") == 0)
5086 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005087 else
5088 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
5089 fname, lnum, items[1]);
Bram Moolenaar371baa92005-12-29 22:43:53 +00005090 if (aff->af_rare != 0
5091 || aff->af_keepcase != 0
5092 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005093 || aff->af_needaffix != 0
5094 || aff->af_needcomp != 0
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005095 || aff->af_nosuggest != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005096 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00005097 || aff->af_suff.ht_used > 0
5098 || aff->af_pref.ht_used > 0)
5099 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
5100 fname, lnum, items[1]);
5101 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005102 else if (spell_info_item(items[0]))
5103 {
5104 p = (char_u *)getroom(spin,
5105 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
5106 + STRLEN(items[0])
5107 + STRLEN(items[1]) + 3, FALSE);
5108 if (p != NULL)
5109 {
5110 if (spin->si_info != NULL)
5111 {
5112 STRCPY(p, spin->si_info);
5113 STRCAT(p, "\n");
5114 }
5115 STRCAT(p, items[0]);
5116 STRCAT(p, " ");
5117 STRCAT(p, items[1]);
5118 spin->si_info = p;
5119 }
5120 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005121 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2
5122 && midword == NULL)
5123 {
5124 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005125 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005126 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005127 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005128 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005129 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005130 /* TODO: remove "RAR" later */
5131 else if ((STRCMP(items[0], "RAR") == 0
5132 || STRCMP(items[0], "RARE") == 0) && itemcnt == 2
5133 && aff->af_rare == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005134 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005135 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005136 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005137 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005138 /* TODO: remove "KEP" later */
5139 else if ((STRCMP(items[0], "KEP") == 0
5140 || STRCMP(items[0], "KEEPCASE") == 0) && itemcnt == 2
5141 && aff->af_keepcase == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005142 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005143 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005144 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005145 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00005146 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
5147 && aff->af_bad == 0)
5148 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005149 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
5150 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005151 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005152 else if (STRCMP(items[0], "NEEDAFFIX") == 0 && itemcnt == 2
5153 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005154 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005155 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
5156 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005157 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005158 else if (STRCMP(items[0], "NOSUGGEST") == 0 && itemcnt == 2
5159 && aff->af_nosuggest == 0)
5160 {
5161 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
5162 fname, lnum);
5163 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005164 else if (STRCMP(items[0], "NEEDCOMPOUND") == 0 && itemcnt == 2
5165 && aff->af_needcomp == 0)
5166 {
5167 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
5168 fname, lnum);
5169 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005170 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005171 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005172 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005173 /* Turn flag "c" into COMPOUNDRULE compatible string "c+",
Bram Moolenaar6de68532005-08-24 22:08:48 +00005174 * "Na" into "Na+", "1234" into "1234+". */
5175 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005176 if (p != NULL)
5177 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005178 STRCPY(p, items[1]);
5179 STRCAT(p, "+");
5180 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005181 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005182 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005183 else if (STRCMP(items[0], "COMPOUNDRULE") == 0 && itemcnt == 2)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005184 {
5185 /* Concatenate this string to previously defined ones, using a
5186 * slash to separate them. */
5187 l = STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005188 if (compflags != NULL)
5189 l += STRLEN(compflags) + 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005190 p = getroom(spin, l, FALSE);
5191 if (p != NULL)
5192 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005193 if (compflags != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005194 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005195 STRCPY(p, compflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005196 STRCAT(p, "/");
5197 }
5198 STRCAT(p, items[1]);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005199 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005200 }
5201 }
5202 else if (STRCMP(items[0], "COMPOUNDMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005203 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005204 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005205 compmax = atoi((char *)items[1]);
5206 if (compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005207 smsg((char_u *)_("Wrong COMPOUNDMAX value in %s line %d: %s"),
5208 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005209 }
5210 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005211 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005212 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005213 compminlen = atoi((char *)items[1]);
5214 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005215 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
5216 fname, lnum, items[1]);
5217 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005218 else if (STRCMP(items[0], "COMPOUNDSYLMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005219 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005220 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005221 compsylmax = atoi((char *)items[1]);
5222 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005223 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
5224 fname, lnum, items[1]);
5225 }
5226 else if (STRCMP(items[0], "SYLLABLE") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005227 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005228 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005229 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005230 }
Bram Moolenaar78622822005-08-23 21:00:13 +00005231 else if (STRCMP(items[0], "NOBREAK") == 0 && itemcnt == 1)
5232 {
5233 spin->si_nobreak = TRUE;
5234 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005235 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
5236 {
5237 spin->si_nosplitsugs = TRUE;
5238 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005239 else if (STRCMP(items[0], "NOSUGFILE") == 0 && itemcnt == 1)
5240 {
5241 spin->si_nosugfile = TRUE;
5242 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005243 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
5244 {
5245 aff->af_pfxpostpone = TRUE;
5246 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005247 else if ((STRCMP(items[0], "PFX") == 0
5248 || STRCMP(items[0], "SFX") == 0)
5249 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005250 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005251 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005252 int lasti = 4;
5253 char_u key[AH_KEY_LEN];
5254
5255 if (*items[0] == 'P')
5256 tp = &aff->af_pref;
5257 else
5258 tp = &aff->af_suff;
5259
5260 /* Myspell allows the same affix name to be used multiple
5261 * times. The affix files that do this have an undocumented
5262 * "S" flag on all but the last block, thus we check for that
5263 * and store it in ah_follows. */
5264 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
5265 hi = hash_find(tp, key);
5266 if (!HASHITEM_EMPTY(hi))
5267 {
5268 cur_aff = HI2AH(hi);
5269 if (cur_aff->ah_combine != (*items[2] == 'Y'))
5270 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
5271 fname, lnum, items[1]);
5272 if (!cur_aff->ah_follows)
5273 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
5274 fname, lnum, items[1]);
5275 }
5276 else
5277 {
5278 /* New affix letter. */
5279 cur_aff = (affheader_T *)getroom(spin,
5280 sizeof(affheader_T), TRUE);
5281 if (cur_aff == NULL)
5282 break;
5283 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
5284 fname, lnum);
5285 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
5286 break;
5287 if (cur_aff->ah_flag == aff->af_bad
Bram Moolenaar371baa92005-12-29 22:43:53 +00005288 || cur_aff->ah_flag == aff->af_rare
5289 || cur_aff->ah_flag == aff->af_keepcase
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005290 || cur_aff->ah_flag == aff->af_needaffix
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005291 || cur_aff->ah_flag == aff->af_nosuggest
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005292 || cur_aff->ah_flag == aff->af_needcomp)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005293 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 +00005294 fname, lnum, items[1]);
5295 STRCPY(cur_aff->ah_key, items[1]);
5296 hash_add(tp, cur_aff->ah_key);
5297
5298 cur_aff->ah_combine = (*items[2] == 'Y');
5299 }
5300
5301 /* Check for the "S" flag, which apparently means that another
5302 * block with the same affix name is following. */
5303 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
5304 {
5305 ++lasti;
5306 cur_aff->ah_follows = TRUE;
5307 }
5308 else
5309 cur_aff->ah_follows = FALSE;
5310
Bram Moolenaar8db73182005-06-17 21:51:16 +00005311 /* Myspell allows extra text after the item, but that might
5312 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00005313 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar8db73182005-06-17 21:51:16 +00005314 smsg((char_u *)_("Trailing text in %s line %d: %s"),
5315 fname, lnum, items[4]);
5316
Bram Moolenaar95529562005-08-25 21:21:38 +00005317 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005318 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
5319 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005320
Bram Moolenaar95529562005-08-25 21:21:38 +00005321 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005322 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005323 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005324 {
5325 /* Use a new number in the .spl file later, to be able
5326 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005327 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005328 cur_aff->ah_newID = ++spin->si_newprefID;
5329
5330 /* We only really use ah_newID if the prefix is
5331 * postponed. We know that only after handling all
5332 * the items. */
5333 did_postpone_prefix = FALSE;
5334 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005335 else
5336 /* Did use the ID in a previous block. */
5337 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005338 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005339
Bram Moolenaar51485f02005-06-04 21:55:20 +00005340 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005341 }
5342 else if ((STRCMP(items[0], "PFX") == 0
5343 || STRCMP(items[0], "SFX") == 0)
5344 && aff_todo > 0
5345 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005346 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005347 {
5348 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005349 int rare = FALSE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005350 int nocomp = FALSE;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005351 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005352 int lasti = 5;
5353
Bram Moolenaar5195e452005-08-19 20:32:47 +00005354 /* Check for "rare" and "nocomp" after the other info. */
5355 while (itemcnt > lasti)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005356 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00005357 if (!rare && STRICMP(items[lasti], "rare") == 0)
5358 {
5359 rare = TRUE;
5360 ++lasti;
5361 }
5362 else if (!nocomp && STRICMP(items[lasti], "nocomp") == 0)
5363 {
5364 nocomp = TRUE;
5365 ++lasti;
5366 }
5367 else
5368 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005369 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005370
Bram Moolenaar8db73182005-06-17 21:51:16 +00005371 /* Myspell allows extra text after the item, but that might
5372 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005373 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005374 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005375
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005376 /* New item for an affix letter. */
5377 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005378 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005379 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005380 if (aff_entry == NULL)
5381 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005382 aff_entry->ae_rare = rare;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005383 aff_entry->ae_nocomp = nocomp;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005384
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005385 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005386 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005387 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005388 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005389
Bram Moolenaar51485f02005-06-04 21:55:20 +00005390 /* Don't use an affix entry with non-ASCII characters when
5391 * "spin->si_ascii" is TRUE. */
5392 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005393 || has_non_ascii(aff_entry->ae_add)))
5394 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00005395 aff_entry->ae_next = cur_aff->ah_first;
5396 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005397
5398 if (STRCMP(items[4], ".") != 0)
5399 {
5400 char_u buf[MAXLINELEN];
5401
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005402 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005403 if (*items[0] == 'P')
5404 sprintf((char *)buf, "^%s", items[4]);
5405 else
5406 sprintf((char *)buf, "%s$", items[4]);
5407 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005408 RE_MAGIC + RE_STRING + RE_STRICT);
5409 if (aff_entry->ae_prog == NULL)
5410 smsg((char_u *)_("Broken condition in %s line %d: %s"),
5411 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005412 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005413
5414 /* For postponed prefixes we need an entry in si_prefcond
5415 * for the condition. Use an existing one if possible. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005416 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005417 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005418 /* When the chop string is one lower-case letter and
5419 * the add string ends in the upper-case letter we set
5420 * the "upper" flag, clear "ae_chop" and remove the
5421 * letters from "ae_add". The condition must either
5422 * be empty or start with the same letter. */
5423 if (aff_entry->ae_chop != NULL
5424 && aff_entry->ae_add != NULL
5425#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005426 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00005427 aff_entry->ae_chop)] == NUL
5428#else
5429 && aff_entry->ae_chop[1] == NUL
5430#endif
5431 )
5432 {
5433 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005434
Bram Moolenaar53805d12005-08-01 07:08:33 +00005435 c = PTR2CHAR(aff_entry->ae_chop);
5436 c_up = SPELL_TOUPPER(c);
5437 if (c_up != c
5438 && (aff_entry->ae_cond == NULL
5439 || PTR2CHAR(aff_entry->ae_cond) == c))
5440 {
5441 p = aff_entry->ae_add
5442 + STRLEN(aff_entry->ae_add);
5443 mb_ptr_back(aff_entry->ae_add, p);
5444 if (PTR2CHAR(p) == c_up)
5445 {
5446 upper = TRUE;
5447 aff_entry->ae_chop = NULL;
5448 *p = NUL;
5449
5450 /* The condition is matched with the
5451 * actual word, thus must check for the
5452 * upper-case letter. */
5453 if (aff_entry->ae_cond != NULL)
5454 {
5455 char_u buf[MAXLINELEN];
5456#ifdef FEAT_MBYTE
5457 if (has_mbyte)
5458 {
5459 onecap_copy(items[4], buf, TRUE);
5460 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005461 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005462 }
5463 else
5464#endif
5465 *aff_entry->ae_cond = c_up;
5466 if (aff_entry->ae_cond != NULL)
5467 {
5468 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005469 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005470 vim_free(aff_entry->ae_prog);
5471 aff_entry->ae_prog = vim_regcomp(
5472 buf, RE_MAGIC + RE_STRING);
5473 }
5474 }
5475 }
5476 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005477 }
5478
Bram Moolenaar53805d12005-08-01 07:08:33 +00005479 if (aff_entry->ae_chop == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005480 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005481 int idx;
5482 char_u **pp;
5483 int n;
5484
Bram Moolenaar6de68532005-08-24 22:08:48 +00005485 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005486 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
5487 --idx)
5488 {
5489 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
5490 if (str_equal(p, aff_entry->ae_cond))
5491 break;
5492 }
5493 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5494 {
5495 /* Not found, add a new condition. */
5496 idx = spin->si_prefcond.ga_len++;
5497 pp = ((char_u **)spin->si_prefcond.ga_data)
5498 + idx;
5499 if (aff_entry->ae_cond == NULL)
5500 *pp = NULL;
5501 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005502 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00005503 aff_entry->ae_cond);
5504 }
5505
5506 /* Add the prefix to the prefix tree. */
5507 if (aff_entry->ae_add == NULL)
5508 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005509 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005510 p = aff_entry->ae_add;
5511 /* PFX_FLAGS is a negative number, so that
5512 * tree_add_word() knows this is the prefix tree. */
5513 n = PFX_FLAGS;
5514 if (rare)
5515 n |= WFP_RARE;
5516 if (!cur_aff->ah_combine)
5517 n |= WFP_NC;
5518 if (upper)
5519 n |= WFP_UP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005520 tree_add_word(spin, p, spin->si_prefroot, n,
5521 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005522 did_postpone_prefix = TRUE;
5523 }
5524
5525 /* Didn't actually use ah_newID, backup si_newprefID. */
5526 if (aff_todo == 0 && !did_postpone_prefix)
5527 {
5528 --spin->si_newprefID;
5529 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005530 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005531 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005532 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005533 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005534 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2
5535 && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005536 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005537 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005538 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005539 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2
5540 && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005541 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005542 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005543 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005544 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2
5545 && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005546 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005547 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005548 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005549 else if ((STRCMP(items[0], "REP") == 0
5550 || STRCMP(items[0], "REPSAL") == 0)
5551 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005552 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005553 /* Ignore REP/REPSAL count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005554 if (!isdigit(*items[1]))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005555 smsg((char_u *)_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005556 fname, lnum);
5557 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005558 else if ((STRCMP(items[0], "REP") == 0
5559 || STRCMP(items[0], "REPSAL") == 0)
5560 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005561 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005562 /* REP/REPSAL item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005563 /* Myspell ignores extra arguments, we require it starts with
5564 * # to detect mistakes. */
5565 if (itemcnt > 3 && items[3][0] != '#')
5566 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005567 if (items[0][3] == 'S' ? do_repsal : do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005568 {
5569 /* Replace underscore with space (can't include a space
5570 * directly). */
5571 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5572 if (*p == '_')
5573 *p = ' ';
5574 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5575 if (*p == '_')
5576 *p = ' ';
Bram Moolenaar4770d092006-01-12 23:22:24 +00005577 add_fromto(spin, items[0][3] == 'S'
5578 ? &spin->si_repsal
5579 : &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005580 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005581 }
5582 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
5583 {
5584 /* MAP item or count */
5585 if (!found_map)
5586 {
5587 /* First line contains the count. */
5588 found_map = TRUE;
5589 if (!isdigit(*items[1]))
5590 smsg((char_u *)_("Expected MAP count in %s line %d"),
5591 fname, lnum);
5592 }
5593 else if (do_map)
5594 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005595 int c;
5596
5597 /* Check that every character appears only once. */
5598 for (p = items[1]; *p != NUL; )
5599 {
5600#ifdef FEAT_MBYTE
5601 c = mb_ptr2char_adv(&p);
5602#else
5603 c = *p++;
5604#endif
5605 if ((spin->si_map.ga_len > 0
5606 && vim_strchr(spin->si_map.ga_data, c)
5607 != NULL)
5608 || vim_strchr(p, c) != NULL)
5609 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5610 fname, lnum);
5611 }
5612
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005613 /* We simply concatenate all the MAP strings, separated by
5614 * slashes. */
5615 ga_concat(&spin->si_map, items[1]);
5616 ga_append(&spin->si_map, '/');
5617 }
5618 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005619 /* Accept "SAL from to" and "SAL from to # comment". */
5620 else if (STRCMP(items[0], "SAL") == 0
5621 && (itemcnt == 3 || (itemcnt > 3 && items[3][0] == '#')))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005622 {
5623 if (do_sal)
5624 {
5625 /* SAL item (sounds-a-like)
5626 * Either one of the known keys or a from-to pair. */
5627 if (STRCMP(items[1], "followup") == 0)
5628 spin->si_followup = sal_to_bool(items[2]);
5629 else if (STRCMP(items[1], "collapse_result") == 0)
5630 spin->si_collapse = sal_to_bool(items[2]);
5631 else if (STRCMP(items[1], "remove_accents") == 0)
5632 spin->si_rem_accents = sal_to_bool(items[2]);
5633 else
5634 /* when "to" is "_" it means empty */
5635 add_fromto(spin, &spin->si_sal, items[1],
5636 STRCMP(items[2], "_") == 0 ? (char_u *)""
5637 : items[2]);
5638 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005639 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005640 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005641 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005642 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005643 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005644 }
5645 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005646 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005647 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005648 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005649 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005650 else if (STRCMP(items[0], "COMMON") == 0)
5651 {
5652 int i;
5653
5654 for (i = 1; i < itemcnt; ++i)
5655 {
5656 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
5657 items[i])))
5658 {
5659 p = vim_strsave(items[i]);
5660 if (p == NULL)
5661 break;
5662 hash_add(&spin->si_commonwords, p);
5663 }
5664 }
5665 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005666 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005667 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005668 fname, lnum, items[0]);
5669 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005670 }
5671
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005672 if (fol != NULL || low != NULL || upp != NULL)
5673 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005674 if (spin->si_clear_chartab)
5675 {
5676 /* Clear the char type tables, don't want to use any of the
5677 * currently used spell properties. */
5678 init_spell_chartab();
5679 spin->si_clear_chartab = FALSE;
5680 }
5681
Bram Moolenaar3982c542005-06-08 21:56:31 +00005682 /*
5683 * Don't write a word table for an ASCII file, so that we don't check
5684 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005685 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00005686 * mb_get_class(), the list of chars in the file will be incomplete.
5687 */
5688 if (!spin->si_ascii
5689#ifdef FEAT_MBYTE
5690 && !enc_utf8
5691#endif
5692 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005693 {
5694 if (fol == NULL || low == NULL || upp == NULL)
5695 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
5696 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00005697 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005698 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005699
5700 vim_free(fol);
5701 vim_free(low);
5702 vim_free(upp);
5703 }
5704
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005705 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005706 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005707 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005708 aff_check_number(spin->si_compmax, compmax, "COMPOUNDMAX");
5709 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005710 }
5711
Bram Moolenaar6de68532005-08-24 22:08:48 +00005712 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005713 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005714 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
5715 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005716 }
5717
Bram Moolenaar6de68532005-08-24 22:08:48 +00005718 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005719 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005720 if (syllable == NULL)
5721 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
5722 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
5723 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005724 }
5725
Bram Moolenaar6de68532005-08-24 22:08:48 +00005726 if (compflags != NULL)
5727 process_compflags(spin, aff, compflags);
5728
5729 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005730 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005731 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005732 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005733 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005734 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005735 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005736 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005737 MSG(_("Too many posponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005738 }
5739
Bram Moolenaar6de68532005-08-24 22:08:48 +00005740 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005741 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005742 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
5743 spin->si_syllable = syllable;
5744 }
5745
5746 if (sofofrom != NULL || sofoto != NULL)
5747 {
5748 if (sofofrom == NULL || sofoto == NULL)
5749 smsg((char_u *)_("Missing SOFO%s line in %s"),
5750 sofofrom == NULL ? "FROM" : "TO", fname);
5751 else if (spin->si_sal.ga_len > 0)
5752 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005753 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005754 {
5755 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
5756 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
5757 spin->si_sofofr = sofofrom;
5758 spin->si_sofoto = sofoto;
5759 }
5760 }
5761
5762 if (midword != NULL)
5763 {
5764 aff_check_string(spin->si_midword, midword, "MIDWORD");
5765 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005766 }
5767
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005768 vim_free(pc);
5769 fclose(fd);
5770 return aff;
5771}
5772
5773/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005774 * Return TRUE if "s" is the name of an info item in the affix file.
5775 */
5776 static int
5777spell_info_item(s)
5778 char_u *s;
5779{
5780 return STRCMP(s, "NAME") == 0
5781 || STRCMP(s, "HOME") == 0
5782 || STRCMP(s, "VERSION") == 0
5783 || STRCMP(s, "AUTHOR") == 0
5784 || STRCMP(s, "EMAIL") == 0
5785 || STRCMP(s, "COPYRIGHT") == 0;
5786}
5787
5788/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005789 * Turn an affix flag name into a number, according to the FLAG type.
5790 * returns zero for failure.
5791 */
5792 static unsigned
5793affitem2flag(flagtype, item, fname, lnum)
5794 int flagtype;
5795 char_u *item;
5796 char_u *fname;
5797 int lnum;
5798{
5799 unsigned res;
5800 char_u *p = item;
5801
5802 res = get_affitem(flagtype, &p);
5803 if (res == 0)
5804 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005805 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005806 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
5807 fname, lnum, item);
5808 else
5809 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
5810 fname, lnum, item);
5811 }
5812 if (*p != NUL)
5813 {
5814 smsg((char_u *)_(e_affname), fname, lnum, item);
5815 return 0;
5816 }
5817
5818 return res;
5819}
5820
5821/*
5822 * Get one affix name from "*pp" and advance the pointer.
5823 * Returns zero for an error, still advances the pointer then.
5824 */
5825 static unsigned
5826get_affitem(flagtype, pp)
5827 int flagtype;
5828 char_u **pp;
5829{
5830 int res;
5831
Bram Moolenaar95529562005-08-25 21:21:38 +00005832 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005833 {
5834 if (!VIM_ISDIGIT(**pp))
5835 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005836 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005837 return 0;
5838 }
5839 res = getdigits(pp);
5840 }
5841 else
5842 {
5843#ifdef FEAT_MBYTE
5844 res = mb_ptr2char_adv(pp);
5845#else
5846 res = *(*pp)++;
5847#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00005848 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00005849 && res >= 'A' && res <= 'Z'))
5850 {
5851 if (**pp == NUL)
5852 return 0;
5853#ifdef FEAT_MBYTE
5854 res = mb_ptr2char_adv(pp) + (res << 16);
5855#else
5856 res = *(*pp)++ + (res << 16);
5857#endif
5858 }
5859 }
5860 return res;
5861}
5862
5863/*
5864 * Process the "compflags" string used in an affix file and append it to
5865 * spin->si_compflags.
5866 * The processing involves changing the affix names to ID numbers, so that
5867 * they fit in one byte.
5868 */
5869 static void
5870process_compflags(spin, aff, compflags)
5871 spellinfo_T *spin;
5872 afffile_T *aff;
5873 char_u *compflags;
5874{
5875 char_u *p;
5876 char_u *prevp;
5877 unsigned flag;
5878 compitem_T *ci;
5879 int id;
5880 int len;
5881 char_u *tp;
5882 char_u key[AH_KEY_LEN];
5883 hashitem_T *hi;
5884
5885 /* Make room for the old and the new compflags, concatenated with a / in
5886 * between. Processing it makes it shorter, but we don't know by how
5887 * much, thus allocate the maximum. */
5888 len = STRLEN(compflags) + 1;
5889 if (spin->si_compflags != NULL)
5890 len += STRLEN(spin->si_compflags) + 1;
5891 p = getroom(spin, len, FALSE);
5892 if (p == NULL)
5893 return;
5894 if (spin->si_compflags != NULL)
5895 {
5896 STRCPY(p, spin->si_compflags);
5897 STRCAT(p, "/");
5898 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005899 spin->si_compflags = p;
5900 tp = p + STRLEN(p);
5901
5902 for (p = compflags; *p != NUL; )
5903 {
5904 if (vim_strchr((char_u *)"/*+[]", *p) != NULL)
5905 /* Copy non-flag characters directly. */
5906 *tp++ = *p++;
5907 else
5908 {
5909 /* First get the flag number, also checks validity. */
5910 prevp = p;
5911 flag = get_affitem(aff->af_flagtype, &p);
5912 if (flag != 0)
5913 {
5914 /* Find the flag in the hashtable. If it was used before, use
5915 * the existing ID. Otherwise add a new entry. */
5916 vim_strncpy(key, prevp, p - prevp);
5917 hi = hash_find(&aff->af_comp, key);
5918 if (!HASHITEM_EMPTY(hi))
5919 id = HI2CI(hi)->ci_newID;
5920 else
5921 {
5922 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
5923 if (ci == NULL)
5924 break;
5925 STRCPY(ci->ci_key, key);
5926 ci->ci_flag = flag;
5927 /* Avoid using a flag ID that has a special meaning in a
5928 * regexp (also inside []). */
5929 do
5930 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005931 check_renumber(spin);
5932 id = spin->si_newcompID--;
5933 } while (vim_strchr((char_u *)"/+*[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005934 ci->ci_newID = id;
5935 hash_add(&aff->af_comp, ci->ci_key);
5936 }
5937 *tp++ = id;
5938 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005939 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00005940 ++p;
5941 }
5942 }
5943
5944 *tp = NUL;
5945}
5946
5947/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005948 * Check that the new IDs for postponed affixes and compounding don't overrun
5949 * each other. We have almost 255 available, but start at 0-127 to avoid
5950 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
5951 * When that is used up an error message is given.
5952 */
5953 static void
5954check_renumber(spin)
5955 spellinfo_T *spin;
5956{
5957 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
5958 {
5959 spin->si_newprefID = 127;
5960 spin->si_newcompID = 255;
5961 }
5962}
5963
5964/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005965 * Return TRUE if flag "flag" appears in affix list "afflist".
5966 */
5967 static int
5968flag_in_afflist(flagtype, afflist, flag)
5969 int flagtype;
5970 char_u *afflist;
5971 unsigned flag;
5972{
5973 char_u *p;
5974 unsigned n;
5975
5976 switch (flagtype)
5977 {
5978 case AFT_CHAR:
5979 return vim_strchr(afflist, flag) != NULL;
5980
Bram Moolenaar95529562005-08-25 21:21:38 +00005981 case AFT_CAPLONG:
5982 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00005983 for (p = afflist; *p != NUL; )
5984 {
5985#ifdef FEAT_MBYTE
5986 n = mb_ptr2char_adv(&p);
5987#else
5988 n = *p++;
5989#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00005990 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00005991 && *p != NUL)
5992#ifdef FEAT_MBYTE
5993 n = mb_ptr2char_adv(&p) + (n << 16);
5994#else
5995 n = *p++ + (n << 16);
5996#endif
5997 if (n == flag)
5998 return TRUE;
5999 }
6000 break;
6001
Bram Moolenaar95529562005-08-25 21:21:38 +00006002 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006003 for (p = afflist; *p != NUL; )
6004 {
6005 n = getdigits(&p);
6006 if (n == flag)
6007 return TRUE;
6008 if (*p != NUL) /* skip over comma */
6009 ++p;
6010 }
6011 break;
6012 }
6013 return FALSE;
6014}
6015
6016/*
6017 * Give a warning when "spinval" and "affval" numbers are set and not the same.
6018 */
6019 static void
6020aff_check_number(spinval, affval, name)
6021 int spinval;
6022 int affval;
6023 char *name;
6024{
6025 if (spinval != 0 && spinval != affval)
6026 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6027}
6028
6029/*
6030 * Give a warning when "spinval" and "affval" strings are set and not the same.
6031 */
6032 static void
6033aff_check_string(spinval, affval, name)
6034 char_u *spinval;
6035 char_u *affval;
6036 char *name;
6037{
6038 if (spinval != NULL && STRCMP(spinval, affval) != 0)
6039 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6040}
6041
6042/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006043 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
6044 * NULL as equal.
6045 */
6046 static int
6047str_equal(s1, s2)
6048 char_u *s1;
6049 char_u *s2;
6050{
6051 if (s1 == NULL || s2 == NULL)
6052 return s1 == s2;
6053 return STRCMP(s1, s2) == 0;
6054}
6055
6056/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006057 * Add a from-to item to "gap". Used for REP and SAL items.
6058 * They are stored case-folded.
6059 */
6060 static void
6061add_fromto(spin, gap, from, to)
6062 spellinfo_T *spin;
6063 garray_T *gap;
6064 char_u *from;
6065 char_u *to;
6066{
6067 fromto_T *ftp;
6068 char_u word[MAXWLEN];
6069
6070 if (ga_grow(gap, 1) == OK)
6071 {
6072 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
6073 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006074 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006075 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006076 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006077 ++gap->ga_len;
6078 }
6079}
6080
6081/*
6082 * Convert a boolean argument in a SAL line to TRUE or FALSE;
6083 */
6084 static int
6085sal_to_bool(s)
6086 char_u *s;
6087{
6088 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
6089}
6090
6091/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00006092 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6093 * When "s" is NULL FALSE is returned.
6094 */
6095 static int
6096has_non_ascii(s)
6097 char_u *s;
6098{
6099 char_u *p;
6100
6101 if (s != NULL)
6102 for (p = s; *p != NUL; ++p)
6103 if (*p >= 128)
6104 return TRUE;
6105 return FALSE;
6106}
6107
6108/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006109 * Free the structure filled by spell_read_aff().
6110 */
6111 static void
6112spell_free_aff(aff)
6113 afffile_T *aff;
6114{
6115 hashtab_T *ht;
6116 hashitem_T *hi;
6117 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006118 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006119 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006120
6121 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006122
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006123 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006124 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
6125 {
6126 todo = ht->ht_used;
6127 for (hi = ht->ht_array; todo > 0; ++hi)
6128 {
6129 if (!HASHITEM_EMPTY(hi))
6130 {
6131 --todo;
6132 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006133 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
6134 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006135 }
6136 }
6137 if (ht == &aff->af_suff)
6138 break;
6139 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006140
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006141 hash_clear(&aff->af_pref);
6142 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006143 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006144}
6145
6146/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006147 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006148 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006149 */
6150 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006151spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006152 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006153 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006154 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006155{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006156 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006157 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006158 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006159 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006160 char_u store_afflist[MAXWLEN];
6161 int pfxlen;
6162 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006163 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006164 char_u *pc;
6165 char_u *w;
6166 int l;
6167 hash_T hash;
6168 hashitem_T *hi;
6169 FILE *fd;
6170 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006171 int non_ascii = 0;
6172 int retval = OK;
6173 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006174 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006175 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006176
Bram Moolenaar51485f02005-06-04 21:55:20 +00006177 /*
6178 * Open the file.
6179 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006180 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006181 if (fd == NULL)
6182 {
6183 EMSG2(_(e_notopen), fname);
6184 return FAIL;
6185 }
6186
Bram Moolenaar51485f02005-06-04 21:55:20 +00006187 /* The hashtable is only used to detect duplicated words. */
6188 hash_init(&ht);
6189
Bram Moolenaar4770d092006-01-12 23:22:24 +00006190 vim_snprintf((char *)IObuff, IOSIZE,
6191 _("Reading dictionary file %s ..."), fname);
6192 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006193
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006194 /* start with a message for the first line */
6195 spin->si_msg_count = 999999;
6196
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006197 /* Read and ignore the first line: word count. */
6198 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006199 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006200 EMSG2(_("E760: No word count in %s"), fname);
6201
6202 /*
6203 * Read all the lines in the file one by one.
6204 * The words are converted to 'encoding' here, before being added to
6205 * the hashtable.
6206 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006207 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006208 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006209 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006210 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00006211 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006212 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006213
Bram Moolenaar51485f02005-06-04 21:55:20 +00006214 /* Remove CR, LF and white space from the end. White space halfway
6215 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006216 l = STRLEN(line);
6217 while (l > 0 && line[l - 1] <= ' ')
6218 --l;
6219 if (l == 0)
6220 continue; /* empty line */
6221 line[l] = NUL;
6222
Bram Moolenaar66fa2712006-01-22 23:22:22 +00006223 /* Truncate the word at the "/", set "afflist" to what follows.
6224 * Replace "\/" by "/" and "\\" by "\". */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006225 afflist = NULL;
6226 for (p = line; *p != NUL; mb_ptr_adv(p))
6227 {
Bram Moolenaar66fa2712006-01-22 23:22:22 +00006228 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
6229 mch_memmove(p, p + 1, STRLEN(p));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006230 else if (*p == '/')
6231 {
6232 *p = NUL;
6233 afflist = p + 1;
6234 break;
6235 }
6236 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006237
6238 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6239 if (spin->si_ascii && has_non_ascii(line))
6240 {
6241 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00006242 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006243 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00006244
Bram Moolenaarb765d632005-06-07 21:00:02 +00006245#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006246 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006247 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006248 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006249 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006250 if (pc == NULL)
6251 {
6252 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6253 fname, lnum, line);
6254 continue;
6255 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006256 w = pc;
6257 }
6258 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006259#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006260 {
6261 pc = NULL;
6262 w = line;
6263 }
6264
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006265 /* This takes time, print a message every 10000 words. */
6266 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006267 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006268 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006269 vim_snprintf((char *)message, sizeof(message),
6270 _("line %6d, word %6d - %s"),
6271 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
6272 msg_start();
6273 msg_puts_long_attr(message, 0);
6274 msg_clr_eos();
6275 msg_didout = FALSE;
6276 msg_col = 0;
6277 out_flush();
6278 }
6279
Bram Moolenaar51485f02005-06-04 21:55:20 +00006280 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006281 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006282 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006283 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006284 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006285 if (retval == FAIL)
6286 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006287
Bram Moolenaar51485f02005-06-04 21:55:20 +00006288 hash = hash_hash(dw);
6289 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006290 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006291 {
6292 if (p_verbose > 0)
6293 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006294 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006295 else if (duplicate == 0)
6296 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
6297 fname, lnum, dw);
6298 ++duplicate;
6299 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006300 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006301 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006302
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006303 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006304 store_afflist[0] = NUL;
6305 pfxlen = 0;
6306 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006307 if (afflist != NULL)
6308 {
6309 /* Check for affix name that stands for keep-case word and stands
6310 * for rare word (if defined). */
Bram Moolenaar371baa92005-12-29 22:43:53 +00006311 if (affile->af_keepcase != 0 && flag_in_afflist(
6312 affile->af_flagtype, afflist, affile->af_keepcase))
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006313 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar371baa92005-12-29 22:43:53 +00006314 if (affile->af_rare != 0 && flag_in_afflist(
6315 affile->af_flagtype, afflist, affile->af_rare))
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006316 flags |= WF_RARE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006317 if (affile->af_bad != 0 && flag_in_afflist(
6318 affile->af_flagtype, afflist, affile->af_bad))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006319 flags |= WF_BANNED;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006320 if (affile->af_needaffix != 0 && flag_in_afflist(
6321 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00006322 need_affix = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006323 if (affile->af_needcomp != 0 && flag_in_afflist(
6324 affile->af_flagtype, afflist, affile->af_needcomp))
6325 flags |= WF_NEEDCOMP;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00006326 if (affile->af_nosuggest != 0 && flag_in_afflist(
6327 affile->af_flagtype, afflist, affile->af_nosuggest))
6328 flags |= WF_NOSUGGEST;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006329
6330 if (affile->af_pfxpostpone)
6331 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006332 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006333
Bram Moolenaar5195e452005-08-19 20:32:47 +00006334 if (spin->si_compflags != NULL)
6335 /* Need to store the list of compound flags with the word.
6336 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006337 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006338 }
6339
Bram Moolenaar51485f02005-06-04 21:55:20 +00006340 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006341 if (store_word(spin, dw, flags, spin->si_region,
6342 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006343 retval = FAIL;
6344
6345 if (afflist != NULL)
6346 {
6347 /* Find all matching suffixes and add the resulting words.
6348 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006349 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006350 &affile->af_suff, &affile->af_pref,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006351 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006352 retval = FAIL;
6353
6354 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006355 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006356 &affile->af_pref, NULL,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006357 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006358 retval = FAIL;
6359 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006360 }
6361
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006362 if (duplicate > 0)
6363 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006364 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006365 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
6366 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006367 hash_clear(&ht);
6368
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006369 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006370 return retval;
6371}
6372
6373/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006374 * Get the list of prefix IDs from the affix list "afflist".
6375 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006376 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
6377 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006378 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006379 static int
6380get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006381 afffile_T *affile;
6382 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006383 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006384{
6385 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006386 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006387 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006388 int id;
6389 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006390 hashitem_T *hi;
6391
Bram Moolenaar6de68532005-08-24 22:08:48 +00006392 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006393 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006394 prevp = p;
6395 if (get_affitem(affile->af_flagtype, &p) != 0)
6396 {
6397 /* A flag is a postponed prefix flag if it appears in "af_pref"
6398 * and it's ID is not zero. */
6399 vim_strncpy(key, prevp, p - prevp);
6400 hi = hash_find(&affile->af_pref, key);
6401 if (!HASHITEM_EMPTY(hi))
6402 {
6403 id = HI2AH(hi)->ah_newID;
6404 if (id != 0)
6405 store_afflist[cnt++] = id;
6406 }
6407 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006408 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006409 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006410 }
6411
Bram Moolenaar5195e452005-08-19 20:32:47 +00006412 store_afflist[cnt] = NUL;
6413 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006414}
6415
6416/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006417 * Get the list of compound IDs from the affix list "afflist" that are used
6418 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006419 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006420 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006421 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00006422get_compflags(affile, afflist, store_afflist)
6423 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006424 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006425 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006426{
6427 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006428 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006429 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006430 char_u key[AH_KEY_LEN];
6431 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006432
Bram Moolenaar6de68532005-08-24 22:08:48 +00006433 for (p = afflist; *p != NUL; )
6434 {
6435 prevp = p;
6436 if (get_affitem(affile->af_flagtype, &p) != 0)
6437 {
6438 /* A flag is a compound flag if it appears in "af_comp". */
6439 vim_strncpy(key, prevp, p - prevp);
6440 hi = hash_find(&affile->af_comp, key);
6441 if (!HASHITEM_EMPTY(hi))
6442 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
6443 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006444 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006445 ++p;
6446 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006447
Bram Moolenaar5195e452005-08-19 20:32:47 +00006448 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006449}
6450
6451/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006452 * Apply affixes to a word and store the resulting words.
6453 * "ht" is the hashtable with affentry_T that need to be applied, either
6454 * prefixes or suffixes.
6455 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
6456 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006457 *
6458 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006459 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006460 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00006461store_aff_word(spin, word, afflist, affile, ht, xht, comb, flags,
6462 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006463 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006464 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006465 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006466 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006467 hashtab_T *ht;
6468 hashtab_T *xht;
6469 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006470 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006471 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006472 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
6473 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006474{
6475 int todo;
6476 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006477 affheader_T *ah;
6478 affentry_T *ae;
6479 regmatch_T regmatch;
6480 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006481 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006482 int i;
6483 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006484 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006485 char_u *use_pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006486 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006487 size_t wordlen = STRLEN(word);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006488
Bram Moolenaar51485f02005-06-04 21:55:20 +00006489 todo = ht->ht_used;
6490 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006491 {
6492 if (!HASHITEM_EMPTY(hi))
6493 {
6494 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006495 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00006496
Bram Moolenaar51485f02005-06-04 21:55:20 +00006497 /* Check that the affix combines, if required, and that the word
6498 * supports this affix. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006499 if ((!comb || ah->ah_combine) && flag_in_afflist(
6500 affile->af_flagtype, afflist, ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00006501 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006502 /* Loop over all affix entries with this name. */
6503 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006504 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006505 /* Check the condition. It's not logical to match case
6506 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006507 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006508 * Another requirement from Myspell is that the chop
6509 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006510 * For prefixes, when "PFXPOSTPONE" was used, only do
6511 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006512 regmatch.regprog = ae->ae_prog;
6513 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006514 if ((xht != NULL || !affile->af_pfxpostpone
6515 || ae->ae_chop != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006516 && (ae->ae_chop == NULL
6517 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006518 && (ae->ae_prog == NULL
6519 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006520 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006521 /* Match. Remove the chop and add the affix. */
6522 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006523 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006524 /* prefix: chop/add at the start of the word */
6525 if (ae->ae_add == NULL)
6526 *newword = NUL;
6527 else
6528 STRCPY(newword, ae->ae_add);
6529 p = word;
6530 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006531 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006532 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006533#ifdef FEAT_MBYTE
6534 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006535 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006536 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006537 for ( ; i > 0; --i)
6538 mb_ptr_adv(p);
6539 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006540 else
6541#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006542 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006543 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006544 STRCAT(newword, p);
6545 }
6546 else
6547 {
6548 /* suffix: chop/add at the end of the word */
6549 STRCPY(newword, word);
6550 if (ae->ae_chop != NULL)
6551 {
6552 /* Remove chop string. */
6553 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006554 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006555 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006556 mb_ptr_back(newword, p);
6557 *p = NUL;
6558 }
6559 if (ae->ae_add != NULL)
6560 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006561 }
6562
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006563 /* Obey the "rare" flag of the affix. */
6564 if (ae->ae_rare)
6565 use_flags = flags | WF_RARE;
6566 else
6567 use_flags = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006568
6569 /* Obey the "nocomp" flag of the affix: don't use the
6570 * compound flags. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006571 use_pfxlist = pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006572 if (ae->ae_nocomp && pfxlist != NULL)
6573 {
6574 vim_strncpy(pfx_pfxlist, pfxlist, pfxlen);
6575 use_pfxlist = pfx_pfxlist;
6576 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006577
6578 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00006579 if (spin->si_prefroot != NULL
6580 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006581 {
6582 /* ... add a flag to indicate an affix was used. */
6583 use_flags |= WF_HAS_AFF;
6584
6585 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00006586 * affixes is not allowed. But do use the
6587 * compound flags after them. */
6588 if ((!ah->ah_combine || comb) && pfxlist != NULL)
6589 use_pfxlist += pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006590 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006591
Bram Moolenaar51485f02005-06-04 21:55:20 +00006592 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006593 if (store_word(spin, newword, use_flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006594 spin->si_region, use_pfxlist, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006595 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006596
Bram Moolenaar51485f02005-06-04 21:55:20 +00006597 /* When added a suffix and combining is allowed also
6598 * try adding prefixes additionally. */
6599 if (xht != NULL && ah->ah_combine)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006600 if (store_aff_word(spin, newword, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006601 xht, NULL, TRUE,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006602 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006603 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006604 }
6605 }
6606 }
6607 }
6608 }
6609
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006610 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006611}
6612
6613/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006614 * Read a file with a list of words.
6615 */
6616 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006617spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006618 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006619 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006620{
6621 FILE *fd;
6622 long lnum = 0;
6623 char_u rline[MAXLINELEN];
6624 char_u *line;
6625 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006626 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006627 int l;
6628 int retval = OK;
6629 int did_word = FALSE;
6630 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006631 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00006632 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006633
6634 /*
6635 * Open the file.
6636 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006637 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00006638 if (fd == NULL)
6639 {
6640 EMSG2(_(e_notopen), fname);
6641 return FAIL;
6642 }
6643
Bram Moolenaar4770d092006-01-12 23:22:24 +00006644 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname);
6645 spell_message(spin, IObuff);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006646
6647 /*
6648 * Read all the lines in the file one by one.
6649 */
6650 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
6651 {
6652 line_breakcheck();
6653 ++lnum;
6654
6655 /* Skip comment lines. */
6656 if (*rline == '#')
6657 continue;
6658
6659 /* Remove CR, LF and white space from the end. */
6660 l = STRLEN(rline);
6661 while (l > 0 && rline[l - 1] <= ' ')
6662 --l;
6663 if (l == 0)
6664 continue; /* empty or blank line */
6665 rline[l] = NUL;
6666
6667 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
6668 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006669#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00006670 if (spin->si_conv.vc_type != CONV_NONE)
6671 {
6672 pc = string_convert(&spin->si_conv, rline, NULL);
6673 if (pc == NULL)
6674 {
6675 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6676 fname, lnum, rline);
6677 continue;
6678 }
6679 line = pc;
6680 }
6681 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006682#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006683 {
6684 pc = NULL;
6685 line = rline;
6686 }
6687
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006688 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00006689 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006690 ++line;
6691 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006692 {
6693 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006694 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
6695 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006696 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006697 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
6698 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006699 else
6700 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006701#ifdef FEAT_MBYTE
6702 char_u *enc;
6703
Bram Moolenaar51485f02005-06-04 21:55:20 +00006704 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006705 line += 10;
6706 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006707 if (enc != NULL && !spin->si_ascii
6708 && convert_setup(&spin->si_conv, enc,
6709 p_enc) == FAIL)
6710 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00006711 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006712 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006713 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006714#else
6715 smsg((char_u *)_("Conversion in %s not supported"), fname);
6716#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006717 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006718 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006719 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006720
Bram Moolenaar3982c542005-06-08 21:56:31 +00006721 if (STRNCMP(line, "regions=", 8) == 0)
6722 {
6723 if (spin->si_region_count > 1)
6724 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
6725 fname, lnum, line);
6726 else
6727 {
6728 line += 8;
6729 if (STRLEN(line) > 16)
6730 smsg((char_u *)_("Too many regions in %s line %d: %s"),
6731 fname, lnum, line);
6732 else
6733 {
6734 spin->si_region_count = STRLEN(line) / 2;
6735 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006736
6737 /* Adjust the mask for a word valid in all regions. */
6738 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00006739 }
6740 }
6741 continue;
6742 }
6743
Bram Moolenaar7887d882005-07-01 22:33:52 +00006744 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
6745 fname, lnum, line - 1);
6746 continue;
6747 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006748
Bram Moolenaar7887d882005-07-01 22:33:52 +00006749 flags = 0;
6750 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006751
Bram Moolenaar7887d882005-07-01 22:33:52 +00006752 /* Check for flags and region after a slash. */
6753 p = vim_strchr(line, '/');
6754 if (p != NULL)
6755 {
6756 *p++ = NUL;
6757 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006758 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006759 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006760 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006761 else if (*p == '!') /* Bad, bad, wicked word. */
6762 flags |= WF_BANNED;
6763 else if (*p == '?') /* Rare word. */
6764 flags |= WF_RARE;
6765 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006766 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006767 if ((flags & WF_REGION) == 0) /* first one */
6768 regionmask = 0;
6769 flags |= WF_REGION;
6770
6771 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00006772 if (l > spin->si_region_count)
6773 {
6774 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00006775 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00006776 break;
6777 }
6778 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00006779 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00006780 else
6781 {
6782 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
6783 fname, lnum, p);
6784 break;
6785 }
6786 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006787 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006788 }
6789
6790 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6791 if (spin->si_ascii && has_non_ascii(line))
6792 {
6793 ++non_ascii;
6794 continue;
6795 }
6796
6797 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006798 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006799 {
6800 retval = FAIL;
6801 break;
6802 }
6803 did_word = TRUE;
6804 }
6805
6806 vim_free(pc);
6807 fclose(fd);
6808
Bram Moolenaar4770d092006-01-12 23:22:24 +00006809 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006810 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006811 vim_snprintf((char *)IObuff, IOSIZE,
6812 _("Ignored %d words with non-ASCII characters"), non_ascii);
6813 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006814 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006815
Bram Moolenaar51485f02005-06-04 21:55:20 +00006816 return retval;
6817}
6818
6819/*
6820 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006821 * This avoids calling free() for every little struct we use (and keeping
6822 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00006823 * The memory is cleared to all zeros.
6824 * Returns NULL when out of memory.
6825 */
6826 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006827getroom(spin, len, align)
6828 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006829 size_t len; /* length needed */
6830 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006831{
6832 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006833 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006834
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006835 if (align && bl != NULL)
6836 /* Round size up for alignment. On some systems structures need to be
6837 * aligned to the size of a pointer (e.g., SPARC). */
6838 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
6839 & ~(sizeof(char *) - 1);
6840
Bram Moolenaar51485f02005-06-04 21:55:20 +00006841 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
6842 {
6843 /* Allocate a block of memory. This is not freed until much later. */
6844 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
6845 if (bl == NULL)
6846 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006847 bl->sb_next = spin->si_blocks;
6848 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006849 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006850 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006851 }
6852
6853 p = bl->sb_data + bl->sb_used;
6854 bl->sb_used += len;
6855
6856 return p;
6857}
6858
6859/*
6860 * Make a copy of a string into memory allocated with getroom().
6861 */
6862 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006863getroom_save(spin, s)
6864 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006865 char_u *s;
6866{
6867 char_u *sc;
6868
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006869 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006870 if (sc != NULL)
6871 STRCPY(sc, s);
6872 return sc;
6873}
6874
6875
6876/*
6877 * Free the list of allocated sblock_T.
6878 */
6879 static void
6880free_blocks(bl)
6881 sblock_T *bl;
6882{
6883 sblock_T *next;
6884
6885 while (bl != NULL)
6886 {
6887 next = bl->sb_next;
6888 vim_free(bl);
6889 bl = next;
6890 }
6891}
6892
6893/*
6894 * Allocate the root of a word tree.
6895 */
6896 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006897wordtree_alloc(spin)
6898 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006899{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006900 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006901}
6902
6903/*
6904 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006905 * Always store it in the case-folded tree. For a keep-case word this is
6906 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
6907 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006908 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006909 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
6910 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006911 */
6912 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00006913store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006914 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006915 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006916 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006917 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006918 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006919 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006920{
6921 int len = STRLEN(word);
6922 int ct = captype(word, word + len);
6923 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006924 int res = OK;
6925 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006926
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006927 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006928 for (p = pfxlist; res == OK; ++p)
6929 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006930 if (!need_affix || (p != NULL && *p != NUL))
6931 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006932 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006933 if (p == NULL || *p == NUL)
6934 break;
6935 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00006936 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006937
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006938 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00006939 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006940 for (p = pfxlist; res == OK; ++p)
6941 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006942 if (!need_affix || (p != NULL && *p != NUL))
6943 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006944 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006945 if (p == NULL || *p == NUL)
6946 break;
6947 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00006948 ++spin->si_keepwcount;
6949 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006950 return res;
6951}
6952
6953/*
6954 * Add word "word" to a word tree at "root".
Bram Moolenaar4770d092006-01-12 23:22:24 +00006955 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006956 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006957 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006958 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006959 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006960tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006961 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006962 char_u *word;
6963 wordnode_T *root;
6964 int flags;
6965 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006966 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006967{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006968 wordnode_T *node = root;
6969 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006970 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006971 wordnode_T **prev = NULL;
6972 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006973
Bram Moolenaar51485f02005-06-04 21:55:20 +00006974 /* Add each byte of the word to the tree, including the NUL at the end. */
6975 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006976 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006977 /* When there is more than one reference to this node we need to make
6978 * a copy, so that we can modify it. Copy the whole list of siblings
6979 * (we don't optimize for a partly shared list of siblings). */
6980 if (node != NULL && node->wn_refs > 1)
6981 {
6982 --node->wn_refs;
6983 copyprev = prev;
6984 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
6985 {
6986 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006987 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006988 if (np == NULL)
6989 return FAIL;
6990 np->wn_child = copyp->wn_child;
6991 if (np->wn_child != NULL)
6992 ++np->wn_child->wn_refs; /* child gets extra ref */
6993 np->wn_byte = copyp->wn_byte;
6994 if (np->wn_byte == NUL)
6995 {
6996 np->wn_flags = copyp->wn_flags;
6997 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006998 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006999 }
7000
7001 /* Link the new node in the list, there will be one ref. */
7002 np->wn_refs = 1;
7003 *copyprev = np;
7004 copyprev = &np->wn_sibling;
7005
7006 /* Let "node" point to the head of the copied list. */
7007 if (copyp == node)
7008 node = np;
7009 }
7010 }
7011
Bram Moolenaar51485f02005-06-04 21:55:20 +00007012 /* Look for the sibling that has the same character. They are sorted
7013 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007014 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007015 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007016 while (node != NULL
7017 && (node->wn_byte < word[i]
7018 || (node->wn_byte == NUL
7019 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007020 ? node->wn_affixID < (unsigned)affixID
7021 : (node->wn_flags < (unsigned)(flags & WN_MASK)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007022 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007023 && (spin->si_sugtree
7024 ? (node->wn_region & 0xffff) < region
7025 : node->wn_affixID
7026 < (unsigned)affixID)))))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007027 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007028 prev = &node->wn_sibling;
7029 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007030 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007031 if (node == NULL
7032 || node->wn_byte != word[i]
7033 || (word[i] == NUL
7034 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007035 || spin->si_sugtree
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007036 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007037 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007038 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007039 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007040 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007041 if (np == NULL)
7042 return FAIL;
7043 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007044
7045 /* If "node" is NULL this is a new child or the end of the sibling
7046 * list: ref count is one. Otherwise use ref count of sibling and
7047 * make ref count of sibling one (matters when inserting in front
7048 * of the list of siblings). */
7049 if (node == NULL)
7050 np->wn_refs = 1;
7051 else
7052 {
7053 np->wn_refs = node->wn_refs;
7054 node->wn_refs = 1;
7055 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007056 *prev = np;
7057 np->wn_sibling = node;
7058 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007059 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007060
Bram Moolenaar51485f02005-06-04 21:55:20 +00007061 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007062 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007063 node->wn_flags = flags;
7064 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007065 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007066 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00007067 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007068 prev = &node->wn_child;
7069 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007070 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007071#ifdef SPELL_PRINTTREE
7072 smsg("Added \"%s\"", word);
7073 spell_print_tree(root->wn_sibling);
7074#endif
7075
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007076 /* count nr of words added since last message */
7077 ++spin->si_msg_count;
7078
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007079 if (spin->si_compress_cnt > 1)
7080 {
7081 if (--spin->si_compress_cnt == 1)
7082 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007083 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007084 }
7085
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007086 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007087 * When we have allocated lots of memory we need to compress the word tree
7088 * to free up some room. But compression is slow, and we might actually
7089 * need that room, thus only compress in the following situations:
7090 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00007091 * "compress_start" blocks.
7092 * 2. When compressed before and used "compress_inc" blocks before
7093 * adding "compress_added" words (si_compress_cnt > 1).
7094 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007095 * (si_compress_cnt == 1) and the number of free nodes drops below the
7096 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007097 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007098#ifndef SPELL_PRINTTREE
7099 if (spin->si_compress_cnt == 1
7100 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00007101 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007102#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007103 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007104 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00007105 * when the freed up room has been used and another "compress_inc"
7106 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007107 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007108 spin->si_blocks_cnt -= compress_inc;
7109 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007110
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007111 if (spin->si_verbose)
7112 {
7113 msg_start();
7114 msg_puts((char_u *)_(msg_compressing));
7115 msg_clr_eos();
7116 msg_didout = FALSE;
7117 msg_col = 0;
7118 out_flush();
7119 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007120
7121 /* Compress both trees. Either they both have many nodes, which makes
7122 * compression useful, or one of them is small, which means
Bram Moolenaar4770d092006-01-12 23:22:24 +00007123 * compression goes fast. But when filling the souldfold word tree
7124 * there is no keep-case tree. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007125 wordtree_compress(spin, spin->si_foldroot);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007126 if (affixID >= 0)
7127 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007128 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007129
7130 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007131}
7132
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007133/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007134 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
7135 * Sets "sps_flags".
7136 */
7137 int
7138spell_check_msm()
7139{
7140 char_u *p = p_msm;
7141 long start = 0;
7142 long inc = 0;
7143 long added = 0;
7144
7145 if (!VIM_ISDIGIT(*p))
7146 return FAIL;
7147 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
7148 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
7149 if (*p != ',')
7150 return FAIL;
7151 ++p;
7152 if (!VIM_ISDIGIT(*p))
7153 return FAIL;
7154 inc = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
7155 if (*p != ',')
7156 return FAIL;
7157 ++p;
7158 if (!VIM_ISDIGIT(*p))
7159 return FAIL;
7160 added = getdigits(&p) * 1024;
7161 if (*p != NUL)
7162 return FAIL;
7163
7164 if (start == 0 || inc == 0 || added == 0 || inc > start)
7165 return FAIL;
7166
7167 compress_start = start;
7168 compress_inc = inc;
7169 compress_added = added;
7170 return OK;
7171}
7172
7173
7174/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007175 * Get a wordnode_T, either from the list of previously freed nodes or
7176 * allocate a new one.
7177 */
7178 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007179get_wordnode(spin)
7180 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007181{
7182 wordnode_T *n;
7183
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007184 if (spin->si_first_free == NULL)
7185 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007186 else
7187 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007188 n = spin->si_first_free;
7189 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007190 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007191 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007192 }
7193#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007194 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007195#endif
7196 return n;
7197}
7198
7199/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007200 * Decrement the reference count on a node (which is the head of a list of
7201 * siblings). If the reference count becomes zero free the node and its
7202 * siblings.
Bram Moolenaar4770d092006-01-12 23:22:24 +00007203 * Returns the number of nodes actually freed.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007204 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007205 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007206deref_wordnode(spin, node)
7207 spellinfo_T *spin;
7208 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007209{
Bram Moolenaar4770d092006-01-12 23:22:24 +00007210 wordnode_T *np;
7211 int cnt = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007212
7213 if (--node->wn_refs == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007214 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007215 for (np = node; np != NULL; np = np->wn_sibling)
7216 {
7217 if (np->wn_child != NULL)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007218 cnt += deref_wordnode(spin, np->wn_child);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007219 free_wordnode(spin, np);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007220 ++cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007221 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007222 ++cnt; /* length field */
7223 }
7224 return cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007225}
7226
7227/*
7228 * Free a wordnode_T for re-use later.
7229 * Only the "wn_child" field becomes invalid.
7230 */
7231 static void
7232free_wordnode(spin, n)
7233 spellinfo_T *spin;
7234 wordnode_T *n;
7235{
7236 n->wn_child = spin->si_first_free;
7237 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007238 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007239}
7240
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007241/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007242 * Compress a tree: find tails that are identical and can be shared.
7243 */
7244 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007245wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007246 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007247 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007248{
7249 hashtab_T ht;
7250 int n;
7251 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007252 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007253
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007254 /* Skip the root itself, it's not actually used. The first sibling is the
7255 * start of the tree. */
7256 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007257 {
7258 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007259 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007260
7261#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00007262 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007263#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00007264 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007265 if (tot > 1000000)
7266 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007267 else if (tot == 0)
7268 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007269 else
7270 perc = (tot - n) * 100 / tot;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007271 vim_snprintf((char *)IObuff, IOSIZE,
7272 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
7273 n, tot, tot - n, perc);
7274 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007275 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007276#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007277 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007278#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007279 hash_clear(&ht);
7280 }
7281}
7282
7283/*
7284 * Compress a node, its siblings and its children, depth first.
7285 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007286 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007287 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007288node_compress(spin, node, ht, tot)
7289 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007290 wordnode_T *node;
7291 hashtab_T *ht;
7292 int *tot; /* total count of nodes before compressing,
7293 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007294{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007295 wordnode_T *np;
7296 wordnode_T *tp;
7297 wordnode_T *child;
7298 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007299 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007300 int len = 0;
7301 unsigned nr, n;
7302 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007303
Bram Moolenaar51485f02005-06-04 21:55:20 +00007304 /*
7305 * Go through the list of siblings. Compress each child and then try
7306 * finding an identical child to replace it.
7307 * Note that with "child" we mean not just the node that is pointed to,
Bram Moolenaar4770d092006-01-12 23:22:24 +00007308 * but the whole list of siblings of which the child node is the first.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007309 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007310 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007311 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007312 ++len;
7313 if ((child = np->wn_child) != NULL)
7314 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007315 /* Compress the child first. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007316 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007317
7318 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007319 hash = hash_hash(child->wn_u1.hashkey);
7320 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007321 if (!HASHITEM_EMPTY(hi))
7322 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007323 /* There are children we encountered before with a hash value
7324 * identical to the current child. Now check if there is one
7325 * that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007326 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007327 if (node_equal(child, tp))
7328 {
7329 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007330 * current one. This means the current child and all
7331 * its siblings is unlinked from the tree. */
7332 ++tp->wn_refs;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007333 compressed += deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007334 np->wn_child = tp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007335 break;
7336 }
7337 if (tp == NULL)
7338 {
7339 /* No other child with this hash value equals the child of
7340 * the node, add it to the linked list after the first
7341 * item. */
7342 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007343 child->wn_u2.next = tp->wn_u2.next;
7344 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007345 }
7346 }
7347 else
7348 /* No other child has this hash value, add it to the
7349 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007350 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007351 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007352 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007353 *tot += len + 1; /* add one for the node that stores the length */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007354
7355 /*
7356 * Make a hash key for the node and its siblings, so that we can quickly
7357 * find a lookalike node. This must be done after compressing the sibling
7358 * list, otherwise the hash key would become invalid by the compression.
7359 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007360 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007361 nr = 0;
7362 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007363 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007364 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007365 /* end node: use wn_flags, wn_region and wn_affixID */
7366 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007367 else
7368 /* byte node: use the byte value and the child pointer */
7369 n = np->wn_byte + ((long_u)np->wn_child << 8);
7370 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007371 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007372
7373 /* Avoid NUL bytes, it terminates the hash key. */
7374 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007375 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007376 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007377 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007378 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007379 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007380 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007381 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
7382 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007383
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007384 /* Check for CTRL-C pressed now and then. */
7385 fast_breakcheck();
7386
Bram Moolenaar51485f02005-06-04 21:55:20 +00007387 return compressed;
7388}
7389
7390/*
7391 * Return TRUE when two nodes have identical siblings and children.
7392 */
7393 static int
7394node_equal(n1, n2)
7395 wordnode_T *n1;
7396 wordnode_T *n2;
7397{
7398 wordnode_T *p1;
7399 wordnode_T *p2;
7400
7401 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
7402 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
7403 if (p1->wn_byte != p2->wn_byte
7404 || (p1->wn_byte == NUL
7405 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007406 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007407 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007408 : (p1->wn_child != p2->wn_child)))
7409 break;
7410
7411 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007412}
7413
7414/*
7415 * Write a number to file "fd", MSB first, in "len" bytes.
7416 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007417 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007418put_bytes(fd, nr, len)
7419 FILE *fd;
7420 long_u nr;
7421 int len;
7422{
7423 int i;
7424
7425 for (i = len - 1; i >= 0; --i)
7426 putc((int)(nr >> (i * 8)), fd);
7427}
7428
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007429#ifdef _MSC_VER
7430# if (_MSC_VER <= 1200)
7431/* This line is required for VC6 without the service pack. Also see the
7432 * matching #pragma below. */
7433/* # pragma optimize("", off) */
7434# endif
7435#endif
7436
Bram Moolenaar4770d092006-01-12 23:22:24 +00007437/*
7438 * Write spin->si_sugtime to file "fd".
7439 */
7440 static void
7441put_sugtime(spin, fd)
7442 spellinfo_T *spin;
7443 FILE *fd;
7444{
7445 int c;
7446 int i;
7447
7448 /* time_t can be up to 8 bytes in size, more than long_u, thus we
7449 * can't use put_bytes() here. */
7450 for (i = 7; i >= 0; --i)
7451 if (i + 1 > sizeof(time_t))
7452 /* ">>" doesn't work well when shifting more bits than avail */
7453 putc(0, fd);
7454 else
7455 {
7456 c = (unsigned)spin->si_sugtime >> (i * 8);
7457 putc(c, fd);
7458 }
7459}
7460
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007461#ifdef _MSC_VER
7462# if (_MSC_VER <= 1200)
7463/* # pragma optimize("", on) */
7464# endif
7465#endif
7466
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007467static int
7468#ifdef __BORLANDC__
7469_RTLENTRYF
7470#endif
7471rep_compare __ARGS((const void *s1, const void *s2));
7472
7473/*
7474 * Function given to qsort() to sort the REP items on "from" string.
7475 */
7476 static int
7477#ifdef __BORLANDC__
7478_RTLENTRYF
7479#endif
7480rep_compare(s1, s2)
7481 const void *s1;
7482 const void *s2;
7483{
7484 fromto_T *p1 = (fromto_T *)s1;
7485 fromto_T *p2 = (fromto_T *)s2;
7486
7487 return STRCMP(p1->ft_from, p2->ft_from);
7488}
7489
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007490/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007491 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007492 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007493 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007494 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007495write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007496 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007497 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007498{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007499 FILE *fd;
7500 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007501 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007502 wordnode_T *tree;
7503 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007504 int i;
7505 int l;
7506 garray_T *gap;
7507 fromto_T *ftp;
7508 char_u *p;
7509 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007510 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007511
Bram Moolenaarb765d632005-06-07 21:00:02 +00007512 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007513 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007514 {
7515 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007516 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007517 }
7518
Bram Moolenaar5195e452005-08-19 20:32:47 +00007519 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007520 /* <fileID> */
7521 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007522 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007523 EMSG(_(e_write));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007524 retval = FAIL;
7525 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007526 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007527
Bram Moolenaar5195e452005-08-19 20:32:47 +00007528 /*
7529 * <SECTIONS>: <section> ... <sectionend>
7530 */
7531
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007532 /* SN_INFO: <infotext> */
7533 if (spin->si_info != NULL)
7534 {
7535 putc(SN_INFO, fd); /* <sectionID> */
7536 putc(0, fd); /* <sectionflags> */
7537
7538 i = STRLEN(spin->si_info);
7539 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
7540 fwrite(spin->si_info, (size_t)i, (size_t)1, fd); /* <infotext> */
7541 }
7542
Bram Moolenaar5195e452005-08-19 20:32:47 +00007543 /* SN_REGION: <regionname> ...
7544 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007545 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007546 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007547 putc(SN_REGION, fd); /* <sectionID> */
7548 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7549 l = spin->si_region_count * 2;
7550 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7551 fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
7552 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007553 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007554 }
7555 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00007556 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007557
Bram Moolenaar5195e452005-08-19 20:32:47 +00007558 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
7559 *
7560 * The table with character flags and the table for case folding.
7561 * This makes sure the same characters are recognized as word characters
7562 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007563 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007564 * 'encoding'.
7565 * Also skip this for an .add.spl file, the main spell file must contain
7566 * the table (avoids that it conflicts). File is shorter too.
7567 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007568 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007569 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007570 char_u folchars[128 * 8];
7571 int flags;
7572
Bram Moolenaard12a1322005-08-21 22:08:24 +00007573 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007574 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7575
7576 /* Form the <folchars> string first, we need to know its length. */
7577 l = 0;
7578 for (i = 128; i < 256; ++i)
7579 {
7580#ifdef FEAT_MBYTE
7581 if (has_mbyte)
7582 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
7583 else
7584#endif
7585 folchars[l++] = spelltab.st_fold[i];
7586 }
7587 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
7588
7589 fputc(128, fd); /* <charflagslen> */
7590 for (i = 128; i < 256; ++i)
7591 {
7592 flags = 0;
7593 if (spelltab.st_isw[i])
7594 flags |= CF_WORD;
7595 if (spelltab.st_isu[i])
7596 flags |= CF_UPPER;
7597 fputc(flags, fd); /* <charflags> */
7598 }
7599
7600 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
7601 fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007602 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007603
Bram Moolenaar5195e452005-08-19 20:32:47 +00007604 /* SN_MIDWORD: <midword> */
7605 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007606 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007607 putc(SN_MIDWORD, fd); /* <sectionID> */
7608 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7609
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007610 i = STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007611 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007612 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
7613 }
7614
Bram Moolenaar5195e452005-08-19 20:32:47 +00007615 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
7616 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007617 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007618 putc(SN_PREFCOND, fd); /* <sectionID> */
7619 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7620
7621 l = write_spell_prefcond(NULL, &spin->si_prefcond);
7622 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7623
7624 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007625 }
7626
Bram Moolenaar5195e452005-08-19 20:32:47 +00007627 /* SN_REP: <repcount> <rep> ...
Bram Moolenaar4770d092006-01-12 23:22:24 +00007628 * SN_SAL: <salflags> <salcount> <sal> ...
7629 * SN_REPSAL: <repcount> <rep> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007630
Bram Moolenaar5195e452005-08-19 20:32:47 +00007631 /* round 1: SN_REP section
Bram Moolenaar4770d092006-01-12 23:22:24 +00007632 * round 2: SN_SAL section (unless SN_SOFO is used)
7633 * round 3: SN_REPSAL section */
7634 for (round = 1; round <= 3; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007635 {
7636 if (round == 1)
7637 gap = &spin->si_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007638 else if (round == 2)
7639 {
7640 /* Don't write SN_SAL when using a SN_SOFO section */
7641 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
7642 continue;
7643 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007644 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007645 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00007646 gap = &spin->si_repsal;
7647
7648 /* Don't write the section if there are no items. */
7649 if (gap->ga_len == 0)
7650 continue;
7651
7652 /* Sort the REP/REPSAL items. */
7653 if (round != 2)
7654 qsort(gap->ga_data, (size_t)gap->ga_len,
7655 sizeof(fromto_T), rep_compare);
7656
7657 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
7658 putc(i, fd); /* <sectionID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007659
Bram Moolenaar5195e452005-08-19 20:32:47 +00007660 /* This is for making suggestions, section is not required. */
7661 putc(0, fd); /* <sectionflags> */
7662
7663 /* Compute the length of what follows. */
7664 l = 2; /* count <repcount> or <salcount> */
7665 for (i = 0; i < gap->ga_len; ++i)
7666 {
7667 ftp = &((fromto_T *)gap->ga_data)[i];
7668 l += 1 + STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
7669 l += 1 + STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
7670 }
7671 if (round == 2)
7672 ++l; /* count <salflags> */
7673 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7674
7675 if (round == 2)
7676 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007677 i = 0;
7678 if (spin->si_followup)
7679 i |= SAL_F0LLOWUP;
7680 if (spin->si_collapse)
7681 i |= SAL_COLLAPSE;
7682 if (spin->si_rem_accents)
7683 i |= SAL_REM_ACCENTS;
7684 putc(i, fd); /* <salflags> */
7685 }
7686
7687 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
7688 for (i = 0; i < gap->ga_len; ++i)
7689 {
7690 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
7691 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
7692 ftp = &((fromto_T *)gap->ga_data)[i];
7693 for (rr = 1; rr <= 2; ++rr)
7694 {
7695 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
7696 l = STRLEN(p);
7697 putc(l, fd);
7698 fwrite(p, l, (size_t)1, fd);
7699 }
7700 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007701
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007702 }
7703
Bram Moolenaar5195e452005-08-19 20:32:47 +00007704 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
7705 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007706 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
7707 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007708 putc(SN_SOFO, fd); /* <sectionID> */
7709 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007710
7711 l = STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007712 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
7713 /* <sectionlen> */
7714
7715 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
7716 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007717
7718 l = STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007719 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
7720 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007721 }
7722
Bram Moolenaar4770d092006-01-12 23:22:24 +00007723 /* SN_WORDS: <word> ...
7724 * This is for making suggestions, section is not required. */
7725 if (spin->si_commonwords.ht_used > 0)
7726 {
7727 putc(SN_WORDS, fd); /* <sectionID> */
7728 putc(0, fd); /* <sectionflags> */
7729
7730 /* round 1: count the bytes
7731 * round 2: write the bytes */
7732 for (round = 1; round <= 2; ++round)
7733 {
7734 int todo;
7735 int len = 0;
7736 hashitem_T *hi;
7737
7738 todo = spin->si_commonwords.ht_used;
7739 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
7740 if (!HASHITEM_EMPTY(hi))
7741 {
7742 l = STRLEN(hi->hi_key) + 1;
7743 len += l;
7744 if (round == 2) /* <word> */
7745 fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
7746 --todo;
7747 }
7748 if (round == 1)
7749 put_bytes(fd, (long_u)len, 4); /* <sectionlen> */
7750 }
7751 }
7752
Bram Moolenaar5195e452005-08-19 20:32:47 +00007753 /* SN_MAP: <mapstr>
7754 * This is for making suggestions, section is not required. */
7755 if (spin->si_map.ga_len > 0)
7756 {
7757 putc(SN_MAP, fd); /* <sectionID> */
7758 putc(0, fd); /* <sectionflags> */
7759 l = spin->si_map.ga_len;
7760 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7761 fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
7762 /* <mapstr> */
7763 }
7764
Bram Moolenaar4770d092006-01-12 23:22:24 +00007765 /* SN_SUGFILE: <timestamp>
7766 * This is used to notify that a .sug file may be available and at the
7767 * same time allows for checking that a .sug file that is found matches
7768 * with this .spl file. That's because the word numbers must be exactly
7769 * right. */
7770 if (!spin->si_nosugfile
7771 && (spin->si_sal.ga_len > 0
7772 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
7773 {
7774 putc(SN_SUGFILE, fd); /* <sectionID> */
7775 putc(0, fd); /* <sectionflags> */
7776 put_bytes(fd, (long_u)8, 4); /* <sectionlen> */
7777
7778 /* Set si_sugtime and write it to the file. */
7779 spin->si_sugtime = time(NULL);
7780 put_sugtime(spin, fd); /* <timestamp> */
7781 }
7782
Bram Moolenaare1438bb2006-03-01 22:01:55 +00007783 /* SN_NOSPLITSUGS: nothing
7784 * This is used to notify that no suggestions with word splits are to be
7785 * made. */
7786 if (spin->si_nosplitsugs)
7787 {
7788 putc(SN_NOSPLITSUGS, fd); /* <sectionID> */
7789 putc(0, fd); /* <sectionflags> */
7790 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
7791 }
7792
Bram Moolenaar5195e452005-08-19 20:32:47 +00007793 /* SN_COMPOUND: compound info.
7794 * We don't mark it required, when not supported all compound words will
7795 * be bad words. */
7796 if (spin->si_compflags != NULL)
7797 {
7798 putc(SN_COMPOUND, fd); /* <sectionID> */
7799 putc(0, fd); /* <sectionflags> */
7800
7801 l = STRLEN(spin->si_compflags);
7802 put_bytes(fd, (long_u)(l + 3), 4); /* <sectionlen> */
7803 putc(spin->si_compmax, fd); /* <compmax> */
7804 putc(spin->si_compminlen, fd); /* <compminlen> */
7805 putc(spin->si_compsylmax, fd); /* <compsylmax> */
7806 /* <compflags> */
7807 fwrite(spin->si_compflags, (size_t)l, (size_t)1, fd);
7808 }
7809
Bram Moolenaar78622822005-08-23 21:00:13 +00007810 /* SN_NOBREAK: NOBREAK flag */
7811 if (spin->si_nobreak)
7812 {
7813 putc(SN_NOBREAK, fd); /* <sectionID> */
7814 putc(0, fd); /* <sectionflags> */
7815
7816 /* It's empty, the precense of the section flags the feature. */
7817 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
7818 }
7819
Bram Moolenaar5195e452005-08-19 20:32:47 +00007820 /* SN_SYLLABLE: syllable info.
7821 * We don't mark it required, when not supported syllables will not be
7822 * counted. */
7823 if (spin->si_syllable != NULL)
7824 {
7825 putc(SN_SYLLABLE, fd); /* <sectionID> */
7826 putc(0, fd); /* <sectionflags> */
7827
7828 l = STRLEN(spin->si_syllable);
7829 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7830 fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd); /* <syllable> */
7831 }
7832
7833 /* end of <SECTIONS> */
7834 putc(SN_END, fd); /* <sectionend> */
7835
Bram Moolenaar50cde822005-06-05 21:54:54 +00007836
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007837 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007838 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007839 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007840 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007841 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007842 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007843 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007844 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007845 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007846 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007847 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007848 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007849
Bram Moolenaar0c405862005-06-22 22:26:26 +00007850 /* Clear the index and wnode fields in the tree. */
7851 clear_node(tree);
7852
Bram Moolenaar51485f02005-06-04 21:55:20 +00007853 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00007854 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00007855 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007856 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007857
Bram Moolenaar51485f02005-06-04 21:55:20 +00007858 /* number of nodes in 4 bytes */
7859 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00007860 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007861
Bram Moolenaar51485f02005-06-04 21:55:20 +00007862 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007863 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007864 }
7865
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007866 /* Write another byte to check for errors. */
7867 if (putc(0, fd) == EOF)
7868 retval = FAIL;
7869
7870 if (fclose(fd) == EOF)
7871 retval = FAIL;
7872
7873 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007874}
7875
7876/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007877 * Clear the index and wnode fields of "node", it siblings and its
7878 * children. This is needed because they are a union with other items to save
7879 * space.
7880 */
7881 static void
7882clear_node(node)
7883 wordnode_T *node;
7884{
7885 wordnode_T *np;
7886
7887 if (node != NULL)
7888 for (np = node; np != NULL; np = np->wn_sibling)
7889 {
7890 np->wn_u1.index = 0;
7891 np->wn_u2.wnode = NULL;
7892
7893 if (np->wn_byte != NUL)
7894 clear_node(np->wn_child);
7895 }
7896}
7897
7898
7899/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007900 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007901 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00007902 * This first writes the list of possible bytes (siblings). Then for each
7903 * byte recursively write the children.
7904 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00007905 * NOTE: The code here must match the code in read_tree_node(), since
7906 * assumptions are made about the indexes (so that we don't have to write them
7907 * in the file).
Bram Moolenaar51485f02005-06-04 21:55:20 +00007908 *
7909 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007910 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007911 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00007912put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007913 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007914 wordnode_T *node;
7915 int index;
7916 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007917 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007918{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007919 int newindex = index;
7920 int siblingcount = 0;
7921 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007922 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007923
Bram Moolenaar51485f02005-06-04 21:55:20 +00007924 /* If "node" is zero the tree is empty. */
7925 if (node == NULL)
7926 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007927
Bram Moolenaar51485f02005-06-04 21:55:20 +00007928 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007929 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007930
7931 /* Count the number of siblings. */
7932 for (np = node; np != NULL; np = np->wn_sibling)
7933 ++siblingcount;
7934
7935 /* Write the sibling count. */
7936 if (fd != NULL)
7937 putc(siblingcount, fd); /* <siblingcount> */
7938
7939 /* Write each sibling byte and optionally extra info. */
7940 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007941 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007942 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007943 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007944 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007945 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007946 /* For a NUL byte (end of word) write the flags etc. */
7947 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007948 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007949 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007950 * associated condition nr (stored in wn_region). The
7951 * byte value is misused to store the "rare" and "not
7952 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00007953 if (np->wn_flags == (short_u)PFX_FLAGS)
7954 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007955 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00007956 {
7957 putc(BY_FLAGS, fd); /* <byte> */
7958 putc(np->wn_flags, fd); /* <pflags> */
7959 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007960 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007961 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007962 }
7963 else
7964 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007965 /* For word trees we write the flag/region items. */
7966 flags = np->wn_flags;
7967 if (regionmask != 0 && np->wn_region != regionmask)
7968 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007969 if (np->wn_affixID != 0)
7970 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007971 if (flags == 0)
7972 {
7973 /* word without flags or region */
7974 putc(BY_NOFLAGS, fd); /* <byte> */
7975 }
7976 else
7977 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007978 if (np->wn_flags >= 0x100)
7979 {
7980 putc(BY_FLAGS2, fd); /* <byte> */
7981 putc(flags, fd); /* <flags> */
7982 putc((unsigned)flags >> 8, fd); /* <flags2> */
7983 }
7984 else
7985 {
7986 putc(BY_FLAGS, fd); /* <byte> */
7987 putc(flags, fd); /* <flags> */
7988 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007989 if (flags & WF_REGION)
7990 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007991 if (flags & WF_AFX)
7992 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007993 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007994 }
7995 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007996 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007997 else
7998 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00007999 if (np->wn_child->wn_u1.index != 0
8000 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008001 {
8002 /* The child is written elsewhere, write the reference. */
8003 if (fd != NULL)
8004 {
8005 putc(BY_INDEX, fd); /* <byte> */
8006 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008007 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008008 }
8009 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008010 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008011 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008012 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008013
Bram Moolenaar51485f02005-06-04 21:55:20 +00008014 if (fd != NULL)
8015 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
8016 {
8017 EMSG(_(e_write));
8018 return 0;
8019 }
8020 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008021 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008022
8023 /* Space used in the array when reading: one for each sibling and one for
8024 * the count. */
8025 newindex += siblingcount + 1;
8026
8027 /* Recursively dump the children of each sibling. */
8028 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008029 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
8030 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008031 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008032
8033 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008034}
8035
8036
8037/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008038 * ":mkspell [-ascii] outfile infile ..."
8039 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008040 */
8041 void
8042ex_mkspell(eap)
8043 exarg_T *eap;
8044{
8045 int fcount;
8046 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008047 char_u *arg = eap->arg;
8048 int ascii = FALSE;
8049
8050 if (STRNCMP(arg, "-ascii", 6) == 0)
8051 {
8052 ascii = TRUE;
8053 arg = skipwhite(arg + 6);
8054 }
8055
8056 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
8057 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
8058 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008059 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008060 FreeWild(fcount, fnames);
8061 }
8062}
8063
8064/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00008065 * Create the .sug file.
8066 * Uses the soundfold info in "spin".
8067 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
8068 */
8069 static void
8070spell_make_sugfile(spin, wfname)
8071 spellinfo_T *spin;
8072 char_u *wfname;
8073{
8074 char_u fname[MAXPATHL];
8075 int len;
8076 slang_T *slang;
8077 int free_slang = FALSE;
8078
8079 /*
8080 * Read back the .spl file that was written. This fills the required
8081 * info for soundfolding. This also uses less memory than the
8082 * pointer-linked version of the trie. And it avoids having two versions
8083 * of the code for the soundfolding stuff.
8084 * It might have been done already by spell_reload_one().
8085 */
8086 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
8087 if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME)
8088 break;
8089 if (slang == NULL)
8090 {
8091 spell_message(spin, (char_u *)_("Reading back spell file..."));
8092 slang = spell_load_file(wfname, NULL, NULL, FALSE);
8093 if (slang == NULL)
8094 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008095 free_slang = TRUE;
8096 }
8097
8098 /*
8099 * Clear the info in "spin" that is used.
8100 */
8101 spin->si_blocks = NULL;
8102 spin->si_blocks_cnt = 0;
8103 spin->si_compress_cnt = 0; /* will stay at 0 all the time*/
8104 spin->si_free_count = 0;
8105 spin->si_first_free = NULL;
8106 spin->si_foldwcount = 0;
8107
8108 /*
8109 * Go through the trie of good words, soundfold each word and add it to
8110 * the soundfold trie.
8111 */
8112 spell_message(spin, (char_u *)_("Performing soundfolding..."));
8113 if (sug_filltree(spin, slang) == FAIL)
8114 goto theend;
8115
8116 /*
8117 * Create the table which links each soundfold word with a list of the
8118 * good words it may come from. Creates buffer "spin->si_spellbuf".
8119 * This also removes the wordnr from the NUL byte entries to make
8120 * compression possible.
8121 */
8122 if (sug_maketable(spin) == FAIL)
8123 goto theend;
8124
8125 smsg((char_u *)_("Number of words after soundfolding: %ld"),
8126 (long)spin->si_spellbuf->b_ml.ml_line_count);
8127
8128 /*
8129 * Compress the soundfold trie.
8130 */
8131 spell_message(spin, (char_u *)_(msg_compressing));
8132 wordtree_compress(spin, spin->si_foldroot);
8133
8134 /*
8135 * Write the .sug file.
8136 * Make the file name by changing ".spl" to ".sug".
8137 */
8138 STRCPY(fname, wfname);
8139 len = STRLEN(fname);
8140 fname[len - 2] = 'u';
8141 fname[len - 1] = 'g';
8142 sug_write(spin, fname);
8143
8144theend:
8145 if (free_slang)
8146 slang_free(slang);
8147 free_blocks(spin->si_blocks);
8148 close_spellbuf(spin->si_spellbuf);
8149}
8150
8151/*
8152 * Build the soundfold trie for language "slang".
8153 */
8154 static int
8155sug_filltree(spin, slang)
8156 spellinfo_T *spin;
8157 slang_T *slang;
8158{
8159 char_u *byts;
8160 idx_T *idxs;
8161 int depth;
8162 idx_T arridx[MAXWLEN];
8163 int curi[MAXWLEN];
8164 char_u tword[MAXWLEN];
8165 char_u tsalword[MAXWLEN];
8166 int c;
8167 idx_T n;
8168 unsigned words_done = 0;
8169 int wordcount[MAXWLEN];
8170
8171 /* We use si_foldroot for the souldfolded trie. */
8172 spin->si_foldroot = wordtree_alloc(spin);
8173 if (spin->si_foldroot == NULL)
8174 return FAIL;
8175
8176 /* let tree_add_word() know we're adding to the soundfolded tree */
8177 spin->si_sugtree = TRUE;
8178
8179 /*
8180 * Go through the whole case-folded tree, soundfold each word and put it
8181 * in the trie.
8182 */
8183 byts = slang->sl_fbyts;
8184 idxs = slang->sl_fidxs;
8185
8186 arridx[0] = 0;
8187 curi[0] = 1;
8188 wordcount[0] = 0;
8189
8190 depth = 0;
8191 while (depth >= 0 && !got_int)
8192 {
8193 if (curi[depth] > byts[arridx[depth]])
8194 {
8195 /* Done all bytes at this node, go up one level. */
8196 idxs[arridx[depth]] = wordcount[depth];
8197 if (depth > 0)
8198 wordcount[depth - 1] += wordcount[depth];
8199
8200 --depth;
8201 line_breakcheck();
8202 }
8203 else
8204 {
8205
8206 /* Do one more byte at this node. */
8207 n = arridx[depth] + curi[depth];
8208 ++curi[depth];
8209
8210 c = byts[n];
8211 if (c == 0)
8212 {
8213 /* Sound-fold the word. */
8214 tword[depth] = NUL;
8215 spell_soundfold(slang, tword, TRUE, tsalword);
8216
8217 /* We use the "flags" field for the MSB of the wordnr,
8218 * "region" for the LSB of the wordnr. */
8219 if (tree_add_word(spin, tsalword, spin->si_foldroot,
8220 words_done >> 16, words_done & 0xffff,
8221 0) == FAIL)
8222 return FAIL;
8223
8224 ++words_done;
8225 ++wordcount[depth];
8226
8227 /* Reset the block count each time to avoid compression
8228 * kicking in. */
8229 spin->si_blocks_cnt = 0;
8230
8231 /* Skip over any other NUL bytes (same word with different
8232 * flags). */
8233 while (byts[n + 1] == 0)
8234 {
8235 ++n;
8236 ++curi[depth];
8237 }
8238 }
8239 else
8240 {
8241 /* Normal char, go one level deeper. */
8242 tword[depth++] = c;
8243 arridx[depth] = idxs[n];
8244 curi[depth] = 1;
8245 wordcount[depth] = 0;
8246 }
8247 }
8248 }
8249
8250 smsg((char_u *)_("Total number of words: %d"), words_done);
8251
8252 return OK;
8253}
8254
8255/*
8256 * Make the table that links each word in the soundfold trie to the words it
8257 * can be produced from.
8258 * This is not unlike lines in a file, thus use a memfile to be able to access
8259 * the table efficiently.
8260 * Returns FAIL when out of memory.
8261 */
8262 static int
8263sug_maketable(spin)
8264 spellinfo_T *spin;
8265{
8266 garray_T ga;
8267 int res = OK;
8268
8269 /* Allocate a buffer, open a memline for it and create the swap file
8270 * (uses a temp file, not a .swp file). */
8271 spin->si_spellbuf = open_spellbuf();
8272 if (spin->si_spellbuf == NULL)
8273 return FAIL;
8274
8275 /* Use a buffer to store the line info, avoids allocating many small
8276 * pieces of memory. */
8277 ga_init2(&ga, 1, 100);
8278
8279 /* recursively go through the tree */
8280 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
8281 res = FAIL;
8282
8283 ga_clear(&ga);
8284 return res;
8285}
8286
8287/*
8288 * Fill the table for one node and its children.
8289 * Returns the wordnr at the start of the node.
8290 * Returns -1 when out of memory.
8291 */
8292 static int
8293sug_filltable(spin, node, startwordnr, gap)
8294 spellinfo_T *spin;
8295 wordnode_T *node;
8296 int startwordnr;
8297 garray_T *gap; /* place to store line of numbers */
8298{
8299 wordnode_T *p, *np;
8300 int wordnr = startwordnr;
8301 int nr;
8302 int prev_nr;
8303
8304 for (p = node; p != NULL; p = p->wn_sibling)
8305 {
8306 if (p->wn_byte == NUL)
8307 {
8308 gap->ga_len = 0;
8309 prev_nr = 0;
8310 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
8311 {
8312 if (ga_grow(gap, 10) == FAIL)
8313 return -1;
8314
8315 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
8316 /* Compute the offset from the previous nr and store the
8317 * offset in a way that it takes a minimum number of bytes.
8318 * It's a bit like utf-8, but without the need to mark
8319 * following bytes. */
8320 nr -= prev_nr;
8321 prev_nr += nr;
8322 gap->ga_len += offset2bytes(nr,
8323 (char_u *)gap->ga_data + gap->ga_len);
8324 }
8325
8326 /* add the NUL byte */
8327 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
8328
8329 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
8330 gap->ga_data, gap->ga_len, TRUE) == FAIL)
8331 return -1;
8332 ++wordnr;
8333
8334 /* Remove extra NUL entries, we no longer need them. We don't
8335 * bother freeing the nodes, the won't be reused anyway. */
8336 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
8337 p->wn_sibling = p->wn_sibling->wn_sibling;
8338
8339 /* Clear the flags on the remaining NUL node, so that compression
8340 * works a lot better. */
8341 p->wn_flags = 0;
8342 p->wn_region = 0;
8343 }
8344 else
8345 {
8346 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
8347 if (wordnr == -1)
8348 return -1;
8349 }
8350 }
8351 return wordnr;
8352}
8353
8354/*
8355 * Convert an offset into a minimal number of bytes.
8356 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
8357 * bytes.
8358 */
8359 static int
8360offset2bytes(nr, buf)
8361 int nr;
8362 char_u *buf;
8363{
8364 int rem;
8365 int b1, b2, b3, b4;
8366
8367 /* Split the number in parts of base 255. We need to avoid NUL bytes. */
8368 b1 = nr % 255 + 1;
8369 rem = nr / 255;
8370 b2 = rem % 255 + 1;
8371 rem = rem / 255;
8372 b3 = rem % 255 + 1;
8373 b4 = rem / 255 + 1;
8374
8375 if (b4 > 1 || b3 > 0x1f) /* 4 bytes */
8376 {
8377 buf[0] = 0xe0 + b4;
8378 buf[1] = b3;
8379 buf[2] = b2;
8380 buf[3] = b1;
8381 return 4;
8382 }
8383 if (b3 > 1 || b2 > 0x3f ) /* 3 bytes */
8384 {
8385 buf[0] = 0xc0 + b3;
8386 buf[1] = b2;
8387 buf[2] = b1;
8388 return 3;
8389 }
8390 if (b2 > 1 || b1 > 0x7f ) /* 2 bytes */
8391 {
8392 buf[0] = 0x80 + b2;
8393 buf[1] = b1;
8394 return 2;
8395 }
8396 /* 1 byte */
8397 buf[0] = b1;
8398 return 1;
8399}
8400
8401/*
8402 * Opposite of offset2bytes().
8403 * "pp" points to the bytes and is advanced over it.
8404 * Returns the offset.
8405 */
8406 static int
8407bytes2offset(pp)
8408 char_u **pp;
8409{
8410 char_u *p = *pp;
8411 int nr;
8412 int c;
8413
8414 c = *p++;
8415 if ((c & 0x80) == 0x00) /* 1 byte */
8416 {
8417 nr = c - 1;
8418 }
8419 else if ((c & 0xc0) == 0x80) /* 2 bytes */
8420 {
8421 nr = (c & 0x3f) - 1;
8422 nr = nr * 255 + (*p++ - 1);
8423 }
8424 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
8425 {
8426 nr = (c & 0x1f) - 1;
8427 nr = nr * 255 + (*p++ - 1);
8428 nr = nr * 255 + (*p++ - 1);
8429 }
8430 else /* 4 bytes */
8431 {
8432 nr = (c & 0x0f) - 1;
8433 nr = nr * 255 + (*p++ - 1);
8434 nr = nr * 255 + (*p++ - 1);
8435 nr = nr * 255 + (*p++ - 1);
8436 }
8437
8438 *pp = p;
8439 return nr;
8440}
8441
8442/*
8443 * Write the .sug file in "fname".
8444 */
8445 static void
8446sug_write(spin, fname)
8447 spellinfo_T *spin;
8448 char_u *fname;
8449{
8450 FILE *fd;
8451 wordnode_T *tree;
8452 int nodecount;
8453 int wcount;
8454 char_u *line;
8455 linenr_T lnum;
8456 int len;
8457
8458 /* Create the file. Note that an existing file is silently overwritten! */
8459 fd = mch_fopen((char *)fname, "w");
8460 if (fd == NULL)
8461 {
8462 EMSG2(_(e_notopen), fname);
8463 return;
8464 }
8465
8466 vim_snprintf((char *)IObuff, IOSIZE,
8467 _("Writing suggestion file %s ..."), fname);
8468 spell_message(spin, IObuff);
8469
8470 /*
8471 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
8472 */
8473 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) /* <fileID> */
8474 {
8475 EMSG(_(e_write));
8476 goto theend;
8477 }
8478 putc(VIMSUGVERSION, fd); /* <versionnr> */
8479
8480 /* Write si_sugtime to the file. */
8481 put_sugtime(spin, fd); /* <timestamp> */
8482
8483 /*
8484 * <SUGWORDTREE>
8485 */
8486 spin->si_memtot = 0;
8487 tree = spin->si_foldroot->wn_sibling;
8488
8489 /* Clear the index and wnode fields in the tree. */
8490 clear_node(tree);
8491
8492 /* Count the number of nodes. Needed to be able to allocate the
8493 * memory when reading the nodes. Also fills in index for shared
8494 * nodes. */
8495 nodecount = put_node(NULL, tree, 0, 0, FALSE);
8496
8497 /* number of nodes in 4 bytes */
8498 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
8499 spin->si_memtot += nodecount + nodecount * sizeof(int);
8500
8501 /* Write the nodes. */
8502 (void)put_node(fd, tree, 0, 0, FALSE);
8503
8504 /*
8505 * <SUGTABLE>: <sugwcount> <sugline> ...
8506 */
8507 wcount = spin->si_spellbuf->b_ml.ml_line_count;
8508 put_bytes(fd, (long_u)wcount, 4); /* <sugwcount> */
8509
8510 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
8511 {
8512 /* <sugline>: <sugnr> ... NUL */
8513 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
8514 len = STRLEN(line) + 1;
8515 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
8516 {
8517 EMSG(_(e_write));
8518 goto theend;
8519 }
8520 spin->si_memtot += len;
8521 }
8522
8523 /* Write another byte to check for errors. */
8524 if (putc(0, fd) == EOF)
8525 EMSG(_(e_write));
8526
8527 vim_snprintf((char *)IObuff, IOSIZE,
8528 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
8529 spell_message(spin, IObuff);
8530
8531theend:
8532 /* close the file */
8533 fclose(fd);
8534}
8535
8536/*
8537 * Open a spell buffer. This is a nameless buffer that is not in the buffer
8538 * list and only contains text lines. Can use a swapfile to reduce memory
8539 * use.
8540 * Most other fields are invalid! Esp. watch out for string options being
8541 * NULL and there is no undo info.
8542 * Returns NULL when out of memory.
8543 */
8544 static buf_T *
8545open_spellbuf()
8546{
8547 buf_T *buf;
8548
8549 buf = (buf_T *)alloc_clear(sizeof(buf_T));
8550 if (buf != NULL)
8551 {
8552 buf->b_spell = TRUE;
8553 buf->b_p_swf = TRUE; /* may create a swap file */
8554 ml_open(buf);
8555 ml_open_file(buf); /* create swap file now */
8556 }
8557 return buf;
8558}
8559
8560/*
8561 * Close the buffer used for spell info.
8562 */
8563 static void
8564close_spellbuf(buf)
8565 buf_T *buf;
8566{
8567 if (buf != NULL)
8568 {
8569 ml_close(buf, TRUE);
8570 vim_free(buf);
8571 }
8572}
8573
8574
8575/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008576 * Create a Vim spell file from one or more word lists.
8577 * "fnames[0]" is the output file name.
8578 * "fnames[fcount - 1]" is the last input file name.
8579 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
8580 * and ".spl" is appended to make the output file name.
8581 */
8582 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008583mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00008584 int fcount;
8585 char_u **fnames;
8586 int ascii; /* -ascii argument given */
8587 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008588 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008589{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008590 char_u fname[MAXPATHL];
8591 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00008592 char_u **innames;
8593 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008594 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008595 int i;
8596 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008597 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008598 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008599 spellinfo_T spin;
8600
8601 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008602 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008603 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008604 spin.si_followup = TRUE;
8605 spin.si_rem_accents = TRUE;
8606 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008607 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008608 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
8609 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008610 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008611 hash_init(&spin.si_commonwords);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008612 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008613
Bram Moolenaarb765d632005-06-07 21:00:02 +00008614 /* default: fnames[0] is output file, following are input files */
8615 innames = &fnames[1];
8616 incount = fcount - 1;
8617
8618 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00008619 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008620 len = STRLEN(fnames[0]);
8621 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
8622 {
8623 /* For ":mkspell path/en.latin1.add" output file is
8624 * "path/en.latin1.add.spl". */
8625 innames = &fnames[0];
8626 incount = 1;
8627 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
8628 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008629 else if (fcount == 1)
8630 {
8631 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
8632 innames = &fnames[0];
8633 incount = 1;
8634 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
8635 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
8636 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00008637 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
8638 {
8639 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008640 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008641 }
8642 else
8643 /* Name should be language, make the file name from it. */
8644 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
8645 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
8646
8647 /* Check for .ascii.spl. */
8648 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
8649 spin.si_ascii = TRUE;
8650
8651 /* Check for .add.spl. */
8652 if (strstr((char *)gettail(wfname), ".add.") != NULL)
8653 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00008654 }
8655
Bram Moolenaarb765d632005-06-07 21:00:02 +00008656 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008657 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008658 else if (vim_strchr(gettail(wfname), '_') != NULL)
8659 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00008660 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008661 EMSG(_("E754: Only up to 8 regions supported"));
8662 else
8663 {
8664 /* Check for overwriting before doing things that may take a lot of
8665 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008666 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008667 {
8668 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00008669 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008670 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00008671 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008672 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008673 EMSG2(_(e_isadir2), wfname);
8674 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008675 }
8676
8677 /*
8678 * Init the aff and dic pointers.
8679 * Get the region names if there are more than 2 arguments.
8680 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008681 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008682 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008683 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008684
Bram Moolenaar3982c542005-06-08 21:56:31 +00008685 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008686 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008687 len = STRLEN(innames[i]);
8688 if (STRLEN(gettail(innames[i])) < 5
8689 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008690 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008691 EMSG2(_("E755: Invalid region in %s"), innames[i]);
8692 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008693 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00008694 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
8695 spin.si_region_name[i * 2 + 1] =
8696 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008697 }
8698 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00008699 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008700
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008701 spin.si_foldroot = wordtree_alloc(&spin);
8702 spin.si_keeproot = wordtree_alloc(&spin);
8703 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008704 if (spin.si_foldroot == NULL
8705 || spin.si_keeproot == NULL
8706 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008707 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008708 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008709 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008710 }
8711
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008712 /* When not producing a .add.spl file clear the character table when
8713 * we encounter one in the .aff file. This means we dump the current
8714 * one in the .spl file if the .aff file doesn't define one. That's
8715 * better than guessing the contents, the table will match a
8716 * previously loaded spell file. */
8717 if (!spin.si_add)
8718 spin.si_clear_chartab = TRUE;
8719
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008720 /*
8721 * Read all the .aff and .dic files.
8722 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00008723 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008724 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008725 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008726 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008727 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008728 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008729
Bram Moolenaarb765d632005-06-07 21:00:02 +00008730 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008731 if (mch_stat((char *)fname, &st) >= 0)
8732 {
8733 /* Read the .aff file. Will init "spin->si_conv" based on the
8734 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008735 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008736 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008737 error = TRUE;
8738 else
8739 {
8740 /* Read the .dic file and store the words in the trees. */
8741 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00008742 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008743 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008744 error = TRUE;
8745 }
8746 }
8747 else
8748 {
8749 /* No .aff file, try reading the file as a word list. Store
8750 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008751 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008752 error = TRUE;
8753 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008754
Bram Moolenaarb765d632005-06-07 21:00:02 +00008755#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008756 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008757 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008758#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008759 }
8760
Bram Moolenaar78622822005-08-23 21:00:13 +00008761 if (spin.si_compflags != NULL && spin.si_nobreak)
8762 MSG(_("Warning: both compounding and NOBREAK specified"));
8763
Bram Moolenaar4770d092006-01-12 23:22:24 +00008764 if (!error && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008765 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008766 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00008767 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008768 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008769 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008770 wordtree_compress(&spin, spin.si_foldroot);
8771 wordtree_compress(&spin, spin.si_keeproot);
8772 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008773 }
8774
Bram Moolenaar4770d092006-01-12 23:22:24 +00008775 if (!error && !got_int)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008776 {
8777 /*
8778 * Write the info in the spell file.
8779 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008780 vim_snprintf((char *)IObuff, IOSIZE,
8781 _("Writing spell file %s ..."), wfname);
8782 spell_message(&spin, IObuff);
Bram Moolenaar50cde822005-06-05 21:54:54 +00008783
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008784 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008785
Bram Moolenaar4770d092006-01-12 23:22:24 +00008786 spell_message(&spin, (char_u *)_("Done!"));
8787 vim_snprintf((char *)IObuff, IOSIZE,
8788 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
8789 spell_message(&spin, IObuff);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008790
Bram Moolenaar4770d092006-01-12 23:22:24 +00008791 /*
8792 * If the file is loaded need to reload it.
8793 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008794 if (!error)
8795 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008796 }
8797
8798 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008799 ga_clear(&spin.si_rep);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008800 ga_clear(&spin.si_repsal);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008801 ga_clear(&spin.si_sal);
8802 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008803 ga_clear(&spin.si_prefcond);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008804 hash_clear_all(&spin.si_commonwords, 0);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008805
8806 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008807 for (i = 0; i < incount; ++i)
8808 if (afile[i] != NULL)
8809 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008810
8811 /* Free all the bits and pieces at once. */
8812 free_blocks(spin.si_blocks);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008813
8814 /*
8815 * If there is soundfolding info and no NOSUGFILE item create the
8816 * .sug file with the soundfolded word trie.
8817 */
8818 if (spin.si_sugtime != 0 && !error && !got_int)
8819 spell_make_sugfile(&spin, wfname);
8820
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008821 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008822}
8823
Bram Moolenaar4770d092006-01-12 23:22:24 +00008824/*
8825 * Display a message for spell file processing when 'verbose' is set or using
8826 * ":mkspell". "str" can be IObuff.
8827 */
8828 static void
8829spell_message(spin, str)
8830 spellinfo_T *spin;
8831 char_u *str;
8832{
8833 if (spin->si_verbose || p_verbose > 2)
8834 {
8835 if (!spin->si_verbose)
8836 verbose_enter();
8837 MSG(str);
8838 out_flush();
8839 if (!spin->si_verbose)
8840 verbose_leave();
8841 }
8842}
Bram Moolenaarb765d632005-06-07 21:00:02 +00008843
8844/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008845 * ":[count]spellgood {word}"
8846 * ":[count]spellwrong {word}"
Bram Moolenaard0131a82006-03-04 21:46:13 +00008847 * ":[count]spellundo {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00008848 */
8849 void
8850ex_spell(eap)
8851 exarg_T *eap;
8852{
Bram Moolenaar7887d882005-07-01 22:33:52 +00008853 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaard0131a82006-03-04 21:46:13 +00008854 eap->forceit ? 0 : (int)eap->line2,
8855 eap->cmdidx == CMD_spellundo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008856}
8857
8858/*
8859 * Add "word[len]" to 'spellfile' as a good or bad word.
8860 */
8861 void
Bram Moolenaard0131a82006-03-04 21:46:13 +00008862spell_add_word(word, len, bad, index, undo)
Bram Moolenaarb765d632005-06-07 21:00:02 +00008863 char_u *word;
8864 int len;
8865 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008866 int index; /* "zG" and "zW": zero, otherwise index in
8867 'spellfile' */
Bram Moolenaard0131a82006-03-04 21:46:13 +00008868 int undo; /* TRUE for "zug", "zuG", "zuw" and "zuW" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008869{
8870 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008871 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008872 int new_spf = FALSE;
Bram Moolenaar7887d882005-07-01 22:33:52 +00008873 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008874 char_u fnamebuf[MAXPATHL];
8875 char_u line[MAXWLEN * 2];
8876 long fpos, fpos_next = 0;
8877 int i;
8878 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008879
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008880 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008881 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008882 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00008883 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008884 int_wordlist = vim_tempname('s');
8885 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00008886 return;
8887 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008888 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008889 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00008890 else
8891 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00008892 /* If 'spellfile' isn't set figure out a good default value. */
8893 if (*curbuf->b_p_spf == NUL)
8894 {
8895 init_spellfile();
8896 new_spf = TRUE;
8897 }
8898
8899 if (*curbuf->b_p_spf == NUL)
8900 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008901 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00008902 return;
8903 }
8904
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008905 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
8906 {
8907 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
8908 if (i == index)
8909 break;
8910 if (*spf == NUL)
8911 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00008912 EMSGN(_("E765: 'spellfile' does not have %ld entries"), index);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008913 return;
8914 }
8915 }
8916
Bram Moolenaarb765d632005-06-07 21:00:02 +00008917 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008918 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008919 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
8920 buf = NULL;
8921 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00008922 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00008923 EMSG(_(e_bufloaded));
8924 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008925 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00008926
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008927 fname = fnamebuf;
8928 }
8929
Bram Moolenaard0131a82006-03-04 21:46:13 +00008930 if (bad || undo)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008931 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00008932 /* When the word appears as good word we need to remove that one,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008933 * since its flags sort before the one with WF_BANNED. */
8934 fd = mch_fopen((char *)fname, "r");
8935 if (fd != NULL)
8936 {
8937 while (!vim_fgets(line, MAXWLEN * 2, fd))
8938 {
8939 fpos = fpos_next;
8940 fpos_next = ftell(fd);
8941 if (STRNCMP(word, line, len) == 0
8942 && (line[len] == '/' || line[len] < ' '))
8943 {
8944 /* Found duplicate word. Remove it by writing a '#' at
8945 * the start of the line. Mixing reading and writing
8946 * doesn't work for all systems, close the file first. */
8947 fclose(fd);
8948 fd = mch_fopen((char *)fname, "r+");
8949 if (fd == NULL)
8950 break;
8951 if (fseek(fd, fpos, SEEK_SET) == 0)
Bram Moolenaard0131a82006-03-04 21:46:13 +00008952 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008953 fputc('#', fd);
Bram Moolenaard0131a82006-03-04 21:46:13 +00008954 if (undo)
8955 smsg((char_u *)_("Word removed from %s"), NameBuff);
8956 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008957 fseek(fd, fpos_next, SEEK_SET);
8958 }
8959 }
8960 fclose(fd);
8961 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00008962 }
8963
Bram Moolenaard0131a82006-03-04 21:46:13 +00008964 if (!undo)
Bram Moolenaar7887d882005-07-01 22:33:52 +00008965 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00008966 fd = mch_fopen((char *)fname, "a");
8967 if (fd == NULL && new_spf)
Bram Moolenaar7887d882005-07-01 22:33:52 +00008968 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00008969 /* We just initialized the 'spellfile' option and can't open the
8970 * file. We may need to create the "spell" directory first. We
8971 * already checked the runtime directory is writable in
8972 * init_spellfile(). */
8973 if (!dir_of_file_exists(fname))
8974 {
8975 /* The directory doesn't exist. Try creating it and opening
8976 * the file again. */
8977 vim_mkdir(NameBuff, 0755);
8978 fd = mch_fopen((char *)fname, "a");
8979 }
8980 }
8981
8982 if (fd == NULL)
8983 EMSG2(_(e_notopen), fname);
8984 else
8985 {
8986 if (bad)
8987 fprintf(fd, "%.*s/!\n", len, word);
8988 else
8989 fprintf(fd, "%.*s\n", len, word);
8990 fclose(fd);
8991
8992 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
8993 smsg((char_u *)_("Word added to %s"), NameBuff);
Bram Moolenaar7887d882005-07-01 22:33:52 +00008994 }
8995 }
8996
Bram Moolenaard0131a82006-03-04 21:46:13 +00008997 if (fd != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00008998 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00008999 /* Update the .add.spl file. */
9000 mkspell(1, &fname, FALSE, TRUE, TRUE);
9001
9002 /* If the .add file is edited somewhere, reload it. */
9003 if (buf != NULL)
Bram Moolenaarea8bd732006-01-14 21:15:59 +00009004 buf_reload(buf, buf->b_orig_mode);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009005
9006 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009007 }
9008}
9009
9010/*
9011 * Initialize 'spellfile' for the current buffer.
9012 */
9013 static void
9014init_spellfile()
9015{
9016 char_u buf[MAXPATHL];
9017 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009018 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009019 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009020 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009021 int aspath = FALSE;
9022 char_u *lstart = curbuf->b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009023
9024 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
9025 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009026 /* Find the end of the language name. Exclude the region. If there
9027 * is a path separator remember the start of the tail. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009028 for (lend = curbuf->b_p_spl; *lend != NUL
9029 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009030 if (vim_ispathsep(*lend))
9031 {
9032 aspath = TRUE;
9033 lstart = lend + 1;
9034 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009035
9036 /* Loop over all entries in 'runtimepath'. Use the first one where we
9037 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009038 rtp = p_rtp;
9039 while (*rtp != NUL)
9040 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009041 if (aspath)
9042 /* Use directory of an entry with path, e.g., for
9043 * "/dir/lg.utf-8.spl" use "/dir". */
9044 vim_strncpy(buf, curbuf->b_p_spl, lstart - curbuf->b_p_spl - 1);
9045 else
9046 /* Copy the path from 'runtimepath' to buf[]. */
9047 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00009048 if (filewritable(buf) == 2)
9049 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00009050 /* Use the first language name from 'spelllang' and the
9051 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009052 if (aspath)
9053 vim_strncpy(buf, curbuf->b_p_spl, lend - curbuf->b_p_spl);
9054 else
9055 {
9056 l = STRLEN(buf);
9057 vim_snprintf((char *)buf + l, MAXPATHL - l,
9058 "/spell/%.*s", (int)(lend - lstart), lstart);
9059 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009060 l = STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009061 fname = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang->sl_fname;
9062 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
9063 fname != NULL
9064 && strstr((char *)gettail(fname), ".ascii.") != NULL
9065 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009066 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
9067 break;
9068 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009069 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009070 }
9071 }
9072}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009073
Bram Moolenaar51485f02005-06-04 21:55:20 +00009074
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009075/*
9076 * Init the chartab used for spelling for ASCII.
9077 * EBCDIC is not supported!
9078 */
9079 static void
9080clear_spell_chartab(sp)
9081 spelltab_T *sp;
9082{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009083 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009084
9085 /* Init everything to FALSE. */
9086 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
9087 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
9088 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009089 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009090 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009091 sp->st_upper[i] = i;
9092 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009093
9094 /* We include digits. A word shouldn't start with a digit, but handling
9095 * that is done separately. */
9096 for (i = '0'; i <= '9'; ++i)
9097 sp->st_isw[i] = TRUE;
9098 for (i = 'A'; i <= 'Z'; ++i)
9099 {
9100 sp->st_isw[i] = TRUE;
9101 sp->st_isu[i] = TRUE;
9102 sp->st_fold[i] = i + 0x20;
9103 }
9104 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009105 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009106 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009107 sp->st_upper[i] = i - 0x20;
9108 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009109}
9110
9111/*
9112 * Init the chartab used for spelling. Only depends on 'encoding'.
9113 * Called once while starting up and when 'encoding' changes.
9114 * The default is to use isalpha(), but the spell file should define the word
9115 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009116 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009117 */
9118 void
9119init_spell_chartab()
9120{
9121 int i;
9122
9123 did_set_spelltab = FALSE;
9124 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009125#ifdef FEAT_MBYTE
9126 if (enc_dbcs)
9127 {
9128 /* DBCS: assume double-wide characters are word characters. */
9129 for (i = 128; i <= 255; ++i)
9130 if (MB_BYTE2LEN(i) == 2)
9131 spelltab.st_isw[i] = TRUE;
9132 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009133 else if (enc_utf8)
9134 {
9135 for (i = 128; i < 256; ++i)
9136 {
9137 spelltab.st_isu[i] = utf_isupper(i);
9138 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
9139 spelltab.st_fold[i] = utf_fold(i);
9140 spelltab.st_upper[i] = utf_toupper(i);
9141 }
9142 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009143 else
9144#endif
9145 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009146 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009147 for (i = 128; i < 256; ++i)
9148 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009149 if (MB_ISUPPER(i))
9150 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009151 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009152 spelltab.st_isu[i] = TRUE;
9153 spelltab.st_fold[i] = MB_TOLOWER(i);
9154 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009155 else if (MB_ISLOWER(i))
9156 {
9157 spelltab.st_isw[i] = TRUE;
9158 spelltab.st_upper[i] = MB_TOUPPER(i);
9159 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009160 }
9161 }
9162}
9163
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009164/*
9165 * Set the spell character tables from strings in the affix file.
9166 */
9167 static int
9168set_spell_chartab(fol, low, upp)
9169 char_u *fol;
9170 char_u *low;
9171 char_u *upp;
9172{
9173 /* We build the new tables here first, so that we can compare with the
9174 * previous one. */
9175 spelltab_T new_st;
9176 char_u *pf = fol, *pl = low, *pu = upp;
9177 int f, l, u;
9178
9179 clear_spell_chartab(&new_st);
9180
9181 while (*pf != NUL)
9182 {
9183 if (*pl == NUL || *pu == NUL)
9184 {
9185 EMSG(_(e_affform));
9186 return FAIL;
9187 }
9188#ifdef FEAT_MBYTE
9189 f = mb_ptr2char_adv(&pf);
9190 l = mb_ptr2char_adv(&pl);
9191 u = mb_ptr2char_adv(&pu);
9192#else
9193 f = *pf++;
9194 l = *pl++;
9195 u = *pu++;
9196#endif
9197 /* Every character that appears is a word character. */
9198 if (f < 256)
9199 new_st.st_isw[f] = TRUE;
9200 if (l < 256)
9201 new_st.st_isw[l] = TRUE;
9202 if (u < 256)
9203 new_st.st_isw[u] = TRUE;
9204
9205 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9206 * case-folding */
9207 if (l < 256 && l != f)
9208 {
9209 if (f >= 256)
9210 {
9211 EMSG(_(e_affrange));
9212 return FAIL;
9213 }
9214 new_st.st_fold[l] = f;
9215 }
9216
9217 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009218 * case-folding, it's upper case and the "UPP" is the upper case of
9219 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009220 if (u < 256 && u != f)
9221 {
9222 if (f >= 256)
9223 {
9224 EMSG(_(e_affrange));
9225 return FAIL;
9226 }
9227 new_st.st_fold[u] = f;
9228 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009229 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009230 }
9231 }
9232
9233 if (*pl != NUL || *pu != NUL)
9234 {
9235 EMSG(_(e_affform));
9236 return FAIL;
9237 }
9238
9239 return set_spell_finish(&new_st);
9240}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009241
9242/*
9243 * Set the spell character tables from strings in the .spl file.
9244 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009245 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009246set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009247 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009248 int cnt; /* length of "flags" */
9249 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009250{
9251 /* We build the new tables here first, so that we can compare with the
9252 * previous one. */
9253 spelltab_T new_st;
9254 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009255 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009256 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009257
9258 clear_spell_chartab(&new_st);
9259
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009260 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009261 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009262 if (i < cnt)
9263 {
9264 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
9265 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
9266 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009267
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009268 if (*p != NUL)
9269 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009270#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009271 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009272#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009273 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009274#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009275 new_st.st_fold[i + 128] = c;
9276 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
9277 new_st.st_upper[c] = i + 128;
9278 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009279 }
9280
Bram Moolenaar5195e452005-08-19 20:32:47 +00009281 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009282}
9283
9284 static int
9285set_spell_finish(new_st)
9286 spelltab_T *new_st;
9287{
9288 int i;
9289
9290 if (did_set_spelltab)
9291 {
9292 /* check that it's the same table */
9293 for (i = 0; i < 256; ++i)
9294 {
9295 if (spelltab.st_isw[i] != new_st->st_isw[i]
9296 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009297 || spelltab.st_fold[i] != new_st->st_fold[i]
9298 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009299 {
9300 EMSG(_("E763: Word characters differ between spell files"));
9301 return FAIL;
9302 }
9303 }
9304 }
9305 else
9306 {
9307 /* copy the new spelltab into the one being used */
9308 spelltab = *new_st;
9309 did_set_spelltab = TRUE;
9310 }
9311
9312 return OK;
9313}
9314
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009315/*
Bram Moolenaarea408852005-06-25 22:49:46 +00009316 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009317 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00009318 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009319 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00009320 */
9321 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009322spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00009323 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009324 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00009325{
Bram Moolenaarea408852005-06-25 22:49:46 +00009326#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009327 char_u *s;
9328 int l;
9329 int c;
9330
9331 if (has_mbyte)
9332 {
9333 l = MB_BYTE2LEN(*p);
9334 s = p;
9335 if (l == 1)
9336 {
9337 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009338 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009339 {
9340 s = p + 1; /* skip a mid-word character */
9341 l = MB_BYTE2LEN(*s);
9342 }
9343 }
9344 else
9345 {
9346 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009347 if (c < 256 ? buf->b_spell_ismw[c]
9348 : (buf->b_spell_ismw_mb != NULL
9349 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009350 {
9351 s = p + l;
9352 l = MB_BYTE2LEN(*s);
9353 }
9354 }
9355
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009356 c = mb_ptr2char(s);
9357 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009358 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009359 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009360 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009361#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009362
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009363 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
9364}
9365
9366/*
9367 * Return TRUE if "p" points to a word character.
9368 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9369 */
9370 static int
9371spell_iswordp_nmw(p)
9372 char_u *p;
9373{
9374#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009375 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009376
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009377 if (has_mbyte)
9378 {
9379 c = mb_ptr2char(p);
9380 if (c > 255)
9381 return mb_get_class(p) >= 2;
9382 return spelltab.st_isw[c];
9383 }
9384#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009385 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00009386}
9387
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009388#ifdef FEAT_MBYTE
9389/*
9390 * Return TRUE if "p" points to a word character.
9391 * Wide version of spell_iswordp().
9392 */
9393 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009394spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009395 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009396 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009397{
9398 int *s;
9399
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009400 if (*p < 256 ? buf->b_spell_ismw[*p]
9401 : (buf->b_spell_ismw_mb != NULL
9402 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009403 s = p + 1;
9404 else
9405 s = p;
9406
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009407 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009408 {
9409 if (enc_utf8)
9410 return utf_class(*s) >= 2;
9411 if (enc_dbcs)
9412 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
9413 return 0;
9414 }
9415 return spelltab.st_isw[*s];
9416}
9417#endif
9418
Bram Moolenaarea408852005-06-25 22:49:46 +00009419/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009420 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009421 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009422 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009423 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009424write_spell_prefcond(fd, gap)
9425 FILE *fd;
9426 garray_T *gap;
9427{
9428 int i;
9429 char_u *p;
9430 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009431 int totlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009432
Bram Moolenaar5195e452005-08-19 20:32:47 +00009433 if (fd != NULL)
9434 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
9435
9436 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009437
9438 for (i = 0; i < gap->ga_len; ++i)
9439 {
9440 /* <prefcond> : <condlen> <condstr> */
9441 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009442 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009443 {
9444 len = STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009445 if (fd != NULL)
9446 {
9447 fputc(len, fd);
9448 fwrite(p, (size_t)len, (size_t)1, fd);
9449 }
9450 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009451 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00009452 else if (fd != NULL)
9453 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009454 }
9455
Bram Moolenaar5195e452005-08-19 20:32:47 +00009456 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009457}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009458
9459/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009460 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
9461 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009462 * When using a multi-byte 'encoding' the length may change!
9463 * Returns FAIL when something wrong.
9464 */
9465 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009466spell_casefold(str, len, buf, buflen)
9467 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009468 int len;
9469 char_u *buf;
9470 int buflen;
9471{
9472 int i;
9473
9474 if (len >= buflen)
9475 {
9476 buf[0] = NUL;
9477 return FAIL; /* result will not fit */
9478 }
9479
9480#ifdef FEAT_MBYTE
9481 if (has_mbyte)
9482 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009483 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009484 char_u *p;
9485 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009486
9487 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009488 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009489 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009490 if (outi + MB_MAXBYTES > buflen)
9491 {
9492 buf[outi] = NUL;
9493 return FAIL;
9494 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009495 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009496 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009497 }
9498 buf[outi] = NUL;
9499 }
9500 else
9501#endif
9502 {
9503 /* Be quick for non-multibyte encodings. */
9504 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009505 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009506 buf[i] = NUL;
9507 }
9508
9509 return OK;
9510}
9511
Bram Moolenaar4770d092006-01-12 23:22:24 +00009512/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009513#define SPS_BEST 1
9514#define SPS_FAST 2
9515#define SPS_DOUBLE 4
9516
Bram Moolenaar4770d092006-01-12 23:22:24 +00009517static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
9518static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009519
9520/*
9521 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009522 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009523 */
9524 int
9525spell_check_sps()
9526{
9527 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009528 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009529 char_u buf[MAXPATHL];
9530 int f;
9531
9532 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009533 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009534
9535 for (p = p_sps; *p != NUL; )
9536 {
9537 copy_option_part(&p, buf, MAXPATHL, ",");
9538
9539 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009540 if (VIM_ISDIGIT(*buf))
9541 {
9542 s = buf;
9543 sps_limit = getdigits(&s);
9544 if (*s != NUL && !VIM_ISDIGIT(*s))
9545 f = -1;
9546 }
9547 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009548 f = SPS_BEST;
9549 else if (STRCMP(buf, "fast") == 0)
9550 f = SPS_FAST;
9551 else if (STRCMP(buf, "double") == 0)
9552 f = SPS_DOUBLE;
9553 else if (STRNCMP(buf, "expr:", 5) != 0
9554 && STRNCMP(buf, "file:", 5) != 0)
9555 f = -1;
9556
9557 if (f == -1 || (sps_flags != 0 && f != 0))
9558 {
9559 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009560 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009561 return FAIL;
9562 }
9563 if (f != 0)
9564 sps_flags = f;
9565 }
9566
9567 if (sps_flags == 0)
9568 sps_flags = SPS_BEST;
9569
9570 return OK;
9571}
9572
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009573/*
9574 * "z?": Find badly spelled word under or after the cursor.
9575 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009576 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00009577 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009578 */
9579 void
Bram Moolenaard12a1322005-08-21 22:08:24 +00009580spell_suggest(count)
9581 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009582{
9583 char_u *line;
9584 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009585 char_u wcopy[MAXWLEN + 2];
9586 char_u *p;
9587 int i;
9588 int c;
9589 suginfo_T sug;
9590 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009591 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009592 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009593 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009594 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009595 int badlen = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009596
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009597 if (no_spell_checking(curwin))
9598 return;
9599
9600#ifdef FEAT_VISUAL
9601 if (VIsual_active)
9602 {
9603 /* Use the Visually selected text as the bad word. But reject
9604 * a multi-line selection. */
9605 if (curwin->w_cursor.lnum != VIsual.lnum)
9606 {
9607 vim_beep();
9608 return;
9609 }
9610 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
9611 if (badlen < 0)
9612 badlen = -badlen;
9613 else
9614 curwin->w_cursor.col = VIsual.col;
9615 ++badlen;
9616 end_visual_mode();
9617 }
9618 else
9619#endif
9620 /* Find the start of the badly spelled word. */
9621 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +00009622 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009623 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00009624 /* No bad word or it starts after the cursor: use the word under the
9625 * cursor. */
9626 curwin->w_cursor = prev_cursor;
9627 line = ml_get_curline();
9628 p = line + curwin->w_cursor.col;
9629 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009630 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00009631 mb_ptr_back(line, p);
9632 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009633 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00009634 mb_ptr_adv(p);
9635
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009636 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00009637 {
9638 beep_flush();
9639 return;
9640 }
9641 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009642 }
9643
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009644 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009645
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009646 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009647 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009648
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009649 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009650
Bram Moolenaar5195e452005-08-19 20:32:47 +00009651 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
9652 * 'spellsuggest', whatever is smaller. */
9653 if (sps_limit > (int)Rows - 2)
9654 limit = (int)Rows - 2;
9655 else
9656 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009657 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +00009658 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009659
9660 if (sug.su_ga.ga_len == 0)
9661 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +00009662 else if (count > 0)
9663 {
9664 if (count > sug.su_ga.ga_len)
9665 smsg((char_u *)_("Sorry, only %ld suggestions"),
9666 (long)sug.su_ga.ga_len);
9667 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009668 else
9669 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009670 vim_free(repl_from);
9671 repl_from = NULL;
9672 vim_free(repl_to);
9673 repl_to = NULL;
9674
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009675#ifdef FEAT_RIGHTLEFT
9676 /* When 'rightleft' is set the list is drawn right-left. */
9677 cmdmsg_rl = curwin->w_p_rl;
9678 if (cmdmsg_rl)
9679 msg_col = Columns - 1;
9680#endif
9681
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009682 /* List the suggestions. */
9683 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009684 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009685 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
9686 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009687#ifdef FEAT_RIGHTLEFT
9688 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
9689 {
9690 /* And now the rabbit from the high hat: Avoid showing the
9691 * untranslated message rightleft. */
9692 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
9693 sug.su_badlen, sug.su_badptr);
9694 }
9695#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009696 msg_puts(IObuff);
9697 msg_clr_eos();
9698 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00009699
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009700 msg_scroll = TRUE;
9701 for (i = 0; i < sug.su_ga.ga_len; ++i)
9702 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009703 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009704
9705 /* The suggested word may replace only part of the bad word, add
9706 * the not replaced part. */
9707 STRCPY(wcopy, stp->st_word);
9708 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +00009709 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009710 sug.su_badptr + stp->st_orglen,
9711 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009712 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
9713#ifdef FEAT_RIGHTLEFT
9714 if (cmdmsg_rl)
9715 rl_mirror(IObuff);
9716#endif
9717 msg_puts(IObuff);
9718
9719 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009720 msg_puts(IObuff);
9721
9722 /* The word may replace more than "su_badlen". */
9723 if (sug.su_badlen < stp->st_orglen)
9724 {
9725 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
9726 stp->st_orglen, sug.su_badptr);
9727 msg_puts(IObuff);
9728 }
9729
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009730 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009731 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00009732 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009733 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009734 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009735 stp->st_salscore ? "s " : "",
9736 stp->st_score, stp->st_altscore);
9737 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009738 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00009739 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009740#ifdef FEAT_RIGHTLEFT
9741 if (cmdmsg_rl)
9742 /* Mirror the numbers, but keep the leading space. */
9743 rl_mirror(IObuff + 1);
9744#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00009745 msg_advance(30);
9746 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009747 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009748 msg_putchar('\n');
9749 }
9750
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009751#ifdef FEAT_RIGHTLEFT
9752 cmdmsg_rl = FALSE;
9753 msg_col = 0;
9754#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009755 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00009756 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009757 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +00009758 selected -= lines_left;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00009759 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009760 }
9761
Bram Moolenaard12a1322005-08-21 22:08:24 +00009762 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
9763 {
9764 /* Save the from and to text for :spellrepall. */
9765 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00009766 if (sug.su_badlen > stp->st_orglen)
9767 {
9768 /* Replacing less than "su_badlen", append the remainder to
9769 * repl_to. */
9770 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
9771 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
9772 sug.su_badlen - stp->st_orglen,
9773 sug.su_badptr + stp->st_orglen);
9774 repl_to = vim_strsave(IObuff);
9775 }
9776 else
9777 {
9778 /* Replacing su_badlen or more, use the whole word. */
9779 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
9780 repl_to = vim_strsave(stp->st_word);
9781 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00009782
9783 /* Replace the word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009784 p = alloc(STRLEN(line) - stp->st_orglen + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +00009785 if (p != NULL)
9786 {
9787 c = sug.su_badptr - line;
9788 mch_memmove(p, line, c);
9789 STRCPY(p + c, stp->st_word);
9790 STRCAT(p, sug.su_badptr + stp->st_orglen);
9791 ml_replace(curwin->w_cursor.lnum, p, FALSE);
9792 curwin->w_cursor.col = c;
9793 changed_bytes(curwin->w_cursor.lnum, c);
9794
9795 /* For redo we use a change-word command. */
9796 ResetRedobuff();
9797 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +00009798 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +00009799 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +00009800 AppendCharToRedobuff(ESC);
9801 }
9802 }
9803 else
9804 curwin->w_cursor = prev_cursor;
9805
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009806 spell_find_cleanup(&sug);
9807}
9808
9809/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009810 * Check if the word at line "lnum" column "col" is required to start with a
9811 * capital. This uses 'spellcapcheck' of the current buffer.
9812 */
9813 static int
9814check_need_cap(lnum, col)
9815 linenr_T lnum;
9816 colnr_T col;
9817{
9818 int need_cap = FALSE;
9819 char_u *line;
9820 char_u *line_copy = NULL;
9821 char_u *p;
9822 colnr_T endcol;
9823 regmatch_T regmatch;
9824
9825 if (curbuf->b_cap_prog == NULL)
9826 return FALSE;
9827
9828 line = ml_get_curline();
9829 endcol = 0;
9830 if ((int)(skipwhite(line) - line) >= (int)col)
9831 {
9832 /* At start of line, check if previous line is empty or sentence
9833 * ends there. */
9834 if (lnum == 1)
9835 need_cap = TRUE;
9836 else
9837 {
9838 line = ml_get(lnum - 1);
9839 if (*skipwhite(line) == NUL)
9840 need_cap = TRUE;
9841 else
9842 {
9843 /* Append a space in place of the line break. */
9844 line_copy = concat_str(line, (char_u *)" ");
9845 line = line_copy;
9846 endcol = STRLEN(line);
9847 }
9848 }
9849 }
9850 else
9851 endcol = col;
9852
9853 if (endcol > 0)
9854 {
9855 /* Check if sentence ends before the bad word. */
9856 regmatch.regprog = curbuf->b_cap_prog;
9857 regmatch.rm_ic = FALSE;
9858 p = line + endcol;
9859 for (;;)
9860 {
9861 mb_ptr_back(line, p);
9862 if (p == line || spell_iswordp_nmw(p))
9863 break;
9864 if (vim_regexec(&regmatch, p, 0)
9865 && regmatch.endp[0] == line + endcol)
9866 {
9867 need_cap = TRUE;
9868 break;
9869 }
9870 }
9871 }
9872
9873 vim_free(line_copy);
9874
9875 return need_cap;
9876}
9877
9878
9879/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009880 * ":spellrepall"
9881 */
9882/*ARGSUSED*/
9883 void
9884ex_spellrepall(eap)
9885 exarg_T *eap;
9886{
9887 pos_T pos = curwin->w_cursor;
9888 char_u *frompat;
9889 int addlen;
9890 char_u *line;
9891 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009892 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009893 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009894
9895 if (repl_from == NULL || repl_to == NULL)
9896 {
9897 EMSG(_("E752: No previous spell replacement"));
9898 return;
9899 }
9900 addlen = STRLEN(repl_to) - STRLEN(repl_from);
9901
9902 frompat = alloc(STRLEN(repl_from) + 7);
9903 if (frompat == NULL)
9904 return;
9905 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
9906 p_ws = FALSE;
9907
Bram Moolenaar5195e452005-08-19 20:32:47 +00009908 sub_nsubs = 0;
9909 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009910 curwin->w_cursor.lnum = 0;
9911 while (!got_int)
9912 {
9913 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
9914 || u_save_cursor() == FAIL)
9915 break;
9916
9917 /* Only replace when the right word isn't there yet. This happens
9918 * when changing "etc" to "etc.". */
9919 line = ml_get_curline();
9920 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
9921 repl_to, STRLEN(repl_to)) != 0)
9922 {
9923 p = alloc(STRLEN(line) + addlen + 1);
9924 if (p == NULL)
9925 break;
9926 mch_memmove(p, line, curwin->w_cursor.col);
9927 STRCPY(p + curwin->w_cursor.col, repl_to);
9928 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
9929 ml_replace(curwin->w_cursor.lnum, p, FALSE);
9930 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009931
9932 if (curwin->w_cursor.lnum != prev_lnum)
9933 {
9934 ++sub_nlines;
9935 prev_lnum = curwin->w_cursor.lnum;
9936 }
9937 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009938 }
9939 curwin->w_cursor.col += STRLEN(repl_to);
9940 }
9941
9942 p_ws = save_ws;
9943 curwin->w_cursor = pos;
9944 vim_free(frompat);
9945
Bram Moolenaar5195e452005-08-19 20:32:47 +00009946 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009947 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009948 else
9949 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009950}
9951
9952/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009953 * Find spell suggestions for "word". Return them in the growarray "*gap" as
9954 * a list of allocated strings.
9955 */
9956 void
Bram Moolenaar4770d092006-01-12 23:22:24 +00009957spell_suggest_list(gap, word, maxcount, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009958 garray_T *gap;
9959 char_u *word;
9960 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009961 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009962 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009963{
9964 suginfo_T sug;
9965 int i;
9966 suggest_T *stp;
9967 char_u *wcopy;
9968
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009969 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009970
9971 /* Make room in "gap". */
9972 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00009973 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009974 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00009975 for (i = 0; i < sug.su_ga.ga_len; ++i)
9976 {
9977 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009978
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00009979 /* The suggested word may replace only part of "word", add the not
9980 * replaced part. */
9981 wcopy = alloc(stp->st_wordlen
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009982 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00009983 if (wcopy == NULL)
9984 break;
9985 STRCPY(wcopy, stp->st_word);
9986 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
9987 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
9988 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009989 }
9990
9991 spell_find_cleanup(&sug);
9992}
9993
9994/*
9995 * Find spell suggestions for the word at the start of "badptr".
9996 * Return the suggestions in "su->su_ga".
9997 * The maximum number of suggestions is "maxcount".
9998 * Note: does use info for the current window.
9999 * This is based on the mechanisms of Aspell, but completely reimplemented.
10000 */
10001 static void
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010002spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010003 char_u *badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010004 int badlen; /* length of bad word or 0 if unknown */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010005 suginfo_T *su;
10006 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +000010007 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010008 int need_cap; /* word should start with capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010009 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010010{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010011 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010012 char_u buf[MAXPATHL];
10013 char_u *p;
10014 int do_combine = FALSE;
10015 char_u *sps_copy;
10016#ifdef FEAT_EVAL
10017 static int expr_busy = FALSE;
10018#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010019 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010020 int i;
10021 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010022
10023 /*
10024 * Set the info in "*su".
10025 */
10026 vim_memset(su, 0, sizeof(suginfo_T));
10027 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
10028 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010029 if (*badptr == NUL)
10030 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010031 hash_init(&su->su_banned);
10032
10033 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010034 if (badlen != 0)
10035 su->su_badlen = badlen;
10036 else
10037 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010038 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010039 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010040
10041 if (su->su_badlen >= MAXWLEN)
10042 su->su_badlen = MAXWLEN - 1; /* just in case */
10043 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
10044 (void)spell_casefold(su->su_badptr, su->su_badlen,
10045 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010046 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010047 su->su_badflags = badword_captype(su->su_badptr,
10048 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010049 if (need_cap)
10050 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010051
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010052 /* Find the default language for sound folding. We simply use the first
10053 * one in 'spelllang' that supports sound folding. That's good for when
10054 * using multiple files for one language, it's not that bad when mixing
10055 * languages (e.g., "pl,en"). */
10056 for (i = 0; i < curbuf->b_langp.ga_len; ++i)
10057 {
10058 lp = LANGP_ENTRY(curbuf->b_langp, i);
10059 if (lp->lp_sallang != NULL)
10060 {
10061 su->su_sallang = lp->lp_sallang;
10062 break;
10063 }
10064 }
10065
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010066 /* Soundfold the bad word with the default sound folding, so that we don't
10067 * have to do this many times. */
10068 if (su->su_sallang != NULL)
10069 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
10070 su->su_sal_badword);
10071
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010072 /* If the word is not capitalised and spell_check() doesn't consider the
10073 * word to be bad then it might need to be capitalised. Add a suggestion
10074 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010075 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010076 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010077 {
10078 make_case_word(su->su_badword, buf, WF_ONECAP);
10079 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010080 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010081 }
10082
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010083 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +000010084 if (banbadword)
10085 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010086
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010087 /* Make a copy of 'spellsuggest', because the expression may change it. */
10088 sps_copy = vim_strsave(p_sps);
10089 if (sps_copy == NULL)
10090 return;
10091
10092 /* Loop over the items in 'spellsuggest'. */
10093 for (p = sps_copy; *p != NUL; )
10094 {
10095 copy_option_part(&p, buf, MAXPATHL, ",");
10096
10097 if (STRNCMP(buf, "expr:", 5) == 0)
10098 {
10099#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010100 /* Evaluate an expression. Skip this when called recursively,
10101 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010102 if (!expr_busy)
10103 {
10104 expr_busy = TRUE;
10105 spell_suggest_expr(su, buf + 5);
10106 expr_busy = FALSE;
10107 }
10108#endif
10109 }
10110 else if (STRNCMP(buf, "file:", 5) == 0)
10111 /* Use list of suggestions in a file. */
10112 spell_suggest_file(su, buf + 5);
10113 else
10114 {
10115 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010116 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010117 if (sps_flags & SPS_DOUBLE)
10118 do_combine = TRUE;
10119 }
10120 }
10121
10122 vim_free(sps_copy);
10123
10124 if (do_combine)
10125 /* Combine the two list of suggestions. This must be done last,
10126 * because sorting changes the order again. */
10127 score_combine(su);
10128}
10129
10130#ifdef FEAT_EVAL
10131/*
10132 * Find suggestions by evaluating expression "expr".
10133 */
10134 static void
10135spell_suggest_expr(su, expr)
10136 suginfo_T *su;
10137 char_u *expr;
10138{
10139 list_T *list;
10140 listitem_T *li;
10141 int score;
10142 char_u *p;
10143
10144 /* The work is split up in a few parts to avoid having to export
10145 * suginfo_T.
10146 * First evaluate the expression and get the resulting list. */
10147 list = eval_spell_expr(su->su_badword, expr);
10148 if (list != NULL)
10149 {
10150 /* Loop over the items in the list. */
10151 for (li = list->lv_first; li != NULL; li = li->li_next)
10152 if (li->li_tv.v_type == VAR_LIST)
10153 {
10154 /* Get the word and the score from the items. */
10155 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010156 if (score >= 0 && score <= su->su_maxscore)
10157 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10158 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010159 }
10160 list_unref(list);
10161 }
10162
Bram Moolenaar4770d092006-01-12 23:22:24 +000010163 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10164 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010165 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10166}
10167#endif
10168
10169/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010170 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010171 */
10172 static void
10173spell_suggest_file(su, fname)
10174 suginfo_T *su;
10175 char_u *fname;
10176{
10177 FILE *fd;
10178 char_u line[MAXWLEN * 2];
10179 char_u *p;
10180 int len;
10181 char_u cword[MAXWLEN];
10182
10183 /* Open the file. */
10184 fd = mch_fopen((char *)fname, "r");
10185 if (fd == NULL)
10186 {
10187 EMSG2(_(e_notopen), fname);
10188 return;
10189 }
10190
10191 /* Read it line by line. */
10192 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
10193 {
10194 line_breakcheck();
10195
10196 p = vim_strchr(line, '/');
10197 if (p == NULL)
10198 continue; /* No Tab found, just skip the line. */
10199 *p++ = NUL;
10200 if (STRICMP(su->su_badword, line) == 0)
10201 {
10202 /* Match! Isolate the good word, until CR or NL. */
10203 for (len = 0; p[len] >= ' '; ++len)
10204 ;
10205 p[len] = NUL;
10206
10207 /* If the suggestion doesn't have specific case duplicate the case
10208 * of the bad word. */
10209 if (captype(p, NULL) == 0)
10210 {
10211 make_case_word(p, cword, su->su_badflags);
10212 p = cword;
10213 }
10214
10215 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010216 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010217 }
10218 }
10219
10220 fclose(fd);
10221
Bram Moolenaar4770d092006-01-12 23:22:24 +000010222 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10223 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010224 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10225}
10226
10227/*
10228 * Find suggestions for the internal method indicated by "sps_flags".
10229 */
10230 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010231spell_suggest_intern(su, interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010232 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010233 int interactive;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010234{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010235 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010236 * Load the .sug file(s) that are available and not done yet.
10237 */
10238 suggest_load_files();
10239
10240 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010241 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010242 *
10243 * Set a maximum score to limit the combination of operations that is
10244 * tried.
10245 */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010246 suggest_try_special(su);
10247
10248 /*
10249 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10250 * from the .aff file and inserting a space (split the word).
10251 */
10252 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010253
10254 /* For the resulting top-scorers compute the sound-a-like score. */
10255 if (sps_flags & SPS_DOUBLE)
10256 score_comp_sal(su);
10257
10258 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010259 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010260 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010261 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010262 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010263 if (sps_flags & SPS_BEST)
10264 /* Adjust the word score for the suggestions found so far for how
10265 * they sounds like. */
10266 rescore_suggestions(su);
10267
10268 /*
10269 * While going throught the soundfold tree "su_maxscore" is the score
10270 * for the soundfold word, limits the changes that are being tried,
10271 * and "su_sfmaxscore" the rescored score, which is set by
10272 * cleanup_suggestions().
10273 * First find words with a small edit distance, because this is much
10274 * faster and often already finds the top-N suggestions. If we didn't
10275 * find many suggestions try again with a higher edit distance.
10276 * "sl_sounddone" is used to avoid doing the same word twice.
10277 */
10278 suggest_try_soundalike_prep();
10279 su->su_maxscore = SCORE_SFMAX1;
10280 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010281 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010282 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10283 {
10284 /* We didn't find enough matches, try again, allowing more
10285 * changes to the soundfold word. */
10286 su->su_maxscore = SCORE_SFMAX2;
10287 suggest_try_soundalike(su);
10288 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10289 {
10290 /* Still didn't find enough matches, try again, allowing even
10291 * more changes to the soundfold word. */
10292 su->su_maxscore = SCORE_SFMAX3;
10293 suggest_try_soundalike(su);
10294 }
10295 }
10296 su->su_maxscore = su->su_sfmaxscore;
10297 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010298 }
10299
Bram Moolenaar4770d092006-01-12 23:22:24 +000010300 /* When CTRL-C was hit while searching do show the results. Only clear
10301 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010302 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +000010303 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010304 {
10305 (void)vgetc();
10306 got_int = FALSE;
10307 }
10308
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010309 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010310 {
10311 if (sps_flags & SPS_BEST)
10312 /* Adjust the word score for how it sounds like. */
10313 rescore_suggestions(su);
10314
Bram Moolenaar4770d092006-01-12 23:22:24 +000010315 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10316 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010317 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010318 }
10319}
10320
10321/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010322 * Load the .sug files for languages that have one and weren't loaded yet.
10323 */
10324 static void
10325suggest_load_files()
10326{
10327 langp_T *lp;
10328 int lpi;
10329 slang_T *slang;
10330 char_u *dotp;
10331 FILE *fd;
10332 char_u buf[MAXWLEN];
10333 int i;
10334 time_t timestamp;
10335 int wcount;
10336 int wordnr;
10337 garray_T ga;
10338 int c;
10339
10340 /* Do this for all languages that support sound folding. */
10341 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
10342 {
10343 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
10344 slang = lp->lp_slang;
10345 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10346 {
10347 /* Change ".spl" to ".sug" and open the file. When the file isn't
10348 * found silently skip it. Do set "sl_sugloaded" so that we
10349 * don't try again and again. */
10350 slang->sl_sugloaded = TRUE;
10351
10352 dotp = vim_strrchr(slang->sl_fname, '.');
10353 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10354 continue;
10355 STRCPY(dotp, ".sug");
10356 fd = fopen((char *)slang->sl_fname, "r");
10357 if (fd == NULL)
10358 goto nextone;
10359
10360 /*
10361 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10362 */
10363 for (i = 0; i < VIMSUGMAGICL; ++i)
10364 buf[i] = getc(fd); /* <fileID> */
10365 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10366 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010367 EMSG2(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010368 slang->sl_fname);
10369 goto nextone;
10370 }
10371 c = getc(fd); /* <versionnr> */
10372 if (c < VIMSUGVERSION)
10373 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010374 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010375 slang->sl_fname);
10376 goto nextone;
10377 }
10378 else if (c > VIMSUGVERSION)
10379 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010380 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010381 slang->sl_fname);
10382 goto nextone;
10383 }
10384
10385 /* Check the timestamp, it must be exactly the same as the one in
10386 * the .spl file. Otherwise the word numbers won't match. */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010387 timestamp = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010388 if (timestamp != slang->sl_sugtime)
10389 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010390 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010391 slang->sl_fname);
10392 goto nextone;
10393 }
10394
10395 /*
10396 * <SUGWORDTREE>: <wordtree>
10397 * Read the trie with the soundfolded words.
10398 */
10399 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
10400 FALSE, 0) != 0)
10401 {
10402someerror:
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010403 EMSG2(_("E782: error while reading .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010404 slang->sl_fname);
10405 slang_clear_sug(slang);
10406 goto nextone;
10407 }
10408
10409 /*
10410 * <SUGTABLE>: <sugwcount> <sugline> ...
10411 *
10412 * Read the table with word numbers. We use a file buffer for
10413 * this, because it's so much like a file with lines. Makes it
10414 * possible to swap the info and save on memory use.
10415 */
10416 slang->sl_sugbuf = open_spellbuf();
10417 if (slang->sl_sugbuf == NULL)
10418 goto someerror;
10419 /* <sugwcount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010420 wcount = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010421 if (wcount < 0)
10422 goto someerror;
10423
10424 /* Read all the wordnr lists into the buffer, one NUL terminated
10425 * list per line. */
10426 ga_init2(&ga, 1, 100);
10427 for (wordnr = 0; wordnr < wcount; ++wordnr)
10428 {
10429 ga.ga_len = 0;
10430 for (;;)
10431 {
10432 c = getc(fd); /* <sugline> */
10433 if (c < 0 || ga_grow(&ga, 1) == FAIL)
10434 goto someerror;
10435 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
10436 if (c == NUL)
10437 break;
10438 }
10439 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
10440 ga.ga_data, ga.ga_len, TRUE) == FAIL)
10441 goto someerror;
10442 }
10443 ga_clear(&ga);
10444
10445 /*
10446 * Need to put word counts in the word tries, so that we can find
10447 * a word by its number.
10448 */
10449 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
10450 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
10451
10452nextone:
10453 if (fd != NULL)
10454 fclose(fd);
10455 STRCPY(dotp, ".spl");
10456 }
10457 }
10458}
10459
10460
10461/*
10462 * Fill in the wordcount fields for a trie.
10463 * Returns the total number of words.
10464 */
10465 static void
10466tree_count_words(byts, idxs)
10467 char_u *byts;
10468 idx_T *idxs;
10469{
10470 int depth;
10471 idx_T arridx[MAXWLEN];
10472 int curi[MAXWLEN];
10473 int c;
10474 idx_T n;
10475 int wordcount[MAXWLEN];
10476
10477 arridx[0] = 0;
10478 curi[0] = 1;
10479 wordcount[0] = 0;
10480 depth = 0;
10481 while (depth >= 0 && !got_int)
10482 {
10483 if (curi[depth] > byts[arridx[depth]])
10484 {
10485 /* Done all bytes at this node, go up one level. */
10486 idxs[arridx[depth]] = wordcount[depth];
10487 if (depth > 0)
10488 wordcount[depth - 1] += wordcount[depth];
10489
10490 --depth;
10491 fast_breakcheck();
10492 }
10493 else
10494 {
10495 /* Do one more byte at this node. */
10496 n = arridx[depth] + curi[depth];
10497 ++curi[depth];
10498
10499 c = byts[n];
10500 if (c == 0)
10501 {
10502 /* End of word, count it. */
10503 ++wordcount[depth];
10504
10505 /* Skip over any other NUL bytes (same word with different
10506 * flags). */
10507 while (byts[n + 1] == 0)
10508 {
10509 ++n;
10510 ++curi[depth];
10511 }
10512 }
10513 else
10514 {
10515 /* Normal char, go one level deeper to count the words. */
10516 ++depth;
10517 arridx[depth] = idxs[n];
10518 curi[depth] = 1;
10519 wordcount[depth] = 0;
10520 }
10521 }
10522 }
10523}
10524
10525/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010526 * Free the info put in "*su" by spell_find_suggest().
10527 */
10528 static void
10529spell_find_cleanup(su)
10530 suginfo_T *su;
10531{
10532 int i;
10533
10534 /* Free the suggestions. */
10535 for (i = 0; i < su->su_ga.ga_len; ++i)
10536 vim_free(SUG(su->su_ga, i).st_word);
10537 ga_clear(&su->su_ga);
10538 for (i = 0; i < su->su_sga.ga_len; ++i)
10539 vim_free(SUG(su->su_sga, i).st_word);
10540 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010541
10542 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010543 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010544}
10545
10546/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010547 * Make a copy of "word", with the first letter upper or lower cased, to
10548 * "wcopy[MAXWLEN]". "word" must not be empty.
10549 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010550 */
10551 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010552onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010553 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010554 char_u *wcopy;
10555 int upper; /* TRUE: first letter made upper case */
10556{
10557 char_u *p;
10558 int c;
10559 int l;
10560
10561 p = word;
10562#ifdef FEAT_MBYTE
10563 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010564 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010565 else
10566#endif
10567 c = *p++;
10568 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010569 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010570 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010571 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010572#ifdef FEAT_MBYTE
10573 if (has_mbyte)
10574 l = mb_char2bytes(c, wcopy);
10575 else
10576#endif
10577 {
10578 l = 1;
10579 wcopy[0] = c;
10580 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010581 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010582}
10583
10584/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010585 * Make a copy of "word" with all the letters upper cased into
10586 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010587 */
10588 static void
10589allcap_copy(word, wcopy)
10590 char_u *word;
10591 char_u *wcopy;
10592{
10593 char_u *s;
10594 char_u *d;
10595 int c;
10596
10597 d = wcopy;
10598 for (s = word; *s != NUL; )
10599 {
10600#ifdef FEAT_MBYTE
10601 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010602 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010603 else
10604#endif
10605 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +000010606
10607#ifdef FEAT_MBYTE
10608 /* We only change ß to SS when we are certain latin1 is used. It
10609 * would cause weird errors in other 8-bit encodings. */
10610 if (enc_latin1like && c == 0xdf)
10611 {
10612 c = 'S';
10613 if (d - wcopy >= MAXWLEN - 1)
10614 break;
10615 *d++ = c;
10616 }
10617 else
10618#endif
10619 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010620
10621#ifdef FEAT_MBYTE
10622 if (has_mbyte)
10623 {
10624 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
10625 break;
10626 d += mb_char2bytes(c, d);
10627 }
10628 else
10629#endif
10630 {
10631 if (d - wcopy >= MAXWLEN - 1)
10632 break;
10633 *d++ = c;
10634 }
10635 }
10636 *d = NUL;
10637}
10638
10639/*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010640 * Try finding suggestions by recognizing specific situations.
10641 */
10642 static void
10643suggest_try_special(su)
10644 suginfo_T *su;
10645{
10646 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010647 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010648 int c;
10649 char_u word[MAXWLEN];
10650
10651 /*
10652 * Recognize a word that is repeated: "the the".
10653 */
10654 p = skiptowhite(su->su_fbadword);
10655 len = p - su->su_fbadword;
10656 p = skipwhite(p);
10657 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
10658 {
10659 /* Include badflags: if the badword is onecap or allcap
10660 * use that for the goodword too: "The the" -> "The". */
10661 c = su->su_fbadword[len];
10662 su->su_fbadword[len] = NUL;
10663 make_case_word(su->su_fbadword, word, su->su_badflags);
10664 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010665
10666 /* Give a soundalike score of 0, compute the score as if deleting one
10667 * character. */
10668 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010669 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010670 }
10671}
10672
10673/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010674 * Try finding suggestions by adding/removing/swapping letters.
10675 */
10676 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000010677suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010678 suginfo_T *su;
10679{
10680 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010681 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010682 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010683 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010684 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010685
10686 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +000010687 * to find matches (esp. REP items). Append some more text, changing
10688 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010689 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010690 n = STRLEN(fword);
10691 p = su->su_badptr + su->su_badlen;
10692 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010693
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010694 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010695 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010696 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010697
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010698 /* If reloading a spell file fails it's still in the list but
10699 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010700 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010701 continue;
10702
Bram Moolenaar4770d092006-01-12 23:22:24 +000010703 /* Try it for this language. Will add possible suggestions. */
10704 suggest_trie_walk(su, lp, fword, FALSE);
10705 }
10706}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010707
Bram Moolenaar4770d092006-01-12 23:22:24 +000010708/* Check the maximum score, if we go over it we won't try this change. */
10709#define TRY_DEEPER(su, stack, depth, add) \
10710 (stack[depth].ts_score + (add) < su->su_maxscore)
10711
10712/*
10713 * Try finding suggestions by adding/removing/swapping letters.
10714 *
10715 * This uses a state machine. At each node in the tree we try various
10716 * operations. When trying if an operation works "depth" is increased and the
10717 * stack[] is used to store info. This allows combinations, thus insert one
10718 * character, replace one and delete another. The number of changes is
10719 * limited by su->su_maxscore.
10720 *
10721 * After implementing this I noticed an article by Kemal Oflazer that
10722 * describes something similar: "Error-tolerant Finite State Recognition with
10723 * Applications to Morphological Analysis and Spelling Correction" (1996).
10724 * The implementation in the article is simplified and requires a stack of
10725 * unknown depth. The implementation here only needs a stack depth equal to
10726 * the length of the word.
10727 *
10728 * This is also used for the sound-folded word, "soundfold" is TRUE then.
10729 * The mechanism is the same, but we find a match with a sound-folded word
10730 * that comes from one or more original words. Each of these words may be
10731 * added, this is done by add_sound_suggest().
10732 * Don't use:
10733 * the prefix tree or the keep-case tree
10734 * "su->su_badlen"
10735 * anything to do with upper and lower case
10736 * anything to do with word or non-word characters ("spell_iswordp()")
10737 * banned words
10738 * word flags (rare, region, compounding)
10739 * word splitting for now
10740 * "similar_chars()"
10741 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
10742 */
10743 static void
10744suggest_trie_walk(su, lp, fword, soundfold)
10745 suginfo_T *su;
10746 langp_T *lp;
10747 char_u *fword;
10748 int soundfold;
10749{
10750 char_u tword[MAXWLEN]; /* good word collected so far */
10751 trystate_T stack[MAXWLEN];
10752 char_u preword[MAXWLEN * 3]; /* word found with proper case;
10753 * concatanation of prefix compound
10754 * words and split word. NUL terminated
10755 * when going deeper but not when coming
10756 * back. */
10757 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
10758 trystate_T *sp;
10759 int newscore;
10760 int score;
10761 char_u *byts, *fbyts, *pbyts;
10762 idx_T *idxs, *fidxs, *pidxs;
10763 int depth;
10764 int c, c2, c3;
10765 int n = 0;
10766 int flags;
10767 garray_T *gap;
10768 idx_T arridx;
10769 int len;
10770 char_u *p;
10771 fromto_T *ftp;
10772 int fl = 0, tl;
10773 int repextra = 0; /* extra bytes in fword[] from REP item */
10774 slang_T *slang = lp->lp_slang;
10775 int fword_ends;
10776 int goodword_ends;
10777#ifdef DEBUG_TRIEWALK
10778 /* Stores the name of the change made at each level. */
10779 char_u changename[MAXWLEN][80];
10780#endif
10781 int breakcheckcount = 1000;
10782 int compound_ok;
10783
10784 /*
10785 * Go through the whole case-fold tree, try changes at each node.
10786 * "tword[]" contains the word collected from nodes in the tree.
10787 * "fword[]" the word we are trying to match with (initially the bad
10788 * word).
10789 */
10790 depth = 0;
10791 sp = &stack[0];
10792 vim_memset(sp, 0, sizeof(trystate_T));
10793 sp->ts_curi = 1;
10794
10795 if (soundfold)
10796 {
10797 /* Going through the soundfold tree. */
10798 byts = fbyts = slang->sl_sbyts;
10799 idxs = fidxs = slang->sl_sidxs;
10800 pbyts = NULL;
10801 pidxs = NULL;
10802 sp->ts_prefixdepth = PFD_NOPREFIX;
10803 sp->ts_state = STATE_START;
10804 }
10805 else
10806 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010807 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010808 * When there are postponed prefixes we need to use these first. At
10809 * the end of the prefix we continue in the case-fold tree.
10810 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010811 fbyts = slang->sl_fbyts;
10812 fidxs = slang->sl_fidxs;
10813 pbyts = slang->sl_pbyts;
10814 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010815 if (pbyts != NULL)
10816 {
10817 byts = pbyts;
10818 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010819 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010820 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
10821 }
10822 else
10823 {
10824 byts = fbyts;
10825 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010826 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010827 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010828 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000010829 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010830
Bram Moolenaar4770d092006-01-12 23:22:24 +000010831 /*
10832 * Loop to find all suggestions. At each round we either:
10833 * - For the current state try one operation, advance "ts_curi",
10834 * increase "depth".
10835 * - When a state is done go to the next, set "ts_state".
10836 * - When all states are tried decrease "depth".
10837 */
10838 while (depth >= 0 && !got_int)
10839 {
10840 sp = &stack[depth];
10841 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010842 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010843 case STATE_START:
10844 case STATE_NOPREFIX:
10845 /*
10846 * Start of node: Deal with NUL bytes, which means
10847 * tword[] may end here.
10848 */
10849 arridx = sp->ts_arridx; /* current node in the tree */
10850 len = byts[arridx]; /* bytes in this node */
10851 arridx += sp->ts_curi; /* index of current byte */
10852
10853 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010854 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010855 /* Skip over the NUL bytes, we use them later. */
10856 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
10857 ;
10858 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010859
Bram Moolenaar4770d092006-01-12 23:22:24 +000010860 /* Always past NUL bytes now. */
10861 n = (int)sp->ts_state;
10862 sp->ts_state = STATE_ENDNUL;
10863 sp->ts_save_badflags = su->su_badflags;
10864
10865 /* At end of a prefix or at start of prefixtree: check for
10866 * following word. */
10867 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010868 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010869 /* Set su->su_badflags to the caps type at this position.
10870 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010871#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000010872 if (has_mbyte)
10873 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
10874 else
Bram Moolenaar53805d12005-08-01 07:08:33 +000010875#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000010876 n = sp->ts_fidx;
10877 flags = badword_captype(su->su_badptr, su->su_badptr + n);
10878 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000010879 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010880#ifdef DEBUG_TRIEWALK
10881 sprintf(changename[depth], "prefix");
10882#endif
10883 go_deeper(stack, depth, 0);
10884 ++depth;
10885 sp = &stack[depth];
10886 sp->ts_prefixdepth = depth - 1;
10887 byts = fbyts;
10888 idxs = fidxs;
10889 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010890
Bram Moolenaar4770d092006-01-12 23:22:24 +000010891 /* Move the prefix to preword[] with the right case
10892 * and make find_keepcap_word() works. */
10893 tword[sp->ts_twordlen] = NUL;
10894 make_case_word(tword + sp->ts_splitoff,
10895 preword + sp->ts_prewordlen, flags);
10896 sp->ts_prewordlen = STRLEN(preword);
10897 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010898 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000010899 break;
10900 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010901
Bram Moolenaar4770d092006-01-12 23:22:24 +000010902 if (sp->ts_curi > len || byts[arridx] != 0)
10903 {
10904 /* Past bytes in node and/or past NUL bytes. */
10905 sp->ts_state = STATE_ENDNUL;
10906 sp->ts_save_badflags = su->su_badflags;
10907 break;
10908 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010909
Bram Moolenaar4770d092006-01-12 23:22:24 +000010910 /*
10911 * End of word in tree.
10912 */
10913 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010914
Bram Moolenaar4770d092006-01-12 23:22:24 +000010915 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010916
10917 /* Skip words with the NOSUGGEST flag. */
10918 if (flags & WF_NOSUGGEST)
10919 break;
10920
Bram Moolenaar4770d092006-01-12 23:22:24 +000010921 fword_ends = (fword[sp->ts_fidx] == NUL
10922 || (soundfold
10923 ? vim_iswhite(fword[sp->ts_fidx])
10924 : !spell_iswordp(fword + sp->ts_fidx, curbuf)));
10925 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010926
Bram Moolenaar4770d092006-01-12 23:22:24 +000010927 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +000010928 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010929 {
10930 /* There was a prefix before the word. Check that the prefix
10931 * can be used with this word. */
10932 /* Count the length of the NULs in the prefix. If there are
10933 * none this must be the first try without a prefix. */
10934 n = stack[sp->ts_prefixdepth].ts_arridx;
10935 len = pbyts[n++];
10936 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
10937 ;
10938 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010939 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010940 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000010941 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010942 if (c == 0)
10943 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010944
Bram Moolenaar4770d092006-01-12 23:22:24 +000010945 /* Use the WF_RARE flag for a rare prefix. */
10946 if (c & WF_RAREPFX)
10947 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010948
Bram Moolenaar4770d092006-01-12 23:22:24 +000010949 /* Tricky: when checking for both prefix and compounding
10950 * we run into the prefix flag first.
10951 * Remember that it's OK, so that we accept the prefix
10952 * when arriving at a compound flag. */
10953 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010954 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000010955 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010956
Bram Moolenaar4770d092006-01-12 23:22:24 +000010957 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
10958 * appending another compound word below. */
10959 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010960 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +000010961 goodword_ends = FALSE;
10962 else
10963 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010964
Bram Moolenaar4770d092006-01-12 23:22:24 +000010965 p = NULL;
10966 compound_ok = TRUE;
10967 if (sp->ts_complen > sp->ts_compsplit)
10968 {
10969 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +000010970 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010971 /* There was a word before this word. When there was no
10972 * change in this word (it was correct) add the first word
10973 * as a suggestion. If this word was corrected too, we
10974 * need to check if a correct word follows. */
10975 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +000010976 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +000010977 && STRNCMP(fword + sp->ts_splitfidx,
10978 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +000010979 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010980 {
10981 preword[sp->ts_prewordlen] = NUL;
10982 newscore = score_wordcount_adj(slang, sp->ts_score,
10983 preword + sp->ts_prewordlen,
10984 sp->ts_prewordlen > 0);
10985 /* Add the suggestion if the score isn't too bad. */
10986 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +000010987 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010988 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010989 newscore, 0, FALSE,
10990 lp->lp_sallang, FALSE);
10991 break;
Bram Moolenaar78622822005-08-23 21:00:13 +000010992 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000010993 }
Bram Moolenaare52325c2005-08-22 22:54:29 +000010994 else
Bram Moolenaar0c405862005-06-22 22:26:26 +000010995 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010996 /* There was a compound word before this word. If this
10997 * word does not support compounding then give up
10998 * (splitting is tried for the word without compound
10999 * flag). */
11000 if (((unsigned)flags >> 24) == 0
11001 || sp->ts_twordlen - sp->ts_splitoff
11002 < slang->sl_compminlen)
11003 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011004#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011005 /* For multi-byte chars check character length against
11006 * COMPOUNDMIN. */
11007 if (has_mbyte
11008 && slang->sl_compminlen > 0
11009 && mb_charlen(tword + sp->ts_splitoff)
11010 < slang->sl_compminlen)
11011 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011012#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +000011013
Bram Moolenaar4770d092006-01-12 23:22:24 +000011014 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11015 compflags[sp->ts_complen + 1] = NUL;
11016 vim_strncpy(preword + sp->ts_prewordlen,
11017 tword + sp->ts_splitoff,
11018 sp->ts_twordlen - sp->ts_splitoff);
11019 p = preword;
11020 while (*skiptowhite(p) != NUL)
11021 p = skipwhite(skiptowhite(p));
11022 if (fword_ends && !can_compound(slang, p,
11023 compflags + sp->ts_compsplit))
11024 /* Compound is not allowed. But it may still be
11025 * possible if we add another (short) word. */
11026 compound_ok = FALSE;
11027
11028 /* Get pointer to last char of previous word. */
11029 p = preword + sp->ts_prewordlen;
11030 mb_ptr_back(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011031 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011032 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011033
Bram Moolenaar4770d092006-01-12 23:22:24 +000011034 /*
11035 * Form the word with proper case in preword.
11036 * If there is a word from a previous split, append.
11037 * For the soundfold tree don't change the case, simply append.
11038 */
11039 if (soundfold)
11040 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
11041 else if (flags & WF_KEEPCAP)
11042 /* Must find the word in the keep-case tree. */
11043 find_keepcap_word(slang, tword + sp->ts_splitoff,
11044 preword + sp->ts_prewordlen);
11045 else
11046 {
11047 /* Include badflags: If the badword is onecap or allcap
11048 * use that for the goodword too. But if the badword is
11049 * allcap and it's only one char long use onecap. */
11050 c = su->su_badflags;
11051 if ((c & WF_ALLCAP)
11052#ifdef FEAT_MBYTE
11053 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
11054#else
11055 && su->su_badlen == 1
11056#endif
11057 )
11058 c = WF_ONECAP;
11059 c |= flags;
11060
11061 /* When appending a compound word after a word character don't
11062 * use Onecap. */
11063 if (p != NULL && spell_iswordp_nmw(p))
11064 c &= ~WF_ONECAP;
11065 make_case_word(tword + sp->ts_splitoff,
11066 preword + sp->ts_prewordlen, c);
11067 }
11068
11069 if (!soundfold)
11070 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011071 /* Don't use a banned word. It may appear again as a good
11072 * word, thus remember it. */
11073 if (flags & WF_BANNED)
11074 {
Bram Moolenaar5195e452005-08-19 20:32:47 +000011075 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011076 break;
11077 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011078 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +000011079 && WAS_BANNED(su, preword + sp->ts_prewordlen))
11080 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011081 {
11082 if (slang->sl_compprog == NULL)
11083 break;
11084 /* the word so far was banned but we may try compounding */
11085 goodword_ends = FALSE;
11086 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011087 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011088
Bram Moolenaar4770d092006-01-12 23:22:24 +000011089 newscore = 0;
11090 if (!soundfold) /* soundfold words don't have flags */
11091 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011092 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011093 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011094 newscore += SCORE_REGION;
11095 if (flags & WF_RARE)
11096 newscore += SCORE_RARE;
11097
Bram Moolenaar0c405862005-06-22 22:26:26 +000011098 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011099 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011100 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011101 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011102
Bram Moolenaar4770d092006-01-12 23:22:24 +000011103 /* TODO: how about splitting in the soundfold tree? */
11104 if (fword_ends
11105 && goodword_ends
11106 && sp->ts_fidx >= sp->ts_fidxtry
11107 && compound_ok)
11108 {
11109 /* The badword also ends: add suggestions. */
11110#ifdef DEBUG_TRIEWALK
11111 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011112 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011113 int j;
11114
11115 /* print the stack of changes that brought us here */
11116 smsg("------ %s -------", fword);
11117 for (j = 0; j < depth; ++j)
11118 smsg("%s", changename[j]);
11119 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011120#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011121 if (soundfold)
11122 {
11123 /* For soundfolded words we need to find the original
11124 * words, the edit distrance and then add them. */
11125 add_sound_suggest(su, preword, sp->ts_score, lp);
11126 }
11127 else
11128 {
11129 /* Give a penalty when changing non-word char to word
11130 * char, e.g., "thes," -> "these". */
11131 p = fword + sp->ts_fidx;
11132 mb_ptr_back(fword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011133 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011134 {
11135 p = preword + STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011136 mb_ptr_back(preword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011137 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011138 newscore += SCORE_NONWORD;
11139 }
11140
Bram Moolenaar4770d092006-01-12 23:22:24 +000011141 /* Give a bonus to words seen before. */
11142 score = score_wordcount_adj(slang,
11143 sp->ts_score + newscore,
11144 preword + sp->ts_prewordlen,
11145 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011146
Bram Moolenaar4770d092006-01-12 23:22:24 +000011147 /* Add the suggestion if the score isn't too bad. */
11148 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011149 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011150 add_suggestion(su, &su->su_ga, preword,
11151 sp->ts_fidx - repextra,
11152 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011153
11154 if (su->su_badflags & WF_MIXCAP)
11155 {
11156 /* We really don't know if the word should be
11157 * upper or lower case, add both. */
11158 c = captype(preword, NULL);
11159 if (c == 0 || c == WF_ALLCAP)
11160 {
11161 make_case_word(tword + sp->ts_splitoff,
11162 preword + sp->ts_prewordlen,
11163 c == 0 ? WF_ALLCAP : 0);
11164
11165 add_suggestion(su, &su->su_ga, preword,
11166 sp->ts_fidx - repextra,
11167 score + SCORE_ICASE, 0, FALSE,
11168 lp->lp_sallang, FALSE);
11169 }
11170 }
11171 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011172 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011173 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011174
Bram Moolenaar4770d092006-01-12 23:22:24 +000011175 /*
11176 * Try word split and/or compounding.
11177 */
11178 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +000011179#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011180 /* Don't split halfway a character. */
11181 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011182#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011183 )
11184 {
11185 int try_compound;
11186 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011187
Bram Moolenaar4770d092006-01-12 23:22:24 +000011188 /* If past the end of the bad word don't try a split.
11189 * Otherwise try changing the next word. E.g., find
11190 * suggestions for "the the" where the second "the" is
11191 * different. It's done like a split.
11192 * TODO: word split for soundfold words */
11193 try_split = (sp->ts_fidx - repextra < su->su_badlen)
11194 && !soundfold;
11195
11196 /* Get here in several situations:
11197 * 1. The word in the tree ends:
11198 * If the word allows compounding try that. Otherwise try
11199 * a split by inserting a space. For both check that a
11200 * valid words starts at fword[sp->ts_fidx].
11201 * For NOBREAK do like compounding to be able to check if
11202 * the next word is valid.
11203 * 2. The badword does end, but it was due to a change (e.g.,
11204 * a swap). No need to split, but do check that the
11205 * following word is valid.
11206 * 3. The badword and the word in the tree end. It may still
11207 * be possible to compound another (short) word.
11208 */
11209 try_compound = FALSE;
11210 if (!soundfold
11211 && slang->sl_compprog != NULL
11212 && ((unsigned)flags >> 24) != 0
11213 && sp->ts_twordlen - sp->ts_splitoff
11214 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011215#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011216 && (!has_mbyte
11217 || slang->sl_compminlen == 0
11218 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011219 >= slang->sl_compminlen)
11220#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011221 && (slang->sl_compsylmax < MAXWLEN
11222 || sp->ts_complen + 1 - sp->ts_compsplit
11223 < slang->sl_compmax)
11224 && (byte_in_str(sp->ts_complen == sp->ts_compsplit
11225 ? slang->sl_compstartflags
11226 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +000011227 ((unsigned)flags >> 24))))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011228 {
11229 try_compound = TRUE;
11230 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11231 compflags[sp->ts_complen + 1] = NUL;
11232 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011233
Bram Moolenaar4770d092006-01-12 23:22:24 +000011234 /* For NOBREAK we never try splitting, it won't make any word
11235 * valid. */
11236 if (slang->sl_nobreak)
11237 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +000011238
Bram Moolenaar4770d092006-01-12 23:22:24 +000011239 /* If we could add a compound word, and it's also possible to
11240 * split at this point, do the split first and set
11241 * TSF_DIDSPLIT to avoid doing it again. */
11242 else if (!fword_ends
11243 && try_compound
11244 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11245 {
11246 try_compound = FALSE;
11247 sp->ts_flags |= TSF_DIDSPLIT;
11248 --sp->ts_curi; /* do the same NUL again */
11249 compflags[sp->ts_complen] = NUL;
11250 }
11251 else
11252 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011253
Bram Moolenaar4770d092006-01-12 23:22:24 +000011254 if (try_split || try_compound)
11255 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011256 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +000011257 {
11258 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011259 * words so far are valid for compounding. If there
11260 * is only one word it must not have the NEEDCOMPOUND
11261 * flag. */
11262 if (sp->ts_complen == sp->ts_compsplit
11263 && (flags & WF_NEEDCOMP))
11264 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +000011265 p = preword;
11266 while (*skiptowhite(p) != NUL)
11267 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +000011268 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +000011269 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +000011270 compflags + sp->ts_compsplit))
11271 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011272
11273 if (slang->sl_nosplitsugs)
11274 newscore += SCORE_SPLIT_NO;
11275 else
11276 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011277
11278 /* Give a bonus to words seen before. */
11279 newscore = score_wordcount_adj(slang, newscore,
11280 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011281 }
11282
Bram Moolenaar4770d092006-01-12 23:22:24 +000011283 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011284 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011285 go_deeper(stack, depth, newscore);
11286#ifdef DEBUG_TRIEWALK
11287 if (!try_compound && !fword_ends)
11288 sprintf(changename[depth], "%.*s-%s: split",
11289 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11290 else
11291 sprintf(changename[depth], "%.*s-%s: compound",
11292 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11293#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011294 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000011295 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011296 sp->ts_state = STATE_SPLITUNDO;
11297
11298 ++depth;
11299 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011300
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011301 /* Append a space to preword when splitting. */
11302 if (!try_compound && !fword_ends)
11303 STRCAT(preword, " ");
Bram Moolenaar5195e452005-08-19 20:32:47 +000011304 sp->ts_prewordlen = STRLEN(preword);
11305 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +000011306 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011307
11308 /* If the badword has a non-word character at this
11309 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011310 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011311 * character when the word ends. But only when the
11312 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011313 if (((!try_compound && !spell_iswordp_nmw(fword
11314 + sp->ts_fidx))
11315 || fword_ends)
11316 && fword[sp->ts_fidx] != NUL
11317 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011318 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011319 int l;
11320
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011321#ifdef FEAT_MBYTE
11322 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011323 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011324 else
11325#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011326 l = 1;
11327 if (fword_ends)
11328 {
11329 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +000011330 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011331 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011332 sp->ts_prewordlen += l;
11333 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011334 }
11335 else
11336 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
11337 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011338 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011339
Bram Moolenaard12a1322005-08-21 22:08:24 +000011340 /* When compounding include compound flag in
11341 * compflags[] (already set above). When splitting we
11342 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011343 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +000011344 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011345 else
Bram Moolenaard12a1322005-08-21 22:08:24 +000011346 sp->ts_compsplit = sp->ts_complen;
11347 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011348
Bram Moolenaar53805d12005-08-01 07:08:33 +000011349 /* set su->su_badflags to the caps type at this
11350 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011351#ifdef FEAT_MBYTE
11352 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +000011353 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011354 else
11355#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +000011356 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011357 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011358 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011359
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011360 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011361 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011362
11363 /* If there are postponed prefixes, try these too. */
11364 if (pbyts != NULL)
11365 {
11366 byts = pbyts;
11367 idxs = pidxs;
11368 sp->ts_prefixdepth = PFD_PREFIXTREE;
11369 sp->ts_state = STATE_NOPREFIX;
11370 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011371 }
11372 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011373 }
11374 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011375
Bram Moolenaar4770d092006-01-12 23:22:24 +000011376 case STATE_SPLITUNDO:
11377 /* Undo the changes done for word split or compound word. */
11378 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011379
Bram Moolenaar4770d092006-01-12 23:22:24 +000011380 /* Continue looking for NUL bytes. */
11381 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011382
Bram Moolenaar4770d092006-01-12 23:22:24 +000011383 /* In case we went into the prefix tree. */
11384 byts = fbyts;
11385 idxs = fidxs;
11386 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011387
Bram Moolenaar4770d092006-01-12 23:22:24 +000011388 case STATE_ENDNUL:
11389 /* Past the NUL bytes in the node. */
11390 su->su_badflags = sp->ts_save_badflags;
11391 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011392#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011393 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011394#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011395 )
11396 {
11397 /* The badword ends, can't use STATE_PLAIN. */
11398 sp->ts_state = STATE_DEL;
11399 break;
11400 }
11401 sp->ts_state = STATE_PLAIN;
11402 /*FALLTHROUGH*/
11403
11404 case STATE_PLAIN:
11405 /*
11406 * Go over all possible bytes at this node, add each to tword[]
11407 * and use child node. "ts_curi" is the index.
11408 */
11409 arridx = sp->ts_arridx;
11410 if (sp->ts_curi > byts[arridx])
11411 {
11412 /* Done all bytes at this node, do next state. When still at
11413 * already changed bytes skip the other tricks. */
11414 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011415 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011416 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011417 sp->ts_state = STATE_FINAL;
11418 }
11419 else
11420 {
11421 arridx += sp->ts_curi++;
11422 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011423
Bram Moolenaar4770d092006-01-12 23:22:24 +000011424 /* Normal byte, go one level deeper. If it's not equal to the
11425 * byte in the bad word adjust the score. But don't even try
11426 * when the byte was already changed. And don't try when we
11427 * just deleted this byte, accepting it is always cheaper then
11428 * delete + substitute. */
11429 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +000011430#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011431 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011432#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011433 )
11434 newscore = 0;
11435 else
11436 newscore = SCORE_SUBST;
11437 if ((newscore == 0
11438 || (sp->ts_fidx >= sp->ts_fidxtry
11439 && ((sp->ts_flags & TSF_DIDDEL) == 0
11440 || c != fword[sp->ts_delidx])))
11441 && TRY_DEEPER(su, stack, depth, newscore))
11442 {
11443 go_deeper(stack, depth, newscore);
11444#ifdef DEBUG_TRIEWALK
11445 if (newscore > 0)
11446 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
11447 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11448 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011449 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011450 sprintf(changename[depth], "%.*s-%s: accept %c",
11451 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11452 fword[sp->ts_fidx]);
11453#endif
11454 ++depth;
11455 sp = &stack[depth];
11456 ++sp->ts_fidx;
11457 tword[sp->ts_twordlen++] = c;
11458 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +000011459#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011460 if (newscore == SCORE_SUBST)
11461 sp->ts_isdiff = DIFF_YES;
11462 if (has_mbyte)
11463 {
11464 /* Multi-byte characters are a bit complicated to
11465 * handle: They differ when any of the bytes differ
11466 * and then their length may also differ. */
11467 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011468 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011469 /* First byte. */
11470 sp->ts_tcharidx = 0;
11471 sp->ts_tcharlen = MB_BYTE2LEN(c);
11472 sp->ts_fcharstart = sp->ts_fidx - 1;
11473 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011474 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011475 }
11476 else if (sp->ts_isdiff == DIFF_INSERT)
11477 /* When inserting trail bytes don't advance in the
11478 * bad word. */
11479 --sp->ts_fidx;
11480 if (++sp->ts_tcharidx == sp->ts_tcharlen)
11481 {
11482 /* Last byte of character. */
11483 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +000011484 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011485 /* Correct ts_fidx for the byte length of the
11486 * character (we didn't check that before). */
11487 sp->ts_fidx = sp->ts_fcharstart
11488 + MB_BYTE2LEN(
Bram Moolenaarea424162005-06-16 21:51:00 +000011489 fword[sp->ts_fcharstart]);
11490
Bram Moolenaar4770d092006-01-12 23:22:24 +000011491 /* For changing a composing character adjust
11492 * the score from SCORE_SUBST to
11493 * SCORE_SUBCOMP. */
11494 if (enc_utf8
11495 && utf_iscomposing(
11496 mb_ptr2char(tword
11497 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011498 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011499 && utf_iscomposing(
11500 mb_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011501 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011502 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011503 SCORE_SUBST - SCORE_SUBCOMP;
11504
Bram Moolenaar4770d092006-01-12 23:22:24 +000011505 /* For a similar character adjust score from
11506 * SCORE_SUBST to SCORE_SIMILAR. */
11507 else if (!soundfold
11508 && slang->sl_has_map
11509 && similar_chars(slang,
11510 mb_ptr2char(tword
11511 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +000011512 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +000011513 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +000011514 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011515 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +000011516 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +000011517 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011518 else if (sp->ts_isdiff == DIFF_INSERT
11519 && sp->ts_twordlen > sp->ts_tcharlen)
11520 {
11521 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
11522 c = mb_ptr2char(p);
11523 if (enc_utf8 && utf_iscomposing(c))
11524 {
11525 /* Inserting a composing char doesn't
11526 * count that much. */
11527 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
11528 }
11529 else
11530 {
11531 /* If the previous character was the same,
11532 * thus doubling a character, give a bonus
11533 * to the score. Also for the soundfold
11534 * tree (might seem illogical but does
11535 * give better scores). */
11536 mb_ptr_back(tword, p);
11537 if (c == mb_ptr2char(p))
11538 sp->ts_score -= SCORE_INS
11539 - SCORE_INSDUP;
11540 }
11541 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011542
Bram Moolenaar4770d092006-01-12 23:22:24 +000011543 /* Starting a new char, reset the length. */
11544 sp->ts_tcharlen = 0;
11545 }
Bram Moolenaarea408852005-06-25 22:49:46 +000011546 }
Bram Moolenaarea424162005-06-16 21:51:00 +000011547 else
11548#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000011549 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011550 /* If we found a similar char adjust the score.
11551 * We do this after calling go_deeper() because
11552 * it's slow. */
11553 if (newscore != 0
11554 && !soundfold
11555 && slang->sl_has_map
11556 && similar_chars(slang,
11557 c, fword[sp->ts_fidx - 1]))
11558 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +000011559 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011560 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011561 }
11562 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011563
Bram Moolenaar4770d092006-01-12 23:22:24 +000011564 case STATE_DEL:
11565#ifdef FEAT_MBYTE
11566 /* When past the first byte of a multi-byte char don't try
11567 * delete/insert/swap a character. */
11568 if (has_mbyte && sp->ts_tcharlen > 0)
11569 {
11570 sp->ts_state = STATE_FINAL;
11571 break;
11572 }
11573#endif
11574 /*
11575 * Try skipping one character in the bad word (delete it).
11576 */
11577 sp->ts_state = STATE_INS_PREP;
11578 sp->ts_curi = 1;
11579 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
11580 /* Deleting a vowel at the start of a word counts less, see
11581 * soundalike_score(). */
11582 newscore = 2 * SCORE_DEL / 3;
11583 else
11584 newscore = SCORE_DEL;
11585 if (fword[sp->ts_fidx] != NUL
11586 && TRY_DEEPER(su, stack, depth, newscore))
11587 {
11588 go_deeper(stack, depth, newscore);
11589#ifdef DEBUG_TRIEWALK
11590 sprintf(changename[depth], "%.*s-%s: delete %c",
11591 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11592 fword[sp->ts_fidx]);
11593#endif
11594 ++depth;
11595
11596 /* Remember what character we deleted, so that we can avoid
11597 * inserting it again. */
11598 stack[depth].ts_flags |= TSF_DIDDEL;
11599 stack[depth].ts_delidx = sp->ts_fidx;
11600
11601 /* Advance over the character in fword[]. Give a bonus to the
11602 * score if the same character is following "nn" -> "n". It's
11603 * a bit illogical for soundfold tree but it does give better
11604 * results. */
11605#ifdef FEAT_MBYTE
11606 if (has_mbyte)
11607 {
11608 c = mb_ptr2char(fword + sp->ts_fidx);
11609 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
11610 if (enc_utf8 && utf_iscomposing(c))
11611 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
11612 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
11613 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
11614 }
11615 else
11616#endif
11617 {
11618 ++stack[depth].ts_fidx;
11619 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
11620 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
11621 }
11622 break;
11623 }
11624 /*FALLTHROUGH*/
11625
11626 case STATE_INS_PREP:
11627 if (sp->ts_flags & TSF_DIDDEL)
11628 {
11629 /* If we just deleted a byte then inserting won't make sense,
11630 * a substitute is always cheaper. */
11631 sp->ts_state = STATE_SWAP;
11632 break;
11633 }
11634
11635 /* skip over NUL bytes */
11636 n = sp->ts_arridx;
11637 for (;;)
11638 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011639 if (sp->ts_curi > byts[n])
11640 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011641 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011642 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011643 break;
11644 }
11645 if (byts[n + sp->ts_curi] != NUL)
11646 {
11647 /* Found a byte to insert. */
11648 sp->ts_state = STATE_INS;
11649 break;
11650 }
11651 ++sp->ts_curi;
11652 }
11653 break;
11654
11655 /*FALLTHROUGH*/
11656
11657 case STATE_INS:
11658 /* Insert one byte. Repeat this for each possible byte at this
11659 * node. */
11660 n = sp->ts_arridx;
11661 if (sp->ts_curi > byts[n])
11662 {
11663 /* Done all bytes at this node, go to next state. */
11664 sp->ts_state = STATE_SWAP;
11665 break;
11666 }
11667
11668 /* Do one more byte at this node, but:
11669 * - Skip NUL bytes.
11670 * - Skip the byte if it's equal to the byte in the word,
11671 * accepting that byte is always better.
11672 */
11673 n += sp->ts_curi++;
11674 c = byts[n];
11675 if (soundfold && sp->ts_twordlen == 0 && c == '*')
11676 /* Inserting a vowel at the start of a word counts less,
11677 * see soundalike_score(). */
11678 newscore = 2 * SCORE_INS / 3;
11679 else
11680 newscore = SCORE_INS;
11681 if (c != fword[sp->ts_fidx]
11682 && TRY_DEEPER(su, stack, depth, newscore))
11683 {
11684 go_deeper(stack, depth, newscore);
11685#ifdef DEBUG_TRIEWALK
11686 sprintf(changename[depth], "%.*s-%s: insert %c",
11687 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11688 c);
11689#endif
11690 ++depth;
11691 sp = &stack[depth];
11692 tword[sp->ts_twordlen++] = c;
11693 sp->ts_arridx = idxs[n];
11694#ifdef FEAT_MBYTE
11695 if (has_mbyte)
11696 {
11697 fl = MB_BYTE2LEN(c);
11698 if (fl > 1)
11699 {
11700 /* There are following bytes for the same character.
11701 * We must find all bytes before trying
11702 * delete/insert/swap/etc. */
11703 sp->ts_tcharlen = fl;
11704 sp->ts_tcharidx = 1;
11705 sp->ts_isdiff = DIFF_INSERT;
11706 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011707 }
11708 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011709 fl = 1;
11710 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000011711#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011712 {
11713 /* If the previous character was the same, thus doubling a
11714 * character, give a bonus to the score. Also for
11715 * soundfold words (illogical but does give a better
11716 * score). */
11717 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +000011718 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011719 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011720 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011721 }
11722 break;
11723
11724 case STATE_SWAP:
11725 /*
11726 * Swap two bytes in the bad word: "12" -> "21".
11727 * We change "fword" here, it's changed back afterwards at
11728 * STATE_UNSWAP.
11729 */
11730 p = fword + sp->ts_fidx;
11731 c = *p;
11732 if (c == NUL)
11733 {
11734 /* End of word, can't swap or replace. */
11735 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011736 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011737 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011738
Bram Moolenaar4770d092006-01-12 23:22:24 +000011739 /* Don't swap if the first character is not a word character.
11740 * SWAP3 etc. also don't make sense then. */
11741 if (!soundfold && !spell_iswordp(p, curbuf))
11742 {
11743 sp->ts_state = STATE_REP_INI;
11744 break;
11745 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000011746
Bram Moolenaar4770d092006-01-12 23:22:24 +000011747#ifdef FEAT_MBYTE
11748 if (has_mbyte)
11749 {
11750 n = mb_cptr2len(p);
11751 c = mb_ptr2char(p);
11752 if (!soundfold && !spell_iswordp(p + n, curbuf))
11753 c2 = c; /* don't swap non-word char */
11754 else
11755 c2 = mb_ptr2char(p + n);
11756 }
11757 else
11758#endif
11759 {
11760 if (!soundfold && !spell_iswordp(p + 1, curbuf))
11761 c2 = c; /* don't swap non-word char */
11762 else
11763 c2 = p[1];
11764 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000011765
Bram Moolenaar4770d092006-01-12 23:22:24 +000011766 /* When characters are identical, swap won't do anything.
11767 * Also get here if the second char is not a word character. */
11768 if (c == c2)
11769 {
11770 sp->ts_state = STATE_SWAP3;
11771 break;
11772 }
11773 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
11774 {
11775 go_deeper(stack, depth, SCORE_SWAP);
11776#ifdef DEBUG_TRIEWALK
11777 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
11778 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11779 c, c2);
11780#endif
11781 sp->ts_state = STATE_UNSWAP;
11782 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000011783#ifdef FEAT_MBYTE
11784 if (has_mbyte)
11785 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011786 fl = mb_char2len(c2);
11787 mch_memmove(p, p + n, fl);
11788 mb_char2bytes(c, p + fl);
11789 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000011790 }
11791 else
11792#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000011793 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011794 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +000011795 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011796 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +000011797 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011798 }
11799 else
11800 /* If this swap doesn't work then SWAP3 won't either. */
11801 sp->ts_state = STATE_REP_INI;
11802 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000011803
Bram Moolenaar4770d092006-01-12 23:22:24 +000011804 case STATE_UNSWAP:
11805 /* Undo the STATE_SWAP swap: "21" -> "12". */
11806 p = fword + sp->ts_fidx;
11807#ifdef FEAT_MBYTE
11808 if (has_mbyte)
11809 {
11810 n = MB_BYTE2LEN(*p);
11811 c = mb_ptr2char(p + n);
11812 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
11813 mb_char2bytes(c, p);
11814 }
11815 else
11816#endif
11817 {
11818 c = *p;
11819 *p = p[1];
11820 p[1] = c;
11821 }
11822 /*FALLTHROUGH*/
11823
11824 case STATE_SWAP3:
11825 /* Swap two bytes, skipping one: "123" -> "321". We change
11826 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
11827 p = fword + sp->ts_fidx;
11828#ifdef FEAT_MBYTE
11829 if (has_mbyte)
11830 {
11831 n = mb_cptr2len(p);
11832 c = mb_ptr2char(p);
11833 fl = mb_cptr2len(p + n);
11834 c2 = mb_ptr2char(p + n);
11835 if (!soundfold && !spell_iswordp(p + n + fl, curbuf))
11836 c3 = c; /* don't swap non-word char */
11837 else
11838 c3 = mb_ptr2char(p + n + fl);
11839 }
11840 else
11841#endif
11842 {
11843 c = *p;
11844 c2 = p[1];
11845 if (!soundfold && !spell_iswordp(p + 2, curbuf))
11846 c3 = c; /* don't swap non-word char */
11847 else
11848 c3 = p[2];
11849 }
11850
11851 /* When characters are identical: "121" then SWAP3 result is
11852 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
11853 * same as SWAP on next char: "112". Thus skip all swapping.
11854 * Also skip when c3 is NUL.
11855 * Also get here when the third character is not a word character.
11856 * Second character may any char: "a.b" -> "b.a" */
11857 if (c == c3 || c3 == NUL)
11858 {
11859 sp->ts_state = STATE_REP_INI;
11860 break;
11861 }
11862 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
11863 {
11864 go_deeper(stack, depth, SCORE_SWAP3);
11865#ifdef DEBUG_TRIEWALK
11866 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
11867 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11868 c, c3);
11869#endif
11870 sp->ts_state = STATE_UNSWAP3;
11871 ++depth;
11872#ifdef FEAT_MBYTE
11873 if (has_mbyte)
11874 {
11875 tl = mb_char2len(c3);
11876 mch_memmove(p, p + n + fl, tl);
11877 mb_char2bytes(c2, p + tl);
11878 mb_char2bytes(c, p + fl + tl);
11879 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
11880 }
11881 else
11882#endif
11883 {
11884 p[0] = p[2];
11885 p[2] = c;
11886 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
11887 }
11888 }
11889 else
11890 sp->ts_state = STATE_REP_INI;
11891 break;
11892
11893 case STATE_UNSWAP3:
11894 /* Undo STATE_SWAP3: "321" -> "123" */
11895 p = fword + sp->ts_fidx;
11896#ifdef FEAT_MBYTE
11897 if (has_mbyte)
11898 {
11899 n = MB_BYTE2LEN(*p);
11900 c2 = mb_ptr2char(p + n);
11901 fl = MB_BYTE2LEN(p[n]);
11902 c = mb_ptr2char(p + n + fl);
11903 tl = MB_BYTE2LEN(p[n + fl]);
11904 mch_memmove(p + fl + tl, p, n);
11905 mb_char2bytes(c, p);
11906 mb_char2bytes(c2, p + tl);
11907 p = p + tl;
11908 }
11909 else
11910#endif
11911 {
11912 c = *p;
11913 *p = p[2];
11914 p[2] = c;
11915 ++p;
11916 }
11917
11918 if (!soundfold && !spell_iswordp(p, curbuf))
11919 {
11920 /* Middle char is not a word char, skip the rotate. First and
11921 * third char were already checked at swap and swap3. */
11922 sp->ts_state = STATE_REP_INI;
11923 break;
11924 }
11925
11926 /* Rotate three characters left: "123" -> "231". We change
11927 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
11928 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
11929 {
11930 go_deeper(stack, depth, SCORE_SWAP3);
11931#ifdef DEBUG_TRIEWALK
11932 p = fword + sp->ts_fidx;
11933 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
11934 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11935 p[0], p[1], p[2]);
11936#endif
11937 sp->ts_state = STATE_UNROT3L;
11938 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000011939 p = fword + sp->ts_fidx;
11940#ifdef FEAT_MBYTE
11941 if (has_mbyte)
11942 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011943 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000011944 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011945 fl = mb_cptr2len(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011946 fl += mb_cptr2len(p + n + fl);
11947 mch_memmove(p, p + n, fl);
11948 mb_char2bytes(c, p + fl);
11949 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000011950 }
11951 else
11952#endif
11953 {
11954 c = *p;
11955 *p = p[1];
11956 p[1] = p[2];
11957 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011958 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +000011959 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011960 }
11961 else
11962 sp->ts_state = STATE_REP_INI;
11963 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011964
Bram Moolenaar4770d092006-01-12 23:22:24 +000011965 case STATE_UNROT3L:
11966 /* Undo ROT3L: "231" -> "123" */
11967 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +000011968#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011969 if (has_mbyte)
11970 {
11971 n = MB_BYTE2LEN(*p);
11972 n += MB_BYTE2LEN(p[n]);
11973 c = mb_ptr2char(p + n);
11974 tl = MB_BYTE2LEN(p[n]);
11975 mch_memmove(p + tl, p, n);
11976 mb_char2bytes(c, p);
11977 }
11978 else
Bram Moolenaarea424162005-06-16 21:51:00 +000011979#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011980 {
11981 c = p[2];
11982 p[2] = p[1];
11983 p[1] = *p;
11984 *p = c;
11985 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011986
Bram Moolenaar4770d092006-01-12 23:22:24 +000011987 /* Rotate three bytes right: "123" -> "312". We change "fword"
11988 * here, it's changed back afterwards at STATE_UNROT3R. */
11989 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
11990 {
11991 go_deeper(stack, depth, SCORE_SWAP3);
11992#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011993 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011994 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
11995 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11996 p[0], p[1], p[2]);
11997#endif
11998 sp->ts_state = STATE_UNROT3R;
11999 ++depth;
12000 p = fword + sp->ts_fidx;
12001#ifdef FEAT_MBYTE
12002 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000012003 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012004 n = mb_cptr2len(p);
12005 n += mb_cptr2len(p + n);
12006 c = mb_ptr2char(p + n);
12007 tl = mb_cptr2len(p + n);
12008 mch_memmove(p + tl, p, n);
12009 mb_char2bytes(c, p);
12010 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +000012011 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012012 else
12013#endif
12014 {
12015 c = p[2];
12016 p[2] = p[1];
12017 p[1] = *p;
12018 *p = c;
12019 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12020 }
12021 }
12022 else
12023 sp->ts_state = STATE_REP_INI;
12024 break;
12025
12026 case STATE_UNROT3R:
12027 /* Undo ROT3R: "312" -> "123" */
12028 p = fword + sp->ts_fidx;
12029#ifdef FEAT_MBYTE
12030 if (has_mbyte)
12031 {
12032 c = mb_ptr2char(p);
12033 tl = MB_BYTE2LEN(*p);
12034 n = MB_BYTE2LEN(p[tl]);
12035 n += MB_BYTE2LEN(p[tl + n]);
12036 mch_memmove(p, p + tl, n);
12037 mb_char2bytes(c, p + n);
12038 }
12039 else
12040#endif
12041 {
12042 c = *p;
12043 *p = p[1];
12044 p[1] = p[2];
12045 p[2] = c;
12046 }
12047 /*FALLTHROUGH*/
12048
12049 case STATE_REP_INI:
12050 /* Check if matching with REP items from the .aff file would work.
12051 * Quickly skip if:
12052 * - there are no REP items and we are not in the soundfold trie
12053 * - the score is going to be too high anyway
12054 * - already applied a REP item or swapped here */
12055 if ((lp->lp_replang == NULL && !soundfold)
12056 || sp->ts_score + SCORE_REP >= su->su_maxscore
12057 || sp->ts_fidx < sp->ts_fidxtry)
12058 {
12059 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012060 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012061 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012062
Bram Moolenaar4770d092006-01-12 23:22:24 +000012063 /* Use the first byte to quickly find the first entry that may
12064 * match. If the index is -1 there is none. */
12065 if (soundfold)
12066 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
12067 else
12068 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012069
Bram Moolenaar4770d092006-01-12 23:22:24 +000012070 if (sp->ts_curi < 0)
12071 {
12072 sp->ts_state = STATE_FINAL;
12073 break;
12074 }
12075
12076 sp->ts_state = STATE_REP;
12077 /*FALLTHROUGH*/
12078
12079 case STATE_REP:
12080 /* Try matching with REP items from the .aff file. For each match
12081 * replace the characters and check if the resulting word is
12082 * valid. */
12083 p = fword + sp->ts_fidx;
12084
12085 if (soundfold)
12086 gap = &slang->sl_repsal;
12087 else
12088 gap = &lp->lp_replang->sl_rep;
12089 while (sp->ts_curi < gap->ga_len)
12090 {
12091 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
12092 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012093 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012094 /* past possible matching entries */
12095 sp->ts_curi = gap->ga_len;
12096 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012097 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012098 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
12099 && TRY_DEEPER(su, stack, depth, SCORE_REP))
12100 {
12101 go_deeper(stack, depth, SCORE_REP);
12102#ifdef DEBUG_TRIEWALK
12103 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
12104 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12105 ftp->ft_from, ftp->ft_to);
12106#endif
12107 /* Need to undo this afterwards. */
12108 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012109
Bram Moolenaar4770d092006-01-12 23:22:24 +000012110 /* Change the "from" to the "to" string. */
12111 ++depth;
12112 fl = STRLEN(ftp->ft_from);
12113 tl = STRLEN(ftp->ft_to);
12114 if (fl != tl)
12115 {
12116 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
12117 repextra += tl - fl;
12118 }
12119 mch_memmove(p, ftp->ft_to, tl);
12120 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
12121#ifdef FEAT_MBYTE
12122 stack[depth].ts_tcharlen = 0;
12123#endif
12124 break;
12125 }
12126 }
12127
12128 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
12129 /* No (more) matches. */
12130 sp->ts_state = STATE_FINAL;
12131
12132 break;
12133
12134 case STATE_REP_UNDO:
12135 /* Undo a REP replacement and continue with the next one. */
12136 if (soundfold)
12137 gap = &slang->sl_repsal;
12138 else
12139 gap = &lp->lp_replang->sl_rep;
12140 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
12141 fl = STRLEN(ftp->ft_from);
12142 tl = STRLEN(ftp->ft_to);
12143 p = fword + sp->ts_fidx;
12144 if (fl != tl)
12145 {
12146 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
12147 repextra -= tl - fl;
12148 }
12149 mch_memmove(p, ftp->ft_from, fl);
12150 sp->ts_state = STATE_REP;
12151 break;
12152
12153 default:
12154 /* Did all possible states at this level, go up one level. */
12155 --depth;
12156
12157 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
12158 {
12159 /* Continue in or go back to the prefix tree. */
12160 byts = pbyts;
12161 idxs = pidxs;
12162 }
12163
12164 /* Don't check for CTRL-C too often, it takes time. */
12165 if (--breakcheckcount == 0)
12166 {
12167 ui_breakcheck();
12168 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012169 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012170 }
12171 }
12172}
12173
Bram Moolenaar4770d092006-01-12 23:22:24 +000012174
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012175/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012176 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012177 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012178 static void
12179go_deeper(stack, depth, score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012180 trystate_T *stack;
12181 int depth;
12182 int score_add;
12183{
Bram Moolenaarea424162005-06-16 21:51:00 +000012184 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012185 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012186 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012187 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000012188 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012189}
12190
Bram Moolenaar53805d12005-08-01 07:08:33 +000012191#ifdef FEAT_MBYTE
12192/*
12193 * Case-folding may change the number of bytes: Count nr of chars in
12194 * fword[flen] and return the byte length of that many chars in "word".
12195 */
12196 static int
12197nofold_len(fword, flen, word)
12198 char_u *fword;
12199 int flen;
12200 char_u *word;
12201{
12202 char_u *p;
12203 int i = 0;
12204
12205 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12206 ++i;
12207 for (p = word; i > 0; mb_ptr_adv(p))
12208 --i;
12209 return (int)(p - word);
12210}
12211#endif
12212
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012213/*
12214 * "fword" is a good word with case folded. Find the matching keep-case
12215 * words and put it in "kword".
12216 * Theoretically there could be several keep-case words that result in the
12217 * same case-folded word, but we only find one...
12218 */
12219 static void
12220find_keepcap_word(slang, fword, kword)
12221 slang_T *slang;
12222 char_u *fword;
12223 char_u *kword;
12224{
12225 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12226 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012227 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012228
12229 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012230 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012231 int round[MAXWLEN];
12232 int fwordidx[MAXWLEN];
12233 int uwordidx[MAXWLEN];
12234 int kwordlen[MAXWLEN];
12235
12236 int flen, ulen;
12237 int l;
12238 int len;
12239 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012240 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012241 char_u *p;
12242 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012243 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012244
12245 if (byts == NULL)
12246 {
12247 /* array is empty: "cannot happen" */
12248 *kword = NUL;
12249 return;
12250 }
12251
12252 /* Make an all-cap version of "fword". */
12253 allcap_copy(fword, uword);
12254
12255 /*
12256 * Each character needs to be tried both case-folded and upper-case.
12257 * All this gets very complicated if we keep in mind that changing case
12258 * may change the byte length of a multi-byte character...
12259 */
12260 depth = 0;
12261 arridx[0] = 0;
12262 round[0] = 0;
12263 fwordidx[0] = 0;
12264 uwordidx[0] = 0;
12265 kwordlen[0] = 0;
12266 while (depth >= 0)
12267 {
12268 if (fword[fwordidx[depth]] == NUL)
12269 {
12270 /* We are at the end of "fword". If the tree allows a word to end
12271 * here we have found a match. */
12272 if (byts[arridx[depth] + 1] == 0)
12273 {
12274 kword[kwordlen[depth]] = NUL;
12275 return;
12276 }
12277
12278 /* kword is getting too long, continue one level up */
12279 --depth;
12280 }
12281 else if (++round[depth] > 2)
12282 {
12283 /* tried both fold-case and upper-case character, continue one
12284 * level up */
12285 --depth;
12286 }
12287 else
12288 {
12289 /*
12290 * round[depth] == 1: Try using the folded-case character.
12291 * round[depth] == 2: Try using the upper-case character.
12292 */
12293#ifdef FEAT_MBYTE
12294 if (has_mbyte)
12295 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012296 flen = mb_cptr2len(fword + fwordidx[depth]);
12297 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012298 }
12299 else
12300#endif
12301 ulen = flen = 1;
12302 if (round[depth] == 1)
12303 {
12304 p = fword + fwordidx[depth];
12305 l = flen;
12306 }
12307 else
12308 {
12309 p = uword + uwordidx[depth];
12310 l = ulen;
12311 }
12312
12313 for (tryidx = arridx[depth]; l > 0; --l)
12314 {
12315 /* Perform a binary search in the list of accepted bytes. */
12316 len = byts[tryidx++];
12317 c = *p++;
12318 lo = tryidx;
12319 hi = tryidx + len - 1;
12320 while (lo < hi)
12321 {
12322 m = (lo + hi) / 2;
12323 if (byts[m] > c)
12324 hi = m - 1;
12325 else if (byts[m] < c)
12326 lo = m + 1;
12327 else
12328 {
12329 lo = hi = m;
12330 break;
12331 }
12332 }
12333
12334 /* Stop if there is no matching byte. */
12335 if (hi < lo || byts[lo] != c)
12336 break;
12337
12338 /* Continue at the child (if there is one). */
12339 tryidx = idxs[lo];
12340 }
12341
12342 if (l == 0)
12343 {
12344 /*
12345 * Found the matching char. Copy it to "kword" and go a
12346 * level deeper.
12347 */
12348 if (round[depth] == 1)
12349 {
12350 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
12351 flen);
12352 kwordlen[depth + 1] = kwordlen[depth] + flen;
12353 }
12354 else
12355 {
12356 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
12357 ulen);
12358 kwordlen[depth + 1] = kwordlen[depth] + ulen;
12359 }
12360 fwordidx[depth + 1] = fwordidx[depth] + flen;
12361 uwordidx[depth + 1] = uwordidx[depth] + ulen;
12362
12363 ++depth;
12364 arridx[depth] = tryidx;
12365 round[depth] = 0;
12366 }
12367 }
12368 }
12369
12370 /* Didn't find it: "cannot happen". */
12371 *kword = NUL;
12372}
12373
12374/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012375 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
12376 * su->su_sga.
12377 */
12378 static void
12379score_comp_sal(su)
12380 suginfo_T *su;
12381{
12382 langp_T *lp;
12383 char_u badsound[MAXWLEN];
12384 int i;
12385 suggest_T *stp;
12386 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012387 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012388 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012389
12390 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
12391 return;
12392
12393 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012394 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012395 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012396 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012397 if (lp->lp_slang->sl_sal.ga_len > 0)
12398 {
12399 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012400 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012401
12402 for (i = 0; i < su->su_ga.ga_len; ++i)
12403 {
12404 stp = &SUG(su->su_ga, i);
12405
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012406 /* Case-fold the suggested word, sound-fold it and compute the
12407 * sound-a-like score. */
12408 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012409 if (score < SCORE_MAXMAX)
12410 {
12411 /* Add the suggestion. */
12412 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
12413 sstp->st_word = vim_strsave(stp->st_word);
12414 if (sstp->st_word != NULL)
12415 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012416 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012417 sstp->st_score = score;
12418 sstp->st_altscore = 0;
12419 sstp->st_orglen = stp->st_orglen;
12420 ++su->su_sga.ga_len;
12421 }
12422 }
12423 }
12424 break;
12425 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012426 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012427}
12428
12429/*
12430 * Combine the list of suggestions in su->su_ga and su->su_sga.
12431 * They are intwined.
12432 */
12433 static void
12434score_combine(su)
12435 suginfo_T *su;
12436{
12437 int i;
12438 int j;
12439 garray_T ga;
12440 garray_T *gap;
12441 langp_T *lp;
12442 suggest_T *stp;
12443 char_u *p;
12444 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012445 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012446 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012447 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012448
12449 /* Add the alternate score to su_ga. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012450 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012451 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012452 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012453 if (lp->lp_slang->sl_sal.ga_len > 0)
12454 {
12455 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012456 slang = lp->lp_slang;
12457 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012458
12459 for (i = 0; i < su->su_ga.ga_len; ++i)
12460 {
12461 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012462 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012463 if (stp->st_altscore == SCORE_MAXMAX)
12464 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
12465 else
12466 stp->st_score = (stp->st_score * 3
12467 + stp->st_altscore) / 4;
12468 stp->st_salscore = FALSE;
12469 }
12470 break;
12471 }
12472 }
12473
Bram Moolenaar4770d092006-01-12 23:22:24 +000012474 if (slang == NULL) /* just in case */
12475 return;
12476
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012477 /* Add the alternate score to su_sga. */
12478 for (i = 0; i < su->su_sga.ga_len; ++i)
12479 {
12480 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012481 stp->st_altscore = spell_edit_score(slang,
12482 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012483 if (stp->st_score == SCORE_MAXMAX)
12484 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
12485 else
12486 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
12487 stp->st_salscore = TRUE;
12488 }
12489
Bram Moolenaar4770d092006-01-12 23:22:24 +000012490 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
12491 * for both lists. */
12492 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012493 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012494 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012495 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
12496
12497 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
12498 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
12499 return;
12500
12501 stp = &SUG(ga, 0);
12502 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
12503 {
12504 /* round 1: get a suggestion from su_ga
12505 * round 2: get a suggestion from su_sga */
12506 for (round = 1; round <= 2; ++round)
12507 {
12508 gap = round == 1 ? &su->su_ga : &su->su_sga;
12509 if (i < gap->ga_len)
12510 {
12511 /* Don't add a word if it's already there. */
12512 p = SUG(*gap, i).st_word;
12513 for (j = 0; j < ga.ga_len; ++j)
12514 if (STRCMP(stp[j].st_word, p) == 0)
12515 break;
12516 if (j == ga.ga_len)
12517 stp[ga.ga_len++] = SUG(*gap, i);
12518 else
12519 vim_free(p);
12520 }
12521 }
12522 }
12523
12524 ga_clear(&su->su_ga);
12525 ga_clear(&su->su_sga);
12526
12527 /* Truncate the list to the number of suggestions that will be displayed. */
12528 if (ga.ga_len > su->su_maxcount)
12529 {
12530 for (i = su->su_maxcount; i < ga.ga_len; ++i)
12531 vim_free(stp[i].st_word);
12532 ga.ga_len = su->su_maxcount;
12533 }
12534
12535 su->su_ga = ga;
12536}
12537
12538/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012539 * For the goodword in "stp" compute the soundalike score compared to the
12540 * badword.
12541 */
12542 static int
12543stp_sal_score(stp, su, slang, badsound)
12544 suggest_T *stp;
12545 suginfo_T *su;
12546 slang_T *slang;
12547 char_u *badsound; /* sound-folded badword */
12548{
12549 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012550 char_u *pbad;
12551 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012552 char_u badsound2[MAXWLEN];
12553 char_u fword[MAXWLEN];
12554 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012555 char_u goodword[MAXWLEN];
12556 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012557
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012558 lendiff = (int)(su->su_badlen - stp->st_orglen);
12559 if (lendiff >= 0)
12560 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012561 else
12562 {
12563 /* soundfold the bad word with more characters following */
12564 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
12565
12566 /* When joining two words the sound often changes a lot. E.g., "t he"
12567 * sounds like "t h" while "the" sounds like "@". Avoid that by
12568 * removing the space. Don't do it when the good word also contains a
12569 * space. */
12570 if (vim_iswhite(su->su_badptr[su->su_badlen])
12571 && *skiptowhite(stp->st_word) == NUL)
12572 for (p = fword; *(p = skiptowhite(p)) != NUL; )
12573 mch_memmove(p, p + 1, STRLEN(p));
12574
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012575 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012576 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012577 }
12578
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012579 if (lendiff > 0)
12580 {
12581 /* Add part of the bad word to the good word, so that we soundfold
12582 * what replaces the bad word. */
12583 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012584 vim_strncpy(goodword + stp->st_wordlen,
12585 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012586 pgood = goodword;
12587 }
12588 else
12589 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012590
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012591 /* Sound-fold the word and compute the score for the difference. */
12592 spell_soundfold(slang, pgood, FALSE, goodsound);
12593
12594 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012595}
12596
Bram Moolenaar4770d092006-01-12 23:22:24 +000012597/* structure used to store soundfolded words that add_sound_suggest() has
12598 * handled already. */
12599typedef struct
12600{
12601 short sft_score; /* lowest score used */
12602 char_u sft_word[1]; /* soundfolded word, actually longer */
12603} sftword_T;
12604
12605static sftword_T dumsft;
12606#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
12607#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
12608
12609/*
12610 * Prepare for calling suggest_try_soundalike().
12611 */
12612 static void
12613suggest_try_soundalike_prep()
12614{
12615 langp_T *lp;
12616 int lpi;
12617 slang_T *slang;
12618
12619 /* Do this for all languages that support sound folding and for which a
12620 * .sug file has been loaded. */
12621 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
12622 {
12623 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
12624 slang = lp->lp_slang;
12625 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
12626 /* prepare the hashtable used by add_sound_suggest() */
12627 hash_init(&slang->sl_sounddone);
12628 }
12629}
12630
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012631/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012632 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012633 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012634 */
12635 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000012636suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012637 suginfo_T *su;
12638{
12639 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012640 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012641 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012642 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012643
Bram Moolenaar4770d092006-01-12 23:22:24 +000012644 /* Do this for all languages that support sound folding and for which a
12645 * .sug file has been loaded. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012646 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012647 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012648 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
12649 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012650 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012651 {
12652 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012653 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012654
Bram Moolenaar4770d092006-01-12 23:22:24 +000012655 /* try all kinds of inserts/deletes/swaps/etc. */
12656 /* TODO: also soundfold the next words, so that we can try joining
12657 * and splitting */
12658 suggest_trie_walk(su, lp, salword, TRUE);
12659 }
12660 }
12661}
12662
12663/*
12664 * Finish up after calling suggest_try_soundalike().
12665 */
12666 static void
12667suggest_try_soundalike_finish()
12668{
12669 langp_T *lp;
12670 int lpi;
12671 slang_T *slang;
12672 int todo;
12673 hashitem_T *hi;
12674
12675 /* Do this for all languages that support sound folding and for which a
12676 * .sug file has been loaded. */
12677 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
12678 {
12679 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
12680 slang = lp->lp_slang;
12681 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
12682 {
12683 /* Free the info about handled words. */
12684 todo = slang->sl_sounddone.ht_used;
12685 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
12686 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012687 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012688 vim_free(HI2SFT(hi));
12689 --todo;
12690 }
12691 hash_clear(&slang->sl_sounddone);
12692 }
12693 }
12694}
12695
12696/*
12697 * A match with a soundfolded word is found. Add the good word(s) that
12698 * produce this soundfolded word.
12699 */
12700 static void
12701add_sound_suggest(su, goodword, score, lp)
12702 suginfo_T *su;
12703 char_u *goodword;
12704 int score; /* soundfold score */
12705 langp_T *lp;
12706{
12707 slang_T *slang = lp->lp_slang; /* language for sound folding */
12708 int sfwordnr;
12709 char_u *nrline;
12710 int orgnr;
12711 char_u theword[MAXWLEN];
12712 int i;
12713 int wlen;
12714 char_u *byts;
12715 idx_T *idxs;
12716 int n;
12717 int wordcount;
12718 int wc;
12719 int goodscore;
12720 hash_T hash;
12721 hashitem_T *hi;
12722 sftword_T *sft;
12723 int bc, gc;
12724 int limit;
12725
12726 /*
12727 * It's very well possible that the same soundfold word is found several
12728 * times with different scores. Since the following is quite slow only do
12729 * the words that have a better score than before. Use a hashtable to
12730 * remember the words that have been done.
12731 */
12732 hash = hash_hash(goodword);
12733 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
12734 if (HASHITEM_EMPTY(hi))
12735 {
12736 sft = (sftword_T *)alloc(sizeof(sftword_T) + STRLEN(goodword));
12737 if (sft != NULL)
12738 {
12739 sft->sft_score = score;
12740 STRCPY(sft->sft_word, goodword);
12741 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
12742 }
12743 }
12744 else
12745 {
12746 sft = HI2SFT(hi);
12747 if (score >= sft->sft_score)
12748 return;
12749 sft->sft_score = score;
12750 }
12751
12752 /*
12753 * Find the word nr in the soundfold tree.
12754 */
12755 sfwordnr = soundfold_find(slang, goodword);
12756 if (sfwordnr < 0)
12757 {
12758 EMSG2(_(e_intern2), "add_sound_suggest()");
12759 return;
12760 }
12761
12762 /*
12763 * go over the list of good words that produce this soundfold word
12764 */
12765 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
12766 orgnr = 0;
12767 while (*nrline != NUL)
12768 {
12769 /* The wordnr was stored in a minimal nr of bytes as an offset to the
12770 * previous wordnr. */
12771 orgnr += bytes2offset(&nrline);
12772
12773 byts = slang->sl_fbyts;
12774 idxs = slang->sl_fidxs;
12775
12776 /* Lookup the word "orgnr" one of the two tries. */
12777 n = 0;
12778 wlen = 0;
12779 wordcount = 0;
12780 for (;;)
12781 {
12782 i = 1;
12783 if (wordcount == orgnr && byts[n + 1] == NUL)
12784 break; /* found end of word */
12785
12786 if (byts[n + 1] == NUL)
12787 ++wordcount;
12788
12789 /* skip over the NUL bytes */
12790 for ( ; byts[n + i] == NUL; ++i)
12791 if (i > byts[n]) /* safety check */
12792 {
12793 STRCPY(theword + wlen, "BAD");
12794 goto badword;
12795 }
12796
12797 /* One of the siblings must have the word. */
12798 for ( ; i < byts[n]; ++i)
12799 {
12800 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
12801 if (wordcount + wc > orgnr)
12802 break;
12803 wordcount += wc;
12804 }
12805
12806 theword[wlen++] = byts[n + i];
12807 n = idxs[n + i];
12808 }
12809badword:
12810 theword[wlen] = NUL;
12811
12812 /* Go over the possible flags and regions. */
12813 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
12814 {
12815 char_u cword[MAXWLEN];
12816 char_u *p;
12817 int flags = (int)idxs[n + i];
12818
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012819 /* Skip words with the NOSUGGEST flag */
12820 if (flags & WF_NOSUGGEST)
12821 continue;
12822
Bram Moolenaar4770d092006-01-12 23:22:24 +000012823 if (flags & WF_KEEPCAP)
12824 {
12825 /* Must find the word in the keep-case tree. */
12826 find_keepcap_word(slang, theword, cword);
12827 p = cword;
12828 }
12829 else
12830 {
12831 flags |= su->su_badflags;
12832 if ((flags & WF_CAPMASK) != 0)
12833 {
12834 /* Need to fix case according to "flags". */
12835 make_case_word(theword, cword, flags);
12836 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012837 }
12838 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012839 p = theword;
12840 }
12841
12842 /* Add the suggestion. */
12843 if (sps_flags & SPS_DOUBLE)
12844 {
12845 /* Add the suggestion if the score isn't too bad. */
12846 if (score <= su->su_maxscore)
12847 add_suggestion(su, &su->su_sga, p, su->su_badlen,
12848 score, 0, FALSE, slang, FALSE);
12849 }
12850 else
12851 {
12852 /* Add a penalty for words in another region. */
12853 if ((flags & WF_REGION)
12854 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
12855 goodscore = SCORE_REGION;
12856 else
12857 goodscore = 0;
12858
12859 /* Add a small penalty for changing the first letter from
12860 * lower to upper case. Helps for "tath" -> "Kath", which is
12861 * less common thatn "tath" -> "path". Don't do it when the
12862 * letter is the same, that has already been counted. */
12863 gc = PTR2CHAR(p);
12864 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012865 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012866 bc = PTR2CHAR(su->su_badword);
12867 if (!SPELL_ISUPPER(bc)
12868 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
12869 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012870 }
12871
Bram Moolenaar4770d092006-01-12 23:22:24 +000012872 /* Compute the score for the good word. This only does letter
12873 * insert/delete/swap/replace. REP items are not considered,
12874 * which may make the score a bit higher.
12875 * Use a limit for the score to make it work faster. Use
12876 * MAXSCORE(), because RESCORE() will change the score.
12877 * If the limit is very high then the iterative method is
12878 * inefficient, using an array is quicker. */
12879 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
12880 if (limit > SCORE_LIMITMAX)
12881 goodscore += spell_edit_score(slang, su->su_badword, p);
12882 else
12883 goodscore += spell_edit_score_limit(slang, su->su_badword,
12884 p, limit);
12885
12886 /* When going over the limit don't bother to do the rest. */
12887 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012888 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012889 /* Give a bonus to words seen before. */
12890 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012891
Bram Moolenaar4770d092006-01-12 23:22:24 +000012892 /* Add the suggestion if the score isn't too bad. */
12893 goodscore = RESCORE(goodscore, score);
12894 if (goodscore <= su->su_sfmaxscore)
12895 add_suggestion(su, &su->su_ga, p, su->su_badlen,
12896 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012897 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012898 }
12899 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012900 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012901 }
12902}
12903
12904/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012905 * Find word "word" in fold-case tree for "slang" and return the word number.
12906 */
12907 static int
12908soundfold_find(slang, word)
12909 slang_T *slang;
12910 char_u *word;
12911{
12912 idx_T arridx = 0;
12913 int len;
12914 int wlen = 0;
12915 int c;
12916 char_u *ptr = word;
12917 char_u *byts;
12918 idx_T *idxs;
12919 int wordnr = 0;
12920
12921 byts = slang->sl_sbyts;
12922 idxs = slang->sl_sidxs;
12923
12924 for (;;)
12925 {
12926 /* First byte is the number of possible bytes. */
12927 len = byts[arridx++];
12928
12929 /* If the first possible byte is a zero the word could end here.
12930 * If the word ends we found the word. If not skip the NUL bytes. */
12931 c = ptr[wlen];
12932 if (byts[arridx] == NUL)
12933 {
12934 if (c == NUL)
12935 break;
12936
12937 /* Skip over the zeros, there can be several. */
12938 while (len > 0 && byts[arridx] == NUL)
12939 {
12940 ++arridx;
12941 --len;
12942 }
12943 if (len == 0)
12944 return -1; /* no children, word should have ended here */
12945 ++wordnr;
12946 }
12947
12948 /* If the word ends we didn't find it. */
12949 if (c == NUL)
12950 return -1;
12951
12952 /* Perform a binary search in the list of accepted bytes. */
12953 if (c == TAB) /* <Tab> is handled like <Space> */
12954 c = ' ';
12955 while (byts[arridx] < c)
12956 {
12957 /* The word count is in the first idxs[] entry of the child. */
12958 wordnr += idxs[idxs[arridx]];
12959 ++arridx;
12960 if (--len == 0) /* end of the bytes, didn't find it */
12961 return -1;
12962 }
12963 if (byts[arridx] != c) /* didn't find the byte */
12964 return -1;
12965
12966 /* Continue at the child (if there is one). */
12967 arridx = idxs[arridx];
12968 ++wlen;
12969
12970 /* One space in the good word may stand for several spaces in the
12971 * checked word. */
12972 if (c == ' ')
12973 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
12974 ++wlen;
12975 }
12976
12977 return wordnr;
12978}
12979
12980/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012981 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012982 */
12983 static void
12984make_case_word(fword, cword, flags)
12985 char_u *fword;
12986 char_u *cword;
12987 int flags;
12988{
12989 if (flags & WF_ALLCAP)
12990 /* Make it all upper-case */
12991 allcap_copy(fword, cword);
12992 else if (flags & WF_ONECAP)
12993 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012994 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012995 else
12996 /* Use goodword as-is. */
12997 STRCPY(cword, fword);
12998}
12999
Bram Moolenaarea424162005-06-16 21:51:00 +000013000/*
13001 * Use map string "map" for languages "lp".
13002 */
13003 static void
13004set_map_str(lp, map)
13005 slang_T *lp;
13006 char_u *map;
13007{
13008 char_u *p;
13009 int headc = 0;
13010 int c;
13011 int i;
13012
13013 if (*map == NUL)
13014 {
13015 lp->sl_has_map = FALSE;
13016 return;
13017 }
13018 lp->sl_has_map = TRUE;
13019
Bram Moolenaar4770d092006-01-12 23:22:24 +000013020 /* Init the array and hash tables empty. */
Bram Moolenaarea424162005-06-16 21:51:00 +000013021 for (i = 0; i < 256; ++i)
13022 lp->sl_map_array[i] = 0;
13023#ifdef FEAT_MBYTE
13024 hash_init(&lp->sl_map_hash);
13025#endif
13026
13027 /*
13028 * The similar characters are stored separated with slashes:
13029 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
13030 * before the same slash. For characters above 255 sl_map_hash is used.
13031 */
13032 for (p = map; *p != NUL; )
13033 {
13034#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013035 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000013036#else
13037 c = *p++;
13038#endif
13039 if (c == '/')
13040 headc = 0;
13041 else
13042 {
13043 if (headc == 0)
13044 headc = c;
13045
13046#ifdef FEAT_MBYTE
13047 /* Characters above 255 don't fit in sl_map_array[], put them in
13048 * the hash table. Each entry is the char, a NUL the headchar and
13049 * a NUL. */
13050 if (c >= 256)
13051 {
13052 int cl = mb_char2len(c);
13053 int headcl = mb_char2len(headc);
13054 char_u *b;
13055 hash_T hash;
13056 hashitem_T *hi;
13057
13058 b = alloc((unsigned)(cl + headcl + 2));
13059 if (b == NULL)
13060 return;
13061 mb_char2bytes(c, b);
13062 b[cl] = NUL;
13063 mb_char2bytes(headc, b + cl + 1);
13064 b[cl + 1 + headcl] = NUL;
13065 hash = hash_hash(b);
13066 hi = hash_lookup(&lp->sl_map_hash, b, hash);
13067 if (HASHITEM_EMPTY(hi))
13068 hash_add_item(&lp->sl_map_hash, hi, b, hash);
13069 else
13070 {
13071 /* This should have been checked when generating the .spl
13072 * file. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013073 EMSG(_("E783: duplicate char in MAP entry"));
Bram Moolenaarea424162005-06-16 21:51:00 +000013074 vim_free(b);
13075 }
13076 }
13077 else
13078#endif
13079 lp->sl_map_array[c] = headc;
13080 }
13081 }
13082}
13083
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013084/*
13085 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
13086 * lines in the .aff file.
13087 */
13088 static int
13089similar_chars(slang, c1, c2)
13090 slang_T *slang;
13091 int c1;
13092 int c2;
13093{
Bram Moolenaarea424162005-06-16 21:51:00 +000013094 int m1, m2;
13095#ifdef FEAT_MBYTE
13096 char_u buf[MB_MAXBYTES];
13097 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013098
Bram Moolenaarea424162005-06-16 21:51:00 +000013099 if (c1 >= 256)
13100 {
13101 buf[mb_char2bytes(c1, buf)] = 0;
13102 hi = hash_find(&slang->sl_map_hash, buf);
13103 if (HASHITEM_EMPTY(hi))
13104 m1 = 0;
13105 else
13106 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13107 }
13108 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013109#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000013110 m1 = slang->sl_map_array[c1];
13111 if (m1 == 0)
13112 return FALSE;
13113
13114
13115#ifdef FEAT_MBYTE
13116 if (c2 >= 256)
13117 {
13118 buf[mb_char2bytes(c2, buf)] = 0;
13119 hi = hash_find(&slang->sl_map_hash, buf);
13120 if (HASHITEM_EMPTY(hi))
13121 m2 = 0;
13122 else
13123 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13124 }
13125 else
13126#endif
13127 m2 = slang->sl_map_array[c2];
13128
13129 return m1 == m2;
13130}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013131
13132/*
13133 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +000013134 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013135 */
13136 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000013137add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus,
13138 slang, maxsf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013139 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013140 garray_T *gap; /* either su_ga or su_sga */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013141 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013142 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013143 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013144 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013145 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013146 slang_T *slang; /* language for sound folding */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013147 int maxsf; /* su_maxscore applies to soundfold score,
13148 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013149{
Bram Moolenaar4770d092006-01-12 23:22:24 +000013150 int goodlen; /* len of goodword changed */
13151 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013152 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013153 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013154 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013155 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013156
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013157 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13158 * "thee the" is added next to changing the first "the" the "thee". */
13159 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013160 pbad = su->su_badptr + badlenarg;
13161 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013162 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013163 goodlen = pgood - goodword;
13164 badlen = pbad - su->su_badptr;
13165 if (goodlen <= 0 || badlen <= 0)
13166 break;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013167 mb_ptr_back(goodword, pgood);
13168 mb_ptr_back(su->su_badptr, pbad);
13169#ifdef FEAT_MBYTE
13170 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013171 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013172 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
13173 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013174 }
13175 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013176#endif
13177 if (*pgood != *pbad)
13178 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013179 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013180
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013181 if (badlen == 0 && goodlen == 0)
13182 /* goodword doesn't change anything; may happen for "the the" changing
13183 * the first "the" to itself. */
13184 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013185
Bram Moolenaar4770d092006-01-12 23:22:24 +000013186 /* Check if the word is already there. Also check the length that is
13187 * being replaced "thes," -> "these" is a different suggestion from
13188 * "thes" -> "these". */
13189 stp = &SUG(*gap, 0);
13190 for (i = gap->ga_len; --i >= 0; ++stp)
13191 if (stp->st_wordlen == goodlen
13192 && stp->st_orglen == badlen
13193 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
13194 {
13195 /*
13196 * Found it. Remember the word with the lowest score.
13197 */
13198 if (stp->st_slang == NULL)
13199 stp->st_slang = slang;
13200
13201 new_sug.st_score = score;
13202 new_sug.st_altscore = altscore;
13203 new_sug.st_had_bonus = had_bonus;
13204
13205 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013206 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013207 /* Only one of the two had the soundalike score computed.
13208 * Need to do that for the other one now, otherwise the
13209 * scores can't be compared. This happens because
13210 * suggest_try_change() doesn't compute the soundalike
13211 * word to keep it fast, while some special methods set
13212 * the soundalike score to zero. */
13213 if (had_bonus)
13214 rescore_one(su, stp);
13215 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013216 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013217 new_sug.st_word = stp->st_word;
13218 new_sug.st_wordlen = stp->st_wordlen;
13219 new_sug.st_slang = stp->st_slang;
13220 new_sug.st_orglen = badlen;
13221 rescore_one(su, &new_sug);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013222 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013223 }
13224
Bram Moolenaar4770d092006-01-12 23:22:24 +000013225 if (stp->st_score > new_sug.st_score)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013226 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013227 stp->st_score = new_sug.st_score;
13228 stp->st_altscore = new_sug.st_altscore;
13229 stp->st_had_bonus = new_sug.st_had_bonus;
13230 }
13231 break;
13232 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013233
Bram Moolenaar4770d092006-01-12 23:22:24 +000013234 if (i < 0 && ga_grow(gap, 1) == OK)
13235 {
13236 /* Add a suggestion. */
13237 stp = &SUG(*gap, gap->ga_len);
13238 stp->st_word = vim_strnsave(goodword, goodlen);
13239 if (stp->st_word != NULL)
13240 {
13241 stp->st_wordlen = goodlen;
13242 stp->st_score = score;
13243 stp->st_altscore = altscore;
13244 stp->st_had_bonus = had_bonus;
13245 stp->st_orglen = badlen;
13246 stp->st_slang = slang;
13247 ++gap->ga_len;
13248
13249 /* If we have too many suggestions now, sort the list and keep
13250 * the best suggestions. */
13251 if (gap->ga_len > SUG_MAX_COUNT(su))
13252 {
13253 if (maxsf)
13254 su->su_sfmaxscore = cleanup_suggestions(gap,
13255 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
13256 else
13257 {
13258 i = su->su_maxscore;
13259 su->su_maxscore = cleanup_suggestions(gap,
13260 su->su_maxscore, SUG_CLEAN_COUNT(su));
13261 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013262 }
13263 }
13264 }
13265}
13266
13267/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013268 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13269 * for split words, such as "the the". Remove these from the list here.
13270 */
13271 static void
13272check_suggestions(su, gap)
13273 suginfo_T *su;
13274 garray_T *gap; /* either su_ga or su_sga */
13275{
13276 suggest_T *stp;
13277 int i;
13278 char_u longword[MAXWLEN + 1];
13279 int len;
13280 hlf_T attr;
13281
13282 stp = &SUG(*gap, 0);
13283 for (i = gap->ga_len - 1; i >= 0; --i)
13284 {
13285 /* Need to append what follows to check for "the the". */
13286 STRCPY(longword, stp[i].st_word);
13287 len = stp[i].st_wordlen;
13288 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
13289 MAXWLEN - len);
13290 attr = HLF_COUNT;
13291 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
13292 if (attr != HLF_COUNT)
13293 {
13294 /* Remove this entry. */
13295 vim_free(stp[i].st_word);
13296 --gap->ga_len;
13297 if (i < gap->ga_len)
13298 mch_memmove(stp + i, stp + i + 1,
13299 sizeof(suggest_T) * (gap->ga_len - i));
13300 }
13301 }
13302}
13303
13304
13305/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013306 * Add a word to be banned.
13307 */
13308 static void
13309add_banned(su, word)
13310 suginfo_T *su;
13311 char_u *word;
13312{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013313 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013314 hash_T hash;
13315 hashitem_T *hi;
13316
Bram Moolenaar4770d092006-01-12 23:22:24 +000013317 hash = hash_hash(word);
13318 hi = hash_lookup(&su->su_banned, word, hash);
13319 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013320 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013321 s = vim_strsave(word);
13322 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013323 hash_add_item(&su->su_banned, hi, s, hash);
13324 }
13325}
13326
13327/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013328 * Recompute the score for all suggestions if sound-folding is possible. This
13329 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013330 */
13331 static void
13332rescore_suggestions(su)
13333 suginfo_T *su;
13334{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013335 int i;
13336
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013337 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013338 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013339 rescore_one(su, &SUG(su->su_ga, i));
13340}
13341
13342/*
13343 * Recompute the score for one suggestion if sound-folding is possible.
13344 */
13345 static void
13346rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013347 suginfo_T *su;
13348 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013349{
13350 slang_T *slang = stp->st_slang;
13351 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000013352 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013353
13354 /* Only rescore suggestions that have no sal score yet and do have a
13355 * language. */
13356 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
13357 {
13358 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013359 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013360 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013361 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013362 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000013363 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013364 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000013365
13366 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013367 if (stp->st_altscore == SCORE_MAXMAX)
13368 stp->st_altscore = SCORE_BIG;
13369 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
13370 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013371 }
13372}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013373
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013374static int
13375#ifdef __BORLANDC__
13376_RTLENTRYF
13377#endif
13378sug_compare __ARGS((const void *s1, const void *s2));
13379
13380/*
13381 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013382 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013383 */
13384 static int
13385#ifdef __BORLANDC__
13386_RTLENTRYF
13387#endif
13388sug_compare(s1, s2)
13389 const void *s1;
13390 const void *s2;
13391{
13392 suggest_T *p1 = (suggest_T *)s1;
13393 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013394 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013395
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013396 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013397 {
13398 n = p1->st_altscore - p2->st_altscore;
13399 if (n == 0)
13400 n = STRICMP(p1->st_word, p2->st_word);
13401 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013402 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013403}
13404
13405/*
13406 * Cleanup the suggestions:
13407 * - Sort on score.
13408 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013409 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013410 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013411 static int
13412cleanup_suggestions(gap, maxscore, keep)
13413 garray_T *gap;
13414 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013415 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013416{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013417 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013418 int i;
13419
13420 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013421 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013422
13423 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013424 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013425 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013426 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013427 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013428 gap->ga_len = keep;
13429 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013430 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013431 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013432}
13433
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013434#if defined(FEAT_EVAL) || defined(PROTO)
13435/*
13436 * Soundfold a string, for soundfold().
13437 * Result is in allocated memory, NULL for an error.
13438 */
13439 char_u *
13440eval_soundfold(word)
13441 char_u *word;
13442{
13443 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013444 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013445 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013446
13447 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13448 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013449 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013450 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013451 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013452 if (lp->lp_slang->sl_sal.ga_len > 0)
13453 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013454 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013455 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013456 return vim_strsave(sound);
13457 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013458 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013459
13460 /* No language with sound folding, return word as-is. */
13461 return vim_strsave(word);
13462}
13463#endif
13464
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013465/*
13466 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000013467 *
13468 * There are many ways to turn a word into a sound-a-like representation. The
13469 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
13470 * swedish name matching - survey and test of different algorithms" by Klas
13471 * Erikson.
13472 *
13473 * We support two methods:
13474 * 1. SOFOFROM/SOFOTO do a simple character mapping.
13475 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013476 */
13477 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013478spell_soundfold(slang, inword, folded, res)
13479 slang_T *slang;
13480 char_u *inword;
13481 int folded; /* "inword" is already case-folded */
13482 char_u *res;
13483{
13484 char_u fword[MAXWLEN];
13485 char_u *word;
13486
13487 if (slang->sl_sofo)
13488 /* SOFOFROM and SOFOTO used */
13489 spell_soundfold_sofo(slang, inword, res);
13490 else
13491 {
13492 /* SAL items used. Requires the word to be case-folded. */
13493 if (folded)
13494 word = inword;
13495 else
13496 {
13497 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
13498 word = fword;
13499 }
13500
13501#ifdef FEAT_MBYTE
13502 if (has_mbyte)
13503 spell_soundfold_wsal(slang, word, res);
13504 else
13505#endif
13506 spell_soundfold_sal(slang, word, res);
13507 }
13508}
13509
13510/*
13511 * Perform sound folding of "inword" into "res" according to SOFOFROM and
13512 * SOFOTO lines.
13513 */
13514 static void
13515spell_soundfold_sofo(slang, inword, res)
13516 slang_T *slang;
13517 char_u *inword;
13518 char_u *res;
13519{
13520 char_u *s;
13521 int ri = 0;
13522 int c;
13523
13524#ifdef FEAT_MBYTE
13525 if (has_mbyte)
13526 {
13527 int prevc = 0;
13528 int *ip;
13529
13530 /* The sl_sal_first[] table contains the translation for chars up to
13531 * 255, sl_sal the rest. */
13532 for (s = inword; *s != NUL; )
13533 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013534 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013535 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
13536 c = ' ';
13537 else if (c < 256)
13538 c = slang->sl_sal_first[c];
13539 else
13540 {
13541 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
13542 if (ip == NULL) /* empty list, can't match */
13543 c = NUL;
13544 else
13545 for (;;) /* find "c" in the list */
13546 {
13547 if (*ip == 0) /* not found */
13548 {
13549 c = NUL;
13550 break;
13551 }
13552 if (*ip == c) /* match! */
13553 {
13554 c = ip[1];
13555 break;
13556 }
13557 ip += 2;
13558 }
13559 }
13560
13561 if (c != NUL && c != prevc)
13562 {
13563 ri += mb_char2bytes(c, res + ri);
13564 if (ri + MB_MAXBYTES > MAXWLEN)
13565 break;
13566 prevc = c;
13567 }
13568 }
13569 }
13570 else
13571#endif
13572 {
13573 /* The sl_sal_first[] table contains the translation. */
13574 for (s = inword; (c = *s) != NUL; ++s)
13575 {
13576 if (vim_iswhite(c))
13577 c = ' ';
13578 else
13579 c = slang->sl_sal_first[c];
13580 if (c != NUL && (ri == 0 || res[ri - 1] != c))
13581 res[ri++] = c;
13582 }
13583 }
13584
13585 res[ri] = NUL;
13586}
13587
13588 static void
13589spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013590 slang_T *slang;
13591 char_u *inword;
13592 char_u *res;
13593{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013594 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013595 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013596 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013597 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013598 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013599 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013600 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013601 int n, k = 0;
13602 int z0;
13603 int k0;
13604 int n0;
13605 int c;
13606 int pri;
13607 int p0 = -333;
13608 int c0;
13609
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013610 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013611 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013612 if (slang->sl_rem_accents)
13613 {
13614 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013615 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013616 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013617 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013618 {
13619 *t++ = ' ';
13620 s = skipwhite(s);
13621 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013622 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013623 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013624 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013625 *t++ = *s;
13626 ++s;
13627 }
13628 }
13629 *t = NUL;
13630 }
13631 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013632 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013633
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013634 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013635
13636 /*
13637 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013638 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013639 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013640 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013641 while ((c = word[i]) != NUL)
13642 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013643 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013644 n = slang->sl_sal_first[c];
13645 z0 = 0;
13646
13647 if (n >= 0)
13648 {
13649 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013650 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013651 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013652 /* Quickly skip entries that don't match the word. Most
13653 * entries are less then three chars, optimize for that. */
13654 k = smp[n].sm_leadlen;
13655 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013656 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013657 if (word[i + 1] != s[1])
13658 continue;
13659 if (k > 2)
13660 {
13661 for (j = 2; j < k; ++j)
13662 if (word[i + j] != s[j])
13663 break;
13664 if (j < k)
13665 continue;
13666 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013667 }
13668
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013669 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013670 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013671 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013672 while (*pf != NUL && *pf != word[i + k])
13673 ++pf;
13674 if (*pf == NUL)
13675 continue;
13676 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013677 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013678 s = smp[n].sm_rules;
13679 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013680
13681 p0 = *s;
13682 k0 = k;
13683 while (*s == '-' && k > 1)
13684 {
13685 k--;
13686 s++;
13687 }
13688 if (*s == '<')
13689 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013690 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013691 {
13692 /* determine priority */
13693 pri = *s - '0';
13694 s++;
13695 }
13696 if (*s == '^' && *(s + 1) == '^')
13697 s++;
13698
13699 if (*s == NUL
13700 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013701 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013702 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013703 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013704 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013705 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013706 && spell_iswordp(word + i - 1, curbuf)
13707 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013708 {
13709 /* search for followup rules, if: */
13710 /* followup and k > 1 and NO '-' in searchstring */
13711 c0 = word[i + k - 1];
13712 n0 = slang->sl_sal_first[c0];
13713
13714 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013715 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013716 {
13717 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013718 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013719 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013720 /* Quickly skip entries that don't match the word.
13721 * */
13722 k0 = smp[n0].sm_leadlen;
13723 if (k0 > 1)
13724 {
13725 if (word[i + k] != s[1])
13726 continue;
13727 if (k0 > 2)
13728 {
13729 pf = word + i + k + 1;
13730 for (j = 2; j < k0; ++j)
13731 if (*pf++ != s[j])
13732 break;
13733 if (j < k0)
13734 continue;
13735 }
13736 }
13737 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013738
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013739 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013740 {
13741 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013742 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013743 while (*pf != NUL && *pf != word[i + k0])
13744 ++pf;
13745 if (*pf == NUL)
13746 continue;
13747 ++k0;
13748 }
13749
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013750 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013751 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013752 while (*s == '-')
13753 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013754 /* "k0" gets NOT reduced because
13755 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013756 s++;
13757 }
13758 if (*s == '<')
13759 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013760 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013761 {
13762 p0 = *s - '0';
13763 s++;
13764 }
13765
13766 if (*s == NUL
13767 /* *s == '^' cuts */
13768 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013769 && !spell_iswordp(word + i + k0,
13770 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013771 {
13772 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013773 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013774 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013775
13776 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013777 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013778 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013779 /* rule fits; stop search */
13780 break;
13781 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013782 }
13783
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013784 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013785 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013786 }
13787
13788 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013789 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000013790 if (s == NULL)
13791 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013792 pf = smp[n].sm_rules;
13793 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013794 if (p0 == 1 && z == 0)
13795 {
13796 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013797 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
13798 || res[reslen - 1] == *s))
13799 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013800 z0 = 1;
13801 z = 1;
13802 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013803 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013804 {
13805 word[i + k0] = *s;
13806 k0++;
13807 s++;
13808 }
13809 if (k > k0)
13810 mch_memmove(word + i + k0, word + i + k,
13811 STRLEN(word + i + k) + 1);
13812
13813 /* new "actual letter" */
13814 c = word[i];
13815 }
13816 else
13817 {
13818 /* no '<' rule used */
13819 i += k - 1;
13820 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013821 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013822 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013823 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013824 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013825 s++;
13826 }
13827 /* new "actual letter" */
13828 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013829 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013830 {
13831 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013832 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013833 mch_memmove(word, word + i + 1,
13834 STRLEN(word + i + 1) + 1);
13835 i = 0;
13836 z0 = 1;
13837 }
13838 }
13839 break;
13840 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013841 }
13842 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013843 else if (vim_iswhite(c))
13844 {
13845 c = ' ';
13846 k = 1;
13847 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013848
13849 if (z0 == 0)
13850 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013851 if (k && !p0 && reslen < MAXWLEN && c != NUL
13852 && (!slang->sl_collapse || reslen == 0
13853 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013854 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013855 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013856
13857 i++;
13858 z = 0;
13859 k = 0;
13860 }
13861 }
13862
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013863 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013864}
13865
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013866#ifdef FEAT_MBYTE
13867/*
13868 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
13869 * Multi-byte version of spell_soundfold().
13870 */
13871 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013872spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013873 slang_T *slang;
13874 char_u *inword;
13875 char_u *res;
13876{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013877 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013878 int word[MAXWLEN];
13879 int wres[MAXWLEN];
13880 int l;
13881 char_u *s;
13882 int *ws;
13883 char_u *t;
13884 int *pf;
13885 int i, j, z;
13886 int reslen;
13887 int n, k = 0;
13888 int z0;
13889 int k0;
13890 int n0;
13891 int c;
13892 int pri;
13893 int p0 = -333;
13894 int c0;
13895 int did_white = FALSE;
13896
13897 /*
13898 * Convert the multi-byte string to a wide-character string.
13899 * Remove accents, if wanted. We actually remove all non-word characters.
13900 * But keep white space.
13901 */
13902 n = 0;
13903 for (s = inword; *s != NUL; )
13904 {
13905 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013906 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013907 if (slang->sl_rem_accents)
13908 {
13909 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
13910 {
13911 if (did_white)
13912 continue;
13913 c = ' ';
13914 did_white = TRUE;
13915 }
13916 else
13917 {
13918 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013919 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013920 continue;
13921 }
13922 }
13923 word[n++] = c;
13924 }
13925 word[n] = NUL;
13926
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013927 /*
13928 * This comes from Aspell phonet.cpp.
13929 * Converted from C++ to C. Added support for multi-byte chars.
13930 * Changed to keep spaces.
13931 */
13932 i = reslen = z = 0;
13933 while ((c = word[i]) != NUL)
13934 {
13935 /* Start with the first rule that has the character in the word. */
13936 n = slang->sl_sal_first[c & 0xff];
13937 z0 = 0;
13938
13939 if (n >= 0)
13940 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013941 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013942 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
13943 {
13944 /* Quickly skip entries that don't match the word. Most
13945 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013946 if (c != ws[0])
13947 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013948 k = smp[n].sm_leadlen;
13949 if (k > 1)
13950 {
13951 if (word[i + 1] != ws[1])
13952 continue;
13953 if (k > 2)
13954 {
13955 for (j = 2; j < k; ++j)
13956 if (word[i + j] != ws[j])
13957 break;
13958 if (j < k)
13959 continue;
13960 }
13961 }
13962
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013963 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013964 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013965 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013966 while (*pf != NUL && *pf != word[i + k])
13967 ++pf;
13968 if (*pf == NUL)
13969 continue;
13970 ++k;
13971 }
13972 s = smp[n].sm_rules;
13973 pri = 5; /* default priority */
13974
13975 p0 = *s;
13976 k0 = k;
13977 while (*s == '-' && k > 1)
13978 {
13979 k--;
13980 s++;
13981 }
13982 if (*s == '<')
13983 s++;
13984 if (VIM_ISDIGIT(*s))
13985 {
13986 /* determine priority */
13987 pri = *s - '0';
13988 s++;
13989 }
13990 if (*s == '^' && *(s + 1) == '^')
13991 s++;
13992
13993 if (*s == NUL
13994 || (*s == '^'
13995 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013996 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013997 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013998 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013999 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014000 && spell_iswordp_w(word + i - 1, curbuf)
14001 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014002 {
14003 /* search for followup rules, if: */
14004 /* followup and k > 1 and NO '-' in searchstring */
14005 c0 = word[i + k - 1];
14006 n0 = slang->sl_sal_first[c0 & 0xff];
14007
14008 if (slang->sl_followup && k > 1 && n0 >= 0
14009 && p0 != '-' && word[i + k] != NUL)
14010 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014011 /* Test follow-up rule for "word[i + k]"; loop over
14012 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014013 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
14014 == (c0 & 0xff); ++n0)
14015 {
14016 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014017 */
14018 if (c0 != ws[0])
14019 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014020 k0 = smp[n0].sm_leadlen;
14021 if (k0 > 1)
14022 {
14023 if (word[i + k] != ws[1])
14024 continue;
14025 if (k0 > 2)
14026 {
14027 pf = word + i + k + 1;
14028 for (j = 2; j < k0; ++j)
14029 if (*pf++ != ws[j])
14030 break;
14031 if (j < k0)
14032 continue;
14033 }
14034 }
14035 k0 += k - 1;
14036
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014037 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014038 {
14039 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014040 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014041 while (*pf != NUL && *pf != word[i + k0])
14042 ++pf;
14043 if (*pf == NUL)
14044 continue;
14045 ++k0;
14046 }
14047
14048 p0 = 5;
14049 s = smp[n0].sm_rules;
14050 while (*s == '-')
14051 {
14052 /* "k0" gets NOT reduced because
14053 * "if (k0 == k)" */
14054 s++;
14055 }
14056 if (*s == '<')
14057 s++;
14058 if (VIM_ISDIGIT(*s))
14059 {
14060 p0 = *s - '0';
14061 s++;
14062 }
14063
14064 if (*s == NUL
14065 /* *s == '^' cuts */
14066 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014067 && !spell_iswordp_w(word + i + k0,
14068 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014069 {
14070 if (k0 == k)
14071 /* this is just a piece of the string */
14072 continue;
14073
14074 if (p0 < pri)
14075 /* priority too low */
14076 continue;
14077 /* rule fits; stop search */
14078 break;
14079 }
14080 }
14081
14082 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
14083 == (c0 & 0xff))
14084 continue;
14085 }
14086
14087 /* replace string */
14088 ws = smp[n].sm_to_w;
14089 s = smp[n].sm_rules;
14090 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
14091 if (p0 == 1 && z == 0)
14092 {
14093 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014094 if (reslen > 0 && ws != NULL && *ws != NUL
14095 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014096 || wres[reslen - 1] == *ws))
14097 reslen--;
14098 z0 = 1;
14099 z = 1;
14100 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014101 if (ws != NULL)
14102 while (*ws != NUL && word[i + k0] != NUL)
14103 {
14104 word[i + k0] = *ws;
14105 k0++;
14106 ws++;
14107 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014108 if (k > k0)
14109 mch_memmove(word + i + k0, word + i + k,
14110 sizeof(int) * (STRLEN(word + i + k) + 1));
14111
14112 /* new "actual letter" */
14113 c = word[i];
14114 }
14115 else
14116 {
14117 /* no '<' rule used */
14118 i += k - 1;
14119 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014120 if (ws != NULL)
14121 while (*ws != NUL && ws[1] != NUL
14122 && reslen < MAXWLEN)
14123 {
14124 if (reslen == 0 || wres[reslen - 1] != *ws)
14125 wres[reslen++] = *ws;
14126 ws++;
14127 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014128 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014129 if (ws == NULL)
14130 c = NUL;
14131 else
14132 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014133 if (strstr((char *)s, "^^") != NULL)
14134 {
14135 if (c != NUL)
14136 wres[reslen++] = c;
14137 mch_memmove(word, word + i + 1,
14138 sizeof(int) * (STRLEN(word + i + 1) + 1));
14139 i = 0;
14140 z0 = 1;
14141 }
14142 }
14143 break;
14144 }
14145 }
14146 }
14147 else if (vim_iswhite(c))
14148 {
14149 c = ' ';
14150 k = 1;
14151 }
14152
14153 if (z0 == 0)
14154 {
14155 if (k && !p0 && reslen < MAXWLEN && c != NUL
14156 && (!slang->sl_collapse || reslen == 0
14157 || wres[reslen - 1] != c))
14158 /* condense only double letters */
14159 wres[reslen++] = c;
14160
14161 i++;
14162 z = 0;
14163 k = 0;
14164 }
14165 }
14166
14167 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14168 l = 0;
14169 for (n = 0; n < reslen; ++n)
14170 {
14171 l += mb_char2bytes(wres[n], res + l);
14172 if (l + MB_MAXBYTES > MAXWLEN)
14173 break;
14174 }
14175 res[l] = NUL;
14176}
14177#endif
14178
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014179/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014180 * Compute a score for two sound-a-like words.
14181 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14182 * Instead of a generic loop we write out the code. That keeps it fast by
14183 * avoiding checks that will not be possible.
14184 */
14185 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014186soundalike_score(goodstart, badstart)
14187 char_u *goodstart; /* sound-folded good word */
14188 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014189{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014190 char_u *goodsound = goodstart;
14191 char_u *badsound = badstart;
14192 int goodlen;
14193 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014194 int n;
14195 char_u *pl, *ps;
14196 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014197 int score = 0;
14198
14199 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
14200 * counted so much, vowels halfway the word aren't counted at all. */
14201 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
14202 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014203 if (badsound[1] == goodsound[1]
14204 || (badsound[1] != NUL
14205 && goodsound[1] != NUL
14206 && badsound[2] == goodsound[2]))
14207 {
14208 /* handle like a substitute */
14209 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014210 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014211 {
14212 score = 2 * SCORE_DEL / 3;
14213 if (*badsound == '*')
14214 ++badsound;
14215 else
14216 ++goodsound;
14217 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014218 }
14219
14220 goodlen = STRLEN(goodsound);
14221 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014222
14223 /* Return quickly if the lenghts are too different to be fixed by two
14224 * changes. */
14225 n = goodlen - badlen;
14226 if (n < -2 || n > 2)
14227 return SCORE_MAXMAX;
14228
14229 if (n > 0)
14230 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014231 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014232 ps = badsound;
14233 }
14234 else
14235 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014236 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014237 ps = goodsound;
14238 }
14239
14240 /* Skip over the identical part. */
14241 while (*pl == *ps && *pl != NUL)
14242 {
14243 ++pl;
14244 ++ps;
14245 }
14246
14247 switch (n)
14248 {
14249 case -2:
14250 case 2:
14251 /*
14252 * Must delete two characters from "pl".
14253 */
14254 ++pl; /* first delete */
14255 while (*pl == *ps)
14256 {
14257 ++pl;
14258 ++ps;
14259 }
14260 /* strings must be equal after second delete */
14261 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014262 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014263
14264 /* Failed to compare. */
14265 break;
14266
14267 case -1:
14268 case 1:
14269 /*
14270 * Minimal one delete from "pl" required.
14271 */
14272
14273 /* 1: delete */
14274 pl2 = pl + 1;
14275 ps2 = ps;
14276 while (*pl2 == *ps2)
14277 {
14278 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014279 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014280 ++pl2;
14281 ++ps2;
14282 }
14283
14284 /* 2: delete then swap, then rest must be equal */
14285 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14286 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014287 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014288
14289 /* 3: delete then substitute, then the rest must be equal */
14290 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014291 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014292
14293 /* 4: first swap then delete */
14294 if (pl[0] == ps[1] && pl[1] == ps[0])
14295 {
14296 pl2 = pl + 2; /* swap, skip two chars */
14297 ps2 = ps + 2;
14298 while (*pl2 == *ps2)
14299 {
14300 ++pl2;
14301 ++ps2;
14302 }
14303 /* delete a char and then strings must be equal */
14304 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014305 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014306 }
14307
14308 /* 5: first substitute then delete */
14309 pl2 = pl + 1; /* substitute, skip one char */
14310 ps2 = ps + 1;
14311 while (*pl2 == *ps2)
14312 {
14313 ++pl2;
14314 ++ps2;
14315 }
14316 /* delete a char and then strings must be equal */
14317 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014318 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014319
14320 /* Failed to compare. */
14321 break;
14322
14323 case 0:
14324 /*
14325 * Lenghts are equal, thus changes must result in same length: An
14326 * insert is only possible in combination with a delete.
14327 * 1: check if for identical strings
14328 */
14329 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014330 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014331
14332 /* 2: swap */
14333 if (pl[0] == ps[1] && pl[1] == ps[0])
14334 {
14335 pl2 = pl + 2; /* swap, skip two chars */
14336 ps2 = ps + 2;
14337 while (*pl2 == *ps2)
14338 {
14339 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014340 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014341 ++pl2;
14342 ++ps2;
14343 }
14344 /* 3: swap and swap again */
14345 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14346 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014347 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014348
14349 /* 4: swap and substitute */
14350 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014351 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014352 }
14353
14354 /* 5: substitute */
14355 pl2 = pl + 1;
14356 ps2 = ps + 1;
14357 while (*pl2 == *ps2)
14358 {
14359 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014360 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014361 ++pl2;
14362 ++ps2;
14363 }
14364
14365 /* 6: substitute and swap */
14366 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14367 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014368 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014369
14370 /* 7: substitute and substitute */
14371 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014372 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014373
14374 /* 8: insert then delete */
14375 pl2 = pl;
14376 ps2 = ps + 1;
14377 while (*pl2 == *ps2)
14378 {
14379 ++pl2;
14380 ++ps2;
14381 }
14382 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014383 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014384
14385 /* 9: delete then insert */
14386 pl2 = pl + 1;
14387 ps2 = ps;
14388 while (*pl2 == *ps2)
14389 {
14390 ++pl2;
14391 ++ps2;
14392 }
14393 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014394 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014395
14396 /* Failed to compare. */
14397 break;
14398 }
14399
14400 return SCORE_MAXMAX;
14401}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014402
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014403/*
14404 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014405 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014406 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000014407 * The algorithm is described by Du and Chang, 1992.
14408 * The implementation of the algorithm comes from Aspell editdist.cpp,
14409 * edit_distance(). It has been converted from C++ to C and modified to
14410 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014411 */
14412 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +000014413spell_edit_score(slang, badword, goodword)
14414 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014415 char_u *badword;
14416 char_u *goodword;
14417{
14418 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014419 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014420 int j, i;
14421 int t;
14422 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014423 int pbc, pgc;
14424#ifdef FEAT_MBYTE
14425 char_u *p;
14426 int wbadword[MAXWLEN];
14427 int wgoodword[MAXWLEN];
14428
14429 if (has_mbyte)
14430 {
14431 /* Get the characters from the multi-byte strings and put them in an
14432 * int array for easy access. */
14433 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014434 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014435 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014436 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014437 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014438 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014439 }
14440 else
14441#endif
14442 {
14443 badlen = STRLEN(badword) + 1;
14444 goodlen = STRLEN(goodword) + 1;
14445 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014446
14447 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
14448#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014449 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
14450 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014451 if (cnt == NULL)
14452 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014453
14454 CNT(0, 0) = 0;
14455 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +000014456 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014457
14458 for (i = 1; i <= badlen; ++i)
14459 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014460 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014461 for (j = 1; j <= goodlen; ++j)
14462 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014463#ifdef FEAT_MBYTE
14464 if (has_mbyte)
14465 {
14466 bc = wbadword[i - 1];
14467 gc = wgoodword[j - 1];
14468 }
14469 else
14470#endif
14471 {
14472 bc = badword[i - 1];
14473 gc = goodword[j - 1];
14474 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014475 if (bc == gc)
14476 CNT(i, j) = CNT(i - 1, j - 1);
14477 else
14478 {
14479 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014480 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014481 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
14482 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014483 {
14484 /* For a similar character use SCORE_SIMILAR. */
14485 if (slang != NULL
14486 && slang->sl_has_map
14487 && similar_chars(slang, gc, bc))
14488 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
14489 else
14490 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
14491 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014492
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014493 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014494 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014495#ifdef FEAT_MBYTE
14496 if (has_mbyte)
14497 {
14498 pbc = wbadword[i - 2];
14499 pgc = wgoodword[j - 2];
14500 }
14501 else
14502#endif
14503 {
14504 pbc = badword[i - 2];
14505 pgc = goodword[j - 2];
14506 }
14507 if (bc == pgc && pbc == gc)
14508 {
14509 t = SCORE_SWAP + CNT(i - 2, j - 2);
14510 if (t < CNT(i, j))
14511 CNT(i, j) = t;
14512 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014513 }
14514 t = SCORE_DEL + CNT(i - 1, j);
14515 if (t < CNT(i, j))
14516 CNT(i, j) = t;
14517 t = SCORE_INS + CNT(i, j - 1);
14518 if (t < CNT(i, j))
14519 CNT(i, j) = t;
14520 }
14521 }
14522 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014523
14524 i = CNT(badlen - 1, goodlen - 1);
14525 vim_free(cnt);
14526 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014527}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000014528
Bram Moolenaar4770d092006-01-12 23:22:24 +000014529typedef struct
14530{
14531 int badi;
14532 int goodi;
14533 int score;
14534} limitscore_T;
14535
14536/*
14537 * Like spell_edit_score(), but with a limit on the score to make it faster.
14538 * May return SCORE_MAXMAX when the score is higher than "limit".
14539 *
14540 * This uses a stack for the edits still to be tried.
14541 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
14542 * for multi-byte characters.
14543 */
14544 static int
14545spell_edit_score_limit(slang, badword, goodword, limit)
14546 slang_T *slang;
14547 char_u *badword;
14548 char_u *goodword;
14549 int limit;
14550{
14551 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
14552 int stackidx;
14553 int bi, gi;
14554 int bi2, gi2;
14555 int bc, gc;
14556 int score;
14557 int score_off;
14558 int minscore;
14559 int round;
14560
14561#ifdef FEAT_MBYTE
14562 /* Multi-byte characters require a bit more work, use a different function
14563 * to avoid testing "has_mbyte" quite often. */
14564 if (has_mbyte)
14565 return spell_edit_score_limit_w(slang, badword, goodword, limit);
14566#endif
14567
14568 /*
14569 * The idea is to go from start to end over the words. So long as
14570 * characters are equal just continue, this always gives the lowest score.
14571 * When there is a difference try several alternatives. Each alternative
14572 * increases "score" for the edit distance. Some of the alternatives are
14573 * pushed unto a stack and tried later, some are tried right away. At the
14574 * end of the word the score for one alternative is known. The lowest
14575 * possible score is stored in "minscore".
14576 */
14577 stackidx = 0;
14578 bi = 0;
14579 gi = 0;
14580 score = 0;
14581 minscore = limit + 1;
14582
14583 for (;;)
14584 {
14585 /* Skip over an equal part, score remains the same. */
14586 for (;;)
14587 {
14588 bc = badword[bi];
14589 gc = goodword[gi];
14590 if (bc != gc) /* stop at a char that's different */
14591 break;
14592 if (bc == NUL) /* both words end */
14593 {
14594 if (score < minscore)
14595 minscore = score;
14596 goto pop; /* do next alternative */
14597 }
14598 ++bi;
14599 ++gi;
14600 }
14601
14602 if (gc == NUL) /* goodword ends, delete badword chars */
14603 {
14604 do
14605 {
14606 if ((score += SCORE_DEL) >= minscore)
14607 goto pop; /* do next alternative */
14608 } while (badword[++bi] != NUL);
14609 minscore = score;
14610 }
14611 else if (bc == NUL) /* badword ends, insert badword chars */
14612 {
14613 do
14614 {
14615 if ((score += SCORE_INS) >= minscore)
14616 goto pop; /* do next alternative */
14617 } while (goodword[++gi] != NUL);
14618 minscore = score;
14619 }
14620 else /* both words continue */
14621 {
14622 /* If not close to the limit, perform a change. Only try changes
14623 * that may lead to a lower score than "minscore".
14624 * round 0: try deleting a char from badword
14625 * round 1: try inserting a char in badword */
14626 for (round = 0; round <= 1; ++round)
14627 {
14628 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
14629 if (score_off < minscore)
14630 {
14631 if (score_off + SCORE_EDIT_MIN >= minscore)
14632 {
14633 /* Near the limit, rest of the words must match. We
14634 * can check that right now, no need to push an item
14635 * onto the stack. */
14636 bi2 = bi + 1 - round;
14637 gi2 = gi + round;
14638 while (goodword[gi2] == badword[bi2])
14639 {
14640 if (goodword[gi2] == NUL)
14641 {
14642 minscore = score_off;
14643 break;
14644 }
14645 ++bi2;
14646 ++gi2;
14647 }
14648 }
14649 else
14650 {
14651 /* try deleting/inserting a character later */
14652 stack[stackidx].badi = bi + 1 - round;
14653 stack[stackidx].goodi = gi + round;
14654 stack[stackidx].score = score_off;
14655 ++stackidx;
14656 }
14657 }
14658 }
14659
14660 if (score + SCORE_SWAP < minscore)
14661 {
14662 /* If swapping two characters makes a match then the
14663 * substitution is more expensive, thus there is no need to
14664 * try both. */
14665 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
14666 {
14667 /* Swap two characters, that is: skip them. */
14668 gi += 2;
14669 bi += 2;
14670 score += SCORE_SWAP;
14671 continue;
14672 }
14673 }
14674
14675 /* Substitute one character for another which is the same
14676 * thing as deleting a character from both goodword and badword.
14677 * Use a better score when there is only a case difference. */
14678 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
14679 score += SCORE_ICASE;
14680 else
14681 {
14682 /* For a similar character use SCORE_SIMILAR. */
14683 if (slang != NULL
14684 && slang->sl_has_map
14685 && similar_chars(slang, gc, bc))
14686 score += SCORE_SIMILAR;
14687 else
14688 score += SCORE_SUBST;
14689 }
14690
14691 if (score < minscore)
14692 {
14693 /* Do the substitution. */
14694 ++gi;
14695 ++bi;
14696 continue;
14697 }
14698 }
14699pop:
14700 /*
14701 * Get here to try the next alternative, pop it from the stack.
14702 */
14703 if (stackidx == 0) /* stack is empty, finished */
14704 break;
14705
14706 /* pop an item from the stack */
14707 --stackidx;
14708 gi = stack[stackidx].goodi;
14709 bi = stack[stackidx].badi;
14710 score = stack[stackidx].score;
14711 }
14712
14713 /* When the score goes over "limit" it may actually be much higher.
14714 * Return a very large number to avoid going below the limit when giving a
14715 * bonus. */
14716 if (minscore > limit)
14717 return SCORE_MAXMAX;
14718 return minscore;
14719}
14720
14721#ifdef FEAT_MBYTE
14722/*
14723 * Multi-byte version of spell_edit_score_limit().
14724 * Keep it in sync with the above!
14725 */
14726 static int
14727spell_edit_score_limit_w(slang, badword, goodword, limit)
14728 slang_T *slang;
14729 char_u *badword;
14730 char_u *goodword;
14731 int limit;
14732{
14733 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
14734 int stackidx;
14735 int bi, gi;
14736 int bi2, gi2;
14737 int bc, gc;
14738 int score;
14739 int score_off;
14740 int minscore;
14741 int round;
14742 char_u *p;
14743 int wbadword[MAXWLEN];
14744 int wgoodword[MAXWLEN];
14745
14746 /* Get the characters from the multi-byte strings and put them in an
14747 * int array for easy access. */
14748 bi = 0;
14749 for (p = badword; *p != NUL; )
14750 wbadword[bi++] = mb_cptr2char_adv(&p);
14751 wbadword[bi++] = 0;
14752 gi = 0;
14753 for (p = goodword; *p != NUL; )
14754 wgoodword[gi++] = mb_cptr2char_adv(&p);
14755 wgoodword[gi++] = 0;
14756
14757 /*
14758 * The idea is to go from start to end over the words. So long as
14759 * characters are equal just continue, this always gives the lowest score.
14760 * When there is a difference try several alternatives. Each alternative
14761 * increases "score" for the edit distance. Some of the alternatives are
14762 * pushed unto a stack and tried later, some are tried right away. At the
14763 * end of the word the score for one alternative is known. The lowest
14764 * possible score is stored in "minscore".
14765 */
14766 stackidx = 0;
14767 bi = 0;
14768 gi = 0;
14769 score = 0;
14770 minscore = limit + 1;
14771
14772 for (;;)
14773 {
14774 /* Skip over an equal part, score remains the same. */
14775 for (;;)
14776 {
14777 bc = wbadword[bi];
14778 gc = wgoodword[gi];
14779
14780 if (bc != gc) /* stop at a char that's different */
14781 break;
14782 if (bc == NUL) /* both words end */
14783 {
14784 if (score < minscore)
14785 minscore = score;
14786 goto pop; /* do next alternative */
14787 }
14788 ++bi;
14789 ++gi;
14790 }
14791
14792 if (gc == NUL) /* goodword ends, delete badword chars */
14793 {
14794 do
14795 {
14796 if ((score += SCORE_DEL) >= minscore)
14797 goto pop; /* do next alternative */
14798 } while (wbadword[++bi] != NUL);
14799 minscore = score;
14800 }
14801 else if (bc == NUL) /* badword ends, insert badword chars */
14802 {
14803 do
14804 {
14805 if ((score += SCORE_INS) >= minscore)
14806 goto pop; /* do next alternative */
14807 } while (wgoodword[++gi] != NUL);
14808 minscore = score;
14809 }
14810 else /* both words continue */
14811 {
14812 /* If not close to the limit, perform a change. Only try changes
14813 * that may lead to a lower score than "minscore".
14814 * round 0: try deleting a char from badword
14815 * round 1: try inserting a char in badword */
14816 for (round = 0; round <= 1; ++round)
14817 {
14818 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
14819 if (score_off < minscore)
14820 {
14821 if (score_off + SCORE_EDIT_MIN >= minscore)
14822 {
14823 /* Near the limit, rest of the words must match. We
14824 * can check that right now, no need to push an item
14825 * onto the stack. */
14826 bi2 = bi + 1 - round;
14827 gi2 = gi + round;
14828 while (wgoodword[gi2] == wbadword[bi2])
14829 {
14830 if (wgoodword[gi2] == NUL)
14831 {
14832 minscore = score_off;
14833 break;
14834 }
14835 ++bi2;
14836 ++gi2;
14837 }
14838 }
14839 else
14840 {
14841 /* try deleting a character from badword later */
14842 stack[stackidx].badi = bi + 1 - round;
14843 stack[stackidx].goodi = gi + round;
14844 stack[stackidx].score = score_off;
14845 ++stackidx;
14846 }
14847 }
14848 }
14849
14850 if (score + SCORE_SWAP < minscore)
14851 {
14852 /* If swapping two characters makes a match then the
14853 * substitution is more expensive, thus there is no need to
14854 * try both. */
14855 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
14856 {
14857 /* Swap two characters, that is: skip them. */
14858 gi += 2;
14859 bi += 2;
14860 score += SCORE_SWAP;
14861 continue;
14862 }
14863 }
14864
14865 /* Substitute one character for another which is the same
14866 * thing as deleting a character from both goodword and badword.
14867 * Use a better score when there is only a case difference. */
14868 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
14869 score += SCORE_ICASE;
14870 else
14871 {
14872 /* For a similar character use SCORE_SIMILAR. */
14873 if (slang != NULL
14874 && slang->sl_has_map
14875 && similar_chars(slang, gc, bc))
14876 score += SCORE_SIMILAR;
14877 else
14878 score += SCORE_SUBST;
14879 }
14880
14881 if (score < minscore)
14882 {
14883 /* Do the substitution. */
14884 ++gi;
14885 ++bi;
14886 continue;
14887 }
14888 }
14889pop:
14890 /*
14891 * Get here to try the next alternative, pop it from the stack.
14892 */
14893 if (stackidx == 0) /* stack is empty, finished */
14894 break;
14895
14896 /* pop an item from the stack */
14897 --stackidx;
14898 gi = stack[stackidx].goodi;
14899 bi = stack[stackidx].badi;
14900 score = stack[stackidx].score;
14901 }
14902
14903 /* When the score goes over "limit" it may actually be much higher.
14904 * Return a very large number to avoid going below the limit when giving a
14905 * bonus. */
14906 if (minscore > limit)
14907 return SCORE_MAXMAX;
14908 return minscore;
14909}
14910#endif
14911
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014912/*
14913 * ":spellinfo"
14914 */
14915/*ARGSUSED*/
14916 void
14917ex_spellinfo(eap)
14918 exarg_T *eap;
14919{
14920 int lpi;
14921 langp_T *lp;
14922 char_u *p;
14923
14924 if (no_spell_checking(curwin))
14925 return;
14926
14927 msg_start();
14928 for (lpi = 0; lpi < curbuf->b_langp.ga_len && !got_int; ++lpi)
14929 {
14930 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
14931 msg_puts((char_u *)"file: ");
14932 msg_puts(lp->lp_slang->sl_fname);
14933 msg_putchar('\n');
14934 p = lp->lp_slang->sl_info;
14935 if (p != NULL)
14936 {
14937 msg_puts(p);
14938 msg_putchar('\n');
14939 }
14940 }
14941 msg_end();
14942}
14943
Bram Moolenaar4770d092006-01-12 23:22:24 +000014944#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
14945#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000014946#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +000014947#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
14948#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +000014949
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014950/*
14951 * ":spelldump"
14952 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014953 void
14954ex_spelldump(eap)
14955 exarg_T *eap;
14956{
14957 buf_T *buf = curbuf;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000014958
14959 if (no_spell_checking(curwin))
14960 return;
14961
14962 /* Create a new empty buffer by splitting the window. */
14963 do_cmdline_cmd((char_u *)"new");
14964 if (!bufempty() || !buf_valid(buf))
14965 return;
14966
14967 spell_dump_compl(buf, NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
14968
14969 /* Delete the empty line that we started with. */
14970 if (curbuf->b_ml.ml_line_count > 1)
14971 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
14972
14973 redraw_later(NOT_VALID);
14974}
14975
14976/*
14977 * Go through all possible words and:
14978 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
14979 * "ic" and "dir" are not used.
14980 * 2. When "pat" is not NULL: add matching words to insert mode completion.
14981 */
14982 void
14983spell_dump_compl(buf, pat, ic, dir, dumpflags_arg)
14984 buf_T *buf; /* buffer with spell checking */
14985 char_u *pat; /* leading part of the word */
14986 int ic; /* ignore case */
14987 int *dir; /* direction for adding matches */
14988 int dumpflags_arg; /* DUMPFLAG_* */
14989{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014990 langp_T *lp;
14991 slang_T *slang;
14992 idx_T arridx[MAXWLEN];
14993 int curi[MAXWLEN];
14994 char_u word[MAXWLEN];
14995 int c;
14996 char_u *byts;
14997 idx_T *idxs;
14998 linenr_T lnum = 0;
14999 int round;
15000 int depth;
15001 int n;
15002 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015003 char_u *region_names = NULL; /* region names being used */
15004 int do_region = TRUE; /* dump region names and numbers */
15005 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015006 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015007 int dumpflags = dumpflags_arg;
15008 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015009
Bram Moolenaard0131a82006-03-04 21:46:13 +000015010 /* When ignoring case or when the pattern starts with capital pass this on
15011 * to dump_word(). */
15012 if (pat != NULL)
15013 {
15014 if (ic)
15015 dumpflags |= DUMPFLAG_ICASE;
15016 else
15017 {
15018 n = captype(pat, NULL);
15019 if (n == WF_ONECAP)
15020 dumpflags |= DUMPFLAG_ONECAP;
15021 else if (n == WF_ALLCAP
15022#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015023 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015024#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015025 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +000015026#endif
15027 )
15028 dumpflags |= DUMPFLAG_ALLCAP;
15029 }
15030 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015031
Bram Moolenaar7887d882005-07-01 22:33:52 +000015032 /* Find out if we can support regions: All languages must support the same
15033 * regions or none at all. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015034 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000015035 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015036 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000015037 p = lp->lp_slang->sl_regions;
15038 if (p[0] != 0)
15039 {
15040 if (region_names == NULL) /* first language with regions */
15041 region_names = p;
15042 else if (STRCMP(region_names, p) != 0)
15043 {
15044 do_region = FALSE; /* region names are different */
15045 break;
15046 }
15047 }
15048 }
15049
15050 if (do_region && region_names != NULL)
15051 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015052 if (pat == NULL)
15053 {
15054 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
15055 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15056 }
Bram Moolenaar7887d882005-07-01 22:33:52 +000015057 }
15058 else
15059 do_region = FALSE;
15060
15061 /*
15062 * Loop over all files loaded for the entries in 'spelllang'.
15063 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015064 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015065 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015066 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015067 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015068 if (slang->sl_fbyts == NULL) /* reloading failed */
15069 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015070
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015071 if (pat == NULL)
15072 {
15073 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
15074 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15075 }
15076
15077 /* When matching with a pattern and there are no prefixes only use
15078 * parts of the tree that match "pat". */
15079 if (pat != NULL && slang->sl_pbyts == NULL)
15080 patlen = STRLEN(pat);
15081 else
15082 patlen = 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015083
15084 /* round 1: case-folded tree
15085 * round 2: keep-case tree */
15086 for (round = 1; round <= 2; ++round)
15087 {
15088 if (round == 1)
15089 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015090 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015091 byts = slang->sl_fbyts;
15092 idxs = slang->sl_fidxs;
15093 }
15094 else
15095 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015096 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015097 byts = slang->sl_kbyts;
15098 idxs = slang->sl_kidxs;
15099 }
15100 if (byts == NULL)
15101 continue; /* array is empty */
15102
15103 depth = 0;
15104 arridx[0] = 0;
15105 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015106 while (depth >= 0 && !got_int
15107 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015108 {
15109 if (curi[depth] > byts[arridx[depth]])
15110 {
15111 /* Done all bytes at this node, go up one level. */
15112 --depth;
15113 line_breakcheck();
Bram Moolenaara2031822006-03-07 22:29:51 +000015114 ins_compl_check_keys(50);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015115 }
15116 else
15117 {
15118 /* Do one more byte at this node. */
15119 n = arridx[depth] + curi[depth];
15120 ++curi[depth];
15121 c = byts[n];
15122 if (c == 0)
15123 {
15124 /* End of word, deal with the word.
15125 * Don't use keep-case words in the fold-case tree,
15126 * they will appear in the keep-case tree.
15127 * Only use the word when the region matches. */
15128 flags = (int)idxs[n];
15129 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015130 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000015131 && (do_region
15132 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015133 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015134 & lp->lp_region) != 0))
15135 {
15136 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015137 if (!do_region)
15138 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015139
15140 /* Dump the basic word if there is no prefix or
15141 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015142 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015143 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015144 {
15145 dump_word(slang, word, pat, dir,
15146 dumpflags, flags, lnum);
15147 if (pat == NULL)
15148 ++lnum;
15149 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015150
15151 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015152 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015153 lnum = dump_prefixes(slang, word, pat, dir,
15154 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015155 }
15156 }
15157 else
15158 {
15159 /* Normal char, go one level deeper. */
15160 word[depth++] = c;
15161 arridx[depth] = idxs[n];
15162 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015163
15164 /* Check if this characters matches with the pattern.
15165 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +000015166 * Always ignore case here, dump_word() will check
15167 * proper case later. This isn't exactly right when
15168 * length changes for multi-byte characters with
15169 * ignore case... */
15170 if (depth <= patlen
15171 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015172 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015173 }
15174 }
15175 }
15176 }
15177 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015178}
15179
15180/*
15181 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015182 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015183 */
15184 static void
Bram Moolenaard0131a82006-03-04 21:46:13 +000015185dump_word(slang, word, pat, dir, dumpflags, wordflags, lnum)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015186 slang_T *slang;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015187 char_u *word;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015188 char_u *pat;
15189 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015190 int dumpflags;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015191 int wordflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015192 linenr_T lnum;
15193{
15194 int keepcap = FALSE;
15195 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015196 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015197 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000015198 char_u badword[MAXWLEN + 10];
15199 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015200 int flags = wordflags;
15201
15202 if (dumpflags & DUMPFLAG_ONECAP)
15203 flags |= WF_ONECAP;
15204 if (dumpflags & DUMPFLAG_ALLCAP)
15205 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015206
Bram Moolenaar4770d092006-01-12 23:22:24 +000015207 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015208 {
15209 /* Need to fix case according to "flags". */
15210 make_case_word(word, cword, flags);
15211 p = cword;
15212 }
15213 else
15214 {
15215 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015216 if ((dumpflags & DUMPFLAG_KEEPCASE)
15217 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000015218 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015219 keepcap = TRUE;
15220 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015221 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015222
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015223 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015224 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015225 /* Add flags and regions after a slash. */
15226 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015227 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015228 STRCPY(badword, p);
15229 STRCAT(badword, "/");
15230 if (keepcap)
15231 STRCAT(badword, "=");
15232 if (flags & WF_BANNED)
15233 STRCAT(badword, "!");
15234 else if (flags & WF_RARE)
15235 STRCAT(badword, "?");
15236 if (flags & WF_REGION)
15237 for (i = 0; i < 7; ++i)
15238 if (flags & (0x10000 << i))
15239 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
15240 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015241 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015242
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015243 if (dumpflags & DUMPFLAG_COUNT)
15244 {
15245 hashitem_T *hi;
15246
15247 /* Include the word count for ":spelldump!". */
15248 hi = hash_find(&slang->sl_wordcount, tw);
15249 if (!HASHITEM_EMPTY(hi))
15250 {
15251 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
15252 tw, HI2WC(hi)->wc_count);
15253 p = IObuff;
15254 }
15255 }
15256
15257 ml_append(lnum, p, (colnr_T)0, FALSE);
15258 }
Bram Moolenaard0131a82006-03-04 21:46:13 +000015259 else if (((dumpflags & DUMPFLAG_ICASE)
15260 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
15261 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015262 && ins_compl_add_infercase(p, (int)STRLEN(p),
15263 dumpflags & DUMPFLAG_ICASE,
15264 NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015265 /* if dir was BACKWARD then honor it just once */
15266 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015267}
15268
15269/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000015270 * For ":spelldump": Find matching prefixes for "word". Prepend each to
15271 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015272 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015273 * Return the updated line number.
15274 */
15275 static linenr_T
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015276dump_prefixes(slang, word, pat, dir, dumpflags, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015277 slang_T *slang;
15278 char_u *word; /* case-folded word */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015279 char_u *pat;
15280 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015281 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015282 int flags; /* flags with prefix ID */
15283 linenr_T startlnum;
15284{
15285 idx_T arridx[MAXWLEN];
15286 int curi[MAXWLEN];
15287 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000015288 char_u word_up[MAXWLEN];
15289 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015290 int c;
15291 char_u *byts;
15292 idx_T *idxs;
15293 linenr_T lnum = startlnum;
15294 int depth;
15295 int n;
15296 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015297 int i;
15298
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015299 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +000015300 * upper-case letter in word_up[]. */
15301 c = PTR2CHAR(word);
15302 if (SPELL_TOUPPER(c) != c)
15303 {
15304 onecap_copy(word, word_up, TRUE);
15305 has_word_up = TRUE;
15306 }
15307
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015308 byts = slang->sl_pbyts;
15309 idxs = slang->sl_pidxs;
15310 if (byts != NULL) /* array not is empty */
15311 {
15312 /*
15313 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015314 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015315 */
15316 depth = 0;
15317 arridx[0] = 0;
15318 curi[0] = 1;
15319 while (depth >= 0 && !got_int)
15320 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015321 n = arridx[depth];
15322 len = byts[n];
15323 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015324 {
15325 /* Done all bytes at this node, go up one level. */
15326 --depth;
15327 line_breakcheck();
15328 }
15329 else
15330 {
15331 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015332 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015333 ++curi[depth];
15334 c = byts[n];
15335 if (c == 0)
15336 {
15337 /* End of prefix, find out how many IDs there are. */
15338 for (i = 1; i < len; ++i)
15339 if (byts[n + i] != 0)
15340 break;
15341 curi[depth] += i - 1;
15342
Bram Moolenaar53805d12005-08-01 07:08:33 +000015343 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
15344 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015345 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000015346 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015347 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015348 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015349 : flags, lnum);
15350 if (lnum != 0)
15351 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015352 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000015353
15354 /* Check for prefix that matches the word when the
15355 * first letter is upper-case, but only if the prefix has
15356 * a condition. */
15357 if (has_word_up)
15358 {
15359 c = valid_word_prefix(i, n, flags, word_up, slang,
15360 TRUE);
15361 if (c != 0)
15362 {
15363 vim_strncpy(prefix + depth, word_up,
15364 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015365 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015366 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015367 : flags, lnum);
15368 if (lnum != 0)
15369 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +000015370 }
15371 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015372 }
15373 else
15374 {
15375 /* Normal char, go one level deeper. */
15376 prefix[depth++] = c;
15377 arridx[depth] = idxs[n];
15378 curi[depth] = 1;
15379 }
15380 }
15381 }
15382 }
15383
15384 return lnum;
15385}
15386
Bram Moolenaar95529562005-08-25 21:21:38 +000015387/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015388 * Move "p" to the end of word "start".
15389 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +000015390 */
15391 char_u *
15392spell_to_word_end(start, buf)
15393 char_u *start;
15394 buf_T *buf;
15395{
15396 char_u *p = start;
15397
15398 while (*p != NUL && spell_iswordp(p, buf))
15399 mb_ptr_adv(p);
15400 return p;
15401}
15402
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015403#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015404/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015405 * For Insert mode completion CTRL-X s:
15406 * Find start of the word in front of column "startcol".
15407 * We don't check if it is badly spelled, with completion we can only change
15408 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015409 * Returns the column number of the word.
15410 */
15411 int
15412spell_word_start(startcol)
15413 int startcol;
15414{
15415 char_u *line;
15416 char_u *p;
15417 int col = 0;
15418
Bram Moolenaar95529562005-08-25 21:21:38 +000015419 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015420 return startcol;
15421
15422 /* Find a word character before "startcol". */
15423 line = ml_get_curline();
15424 for (p = line + startcol; p > line; )
15425 {
15426 mb_ptr_back(line, p);
15427 if (spell_iswordp_nmw(p))
15428 break;
15429 }
15430
15431 /* Go back to start of the word. */
15432 while (p > line)
15433 {
15434 col = p - line;
15435 mb_ptr_back(line, p);
15436 if (!spell_iswordp(p, curbuf))
15437 break;
15438 col = 0;
15439 }
15440
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015441 return col;
15442}
15443
15444/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000015445 * Need to check for 'spellcapcheck' now, the word is removed before
15446 * expand_spelling() is called. Therefore the ugly global variable.
15447 */
15448static int spell_expand_need_cap;
15449
15450 void
15451spell_expand_check_cap(col)
15452 colnr_T col;
15453{
15454 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
15455}
15456
15457/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015458 * Get list of spelling suggestions.
15459 * Used for Insert mode completion CTRL-X ?.
15460 * Returns the number of matches. The matches are in "matchp[]", array of
15461 * allocated strings.
15462 */
15463/*ARGSUSED*/
15464 int
15465expand_spelling(lnum, col, pat, matchp)
15466 linenr_T lnum;
15467 int col;
15468 char_u *pat;
15469 char_u ***matchp;
15470{
15471 garray_T ga;
15472
Bram Moolenaar4770d092006-01-12 23:22:24 +000015473 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015474 *matchp = ga.ga_data;
15475 return ga.ga_len;
15476}
15477#endif
15478
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000015479#endif /* FEAT_SYN_HL */