blob: 090b77f2ddd4c54804fb7a8800536617ff339b0a [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 Moolenaar9ba0eb82005-06-13 22:28:56 +00009759 }
9760
Bram Moolenaard12a1322005-08-21 22:08:24 +00009761 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
9762 {
9763 /* Save the from and to text for :spellrepall. */
9764 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00009765 if (sug.su_badlen > stp->st_orglen)
9766 {
9767 /* Replacing less than "su_badlen", append the remainder to
9768 * repl_to. */
9769 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
9770 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
9771 sug.su_badlen - stp->st_orglen,
9772 sug.su_badptr + stp->st_orglen);
9773 repl_to = vim_strsave(IObuff);
9774 }
9775 else
9776 {
9777 /* Replacing su_badlen or more, use the whole word. */
9778 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
9779 repl_to = vim_strsave(stp->st_word);
9780 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00009781
9782 /* Replace the word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009783 p = alloc(STRLEN(line) - stp->st_orglen + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +00009784 if (p != NULL)
9785 {
9786 c = sug.su_badptr - line;
9787 mch_memmove(p, line, c);
9788 STRCPY(p + c, stp->st_word);
9789 STRCAT(p, sug.su_badptr + stp->st_orglen);
9790 ml_replace(curwin->w_cursor.lnum, p, FALSE);
9791 curwin->w_cursor.col = c;
9792 changed_bytes(curwin->w_cursor.lnum, c);
9793
9794 /* For redo we use a change-word command. */
9795 ResetRedobuff();
9796 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +00009797 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +00009798 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +00009799 AppendCharToRedobuff(ESC);
9800 }
9801 }
9802 else
9803 curwin->w_cursor = prev_cursor;
9804
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009805 spell_find_cleanup(&sug);
9806}
9807
9808/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009809 * Check if the word at line "lnum" column "col" is required to start with a
9810 * capital. This uses 'spellcapcheck' of the current buffer.
9811 */
9812 static int
9813check_need_cap(lnum, col)
9814 linenr_T lnum;
9815 colnr_T col;
9816{
9817 int need_cap = FALSE;
9818 char_u *line;
9819 char_u *line_copy = NULL;
9820 char_u *p;
9821 colnr_T endcol;
9822 regmatch_T regmatch;
9823
9824 if (curbuf->b_cap_prog == NULL)
9825 return FALSE;
9826
9827 line = ml_get_curline();
9828 endcol = 0;
9829 if ((int)(skipwhite(line) - line) >= (int)col)
9830 {
9831 /* At start of line, check if previous line is empty or sentence
9832 * ends there. */
9833 if (lnum == 1)
9834 need_cap = TRUE;
9835 else
9836 {
9837 line = ml_get(lnum - 1);
9838 if (*skipwhite(line) == NUL)
9839 need_cap = TRUE;
9840 else
9841 {
9842 /* Append a space in place of the line break. */
9843 line_copy = concat_str(line, (char_u *)" ");
9844 line = line_copy;
9845 endcol = STRLEN(line);
9846 }
9847 }
9848 }
9849 else
9850 endcol = col;
9851
9852 if (endcol > 0)
9853 {
9854 /* Check if sentence ends before the bad word. */
9855 regmatch.regprog = curbuf->b_cap_prog;
9856 regmatch.rm_ic = FALSE;
9857 p = line + endcol;
9858 for (;;)
9859 {
9860 mb_ptr_back(line, p);
9861 if (p == line || spell_iswordp_nmw(p))
9862 break;
9863 if (vim_regexec(&regmatch, p, 0)
9864 && regmatch.endp[0] == line + endcol)
9865 {
9866 need_cap = TRUE;
9867 break;
9868 }
9869 }
9870 }
9871
9872 vim_free(line_copy);
9873
9874 return need_cap;
9875}
9876
9877
9878/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009879 * ":spellrepall"
9880 */
9881/*ARGSUSED*/
9882 void
9883ex_spellrepall(eap)
9884 exarg_T *eap;
9885{
9886 pos_T pos = curwin->w_cursor;
9887 char_u *frompat;
9888 int addlen;
9889 char_u *line;
9890 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009891 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009892 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009893
9894 if (repl_from == NULL || repl_to == NULL)
9895 {
9896 EMSG(_("E752: No previous spell replacement"));
9897 return;
9898 }
9899 addlen = STRLEN(repl_to) - STRLEN(repl_from);
9900
9901 frompat = alloc(STRLEN(repl_from) + 7);
9902 if (frompat == NULL)
9903 return;
9904 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
9905 p_ws = FALSE;
9906
Bram Moolenaar5195e452005-08-19 20:32:47 +00009907 sub_nsubs = 0;
9908 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009909 curwin->w_cursor.lnum = 0;
9910 while (!got_int)
9911 {
9912 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
9913 || u_save_cursor() == FAIL)
9914 break;
9915
9916 /* Only replace when the right word isn't there yet. This happens
9917 * when changing "etc" to "etc.". */
9918 line = ml_get_curline();
9919 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
9920 repl_to, STRLEN(repl_to)) != 0)
9921 {
9922 p = alloc(STRLEN(line) + addlen + 1);
9923 if (p == NULL)
9924 break;
9925 mch_memmove(p, line, curwin->w_cursor.col);
9926 STRCPY(p + curwin->w_cursor.col, repl_to);
9927 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
9928 ml_replace(curwin->w_cursor.lnum, p, FALSE);
9929 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009930
9931 if (curwin->w_cursor.lnum != prev_lnum)
9932 {
9933 ++sub_nlines;
9934 prev_lnum = curwin->w_cursor.lnum;
9935 }
9936 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009937 }
9938 curwin->w_cursor.col += STRLEN(repl_to);
9939 }
9940
9941 p_ws = save_ws;
9942 curwin->w_cursor = pos;
9943 vim_free(frompat);
9944
Bram Moolenaar5195e452005-08-19 20:32:47 +00009945 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009946 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009947 else
9948 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009949}
9950
9951/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009952 * Find spell suggestions for "word". Return them in the growarray "*gap" as
9953 * a list of allocated strings.
9954 */
9955 void
Bram Moolenaar4770d092006-01-12 23:22:24 +00009956spell_suggest_list(gap, word, maxcount, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009957 garray_T *gap;
9958 char_u *word;
9959 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009960 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009961 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009962{
9963 suginfo_T sug;
9964 int i;
9965 suggest_T *stp;
9966 char_u *wcopy;
9967
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009968 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009969
9970 /* Make room in "gap". */
9971 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00009972 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009973 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00009974 for (i = 0; i < sug.su_ga.ga_len; ++i)
9975 {
9976 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009977
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00009978 /* The suggested word may replace only part of "word", add the not
9979 * replaced part. */
9980 wcopy = alloc(stp->st_wordlen
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009981 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00009982 if (wcopy == NULL)
9983 break;
9984 STRCPY(wcopy, stp->st_word);
9985 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
9986 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
9987 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009988 }
9989
9990 spell_find_cleanup(&sug);
9991}
9992
9993/*
9994 * Find spell suggestions for the word at the start of "badptr".
9995 * Return the suggestions in "su->su_ga".
9996 * The maximum number of suggestions is "maxcount".
9997 * Note: does use info for the current window.
9998 * This is based on the mechanisms of Aspell, but completely reimplemented.
9999 */
10000 static void
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010001spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010002 char_u *badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010003 int badlen; /* length of bad word or 0 if unknown */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010004 suginfo_T *su;
10005 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +000010006 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010007 int need_cap; /* word should start with capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010008 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010009{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010010 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010011 char_u buf[MAXPATHL];
10012 char_u *p;
10013 int do_combine = FALSE;
10014 char_u *sps_copy;
10015#ifdef FEAT_EVAL
10016 static int expr_busy = FALSE;
10017#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010018 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010019 int i;
10020 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010021
10022 /*
10023 * Set the info in "*su".
10024 */
10025 vim_memset(su, 0, sizeof(suginfo_T));
10026 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
10027 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010028 if (*badptr == NUL)
10029 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010030 hash_init(&su->su_banned);
10031
10032 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010033 if (badlen != 0)
10034 su->su_badlen = badlen;
10035 else
10036 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010037 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010038 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010039
10040 if (su->su_badlen >= MAXWLEN)
10041 su->su_badlen = MAXWLEN - 1; /* just in case */
10042 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
10043 (void)spell_casefold(su->su_badptr, su->su_badlen,
10044 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010045 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010046 su->su_badflags = badword_captype(su->su_badptr,
10047 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010048 if (need_cap)
10049 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010050
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010051 /* Find the default language for sound folding. We simply use the first
10052 * one in 'spelllang' that supports sound folding. That's good for when
10053 * using multiple files for one language, it's not that bad when mixing
10054 * languages (e.g., "pl,en"). */
10055 for (i = 0; i < curbuf->b_langp.ga_len; ++i)
10056 {
10057 lp = LANGP_ENTRY(curbuf->b_langp, i);
10058 if (lp->lp_sallang != NULL)
10059 {
10060 su->su_sallang = lp->lp_sallang;
10061 break;
10062 }
10063 }
10064
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010065 /* Soundfold the bad word with the default sound folding, so that we don't
10066 * have to do this many times. */
10067 if (su->su_sallang != NULL)
10068 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
10069 su->su_sal_badword);
10070
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010071 /* If the word is not capitalised and spell_check() doesn't consider the
10072 * word to be bad then it might need to be capitalised. Add a suggestion
10073 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010074 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010075 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010076 {
10077 make_case_word(su->su_badword, buf, WF_ONECAP);
10078 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010079 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010080 }
10081
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010082 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +000010083 if (banbadword)
10084 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010085
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010086 /* Make a copy of 'spellsuggest', because the expression may change it. */
10087 sps_copy = vim_strsave(p_sps);
10088 if (sps_copy == NULL)
10089 return;
10090
10091 /* Loop over the items in 'spellsuggest'. */
10092 for (p = sps_copy; *p != NUL; )
10093 {
10094 copy_option_part(&p, buf, MAXPATHL, ",");
10095
10096 if (STRNCMP(buf, "expr:", 5) == 0)
10097 {
10098#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010099 /* Evaluate an expression. Skip this when called recursively,
10100 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010101 if (!expr_busy)
10102 {
10103 expr_busy = TRUE;
10104 spell_suggest_expr(su, buf + 5);
10105 expr_busy = FALSE;
10106 }
10107#endif
10108 }
10109 else if (STRNCMP(buf, "file:", 5) == 0)
10110 /* Use list of suggestions in a file. */
10111 spell_suggest_file(su, buf + 5);
10112 else
10113 {
10114 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010115 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010116 if (sps_flags & SPS_DOUBLE)
10117 do_combine = TRUE;
10118 }
10119 }
10120
10121 vim_free(sps_copy);
10122
10123 if (do_combine)
10124 /* Combine the two list of suggestions. This must be done last,
10125 * because sorting changes the order again. */
10126 score_combine(su);
10127}
10128
10129#ifdef FEAT_EVAL
10130/*
10131 * Find suggestions by evaluating expression "expr".
10132 */
10133 static void
10134spell_suggest_expr(su, expr)
10135 suginfo_T *su;
10136 char_u *expr;
10137{
10138 list_T *list;
10139 listitem_T *li;
10140 int score;
10141 char_u *p;
10142
10143 /* The work is split up in a few parts to avoid having to export
10144 * suginfo_T.
10145 * First evaluate the expression and get the resulting list. */
10146 list = eval_spell_expr(su->su_badword, expr);
10147 if (list != NULL)
10148 {
10149 /* Loop over the items in the list. */
10150 for (li = list->lv_first; li != NULL; li = li->li_next)
10151 if (li->li_tv.v_type == VAR_LIST)
10152 {
10153 /* Get the word and the score from the items. */
10154 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010155 if (score >= 0 && score <= su->su_maxscore)
10156 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10157 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010158 }
10159 list_unref(list);
10160 }
10161
Bram Moolenaar4770d092006-01-12 23:22:24 +000010162 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10163 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010164 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10165}
10166#endif
10167
10168/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010169 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010170 */
10171 static void
10172spell_suggest_file(su, fname)
10173 suginfo_T *su;
10174 char_u *fname;
10175{
10176 FILE *fd;
10177 char_u line[MAXWLEN * 2];
10178 char_u *p;
10179 int len;
10180 char_u cword[MAXWLEN];
10181
10182 /* Open the file. */
10183 fd = mch_fopen((char *)fname, "r");
10184 if (fd == NULL)
10185 {
10186 EMSG2(_(e_notopen), fname);
10187 return;
10188 }
10189
10190 /* Read it line by line. */
10191 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
10192 {
10193 line_breakcheck();
10194
10195 p = vim_strchr(line, '/');
10196 if (p == NULL)
10197 continue; /* No Tab found, just skip the line. */
10198 *p++ = NUL;
10199 if (STRICMP(su->su_badword, line) == 0)
10200 {
10201 /* Match! Isolate the good word, until CR or NL. */
10202 for (len = 0; p[len] >= ' '; ++len)
10203 ;
10204 p[len] = NUL;
10205
10206 /* If the suggestion doesn't have specific case duplicate the case
10207 * of the bad word. */
10208 if (captype(p, NULL) == 0)
10209 {
10210 make_case_word(p, cword, su->su_badflags);
10211 p = cword;
10212 }
10213
10214 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010215 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010216 }
10217 }
10218
10219 fclose(fd);
10220
Bram Moolenaar4770d092006-01-12 23:22:24 +000010221 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10222 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010223 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10224}
10225
10226/*
10227 * Find suggestions for the internal method indicated by "sps_flags".
10228 */
10229 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010230spell_suggest_intern(su, interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010231 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010232 int interactive;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010233{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010234 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010235 * Load the .sug file(s) that are available and not done yet.
10236 */
10237 suggest_load_files();
10238
10239 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010240 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010241 *
10242 * Set a maximum score to limit the combination of operations that is
10243 * tried.
10244 */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010245 suggest_try_special(su);
10246
10247 /*
10248 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10249 * from the .aff file and inserting a space (split the word).
10250 */
10251 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010252
10253 /* For the resulting top-scorers compute the sound-a-like score. */
10254 if (sps_flags & SPS_DOUBLE)
10255 score_comp_sal(su);
10256
10257 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010258 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010259 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010260 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010261 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010262 if (sps_flags & SPS_BEST)
10263 /* Adjust the word score for the suggestions found so far for how
10264 * they sounds like. */
10265 rescore_suggestions(su);
10266
10267 /*
10268 * While going throught the soundfold tree "su_maxscore" is the score
10269 * for the soundfold word, limits the changes that are being tried,
10270 * and "su_sfmaxscore" the rescored score, which is set by
10271 * cleanup_suggestions().
10272 * First find words with a small edit distance, because this is much
10273 * faster and often already finds the top-N suggestions. If we didn't
10274 * find many suggestions try again with a higher edit distance.
10275 * "sl_sounddone" is used to avoid doing the same word twice.
10276 */
10277 suggest_try_soundalike_prep();
10278 su->su_maxscore = SCORE_SFMAX1;
10279 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010280 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010281 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10282 {
10283 /* We didn't find enough matches, try again, allowing more
10284 * changes to the soundfold word. */
10285 su->su_maxscore = SCORE_SFMAX2;
10286 suggest_try_soundalike(su);
10287 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10288 {
10289 /* Still didn't find enough matches, try again, allowing even
10290 * more changes to the soundfold word. */
10291 su->su_maxscore = SCORE_SFMAX3;
10292 suggest_try_soundalike(su);
10293 }
10294 }
10295 su->su_maxscore = su->su_sfmaxscore;
10296 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010297 }
10298
Bram Moolenaar4770d092006-01-12 23:22:24 +000010299 /* When CTRL-C was hit while searching do show the results. Only clear
10300 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010301 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +000010302 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010303 {
10304 (void)vgetc();
10305 got_int = FALSE;
10306 }
10307
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010308 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010309 {
10310 if (sps_flags & SPS_BEST)
10311 /* Adjust the word score for how it sounds like. */
10312 rescore_suggestions(su);
10313
Bram Moolenaar4770d092006-01-12 23:22:24 +000010314 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10315 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010316 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010317 }
10318}
10319
10320/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010321 * Load the .sug files for languages that have one and weren't loaded yet.
10322 */
10323 static void
10324suggest_load_files()
10325{
10326 langp_T *lp;
10327 int lpi;
10328 slang_T *slang;
10329 char_u *dotp;
10330 FILE *fd;
10331 char_u buf[MAXWLEN];
10332 int i;
10333 time_t timestamp;
10334 int wcount;
10335 int wordnr;
10336 garray_T ga;
10337 int c;
10338
10339 /* Do this for all languages that support sound folding. */
10340 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
10341 {
10342 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
10343 slang = lp->lp_slang;
10344 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10345 {
10346 /* Change ".spl" to ".sug" and open the file. When the file isn't
10347 * found silently skip it. Do set "sl_sugloaded" so that we
10348 * don't try again and again. */
10349 slang->sl_sugloaded = TRUE;
10350
10351 dotp = vim_strrchr(slang->sl_fname, '.');
10352 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10353 continue;
10354 STRCPY(dotp, ".sug");
10355 fd = fopen((char *)slang->sl_fname, "r");
10356 if (fd == NULL)
10357 goto nextone;
10358
10359 /*
10360 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10361 */
10362 for (i = 0; i < VIMSUGMAGICL; ++i)
10363 buf[i] = getc(fd); /* <fileID> */
10364 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10365 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010366 EMSG2(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010367 slang->sl_fname);
10368 goto nextone;
10369 }
10370 c = getc(fd); /* <versionnr> */
10371 if (c < VIMSUGVERSION)
10372 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010373 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010374 slang->sl_fname);
10375 goto nextone;
10376 }
10377 else if (c > VIMSUGVERSION)
10378 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010379 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010380 slang->sl_fname);
10381 goto nextone;
10382 }
10383
10384 /* Check the timestamp, it must be exactly the same as the one in
10385 * the .spl file. Otherwise the word numbers won't match. */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010386 timestamp = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010387 if (timestamp != slang->sl_sugtime)
10388 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010389 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010390 slang->sl_fname);
10391 goto nextone;
10392 }
10393
10394 /*
10395 * <SUGWORDTREE>: <wordtree>
10396 * Read the trie with the soundfolded words.
10397 */
10398 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
10399 FALSE, 0) != 0)
10400 {
10401someerror:
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010402 EMSG2(_("E782: error while reading .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010403 slang->sl_fname);
10404 slang_clear_sug(slang);
10405 goto nextone;
10406 }
10407
10408 /*
10409 * <SUGTABLE>: <sugwcount> <sugline> ...
10410 *
10411 * Read the table with word numbers. We use a file buffer for
10412 * this, because it's so much like a file with lines. Makes it
10413 * possible to swap the info and save on memory use.
10414 */
10415 slang->sl_sugbuf = open_spellbuf();
10416 if (slang->sl_sugbuf == NULL)
10417 goto someerror;
10418 /* <sugwcount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010419 wcount = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010420 if (wcount < 0)
10421 goto someerror;
10422
10423 /* Read all the wordnr lists into the buffer, one NUL terminated
10424 * list per line. */
10425 ga_init2(&ga, 1, 100);
10426 for (wordnr = 0; wordnr < wcount; ++wordnr)
10427 {
10428 ga.ga_len = 0;
10429 for (;;)
10430 {
10431 c = getc(fd); /* <sugline> */
10432 if (c < 0 || ga_grow(&ga, 1) == FAIL)
10433 goto someerror;
10434 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
10435 if (c == NUL)
10436 break;
10437 }
10438 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
10439 ga.ga_data, ga.ga_len, TRUE) == FAIL)
10440 goto someerror;
10441 }
10442 ga_clear(&ga);
10443
10444 /*
10445 * Need to put word counts in the word tries, so that we can find
10446 * a word by its number.
10447 */
10448 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
10449 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
10450
10451nextone:
10452 if (fd != NULL)
10453 fclose(fd);
10454 STRCPY(dotp, ".spl");
10455 }
10456 }
10457}
10458
10459
10460/*
10461 * Fill in the wordcount fields for a trie.
10462 * Returns the total number of words.
10463 */
10464 static void
10465tree_count_words(byts, idxs)
10466 char_u *byts;
10467 idx_T *idxs;
10468{
10469 int depth;
10470 idx_T arridx[MAXWLEN];
10471 int curi[MAXWLEN];
10472 int c;
10473 idx_T n;
10474 int wordcount[MAXWLEN];
10475
10476 arridx[0] = 0;
10477 curi[0] = 1;
10478 wordcount[0] = 0;
10479 depth = 0;
10480 while (depth >= 0 && !got_int)
10481 {
10482 if (curi[depth] > byts[arridx[depth]])
10483 {
10484 /* Done all bytes at this node, go up one level. */
10485 idxs[arridx[depth]] = wordcount[depth];
10486 if (depth > 0)
10487 wordcount[depth - 1] += wordcount[depth];
10488
10489 --depth;
10490 fast_breakcheck();
10491 }
10492 else
10493 {
10494 /* Do one more byte at this node. */
10495 n = arridx[depth] + curi[depth];
10496 ++curi[depth];
10497
10498 c = byts[n];
10499 if (c == 0)
10500 {
10501 /* End of word, count it. */
10502 ++wordcount[depth];
10503
10504 /* Skip over any other NUL bytes (same word with different
10505 * flags). */
10506 while (byts[n + 1] == 0)
10507 {
10508 ++n;
10509 ++curi[depth];
10510 }
10511 }
10512 else
10513 {
10514 /* Normal char, go one level deeper to count the words. */
10515 ++depth;
10516 arridx[depth] = idxs[n];
10517 curi[depth] = 1;
10518 wordcount[depth] = 0;
10519 }
10520 }
10521 }
10522}
10523
10524/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010525 * Free the info put in "*su" by spell_find_suggest().
10526 */
10527 static void
10528spell_find_cleanup(su)
10529 suginfo_T *su;
10530{
10531 int i;
10532
10533 /* Free the suggestions. */
10534 for (i = 0; i < su->su_ga.ga_len; ++i)
10535 vim_free(SUG(su->su_ga, i).st_word);
10536 ga_clear(&su->su_ga);
10537 for (i = 0; i < su->su_sga.ga_len; ++i)
10538 vim_free(SUG(su->su_sga, i).st_word);
10539 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010540
10541 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010542 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010543}
10544
10545/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010546 * Make a copy of "word", with the first letter upper or lower cased, to
10547 * "wcopy[MAXWLEN]". "word" must not be empty.
10548 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010549 */
10550 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010551onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010552 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010553 char_u *wcopy;
10554 int upper; /* TRUE: first letter made upper case */
10555{
10556 char_u *p;
10557 int c;
10558 int l;
10559
10560 p = word;
10561#ifdef FEAT_MBYTE
10562 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010563 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010564 else
10565#endif
10566 c = *p++;
10567 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010568 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010569 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010570 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010571#ifdef FEAT_MBYTE
10572 if (has_mbyte)
10573 l = mb_char2bytes(c, wcopy);
10574 else
10575#endif
10576 {
10577 l = 1;
10578 wcopy[0] = c;
10579 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010580 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010581}
10582
10583/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010584 * Make a copy of "word" with all the letters upper cased into
10585 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010586 */
10587 static void
10588allcap_copy(word, wcopy)
10589 char_u *word;
10590 char_u *wcopy;
10591{
10592 char_u *s;
10593 char_u *d;
10594 int c;
10595
10596 d = wcopy;
10597 for (s = word; *s != NUL; )
10598 {
10599#ifdef FEAT_MBYTE
10600 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010601 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010602 else
10603#endif
10604 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +000010605
10606#ifdef FEAT_MBYTE
10607 /* We only change ß to SS when we are certain latin1 is used. It
10608 * would cause weird errors in other 8-bit encodings. */
10609 if (enc_latin1like && c == 0xdf)
10610 {
10611 c = 'S';
10612 if (d - wcopy >= MAXWLEN - 1)
10613 break;
10614 *d++ = c;
10615 }
10616 else
10617#endif
10618 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010619
10620#ifdef FEAT_MBYTE
10621 if (has_mbyte)
10622 {
10623 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
10624 break;
10625 d += mb_char2bytes(c, d);
10626 }
10627 else
10628#endif
10629 {
10630 if (d - wcopy >= MAXWLEN - 1)
10631 break;
10632 *d++ = c;
10633 }
10634 }
10635 *d = NUL;
10636}
10637
10638/*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010639 * Try finding suggestions by recognizing specific situations.
10640 */
10641 static void
10642suggest_try_special(su)
10643 suginfo_T *su;
10644{
10645 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010646 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010647 int c;
10648 char_u word[MAXWLEN];
10649
10650 /*
10651 * Recognize a word that is repeated: "the the".
10652 */
10653 p = skiptowhite(su->su_fbadword);
10654 len = p - su->su_fbadword;
10655 p = skipwhite(p);
10656 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
10657 {
10658 /* Include badflags: if the badword is onecap or allcap
10659 * use that for the goodword too: "The the" -> "The". */
10660 c = su->su_fbadword[len];
10661 su->su_fbadword[len] = NUL;
10662 make_case_word(su->su_fbadword, word, su->su_badflags);
10663 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010664
10665 /* Give a soundalike score of 0, compute the score as if deleting one
10666 * character. */
10667 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010668 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010669 }
10670}
10671
10672/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010673 * Try finding suggestions by adding/removing/swapping letters.
10674 */
10675 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000010676suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010677 suginfo_T *su;
10678{
10679 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010680 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010681 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010682 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010683 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010684
10685 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +000010686 * to find matches (esp. REP items). Append some more text, changing
10687 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010688 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010689 n = STRLEN(fword);
10690 p = su->su_badptr + su->su_badlen;
10691 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010692
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010693 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010694 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010695 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010696
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010697 /* If reloading a spell file fails it's still in the list but
10698 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010699 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010700 continue;
10701
Bram Moolenaar4770d092006-01-12 23:22:24 +000010702 /* Try it for this language. Will add possible suggestions. */
10703 suggest_trie_walk(su, lp, fword, FALSE);
10704 }
10705}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010706
Bram Moolenaar4770d092006-01-12 23:22:24 +000010707/* Check the maximum score, if we go over it we won't try this change. */
10708#define TRY_DEEPER(su, stack, depth, add) \
10709 (stack[depth].ts_score + (add) < su->su_maxscore)
10710
10711/*
10712 * Try finding suggestions by adding/removing/swapping letters.
10713 *
10714 * This uses a state machine. At each node in the tree we try various
10715 * operations. When trying if an operation works "depth" is increased and the
10716 * stack[] is used to store info. This allows combinations, thus insert one
10717 * character, replace one and delete another. The number of changes is
10718 * limited by su->su_maxscore.
10719 *
10720 * After implementing this I noticed an article by Kemal Oflazer that
10721 * describes something similar: "Error-tolerant Finite State Recognition with
10722 * Applications to Morphological Analysis and Spelling Correction" (1996).
10723 * The implementation in the article is simplified and requires a stack of
10724 * unknown depth. The implementation here only needs a stack depth equal to
10725 * the length of the word.
10726 *
10727 * This is also used for the sound-folded word, "soundfold" is TRUE then.
10728 * The mechanism is the same, but we find a match with a sound-folded word
10729 * that comes from one or more original words. Each of these words may be
10730 * added, this is done by add_sound_suggest().
10731 * Don't use:
10732 * the prefix tree or the keep-case tree
10733 * "su->su_badlen"
10734 * anything to do with upper and lower case
10735 * anything to do with word or non-word characters ("spell_iswordp()")
10736 * banned words
10737 * word flags (rare, region, compounding)
10738 * word splitting for now
10739 * "similar_chars()"
10740 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
10741 */
10742 static void
10743suggest_trie_walk(su, lp, fword, soundfold)
10744 suginfo_T *su;
10745 langp_T *lp;
10746 char_u *fword;
10747 int soundfold;
10748{
10749 char_u tword[MAXWLEN]; /* good word collected so far */
10750 trystate_T stack[MAXWLEN];
10751 char_u preword[MAXWLEN * 3]; /* word found with proper case;
10752 * concatanation of prefix compound
10753 * words and split word. NUL terminated
10754 * when going deeper but not when coming
10755 * back. */
10756 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
10757 trystate_T *sp;
10758 int newscore;
10759 int score;
10760 char_u *byts, *fbyts, *pbyts;
10761 idx_T *idxs, *fidxs, *pidxs;
10762 int depth;
10763 int c, c2, c3;
10764 int n = 0;
10765 int flags;
10766 garray_T *gap;
10767 idx_T arridx;
10768 int len;
10769 char_u *p;
10770 fromto_T *ftp;
10771 int fl = 0, tl;
10772 int repextra = 0; /* extra bytes in fword[] from REP item */
10773 slang_T *slang = lp->lp_slang;
10774 int fword_ends;
10775 int goodword_ends;
10776#ifdef DEBUG_TRIEWALK
10777 /* Stores the name of the change made at each level. */
10778 char_u changename[MAXWLEN][80];
10779#endif
10780 int breakcheckcount = 1000;
10781 int compound_ok;
10782
10783 /*
10784 * Go through the whole case-fold tree, try changes at each node.
10785 * "tword[]" contains the word collected from nodes in the tree.
10786 * "fword[]" the word we are trying to match with (initially the bad
10787 * word).
10788 */
10789 depth = 0;
10790 sp = &stack[0];
10791 vim_memset(sp, 0, sizeof(trystate_T));
10792 sp->ts_curi = 1;
10793
10794 if (soundfold)
10795 {
10796 /* Going through the soundfold tree. */
10797 byts = fbyts = slang->sl_sbyts;
10798 idxs = fidxs = slang->sl_sidxs;
10799 pbyts = NULL;
10800 pidxs = NULL;
10801 sp->ts_prefixdepth = PFD_NOPREFIX;
10802 sp->ts_state = STATE_START;
10803 }
10804 else
10805 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010806 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010807 * When there are postponed prefixes we need to use these first. At
10808 * the end of the prefix we continue in the case-fold tree.
10809 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010810 fbyts = slang->sl_fbyts;
10811 fidxs = slang->sl_fidxs;
10812 pbyts = slang->sl_pbyts;
10813 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010814 if (pbyts != NULL)
10815 {
10816 byts = pbyts;
10817 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010818 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010819 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
10820 }
10821 else
10822 {
10823 byts = fbyts;
10824 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010825 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010826 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010827 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000010828 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010829
Bram Moolenaar4770d092006-01-12 23:22:24 +000010830 /*
10831 * Loop to find all suggestions. At each round we either:
10832 * - For the current state try one operation, advance "ts_curi",
10833 * increase "depth".
10834 * - When a state is done go to the next, set "ts_state".
10835 * - When all states are tried decrease "depth".
10836 */
10837 while (depth >= 0 && !got_int)
10838 {
10839 sp = &stack[depth];
10840 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010841 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010842 case STATE_START:
10843 case STATE_NOPREFIX:
10844 /*
10845 * Start of node: Deal with NUL bytes, which means
10846 * tword[] may end here.
10847 */
10848 arridx = sp->ts_arridx; /* current node in the tree */
10849 len = byts[arridx]; /* bytes in this node */
10850 arridx += sp->ts_curi; /* index of current byte */
10851
10852 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010853 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010854 /* Skip over the NUL bytes, we use them later. */
10855 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
10856 ;
10857 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010858
Bram Moolenaar4770d092006-01-12 23:22:24 +000010859 /* Always past NUL bytes now. */
10860 n = (int)sp->ts_state;
10861 sp->ts_state = STATE_ENDNUL;
10862 sp->ts_save_badflags = su->su_badflags;
10863
10864 /* At end of a prefix or at start of prefixtree: check for
10865 * following word. */
10866 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010867 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010868 /* Set su->su_badflags to the caps type at this position.
10869 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010870#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000010871 if (has_mbyte)
10872 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
10873 else
Bram Moolenaar53805d12005-08-01 07:08:33 +000010874#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000010875 n = sp->ts_fidx;
10876 flags = badword_captype(su->su_badptr, su->su_badptr + n);
10877 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000010878 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010879#ifdef DEBUG_TRIEWALK
10880 sprintf(changename[depth], "prefix");
10881#endif
10882 go_deeper(stack, depth, 0);
10883 ++depth;
10884 sp = &stack[depth];
10885 sp->ts_prefixdepth = depth - 1;
10886 byts = fbyts;
10887 idxs = fidxs;
10888 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010889
Bram Moolenaar4770d092006-01-12 23:22:24 +000010890 /* Move the prefix to preword[] with the right case
10891 * and make find_keepcap_word() works. */
10892 tword[sp->ts_twordlen] = NUL;
10893 make_case_word(tword + sp->ts_splitoff,
10894 preword + sp->ts_prewordlen, flags);
10895 sp->ts_prewordlen = STRLEN(preword);
10896 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010897 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000010898 break;
10899 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010900
Bram Moolenaar4770d092006-01-12 23:22:24 +000010901 if (sp->ts_curi > len || byts[arridx] != 0)
10902 {
10903 /* Past bytes in node and/or past NUL bytes. */
10904 sp->ts_state = STATE_ENDNUL;
10905 sp->ts_save_badflags = su->su_badflags;
10906 break;
10907 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010908
Bram Moolenaar4770d092006-01-12 23:22:24 +000010909 /*
10910 * End of word in tree.
10911 */
10912 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010913
Bram Moolenaar4770d092006-01-12 23:22:24 +000010914 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010915
10916 /* Skip words with the NOSUGGEST flag. */
10917 if (flags & WF_NOSUGGEST)
10918 break;
10919
Bram Moolenaar4770d092006-01-12 23:22:24 +000010920 fword_ends = (fword[sp->ts_fidx] == NUL
10921 || (soundfold
10922 ? vim_iswhite(fword[sp->ts_fidx])
10923 : !spell_iswordp(fword + sp->ts_fidx, curbuf)));
10924 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010925
Bram Moolenaar4770d092006-01-12 23:22:24 +000010926 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +000010927 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010928 {
10929 /* There was a prefix before the word. Check that the prefix
10930 * can be used with this word. */
10931 /* Count the length of the NULs in the prefix. If there are
10932 * none this must be the first try without a prefix. */
10933 n = stack[sp->ts_prefixdepth].ts_arridx;
10934 len = pbyts[n++];
10935 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
10936 ;
10937 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010938 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010939 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000010940 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010941 if (c == 0)
10942 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010943
Bram Moolenaar4770d092006-01-12 23:22:24 +000010944 /* Use the WF_RARE flag for a rare prefix. */
10945 if (c & WF_RAREPFX)
10946 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010947
Bram Moolenaar4770d092006-01-12 23:22:24 +000010948 /* Tricky: when checking for both prefix and compounding
10949 * we run into the prefix flag first.
10950 * Remember that it's OK, so that we accept the prefix
10951 * when arriving at a compound flag. */
10952 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010953 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000010954 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010955
Bram Moolenaar4770d092006-01-12 23:22:24 +000010956 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
10957 * appending another compound word below. */
10958 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010959 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +000010960 goodword_ends = FALSE;
10961 else
10962 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010963
Bram Moolenaar4770d092006-01-12 23:22:24 +000010964 p = NULL;
10965 compound_ok = TRUE;
10966 if (sp->ts_complen > sp->ts_compsplit)
10967 {
10968 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +000010969 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010970 /* There was a word before this word. When there was no
10971 * change in this word (it was correct) add the first word
10972 * as a suggestion. If this word was corrected too, we
10973 * need to check if a correct word follows. */
10974 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +000010975 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +000010976 && STRNCMP(fword + sp->ts_splitfidx,
10977 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +000010978 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010979 {
10980 preword[sp->ts_prewordlen] = NUL;
10981 newscore = score_wordcount_adj(slang, sp->ts_score,
10982 preword + sp->ts_prewordlen,
10983 sp->ts_prewordlen > 0);
10984 /* Add the suggestion if the score isn't too bad. */
10985 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +000010986 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010987 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010988 newscore, 0, FALSE,
10989 lp->lp_sallang, FALSE);
10990 break;
Bram Moolenaar78622822005-08-23 21:00:13 +000010991 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000010992 }
Bram Moolenaare52325c2005-08-22 22:54:29 +000010993 else
Bram Moolenaar0c405862005-06-22 22:26:26 +000010994 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010995 /* There was a compound word before this word. If this
10996 * word does not support compounding then give up
10997 * (splitting is tried for the word without compound
10998 * flag). */
10999 if (((unsigned)flags >> 24) == 0
11000 || sp->ts_twordlen - sp->ts_splitoff
11001 < slang->sl_compminlen)
11002 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011003#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011004 /* For multi-byte chars check character length against
11005 * COMPOUNDMIN. */
11006 if (has_mbyte
11007 && slang->sl_compminlen > 0
11008 && mb_charlen(tword + sp->ts_splitoff)
11009 < slang->sl_compminlen)
11010 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011011#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +000011012
Bram Moolenaar4770d092006-01-12 23:22:24 +000011013 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11014 compflags[sp->ts_complen + 1] = NUL;
11015 vim_strncpy(preword + sp->ts_prewordlen,
11016 tword + sp->ts_splitoff,
11017 sp->ts_twordlen - sp->ts_splitoff);
11018 p = preword;
11019 while (*skiptowhite(p) != NUL)
11020 p = skipwhite(skiptowhite(p));
11021 if (fword_ends && !can_compound(slang, p,
11022 compflags + sp->ts_compsplit))
11023 /* Compound is not allowed. But it may still be
11024 * possible if we add another (short) word. */
11025 compound_ok = FALSE;
11026
11027 /* Get pointer to last char of previous word. */
11028 p = preword + sp->ts_prewordlen;
11029 mb_ptr_back(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011030 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011031 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011032
Bram Moolenaar4770d092006-01-12 23:22:24 +000011033 /*
11034 * Form the word with proper case in preword.
11035 * If there is a word from a previous split, append.
11036 * For the soundfold tree don't change the case, simply append.
11037 */
11038 if (soundfold)
11039 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
11040 else if (flags & WF_KEEPCAP)
11041 /* Must find the word in the keep-case tree. */
11042 find_keepcap_word(slang, tword + sp->ts_splitoff,
11043 preword + sp->ts_prewordlen);
11044 else
11045 {
11046 /* Include badflags: If the badword is onecap or allcap
11047 * use that for the goodword too. But if the badword is
11048 * allcap and it's only one char long use onecap. */
11049 c = su->su_badflags;
11050 if ((c & WF_ALLCAP)
11051#ifdef FEAT_MBYTE
11052 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
11053#else
11054 && su->su_badlen == 1
11055#endif
11056 )
11057 c = WF_ONECAP;
11058 c |= flags;
11059
11060 /* When appending a compound word after a word character don't
11061 * use Onecap. */
11062 if (p != NULL && spell_iswordp_nmw(p))
11063 c &= ~WF_ONECAP;
11064 make_case_word(tword + sp->ts_splitoff,
11065 preword + sp->ts_prewordlen, c);
11066 }
11067
11068 if (!soundfold)
11069 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011070 /* Don't use a banned word. It may appear again as a good
11071 * word, thus remember it. */
11072 if (flags & WF_BANNED)
11073 {
Bram Moolenaar5195e452005-08-19 20:32:47 +000011074 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011075 break;
11076 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011077 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +000011078 && WAS_BANNED(su, preword + sp->ts_prewordlen))
11079 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011080 {
11081 if (slang->sl_compprog == NULL)
11082 break;
11083 /* the word so far was banned but we may try compounding */
11084 goodword_ends = FALSE;
11085 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011086 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011087
Bram Moolenaar4770d092006-01-12 23:22:24 +000011088 newscore = 0;
11089 if (!soundfold) /* soundfold words don't have flags */
11090 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011091 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011092 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011093 newscore += SCORE_REGION;
11094 if (flags & WF_RARE)
11095 newscore += SCORE_RARE;
11096
Bram Moolenaar0c405862005-06-22 22:26:26 +000011097 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011098 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011099 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011100 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011101
Bram Moolenaar4770d092006-01-12 23:22:24 +000011102 /* TODO: how about splitting in the soundfold tree? */
11103 if (fword_ends
11104 && goodword_ends
11105 && sp->ts_fidx >= sp->ts_fidxtry
11106 && compound_ok)
11107 {
11108 /* The badword also ends: add suggestions. */
11109#ifdef DEBUG_TRIEWALK
11110 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011111 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011112 int j;
11113
11114 /* print the stack of changes that brought us here */
11115 smsg("------ %s -------", fword);
11116 for (j = 0; j < depth; ++j)
11117 smsg("%s", changename[j]);
11118 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011119#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011120 if (soundfold)
11121 {
11122 /* For soundfolded words we need to find the original
11123 * words, the edit distrance and then add them. */
11124 add_sound_suggest(su, preword, sp->ts_score, lp);
11125 }
11126 else
11127 {
11128 /* Give a penalty when changing non-word char to word
11129 * char, e.g., "thes," -> "these". */
11130 p = fword + sp->ts_fidx;
11131 mb_ptr_back(fword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011132 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011133 {
11134 p = preword + STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011135 mb_ptr_back(preword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011136 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011137 newscore += SCORE_NONWORD;
11138 }
11139
Bram Moolenaar4770d092006-01-12 23:22:24 +000011140 /* Give a bonus to words seen before. */
11141 score = score_wordcount_adj(slang,
11142 sp->ts_score + newscore,
11143 preword + sp->ts_prewordlen,
11144 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011145
Bram Moolenaar4770d092006-01-12 23:22:24 +000011146 /* Add the suggestion if the score isn't too bad. */
11147 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011148 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011149 add_suggestion(su, &su->su_ga, preword,
11150 sp->ts_fidx - repextra,
11151 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011152
11153 if (su->su_badflags & WF_MIXCAP)
11154 {
11155 /* We really don't know if the word should be
11156 * upper or lower case, add both. */
11157 c = captype(preword, NULL);
11158 if (c == 0 || c == WF_ALLCAP)
11159 {
11160 make_case_word(tword + sp->ts_splitoff,
11161 preword + sp->ts_prewordlen,
11162 c == 0 ? WF_ALLCAP : 0);
11163
11164 add_suggestion(su, &su->su_ga, preword,
11165 sp->ts_fidx - repextra,
11166 score + SCORE_ICASE, 0, FALSE,
11167 lp->lp_sallang, FALSE);
11168 }
11169 }
11170 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011171 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011172 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011173
Bram Moolenaar4770d092006-01-12 23:22:24 +000011174 /*
11175 * Try word split and/or compounding.
11176 */
11177 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +000011178#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011179 /* Don't split halfway a character. */
11180 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011181#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011182 )
11183 {
11184 int try_compound;
11185 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011186
Bram Moolenaar4770d092006-01-12 23:22:24 +000011187 /* If past the end of the bad word don't try a split.
11188 * Otherwise try changing the next word. E.g., find
11189 * suggestions for "the the" where the second "the" is
11190 * different. It's done like a split.
11191 * TODO: word split for soundfold words */
11192 try_split = (sp->ts_fidx - repextra < su->su_badlen)
11193 && !soundfold;
11194
11195 /* Get here in several situations:
11196 * 1. The word in the tree ends:
11197 * If the word allows compounding try that. Otherwise try
11198 * a split by inserting a space. For both check that a
11199 * valid words starts at fword[sp->ts_fidx].
11200 * For NOBREAK do like compounding to be able to check if
11201 * the next word is valid.
11202 * 2. The badword does end, but it was due to a change (e.g.,
11203 * a swap). No need to split, but do check that the
11204 * following word is valid.
11205 * 3. The badword and the word in the tree end. It may still
11206 * be possible to compound another (short) word.
11207 */
11208 try_compound = FALSE;
11209 if (!soundfold
11210 && slang->sl_compprog != NULL
11211 && ((unsigned)flags >> 24) != 0
11212 && sp->ts_twordlen - sp->ts_splitoff
11213 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011214#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011215 && (!has_mbyte
11216 || slang->sl_compminlen == 0
11217 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011218 >= slang->sl_compminlen)
11219#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011220 && (slang->sl_compsylmax < MAXWLEN
11221 || sp->ts_complen + 1 - sp->ts_compsplit
11222 < slang->sl_compmax)
11223 && (byte_in_str(sp->ts_complen == sp->ts_compsplit
11224 ? slang->sl_compstartflags
11225 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +000011226 ((unsigned)flags >> 24))))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011227 {
11228 try_compound = TRUE;
11229 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11230 compflags[sp->ts_complen + 1] = NUL;
11231 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011232
Bram Moolenaar4770d092006-01-12 23:22:24 +000011233 /* For NOBREAK we never try splitting, it won't make any word
11234 * valid. */
11235 if (slang->sl_nobreak)
11236 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +000011237
Bram Moolenaar4770d092006-01-12 23:22:24 +000011238 /* If we could add a compound word, and it's also possible to
11239 * split at this point, do the split first and set
11240 * TSF_DIDSPLIT to avoid doing it again. */
11241 else if (!fword_ends
11242 && try_compound
11243 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11244 {
11245 try_compound = FALSE;
11246 sp->ts_flags |= TSF_DIDSPLIT;
11247 --sp->ts_curi; /* do the same NUL again */
11248 compflags[sp->ts_complen] = NUL;
11249 }
11250 else
11251 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011252
Bram Moolenaar4770d092006-01-12 23:22:24 +000011253 if (try_split || try_compound)
11254 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011255 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +000011256 {
11257 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011258 * words so far are valid for compounding. If there
11259 * is only one word it must not have the NEEDCOMPOUND
11260 * flag. */
11261 if (sp->ts_complen == sp->ts_compsplit
11262 && (flags & WF_NEEDCOMP))
11263 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +000011264 p = preword;
11265 while (*skiptowhite(p) != NUL)
11266 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +000011267 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +000011268 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +000011269 compflags + sp->ts_compsplit))
11270 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011271
11272 if (slang->sl_nosplitsugs)
11273 newscore += SCORE_SPLIT_NO;
11274 else
11275 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011276
11277 /* Give a bonus to words seen before. */
11278 newscore = score_wordcount_adj(slang, newscore,
11279 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011280 }
11281
Bram Moolenaar4770d092006-01-12 23:22:24 +000011282 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011283 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011284 go_deeper(stack, depth, newscore);
11285#ifdef DEBUG_TRIEWALK
11286 if (!try_compound && !fword_ends)
11287 sprintf(changename[depth], "%.*s-%s: split",
11288 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11289 else
11290 sprintf(changename[depth], "%.*s-%s: compound",
11291 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11292#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011293 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000011294 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011295 sp->ts_state = STATE_SPLITUNDO;
11296
11297 ++depth;
11298 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011299
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011300 /* Append a space to preword when splitting. */
11301 if (!try_compound && !fword_ends)
11302 STRCAT(preword, " ");
Bram Moolenaar5195e452005-08-19 20:32:47 +000011303 sp->ts_prewordlen = STRLEN(preword);
11304 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +000011305 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011306
11307 /* If the badword has a non-word character at this
11308 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011309 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011310 * character when the word ends. But only when the
11311 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011312 if (((!try_compound && !spell_iswordp_nmw(fword
11313 + sp->ts_fidx))
11314 || fword_ends)
11315 && fword[sp->ts_fidx] != NUL
11316 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011317 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011318 int l;
11319
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011320#ifdef FEAT_MBYTE
11321 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011322 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011323 else
11324#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011325 l = 1;
11326 if (fword_ends)
11327 {
11328 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +000011329 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011330 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011331 sp->ts_prewordlen += l;
11332 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011333 }
11334 else
11335 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
11336 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011337 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011338
Bram Moolenaard12a1322005-08-21 22:08:24 +000011339 /* When compounding include compound flag in
11340 * compflags[] (already set above). When splitting we
11341 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011342 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +000011343 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011344 else
Bram Moolenaard12a1322005-08-21 22:08:24 +000011345 sp->ts_compsplit = sp->ts_complen;
11346 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011347
Bram Moolenaar53805d12005-08-01 07:08:33 +000011348 /* set su->su_badflags to the caps type at this
11349 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011350#ifdef FEAT_MBYTE
11351 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +000011352 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011353 else
11354#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +000011355 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011356 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011357 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011358
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011359 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011360 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011361
11362 /* If there are postponed prefixes, try these too. */
11363 if (pbyts != NULL)
11364 {
11365 byts = pbyts;
11366 idxs = pidxs;
11367 sp->ts_prefixdepth = PFD_PREFIXTREE;
11368 sp->ts_state = STATE_NOPREFIX;
11369 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011370 }
11371 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011372 }
11373 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011374
Bram Moolenaar4770d092006-01-12 23:22:24 +000011375 case STATE_SPLITUNDO:
11376 /* Undo the changes done for word split or compound word. */
11377 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011378
Bram Moolenaar4770d092006-01-12 23:22:24 +000011379 /* Continue looking for NUL bytes. */
11380 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011381
Bram Moolenaar4770d092006-01-12 23:22:24 +000011382 /* In case we went into the prefix tree. */
11383 byts = fbyts;
11384 idxs = fidxs;
11385 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011386
Bram Moolenaar4770d092006-01-12 23:22:24 +000011387 case STATE_ENDNUL:
11388 /* Past the NUL bytes in the node. */
11389 su->su_badflags = sp->ts_save_badflags;
11390 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011391#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011392 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011393#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011394 )
11395 {
11396 /* The badword ends, can't use STATE_PLAIN. */
11397 sp->ts_state = STATE_DEL;
11398 break;
11399 }
11400 sp->ts_state = STATE_PLAIN;
11401 /*FALLTHROUGH*/
11402
11403 case STATE_PLAIN:
11404 /*
11405 * Go over all possible bytes at this node, add each to tword[]
11406 * and use child node. "ts_curi" is the index.
11407 */
11408 arridx = sp->ts_arridx;
11409 if (sp->ts_curi > byts[arridx])
11410 {
11411 /* Done all bytes at this node, do next state. When still at
11412 * already changed bytes skip the other tricks. */
11413 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011414 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011415 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011416 sp->ts_state = STATE_FINAL;
11417 }
11418 else
11419 {
11420 arridx += sp->ts_curi++;
11421 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011422
Bram Moolenaar4770d092006-01-12 23:22:24 +000011423 /* Normal byte, go one level deeper. If it's not equal to the
11424 * byte in the bad word adjust the score. But don't even try
11425 * when the byte was already changed. And don't try when we
11426 * just deleted this byte, accepting it is always cheaper then
11427 * delete + substitute. */
11428 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +000011429#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011430 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011431#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011432 )
11433 newscore = 0;
11434 else
11435 newscore = SCORE_SUBST;
11436 if ((newscore == 0
11437 || (sp->ts_fidx >= sp->ts_fidxtry
11438 && ((sp->ts_flags & TSF_DIDDEL) == 0
11439 || c != fword[sp->ts_delidx])))
11440 && TRY_DEEPER(su, stack, depth, newscore))
11441 {
11442 go_deeper(stack, depth, newscore);
11443#ifdef DEBUG_TRIEWALK
11444 if (newscore > 0)
11445 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
11446 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11447 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011448 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011449 sprintf(changename[depth], "%.*s-%s: accept %c",
11450 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11451 fword[sp->ts_fidx]);
11452#endif
11453 ++depth;
11454 sp = &stack[depth];
11455 ++sp->ts_fidx;
11456 tword[sp->ts_twordlen++] = c;
11457 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +000011458#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011459 if (newscore == SCORE_SUBST)
11460 sp->ts_isdiff = DIFF_YES;
11461 if (has_mbyte)
11462 {
11463 /* Multi-byte characters are a bit complicated to
11464 * handle: They differ when any of the bytes differ
11465 * and then their length may also differ. */
11466 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011467 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011468 /* First byte. */
11469 sp->ts_tcharidx = 0;
11470 sp->ts_tcharlen = MB_BYTE2LEN(c);
11471 sp->ts_fcharstart = sp->ts_fidx - 1;
11472 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011473 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011474 }
11475 else if (sp->ts_isdiff == DIFF_INSERT)
11476 /* When inserting trail bytes don't advance in the
11477 * bad word. */
11478 --sp->ts_fidx;
11479 if (++sp->ts_tcharidx == sp->ts_tcharlen)
11480 {
11481 /* Last byte of character. */
11482 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +000011483 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011484 /* Correct ts_fidx for the byte length of the
11485 * character (we didn't check that before). */
11486 sp->ts_fidx = sp->ts_fcharstart
11487 + MB_BYTE2LEN(
Bram Moolenaarea424162005-06-16 21:51:00 +000011488 fword[sp->ts_fcharstart]);
11489
Bram Moolenaar4770d092006-01-12 23:22:24 +000011490 /* For changing a composing character adjust
11491 * the score from SCORE_SUBST to
11492 * SCORE_SUBCOMP. */
11493 if (enc_utf8
11494 && utf_iscomposing(
11495 mb_ptr2char(tword
11496 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011497 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011498 && utf_iscomposing(
11499 mb_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011500 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011501 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011502 SCORE_SUBST - SCORE_SUBCOMP;
11503
Bram Moolenaar4770d092006-01-12 23:22:24 +000011504 /* For a similar character adjust score from
11505 * SCORE_SUBST to SCORE_SIMILAR. */
11506 else if (!soundfold
11507 && slang->sl_has_map
11508 && similar_chars(slang,
11509 mb_ptr2char(tword
11510 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +000011511 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +000011512 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +000011513 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011514 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +000011515 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +000011516 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011517 else if (sp->ts_isdiff == DIFF_INSERT
11518 && sp->ts_twordlen > sp->ts_tcharlen)
11519 {
11520 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
11521 c = mb_ptr2char(p);
11522 if (enc_utf8 && utf_iscomposing(c))
11523 {
11524 /* Inserting a composing char doesn't
11525 * count that much. */
11526 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
11527 }
11528 else
11529 {
11530 /* If the previous character was the same,
11531 * thus doubling a character, give a bonus
11532 * to the score. Also for the soundfold
11533 * tree (might seem illogical but does
11534 * give better scores). */
11535 mb_ptr_back(tword, p);
11536 if (c == mb_ptr2char(p))
11537 sp->ts_score -= SCORE_INS
11538 - SCORE_INSDUP;
11539 }
11540 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011541
Bram Moolenaar4770d092006-01-12 23:22:24 +000011542 /* Starting a new char, reset the length. */
11543 sp->ts_tcharlen = 0;
11544 }
Bram Moolenaarea408852005-06-25 22:49:46 +000011545 }
Bram Moolenaarea424162005-06-16 21:51:00 +000011546 else
11547#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000011548 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011549 /* If we found a similar char adjust the score.
11550 * We do this after calling go_deeper() because
11551 * it's slow. */
11552 if (newscore != 0
11553 && !soundfold
11554 && slang->sl_has_map
11555 && similar_chars(slang,
11556 c, fword[sp->ts_fidx - 1]))
11557 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +000011558 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011559 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011560 }
11561 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011562
Bram Moolenaar4770d092006-01-12 23:22:24 +000011563 case STATE_DEL:
11564#ifdef FEAT_MBYTE
11565 /* When past the first byte of a multi-byte char don't try
11566 * delete/insert/swap a character. */
11567 if (has_mbyte && sp->ts_tcharlen > 0)
11568 {
11569 sp->ts_state = STATE_FINAL;
11570 break;
11571 }
11572#endif
11573 /*
11574 * Try skipping one character in the bad word (delete it).
11575 */
11576 sp->ts_state = STATE_INS_PREP;
11577 sp->ts_curi = 1;
11578 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
11579 /* Deleting a vowel at the start of a word counts less, see
11580 * soundalike_score(). */
11581 newscore = 2 * SCORE_DEL / 3;
11582 else
11583 newscore = SCORE_DEL;
11584 if (fword[sp->ts_fidx] != NUL
11585 && TRY_DEEPER(su, stack, depth, newscore))
11586 {
11587 go_deeper(stack, depth, newscore);
11588#ifdef DEBUG_TRIEWALK
11589 sprintf(changename[depth], "%.*s-%s: delete %c",
11590 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11591 fword[sp->ts_fidx]);
11592#endif
11593 ++depth;
11594
11595 /* Remember what character we deleted, so that we can avoid
11596 * inserting it again. */
11597 stack[depth].ts_flags |= TSF_DIDDEL;
11598 stack[depth].ts_delidx = sp->ts_fidx;
11599
11600 /* Advance over the character in fword[]. Give a bonus to the
11601 * score if the same character is following "nn" -> "n". It's
11602 * a bit illogical for soundfold tree but it does give better
11603 * results. */
11604#ifdef FEAT_MBYTE
11605 if (has_mbyte)
11606 {
11607 c = mb_ptr2char(fword + sp->ts_fidx);
11608 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
11609 if (enc_utf8 && utf_iscomposing(c))
11610 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
11611 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
11612 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
11613 }
11614 else
11615#endif
11616 {
11617 ++stack[depth].ts_fidx;
11618 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
11619 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
11620 }
11621 break;
11622 }
11623 /*FALLTHROUGH*/
11624
11625 case STATE_INS_PREP:
11626 if (sp->ts_flags & TSF_DIDDEL)
11627 {
11628 /* If we just deleted a byte then inserting won't make sense,
11629 * a substitute is always cheaper. */
11630 sp->ts_state = STATE_SWAP;
11631 break;
11632 }
11633
11634 /* skip over NUL bytes */
11635 n = sp->ts_arridx;
11636 for (;;)
11637 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011638 if (sp->ts_curi > byts[n])
11639 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011640 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011641 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011642 break;
11643 }
11644 if (byts[n + sp->ts_curi] != NUL)
11645 {
11646 /* Found a byte to insert. */
11647 sp->ts_state = STATE_INS;
11648 break;
11649 }
11650 ++sp->ts_curi;
11651 }
11652 break;
11653
11654 /*FALLTHROUGH*/
11655
11656 case STATE_INS:
11657 /* Insert one byte. Repeat this for each possible byte at this
11658 * node. */
11659 n = sp->ts_arridx;
11660 if (sp->ts_curi > byts[n])
11661 {
11662 /* Done all bytes at this node, go to next state. */
11663 sp->ts_state = STATE_SWAP;
11664 break;
11665 }
11666
11667 /* Do one more byte at this node, but:
11668 * - Skip NUL bytes.
11669 * - Skip the byte if it's equal to the byte in the word,
11670 * accepting that byte is always better.
11671 */
11672 n += sp->ts_curi++;
11673 c = byts[n];
11674 if (soundfold && sp->ts_twordlen == 0 && c == '*')
11675 /* Inserting a vowel at the start of a word counts less,
11676 * see soundalike_score(). */
11677 newscore = 2 * SCORE_INS / 3;
11678 else
11679 newscore = SCORE_INS;
11680 if (c != fword[sp->ts_fidx]
11681 && TRY_DEEPER(su, stack, depth, newscore))
11682 {
11683 go_deeper(stack, depth, newscore);
11684#ifdef DEBUG_TRIEWALK
11685 sprintf(changename[depth], "%.*s-%s: insert %c",
11686 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11687 c);
11688#endif
11689 ++depth;
11690 sp = &stack[depth];
11691 tword[sp->ts_twordlen++] = c;
11692 sp->ts_arridx = idxs[n];
11693#ifdef FEAT_MBYTE
11694 if (has_mbyte)
11695 {
11696 fl = MB_BYTE2LEN(c);
11697 if (fl > 1)
11698 {
11699 /* There are following bytes for the same character.
11700 * We must find all bytes before trying
11701 * delete/insert/swap/etc. */
11702 sp->ts_tcharlen = fl;
11703 sp->ts_tcharidx = 1;
11704 sp->ts_isdiff = DIFF_INSERT;
11705 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011706 }
11707 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011708 fl = 1;
11709 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000011710#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011711 {
11712 /* If the previous character was the same, thus doubling a
11713 * character, give a bonus to the score. Also for
11714 * soundfold words (illogical but does give a better
11715 * score). */
11716 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +000011717 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011718 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011719 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011720 }
11721 break;
11722
11723 case STATE_SWAP:
11724 /*
11725 * Swap two bytes in the bad word: "12" -> "21".
11726 * We change "fword" here, it's changed back afterwards at
11727 * STATE_UNSWAP.
11728 */
11729 p = fword + sp->ts_fidx;
11730 c = *p;
11731 if (c == NUL)
11732 {
11733 /* End of word, can't swap or replace. */
11734 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011735 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011736 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011737
Bram Moolenaar4770d092006-01-12 23:22:24 +000011738 /* Don't swap if the first character is not a word character.
11739 * SWAP3 etc. also don't make sense then. */
11740 if (!soundfold && !spell_iswordp(p, curbuf))
11741 {
11742 sp->ts_state = STATE_REP_INI;
11743 break;
11744 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000011745
Bram Moolenaar4770d092006-01-12 23:22:24 +000011746#ifdef FEAT_MBYTE
11747 if (has_mbyte)
11748 {
11749 n = mb_cptr2len(p);
11750 c = mb_ptr2char(p);
11751 if (!soundfold && !spell_iswordp(p + n, curbuf))
11752 c2 = c; /* don't swap non-word char */
11753 else
11754 c2 = mb_ptr2char(p + n);
11755 }
11756 else
11757#endif
11758 {
11759 if (!soundfold && !spell_iswordp(p + 1, curbuf))
11760 c2 = c; /* don't swap non-word char */
11761 else
11762 c2 = p[1];
11763 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000011764
Bram Moolenaar4770d092006-01-12 23:22:24 +000011765 /* When characters are identical, swap won't do anything.
11766 * Also get here if the second char is not a word character. */
11767 if (c == c2)
11768 {
11769 sp->ts_state = STATE_SWAP3;
11770 break;
11771 }
11772 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
11773 {
11774 go_deeper(stack, depth, SCORE_SWAP);
11775#ifdef DEBUG_TRIEWALK
11776 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
11777 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11778 c, c2);
11779#endif
11780 sp->ts_state = STATE_UNSWAP;
11781 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000011782#ifdef FEAT_MBYTE
11783 if (has_mbyte)
11784 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011785 fl = mb_char2len(c2);
11786 mch_memmove(p, p + n, fl);
11787 mb_char2bytes(c, p + fl);
11788 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000011789 }
11790 else
11791#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000011792 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011793 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +000011794 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011795 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +000011796 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011797 }
11798 else
11799 /* If this swap doesn't work then SWAP3 won't either. */
11800 sp->ts_state = STATE_REP_INI;
11801 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000011802
Bram Moolenaar4770d092006-01-12 23:22:24 +000011803 case STATE_UNSWAP:
11804 /* Undo the STATE_SWAP swap: "21" -> "12". */
11805 p = fword + sp->ts_fidx;
11806#ifdef FEAT_MBYTE
11807 if (has_mbyte)
11808 {
11809 n = MB_BYTE2LEN(*p);
11810 c = mb_ptr2char(p + n);
11811 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
11812 mb_char2bytes(c, p);
11813 }
11814 else
11815#endif
11816 {
11817 c = *p;
11818 *p = p[1];
11819 p[1] = c;
11820 }
11821 /*FALLTHROUGH*/
11822
11823 case STATE_SWAP3:
11824 /* Swap two bytes, skipping one: "123" -> "321". We change
11825 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
11826 p = fword + sp->ts_fidx;
11827#ifdef FEAT_MBYTE
11828 if (has_mbyte)
11829 {
11830 n = mb_cptr2len(p);
11831 c = mb_ptr2char(p);
11832 fl = mb_cptr2len(p + n);
11833 c2 = mb_ptr2char(p + n);
11834 if (!soundfold && !spell_iswordp(p + n + fl, curbuf))
11835 c3 = c; /* don't swap non-word char */
11836 else
11837 c3 = mb_ptr2char(p + n + fl);
11838 }
11839 else
11840#endif
11841 {
11842 c = *p;
11843 c2 = p[1];
11844 if (!soundfold && !spell_iswordp(p + 2, curbuf))
11845 c3 = c; /* don't swap non-word char */
11846 else
11847 c3 = p[2];
11848 }
11849
11850 /* When characters are identical: "121" then SWAP3 result is
11851 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
11852 * same as SWAP on next char: "112". Thus skip all swapping.
11853 * Also skip when c3 is NUL.
11854 * Also get here when the third character is not a word character.
11855 * Second character may any char: "a.b" -> "b.a" */
11856 if (c == c3 || c3 == NUL)
11857 {
11858 sp->ts_state = STATE_REP_INI;
11859 break;
11860 }
11861 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
11862 {
11863 go_deeper(stack, depth, SCORE_SWAP3);
11864#ifdef DEBUG_TRIEWALK
11865 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
11866 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11867 c, c3);
11868#endif
11869 sp->ts_state = STATE_UNSWAP3;
11870 ++depth;
11871#ifdef FEAT_MBYTE
11872 if (has_mbyte)
11873 {
11874 tl = mb_char2len(c3);
11875 mch_memmove(p, p + n + fl, tl);
11876 mb_char2bytes(c2, p + tl);
11877 mb_char2bytes(c, p + fl + tl);
11878 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
11879 }
11880 else
11881#endif
11882 {
11883 p[0] = p[2];
11884 p[2] = c;
11885 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
11886 }
11887 }
11888 else
11889 sp->ts_state = STATE_REP_INI;
11890 break;
11891
11892 case STATE_UNSWAP3:
11893 /* Undo STATE_SWAP3: "321" -> "123" */
11894 p = fword + sp->ts_fidx;
11895#ifdef FEAT_MBYTE
11896 if (has_mbyte)
11897 {
11898 n = MB_BYTE2LEN(*p);
11899 c2 = mb_ptr2char(p + n);
11900 fl = MB_BYTE2LEN(p[n]);
11901 c = mb_ptr2char(p + n + fl);
11902 tl = MB_BYTE2LEN(p[n + fl]);
11903 mch_memmove(p + fl + tl, p, n);
11904 mb_char2bytes(c, p);
11905 mb_char2bytes(c2, p + tl);
11906 p = p + tl;
11907 }
11908 else
11909#endif
11910 {
11911 c = *p;
11912 *p = p[2];
11913 p[2] = c;
11914 ++p;
11915 }
11916
11917 if (!soundfold && !spell_iswordp(p, curbuf))
11918 {
11919 /* Middle char is not a word char, skip the rotate. First and
11920 * third char were already checked at swap and swap3. */
11921 sp->ts_state = STATE_REP_INI;
11922 break;
11923 }
11924
11925 /* Rotate three characters left: "123" -> "231". We change
11926 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
11927 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
11928 {
11929 go_deeper(stack, depth, SCORE_SWAP3);
11930#ifdef DEBUG_TRIEWALK
11931 p = fword + sp->ts_fidx;
11932 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
11933 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11934 p[0], p[1], p[2]);
11935#endif
11936 sp->ts_state = STATE_UNROT3L;
11937 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000011938 p = fword + sp->ts_fidx;
11939#ifdef FEAT_MBYTE
11940 if (has_mbyte)
11941 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011942 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000011943 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011944 fl = mb_cptr2len(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011945 fl += mb_cptr2len(p + n + fl);
11946 mch_memmove(p, p + n, fl);
11947 mb_char2bytes(c, p + fl);
11948 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000011949 }
11950 else
11951#endif
11952 {
11953 c = *p;
11954 *p = p[1];
11955 p[1] = p[2];
11956 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011957 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +000011958 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011959 }
11960 else
11961 sp->ts_state = STATE_REP_INI;
11962 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011963
Bram Moolenaar4770d092006-01-12 23:22:24 +000011964 case STATE_UNROT3L:
11965 /* Undo ROT3L: "231" -> "123" */
11966 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +000011967#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011968 if (has_mbyte)
11969 {
11970 n = MB_BYTE2LEN(*p);
11971 n += MB_BYTE2LEN(p[n]);
11972 c = mb_ptr2char(p + n);
11973 tl = MB_BYTE2LEN(p[n]);
11974 mch_memmove(p + tl, p, n);
11975 mb_char2bytes(c, p);
11976 }
11977 else
Bram Moolenaarea424162005-06-16 21:51:00 +000011978#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011979 {
11980 c = p[2];
11981 p[2] = p[1];
11982 p[1] = *p;
11983 *p = c;
11984 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011985
Bram Moolenaar4770d092006-01-12 23:22:24 +000011986 /* Rotate three bytes right: "123" -> "312". We change "fword"
11987 * here, it's changed back afterwards at STATE_UNROT3R. */
11988 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
11989 {
11990 go_deeper(stack, depth, SCORE_SWAP3);
11991#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011992 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011993 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
11994 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11995 p[0], p[1], p[2]);
11996#endif
11997 sp->ts_state = STATE_UNROT3R;
11998 ++depth;
11999 p = fword + sp->ts_fidx;
12000#ifdef FEAT_MBYTE
12001 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000012002 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012003 n = mb_cptr2len(p);
12004 n += mb_cptr2len(p + n);
12005 c = mb_ptr2char(p + n);
12006 tl = mb_cptr2len(p + n);
12007 mch_memmove(p + tl, p, n);
12008 mb_char2bytes(c, p);
12009 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +000012010 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012011 else
12012#endif
12013 {
12014 c = p[2];
12015 p[2] = p[1];
12016 p[1] = *p;
12017 *p = c;
12018 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12019 }
12020 }
12021 else
12022 sp->ts_state = STATE_REP_INI;
12023 break;
12024
12025 case STATE_UNROT3R:
12026 /* Undo ROT3R: "312" -> "123" */
12027 p = fword + sp->ts_fidx;
12028#ifdef FEAT_MBYTE
12029 if (has_mbyte)
12030 {
12031 c = mb_ptr2char(p);
12032 tl = MB_BYTE2LEN(*p);
12033 n = MB_BYTE2LEN(p[tl]);
12034 n += MB_BYTE2LEN(p[tl + n]);
12035 mch_memmove(p, p + tl, n);
12036 mb_char2bytes(c, p + n);
12037 }
12038 else
12039#endif
12040 {
12041 c = *p;
12042 *p = p[1];
12043 p[1] = p[2];
12044 p[2] = c;
12045 }
12046 /*FALLTHROUGH*/
12047
12048 case STATE_REP_INI:
12049 /* Check if matching with REP items from the .aff file would work.
12050 * Quickly skip if:
12051 * - there are no REP items and we are not in the soundfold trie
12052 * - the score is going to be too high anyway
12053 * - already applied a REP item or swapped here */
12054 if ((lp->lp_replang == NULL && !soundfold)
12055 || sp->ts_score + SCORE_REP >= su->su_maxscore
12056 || sp->ts_fidx < sp->ts_fidxtry)
12057 {
12058 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012059 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012060 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012061
Bram Moolenaar4770d092006-01-12 23:22:24 +000012062 /* Use the first byte to quickly find the first entry that may
12063 * match. If the index is -1 there is none. */
12064 if (soundfold)
12065 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
12066 else
12067 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012068
Bram Moolenaar4770d092006-01-12 23:22:24 +000012069 if (sp->ts_curi < 0)
12070 {
12071 sp->ts_state = STATE_FINAL;
12072 break;
12073 }
12074
12075 sp->ts_state = STATE_REP;
12076 /*FALLTHROUGH*/
12077
12078 case STATE_REP:
12079 /* Try matching with REP items from the .aff file. For each match
12080 * replace the characters and check if the resulting word is
12081 * valid. */
12082 p = fword + sp->ts_fidx;
12083
12084 if (soundfold)
12085 gap = &slang->sl_repsal;
12086 else
12087 gap = &lp->lp_replang->sl_rep;
12088 while (sp->ts_curi < gap->ga_len)
12089 {
12090 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
12091 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012092 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012093 /* past possible matching entries */
12094 sp->ts_curi = gap->ga_len;
12095 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012096 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012097 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
12098 && TRY_DEEPER(su, stack, depth, SCORE_REP))
12099 {
12100 go_deeper(stack, depth, SCORE_REP);
12101#ifdef DEBUG_TRIEWALK
12102 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
12103 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12104 ftp->ft_from, ftp->ft_to);
12105#endif
12106 /* Need to undo this afterwards. */
12107 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012108
Bram Moolenaar4770d092006-01-12 23:22:24 +000012109 /* Change the "from" to the "to" string. */
12110 ++depth;
12111 fl = STRLEN(ftp->ft_from);
12112 tl = STRLEN(ftp->ft_to);
12113 if (fl != tl)
12114 {
12115 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
12116 repextra += tl - fl;
12117 }
12118 mch_memmove(p, ftp->ft_to, tl);
12119 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
12120#ifdef FEAT_MBYTE
12121 stack[depth].ts_tcharlen = 0;
12122#endif
12123 break;
12124 }
12125 }
12126
12127 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
12128 /* No (more) matches. */
12129 sp->ts_state = STATE_FINAL;
12130
12131 break;
12132
12133 case STATE_REP_UNDO:
12134 /* Undo a REP replacement and continue with the next one. */
12135 if (soundfold)
12136 gap = &slang->sl_repsal;
12137 else
12138 gap = &lp->lp_replang->sl_rep;
12139 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
12140 fl = STRLEN(ftp->ft_from);
12141 tl = STRLEN(ftp->ft_to);
12142 p = fword + sp->ts_fidx;
12143 if (fl != tl)
12144 {
12145 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
12146 repextra -= tl - fl;
12147 }
12148 mch_memmove(p, ftp->ft_from, fl);
12149 sp->ts_state = STATE_REP;
12150 break;
12151
12152 default:
12153 /* Did all possible states at this level, go up one level. */
12154 --depth;
12155
12156 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
12157 {
12158 /* Continue in or go back to the prefix tree. */
12159 byts = pbyts;
12160 idxs = pidxs;
12161 }
12162
12163 /* Don't check for CTRL-C too often, it takes time. */
12164 if (--breakcheckcount == 0)
12165 {
12166 ui_breakcheck();
12167 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012168 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012169 }
12170 }
12171}
12172
Bram Moolenaar4770d092006-01-12 23:22:24 +000012173
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012174/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012175 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012176 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012177 static void
12178go_deeper(stack, depth, score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012179 trystate_T *stack;
12180 int depth;
12181 int score_add;
12182{
Bram Moolenaarea424162005-06-16 21:51:00 +000012183 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012184 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012185 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012186 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000012187 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012188}
12189
Bram Moolenaar53805d12005-08-01 07:08:33 +000012190#ifdef FEAT_MBYTE
12191/*
12192 * Case-folding may change the number of bytes: Count nr of chars in
12193 * fword[flen] and return the byte length of that many chars in "word".
12194 */
12195 static int
12196nofold_len(fword, flen, word)
12197 char_u *fword;
12198 int flen;
12199 char_u *word;
12200{
12201 char_u *p;
12202 int i = 0;
12203
12204 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12205 ++i;
12206 for (p = word; i > 0; mb_ptr_adv(p))
12207 --i;
12208 return (int)(p - word);
12209}
12210#endif
12211
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012212/*
12213 * "fword" is a good word with case folded. Find the matching keep-case
12214 * words and put it in "kword".
12215 * Theoretically there could be several keep-case words that result in the
12216 * same case-folded word, but we only find one...
12217 */
12218 static void
12219find_keepcap_word(slang, fword, kword)
12220 slang_T *slang;
12221 char_u *fword;
12222 char_u *kword;
12223{
12224 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12225 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012226 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012227
12228 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012229 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012230 int round[MAXWLEN];
12231 int fwordidx[MAXWLEN];
12232 int uwordidx[MAXWLEN];
12233 int kwordlen[MAXWLEN];
12234
12235 int flen, ulen;
12236 int l;
12237 int len;
12238 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012239 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012240 char_u *p;
12241 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012242 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012243
12244 if (byts == NULL)
12245 {
12246 /* array is empty: "cannot happen" */
12247 *kword = NUL;
12248 return;
12249 }
12250
12251 /* Make an all-cap version of "fword". */
12252 allcap_copy(fword, uword);
12253
12254 /*
12255 * Each character needs to be tried both case-folded and upper-case.
12256 * All this gets very complicated if we keep in mind that changing case
12257 * may change the byte length of a multi-byte character...
12258 */
12259 depth = 0;
12260 arridx[0] = 0;
12261 round[0] = 0;
12262 fwordidx[0] = 0;
12263 uwordidx[0] = 0;
12264 kwordlen[0] = 0;
12265 while (depth >= 0)
12266 {
12267 if (fword[fwordidx[depth]] == NUL)
12268 {
12269 /* We are at the end of "fword". If the tree allows a word to end
12270 * here we have found a match. */
12271 if (byts[arridx[depth] + 1] == 0)
12272 {
12273 kword[kwordlen[depth]] = NUL;
12274 return;
12275 }
12276
12277 /* kword is getting too long, continue one level up */
12278 --depth;
12279 }
12280 else if (++round[depth] > 2)
12281 {
12282 /* tried both fold-case and upper-case character, continue one
12283 * level up */
12284 --depth;
12285 }
12286 else
12287 {
12288 /*
12289 * round[depth] == 1: Try using the folded-case character.
12290 * round[depth] == 2: Try using the upper-case character.
12291 */
12292#ifdef FEAT_MBYTE
12293 if (has_mbyte)
12294 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012295 flen = mb_cptr2len(fword + fwordidx[depth]);
12296 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012297 }
12298 else
12299#endif
12300 ulen = flen = 1;
12301 if (round[depth] == 1)
12302 {
12303 p = fword + fwordidx[depth];
12304 l = flen;
12305 }
12306 else
12307 {
12308 p = uword + uwordidx[depth];
12309 l = ulen;
12310 }
12311
12312 for (tryidx = arridx[depth]; l > 0; --l)
12313 {
12314 /* Perform a binary search in the list of accepted bytes. */
12315 len = byts[tryidx++];
12316 c = *p++;
12317 lo = tryidx;
12318 hi = tryidx + len - 1;
12319 while (lo < hi)
12320 {
12321 m = (lo + hi) / 2;
12322 if (byts[m] > c)
12323 hi = m - 1;
12324 else if (byts[m] < c)
12325 lo = m + 1;
12326 else
12327 {
12328 lo = hi = m;
12329 break;
12330 }
12331 }
12332
12333 /* Stop if there is no matching byte. */
12334 if (hi < lo || byts[lo] != c)
12335 break;
12336
12337 /* Continue at the child (if there is one). */
12338 tryidx = idxs[lo];
12339 }
12340
12341 if (l == 0)
12342 {
12343 /*
12344 * Found the matching char. Copy it to "kword" and go a
12345 * level deeper.
12346 */
12347 if (round[depth] == 1)
12348 {
12349 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
12350 flen);
12351 kwordlen[depth + 1] = kwordlen[depth] + flen;
12352 }
12353 else
12354 {
12355 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
12356 ulen);
12357 kwordlen[depth + 1] = kwordlen[depth] + ulen;
12358 }
12359 fwordidx[depth + 1] = fwordidx[depth] + flen;
12360 uwordidx[depth + 1] = uwordidx[depth] + ulen;
12361
12362 ++depth;
12363 arridx[depth] = tryidx;
12364 round[depth] = 0;
12365 }
12366 }
12367 }
12368
12369 /* Didn't find it: "cannot happen". */
12370 *kword = NUL;
12371}
12372
12373/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012374 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
12375 * su->su_sga.
12376 */
12377 static void
12378score_comp_sal(su)
12379 suginfo_T *su;
12380{
12381 langp_T *lp;
12382 char_u badsound[MAXWLEN];
12383 int i;
12384 suggest_T *stp;
12385 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012386 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012387 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012388
12389 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
12390 return;
12391
12392 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012393 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012394 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012395 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012396 if (lp->lp_slang->sl_sal.ga_len > 0)
12397 {
12398 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012399 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012400
12401 for (i = 0; i < su->su_ga.ga_len; ++i)
12402 {
12403 stp = &SUG(su->su_ga, i);
12404
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012405 /* Case-fold the suggested word, sound-fold it and compute the
12406 * sound-a-like score. */
12407 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012408 if (score < SCORE_MAXMAX)
12409 {
12410 /* Add the suggestion. */
12411 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
12412 sstp->st_word = vim_strsave(stp->st_word);
12413 if (sstp->st_word != NULL)
12414 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012415 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012416 sstp->st_score = score;
12417 sstp->st_altscore = 0;
12418 sstp->st_orglen = stp->st_orglen;
12419 ++su->su_sga.ga_len;
12420 }
12421 }
12422 }
12423 break;
12424 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012425 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012426}
12427
12428/*
12429 * Combine the list of suggestions in su->su_ga and su->su_sga.
12430 * They are intwined.
12431 */
12432 static void
12433score_combine(su)
12434 suginfo_T *su;
12435{
12436 int i;
12437 int j;
12438 garray_T ga;
12439 garray_T *gap;
12440 langp_T *lp;
12441 suggest_T *stp;
12442 char_u *p;
12443 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012444 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012445 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012446 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012447
12448 /* Add the alternate score to su_ga. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012449 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012450 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012451 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012452 if (lp->lp_slang->sl_sal.ga_len > 0)
12453 {
12454 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012455 slang = lp->lp_slang;
12456 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012457
12458 for (i = 0; i < su->su_ga.ga_len; ++i)
12459 {
12460 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012461 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012462 if (stp->st_altscore == SCORE_MAXMAX)
12463 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
12464 else
12465 stp->st_score = (stp->st_score * 3
12466 + stp->st_altscore) / 4;
12467 stp->st_salscore = FALSE;
12468 }
12469 break;
12470 }
12471 }
12472
Bram Moolenaar4770d092006-01-12 23:22:24 +000012473 if (slang == NULL) /* just in case */
12474 return;
12475
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012476 /* Add the alternate score to su_sga. */
12477 for (i = 0; i < su->su_sga.ga_len; ++i)
12478 {
12479 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012480 stp->st_altscore = spell_edit_score(slang,
12481 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012482 if (stp->st_score == SCORE_MAXMAX)
12483 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
12484 else
12485 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
12486 stp->st_salscore = TRUE;
12487 }
12488
Bram Moolenaar4770d092006-01-12 23:22:24 +000012489 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
12490 * for both lists. */
12491 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012492 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012493 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012494 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
12495
12496 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
12497 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
12498 return;
12499
12500 stp = &SUG(ga, 0);
12501 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
12502 {
12503 /* round 1: get a suggestion from su_ga
12504 * round 2: get a suggestion from su_sga */
12505 for (round = 1; round <= 2; ++round)
12506 {
12507 gap = round == 1 ? &su->su_ga : &su->su_sga;
12508 if (i < gap->ga_len)
12509 {
12510 /* Don't add a word if it's already there. */
12511 p = SUG(*gap, i).st_word;
12512 for (j = 0; j < ga.ga_len; ++j)
12513 if (STRCMP(stp[j].st_word, p) == 0)
12514 break;
12515 if (j == ga.ga_len)
12516 stp[ga.ga_len++] = SUG(*gap, i);
12517 else
12518 vim_free(p);
12519 }
12520 }
12521 }
12522
12523 ga_clear(&su->su_ga);
12524 ga_clear(&su->su_sga);
12525
12526 /* Truncate the list to the number of suggestions that will be displayed. */
12527 if (ga.ga_len > su->su_maxcount)
12528 {
12529 for (i = su->su_maxcount; i < ga.ga_len; ++i)
12530 vim_free(stp[i].st_word);
12531 ga.ga_len = su->su_maxcount;
12532 }
12533
12534 su->su_ga = ga;
12535}
12536
12537/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012538 * For the goodword in "stp" compute the soundalike score compared to the
12539 * badword.
12540 */
12541 static int
12542stp_sal_score(stp, su, slang, badsound)
12543 suggest_T *stp;
12544 suginfo_T *su;
12545 slang_T *slang;
12546 char_u *badsound; /* sound-folded badword */
12547{
12548 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012549 char_u *pbad;
12550 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012551 char_u badsound2[MAXWLEN];
12552 char_u fword[MAXWLEN];
12553 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012554 char_u goodword[MAXWLEN];
12555 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012556
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012557 lendiff = (int)(su->su_badlen - stp->st_orglen);
12558 if (lendiff >= 0)
12559 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012560 else
12561 {
12562 /* soundfold the bad word with more characters following */
12563 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
12564
12565 /* When joining two words the sound often changes a lot. E.g., "t he"
12566 * sounds like "t h" while "the" sounds like "@". Avoid that by
12567 * removing the space. Don't do it when the good word also contains a
12568 * space. */
12569 if (vim_iswhite(su->su_badptr[su->su_badlen])
12570 && *skiptowhite(stp->st_word) == NUL)
12571 for (p = fword; *(p = skiptowhite(p)) != NUL; )
12572 mch_memmove(p, p + 1, STRLEN(p));
12573
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012574 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012575 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012576 }
12577
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012578 if (lendiff > 0)
12579 {
12580 /* Add part of the bad word to the good word, so that we soundfold
12581 * what replaces the bad word. */
12582 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012583 vim_strncpy(goodword + stp->st_wordlen,
12584 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012585 pgood = goodword;
12586 }
12587 else
12588 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012589
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012590 /* Sound-fold the word and compute the score for the difference. */
12591 spell_soundfold(slang, pgood, FALSE, goodsound);
12592
12593 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012594}
12595
Bram Moolenaar4770d092006-01-12 23:22:24 +000012596/* structure used to store soundfolded words that add_sound_suggest() has
12597 * handled already. */
12598typedef struct
12599{
12600 short sft_score; /* lowest score used */
12601 char_u sft_word[1]; /* soundfolded word, actually longer */
12602} sftword_T;
12603
12604static sftword_T dumsft;
12605#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
12606#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
12607
12608/*
12609 * Prepare for calling suggest_try_soundalike().
12610 */
12611 static void
12612suggest_try_soundalike_prep()
12613{
12614 langp_T *lp;
12615 int lpi;
12616 slang_T *slang;
12617
12618 /* Do this for all languages that support sound folding and for which a
12619 * .sug file has been loaded. */
12620 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
12621 {
12622 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
12623 slang = lp->lp_slang;
12624 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
12625 /* prepare the hashtable used by add_sound_suggest() */
12626 hash_init(&slang->sl_sounddone);
12627 }
12628}
12629
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012630/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012631 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012632 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012633 */
12634 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000012635suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012636 suginfo_T *su;
12637{
12638 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012639 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012640 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012641 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012642
Bram Moolenaar4770d092006-01-12 23:22:24 +000012643 /* Do this for all languages that support sound folding and for which a
12644 * .sug file has been loaded. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012645 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012646 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012647 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
12648 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012649 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012650 {
12651 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012652 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012653
Bram Moolenaar4770d092006-01-12 23:22:24 +000012654 /* try all kinds of inserts/deletes/swaps/etc. */
12655 /* TODO: also soundfold the next words, so that we can try joining
12656 * and splitting */
12657 suggest_trie_walk(su, lp, salword, TRUE);
12658 }
12659 }
12660}
12661
12662/*
12663 * Finish up after calling suggest_try_soundalike().
12664 */
12665 static void
12666suggest_try_soundalike_finish()
12667{
12668 langp_T *lp;
12669 int lpi;
12670 slang_T *slang;
12671 int todo;
12672 hashitem_T *hi;
12673
12674 /* Do this for all languages that support sound folding and for which a
12675 * .sug file has been loaded. */
12676 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
12677 {
12678 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
12679 slang = lp->lp_slang;
12680 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
12681 {
12682 /* Free the info about handled words. */
12683 todo = slang->sl_sounddone.ht_used;
12684 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
12685 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012686 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012687 vim_free(HI2SFT(hi));
12688 --todo;
12689 }
12690 hash_clear(&slang->sl_sounddone);
12691 }
12692 }
12693}
12694
12695/*
12696 * A match with a soundfolded word is found. Add the good word(s) that
12697 * produce this soundfolded word.
12698 */
12699 static void
12700add_sound_suggest(su, goodword, score, lp)
12701 suginfo_T *su;
12702 char_u *goodword;
12703 int score; /* soundfold score */
12704 langp_T *lp;
12705{
12706 slang_T *slang = lp->lp_slang; /* language for sound folding */
12707 int sfwordnr;
12708 char_u *nrline;
12709 int orgnr;
12710 char_u theword[MAXWLEN];
12711 int i;
12712 int wlen;
12713 char_u *byts;
12714 idx_T *idxs;
12715 int n;
12716 int wordcount;
12717 int wc;
12718 int goodscore;
12719 hash_T hash;
12720 hashitem_T *hi;
12721 sftword_T *sft;
12722 int bc, gc;
12723 int limit;
12724
12725 /*
12726 * It's very well possible that the same soundfold word is found several
12727 * times with different scores. Since the following is quite slow only do
12728 * the words that have a better score than before. Use a hashtable to
12729 * remember the words that have been done.
12730 */
12731 hash = hash_hash(goodword);
12732 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
12733 if (HASHITEM_EMPTY(hi))
12734 {
12735 sft = (sftword_T *)alloc(sizeof(sftword_T) + STRLEN(goodword));
12736 if (sft != NULL)
12737 {
12738 sft->sft_score = score;
12739 STRCPY(sft->sft_word, goodword);
12740 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
12741 }
12742 }
12743 else
12744 {
12745 sft = HI2SFT(hi);
12746 if (score >= sft->sft_score)
12747 return;
12748 sft->sft_score = score;
12749 }
12750
12751 /*
12752 * Find the word nr in the soundfold tree.
12753 */
12754 sfwordnr = soundfold_find(slang, goodword);
12755 if (sfwordnr < 0)
12756 {
12757 EMSG2(_(e_intern2), "add_sound_suggest()");
12758 return;
12759 }
12760
12761 /*
12762 * go over the list of good words that produce this soundfold word
12763 */
12764 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
12765 orgnr = 0;
12766 while (*nrline != NUL)
12767 {
12768 /* The wordnr was stored in a minimal nr of bytes as an offset to the
12769 * previous wordnr. */
12770 orgnr += bytes2offset(&nrline);
12771
12772 byts = slang->sl_fbyts;
12773 idxs = slang->sl_fidxs;
12774
12775 /* Lookup the word "orgnr" one of the two tries. */
12776 n = 0;
12777 wlen = 0;
12778 wordcount = 0;
12779 for (;;)
12780 {
12781 i = 1;
12782 if (wordcount == orgnr && byts[n + 1] == NUL)
12783 break; /* found end of word */
12784
12785 if (byts[n + 1] == NUL)
12786 ++wordcount;
12787
12788 /* skip over the NUL bytes */
12789 for ( ; byts[n + i] == NUL; ++i)
12790 if (i > byts[n]) /* safety check */
12791 {
12792 STRCPY(theword + wlen, "BAD");
12793 goto badword;
12794 }
12795
12796 /* One of the siblings must have the word. */
12797 for ( ; i < byts[n]; ++i)
12798 {
12799 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
12800 if (wordcount + wc > orgnr)
12801 break;
12802 wordcount += wc;
12803 }
12804
12805 theword[wlen++] = byts[n + i];
12806 n = idxs[n + i];
12807 }
12808badword:
12809 theword[wlen] = NUL;
12810
12811 /* Go over the possible flags and regions. */
12812 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
12813 {
12814 char_u cword[MAXWLEN];
12815 char_u *p;
12816 int flags = (int)idxs[n + i];
12817
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012818 /* Skip words with the NOSUGGEST flag */
12819 if (flags & WF_NOSUGGEST)
12820 continue;
12821
Bram Moolenaar4770d092006-01-12 23:22:24 +000012822 if (flags & WF_KEEPCAP)
12823 {
12824 /* Must find the word in the keep-case tree. */
12825 find_keepcap_word(slang, theword, cword);
12826 p = cword;
12827 }
12828 else
12829 {
12830 flags |= su->su_badflags;
12831 if ((flags & WF_CAPMASK) != 0)
12832 {
12833 /* Need to fix case according to "flags". */
12834 make_case_word(theword, cword, flags);
12835 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012836 }
12837 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012838 p = theword;
12839 }
12840
12841 /* Add the suggestion. */
12842 if (sps_flags & SPS_DOUBLE)
12843 {
12844 /* Add the suggestion if the score isn't too bad. */
12845 if (score <= su->su_maxscore)
12846 add_suggestion(su, &su->su_sga, p, su->su_badlen,
12847 score, 0, FALSE, slang, FALSE);
12848 }
12849 else
12850 {
12851 /* Add a penalty for words in another region. */
12852 if ((flags & WF_REGION)
12853 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
12854 goodscore = SCORE_REGION;
12855 else
12856 goodscore = 0;
12857
12858 /* Add a small penalty for changing the first letter from
12859 * lower to upper case. Helps for "tath" -> "Kath", which is
12860 * less common thatn "tath" -> "path". Don't do it when the
12861 * letter is the same, that has already been counted. */
12862 gc = PTR2CHAR(p);
12863 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012864 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012865 bc = PTR2CHAR(su->su_badword);
12866 if (!SPELL_ISUPPER(bc)
12867 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
12868 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012869 }
12870
Bram Moolenaar4770d092006-01-12 23:22:24 +000012871 /* Compute the score for the good word. This only does letter
12872 * insert/delete/swap/replace. REP items are not considered,
12873 * which may make the score a bit higher.
12874 * Use a limit for the score to make it work faster. Use
12875 * MAXSCORE(), because RESCORE() will change the score.
12876 * If the limit is very high then the iterative method is
12877 * inefficient, using an array is quicker. */
12878 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
12879 if (limit > SCORE_LIMITMAX)
12880 goodscore += spell_edit_score(slang, su->su_badword, p);
12881 else
12882 goodscore += spell_edit_score_limit(slang, su->su_badword,
12883 p, limit);
12884
12885 /* When going over the limit don't bother to do the rest. */
12886 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012887 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012888 /* Give a bonus to words seen before. */
12889 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012890
Bram Moolenaar4770d092006-01-12 23:22:24 +000012891 /* Add the suggestion if the score isn't too bad. */
12892 goodscore = RESCORE(goodscore, score);
12893 if (goodscore <= su->su_sfmaxscore)
12894 add_suggestion(su, &su->su_ga, p, su->su_badlen,
12895 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012896 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012897 }
12898 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012899 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012900 }
12901}
12902
12903/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012904 * Find word "word" in fold-case tree for "slang" and return the word number.
12905 */
12906 static int
12907soundfold_find(slang, word)
12908 slang_T *slang;
12909 char_u *word;
12910{
12911 idx_T arridx = 0;
12912 int len;
12913 int wlen = 0;
12914 int c;
12915 char_u *ptr = word;
12916 char_u *byts;
12917 idx_T *idxs;
12918 int wordnr = 0;
12919
12920 byts = slang->sl_sbyts;
12921 idxs = slang->sl_sidxs;
12922
12923 for (;;)
12924 {
12925 /* First byte is the number of possible bytes. */
12926 len = byts[arridx++];
12927
12928 /* If the first possible byte is a zero the word could end here.
12929 * If the word ends we found the word. If not skip the NUL bytes. */
12930 c = ptr[wlen];
12931 if (byts[arridx] == NUL)
12932 {
12933 if (c == NUL)
12934 break;
12935
12936 /* Skip over the zeros, there can be several. */
12937 while (len > 0 && byts[arridx] == NUL)
12938 {
12939 ++arridx;
12940 --len;
12941 }
12942 if (len == 0)
12943 return -1; /* no children, word should have ended here */
12944 ++wordnr;
12945 }
12946
12947 /* If the word ends we didn't find it. */
12948 if (c == NUL)
12949 return -1;
12950
12951 /* Perform a binary search in the list of accepted bytes. */
12952 if (c == TAB) /* <Tab> is handled like <Space> */
12953 c = ' ';
12954 while (byts[arridx] < c)
12955 {
12956 /* The word count is in the first idxs[] entry of the child. */
12957 wordnr += idxs[idxs[arridx]];
12958 ++arridx;
12959 if (--len == 0) /* end of the bytes, didn't find it */
12960 return -1;
12961 }
12962 if (byts[arridx] != c) /* didn't find the byte */
12963 return -1;
12964
12965 /* Continue at the child (if there is one). */
12966 arridx = idxs[arridx];
12967 ++wlen;
12968
12969 /* One space in the good word may stand for several spaces in the
12970 * checked word. */
12971 if (c == ' ')
12972 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
12973 ++wlen;
12974 }
12975
12976 return wordnr;
12977}
12978
12979/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012980 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012981 */
12982 static void
12983make_case_word(fword, cword, flags)
12984 char_u *fword;
12985 char_u *cword;
12986 int flags;
12987{
12988 if (flags & WF_ALLCAP)
12989 /* Make it all upper-case */
12990 allcap_copy(fword, cword);
12991 else if (flags & WF_ONECAP)
12992 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012993 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012994 else
12995 /* Use goodword as-is. */
12996 STRCPY(cword, fword);
12997}
12998
Bram Moolenaarea424162005-06-16 21:51:00 +000012999/*
13000 * Use map string "map" for languages "lp".
13001 */
13002 static void
13003set_map_str(lp, map)
13004 slang_T *lp;
13005 char_u *map;
13006{
13007 char_u *p;
13008 int headc = 0;
13009 int c;
13010 int i;
13011
13012 if (*map == NUL)
13013 {
13014 lp->sl_has_map = FALSE;
13015 return;
13016 }
13017 lp->sl_has_map = TRUE;
13018
Bram Moolenaar4770d092006-01-12 23:22:24 +000013019 /* Init the array and hash tables empty. */
Bram Moolenaarea424162005-06-16 21:51:00 +000013020 for (i = 0; i < 256; ++i)
13021 lp->sl_map_array[i] = 0;
13022#ifdef FEAT_MBYTE
13023 hash_init(&lp->sl_map_hash);
13024#endif
13025
13026 /*
13027 * The similar characters are stored separated with slashes:
13028 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
13029 * before the same slash. For characters above 255 sl_map_hash is used.
13030 */
13031 for (p = map; *p != NUL; )
13032 {
13033#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013034 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000013035#else
13036 c = *p++;
13037#endif
13038 if (c == '/')
13039 headc = 0;
13040 else
13041 {
13042 if (headc == 0)
13043 headc = c;
13044
13045#ifdef FEAT_MBYTE
13046 /* Characters above 255 don't fit in sl_map_array[], put them in
13047 * the hash table. Each entry is the char, a NUL the headchar and
13048 * a NUL. */
13049 if (c >= 256)
13050 {
13051 int cl = mb_char2len(c);
13052 int headcl = mb_char2len(headc);
13053 char_u *b;
13054 hash_T hash;
13055 hashitem_T *hi;
13056
13057 b = alloc((unsigned)(cl + headcl + 2));
13058 if (b == NULL)
13059 return;
13060 mb_char2bytes(c, b);
13061 b[cl] = NUL;
13062 mb_char2bytes(headc, b + cl + 1);
13063 b[cl + 1 + headcl] = NUL;
13064 hash = hash_hash(b);
13065 hi = hash_lookup(&lp->sl_map_hash, b, hash);
13066 if (HASHITEM_EMPTY(hi))
13067 hash_add_item(&lp->sl_map_hash, hi, b, hash);
13068 else
13069 {
13070 /* This should have been checked when generating the .spl
13071 * file. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013072 EMSG(_("E783: duplicate char in MAP entry"));
Bram Moolenaarea424162005-06-16 21:51:00 +000013073 vim_free(b);
13074 }
13075 }
13076 else
13077#endif
13078 lp->sl_map_array[c] = headc;
13079 }
13080 }
13081}
13082
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013083/*
13084 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
13085 * lines in the .aff file.
13086 */
13087 static int
13088similar_chars(slang, c1, c2)
13089 slang_T *slang;
13090 int c1;
13091 int c2;
13092{
Bram Moolenaarea424162005-06-16 21:51:00 +000013093 int m1, m2;
13094#ifdef FEAT_MBYTE
13095 char_u buf[MB_MAXBYTES];
13096 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013097
Bram Moolenaarea424162005-06-16 21:51:00 +000013098 if (c1 >= 256)
13099 {
13100 buf[mb_char2bytes(c1, buf)] = 0;
13101 hi = hash_find(&slang->sl_map_hash, buf);
13102 if (HASHITEM_EMPTY(hi))
13103 m1 = 0;
13104 else
13105 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13106 }
13107 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013108#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000013109 m1 = slang->sl_map_array[c1];
13110 if (m1 == 0)
13111 return FALSE;
13112
13113
13114#ifdef FEAT_MBYTE
13115 if (c2 >= 256)
13116 {
13117 buf[mb_char2bytes(c2, buf)] = 0;
13118 hi = hash_find(&slang->sl_map_hash, buf);
13119 if (HASHITEM_EMPTY(hi))
13120 m2 = 0;
13121 else
13122 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13123 }
13124 else
13125#endif
13126 m2 = slang->sl_map_array[c2];
13127
13128 return m1 == m2;
13129}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013130
13131/*
13132 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +000013133 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013134 */
13135 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000013136add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus,
13137 slang, maxsf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013138 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013139 garray_T *gap; /* either su_ga or su_sga */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013140 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013141 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013142 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013143 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013144 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013145 slang_T *slang; /* language for sound folding */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013146 int maxsf; /* su_maxscore applies to soundfold score,
13147 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013148{
Bram Moolenaar4770d092006-01-12 23:22:24 +000013149 int goodlen; /* len of goodword changed */
13150 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013151 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013152 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013153 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013154 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013155
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013156 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13157 * "thee the" is added next to changing the first "the" the "thee". */
13158 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013159 pbad = su->su_badptr + badlenarg;
13160 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013161 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013162 goodlen = pgood - goodword;
13163 badlen = pbad - su->su_badptr;
13164 if (goodlen <= 0 || badlen <= 0)
13165 break;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013166 mb_ptr_back(goodword, pgood);
13167 mb_ptr_back(su->su_badptr, pbad);
13168#ifdef FEAT_MBYTE
13169 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013170 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013171 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
13172 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013173 }
13174 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013175#endif
13176 if (*pgood != *pbad)
13177 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013178 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013179
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013180 if (badlen == 0 && goodlen == 0)
13181 /* goodword doesn't change anything; may happen for "the the" changing
13182 * the first "the" to itself. */
13183 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013184
Bram Moolenaar4770d092006-01-12 23:22:24 +000013185 /* Check if the word is already there. Also check the length that is
13186 * being replaced "thes," -> "these" is a different suggestion from
13187 * "thes" -> "these". */
13188 stp = &SUG(*gap, 0);
13189 for (i = gap->ga_len; --i >= 0; ++stp)
13190 if (stp->st_wordlen == goodlen
13191 && stp->st_orglen == badlen
13192 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
13193 {
13194 /*
13195 * Found it. Remember the word with the lowest score.
13196 */
13197 if (stp->st_slang == NULL)
13198 stp->st_slang = slang;
13199
13200 new_sug.st_score = score;
13201 new_sug.st_altscore = altscore;
13202 new_sug.st_had_bonus = had_bonus;
13203
13204 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013205 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013206 /* Only one of the two had the soundalike score computed.
13207 * Need to do that for the other one now, otherwise the
13208 * scores can't be compared. This happens because
13209 * suggest_try_change() doesn't compute the soundalike
13210 * word to keep it fast, while some special methods set
13211 * the soundalike score to zero. */
13212 if (had_bonus)
13213 rescore_one(su, stp);
13214 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013215 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013216 new_sug.st_word = stp->st_word;
13217 new_sug.st_wordlen = stp->st_wordlen;
13218 new_sug.st_slang = stp->st_slang;
13219 new_sug.st_orglen = badlen;
13220 rescore_one(su, &new_sug);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013221 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013222 }
13223
Bram Moolenaar4770d092006-01-12 23:22:24 +000013224 if (stp->st_score > new_sug.st_score)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013225 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013226 stp->st_score = new_sug.st_score;
13227 stp->st_altscore = new_sug.st_altscore;
13228 stp->st_had_bonus = new_sug.st_had_bonus;
13229 }
13230 break;
13231 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013232
Bram Moolenaar4770d092006-01-12 23:22:24 +000013233 if (i < 0 && ga_grow(gap, 1) == OK)
13234 {
13235 /* Add a suggestion. */
13236 stp = &SUG(*gap, gap->ga_len);
13237 stp->st_word = vim_strnsave(goodword, goodlen);
13238 if (stp->st_word != NULL)
13239 {
13240 stp->st_wordlen = goodlen;
13241 stp->st_score = score;
13242 stp->st_altscore = altscore;
13243 stp->st_had_bonus = had_bonus;
13244 stp->st_orglen = badlen;
13245 stp->st_slang = slang;
13246 ++gap->ga_len;
13247
13248 /* If we have too many suggestions now, sort the list and keep
13249 * the best suggestions. */
13250 if (gap->ga_len > SUG_MAX_COUNT(su))
13251 {
13252 if (maxsf)
13253 su->su_sfmaxscore = cleanup_suggestions(gap,
13254 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
13255 else
13256 {
13257 i = su->su_maxscore;
13258 su->su_maxscore = cleanup_suggestions(gap,
13259 su->su_maxscore, SUG_CLEAN_COUNT(su));
13260 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013261 }
13262 }
13263 }
13264}
13265
13266/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013267 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13268 * for split words, such as "the the". Remove these from the list here.
13269 */
13270 static void
13271check_suggestions(su, gap)
13272 suginfo_T *su;
13273 garray_T *gap; /* either su_ga or su_sga */
13274{
13275 suggest_T *stp;
13276 int i;
13277 char_u longword[MAXWLEN + 1];
13278 int len;
13279 hlf_T attr;
13280
13281 stp = &SUG(*gap, 0);
13282 for (i = gap->ga_len - 1; i >= 0; --i)
13283 {
13284 /* Need to append what follows to check for "the the". */
13285 STRCPY(longword, stp[i].st_word);
13286 len = stp[i].st_wordlen;
13287 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
13288 MAXWLEN - len);
13289 attr = HLF_COUNT;
13290 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
13291 if (attr != HLF_COUNT)
13292 {
13293 /* Remove this entry. */
13294 vim_free(stp[i].st_word);
13295 --gap->ga_len;
13296 if (i < gap->ga_len)
13297 mch_memmove(stp + i, stp + i + 1,
13298 sizeof(suggest_T) * (gap->ga_len - i));
13299 }
13300 }
13301}
13302
13303
13304/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013305 * Add a word to be banned.
13306 */
13307 static void
13308add_banned(su, word)
13309 suginfo_T *su;
13310 char_u *word;
13311{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013312 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013313 hash_T hash;
13314 hashitem_T *hi;
13315
Bram Moolenaar4770d092006-01-12 23:22:24 +000013316 hash = hash_hash(word);
13317 hi = hash_lookup(&su->su_banned, word, hash);
13318 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013319 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013320 s = vim_strsave(word);
13321 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013322 hash_add_item(&su->su_banned, hi, s, hash);
13323 }
13324}
13325
13326/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013327 * Recompute the score for all suggestions if sound-folding is possible. This
13328 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013329 */
13330 static void
13331rescore_suggestions(su)
13332 suginfo_T *su;
13333{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013334 int i;
13335
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013336 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013337 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013338 rescore_one(su, &SUG(su->su_ga, i));
13339}
13340
13341/*
13342 * Recompute the score for one suggestion if sound-folding is possible.
13343 */
13344 static void
13345rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013346 suginfo_T *su;
13347 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013348{
13349 slang_T *slang = stp->st_slang;
13350 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000013351 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013352
13353 /* Only rescore suggestions that have no sal score yet and do have a
13354 * language. */
13355 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
13356 {
13357 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013358 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013359 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013360 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013361 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000013362 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013363 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000013364
13365 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013366 if (stp->st_altscore == SCORE_MAXMAX)
13367 stp->st_altscore = SCORE_BIG;
13368 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
13369 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013370 }
13371}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013372
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013373static int
13374#ifdef __BORLANDC__
13375_RTLENTRYF
13376#endif
13377sug_compare __ARGS((const void *s1, const void *s2));
13378
13379/*
13380 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013381 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013382 */
13383 static int
13384#ifdef __BORLANDC__
13385_RTLENTRYF
13386#endif
13387sug_compare(s1, s2)
13388 const void *s1;
13389 const void *s2;
13390{
13391 suggest_T *p1 = (suggest_T *)s1;
13392 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013393 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013394
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013395 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013396 {
13397 n = p1->st_altscore - p2->st_altscore;
13398 if (n == 0)
13399 n = STRICMP(p1->st_word, p2->st_word);
13400 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013401 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013402}
13403
13404/*
13405 * Cleanup the suggestions:
13406 * - Sort on score.
13407 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013408 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013409 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013410 static int
13411cleanup_suggestions(gap, maxscore, keep)
13412 garray_T *gap;
13413 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013414 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013415{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013416 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013417 int i;
13418
13419 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013420 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013421
13422 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013423 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013424 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013425 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013426 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013427 gap->ga_len = keep;
13428 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013429 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013430 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013431}
13432
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013433#if defined(FEAT_EVAL) || defined(PROTO)
13434/*
13435 * Soundfold a string, for soundfold().
13436 * Result is in allocated memory, NULL for an error.
13437 */
13438 char_u *
13439eval_soundfold(word)
13440 char_u *word;
13441{
13442 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013443 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013444 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013445
13446 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13447 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013448 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013449 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013450 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013451 if (lp->lp_slang->sl_sal.ga_len > 0)
13452 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013453 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013454 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013455 return vim_strsave(sound);
13456 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013457 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013458
13459 /* No language with sound folding, return word as-is. */
13460 return vim_strsave(word);
13461}
13462#endif
13463
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013464/*
13465 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000013466 *
13467 * There are many ways to turn a word into a sound-a-like representation. The
13468 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
13469 * swedish name matching - survey and test of different algorithms" by Klas
13470 * Erikson.
13471 *
13472 * We support two methods:
13473 * 1. SOFOFROM/SOFOTO do a simple character mapping.
13474 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013475 */
13476 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013477spell_soundfold(slang, inword, folded, res)
13478 slang_T *slang;
13479 char_u *inword;
13480 int folded; /* "inword" is already case-folded */
13481 char_u *res;
13482{
13483 char_u fword[MAXWLEN];
13484 char_u *word;
13485
13486 if (slang->sl_sofo)
13487 /* SOFOFROM and SOFOTO used */
13488 spell_soundfold_sofo(slang, inword, res);
13489 else
13490 {
13491 /* SAL items used. Requires the word to be case-folded. */
13492 if (folded)
13493 word = inword;
13494 else
13495 {
13496 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
13497 word = fword;
13498 }
13499
13500#ifdef FEAT_MBYTE
13501 if (has_mbyte)
13502 spell_soundfold_wsal(slang, word, res);
13503 else
13504#endif
13505 spell_soundfold_sal(slang, word, res);
13506 }
13507}
13508
13509/*
13510 * Perform sound folding of "inword" into "res" according to SOFOFROM and
13511 * SOFOTO lines.
13512 */
13513 static void
13514spell_soundfold_sofo(slang, inword, res)
13515 slang_T *slang;
13516 char_u *inword;
13517 char_u *res;
13518{
13519 char_u *s;
13520 int ri = 0;
13521 int c;
13522
13523#ifdef FEAT_MBYTE
13524 if (has_mbyte)
13525 {
13526 int prevc = 0;
13527 int *ip;
13528
13529 /* The sl_sal_first[] table contains the translation for chars up to
13530 * 255, sl_sal the rest. */
13531 for (s = inword; *s != NUL; )
13532 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013533 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013534 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
13535 c = ' ';
13536 else if (c < 256)
13537 c = slang->sl_sal_first[c];
13538 else
13539 {
13540 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
13541 if (ip == NULL) /* empty list, can't match */
13542 c = NUL;
13543 else
13544 for (;;) /* find "c" in the list */
13545 {
13546 if (*ip == 0) /* not found */
13547 {
13548 c = NUL;
13549 break;
13550 }
13551 if (*ip == c) /* match! */
13552 {
13553 c = ip[1];
13554 break;
13555 }
13556 ip += 2;
13557 }
13558 }
13559
13560 if (c != NUL && c != prevc)
13561 {
13562 ri += mb_char2bytes(c, res + ri);
13563 if (ri + MB_MAXBYTES > MAXWLEN)
13564 break;
13565 prevc = c;
13566 }
13567 }
13568 }
13569 else
13570#endif
13571 {
13572 /* The sl_sal_first[] table contains the translation. */
13573 for (s = inword; (c = *s) != NUL; ++s)
13574 {
13575 if (vim_iswhite(c))
13576 c = ' ';
13577 else
13578 c = slang->sl_sal_first[c];
13579 if (c != NUL && (ri == 0 || res[ri - 1] != c))
13580 res[ri++] = c;
13581 }
13582 }
13583
13584 res[ri] = NUL;
13585}
13586
13587 static void
13588spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013589 slang_T *slang;
13590 char_u *inword;
13591 char_u *res;
13592{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013593 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013594 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013595 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013596 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013597 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013598 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013599 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013600 int n, k = 0;
13601 int z0;
13602 int k0;
13603 int n0;
13604 int c;
13605 int pri;
13606 int p0 = -333;
13607 int c0;
13608
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013609 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013610 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013611 if (slang->sl_rem_accents)
13612 {
13613 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013614 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013615 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013616 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013617 {
13618 *t++ = ' ';
13619 s = skipwhite(s);
13620 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013621 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013622 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013623 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013624 *t++ = *s;
13625 ++s;
13626 }
13627 }
13628 *t = NUL;
13629 }
13630 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013631 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013632
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013633 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013634
13635 /*
13636 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013637 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013638 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013639 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013640 while ((c = word[i]) != NUL)
13641 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013642 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013643 n = slang->sl_sal_first[c];
13644 z0 = 0;
13645
13646 if (n >= 0)
13647 {
13648 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013649 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013650 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013651 /* Quickly skip entries that don't match the word. Most
13652 * entries are less then three chars, optimize for that. */
13653 k = smp[n].sm_leadlen;
13654 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013655 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013656 if (word[i + 1] != s[1])
13657 continue;
13658 if (k > 2)
13659 {
13660 for (j = 2; j < k; ++j)
13661 if (word[i + j] != s[j])
13662 break;
13663 if (j < k)
13664 continue;
13665 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013666 }
13667
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013668 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013669 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013670 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013671 while (*pf != NUL && *pf != word[i + k])
13672 ++pf;
13673 if (*pf == NUL)
13674 continue;
13675 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013676 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013677 s = smp[n].sm_rules;
13678 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013679
13680 p0 = *s;
13681 k0 = k;
13682 while (*s == '-' && k > 1)
13683 {
13684 k--;
13685 s++;
13686 }
13687 if (*s == '<')
13688 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013689 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013690 {
13691 /* determine priority */
13692 pri = *s - '0';
13693 s++;
13694 }
13695 if (*s == '^' && *(s + 1) == '^')
13696 s++;
13697
13698 if (*s == NUL
13699 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013700 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013701 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013702 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013703 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013704 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013705 && spell_iswordp(word + i - 1, curbuf)
13706 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013707 {
13708 /* search for followup rules, if: */
13709 /* followup and k > 1 and NO '-' in searchstring */
13710 c0 = word[i + k - 1];
13711 n0 = slang->sl_sal_first[c0];
13712
13713 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013714 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013715 {
13716 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013717 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013718 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013719 /* Quickly skip entries that don't match the word.
13720 * */
13721 k0 = smp[n0].sm_leadlen;
13722 if (k0 > 1)
13723 {
13724 if (word[i + k] != s[1])
13725 continue;
13726 if (k0 > 2)
13727 {
13728 pf = word + i + k + 1;
13729 for (j = 2; j < k0; ++j)
13730 if (*pf++ != s[j])
13731 break;
13732 if (j < k0)
13733 continue;
13734 }
13735 }
13736 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013737
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013738 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013739 {
13740 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013741 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013742 while (*pf != NUL && *pf != word[i + k0])
13743 ++pf;
13744 if (*pf == NUL)
13745 continue;
13746 ++k0;
13747 }
13748
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013749 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013750 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013751 while (*s == '-')
13752 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013753 /* "k0" gets NOT reduced because
13754 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013755 s++;
13756 }
13757 if (*s == '<')
13758 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013759 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013760 {
13761 p0 = *s - '0';
13762 s++;
13763 }
13764
13765 if (*s == NUL
13766 /* *s == '^' cuts */
13767 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013768 && !spell_iswordp(word + i + k0,
13769 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013770 {
13771 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013772 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013773 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013774
13775 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013776 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013777 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013778 /* rule fits; stop search */
13779 break;
13780 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013781 }
13782
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013783 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013784 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013785 }
13786
13787 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013788 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000013789 if (s == NULL)
13790 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013791 pf = smp[n].sm_rules;
13792 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013793 if (p0 == 1 && z == 0)
13794 {
13795 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013796 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
13797 || res[reslen - 1] == *s))
13798 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013799 z0 = 1;
13800 z = 1;
13801 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013802 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013803 {
13804 word[i + k0] = *s;
13805 k0++;
13806 s++;
13807 }
13808 if (k > k0)
13809 mch_memmove(word + i + k0, word + i + k,
13810 STRLEN(word + i + k) + 1);
13811
13812 /* new "actual letter" */
13813 c = word[i];
13814 }
13815 else
13816 {
13817 /* no '<' rule used */
13818 i += k - 1;
13819 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013820 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013821 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013822 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013823 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013824 s++;
13825 }
13826 /* new "actual letter" */
13827 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013828 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013829 {
13830 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013831 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013832 mch_memmove(word, word + i + 1,
13833 STRLEN(word + i + 1) + 1);
13834 i = 0;
13835 z0 = 1;
13836 }
13837 }
13838 break;
13839 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013840 }
13841 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013842 else if (vim_iswhite(c))
13843 {
13844 c = ' ';
13845 k = 1;
13846 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013847
13848 if (z0 == 0)
13849 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013850 if (k && !p0 && reslen < MAXWLEN && c != NUL
13851 && (!slang->sl_collapse || reslen == 0
13852 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013853 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013854 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013855
13856 i++;
13857 z = 0;
13858 k = 0;
13859 }
13860 }
13861
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013862 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013863}
13864
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013865#ifdef FEAT_MBYTE
13866/*
13867 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
13868 * Multi-byte version of spell_soundfold().
13869 */
13870 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013871spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013872 slang_T *slang;
13873 char_u *inword;
13874 char_u *res;
13875{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013876 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013877 int word[MAXWLEN];
13878 int wres[MAXWLEN];
13879 int l;
13880 char_u *s;
13881 int *ws;
13882 char_u *t;
13883 int *pf;
13884 int i, j, z;
13885 int reslen;
13886 int n, k = 0;
13887 int z0;
13888 int k0;
13889 int n0;
13890 int c;
13891 int pri;
13892 int p0 = -333;
13893 int c0;
13894 int did_white = FALSE;
13895
13896 /*
13897 * Convert the multi-byte string to a wide-character string.
13898 * Remove accents, if wanted. We actually remove all non-word characters.
13899 * But keep white space.
13900 */
13901 n = 0;
13902 for (s = inword; *s != NUL; )
13903 {
13904 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013905 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013906 if (slang->sl_rem_accents)
13907 {
13908 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
13909 {
13910 if (did_white)
13911 continue;
13912 c = ' ';
13913 did_white = TRUE;
13914 }
13915 else
13916 {
13917 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013918 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013919 continue;
13920 }
13921 }
13922 word[n++] = c;
13923 }
13924 word[n] = NUL;
13925
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013926 /*
13927 * This comes from Aspell phonet.cpp.
13928 * Converted from C++ to C. Added support for multi-byte chars.
13929 * Changed to keep spaces.
13930 */
13931 i = reslen = z = 0;
13932 while ((c = word[i]) != NUL)
13933 {
13934 /* Start with the first rule that has the character in the word. */
13935 n = slang->sl_sal_first[c & 0xff];
13936 z0 = 0;
13937
13938 if (n >= 0)
13939 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013940 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013941 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
13942 {
13943 /* Quickly skip entries that don't match the word. Most
13944 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013945 if (c != ws[0])
13946 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013947 k = smp[n].sm_leadlen;
13948 if (k > 1)
13949 {
13950 if (word[i + 1] != ws[1])
13951 continue;
13952 if (k > 2)
13953 {
13954 for (j = 2; j < k; ++j)
13955 if (word[i + j] != ws[j])
13956 break;
13957 if (j < k)
13958 continue;
13959 }
13960 }
13961
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013962 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013963 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013964 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013965 while (*pf != NUL && *pf != word[i + k])
13966 ++pf;
13967 if (*pf == NUL)
13968 continue;
13969 ++k;
13970 }
13971 s = smp[n].sm_rules;
13972 pri = 5; /* default priority */
13973
13974 p0 = *s;
13975 k0 = k;
13976 while (*s == '-' && k > 1)
13977 {
13978 k--;
13979 s++;
13980 }
13981 if (*s == '<')
13982 s++;
13983 if (VIM_ISDIGIT(*s))
13984 {
13985 /* determine priority */
13986 pri = *s - '0';
13987 s++;
13988 }
13989 if (*s == '^' && *(s + 1) == '^')
13990 s++;
13991
13992 if (*s == NUL
13993 || (*s == '^'
13994 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013995 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013996 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013997 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013998 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013999 && spell_iswordp_w(word + i - 1, curbuf)
14000 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014001 {
14002 /* search for followup rules, if: */
14003 /* followup and k > 1 and NO '-' in searchstring */
14004 c0 = word[i + k - 1];
14005 n0 = slang->sl_sal_first[c0 & 0xff];
14006
14007 if (slang->sl_followup && k > 1 && n0 >= 0
14008 && p0 != '-' && word[i + k] != NUL)
14009 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014010 /* Test follow-up rule for "word[i + k]"; loop over
14011 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014012 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
14013 == (c0 & 0xff); ++n0)
14014 {
14015 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014016 */
14017 if (c0 != ws[0])
14018 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014019 k0 = smp[n0].sm_leadlen;
14020 if (k0 > 1)
14021 {
14022 if (word[i + k] != ws[1])
14023 continue;
14024 if (k0 > 2)
14025 {
14026 pf = word + i + k + 1;
14027 for (j = 2; j < k0; ++j)
14028 if (*pf++ != ws[j])
14029 break;
14030 if (j < k0)
14031 continue;
14032 }
14033 }
14034 k0 += k - 1;
14035
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014036 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014037 {
14038 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014039 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014040 while (*pf != NUL && *pf != word[i + k0])
14041 ++pf;
14042 if (*pf == NUL)
14043 continue;
14044 ++k0;
14045 }
14046
14047 p0 = 5;
14048 s = smp[n0].sm_rules;
14049 while (*s == '-')
14050 {
14051 /* "k0" gets NOT reduced because
14052 * "if (k0 == k)" */
14053 s++;
14054 }
14055 if (*s == '<')
14056 s++;
14057 if (VIM_ISDIGIT(*s))
14058 {
14059 p0 = *s - '0';
14060 s++;
14061 }
14062
14063 if (*s == NUL
14064 /* *s == '^' cuts */
14065 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014066 && !spell_iswordp_w(word + i + k0,
14067 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014068 {
14069 if (k0 == k)
14070 /* this is just a piece of the string */
14071 continue;
14072
14073 if (p0 < pri)
14074 /* priority too low */
14075 continue;
14076 /* rule fits; stop search */
14077 break;
14078 }
14079 }
14080
14081 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
14082 == (c0 & 0xff))
14083 continue;
14084 }
14085
14086 /* replace string */
14087 ws = smp[n].sm_to_w;
14088 s = smp[n].sm_rules;
14089 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
14090 if (p0 == 1 && z == 0)
14091 {
14092 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014093 if (reslen > 0 && ws != NULL && *ws != NUL
14094 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014095 || wres[reslen - 1] == *ws))
14096 reslen--;
14097 z0 = 1;
14098 z = 1;
14099 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014100 if (ws != NULL)
14101 while (*ws != NUL && word[i + k0] != NUL)
14102 {
14103 word[i + k0] = *ws;
14104 k0++;
14105 ws++;
14106 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014107 if (k > k0)
14108 mch_memmove(word + i + k0, word + i + k,
14109 sizeof(int) * (STRLEN(word + i + k) + 1));
14110
14111 /* new "actual letter" */
14112 c = word[i];
14113 }
14114 else
14115 {
14116 /* no '<' rule used */
14117 i += k - 1;
14118 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014119 if (ws != NULL)
14120 while (*ws != NUL && ws[1] != NUL
14121 && reslen < MAXWLEN)
14122 {
14123 if (reslen == 0 || wres[reslen - 1] != *ws)
14124 wres[reslen++] = *ws;
14125 ws++;
14126 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014127 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014128 if (ws == NULL)
14129 c = NUL;
14130 else
14131 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014132 if (strstr((char *)s, "^^") != NULL)
14133 {
14134 if (c != NUL)
14135 wres[reslen++] = c;
14136 mch_memmove(word, word + i + 1,
14137 sizeof(int) * (STRLEN(word + i + 1) + 1));
14138 i = 0;
14139 z0 = 1;
14140 }
14141 }
14142 break;
14143 }
14144 }
14145 }
14146 else if (vim_iswhite(c))
14147 {
14148 c = ' ';
14149 k = 1;
14150 }
14151
14152 if (z0 == 0)
14153 {
14154 if (k && !p0 && reslen < MAXWLEN && c != NUL
14155 && (!slang->sl_collapse || reslen == 0
14156 || wres[reslen - 1] != c))
14157 /* condense only double letters */
14158 wres[reslen++] = c;
14159
14160 i++;
14161 z = 0;
14162 k = 0;
14163 }
14164 }
14165
14166 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14167 l = 0;
14168 for (n = 0; n < reslen; ++n)
14169 {
14170 l += mb_char2bytes(wres[n], res + l);
14171 if (l + MB_MAXBYTES > MAXWLEN)
14172 break;
14173 }
14174 res[l] = NUL;
14175}
14176#endif
14177
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014178/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014179 * Compute a score for two sound-a-like words.
14180 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14181 * Instead of a generic loop we write out the code. That keeps it fast by
14182 * avoiding checks that will not be possible.
14183 */
14184 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014185soundalike_score(goodstart, badstart)
14186 char_u *goodstart; /* sound-folded good word */
14187 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014188{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014189 char_u *goodsound = goodstart;
14190 char_u *badsound = badstart;
14191 int goodlen;
14192 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014193 int n;
14194 char_u *pl, *ps;
14195 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014196 int score = 0;
14197
14198 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
14199 * counted so much, vowels halfway the word aren't counted at all. */
14200 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
14201 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014202 if (badsound[1] == goodsound[1]
14203 || (badsound[1] != NUL
14204 && goodsound[1] != NUL
14205 && badsound[2] == goodsound[2]))
14206 {
14207 /* handle like a substitute */
14208 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014209 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014210 {
14211 score = 2 * SCORE_DEL / 3;
14212 if (*badsound == '*')
14213 ++badsound;
14214 else
14215 ++goodsound;
14216 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014217 }
14218
14219 goodlen = STRLEN(goodsound);
14220 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014221
14222 /* Return quickly if the lenghts are too different to be fixed by two
14223 * changes. */
14224 n = goodlen - badlen;
14225 if (n < -2 || n > 2)
14226 return SCORE_MAXMAX;
14227
14228 if (n > 0)
14229 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014230 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014231 ps = badsound;
14232 }
14233 else
14234 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014235 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014236 ps = goodsound;
14237 }
14238
14239 /* Skip over the identical part. */
14240 while (*pl == *ps && *pl != NUL)
14241 {
14242 ++pl;
14243 ++ps;
14244 }
14245
14246 switch (n)
14247 {
14248 case -2:
14249 case 2:
14250 /*
14251 * Must delete two characters from "pl".
14252 */
14253 ++pl; /* first delete */
14254 while (*pl == *ps)
14255 {
14256 ++pl;
14257 ++ps;
14258 }
14259 /* strings must be equal after second delete */
14260 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014261 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014262
14263 /* Failed to compare. */
14264 break;
14265
14266 case -1:
14267 case 1:
14268 /*
14269 * Minimal one delete from "pl" required.
14270 */
14271
14272 /* 1: delete */
14273 pl2 = pl + 1;
14274 ps2 = ps;
14275 while (*pl2 == *ps2)
14276 {
14277 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014278 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014279 ++pl2;
14280 ++ps2;
14281 }
14282
14283 /* 2: delete then swap, then rest must be equal */
14284 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14285 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014286 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014287
14288 /* 3: delete then substitute, then the rest must be equal */
14289 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014290 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014291
14292 /* 4: first swap then delete */
14293 if (pl[0] == ps[1] && pl[1] == ps[0])
14294 {
14295 pl2 = pl + 2; /* swap, skip two chars */
14296 ps2 = ps + 2;
14297 while (*pl2 == *ps2)
14298 {
14299 ++pl2;
14300 ++ps2;
14301 }
14302 /* delete a char and then strings must be equal */
14303 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014304 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014305 }
14306
14307 /* 5: first substitute then delete */
14308 pl2 = pl + 1; /* substitute, skip one char */
14309 ps2 = ps + 1;
14310 while (*pl2 == *ps2)
14311 {
14312 ++pl2;
14313 ++ps2;
14314 }
14315 /* delete a char and then strings must be equal */
14316 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014317 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014318
14319 /* Failed to compare. */
14320 break;
14321
14322 case 0:
14323 /*
14324 * Lenghts are equal, thus changes must result in same length: An
14325 * insert is only possible in combination with a delete.
14326 * 1: check if for identical strings
14327 */
14328 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014329 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014330
14331 /* 2: swap */
14332 if (pl[0] == ps[1] && pl[1] == ps[0])
14333 {
14334 pl2 = pl + 2; /* swap, skip two chars */
14335 ps2 = ps + 2;
14336 while (*pl2 == *ps2)
14337 {
14338 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014339 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014340 ++pl2;
14341 ++ps2;
14342 }
14343 /* 3: swap and swap again */
14344 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14345 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014346 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014347
14348 /* 4: swap and substitute */
14349 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014350 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014351 }
14352
14353 /* 5: substitute */
14354 pl2 = pl + 1;
14355 ps2 = ps + 1;
14356 while (*pl2 == *ps2)
14357 {
14358 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014359 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014360 ++pl2;
14361 ++ps2;
14362 }
14363
14364 /* 6: substitute and swap */
14365 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14366 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014367 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014368
14369 /* 7: substitute and substitute */
14370 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014371 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014372
14373 /* 8: insert then delete */
14374 pl2 = pl;
14375 ps2 = ps + 1;
14376 while (*pl2 == *ps2)
14377 {
14378 ++pl2;
14379 ++ps2;
14380 }
14381 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014382 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014383
14384 /* 9: delete then insert */
14385 pl2 = pl + 1;
14386 ps2 = ps;
14387 while (*pl2 == *ps2)
14388 {
14389 ++pl2;
14390 ++ps2;
14391 }
14392 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014393 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014394
14395 /* Failed to compare. */
14396 break;
14397 }
14398
14399 return SCORE_MAXMAX;
14400}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014401
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014402/*
14403 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014404 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014405 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000014406 * The algorithm is described by Du and Chang, 1992.
14407 * The implementation of the algorithm comes from Aspell editdist.cpp,
14408 * edit_distance(). It has been converted from C++ to C and modified to
14409 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014410 */
14411 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +000014412spell_edit_score(slang, badword, goodword)
14413 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014414 char_u *badword;
14415 char_u *goodword;
14416{
14417 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014418 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014419 int j, i;
14420 int t;
14421 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014422 int pbc, pgc;
14423#ifdef FEAT_MBYTE
14424 char_u *p;
14425 int wbadword[MAXWLEN];
14426 int wgoodword[MAXWLEN];
14427
14428 if (has_mbyte)
14429 {
14430 /* Get the characters from the multi-byte strings and put them in an
14431 * int array for easy access. */
14432 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014433 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014434 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014435 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014436 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014437 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014438 }
14439 else
14440#endif
14441 {
14442 badlen = STRLEN(badword) + 1;
14443 goodlen = STRLEN(goodword) + 1;
14444 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014445
14446 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
14447#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014448 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
14449 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014450 if (cnt == NULL)
14451 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014452
14453 CNT(0, 0) = 0;
14454 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +000014455 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014456
14457 for (i = 1; i <= badlen; ++i)
14458 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014459 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014460 for (j = 1; j <= goodlen; ++j)
14461 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014462#ifdef FEAT_MBYTE
14463 if (has_mbyte)
14464 {
14465 bc = wbadword[i - 1];
14466 gc = wgoodword[j - 1];
14467 }
14468 else
14469#endif
14470 {
14471 bc = badword[i - 1];
14472 gc = goodword[j - 1];
14473 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014474 if (bc == gc)
14475 CNT(i, j) = CNT(i - 1, j - 1);
14476 else
14477 {
14478 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014479 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014480 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
14481 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014482 {
14483 /* For a similar character use SCORE_SIMILAR. */
14484 if (slang != NULL
14485 && slang->sl_has_map
14486 && similar_chars(slang, gc, bc))
14487 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
14488 else
14489 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
14490 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014491
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014492 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014493 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014494#ifdef FEAT_MBYTE
14495 if (has_mbyte)
14496 {
14497 pbc = wbadword[i - 2];
14498 pgc = wgoodword[j - 2];
14499 }
14500 else
14501#endif
14502 {
14503 pbc = badword[i - 2];
14504 pgc = goodword[j - 2];
14505 }
14506 if (bc == pgc && pbc == gc)
14507 {
14508 t = SCORE_SWAP + CNT(i - 2, j - 2);
14509 if (t < CNT(i, j))
14510 CNT(i, j) = t;
14511 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014512 }
14513 t = SCORE_DEL + CNT(i - 1, j);
14514 if (t < CNT(i, j))
14515 CNT(i, j) = t;
14516 t = SCORE_INS + CNT(i, j - 1);
14517 if (t < CNT(i, j))
14518 CNT(i, j) = t;
14519 }
14520 }
14521 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014522
14523 i = CNT(badlen - 1, goodlen - 1);
14524 vim_free(cnt);
14525 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014526}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000014527
Bram Moolenaar4770d092006-01-12 23:22:24 +000014528typedef struct
14529{
14530 int badi;
14531 int goodi;
14532 int score;
14533} limitscore_T;
14534
14535/*
14536 * Like spell_edit_score(), but with a limit on the score to make it faster.
14537 * May return SCORE_MAXMAX when the score is higher than "limit".
14538 *
14539 * This uses a stack for the edits still to be tried.
14540 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
14541 * for multi-byte characters.
14542 */
14543 static int
14544spell_edit_score_limit(slang, badword, goodword, limit)
14545 slang_T *slang;
14546 char_u *badword;
14547 char_u *goodword;
14548 int limit;
14549{
14550 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
14551 int stackidx;
14552 int bi, gi;
14553 int bi2, gi2;
14554 int bc, gc;
14555 int score;
14556 int score_off;
14557 int minscore;
14558 int round;
14559
14560#ifdef FEAT_MBYTE
14561 /* Multi-byte characters require a bit more work, use a different function
14562 * to avoid testing "has_mbyte" quite often. */
14563 if (has_mbyte)
14564 return spell_edit_score_limit_w(slang, badword, goodword, limit);
14565#endif
14566
14567 /*
14568 * The idea is to go from start to end over the words. So long as
14569 * characters are equal just continue, this always gives the lowest score.
14570 * When there is a difference try several alternatives. Each alternative
14571 * increases "score" for the edit distance. Some of the alternatives are
14572 * pushed unto a stack and tried later, some are tried right away. At the
14573 * end of the word the score for one alternative is known. The lowest
14574 * possible score is stored in "minscore".
14575 */
14576 stackidx = 0;
14577 bi = 0;
14578 gi = 0;
14579 score = 0;
14580 minscore = limit + 1;
14581
14582 for (;;)
14583 {
14584 /* Skip over an equal part, score remains the same. */
14585 for (;;)
14586 {
14587 bc = badword[bi];
14588 gc = goodword[gi];
14589 if (bc != gc) /* stop at a char that's different */
14590 break;
14591 if (bc == NUL) /* both words end */
14592 {
14593 if (score < minscore)
14594 minscore = score;
14595 goto pop; /* do next alternative */
14596 }
14597 ++bi;
14598 ++gi;
14599 }
14600
14601 if (gc == NUL) /* goodword ends, delete badword chars */
14602 {
14603 do
14604 {
14605 if ((score += SCORE_DEL) >= minscore)
14606 goto pop; /* do next alternative */
14607 } while (badword[++bi] != NUL);
14608 minscore = score;
14609 }
14610 else if (bc == NUL) /* badword ends, insert badword chars */
14611 {
14612 do
14613 {
14614 if ((score += SCORE_INS) >= minscore)
14615 goto pop; /* do next alternative */
14616 } while (goodword[++gi] != NUL);
14617 minscore = score;
14618 }
14619 else /* both words continue */
14620 {
14621 /* If not close to the limit, perform a change. Only try changes
14622 * that may lead to a lower score than "minscore".
14623 * round 0: try deleting a char from badword
14624 * round 1: try inserting a char in badword */
14625 for (round = 0; round <= 1; ++round)
14626 {
14627 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
14628 if (score_off < minscore)
14629 {
14630 if (score_off + SCORE_EDIT_MIN >= minscore)
14631 {
14632 /* Near the limit, rest of the words must match. We
14633 * can check that right now, no need to push an item
14634 * onto the stack. */
14635 bi2 = bi + 1 - round;
14636 gi2 = gi + round;
14637 while (goodword[gi2] == badword[bi2])
14638 {
14639 if (goodword[gi2] == NUL)
14640 {
14641 minscore = score_off;
14642 break;
14643 }
14644 ++bi2;
14645 ++gi2;
14646 }
14647 }
14648 else
14649 {
14650 /* try deleting/inserting a character later */
14651 stack[stackidx].badi = bi + 1 - round;
14652 stack[stackidx].goodi = gi + round;
14653 stack[stackidx].score = score_off;
14654 ++stackidx;
14655 }
14656 }
14657 }
14658
14659 if (score + SCORE_SWAP < minscore)
14660 {
14661 /* If swapping two characters makes a match then the
14662 * substitution is more expensive, thus there is no need to
14663 * try both. */
14664 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
14665 {
14666 /* Swap two characters, that is: skip them. */
14667 gi += 2;
14668 bi += 2;
14669 score += SCORE_SWAP;
14670 continue;
14671 }
14672 }
14673
14674 /* Substitute one character for another which is the same
14675 * thing as deleting a character from both goodword and badword.
14676 * Use a better score when there is only a case difference. */
14677 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
14678 score += SCORE_ICASE;
14679 else
14680 {
14681 /* For a similar character use SCORE_SIMILAR. */
14682 if (slang != NULL
14683 && slang->sl_has_map
14684 && similar_chars(slang, gc, bc))
14685 score += SCORE_SIMILAR;
14686 else
14687 score += SCORE_SUBST;
14688 }
14689
14690 if (score < minscore)
14691 {
14692 /* Do the substitution. */
14693 ++gi;
14694 ++bi;
14695 continue;
14696 }
14697 }
14698pop:
14699 /*
14700 * Get here to try the next alternative, pop it from the stack.
14701 */
14702 if (stackidx == 0) /* stack is empty, finished */
14703 break;
14704
14705 /* pop an item from the stack */
14706 --stackidx;
14707 gi = stack[stackidx].goodi;
14708 bi = stack[stackidx].badi;
14709 score = stack[stackidx].score;
14710 }
14711
14712 /* When the score goes over "limit" it may actually be much higher.
14713 * Return a very large number to avoid going below the limit when giving a
14714 * bonus. */
14715 if (minscore > limit)
14716 return SCORE_MAXMAX;
14717 return minscore;
14718}
14719
14720#ifdef FEAT_MBYTE
14721/*
14722 * Multi-byte version of spell_edit_score_limit().
14723 * Keep it in sync with the above!
14724 */
14725 static int
14726spell_edit_score_limit_w(slang, badword, goodword, limit)
14727 slang_T *slang;
14728 char_u *badword;
14729 char_u *goodword;
14730 int limit;
14731{
14732 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
14733 int stackidx;
14734 int bi, gi;
14735 int bi2, gi2;
14736 int bc, gc;
14737 int score;
14738 int score_off;
14739 int minscore;
14740 int round;
14741 char_u *p;
14742 int wbadword[MAXWLEN];
14743 int wgoodword[MAXWLEN];
14744
14745 /* Get the characters from the multi-byte strings and put them in an
14746 * int array for easy access. */
14747 bi = 0;
14748 for (p = badword; *p != NUL; )
14749 wbadword[bi++] = mb_cptr2char_adv(&p);
14750 wbadword[bi++] = 0;
14751 gi = 0;
14752 for (p = goodword; *p != NUL; )
14753 wgoodword[gi++] = mb_cptr2char_adv(&p);
14754 wgoodword[gi++] = 0;
14755
14756 /*
14757 * The idea is to go from start to end over the words. So long as
14758 * characters are equal just continue, this always gives the lowest score.
14759 * When there is a difference try several alternatives. Each alternative
14760 * increases "score" for the edit distance. Some of the alternatives are
14761 * pushed unto a stack and tried later, some are tried right away. At the
14762 * end of the word the score for one alternative is known. The lowest
14763 * possible score is stored in "minscore".
14764 */
14765 stackidx = 0;
14766 bi = 0;
14767 gi = 0;
14768 score = 0;
14769 minscore = limit + 1;
14770
14771 for (;;)
14772 {
14773 /* Skip over an equal part, score remains the same. */
14774 for (;;)
14775 {
14776 bc = wbadword[bi];
14777 gc = wgoodword[gi];
14778
14779 if (bc != gc) /* stop at a char that's different */
14780 break;
14781 if (bc == NUL) /* both words end */
14782 {
14783 if (score < minscore)
14784 minscore = score;
14785 goto pop; /* do next alternative */
14786 }
14787 ++bi;
14788 ++gi;
14789 }
14790
14791 if (gc == NUL) /* goodword ends, delete badword chars */
14792 {
14793 do
14794 {
14795 if ((score += SCORE_DEL) >= minscore)
14796 goto pop; /* do next alternative */
14797 } while (wbadword[++bi] != NUL);
14798 minscore = score;
14799 }
14800 else if (bc == NUL) /* badword ends, insert badword chars */
14801 {
14802 do
14803 {
14804 if ((score += SCORE_INS) >= minscore)
14805 goto pop; /* do next alternative */
14806 } while (wgoodword[++gi] != NUL);
14807 minscore = score;
14808 }
14809 else /* both words continue */
14810 {
14811 /* If not close to the limit, perform a change. Only try changes
14812 * that may lead to a lower score than "minscore".
14813 * round 0: try deleting a char from badword
14814 * round 1: try inserting a char in badword */
14815 for (round = 0; round <= 1; ++round)
14816 {
14817 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
14818 if (score_off < minscore)
14819 {
14820 if (score_off + SCORE_EDIT_MIN >= minscore)
14821 {
14822 /* Near the limit, rest of the words must match. We
14823 * can check that right now, no need to push an item
14824 * onto the stack. */
14825 bi2 = bi + 1 - round;
14826 gi2 = gi + round;
14827 while (wgoodword[gi2] == wbadword[bi2])
14828 {
14829 if (wgoodword[gi2] == NUL)
14830 {
14831 minscore = score_off;
14832 break;
14833 }
14834 ++bi2;
14835 ++gi2;
14836 }
14837 }
14838 else
14839 {
14840 /* try deleting a character from badword later */
14841 stack[stackidx].badi = bi + 1 - round;
14842 stack[stackidx].goodi = gi + round;
14843 stack[stackidx].score = score_off;
14844 ++stackidx;
14845 }
14846 }
14847 }
14848
14849 if (score + SCORE_SWAP < minscore)
14850 {
14851 /* If swapping two characters makes a match then the
14852 * substitution is more expensive, thus there is no need to
14853 * try both. */
14854 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
14855 {
14856 /* Swap two characters, that is: skip them. */
14857 gi += 2;
14858 bi += 2;
14859 score += SCORE_SWAP;
14860 continue;
14861 }
14862 }
14863
14864 /* Substitute one character for another which is the same
14865 * thing as deleting a character from both goodword and badword.
14866 * Use a better score when there is only a case difference. */
14867 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
14868 score += SCORE_ICASE;
14869 else
14870 {
14871 /* For a similar character use SCORE_SIMILAR. */
14872 if (slang != NULL
14873 && slang->sl_has_map
14874 && similar_chars(slang, gc, bc))
14875 score += SCORE_SIMILAR;
14876 else
14877 score += SCORE_SUBST;
14878 }
14879
14880 if (score < minscore)
14881 {
14882 /* Do the substitution. */
14883 ++gi;
14884 ++bi;
14885 continue;
14886 }
14887 }
14888pop:
14889 /*
14890 * Get here to try the next alternative, pop it from the stack.
14891 */
14892 if (stackidx == 0) /* stack is empty, finished */
14893 break;
14894
14895 /* pop an item from the stack */
14896 --stackidx;
14897 gi = stack[stackidx].goodi;
14898 bi = stack[stackidx].badi;
14899 score = stack[stackidx].score;
14900 }
14901
14902 /* When the score goes over "limit" it may actually be much higher.
14903 * Return a very large number to avoid going below the limit when giving a
14904 * bonus. */
14905 if (minscore > limit)
14906 return SCORE_MAXMAX;
14907 return minscore;
14908}
14909#endif
14910
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014911/*
14912 * ":spellinfo"
14913 */
14914/*ARGSUSED*/
14915 void
14916ex_spellinfo(eap)
14917 exarg_T *eap;
14918{
14919 int lpi;
14920 langp_T *lp;
14921 char_u *p;
14922
14923 if (no_spell_checking(curwin))
14924 return;
14925
14926 msg_start();
14927 for (lpi = 0; lpi < curbuf->b_langp.ga_len && !got_int; ++lpi)
14928 {
14929 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
14930 msg_puts((char_u *)"file: ");
14931 msg_puts(lp->lp_slang->sl_fname);
14932 msg_putchar('\n');
14933 p = lp->lp_slang->sl_info;
14934 if (p != NULL)
14935 {
14936 msg_puts(p);
14937 msg_putchar('\n');
14938 }
14939 }
14940 msg_end();
14941}
14942
Bram Moolenaar4770d092006-01-12 23:22:24 +000014943#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
14944#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000014945#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +000014946#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
14947#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +000014948
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014949/*
14950 * ":spelldump"
14951 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014952 void
14953ex_spelldump(eap)
14954 exarg_T *eap;
14955{
14956 buf_T *buf = curbuf;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000014957
14958 if (no_spell_checking(curwin))
14959 return;
14960
14961 /* Create a new empty buffer by splitting the window. */
14962 do_cmdline_cmd((char_u *)"new");
14963 if (!bufempty() || !buf_valid(buf))
14964 return;
14965
14966 spell_dump_compl(buf, NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
14967
14968 /* Delete the empty line that we started with. */
14969 if (curbuf->b_ml.ml_line_count > 1)
14970 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
14971
14972 redraw_later(NOT_VALID);
14973}
14974
14975/*
14976 * Go through all possible words and:
14977 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
14978 * "ic" and "dir" are not used.
14979 * 2. When "pat" is not NULL: add matching words to insert mode completion.
14980 */
14981 void
14982spell_dump_compl(buf, pat, ic, dir, dumpflags_arg)
14983 buf_T *buf; /* buffer with spell checking */
14984 char_u *pat; /* leading part of the word */
14985 int ic; /* ignore case */
14986 int *dir; /* direction for adding matches */
14987 int dumpflags_arg; /* DUMPFLAG_* */
14988{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014989 langp_T *lp;
14990 slang_T *slang;
14991 idx_T arridx[MAXWLEN];
14992 int curi[MAXWLEN];
14993 char_u word[MAXWLEN];
14994 int c;
14995 char_u *byts;
14996 idx_T *idxs;
14997 linenr_T lnum = 0;
14998 int round;
14999 int depth;
15000 int n;
15001 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015002 char_u *region_names = NULL; /* region names being used */
15003 int do_region = TRUE; /* dump region names and numbers */
15004 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015005 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015006 int dumpflags = dumpflags_arg;
15007 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015008
Bram Moolenaard0131a82006-03-04 21:46:13 +000015009 /* When ignoring case or when the pattern starts with capital pass this on
15010 * to dump_word(). */
15011 if (pat != NULL)
15012 {
15013 if (ic)
15014 dumpflags |= DUMPFLAG_ICASE;
15015 else
15016 {
15017 n = captype(pat, NULL);
15018 if (n == WF_ONECAP)
15019 dumpflags |= DUMPFLAG_ONECAP;
15020 else if (n == WF_ALLCAP
15021#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015022 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015023#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015024 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +000015025#endif
15026 )
15027 dumpflags |= DUMPFLAG_ALLCAP;
15028 }
15029 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015030
Bram Moolenaar7887d882005-07-01 22:33:52 +000015031 /* Find out if we can support regions: All languages must support the same
15032 * regions or none at all. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015033 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000015034 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015035 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000015036 p = lp->lp_slang->sl_regions;
15037 if (p[0] != 0)
15038 {
15039 if (region_names == NULL) /* first language with regions */
15040 region_names = p;
15041 else if (STRCMP(region_names, p) != 0)
15042 {
15043 do_region = FALSE; /* region names are different */
15044 break;
15045 }
15046 }
15047 }
15048
15049 if (do_region && region_names != NULL)
15050 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015051 if (pat == NULL)
15052 {
15053 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
15054 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15055 }
Bram Moolenaar7887d882005-07-01 22:33:52 +000015056 }
15057 else
15058 do_region = FALSE;
15059
15060 /*
15061 * Loop over all files loaded for the entries in 'spelllang'.
15062 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015063 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015064 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015065 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015066 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015067 if (slang->sl_fbyts == NULL) /* reloading failed */
15068 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015069
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015070 if (pat == NULL)
15071 {
15072 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
15073 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15074 }
15075
15076 /* When matching with a pattern and there are no prefixes only use
15077 * parts of the tree that match "pat". */
15078 if (pat != NULL && slang->sl_pbyts == NULL)
15079 patlen = STRLEN(pat);
15080 else
15081 patlen = 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015082
15083 /* round 1: case-folded tree
15084 * round 2: keep-case tree */
15085 for (round = 1; round <= 2; ++round)
15086 {
15087 if (round == 1)
15088 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015089 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015090 byts = slang->sl_fbyts;
15091 idxs = slang->sl_fidxs;
15092 }
15093 else
15094 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015095 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015096 byts = slang->sl_kbyts;
15097 idxs = slang->sl_kidxs;
15098 }
15099 if (byts == NULL)
15100 continue; /* array is empty */
15101
15102 depth = 0;
15103 arridx[0] = 0;
15104 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015105 while (depth >= 0 && !got_int
15106 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015107 {
15108 if (curi[depth] > byts[arridx[depth]])
15109 {
15110 /* Done all bytes at this node, go up one level. */
15111 --depth;
15112 line_breakcheck();
Bram Moolenaara2031822006-03-07 22:29:51 +000015113 ins_compl_check_keys(50);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015114 }
15115 else
15116 {
15117 /* Do one more byte at this node. */
15118 n = arridx[depth] + curi[depth];
15119 ++curi[depth];
15120 c = byts[n];
15121 if (c == 0)
15122 {
15123 /* End of word, deal with the word.
15124 * Don't use keep-case words in the fold-case tree,
15125 * they will appear in the keep-case tree.
15126 * Only use the word when the region matches. */
15127 flags = (int)idxs[n];
15128 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015129 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000015130 && (do_region
15131 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015132 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015133 & lp->lp_region) != 0))
15134 {
15135 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015136 if (!do_region)
15137 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015138
15139 /* Dump the basic word if there is no prefix or
15140 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015141 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015142 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015143 {
15144 dump_word(slang, word, pat, dir,
15145 dumpflags, flags, lnum);
15146 if (pat == NULL)
15147 ++lnum;
15148 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015149
15150 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015151 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015152 lnum = dump_prefixes(slang, word, pat, dir,
15153 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015154 }
15155 }
15156 else
15157 {
15158 /* Normal char, go one level deeper. */
15159 word[depth++] = c;
15160 arridx[depth] = idxs[n];
15161 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015162
15163 /* Check if this characters matches with the pattern.
15164 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +000015165 * Always ignore case here, dump_word() will check
15166 * proper case later. This isn't exactly right when
15167 * length changes for multi-byte characters with
15168 * ignore case... */
15169 if (depth <= patlen
15170 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015171 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015172 }
15173 }
15174 }
15175 }
15176 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015177}
15178
15179/*
15180 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015181 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015182 */
15183 static void
Bram Moolenaard0131a82006-03-04 21:46:13 +000015184dump_word(slang, word, pat, dir, dumpflags, wordflags, lnum)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015185 slang_T *slang;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015186 char_u *word;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015187 char_u *pat;
15188 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015189 int dumpflags;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015190 int wordflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015191 linenr_T lnum;
15192{
15193 int keepcap = FALSE;
15194 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015195 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015196 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000015197 char_u badword[MAXWLEN + 10];
15198 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015199 int flags = wordflags;
15200
15201 if (dumpflags & DUMPFLAG_ONECAP)
15202 flags |= WF_ONECAP;
15203 if (dumpflags & DUMPFLAG_ALLCAP)
15204 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015205
Bram Moolenaar4770d092006-01-12 23:22:24 +000015206 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015207 {
15208 /* Need to fix case according to "flags". */
15209 make_case_word(word, cword, flags);
15210 p = cword;
15211 }
15212 else
15213 {
15214 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015215 if ((dumpflags & DUMPFLAG_KEEPCASE)
15216 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000015217 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015218 keepcap = TRUE;
15219 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015220 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015221
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015222 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015223 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015224 /* Add flags and regions after a slash. */
15225 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015226 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015227 STRCPY(badword, p);
15228 STRCAT(badword, "/");
15229 if (keepcap)
15230 STRCAT(badword, "=");
15231 if (flags & WF_BANNED)
15232 STRCAT(badword, "!");
15233 else if (flags & WF_RARE)
15234 STRCAT(badword, "?");
15235 if (flags & WF_REGION)
15236 for (i = 0; i < 7; ++i)
15237 if (flags & (0x10000 << i))
15238 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
15239 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015240 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015241
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015242 if (dumpflags & DUMPFLAG_COUNT)
15243 {
15244 hashitem_T *hi;
15245
15246 /* Include the word count for ":spelldump!". */
15247 hi = hash_find(&slang->sl_wordcount, tw);
15248 if (!HASHITEM_EMPTY(hi))
15249 {
15250 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
15251 tw, HI2WC(hi)->wc_count);
15252 p = IObuff;
15253 }
15254 }
15255
15256 ml_append(lnum, p, (colnr_T)0, FALSE);
15257 }
Bram Moolenaard0131a82006-03-04 21:46:13 +000015258 else if (((dumpflags & DUMPFLAG_ICASE)
15259 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
15260 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015261 && ins_compl_add_infercase(p, (int)STRLEN(p),
15262 dumpflags & DUMPFLAG_ICASE,
15263 NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015264 /* if dir was BACKWARD then honor it just once */
15265 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015266}
15267
15268/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000015269 * For ":spelldump": Find matching prefixes for "word". Prepend each to
15270 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015271 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015272 * Return the updated line number.
15273 */
15274 static linenr_T
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015275dump_prefixes(slang, word, pat, dir, dumpflags, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015276 slang_T *slang;
15277 char_u *word; /* case-folded word */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015278 char_u *pat;
15279 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015280 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015281 int flags; /* flags with prefix ID */
15282 linenr_T startlnum;
15283{
15284 idx_T arridx[MAXWLEN];
15285 int curi[MAXWLEN];
15286 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000015287 char_u word_up[MAXWLEN];
15288 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015289 int c;
15290 char_u *byts;
15291 idx_T *idxs;
15292 linenr_T lnum = startlnum;
15293 int depth;
15294 int n;
15295 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015296 int i;
15297
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015298 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +000015299 * upper-case letter in word_up[]. */
15300 c = PTR2CHAR(word);
15301 if (SPELL_TOUPPER(c) != c)
15302 {
15303 onecap_copy(word, word_up, TRUE);
15304 has_word_up = TRUE;
15305 }
15306
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015307 byts = slang->sl_pbyts;
15308 idxs = slang->sl_pidxs;
15309 if (byts != NULL) /* array not is empty */
15310 {
15311 /*
15312 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015313 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015314 */
15315 depth = 0;
15316 arridx[0] = 0;
15317 curi[0] = 1;
15318 while (depth >= 0 && !got_int)
15319 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015320 n = arridx[depth];
15321 len = byts[n];
15322 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015323 {
15324 /* Done all bytes at this node, go up one level. */
15325 --depth;
15326 line_breakcheck();
15327 }
15328 else
15329 {
15330 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015331 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015332 ++curi[depth];
15333 c = byts[n];
15334 if (c == 0)
15335 {
15336 /* End of prefix, find out how many IDs there are. */
15337 for (i = 1; i < len; ++i)
15338 if (byts[n + i] != 0)
15339 break;
15340 curi[depth] += i - 1;
15341
Bram Moolenaar53805d12005-08-01 07:08:33 +000015342 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
15343 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015344 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000015345 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015346 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015347 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015348 : flags, lnum);
15349 if (lnum != 0)
15350 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015351 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000015352
15353 /* Check for prefix that matches the word when the
15354 * first letter is upper-case, but only if the prefix has
15355 * a condition. */
15356 if (has_word_up)
15357 {
15358 c = valid_word_prefix(i, n, flags, word_up, slang,
15359 TRUE);
15360 if (c != 0)
15361 {
15362 vim_strncpy(prefix + depth, word_up,
15363 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015364 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015365 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015366 : flags, lnum);
15367 if (lnum != 0)
15368 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +000015369 }
15370 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015371 }
15372 else
15373 {
15374 /* Normal char, go one level deeper. */
15375 prefix[depth++] = c;
15376 arridx[depth] = idxs[n];
15377 curi[depth] = 1;
15378 }
15379 }
15380 }
15381 }
15382
15383 return lnum;
15384}
15385
Bram Moolenaar95529562005-08-25 21:21:38 +000015386/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015387 * Move "p" to the end of word "start".
15388 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +000015389 */
15390 char_u *
15391spell_to_word_end(start, buf)
15392 char_u *start;
15393 buf_T *buf;
15394{
15395 char_u *p = start;
15396
15397 while (*p != NUL && spell_iswordp(p, buf))
15398 mb_ptr_adv(p);
15399 return p;
15400}
15401
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015402#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015403/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015404 * For Insert mode completion CTRL-X s:
15405 * Find start of the word in front of column "startcol".
15406 * We don't check if it is badly spelled, with completion we can only change
15407 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015408 * Returns the column number of the word.
15409 */
15410 int
15411spell_word_start(startcol)
15412 int startcol;
15413{
15414 char_u *line;
15415 char_u *p;
15416 int col = 0;
15417
Bram Moolenaar95529562005-08-25 21:21:38 +000015418 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015419 return startcol;
15420
15421 /* Find a word character before "startcol". */
15422 line = ml_get_curline();
15423 for (p = line + startcol; p > line; )
15424 {
15425 mb_ptr_back(line, p);
15426 if (spell_iswordp_nmw(p))
15427 break;
15428 }
15429
15430 /* Go back to start of the word. */
15431 while (p > line)
15432 {
15433 col = p - line;
15434 mb_ptr_back(line, p);
15435 if (!spell_iswordp(p, curbuf))
15436 break;
15437 col = 0;
15438 }
15439
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015440 return col;
15441}
15442
15443/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000015444 * Need to check for 'spellcapcheck' now, the word is removed before
15445 * expand_spelling() is called. Therefore the ugly global variable.
15446 */
15447static int spell_expand_need_cap;
15448
15449 void
15450spell_expand_check_cap(col)
15451 colnr_T col;
15452{
15453 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
15454}
15455
15456/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015457 * Get list of spelling suggestions.
15458 * Used for Insert mode completion CTRL-X ?.
15459 * Returns the number of matches. The matches are in "matchp[]", array of
15460 * allocated strings.
15461 */
15462/*ARGSUSED*/
15463 int
15464expand_spelling(lnum, col, pat, matchp)
15465 linenr_T lnum;
15466 int col;
15467 char_u *pat;
15468 char_u ***matchp;
15469{
15470 garray_T ga;
15471
Bram Moolenaar4770d092006-01-12 23:22:24 +000015472 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015473 *matchp = ga.ga_data;
15474 return ga.ga_len;
15475}
15476#endif
15477
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000015478#endif /* FEAT_SYN_HL */