blob: 12a8a09e734d97f80a80bf5b361a98b9fa1b5b9b [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 *
120 * sectionID == SN_REGION: <regionname> ...
121 * <regionname> 2 bytes Up to 8 region names: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000122 * First <regionname> is region 1.
123 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000124 * sectionID == SN_CHARFLAGS: <charflagslen> <charflags>
125 * <folcharslen> <folchars>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000126 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
127 * <charflags> N bytes List of flags (first one is for character 128):
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000128 * 0x01 word character CF_WORD
129 * 0x02 upper-case character CF_UPPER
Bram Moolenaar5195e452005-08-19 20:32:47 +0000130 * <folcharslen> 2 bytes Number of bytes in <folchars>.
131 * <folchars> N bytes Folded characters, first one is for character 128.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000132 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000133 * sectionID == SN_MIDWORD: <midword>
134 * <midword> N bytes Characters that are word characters only when used
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000135 * in the middle of a word.
136 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000137 * sectionID == SN_PREFCOND: <prefcondcnt> <prefcond> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000138 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000139 * <prefcond> : <condlen> <condstr>
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000140 * <condlen> 1 byte Length of <condstr>.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000141 * <condstr> N bytes Condition for the prefix.
142 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000143 * sectionID == SN_REP: <repcount> <rep> ...
144 * <repcount> 2 bytes number of <rep> items, MSB first.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000145 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000146 * <repfromlen> 1 byte length of <repfrom>
147 * <repfrom> N bytes "from" part of replacement
148 * <reptolen> 1 byte length of <repto>
149 * <repto> N bytes "to" part of replacement
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000150 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000151 * sectionID == SN_REPSAL: <repcount> <rep> ...
152 * just like SN_REP but for soundfolded words
153 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000154 * sectionID == SN_SAL: <salflags> <salcount> <sal> ...
155 * <salflags> 1 byte flags for soundsalike conversion:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000156 * SAL_F0LLOWUP
157 * SAL_COLLAPSE
158 * SAL_REM_ACCENTS
Bram Moolenaar5195e452005-08-19 20:32:47 +0000159 * <salcount> 2 bytes number of <sal> items following
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000160 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000161 * <salfromlen> 1 byte length of <salfrom>
162 * <salfrom> N bytes "from" part of soundsalike
163 * <saltolen> 1 byte length of <salto>
164 * <salto> N bytes "to" part of soundsalike
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000165 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000166 * sectionID == SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
167 * <sofofromlen> 2 bytes length of <sofofrom>
168 * <sofofrom> N bytes "from" part of soundfold
169 * <sofotolen> 2 bytes length of <sofoto>
170 * <sofoto> N bytes "to" part of soundfold
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000171 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000172 * sectionID == SN_SUGFILE: <timestamp>
173 * <timestamp> 8 bytes time in seconds that must match with .sug file
174 *
175 * sectionID == SN_WORDS: <word> ...
176 * <word> N bytes NUL terminated common word
177 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000178 * sectionID == SN_MAP: <mapstr>
179 * <mapstr> N bytes String with sequences of similar characters,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000180 * separated by slashes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000181 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000182 * sectionID == SN_COMPOUND: <compmax> <compminlen> <compsylmax> <compflags>
183 * <compmax> 1 byte Maximum nr of words in compound word.
184 * <compminlen> 1 byte Minimal word length for compounding.
185 * <compsylmax> 1 byte Maximum nr of syllables in compound word.
186 * <compflags> N bytes Flags from COMPOUNDFLAGS items, separated by
187 * slashes.
188 *
Bram Moolenaar78622822005-08-23 21:00:13 +0000189 * sectionID == SN_NOBREAK: (empty, its presence is enough)
190 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000191 * sectionID == SN_SYLLABLE: <syllable>
192 * <syllable> N bytes String from SYLLABLE item.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000193 *
194 * <LWORDTREE>: <wordtree>
195 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000196 * <KWORDTREE>: <wordtree>
197 *
198 * <PREFIXTREE>: <wordtree>
199 *
200 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000201 * <wordtree>: <nodecount> <nodedata> ...
202 *
203 * <nodecount> 4 bytes Number of nodes following. MSB first.
204 *
205 * <nodedata>: <siblingcount> <sibling> ...
206 *
207 * <siblingcount> 1 byte Number of siblings in this node. The siblings
208 * follow in sorted order.
209 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000210 * <sibling>: <byte> [ <nodeidx> <xbyte>
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000211 * | <flags> [<flags2>] [<region>] [<affixID>]
212 * | [<pflags>] <affixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000213 *
214 * <byte> 1 byte Byte value of the sibling. Special cases:
215 * BY_NOFLAGS: End of word without flags and for all
216 * regions.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000217 * For PREFIXTREE <affixID> and
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000218 * <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000219 * BY_FLAGS: End of word, <flags> follow.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000220 * For PREFIXTREE <pflags>, <affixID>
Bram Moolenaar53805d12005-08-01 07:08:33 +0000221 * and <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000222 * BY_FLAGS2: End of word, <flags> and <flags2>
223 * follow. Not used in PREFIXTREE.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000224 * BY_INDEX: Child of sibling is shared, <nodeidx>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000225 * and <xbyte> follow.
226 *
227 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
228 *
229 * <xbyte> 1 byte byte value of the sibling.
230 *
231 * <flags> 1 byte bitmask of:
232 * WF_ALLCAP word must have only capitals
233 * WF_ONECAP first char of word must be capital
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000234 * WF_KEEPCAP keep-case word
235 * WF_FIXCAP keep-case word, all caps not allowed
Bram Moolenaar51485f02005-06-04 21:55:20 +0000236 * WF_RARE rare word
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000237 * WF_BANNED bad word
Bram Moolenaar51485f02005-06-04 21:55:20 +0000238 * WF_REGION <region> follows
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000239 * WF_AFX <affixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000240 *
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000241 * <flags2> 1 byte Bitmask of:
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000242 * WF_HAS_AFF >> 8 word includes affix
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000243 * WF_NEEDCOMP >> 8 word only valid in compound
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000244 *
Bram Moolenaar53805d12005-08-01 07:08:33 +0000245 * <pflags> 1 byte bitmask of:
246 * WFP_RARE rare prefix
247 * WFP_NC non-combining prefix
248 * WFP_UP letter after prefix made upper case
249 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000250 * <region> 1 byte Bitmask for regions in which word is valid. When
251 * omitted it's valid in all regions.
252 * Lowest bit is for region 1.
253 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000254 * <affixID> 1 byte ID of affix that can be used with this word. In
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000255 * PREFIXTREE used for the required prefix ID.
256 *
257 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
258 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000259 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000260 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000261 */
262
Bram Moolenaar4770d092006-01-12 23:22:24 +0000263/*
264 * Vim .sug file format: <SUGHEADER>
265 * <SUGWORDTREE>
266 * <SUGTABLE>
267 *
268 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
269 *
270 * <fileID> 6 bytes "VIMsug"
271 * <versionnr> 1 byte VIMSUGVERSION
272 * <timestamp> 8 bytes timestamp that must match with .spl file
273 *
274 *
275 * <SUGWORDTREE>: <wordtree> (see above, no flags or region used)
276 *
277 *
278 * <SUGTABLE>: <sugwcount> <sugline> ...
279 *
280 * <sugwcount> 4 bytes number of <sugline> following
281 *
282 * <sugline>: <sugnr> ... NUL
283 *
284 * <sugnr>: X bytes word number that results in this soundfolded word,
285 * stored as an offset to the previous number in as
286 * few bytes as possible, see offset2bytes())
287 */
288
Bram Moolenaare19defe2005-03-21 08:23:33 +0000289#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
290# include <io.h> /* for lseek(), must be before vim.h */
291#endif
292
293#include "vim.h"
294
295#if defined(FEAT_SYN_HL) || defined(PROTO)
296
297#ifdef HAVE_FCNTL_H
298# include <fcntl.h>
299#endif
300
Bram Moolenaar4770d092006-01-12 23:22:24 +0000301#ifndef UNIX /* it's in os_unix.h for Unix */
302# include <time.h> /* for time_t */
303#endif
304
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000305#define MAXWLEN 250 /* Assume max. word len is this many bytes.
306 Some places assume a word length fits in a
307 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000308
Bram Moolenaare52325c2005-08-22 22:54:29 +0000309/* Type used for indexes in the word tree need to be at least 4 bytes. If int
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000310 * is 8 bytes we could use something smaller, but what? */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000311#if SIZEOF_INT > 3
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000312typedef int idx_T;
313#else
314typedef long idx_T;
315#endif
316
317/* Flags used for a word. Only the lowest byte can be used, the region byte
318 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000319#define WF_REGION 0x01 /* region byte follows */
320#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
321#define WF_ALLCAP 0x04 /* word must be all capitals */
322#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000323#define WF_BANNED 0x10 /* bad word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000324#define WF_AFX 0x20 /* affix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000325#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000326#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000327
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000328/* for <flags2>, shifted up one byte to be used in wn_flags */
329#define WF_HAS_AFF 0x0100 /* word includes affix */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000330#define WF_NEEDCOMP 0x0200 /* word only valid in compound */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000331
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000332/* only used for su_badflags */
333#define WF_MIXCAP 0x20 /* mix of upper and lower case: macaRONI */
334
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000335#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000336
Bram Moolenaar53805d12005-08-01 07:08:33 +0000337/* flags for <pflags> */
338#define WFP_RARE 0x01 /* rare prefix */
339#define WFP_NC 0x02 /* prefix is not combining */
340#define WFP_UP 0x04 /* to-upper prefix */
341
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000342/* Flags for postponed prefixes. Must be above affixID (one byte)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000343 * and prefcondnr (two bytes). */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000344#define WF_RAREPFX (WFP_RARE << 24) /* in sl_pidxs: flag for rare
345 * postponed prefix */
346#define WF_PFX_NC (WFP_NC << 24) /* in sl_pidxs: flag for non-combining
347 * postponed prefix */
348#define WF_PFX_UP (WFP_UP << 24) /* in sl_pidxs: flag for to-upper
349 * postponed prefix */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000350
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000351/* Special byte values for <byte>. Some are only used in the tree for
352 * postponed prefixes, some only in the other trees. This is a bit messy... */
353#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000354 * postponed prefix: no <pflags> */
355#define BY_INDEX 1 /* child is shared, index follows */
356#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
357 * postponed prefix: <pflags> follows */
358#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
359 * follow; never used in prefix tree */
360#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000361
Bram Moolenaar4770d092006-01-12 23:22:24 +0000362/* Info from "REP", "REPSAL" and "SAL" entries in ".aff" file used in si_rep,
363 * si_repsal, sl_rep, and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000364 * One replacement: from "ft_from" to "ft_to". */
365typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000366{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000367 char_u *ft_from;
368 char_u *ft_to;
369} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000370
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000371/* Info from "SAL" entries in ".aff" file used in sl_sal.
372 * The info is split for quick processing by spell_soundfold().
373 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
374typedef struct salitem_S
375{
376 char_u *sm_lead; /* leading letters */
377 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000378 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000379 char_u *sm_rules; /* rules like ^, $, priority */
380 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000381#ifdef FEAT_MBYTE
382 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000383 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000384 int *sm_to_w; /* wide character copy of "sm_to" */
385#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000386} salitem_T;
387
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000388#ifdef FEAT_MBYTE
389typedef int salfirst_T;
390#else
391typedef short salfirst_T;
392#endif
393
Bram Moolenaar5195e452005-08-19 20:32:47 +0000394/* Values for SP_*ERROR are negative, positive values are used by
395 * read_cnt_string(). */
396#define SP_TRUNCERROR -1 /* spell file truncated error */
397#define SP_FORMERROR -2 /* format error in spell file */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000398#define SP_OTHERERROR -3 /* other error while reading spell file */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000399
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000400/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000401 * Structure used to store words and other info for one language, loaded from
402 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000403 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
404 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
405 *
406 * The "byts" array stores the possible bytes in each tree node, preceded by
407 * the number of possible bytes, sorted on byte value:
408 * <len> <byte1> <byte2> ...
409 * The "idxs" array stores the index of the child node corresponding to the
410 * byte in "byts".
411 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000412 * the flags, region mask and affixID for the word. There may be several
413 * zeros in sequence for alternative flag/region/affixID combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000414 */
415typedef struct slang_S slang_T;
416struct slang_S
417{
418 slang_T *sl_next; /* next language */
419 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000420 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000421 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000422
Bram Moolenaar51485f02005-06-04 21:55:20 +0000423 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000424 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000425 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000426 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000427 char_u *sl_pbyts; /* prefix tree word bytes */
428 idx_T *sl_pidxs; /* prefix tree word indexes */
429
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000430 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000431
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000432 char_u *sl_midword; /* MIDWORD string or NULL */
433
Bram Moolenaar4770d092006-01-12 23:22:24 +0000434 hashtab_T sl_wordcount; /* hashtable with word count, wordcount_T */
435
Bram Moolenaar5195e452005-08-19 20:32:47 +0000436 int sl_compmax; /* COMPOUNDMAX (default: MAXWLEN) */
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000437 int sl_compminlen; /* COMPOUNDMIN (default: 0) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000438 int sl_compsylmax; /* COMPOUNDSYLMAX (default: MAXWLEN) */
439 regprog_T *sl_compprog; /* COMPOUNDFLAGS turned into a regexp progrm
440 * (NULL when no compounding) */
441 char_u *sl_compstartflags; /* flags for first compound word */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000442 char_u *sl_compallflags; /* all flags for compound words */
Bram Moolenaar78622822005-08-23 21:00:13 +0000443 char_u sl_nobreak; /* When TRUE: no spaces between words */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000444 char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */
445 garray_T sl_syl_items; /* syllable items */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000446
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000447 int sl_prefixcnt; /* number of items in "sl_prefprog" */
448 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
449
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000450 garray_T sl_rep; /* list of fromto_T entries from REP lines */
451 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
452 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000453 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000454 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000455 there is none */
456 int sl_followup; /* SAL followup */
457 int sl_collapse; /* SAL collapse_result */
458 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000459 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
460 * "sl_sal_first" maps chars, when has_mbyte
461 * "sl_sal" is a list of wide char lists. */
462 garray_T sl_repsal; /* list of fromto_T entries from REPSAL lines */
463 short sl_repsal_first[256]; /* sl_rep_first for REPSAL lines */
464
465 /* Info from the .sug file. Loaded on demand. */
466 time_t sl_sugtime; /* timestamp for .sug file */
467 char_u *sl_sbyts; /* soundfolded word bytes */
468 idx_T *sl_sidxs; /* soundfolded word indexes */
469 buf_T *sl_sugbuf; /* buffer with word number table */
470 int sl_sugloaded; /* TRUE when .sug file was loaded or failed to
471 load */
472
Bram Moolenaarea424162005-06-16 21:51:00 +0000473 int sl_has_map; /* TRUE if there is a MAP line */
474#ifdef FEAT_MBYTE
475 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
476 int sl_map_array[256]; /* MAP for first 256 chars */
477#else
478 char_u sl_map_array[256]; /* MAP for first 256 chars */
479#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000480 hashtab_T sl_sounddone; /* table with soundfolded words that have
481 handled, see add_sound_suggest() */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000482};
483
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000484/* First language that is loaded, start of the linked list of loaded
485 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000486static slang_T *first_lang = NULL;
487
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000488/* Flags used in .spl file for soundsalike flags. */
489#define SAL_F0LLOWUP 1
490#define SAL_COLLAPSE 2
491#define SAL_REM_ACCENTS 4
492
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000493/*
494 * Structure used in "b_langp", filled from 'spelllang'.
495 */
496typedef struct langp_S
497{
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000498 slang_T *lp_slang; /* info for this language */
499 slang_T *lp_sallang; /* language used for sound folding or NULL */
500 slang_T *lp_replang; /* language used for REP items or NULL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000501 int lp_region; /* bitmask for region or REGION_ALL */
502} langp_T;
503
504#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
505
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000506#define REGION_ALL 0xff /* word valid in all regions */
507
Bram Moolenaar5195e452005-08-19 20:32:47 +0000508#define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
509#define VIMSPELLMAGICL 8
510#define VIMSPELLVERSION 50
511
Bram Moolenaar4770d092006-01-12 23:22:24 +0000512#define VIMSUGMAGIC "VIMsug" /* string at start of Vim .sug file */
513#define VIMSUGMAGICL 6
514#define VIMSUGVERSION 1
515
Bram Moolenaar5195e452005-08-19 20:32:47 +0000516/* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
517#define SN_REGION 0 /* <regionname> section */
518#define SN_CHARFLAGS 1 /* charflags section */
519#define SN_MIDWORD 2 /* <midword> section */
520#define SN_PREFCOND 3 /* <prefcond> section */
521#define SN_REP 4 /* REP items section */
522#define SN_SAL 5 /* SAL items section */
523#define SN_SOFO 6 /* soundfolding section */
524#define SN_MAP 7 /* MAP items section */
525#define SN_COMPOUND 8 /* compound words section */
526#define SN_SYLLABLE 9 /* syllable section */
Bram Moolenaar78622822005-08-23 21:00:13 +0000527#define SN_NOBREAK 10 /* NOBREAK section */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000528#define SN_SUGFILE 11 /* timestamp for .sug file */
529#define SN_REPSAL 12 /* REPSAL items section */
530#define SN_WORDS 13 /* common words */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000531#define SN_END 255 /* end of sections */
532
533#define SNF_REQUIRED 1 /* <sectionflags>: required section */
534
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000535/* Result values. Lower number is accepted over higher one. */
536#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000537#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000538#define SP_RARE 1
539#define SP_LOCAL 2
540#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000541
Bram Moolenaar7887d882005-07-01 22:33:52 +0000542/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000543static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000544
Bram Moolenaar4770d092006-01-12 23:22:24 +0000545typedef struct wordcount_S
546{
547 short_u wc_count; /* nr of times word was seen */
548 char_u wc_word[1]; /* word, actually longer */
549} wordcount_T;
550
551static wordcount_T dumwc;
552#define WC_KEY_OFF (dumwc.wc_word - (char_u *)&dumwc)
553#define HI2WC(hi) ((wordcount_T *)((hi)->hi_key - WC_KEY_OFF))
554#define MAXWORDCOUNT 0xffff
555
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000556/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000557 * Information used when looking for suggestions.
558 */
559typedef struct suginfo_S
560{
561 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000562 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000563 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000564 int su_sfmaxscore; /* idem, for when doing soundfold words */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000565 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000566 char_u *su_badptr; /* start of bad word in line */
567 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000568 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000569 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
570 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000571 char_u su_sal_badword[MAXWLEN]; /* su_badword soundfolded */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000572 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000573 slang_T *su_sallang; /* default language for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000574} suginfo_T;
575
576/* One word suggestion. Used in "si_ga". */
577typedef struct suggest_S
578{
579 char_u *st_word; /* suggested word, allocated string */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000580 int st_wordlen; /* STRLEN(st_word) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000581 int st_orglen; /* length of replaced text */
582 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000583 int st_altscore; /* used when st_score compares equal */
584 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000585 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000586 slang_T *st_slang; /* language used for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000587} suggest_T;
588
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000589#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000590
Bram Moolenaar4770d092006-01-12 23:22:24 +0000591/* TRUE if a word appears in the list of banned words. */
592#define WAS_BANNED(su, word) (!HASHITEM_EMPTY(hash_find(&su->su_banned, word)))
593
594/* Number of suggestions kept when cleaning up. we need to keep more than
595 * what is displayed, because when rescore_suggestions() is called the score
596 * may change and wrong suggestions may be removed later. */
597#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 130 ? 150 : (su)->su_maxcount + 20)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000598
599/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
600 * of suggestions that are not going to be displayed. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000601#define SUG_MAX_COUNT(su) (SUG_CLEAN_COUNT(su) + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000602
603/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000604#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000605#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000606#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000607#define SCORE_RARE 180 /* rare word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000608#define SCORE_SWAP 75 /* swap two characters */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000609#define SCORE_SWAP3 110 /* swap two characters in three */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000610#define SCORE_REP 65 /* REP replacement */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000611#define SCORE_SUBST 93 /* substitute a character */
612#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000613#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000614#define SCORE_DEL 94 /* delete a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000615#define SCORE_DELDUP 66 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000616#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000617#define SCORE_INS 96 /* insert a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000618#define SCORE_INSDUP 67 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000619#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000620#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000621
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000622#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000623#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
624 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000625
Bram Moolenaar4770d092006-01-12 23:22:24 +0000626#define SCORE_COMMON1 30 /* subtracted for words seen before */
627#define SCORE_COMMON2 40 /* subtracted for words often seen */
628#define SCORE_COMMON3 50 /* subtracted for words very often seen */
629#define SCORE_THRES2 10 /* word count threshold for COMMON2 */
630#define SCORE_THRES3 100 /* word count threshold for COMMON3 */
631
632/* When trying changed soundfold words it becomes slow when trying more than
633 * two changes. With less then two changes it's slightly faster but we miss a
634 * few good suggestions. In rare cases we need to try three of four changes.
635 */
636#define SCORE_SFMAX1 200 /* maximum score for first try */
637#define SCORE_SFMAX2 300 /* maximum score for second try */
638#define SCORE_SFMAX3 400 /* maximum score for third try */
639
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000640#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000641#define SCORE_MAXMAX 999999 /* accept any score */
642#define SCORE_LIMITMAX 350 /* for spell_edit_score_limit() */
643
644/* for spell_edit_score_limit() we need to know the minimum value of
645 * SCORE_ICASE, SCORE_SWAP, SCORE_DEL, SCORE_SIMILAR and SCORE_INS */
646#define SCORE_EDIT_MIN SCORE_SIMILAR
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000647
648/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000649 * Structure to store info for word matching.
650 */
651typedef struct matchinf_S
652{
653 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000654
655 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000656 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000657 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000658 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000659 char_u *mi_cend; /* char after what was used for
660 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000661
662 /* case-folded text */
663 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000664 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000665
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000666 /* for when checking word after a prefix */
667 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000668 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000669 int mi_prefcnt; /* number of entries at mi_prefarridx */
670 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000671#ifdef FEAT_MBYTE
672 int mi_cprefixlen; /* byte length of prefix in original
673 case */
674#else
675# define mi_cprefixlen mi_prefixlen /* it's the same value */
676#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000677
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000678 /* for when checking a compound word */
679 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000680 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
681 int mi_complen; /* nr of compound words used */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000682
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000683 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000684 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000685 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000686 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000687
688 /* for NOBREAK */
689 int mi_result2; /* "mi_resul" without following word */
690 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000691} matchinf_T;
692
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000693/*
694 * The tables used for recognizing word characters according to spelling.
695 * These are only used for the first 256 characters of 'encoding'.
696 */
697typedef struct spelltab_S
698{
699 char_u st_isw[256]; /* flags: is word char */
700 char_u st_isu[256]; /* flags: is uppercase char */
701 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000702 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000703} spelltab_T;
704
705static spelltab_T spelltab;
706static int did_set_spelltab;
707
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000708#define CF_WORD 0x01
709#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000710
711static void clear_spell_chartab __ARGS((spelltab_T *sp));
712static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000713static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
714static int spell_iswordp_nmw __ARGS((char_u *p));
715#ifdef FEAT_MBYTE
716static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
717#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000718static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000719
720/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000721 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000722 */
723typedef enum
724{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000725 STATE_START = 0, /* At start of node check for NUL bytes (goodword
726 * ends); if badword ends there is a match, otherwise
727 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000728 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000729 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000730 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
731 STATE_PLAIN, /* Use each byte of the node. */
732 STATE_DEL, /* Delete a byte from the bad word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000733 STATE_INS_PREP, /* Prepare for inserting bytes. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000734 STATE_INS, /* Insert a byte in the bad word. */
735 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000736 STATE_UNSWAP, /* Undo swap two characters. */
737 STATE_SWAP3, /* Swap two characters over three. */
738 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000739 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000740 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000741 STATE_REP_INI, /* Prepare for using REP items. */
742 STATE_REP, /* Use matching REP items from the .aff file. */
743 STATE_REP_UNDO, /* Undo a REP item replacement. */
744 STATE_FINAL /* End of this node. */
745} state_T;
746
747/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000748 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000749 */
750typedef struct trystate_S
751{
Bram Moolenaarea424162005-06-16 21:51:00 +0000752 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000753 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000754 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000755 short ts_curi; /* index in list of child nodes */
756 char_u ts_fidx; /* index in fword[], case-folded bad word */
757 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
758 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000759 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000760 * PFD_PREFIXTREE or PFD_NOPREFIX */
761 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000762#ifdef FEAT_MBYTE
763 char_u ts_tcharlen; /* number of bytes in tword character */
764 char_u ts_tcharidx; /* current byte index in tword character */
765 char_u ts_isdiff; /* DIFF_ values */
766 char_u ts_fcharstart; /* index in fword where badword char started */
767#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000768 char_u ts_prewordlen; /* length of word in "preword[]" */
769 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000770 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000771 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000772 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000773 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000774 char_u ts_delidx; /* index in fword for char that was deleted,
775 valid when "ts_flags" has TSF_DIDDEL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000776} trystate_T;
777
Bram Moolenaarea424162005-06-16 21:51:00 +0000778/* values for ts_isdiff */
779#define DIFF_NONE 0 /* no different byte (yet) */
780#define DIFF_YES 1 /* different byte found */
781#define DIFF_INSERT 2 /* inserting character */
782
Bram Moolenaard12a1322005-08-21 22:08:24 +0000783/* values for ts_flags */
784#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
785#define TSF_DIDSPLIT 2 /* tried split at this point */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000786#define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000787
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000788/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000789#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000790#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000791#define PFD_NOTSPECIAL 0xfd /* highest value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000792
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000793/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000794#define FIND_FOLDWORD 0 /* find word case-folded */
795#define FIND_KEEPWORD 1 /* find keep-case word */
796#define FIND_PREFIX 2 /* find word after prefix */
797#define FIND_COMPOUND 3 /* find case-folded compound word */
798#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000799
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000800static slang_T *slang_alloc __ARGS((char_u *lang));
801static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000802static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000803static void slang_clear_sug __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000804static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000805static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000806static 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 +0000807static void find_prefix __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000808static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000809static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaar95529562005-08-25 21:21:38 +0000810static int no_spell_checking __ARGS((win_T *wp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000811static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000812static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000813static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000814static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000815static 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 +0000816static int get2c __ARGS((FILE *fd));
817static int get3c __ARGS((FILE *fd));
818static int get4c __ARGS((FILE *fd));
819static time_t get8c __ARGS((FILE *fd));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000820static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000821static char_u *read_string __ARGS((FILE *fd, int cnt));
822static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
823static int read_charflags_section __ARGS((FILE *fd));
824static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000825static int read_rep_section __ARGS((FILE *fd, garray_T *gap, short *first));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000826static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000827static int read_words_section __ARGS((FILE *fd, slang_T *lp, int len));
828static void count_common_word __ARGS((slang_T *lp, char_u *word, int len, int count));
829static int score_wordcount_adj __ARGS((slang_T *slang, int score, char_u *word, int split));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000830static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
831static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
Bram Moolenaar6de68532005-08-24 22:08:48 +0000832static int byte_in_str __ARGS((char_u *str, int byte));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000833static int init_syl_tab __ARGS((slang_T *slang));
834static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000835static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
836static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000837#ifdef FEAT_MBYTE
838static int *mb_str2wide __ARGS((char_u *s));
839#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000840static int spell_read_tree __ARGS((FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt));
841static 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 +0000842static void clear_midword __ARGS((buf_T *buf));
843static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000844static int find_region __ARGS((char_u *rp, char_u *region));
845static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000846static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000847static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000848static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000849static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000850static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000851static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar66fa2712006-01-22 23:22:22 +0000852static 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 +0000853#ifdef FEAT_EVAL
854static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
855#endif
856static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000857static void spell_suggest_intern __ARGS((suginfo_T *su, int interactive));
858static void suggest_load_files __ARGS((void));
859static void tree_count_words __ARGS((char_u *byts, idx_T *idxs));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000860static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000861static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000862static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000863static void suggest_try_special __ARGS((suginfo_T *su));
864static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000865static void suggest_trie_walk __ARGS((suginfo_T *su, langp_T *lp, char_u *fword, int soundfold));
866static void go_deeper __ARGS((trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000867#ifdef FEAT_MBYTE
868static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
869#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000870static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000871static void score_comp_sal __ARGS((suginfo_T *su));
872static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000873static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000874static void suggest_try_soundalike_prep __ARGS((void));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000875static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000876static void suggest_try_soundalike_finish __ARGS((void));
877static void add_sound_suggest __ARGS((suginfo_T *su, char_u *goodword, int score, langp_T *lp));
878static int soundfold_find __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000879static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000880static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000881static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000882static 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));
883static void check_suggestions __ARGS((suginfo_T *su, garray_T *gap));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000884static void add_banned __ARGS((suginfo_T *su, char_u *word));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000885static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000886static void rescore_one __ARGS((suginfo_T *su, suggest_T *stp));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000887static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000888static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
889static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
890static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000891#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000892static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000893#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000894static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000895static int spell_edit_score __ARGS((slang_T *slang, char_u *badword, char_u *goodword));
896static int spell_edit_score_limit __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
897#ifdef FEAT_MBYTE
898static int spell_edit_score_limit_w __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
899#endif
900static void dump_word __ARGS((slang_T *slang, char_u *word, int round, int flags, linenr_T lnum));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000901static linenr_T dump_prefixes __ARGS((slang_T *slang, char_u *word, int round, int flags, linenr_T startlnum));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000902static buf_T *open_spellbuf __ARGS((void));
903static void close_spellbuf __ARGS((buf_T *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000904
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000905/*
906 * Use our own character-case definitions, because the current locale may
907 * differ from what the .spl file uses.
908 * These must not be called with negative number!
909 */
910#ifndef FEAT_MBYTE
911/* Non-multi-byte implementation. */
912# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
913# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
914# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
915#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000916# if defined(HAVE_WCHAR_H)
917# include <wchar.h> /* for towupper() and towlower() */
918# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000919/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
920 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
921 * the "w" library function for characters above 255 if available. */
922# ifdef HAVE_TOWLOWER
923# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
924 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
925# else
926# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
927 : (c) < 256 ? spelltab.st_fold[c] : (c))
928# endif
929
930# ifdef HAVE_TOWUPPER
931# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
932 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
933# else
934# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
935 : (c) < 256 ? spelltab.st_upper[c] : (c))
936# endif
937
938# ifdef HAVE_ISWUPPER
939# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
940 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
941# else
942# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000943 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000944# endif
945#endif
946
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000947
948static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000949static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000950static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar6de68532005-08-24 22:08:48 +0000951static char *e_affname = N_("Affix name too long in %s line %d: %s");
952static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
953static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000954static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000955
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000956/* Remember what "z?" replaced. */
957static char_u *repl_from = NULL;
958static char_u *repl_to = NULL;
959
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000960/*
961 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000962 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000963 * "*attrp" is set to the highlight index for a badly spelled word. For a
964 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000965 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000966 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000967 * "capcol" is used to check for a Capitalised word after the end of a
968 * sentence. If it's zero then perform the check. Return the column where to
969 * check next, or -1 when no sentence end was found. If it's NULL then don't
970 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000971 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000972 * Returns the length of the word in bytes, also when it's OK, so that the
973 * caller can skip over the word.
974 */
975 int
Bram Moolenaar4770d092006-01-12 23:22:24 +0000976spell_check(wp, ptr, attrp, capcol, docount)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000977 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000978 char_u *ptr;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000979 hlf_T *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000980 int *capcol; /* column to check for Capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000981 int docount; /* count good words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000982{
983 matchinf_T mi; /* Most things are put in "mi" so that it can
984 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000985 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000986 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000987 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000988 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +0000989 int count_word = docount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000990
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000991 /* A word never starts at a space or a control character. Return quickly
992 * then, skipping over the character. */
993 if (*ptr <= ' ')
994 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000995
996 /* Return here when loading language files failed. */
997 if (wp->w_buffer->b_langp.ga_len == 0)
998 return 1;
999
Bram Moolenaar5195e452005-08-19 20:32:47 +00001000 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001001
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001002 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +00001003 * 0X99FF. But always do check spelling to find "3GPP" and "11
1004 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001005 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +00001006 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00001007 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
1008 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001009 else
1010 mi.mi_end = skipdigits(ptr);
Bram Moolenaar43abc522005-12-10 20:15:02 +00001011 nrlen = mi.mi_end - ptr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001012 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001013
Bram Moolenaar0c405862005-06-22 22:26:26 +00001014 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001015 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +00001016 mi.mi_fend = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001017 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001018 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001019 do
Bram Moolenaar51485f02005-06-04 21:55:20 +00001020 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001021 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001022 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001023
1024 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
1025 {
1026 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00001027 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001028 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001029 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001030 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001031 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001032 if (capcol != NULL)
1033 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001034
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001035 /* We always use the characters up to the next non-word character,
1036 * also for bad words. */
1037 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001038
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001039 /* Check caps type later. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001040 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001041
Bram Moolenaar5195e452005-08-19 20:32:47 +00001042 /* case-fold the word with one non-word character, so that we can check
1043 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001044 if (*mi.mi_fend != NUL)
1045 mb_ptr_adv(mi.mi_fend);
1046
1047 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
1048 MAXWLEN + 1);
1049 mi.mi_fwordlen = STRLEN(mi.mi_fword);
1050
1051 /* The word is bad unless we recognize it. */
1052 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +00001053 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001054
1055 /*
1056 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001057 * We check them all, because a word may be matched longer in another
1058 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001059 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001060 for (lpi = 0; lpi < wp->w_buffer->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001061 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001062 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, lpi);
1063
1064 /* If reloading fails the language is still in the list but everything
1065 * has been cleared. */
1066 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
1067 continue;
1068
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001069 /* Check for a matching word in case-folded words. */
1070 find_word(&mi, FIND_FOLDWORD);
1071
1072 /* Check for a matching word in keep-case words. */
1073 find_word(&mi, FIND_KEEPWORD);
1074
1075 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001076 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +00001077
1078 /* For a NOBREAK language, may want to use a word without a following
1079 * word as a backup. */
1080 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
1081 && mi.mi_result2 != SP_BAD)
1082 {
1083 mi.mi_result = mi.mi_result2;
1084 mi.mi_end = mi.mi_end2;
1085 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001086
1087 /* Count the word in the first language where it's found to be OK. */
1088 if (count_word && mi.mi_result == SP_OK)
1089 {
1090 count_common_word(mi.mi_lp->lp_slang, ptr,
1091 (int)(mi.mi_end - ptr), 1);
1092 count_word = FALSE;
1093 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001094 }
1095
1096 if (mi.mi_result != SP_OK)
1097 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001098 /* If we found a number skip over it. Allows for "42nd". Do flag
1099 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001100 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001101 {
1102 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
1103 return nrlen;
1104 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001105
1106 /* When we are at a non-word character there is no error, just
1107 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001108 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001109 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001110 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
1111 {
1112 regmatch_T regmatch;
1113
1114 /* Check for end of sentence. */
1115 regmatch.regprog = wp->w_buffer->b_cap_prog;
1116 regmatch.rm_ic = FALSE;
1117 if (vim_regexec(&regmatch, ptr, 0))
1118 *capcol = (int)(regmatch.endp[0] - ptr);
1119 }
1120
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001121#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001122 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001123 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001124#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001125 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001126 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001127 else if (mi.mi_end == ptr)
1128 /* Always include at least one character. Required for when there
1129 * is a mixup in "midword". */
1130 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +00001131 else if (mi.mi_result == SP_BAD
1132 && LANGP_ENTRY(wp->w_buffer->b_langp, 0)->lp_slang->sl_nobreak)
1133 {
1134 char_u *p, *fp;
1135 int save_result = mi.mi_result;
1136
1137 /* First language in 'spelllang' is NOBREAK. Find first position
1138 * at which any word would be valid. */
1139 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001140 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +00001141 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001142 p = mi.mi_word;
1143 fp = mi.mi_fword;
1144 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +00001145 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001146 mb_ptr_adv(p);
1147 mb_ptr_adv(fp);
1148 if (p >= mi.mi_end)
1149 break;
1150 mi.mi_compoff = fp - mi.mi_fword;
1151 find_word(&mi, FIND_COMPOUND);
1152 if (mi.mi_result != SP_BAD)
1153 {
1154 mi.mi_end = p;
1155 break;
1156 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001157 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001158 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +00001159 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001160 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001161
1162 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001163 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001164 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001165 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001166 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001167 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001168 }
1169
Bram Moolenaar5195e452005-08-19 20:32:47 +00001170 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1171 {
1172 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001173 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001174 return wrongcaplen;
1175 }
1176
Bram Moolenaar51485f02005-06-04 21:55:20 +00001177 return (int)(mi.mi_end - ptr);
1178}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001179
Bram Moolenaar51485f02005-06-04 21:55:20 +00001180/*
1181 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001182 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1183 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1184 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1185 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001186 *
1187 * For a match mip->mi_result is updated.
1188 */
1189 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001190find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001191 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001192 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001193{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001194 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001195 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001196 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001197 int endidxcnt = 0;
1198 int len;
1199 int wlen = 0;
1200 int flen;
1201 int c;
1202 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001203 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001204#ifdef FEAT_MBYTE
1205 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001206#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001207 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001208 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001209 slang_T *slang = mip->mi_lp->lp_slang;
1210 unsigned flags;
1211 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001212 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001213 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001214 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001215 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001216
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001217 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001218 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001219 /* Check for word with matching case in keep-case tree. */
1220 ptr = mip->mi_word;
1221 flen = 9999; /* no case folding, always enough bytes */
1222 byts = slang->sl_kbyts;
1223 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001224
1225 if (mode == FIND_KEEPCOMPOUND)
1226 /* Skip over the previously found word(s). */
1227 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001228 }
1229 else
1230 {
1231 /* Check for case-folded in case-folded tree. */
1232 ptr = mip->mi_fword;
1233 flen = mip->mi_fwordlen; /* available case-folded bytes */
1234 byts = slang->sl_fbyts;
1235 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001236
1237 if (mode == FIND_PREFIX)
1238 {
1239 /* Skip over the prefix. */
1240 wlen = mip->mi_prefixlen;
1241 flen -= mip->mi_prefixlen;
1242 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001243 else if (mode == FIND_COMPOUND)
1244 {
1245 /* Skip over the previously found word(s). */
1246 wlen = mip->mi_compoff;
1247 flen -= mip->mi_compoff;
1248 }
1249
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001250 }
1251
Bram Moolenaar51485f02005-06-04 21:55:20 +00001252 if (byts == NULL)
1253 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001254
Bram Moolenaar51485f02005-06-04 21:55:20 +00001255 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001256 * Repeat advancing in the tree until:
1257 * - there is a byte that doesn't match,
1258 * - we reach the end of the tree,
1259 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001260 */
1261 for (;;)
1262 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001263 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001264 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001265
1266 len = byts[arridx++];
1267
1268 /* If the first possible byte is a zero the word could end here.
1269 * Remember this index, we first check for the longest word. */
1270 if (byts[arridx] == 0)
1271 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001272 if (endidxcnt == MAXWLEN)
1273 {
1274 /* Must be a corrupted spell file. */
1275 EMSG(_(e_format));
1276 return;
1277 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001278 endlen[endidxcnt] = wlen;
1279 endidx[endidxcnt++] = arridx++;
1280 --len;
1281
1282 /* Skip over the zeros, there can be several flag/region
1283 * combinations. */
1284 while (len > 0 && byts[arridx] == 0)
1285 {
1286 ++arridx;
1287 --len;
1288 }
1289 if (len == 0)
1290 break; /* no children, word must end here */
1291 }
1292
1293 /* Stop looking at end of the line. */
1294 if (ptr[wlen] == NUL)
1295 break;
1296
1297 /* Perform a binary search in the list of accepted bytes. */
1298 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001299 if (c == TAB) /* <Tab> is handled like <Space> */
1300 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001301 lo = arridx;
1302 hi = arridx + len - 1;
1303 while (lo < hi)
1304 {
1305 m = (lo + hi) / 2;
1306 if (byts[m] > c)
1307 hi = m - 1;
1308 else if (byts[m] < c)
1309 lo = m + 1;
1310 else
1311 {
1312 lo = hi = m;
1313 break;
1314 }
1315 }
1316
1317 /* Stop if there is no matching byte. */
1318 if (hi < lo || byts[lo] != c)
1319 break;
1320
1321 /* Continue at the child (if there is one). */
1322 arridx = idxs[lo];
1323 ++wlen;
1324 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001325
1326 /* One space in the good word may stand for several spaces in the
1327 * checked word. */
1328 if (c == ' ')
1329 {
1330 for (;;)
1331 {
1332 if (flen <= 0 && *mip->mi_fend != NUL)
1333 flen = fold_more(mip);
1334 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1335 break;
1336 ++wlen;
1337 --flen;
1338 }
1339 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001340 }
1341
1342 /*
1343 * Verify that one of the possible endings is valid. Try the longest
1344 * first.
1345 */
1346 while (endidxcnt > 0)
1347 {
1348 --endidxcnt;
1349 arridx = endidx[endidxcnt];
1350 wlen = endlen[endidxcnt];
1351
1352#ifdef FEAT_MBYTE
1353 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1354 continue; /* not at first byte of character */
1355#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001356 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001357 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001358 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001359 continue; /* next char is a word character */
1360 word_ends = FALSE;
1361 }
1362 else
1363 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001364 /* The prefix flag is before compound flags. Once a valid prefix flag
1365 * has been found we try compound flags. */
1366 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001367
1368#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001369 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001370 {
1371 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001372 * when folding case. This can be slow, take a shortcut when the
1373 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001374 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001375 if (STRNCMP(ptr, p, wlen) != 0)
1376 {
1377 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1378 mb_ptr_adv(p);
1379 wlen = p - mip->mi_word;
1380 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001381 }
1382#endif
1383
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001384 /* Check flags and region. For FIND_PREFIX check the condition and
1385 * prefix ID.
1386 * Repeat this if there are more flags/region alternatives until there
1387 * is a match. */
1388 res = SP_BAD;
1389 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1390 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001391 {
1392 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001393
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001394 /* For the fold-case tree check that the case of the checked word
1395 * matches with what the word in the tree requires.
1396 * For keep-case tree the case is always right. For prefixes we
1397 * don't bother to check. */
1398 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001399 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001400 if (mip->mi_cend != mip->mi_word + wlen)
1401 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001402 /* mi_capflags was set for a different word length, need
1403 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001404 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001405 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001406 }
1407
Bram Moolenaar0c405862005-06-22 22:26:26 +00001408 if (mip->mi_capflags == WF_KEEPCAP
1409 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001410 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001411 }
1412
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001413 /* When mode is FIND_PREFIX the word must support the prefix:
1414 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001415 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001416 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001417 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001418 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001419 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001420 mip->mi_word + mip->mi_cprefixlen, slang,
1421 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001422 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001423 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001424
1425 /* Use the WF_RARE flag for a rare prefix. */
1426 if (c & WF_RAREPFX)
1427 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001428 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001429 }
1430
Bram Moolenaar78622822005-08-23 21:00:13 +00001431 if (slang->sl_nobreak)
1432 {
1433 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1434 && (flags & WF_BANNED) == 0)
1435 {
1436 /* NOBREAK: found a valid following word. That's all we
1437 * need to know, so return. */
1438 mip->mi_result = SP_OK;
1439 break;
1440 }
1441 }
1442
1443 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1444 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001445 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00001446 /* If there is no flag or the word is shorter than
1447 * COMPOUNDMIN reject it quickly.
1448 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001449 * that's too short... Myspell compatibility requires this
1450 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001451 if (((unsigned)flags >> 24) == 0
1452 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001453 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001454#ifdef FEAT_MBYTE
1455 /* For multi-byte chars check character length against
1456 * COMPOUNDMIN. */
1457 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001458 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001459 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
1460 wlen - mip->mi_compoff) < slang->sl_compminlen)
1461 continue;
1462#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001463
Bram Moolenaare52325c2005-08-22 22:54:29 +00001464 /* Limit the number of compound words to COMPOUNDMAX if no
1465 * maximum for syllables is specified. */
1466 if (!word_ends && mip->mi_complen + 2 > slang->sl_compmax
1467 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001468 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001469
Bram Moolenaard12a1322005-08-21 22:08:24 +00001470 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001471 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001472 ? slang->sl_compstartflags
1473 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001474 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001475 continue;
1476
Bram Moolenaare52325c2005-08-22 22:54:29 +00001477 if (mode == FIND_COMPOUND)
1478 {
1479 int capflags;
1480
1481 /* Need to check the caps type of the appended compound
1482 * word. */
1483#ifdef FEAT_MBYTE
1484 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1485 mip->mi_compoff) != 0)
1486 {
1487 /* case folding may have changed the length */
1488 p = mip->mi_word;
1489 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1490 mb_ptr_adv(p);
1491 }
1492 else
1493#endif
1494 p = mip->mi_word + mip->mi_compoff;
1495 capflags = captype(p, mip->mi_word + wlen);
1496 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1497 && (flags & WF_FIXCAP) != 0))
1498 continue;
1499
1500 if (capflags != WF_ALLCAP)
1501 {
1502 /* When the character before the word is a word
1503 * character we do not accept a Onecap word. We do
1504 * accept a no-caps word, even when the dictionary
1505 * word specifies ONECAP. */
1506 mb_ptr_back(mip->mi_word, p);
1507 if (spell_iswordp_nmw(p)
1508 ? capflags == WF_ONECAP
1509 : (flags & WF_ONECAP) != 0
1510 && capflags != WF_ONECAP)
1511 continue;
1512 }
1513 }
1514
Bram Moolenaar5195e452005-08-19 20:32:47 +00001515 /* If the word ends the sequence of compound flags of the
1516 * words must match with one of the COMPOUNDFLAGS items and
1517 * the number of syllables must not be too large. */
1518 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1519 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1520 if (word_ends)
1521 {
1522 char_u fword[MAXWLEN];
1523
1524 if (slang->sl_compsylmax < MAXWLEN)
1525 {
1526 /* "fword" is only needed for checking syllables. */
1527 if (ptr == mip->mi_word)
1528 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1529 else
1530 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1531 }
1532 if (!can_compound(slang, fword, mip->mi_compflags))
1533 continue;
1534 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001535 }
1536
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001537 /* Check NEEDCOMPOUND: can't use word without compounding. */
1538 else if (flags & WF_NEEDCOMP)
1539 continue;
1540
Bram Moolenaar78622822005-08-23 21:00:13 +00001541 nobreak_result = SP_OK;
1542
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001543 if (!word_ends)
1544 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001545 int save_result = mip->mi_result;
1546 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001547 langp_T *save_lp = mip->mi_lp;
1548 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +00001549
1550 /* Check that a valid word follows. If there is one and we
1551 * are compounding, it will set "mi_result", thus we are
1552 * always finished here. For NOBREAK we only check that a
1553 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001554 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001555 if (slang->sl_nobreak)
1556 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001557
1558 /* Find following word in case-folded tree. */
1559 mip->mi_compoff = endlen[endidxcnt];
1560#ifdef FEAT_MBYTE
1561 if (has_mbyte && mode == FIND_KEEPWORD)
1562 {
1563 /* Compute byte length in case-folded word from "wlen":
1564 * byte length in keep-case word. Length may change when
1565 * folding case. This can be slow, take a shortcut when
1566 * the case-folded word is equal to the keep-case word. */
1567 p = mip->mi_fword;
1568 if (STRNCMP(ptr, p, wlen) != 0)
1569 {
1570 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1571 mb_ptr_adv(p);
1572 mip->mi_compoff = p - mip->mi_fword;
1573 }
1574 }
1575#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001576 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001577 ++mip->mi_complen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001578
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001579 /* For NOBREAK we need to try all NOBREAK languages, at least
1580 * to find the ".add" file(s). */
1581 for (lpi = 0; lpi < mip->mi_buf->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001582 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001583 if (slang->sl_nobreak)
1584 {
1585 mip->mi_lp = LANGP_ENTRY(mip->mi_buf->b_langp, lpi);
1586 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1587 || !mip->mi_lp->lp_slang->sl_nobreak)
1588 continue;
1589 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001590
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001591 find_word(mip, FIND_COMPOUND);
1592
1593 /* When NOBREAK any word that matches is OK. Otherwise we
1594 * need to find the longest match, thus try with keep-case
1595 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001596 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1597 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001598 /* Find following word in keep-case tree. */
1599 mip->mi_compoff = wlen;
1600 find_word(mip, FIND_KEEPCOMPOUND);
1601
1602 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1603 {
1604 /* Check for following word with prefix. */
1605 mip->mi_compoff = c;
1606 find_prefix(mip, FIND_COMPOUND);
1607 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001608 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001609
1610 if (!slang->sl_nobreak)
1611 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001612 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001613 --mip->mi_complen;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001614 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001615
Bram Moolenaar78622822005-08-23 21:00:13 +00001616 if (slang->sl_nobreak)
1617 {
1618 nobreak_result = mip->mi_result;
1619 mip->mi_result = save_result;
1620 mip->mi_end = save_end;
1621 }
1622 else
1623 {
1624 if (mip->mi_result == SP_OK)
1625 break;
1626 continue;
1627 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001628 }
1629
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001630 if (flags & WF_BANNED)
1631 res = SP_BANNED;
1632 else if (flags & WF_REGION)
1633 {
1634 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001635 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001636 res = SP_OK;
1637 else
1638 res = SP_LOCAL;
1639 }
1640 else if (flags & WF_RARE)
1641 res = SP_RARE;
1642 else
1643 res = SP_OK;
1644
Bram Moolenaar78622822005-08-23 21:00:13 +00001645 /* Always use the longest match and the best result. For NOBREAK
1646 * we separately keep the longest match without a following good
1647 * word as a fall-back. */
1648 if (nobreak_result == SP_BAD)
1649 {
1650 if (mip->mi_result2 > res)
1651 {
1652 mip->mi_result2 = res;
1653 mip->mi_end2 = mip->mi_word + wlen;
1654 }
1655 else if (mip->mi_result2 == res
1656 && mip->mi_end2 < mip->mi_word + wlen)
1657 mip->mi_end2 = mip->mi_word + wlen;
1658 }
1659 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001660 {
1661 mip->mi_result = res;
1662 mip->mi_end = mip->mi_word + wlen;
1663 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001664 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001665 mip->mi_end = mip->mi_word + wlen;
1666
Bram Moolenaar78622822005-08-23 21:00:13 +00001667 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001668 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001669 }
1670
Bram Moolenaar78622822005-08-23 21:00:13 +00001671 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001672 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001673 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001674}
1675
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001676/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001677 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1678 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001679 */
1680 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001681can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001682 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001683 char_u *word;
1684 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001685{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001686 regmatch_T regmatch;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001687#ifdef FEAT_MBYTE
1688 char_u uflags[MAXWLEN * 2];
1689 int i;
1690#endif
1691 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001692
1693 if (slang->sl_compprog == NULL)
1694 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001695#ifdef FEAT_MBYTE
1696 if (enc_utf8)
1697 {
1698 /* Need to convert the single byte flags to utf8 characters. */
1699 p = uflags;
1700 for (i = 0; flags[i] != NUL; ++i)
1701 p += mb_char2bytes(flags[i], p);
1702 *p = NUL;
1703 p = uflags;
1704 }
1705 else
1706#endif
1707 p = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001708 regmatch.regprog = slang->sl_compprog;
1709 regmatch.rm_ic = FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001710 if (!vim_regexec(&regmatch, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001711 return FALSE;
1712
Bram Moolenaare52325c2005-08-22 22:54:29 +00001713 /* Count the number of syllables. This may be slow, do it last. If there
1714 * are too many syllables AND the number of compound words is above
1715 * COMPOUNDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001716 if (slang->sl_compsylmax < MAXWLEN
1717 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001718 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001719 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001720}
1721
1722/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001723 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1724 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001725 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001726 */
1727 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001728valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001729 int totprefcnt; /* nr of prefix IDs */
1730 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001731 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001732 char_u *word;
1733 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001734 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001735{
1736 int prefcnt;
1737 int pidx;
1738 regprog_T *rp;
1739 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001740 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001741
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001742 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001743 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1744 {
1745 pidx = slang->sl_pidxs[arridx + prefcnt];
1746
1747 /* Check the prefix ID. */
1748 if (prefid != (pidx & 0xff))
1749 continue;
1750
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001751 /* Check if the prefix doesn't combine and the word already has a
1752 * suffix. */
1753 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1754 continue;
1755
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001756 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001757 * stored in the two bytes above the prefix ID byte. */
1758 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001759 if (rp != NULL)
1760 {
1761 regmatch.regprog = rp;
1762 regmatch.rm_ic = FALSE;
1763 if (!vim_regexec(&regmatch, word, 0))
1764 continue;
1765 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001766 else if (cond_req)
1767 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001768
Bram Moolenaar53805d12005-08-01 07:08:33 +00001769 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001770 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001771 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001772 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001773}
1774
1775/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001776 * Check if the word at "mip->mi_word" has a matching prefix.
1777 * If it does, then check the following word.
1778 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001779 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1780 * prefix in a compound word.
1781 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001782 * For a match mip->mi_result is updated.
1783 */
1784 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001785find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001786 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001787 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001788{
1789 idx_T arridx = 0;
1790 int len;
1791 int wlen = 0;
1792 int flen;
1793 int c;
1794 char_u *ptr;
1795 idx_T lo, hi, m;
1796 slang_T *slang = mip->mi_lp->lp_slang;
1797 char_u *byts;
1798 idx_T *idxs;
1799
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001800 byts = slang->sl_pbyts;
1801 if (byts == NULL)
1802 return; /* array is empty */
1803
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001804 /* We use the case-folded word here, since prefixes are always
1805 * case-folded. */
1806 ptr = mip->mi_fword;
1807 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001808 if (mode == FIND_COMPOUND)
1809 {
1810 /* Skip over the previously found word(s). */
1811 ptr += mip->mi_compoff;
1812 flen -= mip->mi_compoff;
1813 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001814 idxs = slang->sl_pidxs;
1815
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001816 /*
1817 * Repeat advancing in the tree until:
1818 * - there is a byte that doesn't match,
1819 * - we reach the end of the tree,
1820 * - or we reach the end of the line.
1821 */
1822 for (;;)
1823 {
1824 if (flen == 0 && *mip->mi_fend != NUL)
1825 flen = fold_more(mip);
1826
1827 len = byts[arridx++];
1828
1829 /* If the first possible byte is a zero the prefix could end here.
1830 * Check if the following word matches and supports the prefix. */
1831 if (byts[arridx] == 0)
1832 {
1833 /* There can be several prefixes with different conditions. We
1834 * try them all, since we don't know which one will give the
1835 * longest match. The word is the same each time, pass the list
1836 * of possible prefixes to find_word(). */
1837 mip->mi_prefarridx = arridx;
1838 mip->mi_prefcnt = len;
1839 while (len > 0 && byts[arridx] == 0)
1840 {
1841 ++arridx;
1842 --len;
1843 }
1844 mip->mi_prefcnt -= len;
1845
1846 /* Find the word that comes after the prefix. */
1847 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001848 if (mode == FIND_COMPOUND)
1849 /* Skip over the previously found word(s). */
1850 mip->mi_prefixlen += mip->mi_compoff;
1851
Bram Moolenaar53805d12005-08-01 07:08:33 +00001852#ifdef FEAT_MBYTE
1853 if (has_mbyte)
1854 {
1855 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001856 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1857 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001858 }
1859 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001860 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001861#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001862 find_word(mip, FIND_PREFIX);
1863
1864
1865 if (len == 0)
1866 break; /* no children, word must end here */
1867 }
1868
1869 /* Stop looking at end of the line. */
1870 if (ptr[wlen] == NUL)
1871 break;
1872
1873 /* Perform a binary search in the list of accepted bytes. */
1874 c = ptr[wlen];
1875 lo = arridx;
1876 hi = arridx + len - 1;
1877 while (lo < hi)
1878 {
1879 m = (lo + hi) / 2;
1880 if (byts[m] > c)
1881 hi = m - 1;
1882 else if (byts[m] < c)
1883 lo = m + 1;
1884 else
1885 {
1886 lo = hi = m;
1887 break;
1888 }
1889 }
1890
1891 /* Stop if there is no matching byte. */
1892 if (hi < lo || byts[lo] != c)
1893 break;
1894
1895 /* Continue at the child (if there is one). */
1896 arridx = idxs[lo];
1897 ++wlen;
1898 --flen;
1899 }
1900}
1901
1902/*
1903 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001904 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001905 * Return the length of the folded chars in bytes.
1906 */
1907 static int
1908fold_more(mip)
1909 matchinf_T *mip;
1910{
1911 int flen;
1912 char_u *p;
1913
1914 p = mip->mi_fend;
1915 do
1916 {
1917 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001918 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001919
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001920 /* Include the non-word character so that we can check for the word end. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001921 if (*mip->mi_fend != NUL)
1922 mb_ptr_adv(mip->mi_fend);
1923
1924 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1925 mip->mi_fword + mip->mi_fwordlen,
1926 MAXWLEN - mip->mi_fwordlen);
1927 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1928 mip->mi_fwordlen += flen;
1929 return flen;
1930}
1931
1932/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001933 * Check case flags for a word. Return TRUE if the word has the requested
1934 * case.
1935 */
1936 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001937spell_valid_case(wordflags, treeflags)
1938 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001939 int treeflags; /* flags for the word in the spell tree */
1940{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001941 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001942 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001943 && ((treeflags & WF_ONECAP) == 0
1944 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001945}
1946
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001947/*
1948 * Return TRUE if spell checking is not enabled.
1949 */
1950 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00001951no_spell_checking(wp)
1952 win_T *wp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001953{
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001954 if (!wp->w_p_spell || *wp->w_buffer->b_p_spl == NUL
1955 || wp->w_buffer->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001956 {
1957 EMSG(_("E756: Spell checking is not enabled"));
1958 return TRUE;
1959 }
1960 return FALSE;
1961}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001962
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001963/*
1964 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001965 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
1966 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001967 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1968 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00001969 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001970 */
1971 int
Bram Moolenaar95529562005-08-25 21:21:38 +00001972spell_move_to(wp, dir, allwords, curline, attrp)
1973 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001974 int dir; /* FORWARD or BACKWARD */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001975 int allwords; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001976 int curline;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001977 hlf_T *attrp; /* return: attributes of bad word or NULL
1978 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001979{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001980 linenr_T lnum;
1981 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001982 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001983 char_u *line;
1984 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001985 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001986 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001987 int len;
Bram Moolenaar95529562005-08-25 21:21:38 +00001988 int has_syntax = syntax_present(wp->w_buffer);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001989 int col;
1990 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001991 char_u *buf = NULL;
1992 int buflen = 0;
1993 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001994 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001995 int found_one = FALSE;
1996 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001997
Bram Moolenaar95529562005-08-25 21:21:38 +00001998 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00001999 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002000
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002001 /*
2002 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00002003 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002004 *
2005 * When searching backwards, we continue in the line to find the last
2006 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00002007 *
2008 * We concatenate the start of the next line, so that wrapped words work
2009 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
2010 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002011 */
Bram Moolenaar95529562005-08-25 21:21:38 +00002012 lnum = wp->w_cursor.lnum;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002013 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002014
2015 while (!got_int)
2016 {
Bram Moolenaar95529562005-08-25 21:21:38 +00002017 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002018
Bram Moolenaar0c405862005-06-22 22:26:26 +00002019 len = STRLEN(line);
2020 if (buflen < len + MAXWLEN + 2)
2021 {
2022 vim_free(buf);
2023 buflen = len + MAXWLEN + 2;
2024 buf = alloc(buflen);
2025 if (buf == NULL)
2026 break;
2027 }
2028
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002029 /* In first line check first word for Capital. */
2030 if (lnum == 1)
2031 capcol = 0;
2032
2033 /* For checking first word with a capital skip white space. */
2034 if (capcol == 0)
2035 capcol = skipwhite(line) - line;
2036
Bram Moolenaar0c405862005-06-22 22:26:26 +00002037 /* Copy the line into "buf" and append the start of the next line if
2038 * possible. */
2039 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00002040 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002041 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
2042
2043 p = buf + skip;
2044 endp = buf + len;
2045 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002046 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002047 /* When searching backward don't search after the cursor. Unless
2048 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002049 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00002050 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002051 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00002052 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002053 break;
2054
2055 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002056 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002057 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002058
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002059 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002060 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002061 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002062 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002063 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002064 found_one = TRUE;
2065
Bram Moolenaar51485f02005-06-04 21:55:20 +00002066 /* When searching forward only accept a bad word after
2067 * the cursor. */
2068 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002069 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00002070 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002071 && (wrapped
2072 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00002073 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002074 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002075 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002076 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002077 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00002078 col = p - buf;
Bram Moolenaar95529562005-08-25 21:21:38 +00002079 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar51485f02005-06-04 21:55:20 +00002080 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002081 }
2082 else
2083 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002084
Bram Moolenaar51485f02005-06-04 21:55:20 +00002085 if (can_spell)
2086 {
2087 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002088 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002089#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00002090 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002091#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002092 if (dir == FORWARD)
2093 {
2094 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00002095 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002096 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002097 if (attrp != NULL)
2098 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002099 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002100 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002101 else if (curline)
2102 /* Insert mode completion: put cursor after
2103 * the bad word. */
2104 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002105 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002106 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002107 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002108 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002109 }
2110
Bram Moolenaar51485f02005-06-04 21:55:20 +00002111 /* advance to character after the word */
2112 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002113 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002114 }
2115
Bram Moolenaar5195e452005-08-19 20:32:47 +00002116 if (dir == BACKWARD && found_pos.lnum != 0)
2117 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002118 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00002119 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002120 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002121 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002122 }
2123
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002124 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002125 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002126
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002127 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002128 if (dir == BACKWARD)
2129 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002130 /* If we are back at the starting line and searched it again there
2131 * is no match, give up. */
2132 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002133 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002134
2135 if (lnum > 1)
2136 --lnum;
2137 else if (!p_ws)
2138 break; /* at first line and 'nowrapscan' */
2139 else
2140 {
2141 /* Wrap around to the end of the buffer. May search the
2142 * starting line again and accept the last match. */
2143 lnum = wp->w_buffer->b_ml.ml_line_count;
2144 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002145 if (!shortmess(SHM_SEARCH))
2146 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002147 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002148 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002149 }
2150 else
2151 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002152 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2153 ++lnum;
2154 else if (!p_ws)
2155 break; /* at first line and 'nowrapscan' */
2156 else
2157 {
2158 /* Wrap around to the start of the buffer. May search the
2159 * starting line again and accept the first match. */
2160 lnum = 1;
2161 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002162 if (!shortmess(SHM_SEARCH))
2163 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002164 }
2165
2166 /* If we are back at the starting line and there is no match then
2167 * give up. */
2168 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002169 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002170
2171 /* Skip the characters at the start of the next line that were
2172 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002173 if (attr == HLF_COUNT)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002174 skip = p - endp;
2175 else
2176 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002177
2178 /* Capscol skips over the inserted space. */
2179 --capcol;
2180
2181 /* But after empty line check first word in next line */
2182 if (*skipwhite(line) == NUL)
2183 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002184 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002185
2186 line_breakcheck();
2187 }
2188
Bram Moolenaar0c405862005-06-22 22:26:26 +00002189 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002190 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002191}
2192
2193/*
2194 * For spell checking: concatenate the start of the following line "line" into
2195 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
2196 */
2197 void
2198spell_cat_line(buf, line, maxlen)
2199 char_u *buf;
2200 char_u *line;
2201 int maxlen;
2202{
2203 char_u *p;
2204 int n;
2205
2206 p = skipwhite(line);
2207 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2208 p = skipwhite(p + 1);
2209
2210 if (*p != NUL)
2211 {
2212 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002213 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002214 n = p - line;
2215 if (n >= maxlen)
2216 n = maxlen - 1;
2217 vim_memset(buf + 1, ' ', n);
2218 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002219}
2220
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002221/*
2222 * Structure used for the cookie argument of do_in_runtimepath().
2223 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002224typedef struct spelload_S
2225{
2226 char_u sl_lang[MAXWLEN + 1]; /* language name */
2227 slang_T *sl_slang; /* resulting slang_T struct */
2228 int sl_nobreak; /* NOBREAK language found */
2229} spelload_T;
2230
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002231/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002232 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002233 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002234 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002235 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002236spell_load_lang(lang)
2237 char_u *lang;
2238{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002239 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002240 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002241 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002242#ifdef FEAT_AUTOCMD
2243 int round;
2244#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002245
Bram Moolenaarb765d632005-06-07 21:00:02 +00002246 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002247 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002248 STRCPY(sl.sl_lang, lang);
2249 sl.sl_slang = NULL;
2250 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002251
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002252#ifdef FEAT_AUTOCMD
2253 /* We may retry when no spell file is found for the language, an
2254 * autocommand may load it then. */
2255 for (round = 1; round <= 2; ++round)
2256#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002257 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002258 /*
2259 * Find the first spell file for "lang" in 'runtimepath' and load it.
2260 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002261 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002262 "spell/%s.%s.spl", lang, spell_enc());
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002263 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002264
2265 if (r == FAIL && *sl.sl_lang != NUL)
2266 {
2267 /* Try loading the ASCII version. */
2268 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
2269 "spell/%s.ascii.spl", lang);
2270 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
2271
2272#ifdef FEAT_AUTOCMD
2273 if (r == FAIL && *sl.sl_lang != NUL && round == 1
2274 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
2275 curbuf->b_fname, FALSE, curbuf))
2276 continue;
2277 break;
2278#endif
2279 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002280 }
2281
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002282 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002283 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002284 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2285 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002286 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002287 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002288 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002289 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002290 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002291 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002292 }
2293}
2294
2295/*
2296 * Return the encoding used for spell checking: Use 'encoding', except that we
2297 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2298 */
2299 static char_u *
2300spell_enc()
2301{
2302
2303#ifdef FEAT_MBYTE
2304 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2305 return p_enc;
2306#endif
2307 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002308}
2309
2310/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002311 * Get the name of the .spl file for the internal wordlist into
2312 * "fname[MAXPATHL]".
2313 */
2314 static void
2315int_wordlist_spl(fname)
2316 char_u *fname;
2317{
2318 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
2319 int_wordlist, spell_enc());
2320}
2321
2322/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002323 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002324 * Caller must fill "sl_next".
2325 */
2326 static slang_T *
2327slang_alloc(lang)
2328 char_u *lang;
2329{
2330 slang_T *lp;
2331
Bram Moolenaar51485f02005-06-04 21:55:20 +00002332 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002333 if (lp != NULL)
2334 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002335 if (lang != NULL)
2336 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002337 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002338 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002339 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002340 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002341 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002342 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002343
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002344 return lp;
2345}
2346
2347/*
2348 * Free the contents of an slang_T and the structure itself.
2349 */
2350 static void
2351slang_free(lp)
2352 slang_T *lp;
2353{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002354 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002355 vim_free(lp->sl_fname);
2356 slang_clear(lp);
2357 vim_free(lp);
2358}
2359
2360/*
2361 * Clear an slang_T so that the file can be reloaded.
2362 */
2363 static void
2364slang_clear(lp)
2365 slang_T *lp;
2366{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002367 garray_T *gap;
2368 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002369 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002370 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002371 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002372
Bram Moolenaar51485f02005-06-04 21:55:20 +00002373 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002374 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002375 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002376 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002377 vim_free(lp->sl_pbyts);
2378 lp->sl_pbyts = NULL;
2379
Bram Moolenaar51485f02005-06-04 21:55:20 +00002380 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002381 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002382 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002383 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002384 vim_free(lp->sl_pidxs);
2385 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002386
Bram Moolenaar4770d092006-01-12 23:22:24 +00002387 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002388 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002389 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
2390 while (gap->ga_len > 0)
2391 {
2392 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2393 vim_free(ftp->ft_from);
2394 vim_free(ftp->ft_to);
2395 }
2396 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002397 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002398
2399 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002400 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002401 {
2402 /* "ga_len" is set to 1 without adding an item for latin1 */
2403 if (gap->ga_data != NULL)
2404 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2405 for (i = 0; i < gap->ga_len; ++i)
2406 vim_free(((int **)gap->ga_data)[i]);
2407 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002408 else
2409 /* SAL items: free salitem_T items */
2410 while (gap->ga_len > 0)
2411 {
2412 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2413 vim_free(smp->sm_lead);
2414 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2415 vim_free(smp->sm_to);
2416#ifdef FEAT_MBYTE
2417 vim_free(smp->sm_lead_w);
2418 vim_free(smp->sm_oneof_w);
2419 vim_free(smp->sm_to_w);
2420#endif
2421 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002422 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002423
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002424 for (i = 0; i < lp->sl_prefixcnt; ++i)
2425 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002426 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002427 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002428 lp->sl_prefprog = NULL;
2429
2430 vim_free(lp->sl_midword);
2431 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002432
Bram Moolenaar5195e452005-08-19 20:32:47 +00002433 vim_free(lp->sl_compprog);
2434 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002435 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002436 lp->sl_compprog = NULL;
2437 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002438 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002439
2440 vim_free(lp->sl_syllable);
2441 lp->sl_syllable = NULL;
2442 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002443
Bram Moolenaar4770d092006-01-12 23:22:24 +00002444 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2445 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002446
Bram Moolenaar4770d092006-01-12 23:22:24 +00002447#ifdef FEAT_MBYTE
2448 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaarea424162005-06-16 21:51:00 +00002449#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002450
Bram Moolenaar4770d092006-01-12 23:22:24 +00002451 /* Clear info from .sug file. */
2452 slang_clear_sug(lp);
2453
Bram Moolenaar5195e452005-08-19 20:32:47 +00002454 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002455 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002456 lp->sl_compsylmax = MAXWLEN;
2457 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002458}
2459
2460/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002461 * Clear the info from the .sug file in "lp".
2462 */
2463 static void
2464slang_clear_sug(lp)
2465 slang_T *lp;
2466{
2467 vim_free(lp->sl_sbyts);
2468 lp->sl_sbyts = NULL;
2469 vim_free(lp->sl_sidxs);
2470 lp->sl_sidxs = NULL;
2471 close_spellbuf(lp->sl_sugbuf);
2472 lp->sl_sugbuf = NULL;
2473 lp->sl_sugloaded = FALSE;
2474 lp->sl_sugtime = 0;
2475}
2476
2477/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002478 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002479 * Invoked through do_in_runtimepath().
2480 */
2481 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002482spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002483 char_u *fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002484 void *cookie;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002485{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002486 spelload_T *slp = (spelload_T *)cookie;
2487 slang_T *slang;
2488
2489 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2490 if (slang != NULL)
2491 {
2492 /* When a previously loaded file has NOBREAK also use it for the
2493 * ".add" files. */
2494 if (slp->sl_nobreak && slang->sl_add)
2495 slang->sl_nobreak = TRUE;
2496 else if (slang->sl_nobreak)
2497 slp->sl_nobreak = TRUE;
2498
2499 slp->sl_slang = slang;
2500 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002501}
2502
2503/*
2504 * Load one spell file and store the info into a slang_T.
2505 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00002506 * This is invoked in three ways:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002507 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2508 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2509 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2510 * points to the existing slang_T.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002511 * - Just after writing a .spl file; it's read back to produce the .sug file.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002512 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
2513 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002514 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002515 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002516 static slang_T *
2517spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002518 char_u *fname;
2519 char_u *lang;
2520 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002521 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002522{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002523 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002524 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002525 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002526 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002527 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002528 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002529 char_u *save_sourcing_name = sourcing_name;
2530 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002531 slang_T *lp = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002532 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002533 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002534
Bram Moolenaarb765d632005-06-07 21:00:02 +00002535 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002536 if (fd == NULL)
2537 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002538 if (!silent)
2539 EMSG2(_(e_notopen), fname);
2540 else if (p_verbose > 2)
2541 {
2542 verbose_enter();
2543 smsg((char_u *)e_notopen, fname);
2544 verbose_leave();
2545 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002546 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002547 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002548 if (p_verbose > 2)
2549 {
2550 verbose_enter();
2551 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2552 verbose_leave();
2553 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002554
Bram Moolenaarb765d632005-06-07 21:00:02 +00002555 if (old_lp == NULL)
2556 {
2557 lp = slang_alloc(lang);
2558 if (lp == NULL)
2559 goto endFAIL;
2560
2561 /* Remember the file name, used to reload the file when it's updated. */
2562 lp->sl_fname = vim_strsave(fname);
2563 if (lp->sl_fname == NULL)
2564 goto endFAIL;
2565
2566 /* Check for .add.spl. */
2567 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2568 }
2569 else
2570 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002571
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002572 /* Set sourcing_name, so that error messages mention the file name. */
2573 sourcing_name = fname;
2574 sourcing_lnum = 0;
2575
Bram Moolenaar4770d092006-01-12 23:22:24 +00002576 /*
2577 * <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002578 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002579 for (i = 0; i < VIMSPELLMAGICL; ++i)
2580 buf[i] = getc(fd); /* <fileID> */
2581 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2582 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002583 EMSG(_("E757: This does not look like a spell file"));
2584 goto endFAIL;
2585 }
2586 c = getc(fd); /* <versionnr> */
2587 if (c < VIMSPELLVERSION)
2588 {
2589 EMSG(_("E771: Old spell file, needs to be updated"));
2590 goto endFAIL;
2591 }
2592 else if (c > VIMSPELLVERSION)
2593 {
2594 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002595 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002596 }
2597
Bram Moolenaar5195e452005-08-19 20:32:47 +00002598
2599 /*
2600 * <SECTIONS>: <section> ... <sectionend>
2601 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2602 */
2603 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002604 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002605 n = getc(fd); /* <sectionID> or <sectionend> */
2606 if (n == SN_END)
2607 break;
2608 c = getc(fd); /* <sectionflags> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002609 len = get4c(fd); /* <sectionlen> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002610 if (len < 0)
2611 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002612
Bram Moolenaar5195e452005-08-19 20:32:47 +00002613 res = 0;
2614 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002615 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002616 case SN_REGION:
2617 res = read_region_section(fd, lp, len);
2618 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002619
Bram Moolenaar5195e452005-08-19 20:32:47 +00002620 case SN_CHARFLAGS:
2621 res = read_charflags_section(fd);
2622 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002623
Bram Moolenaar5195e452005-08-19 20:32:47 +00002624 case SN_MIDWORD:
2625 lp->sl_midword = read_string(fd, len); /* <midword> */
2626 if (lp->sl_midword == NULL)
2627 goto endFAIL;
2628 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002629
Bram Moolenaar5195e452005-08-19 20:32:47 +00002630 case SN_PREFCOND:
2631 res = read_prefcond_section(fd, lp);
2632 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002633
Bram Moolenaar5195e452005-08-19 20:32:47 +00002634 case SN_REP:
Bram Moolenaar4770d092006-01-12 23:22:24 +00002635 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
2636 break;
2637
2638 case SN_REPSAL:
2639 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002640 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002641
Bram Moolenaar5195e452005-08-19 20:32:47 +00002642 case SN_SAL:
2643 res = read_sal_section(fd, lp);
2644 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002645
Bram Moolenaar5195e452005-08-19 20:32:47 +00002646 case SN_SOFO:
2647 res = read_sofo_section(fd, lp);
2648 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002649
Bram Moolenaar5195e452005-08-19 20:32:47 +00002650 case SN_MAP:
2651 p = read_string(fd, len); /* <mapstr> */
2652 if (p == NULL)
2653 goto endFAIL;
2654 set_map_str(lp, p);
2655 vim_free(p);
2656 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002657
Bram Moolenaar4770d092006-01-12 23:22:24 +00002658 case SN_WORDS:
2659 res = read_words_section(fd, lp, len);
2660 break;
2661
2662 case SN_SUGFILE:
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002663 lp->sl_sugtime = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002664 break;
2665
Bram Moolenaar5195e452005-08-19 20:32:47 +00002666 case SN_COMPOUND:
2667 res = read_compound(fd, lp, len);
2668 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002669
Bram Moolenaar78622822005-08-23 21:00:13 +00002670 case SN_NOBREAK:
2671 lp->sl_nobreak = TRUE;
2672 break;
2673
Bram Moolenaar5195e452005-08-19 20:32:47 +00002674 case SN_SYLLABLE:
2675 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2676 if (lp->sl_syllable == NULL)
2677 goto endFAIL;
2678 if (init_syl_tab(lp) == FAIL)
2679 goto endFAIL;
2680 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002681
Bram Moolenaar5195e452005-08-19 20:32:47 +00002682 default:
2683 /* Unsupported section. When it's required give an error
2684 * message. When it's not required skip the contents. */
2685 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002686 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002687 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002688 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002689 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002690 while (--len >= 0)
2691 if (getc(fd) < 0)
2692 goto truncerr;
2693 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002694 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002695someerror:
Bram Moolenaar5195e452005-08-19 20:32:47 +00002696 if (res == SP_FORMERROR)
2697 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002698 EMSG(_(e_format));
2699 goto endFAIL;
2700 }
2701 if (res == SP_TRUNCERROR)
2702 {
2703truncerr:
2704 EMSG(_(e_spell_trunc));
2705 goto endFAIL;
2706 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002707 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002708 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002709 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002710
Bram Moolenaar4770d092006-01-12 23:22:24 +00002711 /* <LWORDTREE> */
2712 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
2713 if (res != 0)
2714 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002715
Bram Moolenaar4770d092006-01-12 23:22:24 +00002716 /* <KWORDTREE> */
2717 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
2718 if (res != 0)
2719 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002720
Bram Moolenaar4770d092006-01-12 23:22:24 +00002721 /* <PREFIXTREE> */
2722 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
2723 lp->sl_prefixcnt);
2724 if (res != 0)
2725 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002726
Bram Moolenaarb765d632005-06-07 21:00:02 +00002727 /* For a new file link it in the list of spell files. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002728 if (old_lp == NULL && lang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002729 {
2730 lp->sl_next = first_lang;
2731 first_lang = lp;
2732 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002733
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002734 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002735
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002736endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002737 if (lang != NULL)
2738 /* truncating the name signals the error to spell_load_lang() */
2739 *lang = NUL;
2740 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002741 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002742 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002743
2744endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002745 if (fd != NULL)
2746 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002747 sourcing_name = save_sourcing_name;
2748 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002749
2750 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002751}
2752
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002753/*
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002754 * Read 2 bytes from "fd" and turn them into an int, MSB first.
2755 */
2756 static int
2757get2c(fd)
2758 FILE *fd;
2759{
2760 long n;
2761
2762 n = getc(fd);
2763 n = (n << 8) + getc(fd);
2764 return n;
2765}
2766
2767/*
2768 * Read 3 bytes from "fd" and turn them into an int, MSB first.
2769 */
2770 static int
2771get3c(fd)
2772 FILE *fd;
2773{
2774 long n;
2775
2776 n = getc(fd);
2777 n = (n << 8) + getc(fd);
2778 n = (n << 8) + getc(fd);
2779 return n;
2780}
2781
2782/*
2783 * Read 4 bytes from "fd" and turn them into an int, MSB first.
2784 */
2785 static int
2786get4c(fd)
2787 FILE *fd;
2788{
2789 long n;
2790
2791 n = getc(fd);
2792 n = (n << 8) + getc(fd);
2793 n = (n << 8) + getc(fd);
2794 n = (n << 8) + getc(fd);
2795 return n;
2796}
2797
2798/*
2799 * Read 8 bytes from "fd" and turn them into a time_t, MSB first.
2800 */
2801 static time_t
2802get8c(fd)
2803 FILE *fd;
2804{
2805 time_t n = 0;
2806 int i;
2807
2808 for (i = 0; i < 8; ++i)
2809 n = (n << 8) + getc(fd);
2810 return n;
2811}
2812
2813/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002814 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002815 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002816 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002817 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2818 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002819 */
2820 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002821read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002822 FILE *fd;
2823 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002824 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002825{
2826 int cnt = 0;
2827 int i;
2828 char_u *str;
2829
2830 /* read the length bytes, MSB first */
2831 for (i = 0; i < cnt_bytes; ++i)
2832 cnt = (cnt << 8) + getc(fd);
2833 if (cnt < 0)
2834 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002835 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002836 return NULL;
2837 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002838 *cntp = cnt;
2839 if (cnt == 0)
2840 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002841
Bram Moolenaar5195e452005-08-19 20:32:47 +00002842 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002843 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002844 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002845 return str;
2846}
2847
Bram Moolenaar7887d882005-07-01 22:33:52 +00002848/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00002849 * Read a string of length "cnt" from "fd" into allocated memory.
2850 * Returns NULL when out of memory.
2851 */
2852 static char_u *
2853read_string(fd, cnt)
2854 FILE *fd;
2855 int cnt;
2856{
2857 char_u *str;
2858 int i;
2859
2860 /* allocate memory */
2861 str = alloc((unsigned)cnt + 1);
2862 if (str != NULL)
2863 {
2864 /* Read the string. Doesn't check for truncated file. */
2865 for (i = 0; i < cnt; ++i)
2866 str[i] = getc(fd);
2867 str[i] = NUL;
2868 }
2869 return str;
2870}
2871
2872/*
2873 * Read SN_REGION: <regionname> ...
2874 * Return SP_*ERROR flags.
2875 */
2876 static int
2877read_region_section(fd, lp, len)
2878 FILE *fd;
2879 slang_T *lp;
2880 int len;
2881{
2882 int i;
2883
2884 if (len > 16)
2885 return SP_FORMERROR;
2886 for (i = 0; i < len; ++i)
2887 lp->sl_regions[i] = getc(fd); /* <regionname> */
2888 lp->sl_regions[len] = NUL;
2889 return 0;
2890}
2891
2892/*
2893 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
2894 * <folcharslen> <folchars>
2895 * Return SP_*ERROR flags.
2896 */
2897 static int
2898read_charflags_section(fd)
2899 FILE *fd;
2900{
2901 char_u *flags;
2902 char_u *fol;
2903 int flagslen, follen;
2904
2905 /* <charflagslen> <charflags> */
2906 flags = read_cnt_string(fd, 1, &flagslen);
2907 if (flagslen < 0)
2908 return flagslen;
2909
2910 /* <folcharslen> <folchars> */
2911 fol = read_cnt_string(fd, 2, &follen);
2912 if (follen < 0)
2913 {
2914 vim_free(flags);
2915 return follen;
2916 }
2917
2918 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
2919 if (flags != NULL && fol != NULL)
2920 set_spell_charflags(flags, flagslen, fol);
2921
2922 vim_free(flags);
2923 vim_free(fol);
2924
2925 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
2926 if ((flags == NULL) != (fol == NULL))
2927 return SP_FORMERROR;
2928 return 0;
2929}
2930
2931/*
2932 * Read SN_PREFCOND section.
2933 * Return SP_*ERROR flags.
2934 */
2935 static int
2936read_prefcond_section(fd, lp)
2937 FILE *fd;
2938 slang_T *lp;
2939{
2940 int cnt;
2941 int i;
2942 int n;
2943 char_u *p;
2944 char_u buf[MAXWLEN + 1];
2945
2946 /* <prefcondcnt> <prefcond> ... */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002947 cnt = get2c(fd); /* <prefcondcnt> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002948 if (cnt <= 0)
2949 return SP_FORMERROR;
2950
2951 lp->sl_prefprog = (regprog_T **)alloc_clear(
2952 (unsigned)sizeof(regprog_T *) * cnt);
2953 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002954 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002955 lp->sl_prefixcnt = cnt;
2956
2957 for (i = 0; i < cnt; ++i)
2958 {
2959 /* <prefcond> : <condlen> <condstr> */
2960 n = getc(fd); /* <condlen> */
2961 if (n < 0 || n >= MAXWLEN)
2962 return SP_FORMERROR;
2963
2964 /* When <condlen> is zero we have an empty condition. Otherwise
2965 * compile the regexp program used to check for the condition. */
2966 if (n > 0)
2967 {
2968 buf[0] = '^'; /* always match at one position only */
2969 p = buf + 1;
2970 while (n-- > 0)
2971 *p++ = getc(fd); /* <condstr> */
2972 *p = NUL;
2973 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
2974 }
2975 }
2976 return 0;
2977}
2978
2979/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002980 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
Bram Moolenaar5195e452005-08-19 20:32:47 +00002981 * Return SP_*ERROR flags.
2982 */
2983 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002984read_rep_section(fd, gap, first)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002985 FILE *fd;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002986 garray_T *gap;
2987 short *first;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002988{
2989 int cnt;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002990 fromto_T *ftp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002991 int i;
2992
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002993 cnt = get2c(fd); /* <repcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002994 if (cnt < 0)
2995 return SP_TRUNCERROR;
2996
Bram Moolenaar5195e452005-08-19 20:32:47 +00002997 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002998 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002999
3000 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
3001 for (; gap->ga_len < cnt; ++gap->ga_len)
3002 {
3003 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
3004 ftp->ft_from = read_cnt_string(fd, 1, &i);
3005 if (i < 0)
3006 return i;
3007 if (i == 0)
3008 return SP_FORMERROR;
3009 ftp->ft_to = read_cnt_string(fd, 1, &i);
3010 if (i <= 0)
3011 {
3012 vim_free(ftp->ft_from);
3013 if (i < 0)
3014 return i;
3015 return SP_FORMERROR;
3016 }
3017 }
3018
3019 /* Fill the first-index table. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003020 for (i = 0; i < 256; ++i)
3021 first[i] = -1;
3022 for (i = 0; i < gap->ga_len; ++i)
3023 {
3024 ftp = &((fromto_T *)gap->ga_data)[i];
3025 if (first[*ftp->ft_from] == -1)
3026 first[*ftp->ft_from] = i;
3027 }
3028 return 0;
3029}
3030
3031/*
3032 * Read SN_SAL section: <salflags> <salcount> <sal> ...
3033 * Return SP_*ERROR flags.
3034 */
3035 static int
3036read_sal_section(fd, slang)
3037 FILE *fd;
3038 slang_T *slang;
3039{
3040 int i;
3041 int cnt;
3042 garray_T *gap;
3043 salitem_T *smp;
3044 int ccnt;
3045 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003046 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003047
3048 slang->sl_sofo = FALSE;
3049
3050 i = getc(fd); /* <salflags> */
3051 if (i & SAL_F0LLOWUP)
3052 slang->sl_followup = TRUE;
3053 if (i & SAL_COLLAPSE)
3054 slang->sl_collapse = TRUE;
3055 if (i & SAL_REM_ACCENTS)
3056 slang->sl_rem_accents = TRUE;
3057
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003058 cnt = get2c(fd); /* <salcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003059 if (cnt < 0)
3060 return SP_TRUNCERROR;
3061
3062 gap = &slang->sl_sal;
3063 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003064 if (ga_grow(gap, cnt + 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003065 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003066
3067 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
3068 for (; gap->ga_len < cnt; ++gap->ga_len)
3069 {
3070 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3071 ccnt = getc(fd); /* <salfromlen> */
3072 if (ccnt < 0)
3073 return SP_TRUNCERROR;
3074 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003075 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003076 smp->sm_lead = p;
3077
3078 /* Read up to the first special char into sm_lead. */
3079 for (i = 0; i < ccnt; ++i)
3080 {
3081 c = getc(fd); /* <salfrom> */
3082 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
3083 break;
3084 *p++ = c;
3085 }
3086 smp->sm_leadlen = p - smp->sm_lead;
3087 *p++ = NUL;
3088
3089 /* Put (abc) chars in sm_oneof, if any. */
3090 if (c == '(')
3091 {
3092 smp->sm_oneof = p;
3093 for (++i; i < ccnt; ++i)
3094 {
3095 c = getc(fd); /* <salfrom> */
3096 if (c == ')')
3097 break;
3098 *p++ = c;
3099 }
3100 *p++ = NUL;
3101 if (++i < ccnt)
3102 c = getc(fd);
3103 }
3104 else
3105 smp->sm_oneof = NULL;
3106
3107 /* Any following chars go in sm_rules. */
3108 smp->sm_rules = p;
3109 if (i < ccnt)
3110 /* store the char we got while checking for end of sm_lead */
3111 *p++ = c;
3112 for (++i; i < ccnt; ++i)
3113 *p++ = getc(fd); /* <salfrom> */
3114 *p++ = NUL;
3115
3116 /* <saltolen> <salto> */
3117 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
3118 if (ccnt < 0)
3119 {
3120 vim_free(smp->sm_lead);
3121 return ccnt;
3122 }
3123
3124#ifdef FEAT_MBYTE
3125 if (has_mbyte)
3126 {
3127 /* convert the multi-byte strings to wide char strings */
3128 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3129 smp->sm_leadlen = mb_charlen(smp->sm_lead);
3130 if (smp->sm_oneof == NULL)
3131 smp->sm_oneof_w = NULL;
3132 else
3133 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
3134 if (smp->sm_to == NULL)
3135 smp->sm_to_w = NULL;
3136 else
3137 smp->sm_to_w = mb_str2wide(smp->sm_to);
3138 if (smp->sm_lead_w == NULL
3139 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
3140 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
3141 {
3142 vim_free(smp->sm_lead);
3143 vim_free(smp->sm_to);
3144 vim_free(smp->sm_lead_w);
3145 vim_free(smp->sm_oneof_w);
3146 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003147 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003148 }
3149 }
3150#endif
3151 }
3152
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003153 if (gap->ga_len > 0)
3154 {
3155 /* Add one extra entry to mark the end with an empty sm_lead. Avoids
3156 * that we need to check the index every time. */
3157 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3158 if ((p = alloc(1)) == NULL)
3159 return SP_OTHERERROR;
3160 p[0] = NUL;
3161 smp->sm_lead = p;
3162 smp->sm_leadlen = 0;
3163 smp->sm_oneof = NULL;
3164 smp->sm_rules = p;
3165 smp->sm_to = NULL;
3166#ifdef FEAT_MBYTE
3167 if (has_mbyte)
3168 {
3169 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3170 smp->sm_leadlen = 0;
3171 smp->sm_oneof_w = NULL;
3172 smp->sm_to_w = NULL;
3173 }
3174#endif
3175 ++gap->ga_len;
3176 }
3177
Bram Moolenaar5195e452005-08-19 20:32:47 +00003178 /* Fill the first-index table. */
3179 set_sal_first(slang);
3180
3181 return 0;
3182}
3183
3184/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003185 * Read SN_WORDS: <word> ...
3186 * Return SP_*ERROR flags.
3187 */
3188 static int
3189read_words_section(fd, lp, len)
3190 FILE *fd;
3191 slang_T *lp;
3192 int len;
3193{
3194 int done = 0;
3195 int i;
3196 char_u word[MAXWLEN];
3197
3198 while (done < len)
3199 {
3200 /* Read one word at a time. */
3201 for (i = 0; ; ++i)
3202 {
3203 word[i] = getc(fd);
3204 if (word[i] == NUL)
3205 break;
3206 if (i == MAXWLEN - 1)
3207 return SP_FORMERROR;
3208 }
3209
3210 /* Init the count to 10. */
3211 count_common_word(lp, word, -1, 10);
3212 done += i + 1;
3213 }
3214 return 0;
3215}
3216
3217/*
3218 * Add a word to the hashtable of common words.
3219 * If it's already there then the counter is increased.
3220 */
3221 static void
3222count_common_word(lp, word, len, count)
3223 slang_T *lp;
3224 char_u *word;
3225 int len; /* word length, -1 for upto NUL */
3226 int count; /* 1 to count once, 10 to init */
3227{
3228 hash_T hash;
3229 hashitem_T *hi;
3230 wordcount_T *wc;
3231 char_u buf[MAXWLEN];
3232 char_u *p;
3233
3234 if (len == -1)
3235 p = word;
3236 else
3237 {
3238 vim_strncpy(buf, word, len);
3239 p = buf;
3240 }
3241
3242 hash = hash_hash(p);
3243 hi = hash_lookup(&lp->sl_wordcount, p, hash);
3244 if (HASHITEM_EMPTY(hi))
3245 {
3246 wc = (wordcount_T *)alloc(sizeof(wordcount_T) + STRLEN(p));
3247 if (wc == NULL)
3248 return;
3249 STRCPY(wc->wc_word, p);
3250 wc->wc_count = count;
3251 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
3252 }
3253 else
3254 {
3255 wc = HI2WC(hi);
3256 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
3257 wc->wc_count = MAXWORDCOUNT;
3258 }
3259}
3260
3261/*
3262 * Adjust the score of common words.
3263 */
3264 static int
3265score_wordcount_adj(slang, score, word, split)
3266 slang_T *slang;
3267 int score;
3268 char_u *word;
3269 int split; /* word was split, less bonus */
3270{
3271 hashitem_T *hi;
3272 wordcount_T *wc;
3273 int bonus;
3274 int newscore;
3275
3276 hi = hash_find(&slang->sl_wordcount, word);
3277 if (!HASHITEM_EMPTY(hi))
3278 {
3279 wc = HI2WC(hi);
3280 if (wc->wc_count < SCORE_THRES2)
3281 bonus = SCORE_COMMON1;
3282 else if (wc->wc_count < SCORE_THRES3)
3283 bonus = SCORE_COMMON2;
3284 else
3285 bonus = SCORE_COMMON3;
3286 if (split)
3287 newscore = score - bonus / 2;
3288 else
3289 newscore = score - bonus;
3290 if (newscore < 0)
3291 return 0;
3292 return newscore;
3293 }
3294 return score;
3295}
3296
3297/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003298 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
3299 * Return SP_*ERROR flags.
3300 */
3301 static int
3302read_sofo_section(fd, slang)
3303 FILE *fd;
3304 slang_T *slang;
3305{
3306 int cnt;
3307 char_u *from, *to;
3308 int res;
3309
3310 slang->sl_sofo = TRUE;
3311
3312 /* <sofofromlen> <sofofrom> */
3313 from = read_cnt_string(fd, 2, &cnt);
3314 if (cnt < 0)
3315 return cnt;
3316
3317 /* <sofotolen> <sofoto> */
3318 to = read_cnt_string(fd, 2, &cnt);
3319 if (cnt < 0)
3320 {
3321 vim_free(from);
3322 return cnt;
3323 }
3324
3325 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
3326 if (from != NULL && to != NULL)
3327 res = set_sofo(slang, from, to);
3328 else if (from != NULL || to != NULL)
3329 res = SP_FORMERROR; /* only one of two strings is an error */
3330 else
3331 res = 0;
3332
3333 vim_free(from);
3334 vim_free(to);
3335 return res;
3336}
3337
3338/*
3339 * Read the compound section from the .spl file:
3340 * <compmax> <compminlen> <compsylmax> <compflags>
3341 * Returns SP_*ERROR flags.
3342 */
3343 static int
3344read_compound(fd, slang, len)
3345 FILE *fd;
3346 slang_T *slang;
3347 int len;
3348{
3349 int todo = len;
3350 int c;
3351 int atstart;
3352 char_u *pat;
3353 char_u *pp;
3354 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003355 char_u *ap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003356
3357 if (todo < 2)
3358 return SP_FORMERROR; /* need at least two bytes */
3359
3360 --todo;
3361 c = getc(fd); /* <compmax> */
3362 if (c < 2)
3363 c = MAXWLEN;
3364 slang->sl_compmax = c;
3365
3366 --todo;
3367 c = getc(fd); /* <compminlen> */
3368 if (c < 1)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003369 c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003370 slang->sl_compminlen = c;
3371
3372 --todo;
3373 c = getc(fd); /* <compsylmax> */
3374 if (c < 1)
3375 c = MAXWLEN;
3376 slang->sl_compsylmax = c;
3377
3378 /* Turn the COMPOUNDFLAGS items into a regexp pattern:
3379 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003380 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3381 * Conversion to utf-8 may double the size. */
3382 c = todo * 2 + 7;
3383#ifdef FEAT_MBYTE
3384 if (enc_utf8)
3385 c += todo * 2;
3386#endif
3387 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003388 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003389 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003390
Bram Moolenaard12a1322005-08-21 22:08:24 +00003391 /* We also need a list of all flags that can appear at the start and one
3392 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003393 cp = alloc(todo + 1);
3394 if (cp == NULL)
3395 {
3396 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003397 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003398 }
3399 slang->sl_compstartflags = cp;
3400 *cp = NUL;
3401
Bram Moolenaard12a1322005-08-21 22:08:24 +00003402 ap = alloc(todo + 1);
3403 if (ap == NULL)
3404 {
3405 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003406 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003407 }
3408 slang->sl_compallflags = ap;
3409 *ap = NUL;
3410
Bram Moolenaar5195e452005-08-19 20:32:47 +00003411 pp = pat;
3412 *pp++ = '^';
3413 *pp++ = '\\';
3414 *pp++ = '(';
3415
3416 atstart = 1;
3417 while (todo-- > 0)
3418 {
3419 c = getc(fd); /* <compflags> */
Bram Moolenaard12a1322005-08-21 22:08:24 +00003420
3421 /* Add all flags to "sl_compallflags". */
3422 if (vim_strchr((char_u *)"+*[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00003423 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003424 {
3425 *ap++ = c;
3426 *ap = NUL;
3427 }
3428
Bram Moolenaar5195e452005-08-19 20:32:47 +00003429 if (atstart != 0)
3430 {
3431 /* At start of item: copy flags to "sl_compstartflags". For a
3432 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3433 if (c == '[')
3434 atstart = 2;
3435 else if (c == ']')
3436 atstart = 0;
3437 else
3438 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003439 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003440 {
3441 *cp++ = c;
3442 *cp = NUL;
3443 }
3444 if (atstart == 1)
3445 atstart = 0;
3446 }
3447 }
3448 if (c == '/') /* slash separates two items */
3449 {
3450 *pp++ = '\\';
3451 *pp++ = '|';
3452 atstart = 1;
3453 }
3454 else /* normal char, "[abc]" and '*' are copied as-is */
3455 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003456 if (c == '+' || c == '~')
Bram Moolenaar5195e452005-08-19 20:32:47 +00003457 *pp++ = '\\'; /* "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003458#ifdef FEAT_MBYTE
3459 if (enc_utf8)
3460 pp += mb_char2bytes(c, pp);
3461 else
3462#endif
3463 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003464 }
3465 }
3466
3467 *pp++ = '\\';
3468 *pp++ = ')';
3469 *pp++ = '$';
3470 *pp = NUL;
3471
3472 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3473 vim_free(pat);
3474 if (slang->sl_compprog == NULL)
3475 return SP_FORMERROR;
3476
3477 return 0;
3478}
3479
Bram Moolenaar6de68532005-08-24 22:08:48 +00003480/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003481 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003482 * Like strchr() but independent of locale.
3483 */
3484 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003485byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003486 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003487 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003488{
3489 char_u *p;
3490
3491 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003492 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003493 return TRUE;
3494 return FALSE;
3495}
3496
Bram Moolenaar5195e452005-08-19 20:32:47 +00003497#define SY_MAXLEN 30
3498typedef struct syl_item_S
3499{
3500 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3501 int sy_len;
3502} syl_item_T;
3503
3504/*
3505 * Truncate "slang->sl_syllable" at the first slash and put the following items
3506 * in "slang->sl_syl_items".
3507 */
3508 static int
3509init_syl_tab(slang)
3510 slang_T *slang;
3511{
3512 char_u *p;
3513 char_u *s;
3514 int l;
3515 syl_item_T *syl;
3516
3517 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3518 p = vim_strchr(slang->sl_syllable, '/');
3519 while (p != NULL)
3520 {
3521 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003522 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003523 break;
3524 s = p;
3525 p = vim_strchr(p, '/');
3526 if (p == NULL)
3527 l = STRLEN(s);
3528 else
3529 l = p - s;
3530 if (l >= SY_MAXLEN)
3531 return SP_FORMERROR;
3532 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003533 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003534 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3535 + slang->sl_syl_items.ga_len++;
3536 vim_strncpy(syl->sy_chars, s, l);
3537 syl->sy_len = l;
3538 }
3539 return OK;
3540}
3541
3542/*
3543 * Count the number of syllables in "word".
3544 * When "word" contains spaces the syllables after the last space are counted.
3545 * Returns zero if syllables are not defines.
3546 */
3547 static int
3548count_syllables(slang, word)
3549 slang_T *slang;
3550 char_u *word;
3551{
3552 int cnt = 0;
3553 int skip = FALSE;
3554 char_u *p;
3555 int len;
3556 int i;
3557 syl_item_T *syl;
3558 int c;
3559
3560 if (slang->sl_syllable == NULL)
3561 return 0;
3562
3563 for (p = word; *p != NUL; p += len)
3564 {
3565 /* When running into a space reset counter. */
3566 if (*p == ' ')
3567 {
3568 len = 1;
3569 cnt = 0;
3570 continue;
3571 }
3572
3573 /* Find longest match of syllable items. */
3574 len = 0;
3575 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3576 {
3577 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3578 if (syl->sy_len > len
3579 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3580 len = syl->sy_len;
3581 }
3582 if (len != 0) /* found a match, count syllable */
3583 {
3584 ++cnt;
3585 skip = FALSE;
3586 }
3587 else
3588 {
3589 /* No recognized syllable item, at least a syllable char then? */
3590#ifdef FEAT_MBYTE
3591 c = mb_ptr2char(p);
3592 len = (*mb_ptr2len)(p);
3593#else
3594 c = *p;
3595 len = 1;
3596#endif
3597 if (vim_strchr(slang->sl_syllable, c) == NULL)
3598 skip = FALSE; /* No, search for next syllable */
3599 else if (!skip)
3600 {
3601 ++cnt; /* Yes, count it */
3602 skip = TRUE; /* don't count following syllable chars */
3603 }
3604 }
3605 }
3606 return cnt;
3607}
3608
3609/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003610 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003611 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003612 */
3613 static int
3614set_sofo(lp, from, to)
3615 slang_T *lp;
3616 char_u *from;
3617 char_u *to;
3618{
3619 int i;
3620
3621#ifdef FEAT_MBYTE
3622 garray_T *gap;
3623 char_u *s;
3624 char_u *p;
3625 int c;
3626 int *inp;
3627
3628 if (has_mbyte)
3629 {
3630 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3631 * characters. The index is the low byte of the character.
3632 * The list contains from-to pairs with a terminating NUL.
3633 * sl_sal_first[] is used for latin1 "from" characters. */
3634 gap = &lp->sl_sal;
3635 ga_init2(gap, sizeof(int *), 1);
3636 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003637 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003638 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3639 gap->ga_len = 256;
3640
3641 /* First count the number of items for each list. Temporarily use
3642 * sl_sal_first[] for this. */
3643 for (p = from, s = to; *p != NUL && *s != NUL; )
3644 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003645 c = mb_cptr2char_adv(&p);
3646 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003647 if (c >= 256)
3648 ++lp->sl_sal_first[c & 0xff];
3649 }
3650 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003651 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003652
3653 /* Allocate the lists. */
3654 for (i = 0; i < 256; ++i)
3655 if (lp->sl_sal_first[i] > 0)
3656 {
3657 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3658 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003659 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003660 ((int **)gap->ga_data)[i] = (int *)p;
3661 *(int *)p = 0;
3662 }
3663
3664 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3665 * list. */
3666 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3667 for (p = from, s = to; *p != NUL && *s != NUL; )
3668 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003669 c = mb_cptr2char_adv(&p);
3670 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003671 if (c >= 256)
3672 {
3673 /* Append the from-to chars at the end of the list with
3674 * the low byte. */
3675 inp = ((int **)gap->ga_data)[c & 0xff];
3676 while (*inp != 0)
3677 ++inp;
3678 *inp++ = c; /* from char */
3679 *inp++ = i; /* to char */
3680 *inp++ = NUL; /* NUL at the end */
3681 }
3682 else
3683 /* mapping byte to char is done in sl_sal_first[] */
3684 lp->sl_sal_first[c] = i;
3685 }
3686 }
3687 else
3688#endif
3689 {
3690 /* mapping bytes to bytes is done in sl_sal_first[] */
3691 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003692 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003693
3694 for (i = 0; to[i] != NUL; ++i)
3695 lp->sl_sal_first[from[i]] = to[i];
3696 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3697 }
3698
Bram Moolenaar5195e452005-08-19 20:32:47 +00003699 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003700}
3701
3702/*
3703 * Fill the first-index table for "lp".
3704 */
3705 static void
3706set_sal_first(lp)
3707 slang_T *lp;
3708{
3709 salfirst_T *sfirst;
3710 int i;
3711 salitem_T *smp;
3712 int c;
3713 garray_T *gap = &lp->sl_sal;
3714
3715 sfirst = lp->sl_sal_first;
3716 for (i = 0; i < 256; ++i)
3717 sfirst[i] = -1;
3718 smp = (salitem_T *)gap->ga_data;
3719 for (i = 0; i < gap->ga_len; ++i)
3720 {
3721#ifdef FEAT_MBYTE
3722 if (has_mbyte)
3723 /* Use the lowest byte of the first character. For latin1 it's
3724 * the character, for other encodings it should differ for most
3725 * characters. */
3726 c = *smp[i].sm_lead_w & 0xff;
3727 else
3728#endif
3729 c = *smp[i].sm_lead;
3730 if (sfirst[c] == -1)
3731 {
3732 sfirst[c] = i;
3733#ifdef FEAT_MBYTE
3734 if (has_mbyte)
3735 {
3736 int n;
3737
3738 /* Make sure all entries with this byte are following each
3739 * other. Move the ones that are in the wrong position. Do
3740 * keep the same ordering! */
3741 while (i + 1 < gap->ga_len
3742 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3743 /* Skip over entry with same index byte. */
3744 ++i;
3745
3746 for (n = 1; i + n < gap->ga_len; ++n)
3747 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3748 {
3749 salitem_T tsal;
3750
3751 /* Move entry with same index byte after the entries
3752 * we already found. */
3753 ++i;
3754 --n;
3755 tsal = smp[i + n];
3756 mch_memmove(smp + i + 1, smp + i,
3757 sizeof(salitem_T) * n);
3758 smp[i] = tsal;
3759 }
3760 }
3761#endif
3762 }
3763 }
3764}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003765
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003766#ifdef FEAT_MBYTE
3767/*
3768 * Turn a multi-byte string into a wide character string.
3769 * Return it in allocated memory (NULL for out-of-memory)
3770 */
3771 static int *
3772mb_str2wide(s)
3773 char_u *s;
3774{
3775 int *res;
3776 char_u *p;
3777 int i = 0;
3778
3779 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3780 if (res != NULL)
3781 {
3782 for (p = s; *p != NUL; )
3783 res[i++] = mb_ptr2char_adv(&p);
3784 res[i] = NUL;
3785 }
3786 return res;
3787}
3788#endif
3789
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003790/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003791 * Read a tree from the .spl or .sug file.
3792 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
3793 * This is skipped when the tree has zero length.
3794 * Returns zero when OK, SP_ value for an error.
3795 */
3796 static int
3797spell_read_tree(fd, bytsp, idxsp, prefixtree, prefixcnt)
3798 FILE *fd;
3799 char_u **bytsp;
3800 idx_T **idxsp;
3801 int prefixtree; /* TRUE for the prefix tree */
3802 int prefixcnt; /* when "prefixtree" is TRUE: prefix count */
3803{
3804 int len;
3805 int idx;
3806 char_u *bp;
3807 idx_T *ip;
3808
3809 /* The tree size was computed when writing the file, so that we can
3810 * allocate it as one long block. <nodecount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003811 len = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003812 if (len < 0)
3813 return SP_TRUNCERROR;
3814 if (len > 0)
3815 {
3816 /* Allocate the byte array. */
3817 bp = lalloc((long_u)len, TRUE);
3818 if (bp == NULL)
3819 return SP_OTHERERROR;
3820 *bytsp = bp;
3821
3822 /* Allocate the index array. */
3823 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
3824 if (ip == NULL)
3825 return SP_OTHERERROR;
3826 *idxsp = ip;
3827
3828 /* Recursively read the tree and store it in the array. */
3829 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
3830 if (idx < 0)
3831 return idx;
3832 }
3833 return 0;
3834}
3835
3836/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003837 * Read one row of siblings from the spell file and store it in the byte array
3838 * "byts" and index array "idxs". Recursively read the children.
3839 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003840 * NOTE: The code here must match put_node()!
Bram Moolenaar51485f02005-06-04 21:55:20 +00003841 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003842 * Returns the index (>= 0) following the siblings.
3843 * Returns SP_TRUNCERROR if the file is shorter than expected.
3844 * Returns SP_FORMERROR if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003845 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003846 static idx_T
Bram Moolenaar4770d092006-01-12 23:22:24 +00003847read_tree_node(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003848 FILE *fd;
3849 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003850 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003851 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003852 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003853 int prefixtree; /* TRUE for reading PREFIXTREE */
3854 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003855{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003856 int len;
3857 int i;
3858 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003859 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003860 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003861 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003862#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003863
Bram Moolenaar51485f02005-06-04 21:55:20 +00003864 len = getc(fd); /* <siblingcount> */
3865 if (len <= 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003866 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003867
3868 if (startidx + len >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003869 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003870 byts[idx++] = len;
3871
3872 /* Read the byte values, flag/region bytes and shared indexes. */
3873 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003874 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003875 c = getc(fd); /* <byte> */
3876 if (c < 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003877 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003878 if (c <= BY_SPECIAL)
3879 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003880 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003881 {
3882 /* No flags, all regions. */
3883 idxs[idx] = 0;
3884 c = 0;
3885 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003886 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003887 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003888 if (prefixtree)
3889 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003890 /* Read the optional pflags byte, the prefix ID and the
3891 * condition nr. In idxs[] store the prefix ID in the low
3892 * byte, the condition index shifted up 8 bits, the flags
3893 * shifted up 24 bits. */
3894 if (c == BY_FLAGS)
3895 c = getc(fd) << 24; /* <pflags> */
3896 else
3897 c = 0;
3898
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003899 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003900
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003901 n = get2c(fd); /* <prefcondnr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003902 if (n >= maxprefcondnr)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003903 return SP_FORMERROR;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003904 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003905 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003906 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003907 {
3908 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003909 * idxs[] the flags go in the low two bytes, region above
3910 * that and prefix ID above the region. */
3911 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003912 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003913 if (c2 == BY_FLAGS2)
3914 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003915 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003916 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003917 if (c & WF_AFX)
3918 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003919 }
3920
Bram Moolenaar51485f02005-06-04 21:55:20 +00003921 idxs[idx] = c;
3922 c = 0;
3923 }
3924 else /* c == BY_INDEX */
3925 {
3926 /* <nodeidx> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003927 n = get3c(fd);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003928 if (n < 0 || n >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003929 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003930 idxs[idx] = n + SHARED_MASK;
3931 c = getc(fd); /* <xbyte> */
3932 }
3933 }
3934 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003935 }
3936
Bram Moolenaar51485f02005-06-04 21:55:20 +00003937 /* Recursively read the children for non-shared siblings.
3938 * Skip the end-of-word ones (zero byte value) and the shared ones (and
3939 * remove SHARED_MASK) */
3940 for (i = 1; i <= len; ++i)
3941 if (byts[startidx + i] != 0)
3942 {
3943 if (idxs[startidx + i] & SHARED_MASK)
3944 idxs[startidx + i] &= ~SHARED_MASK;
3945 else
3946 {
3947 idxs[startidx + i] = idx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003948 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003949 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003950 if (idx < 0)
3951 break;
3952 }
3953 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003954
Bram Moolenaar51485f02005-06-04 21:55:20 +00003955 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003956}
3957
3958/*
3959 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003960 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003961 */
3962 char_u *
3963did_set_spelllang(buf)
3964 buf_T *buf;
3965{
3966 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003967 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003968 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00003969 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003970 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003971 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003972 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003973 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003974 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003975 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003976 int len;
3977 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003978 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003979 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003980 char_u *use_region = NULL;
3981 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003982 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003983 int i, j;
3984 langp_T *lp, *lp2;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003985
3986 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003987 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003988
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003989 /* loop over comma separated language names. */
3990 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003991 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003992 /* Get one language name. */
3993 copy_option_part(&splp, lang, MAXWLEN, ",");
3994
Bram Moolenaar5482f332005-04-17 20:18:43 +00003995 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003996 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003997
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003998 /* If the name ends in ".spl" use it as the name of the spell file.
3999 * If there is a region name let "region" point to it and remove it
4000 * from the name. */
4001 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
4002 {
4003 filename = TRUE;
4004
Bram Moolenaarb6356332005-07-18 21:40:44 +00004005 /* Locate a region and remove it from the file name. */
4006 p = vim_strchr(gettail(lang), '_');
4007 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
4008 && !ASCII_ISALPHA(p[3]))
4009 {
4010 vim_strncpy(region_cp, p + 1, 2);
4011 mch_memmove(p, p + 3, len - (p - lang) - 2);
4012 len -= 3;
4013 region = region_cp;
4014 }
4015 else
4016 dont_use_region = TRUE;
4017
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004018 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004019 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4020 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004021 break;
4022 }
4023 else
4024 {
4025 filename = FALSE;
4026 if (len > 3 && lang[len - 3] == '_')
4027 {
4028 region = lang + len - 2;
4029 len -= 3;
4030 lang[len] = NUL;
4031 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004032 else
4033 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004034
4035 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004036 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4037 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004038 break;
4039 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004040
Bram Moolenaarb6356332005-07-18 21:40:44 +00004041 if (region != NULL)
4042 {
4043 /* If the region differs from what was used before then don't
4044 * use it for 'spellfile'. */
4045 if (use_region != NULL && STRCMP(region, use_region) != 0)
4046 dont_use_region = TRUE;
4047 use_region = region;
4048 }
4049
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004050 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004051 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004052 {
4053 if (filename)
4054 (void)spell_load_file(lang, lang, NULL, FALSE);
4055 else
4056 spell_load_lang(lang);
4057 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004058
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004059 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004060 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004061 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004062 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4063 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
4064 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004065 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004066 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004067 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004068 {
4069 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004070 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004071 if (c == REGION_ALL)
4072 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004073 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004074 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004075 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004076 /* This addition file is for other regions. */
4077 region_mask = 0;
4078 }
4079 else
4080 /* This is probably an error. Give a warning and
4081 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004082 smsg((char_u *)
4083 _("Warning: region %s not supported"),
4084 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004085 }
4086 else
4087 region_mask = 1 << c;
4088 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004089
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004090 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004091 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004092 if (ga_grow(&ga, 1) == FAIL)
4093 {
4094 ga_clear(&ga);
4095 return e_outofmem;
4096 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004097 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004098 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4099 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004100 use_midword(slang, buf);
4101 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004102 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004103 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004104 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004105 }
4106
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004107 /* round 0: load int_wordlist, if possible.
4108 * round 1: load first name in 'spellfile'.
4109 * round 2: load second name in 'spellfile.
4110 * etc. */
4111 spf = curbuf->b_p_spf;
4112 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004113 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004114 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004115 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004116 /* Internal wordlist, if there is one. */
4117 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004118 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004119 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004120 }
4121 else
4122 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004123 /* One entry in 'spellfile'. */
4124 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
4125 STRCAT(spf_name, ".spl");
4126
4127 /* If it was already found above then skip it. */
4128 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004129 {
4130 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
4131 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004132 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004133 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004134 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004135 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004136 }
4137
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004138 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004139 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4140 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004141 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004142 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004143 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004144 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004145 * region name, the region is ignored otherwise. for int_wordlist
4146 * use an arbitrary name. */
4147 if (round == 0)
4148 STRCPY(lang, "internal wordlist");
4149 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00004150 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004151 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004152 p = vim_strchr(lang, '.');
4153 if (p != NULL)
4154 *p = NUL; /* truncate at ".encoding.add" */
4155 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004156 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004157
4158 /* If one of the languages has NOBREAK we assume the addition
4159 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004160 if (slang != NULL && nobreak)
4161 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004162 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004163 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004164 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004165 region_mask = REGION_ALL;
4166 if (use_region != NULL && !dont_use_region)
4167 {
4168 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004169 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004170 if (c != REGION_ALL)
4171 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004172 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004173 /* This spell file is for other regions. */
4174 region_mask = 0;
4175 }
4176
4177 if (region_mask != 0)
4178 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004179 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
4180 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
4181 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004182 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4183 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004184 use_midword(slang, buf);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004185 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004186 }
4187 }
4188
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004189 /* Everything is fine, store the new b_langp value. */
4190 ga_clear(&buf->b_langp);
4191 buf->b_langp = ga;
4192
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004193 /* For each language figure out what language to use for sound folding and
4194 * REP items. If the language doesn't support it itself use another one
4195 * with the same name. E.g. for "en-math" use "en". */
4196 for (i = 0; i < ga.ga_len; ++i)
4197 {
4198 lp = LANGP_ENTRY(ga, i);
4199
4200 /* sound folding */
4201 if (lp->lp_slang->sl_sal.ga_len > 0)
4202 /* language does sound folding itself */
4203 lp->lp_sallang = lp->lp_slang;
4204 else
4205 /* find first similar language that does sound folding */
4206 for (j = 0; j < ga.ga_len; ++j)
4207 {
4208 lp2 = LANGP_ENTRY(ga, j);
4209 if (lp2->lp_slang->sl_sal.ga_len > 0
4210 && STRNCMP(lp->lp_slang->sl_name,
4211 lp2->lp_slang->sl_name, 2) == 0)
4212 {
4213 lp->lp_sallang = lp2->lp_slang;
4214 break;
4215 }
4216 }
4217
4218 /* REP items */
4219 if (lp->lp_slang->sl_rep.ga_len > 0)
4220 /* language has REP items itself */
4221 lp->lp_replang = lp->lp_slang;
4222 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004223 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004224 for (j = 0; j < ga.ga_len; ++j)
4225 {
4226 lp2 = LANGP_ENTRY(ga, j);
4227 if (lp2->lp_slang->sl_rep.ga_len > 0
4228 && STRNCMP(lp->lp_slang->sl_name,
4229 lp2->lp_slang->sl_name, 2) == 0)
4230 {
4231 lp->lp_replang = lp2->lp_slang;
4232 break;
4233 }
4234 }
4235 }
4236
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004237 return NULL;
4238}
4239
4240/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004241 * Clear the midword characters for buffer "buf".
4242 */
4243 static void
4244clear_midword(buf)
4245 buf_T *buf;
4246{
4247 vim_memset(buf->b_spell_ismw, 0, 256);
4248#ifdef FEAT_MBYTE
4249 vim_free(buf->b_spell_ismw_mb);
4250 buf->b_spell_ismw_mb = NULL;
4251#endif
4252}
4253
4254/*
4255 * Use the "sl_midword" field of language "lp" for buffer "buf".
4256 * They add up to any currently used midword characters.
4257 */
4258 static void
4259use_midword(lp, buf)
4260 slang_T *lp;
4261 buf_T *buf;
4262{
4263 char_u *p;
4264
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004265 if (lp->sl_midword == NULL) /* there aren't any */
4266 return;
4267
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004268 for (p = lp->sl_midword; *p != NUL; )
4269#ifdef FEAT_MBYTE
4270 if (has_mbyte)
4271 {
4272 int c, l, n;
4273 char_u *bp;
4274
4275 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004276 l = (*mb_ptr2len)(p);
4277 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004278 buf->b_spell_ismw[c] = TRUE;
4279 else if (buf->b_spell_ismw_mb == NULL)
4280 /* First multi-byte char in "b_spell_ismw_mb". */
4281 buf->b_spell_ismw_mb = vim_strnsave(p, l);
4282 else
4283 {
4284 /* Append multi-byte chars to "b_spell_ismw_mb". */
4285 n = STRLEN(buf->b_spell_ismw_mb);
4286 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
4287 if (bp != NULL)
4288 {
4289 vim_free(buf->b_spell_ismw_mb);
4290 buf->b_spell_ismw_mb = bp;
4291 vim_strncpy(bp + n, p, l);
4292 }
4293 }
4294 p += l;
4295 }
4296 else
4297#endif
4298 buf->b_spell_ismw[*p++] = TRUE;
4299}
4300
4301/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004302 * Find the region "region[2]" in "rp" (points to "sl_regions").
4303 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00004304 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004305 */
4306 static int
4307find_region(rp, region)
4308 char_u *rp;
4309 char_u *region;
4310{
4311 int i;
4312
4313 for (i = 0; ; i += 2)
4314 {
4315 if (rp[i] == NUL)
4316 return REGION_ALL;
4317 if (rp[i] == region[0] && rp[i + 1] == region[1])
4318 break;
4319 }
4320 return i / 2;
4321}
4322
4323/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004324 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004325 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00004326 * Word WF_ONECAP
4327 * W WORD WF_ALLCAP
4328 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004329 */
4330 static int
4331captype(word, end)
4332 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004333 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004334{
4335 char_u *p;
4336 int c;
4337 int firstcap;
4338 int allcap;
4339 int past_second = FALSE; /* past second word char */
4340
4341 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004342 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004343 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004344 return 0; /* only non-word characters, illegal word */
4345#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00004346 if (has_mbyte)
4347 c = mb_ptr2char_adv(&p);
4348 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004349#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00004350 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004351 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004352
4353 /*
4354 * Need to check all letters to find a word with mixed upper/lower.
4355 * But a word with an upper char only at start is a ONECAP.
4356 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004357 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004358 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004359 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004360 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004361 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004362 {
4363 /* UUl -> KEEPCAP */
4364 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004365 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004366 allcap = FALSE;
4367 }
4368 else if (!allcap)
4369 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004370 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004371 past_second = TRUE;
4372 }
4373
4374 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004375 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004376 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004377 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004378 return 0;
4379}
4380
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004381/*
4382 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
4383 * capital. So that make_case_word() can turn WOrd into Word.
4384 * Add ALLCAP for "WOrD".
4385 */
4386 static int
4387badword_captype(word, end)
4388 char_u *word;
4389 char_u *end;
4390{
4391 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004392 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004393 int l, u;
4394 int first;
4395 char_u *p;
4396
4397 if (flags & WF_KEEPCAP)
4398 {
4399 /* Count the number of UPPER and lower case letters. */
4400 l = u = 0;
4401 first = FALSE;
4402 for (p = word; p < end; mb_ptr_adv(p))
4403 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004404 c = PTR2CHAR(p);
4405 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004406 {
4407 ++u;
4408 if (p == word)
4409 first = TRUE;
4410 }
4411 else
4412 ++l;
4413 }
4414
4415 /* If there are more UPPER than lower case letters suggest an
4416 * ALLCAP word. Otherwise, if the first letter is UPPER then
4417 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4418 * require three upper case letters. */
4419 if (u > l && u > 2)
4420 flags |= WF_ALLCAP;
4421 else if (first)
4422 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004423
4424 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
4425 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004426 }
4427 return flags;
4428}
4429
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004430# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
4431/*
4432 * Free all languages.
4433 */
4434 void
4435spell_free_all()
4436{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004437 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004438 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004439 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004440
4441 /* Go through all buffers and handle 'spelllang'. */
4442 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4443 ga_clear(&buf->b_langp);
4444
4445 while (first_lang != NULL)
4446 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004447 slang = first_lang;
4448 first_lang = slang->sl_next;
4449 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004450 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004451
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004452 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004453 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004454 /* Delete the internal wordlist and its .spl file */
4455 mch_remove(int_wordlist);
4456 int_wordlist_spl(fname);
4457 mch_remove(fname);
4458 vim_free(int_wordlist);
4459 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004460 }
4461
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004462 init_spell_chartab();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004463
4464 vim_free(repl_to);
4465 repl_to = NULL;
4466 vim_free(repl_from);
4467 repl_from = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004468}
4469# endif
4470
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004471# if defined(FEAT_MBYTE) || defined(PROTO)
4472/*
4473 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004474 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004475 */
4476 void
4477spell_reload()
4478{
4479 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004480 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004481
Bram Moolenaarea408852005-06-25 22:49:46 +00004482 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004483 init_spell_chartab();
4484
4485 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004486 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004487
4488 /* Go through all buffers and handle 'spelllang'. */
4489 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4490 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004491 /* Only load the wordlists when 'spelllang' is set and there is a
4492 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004493 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004494 {
4495 FOR_ALL_WINDOWS(wp)
4496 if (wp->w_buffer == buf && wp->w_p_spell)
4497 {
4498 (void)did_set_spelllang(buf);
4499# ifdef FEAT_WINDOWS
4500 break;
4501# endif
4502 }
4503 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004504 }
4505}
4506# endif
4507
Bram Moolenaarb765d632005-06-07 21:00:02 +00004508/*
4509 * Reload the spell file "fname" if it's loaded.
4510 */
4511 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004512spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004513 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004514 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004515{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004516 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004517 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004518
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004519 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004520 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004521 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004522 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004523 slang_clear(slang);
4524 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004525 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004526 slang_clear(slang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004527 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004528 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004529 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004530 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004531
4532 /* When "zg" was used and the file wasn't loaded yet, should redo
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004533 * 'spelllang' to load it now. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004534 if (added_word && !didit)
4535 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004536}
4537
4538
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004539/*
4540 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004541 */
4542
Bram Moolenaar51485f02005-06-04 21:55:20 +00004543#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004544 and .dic file. */
4545/*
4546 * Main structure to store the contents of a ".aff" file.
4547 */
4548typedef struct afffile_S
4549{
4550 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004551 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaar371baa92005-12-29 22:43:53 +00004552 unsigned af_rare; /* RARE ID for rare word */
4553 unsigned af_keepcase; /* KEEPCASE ID for keep-case word */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004554 unsigned af_bad; /* BAD ID for banned word */
4555 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004556 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004557 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004558 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4559 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004560 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004561} afffile_T;
4562
Bram Moolenaar6de68532005-08-24 22:08:48 +00004563#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004564#define AFT_LONG 1 /* flags are two characters */
4565#define AFT_CAPLONG 2 /* flags are one or two characters */
4566#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004567
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004568typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004569/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4570struct affentry_S
4571{
4572 affentry_T *ae_next; /* next affix with same name/number */
4573 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4574 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004575 char_u *ae_cond; /* condition (NULL for ".") */
4576 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004577 char_u ae_rare; /* rare affix */
4578 char_u ae_nocomp; /* word with affix not compoundable */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004579};
4580
Bram Moolenaar6de68532005-08-24 22:08:48 +00004581#ifdef FEAT_MBYTE
4582# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4583#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004584# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004585#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004586
Bram Moolenaar51485f02005-06-04 21:55:20 +00004587/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4588typedef struct affheader_S
4589{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004590 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4591 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4592 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004593 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004594 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004595 affentry_T *ah_first; /* first affix entry */
4596} affheader_T;
4597
4598#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4599
Bram Moolenaar6de68532005-08-24 22:08:48 +00004600/* Flag used in compound items. */
4601typedef struct compitem_S
4602{
4603 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4604 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4605 int ci_newID; /* affix ID after renumbering. */
4606} compitem_T;
4607
4608#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4609
Bram Moolenaar51485f02005-06-04 21:55:20 +00004610/*
4611 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004612 * the need to keep track of each allocated thing, everything is freed all at
4613 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004614 */
4615#define SBLOCKSIZE 16000 /* size of sb_data */
4616typedef struct sblock_S sblock_T;
4617struct sblock_S
4618{
4619 sblock_T *sb_next; /* next block in list */
4620 int sb_used; /* nr of bytes already in use */
4621 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004622};
4623
4624/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004625 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004626 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004627typedef struct wordnode_S wordnode_T;
4628struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004629{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004630 union /* shared to save space */
4631 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004632 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004633 int index; /* index in written nodes (valid after first
4634 round) */
4635 } wn_u1;
4636 union /* shared to save space */
4637 {
4638 wordnode_T *next; /* next node with same hash key */
4639 wordnode_T *wnode; /* parent node that will write this node */
4640 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004641 wordnode_T *wn_child; /* child (next byte in word) */
4642 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4643 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004644 int wn_refs; /* Nr. of references to this node. Only
4645 relevant for first node in a list of
4646 siblings, in following siblings it is
4647 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004648 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004649
4650 /* Info for when "wn_byte" is NUL.
4651 * In PREFIXTREE "wn_region" is used for the prefcondnr.
4652 * In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
4653 * "wn_region" the LSW of the wordnr. */
4654 char_u wn_affixID; /* supported/required prefix ID or 0 */
4655 short_u wn_flags; /* WF_ flags */
4656 short wn_region; /* region mask */
4657
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004658#ifdef SPELL_PRINTTREE
4659 int wn_nr; /* sequence nr for printing */
4660#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004661};
4662
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004663#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4664
Bram Moolenaar51485f02005-06-04 21:55:20 +00004665#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004666
Bram Moolenaar51485f02005-06-04 21:55:20 +00004667/*
4668 * Info used while reading the spell files.
4669 */
4670typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004671{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004672 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004673 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004674
Bram Moolenaar51485f02005-06-04 21:55:20 +00004675 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004676 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004677
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004678 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004679
Bram Moolenaar4770d092006-01-12 23:22:24 +00004680 long si_sugtree; /* creating the soundfolding trie */
4681
Bram Moolenaar51485f02005-06-04 21:55:20 +00004682 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004683 long si_blocks_cnt; /* memory blocks allocated */
4684 long si_compress_cnt; /* words to add before lowering
4685 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004686 wordnode_T *si_first_free; /* List of nodes that have been freed during
4687 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004688 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004689#ifdef SPELL_PRINTTREE
4690 int si_wordnode_nr; /* sequence nr for nodes */
4691#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004692 buf_T *si_spellbuf; /* buffer used to store soundfold word table */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004693
Bram Moolenaar51485f02005-06-04 21:55:20 +00004694 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004695 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004696 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004697 int si_region; /* region mask */
4698 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004699 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004700 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004701 int si_msg_count; /* number of words added since last message */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004702 int si_region_count; /* number of regions supported (1 when there
4703 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004704 char_u si_region_name[16]; /* region names; used only if
4705 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004706
4707 garray_T si_rep; /* list of fromto_T entries from REP lines */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004708 garray_T si_repsal; /* list of fromto_T entries from REPSAL lines */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004709 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004710 char_u *si_sofofr; /* SOFOFROM text */
4711 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004712 int si_nosugfile; /* NOSUGFILE item found */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004713 int si_followup; /* soundsalike: ? */
4714 int si_collapse; /* soundsalike: ? */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004715 hashtab_T si_commonwords; /* hashtable for common words */
4716 time_t si_sugtime; /* timestamp for .sug file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004717 int si_rem_accents; /* soundsalike: remove accents */
4718 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004719 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004720 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004721 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004722 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004723 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004724 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004725 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004726 garray_T si_prefcond; /* table with conditions for postponed
4727 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004728 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004729 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004730} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004731
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004732static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004733static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
4734static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
4735static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004736static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004737static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
4738static void aff_check_number __ARGS((int spinval, int affval, char *name));
4739static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004740static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004741static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
4742static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00004743static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004744static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004745static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004746static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004747static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004748static 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 +00004749static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
4750static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
4751static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004752static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004753static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004754static 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 +00004755static 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 +00004756static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004757static int deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004758static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
4759static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
4760static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004761static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004762static void put_sugtime __ARGS((spellinfo_T *spin, FILE *fd));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004763static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00004764static void clear_node __ARGS((wordnode_T *node));
4765static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004766static void spell_make_sugfile __ARGS((spellinfo_T *spin, char_u *wfname));
4767static int sug_filltree __ARGS((spellinfo_T *spin, slang_T *slang));
4768static int sug_maketable __ARGS((spellinfo_T *spin));
4769static int sug_filltable __ARGS((spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap));
4770static int offset2bytes __ARGS((int nr, char_u *buf));
4771static int bytes2offset __ARGS((char_u **pp));
4772static void sug_write __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004773static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004774static void spell_message __ARGS((spellinfo_T *spin, char_u *str));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004775static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004776
Bram Moolenaar53805d12005-08-01 07:08:33 +00004777/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
4778 * but it must be negative to indicate the prefix tree to tree_add_word().
4779 * Use a negative number with the lower 8 bits zero. */
4780#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004781
Bram Moolenaar5195e452005-08-19 20:32:47 +00004782/*
4783 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
4784 */
4785static long compress_start = 30000; /* memory / SBLOCKSIZE */
4786static long compress_inc = 100; /* memory / SBLOCKSIZE */
4787static long compress_added = 500000; /* word count */
4788
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004789#ifdef SPELL_PRINTTREE
4790/*
4791 * For debugging the tree code: print the current tree in a (more or less)
4792 * readable format, so that we can see what happens when adding a word and/or
4793 * compressing the tree.
4794 * Based on code from Olaf Seibert.
4795 */
4796#define PRINTLINESIZE 1000
4797#define PRINTWIDTH 6
4798
4799#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
4800 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
4801
4802static char line1[PRINTLINESIZE];
4803static char line2[PRINTLINESIZE];
4804static char line3[PRINTLINESIZE];
4805
4806 static void
4807spell_clear_flags(wordnode_T *node)
4808{
4809 wordnode_T *np;
4810
4811 for (np = node; np != NULL; np = np->wn_sibling)
4812 {
4813 np->wn_u1.index = FALSE;
4814 spell_clear_flags(np->wn_child);
4815 }
4816}
4817
4818 static void
4819spell_print_node(wordnode_T *node, int depth)
4820{
4821 if (node->wn_u1.index)
4822 {
4823 /* Done this node before, print the reference. */
4824 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
4825 PRINTSOME(line2, depth, " ", 0, 0);
4826 PRINTSOME(line3, depth, " ", 0, 0);
4827 msg(line1);
4828 msg(line2);
4829 msg(line3);
4830 }
4831 else
4832 {
4833 node->wn_u1.index = TRUE;
4834
4835 if (node->wn_byte != NUL)
4836 {
4837 if (node->wn_child != NULL)
4838 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
4839 else
4840 /* Cannot happen? */
4841 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
4842 }
4843 else
4844 PRINTSOME(line1, depth, " $ ", 0, 0);
4845
4846 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
4847
4848 if (node->wn_sibling != NULL)
4849 PRINTSOME(line3, depth, " | ", 0, 0);
4850 else
4851 PRINTSOME(line3, depth, " ", 0, 0);
4852
4853 if (node->wn_byte == NUL)
4854 {
4855 msg(line1);
4856 msg(line2);
4857 msg(line3);
4858 }
4859
4860 /* do the children */
4861 if (node->wn_byte != NUL && node->wn_child != NULL)
4862 spell_print_node(node->wn_child, depth + 1);
4863
4864 /* do the siblings */
4865 if (node->wn_sibling != NULL)
4866 {
4867 /* get rid of all parent details except | */
4868 STRCPY(line1, line3);
4869 STRCPY(line2, line3);
4870 spell_print_node(node->wn_sibling, depth);
4871 }
4872 }
4873}
4874
4875 static void
4876spell_print_tree(wordnode_T *root)
4877{
4878 if (root != NULL)
4879 {
4880 /* Clear the "wn_u1.index" fields, used to remember what has been
4881 * done. */
4882 spell_clear_flags(root);
4883
4884 /* Recursively print the tree. */
4885 spell_print_node(root, 0);
4886 }
4887}
4888#endif /* SPELL_PRINTTREE */
4889
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004890/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004891 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00004892 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004893 */
4894 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004895spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004896 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004897 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004898{
4899 FILE *fd;
4900 afffile_T *aff;
4901 char_u rline[MAXLINELEN];
4902 char_u *line;
4903 char_u *pc = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004904#define MAXITEMCNT 30
Bram Moolenaar8db73182005-06-17 21:51:16 +00004905 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004906 int itemcnt;
4907 char_u *p;
4908 int lnum = 0;
4909 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004910 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004911 int aff_todo = 0;
4912 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004913 char_u *low = NULL;
4914 char_u *fol = NULL;
4915 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004916 int do_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004917 int do_repsal;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004918 int do_sal;
4919 int do_map;
4920 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004921 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004922 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004923 int compminlen = 0; /* COMPOUNDMIN value */
4924 int compsylmax = 0; /* COMPOUNDSYLMAX value */
4925 int compmax = 0; /* COMPOUNDMAX value */
4926 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDFLAGS
4927 concatenated */
4928 char_u *midword = NULL; /* MIDWORD value */
4929 char_u *syllable = NULL; /* SYLLABLE value */
4930 char_u *sofofrom = NULL; /* SOFOFROM value */
4931 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004932
Bram Moolenaar51485f02005-06-04 21:55:20 +00004933 /*
4934 * Open the file.
4935 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004936 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004937 if (fd == NULL)
4938 {
4939 EMSG2(_(e_notopen), fname);
4940 return NULL;
4941 }
4942
Bram Moolenaar4770d092006-01-12 23:22:24 +00004943 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s ..."), fname);
4944 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004945
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004946 /* Only do REP lines when not done in another .aff file already. */
4947 do_rep = spin->si_rep.ga_len == 0;
4948
Bram Moolenaar4770d092006-01-12 23:22:24 +00004949 /* Only do REPSAL lines when not done in another .aff file already. */
4950 do_repsal = spin->si_repsal.ga_len == 0;
4951
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004952 /* Only do SAL lines when not done in another .aff file already. */
4953 do_sal = spin->si_sal.ga_len == 0;
4954
4955 /* Only do MAP lines when not done in another .aff file already. */
4956 do_map = spin->si_map.ga_len == 0;
4957
Bram Moolenaar51485f02005-06-04 21:55:20 +00004958 /*
4959 * Allocate and init the afffile_T structure.
4960 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004961 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004962 if (aff == NULL)
4963 return NULL;
4964 hash_init(&aff->af_pref);
4965 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004966 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004967
4968 /*
4969 * Read all the lines in the file one by one.
4970 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004971 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004972 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004973 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004974 ++lnum;
4975
4976 /* Skip comment lines. */
4977 if (*rline == '#')
4978 continue;
4979
4980 /* Convert from "SET" to 'encoding' when needed. */
4981 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004982#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004983 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004984 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004985 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004986 if (pc == NULL)
4987 {
4988 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4989 fname, lnum, rline);
4990 continue;
4991 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004992 line = pc;
4993 }
4994 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004995#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004996 {
4997 pc = NULL;
4998 line = rline;
4999 }
5000
5001 /* Split the line up in white separated items. Put a NUL after each
5002 * item. */
5003 itemcnt = 0;
5004 for (p = line; ; )
5005 {
5006 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
5007 ++p;
5008 if (*p == NUL)
5009 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00005010 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005011 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005012 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005013 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005014 ++p;
5015 if (*p == NUL)
5016 break;
5017 *p++ = NUL;
5018 }
5019
5020 /* Handle non-empty lines. */
5021 if (itemcnt > 0)
5022 {
5023 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
5024 && aff->af_enc == NULL)
5025 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005026#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005027 /* Setup for conversion from "ENC" to 'encoding'. */
5028 aff->af_enc = enc_canonize(items[1]);
5029 if (aff->af_enc != NULL && !spin->si_ascii
5030 && convert_setup(&spin->si_conv, aff->af_enc,
5031 p_enc) == FAIL)
5032 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
5033 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005034 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005035#else
5036 smsg((char_u *)_("Conversion in %s not supported"), fname);
5037#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005038 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005039 else if (STRCMP(items[0], "FLAG") == 0 && itemcnt == 2
5040 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005041 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005042 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005043 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005044 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005045 aff->af_flagtype = AFT_NUM;
5046 else if (STRCMP(items[1], "caplong") == 0)
5047 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005048 else
5049 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
5050 fname, lnum, items[1]);
Bram Moolenaar371baa92005-12-29 22:43:53 +00005051 if (aff->af_rare != 0
5052 || aff->af_keepcase != 0
5053 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005054 || aff->af_needaffix != 0
5055 || aff->af_needcomp != 0
5056 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00005057 || aff->af_suff.ht_used > 0
5058 || aff->af_pref.ht_used > 0)
5059 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
5060 fname, lnum, items[1]);
5061 }
5062 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2
5063 && midword == NULL)
5064 {
5065 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005066 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00005067 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
5068 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005069 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00005070 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005071 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005072 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005073 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005074 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005075 /* TODO: remove "RAR" later */
5076 else if ((STRCMP(items[0], "RAR") == 0
5077 || STRCMP(items[0], "RARE") == 0) && itemcnt == 2
5078 && aff->af_rare == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005079 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005080 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005081 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005082 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005083 /* TODO: remove "KEP" later */
5084 else if ((STRCMP(items[0], "KEP") == 0
5085 || STRCMP(items[0], "KEEPCASE") == 0) && itemcnt == 2
5086 && aff->af_keepcase == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005087 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005088 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005089 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005090 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00005091 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
5092 && aff->af_bad == 0)
5093 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005094 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
5095 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005096 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005097 else if (STRCMP(items[0], "NEEDAFFIX") == 0 && itemcnt == 2
5098 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005099 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005100 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
5101 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005102 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005103 else if (STRCMP(items[0], "NEEDCOMPOUND") == 0 && itemcnt == 2
5104 && aff->af_needcomp == 0)
5105 {
5106 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
5107 fname, lnum);
5108 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005109 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005110 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005111 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005112 /* Turn flag "c" into COMPOUNDFLAGS compatible string "c+",
5113 * "Na" into "Na+", "1234" into "1234+". */
5114 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005115 if (p != NULL)
5116 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005117 STRCPY(p, items[1]);
5118 STRCAT(p, "+");
5119 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005120 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005121 }
5122 else if (STRCMP(items[0], "COMPOUNDFLAGS") == 0 && itemcnt == 2)
5123 {
5124 /* Concatenate this string to previously defined ones, using a
5125 * slash to separate them. */
5126 l = STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005127 if (compflags != NULL)
5128 l += STRLEN(compflags) + 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005129 p = getroom(spin, l, FALSE);
5130 if (p != NULL)
5131 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005132 if (compflags != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005133 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005134 STRCPY(p, compflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005135 STRCAT(p, "/");
5136 }
5137 STRCAT(p, items[1]);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005138 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005139 }
5140 }
5141 else if (STRCMP(items[0], "COMPOUNDMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005142 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005143 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005144 compmax = atoi((char *)items[1]);
5145 if (compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005146 smsg((char_u *)_("Wrong COMPOUNDMAX value in %s line %d: %s"),
5147 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005148 }
5149 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005150 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005151 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005152 compminlen = atoi((char *)items[1]);
5153 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005154 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
5155 fname, lnum, items[1]);
5156 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005157 else if (STRCMP(items[0], "COMPOUNDSYLMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005158 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005159 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005160 compsylmax = atoi((char *)items[1]);
5161 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005162 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
5163 fname, lnum, items[1]);
5164 }
5165 else if (STRCMP(items[0], "SYLLABLE") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005166 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005167 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005168 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005169 }
Bram Moolenaar78622822005-08-23 21:00:13 +00005170 else if (STRCMP(items[0], "NOBREAK") == 0 && itemcnt == 1)
5171 {
5172 spin->si_nobreak = TRUE;
5173 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005174 else if (STRCMP(items[0], "NOSUGFILE") == 0 && itemcnt == 1)
5175 {
5176 spin->si_nosugfile = TRUE;
5177 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005178 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
5179 {
5180 aff->af_pfxpostpone = TRUE;
5181 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005182 else if ((STRCMP(items[0], "PFX") == 0
5183 || STRCMP(items[0], "SFX") == 0)
5184 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005185 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005186 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005187 int lasti = 4;
5188 char_u key[AH_KEY_LEN];
5189
5190 if (*items[0] == 'P')
5191 tp = &aff->af_pref;
5192 else
5193 tp = &aff->af_suff;
5194
5195 /* Myspell allows the same affix name to be used multiple
5196 * times. The affix files that do this have an undocumented
5197 * "S" flag on all but the last block, thus we check for that
5198 * and store it in ah_follows. */
5199 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
5200 hi = hash_find(tp, key);
5201 if (!HASHITEM_EMPTY(hi))
5202 {
5203 cur_aff = HI2AH(hi);
5204 if (cur_aff->ah_combine != (*items[2] == 'Y'))
5205 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
5206 fname, lnum, items[1]);
5207 if (!cur_aff->ah_follows)
5208 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
5209 fname, lnum, items[1]);
5210 }
5211 else
5212 {
5213 /* New affix letter. */
5214 cur_aff = (affheader_T *)getroom(spin,
5215 sizeof(affheader_T), TRUE);
5216 if (cur_aff == NULL)
5217 break;
5218 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
5219 fname, lnum);
5220 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
5221 break;
5222 if (cur_aff->ah_flag == aff->af_bad
Bram Moolenaar371baa92005-12-29 22:43:53 +00005223 || cur_aff->ah_flag == aff->af_rare
5224 || cur_aff->ah_flag == aff->af_keepcase
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005225 || cur_aff->ah_flag == aff->af_needaffix
5226 || cur_aff->ah_flag == aff->af_needcomp)
Bram Moolenaar371baa92005-12-29 22:43:53 +00005227 smsg((char_u *)_("Affix also used for BAD/RARE/KEEPCASE/NEEDAFFIX/NEEDCOMPOUND in %s line %d: %s"),
Bram Moolenaar95529562005-08-25 21:21:38 +00005228 fname, lnum, items[1]);
5229 STRCPY(cur_aff->ah_key, items[1]);
5230 hash_add(tp, cur_aff->ah_key);
5231
5232 cur_aff->ah_combine = (*items[2] == 'Y');
5233 }
5234
5235 /* Check for the "S" flag, which apparently means that another
5236 * block with the same affix name is following. */
5237 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
5238 {
5239 ++lasti;
5240 cur_aff->ah_follows = TRUE;
5241 }
5242 else
5243 cur_aff->ah_follows = FALSE;
5244
Bram Moolenaar8db73182005-06-17 21:51:16 +00005245 /* Myspell allows extra text after the item, but that might
5246 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00005247 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar8db73182005-06-17 21:51:16 +00005248 smsg((char_u *)_("Trailing text in %s line %d: %s"),
5249 fname, lnum, items[4]);
5250
Bram Moolenaar95529562005-08-25 21:21:38 +00005251 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005252 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
5253 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005254
Bram Moolenaar95529562005-08-25 21:21:38 +00005255 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005256 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005257 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005258 {
5259 /* Use a new number in the .spl file later, to be able
5260 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005261 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005262 cur_aff->ah_newID = ++spin->si_newprefID;
5263
5264 /* We only really use ah_newID if the prefix is
5265 * postponed. We know that only after handling all
5266 * the items. */
5267 did_postpone_prefix = FALSE;
5268 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005269 else
5270 /* Did use the ID in a previous block. */
5271 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005272 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005273
Bram Moolenaar51485f02005-06-04 21:55:20 +00005274 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005275 }
5276 else if ((STRCMP(items[0], "PFX") == 0
5277 || STRCMP(items[0], "SFX") == 0)
5278 && aff_todo > 0
5279 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005280 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005281 {
5282 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005283 int rare = FALSE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005284 int nocomp = FALSE;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005285 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005286 int lasti = 5;
5287
Bram Moolenaar5195e452005-08-19 20:32:47 +00005288 /* Check for "rare" and "nocomp" after the other info. */
5289 while (itemcnt > lasti)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005290 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00005291 if (!rare && STRICMP(items[lasti], "rare") == 0)
5292 {
5293 rare = TRUE;
5294 ++lasti;
5295 }
5296 else if (!nocomp && STRICMP(items[lasti], "nocomp") == 0)
5297 {
5298 nocomp = TRUE;
5299 ++lasti;
5300 }
5301 else
5302 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005303 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005304
Bram Moolenaar8db73182005-06-17 21:51:16 +00005305 /* Myspell allows extra text after the item, but that might
5306 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005307 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005308 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005309
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005310 /* New item for an affix letter. */
5311 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005312 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005313 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005314 if (aff_entry == NULL)
5315 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005316 aff_entry->ae_rare = rare;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005317 aff_entry->ae_nocomp = nocomp;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005318
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005319 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005320 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005321 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005322 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005323
Bram Moolenaar51485f02005-06-04 21:55:20 +00005324 /* Don't use an affix entry with non-ASCII characters when
5325 * "spin->si_ascii" is TRUE. */
5326 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005327 || has_non_ascii(aff_entry->ae_add)))
5328 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00005329 aff_entry->ae_next = cur_aff->ah_first;
5330 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005331
5332 if (STRCMP(items[4], ".") != 0)
5333 {
5334 char_u buf[MAXLINELEN];
5335
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005336 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005337 if (*items[0] == 'P')
5338 sprintf((char *)buf, "^%s", items[4]);
5339 else
5340 sprintf((char *)buf, "%s$", items[4]);
5341 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005342 RE_MAGIC + RE_STRING + RE_STRICT);
5343 if (aff_entry->ae_prog == NULL)
5344 smsg((char_u *)_("Broken condition in %s line %d: %s"),
5345 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005346 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005347
5348 /* For postponed prefixes we need an entry in si_prefcond
5349 * for the condition. Use an existing one if possible. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005350 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005351 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005352 /* When the chop string is one lower-case letter and
5353 * the add string ends in the upper-case letter we set
5354 * the "upper" flag, clear "ae_chop" and remove the
5355 * letters from "ae_add". The condition must either
5356 * be empty or start with the same letter. */
5357 if (aff_entry->ae_chop != NULL
5358 && aff_entry->ae_add != NULL
5359#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005360 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00005361 aff_entry->ae_chop)] == NUL
5362#else
5363 && aff_entry->ae_chop[1] == NUL
5364#endif
5365 )
5366 {
5367 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005368
Bram Moolenaar53805d12005-08-01 07:08:33 +00005369 c = PTR2CHAR(aff_entry->ae_chop);
5370 c_up = SPELL_TOUPPER(c);
5371 if (c_up != c
5372 && (aff_entry->ae_cond == NULL
5373 || PTR2CHAR(aff_entry->ae_cond) == c))
5374 {
5375 p = aff_entry->ae_add
5376 + STRLEN(aff_entry->ae_add);
5377 mb_ptr_back(aff_entry->ae_add, p);
5378 if (PTR2CHAR(p) == c_up)
5379 {
5380 upper = TRUE;
5381 aff_entry->ae_chop = NULL;
5382 *p = NUL;
5383
5384 /* The condition is matched with the
5385 * actual word, thus must check for the
5386 * upper-case letter. */
5387 if (aff_entry->ae_cond != NULL)
5388 {
5389 char_u buf[MAXLINELEN];
5390#ifdef FEAT_MBYTE
5391 if (has_mbyte)
5392 {
5393 onecap_copy(items[4], buf, TRUE);
5394 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005395 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005396 }
5397 else
5398#endif
5399 *aff_entry->ae_cond = c_up;
5400 if (aff_entry->ae_cond != NULL)
5401 {
5402 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005403 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005404 vim_free(aff_entry->ae_prog);
5405 aff_entry->ae_prog = vim_regcomp(
5406 buf, RE_MAGIC + RE_STRING);
5407 }
5408 }
5409 }
5410 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005411 }
5412
Bram Moolenaar53805d12005-08-01 07:08:33 +00005413 if (aff_entry->ae_chop == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005414 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005415 int idx;
5416 char_u **pp;
5417 int n;
5418
Bram Moolenaar6de68532005-08-24 22:08:48 +00005419 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005420 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
5421 --idx)
5422 {
5423 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
5424 if (str_equal(p, aff_entry->ae_cond))
5425 break;
5426 }
5427 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5428 {
5429 /* Not found, add a new condition. */
5430 idx = spin->si_prefcond.ga_len++;
5431 pp = ((char_u **)spin->si_prefcond.ga_data)
5432 + idx;
5433 if (aff_entry->ae_cond == NULL)
5434 *pp = NULL;
5435 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005436 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00005437 aff_entry->ae_cond);
5438 }
5439
5440 /* Add the prefix to the prefix tree. */
5441 if (aff_entry->ae_add == NULL)
5442 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005443 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005444 p = aff_entry->ae_add;
5445 /* PFX_FLAGS is a negative number, so that
5446 * tree_add_word() knows this is the prefix tree. */
5447 n = PFX_FLAGS;
5448 if (rare)
5449 n |= WFP_RARE;
5450 if (!cur_aff->ah_combine)
5451 n |= WFP_NC;
5452 if (upper)
5453 n |= WFP_UP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005454 tree_add_word(spin, p, spin->si_prefroot, n,
5455 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005456 did_postpone_prefix = TRUE;
5457 }
5458
5459 /* Didn't actually use ah_newID, backup si_newprefID. */
5460 if (aff_todo == 0 && !did_postpone_prefix)
5461 {
5462 --spin->si_newprefID;
5463 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005464 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005465 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005466 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005467 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005468 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2
5469 && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005470 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005471 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005472 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005473 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2
5474 && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005475 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005476 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005477 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005478 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2
5479 && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005480 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005481 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005482 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005483 else if ((STRCMP(items[0], "REP") == 0
5484 || STRCMP(items[0], "REPSAL") == 0)
5485 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005486 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005487 /* Ignore REP/REPSAL count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005488 if (!isdigit(*items[1]))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005489 smsg((char_u *)_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005490 fname, lnum);
5491 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005492 else if ((STRCMP(items[0], "REP") == 0
5493 || STRCMP(items[0], "REPSAL") == 0)
5494 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005495 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005496 /* REP/REPSAL item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005497 /* Myspell ignores extra arguments, we require it starts with
5498 * # to detect mistakes. */
5499 if (itemcnt > 3 && items[3][0] != '#')
5500 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005501 if (items[0][3] == 'S' ? do_repsal : do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005502 {
5503 /* Replace underscore with space (can't include a space
5504 * directly). */
5505 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5506 if (*p == '_')
5507 *p = ' ';
5508 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5509 if (*p == '_')
5510 *p = ' ';
Bram Moolenaar4770d092006-01-12 23:22:24 +00005511 add_fromto(spin, items[0][3] == 'S'
5512 ? &spin->si_repsal
5513 : &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005514 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005515 }
5516 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
5517 {
5518 /* MAP item or count */
5519 if (!found_map)
5520 {
5521 /* First line contains the count. */
5522 found_map = TRUE;
5523 if (!isdigit(*items[1]))
5524 smsg((char_u *)_("Expected MAP count in %s line %d"),
5525 fname, lnum);
5526 }
5527 else if (do_map)
5528 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005529 int c;
5530
5531 /* Check that every character appears only once. */
5532 for (p = items[1]; *p != NUL; )
5533 {
5534#ifdef FEAT_MBYTE
5535 c = mb_ptr2char_adv(&p);
5536#else
5537 c = *p++;
5538#endif
5539 if ((spin->si_map.ga_len > 0
5540 && vim_strchr(spin->si_map.ga_data, c)
5541 != NULL)
5542 || vim_strchr(p, c) != NULL)
5543 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5544 fname, lnum);
5545 }
5546
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005547 /* We simply concatenate all the MAP strings, separated by
5548 * slashes. */
5549 ga_concat(&spin->si_map, items[1]);
5550 ga_append(&spin->si_map, '/');
5551 }
5552 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005553 /* Accept "SAL from to" and "SAL from to # comment". */
5554 else if (STRCMP(items[0], "SAL") == 0
5555 && (itemcnt == 3 || (itemcnt > 3 && items[3][0] == '#')))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005556 {
5557 if (do_sal)
5558 {
5559 /* SAL item (sounds-a-like)
5560 * Either one of the known keys or a from-to pair. */
5561 if (STRCMP(items[1], "followup") == 0)
5562 spin->si_followup = sal_to_bool(items[2]);
5563 else if (STRCMP(items[1], "collapse_result") == 0)
5564 spin->si_collapse = sal_to_bool(items[2]);
5565 else if (STRCMP(items[1], "remove_accents") == 0)
5566 spin->si_rem_accents = sal_to_bool(items[2]);
5567 else
5568 /* when "to" is "_" it means empty */
5569 add_fromto(spin, &spin->si_sal, items[1],
5570 STRCMP(items[2], "_") == 0 ? (char_u *)""
5571 : items[2]);
5572 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005573 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005574 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005575 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005576 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005577 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005578 }
5579 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005580 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005581 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005582 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005583 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005584 else if (STRCMP(items[0], "COMMON") == 0)
5585 {
5586 int i;
5587
5588 for (i = 1; i < itemcnt; ++i)
5589 {
5590 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
5591 items[i])))
5592 {
5593 p = vim_strsave(items[i]);
5594 if (p == NULL)
5595 break;
5596 hash_add(&spin->si_commonwords, p);
5597 }
5598 }
5599 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005600 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005601 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005602 fname, lnum, items[0]);
5603 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005604 }
5605
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005606 if (fol != NULL || low != NULL || upp != NULL)
5607 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005608 if (spin->si_clear_chartab)
5609 {
5610 /* Clear the char type tables, don't want to use any of the
5611 * currently used spell properties. */
5612 init_spell_chartab();
5613 spin->si_clear_chartab = FALSE;
5614 }
5615
Bram Moolenaar3982c542005-06-08 21:56:31 +00005616 /*
5617 * Don't write a word table for an ASCII file, so that we don't check
5618 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005619 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00005620 * mb_get_class(), the list of chars in the file will be incomplete.
5621 */
5622 if (!spin->si_ascii
5623#ifdef FEAT_MBYTE
5624 && !enc_utf8
5625#endif
5626 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005627 {
5628 if (fol == NULL || low == NULL || upp == NULL)
5629 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
5630 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00005631 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005632 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005633
5634 vim_free(fol);
5635 vim_free(low);
5636 vim_free(upp);
5637 }
5638
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005639 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005640 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005641 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005642 aff_check_number(spin->si_compmax, compmax, "COMPOUNDMAX");
5643 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005644 }
5645
Bram Moolenaar6de68532005-08-24 22:08:48 +00005646 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005647 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005648 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
5649 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005650 }
5651
Bram Moolenaar6de68532005-08-24 22:08:48 +00005652 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005653 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005654 if (syllable == NULL)
5655 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
5656 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
5657 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005658 }
5659
Bram Moolenaar6de68532005-08-24 22:08:48 +00005660 if (compflags != NULL)
5661 process_compflags(spin, aff, compflags);
5662
5663 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005664 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005665 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005666 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005667 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005668 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005669 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005670 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005671 MSG(_("Too many posponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005672 }
5673
Bram Moolenaar6de68532005-08-24 22:08:48 +00005674 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005675 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005676 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
5677 spin->si_syllable = syllable;
5678 }
5679
5680 if (sofofrom != NULL || sofoto != NULL)
5681 {
5682 if (sofofrom == NULL || sofoto == NULL)
5683 smsg((char_u *)_("Missing SOFO%s line in %s"),
5684 sofofrom == NULL ? "FROM" : "TO", fname);
5685 else if (spin->si_sal.ga_len > 0)
5686 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005687 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005688 {
5689 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
5690 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
5691 spin->si_sofofr = sofofrom;
5692 spin->si_sofoto = sofoto;
5693 }
5694 }
5695
5696 if (midword != NULL)
5697 {
5698 aff_check_string(spin->si_midword, midword, "MIDWORD");
5699 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005700 }
5701
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005702 vim_free(pc);
5703 fclose(fd);
5704 return aff;
5705}
5706
5707/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005708 * Turn an affix flag name into a number, according to the FLAG type.
5709 * returns zero for failure.
5710 */
5711 static unsigned
5712affitem2flag(flagtype, item, fname, lnum)
5713 int flagtype;
5714 char_u *item;
5715 char_u *fname;
5716 int lnum;
5717{
5718 unsigned res;
5719 char_u *p = item;
5720
5721 res = get_affitem(flagtype, &p);
5722 if (res == 0)
5723 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005724 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005725 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
5726 fname, lnum, item);
5727 else
5728 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
5729 fname, lnum, item);
5730 }
5731 if (*p != NUL)
5732 {
5733 smsg((char_u *)_(e_affname), fname, lnum, item);
5734 return 0;
5735 }
5736
5737 return res;
5738}
5739
5740/*
5741 * Get one affix name from "*pp" and advance the pointer.
5742 * Returns zero for an error, still advances the pointer then.
5743 */
5744 static unsigned
5745get_affitem(flagtype, pp)
5746 int flagtype;
5747 char_u **pp;
5748{
5749 int res;
5750
Bram Moolenaar95529562005-08-25 21:21:38 +00005751 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005752 {
5753 if (!VIM_ISDIGIT(**pp))
5754 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005755 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005756 return 0;
5757 }
5758 res = getdigits(pp);
5759 }
5760 else
5761 {
5762#ifdef FEAT_MBYTE
5763 res = mb_ptr2char_adv(pp);
5764#else
5765 res = *(*pp)++;
5766#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00005767 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00005768 && res >= 'A' && res <= 'Z'))
5769 {
5770 if (**pp == NUL)
5771 return 0;
5772#ifdef FEAT_MBYTE
5773 res = mb_ptr2char_adv(pp) + (res << 16);
5774#else
5775 res = *(*pp)++ + (res << 16);
5776#endif
5777 }
5778 }
5779 return res;
5780}
5781
5782/*
5783 * Process the "compflags" string used in an affix file and append it to
5784 * spin->si_compflags.
5785 * The processing involves changing the affix names to ID numbers, so that
5786 * they fit in one byte.
5787 */
5788 static void
5789process_compflags(spin, aff, compflags)
5790 spellinfo_T *spin;
5791 afffile_T *aff;
5792 char_u *compflags;
5793{
5794 char_u *p;
5795 char_u *prevp;
5796 unsigned flag;
5797 compitem_T *ci;
5798 int id;
5799 int len;
5800 char_u *tp;
5801 char_u key[AH_KEY_LEN];
5802 hashitem_T *hi;
5803
5804 /* Make room for the old and the new compflags, concatenated with a / in
5805 * between. Processing it makes it shorter, but we don't know by how
5806 * much, thus allocate the maximum. */
5807 len = STRLEN(compflags) + 1;
5808 if (spin->si_compflags != NULL)
5809 len += STRLEN(spin->si_compflags) + 1;
5810 p = getroom(spin, len, FALSE);
5811 if (p == NULL)
5812 return;
5813 if (spin->si_compflags != NULL)
5814 {
5815 STRCPY(p, spin->si_compflags);
5816 STRCAT(p, "/");
5817 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005818 spin->si_compflags = p;
5819 tp = p + STRLEN(p);
5820
5821 for (p = compflags; *p != NUL; )
5822 {
5823 if (vim_strchr((char_u *)"/*+[]", *p) != NULL)
5824 /* Copy non-flag characters directly. */
5825 *tp++ = *p++;
5826 else
5827 {
5828 /* First get the flag number, also checks validity. */
5829 prevp = p;
5830 flag = get_affitem(aff->af_flagtype, &p);
5831 if (flag != 0)
5832 {
5833 /* Find the flag in the hashtable. If it was used before, use
5834 * the existing ID. Otherwise add a new entry. */
5835 vim_strncpy(key, prevp, p - prevp);
5836 hi = hash_find(&aff->af_comp, key);
5837 if (!HASHITEM_EMPTY(hi))
5838 id = HI2CI(hi)->ci_newID;
5839 else
5840 {
5841 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
5842 if (ci == NULL)
5843 break;
5844 STRCPY(ci->ci_key, key);
5845 ci->ci_flag = flag;
5846 /* Avoid using a flag ID that has a special meaning in a
5847 * regexp (also inside []). */
5848 do
5849 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005850 check_renumber(spin);
5851 id = spin->si_newcompID--;
5852 } while (vim_strchr((char_u *)"/+*[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005853 ci->ci_newID = id;
5854 hash_add(&aff->af_comp, ci->ci_key);
5855 }
5856 *tp++ = id;
5857 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005858 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00005859 ++p;
5860 }
5861 }
5862
5863 *tp = NUL;
5864}
5865
5866/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005867 * Check that the new IDs for postponed affixes and compounding don't overrun
5868 * each other. We have almost 255 available, but start at 0-127 to avoid
5869 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
5870 * When that is used up an error message is given.
5871 */
5872 static void
5873check_renumber(spin)
5874 spellinfo_T *spin;
5875{
5876 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
5877 {
5878 spin->si_newprefID = 127;
5879 spin->si_newcompID = 255;
5880 }
5881}
5882
5883/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005884 * Return TRUE if flag "flag" appears in affix list "afflist".
5885 */
5886 static int
5887flag_in_afflist(flagtype, afflist, flag)
5888 int flagtype;
5889 char_u *afflist;
5890 unsigned flag;
5891{
5892 char_u *p;
5893 unsigned n;
5894
5895 switch (flagtype)
5896 {
5897 case AFT_CHAR:
5898 return vim_strchr(afflist, flag) != NULL;
5899
Bram Moolenaar95529562005-08-25 21:21:38 +00005900 case AFT_CAPLONG:
5901 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00005902 for (p = afflist; *p != NUL; )
5903 {
5904#ifdef FEAT_MBYTE
5905 n = mb_ptr2char_adv(&p);
5906#else
5907 n = *p++;
5908#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00005909 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00005910 && *p != NUL)
5911#ifdef FEAT_MBYTE
5912 n = mb_ptr2char_adv(&p) + (n << 16);
5913#else
5914 n = *p++ + (n << 16);
5915#endif
5916 if (n == flag)
5917 return TRUE;
5918 }
5919 break;
5920
Bram Moolenaar95529562005-08-25 21:21:38 +00005921 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00005922 for (p = afflist; *p != NUL; )
5923 {
5924 n = getdigits(&p);
5925 if (n == flag)
5926 return TRUE;
5927 if (*p != NUL) /* skip over comma */
5928 ++p;
5929 }
5930 break;
5931 }
5932 return FALSE;
5933}
5934
5935/*
5936 * Give a warning when "spinval" and "affval" numbers are set and not the same.
5937 */
5938 static void
5939aff_check_number(spinval, affval, name)
5940 int spinval;
5941 int affval;
5942 char *name;
5943{
5944 if (spinval != 0 && spinval != affval)
5945 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
5946}
5947
5948/*
5949 * Give a warning when "spinval" and "affval" strings are set and not the same.
5950 */
5951 static void
5952aff_check_string(spinval, affval, name)
5953 char_u *spinval;
5954 char_u *affval;
5955 char *name;
5956{
5957 if (spinval != NULL && STRCMP(spinval, affval) != 0)
5958 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
5959}
5960
5961/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005962 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
5963 * NULL as equal.
5964 */
5965 static int
5966str_equal(s1, s2)
5967 char_u *s1;
5968 char_u *s2;
5969{
5970 if (s1 == NULL || s2 == NULL)
5971 return s1 == s2;
5972 return STRCMP(s1, s2) == 0;
5973}
5974
5975/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005976 * Add a from-to item to "gap". Used for REP and SAL items.
5977 * They are stored case-folded.
5978 */
5979 static void
5980add_fromto(spin, gap, from, to)
5981 spellinfo_T *spin;
5982 garray_T *gap;
5983 char_u *from;
5984 char_u *to;
5985{
5986 fromto_T *ftp;
5987 char_u word[MAXWLEN];
5988
5989 if (ga_grow(gap, 1) == OK)
5990 {
5991 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
5992 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005993 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005994 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005995 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005996 ++gap->ga_len;
5997 }
5998}
5999
6000/*
6001 * Convert a boolean argument in a SAL line to TRUE or FALSE;
6002 */
6003 static int
6004sal_to_bool(s)
6005 char_u *s;
6006{
6007 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
6008}
6009
6010/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00006011 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6012 * When "s" is NULL FALSE is returned.
6013 */
6014 static int
6015has_non_ascii(s)
6016 char_u *s;
6017{
6018 char_u *p;
6019
6020 if (s != NULL)
6021 for (p = s; *p != NUL; ++p)
6022 if (*p >= 128)
6023 return TRUE;
6024 return FALSE;
6025}
6026
6027/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006028 * Free the structure filled by spell_read_aff().
6029 */
6030 static void
6031spell_free_aff(aff)
6032 afffile_T *aff;
6033{
6034 hashtab_T *ht;
6035 hashitem_T *hi;
6036 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006037 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006038 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006039
6040 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006041
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006042 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006043 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
6044 {
6045 todo = ht->ht_used;
6046 for (hi = ht->ht_array; todo > 0; ++hi)
6047 {
6048 if (!HASHITEM_EMPTY(hi))
6049 {
6050 --todo;
6051 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006052 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
6053 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006054 }
6055 }
6056 if (ht == &aff->af_suff)
6057 break;
6058 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006059
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006060 hash_clear(&aff->af_pref);
6061 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006062 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006063}
6064
6065/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006066 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006067 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006068 */
6069 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006070spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006071 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006072 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006073 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006074{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006075 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006076 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006077 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006078 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006079 char_u store_afflist[MAXWLEN];
6080 int pfxlen;
6081 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006082 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006083 char_u *pc;
6084 char_u *w;
6085 int l;
6086 hash_T hash;
6087 hashitem_T *hi;
6088 FILE *fd;
6089 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006090 int non_ascii = 0;
6091 int retval = OK;
6092 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006093 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006094 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006095
Bram Moolenaar51485f02005-06-04 21:55:20 +00006096 /*
6097 * Open the file.
6098 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006099 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006100 if (fd == NULL)
6101 {
6102 EMSG2(_(e_notopen), fname);
6103 return FAIL;
6104 }
6105
Bram Moolenaar51485f02005-06-04 21:55:20 +00006106 /* The hashtable is only used to detect duplicated words. */
6107 hash_init(&ht);
6108
Bram Moolenaar4770d092006-01-12 23:22:24 +00006109 vim_snprintf((char *)IObuff, IOSIZE,
6110 _("Reading dictionary file %s ..."), fname);
6111 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006112
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006113 /* start with a message for the first line */
6114 spin->si_msg_count = 999999;
6115
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006116 /* Read and ignore the first line: word count. */
6117 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006118 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006119 EMSG2(_("E760: No word count in %s"), fname);
6120
6121 /*
6122 * Read all the lines in the file one by one.
6123 * The words are converted to 'encoding' here, before being added to
6124 * the hashtable.
6125 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006126 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006127 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006128 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006129 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00006130 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006131 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006132
Bram Moolenaar51485f02005-06-04 21:55:20 +00006133 /* Remove CR, LF and white space from the end. White space halfway
6134 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006135 l = STRLEN(line);
6136 while (l > 0 && line[l - 1] <= ' ')
6137 --l;
6138 if (l == 0)
6139 continue; /* empty line */
6140 line[l] = NUL;
6141
Bram Moolenaar66fa2712006-01-22 23:22:22 +00006142 /* Truncate the word at the "/", set "afflist" to what follows.
6143 * Replace "\/" by "/" and "\\" by "\". */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006144 afflist = NULL;
6145 for (p = line; *p != NUL; mb_ptr_adv(p))
6146 {
Bram Moolenaar66fa2712006-01-22 23:22:22 +00006147 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
6148 mch_memmove(p, p + 1, STRLEN(p));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006149 else if (*p == '/')
6150 {
6151 *p = NUL;
6152 afflist = p + 1;
6153 break;
6154 }
6155 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006156
6157 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6158 if (spin->si_ascii && has_non_ascii(line))
6159 {
6160 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00006161 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006162 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00006163
Bram Moolenaarb765d632005-06-07 21:00:02 +00006164#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006165 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006166 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006167 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006168 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006169 if (pc == NULL)
6170 {
6171 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6172 fname, lnum, line);
6173 continue;
6174 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006175 w = pc;
6176 }
6177 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006178#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006179 {
6180 pc = NULL;
6181 w = line;
6182 }
6183
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006184 /* This takes time, print a message every 10000 words. */
6185 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006186 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006187 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006188 vim_snprintf((char *)message, sizeof(message),
6189 _("line %6d, word %6d - %s"),
6190 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
6191 msg_start();
6192 msg_puts_long_attr(message, 0);
6193 msg_clr_eos();
6194 msg_didout = FALSE;
6195 msg_col = 0;
6196 out_flush();
6197 }
6198
Bram Moolenaar51485f02005-06-04 21:55:20 +00006199 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006200 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006201 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006202 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006203 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006204 if (retval == FAIL)
6205 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006206
Bram Moolenaar51485f02005-06-04 21:55:20 +00006207 hash = hash_hash(dw);
6208 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006209 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006210 {
6211 if (p_verbose > 0)
6212 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006213 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006214 else if (duplicate == 0)
6215 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
6216 fname, lnum, dw);
6217 ++duplicate;
6218 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006219 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006220 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006221
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006222 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006223 store_afflist[0] = NUL;
6224 pfxlen = 0;
6225 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006226 if (afflist != NULL)
6227 {
6228 /* Check for affix name that stands for keep-case word and stands
6229 * for rare word (if defined). */
Bram Moolenaar371baa92005-12-29 22:43:53 +00006230 if (affile->af_keepcase != 0 && flag_in_afflist(
6231 affile->af_flagtype, afflist, affile->af_keepcase))
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006232 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar371baa92005-12-29 22:43:53 +00006233 if (affile->af_rare != 0 && flag_in_afflist(
6234 affile->af_flagtype, afflist, affile->af_rare))
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006235 flags |= WF_RARE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006236 if (affile->af_bad != 0 && flag_in_afflist(
6237 affile->af_flagtype, afflist, affile->af_bad))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006238 flags |= WF_BANNED;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006239 if (affile->af_needaffix != 0 && flag_in_afflist(
6240 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00006241 need_affix = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006242 if (affile->af_needcomp != 0 && flag_in_afflist(
6243 affile->af_flagtype, afflist, affile->af_needcomp))
6244 flags |= WF_NEEDCOMP;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006245
6246 if (affile->af_pfxpostpone)
6247 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006248 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006249
Bram Moolenaar5195e452005-08-19 20:32:47 +00006250 if (spin->si_compflags != NULL)
6251 /* Need to store the list of compound flags with the word.
6252 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006253 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006254 }
6255
Bram Moolenaar51485f02005-06-04 21:55:20 +00006256 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006257 if (store_word(spin, dw, flags, spin->si_region,
6258 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006259 retval = FAIL;
6260
6261 if (afflist != NULL)
6262 {
6263 /* Find all matching suffixes and add the resulting words.
6264 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006265 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006266 &affile->af_suff, &affile->af_pref,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006267 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006268 retval = FAIL;
6269
6270 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006271 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006272 &affile->af_pref, NULL,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006273 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006274 retval = FAIL;
6275 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006276 }
6277
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006278 if (duplicate > 0)
6279 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006280 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006281 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
6282 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006283 hash_clear(&ht);
6284
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006285 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006286 return retval;
6287}
6288
6289/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006290 * Get the list of prefix IDs from the affix list "afflist".
6291 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006292 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
6293 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006294 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006295 static int
6296get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006297 afffile_T *affile;
6298 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006299 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006300{
6301 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006302 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006303 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006304 int id;
6305 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006306 hashitem_T *hi;
6307
Bram Moolenaar6de68532005-08-24 22:08:48 +00006308 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006309 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006310 prevp = p;
6311 if (get_affitem(affile->af_flagtype, &p) != 0)
6312 {
6313 /* A flag is a postponed prefix flag if it appears in "af_pref"
6314 * and it's ID is not zero. */
6315 vim_strncpy(key, prevp, p - prevp);
6316 hi = hash_find(&affile->af_pref, key);
6317 if (!HASHITEM_EMPTY(hi))
6318 {
6319 id = HI2AH(hi)->ah_newID;
6320 if (id != 0)
6321 store_afflist[cnt++] = id;
6322 }
6323 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006324 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006325 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006326 }
6327
Bram Moolenaar5195e452005-08-19 20:32:47 +00006328 store_afflist[cnt] = NUL;
6329 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006330}
6331
6332/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006333 * Get the list of compound IDs from the affix list "afflist" that are used
6334 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006335 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006336 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006337 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00006338get_compflags(affile, afflist, store_afflist)
6339 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006340 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006341 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006342{
6343 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006344 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006345 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006346 char_u key[AH_KEY_LEN];
6347 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006348
Bram Moolenaar6de68532005-08-24 22:08:48 +00006349 for (p = afflist; *p != NUL; )
6350 {
6351 prevp = p;
6352 if (get_affitem(affile->af_flagtype, &p) != 0)
6353 {
6354 /* A flag is a compound flag if it appears in "af_comp". */
6355 vim_strncpy(key, prevp, p - prevp);
6356 hi = hash_find(&affile->af_comp, key);
6357 if (!HASHITEM_EMPTY(hi))
6358 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
6359 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006360 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006361 ++p;
6362 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006363
Bram Moolenaar5195e452005-08-19 20:32:47 +00006364 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006365}
6366
6367/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006368 * Apply affixes to a word and store the resulting words.
6369 * "ht" is the hashtable with affentry_T that need to be applied, either
6370 * prefixes or suffixes.
6371 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
6372 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006373 *
6374 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006375 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006376 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00006377store_aff_word(spin, word, afflist, affile, ht, xht, comb, flags,
6378 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006379 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006380 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006381 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006382 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006383 hashtab_T *ht;
6384 hashtab_T *xht;
6385 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006386 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006387 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006388 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
6389 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006390{
6391 int todo;
6392 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006393 affheader_T *ah;
6394 affentry_T *ae;
6395 regmatch_T regmatch;
6396 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006397 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006398 int i;
6399 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006400 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006401 char_u *use_pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006402 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006403 size_t wordlen = STRLEN(word);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006404
Bram Moolenaar51485f02005-06-04 21:55:20 +00006405 todo = ht->ht_used;
6406 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006407 {
6408 if (!HASHITEM_EMPTY(hi))
6409 {
6410 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006411 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00006412
Bram Moolenaar51485f02005-06-04 21:55:20 +00006413 /* Check that the affix combines, if required, and that the word
6414 * supports this affix. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006415 if ((!comb || ah->ah_combine) && flag_in_afflist(
6416 affile->af_flagtype, afflist, ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00006417 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006418 /* Loop over all affix entries with this name. */
6419 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006420 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006421 /* Check the condition. It's not logical to match case
6422 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006423 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006424 * Another requirement from Myspell is that the chop
6425 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006426 * For prefixes, when "PFXPOSTPONE" was used, only do
6427 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006428 regmatch.regprog = ae->ae_prog;
6429 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006430 if ((xht != NULL || !affile->af_pfxpostpone
6431 || ae->ae_chop != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006432 && (ae->ae_chop == NULL
6433 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006434 && (ae->ae_prog == NULL
6435 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006436 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006437 /* Match. Remove the chop and add the affix. */
6438 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006439 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006440 /* prefix: chop/add at the start of the word */
6441 if (ae->ae_add == NULL)
6442 *newword = NUL;
6443 else
6444 STRCPY(newword, ae->ae_add);
6445 p = word;
6446 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006447 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006448 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006449#ifdef FEAT_MBYTE
6450 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006451 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006452 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006453 for ( ; i > 0; --i)
6454 mb_ptr_adv(p);
6455 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006456 else
6457#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006458 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006459 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006460 STRCAT(newword, p);
6461 }
6462 else
6463 {
6464 /* suffix: chop/add at the end of the word */
6465 STRCPY(newword, word);
6466 if (ae->ae_chop != NULL)
6467 {
6468 /* Remove chop string. */
6469 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006470 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006471 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006472 mb_ptr_back(newword, p);
6473 *p = NUL;
6474 }
6475 if (ae->ae_add != NULL)
6476 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006477 }
6478
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006479 /* Obey the "rare" flag of the affix. */
6480 if (ae->ae_rare)
6481 use_flags = flags | WF_RARE;
6482 else
6483 use_flags = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006484
6485 /* Obey the "nocomp" flag of the affix: don't use the
6486 * compound flags. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006487 use_pfxlist = pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006488 if (ae->ae_nocomp && pfxlist != NULL)
6489 {
6490 vim_strncpy(pfx_pfxlist, pfxlist, pfxlen);
6491 use_pfxlist = pfx_pfxlist;
6492 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006493
6494 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00006495 if (spin->si_prefroot != NULL
6496 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006497 {
6498 /* ... add a flag to indicate an affix was used. */
6499 use_flags |= WF_HAS_AFF;
6500
6501 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00006502 * affixes is not allowed. But do use the
6503 * compound flags after them. */
6504 if ((!ah->ah_combine || comb) && pfxlist != NULL)
6505 use_pfxlist += pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006506 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006507
Bram Moolenaar51485f02005-06-04 21:55:20 +00006508 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006509 if (store_word(spin, newword, use_flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006510 spin->si_region, use_pfxlist, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006511 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006512
Bram Moolenaar51485f02005-06-04 21:55:20 +00006513 /* When added a suffix and combining is allowed also
6514 * try adding prefixes additionally. */
6515 if (xht != NULL && ah->ah_combine)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006516 if (store_aff_word(spin, newword, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006517 xht, NULL, TRUE,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006518 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006519 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006520 }
6521 }
6522 }
6523 }
6524 }
6525
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006526 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006527}
6528
6529/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006530 * Read a file with a list of words.
6531 */
6532 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006533spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006534 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006535 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006536{
6537 FILE *fd;
6538 long lnum = 0;
6539 char_u rline[MAXLINELEN];
6540 char_u *line;
6541 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006542 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006543 int l;
6544 int retval = OK;
6545 int did_word = FALSE;
6546 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006547 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00006548 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006549
6550 /*
6551 * Open the file.
6552 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006553 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00006554 if (fd == NULL)
6555 {
6556 EMSG2(_(e_notopen), fname);
6557 return FAIL;
6558 }
6559
Bram Moolenaar4770d092006-01-12 23:22:24 +00006560 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname);
6561 spell_message(spin, IObuff);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006562
6563 /*
6564 * Read all the lines in the file one by one.
6565 */
6566 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
6567 {
6568 line_breakcheck();
6569 ++lnum;
6570
6571 /* Skip comment lines. */
6572 if (*rline == '#')
6573 continue;
6574
6575 /* Remove CR, LF and white space from the end. */
6576 l = STRLEN(rline);
6577 while (l > 0 && rline[l - 1] <= ' ')
6578 --l;
6579 if (l == 0)
6580 continue; /* empty or blank line */
6581 rline[l] = NUL;
6582
6583 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
6584 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006585#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00006586 if (spin->si_conv.vc_type != CONV_NONE)
6587 {
6588 pc = string_convert(&spin->si_conv, rline, NULL);
6589 if (pc == NULL)
6590 {
6591 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6592 fname, lnum, rline);
6593 continue;
6594 }
6595 line = pc;
6596 }
6597 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006598#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006599 {
6600 pc = NULL;
6601 line = rline;
6602 }
6603
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006604 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00006605 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006606 ++line;
6607 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006608 {
6609 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006610 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
6611 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006612 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006613 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
6614 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006615 else
6616 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006617#ifdef FEAT_MBYTE
6618 char_u *enc;
6619
Bram Moolenaar51485f02005-06-04 21:55:20 +00006620 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006621 line += 10;
6622 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006623 if (enc != NULL && !spin->si_ascii
6624 && convert_setup(&spin->si_conv, enc,
6625 p_enc) == FAIL)
6626 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00006627 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006628 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006629 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006630#else
6631 smsg((char_u *)_("Conversion in %s not supported"), fname);
6632#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006633 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006634 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006635 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006636
Bram Moolenaar3982c542005-06-08 21:56:31 +00006637 if (STRNCMP(line, "regions=", 8) == 0)
6638 {
6639 if (spin->si_region_count > 1)
6640 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
6641 fname, lnum, line);
6642 else
6643 {
6644 line += 8;
6645 if (STRLEN(line) > 16)
6646 smsg((char_u *)_("Too many regions in %s line %d: %s"),
6647 fname, lnum, line);
6648 else
6649 {
6650 spin->si_region_count = STRLEN(line) / 2;
6651 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006652
6653 /* Adjust the mask for a word valid in all regions. */
6654 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00006655 }
6656 }
6657 continue;
6658 }
6659
Bram Moolenaar7887d882005-07-01 22:33:52 +00006660 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
6661 fname, lnum, line - 1);
6662 continue;
6663 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006664
Bram Moolenaar7887d882005-07-01 22:33:52 +00006665 flags = 0;
6666 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006667
Bram Moolenaar7887d882005-07-01 22:33:52 +00006668 /* Check for flags and region after a slash. */
6669 p = vim_strchr(line, '/');
6670 if (p != NULL)
6671 {
6672 *p++ = NUL;
6673 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006674 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006675 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006676 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006677 else if (*p == '!') /* Bad, bad, wicked word. */
6678 flags |= WF_BANNED;
6679 else if (*p == '?') /* Rare word. */
6680 flags |= WF_RARE;
6681 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006682 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006683 if ((flags & WF_REGION) == 0) /* first one */
6684 regionmask = 0;
6685 flags |= WF_REGION;
6686
6687 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00006688 if (l > spin->si_region_count)
6689 {
6690 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00006691 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00006692 break;
6693 }
6694 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00006695 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00006696 else
6697 {
6698 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
6699 fname, lnum, p);
6700 break;
6701 }
6702 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006703 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006704 }
6705
6706 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6707 if (spin->si_ascii && has_non_ascii(line))
6708 {
6709 ++non_ascii;
6710 continue;
6711 }
6712
6713 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006714 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006715 {
6716 retval = FAIL;
6717 break;
6718 }
6719 did_word = TRUE;
6720 }
6721
6722 vim_free(pc);
6723 fclose(fd);
6724
Bram Moolenaar4770d092006-01-12 23:22:24 +00006725 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006726 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006727 vim_snprintf((char *)IObuff, IOSIZE,
6728 _("Ignored %d words with non-ASCII characters"), non_ascii);
6729 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006730 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006731
Bram Moolenaar51485f02005-06-04 21:55:20 +00006732 return retval;
6733}
6734
6735/*
6736 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006737 * This avoids calling free() for every little struct we use (and keeping
6738 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00006739 * The memory is cleared to all zeros.
6740 * Returns NULL when out of memory.
6741 */
6742 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006743getroom(spin, len, align)
6744 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006745 size_t len; /* length needed */
6746 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006747{
6748 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006749 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006750
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006751 if (align && bl != NULL)
6752 /* Round size up for alignment. On some systems structures need to be
6753 * aligned to the size of a pointer (e.g., SPARC). */
6754 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
6755 & ~(sizeof(char *) - 1);
6756
Bram Moolenaar51485f02005-06-04 21:55:20 +00006757 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
6758 {
6759 /* Allocate a block of memory. This is not freed until much later. */
6760 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
6761 if (bl == NULL)
6762 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006763 bl->sb_next = spin->si_blocks;
6764 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006765 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006766 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006767 }
6768
6769 p = bl->sb_data + bl->sb_used;
6770 bl->sb_used += len;
6771
6772 return p;
6773}
6774
6775/*
6776 * Make a copy of a string into memory allocated with getroom().
6777 */
6778 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006779getroom_save(spin, s)
6780 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006781 char_u *s;
6782{
6783 char_u *sc;
6784
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006785 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006786 if (sc != NULL)
6787 STRCPY(sc, s);
6788 return sc;
6789}
6790
6791
6792/*
6793 * Free the list of allocated sblock_T.
6794 */
6795 static void
6796free_blocks(bl)
6797 sblock_T *bl;
6798{
6799 sblock_T *next;
6800
6801 while (bl != NULL)
6802 {
6803 next = bl->sb_next;
6804 vim_free(bl);
6805 bl = next;
6806 }
6807}
6808
6809/*
6810 * Allocate the root of a word tree.
6811 */
6812 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006813wordtree_alloc(spin)
6814 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006815{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006816 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006817}
6818
6819/*
6820 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006821 * Always store it in the case-folded tree. For a keep-case word this is
6822 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
6823 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006824 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006825 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
6826 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006827 */
6828 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00006829store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006830 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006831 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006832 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006833 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006834 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006835 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006836{
6837 int len = STRLEN(word);
6838 int ct = captype(word, word + len);
6839 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006840 int res = OK;
6841 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006842
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006843 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006844 for (p = pfxlist; res == OK; ++p)
6845 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006846 if (!need_affix || (p != NULL && *p != NUL))
6847 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006848 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006849 if (p == NULL || *p == NUL)
6850 break;
6851 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00006852 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006853
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006854 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00006855 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006856 for (p = pfxlist; res == OK; ++p)
6857 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006858 if (!need_affix || (p != NULL && *p != NUL))
6859 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006860 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006861 if (p == NULL || *p == NUL)
6862 break;
6863 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00006864 ++spin->si_keepwcount;
6865 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006866 return res;
6867}
6868
6869/*
6870 * Add word "word" to a word tree at "root".
Bram Moolenaar4770d092006-01-12 23:22:24 +00006871 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006872 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006873 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006874 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006875 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006876tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006877 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006878 char_u *word;
6879 wordnode_T *root;
6880 int flags;
6881 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006882 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006883{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006884 wordnode_T *node = root;
6885 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006886 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006887 wordnode_T **prev = NULL;
6888 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006889
Bram Moolenaar51485f02005-06-04 21:55:20 +00006890 /* Add each byte of the word to the tree, including the NUL at the end. */
6891 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006892 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006893 /* When there is more than one reference to this node we need to make
6894 * a copy, so that we can modify it. Copy the whole list of siblings
6895 * (we don't optimize for a partly shared list of siblings). */
6896 if (node != NULL && node->wn_refs > 1)
6897 {
6898 --node->wn_refs;
6899 copyprev = prev;
6900 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
6901 {
6902 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006903 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006904 if (np == NULL)
6905 return FAIL;
6906 np->wn_child = copyp->wn_child;
6907 if (np->wn_child != NULL)
6908 ++np->wn_child->wn_refs; /* child gets extra ref */
6909 np->wn_byte = copyp->wn_byte;
6910 if (np->wn_byte == NUL)
6911 {
6912 np->wn_flags = copyp->wn_flags;
6913 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006914 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006915 }
6916
6917 /* Link the new node in the list, there will be one ref. */
6918 np->wn_refs = 1;
6919 *copyprev = np;
6920 copyprev = &np->wn_sibling;
6921
6922 /* Let "node" point to the head of the copied list. */
6923 if (copyp == node)
6924 node = np;
6925 }
6926 }
6927
Bram Moolenaar51485f02005-06-04 21:55:20 +00006928 /* Look for the sibling that has the same character. They are sorted
6929 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006930 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006931 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006932 while (node != NULL
6933 && (node->wn_byte < word[i]
6934 || (node->wn_byte == NUL
6935 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00006936 ? node->wn_affixID < (unsigned)affixID
6937 : (node->wn_flags < (unsigned)(flags & WN_MASK)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006938 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006939 && (spin->si_sugtree
6940 ? (node->wn_region & 0xffff) < region
6941 : node->wn_affixID
6942 < (unsigned)affixID)))))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006943 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006944 prev = &node->wn_sibling;
6945 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006946 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006947 if (node == NULL
6948 || node->wn_byte != word[i]
6949 || (word[i] == NUL
6950 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00006951 || spin->si_sugtree
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006952 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006953 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006954 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006955 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006956 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006957 if (np == NULL)
6958 return FAIL;
6959 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006960
6961 /* If "node" is NULL this is a new child or the end of the sibling
6962 * list: ref count is one. Otherwise use ref count of sibling and
6963 * make ref count of sibling one (matters when inserting in front
6964 * of the list of siblings). */
6965 if (node == NULL)
6966 np->wn_refs = 1;
6967 else
6968 {
6969 np->wn_refs = node->wn_refs;
6970 node->wn_refs = 1;
6971 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006972 *prev = np;
6973 np->wn_sibling = node;
6974 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006975 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006976
Bram Moolenaar51485f02005-06-04 21:55:20 +00006977 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006978 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006979 node->wn_flags = flags;
6980 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006981 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006982 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00006983 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006984 prev = &node->wn_child;
6985 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006986 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006987#ifdef SPELL_PRINTTREE
6988 smsg("Added \"%s\"", word);
6989 spell_print_tree(root->wn_sibling);
6990#endif
6991
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006992 /* count nr of words added since last message */
6993 ++spin->si_msg_count;
6994
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006995 if (spin->si_compress_cnt > 1)
6996 {
6997 if (--spin->si_compress_cnt == 1)
6998 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006999 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007000 }
7001
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007002 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007003 * When we have allocated lots of memory we need to compress the word tree
7004 * to free up some room. But compression is slow, and we might actually
7005 * need that room, thus only compress in the following situations:
7006 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00007007 * "compress_start" blocks.
7008 * 2. When compressed before and used "compress_inc" blocks before
7009 * adding "compress_added" words (si_compress_cnt > 1).
7010 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007011 * (si_compress_cnt == 1) and the number of free nodes drops below the
7012 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007013 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007014#ifndef SPELL_PRINTTREE
7015 if (spin->si_compress_cnt == 1
7016 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00007017 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007018#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007019 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007020 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00007021 * when the freed up room has been used and another "compress_inc"
7022 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007023 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007024 spin->si_blocks_cnt -= compress_inc;
7025 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007026
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007027 if (spin->si_verbose)
7028 {
7029 msg_start();
7030 msg_puts((char_u *)_(msg_compressing));
7031 msg_clr_eos();
7032 msg_didout = FALSE;
7033 msg_col = 0;
7034 out_flush();
7035 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007036
7037 /* Compress both trees. Either they both have many nodes, which makes
7038 * compression useful, or one of them is small, which means
Bram Moolenaar4770d092006-01-12 23:22:24 +00007039 * compression goes fast. But when filling the souldfold word tree
7040 * there is no keep-case tree. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007041 wordtree_compress(spin, spin->si_foldroot);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007042 if (affixID >= 0)
7043 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007044 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007045
7046 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007047}
7048
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007049/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007050 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
7051 * Sets "sps_flags".
7052 */
7053 int
7054spell_check_msm()
7055{
7056 char_u *p = p_msm;
7057 long start = 0;
7058 long inc = 0;
7059 long added = 0;
7060
7061 if (!VIM_ISDIGIT(*p))
7062 return FAIL;
7063 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
7064 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
7065 if (*p != ',')
7066 return FAIL;
7067 ++p;
7068 if (!VIM_ISDIGIT(*p))
7069 return FAIL;
7070 inc = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
7071 if (*p != ',')
7072 return FAIL;
7073 ++p;
7074 if (!VIM_ISDIGIT(*p))
7075 return FAIL;
7076 added = getdigits(&p) * 1024;
7077 if (*p != NUL)
7078 return FAIL;
7079
7080 if (start == 0 || inc == 0 || added == 0 || inc > start)
7081 return FAIL;
7082
7083 compress_start = start;
7084 compress_inc = inc;
7085 compress_added = added;
7086 return OK;
7087}
7088
7089
7090/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007091 * Get a wordnode_T, either from the list of previously freed nodes or
7092 * allocate a new one.
7093 */
7094 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007095get_wordnode(spin)
7096 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007097{
7098 wordnode_T *n;
7099
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007100 if (spin->si_first_free == NULL)
7101 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007102 else
7103 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007104 n = spin->si_first_free;
7105 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007106 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007107 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007108 }
7109#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007110 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007111#endif
7112 return n;
7113}
7114
7115/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007116 * Decrement the reference count on a node (which is the head of a list of
7117 * siblings). If the reference count becomes zero free the node and its
7118 * siblings.
Bram Moolenaar4770d092006-01-12 23:22:24 +00007119 * Returns the number of nodes actually freed.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007120 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007121 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007122deref_wordnode(spin, node)
7123 spellinfo_T *spin;
7124 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007125{
Bram Moolenaar4770d092006-01-12 23:22:24 +00007126 wordnode_T *np;
7127 int cnt = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007128
7129 if (--node->wn_refs == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007130 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007131 for (np = node; np != NULL; np = np->wn_sibling)
7132 {
7133 if (np->wn_child != NULL)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007134 cnt += deref_wordnode(spin, np->wn_child);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007135 free_wordnode(spin, np);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007136 ++cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007137 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007138 ++cnt; /* length field */
7139 }
7140 return cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007141}
7142
7143/*
7144 * Free a wordnode_T for re-use later.
7145 * Only the "wn_child" field becomes invalid.
7146 */
7147 static void
7148free_wordnode(spin, n)
7149 spellinfo_T *spin;
7150 wordnode_T *n;
7151{
7152 n->wn_child = spin->si_first_free;
7153 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007154 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007155}
7156
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007157/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007158 * Compress a tree: find tails that are identical and can be shared.
7159 */
7160 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007161wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007162 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007163 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007164{
7165 hashtab_T ht;
7166 int n;
7167 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007168 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007169
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007170 /* Skip the root itself, it's not actually used. The first sibling is the
7171 * start of the tree. */
7172 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007173 {
7174 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007175 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007176
7177#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00007178 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007179#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00007180 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007181 if (tot > 1000000)
7182 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007183 else if (tot == 0)
7184 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007185 else
7186 perc = (tot - n) * 100 / tot;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007187 vim_snprintf((char *)IObuff, IOSIZE,
7188 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
7189 n, tot, tot - n, perc);
7190 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007191 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007192#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007193 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007194#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007195 hash_clear(&ht);
7196 }
7197}
7198
7199/*
7200 * Compress a node, its siblings and its children, depth first.
7201 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007202 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007203 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007204node_compress(spin, node, ht, tot)
7205 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007206 wordnode_T *node;
7207 hashtab_T *ht;
7208 int *tot; /* total count of nodes before compressing,
7209 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007210{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007211 wordnode_T *np;
7212 wordnode_T *tp;
7213 wordnode_T *child;
7214 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007215 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007216 int len = 0;
7217 unsigned nr, n;
7218 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007219
Bram Moolenaar51485f02005-06-04 21:55:20 +00007220 /*
7221 * Go through the list of siblings. Compress each child and then try
7222 * finding an identical child to replace it.
7223 * Note that with "child" we mean not just the node that is pointed to,
Bram Moolenaar4770d092006-01-12 23:22:24 +00007224 * but the whole list of siblings of which the child node is the first.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007225 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007226 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007227 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007228 ++len;
7229 if ((child = np->wn_child) != NULL)
7230 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007231 /* Compress the child first. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007232 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007233
7234 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007235 hash = hash_hash(child->wn_u1.hashkey);
7236 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007237 if (!HASHITEM_EMPTY(hi))
7238 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007239 /* There are children we encountered before with a hash value
7240 * identical to the current child. Now check if there is one
7241 * that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007242 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007243 if (node_equal(child, tp))
7244 {
7245 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007246 * current one. This means the current child and all
7247 * its siblings is unlinked from the tree. */
7248 ++tp->wn_refs;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007249 compressed += deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007250 np->wn_child = tp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007251 break;
7252 }
7253 if (tp == NULL)
7254 {
7255 /* No other child with this hash value equals the child of
7256 * the node, add it to the linked list after the first
7257 * item. */
7258 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007259 child->wn_u2.next = tp->wn_u2.next;
7260 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007261 }
7262 }
7263 else
7264 /* No other child has this hash value, add it to the
7265 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007266 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007267 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007268 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007269 *tot += len + 1; /* add one for the node that stores the length */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007270
7271 /*
7272 * Make a hash key for the node and its siblings, so that we can quickly
7273 * find a lookalike node. This must be done after compressing the sibling
7274 * list, otherwise the hash key would become invalid by the compression.
7275 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007276 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007277 nr = 0;
7278 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007279 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007280 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007281 /* end node: use wn_flags, wn_region and wn_affixID */
7282 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007283 else
7284 /* byte node: use the byte value and the child pointer */
7285 n = np->wn_byte + ((long_u)np->wn_child << 8);
7286 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007287 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007288
7289 /* Avoid NUL bytes, it terminates the hash key. */
7290 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007291 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007292 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007293 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007294 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007295 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007296 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007297 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
7298 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007299
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007300 /* Check for CTRL-C pressed now and then. */
7301 fast_breakcheck();
7302
Bram Moolenaar51485f02005-06-04 21:55:20 +00007303 return compressed;
7304}
7305
7306/*
7307 * Return TRUE when two nodes have identical siblings and children.
7308 */
7309 static int
7310node_equal(n1, n2)
7311 wordnode_T *n1;
7312 wordnode_T *n2;
7313{
7314 wordnode_T *p1;
7315 wordnode_T *p2;
7316
7317 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
7318 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
7319 if (p1->wn_byte != p2->wn_byte
7320 || (p1->wn_byte == NUL
7321 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007322 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007323 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007324 : (p1->wn_child != p2->wn_child)))
7325 break;
7326
7327 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007328}
7329
7330/*
7331 * Write a number to file "fd", MSB first, in "len" bytes.
7332 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007333 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007334put_bytes(fd, nr, len)
7335 FILE *fd;
7336 long_u nr;
7337 int len;
7338{
7339 int i;
7340
7341 for (i = len - 1; i >= 0; --i)
7342 putc((int)(nr >> (i * 8)), fd);
7343}
7344
Bram Moolenaar4770d092006-01-12 23:22:24 +00007345/*
7346 * Write spin->si_sugtime to file "fd".
7347 */
7348 static void
7349put_sugtime(spin, fd)
7350 spellinfo_T *spin;
7351 FILE *fd;
7352{
7353 int c;
7354 int i;
7355
7356 /* time_t can be up to 8 bytes in size, more than long_u, thus we
7357 * can't use put_bytes() here. */
7358 for (i = 7; i >= 0; --i)
7359 if (i + 1 > sizeof(time_t))
7360 /* ">>" doesn't work well when shifting more bits than avail */
7361 putc(0, fd);
7362 else
7363 {
7364 c = (unsigned)spin->si_sugtime >> (i * 8);
7365 putc(c, fd);
7366 }
7367}
7368
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007369static int
7370#ifdef __BORLANDC__
7371_RTLENTRYF
7372#endif
7373rep_compare __ARGS((const void *s1, const void *s2));
7374
7375/*
7376 * Function given to qsort() to sort the REP items on "from" string.
7377 */
7378 static int
7379#ifdef __BORLANDC__
7380_RTLENTRYF
7381#endif
7382rep_compare(s1, s2)
7383 const void *s1;
7384 const void *s2;
7385{
7386 fromto_T *p1 = (fromto_T *)s1;
7387 fromto_T *p2 = (fromto_T *)s2;
7388
7389 return STRCMP(p1->ft_from, p2->ft_from);
7390}
7391
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007392/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007393 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007394 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007395 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007396 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007397write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007398 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007399 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007400{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007401 FILE *fd;
7402 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007403 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007404 wordnode_T *tree;
7405 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007406 int i;
7407 int l;
7408 garray_T *gap;
7409 fromto_T *ftp;
7410 char_u *p;
7411 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007412 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007413
Bram Moolenaarb765d632005-06-07 21:00:02 +00007414 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007415 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007416 {
7417 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007418 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007419 }
7420
Bram Moolenaar5195e452005-08-19 20:32:47 +00007421 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007422 /* <fileID> */
7423 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007424 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007425 EMSG(_(e_write));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007426 retval = FAIL;
7427 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007428 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007429
Bram Moolenaar5195e452005-08-19 20:32:47 +00007430 /*
7431 * <SECTIONS>: <section> ... <sectionend>
7432 */
7433
7434 /* SN_REGION: <regionname> ...
7435 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007436 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007437 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007438 putc(SN_REGION, fd); /* <sectionID> */
7439 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7440 l = spin->si_region_count * 2;
7441 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7442 fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
7443 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007444 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007445 }
7446 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00007447 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007448
Bram Moolenaar5195e452005-08-19 20:32:47 +00007449 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
7450 *
7451 * The table with character flags and the table for case folding.
7452 * This makes sure the same characters are recognized as word characters
7453 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007454 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007455 * 'encoding'.
7456 * Also skip this for an .add.spl file, the main spell file must contain
7457 * the table (avoids that it conflicts). File is shorter too.
7458 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007459 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007460 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007461 char_u folchars[128 * 8];
7462 int flags;
7463
Bram Moolenaard12a1322005-08-21 22:08:24 +00007464 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007465 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7466
7467 /* Form the <folchars> string first, we need to know its length. */
7468 l = 0;
7469 for (i = 128; i < 256; ++i)
7470 {
7471#ifdef FEAT_MBYTE
7472 if (has_mbyte)
7473 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
7474 else
7475#endif
7476 folchars[l++] = spelltab.st_fold[i];
7477 }
7478 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
7479
7480 fputc(128, fd); /* <charflagslen> */
7481 for (i = 128; i < 256; ++i)
7482 {
7483 flags = 0;
7484 if (spelltab.st_isw[i])
7485 flags |= CF_WORD;
7486 if (spelltab.st_isu[i])
7487 flags |= CF_UPPER;
7488 fputc(flags, fd); /* <charflags> */
7489 }
7490
7491 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
7492 fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007493 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007494
Bram Moolenaar5195e452005-08-19 20:32:47 +00007495 /* SN_MIDWORD: <midword> */
7496 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007497 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007498 putc(SN_MIDWORD, fd); /* <sectionID> */
7499 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7500
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007501 i = STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007502 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007503 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
7504 }
7505
Bram Moolenaar5195e452005-08-19 20:32:47 +00007506 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
7507 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007508 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007509 putc(SN_PREFCOND, fd); /* <sectionID> */
7510 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7511
7512 l = write_spell_prefcond(NULL, &spin->si_prefcond);
7513 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7514
7515 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007516 }
7517
Bram Moolenaar5195e452005-08-19 20:32:47 +00007518 /* SN_REP: <repcount> <rep> ...
Bram Moolenaar4770d092006-01-12 23:22:24 +00007519 * SN_SAL: <salflags> <salcount> <sal> ...
7520 * SN_REPSAL: <repcount> <rep> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007521
Bram Moolenaar5195e452005-08-19 20:32:47 +00007522 /* round 1: SN_REP section
Bram Moolenaar4770d092006-01-12 23:22:24 +00007523 * round 2: SN_SAL section (unless SN_SOFO is used)
7524 * round 3: SN_REPSAL section */
7525 for (round = 1; round <= 3; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007526 {
7527 if (round == 1)
7528 gap = &spin->si_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007529 else if (round == 2)
7530 {
7531 /* Don't write SN_SAL when using a SN_SOFO section */
7532 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
7533 continue;
7534 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007535 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007536 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00007537 gap = &spin->si_repsal;
7538
7539 /* Don't write the section if there are no items. */
7540 if (gap->ga_len == 0)
7541 continue;
7542
7543 /* Sort the REP/REPSAL items. */
7544 if (round != 2)
7545 qsort(gap->ga_data, (size_t)gap->ga_len,
7546 sizeof(fromto_T), rep_compare);
7547
7548 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
7549 putc(i, fd); /* <sectionID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007550
Bram Moolenaar5195e452005-08-19 20:32:47 +00007551 /* This is for making suggestions, section is not required. */
7552 putc(0, fd); /* <sectionflags> */
7553
7554 /* Compute the length of what follows. */
7555 l = 2; /* count <repcount> or <salcount> */
7556 for (i = 0; i < gap->ga_len; ++i)
7557 {
7558 ftp = &((fromto_T *)gap->ga_data)[i];
7559 l += 1 + STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
7560 l += 1 + STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
7561 }
7562 if (round == 2)
7563 ++l; /* count <salflags> */
7564 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7565
7566 if (round == 2)
7567 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007568 i = 0;
7569 if (spin->si_followup)
7570 i |= SAL_F0LLOWUP;
7571 if (spin->si_collapse)
7572 i |= SAL_COLLAPSE;
7573 if (spin->si_rem_accents)
7574 i |= SAL_REM_ACCENTS;
7575 putc(i, fd); /* <salflags> */
7576 }
7577
7578 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
7579 for (i = 0; i < gap->ga_len; ++i)
7580 {
7581 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
7582 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
7583 ftp = &((fromto_T *)gap->ga_data)[i];
7584 for (rr = 1; rr <= 2; ++rr)
7585 {
7586 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
7587 l = STRLEN(p);
7588 putc(l, fd);
7589 fwrite(p, l, (size_t)1, fd);
7590 }
7591 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007592
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007593 }
7594
Bram Moolenaar5195e452005-08-19 20:32:47 +00007595 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
7596 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007597 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
7598 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007599 putc(SN_SOFO, fd); /* <sectionID> */
7600 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007601
7602 l = STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007603 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
7604 /* <sectionlen> */
7605
7606 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
7607 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007608
7609 l = STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007610 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
7611 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007612 }
7613
Bram Moolenaar4770d092006-01-12 23:22:24 +00007614 /* SN_WORDS: <word> ...
7615 * This is for making suggestions, section is not required. */
7616 if (spin->si_commonwords.ht_used > 0)
7617 {
7618 putc(SN_WORDS, fd); /* <sectionID> */
7619 putc(0, fd); /* <sectionflags> */
7620
7621 /* round 1: count the bytes
7622 * round 2: write the bytes */
7623 for (round = 1; round <= 2; ++round)
7624 {
7625 int todo;
7626 int len = 0;
7627 hashitem_T *hi;
7628
7629 todo = spin->si_commonwords.ht_used;
7630 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
7631 if (!HASHITEM_EMPTY(hi))
7632 {
7633 l = STRLEN(hi->hi_key) + 1;
7634 len += l;
7635 if (round == 2) /* <word> */
7636 fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
7637 --todo;
7638 }
7639 if (round == 1)
7640 put_bytes(fd, (long_u)len, 4); /* <sectionlen> */
7641 }
7642 }
7643
Bram Moolenaar5195e452005-08-19 20:32:47 +00007644 /* SN_MAP: <mapstr>
7645 * This is for making suggestions, section is not required. */
7646 if (spin->si_map.ga_len > 0)
7647 {
7648 putc(SN_MAP, fd); /* <sectionID> */
7649 putc(0, fd); /* <sectionflags> */
7650 l = spin->si_map.ga_len;
7651 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7652 fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
7653 /* <mapstr> */
7654 }
7655
Bram Moolenaar4770d092006-01-12 23:22:24 +00007656 /* SN_SUGFILE: <timestamp>
7657 * This is used to notify that a .sug file may be available and at the
7658 * same time allows for checking that a .sug file that is found matches
7659 * with this .spl file. That's because the word numbers must be exactly
7660 * right. */
7661 if (!spin->si_nosugfile
7662 && (spin->si_sal.ga_len > 0
7663 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
7664 {
7665 putc(SN_SUGFILE, fd); /* <sectionID> */
7666 putc(0, fd); /* <sectionflags> */
7667 put_bytes(fd, (long_u)8, 4); /* <sectionlen> */
7668
7669 /* Set si_sugtime and write it to the file. */
7670 spin->si_sugtime = time(NULL);
7671 put_sugtime(spin, fd); /* <timestamp> */
7672 }
7673
Bram Moolenaar5195e452005-08-19 20:32:47 +00007674 /* SN_COMPOUND: compound info.
7675 * We don't mark it required, when not supported all compound words will
7676 * be bad words. */
7677 if (spin->si_compflags != NULL)
7678 {
7679 putc(SN_COMPOUND, fd); /* <sectionID> */
7680 putc(0, fd); /* <sectionflags> */
7681
7682 l = STRLEN(spin->si_compflags);
7683 put_bytes(fd, (long_u)(l + 3), 4); /* <sectionlen> */
7684 putc(spin->si_compmax, fd); /* <compmax> */
7685 putc(spin->si_compminlen, fd); /* <compminlen> */
7686 putc(spin->si_compsylmax, fd); /* <compsylmax> */
7687 /* <compflags> */
7688 fwrite(spin->si_compflags, (size_t)l, (size_t)1, fd);
7689 }
7690
Bram Moolenaar78622822005-08-23 21:00:13 +00007691 /* SN_NOBREAK: NOBREAK flag */
7692 if (spin->si_nobreak)
7693 {
7694 putc(SN_NOBREAK, fd); /* <sectionID> */
7695 putc(0, fd); /* <sectionflags> */
7696
7697 /* It's empty, the precense of the section flags the feature. */
7698 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
7699 }
7700
Bram Moolenaar5195e452005-08-19 20:32:47 +00007701 /* SN_SYLLABLE: syllable info.
7702 * We don't mark it required, when not supported syllables will not be
7703 * counted. */
7704 if (spin->si_syllable != NULL)
7705 {
7706 putc(SN_SYLLABLE, fd); /* <sectionID> */
7707 putc(0, fd); /* <sectionflags> */
7708
7709 l = STRLEN(spin->si_syllable);
7710 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7711 fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd); /* <syllable> */
7712 }
7713
7714 /* end of <SECTIONS> */
7715 putc(SN_END, fd); /* <sectionend> */
7716
Bram Moolenaar50cde822005-06-05 21:54:54 +00007717
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007718 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007719 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007720 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007721 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007722 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007723 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007724 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007725 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007726 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007727 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007728 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007729 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007730
Bram Moolenaar0c405862005-06-22 22:26:26 +00007731 /* Clear the index and wnode fields in the tree. */
7732 clear_node(tree);
7733
Bram Moolenaar51485f02005-06-04 21:55:20 +00007734 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00007735 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00007736 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007737 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007738
Bram Moolenaar51485f02005-06-04 21:55:20 +00007739 /* number of nodes in 4 bytes */
7740 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00007741 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007742
Bram Moolenaar51485f02005-06-04 21:55:20 +00007743 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007744 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007745 }
7746
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007747 /* Write another byte to check for errors. */
7748 if (putc(0, fd) == EOF)
7749 retval = FAIL;
7750
7751 if (fclose(fd) == EOF)
7752 retval = FAIL;
7753
7754 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007755}
7756
7757/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007758 * Clear the index and wnode fields of "node", it siblings and its
7759 * children. This is needed because they are a union with other items to save
7760 * space.
7761 */
7762 static void
7763clear_node(node)
7764 wordnode_T *node;
7765{
7766 wordnode_T *np;
7767
7768 if (node != NULL)
7769 for (np = node; np != NULL; np = np->wn_sibling)
7770 {
7771 np->wn_u1.index = 0;
7772 np->wn_u2.wnode = NULL;
7773
7774 if (np->wn_byte != NUL)
7775 clear_node(np->wn_child);
7776 }
7777}
7778
7779
7780/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007781 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007782 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00007783 * This first writes the list of possible bytes (siblings). Then for each
7784 * byte recursively write the children.
7785 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00007786 * NOTE: The code here must match the code in read_tree_node(), since
7787 * assumptions are made about the indexes (so that we don't have to write them
7788 * in the file).
Bram Moolenaar51485f02005-06-04 21:55:20 +00007789 *
7790 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007791 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007792 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00007793put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007794 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007795 wordnode_T *node;
7796 int index;
7797 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007798 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007799{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007800 int newindex = index;
7801 int siblingcount = 0;
7802 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007803 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007804
Bram Moolenaar51485f02005-06-04 21:55:20 +00007805 /* If "node" is zero the tree is empty. */
7806 if (node == NULL)
7807 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007808
Bram Moolenaar51485f02005-06-04 21:55:20 +00007809 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007810 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007811
7812 /* Count the number of siblings. */
7813 for (np = node; np != NULL; np = np->wn_sibling)
7814 ++siblingcount;
7815
7816 /* Write the sibling count. */
7817 if (fd != NULL)
7818 putc(siblingcount, fd); /* <siblingcount> */
7819
7820 /* Write each sibling byte and optionally extra info. */
7821 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007822 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007823 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007824 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007825 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007826 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007827 /* For a NUL byte (end of word) write the flags etc. */
7828 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007829 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007830 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007831 * associated condition nr (stored in wn_region). The
7832 * byte value is misused to store the "rare" and "not
7833 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00007834 if (np->wn_flags == (short_u)PFX_FLAGS)
7835 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007836 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00007837 {
7838 putc(BY_FLAGS, fd); /* <byte> */
7839 putc(np->wn_flags, fd); /* <pflags> */
7840 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007841 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007842 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007843 }
7844 else
7845 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007846 /* For word trees we write the flag/region items. */
7847 flags = np->wn_flags;
7848 if (regionmask != 0 && np->wn_region != regionmask)
7849 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007850 if (np->wn_affixID != 0)
7851 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007852 if (flags == 0)
7853 {
7854 /* word without flags or region */
7855 putc(BY_NOFLAGS, fd); /* <byte> */
7856 }
7857 else
7858 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007859 if (np->wn_flags >= 0x100)
7860 {
7861 putc(BY_FLAGS2, fd); /* <byte> */
7862 putc(flags, fd); /* <flags> */
7863 putc((unsigned)flags >> 8, fd); /* <flags2> */
7864 }
7865 else
7866 {
7867 putc(BY_FLAGS, fd); /* <byte> */
7868 putc(flags, fd); /* <flags> */
7869 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007870 if (flags & WF_REGION)
7871 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007872 if (flags & WF_AFX)
7873 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007874 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007875 }
7876 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007877 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007878 else
7879 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00007880 if (np->wn_child->wn_u1.index != 0
7881 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007882 {
7883 /* The child is written elsewhere, write the reference. */
7884 if (fd != NULL)
7885 {
7886 putc(BY_INDEX, fd); /* <byte> */
7887 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007888 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007889 }
7890 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00007891 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007892 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007893 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007894
Bram Moolenaar51485f02005-06-04 21:55:20 +00007895 if (fd != NULL)
7896 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
7897 {
7898 EMSG(_(e_write));
7899 return 0;
7900 }
7901 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007902 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007903
7904 /* Space used in the array when reading: one for each sibling and one for
7905 * the count. */
7906 newindex += siblingcount + 1;
7907
7908 /* Recursively dump the children of each sibling. */
7909 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007910 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
7911 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007912 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007913
7914 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007915}
7916
7917
7918/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00007919 * ":mkspell [-ascii] outfile infile ..."
7920 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007921 */
7922 void
7923ex_mkspell(eap)
7924 exarg_T *eap;
7925{
7926 int fcount;
7927 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007928 char_u *arg = eap->arg;
7929 int ascii = FALSE;
7930
7931 if (STRNCMP(arg, "-ascii", 6) == 0)
7932 {
7933 ascii = TRUE;
7934 arg = skipwhite(arg + 6);
7935 }
7936
7937 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
7938 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
7939 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007940 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007941 FreeWild(fcount, fnames);
7942 }
7943}
7944
7945/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00007946 * Create the .sug file.
7947 * Uses the soundfold info in "spin".
7948 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
7949 */
7950 static void
7951spell_make_sugfile(spin, wfname)
7952 spellinfo_T *spin;
7953 char_u *wfname;
7954{
7955 char_u fname[MAXPATHL];
7956 int len;
7957 slang_T *slang;
7958 int free_slang = FALSE;
7959
7960 /*
7961 * Read back the .spl file that was written. This fills the required
7962 * info for soundfolding. This also uses less memory than the
7963 * pointer-linked version of the trie. And it avoids having two versions
7964 * of the code for the soundfolding stuff.
7965 * It might have been done already by spell_reload_one().
7966 */
7967 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
7968 if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME)
7969 break;
7970 if (slang == NULL)
7971 {
7972 spell_message(spin, (char_u *)_("Reading back spell file..."));
7973 slang = spell_load_file(wfname, NULL, NULL, FALSE);
7974 if (slang == NULL)
7975 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007976 free_slang = TRUE;
7977 }
7978
7979 /*
7980 * Clear the info in "spin" that is used.
7981 */
7982 spin->si_blocks = NULL;
7983 spin->si_blocks_cnt = 0;
7984 spin->si_compress_cnt = 0; /* will stay at 0 all the time*/
7985 spin->si_free_count = 0;
7986 spin->si_first_free = NULL;
7987 spin->si_foldwcount = 0;
7988
7989 /*
7990 * Go through the trie of good words, soundfold each word and add it to
7991 * the soundfold trie.
7992 */
7993 spell_message(spin, (char_u *)_("Performing soundfolding..."));
7994 if (sug_filltree(spin, slang) == FAIL)
7995 goto theend;
7996
7997 /*
7998 * Create the table which links each soundfold word with a list of the
7999 * good words it may come from. Creates buffer "spin->si_spellbuf".
8000 * This also removes the wordnr from the NUL byte entries to make
8001 * compression possible.
8002 */
8003 if (sug_maketable(spin) == FAIL)
8004 goto theend;
8005
8006 smsg((char_u *)_("Number of words after soundfolding: %ld"),
8007 (long)spin->si_spellbuf->b_ml.ml_line_count);
8008
8009 /*
8010 * Compress the soundfold trie.
8011 */
8012 spell_message(spin, (char_u *)_(msg_compressing));
8013 wordtree_compress(spin, spin->si_foldroot);
8014
8015 /*
8016 * Write the .sug file.
8017 * Make the file name by changing ".spl" to ".sug".
8018 */
8019 STRCPY(fname, wfname);
8020 len = STRLEN(fname);
8021 fname[len - 2] = 'u';
8022 fname[len - 1] = 'g';
8023 sug_write(spin, fname);
8024
8025theend:
8026 if (free_slang)
8027 slang_free(slang);
8028 free_blocks(spin->si_blocks);
8029 close_spellbuf(spin->si_spellbuf);
8030}
8031
8032/*
8033 * Build the soundfold trie for language "slang".
8034 */
8035 static int
8036sug_filltree(spin, slang)
8037 spellinfo_T *spin;
8038 slang_T *slang;
8039{
8040 char_u *byts;
8041 idx_T *idxs;
8042 int depth;
8043 idx_T arridx[MAXWLEN];
8044 int curi[MAXWLEN];
8045 char_u tword[MAXWLEN];
8046 char_u tsalword[MAXWLEN];
8047 int c;
8048 idx_T n;
8049 unsigned words_done = 0;
8050 int wordcount[MAXWLEN];
8051
8052 /* We use si_foldroot for the souldfolded trie. */
8053 spin->si_foldroot = wordtree_alloc(spin);
8054 if (spin->si_foldroot == NULL)
8055 return FAIL;
8056
8057 /* let tree_add_word() know we're adding to the soundfolded tree */
8058 spin->si_sugtree = TRUE;
8059
8060 /*
8061 * Go through the whole case-folded tree, soundfold each word and put it
8062 * in the trie.
8063 */
8064 byts = slang->sl_fbyts;
8065 idxs = slang->sl_fidxs;
8066
8067 arridx[0] = 0;
8068 curi[0] = 1;
8069 wordcount[0] = 0;
8070
8071 depth = 0;
8072 while (depth >= 0 && !got_int)
8073 {
8074 if (curi[depth] > byts[arridx[depth]])
8075 {
8076 /* Done all bytes at this node, go up one level. */
8077 idxs[arridx[depth]] = wordcount[depth];
8078 if (depth > 0)
8079 wordcount[depth - 1] += wordcount[depth];
8080
8081 --depth;
8082 line_breakcheck();
8083 }
8084 else
8085 {
8086
8087 /* Do one more byte at this node. */
8088 n = arridx[depth] + curi[depth];
8089 ++curi[depth];
8090
8091 c = byts[n];
8092 if (c == 0)
8093 {
8094 /* Sound-fold the word. */
8095 tword[depth] = NUL;
8096 spell_soundfold(slang, tword, TRUE, tsalword);
8097
8098 /* We use the "flags" field for the MSB of the wordnr,
8099 * "region" for the LSB of the wordnr. */
8100 if (tree_add_word(spin, tsalword, spin->si_foldroot,
8101 words_done >> 16, words_done & 0xffff,
8102 0) == FAIL)
8103 return FAIL;
8104
8105 ++words_done;
8106 ++wordcount[depth];
8107
8108 /* Reset the block count each time to avoid compression
8109 * kicking in. */
8110 spin->si_blocks_cnt = 0;
8111
8112 /* Skip over any other NUL bytes (same word with different
8113 * flags). */
8114 while (byts[n + 1] == 0)
8115 {
8116 ++n;
8117 ++curi[depth];
8118 }
8119 }
8120 else
8121 {
8122 /* Normal char, go one level deeper. */
8123 tword[depth++] = c;
8124 arridx[depth] = idxs[n];
8125 curi[depth] = 1;
8126 wordcount[depth] = 0;
8127 }
8128 }
8129 }
8130
8131 smsg((char_u *)_("Total number of words: %d"), words_done);
8132
8133 return OK;
8134}
8135
8136/*
8137 * Make the table that links each word in the soundfold trie to the words it
8138 * can be produced from.
8139 * This is not unlike lines in a file, thus use a memfile to be able to access
8140 * the table efficiently.
8141 * Returns FAIL when out of memory.
8142 */
8143 static int
8144sug_maketable(spin)
8145 spellinfo_T *spin;
8146{
8147 garray_T ga;
8148 int res = OK;
8149
8150 /* Allocate a buffer, open a memline for it and create the swap file
8151 * (uses a temp file, not a .swp file). */
8152 spin->si_spellbuf = open_spellbuf();
8153 if (spin->si_spellbuf == NULL)
8154 return FAIL;
8155
8156 /* Use a buffer to store the line info, avoids allocating many small
8157 * pieces of memory. */
8158 ga_init2(&ga, 1, 100);
8159
8160 /* recursively go through the tree */
8161 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
8162 res = FAIL;
8163
8164 ga_clear(&ga);
8165 return res;
8166}
8167
8168/*
8169 * Fill the table for one node and its children.
8170 * Returns the wordnr at the start of the node.
8171 * Returns -1 when out of memory.
8172 */
8173 static int
8174sug_filltable(spin, node, startwordnr, gap)
8175 spellinfo_T *spin;
8176 wordnode_T *node;
8177 int startwordnr;
8178 garray_T *gap; /* place to store line of numbers */
8179{
8180 wordnode_T *p, *np;
8181 int wordnr = startwordnr;
8182 int nr;
8183 int prev_nr;
8184
8185 for (p = node; p != NULL; p = p->wn_sibling)
8186 {
8187 if (p->wn_byte == NUL)
8188 {
8189 gap->ga_len = 0;
8190 prev_nr = 0;
8191 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
8192 {
8193 if (ga_grow(gap, 10) == FAIL)
8194 return -1;
8195
8196 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
8197 /* Compute the offset from the previous nr and store the
8198 * offset in a way that it takes a minimum number of bytes.
8199 * It's a bit like utf-8, but without the need to mark
8200 * following bytes. */
8201 nr -= prev_nr;
8202 prev_nr += nr;
8203 gap->ga_len += offset2bytes(nr,
8204 (char_u *)gap->ga_data + gap->ga_len);
8205 }
8206
8207 /* add the NUL byte */
8208 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
8209
8210 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
8211 gap->ga_data, gap->ga_len, TRUE) == FAIL)
8212 return -1;
8213 ++wordnr;
8214
8215 /* Remove extra NUL entries, we no longer need them. We don't
8216 * bother freeing the nodes, the won't be reused anyway. */
8217 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
8218 p->wn_sibling = p->wn_sibling->wn_sibling;
8219
8220 /* Clear the flags on the remaining NUL node, so that compression
8221 * works a lot better. */
8222 p->wn_flags = 0;
8223 p->wn_region = 0;
8224 }
8225 else
8226 {
8227 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
8228 if (wordnr == -1)
8229 return -1;
8230 }
8231 }
8232 return wordnr;
8233}
8234
8235/*
8236 * Convert an offset into a minimal number of bytes.
8237 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
8238 * bytes.
8239 */
8240 static int
8241offset2bytes(nr, buf)
8242 int nr;
8243 char_u *buf;
8244{
8245 int rem;
8246 int b1, b2, b3, b4;
8247
8248 /* Split the number in parts of base 255. We need to avoid NUL bytes. */
8249 b1 = nr % 255 + 1;
8250 rem = nr / 255;
8251 b2 = rem % 255 + 1;
8252 rem = rem / 255;
8253 b3 = rem % 255 + 1;
8254 b4 = rem / 255 + 1;
8255
8256 if (b4 > 1 || b3 > 0x1f) /* 4 bytes */
8257 {
8258 buf[0] = 0xe0 + b4;
8259 buf[1] = b3;
8260 buf[2] = b2;
8261 buf[3] = b1;
8262 return 4;
8263 }
8264 if (b3 > 1 || b2 > 0x3f ) /* 3 bytes */
8265 {
8266 buf[0] = 0xc0 + b3;
8267 buf[1] = b2;
8268 buf[2] = b1;
8269 return 3;
8270 }
8271 if (b2 > 1 || b1 > 0x7f ) /* 2 bytes */
8272 {
8273 buf[0] = 0x80 + b2;
8274 buf[1] = b1;
8275 return 2;
8276 }
8277 /* 1 byte */
8278 buf[0] = b1;
8279 return 1;
8280}
8281
8282/*
8283 * Opposite of offset2bytes().
8284 * "pp" points to the bytes and is advanced over it.
8285 * Returns the offset.
8286 */
8287 static int
8288bytes2offset(pp)
8289 char_u **pp;
8290{
8291 char_u *p = *pp;
8292 int nr;
8293 int c;
8294
8295 c = *p++;
8296 if ((c & 0x80) == 0x00) /* 1 byte */
8297 {
8298 nr = c - 1;
8299 }
8300 else if ((c & 0xc0) == 0x80) /* 2 bytes */
8301 {
8302 nr = (c & 0x3f) - 1;
8303 nr = nr * 255 + (*p++ - 1);
8304 }
8305 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
8306 {
8307 nr = (c & 0x1f) - 1;
8308 nr = nr * 255 + (*p++ - 1);
8309 nr = nr * 255 + (*p++ - 1);
8310 }
8311 else /* 4 bytes */
8312 {
8313 nr = (c & 0x0f) - 1;
8314 nr = nr * 255 + (*p++ - 1);
8315 nr = nr * 255 + (*p++ - 1);
8316 nr = nr * 255 + (*p++ - 1);
8317 }
8318
8319 *pp = p;
8320 return nr;
8321}
8322
8323/*
8324 * Write the .sug file in "fname".
8325 */
8326 static void
8327sug_write(spin, fname)
8328 spellinfo_T *spin;
8329 char_u *fname;
8330{
8331 FILE *fd;
8332 wordnode_T *tree;
8333 int nodecount;
8334 int wcount;
8335 char_u *line;
8336 linenr_T lnum;
8337 int len;
8338
8339 /* Create the file. Note that an existing file is silently overwritten! */
8340 fd = mch_fopen((char *)fname, "w");
8341 if (fd == NULL)
8342 {
8343 EMSG2(_(e_notopen), fname);
8344 return;
8345 }
8346
8347 vim_snprintf((char *)IObuff, IOSIZE,
8348 _("Writing suggestion file %s ..."), fname);
8349 spell_message(spin, IObuff);
8350
8351 /*
8352 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
8353 */
8354 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) /* <fileID> */
8355 {
8356 EMSG(_(e_write));
8357 goto theend;
8358 }
8359 putc(VIMSUGVERSION, fd); /* <versionnr> */
8360
8361 /* Write si_sugtime to the file. */
8362 put_sugtime(spin, fd); /* <timestamp> */
8363
8364 /*
8365 * <SUGWORDTREE>
8366 */
8367 spin->si_memtot = 0;
8368 tree = spin->si_foldroot->wn_sibling;
8369
8370 /* Clear the index and wnode fields in the tree. */
8371 clear_node(tree);
8372
8373 /* Count the number of nodes. Needed to be able to allocate the
8374 * memory when reading the nodes. Also fills in index for shared
8375 * nodes. */
8376 nodecount = put_node(NULL, tree, 0, 0, FALSE);
8377
8378 /* number of nodes in 4 bytes */
8379 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
8380 spin->si_memtot += nodecount + nodecount * sizeof(int);
8381
8382 /* Write the nodes. */
8383 (void)put_node(fd, tree, 0, 0, FALSE);
8384
8385 /*
8386 * <SUGTABLE>: <sugwcount> <sugline> ...
8387 */
8388 wcount = spin->si_spellbuf->b_ml.ml_line_count;
8389 put_bytes(fd, (long_u)wcount, 4); /* <sugwcount> */
8390
8391 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
8392 {
8393 /* <sugline>: <sugnr> ... NUL */
8394 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
8395 len = STRLEN(line) + 1;
8396 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
8397 {
8398 EMSG(_(e_write));
8399 goto theend;
8400 }
8401 spin->si_memtot += len;
8402 }
8403
8404 /* Write another byte to check for errors. */
8405 if (putc(0, fd) == EOF)
8406 EMSG(_(e_write));
8407
8408 vim_snprintf((char *)IObuff, IOSIZE,
8409 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
8410 spell_message(spin, IObuff);
8411
8412theend:
8413 /* close the file */
8414 fclose(fd);
8415}
8416
8417/*
8418 * Open a spell buffer. This is a nameless buffer that is not in the buffer
8419 * list and only contains text lines. Can use a swapfile to reduce memory
8420 * use.
8421 * Most other fields are invalid! Esp. watch out for string options being
8422 * NULL and there is no undo info.
8423 * Returns NULL when out of memory.
8424 */
8425 static buf_T *
8426open_spellbuf()
8427{
8428 buf_T *buf;
8429
8430 buf = (buf_T *)alloc_clear(sizeof(buf_T));
8431 if (buf != NULL)
8432 {
8433 buf->b_spell = TRUE;
8434 buf->b_p_swf = TRUE; /* may create a swap file */
8435 ml_open(buf);
8436 ml_open_file(buf); /* create swap file now */
8437 }
8438 return buf;
8439}
8440
8441/*
8442 * Close the buffer used for spell info.
8443 */
8444 static void
8445close_spellbuf(buf)
8446 buf_T *buf;
8447{
8448 if (buf != NULL)
8449 {
8450 ml_close(buf, TRUE);
8451 vim_free(buf);
8452 }
8453}
8454
8455
8456/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008457 * Create a Vim spell file from one or more word lists.
8458 * "fnames[0]" is the output file name.
8459 * "fnames[fcount - 1]" is the last input file name.
8460 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
8461 * and ".spl" is appended to make the output file name.
8462 */
8463 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008464mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00008465 int fcount;
8466 char_u **fnames;
8467 int ascii; /* -ascii argument given */
8468 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008469 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008470{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008471 char_u fname[MAXPATHL];
8472 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00008473 char_u **innames;
8474 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008475 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008476 int i;
8477 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008478 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008479 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008480 spellinfo_T spin;
8481
8482 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008483 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008484 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008485 spin.si_followup = TRUE;
8486 spin.si_rem_accents = TRUE;
8487 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008488 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008489 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
8490 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008491 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008492 hash_init(&spin.si_commonwords);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008493 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008494
Bram Moolenaarb765d632005-06-07 21:00:02 +00008495 /* default: fnames[0] is output file, following are input files */
8496 innames = &fnames[1];
8497 incount = fcount - 1;
8498
8499 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00008500 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008501 len = STRLEN(fnames[0]);
8502 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
8503 {
8504 /* For ":mkspell path/en.latin1.add" output file is
8505 * "path/en.latin1.add.spl". */
8506 innames = &fnames[0];
8507 incount = 1;
8508 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
8509 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008510 else if (fcount == 1)
8511 {
8512 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
8513 innames = &fnames[0];
8514 incount = 1;
8515 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
8516 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
8517 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00008518 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
8519 {
8520 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008521 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008522 }
8523 else
8524 /* Name should be language, make the file name from it. */
8525 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
8526 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
8527
8528 /* Check for .ascii.spl. */
8529 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
8530 spin.si_ascii = TRUE;
8531
8532 /* Check for .add.spl. */
8533 if (strstr((char *)gettail(wfname), ".add.") != NULL)
8534 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00008535 }
8536
Bram Moolenaarb765d632005-06-07 21:00:02 +00008537 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008538 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008539 else if (vim_strchr(gettail(wfname), '_') != NULL)
8540 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00008541 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008542 EMSG(_("E754: Only up to 8 regions supported"));
8543 else
8544 {
8545 /* Check for overwriting before doing things that may take a lot of
8546 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008547 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008548 {
8549 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00008550 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008551 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00008552 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008553 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008554 EMSG2(_(e_isadir2), wfname);
8555 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008556 }
8557
8558 /*
8559 * Init the aff and dic pointers.
8560 * Get the region names if there are more than 2 arguments.
8561 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008562 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008563 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008564 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008565
Bram Moolenaar3982c542005-06-08 21:56:31 +00008566 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008567 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008568 len = STRLEN(innames[i]);
8569 if (STRLEN(gettail(innames[i])) < 5
8570 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008571 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008572 EMSG2(_("E755: Invalid region in %s"), innames[i]);
8573 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008574 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00008575 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
8576 spin.si_region_name[i * 2 + 1] =
8577 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008578 }
8579 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00008580 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008581
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008582 spin.si_foldroot = wordtree_alloc(&spin);
8583 spin.si_keeproot = wordtree_alloc(&spin);
8584 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008585 if (spin.si_foldroot == NULL
8586 || spin.si_keeproot == NULL
8587 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008588 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008589 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008590 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008591 }
8592
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008593 /* When not producing a .add.spl file clear the character table when
8594 * we encounter one in the .aff file. This means we dump the current
8595 * one in the .spl file if the .aff file doesn't define one. That's
8596 * better than guessing the contents, the table will match a
8597 * previously loaded spell file. */
8598 if (!spin.si_add)
8599 spin.si_clear_chartab = TRUE;
8600
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008601 /*
8602 * Read all the .aff and .dic files.
8603 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00008604 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008605 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008606 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008607 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008608 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008609 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008610
Bram Moolenaarb765d632005-06-07 21:00:02 +00008611 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008612 if (mch_stat((char *)fname, &st) >= 0)
8613 {
8614 /* Read the .aff file. Will init "spin->si_conv" based on the
8615 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008616 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008617 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008618 error = TRUE;
8619 else
8620 {
8621 /* Read the .dic file and store the words in the trees. */
8622 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00008623 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008624 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008625 error = TRUE;
8626 }
8627 }
8628 else
8629 {
8630 /* No .aff file, try reading the file as a word list. Store
8631 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008632 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008633 error = TRUE;
8634 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008635
Bram Moolenaarb765d632005-06-07 21:00:02 +00008636#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008637 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008638 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008639#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008640 }
8641
Bram Moolenaar78622822005-08-23 21:00:13 +00008642 if (spin.si_compflags != NULL && spin.si_nobreak)
8643 MSG(_("Warning: both compounding and NOBREAK specified"));
8644
Bram Moolenaar4770d092006-01-12 23:22:24 +00008645 if (!error && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008646 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008647 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00008648 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008649 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008650 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008651 wordtree_compress(&spin, spin.si_foldroot);
8652 wordtree_compress(&spin, spin.si_keeproot);
8653 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008654 }
8655
Bram Moolenaar4770d092006-01-12 23:22:24 +00008656 if (!error && !got_int)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008657 {
8658 /*
8659 * Write the info in the spell file.
8660 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008661 vim_snprintf((char *)IObuff, IOSIZE,
8662 _("Writing spell file %s ..."), wfname);
8663 spell_message(&spin, IObuff);
Bram Moolenaar50cde822005-06-05 21:54:54 +00008664
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008665 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008666
Bram Moolenaar4770d092006-01-12 23:22:24 +00008667 spell_message(&spin, (char_u *)_("Done!"));
8668 vim_snprintf((char *)IObuff, IOSIZE,
8669 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
8670 spell_message(&spin, IObuff);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008671
Bram Moolenaar4770d092006-01-12 23:22:24 +00008672 /*
8673 * If the file is loaded need to reload it.
8674 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008675 if (!error)
8676 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008677 }
8678
8679 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008680 ga_clear(&spin.si_rep);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008681 ga_clear(&spin.si_repsal);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008682 ga_clear(&spin.si_sal);
8683 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008684 ga_clear(&spin.si_prefcond);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008685 hash_clear_all(&spin.si_commonwords, 0);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008686
8687 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008688 for (i = 0; i < incount; ++i)
8689 if (afile[i] != NULL)
8690 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008691
8692 /* Free all the bits and pieces at once. */
8693 free_blocks(spin.si_blocks);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008694
8695 /*
8696 * If there is soundfolding info and no NOSUGFILE item create the
8697 * .sug file with the soundfolded word trie.
8698 */
8699 if (spin.si_sugtime != 0 && !error && !got_int)
8700 spell_make_sugfile(&spin, wfname);
8701
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008702 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008703}
8704
Bram Moolenaar4770d092006-01-12 23:22:24 +00008705/*
8706 * Display a message for spell file processing when 'verbose' is set or using
8707 * ":mkspell". "str" can be IObuff.
8708 */
8709 static void
8710spell_message(spin, str)
8711 spellinfo_T *spin;
8712 char_u *str;
8713{
8714 if (spin->si_verbose || p_verbose > 2)
8715 {
8716 if (!spin->si_verbose)
8717 verbose_enter();
8718 MSG(str);
8719 out_flush();
8720 if (!spin->si_verbose)
8721 verbose_leave();
8722 }
8723}
Bram Moolenaarb765d632005-06-07 21:00:02 +00008724
8725/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008726 * ":[count]spellgood {word}"
8727 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00008728 */
8729 void
8730ex_spell(eap)
8731 exarg_T *eap;
8732{
Bram Moolenaar7887d882005-07-01 22:33:52 +00008733 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008734 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008735}
8736
8737/*
8738 * Add "word[len]" to 'spellfile' as a good or bad word.
8739 */
8740 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008741spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00008742 char_u *word;
8743 int len;
8744 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008745 int index; /* "zG" and "zW": zero, otherwise index in
8746 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008747{
8748 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008749 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008750 int new_spf = FALSE;
Bram Moolenaar7887d882005-07-01 22:33:52 +00008751 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008752 char_u fnamebuf[MAXPATHL];
8753 char_u line[MAXWLEN * 2];
8754 long fpos, fpos_next = 0;
8755 int i;
8756 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008757
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008758 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008759 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008760 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00008761 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008762 int_wordlist = vim_tempname('s');
8763 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00008764 return;
8765 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008766 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008767 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00008768 else
8769 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00008770 /* If 'spellfile' isn't set figure out a good default value. */
8771 if (*curbuf->b_p_spf == NUL)
8772 {
8773 init_spellfile();
8774 new_spf = TRUE;
8775 }
8776
8777 if (*curbuf->b_p_spf == NUL)
8778 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008779 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00008780 return;
8781 }
8782
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008783 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
8784 {
8785 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
8786 if (i == index)
8787 break;
8788 if (*spf == NUL)
8789 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00008790 EMSGN(_("E765: 'spellfile' does not have %ld entries"), index);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008791 return;
8792 }
8793 }
8794
Bram Moolenaarb765d632005-06-07 21:00:02 +00008795 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008796 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008797 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
8798 buf = NULL;
8799 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00008800 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00008801 EMSG(_(e_bufloaded));
8802 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008803 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00008804
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008805 fname = fnamebuf;
8806 }
8807
8808 if (bad)
8809 {
8810 /* When the word also appears as good word we need to remove that one,
8811 * since its flags sort before the one with WF_BANNED. */
8812 fd = mch_fopen((char *)fname, "r");
8813 if (fd != NULL)
8814 {
8815 while (!vim_fgets(line, MAXWLEN * 2, fd))
8816 {
8817 fpos = fpos_next;
8818 fpos_next = ftell(fd);
8819 if (STRNCMP(word, line, len) == 0
8820 && (line[len] == '/' || line[len] < ' '))
8821 {
8822 /* Found duplicate word. Remove it by writing a '#' at
8823 * the start of the line. Mixing reading and writing
8824 * doesn't work for all systems, close the file first. */
8825 fclose(fd);
8826 fd = mch_fopen((char *)fname, "r+");
8827 if (fd == NULL)
8828 break;
8829 if (fseek(fd, fpos, SEEK_SET) == 0)
8830 fputc('#', fd);
8831 fseek(fd, fpos_next, SEEK_SET);
8832 }
8833 }
8834 fclose(fd);
8835 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00008836 }
8837
8838 fd = mch_fopen((char *)fname, "a");
8839 if (fd == NULL && new_spf)
8840 {
8841 /* We just initialized the 'spellfile' option and can't open the file.
8842 * We may need to create the "spell" directory first. We already
8843 * checked the runtime directory is writable in init_spellfile(). */
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00008844 if (!dir_of_file_exists(fname))
Bram Moolenaar7887d882005-07-01 22:33:52 +00008845 {
8846 /* The directory doesn't exist. Try creating it and opening the
8847 * file again. */
8848 vim_mkdir(NameBuff, 0755);
8849 fd = mch_fopen((char *)fname, "a");
8850 }
8851 }
8852
8853 if (fd == NULL)
8854 EMSG2(_(e_notopen), fname);
8855 else
8856 {
8857 if (bad)
8858 fprintf(fd, "%.*s/!\n", len, word);
8859 else
8860 fprintf(fd, "%.*s\n", len, word);
8861 fclose(fd);
8862
8863 /* Update the .add.spl file. */
8864 mkspell(1, &fname, FALSE, TRUE, TRUE);
8865
8866 /* If the .add file is edited somewhere, reload it. */
8867 if (buf != NULL)
Bram Moolenaarea8bd732006-01-14 21:15:59 +00008868 buf_reload(buf, buf->b_orig_mode);
Bram Moolenaar7887d882005-07-01 22:33:52 +00008869
8870 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008871 }
8872}
8873
8874/*
8875 * Initialize 'spellfile' for the current buffer.
8876 */
8877 static void
8878init_spellfile()
8879{
8880 char_u buf[MAXPATHL];
8881 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008882 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008883 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008884 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00008885 int aspath = FALSE;
8886 char_u *lstart = curbuf->b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008887
8888 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
8889 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00008890 /* Find the end of the language name. Exclude the region. If there
8891 * is a path separator remember the start of the tail. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008892 for (lend = curbuf->b_p_spl; *lend != NUL
8893 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00008894 if (vim_ispathsep(*lend))
8895 {
8896 aspath = TRUE;
8897 lstart = lend + 1;
8898 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008899
8900 /* Loop over all entries in 'runtimepath'. Use the first one where we
8901 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008902 rtp = p_rtp;
8903 while (*rtp != NUL)
8904 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00008905 if (aspath)
8906 /* Use directory of an entry with path, e.g., for
8907 * "/dir/lg.utf-8.spl" use "/dir". */
8908 vim_strncpy(buf, curbuf->b_p_spl, lstart - curbuf->b_p_spl - 1);
8909 else
8910 /* Copy the path from 'runtimepath' to buf[]. */
8911 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00008912 if (filewritable(buf) == 2)
8913 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00008914 /* Use the first language name from 'spelllang' and the
8915 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00008916 if (aspath)
8917 vim_strncpy(buf, curbuf->b_p_spl, lend - curbuf->b_p_spl);
8918 else
8919 {
8920 l = STRLEN(buf);
8921 vim_snprintf((char *)buf + l, MAXPATHL - l,
8922 "/spell/%.*s", (int)(lend - lstart), lstart);
8923 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00008924 l = STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00008925 fname = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang->sl_fname;
8926 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
8927 fname != NULL
8928 && strstr((char *)gettail(fname), ".ascii.") != NULL
8929 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00008930 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
8931 break;
8932 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00008933 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008934 }
8935 }
8936}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008937
Bram Moolenaar51485f02005-06-04 21:55:20 +00008938
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008939/*
8940 * Init the chartab used for spelling for ASCII.
8941 * EBCDIC is not supported!
8942 */
8943 static void
8944clear_spell_chartab(sp)
8945 spelltab_T *sp;
8946{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008947 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008948
8949 /* Init everything to FALSE. */
8950 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
8951 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
8952 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008953 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008954 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008955 sp->st_upper[i] = i;
8956 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008957
8958 /* We include digits. A word shouldn't start with a digit, but handling
8959 * that is done separately. */
8960 for (i = '0'; i <= '9'; ++i)
8961 sp->st_isw[i] = TRUE;
8962 for (i = 'A'; i <= 'Z'; ++i)
8963 {
8964 sp->st_isw[i] = TRUE;
8965 sp->st_isu[i] = TRUE;
8966 sp->st_fold[i] = i + 0x20;
8967 }
8968 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008969 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008970 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008971 sp->st_upper[i] = i - 0x20;
8972 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008973}
8974
8975/*
8976 * Init the chartab used for spelling. Only depends on 'encoding'.
8977 * Called once while starting up and when 'encoding' changes.
8978 * The default is to use isalpha(), but the spell file should define the word
8979 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008980 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008981 */
8982 void
8983init_spell_chartab()
8984{
8985 int i;
8986
8987 did_set_spelltab = FALSE;
8988 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008989#ifdef FEAT_MBYTE
8990 if (enc_dbcs)
8991 {
8992 /* DBCS: assume double-wide characters are word characters. */
8993 for (i = 128; i <= 255; ++i)
8994 if (MB_BYTE2LEN(i) == 2)
8995 spelltab.st_isw[i] = TRUE;
8996 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008997 else if (enc_utf8)
8998 {
8999 for (i = 128; i < 256; ++i)
9000 {
9001 spelltab.st_isu[i] = utf_isupper(i);
9002 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
9003 spelltab.st_fold[i] = utf_fold(i);
9004 spelltab.st_upper[i] = utf_toupper(i);
9005 }
9006 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009007 else
9008#endif
9009 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009010 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009011 for (i = 128; i < 256; ++i)
9012 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009013 if (MB_ISUPPER(i))
9014 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009015 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009016 spelltab.st_isu[i] = TRUE;
9017 spelltab.st_fold[i] = MB_TOLOWER(i);
9018 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009019 else if (MB_ISLOWER(i))
9020 {
9021 spelltab.st_isw[i] = TRUE;
9022 spelltab.st_upper[i] = MB_TOUPPER(i);
9023 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009024 }
9025 }
9026}
9027
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009028/*
9029 * Set the spell character tables from strings in the affix file.
9030 */
9031 static int
9032set_spell_chartab(fol, low, upp)
9033 char_u *fol;
9034 char_u *low;
9035 char_u *upp;
9036{
9037 /* We build the new tables here first, so that we can compare with the
9038 * previous one. */
9039 spelltab_T new_st;
9040 char_u *pf = fol, *pl = low, *pu = upp;
9041 int f, l, u;
9042
9043 clear_spell_chartab(&new_st);
9044
9045 while (*pf != NUL)
9046 {
9047 if (*pl == NUL || *pu == NUL)
9048 {
9049 EMSG(_(e_affform));
9050 return FAIL;
9051 }
9052#ifdef FEAT_MBYTE
9053 f = mb_ptr2char_adv(&pf);
9054 l = mb_ptr2char_adv(&pl);
9055 u = mb_ptr2char_adv(&pu);
9056#else
9057 f = *pf++;
9058 l = *pl++;
9059 u = *pu++;
9060#endif
9061 /* Every character that appears is a word character. */
9062 if (f < 256)
9063 new_st.st_isw[f] = TRUE;
9064 if (l < 256)
9065 new_st.st_isw[l] = TRUE;
9066 if (u < 256)
9067 new_st.st_isw[u] = TRUE;
9068
9069 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9070 * case-folding */
9071 if (l < 256 && l != f)
9072 {
9073 if (f >= 256)
9074 {
9075 EMSG(_(e_affrange));
9076 return FAIL;
9077 }
9078 new_st.st_fold[l] = f;
9079 }
9080
9081 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009082 * case-folding, it's upper case and the "UPP" is the upper case of
9083 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009084 if (u < 256 && u != f)
9085 {
9086 if (f >= 256)
9087 {
9088 EMSG(_(e_affrange));
9089 return FAIL;
9090 }
9091 new_st.st_fold[u] = f;
9092 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009093 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009094 }
9095 }
9096
9097 if (*pl != NUL || *pu != NUL)
9098 {
9099 EMSG(_(e_affform));
9100 return FAIL;
9101 }
9102
9103 return set_spell_finish(&new_st);
9104}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009105
9106/*
9107 * Set the spell character tables from strings in the .spl file.
9108 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009109 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009110set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009111 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009112 int cnt; /* length of "flags" */
9113 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009114{
9115 /* We build the new tables here first, so that we can compare with the
9116 * previous one. */
9117 spelltab_T new_st;
9118 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009119 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009120 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009121
9122 clear_spell_chartab(&new_st);
9123
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009124 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009125 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009126 if (i < cnt)
9127 {
9128 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
9129 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
9130 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009131
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009132 if (*p != NUL)
9133 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009134#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009135 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009136#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009137 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009138#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009139 new_st.st_fold[i + 128] = c;
9140 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
9141 new_st.st_upper[c] = i + 128;
9142 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009143 }
9144
Bram Moolenaar5195e452005-08-19 20:32:47 +00009145 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009146}
9147
9148 static int
9149set_spell_finish(new_st)
9150 spelltab_T *new_st;
9151{
9152 int i;
9153
9154 if (did_set_spelltab)
9155 {
9156 /* check that it's the same table */
9157 for (i = 0; i < 256; ++i)
9158 {
9159 if (spelltab.st_isw[i] != new_st->st_isw[i]
9160 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009161 || spelltab.st_fold[i] != new_st->st_fold[i]
9162 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009163 {
9164 EMSG(_("E763: Word characters differ between spell files"));
9165 return FAIL;
9166 }
9167 }
9168 }
9169 else
9170 {
9171 /* copy the new spelltab into the one being used */
9172 spelltab = *new_st;
9173 did_set_spelltab = TRUE;
9174 }
9175
9176 return OK;
9177}
9178
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009179/*
Bram Moolenaarea408852005-06-25 22:49:46 +00009180 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009181 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00009182 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009183 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00009184 */
9185 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009186spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00009187 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009188 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00009189{
Bram Moolenaarea408852005-06-25 22:49:46 +00009190#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009191 char_u *s;
9192 int l;
9193 int c;
9194
9195 if (has_mbyte)
9196 {
9197 l = MB_BYTE2LEN(*p);
9198 s = p;
9199 if (l == 1)
9200 {
9201 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009202 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009203 {
9204 s = p + 1; /* skip a mid-word character */
9205 l = MB_BYTE2LEN(*s);
9206 }
9207 }
9208 else
9209 {
9210 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009211 if (c < 256 ? buf->b_spell_ismw[c]
9212 : (buf->b_spell_ismw_mb != NULL
9213 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009214 {
9215 s = p + l;
9216 l = MB_BYTE2LEN(*s);
9217 }
9218 }
9219
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009220 c = mb_ptr2char(s);
9221 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009222 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009223 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009224 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009225#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009226
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009227 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
9228}
9229
9230/*
9231 * Return TRUE if "p" points to a word character.
9232 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9233 */
9234 static int
9235spell_iswordp_nmw(p)
9236 char_u *p;
9237{
9238#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009239 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009240
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009241 if (has_mbyte)
9242 {
9243 c = mb_ptr2char(p);
9244 if (c > 255)
9245 return mb_get_class(p) >= 2;
9246 return spelltab.st_isw[c];
9247 }
9248#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009249 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00009250}
9251
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009252#ifdef FEAT_MBYTE
9253/*
9254 * Return TRUE if "p" points to a word character.
9255 * Wide version of spell_iswordp().
9256 */
9257 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009258spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009259 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009260 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009261{
9262 int *s;
9263
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009264 if (*p < 256 ? buf->b_spell_ismw[*p]
9265 : (buf->b_spell_ismw_mb != NULL
9266 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009267 s = p + 1;
9268 else
9269 s = p;
9270
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009271 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009272 {
9273 if (enc_utf8)
9274 return utf_class(*s) >= 2;
9275 if (enc_dbcs)
9276 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
9277 return 0;
9278 }
9279 return spelltab.st_isw[*s];
9280}
9281#endif
9282
Bram Moolenaarea408852005-06-25 22:49:46 +00009283/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009284 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009285 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009286 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009287 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009288write_spell_prefcond(fd, gap)
9289 FILE *fd;
9290 garray_T *gap;
9291{
9292 int i;
9293 char_u *p;
9294 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009295 int totlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009296
Bram Moolenaar5195e452005-08-19 20:32:47 +00009297 if (fd != NULL)
9298 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
9299
9300 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009301
9302 for (i = 0; i < gap->ga_len; ++i)
9303 {
9304 /* <prefcond> : <condlen> <condstr> */
9305 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009306 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009307 {
9308 len = STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009309 if (fd != NULL)
9310 {
9311 fputc(len, fd);
9312 fwrite(p, (size_t)len, (size_t)1, fd);
9313 }
9314 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009315 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00009316 else if (fd != NULL)
9317 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009318 }
9319
Bram Moolenaar5195e452005-08-19 20:32:47 +00009320 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009321}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009322
9323/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009324 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
9325 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009326 * When using a multi-byte 'encoding' the length may change!
9327 * Returns FAIL when something wrong.
9328 */
9329 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009330spell_casefold(str, len, buf, buflen)
9331 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009332 int len;
9333 char_u *buf;
9334 int buflen;
9335{
9336 int i;
9337
9338 if (len >= buflen)
9339 {
9340 buf[0] = NUL;
9341 return FAIL; /* result will not fit */
9342 }
9343
9344#ifdef FEAT_MBYTE
9345 if (has_mbyte)
9346 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009347 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009348 char_u *p;
9349 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009350
9351 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009352 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009353 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009354 if (outi + MB_MAXBYTES > buflen)
9355 {
9356 buf[outi] = NUL;
9357 return FAIL;
9358 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009359 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009360 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009361 }
9362 buf[outi] = NUL;
9363 }
9364 else
9365#endif
9366 {
9367 /* Be quick for non-multibyte encodings. */
9368 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009369 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009370 buf[i] = NUL;
9371 }
9372
9373 return OK;
9374}
9375
Bram Moolenaar4770d092006-01-12 23:22:24 +00009376/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009377#define SPS_BEST 1
9378#define SPS_FAST 2
9379#define SPS_DOUBLE 4
9380
Bram Moolenaar4770d092006-01-12 23:22:24 +00009381static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
9382static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009383
9384/*
9385 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009386 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009387 */
9388 int
9389spell_check_sps()
9390{
9391 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009392 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009393 char_u buf[MAXPATHL];
9394 int f;
9395
9396 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009397 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009398
9399 for (p = p_sps; *p != NUL; )
9400 {
9401 copy_option_part(&p, buf, MAXPATHL, ",");
9402
9403 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009404 if (VIM_ISDIGIT(*buf))
9405 {
9406 s = buf;
9407 sps_limit = getdigits(&s);
9408 if (*s != NUL && !VIM_ISDIGIT(*s))
9409 f = -1;
9410 }
9411 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009412 f = SPS_BEST;
9413 else if (STRCMP(buf, "fast") == 0)
9414 f = SPS_FAST;
9415 else if (STRCMP(buf, "double") == 0)
9416 f = SPS_DOUBLE;
9417 else if (STRNCMP(buf, "expr:", 5) != 0
9418 && STRNCMP(buf, "file:", 5) != 0)
9419 f = -1;
9420
9421 if (f == -1 || (sps_flags != 0 && f != 0))
9422 {
9423 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009424 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009425 return FAIL;
9426 }
9427 if (f != 0)
9428 sps_flags = f;
9429 }
9430
9431 if (sps_flags == 0)
9432 sps_flags = SPS_BEST;
9433
9434 return OK;
9435}
9436
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009437/*
9438 * "z?": Find badly spelled word under or after the cursor.
9439 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009440 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00009441 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009442 */
9443 void
Bram Moolenaard12a1322005-08-21 22:08:24 +00009444spell_suggest(count)
9445 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009446{
9447 char_u *line;
9448 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009449 char_u wcopy[MAXWLEN + 2];
9450 char_u *p;
9451 int i;
9452 int c;
9453 suginfo_T sug;
9454 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009455 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009456 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009457 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009458 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009459 int badlen = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009460
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009461 if (no_spell_checking(curwin))
9462 return;
9463
9464#ifdef FEAT_VISUAL
9465 if (VIsual_active)
9466 {
9467 /* Use the Visually selected text as the bad word. But reject
9468 * a multi-line selection. */
9469 if (curwin->w_cursor.lnum != VIsual.lnum)
9470 {
9471 vim_beep();
9472 return;
9473 }
9474 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
9475 if (badlen < 0)
9476 badlen = -badlen;
9477 else
9478 curwin->w_cursor.col = VIsual.col;
9479 ++badlen;
9480 end_visual_mode();
9481 }
9482 else
9483#endif
9484 /* Find the start of the badly spelled word. */
9485 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +00009486 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009487 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00009488 /* No bad word or it starts after the cursor: use the word under the
9489 * cursor. */
9490 curwin->w_cursor = prev_cursor;
9491 line = ml_get_curline();
9492 p = line + curwin->w_cursor.col;
9493 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009494 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00009495 mb_ptr_back(line, p);
9496 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009497 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00009498 mb_ptr_adv(p);
9499
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009500 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00009501 {
9502 beep_flush();
9503 return;
9504 }
9505 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009506 }
9507
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009508 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009509
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009510 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009511 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009512
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009513 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009514
Bram Moolenaar5195e452005-08-19 20:32:47 +00009515 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
9516 * 'spellsuggest', whatever is smaller. */
9517 if (sps_limit > (int)Rows - 2)
9518 limit = (int)Rows - 2;
9519 else
9520 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009521 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +00009522 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009523
9524 if (sug.su_ga.ga_len == 0)
9525 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +00009526 else if (count > 0)
9527 {
9528 if (count > sug.su_ga.ga_len)
9529 smsg((char_u *)_("Sorry, only %ld suggestions"),
9530 (long)sug.su_ga.ga_len);
9531 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009532 else
9533 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009534 vim_free(repl_from);
9535 repl_from = NULL;
9536 vim_free(repl_to);
9537 repl_to = NULL;
9538
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009539#ifdef FEAT_RIGHTLEFT
9540 /* When 'rightleft' is set the list is drawn right-left. */
9541 cmdmsg_rl = curwin->w_p_rl;
9542 if (cmdmsg_rl)
9543 msg_col = Columns - 1;
9544#endif
9545
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009546 /* List the suggestions. */
9547 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009548 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009549 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
9550 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009551#ifdef FEAT_RIGHTLEFT
9552 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
9553 {
9554 /* And now the rabbit from the high hat: Avoid showing the
9555 * untranslated message rightleft. */
9556 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
9557 sug.su_badlen, sug.su_badptr);
9558 }
9559#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009560 msg_puts(IObuff);
9561 msg_clr_eos();
9562 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00009563
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009564 msg_scroll = TRUE;
9565 for (i = 0; i < sug.su_ga.ga_len; ++i)
9566 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009567 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009568
9569 /* The suggested word may replace only part of the bad word, add
9570 * the not replaced part. */
9571 STRCPY(wcopy, stp->st_word);
9572 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +00009573 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009574 sug.su_badptr + stp->st_orglen,
9575 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009576 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
9577#ifdef FEAT_RIGHTLEFT
9578 if (cmdmsg_rl)
9579 rl_mirror(IObuff);
9580#endif
9581 msg_puts(IObuff);
9582
9583 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009584 msg_puts(IObuff);
9585
9586 /* The word may replace more than "su_badlen". */
9587 if (sug.su_badlen < stp->st_orglen)
9588 {
9589 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
9590 stp->st_orglen, sug.su_badptr);
9591 msg_puts(IObuff);
9592 }
9593
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009594 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009595 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00009596 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009597 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009598 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009599 stp->st_salscore ? "s " : "",
9600 stp->st_score, stp->st_altscore);
9601 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009602 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00009603 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009604#ifdef FEAT_RIGHTLEFT
9605 if (cmdmsg_rl)
9606 /* Mirror the numbers, but keep the leading space. */
9607 rl_mirror(IObuff + 1);
9608#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00009609 msg_advance(30);
9610 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009611 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009612 msg_putchar('\n');
9613 }
9614
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009615#ifdef FEAT_RIGHTLEFT
9616 cmdmsg_rl = FALSE;
9617 msg_col = 0;
9618#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009619 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00009620 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009621 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +00009622 selected -= lines_left;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009623 }
9624
Bram Moolenaard12a1322005-08-21 22:08:24 +00009625 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
9626 {
9627 /* Save the from and to text for :spellrepall. */
9628 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00009629 if (sug.su_badlen > stp->st_orglen)
9630 {
9631 /* Replacing less than "su_badlen", append the remainder to
9632 * repl_to. */
9633 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
9634 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
9635 sug.su_badlen - stp->st_orglen,
9636 sug.su_badptr + stp->st_orglen);
9637 repl_to = vim_strsave(IObuff);
9638 }
9639 else
9640 {
9641 /* Replacing su_badlen or more, use the whole word. */
9642 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
9643 repl_to = vim_strsave(stp->st_word);
9644 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00009645
9646 /* Replace the word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009647 p = alloc(STRLEN(line) - stp->st_orglen + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +00009648 if (p != NULL)
9649 {
9650 c = sug.su_badptr - line;
9651 mch_memmove(p, line, c);
9652 STRCPY(p + c, stp->st_word);
9653 STRCAT(p, sug.su_badptr + stp->st_orglen);
9654 ml_replace(curwin->w_cursor.lnum, p, FALSE);
9655 curwin->w_cursor.col = c;
9656 changed_bytes(curwin->w_cursor.lnum, c);
9657
9658 /* For redo we use a change-word command. */
9659 ResetRedobuff();
9660 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +00009661 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +00009662 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +00009663 AppendCharToRedobuff(ESC);
9664 }
9665 }
9666 else
9667 curwin->w_cursor = prev_cursor;
9668
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009669 spell_find_cleanup(&sug);
9670}
9671
9672/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009673 * Check if the word at line "lnum" column "col" is required to start with a
9674 * capital. This uses 'spellcapcheck' of the current buffer.
9675 */
9676 static int
9677check_need_cap(lnum, col)
9678 linenr_T lnum;
9679 colnr_T col;
9680{
9681 int need_cap = FALSE;
9682 char_u *line;
9683 char_u *line_copy = NULL;
9684 char_u *p;
9685 colnr_T endcol;
9686 regmatch_T regmatch;
9687
9688 if (curbuf->b_cap_prog == NULL)
9689 return FALSE;
9690
9691 line = ml_get_curline();
9692 endcol = 0;
9693 if ((int)(skipwhite(line) - line) >= (int)col)
9694 {
9695 /* At start of line, check if previous line is empty or sentence
9696 * ends there. */
9697 if (lnum == 1)
9698 need_cap = TRUE;
9699 else
9700 {
9701 line = ml_get(lnum - 1);
9702 if (*skipwhite(line) == NUL)
9703 need_cap = TRUE;
9704 else
9705 {
9706 /* Append a space in place of the line break. */
9707 line_copy = concat_str(line, (char_u *)" ");
9708 line = line_copy;
9709 endcol = STRLEN(line);
9710 }
9711 }
9712 }
9713 else
9714 endcol = col;
9715
9716 if (endcol > 0)
9717 {
9718 /* Check if sentence ends before the bad word. */
9719 regmatch.regprog = curbuf->b_cap_prog;
9720 regmatch.rm_ic = FALSE;
9721 p = line + endcol;
9722 for (;;)
9723 {
9724 mb_ptr_back(line, p);
9725 if (p == line || spell_iswordp_nmw(p))
9726 break;
9727 if (vim_regexec(&regmatch, p, 0)
9728 && regmatch.endp[0] == line + endcol)
9729 {
9730 need_cap = TRUE;
9731 break;
9732 }
9733 }
9734 }
9735
9736 vim_free(line_copy);
9737
9738 return need_cap;
9739}
9740
9741
9742/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009743 * ":spellrepall"
9744 */
9745/*ARGSUSED*/
9746 void
9747ex_spellrepall(eap)
9748 exarg_T *eap;
9749{
9750 pos_T pos = curwin->w_cursor;
9751 char_u *frompat;
9752 int addlen;
9753 char_u *line;
9754 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009755 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009756 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009757
9758 if (repl_from == NULL || repl_to == NULL)
9759 {
9760 EMSG(_("E752: No previous spell replacement"));
9761 return;
9762 }
9763 addlen = STRLEN(repl_to) - STRLEN(repl_from);
9764
9765 frompat = alloc(STRLEN(repl_from) + 7);
9766 if (frompat == NULL)
9767 return;
9768 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
9769 p_ws = FALSE;
9770
Bram Moolenaar5195e452005-08-19 20:32:47 +00009771 sub_nsubs = 0;
9772 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009773 curwin->w_cursor.lnum = 0;
9774 while (!got_int)
9775 {
9776 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
9777 || u_save_cursor() == FAIL)
9778 break;
9779
9780 /* Only replace when the right word isn't there yet. This happens
9781 * when changing "etc" to "etc.". */
9782 line = ml_get_curline();
9783 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
9784 repl_to, STRLEN(repl_to)) != 0)
9785 {
9786 p = alloc(STRLEN(line) + addlen + 1);
9787 if (p == NULL)
9788 break;
9789 mch_memmove(p, line, curwin->w_cursor.col);
9790 STRCPY(p + curwin->w_cursor.col, repl_to);
9791 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
9792 ml_replace(curwin->w_cursor.lnum, p, FALSE);
9793 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009794
9795 if (curwin->w_cursor.lnum != prev_lnum)
9796 {
9797 ++sub_nlines;
9798 prev_lnum = curwin->w_cursor.lnum;
9799 }
9800 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009801 }
9802 curwin->w_cursor.col += STRLEN(repl_to);
9803 }
9804
9805 p_ws = save_ws;
9806 curwin->w_cursor = pos;
9807 vim_free(frompat);
9808
Bram Moolenaar5195e452005-08-19 20:32:47 +00009809 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009810 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009811 else
9812 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009813}
9814
9815/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009816 * Find spell suggestions for "word". Return them in the growarray "*gap" as
9817 * a list of allocated strings.
9818 */
9819 void
Bram Moolenaar4770d092006-01-12 23:22:24 +00009820spell_suggest_list(gap, word, maxcount, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009821 garray_T *gap;
9822 char_u *word;
9823 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009824 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009825 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009826{
9827 suginfo_T sug;
9828 int i;
9829 suggest_T *stp;
9830 char_u *wcopy;
9831
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009832 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009833
9834 /* Make room in "gap". */
9835 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00009836 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009837 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00009838 for (i = 0; i < sug.su_ga.ga_len; ++i)
9839 {
9840 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009841
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00009842 /* The suggested word may replace only part of "word", add the not
9843 * replaced part. */
9844 wcopy = alloc(stp->st_wordlen
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009845 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00009846 if (wcopy == NULL)
9847 break;
9848 STRCPY(wcopy, stp->st_word);
9849 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
9850 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
9851 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009852 }
9853
9854 spell_find_cleanup(&sug);
9855}
9856
9857/*
9858 * Find spell suggestions for the word at the start of "badptr".
9859 * Return the suggestions in "su->su_ga".
9860 * The maximum number of suggestions is "maxcount".
9861 * Note: does use info for the current window.
9862 * This is based on the mechanisms of Aspell, but completely reimplemented.
9863 */
9864 static void
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009865spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009866 char_u *badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009867 int badlen; /* length of bad word or 0 if unknown */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009868 suginfo_T *su;
9869 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00009870 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009871 int need_cap; /* word should start with capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009872 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009873{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009874 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009875 char_u buf[MAXPATHL];
9876 char_u *p;
9877 int do_combine = FALSE;
9878 char_u *sps_copy;
9879#ifdef FEAT_EVAL
9880 static int expr_busy = FALSE;
9881#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009882 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00009883 int i;
9884 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009885
9886 /*
9887 * Set the info in "*su".
9888 */
9889 vim_memset(su, 0, sizeof(suginfo_T));
9890 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
9891 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009892 if (*badptr == NUL)
9893 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009894 hash_init(&su->su_banned);
9895
9896 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009897 if (badlen != 0)
9898 su->su_badlen = badlen;
9899 else
9900 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009901 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009902 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009903
9904 if (su->su_badlen >= MAXWLEN)
9905 su->su_badlen = MAXWLEN - 1; /* just in case */
9906 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
9907 (void)spell_casefold(su->su_badptr, su->su_badlen,
9908 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009909 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009910 su->su_badflags = badword_captype(su->su_badptr,
9911 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009912 if (need_cap)
9913 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009914
Bram Moolenaar8b96d642005-09-05 22:05:30 +00009915 /* Find the default language for sound folding. We simply use the first
9916 * one in 'spelllang' that supports sound folding. That's good for when
9917 * using multiple files for one language, it's not that bad when mixing
9918 * languages (e.g., "pl,en"). */
9919 for (i = 0; i < curbuf->b_langp.ga_len; ++i)
9920 {
9921 lp = LANGP_ENTRY(curbuf->b_langp, i);
9922 if (lp->lp_sallang != NULL)
9923 {
9924 su->su_sallang = lp->lp_sallang;
9925 break;
9926 }
9927 }
9928
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009929 /* Soundfold the bad word with the default sound folding, so that we don't
9930 * have to do this many times. */
9931 if (su->su_sallang != NULL)
9932 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
9933 su->su_sal_badword);
9934
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009935 /* If the word is not capitalised and spell_check() doesn't consider the
9936 * word to be bad then it might need to be capitalised. Add a suggestion
9937 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00009938 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009939 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009940 {
9941 make_case_word(su->su_badword, buf, WF_ONECAP);
9942 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +00009943 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009944 }
9945
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009946 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00009947 if (banbadword)
9948 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009949
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009950 /* Make a copy of 'spellsuggest', because the expression may change it. */
9951 sps_copy = vim_strsave(p_sps);
9952 if (sps_copy == NULL)
9953 return;
9954
9955 /* Loop over the items in 'spellsuggest'. */
9956 for (p = sps_copy; *p != NUL; )
9957 {
9958 copy_option_part(&p, buf, MAXPATHL, ",");
9959
9960 if (STRNCMP(buf, "expr:", 5) == 0)
9961 {
9962#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009963 /* Evaluate an expression. Skip this when called recursively,
9964 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009965 if (!expr_busy)
9966 {
9967 expr_busy = TRUE;
9968 spell_suggest_expr(su, buf + 5);
9969 expr_busy = FALSE;
9970 }
9971#endif
9972 }
9973 else if (STRNCMP(buf, "file:", 5) == 0)
9974 /* Use list of suggestions in a file. */
9975 spell_suggest_file(su, buf + 5);
9976 else
9977 {
9978 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009979 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009980 if (sps_flags & SPS_DOUBLE)
9981 do_combine = TRUE;
9982 }
9983 }
9984
9985 vim_free(sps_copy);
9986
9987 if (do_combine)
9988 /* Combine the two list of suggestions. This must be done last,
9989 * because sorting changes the order again. */
9990 score_combine(su);
9991}
9992
9993#ifdef FEAT_EVAL
9994/*
9995 * Find suggestions by evaluating expression "expr".
9996 */
9997 static void
9998spell_suggest_expr(su, expr)
9999 suginfo_T *su;
10000 char_u *expr;
10001{
10002 list_T *list;
10003 listitem_T *li;
10004 int score;
10005 char_u *p;
10006
10007 /* The work is split up in a few parts to avoid having to export
10008 * suginfo_T.
10009 * First evaluate the expression and get the resulting list. */
10010 list = eval_spell_expr(su->su_badword, expr);
10011 if (list != NULL)
10012 {
10013 /* Loop over the items in the list. */
10014 for (li = list->lv_first; li != NULL; li = li->li_next)
10015 if (li->li_tv.v_type == VAR_LIST)
10016 {
10017 /* Get the word and the score from the items. */
10018 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010019 if (score >= 0 && score <= su->su_maxscore)
10020 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10021 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010022 }
10023 list_unref(list);
10024 }
10025
Bram Moolenaar4770d092006-01-12 23:22:24 +000010026 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10027 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010028 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10029}
10030#endif
10031
10032/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010033 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010034 */
10035 static void
10036spell_suggest_file(su, fname)
10037 suginfo_T *su;
10038 char_u *fname;
10039{
10040 FILE *fd;
10041 char_u line[MAXWLEN * 2];
10042 char_u *p;
10043 int len;
10044 char_u cword[MAXWLEN];
10045
10046 /* Open the file. */
10047 fd = mch_fopen((char *)fname, "r");
10048 if (fd == NULL)
10049 {
10050 EMSG2(_(e_notopen), fname);
10051 return;
10052 }
10053
10054 /* Read it line by line. */
10055 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
10056 {
10057 line_breakcheck();
10058
10059 p = vim_strchr(line, '/');
10060 if (p == NULL)
10061 continue; /* No Tab found, just skip the line. */
10062 *p++ = NUL;
10063 if (STRICMP(su->su_badword, line) == 0)
10064 {
10065 /* Match! Isolate the good word, until CR or NL. */
10066 for (len = 0; p[len] >= ' '; ++len)
10067 ;
10068 p[len] = NUL;
10069
10070 /* If the suggestion doesn't have specific case duplicate the case
10071 * of the bad word. */
10072 if (captype(p, NULL) == 0)
10073 {
10074 make_case_word(p, cword, su->su_badflags);
10075 p = cword;
10076 }
10077
10078 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010079 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010080 }
10081 }
10082
10083 fclose(fd);
10084
Bram Moolenaar4770d092006-01-12 23:22:24 +000010085 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10086 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010087 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10088}
10089
10090/*
10091 * Find suggestions for the internal method indicated by "sps_flags".
10092 */
10093 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010094spell_suggest_intern(su, interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010095 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010096 int interactive;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010097{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010098 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010099 * Load the .sug file(s) that are available and not done yet.
10100 */
10101 suggest_load_files();
10102
10103 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010104 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010105 *
10106 * Set a maximum score to limit the combination of operations that is
10107 * tried.
10108 */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010109 suggest_try_special(su);
10110
10111 /*
10112 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10113 * from the .aff file and inserting a space (split the word).
10114 */
10115 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010116
10117 /* For the resulting top-scorers compute the sound-a-like score. */
10118 if (sps_flags & SPS_DOUBLE)
10119 score_comp_sal(su);
10120
10121 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010122 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010123 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010124 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010125 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010126 if (sps_flags & SPS_BEST)
10127 /* Adjust the word score for the suggestions found so far for how
10128 * they sounds like. */
10129 rescore_suggestions(su);
10130
10131 /*
10132 * While going throught the soundfold tree "su_maxscore" is the score
10133 * for the soundfold word, limits the changes that are being tried,
10134 * and "su_sfmaxscore" the rescored score, which is set by
10135 * cleanup_suggestions().
10136 * First find words with a small edit distance, because this is much
10137 * faster and often already finds the top-N suggestions. If we didn't
10138 * find many suggestions try again with a higher edit distance.
10139 * "sl_sounddone" is used to avoid doing the same word twice.
10140 */
10141 suggest_try_soundalike_prep();
10142 su->su_maxscore = SCORE_SFMAX1;
10143 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010144 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010145 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10146 {
10147 /* We didn't find enough matches, try again, allowing more
10148 * changes to the soundfold word. */
10149 su->su_maxscore = SCORE_SFMAX2;
10150 suggest_try_soundalike(su);
10151 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10152 {
10153 /* Still didn't find enough matches, try again, allowing even
10154 * more changes to the soundfold word. */
10155 su->su_maxscore = SCORE_SFMAX3;
10156 suggest_try_soundalike(su);
10157 }
10158 }
10159 su->su_maxscore = su->su_sfmaxscore;
10160 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010161 }
10162
Bram Moolenaar4770d092006-01-12 23:22:24 +000010163 /* When CTRL-C was hit while searching do show the results. Only clear
10164 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010165 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +000010166 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010167 {
10168 (void)vgetc();
10169 got_int = FALSE;
10170 }
10171
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010172 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010173 {
10174 if (sps_flags & SPS_BEST)
10175 /* Adjust the word score for how it sounds like. */
10176 rescore_suggestions(su);
10177
Bram Moolenaar4770d092006-01-12 23:22:24 +000010178 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10179 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010180 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010181 }
10182}
10183
10184/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010185 * Load the .sug files for languages that have one and weren't loaded yet.
10186 */
10187 static void
10188suggest_load_files()
10189{
10190 langp_T *lp;
10191 int lpi;
10192 slang_T *slang;
10193 char_u *dotp;
10194 FILE *fd;
10195 char_u buf[MAXWLEN];
10196 int i;
10197 time_t timestamp;
10198 int wcount;
10199 int wordnr;
10200 garray_T ga;
10201 int c;
10202
10203 /* Do this for all languages that support sound folding. */
10204 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
10205 {
10206 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
10207 slang = lp->lp_slang;
10208 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10209 {
10210 /* Change ".spl" to ".sug" and open the file. When the file isn't
10211 * found silently skip it. Do set "sl_sugloaded" so that we
10212 * don't try again and again. */
10213 slang->sl_sugloaded = TRUE;
10214
10215 dotp = vim_strrchr(slang->sl_fname, '.');
10216 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10217 continue;
10218 STRCPY(dotp, ".sug");
10219 fd = fopen((char *)slang->sl_fname, "r");
10220 if (fd == NULL)
10221 goto nextone;
10222
10223 /*
10224 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10225 */
10226 for (i = 0; i < VIMSUGMAGICL; ++i)
10227 buf[i] = getc(fd); /* <fileID> */
10228 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10229 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010230 EMSG2(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010231 slang->sl_fname);
10232 goto nextone;
10233 }
10234 c = getc(fd); /* <versionnr> */
10235 if (c < VIMSUGVERSION)
10236 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010237 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010238 slang->sl_fname);
10239 goto nextone;
10240 }
10241 else if (c > VIMSUGVERSION)
10242 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010243 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010244 slang->sl_fname);
10245 goto nextone;
10246 }
10247
10248 /* Check the timestamp, it must be exactly the same as the one in
10249 * the .spl file. Otherwise the word numbers won't match. */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010250 timestamp = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010251 if (timestamp != slang->sl_sugtime)
10252 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010253 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010254 slang->sl_fname);
10255 goto nextone;
10256 }
10257
10258 /*
10259 * <SUGWORDTREE>: <wordtree>
10260 * Read the trie with the soundfolded words.
10261 */
10262 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
10263 FALSE, 0) != 0)
10264 {
10265someerror:
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010266 EMSG2(_("E782: error while reading .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010267 slang->sl_fname);
10268 slang_clear_sug(slang);
10269 goto nextone;
10270 }
10271
10272 /*
10273 * <SUGTABLE>: <sugwcount> <sugline> ...
10274 *
10275 * Read the table with word numbers. We use a file buffer for
10276 * this, because it's so much like a file with lines. Makes it
10277 * possible to swap the info and save on memory use.
10278 */
10279 slang->sl_sugbuf = open_spellbuf();
10280 if (slang->sl_sugbuf == NULL)
10281 goto someerror;
10282 /* <sugwcount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010283 wcount = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010284 if (wcount < 0)
10285 goto someerror;
10286
10287 /* Read all the wordnr lists into the buffer, one NUL terminated
10288 * list per line. */
10289 ga_init2(&ga, 1, 100);
10290 for (wordnr = 0; wordnr < wcount; ++wordnr)
10291 {
10292 ga.ga_len = 0;
10293 for (;;)
10294 {
10295 c = getc(fd); /* <sugline> */
10296 if (c < 0 || ga_grow(&ga, 1) == FAIL)
10297 goto someerror;
10298 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
10299 if (c == NUL)
10300 break;
10301 }
10302 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
10303 ga.ga_data, ga.ga_len, TRUE) == FAIL)
10304 goto someerror;
10305 }
10306 ga_clear(&ga);
10307
10308 /*
10309 * Need to put word counts in the word tries, so that we can find
10310 * a word by its number.
10311 */
10312 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
10313 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
10314
10315nextone:
10316 if (fd != NULL)
10317 fclose(fd);
10318 STRCPY(dotp, ".spl");
10319 }
10320 }
10321}
10322
10323
10324/*
10325 * Fill in the wordcount fields for a trie.
10326 * Returns the total number of words.
10327 */
10328 static void
10329tree_count_words(byts, idxs)
10330 char_u *byts;
10331 idx_T *idxs;
10332{
10333 int depth;
10334 idx_T arridx[MAXWLEN];
10335 int curi[MAXWLEN];
10336 int c;
10337 idx_T n;
10338 int wordcount[MAXWLEN];
10339
10340 arridx[0] = 0;
10341 curi[0] = 1;
10342 wordcount[0] = 0;
10343 depth = 0;
10344 while (depth >= 0 && !got_int)
10345 {
10346 if (curi[depth] > byts[arridx[depth]])
10347 {
10348 /* Done all bytes at this node, go up one level. */
10349 idxs[arridx[depth]] = wordcount[depth];
10350 if (depth > 0)
10351 wordcount[depth - 1] += wordcount[depth];
10352
10353 --depth;
10354 fast_breakcheck();
10355 }
10356 else
10357 {
10358 /* Do one more byte at this node. */
10359 n = arridx[depth] + curi[depth];
10360 ++curi[depth];
10361
10362 c = byts[n];
10363 if (c == 0)
10364 {
10365 /* End of word, count it. */
10366 ++wordcount[depth];
10367
10368 /* Skip over any other NUL bytes (same word with different
10369 * flags). */
10370 while (byts[n + 1] == 0)
10371 {
10372 ++n;
10373 ++curi[depth];
10374 }
10375 }
10376 else
10377 {
10378 /* Normal char, go one level deeper to count the words. */
10379 ++depth;
10380 arridx[depth] = idxs[n];
10381 curi[depth] = 1;
10382 wordcount[depth] = 0;
10383 }
10384 }
10385 }
10386}
10387
10388/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010389 * Free the info put in "*su" by spell_find_suggest().
10390 */
10391 static void
10392spell_find_cleanup(su)
10393 suginfo_T *su;
10394{
10395 int i;
10396
10397 /* Free the suggestions. */
10398 for (i = 0; i < su->su_ga.ga_len; ++i)
10399 vim_free(SUG(su->su_ga, i).st_word);
10400 ga_clear(&su->su_ga);
10401 for (i = 0; i < su->su_sga.ga_len; ++i)
10402 vim_free(SUG(su->su_sga, i).st_word);
10403 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010404
10405 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010406 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010407}
10408
10409/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010410 * Make a copy of "word", with the first letter upper or lower cased, to
10411 * "wcopy[MAXWLEN]". "word" must not be empty.
10412 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010413 */
10414 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010415onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010416 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010417 char_u *wcopy;
10418 int upper; /* TRUE: first letter made upper case */
10419{
10420 char_u *p;
10421 int c;
10422 int l;
10423
10424 p = word;
10425#ifdef FEAT_MBYTE
10426 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010427 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010428 else
10429#endif
10430 c = *p++;
10431 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010432 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010433 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010434 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010435#ifdef FEAT_MBYTE
10436 if (has_mbyte)
10437 l = mb_char2bytes(c, wcopy);
10438 else
10439#endif
10440 {
10441 l = 1;
10442 wcopy[0] = c;
10443 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010444 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010445}
10446
10447/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010448 * Make a copy of "word" with all the letters upper cased into
10449 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010450 */
10451 static void
10452allcap_copy(word, wcopy)
10453 char_u *word;
10454 char_u *wcopy;
10455{
10456 char_u *s;
10457 char_u *d;
10458 int c;
10459
10460 d = wcopy;
10461 for (s = word; *s != NUL; )
10462 {
10463#ifdef FEAT_MBYTE
10464 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010465 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010466 else
10467#endif
10468 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +000010469
10470#ifdef FEAT_MBYTE
10471 /* We only change ß to SS when we are certain latin1 is used. It
10472 * would cause weird errors in other 8-bit encodings. */
10473 if (enc_latin1like && c == 0xdf)
10474 {
10475 c = 'S';
10476 if (d - wcopy >= MAXWLEN - 1)
10477 break;
10478 *d++ = c;
10479 }
10480 else
10481#endif
10482 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010483
10484#ifdef FEAT_MBYTE
10485 if (has_mbyte)
10486 {
10487 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
10488 break;
10489 d += mb_char2bytes(c, d);
10490 }
10491 else
10492#endif
10493 {
10494 if (d - wcopy >= MAXWLEN - 1)
10495 break;
10496 *d++ = c;
10497 }
10498 }
10499 *d = NUL;
10500}
10501
10502/*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010503 * Try finding suggestions by recognizing specific situations.
10504 */
10505 static void
10506suggest_try_special(su)
10507 suginfo_T *su;
10508{
10509 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010510 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010511 int c;
10512 char_u word[MAXWLEN];
10513
10514 /*
10515 * Recognize a word that is repeated: "the the".
10516 */
10517 p = skiptowhite(su->su_fbadword);
10518 len = p - su->su_fbadword;
10519 p = skipwhite(p);
10520 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
10521 {
10522 /* Include badflags: if the badword is onecap or allcap
10523 * use that for the goodword too: "The the" -> "The". */
10524 c = su->su_fbadword[len];
10525 su->su_fbadword[len] = NUL;
10526 make_case_word(su->su_fbadword, word, su->su_badflags);
10527 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010528
10529 /* Give a soundalike score of 0, compute the score as if deleting one
10530 * character. */
10531 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010532 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010533 }
10534}
10535
10536/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010537 * Try finding suggestions by adding/removing/swapping letters.
10538 */
10539 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000010540suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010541 suginfo_T *su;
10542{
10543 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010544 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010545 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010546 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010547 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010548
10549 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +000010550 * to find matches (esp. REP items). Append some more text, changing
10551 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010552 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010553 n = STRLEN(fword);
10554 p = su->su_badptr + su->su_badlen;
10555 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010556
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010557 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010558 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010559 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010560
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010561 /* If reloading a spell file fails it's still in the list but
10562 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010563 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010564 continue;
10565
Bram Moolenaar4770d092006-01-12 23:22:24 +000010566 /* Try it for this language. Will add possible suggestions. */
10567 suggest_trie_walk(su, lp, fword, FALSE);
10568 }
10569}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010570
Bram Moolenaar4770d092006-01-12 23:22:24 +000010571/* Check the maximum score, if we go over it we won't try this change. */
10572#define TRY_DEEPER(su, stack, depth, add) \
10573 (stack[depth].ts_score + (add) < su->su_maxscore)
10574
10575/*
10576 * Try finding suggestions by adding/removing/swapping letters.
10577 *
10578 * This uses a state machine. At each node in the tree we try various
10579 * operations. When trying if an operation works "depth" is increased and the
10580 * stack[] is used to store info. This allows combinations, thus insert one
10581 * character, replace one and delete another. The number of changes is
10582 * limited by su->su_maxscore.
10583 *
10584 * After implementing this I noticed an article by Kemal Oflazer that
10585 * describes something similar: "Error-tolerant Finite State Recognition with
10586 * Applications to Morphological Analysis and Spelling Correction" (1996).
10587 * The implementation in the article is simplified and requires a stack of
10588 * unknown depth. The implementation here only needs a stack depth equal to
10589 * the length of the word.
10590 *
10591 * This is also used for the sound-folded word, "soundfold" is TRUE then.
10592 * The mechanism is the same, but we find a match with a sound-folded word
10593 * that comes from one or more original words. Each of these words may be
10594 * added, this is done by add_sound_suggest().
10595 * Don't use:
10596 * the prefix tree or the keep-case tree
10597 * "su->su_badlen"
10598 * anything to do with upper and lower case
10599 * anything to do with word or non-word characters ("spell_iswordp()")
10600 * banned words
10601 * word flags (rare, region, compounding)
10602 * word splitting for now
10603 * "similar_chars()"
10604 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
10605 */
10606 static void
10607suggest_trie_walk(su, lp, fword, soundfold)
10608 suginfo_T *su;
10609 langp_T *lp;
10610 char_u *fword;
10611 int soundfold;
10612{
10613 char_u tword[MAXWLEN]; /* good word collected so far */
10614 trystate_T stack[MAXWLEN];
10615 char_u preword[MAXWLEN * 3]; /* word found with proper case;
10616 * concatanation of prefix compound
10617 * words and split word. NUL terminated
10618 * when going deeper but not when coming
10619 * back. */
10620 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
10621 trystate_T *sp;
10622 int newscore;
10623 int score;
10624 char_u *byts, *fbyts, *pbyts;
10625 idx_T *idxs, *fidxs, *pidxs;
10626 int depth;
10627 int c, c2, c3;
10628 int n = 0;
10629 int flags;
10630 garray_T *gap;
10631 idx_T arridx;
10632 int len;
10633 char_u *p;
10634 fromto_T *ftp;
10635 int fl = 0, tl;
10636 int repextra = 0; /* extra bytes in fword[] from REP item */
10637 slang_T *slang = lp->lp_slang;
10638 int fword_ends;
10639 int goodword_ends;
10640#ifdef DEBUG_TRIEWALK
10641 /* Stores the name of the change made at each level. */
10642 char_u changename[MAXWLEN][80];
10643#endif
10644 int breakcheckcount = 1000;
10645 int compound_ok;
10646
10647 /*
10648 * Go through the whole case-fold tree, try changes at each node.
10649 * "tword[]" contains the word collected from nodes in the tree.
10650 * "fword[]" the word we are trying to match with (initially the bad
10651 * word).
10652 */
10653 depth = 0;
10654 sp = &stack[0];
10655 vim_memset(sp, 0, sizeof(trystate_T));
10656 sp->ts_curi = 1;
10657
10658 if (soundfold)
10659 {
10660 /* Going through the soundfold tree. */
10661 byts = fbyts = slang->sl_sbyts;
10662 idxs = fidxs = slang->sl_sidxs;
10663 pbyts = NULL;
10664 pidxs = NULL;
10665 sp->ts_prefixdepth = PFD_NOPREFIX;
10666 sp->ts_state = STATE_START;
10667 }
10668 else
10669 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010670 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010671 * When there are postponed prefixes we need to use these first. At
10672 * the end of the prefix we continue in the case-fold tree.
10673 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010674 fbyts = slang->sl_fbyts;
10675 fidxs = slang->sl_fidxs;
10676 pbyts = slang->sl_pbyts;
10677 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010678 if (pbyts != NULL)
10679 {
10680 byts = pbyts;
10681 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010682 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010683 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
10684 }
10685 else
10686 {
10687 byts = fbyts;
10688 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010689 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010690 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010691 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000010692 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010693
Bram Moolenaar4770d092006-01-12 23:22:24 +000010694 /*
10695 * Loop to find all suggestions. At each round we either:
10696 * - For the current state try one operation, advance "ts_curi",
10697 * increase "depth".
10698 * - When a state is done go to the next, set "ts_state".
10699 * - When all states are tried decrease "depth".
10700 */
10701 while (depth >= 0 && !got_int)
10702 {
10703 sp = &stack[depth];
10704 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010705 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010706 case STATE_START:
10707 case STATE_NOPREFIX:
10708 /*
10709 * Start of node: Deal with NUL bytes, which means
10710 * tword[] may end here.
10711 */
10712 arridx = sp->ts_arridx; /* current node in the tree */
10713 len = byts[arridx]; /* bytes in this node */
10714 arridx += sp->ts_curi; /* index of current byte */
10715
10716 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010717 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010718 /* Skip over the NUL bytes, we use them later. */
10719 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
10720 ;
10721 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010722
Bram Moolenaar4770d092006-01-12 23:22:24 +000010723 /* Always past NUL bytes now. */
10724 n = (int)sp->ts_state;
10725 sp->ts_state = STATE_ENDNUL;
10726 sp->ts_save_badflags = su->su_badflags;
10727
10728 /* At end of a prefix or at start of prefixtree: check for
10729 * following word. */
10730 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010731 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010732 /* Set su->su_badflags to the caps type at this position.
10733 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010734#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000010735 if (has_mbyte)
10736 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
10737 else
Bram Moolenaar53805d12005-08-01 07:08:33 +000010738#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000010739 n = sp->ts_fidx;
10740 flags = badword_captype(su->su_badptr, su->su_badptr + n);
10741 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000010742 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010743#ifdef DEBUG_TRIEWALK
10744 sprintf(changename[depth], "prefix");
10745#endif
10746 go_deeper(stack, depth, 0);
10747 ++depth;
10748 sp = &stack[depth];
10749 sp->ts_prefixdepth = depth - 1;
10750 byts = fbyts;
10751 idxs = fidxs;
10752 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010753
Bram Moolenaar4770d092006-01-12 23:22:24 +000010754 /* Move the prefix to preword[] with the right case
10755 * and make find_keepcap_word() works. */
10756 tword[sp->ts_twordlen] = NUL;
10757 make_case_word(tword + sp->ts_splitoff,
10758 preword + sp->ts_prewordlen, flags);
10759 sp->ts_prewordlen = STRLEN(preword);
10760 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010761 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000010762 break;
10763 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010764
Bram Moolenaar4770d092006-01-12 23:22:24 +000010765 if (sp->ts_curi > len || byts[arridx] != 0)
10766 {
10767 /* Past bytes in node and/or past NUL bytes. */
10768 sp->ts_state = STATE_ENDNUL;
10769 sp->ts_save_badflags = su->su_badflags;
10770 break;
10771 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010772
Bram Moolenaar4770d092006-01-12 23:22:24 +000010773 /*
10774 * End of word in tree.
10775 */
10776 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010777
Bram Moolenaar4770d092006-01-12 23:22:24 +000010778 flags = (int)idxs[arridx];
10779 fword_ends = (fword[sp->ts_fidx] == NUL
10780 || (soundfold
10781 ? vim_iswhite(fword[sp->ts_fidx])
10782 : !spell_iswordp(fword + sp->ts_fidx, curbuf)));
10783 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010784
Bram Moolenaar4770d092006-01-12 23:22:24 +000010785 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +000010786 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010787 {
10788 /* There was a prefix before the word. Check that the prefix
10789 * can be used with this word. */
10790 /* Count the length of the NULs in the prefix. If there are
10791 * none this must be the first try without a prefix. */
10792 n = stack[sp->ts_prefixdepth].ts_arridx;
10793 len = pbyts[n++];
10794 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
10795 ;
10796 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010797 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010798 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000010799 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010800 if (c == 0)
10801 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010802
Bram Moolenaar4770d092006-01-12 23:22:24 +000010803 /* Use the WF_RARE flag for a rare prefix. */
10804 if (c & WF_RAREPFX)
10805 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010806
Bram Moolenaar4770d092006-01-12 23:22:24 +000010807 /* Tricky: when checking for both prefix and compounding
10808 * we run into the prefix flag first.
10809 * Remember that it's OK, so that we accept the prefix
10810 * when arriving at a compound flag. */
10811 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010812 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000010813 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010814
Bram Moolenaar4770d092006-01-12 23:22:24 +000010815 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
10816 * appending another compound word below. */
10817 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010818 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +000010819 goodword_ends = FALSE;
10820 else
10821 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010822
Bram Moolenaar4770d092006-01-12 23:22:24 +000010823 p = NULL;
10824 compound_ok = TRUE;
10825 if (sp->ts_complen > sp->ts_compsplit)
10826 {
10827 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +000010828 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010829 /* There was a word before this word. When there was no
10830 * change in this word (it was correct) add the first word
10831 * as a suggestion. If this word was corrected too, we
10832 * need to check if a correct word follows. */
10833 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +000010834 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +000010835 && STRNCMP(fword + sp->ts_splitfidx,
10836 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +000010837 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010838 {
10839 preword[sp->ts_prewordlen] = NUL;
10840 newscore = score_wordcount_adj(slang, sp->ts_score,
10841 preword + sp->ts_prewordlen,
10842 sp->ts_prewordlen > 0);
10843 /* Add the suggestion if the score isn't too bad. */
10844 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +000010845 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010846 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010847 newscore, 0, FALSE,
10848 lp->lp_sallang, FALSE);
10849 break;
Bram Moolenaar78622822005-08-23 21:00:13 +000010850 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000010851 }
Bram Moolenaare52325c2005-08-22 22:54:29 +000010852 else
Bram Moolenaar0c405862005-06-22 22:26:26 +000010853 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010854 /* There was a compound word before this word. If this
10855 * word does not support compounding then give up
10856 * (splitting is tried for the word without compound
10857 * flag). */
10858 if (((unsigned)flags >> 24) == 0
10859 || sp->ts_twordlen - sp->ts_splitoff
10860 < slang->sl_compminlen)
10861 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010862#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000010863 /* For multi-byte chars check character length against
10864 * COMPOUNDMIN. */
10865 if (has_mbyte
10866 && slang->sl_compminlen > 0
10867 && mb_charlen(tword + sp->ts_splitoff)
10868 < slang->sl_compminlen)
10869 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010870#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +000010871
Bram Moolenaar4770d092006-01-12 23:22:24 +000010872 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
10873 compflags[sp->ts_complen + 1] = NUL;
10874 vim_strncpy(preword + sp->ts_prewordlen,
10875 tword + sp->ts_splitoff,
10876 sp->ts_twordlen - sp->ts_splitoff);
10877 p = preword;
10878 while (*skiptowhite(p) != NUL)
10879 p = skipwhite(skiptowhite(p));
10880 if (fword_ends && !can_compound(slang, p,
10881 compflags + sp->ts_compsplit))
10882 /* Compound is not allowed. But it may still be
10883 * possible if we add another (short) word. */
10884 compound_ok = FALSE;
10885
10886 /* Get pointer to last char of previous word. */
10887 p = preword + sp->ts_prewordlen;
10888 mb_ptr_back(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010889 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000010890 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010891
Bram Moolenaar4770d092006-01-12 23:22:24 +000010892 /*
10893 * Form the word with proper case in preword.
10894 * If there is a word from a previous split, append.
10895 * For the soundfold tree don't change the case, simply append.
10896 */
10897 if (soundfold)
10898 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
10899 else if (flags & WF_KEEPCAP)
10900 /* Must find the word in the keep-case tree. */
10901 find_keepcap_word(slang, tword + sp->ts_splitoff,
10902 preword + sp->ts_prewordlen);
10903 else
10904 {
10905 /* Include badflags: If the badword is onecap or allcap
10906 * use that for the goodword too. But if the badword is
10907 * allcap and it's only one char long use onecap. */
10908 c = su->su_badflags;
10909 if ((c & WF_ALLCAP)
10910#ifdef FEAT_MBYTE
10911 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
10912#else
10913 && su->su_badlen == 1
10914#endif
10915 )
10916 c = WF_ONECAP;
10917 c |= flags;
10918
10919 /* When appending a compound word after a word character don't
10920 * use Onecap. */
10921 if (p != NULL && spell_iswordp_nmw(p))
10922 c &= ~WF_ONECAP;
10923 make_case_word(tword + sp->ts_splitoff,
10924 preword + sp->ts_prewordlen, c);
10925 }
10926
10927 if (!soundfold)
10928 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010929 /* Don't use a banned word. It may appear again as a good
10930 * word, thus remember it. */
10931 if (flags & WF_BANNED)
10932 {
Bram Moolenaar5195e452005-08-19 20:32:47 +000010933 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010934 break;
10935 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010936 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +000010937 && WAS_BANNED(su, preword + sp->ts_prewordlen))
10938 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010939 {
10940 if (slang->sl_compprog == NULL)
10941 break;
10942 /* the word so far was banned but we may try compounding */
10943 goodword_ends = FALSE;
10944 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000010945 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010946
Bram Moolenaar4770d092006-01-12 23:22:24 +000010947 newscore = 0;
10948 if (!soundfold) /* soundfold words don't have flags */
10949 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010950 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010951 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010952 newscore += SCORE_REGION;
10953 if (flags & WF_RARE)
10954 newscore += SCORE_RARE;
10955
Bram Moolenaar0c405862005-06-22 22:26:26 +000010956 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000010957 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010958 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010959 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010960
Bram Moolenaar4770d092006-01-12 23:22:24 +000010961 /* TODO: how about splitting in the soundfold tree? */
10962 if (fword_ends
10963 && goodword_ends
10964 && sp->ts_fidx >= sp->ts_fidxtry
10965 && compound_ok)
10966 {
10967 /* The badword also ends: add suggestions. */
10968#ifdef DEBUG_TRIEWALK
10969 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010970 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010971 int j;
10972
10973 /* print the stack of changes that brought us here */
10974 smsg("------ %s -------", fword);
10975 for (j = 0; j < depth; ++j)
10976 smsg("%s", changename[j]);
10977 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010978#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000010979 if (soundfold)
10980 {
10981 /* For soundfolded words we need to find the original
10982 * words, the edit distrance and then add them. */
10983 add_sound_suggest(su, preword, sp->ts_score, lp);
10984 }
10985 else
10986 {
10987 /* Give a penalty when changing non-word char to word
10988 * char, e.g., "thes," -> "these". */
10989 p = fword + sp->ts_fidx;
10990 mb_ptr_back(fword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010991 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010992 {
10993 p = preword + STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010994 mb_ptr_back(preword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010995 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010996 newscore += SCORE_NONWORD;
10997 }
10998
Bram Moolenaar4770d092006-01-12 23:22:24 +000010999 /* Give a bonus to words seen before. */
11000 score = score_wordcount_adj(slang,
11001 sp->ts_score + newscore,
11002 preword + sp->ts_prewordlen,
11003 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011004
Bram Moolenaar4770d092006-01-12 23:22:24 +000011005 /* Add the suggestion if the score isn't too bad. */
11006 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011007 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011008 add_suggestion(su, &su->su_ga, preword,
11009 sp->ts_fidx - repextra,
11010 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011011
11012 if (su->su_badflags & WF_MIXCAP)
11013 {
11014 /* We really don't know if the word should be
11015 * upper or lower case, add both. */
11016 c = captype(preword, NULL);
11017 if (c == 0 || c == WF_ALLCAP)
11018 {
11019 make_case_word(tword + sp->ts_splitoff,
11020 preword + sp->ts_prewordlen,
11021 c == 0 ? WF_ALLCAP : 0);
11022
11023 add_suggestion(su, &su->su_ga, preword,
11024 sp->ts_fidx - repextra,
11025 score + SCORE_ICASE, 0, FALSE,
11026 lp->lp_sallang, FALSE);
11027 }
11028 }
11029 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011030 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011031 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011032
Bram Moolenaar4770d092006-01-12 23:22:24 +000011033 /*
11034 * Try word split and/or compounding.
11035 */
11036 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +000011037#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011038 /* Don't split halfway a character. */
11039 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011040#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011041 )
11042 {
11043 int try_compound;
11044 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011045
Bram Moolenaar4770d092006-01-12 23:22:24 +000011046 /* If past the end of the bad word don't try a split.
11047 * Otherwise try changing the next word. E.g., find
11048 * suggestions for "the the" where the second "the" is
11049 * different. It's done like a split.
11050 * TODO: word split for soundfold words */
11051 try_split = (sp->ts_fidx - repextra < su->su_badlen)
11052 && !soundfold;
11053
11054 /* Get here in several situations:
11055 * 1. The word in the tree ends:
11056 * If the word allows compounding try that. Otherwise try
11057 * a split by inserting a space. For both check that a
11058 * valid words starts at fword[sp->ts_fidx].
11059 * For NOBREAK do like compounding to be able to check if
11060 * the next word is valid.
11061 * 2. The badword does end, but it was due to a change (e.g.,
11062 * a swap). No need to split, but do check that the
11063 * following word is valid.
11064 * 3. The badword and the word in the tree end. It may still
11065 * be possible to compound another (short) word.
11066 */
11067 try_compound = FALSE;
11068 if (!soundfold
11069 && slang->sl_compprog != NULL
11070 && ((unsigned)flags >> 24) != 0
11071 && sp->ts_twordlen - sp->ts_splitoff
11072 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011073#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011074 && (!has_mbyte
11075 || slang->sl_compminlen == 0
11076 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011077 >= slang->sl_compminlen)
11078#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011079 && (slang->sl_compsylmax < MAXWLEN
11080 || sp->ts_complen + 1 - sp->ts_compsplit
11081 < slang->sl_compmax)
11082 && (byte_in_str(sp->ts_complen == sp->ts_compsplit
11083 ? slang->sl_compstartflags
11084 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +000011085 ((unsigned)flags >> 24))))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011086 {
11087 try_compound = TRUE;
11088 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11089 compflags[sp->ts_complen + 1] = NUL;
11090 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011091
Bram Moolenaar4770d092006-01-12 23:22:24 +000011092 /* For NOBREAK we never try splitting, it won't make any word
11093 * valid. */
11094 if (slang->sl_nobreak)
11095 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +000011096
Bram Moolenaar4770d092006-01-12 23:22:24 +000011097 /* If we could add a compound word, and it's also possible to
11098 * split at this point, do the split first and set
11099 * TSF_DIDSPLIT to avoid doing it again. */
11100 else if (!fword_ends
11101 && try_compound
11102 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11103 {
11104 try_compound = FALSE;
11105 sp->ts_flags |= TSF_DIDSPLIT;
11106 --sp->ts_curi; /* do the same NUL again */
11107 compflags[sp->ts_complen] = NUL;
11108 }
11109 else
11110 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011111
Bram Moolenaar4770d092006-01-12 23:22:24 +000011112 if (try_split || try_compound)
11113 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011114 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +000011115 {
11116 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011117 * words so far are valid for compounding. If there
11118 * is only one word it must not have the NEEDCOMPOUND
11119 * flag. */
11120 if (sp->ts_complen == sp->ts_compsplit
11121 && (flags & WF_NEEDCOMP))
11122 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +000011123 p = preword;
11124 while (*skiptowhite(p) != NUL)
11125 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +000011126 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +000011127 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +000011128 compflags + sp->ts_compsplit))
11129 break;
11130 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011131
11132 /* Give a bonus to words seen before. */
11133 newscore = score_wordcount_adj(slang, newscore,
11134 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011135 }
11136
Bram Moolenaar4770d092006-01-12 23:22:24 +000011137 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011138 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011139 go_deeper(stack, depth, newscore);
11140#ifdef DEBUG_TRIEWALK
11141 if (!try_compound && !fword_ends)
11142 sprintf(changename[depth], "%.*s-%s: split",
11143 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11144 else
11145 sprintf(changename[depth], "%.*s-%s: compound",
11146 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11147#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011148 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000011149 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011150 sp->ts_state = STATE_SPLITUNDO;
11151
11152 ++depth;
11153 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011154
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011155 /* Append a space to preword when splitting. */
11156 if (!try_compound && !fword_ends)
11157 STRCAT(preword, " ");
Bram Moolenaar5195e452005-08-19 20:32:47 +000011158 sp->ts_prewordlen = STRLEN(preword);
11159 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +000011160 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011161
11162 /* If the badword has a non-word character at this
11163 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011164 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011165 * character when the word ends. But only when the
11166 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011167 if (((!try_compound && !spell_iswordp_nmw(fword
11168 + sp->ts_fidx))
11169 || fword_ends)
11170 && fword[sp->ts_fidx] != NUL
11171 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011172 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011173 int l;
11174
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011175#ifdef FEAT_MBYTE
11176 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011177 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011178 else
11179#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011180 l = 1;
11181 if (fword_ends)
11182 {
11183 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +000011184 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011185 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011186 sp->ts_prewordlen += l;
11187 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011188 }
11189 else
11190 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
11191 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011192 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011193
Bram Moolenaard12a1322005-08-21 22:08:24 +000011194 /* When compounding include compound flag in
11195 * compflags[] (already set above). When splitting we
11196 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011197 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +000011198 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011199 else
Bram Moolenaard12a1322005-08-21 22:08:24 +000011200 sp->ts_compsplit = sp->ts_complen;
11201 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011202
Bram Moolenaar53805d12005-08-01 07:08:33 +000011203 /* set su->su_badflags to the caps type at this
11204 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011205#ifdef FEAT_MBYTE
11206 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +000011207 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011208 else
11209#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +000011210 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011211 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011212 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011213
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011214 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011215 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011216
11217 /* If there are postponed prefixes, try these too. */
11218 if (pbyts != NULL)
11219 {
11220 byts = pbyts;
11221 idxs = pidxs;
11222 sp->ts_prefixdepth = PFD_PREFIXTREE;
11223 sp->ts_state = STATE_NOPREFIX;
11224 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011225 }
11226 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011227 }
11228 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011229
Bram Moolenaar4770d092006-01-12 23:22:24 +000011230 case STATE_SPLITUNDO:
11231 /* Undo the changes done for word split or compound word. */
11232 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011233
Bram Moolenaar4770d092006-01-12 23:22:24 +000011234 /* Continue looking for NUL bytes. */
11235 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011236
Bram Moolenaar4770d092006-01-12 23:22:24 +000011237 /* In case we went into the prefix tree. */
11238 byts = fbyts;
11239 idxs = fidxs;
11240 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011241
Bram Moolenaar4770d092006-01-12 23:22:24 +000011242 case STATE_ENDNUL:
11243 /* Past the NUL bytes in the node. */
11244 su->su_badflags = sp->ts_save_badflags;
11245 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011246#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011247 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011248#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011249 )
11250 {
11251 /* The badword ends, can't use STATE_PLAIN. */
11252 sp->ts_state = STATE_DEL;
11253 break;
11254 }
11255 sp->ts_state = STATE_PLAIN;
11256 /*FALLTHROUGH*/
11257
11258 case STATE_PLAIN:
11259 /*
11260 * Go over all possible bytes at this node, add each to tword[]
11261 * and use child node. "ts_curi" is the index.
11262 */
11263 arridx = sp->ts_arridx;
11264 if (sp->ts_curi > byts[arridx])
11265 {
11266 /* Done all bytes at this node, do next state. When still at
11267 * already changed bytes skip the other tricks. */
11268 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011269 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011270 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011271 sp->ts_state = STATE_FINAL;
11272 }
11273 else
11274 {
11275 arridx += sp->ts_curi++;
11276 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011277
Bram Moolenaar4770d092006-01-12 23:22:24 +000011278 /* Normal byte, go one level deeper. If it's not equal to the
11279 * byte in the bad word adjust the score. But don't even try
11280 * when the byte was already changed. And don't try when we
11281 * just deleted this byte, accepting it is always cheaper then
11282 * delete + substitute. */
11283 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +000011284#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011285 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011286#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011287 )
11288 newscore = 0;
11289 else
11290 newscore = SCORE_SUBST;
11291 if ((newscore == 0
11292 || (sp->ts_fidx >= sp->ts_fidxtry
11293 && ((sp->ts_flags & TSF_DIDDEL) == 0
11294 || c != fword[sp->ts_delidx])))
11295 && TRY_DEEPER(su, stack, depth, newscore))
11296 {
11297 go_deeper(stack, depth, newscore);
11298#ifdef DEBUG_TRIEWALK
11299 if (newscore > 0)
11300 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
11301 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11302 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011303 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011304 sprintf(changename[depth], "%.*s-%s: accept %c",
11305 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11306 fword[sp->ts_fidx]);
11307#endif
11308 ++depth;
11309 sp = &stack[depth];
11310 ++sp->ts_fidx;
11311 tword[sp->ts_twordlen++] = c;
11312 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +000011313#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011314 if (newscore == SCORE_SUBST)
11315 sp->ts_isdiff = DIFF_YES;
11316 if (has_mbyte)
11317 {
11318 /* Multi-byte characters are a bit complicated to
11319 * handle: They differ when any of the bytes differ
11320 * and then their length may also differ. */
11321 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011322 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011323 /* First byte. */
11324 sp->ts_tcharidx = 0;
11325 sp->ts_tcharlen = MB_BYTE2LEN(c);
11326 sp->ts_fcharstart = sp->ts_fidx - 1;
11327 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011328 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011329 }
11330 else if (sp->ts_isdiff == DIFF_INSERT)
11331 /* When inserting trail bytes don't advance in the
11332 * bad word. */
11333 --sp->ts_fidx;
11334 if (++sp->ts_tcharidx == sp->ts_tcharlen)
11335 {
11336 /* Last byte of character. */
11337 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +000011338 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011339 /* Correct ts_fidx for the byte length of the
11340 * character (we didn't check that before). */
11341 sp->ts_fidx = sp->ts_fcharstart
11342 + MB_BYTE2LEN(
Bram Moolenaarea424162005-06-16 21:51:00 +000011343 fword[sp->ts_fcharstart]);
11344
Bram Moolenaar4770d092006-01-12 23:22:24 +000011345 /* For changing a composing character adjust
11346 * the score from SCORE_SUBST to
11347 * SCORE_SUBCOMP. */
11348 if (enc_utf8
11349 && utf_iscomposing(
11350 mb_ptr2char(tword
11351 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011352 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011353 && utf_iscomposing(
11354 mb_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011355 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011356 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011357 SCORE_SUBST - SCORE_SUBCOMP;
11358
Bram Moolenaar4770d092006-01-12 23:22:24 +000011359 /* For a similar character adjust score from
11360 * SCORE_SUBST to SCORE_SIMILAR. */
11361 else if (!soundfold
11362 && slang->sl_has_map
11363 && similar_chars(slang,
11364 mb_ptr2char(tword
11365 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +000011366 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +000011367 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +000011368 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011369 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +000011370 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +000011371 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011372 else if (sp->ts_isdiff == DIFF_INSERT
11373 && sp->ts_twordlen > sp->ts_tcharlen)
11374 {
11375 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
11376 c = mb_ptr2char(p);
11377 if (enc_utf8 && utf_iscomposing(c))
11378 {
11379 /* Inserting a composing char doesn't
11380 * count that much. */
11381 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
11382 }
11383 else
11384 {
11385 /* If the previous character was the same,
11386 * thus doubling a character, give a bonus
11387 * to the score. Also for the soundfold
11388 * tree (might seem illogical but does
11389 * give better scores). */
11390 mb_ptr_back(tword, p);
11391 if (c == mb_ptr2char(p))
11392 sp->ts_score -= SCORE_INS
11393 - SCORE_INSDUP;
11394 }
11395 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011396
Bram Moolenaar4770d092006-01-12 23:22:24 +000011397 /* Starting a new char, reset the length. */
11398 sp->ts_tcharlen = 0;
11399 }
Bram Moolenaarea408852005-06-25 22:49:46 +000011400 }
Bram Moolenaarea424162005-06-16 21:51:00 +000011401 else
11402#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000011403 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011404 /* If we found a similar char adjust the score.
11405 * We do this after calling go_deeper() because
11406 * it's slow. */
11407 if (newscore != 0
11408 && !soundfold
11409 && slang->sl_has_map
11410 && similar_chars(slang,
11411 c, fword[sp->ts_fidx - 1]))
11412 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +000011413 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011414 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011415 }
11416 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011417
Bram Moolenaar4770d092006-01-12 23:22:24 +000011418 case STATE_DEL:
11419#ifdef FEAT_MBYTE
11420 /* When past the first byte of a multi-byte char don't try
11421 * delete/insert/swap a character. */
11422 if (has_mbyte && sp->ts_tcharlen > 0)
11423 {
11424 sp->ts_state = STATE_FINAL;
11425 break;
11426 }
11427#endif
11428 /*
11429 * Try skipping one character in the bad word (delete it).
11430 */
11431 sp->ts_state = STATE_INS_PREP;
11432 sp->ts_curi = 1;
11433 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
11434 /* Deleting a vowel at the start of a word counts less, see
11435 * soundalike_score(). */
11436 newscore = 2 * SCORE_DEL / 3;
11437 else
11438 newscore = SCORE_DEL;
11439 if (fword[sp->ts_fidx] != NUL
11440 && TRY_DEEPER(su, stack, depth, newscore))
11441 {
11442 go_deeper(stack, depth, newscore);
11443#ifdef DEBUG_TRIEWALK
11444 sprintf(changename[depth], "%.*s-%s: delete %c",
11445 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11446 fword[sp->ts_fidx]);
11447#endif
11448 ++depth;
11449
11450 /* Remember what character we deleted, so that we can avoid
11451 * inserting it again. */
11452 stack[depth].ts_flags |= TSF_DIDDEL;
11453 stack[depth].ts_delidx = sp->ts_fidx;
11454
11455 /* Advance over the character in fword[]. Give a bonus to the
11456 * score if the same character is following "nn" -> "n". It's
11457 * a bit illogical for soundfold tree but it does give better
11458 * results. */
11459#ifdef FEAT_MBYTE
11460 if (has_mbyte)
11461 {
11462 c = mb_ptr2char(fword + sp->ts_fidx);
11463 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
11464 if (enc_utf8 && utf_iscomposing(c))
11465 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
11466 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
11467 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
11468 }
11469 else
11470#endif
11471 {
11472 ++stack[depth].ts_fidx;
11473 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
11474 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
11475 }
11476 break;
11477 }
11478 /*FALLTHROUGH*/
11479
11480 case STATE_INS_PREP:
11481 if (sp->ts_flags & TSF_DIDDEL)
11482 {
11483 /* If we just deleted a byte then inserting won't make sense,
11484 * a substitute is always cheaper. */
11485 sp->ts_state = STATE_SWAP;
11486 break;
11487 }
11488
11489 /* skip over NUL bytes */
11490 n = sp->ts_arridx;
11491 for (;;)
11492 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011493 if (sp->ts_curi > byts[n])
11494 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011495 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011496 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011497 break;
11498 }
11499 if (byts[n + sp->ts_curi] != NUL)
11500 {
11501 /* Found a byte to insert. */
11502 sp->ts_state = STATE_INS;
11503 break;
11504 }
11505 ++sp->ts_curi;
11506 }
11507 break;
11508
11509 /*FALLTHROUGH*/
11510
11511 case STATE_INS:
11512 /* Insert one byte. Repeat this for each possible byte at this
11513 * node. */
11514 n = sp->ts_arridx;
11515 if (sp->ts_curi > byts[n])
11516 {
11517 /* Done all bytes at this node, go to next state. */
11518 sp->ts_state = STATE_SWAP;
11519 break;
11520 }
11521
11522 /* Do one more byte at this node, but:
11523 * - Skip NUL bytes.
11524 * - Skip the byte if it's equal to the byte in the word,
11525 * accepting that byte is always better.
11526 */
11527 n += sp->ts_curi++;
11528 c = byts[n];
11529 if (soundfold && sp->ts_twordlen == 0 && c == '*')
11530 /* Inserting a vowel at the start of a word counts less,
11531 * see soundalike_score(). */
11532 newscore = 2 * SCORE_INS / 3;
11533 else
11534 newscore = SCORE_INS;
11535 if (c != fword[sp->ts_fidx]
11536 && TRY_DEEPER(su, stack, depth, newscore))
11537 {
11538 go_deeper(stack, depth, newscore);
11539#ifdef DEBUG_TRIEWALK
11540 sprintf(changename[depth], "%.*s-%s: insert %c",
11541 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11542 c);
11543#endif
11544 ++depth;
11545 sp = &stack[depth];
11546 tword[sp->ts_twordlen++] = c;
11547 sp->ts_arridx = idxs[n];
11548#ifdef FEAT_MBYTE
11549 if (has_mbyte)
11550 {
11551 fl = MB_BYTE2LEN(c);
11552 if (fl > 1)
11553 {
11554 /* There are following bytes for the same character.
11555 * We must find all bytes before trying
11556 * delete/insert/swap/etc. */
11557 sp->ts_tcharlen = fl;
11558 sp->ts_tcharidx = 1;
11559 sp->ts_isdiff = DIFF_INSERT;
11560 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011561 }
11562 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011563 fl = 1;
11564 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000011565#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011566 {
11567 /* If the previous character was the same, thus doubling a
11568 * character, give a bonus to the score. Also for
11569 * soundfold words (illogical but does give a better
11570 * score). */
11571 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +000011572 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011573 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011574 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011575 }
11576 break;
11577
11578 case STATE_SWAP:
11579 /*
11580 * Swap two bytes in the bad word: "12" -> "21".
11581 * We change "fword" here, it's changed back afterwards at
11582 * STATE_UNSWAP.
11583 */
11584 p = fword + sp->ts_fidx;
11585 c = *p;
11586 if (c == NUL)
11587 {
11588 /* End of word, can't swap or replace. */
11589 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011590 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011591 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011592
Bram Moolenaar4770d092006-01-12 23:22:24 +000011593 /* Don't swap if the first character is not a word character.
11594 * SWAP3 etc. also don't make sense then. */
11595 if (!soundfold && !spell_iswordp(p, curbuf))
11596 {
11597 sp->ts_state = STATE_REP_INI;
11598 break;
11599 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000011600
Bram Moolenaar4770d092006-01-12 23:22:24 +000011601#ifdef FEAT_MBYTE
11602 if (has_mbyte)
11603 {
11604 n = mb_cptr2len(p);
11605 c = mb_ptr2char(p);
11606 if (!soundfold && !spell_iswordp(p + n, curbuf))
11607 c2 = c; /* don't swap non-word char */
11608 else
11609 c2 = mb_ptr2char(p + n);
11610 }
11611 else
11612#endif
11613 {
11614 if (!soundfold && !spell_iswordp(p + 1, curbuf))
11615 c2 = c; /* don't swap non-word char */
11616 else
11617 c2 = p[1];
11618 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000011619
Bram Moolenaar4770d092006-01-12 23:22:24 +000011620 /* When characters are identical, swap won't do anything.
11621 * Also get here if the second char is not a word character. */
11622 if (c == c2)
11623 {
11624 sp->ts_state = STATE_SWAP3;
11625 break;
11626 }
11627 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
11628 {
11629 go_deeper(stack, depth, SCORE_SWAP);
11630#ifdef DEBUG_TRIEWALK
11631 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
11632 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11633 c, c2);
11634#endif
11635 sp->ts_state = STATE_UNSWAP;
11636 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000011637#ifdef FEAT_MBYTE
11638 if (has_mbyte)
11639 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011640 fl = mb_char2len(c2);
11641 mch_memmove(p, p + n, fl);
11642 mb_char2bytes(c, p + fl);
11643 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000011644 }
11645 else
11646#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000011647 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011648 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +000011649 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011650 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +000011651 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011652 }
11653 else
11654 /* If this swap doesn't work then SWAP3 won't either. */
11655 sp->ts_state = STATE_REP_INI;
11656 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000011657
Bram Moolenaar4770d092006-01-12 23:22:24 +000011658 case STATE_UNSWAP:
11659 /* Undo the STATE_SWAP swap: "21" -> "12". */
11660 p = fword + sp->ts_fidx;
11661#ifdef FEAT_MBYTE
11662 if (has_mbyte)
11663 {
11664 n = MB_BYTE2LEN(*p);
11665 c = mb_ptr2char(p + n);
11666 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
11667 mb_char2bytes(c, p);
11668 }
11669 else
11670#endif
11671 {
11672 c = *p;
11673 *p = p[1];
11674 p[1] = c;
11675 }
11676 /*FALLTHROUGH*/
11677
11678 case STATE_SWAP3:
11679 /* Swap two bytes, skipping one: "123" -> "321". We change
11680 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
11681 p = fword + sp->ts_fidx;
11682#ifdef FEAT_MBYTE
11683 if (has_mbyte)
11684 {
11685 n = mb_cptr2len(p);
11686 c = mb_ptr2char(p);
11687 fl = mb_cptr2len(p + n);
11688 c2 = mb_ptr2char(p + n);
11689 if (!soundfold && !spell_iswordp(p + n + fl, curbuf))
11690 c3 = c; /* don't swap non-word char */
11691 else
11692 c3 = mb_ptr2char(p + n + fl);
11693 }
11694 else
11695#endif
11696 {
11697 c = *p;
11698 c2 = p[1];
11699 if (!soundfold && !spell_iswordp(p + 2, curbuf))
11700 c3 = c; /* don't swap non-word char */
11701 else
11702 c3 = p[2];
11703 }
11704
11705 /* When characters are identical: "121" then SWAP3 result is
11706 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
11707 * same as SWAP on next char: "112". Thus skip all swapping.
11708 * Also skip when c3 is NUL.
11709 * Also get here when the third character is not a word character.
11710 * Second character may any char: "a.b" -> "b.a" */
11711 if (c == c3 || c3 == NUL)
11712 {
11713 sp->ts_state = STATE_REP_INI;
11714 break;
11715 }
11716 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
11717 {
11718 go_deeper(stack, depth, SCORE_SWAP3);
11719#ifdef DEBUG_TRIEWALK
11720 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
11721 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11722 c, c3);
11723#endif
11724 sp->ts_state = STATE_UNSWAP3;
11725 ++depth;
11726#ifdef FEAT_MBYTE
11727 if (has_mbyte)
11728 {
11729 tl = mb_char2len(c3);
11730 mch_memmove(p, p + n + fl, tl);
11731 mb_char2bytes(c2, p + tl);
11732 mb_char2bytes(c, p + fl + tl);
11733 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
11734 }
11735 else
11736#endif
11737 {
11738 p[0] = p[2];
11739 p[2] = c;
11740 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
11741 }
11742 }
11743 else
11744 sp->ts_state = STATE_REP_INI;
11745 break;
11746
11747 case STATE_UNSWAP3:
11748 /* Undo STATE_SWAP3: "321" -> "123" */
11749 p = fword + sp->ts_fidx;
11750#ifdef FEAT_MBYTE
11751 if (has_mbyte)
11752 {
11753 n = MB_BYTE2LEN(*p);
11754 c2 = mb_ptr2char(p + n);
11755 fl = MB_BYTE2LEN(p[n]);
11756 c = mb_ptr2char(p + n + fl);
11757 tl = MB_BYTE2LEN(p[n + fl]);
11758 mch_memmove(p + fl + tl, p, n);
11759 mb_char2bytes(c, p);
11760 mb_char2bytes(c2, p + tl);
11761 p = p + tl;
11762 }
11763 else
11764#endif
11765 {
11766 c = *p;
11767 *p = p[2];
11768 p[2] = c;
11769 ++p;
11770 }
11771
11772 if (!soundfold && !spell_iswordp(p, curbuf))
11773 {
11774 /* Middle char is not a word char, skip the rotate. First and
11775 * third char were already checked at swap and swap3. */
11776 sp->ts_state = STATE_REP_INI;
11777 break;
11778 }
11779
11780 /* Rotate three characters left: "123" -> "231". We change
11781 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
11782 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
11783 {
11784 go_deeper(stack, depth, SCORE_SWAP3);
11785#ifdef DEBUG_TRIEWALK
11786 p = fword + sp->ts_fidx;
11787 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
11788 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11789 p[0], p[1], p[2]);
11790#endif
11791 sp->ts_state = STATE_UNROT3L;
11792 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000011793 p = fword + sp->ts_fidx;
11794#ifdef FEAT_MBYTE
11795 if (has_mbyte)
11796 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011797 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000011798 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011799 fl = mb_cptr2len(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011800 fl += mb_cptr2len(p + n + fl);
11801 mch_memmove(p, p + n, fl);
11802 mb_char2bytes(c, p + fl);
11803 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000011804 }
11805 else
11806#endif
11807 {
11808 c = *p;
11809 *p = p[1];
11810 p[1] = p[2];
11811 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011812 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +000011813 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011814 }
11815 else
11816 sp->ts_state = STATE_REP_INI;
11817 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011818
Bram Moolenaar4770d092006-01-12 23:22:24 +000011819 case STATE_UNROT3L:
11820 /* Undo ROT3L: "231" -> "123" */
11821 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +000011822#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011823 if (has_mbyte)
11824 {
11825 n = MB_BYTE2LEN(*p);
11826 n += MB_BYTE2LEN(p[n]);
11827 c = mb_ptr2char(p + n);
11828 tl = MB_BYTE2LEN(p[n]);
11829 mch_memmove(p + tl, p, n);
11830 mb_char2bytes(c, p);
11831 }
11832 else
Bram Moolenaarea424162005-06-16 21:51:00 +000011833#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011834 {
11835 c = p[2];
11836 p[2] = p[1];
11837 p[1] = *p;
11838 *p = c;
11839 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011840
Bram Moolenaar4770d092006-01-12 23:22:24 +000011841 /* Rotate three bytes right: "123" -> "312". We change "fword"
11842 * here, it's changed back afterwards at STATE_UNROT3R. */
11843 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
11844 {
11845 go_deeper(stack, depth, SCORE_SWAP3);
11846#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011847 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011848 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
11849 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11850 p[0], p[1], p[2]);
11851#endif
11852 sp->ts_state = STATE_UNROT3R;
11853 ++depth;
11854 p = fword + sp->ts_fidx;
11855#ifdef FEAT_MBYTE
11856 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000011857 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011858 n = mb_cptr2len(p);
11859 n += mb_cptr2len(p + n);
11860 c = mb_ptr2char(p + n);
11861 tl = mb_cptr2len(p + n);
11862 mch_memmove(p + tl, p, n);
11863 mb_char2bytes(c, p);
11864 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011865 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011866 else
11867#endif
11868 {
11869 c = p[2];
11870 p[2] = p[1];
11871 p[1] = *p;
11872 *p = c;
11873 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
11874 }
11875 }
11876 else
11877 sp->ts_state = STATE_REP_INI;
11878 break;
11879
11880 case STATE_UNROT3R:
11881 /* Undo ROT3R: "312" -> "123" */
11882 p = fword + sp->ts_fidx;
11883#ifdef FEAT_MBYTE
11884 if (has_mbyte)
11885 {
11886 c = mb_ptr2char(p);
11887 tl = MB_BYTE2LEN(*p);
11888 n = MB_BYTE2LEN(p[tl]);
11889 n += MB_BYTE2LEN(p[tl + n]);
11890 mch_memmove(p, p + tl, n);
11891 mb_char2bytes(c, p + n);
11892 }
11893 else
11894#endif
11895 {
11896 c = *p;
11897 *p = p[1];
11898 p[1] = p[2];
11899 p[2] = c;
11900 }
11901 /*FALLTHROUGH*/
11902
11903 case STATE_REP_INI:
11904 /* Check if matching with REP items from the .aff file would work.
11905 * Quickly skip if:
11906 * - there are no REP items and we are not in the soundfold trie
11907 * - the score is going to be too high anyway
11908 * - already applied a REP item or swapped here */
11909 if ((lp->lp_replang == NULL && !soundfold)
11910 || sp->ts_score + SCORE_REP >= su->su_maxscore
11911 || sp->ts_fidx < sp->ts_fidxtry)
11912 {
11913 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011914 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011915 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011916
Bram Moolenaar4770d092006-01-12 23:22:24 +000011917 /* Use the first byte to quickly find the first entry that may
11918 * match. If the index is -1 there is none. */
11919 if (soundfold)
11920 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
11921 else
11922 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011923
Bram Moolenaar4770d092006-01-12 23:22:24 +000011924 if (sp->ts_curi < 0)
11925 {
11926 sp->ts_state = STATE_FINAL;
11927 break;
11928 }
11929
11930 sp->ts_state = STATE_REP;
11931 /*FALLTHROUGH*/
11932
11933 case STATE_REP:
11934 /* Try matching with REP items from the .aff file. For each match
11935 * replace the characters and check if the resulting word is
11936 * valid. */
11937 p = fword + sp->ts_fidx;
11938
11939 if (soundfold)
11940 gap = &slang->sl_repsal;
11941 else
11942 gap = &lp->lp_replang->sl_rep;
11943 while (sp->ts_curi < gap->ga_len)
11944 {
11945 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
11946 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011947 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011948 /* past possible matching entries */
11949 sp->ts_curi = gap->ga_len;
11950 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011951 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011952 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
11953 && TRY_DEEPER(su, stack, depth, SCORE_REP))
11954 {
11955 go_deeper(stack, depth, SCORE_REP);
11956#ifdef DEBUG_TRIEWALK
11957 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
11958 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11959 ftp->ft_from, ftp->ft_to);
11960#endif
11961 /* Need to undo this afterwards. */
11962 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011963
Bram Moolenaar4770d092006-01-12 23:22:24 +000011964 /* Change the "from" to the "to" string. */
11965 ++depth;
11966 fl = STRLEN(ftp->ft_from);
11967 tl = STRLEN(ftp->ft_to);
11968 if (fl != tl)
11969 {
11970 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
11971 repextra += tl - fl;
11972 }
11973 mch_memmove(p, ftp->ft_to, tl);
11974 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
11975#ifdef FEAT_MBYTE
11976 stack[depth].ts_tcharlen = 0;
11977#endif
11978 break;
11979 }
11980 }
11981
11982 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
11983 /* No (more) matches. */
11984 sp->ts_state = STATE_FINAL;
11985
11986 break;
11987
11988 case STATE_REP_UNDO:
11989 /* Undo a REP replacement and continue with the next one. */
11990 if (soundfold)
11991 gap = &slang->sl_repsal;
11992 else
11993 gap = &lp->lp_replang->sl_rep;
11994 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
11995 fl = STRLEN(ftp->ft_from);
11996 tl = STRLEN(ftp->ft_to);
11997 p = fword + sp->ts_fidx;
11998 if (fl != tl)
11999 {
12000 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
12001 repextra -= tl - fl;
12002 }
12003 mch_memmove(p, ftp->ft_from, fl);
12004 sp->ts_state = STATE_REP;
12005 break;
12006
12007 default:
12008 /* Did all possible states at this level, go up one level. */
12009 --depth;
12010
12011 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
12012 {
12013 /* Continue in or go back to the prefix tree. */
12014 byts = pbyts;
12015 idxs = pidxs;
12016 }
12017
12018 /* Don't check for CTRL-C too often, it takes time. */
12019 if (--breakcheckcount == 0)
12020 {
12021 ui_breakcheck();
12022 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012023 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012024 }
12025 }
12026}
12027
Bram Moolenaar4770d092006-01-12 23:22:24 +000012028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012029/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012030 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012031 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012032 static void
12033go_deeper(stack, depth, score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012034 trystate_T *stack;
12035 int depth;
12036 int score_add;
12037{
Bram Moolenaarea424162005-06-16 21:51:00 +000012038 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012039 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012040 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012041 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000012042 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012043}
12044
Bram Moolenaar53805d12005-08-01 07:08:33 +000012045#ifdef FEAT_MBYTE
12046/*
12047 * Case-folding may change the number of bytes: Count nr of chars in
12048 * fword[flen] and return the byte length of that many chars in "word".
12049 */
12050 static int
12051nofold_len(fword, flen, word)
12052 char_u *fword;
12053 int flen;
12054 char_u *word;
12055{
12056 char_u *p;
12057 int i = 0;
12058
12059 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12060 ++i;
12061 for (p = word; i > 0; mb_ptr_adv(p))
12062 --i;
12063 return (int)(p - word);
12064}
12065#endif
12066
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012067/*
12068 * "fword" is a good word with case folded. Find the matching keep-case
12069 * words and put it in "kword".
12070 * Theoretically there could be several keep-case words that result in the
12071 * same case-folded word, but we only find one...
12072 */
12073 static void
12074find_keepcap_word(slang, fword, kword)
12075 slang_T *slang;
12076 char_u *fword;
12077 char_u *kword;
12078{
12079 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12080 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012081 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012082
12083 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012084 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012085 int round[MAXWLEN];
12086 int fwordidx[MAXWLEN];
12087 int uwordidx[MAXWLEN];
12088 int kwordlen[MAXWLEN];
12089
12090 int flen, ulen;
12091 int l;
12092 int len;
12093 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012094 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012095 char_u *p;
12096 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012097 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012098
12099 if (byts == NULL)
12100 {
12101 /* array is empty: "cannot happen" */
12102 *kword = NUL;
12103 return;
12104 }
12105
12106 /* Make an all-cap version of "fword". */
12107 allcap_copy(fword, uword);
12108
12109 /*
12110 * Each character needs to be tried both case-folded and upper-case.
12111 * All this gets very complicated if we keep in mind that changing case
12112 * may change the byte length of a multi-byte character...
12113 */
12114 depth = 0;
12115 arridx[0] = 0;
12116 round[0] = 0;
12117 fwordidx[0] = 0;
12118 uwordidx[0] = 0;
12119 kwordlen[0] = 0;
12120 while (depth >= 0)
12121 {
12122 if (fword[fwordidx[depth]] == NUL)
12123 {
12124 /* We are at the end of "fword". If the tree allows a word to end
12125 * here we have found a match. */
12126 if (byts[arridx[depth] + 1] == 0)
12127 {
12128 kword[kwordlen[depth]] = NUL;
12129 return;
12130 }
12131
12132 /* kword is getting too long, continue one level up */
12133 --depth;
12134 }
12135 else if (++round[depth] > 2)
12136 {
12137 /* tried both fold-case and upper-case character, continue one
12138 * level up */
12139 --depth;
12140 }
12141 else
12142 {
12143 /*
12144 * round[depth] == 1: Try using the folded-case character.
12145 * round[depth] == 2: Try using the upper-case character.
12146 */
12147#ifdef FEAT_MBYTE
12148 if (has_mbyte)
12149 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012150 flen = mb_cptr2len(fword + fwordidx[depth]);
12151 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012152 }
12153 else
12154#endif
12155 ulen = flen = 1;
12156 if (round[depth] == 1)
12157 {
12158 p = fword + fwordidx[depth];
12159 l = flen;
12160 }
12161 else
12162 {
12163 p = uword + uwordidx[depth];
12164 l = ulen;
12165 }
12166
12167 for (tryidx = arridx[depth]; l > 0; --l)
12168 {
12169 /* Perform a binary search in the list of accepted bytes. */
12170 len = byts[tryidx++];
12171 c = *p++;
12172 lo = tryidx;
12173 hi = tryidx + len - 1;
12174 while (lo < hi)
12175 {
12176 m = (lo + hi) / 2;
12177 if (byts[m] > c)
12178 hi = m - 1;
12179 else if (byts[m] < c)
12180 lo = m + 1;
12181 else
12182 {
12183 lo = hi = m;
12184 break;
12185 }
12186 }
12187
12188 /* Stop if there is no matching byte. */
12189 if (hi < lo || byts[lo] != c)
12190 break;
12191
12192 /* Continue at the child (if there is one). */
12193 tryidx = idxs[lo];
12194 }
12195
12196 if (l == 0)
12197 {
12198 /*
12199 * Found the matching char. Copy it to "kword" and go a
12200 * level deeper.
12201 */
12202 if (round[depth] == 1)
12203 {
12204 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
12205 flen);
12206 kwordlen[depth + 1] = kwordlen[depth] + flen;
12207 }
12208 else
12209 {
12210 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
12211 ulen);
12212 kwordlen[depth + 1] = kwordlen[depth] + ulen;
12213 }
12214 fwordidx[depth + 1] = fwordidx[depth] + flen;
12215 uwordidx[depth + 1] = uwordidx[depth] + ulen;
12216
12217 ++depth;
12218 arridx[depth] = tryidx;
12219 round[depth] = 0;
12220 }
12221 }
12222 }
12223
12224 /* Didn't find it: "cannot happen". */
12225 *kword = NUL;
12226}
12227
12228/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012229 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
12230 * su->su_sga.
12231 */
12232 static void
12233score_comp_sal(su)
12234 suginfo_T *su;
12235{
12236 langp_T *lp;
12237 char_u badsound[MAXWLEN];
12238 int i;
12239 suggest_T *stp;
12240 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012241 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012242 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012243
12244 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
12245 return;
12246
12247 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012248 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012249 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012250 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012251 if (lp->lp_slang->sl_sal.ga_len > 0)
12252 {
12253 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012254 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012255
12256 for (i = 0; i < su->su_ga.ga_len; ++i)
12257 {
12258 stp = &SUG(su->su_ga, i);
12259
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012260 /* Case-fold the suggested word, sound-fold it and compute the
12261 * sound-a-like score. */
12262 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012263 if (score < SCORE_MAXMAX)
12264 {
12265 /* Add the suggestion. */
12266 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
12267 sstp->st_word = vim_strsave(stp->st_word);
12268 if (sstp->st_word != NULL)
12269 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012270 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012271 sstp->st_score = score;
12272 sstp->st_altscore = 0;
12273 sstp->st_orglen = stp->st_orglen;
12274 ++su->su_sga.ga_len;
12275 }
12276 }
12277 }
12278 break;
12279 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012280 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012281}
12282
12283/*
12284 * Combine the list of suggestions in su->su_ga and su->su_sga.
12285 * They are intwined.
12286 */
12287 static void
12288score_combine(su)
12289 suginfo_T *su;
12290{
12291 int i;
12292 int j;
12293 garray_T ga;
12294 garray_T *gap;
12295 langp_T *lp;
12296 suggest_T *stp;
12297 char_u *p;
12298 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012299 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012300 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012301 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012302
12303 /* Add the alternate score to su_ga. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012304 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012305 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012306 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012307 if (lp->lp_slang->sl_sal.ga_len > 0)
12308 {
12309 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012310 slang = lp->lp_slang;
12311 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012312
12313 for (i = 0; i < su->su_ga.ga_len; ++i)
12314 {
12315 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012316 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012317 if (stp->st_altscore == SCORE_MAXMAX)
12318 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
12319 else
12320 stp->st_score = (stp->st_score * 3
12321 + stp->st_altscore) / 4;
12322 stp->st_salscore = FALSE;
12323 }
12324 break;
12325 }
12326 }
12327
Bram Moolenaar4770d092006-01-12 23:22:24 +000012328 if (slang == NULL) /* just in case */
12329 return;
12330
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012331 /* Add the alternate score to su_sga. */
12332 for (i = 0; i < su->su_sga.ga_len; ++i)
12333 {
12334 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012335 stp->st_altscore = spell_edit_score(slang,
12336 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012337 if (stp->st_score == SCORE_MAXMAX)
12338 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
12339 else
12340 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
12341 stp->st_salscore = TRUE;
12342 }
12343
Bram Moolenaar4770d092006-01-12 23:22:24 +000012344 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
12345 * for both lists. */
12346 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012347 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012348 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012349 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
12350
12351 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
12352 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
12353 return;
12354
12355 stp = &SUG(ga, 0);
12356 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
12357 {
12358 /* round 1: get a suggestion from su_ga
12359 * round 2: get a suggestion from su_sga */
12360 for (round = 1; round <= 2; ++round)
12361 {
12362 gap = round == 1 ? &su->su_ga : &su->su_sga;
12363 if (i < gap->ga_len)
12364 {
12365 /* Don't add a word if it's already there. */
12366 p = SUG(*gap, i).st_word;
12367 for (j = 0; j < ga.ga_len; ++j)
12368 if (STRCMP(stp[j].st_word, p) == 0)
12369 break;
12370 if (j == ga.ga_len)
12371 stp[ga.ga_len++] = SUG(*gap, i);
12372 else
12373 vim_free(p);
12374 }
12375 }
12376 }
12377
12378 ga_clear(&su->su_ga);
12379 ga_clear(&su->su_sga);
12380
12381 /* Truncate the list to the number of suggestions that will be displayed. */
12382 if (ga.ga_len > su->su_maxcount)
12383 {
12384 for (i = su->su_maxcount; i < ga.ga_len; ++i)
12385 vim_free(stp[i].st_word);
12386 ga.ga_len = su->su_maxcount;
12387 }
12388
12389 su->su_ga = ga;
12390}
12391
12392/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012393 * For the goodword in "stp" compute the soundalike score compared to the
12394 * badword.
12395 */
12396 static int
12397stp_sal_score(stp, su, slang, badsound)
12398 suggest_T *stp;
12399 suginfo_T *su;
12400 slang_T *slang;
12401 char_u *badsound; /* sound-folded badword */
12402{
12403 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012404 char_u *pbad;
12405 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012406 char_u badsound2[MAXWLEN];
12407 char_u fword[MAXWLEN];
12408 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012409 char_u goodword[MAXWLEN];
12410 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012411
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012412 lendiff = (int)(su->su_badlen - stp->st_orglen);
12413 if (lendiff >= 0)
12414 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012415 else
12416 {
12417 /* soundfold the bad word with more characters following */
12418 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
12419
12420 /* When joining two words the sound often changes a lot. E.g., "t he"
12421 * sounds like "t h" while "the" sounds like "@". Avoid that by
12422 * removing the space. Don't do it when the good word also contains a
12423 * space. */
12424 if (vim_iswhite(su->su_badptr[su->su_badlen])
12425 && *skiptowhite(stp->st_word) == NUL)
12426 for (p = fword; *(p = skiptowhite(p)) != NUL; )
12427 mch_memmove(p, p + 1, STRLEN(p));
12428
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012429 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012430 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012431 }
12432
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012433 if (lendiff > 0)
12434 {
12435 /* Add part of the bad word to the good word, so that we soundfold
12436 * what replaces the bad word. */
12437 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012438 vim_strncpy(goodword + stp->st_wordlen,
12439 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012440 pgood = goodword;
12441 }
12442 else
12443 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012444
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012445 /* Sound-fold the word and compute the score for the difference. */
12446 spell_soundfold(slang, pgood, FALSE, goodsound);
12447
12448 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012449}
12450
Bram Moolenaar4770d092006-01-12 23:22:24 +000012451/* structure used to store soundfolded words that add_sound_suggest() has
12452 * handled already. */
12453typedef struct
12454{
12455 short sft_score; /* lowest score used */
12456 char_u sft_word[1]; /* soundfolded word, actually longer */
12457} sftword_T;
12458
12459static sftword_T dumsft;
12460#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
12461#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
12462
12463/*
12464 * Prepare for calling suggest_try_soundalike().
12465 */
12466 static void
12467suggest_try_soundalike_prep()
12468{
12469 langp_T *lp;
12470 int lpi;
12471 slang_T *slang;
12472
12473 /* Do this for all languages that support sound folding and for which a
12474 * .sug file has been loaded. */
12475 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
12476 {
12477 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
12478 slang = lp->lp_slang;
12479 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
12480 /* prepare the hashtable used by add_sound_suggest() */
12481 hash_init(&slang->sl_sounddone);
12482 }
12483}
12484
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012485/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012486 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012487 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012488 */
12489 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000012490suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012491 suginfo_T *su;
12492{
12493 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012494 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012495 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012496 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012497
Bram Moolenaar4770d092006-01-12 23:22:24 +000012498 /* Do this for all languages that support sound folding and for which a
12499 * .sug file has been loaded. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012500 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012501 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012502 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
12503 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012504 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012505 {
12506 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012507 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012508
Bram Moolenaar4770d092006-01-12 23:22:24 +000012509 /* try all kinds of inserts/deletes/swaps/etc. */
12510 /* TODO: also soundfold the next words, so that we can try joining
12511 * and splitting */
12512 suggest_trie_walk(su, lp, salword, TRUE);
12513 }
12514 }
12515}
12516
12517/*
12518 * Finish up after calling suggest_try_soundalike().
12519 */
12520 static void
12521suggest_try_soundalike_finish()
12522{
12523 langp_T *lp;
12524 int lpi;
12525 slang_T *slang;
12526 int todo;
12527 hashitem_T *hi;
12528
12529 /* Do this for all languages that support sound folding and for which a
12530 * .sug file has been loaded. */
12531 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
12532 {
12533 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
12534 slang = lp->lp_slang;
12535 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
12536 {
12537 /* Free the info about handled words. */
12538 todo = slang->sl_sounddone.ht_used;
12539 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
12540 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012541 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012542 vim_free(HI2SFT(hi));
12543 --todo;
12544 }
12545 hash_clear(&slang->sl_sounddone);
12546 }
12547 }
12548}
12549
12550/*
12551 * A match with a soundfolded word is found. Add the good word(s) that
12552 * produce this soundfolded word.
12553 */
12554 static void
12555add_sound_suggest(su, goodword, score, lp)
12556 suginfo_T *su;
12557 char_u *goodword;
12558 int score; /* soundfold score */
12559 langp_T *lp;
12560{
12561 slang_T *slang = lp->lp_slang; /* language for sound folding */
12562 int sfwordnr;
12563 char_u *nrline;
12564 int orgnr;
12565 char_u theword[MAXWLEN];
12566 int i;
12567 int wlen;
12568 char_u *byts;
12569 idx_T *idxs;
12570 int n;
12571 int wordcount;
12572 int wc;
12573 int goodscore;
12574 hash_T hash;
12575 hashitem_T *hi;
12576 sftword_T *sft;
12577 int bc, gc;
12578 int limit;
12579
12580 /*
12581 * It's very well possible that the same soundfold word is found several
12582 * times with different scores. Since the following is quite slow only do
12583 * the words that have a better score than before. Use a hashtable to
12584 * remember the words that have been done.
12585 */
12586 hash = hash_hash(goodword);
12587 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
12588 if (HASHITEM_EMPTY(hi))
12589 {
12590 sft = (sftword_T *)alloc(sizeof(sftword_T) + STRLEN(goodword));
12591 if (sft != NULL)
12592 {
12593 sft->sft_score = score;
12594 STRCPY(sft->sft_word, goodword);
12595 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
12596 }
12597 }
12598 else
12599 {
12600 sft = HI2SFT(hi);
12601 if (score >= sft->sft_score)
12602 return;
12603 sft->sft_score = score;
12604 }
12605
12606 /*
12607 * Find the word nr in the soundfold tree.
12608 */
12609 sfwordnr = soundfold_find(slang, goodword);
12610 if (sfwordnr < 0)
12611 {
12612 EMSG2(_(e_intern2), "add_sound_suggest()");
12613 return;
12614 }
12615
12616 /*
12617 * go over the list of good words that produce this soundfold word
12618 */
12619 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
12620 orgnr = 0;
12621 while (*nrline != NUL)
12622 {
12623 /* The wordnr was stored in a minimal nr of bytes as an offset to the
12624 * previous wordnr. */
12625 orgnr += bytes2offset(&nrline);
12626
12627 byts = slang->sl_fbyts;
12628 idxs = slang->sl_fidxs;
12629
12630 /* Lookup the word "orgnr" one of the two tries. */
12631 n = 0;
12632 wlen = 0;
12633 wordcount = 0;
12634 for (;;)
12635 {
12636 i = 1;
12637 if (wordcount == orgnr && byts[n + 1] == NUL)
12638 break; /* found end of word */
12639
12640 if (byts[n + 1] == NUL)
12641 ++wordcount;
12642
12643 /* skip over the NUL bytes */
12644 for ( ; byts[n + i] == NUL; ++i)
12645 if (i > byts[n]) /* safety check */
12646 {
12647 STRCPY(theword + wlen, "BAD");
12648 goto badword;
12649 }
12650
12651 /* One of the siblings must have the word. */
12652 for ( ; i < byts[n]; ++i)
12653 {
12654 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
12655 if (wordcount + wc > orgnr)
12656 break;
12657 wordcount += wc;
12658 }
12659
12660 theword[wlen++] = byts[n + i];
12661 n = idxs[n + i];
12662 }
12663badword:
12664 theword[wlen] = NUL;
12665
12666 /* Go over the possible flags and regions. */
12667 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
12668 {
12669 char_u cword[MAXWLEN];
12670 char_u *p;
12671 int flags = (int)idxs[n + i];
12672
12673 if (flags & WF_KEEPCAP)
12674 {
12675 /* Must find the word in the keep-case tree. */
12676 find_keepcap_word(slang, theword, cword);
12677 p = cword;
12678 }
12679 else
12680 {
12681 flags |= su->su_badflags;
12682 if ((flags & WF_CAPMASK) != 0)
12683 {
12684 /* Need to fix case according to "flags". */
12685 make_case_word(theword, cword, flags);
12686 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012687 }
12688 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012689 p = theword;
12690 }
12691
12692 /* Add the suggestion. */
12693 if (sps_flags & SPS_DOUBLE)
12694 {
12695 /* Add the suggestion if the score isn't too bad. */
12696 if (score <= su->su_maxscore)
12697 add_suggestion(su, &su->su_sga, p, su->su_badlen,
12698 score, 0, FALSE, slang, FALSE);
12699 }
12700 else
12701 {
12702 /* Add a penalty for words in another region. */
12703 if ((flags & WF_REGION)
12704 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
12705 goodscore = SCORE_REGION;
12706 else
12707 goodscore = 0;
12708
12709 /* Add a small penalty for changing the first letter from
12710 * lower to upper case. Helps for "tath" -> "Kath", which is
12711 * less common thatn "tath" -> "path". Don't do it when the
12712 * letter is the same, that has already been counted. */
12713 gc = PTR2CHAR(p);
12714 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012715 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012716 bc = PTR2CHAR(su->su_badword);
12717 if (!SPELL_ISUPPER(bc)
12718 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
12719 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012720 }
12721
Bram Moolenaar4770d092006-01-12 23:22:24 +000012722 /* Compute the score for the good word. This only does letter
12723 * insert/delete/swap/replace. REP items are not considered,
12724 * which may make the score a bit higher.
12725 * Use a limit for the score to make it work faster. Use
12726 * MAXSCORE(), because RESCORE() will change the score.
12727 * If the limit is very high then the iterative method is
12728 * inefficient, using an array is quicker. */
12729 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
12730 if (limit > SCORE_LIMITMAX)
12731 goodscore += spell_edit_score(slang, su->su_badword, p);
12732 else
12733 goodscore += spell_edit_score_limit(slang, su->su_badword,
12734 p, limit);
12735
12736 /* When going over the limit don't bother to do the rest. */
12737 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012738 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012739 /* Give a bonus to words seen before. */
12740 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012741
Bram Moolenaar4770d092006-01-12 23:22:24 +000012742 /* Add the suggestion if the score isn't too bad. */
12743 goodscore = RESCORE(goodscore, score);
12744 if (goodscore <= su->su_sfmaxscore)
12745 add_suggestion(su, &su->su_ga, p, su->su_badlen,
12746 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012747 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012748 }
12749 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012750 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012751 }
12752}
12753
12754/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012755 * Find word "word" in fold-case tree for "slang" and return the word number.
12756 */
12757 static int
12758soundfold_find(slang, word)
12759 slang_T *slang;
12760 char_u *word;
12761{
12762 idx_T arridx = 0;
12763 int len;
12764 int wlen = 0;
12765 int c;
12766 char_u *ptr = word;
12767 char_u *byts;
12768 idx_T *idxs;
12769 int wordnr = 0;
12770
12771 byts = slang->sl_sbyts;
12772 idxs = slang->sl_sidxs;
12773
12774 for (;;)
12775 {
12776 /* First byte is the number of possible bytes. */
12777 len = byts[arridx++];
12778
12779 /* If the first possible byte is a zero the word could end here.
12780 * If the word ends we found the word. If not skip the NUL bytes. */
12781 c = ptr[wlen];
12782 if (byts[arridx] == NUL)
12783 {
12784 if (c == NUL)
12785 break;
12786
12787 /* Skip over the zeros, there can be several. */
12788 while (len > 0 && byts[arridx] == NUL)
12789 {
12790 ++arridx;
12791 --len;
12792 }
12793 if (len == 0)
12794 return -1; /* no children, word should have ended here */
12795 ++wordnr;
12796 }
12797
12798 /* If the word ends we didn't find it. */
12799 if (c == NUL)
12800 return -1;
12801
12802 /* Perform a binary search in the list of accepted bytes. */
12803 if (c == TAB) /* <Tab> is handled like <Space> */
12804 c = ' ';
12805 while (byts[arridx] < c)
12806 {
12807 /* The word count is in the first idxs[] entry of the child. */
12808 wordnr += idxs[idxs[arridx]];
12809 ++arridx;
12810 if (--len == 0) /* end of the bytes, didn't find it */
12811 return -1;
12812 }
12813 if (byts[arridx] != c) /* didn't find the byte */
12814 return -1;
12815
12816 /* Continue at the child (if there is one). */
12817 arridx = idxs[arridx];
12818 ++wlen;
12819
12820 /* One space in the good word may stand for several spaces in the
12821 * checked word. */
12822 if (c == ' ')
12823 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
12824 ++wlen;
12825 }
12826
12827 return wordnr;
12828}
12829
12830/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012831 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012832 */
12833 static void
12834make_case_word(fword, cword, flags)
12835 char_u *fword;
12836 char_u *cword;
12837 int flags;
12838{
12839 if (flags & WF_ALLCAP)
12840 /* Make it all upper-case */
12841 allcap_copy(fword, cword);
12842 else if (flags & WF_ONECAP)
12843 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012844 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012845 else
12846 /* Use goodword as-is. */
12847 STRCPY(cword, fword);
12848}
12849
Bram Moolenaarea424162005-06-16 21:51:00 +000012850/*
12851 * Use map string "map" for languages "lp".
12852 */
12853 static void
12854set_map_str(lp, map)
12855 slang_T *lp;
12856 char_u *map;
12857{
12858 char_u *p;
12859 int headc = 0;
12860 int c;
12861 int i;
12862
12863 if (*map == NUL)
12864 {
12865 lp->sl_has_map = FALSE;
12866 return;
12867 }
12868 lp->sl_has_map = TRUE;
12869
Bram Moolenaar4770d092006-01-12 23:22:24 +000012870 /* Init the array and hash tables empty. */
Bram Moolenaarea424162005-06-16 21:51:00 +000012871 for (i = 0; i < 256; ++i)
12872 lp->sl_map_array[i] = 0;
12873#ifdef FEAT_MBYTE
12874 hash_init(&lp->sl_map_hash);
12875#endif
12876
12877 /*
12878 * The similar characters are stored separated with slashes:
12879 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
12880 * before the same slash. For characters above 255 sl_map_hash is used.
12881 */
12882 for (p = map; *p != NUL; )
12883 {
12884#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012885 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000012886#else
12887 c = *p++;
12888#endif
12889 if (c == '/')
12890 headc = 0;
12891 else
12892 {
12893 if (headc == 0)
12894 headc = c;
12895
12896#ifdef FEAT_MBYTE
12897 /* Characters above 255 don't fit in sl_map_array[], put them in
12898 * the hash table. Each entry is the char, a NUL the headchar and
12899 * a NUL. */
12900 if (c >= 256)
12901 {
12902 int cl = mb_char2len(c);
12903 int headcl = mb_char2len(headc);
12904 char_u *b;
12905 hash_T hash;
12906 hashitem_T *hi;
12907
12908 b = alloc((unsigned)(cl + headcl + 2));
12909 if (b == NULL)
12910 return;
12911 mb_char2bytes(c, b);
12912 b[cl] = NUL;
12913 mb_char2bytes(headc, b + cl + 1);
12914 b[cl + 1 + headcl] = NUL;
12915 hash = hash_hash(b);
12916 hi = hash_lookup(&lp->sl_map_hash, b, hash);
12917 if (HASHITEM_EMPTY(hi))
12918 hash_add_item(&lp->sl_map_hash, hi, b, hash);
12919 else
12920 {
12921 /* This should have been checked when generating the .spl
12922 * file. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000012923 EMSG(_("E783: duplicate char in MAP entry"));
Bram Moolenaarea424162005-06-16 21:51:00 +000012924 vim_free(b);
12925 }
12926 }
12927 else
12928#endif
12929 lp->sl_map_array[c] = headc;
12930 }
12931 }
12932}
12933
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012934/*
12935 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
12936 * lines in the .aff file.
12937 */
12938 static int
12939similar_chars(slang, c1, c2)
12940 slang_T *slang;
12941 int c1;
12942 int c2;
12943{
Bram Moolenaarea424162005-06-16 21:51:00 +000012944 int m1, m2;
12945#ifdef FEAT_MBYTE
12946 char_u buf[MB_MAXBYTES];
12947 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012948
Bram Moolenaarea424162005-06-16 21:51:00 +000012949 if (c1 >= 256)
12950 {
12951 buf[mb_char2bytes(c1, buf)] = 0;
12952 hi = hash_find(&slang->sl_map_hash, buf);
12953 if (HASHITEM_EMPTY(hi))
12954 m1 = 0;
12955 else
12956 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
12957 }
12958 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012959#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000012960 m1 = slang->sl_map_array[c1];
12961 if (m1 == 0)
12962 return FALSE;
12963
12964
12965#ifdef FEAT_MBYTE
12966 if (c2 >= 256)
12967 {
12968 buf[mb_char2bytes(c2, buf)] = 0;
12969 hi = hash_find(&slang->sl_map_hash, buf);
12970 if (HASHITEM_EMPTY(hi))
12971 m2 = 0;
12972 else
12973 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
12974 }
12975 else
12976#endif
12977 m2 = slang->sl_map_array[c2];
12978
12979 return m1 == m2;
12980}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012981
12982/*
12983 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +000012984 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012985 */
12986 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000012987add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus,
12988 slang, maxsf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012989 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012990 garray_T *gap; /* either su_ga or su_sga */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012991 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012992 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012993 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012994 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012995 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012996 slang_T *slang; /* language for sound folding */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012997 int maxsf; /* su_maxscore applies to soundfold score,
12998 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012999{
Bram Moolenaar4770d092006-01-12 23:22:24 +000013000 int goodlen; /* len of goodword changed */
13001 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013002 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013003 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013004 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013005 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013006
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013007 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13008 * "thee the" is added next to changing the first "the" the "thee". */
13009 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013010 pbad = su->su_badptr + badlenarg;
13011 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013012 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013013 goodlen = pgood - goodword;
13014 badlen = pbad - su->su_badptr;
13015 if (goodlen <= 0 || badlen <= 0)
13016 break;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013017 mb_ptr_back(goodword, pgood);
13018 mb_ptr_back(su->su_badptr, pbad);
13019#ifdef FEAT_MBYTE
13020 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013021 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013022 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
13023 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013024 }
13025 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013026#endif
13027 if (*pgood != *pbad)
13028 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013029 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013030
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013031 if (badlen == 0 && goodlen == 0)
13032 /* goodword doesn't change anything; may happen for "the the" changing
13033 * the first "the" to itself. */
13034 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013035
Bram Moolenaar4770d092006-01-12 23:22:24 +000013036 /* Check if the word is already there. Also check the length that is
13037 * being replaced "thes," -> "these" is a different suggestion from
13038 * "thes" -> "these". */
13039 stp = &SUG(*gap, 0);
13040 for (i = gap->ga_len; --i >= 0; ++stp)
13041 if (stp->st_wordlen == goodlen
13042 && stp->st_orglen == badlen
13043 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
13044 {
13045 /*
13046 * Found it. Remember the word with the lowest score.
13047 */
13048 if (stp->st_slang == NULL)
13049 stp->st_slang = slang;
13050
13051 new_sug.st_score = score;
13052 new_sug.st_altscore = altscore;
13053 new_sug.st_had_bonus = had_bonus;
13054
13055 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013056 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013057 /* Only one of the two had the soundalike score computed.
13058 * Need to do that for the other one now, otherwise the
13059 * scores can't be compared. This happens because
13060 * suggest_try_change() doesn't compute the soundalike
13061 * word to keep it fast, while some special methods set
13062 * the soundalike score to zero. */
13063 if (had_bonus)
13064 rescore_one(su, stp);
13065 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013066 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013067 new_sug.st_word = stp->st_word;
13068 new_sug.st_wordlen = stp->st_wordlen;
13069 new_sug.st_slang = stp->st_slang;
13070 new_sug.st_orglen = badlen;
13071 rescore_one(su, &new_sug);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013072 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013073 }
13074
Bram Moolenaar4770d092006-01-12 23:22:24 +000013075 if (stp->st_score > new_sug.st_score)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013076 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013077 stp->st_score = new_sug.st_score;
13078 stp->st_altscore = new_sug.st_altscore;
13079 stp->st_had_bonus = new_sug.st_had_bonus;
13080 }
13081 break;
13082 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013083
Bram Moolenaar4770d092006-01-12 23:22:24 +000013084 if (i < 0 && ga_grow(gap, 1) == OK)
13085 {
13086 /* Add a suggestion. */
13087 stp = &SUG(*gap, gap->ga_len);
13088 stp->st_word = vim_strnsave(goodword, goodlen);
13089 if (stp->st_word != NULL)
13090 {
13091 stp->st_wordlen = goodlen;
13092 stp->st_score = score;
13093 stp->st_altscore = altscore;
13094 stp->st_had_bonus = had_bonus;
13095 stp->st_orglen = badlen;
13096 stp->st_slang = slang;
13097 ++gap->ga_len;
13098
13099 /* If we have too many suggestions now, sort the list and keep
13100 * the best suggestions. */
13101 if (gap->ga_len > SUG_MAX_COUNT(su))
13102 {
13103 if (maxsf)
13104 su->su_sfmaxscore = cleanup_suggestions(gap,
13105 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
13106 else
13107 {
13108 i = su->su_maxscore;
13109 su->su_maxscore = cleanup_suggestions(gap,
13110 su->su_maxscore, SUG_CLEAN_COUNT(su));
13111 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013112 }
13113 }
13114 }
13115}
13116
13117/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013118 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13119 * for split words, such as "the the". Remove these from the list here.
13120 */
13121 static void
13122check_suggestions(su, gap)
13123 suginfo_T *su;
13124 garray_T *gap; /* either su_ga or su_sga */
13125{
13126 suggest_T *stp;
13127 int i;
13128 char_u longword[MAXWLEN + 1];
13129 int len;
13130 hlf_T attr;
13131
13132 stp = &SUG(*gap, 0);
13133 for (i = gap->ga_len - 1; i >= 0; --i)
13134 {
13135 /* Need to append what follows to check for "the the". */
13136 STRCPY(longword, stp[i].st_word);
13137 len = stp[i].st_wordlen;
13138 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
13139 MAXWLEN - len);
13140 attr = HLF_COUNT;
13141 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
13142 if (attr != HLF_COUNT)
13143 {
13144 /* Remove this entry. */
13145 vim_free(stp[i].st_word);
13146 --gap->ga_len;
13147 if (i < gap->ga_len)
13148 mch_memmove(stp + i, stp + i + 1,
13149 sizeof(suggest_T) * (gap->ga_len - i));
13150 }
13151 }
13152}
13153
13154
13155/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013156 * Add a word to be banned.
13157 */
13158 static void
13159add_banned(su, word)
13160 suginfo_T *su;
13161 char_u *word;
13162{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013163 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013164 hash_T hash;
13165 hashitem_T *hi;
13166
Bram Moolenaar4770d092006-01-12 23:22:24 +000013167 hash = hash_hash(word);
13168 hi = hash_lookup(&su->su_banned, word, hash);
13169 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013170 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013171 s = vim_strsave(word);
13172 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013173 hash_add_item(&su->su_banned, hi, s, hash);
13174 }
13175}
13176
13177/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013178 * Recompute the score for all suggestions if sound-folding is possible. This
13179 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013180 */
13181 static void
13182rescore_suggestions(su)
13183 suginfo_T *su;
13184{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013185 int i;
13186
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013187 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013188 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013189 rescore_one(su, &SUG(su->su_ga, i));
13190}
13191
13192/*
13193 * Recompute the score for one suggestion if sound-folding is possible.
13194 */
13195 static void
13196rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013197 suginfo_T *su;
13198 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013199{
13200 slang_T *slang = stp->st_slang;
13201 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000013202 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013203
13204 /* Only rescore suggestions that have no sal score yet and do have a
13205 * language. */
13206 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
13207 {
13208 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013209 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013210 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013211 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013212 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000013213 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013214 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000013215
13216 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013217 if (stp->st_altscore == SCORE_MAXMAX)
13218 stp->st_altscore = SCORE_BIG;
13219 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
13220 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013221 }
13222}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013223
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013224static int
13225#ifdef __BORLANDC__
13226_RTLENTRYF
13227#endif
13228sug_compare __ARGS((const void *s1, const void *s2));
13229
13230/*
13231 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013232 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013233 */
13234 static int
13235#ifdef __BORLANDC__
13236_RTLENTRYF
13237#endif
13238sug_compare(s1, s2)
13239 const void *s1;
13240 const void *s2;
13241{
13242 suggest_T *p1 = (suggest_T *)s1;
13243 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013244 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013245
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013246 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013247 {
13248 n = p1->st_altscore - p2->st_altscore;
13249 if (n == 0)
13250 n = STRICMP(p1->st_word, p2->st_word);
13251 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013252 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013253}
13254
13255/*
13256 * Cleanup the suggestions:
13257 * - Sort on score.
13258 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013259 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013260 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013261 static int
13262cleanup_suggestions(gap, maxscore, keep)
13263 garray_T *gap;
13264 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013265 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013266{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013267 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013268 int i;
13269
13270 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013271 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013272
13273 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013274 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013275 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013276 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013277 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013278 gap->ga_len = keep;
13279 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013280 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013281 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013282}
13283
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013284#if defined(FEAT_EVAL) || defined(PROTO)
13285/*
13286 * Soundfold a string, for soundfold().
13287 * Result is in allocated memory, NULL for an error.
13288 */
13289 char_u *
13290eval_soundfold(word)
13291 char_u *word;
13292{
13293 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013294 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013295 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013296
13297 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13298 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013299 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013300 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013301 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013302 if (lp->lp_slang->sl_sal.ga_len > 0)
13303 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013304 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013305 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013306 return vim_strsave(sound);
13307 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013308 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013309
13310 /* No language with sound folding, return word as-is. */
13311 return vim_strsave(word);
13312}
13313#endif
13314
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013315/*
13316 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000013317 *
13318 * There are many ways to turn a word into a sound-a-like representation. The
13319 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
13320 * swedish name matching - survey and test of different algorithms" by Klas
13321 * Erikson.
13322 *
13323 * We support two methods:
13324 * 1. SOFOFROM/SOFOTO do a simple character mapping.
13325 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013326 */
13327 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013328spell_soundfold(slang, inword, folded, res)
13329 slang_T *slang;
13330 char_u *inword;
13331 int folded; /* "inword" is already case-folded */
13332 char_u *res;
13333{
13334 char_u fword[MAXWLEN];
13335 char_u *word;
13336
13337 if (slang->sl_sofo)
13338 /* SOFOFROM and SOFOTO used */
13339 spell_soundfold_sofo(slang, inword, res);
13340 else
13341 {
13342 /* SAL items used. Requires the word to be case-folded. */
13343 if (folded)
13344 word = inword;
13345 else
13346 {
13347 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
13348 word = fword;
13349 }
13350
13351#ifdef FEAT_MBYTE
13352 if (has_mbyte)
13353 spell_soundfold_wsal(slang, word, res);
13354 else
13355#endif
13356 spell_soundfold_sal(slang, word, res);
13357 }
13358}
13359
13360/*
13361 * Perform sound folding of "inword" into "res" according to SOFOFROM and
13362 * SOFOTO lines.
13363 */
13364 static void
13365spell_soundfold_sofo(slang, inword, res)
13366 slang_T *slang;
13367 char_u *inword;
13368 char_u *res;
13369{
13370 char_u *s;
13371 int ri = 0;
13372 int c;
13373
13374#ifdef FEAT_MBYTE
13375 if (has_mbyte)
13376 {
13377 int prevc = 0;
13378 int *ip;
13379
13380 /* The sl_sal_first[] table contains the translation for chars up to
13381 * 255, sl_sal the rest. */
13382 for (s = inword; *s != NUL; )
13383 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013384 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013385 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
13386 c = ' ';
13387 else if (c < 256)
13388 c = slang->sl_sal_first[c];
13389 else
13390 {
13391 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
13392 if (ip == NULL) /* empty list, can't match */
13393 c = NUL;
13394 else
13395 for (;;) /* find "c" in the list */
13396 {
13397 if (*ip == 0) /* not found */
13398 {
13399 c = NUL;
13400 break;
13401 }
13402 if (*ip == c) /* match! */
13403 {
13404 c = ip[1];
13405 break;
13406 }
13407 ip += 2;
13408 }
13409 }
13410
13411 if (c != NUL && c != prevc)
13412 {
13413 ri += mb_char2bytes(c, res + ri);
13414 if (ri + MB_MAXBYTES > MAXWLEN)
13415 break;
13416 prevc = c;
13417 }
13418 }
13419 }
13420 else
13421#endif
13422 {
13423 /* The sl_sal_first[] table contains the translation. */
13424 for (s = inword; (c = *s) != NUL; ++s)
13425 {
13426 if (vim_iswhite(c))
13427 c = ' ';
13428 else
13429 c = slang->sl_sal_first[c];
13430 if (c != NUL && (ri == 0 || res[ri - 1] != c))
13431 res[ri++] = c;
13432 }
13433 }
13434
13435 res[ri] = NUL;
13436}
13437
13438 static void
13439spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013440 slang_T *slang;
13441 char_u *inword;
13442 char_u *res;
13443{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013444 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013445 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013446 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013447 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013448 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013449 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013450 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013451 int n, k = 0;
13452 int z0;
13453 int k0;
13454 int n0;
13455 int c;
13456 int pri;
13457 int p0 = -333;
13458 int c0;
13459
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013460 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013461 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013462 if (slang->sl_rem_accents)
13463 {
13464 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013465 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013466 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013467 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013468 {
13469 *t++ = ' ';
13470 s = skipwhite(s);
13471 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013472 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013473 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013474 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013475 *t++ = *s;
13476 ++s;
13477 }
13478 }
13479 *t = NUL;
13480 }
13481 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013482 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013483
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013484 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013485
13486 /*
13487 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013488 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013489 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013490 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013491 while ((c = word[i]) != NUL)
13492 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013493 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013494 n = slang->sl_sal_first[c];
13495 z0 = 0;
13496
13497 if (n >= 0)
13498 {
13499 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013500 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013501 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013502 /* Quickly skip entries that don't match the word. Most
13503 * entries are less then three chars, optimize for that. */
13504 k = smp[n].sm_leadlen;
13505 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013506 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013507 if (word[i + 1] != s[1])
13508 continue;
13509 if (k > 2)
13510 {
13511 for (j = 2; j < k; ++j)
13512 if (word[i + j] != s[j])
13513 break;
13514 if (j < k)
13515 continue;
13516 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013517 }
13518
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013519 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013520 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013521 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013522 while (*pf != NUL && *pf != word[i + k])
13523 ++pf;
13524 if (*pf == NUL)
13525 continue;
13526 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013527 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013528 s = smp[n].sm_rules;
13529 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013530
13531 p0 = *s;
13532 k0 = k;
13533 while (*s == '-' && k > 1)
13534 {
13535 k--;
13536 s++;
13537 }
13538 if (*s == '<')
13539 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013540 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013541 {
13542 /* determine priority */
13543 pri = *s - '0';
13544 s++;
13545 }
13546 if (*s == '^' && *(s + 1) == '^')
13547 s++;
13548
13549 if (*s == NUL
13550 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013551 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013552 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013553 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013554 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013555 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013556 && spell_iswordp(word + i - 1, curbuf)
13557 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013558 {
13559 /* search for followup rules, if: */
13560 /* followup and k > 1 and NO '-' in searchstring */
13561 c0 = word[i + k - 1];
13562 n0 = slang->sl_sal_first[c0];
13563
13564 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013565 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013566 {
13567 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013568 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013569 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013570 /* Quickly skip entries that don't match the word.
13571 * */
13572 k0 = smp[n0].sm_leadlen;
13573 if (k0 > 1)
13574 {
13575 if (word[i + k] != s[1])
13576 continue;
13577 if (k0 > 2)
13578 {
13579 pf = word + i + k + 1;
13580 for (j = 2; j < k0; ++j)
13581 if (*pf++ != s[j])
13582 break;
13583 if (j < k0)
13584 continue;
13585 }
13586 }
13587 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013588
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013589 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013590 {
13591 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013592 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013593 while (*pf != NUL && *pf != word[i + k0])
13594 ++pf;
13595 if (*pf == NUL)
13596 continue;
13597 ++k0;
13598 }
13599
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013600 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013601 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013602 while (*s == '-')
13603 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013604 /* "k0" gets NOT reduced because
13605 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013606 s++;
13607 }
13608 if (*s == '<')
13609 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013610 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013611 {
13612 p0 = *s - '0';
13613 s++;
13614 }
13615
13616 if (*s == NUL
13617 /* *s == '^' cuts */
13618 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013619 && !spell_iswordp(word + i + k0,
13620 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013621 {
13622 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013623 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013624 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013625
13626 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013627 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013628 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013629 /* rule fits; stop search */
13630 break;
13631 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013632 }
13633
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013634 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013635 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013636 }
13637
13638 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013639 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000013640 if (s == NULL)
13641 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013642 pf = smp[n].sm_rules;
13643 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013644 if (p0 == 1 && z == 0)
13645 {
13646 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013647 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
13648 || res[reslen - 1] == *s))
13649 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013650 z0 = 1;
13651 z = 1;
13652 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013653 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013654 {
13655 word[i + k0] = *s;
13656 k0++;
13657 s++;
13658 }
13659 if (k > k0)
13660 mch_memmove(word + i + k0, word + i + k,
13661 STRLEN(word + i + k) + 1);
13662
13663 /* new "actual letter" */
13664 c = word[i];
13665 }
13666 else
13667 {
13668 /* no '<' rule used */
13669 i += k - 1;
13670 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013671 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013672 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013673 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013674 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013675 s++;
13676 }
13677 /* new "actual letter" */
13678 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013679 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013680 {
13681 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013682 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013683 mch_memmove(word, word + i + 1,
13684 STRLEN(word + i + 1) + 1);
13685 i = 0;
13686 z0 = 1;
13687 }
13688 }
13689 break;
13690 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013691 }
13692 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013693 else if (vim_iswhite(c))
13694 {
13695 c = ' ';
13696 k = 1;
13697 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013698
13699 if (z0 == 0)
13700 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013701 if (k && !p0 && reslen < MAXWLEN && c != NUL
13702 && (!slang->sl_collapse || reslen == 0
13703 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013704 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013705 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013706
13707 i++;
13708 z = 0;
13709 k = 0;
13710 }
13711 }
13712
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013713 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013714}
13715
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013716#ifdef FEAT_MBYTE
13717/*
13718 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
13719 * Multi-byte version of spell_soundfold().
13720 */
13721 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013722spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013723 slang_T *slang;
13724 char_u *inword;
13725 char_u *res;
13726{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013727 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013728 int word[MAXWLEN];
13729 int wres[MAXWLEN];
13730 int l;
13731 char_u *s;
13732 int *ws;
13733 char_u *t;
13734 int *pf;
13735 int i, j, z;
13736 int reslen;
13737 int n, k = 0;
13738 int z0;
13739 int k0;
13740 int n0;
13741 int c;
13742 int pri;
13743 int p0 = -333;
13744 int c0;
13745 int did_white = FALSE;
13746
13747 /*
13748 * Convert the multi-byte string to a wide-character string.
13749 * Remove accents, if wanted. We actually remove all non-word characters.
13750 * But keep white space.
13751 */
13752 n = 0;
13753 for (s = inword; *s != NUL; )
13754 {
13755 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013756 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013757 if (slang->sl_rem_accents)
13758 {
13759 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
13760 {
13761 if (did_white)
13762 continue;
13763 c = ' ';
13764 did_white = TRUE;
13765 }
13766 else
13767 {
13768 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013769 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013770 continue;
13771 }
13772 }
13773 word[n++] = c;
13774 }
13775 word[n] = NUL;
13776
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013777 /*
13778 * This comes from Aspell phonet.cpp.
13779 * Converted from C++ to C. Added support for multi-byte chars.
13780 * Changed to keep spaces.
13781 */
13782 i = reslen = z = 0;
13783 while ((c = word[i]) != NUL)
13784 {
13785 /* Start with the first rule that has the character in the word. */
13786 n = slang->sl_sal_first[c & 0xff];
13787 z0 = 0;
13788
13789 if (n >= 0)
13790 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013791 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013792 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
13793 {
13794 /* Quickly skip entries that don't match the word. Most
13795 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013796 if (c != ws[0])
13797 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013798 k = smp[n].sm_leadlen;
13799 if (k > 1)
13800 {
13801 if (word[i + 1] != ws[1])
13802 continue;
13803 if (k > 2)
13804 {
13805 for (j = 2; j < k; ++j)
13806 if (word[i + j] != ws[j])
13807 break;
13808 if (j < k)
13809 continue;
13810 }
13811 }
13812
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013813 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013814 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013815 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013816 while (*pf != NUL && *pf != word[i + k])
13817 ++pf;
13818 if (*pf == NUL)
13819 continue;
13820 ++k;
13821 }
13822 s = smp[n].sm_rules;
13823 pri = 5; /* default priority */
13824
13825 p0 = *s;
13826 k0 = k;
13827 while (*s == '-' && k > 1)
13828 {
13829 k--;
13830 s++;
13831 }
13832 if (*s == '<')
13833 s++;
13834 if (VIM_ISDIGIT(*s))
13835 {
13836 /* determine priority */
13837 pri = *s - '0';
13838 s++;
13839 }
13840 if (*s == '^' && *(s + 1) == '^')
13841 s++;
13842
13843 if (*s == NUL
13844 || (*s == '^'
13845 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013846 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013847 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013848 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013849 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013850 && spell_iswordp_w(word + i - 1, curbuf)
13851 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013852 {
13853 /* search for followup rules, if: */
13854 /* followup and k > 1 and NO '-' in searchstring */
13855 c0 = word[i + k - 1];
13856 n0 = slang->sl_sal_first[c0 & 0xff];
13857
13858 if (slang->sl_followup && k > 1 && n0 >= 0
13859 && p0 != '-' && word[i + k] != NUL)
13860 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013861 /* Test follow-up rule for "word[i + k]"; loop over
13862 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013863 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
13864 == (c0 & 0xff); ++n0)
13865 {
13866 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013867 */
13868 if (c0 != ws[0])
13869 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013870 k0 = smp[n0].sm_leadlen;
13871 if (k0 > 1)
13872 {
13873 if (word[i + k] != ws[1])
13874 continue;
13875 if (k0 > 2)
13876 {
13877 pf = word + i + k + 1;
13878 for (j = 2; j < k0; ++j)
13879 if (*pf++ != ws[j])
13880 break;
13881 if (j < k0)
13882 continue;
13883 }
13884 }
13885 k0 += k - 1;
13886
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013887 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013888 {
13889 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013890 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013891 while (*pf != NUL && *pf != word[i + k0])
13892 ++pf;
13893 if (*pf == NUL)
13894 continue;
13895 ++k0;
13896 }
13897
13898 p0 = 5;
13899 s = smp[n0].sm_rules;
13900 while (*s == '-')
13901 {
13902 /* "k0" gets NOT reduced because
13903 * "if (k0 == k)" */
13904 s++;
13905 }
13906 if (*s == '<')
13907 s++;
13908 if (VIM_ISDIGIT(*s))
13909 {
13910 p0 = *s - '0';
13911 s++;
13912 }
13913
13914 if (*s == NUL
13915 /* *s == '^' cuts */
13916 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013917 && !spell_iswordp_w(word + i + k0,
13918 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013919 {
13920 if (k0 == k)
13921 /* this is just a piece of the string */
13922 continue;
13923
13924 if (p0 < pri)
13925 /* priority too low */
13926 continue;
13927 /* rule fits; stop search */
13928 break;
13929 }
13930 }
13931
13932 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
13933 == (c0 & 0xff))
13934 continue;
13935 }
13936
13937 /* replace string */
13938 ws = smp[n].sm_to_w;
13939 s = smp[n].sm_rules;
13940 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
13941 if (p0 == 1 && z == 0)
13942 {
13943 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000013944 if (reslen > 0 && ws != NULL && *ws != NUL
13945 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013946 || wres[reslen - 1] == *ws))
13947 reslen--;
13948 z0 = 1;
13949 z = 1;
13950 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000013951 if (ws != NULL)
13952 while (*ws != NUL && word[i + k0] != NUL)
13953 {
13954 word[i + k0] = *ws;
13955 k0++;
13956 ws++;
13957 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013958 if (k > k0)
13959 mch_memmove(word + i + k0, word + i + k,
13960 sizeof(int) * (STRLEN(word + i + k) + 1));
13961
13962 /* new "actual letter" */
13963 c = word[i];
13964 }
13965 else
13966 {
13967 /* no '<' rule used */
13968 i += k - 1;
13969 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000013970 if (ws != NULL)
13971 while (*ws != NUL && ws[1] != NUL
13972 && reslen < MAXWLEN)
13973 {
13974 if (reslen == 0 || wres[reslen - 1] != *ws)
13975 wres[reslen++] = *ws;
13976 ws++;
13977 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013978 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000013979 if (ws == NULL)
13980 c = NUL;
13981 else
13982 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013983 if (strstr((char *)s, "^^") != NULL)
13984 {
13985 if (c != NUL)
13986 wres[reslen++] = c;
13987 mch_memmove(word, word + i + 1,
13988 sizeof(int) * (STRLEN(word + i + 1) + 1));
13989 i = 0;
13990 z0 = 1;
13991 }
13992 }
13993 break;
13994 }
13995 }
13996 }
13997 else if (vim_iswhite(c))
13998 {
13999 c = ' ';
14000 k = 1;
14001 }
14002
14003 if (z0 == 0)
14004 {
14005 if (k && !p0 && reslen < MAXWLEN && c != NUL
14006 && (!slang->sl_collapse || reslen == 0
14007 || wres[reslen - 1] != c))
14008 /* condense only double letters */
14009 wres[reslen++] = c;
14010
14011 i++;
14012 z = 0;
14013 k = 0;
14014 }
14015 }
14016
14017 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14018 l = 0;
14019 for (n = 0; n < reslen; ++n)
14020 {
14021 l += mb_char2bytes(wres[n], res + l);
14022 if (l + MB_MAXBYTES > MAXWLEN)
14023 break;
14024 }
14025 res[l] = NUL;
14026}
14027#endif
14028
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014029/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014030 * Compute a score for two sound-a-like words.
14031 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14032 * Instead of a generic loop we write out the code. That keeps it fast by
14033 * avoiding checks that will not be possible.
14034 */
14035 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014036soundalike_score(goodstart, badstart)
14037 char_u *goodstart; /* sound-folded good word */
14038 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014039{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014040 char_u *goodsound = goodstart;
14041 char_u *badsound = badstart;
14042 int goodlen;
14043 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014044 int n;
14045 char_u *pl, *ps;
14046 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014047 int score = 0;
14048
14049 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
14050 * counted so much, vowels halfway the word aren't counted at all. */
14051 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
14052 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014053 if (badsound[1] == goodsound[1]
14054 || (badsound[1] != NUL
14055 && goodsound[1] != NUL
14056 && badsound[2] == goodsound[2]))
14057 {
14058 /* handle like a substitute */
14059 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014060 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014061 {
14062 score = 2 * SCORE_DEL / 3;
14063 if (*badsound == '*')
14064 ++badsound;
14065 else
14066 ++goodsound;
14067 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014068 }
14069
14070 goodlen = STRLEN(goodsound);
14071 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014072
14073 /* Return quickly if the lenghts are too different to be fixed by two
14074 * changes. */
14075 n = goodlen - badlen;
14076 if (n < -2 || n > 2)
14077 return SCORE_MAXMAX;
14078
14079 if (n > 0)
14080 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014081 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014082 ps = badsound;
14083 }
14084 else
14085 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014086 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014087 ps = goodsound;
14088 }
14089
14090 /* Skip over the identical part. */
14091 while (*pl == *ps && *pl != NUL)
14092 {
14093 ++pl;
14094 ++ps;
14095 }
14096
14097 switch (n)
14098 {
14099 case -2:
14100 case 2:
14101 /*
14102 * Must delete two characters from "pl".
14103 */
14104 ++pl; /* first delete */
14105 while (*pl == *ps)
14106 {
14107 ++pl;
14108 ++ps;
14109 }
14110 /* strings must be equal after second delete */
14111 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014112 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014113
14114 /* Failed to compare. */
14115 break;
14116
14117 case -1:
14118 case 1:
14119 /*
14120 * Minimal one delete from "pl" required.
14121 */
14122
14123 /* 1: delete */
14124 pl2 = pl + 1;
14125 ps2 = ps;
14126 while (*pl2 == *ps2)
14127 {
14128 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014129 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014130 ++pl2;
14131 ++ps2;
14132 }
14133
14134 /* 2: delete then swap, then rest must be equal */
14135 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14136 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014137 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014138
14139 /* 3: delete then substitute, then the rest must be equal */
14140 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014141 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014142
14143 /* 4: first swap then delete */
14144 if (pl[0] == ps[1] && pl[1] == ps[0])
14145 {
14146 pl2 = pl + 2; /* swap, skip two chars */
14147 ps2 = ps + 2;
14148 while (*pl2 == *ps2)
14149 {
14150 ++pl2;
14151 ++ps2;
14152 }
14153 /* delete a char and then strings must be equal */
14154 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014155 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014156 }
14157
14158 /* 5: first substitute then delete */
14159 pl2 = pl + 1; /* substitute, skip one char */
14160 ps2 = ps + 1;
14161 while (*pl2 == *ps2)
14162 {
14163 ++pl2;
14164 ++ps2;
14165 }
14166 /* delete a char and then strings must be equal */
14167 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014168 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014169
14170 /* Failed to compare. */
14171 break;
14172
14173 case 0:
14174 /*
14175 * Lenghts are equal, thus changes must result in same length: An
14176 * insert is only possible in combination with a delete.
14177 * 1: check if for identical strings
14178 */
14179 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014180 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014181
14182 /* 2: swap */
14183 if (pl[0] == ps[1] && pl[1] == ps[0])
14184 {
14185 pl2 = pl + 2; /* swap, skip two chars */
14186 ps2 = ps + 2;
14187 while (*pl2 == *ps2)
14188 {
14189 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014190 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014191 ++pl2;
14192 ++ps2;
14193 }
14194 /* 3: swap and swap again */
14195 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14196 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014197 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014198
14199 /* 4: swap and substitute */
14200 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014201 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014202 }
14203
14204 /* 5: substitute */
14205 pl2 = pl + 1;
14206 ps2 = ps + 1;
14207 while (*pl2 == *ps2)
14208 {
14209 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014210 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014211 ++pl2;
14212 ++ps2;
14213 }
14214
14215 /* 6: substitute and swap */
14216 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14217 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014218 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014219
14220 /* 7: substitute and substitute */
14221 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014222 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014223
14224 /* 8: insert then delete */
14225 pl2 = pl;
14226 ps2 = ps + 1;
14227 while (*pl2 == *ps2)
14228 {
14229 ++pl2;
14230 ++ps2;
14231 }
14232 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014233 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014234
14235 /* 9: delete then insert */
14236 pl2 = pl + 1;
14237 ps2 = ps;
14238 while (*pl2 == *ps2)
14239 {
14240 ++pl2;
14241 ++ps2;
14242 }
14243 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014244 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014245
14246 /* Failed to compare. */
14247 break;
14248 }
14249
14250 return SCORE_MAXMAX;
14251}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014252
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014253/*
14254 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014255 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014256 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000014257 * The algorithm is described by Du and Chang, 1992.
14258 * The implementation of the algorithm comes from Aspell editdist.cpp,
14259 * edit_distance(). It has been converted from C++ to C and modified to
14260 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014261 */
14262 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +000014263spell_edit_score(slang, badword, goodword)
14264 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014265 char_u *badword;
14266 char_u *goodword;
14267{
14268 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014269 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014270 int j, i;
14271 int t;
14272 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014273 int pbc, pgc;
14274#ifdef FEAT_MBYTE
14275 char_u *p;
14276 int wbadword[MAXWLEN];
14277 int wgoodword[MAXWLEN];
14278
14279 if (has_mbyte)
14280 {
14281 /* Get the characters from the multi-byte strings and put them in an
14282 * int array for easy access. */
14283 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014284 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014285 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014286 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014287 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014288 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014289 }
14290 else
14291#endif
14292 {
14293 badlen = STRLEN(badword) + 1;
14294 goodlen = STRLEN(goodword) + 1;
14295 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014296
14297 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
14298#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014299 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
14300 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014301 if (cnt == NULL)
14302 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014303
14304 CNT(0, 0) = 0;
14305 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +000014306 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014307
14308 for (i = 1; i <= badlen; ++i)
14309 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014310 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014311 for (j = 1; j <= goodlen; ++j)
14312 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014313#ifdef FEAT_MBYTE
14314 if (has_mbyte)
14315 {
14316 bc = wbadword[i - 1];
14317 gc = wgoodword[j - 1];
14318 }
14319 else
14320#endif
14321 {
14322 bc = badword[i - 1];
14323 gc = goodword[j - 1];
14324 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014325 if (bc == gc)
14326 CNT(i, j) = CNT(i - 1, j - 1);
14327 else
14328 {
14329 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014330 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014331 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
14332 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014333 {
14334 /* For a similar character use SCORE_SIMILAR. */
14335 if (slang != NULL
14336 && slang->sl_has_map
14337 && similar_chars(slang, gc, bc))
14338 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
14339 else
14340 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
14341 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014342
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014343 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014344 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014345#ifdef FEAT_MBYTE
14346 if (has_mbyte)
14347 {
14348 pbc = wbadword[i - 2];
14349 pgc = wgoodword[j - 2];
14350 }
14351 else
14352#endif
14353 {
14354 pbc = badword[i - 2];
14355 pgc = goodword[j - 2];
14356 }
14357 if (bc == pgc && pbc == gc)
14358 {
14359 t = SCORE_SWAP + CNT(i - 2, j - 2);
14360 if (t < CNT(i, j))
14361 CNT(i, j) = t;
14362 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014363 }
14364 t = SCORE_DEL + CNT(i - 1, j);
14365 if (t < CNT(i, j))
14366 CNT(i, j) = t;
14367 t = SCORE_INS + CNT(i, j - 1);
14368 if (t < CNT(i, j))
14369 CNT(i, j) = t;
14370 }
14371 }
14372 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014373
14374 i = CNT(badlen - 1, goodlen - 1);
14375 vim_free(cnt);
14376 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014377}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000014378
Bram Moolenaar4770d092006-01-12 23:22:24 +000014379typedef struct
14380{
14381 int badi;
14382 int goodi;
14383 int score;
14384} limitscore_T;
14385
14386/*
14387 * Like spell_edit_score(), but with a limit on the score to make it faster.
14388 * May return SCORE_MAXMAX when the score is higher than "limit".
14389 *
14390 * This uses a stack for the edits still to be tried.
14391 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
14392 * for multi-byte characters.
14393 */
14394 static int
14395spell_edit_score_limit(slang, badword, goodword, limit)
14396 slang_T *slang;
14397 char_u *badword;
14398 char_u *goodword;
14399 int limit;
14400{
14401 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
14402 int stackidx;
14403 int bi, gi;
14404 int bi2, gi2;
14405 int bc, gc;
14406 int score;
14407 int score_off;
14408 int minscore;
14409 int round;
14410
14411#ifdef FEAT_MBYTE
14412 /* Multi-byte characters require a bit more work, use a different function
14413 * to avoid testing "has_mbyte" quite often. */
14414 if (has_mbyte)
14415 return spell_edit_score_limit_w(slang, badword, goodword, limit);
14416#endif
14417
14418 /*
14419 * The idea is to go from start to end over the words. So long as
14420 * characters are equal just continue, this always gives the lowest score.
14421 * When there is a difference try several alternatives. Each alternative
14422 * increases "score" for the edit distance. Some of the alternatives are
14423 * pushed unto a stack and tried later, some are tried right away. At the
14424 * end of the word the score for one alternative is known. The lowest
14425 * possible score is stored in "minscore".
14426 */
14427 stackidx = 0;
14428 bi = 0;
14429 gi = 0;
14430 score = 0;
14431 minscore = limit + 1;
14432
14433 for (;;)
14434 {
14435 /* Skip over an equal part, score remains the same. */
14436 for (;;)
14437 {
14438 bc = badword[bi];
14439 gc = goodword[gi];
14440 if (bc != gc) /* stop at a char that's different */
14441 break;
14442 if (bc == NUL) /* both words end */
14443 {
14444 if (score < minscore)
14445 minscore = score;
14446 goto pop; /* do next alternative */
14447 }
14448 ++bi;
14449 ++gi;
14450 }
14451
14452 if (gc == NUL) /* goodword ends, delete badword chars */
14453 {
14454 do
14455 {
14456 if ((score += SCORE_DEL) >= minscore)
14457 goto pop; /* do next alternative */
14458 } while (badword[++bi] != NUL);
14459 minscore = score;
14460 }
14461 else if (bc == NUL) /* badword ends, insert badword chars */
14462 {
14463 do
14464 {
14465 if ((score += SCORE_INS) >= minscore)
14466 goto pop; /* do next alternative */
14467 } while (goodword[++gi] != NUL);
14468 minscore = score;
14469 }
14470 else /* both words continue */
14471 {
14472 /* If not close to the limit, perform a change. Only try changes
14473 * that may lead to a lower score than "minscore".
14474 * round 0: try deleting a char from badword
14475 * round 1: try inserting a char in badword */
14476 for (round = 0; round <= 1; ++round)
14477 {
14478 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
14479 if (score_off < minscore)
14480 {
14481 if (score_off + SCORE_EDIT_MIN >= minscore)
14482 {
14483 /* Near the limit, rest of the words must match. We
14484 * can check that right now, no need to push an item
14485 * onto the stack. */
14486 bi2 = bi + 1 - round;
14487 gi2 = gi + round;
14488 while (goodword[gi2] == badword[bi2])
14489 {
14490 if (goodword[gi2] == NUL)
14491 {
14492 minscore = score_off;
14493 break;
14494 }
14495 ++bi2;
14496 ++gi2;
14497 }
14498 }
14499 else
14500 {
14501 /* try deleting/inserting a character later */
14502 stack[stackidx].badi = bi + 1 - round;
14503 stack[stackidx].goodi = gi + round;
14504 stack[stackidx].score = score_off;
14505 ++stackidx;
14506 }
14507 }
14508 }
14509
14510 if (score + SCORE_SWAP < minscore)
14511 {
14512 /* If swapping two characters makes a match then the
14513 * substitution is more expensive, thus there is no need to
14514 * try both. */
14515 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
14516 {
14517 /* Swap two characters, that is: skip them. */
14518 gi += 2;
14519 bi += 2;
14520 score += SCORE_SWAP;
14521 continue;
14522 }
14523 }
14524
14525 /* Substitute one character for another which is the same
14526 * thing as deleting a character from both goodword and badword.
14527 * Use a better score when there is only a case difference. */
14528 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
14529 score += SCORE_ICASE;
14530 else
14531 {
14532 /* For a similar character use SCORE_SIMILAR. */
14533 if (slang != NULL
14534 && slang->sl_has_map
14535 && similar_chars(slang, gc, bc))
14536 score += SCORE_SIMILAR;
14537 else
14538 score += SCORE_SUBST;
14539 }
14540
14541 if (score < minscore)
14542 {
14543 /* Do the substitution. */
14544 ++gi;
14545 ++bi;
14546 continue;
14547 }
14548 }
14549pop:
14550 /*
14551 * Get here to try the next alternative, pop it from the stack.
14552 */
14553 if (stackidx == 0) /* stack is empty, finished */
14554 break;
14555
14556 /* pop an item from the stack */
14557 --stackidx;
14558 gi = stack[stackidx].goodi;
14559 bi = stack[stackidx].badi;
14560 score = stack[stackidx].score;
14561 }
14562
14563 /* When the score goes over "limit" it may actually be much higher.
14564 * Return a very large number to avoid going below the limit when giving a
14565 * bonus. */
14566 if (minscore > limit)
14567 return SCORE_MAXMAX;
14568 return minscore;
14569}
14570
14571#ifdef FEAT_MBYTE
14572/*
14573 * Multi-byte version of spell_edit_score_limit().
14574 * Keep it in sync with the above!
14575 */
14576 static int
14577spell_edit_score_limit_w(slang, badword, goodword, limit)
14578 slang_T *slang;
14579 char_u *badword;
14580 char_u *goodword;
14581 int limit;
14582{
14583 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
14584 int stackidx;
14585 int bi, gi;
14586 int bi2, gi2;
14587 int bc, gc;
14588 int score;
14589 int score_off;
14590 int minscore;
14591 int round;
14592 char_u *p;
14593 int wbadword[MAXWLEN];
14594 int wgoodword[MAXWLEN];
14595
14596 /* Get the characters from the multi-byte strings and put them in an
14597 * int array for easy access. */
14598 bi = 0;
14599 for (p = badword; *p != NUL; )
14600 wbadword[bi++] = mb_cptr2char_adv(&p);
14601 wbadword[bi++] = 0;
14602 gi = 0;
14603 for (p = goodword; *p != NUL; )
14604 wgoodword[gi++] = mb_cptr2char_adv(&p);
14605 wgoodword[gi++] = 0;
14606
14607 /*
14608 * The idea is to go from start to end over the words. So long as
14609 * characters are equal just continue, this always gives the lowest score.
14610 * When there is a difference try several alternatives. Each alternative
14611 * increases "score" for the edit distance. Some of the alternatives are
14612 * pushed unto a stack and tried later, some are tried right away. At the
14613 * end of the word the score for one alternative is known. The lowest
14614 * possible score is stored in "minscore".
14615 */
14616 stackidx = 0;
14617 bi = 0;
14618 gi = 0;
14619 score = 0;
14620 minscore = limit + 1;
14621
14622 for (;;)
14623 {
14624 /* Skip over an equal part, score remains the same. */
14625 for (;;)
14626 {
14627 bc = wbadword[bi];
14628 gc = wgoodword[gi];
14629
14630 if (bc != gc) /* stop at a char that's different */
14631 break;
14632 if (bc == NUL) /* both words end */
14633 {
14634 if (score < minscore)
14635 minscore = score;
14636 goto pop; /* do next alternative */
14637 }
14638 ++bi;
14639 ++gi;
14640 }
14641
14642 if (gc == NUL) /* goodword ends, delete badword chars */
14643 {
14644 do
14645 {
14646 if ((score += SCORE_DEL) >= minscore)
14647 goto pop; /* do next alternative */
14648 } while (wbadword[++bi] != NUL);
14649 minscore = score;
14650 }
14651 else if (bc == NUL) /* badword ends, insert badword chars */
14652 {
14653 do
14654 {
14655 if ((score += SCORE_INS) >= minscore)
14656 goto pop; /* do next alternative */
14657 } while (wgoodword[++gi] != NUL);
14658 minscore = score;
14659 }
14660 else /* both words continue */
14661 {
14662 /* If not close to the limit, perform a change. Only try changes
14663 * that may lead to a lower score than "minscore".
14664 * round 0: try deleting a char from badword
14665 * round 1: try inserting a char in badword */
14666 for (round = 0; round <= 1; ++round)
14667 {
14668 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
14669 if (score_off < minscore)
14670 {
14671 if (score_off + SCORE_EDIT_MIN >= minscore)
14672 {
14673 /* Near the limit, rest of the words must match. We
14674 * can check that right now, no need to push an item
14675 * onto the stack. */
14676 bi2 = bi + 1 - round;
14677 gi2 = gi + round;
14678 while (wgoodword[gi2] == wbadword[bi2])
14679 {
14680 if (wgoodword[gi2] == NUL)
14681 {
14682 minscore = score_off;
14683 break;
14684 }
14685 ++bi2;
14686 ++gi2;
14687 }
14688 }
14689 else
14690 {
14691 /* try deleting a character from badword later */
14692 stack[stackidx].badi = bi + 1 - round;
14693 stack[stackidx].goodi = gi + round;
14694 stack[stackidx].score = score_off;
14695 ++stackidx;
14696 }
14697 }
14698 }
14699
14700 if (score + SCORE_SWAP < minscore)
14701 {
14702 /* If swapping two characters makes a match then the
14703 * substitution is more expensive, thus there is no need to
14704 * try both. */
14705 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
14706 {
14707 /* Swap two characters, that is: skip them. */
14708 gi += 2;
14709 bi += 2;
14710 score += SCORE_SWAP;
14711 continue;
14712 }
14713 }
14714
14715 /* Substitute one character for another which is the same
14716 * thing as deleting a character from both goodword and badword.
14717 * Use a better score when there is only a case difference. */
14718 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
14719 score += SCORE_ICASE;
14720 else
14721 {
14722 /* For a similar character use SCORE_SIMILAR. */
14723 if (slang != NULL
14724 && slang->sl_has_map
14725 && similar_chars(slang, gc, bc))
14726 score += SCORE_SIMILAR;
14727 else
14728 score += SCORE_SUBST;
14729 }
14730
14731 if (score < minscore)
14732 {
14733 /* Do the substitution. */
14734 ++gi;
14735 ++bi;
14736 continue;
14737 }
14738 }
14739pop:
14740 /*
14741 * Get here to try the next alternative, pop it from the stack.
14742 */
14743 if (stackidx == 0) /* stack is empty, finished */
14744 break;
14745
14746 /* pop an item from the stack */
14747 --stackidx;
14748 gi = stack[stackidx].goodi;
14749 bi = stack[stackidx].badi;
14750 score = stack[stackidx].score;
14751 }
14752
14753 /* When the score goes over "limit" it may actually be much higher.
14754 * Return a very large number to avoid going below the limit when giving a
14755 * bonus. */
14756 if (minscore > limit)
14757 return SCORE_MAXMAX;
14758 return minscore;
14759}
14760#endif
14761
14762#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
14763#define DUMPFLAG_COUNT 2 /* include word count */
14764
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014765/*
14766 * ":spelldump"
14767 */
14768/*ARGSUSED*/
14769 void
14770ex_spelldump(eap)
14771 exarg_T *eap;
14772{
14773 buf_T *buf = curbuf;
14774 langp_T *lp;
14775 slang_T *slang;
14776 idx_T arridx[MAXWLEN];
14777 int curi[MAXWLEN];
14778 char_u word[MAXWLEN];
14779 int c;
14780 char_u *byts;
14781 idx_T *idxs;
14782 linenr_T lnum = 0;
14783 int round;
14784 int depth;
14785 int n;
14786 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000014787 char_u *region_names = NULL; /* region names being used */
14788 int do_region = TRUE; /* dump region names and numbers */
14789 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014790 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000014791 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014792
Bram Moolenaar95529562005-08-25 21:21:38 +000014793 if (no_spell_checking(curwin))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014794 return;
14795
14796 /* Create a new empty buffer by splitting the window. */
14797 do_cmdline_cmd((char_u *)"new");
14798 if (!bufempty() || !buf_valid(buf))
14799 return;
14800
Bram Moolenaar7887d882005-07-01 22:33:52 +000014801 /* Find out if we can support regions: All languages must support the same
14802 * regions or none at all. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014803 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000014804 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014805 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000014806 p = lp->lp_slang->sl_regions;
14807 if (p[0] != 0)
14808 {
14809 if (region_names == NULL) /* first language with regions */
14810 region_names = p;
14811 else if (STRCMP(region_names, p) != 0)
14812 {
14813 do_region = FALSE; /* region names are different */
14814 break;
14815 }
14816 }
14817 }
14818
14819 if (do_region && region_names != NULL)
14820 {
14821 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
14822 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
14823 }
14824 else
14825 do_region = FALSE;
14826
14827 /*
14828 * Loop over all files loaded for the entries in 'spelllang'.
14829 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014830 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014831 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014832 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014833 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014834 if (slang->sl_fbyts == NULL) /* reloading failed */
14835 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014836
14837 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
14838 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
14839
14840 /* round 1: case-folded tree
14841 * round 2: keep-case tree */
14842 for (round = 1; round <= 2; ++round)
14843 {
14844 if (round == 1)
14845 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014846 dumpflags = 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014847 byts = slang->sl_fbyts;
14848 idxs = slang->sl_fidxs;
14849 }
14850 else
14851 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014852 dumpflags = DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014853 byts = slang->sl_kbyts;
14854 idxs = slang->sl_kidxs;
14855 }
14856 if (byts == NULL)
14857 continue; /* array is empty */
14858
Bram Moolenaar4770d092006-01-12 23:22:24 +000014859 if (eap->forceit)
14860 dumpflags |= DUMPFLAG_COUNT;
14861
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014862 depth = 0;
14863 arridx[0] = 0;
14864 curi[0] = 1;
14865 while (depth >= 0 && !got_int)
14866 {
14867 if (curi[depth] > byts[arridx[depth]])
14868 {
14869 /* Done all bytes at this node, go up one level. */
14870 --depth;
14871 line_breakcheck();
14872 }
14873 else
14874 {
14875 /* Do one more byte at this node. */
14876 n = arridx[depth] + curi[depth];
14877 ++curi[depth];
14878 c = byts[n];
14879 if (c == 0)
14880 {
14881 /* End of word, deal with the word.
14882 * Don't use keep-case words in the fold-case tree,
14883 * they will appear in the keep-case tree.
14884 * Only use the word when the region matches. */
14885 flags = (int)idxs[n];
14886 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014887 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000014888 && (do_region
14889 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000014890 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014891 & lp->lp_region) != 0))
14892 {
14893 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000014894 if (!do_region)
14895 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000014896
14897 /* Dump the basic word if there is no prefix or
14898 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000014899 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000014900 if (c == 0 || curi[depth] == 2)
Bram Moolenaar4770d092006-01-12 23:22:24 +000014901 dump_word(slang, word, dumpflags,
14902 flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014903
14904 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000014905 if (c != 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000014906 lnum = dump_prefixes(slang, word, dumpflags,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014907 flags, lnum);
14908 }
14909 }
14910 else
14911 {
14912 /* Normal char, go one level deeper. */
14913 word[depth++] = c;
14914 arridx[depth] = idxs[n];
14915 curi[depth] = 1;
14916 }
14917 }
14918 }
14919 }
14920 }
14921
14922 /* Delete the empty line that we started with. */
14923 if (curbuf->b_ml.ml_line_count > 1)
14924 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
14925
14926 redraw_later(NOT_VALID);
14927}
14928
14929/*
14930 * Dump one word: apply case modifications and append a line to the buffer.
14931 */
14932 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000014933dump_word(slang, word, dumpflags, flags, lnum)
14934 slang_T *slang;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014935 char_u *word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000014936 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014937 int flags;
14938 linenr_T lnum;
14939{
14940 int keepcap = FALSE;
14941 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +000014942 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014943 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000014944 char_u badword[MAXWLEN + 10];
14945 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014946
Bram Moolenaar4770d092006-01-12 23:22:24 +000014947 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014948 {
14949 /* Need to fix case according to "flags". */
14950 make_case_word(word, cword, flags);
14951 p = cword;
14952 }
14953 else
14954 {
14955 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000014956 if ((dumpflags & DUMPFLAG_KEEPCASE)
14957 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014958 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014959 keepcap = TRUE;
14960 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000014961 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014962
Bram Moolenaar7887d882005-07-01 22:33:52 +000014963 /* Add flags and regions after a slash. */
14964 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014965 {
Bram Moolenaar7887d882005-07-01 22:33:52 +000014966 STRCPY(badword, p);
14967 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014968 if (keepcap)
14969 STRCAT(badword, "=");
14970 if (flags & WF_BANNED)
14971 STRCAT(badword, "!");
14972 else if (flags & WF_RARE)
14973 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +000014974 if (flags & WF_REGION)
14975 for (i = 0; i < 7; ++i)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000014976 if (flags & (0x10000 << i))
Bram Moolenaar7887d882005-07-01 22:33:52 +000014977 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014978 p = badword;
14979 }
14980
Bram Moolenaar4770d092006-01-12 23:22:24 +000014981 if (dumpflags & DUMPFLAG_COUNT)
14982 {
14983 hashitem_T *hi;
14984
14985 /* Include the word count for ":spelldump!". */
14986 hi = hash_find(&slang->sl_wordcount, tw);
14987 if (!HASHITEM_EMPTY(hi))
14988 {
14989 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
14990 tw, HI2WC(hi)->wc_count);
14991 p = IObuff;
14992 }
14993 }
14994
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014995 ml_append(lnum, p, (colnr_T)0, FALSE);
14996}
14997
14998/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014999 * For ":spelldump": Find matching prefixes for "word". Prepend each to
15000 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015001 * Return the updated line number.
15002 */
15003 static linenr_T
Bram Moolenaar4770d092006-01-12 23:22:24 +000015004dump_prefixes(slang, word, dumpflags, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015005 slang_T *slang;
15006 char_u *word; /* case-folded word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000015007 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015008 int flags; /* flags with prefix ID */
15009 linenr_T startlnum;
15010{
15011 idx_T arridx[MAXWLEN];
15012 int curi[MAXWLEN];
15013 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000015014 char_u word_up[MAXWLEN];
15015 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015016 int c;
15017 char_u *byts;
15018 idx_T *idxs;
15019 linenr_T lnum = startlnum;
15020 int depth;
15021 int n;
15022 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015023 int i;
15024
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015025 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +000015026 * upper-case letter in word_up[]. */
15027 c = PTR2CHAR(word);
15028 if (SPELL_TOUPPER(c) != c)
15029 {
15030 onecap_copy(word, word_up, TRUE);
15031 has_word_up = TRUE;
15032 }
15033
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015034 byts = slang->sl_pbyts;
15035 idxs = slang->sl_pidxs;
15036 if (byts != NULL) /* array not is empty */
15037 {
15038 /*
15039 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015040 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015041 */
15042 depth = 0;
15043 arridx[0] = 0;
15044 curi[0] = 1;
15045 while (depth >= 0 && !got_int)
15046 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015047 n = arridx[depth];
15048 len = byts[n];
15049 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015050 {
15051 /* Done all bytes at this node, go up one level. */
15052 --depth;
15053 line_breakcheck();
15054 }
15055 else
15056 {
15057 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015058 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015059 ++curi[depth];
15060 c = byts[n];
15061 if (c == 0)
15062 {
15063 /* End of prefix, find out how many IDs there are. */
15064 for (i = 1; i < len; ++i)
15065 if (byts[n + i] != 0)
15066 break;
15067 curi[depth] += i - 1;
15068
Bram Moolenaar53805d12005-08-01 07:08:33 +000015069 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
15070 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015071 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000015072 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaar4770d092006-01-12 23:22:24 +000015073 dump_word(slang, prefix, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015074 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000015075 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015076 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000015077
15078 /* Check for prefix that matches the word when the
15079 * first letter is upper-case, but only if the prefix has
15080 * a condition. */
15081 if (has_word_up)
15082 {
15083 c = valid_word_prefix(i, n, flags, word_up, slang,
15084 TRUE);
15085 if (c != 0)
15086 {
15087 vim_strncpy(prefix + depth, word_up,
15088 MAXWLEN - depth - 1);
Bram Moolenaar4770d092006-01-12 23:22:24 +000015089 dump_word(slang, prefix, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015090 (c & WF_RAREPFX) ? (flags | WF_RARE)
15091 : flags, lnum++);
15092 }
15093 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015094 }
15095 else
15096 {
15097 /* Normal char, go one level deeper. */
15098 prefix[depth++] = c;
15099 arridx[depth] = idxs[n];
15100 curi[depth] = 1;
15101 }
15102 }
15103 }
15104 }
15105
15106 return lnum;
15107}
15108
Bram Moolenaar95529562005-08-25 21:21:38 +000015109/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015110 * Move "p" to the end of word "start".
15111 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +000015112 */
15113 char_u *
15114spell_to_word_end(start, buf)
15115 char_u *start;
15116 buf_T *buf;
15117{
15118 char_u *p = start;
15119
15120 while (*p != NUL && spell_iswordp(p, buf))
15121 mb_ptr_adv(p);
15122 return p;
15123}
15124
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015125#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015126/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015127 * For Insert mode completion CTRL-X s:
15128 * Find start of the word in front of column "startcol".
15129 * We don't check if it is badly spelled, with completion we can only change
15130 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015131 * Returns the column number of the word.
15132 */
15133 int
15134spell_word_start(startcol)
15135 int startcol;
15136{
15137 char_u *line;
15138 char_u *p;
15139 int col = 0;
15140
Bram Moolenaar95529562005-08-25 21:21:38 +000015141 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015142 return startcol;
15143
15144 /* Find a word character before "startcol". */
15145 line = ml_get_curline();
15146 for (p = line + startcol; p > line; )
15147 {
15148 mb_ptr_back(line, p);
15149 if (spell_iswordp_nmw(p))
15150 break;
15151 }
15152
15153 /* Go back to start of the word. */
15154 while (p > line)
15155 {
15156 col = p - line;
15157 mb_ptr_back(line, p);
15158 if (!spell_iswordp(p, curbuf))
15159 break;
15160 col = 0;
15161 }
15162
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015163 return col;
15164}
15165
15166/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000015167 * Need to check for 'spellcapcheck' now, the word is removed before
15168 * expand_spelling() is called. Therefore the ugly global variable.
15169 */
15170static int spell_expand_need_cap;
15171
15172 void
15173spell_expand_check_cap(col)
15174 colnr_T col;
15175{
15176 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
15177}
15178
15179/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015180 * Get list of spelling suggestions.
15181 * Used for Insert mode completion CTRL-X ?.
15182 * Returns the number of matches. The matches are in "matchp[]", array of
15183 * allocated strings.
15184 */
15185/*ARGSUSED*/
15186 int
15187expand_spelling(lnum, col, pat, matchp)
15188 linenr_T lnum;
15189 int col;
15190 char_u *pat;
15191 char_u ***matchp;
15192{
15193 garray_T ga;
15194
Bram Moolenaar4770d092006-01-12 23:22:24 +000015195 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015196 *matchp = ga.ga_data;
15197 return ga.ga_len;
15198}
15199#endif
15200
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000015201#endif /* FEAT_SYN_HL */