blob: d94fb74786b4c9c10ed30b789cccccdac3e87016 [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 *
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000175 * sectionID == SN_NOSPLITSUGS: nothing
176 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000177 * sectionID == SN_WORDS: <word> ...
178 * <word> N bytes NUL terminated common word
179 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000180 * sectionID == SN_MAP: <mapstr>
181 * <mapstr> N bytes String with sequences of similar characters,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000182 * separated by slashes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000183 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000184 * sectionID == SN_COMPOUND: <compmax> <compminlen> <compsylmax> <compflags>
185 * <compmax> 1 byte Maximum nr of words in compound word.
186 * <compminlen> 1 byte Minimal word length for compounding.
187 * <compsylmax> 1 byte Maximum nr of syllables in compound word.
188 * <compflags> N bytes Flags from COMPOUNDFLAGS items, separated by
189 * slashes.
190 *
Bram Moolenaar78622822005-08-23 21:00:13 +0000191 * sectionID == SN_NOBREAK: (empty, its presence is enough)
192 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000193 * sectionID == SN_SYLLABLE: <syllable>
194 * <syllable> N bytes String from SYLLABLE item.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000195 *
196 * <LWORDTREE>: <wordtree>
197 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000198 * <KWORDTREE>: <wordtree>
199 *
200 * <PREFIXTREE>: <wordtree>
201 *
202 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000203 * <wordtree>: <nodecount> <nodedata> ...
204 *
205 * <nodecount> 4 bytes Number of nodes following. MSB first.
206 *
207 * <nodedata>: <siblingcount> <sibling> ...
208 *
209 * <siblingcount> 1 byte Number of siblings in this node. The siblings
210 * follow in sorted order.
211 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000212 * <sibling>: <byte> [ <nodeidx> <xbyte>
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000213 * | <flags> [<flags2>] [<region>] [<affixID>]
214 * | [<pflags>] <affixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000215 *
216 * <byte> 1 byte Byte value of the sibling. Special cases:
217 * BY_NOFLAGS: End of word without flags and for all
218 * regions.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000219 * For PREFIXTREE <affixID> and
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000220 * <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000221 * BY_FLAGS: End of word, <flags> follow.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000222 * For PREFIXTREE <pflags>, <affixID>
Bram Moolenaar53805d12005-08-01 07:08:33 +0000223 * and <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000224 * BY_FLAGS2: End of word, <flags> and <flags2>
225 * follow. Not used in PREFIXTREE.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000226 * BY_INDEX: Child of sibling is shared, <nodeidx>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000227 * and <xbyte> follow.
228 *
229 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
230 *
231 * <xbyte> 1 byte byte value of the sibling.
232 *
233 * <flags> 1 byte bitmask of:
234 * WF_ALLCAP word must have only capitals
235 * WF_ONECAP first char of word must be capital
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000236 * WF_KEEPCAP keep-case word
237 * WF_FIXCAP keep-case word, all caps not allowed
Bram Moolenaar51485f02005-06-04 21:55:20 +0000238 * WF_RARE rare word
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000239 * WF_BANNED bad word
Bram Moolenaar51485f02005-06-04 21:55:20 +0000240 * WF_REGION <region> follows
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000241 * WF_AFX <affixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000242 *
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000243 * <flags2> 1 byte Bitmask of:
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000244 * WF_HAS_AFF >> 8 word includes affix
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000245 * WF_NEEDCOMP >> 8 word only valid in compound
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000246 * WF_NOSUGGEST >> 8 word not used for suggestions
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000247 *
Bram Moolenaar53805d12005-08-01 07:08:33 +0000248 * <pflags> 1 byte bitmask of:
249 * WFP_RARE rare prefix
250 * WFP_NC non-combining prefix
251 * WFP_UP letter after prefix made upper case
252 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000253 * <region> 1 byte Bitmask for regions in which word is valid. When
254 * omitted it's valid in all regions.
255 * Lowest bit is for region 1.
256 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000257 * <affixID> 1 byte ID of affix that can be used with this word. In
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000258 * PREFIXTREE used for the required prefix ID.
259 *
260 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
261 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000262 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000263 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000264 */
265
Bram Moolenaar4770d092006-01-12 23:22:24 +0000266/*
267 * Vim .sug file format: <SUGHEADER>
268 * <SUGWORDTREE>
269 * <SUGTABLE>
270 *
271 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
272 *
273 * <fileID> 6 bytes "VIMsug"
274 * <versionnr> 1 byte VIMSUGVERSION
275 * <timestamp> 8 bytes timestamp that must match with .spl file
276 *
277 *
278 * <SUGWORDTREE>: <wordtree> (see above, no flags or region used)
279 *
280 *
281 * <SUGTABLE>: <sugwcount> <sugline> ...
282 *
283 * <sugwcount> 4 bytes number of <sugline> following
284 *
285 * <sugline>: <sugnr> ... NUL
286 *
287 * <sugnr>: X bytes word number that results in this soundfolded word,
288 * stored as an offset to the previous number in as
289 * few bytes as possible, see offset2bytes())
290 */
291
Bram Moolenaare19defe2005-03-21 08:23:33 +0000292#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
293# include <io.h> /* for lseek(), must be before vim.h */
294#endif
295
296#include "vim.h"
297
298#if defined(FEAT_SYN_HL) || defined(PROTO)
299
300#ifdef HAVE_FCNTL_H
301# include <fcntl.h>
302#endif
303
Bram Moolenaar4770d092006-01-12 23:22:24 +0000304#ifndef UNIX /* it's in os_unix.h for Unix */
305# include <time.h> /* for time_t */
306#endif
307
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000308#define MAXWLEN 250 /* Assume max. word len is this many bytes.
309 Some places assume a word length fits in a
310 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000311
Bram Moolenaare52325c2005-08-22 22:54:29 +0000312/* Type used for indexes in the word tree need to be at least 4 bytes. If int
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000313 * is 8 bytes we could use something smaller, but what? */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000314#if SIZEOF_INT > 3
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000315typedef int idx_T;
316#else
317typedef long idx_T;
318#endif
319
320/* Flags used for a word. Only the lowest byte can be used, the region byte
321 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000322#define WF_REGION 0x01 /* region byte follows */
323#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
324#define WF_ALLCAP 0x04 /* word must be all capitals */
325#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000326#define WF_BANNED 0x10 /* bad word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000327#define WF_AFX 0x20 /* affix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000328#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000329#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000330
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000331/* for <flags2>, shifted up one byte to be used in wn_flags */
332#define WF_HAS_AFF 0x0100 /* word includes affix */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000333#define WF_NEEDCOMP 0x0200 /* word only valid in compound */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000334#define WF_NOSUGGEST 0x0400 /* word not to be suggested */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000335
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000336/* only used for su_badflags */
337#define WF_MIXCAP 0x20 /* mix of upper and lower case: macaRONI */
338
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000339#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000340
Bram Moolenaar53805d12005-08-01 07:08:33 +0000341/* flags for <pflags> */
342#define WFP_RARE 0x01 /* rare prefix */
343#define WFP_NC 0x02 /* prefix is not combining */
344#define WFP_UP 0x04 /* to-upper prefix */
345
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000346/* Flags for postponed prefixes. Must be above affixID (one byte)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000347 * and prefcondnr (two bytes). */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000348#define WF_RAREPFX (WFP_RARE << 24) /* in sl_pidxs: flag for rare
349 * postponed prefix */
350#define WF_PFX_NC (WFP_NC << 24) /* in sl_pidxs: flag for non-combining
351 * postponed prefix */
352#define WF_PFX_UP (WFP_UP << 24) /* in sl_pidxs: flag for to-upper
353 * postponed prefix */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000354
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000355/* Special byte values for <byte>. Some are only used in the tree for
356 * postponed prefixes, some only in the other trees. This is a bit messy... */
357#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000358 * postponed prefix: no <pflags> */
359#define BY_INDEX 1 /* child is shared, index follows */
360#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
361 * postponed prefix: <pflags> follows */
362#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
363 * follow; never used in prefix tree */
364#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000365
Bram Moolenaar4770d092006-01-12 23:22:24 +0000366/* Info from "REP", "REPSAL" and "SAL" entries in ".aff" file used in si_rep,
367 * si_repsal, sl_rep, and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000368 * One replacement: from "ft_from" to "ft_to". */
369typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000370{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000371 char_u *ft_from;
372 char_u *ft_to;
373} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000374
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000375/* Info from "SAL" entries in ".aff" file used in sl_sal.
376 * The info is split for quick processing by spell_soundfold().
377 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
378typedef struct salitem_S
379{
380 char_u *sm_lead; /* leading letters */
381 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000382 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000383 char_u *sm_rules; /* rules like ^, $, priority */
384 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000385#ifdef FEAT_MBYTE
386 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000387 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000388 int *sm_to_w; /* wide character copy of "sm_to" */
389#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000390} salitem_T;
391
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000392#ifdef FEAT_MBYTE
393typedef int salfirst_T;
394#else
395typedef short salfirst_T;
396#endif
397
Bram Moolenaar5195e452005-08-19 20:32:47 +0000398/* Values for SP_*ERROR are negative, positive values are used by
399 * read_cnt_string(). */
400#define SP_TRUNCERROR -1 /* spell file truncated error */
401#define SP_FORMERROR -2 /* format error in spell file */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000402#define SP_OTHERERROR -3 /* other error while reading spell file */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000403
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000404/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000405 * Structure used to store words and other info for one language, loaded from
406 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000407 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
408 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
409 *
410 * The "byts" array stores the possible bytes in each tree node, preceded by
411 * the number of possible bytes, sorted on byte value:
412 * <len> <byte1> <byte2> ...
413 * The "idxs" array stores the index of the child node corresponding to the
414 * byte in "byts".
415 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000416 * the flags, region mask and affixID for the word. There may be several
417 * zeros in sequence for alternative flag/region/affixID combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000418 */
419typedef struct slang_S slang_T;
420struct slang_S
421{
422 slang_T *sl_next; /* next language */
423 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000424 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000425 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000426
Bram Moolenaar51485f02005-06-04 21:55:20 +0000427 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000428 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000429 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000430 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000431 char_u *sl_pbyts; /* prefix tree word bytes */
432 idx_T *sl_pidxs; /* prefix tree word indexes */
433
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000434 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000435
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000436 char_u *sl_midword; /* MIDWORD string or NULL */
437
Bram Moolenaar4770d092006-01-12 23:22:24 +0000438 hashtab_T sl_wordcount; /* hashtable with word count, wordcount_T */
439
Bram Moolenaar5195e452005-08-19 20:32:47 +0000440 int sl_compmax; /* COMPOUNDMAX (default: MAXWLEN) */
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000441 int sl_compminlen; /* COMPOUNDMIN (default: 0) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000442 int sl_compsylmax; /* COMPOUNDSYLMAX (default: MAXWLEN) */
443 regprog_T *sl_compprog; /* COMPOUNDFLAGS turned into a regexp progrm
444 * (NULL when no compounding) */
445 char_u *sl_compstartflags; /* flags for first compound word */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000446 char_u *sl_compallflags; /* all flags for compound words */
Bram Moolenaar78622822005-08-23 21:00:13 +0000447 char_u sl_nobreak; /* When TRUE: no spaces between words */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000448 char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */
449 garray_T sl_syl_items; /* syllable items */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000450
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000451 int sl_prefixcnt; /* number of items in "sl_prefprog" */
452 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
453
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000454 garray_T sl_rep; /* list of fromto_T entries from REP lines */
455 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
456 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000457 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000458 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000459 there is none */
460 int sl_followup; /* SAL followup */
461 int sl_collapse; /* SAL collapse_result */
462 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000463 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
464 * "sl_sal_first" maps chars, when has_mbyte
465 * "sl_sal" is a list of wide char lists. */
466 garray_T sl_repsal; /* list of fromto_T entries from REPSAL lines */
467 short sl_repsal_first[256]; /* sl_rep_first for REPSAL lines */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000468 int sl_nosplitsugs; /* don't suggest splitting a word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000469
470 /* Info from the .sug file. Loaded on demand. */
471 time_t sl_sugtime; /* timestamp for .sug file */
472 char_u *sl_sbyts; /* soundfolded word bytes */
473 idx_T *sl_sidxs; /* soundfolded word indexes */
474 buf_T *sl_sugbuf; /* buffer with word number table */
475 int sl_sugloaded; /* TRUE when .sug file was loaded or failed to
476 load */
477
Bram Moolenaarea424162005-06-16 21:51:00 +0000478 int sl_has_map; /* TRUE if there is a MAP line */
479#ifdef FEAT_MBYTE
480 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
481 int sl_map_array[256]; /* MAP for first 256 chars */
482#else
483 char_u sl_map_array[256]; /* MAP for first 256 chars */
484#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000485 hashtab_T sl_sounddone; /* table with soundfolded words that have
486 handled, see add_sound_suggest() */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000487};
488
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000489/* First language that is loaded, start of the linked list of loaded
490 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000491static slang_T *first_lang = NULL;
492
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000493/* Flags used in .spl file for soundsalike flags. */
494#define SAL_F0LLOWUP 1
495#define SAL_COLLAPSE 2
496#define SAL_REM_ACCENTS 4
497
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000498/*
499 * Structure used in "b_langp", filled from 'spelllang'.
500 */
501typedef struct langp_S
502{
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000503 slang_T *lp_slang; /* info for this language */
504 slang_T *lp_sallang; /* language used for sound folding or NULL */
505 slang_T *lp_replang; /* language used for REP items or NULL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000506 int lp_region; /* bitmask for region or REGION_ALL */
507} langp_T;
508
509#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
510
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000511#define REGION_ALL 0xff /* word valid in all regions */
512
Bram Moolenaar5195e452005-08-19 20:32:47 +0000513#define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
514#define VIMSPELLMAGICL 8
515#define VIMSPELLVERSION 50
516
Bram Moolenaar4770d092006-01-12 23:22:24 +0000517#define VIMSUGMAGIC "VIMsug" /* string at start of Vim .sug file */
518#define VIMSUGMAGICL 6
519#define VIMSUGVERSION 1
520
Bram Moolenaar5195e452005-08-19 20:32:47 +0000521/* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
522#define SN_REGION 0 /* <regionname> section */
523#define SN_CHARFLAGS 1 /* charflags section */
524#define SN_MIDWORD 2 /* <midword> section */
525#define SN_PREFCOND 3 /* <prefcond> section */
526#define SN_REP 4 /* REP items section */
527#define SN_SAL 5 /* SAL items section */
528#define SN_SOFO 6 /* soundfolding section */
529#define SN_MAP 7 /* MAP items section */
530#define SN_COMPOUND 8 /* compound words section */
531#define SN_SYLLABLE 9 /* syllable section */
Bram Moolenaar78622822005-08-23 21:00:13 +0000532#define SN_NOBREAK 10 /* NOBREAK section */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000533#define SN_SUGFILE 11 /* timestamp for .sug file */
534#define SN_REPSAL 12 /* REPSAL items section */
535#define SN_WORDS 13 /* common words */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000536#define SN_NOSPLITSUGS 14 /* don't split word for suggestions */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000537#define SN_END 255 /* end of sections */
538
539#define SNF_REQUIRED 1 /* <sectionflags>: required section */
540
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000541/* Result values. Lower number is accepted over higher one. */
542#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000543#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000544#define SP_RARE 1
545#define SP_LOCAL 2
546#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000547
Bram Moolenaar7887d882005-07-01 22:33:52 +0000548/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000549static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000550
Bram Moolenaar4770d092006-01-12 23:22:24 +0000551typedef struct wordcount_S
552{
553 short_u wc_count; /* nr of times word was seen */
554 char_u wc_word[1]; /* word, actually longer */
555} wordcount_T;
556
557static wordcount_T dumwc;
558#define WC_KEY_OFF (dumwc.wc_word - (char_u *)&dumwc)
559#define HI2WC(hi) ((wordcount_T *)((hi)->hi_key - WC_KEY_OFF))
560#define MAXWORDCOUNT 0xffff
561
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000562/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000563 * Information used when looking for suggestions.
564 */
565typedef struct suginfo_S
566{
567 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000568 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000569 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000570 int su_sfmaxscore; /* idem, for when doing soundfold words */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000571 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000572 char_u *su_badptr; /* start of bad word in line */
573 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000574 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000575 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
576 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000577 char_u su_sal_badword[MAXWLEN]; /* su_badword soundfolded */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000578 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000579 slang_T *su_sallang; /* default language for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000580} suginfo_T;
581
582/* One word suggestion. Used in "si_ga". */
583typedef struct suggest_S
584{
585 char_u *st_word; /* suggested word, allocated string */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000586 int st_wordlen; /* STRLEN(st_word) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000587 int st_orglen; /* length of replaced text */
588 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000589 int st_altscore; /* used when st_score compares equal */
590 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000591 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000592 slang_T *st_slang; /* language used for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000593} suggest_T;
594
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000595#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000596
Bram Moolenaar4770d092006-01-12 23:22:24 +0000597/* TRUE if a word appears in the list of banned words. */
598#define WAS_BANNED(su, word) (!HASHITEM_EMPTY(hash_find(&su->su_banned, word)))
599
600/* Number of suggestions kept when cleaning up. we need to keep more than
601 * what is displayed, because when rescore_suggestions() is called the score
602 * may change and wrong suggestions may be removed later. */
603#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 130 ? 150 : (su)->su_maxcount + 20)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000604
605/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
606 * of suggestions that are not going to be displayed. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000607#define SUG_MAX_COUNT(su) (SUG_CLEAN_COUNT(su) + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000608
609/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000610#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000611#define SCORE_SPLIT_NO 249 /* split bad word with NOSPLITSUGS */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000612#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000613#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000614#define SCORE_RARE 180 /* rare word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000615#define SCORE_SWAP 75 /* swap two characters */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000616#define SCORE_SWAP3 110 /* swap two characters in three */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000617#define SCORE_REP 65 /* REP replacement */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000618#define SCORE_SUBST 93 /* substitute a character */
619#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000620#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000621#define SCORE_DEL 94 /* delete a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000622#define SCORE_DELDUP 66 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000623#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000624#define SCORE_INS 96 /* insert a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000625#define SCORE_INSDUP 67 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000626#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000627#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000628
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000629#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000630#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
631 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000632
Bram Moolenaar4770d092006-01-12 23:22:24 +0000633#define SCORE_COMMON1 30 /* subtracted for words seen before */
634#define SCORE_COMMON2 40 /* subtracted for words often seen */
635#define SCORE_COMMON3 50 /* subtracted for words very often seen */
636#define SCORE_THRES2 10 /* word count threshold for COMMON2 */
637#define SCORE_THRES3 100 /* word count threshold for COMMON3 */
638
639/* When trying changed soundfold words it becomes slow when trying more than
640 * two changes. With less then two changes it's slightly faster but we miss a
641 * few good suggestions. In rare cases we need to try three of four changes.
642 */
643#define SCORE_SFMAX1 200 /* maximum score for first try */
644#define SCORE_SFMAX2 300 /* maximum score for second try */
645#define SCORE_SFMAX3 400 /* maximum score for third try */
646
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000647#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000648#define SCORE_MAXMAX 999999 /* accept any score */
649#define SCORE_LIMITMAX 350 /* for spell_edit_score_limit() */
650
651/* for spell_edit_score_limit() we need to know the minimum value of
652 * SCORE_ICASE, SCORE_SWAP, SCORE_DEL, SCORE_SIMILAR and SCORE_INS */
653#define SCORE_EDIT_MIN SCORE_SIMILAR
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000654
655/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000656 * Structure to store info for word matching.
657 */
658typedef struct matchinf_S
659{
660 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000661
662 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000663 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000664 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000665 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000666 char_u *mi_cend; /* char after what was used for
667 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000668
669 /* case-folded text */
670 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000671 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000672
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000673 /* for when checking word after a prefix */
674 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000675 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000676 int mi_prefcnt; /* number of entries at mi_prefarridx */
677 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000678#ifdef FEAT_MBYTE
679 int mi_cprefixlen; /* byte length of prefix in original
680 case */
681#else
682# define mi_cprefixlen mi_prefixlen /* it's the same value */
683#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000684
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000685 /* for when checking a compound word */
686 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000687 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
688 int mi_complen; /* nr of compound words used */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000689
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000690 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000691 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000692 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000693 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000694
695 /* for NOBREAK */
696 int mi_result2; /* "mi_resul" without following word */
697 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000698} matchinf_T;
699
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000700/*
701 * The tables used for recognizing word characters according to spelling.
702 * These are only used for the first 256 characters of 'encoding'.
703 */
704typedef struct spelltab_S
705{
706 char_u st_isw[256]; /* flags: is word char */
707 char_u st_isu[256]; /* flags: is uppercase char */
708 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000709 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000710} spelltab_T;
711
712static spelltab_T spelltab;
713static int did_set_spelltab;
714
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000715#define CF_WORD 0x01
716#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000717
718static void clear_spell_chartab __ARGS((spelltab_T *sp));
719static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000720static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
721static int spell_iswordp_nmw __ARGS((char_u *p));
722#ifdef FEAT_MBYTE
723static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
724#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000725static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000726
727/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000728 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000729 */
730typedef enum
731{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000732 STATE_START = 0, /* At start of node check for NUL bytes (goodword
733 * ends); if badword ends there is a match, otherwise
734 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000735 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000736 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000737 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
738 STATE_PLAIN, /* Use each byte of the node. */
739 STATE_DEL, /* Delete a byte from the bad word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000740 STATE_INS_PREP, /* Prepare for inserting bytes. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000741 STATE_INS, /* Insert a byte in the bad word. */
742 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000743 STATE_UNSWAP, /* Undo swap two characters. */
744 STATE_SWAP3, /* Swap two characters over three. */
745 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000746 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000747 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000748 STATE_REP_INI, /* Prepare for using REP items. */
749 STATE_REP, /* Use matching REP items from the .aff file. */
750 STATE_REP_UNDO, /* Undo a REP item replacement. */
751 STATE_FINAL /* End of this node. */
752} state_T;
753
754/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000755 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000756 */
757typedef struct trystate_S
758{
Bram Moolenaarea424162005-06-16 21:51:00 +0000759 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000760 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000761 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000762 short ts_curi; /* index in list of child nodes */
763 char_u ts_fidx; /* index in fword[], case-folded bad word */
764 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
765 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000766 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000767 * PFD_PREFIXTREE or PFD_NOPREFIX */
768 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000769#ifdef FEAT_MBYTE
770 char_u ts_tcharlen; /* number of bytes in tword character */
771 char_u ts_tcharidx; /* current byte index in tword character */
772 char_u ts_isdiff; /* DIFF_ values */
773 char_u ts_fcharstart; /* index in fword where badword char started */
774#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000775 char_u ts_prewordlen; /* length of word in "preword[]" */
776 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000777 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000778 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000779 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000780 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000781 char_u ts_delidx; /* index in fword for char that was deleted,
782 valid when "ts_flags" has TSF_DIDDEL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000783} trystate_T;
784
Bram Moolenaarea424162005-06-16 21:51:00 +0000785/* values for ts_isdiff */
786#define DIFF_NONE 0 /* no different byte (yet) */
787#define DIFF_YES 1 /* different byte found */
788#define DIFF_INSERT 2 /* inserting character */
789
Bram Moolenaard12a1322005-08-21 22:08:24 +0000790/* values for ts_flags */
791#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
792#define TSF_DIDSPLIT 2 /* tried split at this point */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000793#define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000794
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000795/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000796#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000797#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000798#define PFD_NOTSPECIAL 0xfd /* highest value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000799
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000800/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000801#define FIND_FOLDWORD 0 /* find word case-folded */
802#define FIND_KEEPWORD 1 /* find keep-case word */
803#define FIND_PREFIX 2 /* find word after prefix */
804#define FIND_COMPOUND 3 /* find case-folded compound word */
805#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000806
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000807static slang_T *slang_alloc __ARGS((char_u *lang));
808static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000809static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000810static void slang_clear_sug __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000811static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000812static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000813static 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 +0000814static void find_prefix __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000815static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000816static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaar95529562005-08-25 21:21:38 +0000817static int no_spell_checking __ARGS((win_T *wp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000818static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000819static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000820static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000821static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000822static 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 +0000823static int get2c __ARGS((FILE *fd));
824static int get3c __ARGS((FILE *fd));
825static int get4c __ARGS((FILE *fd));
826static time_t get8c __ARGS((FILE *fd));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000827static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000828static char_u *read_string __ARGS((FILE *fd, int cnt));
829static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
830static int read_charflags_section __ARGS((FILE *fd));
831static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000832static int read_rep_section __ARGS((FILE *fd, garray_T *gap, short *first));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000833static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000834static int read_words_section __ARGS((FILE *fd, slang_T *lp, int len));
835static void count_common_word __ARGS((slang_T *lp, char_u *word, int len, int count));
836static int score_wordcount_adj __ARGS((slang_T *slang, int score, char_u *word, int split));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000837static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
838static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
Bram Moolenaar6de68532005-08-24 22:08:48 +0000839static int byte_in_str __ARGS((char_u *str, int byte));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000840static int init_syl_tab __ARGS((slang_T *slang));
841static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000842static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
843static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000844#ifdef FEAT_MBYTE
845static int *mb_str2wide __ARGS((char_u *s));
846#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000847static int spell_read_tree __ARGS((FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt));
848static 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 +0000849static void clear_midword __ARGS((buf_T *buf));
850static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000851static int find_region __ARGS((char_u *rp, char_u *region));
852static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000853static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000854static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000855static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000856static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000857static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000858static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar66fa2712006-01-22 23:22:22 +0000859static 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 +0000860#ifdef FEAT_EVAL
861static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
862#endif
863static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000864static void spell_suggest_intern __ARGS((suginfo_T *su, int interactive));
865static void suggest_load_files __ARGS((void));
866static void tree_count_words __ARGS((char_u *byts, idx_T *idxs));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000867static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000868static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000869static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000870static void suggest_try_special __ARGS((suginfo_T *su));
871static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000872static void suggest_trie_walk __ARGS((suginfo_T *su, langp_T *lp, char_u *fword, int soundfold));
873static void go_deeper __ARGS((trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000874#ifdef FEAT_MBYTE
875static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
876#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000877static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000878static void score_comp_sal __ARGS((suginfo_T *su));
879static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000880static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000881static void suggest_try_soundalike_prep __ARGS((void));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000882static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000883static void suggest_try_soundalike_finish __ARGS((void));
884static void add_sound_suggest __ARGS((suginfo_T *su, char_u *goodword, int score, langp_T *lp));
885static int soundfold_find __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000886static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000887static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000888static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000889static 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));
890static void check_suggestions __ARGS((suginfo_T *su, garray_T *gap));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000891static void add_banned __ARGS((suginfo_T *su, char_u *word));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000892static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000893static void rescore_one __ARGS((suginfo_T *su, suggest_T *stp));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000894static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000895static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
896static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
897static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000898#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000899static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000900#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000901static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000902static int spell_edit_score __ARGS((slang_T *slang, char_u *badword, char_u *goodword));
903static int spell_edit_score_limit __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
904#ifdef FEAT_MBYTE
905static int spell_edit_score_limit_w __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
906#endif
907static void dump_word __ARGS((slang_T *slang, char_u *word, int round, int flags, linenr_T lnum));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000908static 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 +0000909static buf_T *open_spellbuf __ARGS((void));
910static void close_spellbuf __ARGS((buf_T *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000911
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000912/*
913 * Use our own character-case definitions, because the current locale may
914 * differ from what the .spl file uses.
915 * These must not be called with negative number!
916 */
917#ifndef FEAT_MBYTE
918/* Non-multi-byte implementation. */
919# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
920# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
921# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
922#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000923# if defined(HAVE_WCHAR_H)
924# include <wchar.h> /* for towupper() and towlower() */
925# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000926/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
927 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
928 * the "w" library function for characters above 255 if available. */
929# ifdef HAVE_TOWLOWER
930# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
931 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
932# else
933# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
934 : (c) < 256 ? spelltab.st_fold[c] : (c))
935# endif
936
937# ifdef HAVE_TOWUPPER
938# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
939 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
940# else
941# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
942 : (c) < 256 ? spelltab.st_upper[c] : (c))
943# endif
944
945# ifdef HAVE_ISWUPPER
946# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
947 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
948# else
949# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000950 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000951# endif
952#endif
953
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000954
955static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000956static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000957static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar6de68532005-08-24 22:08:48 +0000958static char *e_affname = N_("Affix name too long in %s line %d: %s");
959static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
960static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000961static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000962
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000963/* Remember what "z?" replaced. */
964static char_u *repl_from = NULL;
965static char_u *repl_to = NULL;
966
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000967/*
968 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000969 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000970 * "*attrp" is set to the highlight index for a badly spelled word. For a
971 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000972 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000973 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000974 * "capcol" is used to check for a Capitalised word after the end of a
975 * sentence. If it's zero then perform the check. Return the column where to
976 * check next, or -1 when no sentence end was found. If it's NULL then don't
977 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000978 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000979 * Returns the length of the word in bytes, also when it's OK, so that the
980 * caller can skip over the word.
981 */
982 int
Bram Moolenaar4770d092006-01-12 23:22:24 +0000983spell_check(wp, ptr, attrp, capcol, docount)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000984 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000985 char_u *ptr;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000986 hlf_T *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000987 int *capcol; /* column to check for Capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000988 int docount; /* count good words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000989{
990 matchinf_T mi; /* Most things are put in "mi" so that it can
991 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000992 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000993 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000994 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000995 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +0000996 int count_word = docount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000997
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000998 /* A word never starts at a space or a control character. Return quickly
999 * then, skipping over the character. */
1000 if (*ptr <= ' ')
1001 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001002
1003 /* Return here when loading language files failed. */
1004 if (wp->w_buffer->b_langp.ga_len == 0)
1005 return 1;
1006
Bram Moolenaar5195e452005-08-19 20:32:47 +00001007 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001008
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001009 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +00001010 * 0X99FF. But always do check spelling to find "3GPP" and "11
1011 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001012 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +00001013 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00001014 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
1015 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001016 else
1017 mi.mi_end = skipdigits(ptr);
Bram Moolenaar43abc522005-12-10 20:15:02 +00001018 nrlen = mi.mi_end - ptr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001019 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001020
Bram Moolenaar0c405862005-06-22 22:26:26 +00001021 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001022 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +00001023 mi.mi_fend = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001024 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001025 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001026 do
Bram Moolenaar51485f02005-06-04 21:55:20 +00001027 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001028 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001029 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001030
1031 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
1032 {
1033 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00001034 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001035 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001036 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001037 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001038 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001039 if (capcol != NULL)
1040 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001041
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001042 /* We always use the characters up to the next non-word character,
1043 * also for bad words. */
1044 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001045
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001046 /* Check caps type later. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001047 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001048
Bram Moolenaar5195e452005-08-19 20:32:47 +00001049 /* case-fold the word with one non-word character, so that we can check
1050 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001051 if (*mi.mi_fend != NUL)
1052 mb_ptr_adv(mi.mi_fend);
1053
1054 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
1055 MAXWLEN + 1);
1056 mi.mi_fwordlen = STRLEN(mi.mi_fword);
1057
1058 /* The word is bad unless we recognize it. */
1059 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +00001060 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001061
1062 /*
1063 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001064 * We check them all, because a word may be matched longer in another
1065 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001066 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001067 for (lpi = 0; lpi < wp->w_buffer->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001068 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001069 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, lpi);
1070
1071 /* If reloading fails the language is still in the list but everything
1072 * has been cleared. */
1073 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
1074 continue;
1075
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001076 /* Check for a matching word in case-folded words. */
1077 find_word(&mi, FIND_FOLDWORD);
1078
1079 /* Check for a matching word in keep-case words. */
1080 find_word(&mi, FIND_KEEPWORD);
1081
1082 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001083 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +00001084
1085 /* For a NOBREAK language, may want to use a word without a following
1086 * word as a backup. */
1087 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
1088 && mi.mi_result2 != SP_BAD)
1089 {
1090 mi.mi_result = mi.mi_result2;
1091 mi.mi_end = mi.mi_end2;
1092 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001093
1094 /* Count the word in the first language where it's found to be OK. */
1095 if (count_word && mi.mi_result == SP_OK)
1096 {
1097 count_common_word(mi.mi_lp->lp_slang, ptr,
1098 (int)(mi.mi_end - ptr), 1);
1099 count_word = FALSE;
1100 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001101 }
1102
1103 if (mi.mi_result != SP_OK)
1104 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001105 /* If we found a number skip over it. Allows for "42nd". Do flag
1106 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001107 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001108 {
1109 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
1110 return nrlen;
1111 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001112
1113 /* When we are at a non-word character there is no error, just
1114 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001115 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001116 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001117 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
1118 {
1119 regmatch_T regmatch;
1120
1121 /* Check for end of sentence. */
1122 regmatch.regprog = wp->w_buffer->b_cap_prog;
1123 regmatch.rm_ic = FALSE;
1124 if (vim_regexec(&regmatch, ptr, 0))
1125 *capcol = (int)(regmatch.endp[0] - ptr);
1126 }
1127
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001128#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001129 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001130 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001131#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001132 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001133 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001134 else if (mi.mi_end == ptr)
1135 /* Always include at least one character. Required for when there
1136 * is a mixup in "midword". */
1137 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +00001138 else if (mi.mi_result == SP_BAD
1139 && LANGP_ENTRY(wp->w_buffer->b_langp, 0)->lp_slang->sl_nobreak)
1140 {
1141 char_u *p, *fp;
1142 int save_result = mi.mi_result;
1143
1144 /* First language in 'spelllang' is NOBREAK. Find first position
1145 * at which any word would be valid. */
1146 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001147 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +00001148 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001149 p = mi.mi_word;
1150 fp = mi.mi_fword;
1151 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +00001152 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001153 mb_ptr_adv(p);
1154 mb_ptr_adv(fp);
1155 if (p >= mi.mi_end)
1156 break;
1157 mi.mi_compoff = fp - mi.mi_fword;
1158 find_word(&mi, FIND_COMPOUND);
1159 if (mi.mi_result != SP_BAD)
1160 {
1161 mi.mi_end = p;
1162 break;
1163 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001164 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001165 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +00001166 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001167 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001168
1169 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001170 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001171 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001172 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001173 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001174 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001175 }
1176
Bram Moolenaar5195e452005-08-19 20:32:47 +00001177 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1178 {
1179 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001180 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001181 return wrongcaplen;
1182 }
1183
Bram Moolenaar51485f02005-06-04 21:55:20 +00001184 return (int)(mi.mi_end - ptr);
1185}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001186
Bram Moolenaar51485f02005-06-04 21:55:20 +00001187/*
1188 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001189 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1190 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1191 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1192 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001193 *
1194 * For a match mip->mi_result is updated.
1195 */
1196 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001197find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001198 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001199 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001200{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001201 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001202 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001203 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001204 int endidxcnt = 0;
1205 int len;
1206 int wlen = 0;
1207 int flen;
1208 int c;
1209 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001210 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001211#ifdef FEAT_MBYTE
1212 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001213#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001214 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001215 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001216 slang_T *slang = mip->mi_lp->lp_slang;
1217 unsigned flags;
1218 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001219 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001220 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001221 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001222 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001223
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001224 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001225 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001226 /* Check for word with matching case in keep-case tree. */
1227 ptr = mip->mi_word;
1228 flen = 9999; /* no case folding, always enough bytes */
1229 byts = slang->sl_kbyts;
1230 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001231
1232 if (mode == FIND_KEEPCOMPOUND)
1233 /* Skip over the previously found word(s). */
1234 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001235 }
1236 else
1237 {
1238 /* Check for case-folded in case-folded tree. */
1239 ptr = mip->mi_fword;
1240 flen = mip->mi_fwordlen; /* available case-folded bytes */
1241 byts = slang->sl_fbyts;
1242 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001243
1244 if (mode == FIND_PREFIX)
1245 {
1246 /* Skip over the prefix. */
1247 wlen = mip->mi_prefixlen;
1248 flen -= mip->mi_prefixlen;
1249 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001250 else if (mode == FIND_COMPOUND)
1251 {
1252 /* Skip over the previously found word(s). */
1253 wlen = mip->mi_compoff;
1254 flen -= mip->mi_compoff;
1255 }
1256
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001257 }
1258
Bram Moolenaar51485f02005-06-04 21:55:20 +00001259 if (byts == NULL)
1260 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001261
Bram Moolenaar51485f02005-06-04 21:55:20 +00001262 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001263 * Repeat advancing in the tree until:
1264 * - there is a byte that doesn't match,
1265 * - we reach the end of the tree,
1266 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001267 */
1268 for (;;)
1269 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001270 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001271 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001272
1273 len = byts[arridx++];
1274
1275 /* If the first possible byte is a zero the word could end here.
1276 * Remember this index, we first check for the longest word. */
1277 if (byts[arridx] == 0)
1278 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001279 if (endidxcnt == MAXWLEN)
1280 {
1281 /* Must be a corrupted spell file. */
1282 EMSG(_(e_format));
1283 return;
1284 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001285 endlen[endidxcnt] = wlen;
1286 endidx[endidxcnt++] = arridx++;
1287 --len;
1288
1289 /* Skip over the zeros, there can be several flag/region
1290 * combinations. */
1291 while (len > 0 && byts[arridx] == 0)
1292 {
1293 ++arridx;
1294 --len;
1295 }
1296 if (len == 0)
1297 break; /* no children, word must end here */
1298 }
1299
1300 /* Stop looking at end of the line. */
1301 if (ptr[wlen] == NUL)
1302 break;
1303
1304 /* Perform a binary search in the list of accepted bytes. */
1305 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001306 if (c == TAB) /* <Tab> is handled like <Space> */
1307 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001308 lo = arridx;
1309 hi = arridx + len - 1;
1310 while (lo < hi)
1311 {
1312 m = (lo + hi) / 2;
1313 if (byts[m] > c)
1314 hi = m - 1;
1315 else if (byts[m] < c)
1316 lo = m + 1;
1317 else
1318 {
1319 lo = hi = m;
1320 break;
1321 }
1322 }
1323
1324 /* Stop if there is no matching byte. */
1325 if (hi < lo || byts[lo] != c)
1326 break;
1327
1328 /* Continue at the child (if there is one). */
1329 arridx = idxs[lo];
1330 ++wlen;
1331 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001332
1333 /* One space in the good word may stand for several spaces in the
1334 * checked word. */
1335 if (c == ' ')
1336 {
1337 for (;;)
1338 {
1339 if (flen <= 0 && *mip->mi_fend != NUL)
1340 flen = fold_more(mip);
1341 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1342 break;
1343 ++wlen;
1344 --flen;
1345 }
1346 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001347 }
1348
1349 /*
1350 * Verify that one of the possible endings is valid. Try the longest
1351 * first.
1352 */
1353 while (endidxcnt > 0)
1354 {
1355 --endidxcnt;
1356 arridx = endidx[endidxcnt];
1357 wlen = endlen[endidxcnt];
1358
1359#ifdef FEAT_MBYTE
1360 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1361 continue; /* not at first byte of character */
1362#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001363 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001364 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001365 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001366 continue; /* next char is a word character */
1367 word_ends = FALSE;
1368 }
1369 else
1370 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001371 /* The prefix flag is before compound flags. Once a valid prefix flag
1372 * has been found we try compound flags. */
1373 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001374
1375#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001376 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001377 {
1378 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001379 * when folding case. This can be slow, take a shortcut when the
1380 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001381 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001382 if (STRNCMP(ptr, p, wlen) != 0)
1383 {
1384 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1385 mb_ptr_adv(p);
1386 wlen = p - mip->mi_word;
1387 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001388 }
1389#endif
1390
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001391 /* Check flags and region. For FIND_PREFIX check the condition and
1392 * prefix ID.
1393 * Repeat this if there are more flags/region alternatives until there
1394 * is a match. */
1395 res = SP_BAD;
1396 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1397 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001398 {
1399 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001400
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001401 /* For the fold-case tree check that the case of the checked word
1402 * matches with what the word in the tree requires.
1403 * For keep-case tree the case is always right. For prefixes we
1404 * don't bother to check. */
1405 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001406 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001407 if (mip->mi_cend != mip->mi_word + wlen)
1408 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001409 /* mi_capflags was set for a different word length, need
1410 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001411 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001412 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001413 }
1414
Bram Moolenaar0c405862005-06-22 22:26:26 +00001415 if (mip->mi_capflags == WF_KEEPCAP
1416 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001417 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001418 }
1419
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001420 /* When mode is FIND_PREFIX the word must support the prefix:
1421 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001422 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001423 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001424 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001425 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001426 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001427 mip->mi_word + mip->mi_cprefixlen, slang,
1428 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001429 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001430 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001431
1432 /* Use the WF_RARE flag for a rare prefix. */
1433 if (c & WF_RAREPFX)
1434 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001435 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001436 }
1437
Bram Moolenaar78622822005-08-23 21:00:13 +00001438 if (slang->sl_nobreak)
1439 {
1440 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1441 && (flags & WF_BANNED) == 0)
1442 {
1443 /* NOBREAK: found a valid following word. That's all we
1444 * need to know, so return. */
1445 mip->mi_result = SP_OK;
1446 break;
1447 }
1448 }
1449
1450 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1451 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001452 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00001453 /* If there is no flag or the word is shorter than
1454 * COMPOUNDMIN reject it quickly.
1455 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001456 * that's too short... Myspell compatibility requires this
1457 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001458 if (((unsigned)flags >> 24) == 0
1459 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001460 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001461#ifdef FEAT_MBYTE
1462 /* For multi-byte chars check character length against
1463 * COMPOUNDMIN. */
1464 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001465 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001466 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
1467 wlen - mip->mi_compoff) < slang->sl_compminlen)
1468 continue;
1469#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001470
Bram Moolenaare52325c2005-08-22 22:54:29 +00001471 /* Limit the number of compound words to COMPOUNDMAX if no
1472 * maximum for syllables is specified. */
1473 if (!word_ends && mip->mi_complen + 2 > slang->sl_compmax
1474 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001475 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001476
Bram Moolenaard12a1322005-08-21 22:08:24 +00001477 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001478 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001479 ? slang->sl_compstartflags
1480 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001481 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001482 continue;
1483
Bram Moolenaare52325c2005-08-22 22:54:29 +00001484 if (mode == FIND_COMPOUND)
1485 {
1486 int capflags;
1487
1488 /* Need to check the caps type of the appended compound
1489 * word. */
1490#ifdef FEAT_MBYTE
1491 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1492 mip->mi_compoff) != 0)
1493 {
1494 /* case folding may have changed the length */
1495 p = mip->mi_word;
1496 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1497 mb_ptr_adv(p);
1498 }
1499 else
1500#endif
1501 p = mip->mi_word + mip->mi_compoff;
1502 capflags = captype(p, mip->mi_word + wlen);
1503 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1504 && (flags & WF_FIXCAP) != 0))
1505 continue;
1506
1507 if (capflags != WF_ALLCAP)
1508 {
1509 /* When the character before the word is a word
1510 * character we do not accept a Onecap word. We do
1511 * accept a no-caps word, even when the dictionary
1512 * word specifies ONECAP. */
1513 mb_ptr_back(mip->mi_word, p);
1514 if (spell_iswordp_nmw(p)
1515 ? capflags == WF_ONECAP
1516 : (flags & WF_ONECAP) != 0
1517 && capflags != WF_ONECAP)
1518 continue;
1519 }
1520 }
1521
Bram Moolenaar5195e452005-08-19 20:32:47 +00001522 /* If the word ends the sequence of compound flags of the
1523 * words must match with one of the COMPOUNDFLAGS items and
1524 * the number of syllables must not be too large. */
1525 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1526 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1527 if (word_ends)
1528 {
1529 char_u fword[MAXWLEN];
1530
1531 if (slang->sl_compsylmax < MAXWLEN)
1532 {
1533 /* "fword" is only needed for checking syllables. */
1534 if (ptr == mip->mi_word)
1535 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1536 else
1537 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1538 }
1539 if (!can_compound(slang, fword, mip->mi_compflags))
1540 continue;
1541 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001542 }
1543
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001544 /* Check NEEDCOMPOUND: can't use word without compounding. */
1545 else if (flags & WF_NEEDCOMP)
1546 continue;
1547
Bram Moolenaar78622822005-08-23 21:00:13 +00001548 nobreak_result = SP_OK;
1549
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001550 if (!word_ends)
1551 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001552 int save_result = mip->mi_result;
1553 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001554 langp_T *save_lp = mip->mi_lp;
1555 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +00001556
1557 /* Check that a valid word follows. If there is one and we
1558 * are compounding, it will set "mi_result", thus we are
1559 * always finished here. For NOBREAK we only check that a
1560 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001561 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001562 if (slang->sl_nobreak)
1563 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001564
1565 /* Find following word in case-folded tree. */
1566 mip->mi_compoff = endlen[endidxcnt];
1567#ifdef FEAT_MBYTE
1568 if (has_mbyte && mode == FIND_KEEPWORD)
1569 {
1570 /* Compute byte length in case-folded word from "wlen":
1571 * byte length in keep-case word. Length may change when
1572 * folding case. This can be slow, take a shortcut when
1573 * the case-folded word is equal to the keep-case word. */
1574 p = mip->mi_fword;
1575 if (STRNCMP(ptr, p, wlen) != 0)
1576 {
1577 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1578 mb_ptr_adv(p);
1579 mip->mi_compoff = p - mip->mi_fword;
1580 }
1581 }
1582#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001583 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001584 ++mip->mi_complen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001585
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001586 /* For NOBREAK we need to try all NOBREAK languages, at least
1587 * to find the ".add" file(s). */
1588 for (lpi = 0; lpi < mip->mi_buf->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001589 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001590 if (slang->sl_nobreak)
1591 {
1592 mip->mi_lp = LANGP_ENTRY(mip->mi_buf->b_langp, lpi);
1593 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1594 || !mip->mi_lp->lp_slang->sl_nobreak)
1595 continue;
1596 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001597
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001598 find_word(mip, FIND_COMPOUND);
1599
1600 /* When NOBREAK any word that matches is OK. Otherwise we
1601 * need to find the longest match, thus try with keep-case
1602 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001603 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1604 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001605 /* Find following word in keep-case tree. */
1606 mip->mi_compoff = wlen;
1607 find_word(mip, FIND_KEEPCOMPOUND);
1608
1609 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1610 {
1611 /* Check for following word with prefix. */
1612 mip->mi_compoff = c;
1613 find_prefix(mip, FIND_COMPOUND);
1614 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001615 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001616
1617 if (!slang->sl_nobreak)
1618 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001619 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001620 --mip->mi_complen;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001621 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001622
Bram Moolenaar78622822005-08-23 21:00:13 +00001623 if (slang->sl_nobreak)
1624 {
1625 nobreak_result = mip->mi_result;
1626 mip->mi_result = save_result;
1627 mip->mi_end = save_end;
1628 }
1629 else
1630 {
1631 if (mip->mi_result == SP_OK)
1632 break;
1633 continue;
1634 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001635 }
1636
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001637 if (flags & WF_BANNED)
1638 res = SP_BANNED;
1639 else if (flags & WF_REGION)
1640 {
1641 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001642 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001643 res = SP_OK;
1644 else
1645 res = SP_LOCAL;
1646 }
1647 else if (flags & WF_RARE)
1648 res = SP_RARE;
1649 else
1650 res = SP_OK;
1651
Bram Moolenaar78622822005-08-23 21:00:13 +00001652 /* Always use the longest match and the best result. For NOBREAK
1653 * we separately keep the longest match without a following good
1654 * word as a fall-back. */
1655 if (nobreak_result == SP_BAD)
1656 {
1657 if (mip->mi_result2 > res)
1658 {
1659 mip->mi_result2 = res;
1660 mip->mi_end2 = mip->mi_word + wlen;
1661 }
1662 else if (mip->mi_result2 == res
1663 && mip->mi_end2 < mip->mi_word + wlen)
1664 mip->mi_end2 = mip->mi_word + wlen;
1665 }
1666 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001667 {
1668 mip->mi_result = res;
1669 mip->mi_end = mip->mi_word + wlen;
1670 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001671 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001672 mip->mi_end = mip->mi_word + wlen;
1673
Bram Moolenaar78622822005-08-23 21:00:13 +00001674 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001675 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001676 }
1677
Bram Moolenaar78622822005-08-23 21:00:13 +00001678 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001679 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001680 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001681}
1682
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001683/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001684 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1685 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001686 */
1687 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001688can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001689 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001690 char_u *word;
1691 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001692{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001693 regmatch_T regmatch;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001694#ifdef FEAT_MBYTE
1695 char_u uflags[MAXWLEN * 2];
1696 int i;
1697#endif
1698 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001699
1700 if (slang->sl_compprog == NULL)
1701 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001702#ifdef FEAT_MBYTE
1703 if (enc_utf8)
1704 {
1705 /* Need to convert the single byte flags to utf8 characters. */
1706 p = uflags;
1707 for (i = 0; flags[i] != NUL; ++i)
1708 p += mb_char2bytes(flags[i], p);
1709 *p = NUL;
1710 p = uflags;
1711 }
1712 else
1713#endif
1714 p = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001715 regmatch.regprog = slang->sl_compprog;
1716 regmatch.rm_ic = FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001717 if (!vim_regexec(&regmatch, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001718 return FALSE;
1719
Bram Moolenaare52325c2005-08-22 22:54:29 +00001720 /* Count the number of syllables. This may be slow, do it last. If there
1721 * are too many syllables AND the number of compound words is above
1722 * COMPOUNDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001723 if (slang->sl_compsylmax < MAXWLEN
1724 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001725 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001726 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001727}
1728
1729/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001730 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1731 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001732 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001733 */
1734 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001735valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001736 int totprefcnt; /* nr of prefix IDs */
1737 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001738 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001739 char_u *word;
1740 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001741 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001742{
1743 int prefcnt;
1744 int pidx;
1745 regprog_T *rp;
1746 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001747 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001748
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001749 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001750 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1751 {
1752 pidx = slang->sl_pidxs[arridx + prefcnt];
1753
1754 /* Check the prefix ID. */
1755 if (prefid != (pidx & 0xff))
1756 continue;
1757
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001758 /* Check if the prefix doesn't combine and the word already has a
1759 * suffix. */
1760 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1761 continue;
1762
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001763 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001764 * stored in the two bytes above the prefix ID byte. */
1765 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001766 if (rp != NULL)
1767 {
1768 regmatch.regprog = rp;
1769 regmatch.rm_ic = FALSE;
1770 if (!vim_regexec(&regmatch, word, 0))
1771 continue;
1772 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001773 else if (cond_req)
1774 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001775
Bram Moolenaar53805d12005-08-01 07:08:33 +00001776 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001777 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001778 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001779 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001780}
1781
1782/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001783 * Check if the word at "mip->mi_word" has a matching prefix.
1784 * If it does, then check the following word.
1785 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001786 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1787 * prefix in a compound word.
1788 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001789 * For a match mip->mi_result is updated.
1790 */
1791 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001792find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001793 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001794 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001795{
1796 idx_T arridx = 0;
1797 int len;
1798 int wlen = 0;
1799 int flen;
1800 int c;
1801 char_u *ptr;
1802 idx_T lo, hi, m;
1803 slang_T *slang = mip->mi_lp->lp_slang;
1804 char_u *byts;
1805 idx_T *idxs;
1806
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001807 byts = slang->sl_pbyts;
1808 if (byts == NULL)
1809 return; /* array is empty */
1810
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001811 /* We use the case-folded word here, since prefixes are always
1812 * case-folded. */
1813 ptr = mip->mi_fword;
1814 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001815 if (mode == FIND_COMPOUND)
1816 {
1817 /* Skip over the previously found word(s). */
1818 ptr += mip->mi_compoff;
1819 flen -= mip->mi_compoff;
1820 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001821 idxs = slang->sl_pidxs;
1822
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001823 /*
1824 * Repeat advancing in the tree until:
1825 * - there is a byte that doesn't match,
1826 * - we reach the end of the tree,
1827 * - or we reach the end of the line.
1828 */
1829 for (;;)
1830 {
1831 if (flen == 0 && *mip->mi_fend != NUL)
1832 flen = fold_more(mip);
1833
1834 len = byts[arridx++];
1835
1836 /* If the first possible byte is a zero the prefix could end here.
1837 * Check if the following word matches and supports the prefix. */
1838 if (byts[arridx] == 0)
1839 {
1840 /* There can be several prefixes with different conditions. We
1841 * try them all, since we don't know which one will give the
1842 * longest match. The word is the same each time, pass the list
1843 * of possible prefixes to find_word(). */
1844 mip->mi_prefarridx = arridx;
1845 mip->mi_prefcnt = len;
1846 while (len > 0 && byts[arridx] == 0)
1847 {
1848 ++arridx;
1849 --len;
1850 }
1851 mip->mi_prefcnt -= len;
1852
1853 /* Find the word that comes after the prefix. */
1854 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001855 if (mode == FIND_COMPOUND)
1856 /* Skip over the previously found word(s). */
1857 mip->mi_prefixlen += mip->mi_compoff;
1858
Bram Moolenaar53805d12005-08-01 07:08:33 +00001859#ifdef FEAT_MBYTE
1860 if (has_mbyte)
1861 {
1862 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001863 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1864 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001865 }
1866 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001867 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001868#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001869 find_word(mip, FIND_PREFIX);
1870
1871
1872 if (len == 0)
1873 break; /* no children, word must end here */
1874 }
1875
1876 /* Stop looking at end of the line. */
1877 if (ptr[wlen] == NUL)
1878 break;
1879
1880 /* Perform a binary search in the list of accepted bytes. */
1881 c = ptr[wlen];
1882 lo = arridx;
1883 hi = arridx + len - 1;
1884 while (lo < hi)
1885 {
1886 m = (lo + hi) / 2;
1887 if (byts[m] > c)
1888 hi = m - 1;
1889 else if (byts[m] < c)
1890 lo = m + 1;
1891 else
1892 {
1893 lo = hi = m;
1894 break;
1895 }
1896 }
1897
1898 /* Stop if there is no matching byte. */
1899 if (hi < lo || byts[lo] != c)
1900 break;
1901
1902 /* Continue at the child (if there is one). */
1903 arridx = idxs[lo];
1904 ++wlen;
1905 --flen;
1906 }
1907}
1908
1909/*
1910 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001911 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001912 * Return the length of the folded chars in bytes.
1913 */
1914 static int
1915fold_more(mip)
1916 matchinf_T *mip;
1917{
1918 int flen;
1919 char_u *p;
1920
1921 p = mip->mi_fend;
1922 do
1923 {
1924 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001925 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001926
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001927 /* Include the non-word character so that we can check for the word end. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001928 if (*mip->mi_fend != NUL)
1929 mb_ptr_adv(mip->mi_fend);
1930
1931 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1932 mip->mi_fword + mip->mi_fwordlen,
1933 MAXWLEN - mip->mi_fwordlen);
1934 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1935 mip->mi_fwordlen += flen;
1936 return flen;
1937}
1938
1939/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001940 * Check case flags for a word. Return TRUE if the word has the requested
1941 * case.
1942 */
1943 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001944spell_valid_case(wordflags, treeflags)
1945 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001946 int treeflags; /* flags for the word in the spell tree */
1947{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001948 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001949 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001950 && ((treeflags & WF_ONECAP) == 0
1951 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001952}
1953
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001954/*
1955 * Return TRUE if spell checking is not enabled.
1956 */
1957 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00001958no_spell_checking(wp)
1959 win_T *wp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001960{
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001961 if (!wp->w_p_spell || *wp->w_buffer->b_p_spl == NUL
1962 || wp->w_buffer->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001963 {
1964 EMSG(_("E756: Spell checking is not enabled"));
1965 return TRUE;
1966 }
1967 return FALSE;
1968}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001969
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001970/*
1971 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001972 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
1973 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001974 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1975 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00001976 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001977 */
1978 int
Bram Moolenaar95529562005-08-25 21:21:38 +00001979spell_move_to(wp, dir, allwords, curline, attrp)
1980 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001981 int dir; /* FORWARD or BACKWARD */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001982 int allwords; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001983 int curline;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001984 hlf_T *attrp; /* return: attributes of bad word or NULL
1985 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001986{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001987 linenr_T lnum;
1988 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001989 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001990 char_u *line;
1991 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001992 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001993 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001994 int len;
Bram Moolenaar95529562005-08-25 21:21:38 +00001995 int has_syntax = syntax_present(wp->w_buffer);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001996 int col;
1997 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001998 char_u *buf = NULL;
1999 int buflen = 0;
2000 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002001 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002002 int found_one = FALSE;
2003 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002004
Bram Moolenaar95529562005-08-25 21:21:38 +00002005 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00002006 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002007
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002008 /*
2009 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00002010 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002011 *
2012 * When searching backwards, we continue in the line to find the last
2013 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00002014 *
2015 * We concatenate the start of the next line, so that wrapped words work
2016 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
2017 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002018 */
Bram Moolenaar95529562005-08-25 21:21:38 +00002019 lnum = wp->w_cursor.lnum;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002020 clearpos(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002021
2022 while (!got_int)
2023 {
Bram Moolenaar95529562005-08-25 21:21:38 +00002024 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002025
Bram Moolenaar0c405862005-06-22 22:26:26 +00002026 len = STRLEN(line);
2027 if (buflen < len + MAXWLEN + 2)
2028 {
2029 vim_free(buf);
2030 buflen = len + MAXWLEN + 2;
2031 buf = alloc(buflen);
2032 if (buf == NULL)
2033 break;
2034 }
2035
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002036 /* In first line check first word for Capital. */
2037 if (lnum == 1)
2038 capcol = 0;
2039
2040 /* For checking first word with a capital skip white space. */
2041 if (capcol == 0)
2042 capcol = skipwhite(line) - line;
2043
Bram Moolenaar0c405862005-06-22 22:26:26 +00002044 /* Copy the line into "buf" and append the start of the next line if
2045 * possible. */
2046 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00002047 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002048 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
2049
2050 p = buf + skip;
2051 endp = buf + len;
2052 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002053 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002054 /* When searching backward don't search after the cursor. Unless
2055 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002056 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00002057 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002058 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00002059 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002060 break;
2061
2062 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002063 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002064 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002065
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002066 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002067 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002068 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002069 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002070 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002071 found_one = TRUE;
2072
Bram Moolenaar51485f02005-06-04 21:55:20 +00002073 /* When searching forward only accept a bad word after
2074 * the cursor. */
2075 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002076 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00002077 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002078 && (wrapped
2079 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00002080 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002081 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002082 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002083 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002084 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00002085 col = p - buf;
Bram Moolenaar95529562005-08-25 21:21:38 +00002086 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar51485f02005-06-04 21:55:20 +00002087 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002088 }
2089 else
2090 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002091
Bram Moolenaar51485f02005-06-04 21:55:20 +00002092 if (can_spell)
2093 {
2094 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002095 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002096#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00002097 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002098#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002099 if (dir == FORWARD)
2100 {
2101 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00002102 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002103 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002104 if (attrp != NULL)
2105 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002106 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002107 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002108 else if (curline)
2109 /* Insert mode completion: put cursor after
2110 * the bad word. */
2111 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002112 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002113 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002114 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002115 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002116 }
2117
Bram Moolenaar51485f02005-06-04 21:55:20 +00002118 /* advance to character after the word */
2119 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002120 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002121 }
2122
Bram Moolenaar5195e452005-08-19 20:32:47 +00002123 if (dir == BACKWARD && found_pos.lnum != 0)
2124 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002125 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00002126 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002127 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002128 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002129 }
2130
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002131 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002132 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002133
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002134 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002135 if (dir == BACKWARD)
2136 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002137 /* If we are back at the starting line and searched it again there
2138 * is no match, give up. */
2139 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002140 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002141
2142 if (lnum > 1)
2143 --lnum;
2144 else if (!p_ws)
2145 break; /* at first line and 'nowrapscan' */
2146 else
2147 {
2148 /* Wrap around to the end of the buffer. May search the
2149 * starting line again and accept the last match. */
2150 lnum = wp->w_buffer->b_ml.ml_line_count;
2151 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002152 if (!shortmess(SHM_SEARCH))
2153 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002154 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002155 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002156 }
2157 else
2158 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002159 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2160 ++lnum;
2161 else if (!p_ws)
2162 break; /* at first line and 'nowrapscan' */
2163 else
2164 {
2165 /* Wrap around to the start of the buffer. May search the
2166 * starting line again and accept the first match. */
2167 lnum = 1;
2168 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002169 if (!shortmess(SHM_SEARCH))
2170 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002171 }
2172
2173 /* If we are back at the starting line and there is no match then
2174 * give up. */
2175 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002176 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002177
2178 /* Skip the characters at the start of the next line that were
2179 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002180 if (attr == HLF_COUNT)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002181 skip = p - endp;
2182 else
2183 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002184
2185 /* Capscol skips over the inserted space. */
2186 --capcol;
2187
2188 /* But after empty line check first word in next line */
2189 if (*skipwhite(line) == NUL)
2190 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002191 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002192
2193 line_breakcheck();
2194 }
2195
Bram Moolenaar0c405862005-06-22 22:26:26 +00002196 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002197 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002198}
2199
2200/*
2201 * For spell checking: concatenate the start of the following line "line" into
2202 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
2203 */
2204 void
2205spell_cat_line(buf, line, maxlen)
2206 char_u *buf;
2207 char_u *line;
2208 int maxlen;
2209{
2210 char_u *p;
2211 int n;
2212
2213 p = skipwhite(line);
2214 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2215 p = skipwhite(p + 1);
2216
2217 if (*p != NUL)
2218 {
2219 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002220 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002221 n = p - line;
2222 if (n >= maxlen)
2223 n = maxlen - 1;
2224 vim_memset(buf + 1, ' ', n);
2225 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002226}
2227
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002228/*
2229 * Structure used for the cookie argument of do_in_runtimepath().
2230 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002231typedef struct spelload_S
2232{
2233 char_u sl_lang[MAXWLEN + 1]; /* language name */
2234 slang_T *sl_slang; /* resulting slang_T struct */
2235 int sl_nobreak; /* NOBREAK language found */
2236} spelload_T;
2237
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002238/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002239 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002240 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002241 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002242 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002243spell_load_lang(lang)
2244 char_u *lang;
2245{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002246 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002247 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002248 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002249#ifdef FEAT_AUTOCMD
2250 int round;
2251#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002252
Bram Moolenaarb765d632005-06-07 21:00:02 +00002253 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002254 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002255 STRCPY(sl.sl_lang, lang);
2256 sl.sl_slang = NULL;
2257 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002258
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002259#ifdef FEAT_AUTOCMD
2260 /* We may retry when no spell file is found for the language, an
2261 * autocommand may load it then. */
2262 for (round = 1; round <= 2; ++round)
2263#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002264 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002265 /*
2266 * Find the first spell file for "lang" in 'runtimepath' and load it.
2267 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002268 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002269 "spell/%s.%s.spl", lang, spell_enc());
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002270 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002271
2272 if (r == FAIL && *sl.sl_lang != NUL)
2273 {
2274 /* Try loading the ASCII version. */
2275 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
2276 "spell/%s.ascii.spl", lang);
2277 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
2278
2279#ifdef FEAT_AUTOCMD
2280 if (r == FAIL && *sl.sl_lang != NUL && round == 1
2281 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
2282 curbuf->b_fname, FALSE, curbuf))
2283 continue;
2284 break;
2285#endif
2286 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002287 }
2288
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002289 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002290 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002291 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2292 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002293 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002294 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002295 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002296 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002297 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002298 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002299 }
2300}
2301
2302/*
2303 * Return the encoding used for spell checking: Use 'encoding', except that we
2304 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2305 */
2306 static char_u *
2307spell_enc()
2308{
2309
2310#ifdef FEAT_MBYTE
2311 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2312 return p_enc;
2313#endif
2314 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002315}
2316
2317/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002318 * Get the name of the .spl file for the internal wordlist into
2319 * "fname[MAXPATHL]".
2320 */
2321 static void
2322int_wordlist_spl(fname)
2323 char_u *fname;
2324{
2325 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
2326 int_wordlist, spell_enc());
2327}
2328
2329/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002330 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002331 * Caller must fill "sl_next".
2332 */
2333 static slang_T *
2334slang_alloc(lang)
2335 char_u *lang;
2336{
2337 slang_T *lp;
2338
Bram Moolenaar51485f02005-06-04 21:55:20 +00002339 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002340 if (lp != NULL)
2341 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002342 if (lang != NULL)
2343 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002344 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002345 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002346 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002347 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002348 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002349 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002350
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002351 return lp;
2352}
2353
2354/*
2355 * Free the contents of an slang_T and the structure itself.
2356 */
2357 static void
2358slang_free(lp)
2359 slang_T *lp;
2360{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002361 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002362 vim_free(lp->sl_fname);
2363 slang_clear(lp);
2364 vim_free(lp);
2365}
2366
2367/*
2368 * Clear an slang_T so that the file can be reloaded.
2369 */
2370 static void
2371slang_clear(lp)
2372 slang_T *lp;
2373{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002374 garray_T *gap;
2375 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002376 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002377 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002378 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002379
Bram Moolenaar51485f02005-06-04 21:55:20 +00002380 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002381 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002382 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002383 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002384 vim_free(lp->sl_pbyts);
2385 lp->sl_pbyts = NULL;
2386
Bram Moolenaar51485f02005-06-04 21:55:20 +00002387 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002388 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002389 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002390 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002391 vim_free(lp->sl_pidxs);
2392 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002393
Bram Moolenaar4770d092006-01-12 23:22:24 +00002394 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002395 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002396 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
2397 while (gap->ga_len > 0)
2398 {
2399 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2400 vim_free(ftp->ft_from);
2401 vim_free(ftp->ft_to);
2402 }
2403 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002404 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002405
2406 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002407 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002408 {
2409 /* "ga_len" is set to 1 without adding an item for latin1 */
2410 if (gap->ga_data != NULL)
2411 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2412 for (i = 0; i < gap->ga_len; ++i)
2413 vim_free(((int **)gap->ga_data)[i]);
2414 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002415 else
2416 /* SAL items: free salitem_T items */
2417 while (gap->ga_len > 0)
2418 {
2419 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2420 vim_free(smp->sm_lead);
2421 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2422 vim_free(smp->sm_to);
2423#ifdef FEAT_MBYTE
2424 vim_free(smp->sm_lead_w);
2425 vim_free(smp->sm_oneof_w);
2426 vim_free(smp->sm_to_w);
2427#endif
2428 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002429 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002430
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002431 for (i = 0; i < lp->sl_prefixcnt; ++i)
2432 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002433 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002434 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002435 lp->sl_prefprog = NULL;
2436
2437 vim_free(lp->sl_midword);
2438 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002439
Bram Moolenaar5195e452005-08-19 20:32:47 +00002440 vim_free(lp->sl_compprog);
2441 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002442 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002443 lp->sl_compprog = NULL;
2444 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002445 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002446
2447 vim_free(lp->sl_syllable);
2448 lp->sl_syllable = NULL;
2449 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002450
Bram Moolenaar4770d092006-01-12 23:22:24 +00002451 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2452 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002453
Bram Moolenaar4770d092006-01-12 23:22:24 +00002454#ifdef FEAT_MBYTE
2455 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaarea424162005-06-16 21:51:00 +00002456#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002457
Bram Moolenaar4770d092006-01-12 23:22:24 +00002458 /* Clear info from .sug file. */
2459 slang_clear_sug(lp);
2460
Bram Moolenaar5195e452005-08-19 20:32:47 +00002461 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002462 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002463 lp->sl_compsylmax = MAXWLEN;
2464 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002465}
2466
2467/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002468 * Clear the info from the .sug file in "lp".
2469 */
2470 static void
2471slang_clear_sug(lp)
2472 slang_T *lp;
2473{
2474 vim_free(lp->sl_sbyts);
2475 lp->sl_sbyts = NULL;
2476 vim_free(lp->sl_sidxs);
2477 lp->sl_sidxs = NULL;
2478 close_spellbuf(lp->sl_sugbuf);
2479 lp->sl_sugbuf = NULL;
2480 lp->sl_sugloaded = FALSE;
2481 lp->sl_sugtime = 0;
2482}
2483
2484/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002485 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002486 * Invoked through do_in_runtimepath().
2487 */
2488 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002489spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002490 char_u *fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002491 void *cookie;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002492{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002493 spelload_T *slp = (spelload_T *)cookie;
2494 slang_T *slang;
2495
2496 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2497 if (slang != NULL)
2498 {
2499 /* When a previously loaded file has NOBREAK also use it for the
2500 * ".add" files. */
2501 if (slp->sl_nobreak && slang->sl_add)
2502 slang->sl_nobreak = TRUE;
2503 else if (slang->sl_nobreak)
2504 slp->sl_nobreak = TRUE;
2505
2506 slp->sl_slang = slang;
2507 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002508}
2509
2510/*
2511 * Load one spell file and store the info into a slang_T.
2512 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00002513 * This is invoked in three ways:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002514 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2515 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2516 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2517 * points to the existing slang_T.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002518 * - Just after writing a .spl file; it's read back to produce the .sug file.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002519 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
2520 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002521 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002522 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002523 static slang_T *
2524spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002525 char_u *fname;
2526 char_u *lang;
2527 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002528 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002529{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002530 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002531 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002532 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002533 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002534 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002535 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002536 char_u *save_sourcing_name = sourcing_name;
2537 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002538 slang_T *lp = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002539 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002540 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002541
Bram Moolenaarb765d632005-06-07 21:00:02 +00002542 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002543 if (fd == NULL)
2544 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002545 if (!silent)
2546 EMSG2(_(e_notopen), fname);
2547 else if (p_verbose > 2)
2548 {
2549 verbose_enter();
2550 smsg((char_u *)e_notopen, fname);
2551 verbose_leave();
2552 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002553 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002554 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002555 if (p_verbose > 2)
2556 {
2557 verbose_enter();
2558 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2559 verbose_leave();
2560 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002561
Bram Moolenaarb765d632005-06-07 21:00:02 +00002562 if (old_lp == NULL)
2563 {
2564 lp = slang_alloc(lang);
2565 if (lp == NULL)
2566 goto endFAIL;
2567
2568 /* Remember the file name, used to reload the file when it's updated. */
2569 lp->sl_fname = vim_strsave(fname);
2570 if (lp->sl_fname == NULL)
2571 goto endFAIL;
2572
2573 /* Check for .add.spl. */
2574 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2575 }
2576 else
2577 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002578
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002579 /* Set sourcing_name, so that error messages mention the file name. */
2580 sourcing_name = fname;
2581 sourcing_lnum = 0;
2582
Bram Moolenaar4770d092006-01-12 23:22:24 +00002583 /*
2584 * <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002585 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002586 for (i = 0; i < VIMSPELLMAGICL; ++i)
2587 buf[i] = getc(fd); /* <fileID> */
2588 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2589 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002590 EMSG(_("E757: This does not look like a spell file"));
2591 goto endFAIL;
2592 }
2593 c = getc(fd); /* <versionnr> */
2594 if (c < VIMSPELLVERSION)
2595 {
2596 EMSG(_("E771: Old spell file, needs to be updated"));
2597 goto endFAIL;
2598 }
2599 else if (c > VIMSPELLVERSION)
2600 {
2601 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002602 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002603 }
2604
Bram Moolenaar5195e452005-08-19 20:32:47 +00002605
2606 /*
2607 * <SECTIONS>: <section> ... <sectionend>
2608 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2609 */
2610 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002611 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002612 n = getc(fd); /* <sectionID> or <sectionend> */
2613 if (n == SN_END)
2614 break;
2615 c = getc(fd); /* <sectionflags> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002616 len = get4c(fd); /* <sectionlen> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002617 if (len < 0)
2618 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002619
Bram Moolenaar5195e452005-08-19 20:32:47 +00002620 res = 0;
2621 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002622 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002623 case SN_REGION:
2624 res = read_region_section(fd, lp, len);
2625 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002626
Bram Moolenaar5195e452005-08-19 20:32:47 +00002627 case SN_CHARFLAGS:
2628 res = read_charflags_section(fd);
2629 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002630
Bram Moolenaar5195e452005-08-19 20:32:47 +00002631 case SN_MIDWORD:
2632 lp->sl_midword = read_string(fd, len); /* <midword> */
2633 if (lp->sl_midword == NULL)
2634 goto endFAIL;
2635 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002636
Bram Moolenaar5195e452005-08-19 20:32:47 +00002637 case SN_PREFCOND:
2638 res = read_prefcond_section(fd, lp);
2639 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002640
Bram Moolenaar5195e452005-08-19 20:32:47 +00002641 case SN_REP:
Bram Moolenaar4770d092006-01-12 23:22:24 +00002642 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
2643 break;
2644
2645 case SN_REPSAL:
2646 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002647 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002648
Bram Moolenaar5195e452005-08-19 20:32:47 +00002649 case SN_SAL:
2650 res = read_sal_section(fd, lp);
2651 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002652
Bram Moolenaar5195e452005-08-19 20:32:47 +00002653 case SN_SOFO:
2654 res = read_sofo_section(fd, lp);
2655 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002656
Bram Moolenaar5195e452005-08-19 20:32:47 +00002657 case SN_MAP:
2658 p = read_string(fd, len); /* <mapstr> */
2659 if (p == NULL)
2660 goto endFAIL;
2661 set_map_str(lp, p);
2662 vim_free(p);
2663 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002664
Bram Moolenaar4770d092006-01-12 23:22:24 +00002665 case SN_WORDS:
2666 res = read_words_section(fd, lp, len);
2667 break;
2668
2669 case SN_SUGFILE:
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002670 lp->sl_sugtime = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002671 break;
2672
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002673 case SN_NOSPLITSUGS:
2674 lp->sl_nosplitsugs = TRUE; /* <timestamp> */
2675 break;
2676
Bram Moolenaar5195e452005-08-19 20:32:47 +00002677 case SN_COMPOUND:
2678 res = read_compound(fd, lp, len);
2679 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002680
Bram Moolenaar78622822005-08-23 21:00:13 +00002681 case SN_NOBREAK:
2682 lp->sl_nobreak = TRUE;
2683 break;
2684
Bram Moolenaar5195e452005-08-19 20:32:47 +00002685 case SN_SYLLABLE:
2686 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2687 if (lp->sl_syllable == NULL)
2688 goto endFAIL;
2689 if (init_syl_tab(lp) == FAIL)
2690 goto endFAIL;
2691 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002692
Bram Moolenaar5195e452005-08-19 20:32:47 +00002693 default:
2694 /* Unsupported section. When it's required give an error
2695 * message. When it's not required skip the contents. */
2696 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002697 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002698 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002699 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002700 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002701 while (--len >= 0)
2702 if (getc(fd) < 0)
2703 goto truncerr;
2704 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002705 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002706someerror:
Bram Moolenaar5195e452005-08-19 20:32:47 +00002707 if (res == SP_FORMERROR)
2708 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002709 EMSG(_(e_format));
2710 goto endFAIL;
2711 }
2712 if (res == SP_TRUNCERROR)
2713 {
2714truncerr:
2715 EMSG(_(e_spell_trunc));
2716 goto endFAIL;
2717 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002718 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002719 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002720 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002721
Bram Moolenaar4770d092006-01-12 23:22:24 +00002722 /* <LWORDTREE> */
2723 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
2724 if (res != 0)
2725 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002726
Bram Moolenaar4770d092006-01-12 23:22:24 +00002727 /* <KWORDTREE> */
2728 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
2729 if (res != 0)
2730 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002731
Bram Moolenaar4770d092006-01-12 23:22:24 +00002732 /* <PREFIXTREE> */
2733 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
2734 lp->sl_prefixcnt);
2735 if (res != 0)
2736 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002737
Bram Moolenaarb765d632005-06-07 21:00:02 +00002738 /* For a new file link it in the list of spell files. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002739 if (old_lp == NULL && lang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002740 {
2741 lp->sl_next = first_lang;
2742 first_lang = lp;
2743 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002744
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002745 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002746
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002747endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002748 if (lang != NULL)
2749 /* truncating the name signals the error to spell_load_lang() */
2750 *lang = NUL;
2751 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002752 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002753 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002754
2755endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002756 if (fd != NULL)
2757 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002758 sourcing_name = save_sourcing_name;
2759 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002760
2761 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002762}
2763
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002764/*
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002765 * Read 2 bytes from "fd" and turn them into an int, MSB first.
2766 */
2767 static int
2768get2c(fd)
2769 FILE *fd;
2770{
2771 long n;
2772
2773 n = getc(fd);
2774 n = (n << 8) + getc(fd);
2775 return n;
2776}
2777
2778/*
2779 * Read 3 bytes from "fd" and turn them into an int, MSB first.
2780 */
2781 static int
2782get3c(fd)
2783 FILE *fd;
2784{
2785 long n;
2786
2787 n = getc(fd);
2788 n = (n << 8) + getc(fd);
2789 n = (n << 8) + getc(fd);
2790 return n;
2791}
2792
2793/*
2794 * Read 4 bytes from "fd" and turn them into an int, MSB first.
2795 */
2796 static int
2797get4c(fd)
2798 FILE *fd;
2799{
2800 long n;
2801
2802 n = getc(fd);
2803 n = (n << 8) + getc(fd);
2804 n = (n << 8) + getc(fd);
2805 n = (n << 8) + getc(fd);
2806 return n;
2807}
2808
2809/*
2810 * Read 8 bytes from "fd" and turn them into a time_t, MSB first.
2811 */
2812 static time_t
2813get8c(fd)
2814 FILE *fd;
2815{
2816 time_t n = 0;
2817 int i;
2818
2819 for (i = 0; i < 8; ++i)
2820 n = (n << 8) + getc(fd);
2821 return n;
2822}
2823
2824/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002825 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002826 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002827 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002828 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2829 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002830 */
2831 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002832read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002833 FILE *fd;
2834 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002835 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002836{
2837 int cnt = 0;
2838 int i;
2839 char_u *str;
2840
2841 /* read the length bytes, MSB first */
2842 for (i = 0; i < cnt_bytes; ++i)
2843 cnt = (cnt << 8) + getc(fd);
2844 if (cnt < 0)
2845 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002846 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002847 return NULL;
2848 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002849 *cntp = cnt;
2850 if (cnt == 0)
2851 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002852
Bram Moolenaar5195e452005-08-19 20:32:47 +00002853 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002854 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002855 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002856 return str;
2857}
2858
Bram Moolenaar7887d882005-07-01 22:33:52 +00002859/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00002860 * Read a string of length "cnt" from "fd" into allocated memory.
2861 * Returns NULL when out of memory.
2862 */
2863 static char_u *
2864read_string(fd, cnt)
2865 FILE *fd;
2866 int cnt;
2867{
2868 char_u *str;
2869 int i;
2870
2871 /* allocate memory */
2872 str = alloc((unsigned)cnt + 1);
2873 if (str != NULL)
2874 {
2875 /* Read the string. Doesn't check for truncated file. */
2876 for (i = 0; i < cnt; ++i)
2877 str[i] = getc(fd);
2878 str[i] = NUL;
2879 }
2880 return str;
2881}
2882
2883/*
2884 * Read SN_REGION: <regionname> ...
2885 * Return SP_*ERROR flags.
2886 */
2887 static int
2888read_region_section(fd, lp, len)
2889 FILE *fd;
2890 slang_T *lp;
2891 int len;
2892{
2893 int i;
2894
2895 if (len > 16)
2896 return SP_FORMERROR;
2897 for (i = 0; i < len; ++i)
2898 lp->sl_regions[i] = getc(fd); /* <regionname> */
2899 lp->sl_regions[len] = NUL;
2900 return 0;
2901}
2902
2903/*
2904 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
2905 * <folcharslen> <folchars>
2906 * Return SP_*ERROR flags.
2907 */
2908 static int
2909read_charflags_section(fd)
2910 FILE *fd;
2911{
2912 char_u *flags;
2913 char_u *fol;
2914 int flagslen, follen;
2915
2916 /* <charflagslen> <charflags> */
2917 flags = read_cnt_string(fd, 1, &flagslen);
2918 if (flagslen < 0)
2919 return flagslen;
2920
2921 /* <folcharslen> <folchars> */
2922 fol = read_cnt_string(fd, 2, &follen);
2923 if (follen < 0)
2924 {
2925 vim_free(flags);
2926 return follen;
2927 }
2928
2929 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
2930 if (flags != NULL && fol != NULL)
2931 set_spell_charflags(flags, flagslen, fol);
2932
2933 vim_free(flags);
2934 vim_free(fol);
2935
2936 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
2937 if ((flags == NULL) != (fol == NULL))
2938 return SP_FORMERROR;
2939 return 0;
2940}
2941
2942/*
2943 * Read SN_PREFCOND section.
2944 * Return SP_*ERROR flags.
2945 */
2946 static int
2947read_prefcond_section(fd, lp)
2948 FILE *fd;
2949 slang_T *lp;
2950{
2951 int cnt;
2952 int i;
2953 int n;
2954 char_u *p;
2955 char_u buf[MAXWLEN + 1];
2956
2957 /* <prefcondcnt> <prefcond> ... */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002958 cnt = get2c(fd); /* <prefcondcnt> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002959 if (cnt <= 0)
2960 return SP_FORMERROR;
2961
2962 lp->sl_prefprog = (regprog_T **)alloc_clear(
2963 (unsigned)sizeof(regprog_T *) * cnt);
2964 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002965 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002966 lp->sl_prefixcnt = cnt;
2967
2968 for (i = 0; i < cnt; ++i)
2969 {
2970 /* <prefcond> : <condlen> <condstr> */
2971 n = getc(fd); /* <condlen> */
2972 if (n < 0 || n >= MAXWLEN)
2973 return SP_FORMERROR;
2974
2975 /* When <condlen> is zero we have an empty condition. Otherwise
2976 * compile the regexp program used to check for the condition. */
2977 if (n > 0)
2978 {
2979 buf[0] = '^'; /* always match at one position only */
2980 p = buf + 1;
2981 while (n-- > 0)
2982 *p++ = getc(fd); /* <condstr> */
2983 *p = NUL;
2984 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
2985 }
2986 }
2987 return 0;
2988}
2989
2990/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002991 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
Bram Moolenaar5195e452005-08-19 20:32:47 +00002992 * Return SP_*ERROR flags.
2993 */
2994 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002995read_rep_section(fd, gap, first)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002996 FILE *fd;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002997 garray_T *gap;
2998 short *first;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002999{
3000 int cnt;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003001 fromto_T *ftp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003002 int i;
3003
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003004 cnt = get2c(fd); /* <repcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003005 if (cnt < 0)
3006 return SP_TRUNCERROR;
3007
Bram Moolenaar5195e452005-08-19 20:32:47 +00003008 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003009 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003010
3011 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
3012 for (; gap->ga_len < cnt; ++gap->ga_len)
3013 {
3014 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
3015 ftp->ft_from = read_cnt_string(fd, 1, &i);
3016 if (i < 0)
3017 return i;
3018 if (i == 0)
3019 return SP_FORMERROR;
3020 ftp->ft_to = read_cnt_string(fd, 1, &i);
3021 if (i <= 0)
3022 {
3023 vim_free(ftp->ft_from);
3024 if (i < 0)
3025 return i;
3026 return SP_FORMERROR;
3027 }
3028 }
3029
3030 /* Fill the first-index table. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003031 for (i = 0; i < 256; ++i)
3032 first[i] = -1;
3033 for (i = 0; i < gap->ga_len; ++i)
3034 {
3035 ftp = &((fromto_T *)gap->ga_data)[i];
3036 if (first[*ftp->ft_from] == -1)
3037 first[*ftp->ft_from] = i;
3038 }
3039 return 0;
3040}
3041
3042/*
3043 * Read SN_SAL section: <salflags> <salcount> <sal> ...
3044 * Return SP_*ERROR flags.
3045 */
3046 static int
3047read_sal_section(fd, slang)
3048 FILE *fd;
3049 slang_T *slang;
3050{
3051 int i;
3052 int cnt;
3053 garray_T *gap;
3054 salitem_T *smp;
3055 int ccnt;
3056 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003057 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003058
3059 slang->sl_sofo = FALSE;
3060
3061 i = getc(fd); /* <salflags> */
3062 if (i & SAL_F0LLOWUP)
3063 slang->sl_followup = TRUE;
3064 if (i & SAL_COLLAPSE)
3065 slang->sl_collapse = TRUE;
3066 if (i & SAL_REM_ACCENTS)
3067 slang->sl_rem_accents = TRUE;
3068
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003069 cnt = get2c(fd); /* <salcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003070 if (cnt < 0)
3071 return SP_TRUNCERROR;
3072
3073 gap = &slang->sl_sal;
3074 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003075 if (ga_grow(gap, cnt + 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003076 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003077
3078 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
3079 for (; gap->ga_len < cnt; ++gap->ga_len)
3080 {
3081 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3082 ccnt = getc(fd); /* <salfromlen> */
3083 if (ccnt < 0)
3084 return SP_TRUNCERROR;
3085 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003086 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003087 smp->sm_lead = p;
3088
3089 /* Read up to the first special char into sm_lead. */
3090 for (i = 0; i < ccnt; ++i)
3091 {
3092 c = getc(fd); /* <salfrom> */
3093 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
3094 break;
3095 *p++ = c;
3096 }
3097 smp->sm_leadlen = p - smp->sm_lead;
3098 *p++ = NUL;
3099
3100 /* Put (abc) chars in sm_oneof, if any. */
3101 if (c == '(')
3102 {
3103 smp->sm_oneof = p;
3104 for (++i; i < ccnt; ++i)
3105 {
3106 c = getc(fd); /* <salfrom> */
3107 if (c == ')')
3108 break;
3109 *p++ = c;
3110 }
3111 *p++ = NUL;
3112 if (++i < ccnt)
3113 c = getc(fd);
3114 }
3115 else
3116 smp->sm_oneof = NULL;
3117
3118 /* Any following chars go in sm_rules. */
3119 smp->sm_rules = p;
3120 if (i < ccnt)
3121 /* store the char we got while checking for end of sm_lead */
3122 *p++ = c;
3123 for (++i; i < ccnt; ++i)
3124 *p++ = getc(fd); /* <salfrom> */
3125 *p++ = NUL;
3126
3127 /* <saltolen> <salto> */
3128 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
3129 if (ccnt < 0)
3130 {
3131 vim_free(smp->sm_lead);
3132 return ccnt;
3133 }
3134
3135#ifdef FEAT_MBYTE
3136 if (has_mbyte)
3137 {
3138 /* convert the multi-byte strings to wide char strings */
3139 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3140 smp->sm_leadlen = mb_charlen(smp->sm_lead);
3141 if (smp->sm_oneof == NULL)
3142 smp->sm_oneof_w = NULL;
3143 else
3144 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
3145 if (smp->sm_to == NULL)
3146 smp->sm_to_w = NULL;
3147 else
3148 smp->sm_to_w = mb_str2wide(smp->sm_to);
3149 if (smp->sm_lead_w == NULL
3150 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
3151 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
3152 {
3153 vim_free(smp->sm_lead);
3154 vim_free(smp->sm_to);
3155 vim_free(smp->sm_lead_w);
3156 vim_free(smp->sm_oneof_w);
3157 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003158 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003159 }
3160 }
3161#endif
3162 }
3163
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003164 if (gap->ga_len > 0)
3165 {
3166 /* Add one extra entry to mark the end with an empty sm_lead. Avoids
3167 * that we need to check the index every time. */
3168 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3169 if ((p = alloc(1)) == NULL)
3170 return SP_OTHERERROR;
3171 p[0] = NUL;
3172 smp->sm_lead = p;
3173 smp->sm_leadlen = 0;
3174 smp->sm_oneof = NULL;
3175 smp->sm_rules = p;
3176 smp->sm_to = NULL;
3177#ifdef FEAT_MBYTE
3178 if (has_mbyte)
3179 {
3180 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3181 smp->sm_leadlen = 0;
3182 smp->sm_oneof_w = NULL;
3183 smp->sm_to_w = NULL;
3184 }
3185#endif
3186 ++gap->ga_len;
3187 }
3188
Bram Moolenaar5195e452005-08-19 20:32:47 +00003189 /* Fill the first-index table. */
3190 set_sal_first(slang);
3191
3192 return 0;
3193}
3194
3195/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003196 * Read SN_WORDS: <word> ...
3197 * Return SP_*ERROR flags.
3198 */
3199 static int
3200read_words_section(fd, lp, len)
3201 FILE *fd;
3202 slang_T *lp;
3203 int len;
3204{
3205 int done = 0;
3206 int i;
3207 char_u word[MAXWLEN];
3208
3209 while (done < len)
3210 {
3211 /* Read one word at a time. */
3212 for (i = 0; ; ++i)
3213 {
3214 word[i] = getc(fd);
3215 if (word[i] == NUL)
3216 break;
3217 if (i == MAXWLEN - 1)
3218 return SP_FORMERROR;
3219 }
3220
3221 /* Init the count to 10. */
3222 count_common_word(lp, word, -1, 10);
3223 done += i + 1;
3224 }
3225 return 0;
3226}
3227
3228/*
3229 * Add a word to the hashtable of common words.
3230 * If it's already there then the counter is increased.
3231 */
3232 static void
3233count_common_word(lp, word, len, count)
3234 slang_T *lp;
3235 char_u *word;
3236 int len; /* word length, -1 for upto NUL */
3237 int count; /* 1 to count once, 10 to init */
3238{
3239 hash_T hash;
3240 hashitem_T *hi;
3241 wordcount_T *wc;
3242 char_u buf[MAXWLEN];
3243 char_u *p;
3244
3245 if (len == -1)
3246 p = word;
3247 else
3248 {
3249 vim_strncpy(buf, word, len);
3250 p = buf;
3251 }
3252
3253 hash = hash_hash(p);
3254 hi = hash_lookup(&lp->sl_wordcount, p, hash);
3255 if (HASHITEM_EMPTY(hi))
3256 {
3257 wc = (wordcount_T *)alloc(sizeof(wordcount_T) + STRLEN(p));
3258 if (wc == NULL)
3259 return;
3260 STRCPY(wc->wc_word, p);
3261 wc->wc_count = count;
3262 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
3263 }
3264 else
3265 {
3266 wc = HI2WC(hi);
3267 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
3268 wc->wc_count = MAXWORDCOUNT;
3269 }
3270}
3271
3272/*
3273 * Adjust the score of common words.
3274 */
3275 static int
3276score_wordcount_adj(slang, score, word, split)
3277 slang_T *slang;
3278 int score;
3279 char_u *word;
3280 int split; /* word was split, less bonus */
3281{
3282 hashitem_T *hi;
3283 wordcount_T *wc;
3284 int bonus;
3285 int newscore;
3286
3287 hi = hash_find(&slang->sl_wordcount, word);
3288 if (!HASHITEM_EMPTY(hi))
3289 {
3290 wc = HI2WC(hi);
3291 if (wc->wc_count < SCORE_THRES2)
3292 bonus = SCORE_COMMON1;
3293 else if (wc->wc_count < SCORE_THRES3)
3294 bonus = SCORE_COMMON2;
3295 else
3296 bonus = SCORE_COMMON3;
3297 if (split)
3298 newscore = score - bonus / 2;
3299 else
3300 newscore = score - bonus;
3301 if (newscore < 0)
3302 return 0;
3303 return newscore;
3304 }
3305 return score;
3306}
3307
3308/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003309 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
3310 * Return SP_*ERROR flags.
3311 */
3312 static int
3313read_sofo_section(fd, slang)
3314 FILE *fd;
3315 slang_T *slang;
3316{
3317 int cnt;
3318 char_u *from, *to;
3319 int res;
3320
3321 slang->sl_sofo = TRUE;
3322
3323 /* <sofofromlen> <sofofrom> */
3324 from = read_cnt_string(fd, 2, &cnt);
3325 if (cnt < 0)
3326 return cnt;
3327
3328 /* <sofotolen> <sofoto> */
3329 to = read_cnt_string(fd, 2, &cnt);
3330 if (cnt < 0)
3331 {
3332 vim_free(from);
3333 return cnt;
3334 }
3335
3336 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
3337 if (from != NULL && to != NULL)
3338 res = set_sofo(slang, from, to);
3339 else if (from != NULL || to != NULL)
3340 res = SP_FORMERROR; /* only one of two strings is an error */
3341 else
3342 res = 0;
3343
3344 vim_free(from);
3345 vim_free(to);
3346 return res;
3347}
3348
3349/*
3350 * Read the compound section from the .spl file:
3351 * <compmax> <compminlen> <compsylmax> <compflags>
3352 * Returns SP_*ERROR flags.
3353 */
3354 static int
3355read_compound(fd, slang, len)
3356 FILE *fd;
3357 slang_T *slang;
3358 int len;
3359{
3360 int todo = len;
3361 int c;
3362 int atstart;
3363 char_u *pat;
3364 char_u *pp;
3365 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003366 char_u *ap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003367
3368 if (todo < 2)
3369 return SP_FORMERROR; /* need at least two bytes */
3370
3371 --todo;
3372 c = getc(fd); /* <compmax> */
3373 if (c < 2)
3374 c = MAXWLEN;
3375 slang->sl_compmax = c;
3376
3377 --todo;
3378 c = getc(fd); /* <compminlen> */
3379 if (c < 1)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003380 c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003381 slang->sl_compminlen = c;
3382
3383 --todo;
3384 c = getc(fd); /* <compsylmax> */
3385 if (c < 1)
3386 c = MAXWLEN;
3387 slang->sl_compsylmax = c;
3388
3389 /* Turn the COMPOUNDFLAGS items into a regexp pattern:
3390 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003391 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3392 * Conversion to utf-8 may double the size. */
3393 c = todo * 2 + 7;
3394#ifdef FEAT_MBYTE
3395 if (enc_utf8)
3396 c += todo * 2;
3397#endif
3398 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003399 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003400 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003401
Bram Moolenaard12a1322005-08-21 22:08:24 +00003402 /* We also need a list of all flags that can appear at the start and one
3403 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003404 cp = alloc(todo + 1);
3405 if (cp == NULL)
3406 {
3407 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003408 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003409 }
3410 slang->sl_compstartflags = cp;
3411 *cp = NUL;
3412
Bram Moolenaard12a1322005-08-21 22:08:24 +00003413 ap = alloc(todo + 1);
3414 if (ap == NULL)
3415 {
3416 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003417 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003418 }
3419 slang->sl_compallflags = ap;
3420 *ap = NUL;
3421
Bram Moolenaar5195e452005-08-19 20:32:47 +00003422 pp = pat;
3423 *pp++ = '^';
3424 *pp++ = '\\';
3425 *pp++ = '(';
3426
3427 atstart = 1;
3428 while (todo-- > 0)
3429 {
3430 c = getc(fd); /* <compflags> */
Bram Moolenaard12a1322005-08-21 22:08:24 +00003431
3432 /* Add all flags to "sl_compallflags". */
3433 if (vim_strchr((char_u *)"+*[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00003434 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003435 {
3436 *ap++ = c;
3437 *ap = NUL;
3438 }
3439
Bram Moolenaar5195e452005-08-19 20:32:47 +00003440 if (atstart != 0)
3441 {
3442 /* At start of item: copy flags to "sl_compstartflags". For a
3443 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3444 if (c == '[')
3445 atstart = 2;
3446 else if (c == ']')
3447 atstart = 0;
3448 else
3449 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003450 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003451 {
3452 *cp++ = c;
3453 *cp = NUL;
3454 }
3455 if (atstart == 1)
3456 atstart = 0;
3457 }
3458 }
3459 if (c == '/') /* slash separates two items */
3460 {
3461 *pp++ = '\\';
3462 *pp++ = '|';
3463 atstart = 1;
3464 }
3465 else /* normal char, "[abc]" and '*' are copied as-is */
3466 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003467 if (c == '+' || c == '~')
Bram Moolenaar5195e452005-08-19 20:32:47 +00003468 *pp++ = '\\'; /* "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003469#ifdef FEAT_MBYTE
3470 if (enc_utf8)
3471 pp += mb_char2bytes(c, pp);
3472 else
3473#endif
3474 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003475 }
3476 }
3477
3478 *pp++ = '\\';
3479 *pp++ = ')';
3480 *pp++ = '$';
3481 *pp = NUL;
3482
3483 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3484 vim_free(pat);
3485 if (slang->sl_compprog == NULL)
3486 return SP_FORMERROR;
3487
3488 return 0;
3489}
3490
Bram Moolenaar6de68532005-08-24 22:08:48 +00003491/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003492 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003493 * Like strchr() but independent of locale.
3494 */
3495 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003496byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003497 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003498 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003499{
3500 char_u *p;
3501
3502 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003503 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003504 return TRUE;
3505 return FALSE;
3506}
3507
Bram Moolenaar5195e452005-08-19 20:32:47 +00003508#define SY_MAXLEN 30
3509typedef struct syl_item_S
3510{
3511 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3512 int sy_len;
3513} syl_item_T;
3514
3515/*
3516 * Truncate "slang->sl_syllable" at the first slash and put the following items
3517 * in "slang->sl_syl_items".
3518 */
3519 static int
3520init_syl_tab(slang)
3521 slang_T *slang;
3522{
3523 char_u *p;
3524 char_u *s;
3525 int l;
3526 syl_item_T *syl;
3527
3528 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3529 p = vim_strchr(slang->sl_syllable, '/');
3530 while (p != NULL)
3531 {
3532 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003533 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003534 break;
3535 s = p;
3536 p = vim_strchr(p, '/');
3537 if (p == NULL)
3538 l = STRLEN(s);
3539 else
3540 l = p - s;
3541 if (l >= SY_MAXLEN)
3542 return SP_FORMERROR;
3543 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003544 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003545 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3546 + slang->sl_syl_items.ga_len++;
3547 vim_strncpy(syl->sy_chars, s, l);
3548 syl->sy_len = l;
3549 }
3550 return OK;
3551}
3552
3553/*
3554 * Count the number of syllables in "word".
3555 * When "word" contains spaces the syllables after the last space are counted.
3556 * Returns zero if syllables are not defines.
3557 */
3558 static int
3559count_syllables(slang, word)
3560 slang_T *slang;
3561 char_u *word;
3562{
3563 int cnt = 0;
3564 int skip = FALSE;
3565 char_u *p;
3566 int len;
3567 int i;
3568 syl_item_T *syl;
3569 int c;
3570
3571 if (slang->sl_syllable == NULL)
3572 return 0;
3573
3574 for (p = word; *p != NUL; p += len)
3575 {
3576 /* When running into a space reset counter. */
3577 if (*p == ' ')
3578 {
3579 len = 1;
3580 cnt = 0;
3581 continue;
3582 }
3583
3584 /* Find longest match of syllable items. */
3585 len = 0;
3586 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3587 {
3588 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3589 if (syl->sy_len > len
3590 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3591 len = syl->sy_len;
3592 }
3593 if (len != 0) /* found a match, count syllable */
3594 {
3595 ++cnt;
3596 skip = FALSE;
3597 }
3598 else
3599 {
3600 /* No recognized syllable item, at least a syllable char then? */
3601#ifdef FEAT_MBYTE
3602 c = mb_ptr2char(p);
3603 len = (*mb_ptr2len)(p);
3604#else
3605 c = *p;
3606 len = 1;
3607#endif
3608 if (vim_strchr(slang->sl_syllable, c) == NULL)
3609 skip = FALSE; /* No, search for next syllable */
3610 else if (!skip)
3611 {
3612 ++cnt; /* Yes, count it */
3613 skip = TRUE; /* don't count following syllable chars */
3614 }
3615 }
3616 }
3617 return cnt;
3618}
3619
3620/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003621 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003622 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003623 */
3624 static int
3625set_sofo(lp, from, to)
3626 slang_T *lp;
3627 char_u *from;
3628 char_u *to;
3629{
3630 int i;
3631
3632#ifdef FEAT_MBYTE
3633 garray_T *gap;
3634 char_u *s;
3635 char_u *p;
3636 int c;
3637 int *inp;
3638
3639 if (has_mbyte)
3640 {
3641 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3642 * characters. The index is the low byte of the character.
3643 * The list contains from-to pairs with a terminating NUL.
3644 * sl_sal_first[] is used for latin1 "from" characters. */
3645 gap = &lp->sl_sal;
3646 ga_init2(gap, sizeof(int *), 1);
3647 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003648 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003649 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3650 gap->ga_len = 256;
3651
3652 /* First count the number of items for each list. Temporarily use
3653 * sl_sal_first[] for this. */
3654 for (p = from, s = to; *p != NUL && *s != NUL; )
3655 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003656 c = mb_cptr2char_adv(&p);
3657 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003658 if (c >= 256)
3659 ++lp->sl_sal_first[c & 0xff];
3660 }
3661 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003662 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003663
3664 /* Allocate the lists. */
3665 for (i = 0; i < 256; ++i)
3666 if (lp->sl_sal_first[i] > 0)
3667 {
3668 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3669 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003670 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003671 ((int **)gap->ga_data)[i] = (int *)p;
3672 *(int *)p = 0;
3673 }
3674
3675 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3676 * list. */
3677 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3678 for (p = from, s = to; *p != NUL && *s != NUL; )
3679 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003680 c = mb_cptr2char_adv(&p);
3681 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003682 if (c >= 256)
3683 {
3684 /* Append the from-to chars at the end of the list with
3685 * the low byte. */
3686 inp = ((int **)gap->ga_data)[c & 0xff];
3687 while (*inp != 0)
3688 ++inp;
3689 *inp++ = c; /* from char */
3690 *inp++ = i; /* to char */
3691 *inp++ = NUL; /* NUL at the end */
3692 }
3693 else
3694 /* mapping byte to char is done in sl_sal_first[] */
3695 lp->sl_sal_first[c] = i;
3696 }
3697 }
3698 else
3699#endif
3700 {
3701 /* mapping bytes to bytes is done in sl_sal_first[] */
3702 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003703 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003704
3705 for (i = 0; to[i] != NUL; ++i)
3706 lp->sl_sal_first[from[i]] = to[i];
3707 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3708 }
3709
Bram Moolenaar5195e452005-08-19 20:32:47 +00003710 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003711}
3712
3713/*
3714 * Fill the first-index table for "lp".
3715 */
3716 static void
3717set_sal_first(lp)
3718 slang_T *lp;
3719{
3720 salfirst_T *sfirst;
3721 int i;
3722 salitem_T *smp;
3723 int c;
3724 garray_T *gap = &lp->sl_sal;
3725
3726 sfirst = lp->sl_sal_first;
3727 for (i = 0; i < 256; ++i)
3728 sfirst[i] = -1;
3729 smp = (salitem_T *)gap->ga_data;
3730 for (i = 0; i < gap->ga_len; ++i)
3731 {
3732#ifdef FEAT_MBYTE
3733 if (has_mbyte)
3734 /* Use the lowest byte of the first character. For latin1 it's
3735 * the character, for other encodings it should differ for most
3736 * characters. */
3737 c = *smp[i].sm_lead_w & 0xff;
3738 else
3739#endif
3740 c = *smp[i].sm_lead;
3741 if (sfirst[c] == -1)
3742 {
3743 sfirst[c] = i;
3744#ifdef FEAT_MBYTE
3745 if (has_mbyte)
3746 {
3747 int n;
3748
3749 /* Make sure all entries with this byte are following each
3750 * other. Move the ones that are in the wrong position. Do
3751 * keep the same ordering! */
3752 while (i + 1 < gap->ga_len
3753 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3754 /* Skip over entry with same index byte. */
3755 ++i;
3756
3757 for (n = 1; i + n < gap->ga_len; ++n)
3758 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3759 {
3760 salitem_T tsal;
3761
3762 /* Move entry with same index byte after the entries
3763 * we already found. */
3764 ++i;
3765 --n;
3766 tsal = smp[i + n];
3767 mch_memmove(smp + i + 1, smp + i,
3768 sizeof(salitem_T) * n);
3769 smp[i] = tsal;
3770 }
3771 }
3772#endif
3773 }
3774 }
3775}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003776
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003777#ifdef FEAT_MBYTE
3778/*
3779 * Turn a multi-byte string into a wide character string.
3780 * Return it in allocated memory (NULL for out-of-memory)
3781 */
3782 static int *
3783mb_str2wide(s)
3784 char_u *s;
3785{
3786 int *res;
3787 char_u *p;
3788 int i = 0;
3789
3790 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3791 if (res != NULL)
3792 {
3793 for (p = s; *p != NUL; )
3794 res[i++] = mb_ptr2char_adv(&p);
3795 res[i] = NUL;
3796 }
3797 return res;
3798}
3799#endif
3800
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003801/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003802 * Read a tree from the .spl or .sug file.
3803 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
3804 * This is skipped when the tree has zero length.
3805 * Returns zero when OK, SP_ value for an error.
3806 */
3807 static int
3808spell_read_tree(fd, bytsp, idxsp, prefixtree, prefixcnt)
3809 FILE *fd;
3810 char_u **bytsp;
3811 idx_T **idxsp;
3812 int prefixtree; /* TRUE for the prefix tree */
3813 int prefixcnt; /* when "prefixtree" is TRUE: prefix count */
3814{
3815 int len;
3816 int idx;
3817 char_u *bp;
3818 idx_T *ip;
3819
3820 /* The tree size was computed when writing the file, so that we can
3821 * allocate it as one long block. <nodecount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003822 len = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003823 if (len < 0)
3824 return SP_TRUNCERROR;
3825 if (len > 0)
3826 {
3827 /* Allocate the byte array. */
3828 bp = lalloc((long_u)len, TRUE);
3829 if (bp == NULL)
3830 return SP_OTHERERROR;
3831 *bytsp = bp;
3832
3833 /* Allocate the index array. */
3834 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
3835 if (ip == NULL)
3836 return SP_OTHERERROR;
3837 *idxsp = ip;
3838
3839 /* Recursively read the tree and store it in the array. */
3840 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
3841 if (idx < 0)
3842 return idx;
3843 }
3844 return 0;
3845}
3846
3847/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003848 * Read one row of siblings from the spell file and store it in the byte array
3849 * "byts" and index array "idxs". Recursively read the children.
3850 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003851 * NOTE: The code here must match put_node()!
Bram Moolenaar51485f02005-06-04 21:55:20 +00003852 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003853 * Returns the index (>= 0) following the siblings.
3854 * Returns SP_TRUNCERROR if the file is shorter than expected.
3855 * Returns SP_FORMERROR if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003856 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003857 static idx_T
Bram Moolenaar4770d092006-01-12 23:22:24 +00003858read_tree_node(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003859 FILE *fd;
3860 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003861 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003862 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003863 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003864 int prefixtree; /* TRUE for reading PREFIXTREE */
3865 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003866{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003867 int len;
3868 int i;
3869 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003870 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003871 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003872 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003873#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003874
Bram Moolenaar51485f02005-06-04 21:55:20 +00003875 len = getc(fd); /* <siblingcount> */
3876 if (len <= 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003877 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003878
3879 if (startidx + len >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003880 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003881 byts[idx++] = len;
3882
3883 /* Read the byte values, flag/region bytes and shared indexes. */
3884 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003885 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003886 c = getc(fd); /* <byte> */
3887 if (c < 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003888 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003889 if (c <= BY_SPECIAL)
3890 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003891 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003892 {
3893 /* No flags, all regions. */
3894 idxs[idx] = 0;
3895 c = 0;
3896 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003897 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003898 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003899 if (prefixtree)
3900 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003901 /* Read the optional pflags byte, the prefix ID and the
3902 * condition nr. In idxs[] store the prefix ID in the low
3903 * byte, the condition index shifted up 8 bits, the flags
3904 * shifted up 24 bits. */
3905 if (c == BY_FLAGS)
3906 c = getc(fd) << 24; /* <pflags> */
3907 else
3908 c = 0;
3909
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003910 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003911
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003912 n = get2c(fd); /* <prefcondnr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003913 if (n >= maxprefcondnr)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003914 return SP_FORMERROR;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003915 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003916 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003917 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003918 {
3919 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003920 * idxs[] the flags go in the low two bytes, region above
3921 * that and prefix ID above the region. */
3922 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003923 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003924 if (c2 == BY_FLAGS2)
3925 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003926 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003927 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003928 if (c & WF_AFX)
3929 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003930 }
3931
Bram Moolenaar51485f02005-06-04 21:55:20 +00003932 idxs[idx] = c;
3933 c = 0;
3934 }
3935 else /* c == BY_INDEX */
3936 {
3937 /* <nodeidx> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003938 n = get3c(fd);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003939 if (n < 0 || n >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003940 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003941 idxs[idx] = n + SHARED_MASK;
3942 c = getc(fd); /* <xbyte> */
3943 }
3944 }
3945 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003946 }
3947
Bram Moolenaar51485f02005-06-04 21:55:20 +00003948 /* Recursively read the children for non-shared siblings.
3949 * Skip the end-of-word ones (zero byte value) and the shared ones (and
3950 * remove SHARED_MASK) */
3951 for (i = 1; i <= len; ++i)
3952 if (byts[startidx + i] != 0)
3953 {
3954 if (idxs[startidx + i] & SHARED_MASK)
3955 idxs[startidx + i] &= ~SHARED_MASK;
3956 else
3957 {
3958 idxs[startidx + i] = idx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003959 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003960 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003961 if (idx < 0)
3962 break;
3963 }
3964 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003965
Bram Moolenaar51485f02005-06-04 21:55:20 +00003966 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003967}
3968
3969/*
3970 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003971 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003972 */
3973 char_u *
3974did_set_spelllang(buf)
3975 buf_T *buf;
3976{
3977 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003978 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003979 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00003980 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003981 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003982 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003983 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003984 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003985 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003986 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003987 int len;
3988 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003989 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003990 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003991 char_u *use_region = NULL;
3992 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003993 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003994 int i, j;
3995 langp_T *lp, *lp2;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003996
3997 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003998 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003999
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004000 /* loop over comma separated language names. */
4001 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004002 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004003 /* Get one language name. */
4004 copy_option_part(&splp, lang, MAXWLEN, ",");
4005
Bram Moolenaar5482f332005-04-17 20:18:43 +00004006 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004007 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004008
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004009 /* If the name ends in ".spl" use it as the name of the spell file.
4010 * If there is a region name let "region" point to it and remove it
4011 * from the name. */
4012 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
4013 {
4014 filename = TRUE;
4015
Bram Moolenaarb6356332005-07-18 21:40:44 +00004016 /* Locate a region and remove it from the file name. */
4017 p = vim_strchr(gettail(lang), '_');
4018 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
4019 && !ASCII_ISALPHA(p[3]))
4020 {
4021 vim_strncpy(region_cp, p + 1, 2);
4022 mch_memmove(p, p + 3, len - (p - lang) - 2);
4023 len -= 3;
4024 region = region_cp;
4025 }
4026 else
4027 dont_use_region = TRUE;
4028
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004029 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004030 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4031 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004032 break;
4033 }
4034 else
4035 {
4036 filename = FALSE;
4037 if (len > 3 && lang[len - 3] == '_')
4038 {
4039 region = lang + len - 2;
4040 len -= 3;
4041 lang[len] = NUL;
4042 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004043 else
4044 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004045
4046 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004047 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4048 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004049 break;
4050 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004051
Bram Moolenaarb6356332005-07-18 21:40:44 +00004052 if (region != NULL)
4053 {
4054 /* If the region differs from what was used before then don't
4055 * use it for 'spellfile'. */
4056 if (use_region != NULL && STRCMP(region, use_region) != 0)
4057 dont_use_region = TRUE;
4058 use_region = region;
4059 }
4060
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004061 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004062 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004063 {
4064 if (filename)
4065 (void)spell_load_file(lang, lang, NULL, FALSE);
4066 else
4067 spell_load_lang(lang);
4068 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004069
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004070 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004071 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004072 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004073 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4074 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
4075 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004076 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004077 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004078 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004079 {
4080 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004081 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004082 if (c == REGION_ALL)
4083 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004084 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004085 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004086 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004087 /* This addition file is for other regions. */
4088 region_mask = 0;
4089 }
4090 else
4091 /* This is probably an error. Give a warning and
4092 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004093 smsg((char_u *)
4094 _("Warning: region %s not supported"),
4095 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004096 }
4097 else
4098 region_mask = 1 << c;
4099 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004100
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004101 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004102 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004103 if (ga_grow(&ga, 1) == FAIL)
4104 {
4105 ga_clear(&ga);
4106 return e_outofmem;
4107 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004108 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004109 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4110 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004111 use_midword(slang, buf);
4112 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004113 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004114 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004115 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004116 }
4117
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004118 /* round 0: load int_wordlist, if possible.
4119 * round 1: load first name in 'spellfile'.
4120 * round 2: load second name in 'spellfile.
4121 * etc. */
4122 spf = curbuf->b_p_spf;
4123 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004124 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004125 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004126 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004127 /* Internal wordlist, if there is one. */
4128 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004129 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004130 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004131 }
4132 else
4133 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004134 /* One entry in 'spellfile'. */
4135 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
4136 STRCAT(spf_name, ".spl");
4137
4138 /* If it was already found above then skip it. */
4139 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004140 {
4141 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
4142 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004143 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004144 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004145 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004146 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004147 }
4148
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004149 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004150 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4151 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004152 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004153 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004154 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004155 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004156 * region name, the region is ignored otherwise. for int_wordlist
4157 * use an arbitrary name. */
4158 if (round == 0)
4159 STRCPY(lang, "internal wordlist");
4160 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00004161 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004162 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004163 p = vim_strchr(lang, '.');
4164 if (p != NULL)
4165 *p = NUL; /* truncate at ".encoding.add" */
4166 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004167 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004168
4169 /* If one of the languages has NOBREAK we assume the addition
4170 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004171 if (slang != NULL && nobreak)
4172 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004173 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004174 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004175 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004176 region_mask = REGION_ALL;
4177 if (use_region != NULL && !dont_use_region)
4178 {
4179 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004180 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004181 if (c != REGION_ALL)
4182 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004183 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004184 /* This spell file is for other regions. */
4185 region_mask = 0;
4186 }
4187
4188 if (region_mask != 0)
4189 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004190 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
4191 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
4192 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004193 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4194 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004195 use_midword(slang, buf);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004196 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004197 }
4198 }
4199
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004200 /* Everything is fine, store the new b_langp value. */
4201 ga_clear(&buf->b_langp);
4202 buf->b_langp = ga;
4203
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004204 /* For each language figure out what language to use for sound folding and
4205 * REP items. If the language doesn't support it itself use another one
4206 * with the same name. E.g. for "en-math" use "en". */
4207 for (i = 0; i < ga.ga_len; ++i)
4208 {
4209 lp = LANGP_ENTRY(ga, i);
4210
4211 /* sound folding */
4212 if (lp->lp_slang->sl_sal.ga_len > 0)
4213 /* language does sound folding itself */
4214 lp->lp_sallang = lp->lp_slang;
4215 else
4216 /* find first similar language that does sound folding */
4217 for (j = 0; j < ga.ga_len; ++j)
4218 {
4219 lp2 = LANGP_ENTRY(ga, j);
4220 if (lp2->lp_slang->sl_sal.ga_len > 0
4221 && STRNCMP(lp->lp_slang->sl_name,
4222 lp2->lp_slang->sl_name, 2) == 0)
4223 {
4224 lp->lp_sallang = lp2->lp_slang;
4225 break;
4226 }
4227 }
4228
4229 /* REP items */
4230 if (lp->lp_slang->sl_rep.ga_len > 0)
4231 /* language has REP items itself */
4232 lp->lp_replang = lp->lp_slang;
4233 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004234 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004235 for (j = 0; j < ga.ga_len; ++j)
4236 {
4237 lp2 = LANGP_ENTRY(ga, j);
4238 if (lp2->lp_slang->sl_rep.ga_len > 0
4239 && STRNCMP(lp->lp_slang->sl_name,
4240 lp2->lp_slang->sl_name, 2) == 0)
4241 {
4242 lp->lp_replang = lp2->lp_slang;
4243 break;
4244 }
4245 }
4246 }
4247
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004248 return NULL;
4249}
4250
4251/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004252 * Clear the midword characters for buffer "buf".
4253 */
4254 static void
4255clear_midword(buf)
4256 buf_T *buf;
4257{
4258 vim_memset(buf->b_spell_ismw, 0, 256);
4259#ifdef FEAT_MBYTE
4260 vim_free(buf->b_spell_ismw_mb);
4261 buf->b_spell_ismw_mb = NULL;
4262#endif
4263}
4264
4265/*
4266 * Use the "sl_midword" field of language "lp" for buffer "buf".
4267 * They add up to any currently used midword characters.
4268 */
4269 static void
4270use_midword(lp, buf)
4271 slang_T *lp;
4272 buf_T *buf;
4273{
4274 char_u *p;
4275
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004276 if (lp->sl_midword == NULL) /* there aren't any */
4277 return;
4278
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004279 for (p = lp->sl_midword; *p != NUL; )
4280#ifdef FEAT_MBYTE
4281 if (has_mbyte)
4282 {
4283 int c, l, n;
4284 char_u *bp;
4285
4286 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004287 l = (*mb_ptr2len)(p);
4288 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004289 buf->b_spell_ismw[c] = TRUE;
4290 else if (buf->b_spell_ismw_mb == NULL)
4291 /* First multi-byte char in "b_spell_ismw_mb". */
4292 buf->b_spell_ismw_mb = vim_strnsave(p, l);
4293 else
4294 {
4295 /* Append multi-byte chars to "b_spell_ismw_mb". */
4296 n = STRLEN(buf->b_spell_ismw_mb);
4297 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
4298 if (bp != NULL)
4299 {
4300 vim_free(buf->b_spell_ismw_mb);
4301 buf->b_spell_ismw_mb = bp;
4302 vim_strncpy(bp + n, p, l);
4303 }
4304 }
4305 p += l;
4306 }
4307 else
4308#endif
4309 buf->b_spell_ismw[*p++] = TRUE;
4310}
4311
4312/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004313 * Find the region "region[2]" in "rp" (points to "sl_regions").
4314 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00004315 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004316 */
4317 static int
4318find_region(rp, region)
4319 char_u *rp;
4320 char_u *region;
4321{
4322 int i;
4323
4324 for (i = 0; ; i += 2)
4325 {
4326 if (rp[i] == NUL)
4327 return REGION_ALL;
4328 if (rp[i] == region[0] && rp[i + 1] == region[1])
4329 break;
4330 }
4331 return i / 2;
4332}
4333
4334/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004335 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004336 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00004337 * Word WF_ONECAP
4338 * W WORD WF_ALLCAP
4339 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004340 */
4341 static int
4342captype(word, end)
4343 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004344 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004345{
4346 char_u *p;
4347 int c;
4348 int firstcap;
4349 int allcap;
4350 int past_second = FALSE; /* past second word char */
4351
4352 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004353 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004354 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004355 return 0; /* only non-word characters, illegal word */
4356#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00004357 if (has_mbyte)
4358 c = mb_ptr2char_adv(&p);
4359 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004360#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00004361 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004362 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004363
4364 /*
4365 * Need to check all letters to find a word with mixed upper/lower.
4366 * But a word with an upper char only at start is a ONECAP.
4367 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004368 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004369 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004370 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004371 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004372 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004373 {
4374 /* UUl -> KEEPCAP */
4375 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004376 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004377 allcap = FALSE;
4378 }
4379 else if (!allcap)
4380 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004381 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004382 past_second = TRUE;
4383 }
4384
4385 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004386 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004387 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004388 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004389 return 0;
4390}
4391
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004392/*
4393 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
4394 * capital. So that make_case_word() can turn WOrd into Word.
4395 * Add ALLCAP for "WOrD".
4396 */
4397 static int
4398badword_captype(word, end)
4399 char_u *word;
4400 char_u *end;
4401{
4402 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004403 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004404 int l, u;
4405 int first;
4406 char_u *p;
4407
4408 if (flags & WF_KEEPCAP)
4409 {
4410 /* Count the number of UPPER and lower case letters. */
4411 l = u = 0;
4412 first = FALSE;
4413 for (p = word; p < end; mb_ptr_adv(p))
4414 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004415 c = PTR2CHAR(p);
4416 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004417 {
4418 ++u;
4419 if (p == word)
4420 first = TRUE;
4421 }
4422 else
4423 ++l;
4424 }
4425
4426 /* If there are more UPPER than lower case letters suggest an
4427 * ALLCAP word. Otherwise, if the first letter is UPPER then
4428 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4429 * require three upper case letters. */
4430 if (u > l && u > 2)
4431 flags |= WF_ALLCAP;
4432 else if (first)
4433 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004434
4435 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
4436 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004437 }
4438 return flags;
4439}
4440
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004441# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
4442/*
4443 * Free all languages.
4444 */
4445 void
4446spell_free_all()
4447{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004448 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004449 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004450 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004451
4452 /* Go through all buffers and handle 'spelllang'. */
4453 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4454 ga_clear(&buf->b_langp);
4455
4456 while (first_lang != NULL)
4457 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004458 slang = first_lang;
4459 first_lang = slang->sl_next;
4460 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004461 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004462
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004463 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004464 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004465 /* Delete the internal wordlist and its .spl file */
4466 mch_remove(int_wordlist);
4467 int_wordlist_spl(fname);
4468 mch_remove(fname);
4469 vim_free(int_wordlist);
4470 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004471 }
4472
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004473 init_spell_chartab();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004474
4475 vim_free(repl_to);
4476 repl_to = NULL;
4477 vim_free(repl_from);
4478 repl_from = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004479}
4480# endif
4481
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004482# if defined(FEAT_MBYTE) || defined(PROTO)
4483/*
4484 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004485 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004486 */
4487 void
4488spell_reload()
4489{
4490 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004491 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004492
Bram Moolenaarea408852005-06-25 22:49:46 +00004493 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004494 init_spell_chartab();
4495
4496 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004497 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004498
4499 /* Go through all buffers and handle 'spelllang'. */
4500 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4501 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004502 /* Only load the wordlists when 'spelllang' is set and there is a
4503 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004504 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004505 {
4506 FOR_ALL_WINDOWS(wp)
4507 if (wp->w_buffer == buf && wp->w_p_spell)
4508 {
4509 (void)did_set_spelllang(buf);
4510# ifdef FEAT_WINDOWS
4511 break;
4512# endif
4513 }
4514 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004515 }
4516}
4517# endif
4518
Bram Moolenaarb765d632005-06-07 21:00:02 +00004519/*
4520 * Reload the spell file "fname" if it's loaded.
4521 */
4522 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004523spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004524 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004525 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004526{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004527 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004528 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004529
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004530 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004531 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004532 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004533 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004534 slang_clear(slang);
4535 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004536 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004537 slang_clear(slang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004538 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004539 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004540 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004541 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004542
4543 /* When "zg" was used and the file wasn't loaded yet, should redo
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004544 * 'spelllang' to load it now. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004545 if (added_word && !didit)
4546 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004547}
4548
4549
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004550/*
4551 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004552 */
4553
Bram Moolenaar51485f02005-06-04 21:55:20 +00004554#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004555 and .dic file. */
4556/*
4557 * Main structure to store the contents of a ".aff" file.
4558 */
4559typedef struct afffile_S
4560{
4561 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004562 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaar371baa92005-12-29 22:43:53 +00004563 unsigned af_rare; /* RARE ID for rare word */
4564 unsigned af_keepcase; /* KEEPCASE ID for keep-case word */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004565 unsigned af_bad; /* BAD ID for banned word */
4566 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004567 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004568 unsigned af_nosuggest; /* NOSUGGEST ID */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004569 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004570 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4571 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004572 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004573} afffile_T;
4574
Bram Moolenaar6de68532005-08-24 22:08:48 +00004575#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004576#define AFT_LONG 1 /* flags are two characters */
4577#define AFT_CAPLONG 2 /* flags are one or two characters */
4578#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004579
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004580typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004581/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4582struct affentry_S
4583{
4584 affentry_T *ae_next; /* next affix with same name/number */
4585 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4586 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004587 char_u *ae_cond; /* condition (NULL for ".") */
4588 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004589 char_u ae_rare; /* rare affix */
4590 char_u ae_nocomp; /* word with affix not compoundable */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004591};
4592
Bram Moolenaar6de68532005-08-24 22:08:48 +00004593#ifdef FEAT_MBYTE
4594# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4595#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004596# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004597#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004598
Bram Moolenaar51485f02005-06-04 21:55:20 +00004599/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4600typedef struct affheader_S
4601{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004602 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4603 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4604 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004605 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004606 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004607 affentry_T *ah_first; /* first affix entry */
4608} affheader_T;
4609
4610#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4611
Bram Moolenaar6de68532005-08-24 22:08:48 +00004612/* Flag used in compound items. */
4613typedef struct compitem_S
4614{
4615 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4616 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4617 int ci_newID; /* affix ID after renumbering. */
4618} compitem_T;
4619
4620#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4621
Bram Moolenaar51485f02005-06-04 21:55:20 +00004622/*
4623 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004624 * the need to keep track of each allocated thing, everything is freed all at
4625 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004626 */
4627#define SBLOCKSIZE 16000 /* size of sb_data */
4628typedef struct sblock_S sblock_T;
4629struct sblock_S
4630{
4631 sblock_T *sb_next; /* next block in list */
4632 int sb_used; /* nr of bytes already in use */
4633 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004634};
4635
4636/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004637 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004638 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004639typedef struct wordnode_S wordnode_T;
4640struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004641{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004642 union /* shared to save space */
4643 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004644 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004645 int index; /* index in written nodes (valid after first
4646 round) */
4647 } wn_u1;
4648 union /* shared to save space */
4649 {
4650 wordnode_T *next; /* next node with same hash key */
4651 wordnode_T *wnode; /* parent node that will write this node */
4652 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004653 wordnode_T *wn_child; /* child (next byte in word) */
4654 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4655 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004656 int wn_refs; /* Nr. of references to this node. Only
4657 relevant for first node in a list of
4658 siblings, in following siblings it is
4659 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004660 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004661
4662 /* Info for when "wn_byte" is NUL.
4663 * In PREFIXTREE "wn_region" is used for the prefcondnr.
4664 * In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
4665 * "wn_region" the LSW of the wordnr. */
4666 char_u wn_affixID; /* supported/required prefix ID or 0 */
4667 short_u wn_flags; /* WF_ flags */
4668 short wn_region; /* region mask */
4669
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004670#ifdef SPELL_PRINTTREE
4671 int wn_nr; /* sequence nr for printing */
4672#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004673};
4674
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004675#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4676
Bram Moolenaar51485f02005-06-04 21:55:20 +00004677#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004678
Bram Moolenaar51485f02005-06-04 21:55:20 +00004679/*
4680 * Info used while reading the spell files.
4681 */
4682typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004683{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004684 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004685 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004686
Bram Moolenaar51485f02005-06-04 21:55:20 +00004687 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004688 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004689
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004690 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004691
Bram Moolenaar4770d092006-01-12 23:22:24 +00004692 long si_sugtree; /* creating the soundfolding trie */
4693
Bram Moolenaar51485f02005-06-04 21:55:20 +00004694 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004695 long si_blocks_cnt; /* memory blocks allocated */
4696 long si_compress_cnt; /* words to add before lowering
4697 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004698 wordnode_T *si_first_free; /* List of nodes that have been freed during
4699 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004700 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004701#ifdef SPELL_PRINTTREE
4702 int si_wordnode_nr; /* sequence nr for nodes */
4703#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004704 buf_T *si_spellbuf; /* buffer used to store soundfold word table */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004705
Bram Moolenaar51485f02005-06-04 21:55:20 +00004706 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004707 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004708 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004709 int si_region; /* region mask */
4710 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004711 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004712 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004713 int si_msg_count; /* number of words added since last message */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004714 int si_region_count; /* number of regions supported (1 when there
4715 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004716 char_u si_region_name[16]; /* region names; used only if
4717 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004718
4719 garray_T si_rep; /* list of fromto_T entries from REP lines */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004720 garray_T si_repsal; /* list of fromto_T entries from REPSAL lines */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004721 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004722 char_u *si_sofofr; /* SOFOFROM text */
4723 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004724 int si_nosugfile; /* NOSUGFILE item found */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004725 int si_nosplitsugs; /* NOSPLITSUGS item found */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004726 int si_followup; /* soundsalike: ? */
4727 int si_collapse; /* soundsalike: ? */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004728 hashtab_T si_commonwords; /* hashtable for common words */
4729 time_t si_sugtime; /* timestamp for .sug file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004730 int si_rem_accents; /* soundsalike: remove accents */
4731 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004732 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004733 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004734 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004735 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004736 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004737 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004738 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004739 garray_T si_prefcond; /* table with conditions for postponed
4740 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004741 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004742 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004743} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004744
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004745static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004746static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
4747static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
4748static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004749static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004750static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
4751static void aff_check_number __ARGS((int spinval, int affval, char *name));
4752static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004753static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004754static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
4755static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00004756static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004757static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004758static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004759static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004760static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004761static 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 +00004762static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
4763static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
4764static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004765static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004766static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004767static 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 +00004768static 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 +00004769static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004770static int deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004771static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
4772static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
4773static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004774static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004775static void put_sugtime __ARGS((spellinfo_T *spin, FILE *fd));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004776static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00004777static void clear_node __ARGS((wordnode_T *node));
4778static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004779static void spell_make_sugfile __ARGS((spellinfo_T *spin, char_u *wfname));
4780static int sug_filltree __ARGS((spellinfo_T *spin, slang_T *slang));
4781static int sug_maketable __ARGS((spellinfo_T *spin));
4782static int sug_filltable __ARGS((spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap));
4783static int offset2bytes __ARGS((int nr, char_u *buf));
4784static int bytes2offset __ARGS((char_u **pp));
4785static void sug_write __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004786static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004787static void spell_message __ARGS((spellinfo_T *spin, char_u *str));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004788static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004789
Bram Moolenaar53805d12005-08-01 07:08:33 +00004790/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
4791 * but it must be negative to indicate the prefix tree to tree_add_word().
4792 * Use a negative number with the lower 8 bits zero. */
4793#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004794
Bram Moolenaar5195e452005-08-19 20:32:47 +00004795/*
4796 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
4797 */
4798static long compress_start = 30000; /* memory / SBLOCKSIZE */
4799static long compress_inc = 100; /* memory / SBLOCKSIZE */
4800static long compress_added = 500000; /* word count */
4801
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004802#ifdef SPELL_PRINTTREE
4803/*
4804 * For debugging the tree code: print the current tree in a (more or less)
4805 * readable format, so that we can see what happens when adding a word and/or
4806 * compressing the tree.
4807 * Based on code from Olaf Seibert.
4808 */
4809#define PRINTLINESIZE 1000
4810#define PRINTWIDTH 6
4811
4812#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
4813 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
4814
4815static char line1[PRINTLINESIZE];
4816static char line2[PRINTLINESIZE];
4817static char line3[PRINTLINESIZE];
4818
4819 static void
4820spell_clear_flags(wordnode_T *node)
4821{
4822 wordnode_T *np;
4823
4824 for (np = node; np != NULL; np = np->wn_sibling)
4825 {
4826 np->wn_u1.index = FALSE;
4827 spell_clear_flags(np->wn_child);
4828 }
4829}
4830
4831 static void
4832spell_print_node(wordnode_T *node, int depth)
4833{
4834 if (node->wn_u1.index)
4835 {
4836 /* Done this node before, print the reference. */
4837 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
4838 PRINTSOME(line2, depth, " ", 0, 0);
4839 PRINTSOME(line3, depth, " ", 0, 0);
4840 msg(line1);
4841 msg(line2);
4842 msg(line3);
4843 }
4844 else
4845 {
4846 node->wn_u1.index = TRUE;
4847
4848 if (node->wn_byte != NUL)
4849 {
4850 if (node->wn_child != NULL)
4851 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
4852 else
4853 /* Cannot happen? */
4854 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
4855 }
4856 else
4857 PRINTSOME(line1, depth, " $ ", 0, 0);
4858
4859 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
4860
4861 if (node->wn_sibling != NULL)
4862 PRINTSOME(line3, depth, " | ", 0, 0);
4863 else
4864 PRINTSOME(line3, depth, " ", 0, 0);
4865
4866 if (node->wn_byte == NUL)
4867 {
4868 msg(line1);
4869 msg(line2);
4870 msg(line3);
4871 }
4872
4873 /* do the children */
4874 if (node->wn_byte != NUL && node->wn_child != NULL)
4875 spell_print_node(node->wn_child, depth + 1);
4876
4877 /* do the siblings */
4878 if (node->wn_sibling != NULL)
4879 {
4880 /* get rid of all parent details except | */
4881 STRCPY(line1, line3);
4882 STRCPY(line2, line3);
4883 spell_print_node(node->wn_sibling, depth);
4884 }
4885 }
4886}
4887
4888 static void
4889spell_print_tree(wordnode_T *root)
4890{
4891 if (root != NULL)
4892 {
4893 /* Clear the "wn_u1.index" fields, used to remember what has been
4894 * done. */
4895 spell_clear_flags(root);
4896
4897 /* Recursively print the tree. */
4898 spell_print_node(root, 0);
4899 }
4900}
4901#endif /* SPELL_PRINTTREE */
4902
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004903/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004904 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00004905 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004906 */
4907 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004908spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004909 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004910 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004911{
4912 FILE *fd;
4913 afffile_T *aff;
4914 char_u rline[MAXLINELEN];
4915 char_u *line;
4916 char_u *pc = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004917#define MAXITEMCNT 30
Bram Moolenaar8db73182005-06-17 21:51:16 +00004918 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004919 int itemcnt;
4920 char_u *p;
4921 int lnum = 0;
4922 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004923 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004924 int aff_todo = 0;
4925 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004926 char_u *low = NULL;
4927 char_u *fol = NULL;
4928 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004929 int do_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004930 int do_repsal;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004931 int do_sal;
4932 int do_map;
4933 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004934 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004935 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004936 int compminlen = 0; /* COMPOUNDMIN value */
4937 int compsylmax = 0; /* COMPOUNDSYLMAX value */
4938 int compmax = 0; /* COMPOUNDMAX value */
4939 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDFLAGS
4940 concatenated */
4941 char_u *midword = NULL; /* MIDWORD value */
4942 char_u *syllable = NULL; /* SYLLABLE value */
4943 char_u *sofofrom = NULL; /* SOFOFROM value */
4944 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004945
Bram Moolenaar51485f02005-06-04 21:55:20 +00004946 /*
4947 * Open the file.
4948 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004949 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004950 if (fd == NULL)
4951 {
4952 EMSG2(_(e_notopen), fname);
4953 return NULL;
4954 }
4955
Bram Moolenaar4770d092006-01-12 23:22:24 +00004956 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s ..."), fname);
4957 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004958
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004959 /* Only do REP lines when not done in another .aff file already. */
4960 do_rep = spin->si_rep.ga_len == 0;
4961
Bram Moolenaar4770d092006-01-12 23:22:24 +00004962 /* Only do REPSAL lines when not done in another .aff file already. */
4963 do_repsal = spin->si_repsal.ga_len == 0;
4964
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004965 /* Only do SAL lines when not done in another .aff file already. */
4966 do_sal = spin->si_sal.ga_len == 0;
4967
4968 /* Only do MAP lines when not done in another .aff file already. */
4969 do_map = spin->si_map.ga_len == 0;
4970
Bram Moolenaar51485f02005-06-04 21:55:20 +00004971 /*
4972 * Allocate and init the afffile_T structure.
4973 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004974 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004975 if (aff == NULL)
4976 return NULL;
4977 hash_init(&aff->af_pref);
4978 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004979 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004980
4981 /*
4982 * Read all the lines in the file one by one.
4983 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004984 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004985 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004986 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004987 ++lnum;
4988
4989 /* Skip comment lines. */
4990 if (*rline == '#')
4991 continue;
4992
4993 /* Convert from "SET" to 'encoding' when needed. */
4994 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004995#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004996 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004997 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004998 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004999 if (pc == NULL)
5000 {
5001 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5002 fname, lnum, rline);
5003 continue;
5004 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005005 line = pc;
5006 }
5007 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005008#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005009 {
5010 pc = NULL;
5011 line = rline;
5012 }
5013
5014 /* Split the line up in white separated items. Put a NUL after each
5015 * item. */
5016 itemcnt = 0;
5017 for (p = line; ; )
5018 {
5019 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
5020 ++p;
5021 if (*p == NUL)
5022 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00005023 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005024 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005025 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005026 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005027 ++p;
5028 if (*p == NUL)
5029 break;
5030 *p++ = NUL;
5031 }
5032
5033 /* Handle non-empty lines. */
5034 if (itemcnt > 0)
5035 {
5036 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
5037 && aff->af_enc == NULL)
5038 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005039#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005040 /* Setup for conversion from "ENC" to 'encoding'. */
5041 aff->af_enc = enc_canonize(items[1]);
5042 if (aff->af_enc != NULL && !spin->si_ascii
5043 && convert_setup(&spin->si_conv, aff->af_enc,
5044 p_enc) == FAIL)
5045 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
5046 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005047 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005048#else
5049 smsg((char_u *)_("Conversion in %s not supported"), fname);
5050#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005051 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005052 else if (STRCMP(items[0], "FLAG") == 0 && itemcnt == 2
5053 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005054 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005055 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005056 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005057 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005058 aff->af_flagtype = AFT_NUM;
5059 else if (STRCMP(items[1], "caplong") == 0)
5060 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005061 else
5062 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
5063 fname, lnum, items[1]);
Bram Moolenaar371baa92005-12-29 22:43:53 +00005064 if (aff->af_rare != 0
5065 || aff->af_keepcase != 0
5066 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005067 || aff->af_needaffix != 0
5068 || aff->af_needcomp != 0
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005069 || aff->af_nosuggest != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005070 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00005071 || aff->af_suff.ht_used > 0
5072 || aff->af_pref.ht_used > 0)
5073 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
5074 fname, lnum, items[1]);
5075 }
5076 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2
5077 && midword == NULL)
5078 {
5079 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005080 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005081 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005082 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005083 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005084 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005085 /* TODO: remove "RAR" later */
5086 else if ((STRCMP(items[0], "RAR") == 0
5087 || STRCMP(items[0], "RARE") == 0) && itemcnt == 2
5088 && aff->af_rare == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005089 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005090 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005091 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005092 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005093 /* TODO: remove "KEP" later */
5094 else if ((STRCMP(items[0], "KEP") == 0
5095 || STRCMP(items[0], "KEEPCASE") == 0) && itemcnt == 2
5096 && aff->af_keepcase == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005097 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005098 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005099 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005100 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00005101 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
5102 && aff->af_bad == 0)
5103 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005104 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
5105 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005106 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005107 else if (STRCMP(items[0], "NEEDAFFIX") == 0 && itemcnt == 2
5108 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005109 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005110 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
5111 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005112 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005113 else if (STRCMP(items[0], "NOSUGGEST") == 0 && itemcnt == 2
5114 && aff->af_nosuggest == 0)
5115 {
5116 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
5117 fname, lnum);
5118 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005119 else if (STRCMP(items[0], "NEEDCOMPOUND") == 0 && itemcnt == 2
5120 && aff->af_needcomp == 0)
5121 {
5122 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
5123 fname, lnum);
5124 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005125 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005126 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005127 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005128 /* Turn flag "c" into COMPOUNDFLAGS compatible string "c+",
5129 * "Na" into "Na+", "1234" into "1234+". */
5130 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005131 if (p != NULL)
5132 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005133 STRCPY(p, items[1]);
5134 STRCAT(p, "+");
5135 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005136 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005137 }
5138 else if (STRCMP(items[0], "COMPOUNDFLAGS") == 0 && itemcnt == 2)
5139 {
5140 /* Concatenate this string to previously defined ones, using a
5141 * slash to separate them. */
5142 l = STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005143 if (compflags != NULL)
5144 l += STRLEN(compflags) + 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005145 p = getroom(spin, l, FALSE);
5146 if (p != NULL)
5147 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005148 if (compflags != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005149 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005150 STRCPY(p, compflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005151 STRCAT(p, "/");
5152 }
5153 STRCAT(p, items[1]);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005154 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005155 }
5156 }
5157 else if (STRCMP(items[0], "COMPOUNDMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005158 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005159 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005160 compmax = atoi((char *)items[1]);
5161 if (compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005162 smsg((char_u *)_("Wrong COMPOUNDMAX value in %s line %d: %s"),
5163 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005164 }
5165 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005166 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005167 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005168 compminlen = atoi((char *)items[1]);
5169 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005170 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
5171 fname, lnum, items[1]);
5172 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005173 else if (STRCMP(items[0], "COMPOUNDSYLMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005174 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005175 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005176 compsylmax = atoi((char *)items[1]);
5177 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005178 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
5179 fname, lnum, items[1]);
5180 }
5181 else if (STRCMP(items[0], "SYLLABLE") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005182 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005183 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005184 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005185 }
Bram Moolenaar78622822005-08-23 21:00:13 +00005186 else if (STRCMP(items[0], "NOBREAK") == 0 && itemcnt == 1)
5187 {
5188 spin->si_nobreak = TRUE;
5189 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005190 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
5191 {
5192 spin->si_nosplitsugs = TRUE;
5193 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005194 else if (STRCMP(items[0], "NOSUGFILE") == 0 && itemcnt == 1)
5195 {
5196 spin->si_nosugfile = TRUE;
5197 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005198 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
5199 {
5200 aff->af_pfxpostpone = TRUE;
5201 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005202 else if ((STRCMP(items[0], "PFX") == 0
5203 || STRCMP(items[0], "SFX") == 0)
5204 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005205 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005206 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005207 int lasti = 4;
5208 char_u key[AH_KEY_LEN];
5209
5210 if (*items[0] == 'P')
5211 tp = &aff->af_pref;
5212 else
5213 tp = &aff->af_suff;
5214
5215 /* Myspell allows the same affix name to be used multiple
5216 * times. The affix files that do this have an undocumented
5217 * "S" flag on all but the last block, thus we check for that
5218 * and store it in ah_follows. */
5219 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
5220 hi = hash_find(tp, key);
5221 if (!HASHITEM_EMPTY(hi))
5222 {
5223 cur_aff = HI2AH(hi);
5224 if (cur_aff->ah_combine != (*items[2] == 'Y'))
5225 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
5226 fname, lnum, items[1]);
5227 if (!cur_aff->ah_follows)
5228 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
5229 fname, lnum, items[1]);
5230 }
5231 else
5232 {
5233 /* New affix letter. */
5234 cur_aff = (affheader_T *)getroom(spin,
5235 sizeof(affheader_T), TRUE);
5236 if (cur_aff == NULL)
5237 break;
5238 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
5239 fname, lnum);
5240 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
5241 break;
5242 if (cur_aff->ah_flag == aff->af_bad
Bram Moolenaar371baa92005-12-29 22:43:53 +00005243 || cur_aff->ah_flag == aff->af_rare
5244 || cur_aff->ah_flag == aff->af_keepcase
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005245 || cur_aff->ah_flag == aff->af_needaffix
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005246 || cur_aff->ah_flag == aff->af_nosuggest
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005247 || cur_aff->ah_flag == aff->af_needcomp)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005248 smsg((char_u *)_("Affix also used for BAD/RARE/KEEPCASE/NEEDAFFIX/NEEDCOMPOUND/NOSUGGEST in %s line %d: %s"),
Bram Moolenaar95529562005-08-25 21:21:38 +00005249 fname, lnum, items[1]);
5250 STRCPY(cur_aff->ah_key, items[1]);
5251 hash_add(tp, cur_aff->ah_key);
5252
5253 cur_aff->ah_combine = (*items[2] == 'Y');
5254 }
5255
5256 /* Check for the "S" flag, which apparently means that another
5257 * block with the same affix name is following. */
5258 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
5259 {
5260 ++lasti;
5261 cur_aff->ah_follows = TRUE;
5262 }
5263 else
5264 cur_aff->ah_follows = FALSE;
5265
Bram Moolenaar8db73182005-06-17 21:51:16 +00005266 /* Myspell allows extra text after the item, but that might
5267 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00005268 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar8db73182005-06-17 21:51:16 +00005269 smsg((char_u *)_("Trailing text in %s line %d: %s"),
5270 fname, lnum, items[4]);
5271
Bram Moolenaar95529562005-08-25 21:21:38 +00005272 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005273 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
5274 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005275
Bram Moolenaar95529562005-08-25 21:21:38 +00005276 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005277 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005278 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005279 {
5280 /* Use a new number in the .spl file later, to be able
5281 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005282 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005283 cur_aff->ah_newID = ++spin->si_newprefID;
5284
5285 /* We only really use ah_newID if the prefix is
5286 * postponed. We know that only after handling all
5287 * the items. */
5288 did_postpone_prefix = FALSE;
5289 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005290 else
5291 /* Did use the ID in a previous block. */
5292 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005293 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005294
Bram Moolenaar51485f02005-06-04 21:55:20 +00005295 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005296 }
5297 else if ((STRCMP(items[0], "PFX") == 0
5298 || STRCMP(items[0], "SFX") == 0)
5299 && aff_todo > 0
5300 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005301 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005302 {
5303 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005304 int rare = FALSE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005305 int nocomp = FALSE;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005306 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005307 int lasti = 5;
5308
Bram Moolenaar5195e452005-08-19 20:32:47 +00005309 /* Check for "rare" and "nocomp" after the other info. */
5310 while (itemcnt > lasti)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005311 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00005312 if (!rare && STRICMP(items[lasti], "rare") == 0)
5313 {
5314 rare = TRUE;
5315 ++lasti;
5316 }
5317 else if (!nocomp && STRICMP(items[lasti], "nocomp") == 0)
5318 {
5319 nocomp = TRUE;
5320 ++lasti;
5321 }
5322 else
5323 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005324 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005325
Bram Moolenaar8db73182005-06-17 21:51:16 +00005326 /* Myspell allows extra text after the item, but that might
5327 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005328 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005329 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005330
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005331 /* New item for an affix letter. */
5332 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005333 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005334 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005335 if (aff_entry == NULL)
5336 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005337 aff_entry->ae_rare = rare;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005338 aff_entry->ae_nocomp = nocomp;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005339
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005340 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005341 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005342 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005343 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005344
Bram Moolenaar51485f02005-06-04 21:55:20 +00005345 /* Don't use an affix entry with non-ASCII characters when
5346 * "spin->si_ascii" is TRUE. */
5347 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005348 || has_non_ascii(aff_entry->ae_add)))
5349 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00005350 aff_entry->ae_next = cur_aff->ah_first;
5351 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005352
5353 if (STRCMP(items[4], ".") != 0)
5354 {
5355 char_u buf[MAXLINELEN];
5356
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005357 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005358 if (*items[0] == 'P')
5359 sprintf((char *)buf, "^%s", items[4]);
5360 else
5361 sprintf((char *)buf, "%s$", items[4]);
5362 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005363 RE_MAGIC + RE_STRING + RE_STRICT);
5364 if (aff_entry->ae_prog == NULL)
5365 smsg((char_u *)_("Broken condition in %s line %d: %s"),
5366 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005367 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005368
5369 /* For postponed prefixes we need an entry in si_prefcond
5370 * for the condition. Use an existing one if possible. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005371 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005372 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005373 /* When the chop string is one lower-case letter and
5374 * the add string ends in the upper-case letter we set
5375 * the "upper" flag, clear "ae_chop" and remove the
5376 * letters from "ae_add". The condition must either
5377 * be empty or start with the same letter. */
5378 if (aff_entry->ae_chop != NULL
5379 && aff_entry->ae_add != NULL
5380#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005381 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00005382 aff_entry->ae_chop)] == NUL
5383#else
5384 && aff_entry->ae_chop[1] == NUL
5385#endif
5386 )
5387 {
5388 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005389
Bram Moolenaar53805d12005-08-01 07:08:33 +00005390 c = PTR2CHAR(aff_entry->ae_chop);
5391 c_up = SPELL_TOUPPER(c);
5392 if (c_up != c
5393 && (aff_entry->ae_cond == NULL
5394 || PTR2CHAR(aff_entry->ae_cond) == c))
5395 {
5396 p = aff_entry->ae_add
5397 + STRLEN(aff_entry->ae_add);
5398 mb_ptr_back(aff_entry->ae_add, p);
5399 if (PTR2CHAR(p) == c_up)
5400 {
5401 upper = TRUE;
5402 aff_entry->ae_chop = NULL;
5403 *p = NUL;
5404
5405 /* The condition is matched with the
5406 * actual word, thus must check for the
5407 * upper-case letter. */
5408 if (aff_entry->ae_cond != NULL)
5409 {
5410 char_u buf[MAXLINELEN];
5411#ifdef FEAT_MBYTE
5412 if (has_mbyte)
5413 {
5414 onecap_copy(items[4], buf, TRUE);
5415 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005416 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005417 }
5418 else
5419#endif
5420 *aff_entry->ae_cond = c_up;
5421 if (aff_entry->ae_cond != NULL)
5422 {
5423 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005424 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005425 vim_free(aff_entry->ae_prog);
5426 aff_entry->ae_prog = vim_regcomp(
5427 buf, RE_MAGIC + RE_STRING);
5428 }
5429 }
5430 }
5431 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005432 }
5433
Bram Moolenaar53805d12005-08-01 07:08:33 +00005434 if (aff_entry->ae_chop == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005435 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005436 int idx;
5437 char_u **pp;
5438 int n;
5439
Bram Moolenaar6de68532005-08-24 22:08:48 +00005440 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005441 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
5442 --idx)
5443 {
5444 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
5445 if (str_equal(p, aff_entry->ae_cond))
5446 break;
5447 }
5448 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5449 {
5450 /* Not found, add a new condition. */
5451 idx = spin->si_prefcond.ga_len++;
5452 pp = ((char_u **)spin->si_prefcond.ga_data)
5453 + idx;
5454 if (aff_entry->ae_cond == NULL)
5455 *pp = NULL;
5456 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005457 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00005458 aff_entry->ae_cond);
5459 }
5460
5461 /* Add the prefix to the prefix tree. */
5462 if (aff_entry->ae_add == NULL)
5463 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005464 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005465 p = aff_entry->ae_add;
5466 /* PFX_FLAGS is a negative number, so that
5467 * tree_add_word() knows this is the prefix tree. */
5468 n = PFX_FLAGS;
5469 if (rare)
5470 n |= WFP_RARE;
5471 if (!cur_aff->ah_combine)
5472 n |= WFP_NC;
5473 if (upper)
5474 n |= WFP_UP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005475 tree_add_word(spin, p, spin->si_prefroot, n,
5476 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005477 did_postpone_prefix = TRUE;
5478 }
5479
5480 /* Didn't actually use ah_newID, backup si_newprefID. */
5481 if (aff_todo == 0 && !did_postpone_prefix)
5482 {
5483 --spin->si_newprefID;
5484 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005485 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005486 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005487 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005488 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005489 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2
5490 && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005491 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005492 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005493 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005494 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2
5495 && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005496 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005497 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005498 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005499 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2
5500 && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005501 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005502 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005503 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005504 else if ((STRCMP(items[0], "REP") == 0
5505 || STRCMP(items[0], "REPSAL") == 0)
5506 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005507 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005508 /* Ignore REP/REPSAL count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005509 if (!isdigit(*items[1]))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005510 smsg((char_u *)_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005511 fname, lnum);
5512 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005513 else if ((STRCMP(items[0], "REP") == 0
5514 || STRCMP(items[0], "REPSAL") == 0)
5515 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005516 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005517 /* REP/REPSAL item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005518 /* Myspell ignores extra arguments, we require it starts with
5519 * # to detect mistakes. */
5520 if (itemcnt > 3 && items[3][0] != '#')
5521 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005522 if (items[0][3] == 'S' ? do_repsal : do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005523 {
5524 /* Replace underscore with space (can't include a space
5525 * directly). */
5526 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5527 if (*p == '_')
5528 *p = ' ';
5529 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5530 if (*p == '_')
5531 *p = ' ';
Bram Moolenaar4770d092006-01-12 23:22:24 +00005532 add_fromto(spin, items[0][3] == 'S'
5533 ? &spin->si_repsal
5534 : &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005535 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005536 }
5537 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
5538 {
5539 /* MAP item or count */
5540 if (!found_map)
5541 {
5542 /* First line contains the count. */
5543 found_map = TRUE;
5544 if (!isdigit(*items[1]))
5545 smsg((char_u *)_("Expected MAP count in %s line %d"),
5546 fname, lnum);
5547 }
5548 else if (do_map)
5549 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005550 int c;
5551
5552 /* Check that every character appears only once. */
5553 for (p = items[1]; *p != NUL; )
5554 {
5555#ifdef FEAT_MBYTE
5556 c = mb_ptr2char_adv(&p);
5557#else
5558 c = *p++;
5559#endif
5560 if ((spin->si_map.ga_len > 0
5561 && vim_strchr(spin->si_map.ga_data, c)
5562 != NULL)
5563 || vim_strchr(p, c) != NULL)
5564 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5565 fname, lnum);
5566 }
5567
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005568 /* We simply concatenate all the MAP strings, separated by
5569 * slashes. */
5570 ga_concat(&spin->si_map, items[1]);
5571 ga_append(&spin->si_map, '/');
5572 }
5573 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005574 /* Accept "SAL from to" and "SAL from to # comment". */
5575 else if (STRCMP(items[0], "SAL") == 0
5576 && (itemcnt == 3 || (itemcnt > 3 && items[3][0] == '#')))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005577 {
5578 if (do_sal)
5579 {
5580 /* SAL item (sounds-a-like)
5581 * Either one of the known keys or a from-to pair. */
5582 if (STRCMP(items[1], "followup") == 0)
5583 spin->si_followup = sal_to_bool(items[2]);
5584 else if (STRCMP(items[1], "collapse_result") == 0)
5585 spin->si_collapse = sal_to_bool(items[2]);
5586 else if (STRCMP(items[1], "remove_accents") == 0)
5587 spin->si_rem_accents = sal_to_bool(items[2]);
5588 else
5589 /* when "to" is "_" it means empty */
5590 add_fromto(spin, &spin->si_sal, items[1],
5591 STRCMP(items[2], "_") == 0 ? (char_u *)""
5592 : items[2]);
5593 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005594 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005595 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005596 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005597 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005598 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005599 }
5600 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005601 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005602 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005603 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005604 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005605 else if (STRCMP(items[0], "COMMON") == 0)
5606 {
5607 int i;
5608
5609 for (i = 1; i < itemcnt; ++i)
5610 {
5611 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
5612 items[i])))
5613 {
5614 p = vim_strsave(items[i]);
5615 if (p == NULL)
5616 break;
5617 hash_add(&spin->si_commonwords, p);
5618 }
5619 }
5620 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005621 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005622 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005623 fname, lnum, items[0]);
5624 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005625 }
5626
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005627 if (fol != NULL || low != NULL || upp != NULL)
5628 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005629 if (spin->si_clear_chartab)
5630 {
5631 /* Clear the char type tables, don't want to use any of the
5632 * currently used spell properties. */
5633 init_spell_chartab();
5634 spin->si_clear_chartab = FALSE;
5635 }
5636
Bram Moolenaar3982c542005-06-08 21:56:31 +00005637 /*
5638 * Don't write a word table for an ASCII file, so that we don't check
5639 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005640 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00005641 * mb_get_class(), the list of chars in the file will be incomplete.
5642 */
5643 if (!spin->si_ascii
5644#ifdef FEAT_MBYTE
5645 && !enc_utf8
5646#endif
5647 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005648 {
5649 if (fol == NULL || low == NULL || upp == NULL)
5650 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
5651 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00005652 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005653 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005654
5655 vim_free(fol);
5656 vim_free(low);
5657 vim_free(upp);
5658 }
5659
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005660 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005661 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005662 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005663 aff_check_number(spin->si_compmax, compmax, "COMPOUNDMAX");
5664 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005665 }
5666
Bram Moolenaar6de68532005-08-24 22:08:48 +00005667 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005668 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005669 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
5670 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005671 }
5672
Bram Moolenaar6de68532005-08-24 22:08:48 +00005673 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005674 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005675 if (syllable == NULL)
5676 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
5677 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
5678 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005679 }
5680
Bram Moolenaar6de68532005-08-24 22:08:48 +00005681 if (compflags != NULL)
5682 process_compflags(spin, aff, compflags);
5683
5684 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005685 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005686 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005687 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005688 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005689 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005690 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005691 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005692 MSG(_("Too many posponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005693 }
5694
Bram Moolenaar6de68532005-08-24 22:08:48 +00005695 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005696 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005697 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
5698 spin->si_syllable = syllable;
5699 }
5700
5701 if (sofofrom != NULL || sofoto != NULL)
5702 {
5703 if (sofofrom == NULL || sofoto == NULL)
5704 smsg((char_u *)_("Missing SOFO%s line in %s"),
5705 sofofrom == NULL ? "FROM" : "TO", fname);
5706 else if (spin->si_sal.ga_len > 0)
5707 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005708 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005709 {
5710 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
5711 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
5712 spin->si_sofofr = sofofrom;
5713 spin->si_sofoto = sofoto;
5714 }
5715 }
5716
5717 if (midword != NULL)
5718 {
5719 aff_check_string(spin->si_midword, midword, "MIDWORD");
5720 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005721 }
5722
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005723 vim_free(pc);
5724 fclose(fd);
5725 return aff;
5726}
5727
5728/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005729 * Turn an affix flag name into a number, according to the FLAG type.
5730 * returns zero for failure.
5731 */
5732 static unsigned
5733affitem2flag(flagtype, item, fname, lnum)
5734 int flagtype;
5735 char_u *item;
5736 char_u *fname;
5737 int lnum;
5738{
5739 unsigned res;
5740 char_u *p = item;
5741
5742 res = get_affitem(flagtype, &p);
5743 if (res == 0)
5744 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005745 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005746 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
5747 fname, lnum, item);
5748 else
5749 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
5750 fname, lnum, item);
5751 }
5752 if (*p != NUL)
5753 {
5754 smsg((char_u *)_(e_affname), fname, lnum, item);
5755 return 0;
5756 }
5757
5758 return res;
5759}
5760
5761/*
5762 * Get one affix name from "*pp" and advance the pointer.
5763 * Returns zero for an error, still advances the pointer then.
5764 */
5765 static unsigned
5766get_affitem(flagtype, pp)
5767 int flagtype;
5768 char_u **pp;
5769{
5770 int res;
5771
Bram Moolenaar95529562005-08-25 21:21:38 +00005772 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005773 {
5774 if (!VIM_ISDIGIT(**pp))
5775 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005776 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005777 return 0;
5778 }
5779 res = getdigits(pp);
5780 }
5781 else
5782 {
5783#ifdef FEAT_MBYTE
5784 res = mb_ptr2char_adv(pp);
5785#else
5786 res = *(*pp)++;
5787#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00005788 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00005789 && res >= 'A' && res <= 'Z'))
5790 {
5791 if (**pp == NUL)
5792 return 0;
5793#ifdef FEAT_MBYTE
5794 res = mb_ptr2char_adv(pp) + (res << 16);
5795#else
5796 res = *(*pp)++ + (res << 16);
5797#endif
5798 }
5799 }
5800 return res;
5801}
5802
5803/*
5804 * Process the "compflags" string used in an affix file and append it to
5805 * spin->si_compflags.
5806 * The processing involves changing the affix names to ID numbers, so that
5807 * they fit in one byte.
5808 */
5809 static void
5810process_compflags(spin, aff, compflags)
5811 spellinfo_T *spin;
5812 afffile_T *aff;
5813 char_u *compflags;
5814{
5815 char_u *p;
5816 char_u *prevp;
5817 unsigned flag;
5818 compitem_T *ci;
5819 int id;
5820 int len;
5821 char_u *tp;
5822 char_u key[AH_KEY_LEN];
5823 hashitem_T *hi;
5824
5825 /* Make room for the old and the new compflags, concatenated with a / in
5826 * between. Processing it makes it shorter, but we don't know by how
5827 * much, thus allocate the maximum. */
5828 len = STRLEN(compflags) + 1;
5829 if (spin->si_compflags != NULL)
5830 len += STRLEN(spin->si_compflags) + 1;
5831 p = getroom(spin, len, FALSE);
5832 if (p == NULL)
5833 return;
5834 if (spin->si_compflags != NULL)
5835 {
5836 STRCPY(p, spin->si_compflags);
5837 STRCAT(p, "/");
5838 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005839 spin->si_compflags = p;
5840 tp = p + STRLEN(p);
5841
5842 for (p = compflags; *p != NUL; )
5843 {
5844 if (vim_strchr((char_u *)"/*+[]", *p) != NULL)
5845 /* Copy non-flag characters directly. */
5846 *tp++ = *p++;
5847 else
5848 {
5849 /* First get the flag number, also checks validity. */
5850 prevp = p;
5851 flag = get_affitem(aff->af_flagtype, &p);
5852 if (flag != 0)
5853 {
5854 /* Find the flag in the hashtable. If it was used before, use
5855 * the existing ID. Otherwise add a new entry. */
5856 vim_strncpy(key, prevp, p - prevp);
5857 hi = hash_find(&aff->af_comp, key);
5858 if (!HASHITEM_EMPTY(hi))
5859 id = HI2CI(hi)->ci_newID;
5860 else
5861 {
5862 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
5863 if (ci == NULL)
5864 break;
5865 STRCPY(ci->ci_key, key);
5866 ci->ci_flag = flag;
5867 /* Avoid using a flag ID that has a special meaning in a
5868 * regexp (also inside []). */
5869 do
5870 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005871 check_renumber(spin);
5872 id = spin->si_newcompID--;
5873 } while (vim_strchr((char_u *)"/+*[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005874 ci->ci_newID = id;
5875 hash_add(&aff->af_comp, ci->ci_key);
5876 }
5877 *tp++ = id;
5878 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005879 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00005880 ++p;
5881 }
5882 }
5883
5884 *tp = NUL;
5885}
5886
5887/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005888 * Check that the new IDs for postponed affixes and compounding don't overrun
5889 * each other. We have almost 255 available, but start at 0-127 to avoid
5890 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
5891 * When that is used up an error message is given.
5892 */
5893 static void
5894check_renumber(spin)
5895 spellinfo_T *spin;
5896{
5897 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
5898 {
5899 spin->si_newprefID = 127;
5900 spin->si_newcompID = 255;
5901 }
5902}
5903
5904/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005905 * Return TRUE if flag "flag" appears in affix list "afflist".
5906 */
5907 static int
5908flag_in_afflist(flagtype, afflist, flag)
5909 int flagtype;
5910 char_u *afflist;
5911 unsigned flag;
5912{
5913 char_u *p;
5914 unsigned n;
5915
5916 switch (flagtype)
5917 {
5918 case AFT_CHAR:
5919 return vim_strchr(afflist, flag) != NULL;
5920
Bram Moolenaar95529562005-08-25 21:21:38 +00005921 case AFT_CAPLONG:
5922 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00005923 for (p = afflist; *p != NUL; )
5924 {
5925#ifdef FEAT_MBYTE
5926 n = mb_ptr2char_adv(&p);
5927#else
5928 n = *p++;
5929#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00005930 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00005931 && *p != NUL)
5932#ifdef FEAT_MBYTE
5933 n = mb_ptr2char_adv(&p) + (n << 16);
5934#else
5935 n = *p++ + (n << 16);
5936#endif
5937 if (n == flag)
5938 return TRUE;
5939 }
5940 break;
5941
Bram Moolenaar95529562005-08-25 21:21:38 +00005942 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00005943 for (p = afflist; *p != NUL; )
5944 {
5945 n = getdigits(&p);
5946 if (n == flag)
5947 return TRUE;
5948 if (*p != NUL) /* skip over comma */
5949 ++p;
5950 }
5951 break;
5952 }
5953 return FALSE;
5954}
5955
5956/*
5957 * Give a warning when "spinval" and "affval" numbers are set and not the same.
5958 */
5959 static void
5960aff_check_number(spinval, affval, name)
5961 int spinval;
5962 int affval;
5963 char *name;
5964{
5965 if (spinval != 0 && spinval != affval)
5966 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
5967}
5968
5969/*
5970 * Give a warning when "spinval" and "affval" strings are set and not the same.
5971 */
5972 static void
5973aff_check_string(spinval, affval, name)
5974 char_u *spinval;
5975 char_u *affval;
5976 char *name;
5977{
5978 if (spinval != NULL && STRCMP(spinval, affval) != 0)
5979 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
5980}
5981
5982/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005983 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
5984 * NULL as equal.
5985 */
5986 static int
5987str_equal(s1, s2)
5988 char_u *s1;
5989 char_u *s2;
5990{
5991 if (s1 == NULL || s2 == NULL)
5992 return s1 == s2;
5993 return STRCMP(s1, s2) == 0;
5994}
5995
5996/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005997 * Add a from-to item to "gap". Used for REP and SAL items.
5998 * They are stored case-folded.
5999 */
6000 static void
6001add_fromto(spin, gap, from, to)
6002 spellinfo_T *spin;
6003 garray_T *gap;
6004 char_u *from;
6005 char_u *to;
6006{
6007 fromto_T *ftp;
6008 char_u word[MAXWLEN];
6009
6010 if (ga_grow(gap, 1) == OK)
6011 {
6012 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
6013 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006014 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006015 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006016 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006017 ++gap->ga_len;
6018 }
6019}
6020
6021/*
6022 * Convert a boolean argument in a SAL line to TRUE or FALSE;
6023 */
6024 static int
6025sal_to_bool(s)
6026 char_u *s;
6027{
6028 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
6029}
6030
6031/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00006032 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6033 * When "s" is NULL FALSE is returned.
6034 */
6035 static int
6036has_non_ascii(s)
6037 char_u *s;
6038{
6039 char_u *p;
6040
6041 if (s != NULL)
6042 for (p = s; *p != NUL; ++p)
6043 if (*p >= 128)
6044 return TRUE;
6045 return FALSE;
6046}
6047
6048/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006049 * Free the structure filled by spell_read_aff().
6050 */
6051 static void
6052spell_free_aff(aff)
6053 afffile_T *aff;
6054{
6055 hashtab_T *ht;
6056 hashitem_T *hi;
6057 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006058 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006059 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006060
6061 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006062
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006063 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006064 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
6065 {
6066 todo = ht->ht_used;
6067 for (hi = ht->ht_array; todo > 0; ++hi)
6068 {
6069 if (!HASHITEM_EMPTY(hi))
6070 {
6071 --todo;
6072 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006073 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
6074 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006075 }
6076 }
6077 if (ht == &aff->af_suff)
6078 break;
6079 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006080
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006081 hash_clear(&aff->af_pref);
6082 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006083 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006084}
6085
6086/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006087 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006088 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006089 */
6090 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006091spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006092 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006093 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006094 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006095{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006096 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006097 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006098 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006099 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006100 char_u store_afflist[MAXWLEN];
6101 int pfxlen;
6102 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006103 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006104 char_u *pc;
6105 char_u *w;
6106 int l;
6107 hash_T hash;
6108 hashitem_T *hi;
6109 FILE *fd;
6110 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006111 int non_ascii = 0;
6112 int retval = OK;
6113 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006114 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006115 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006116
Bram Moolenaar51485f02005-06-04 21:55:20 +00006117 /*
6118 * Open the file.
6119 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006120 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006121 if (fd == NULL)
6122 {
6123 EMSG2(_(e_notopen), fname);
6124 return FAIL;
6125 }
6126
Bram Moolenaar51485f02005-06-04 21:55:20 +00006127 /* The hashtable is only used to detect duplicated words. */
6128 hash_init(&ht);
6129
Bram Moolenaar4770d092006-01-12 23:22:24 +00006130 vim_snprintf((char *)IObuff, IOSIZE,
6131 _("Reading dictionary file %s ..."), fname);
6132 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006133
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006134 /* start with a message for the first line */
6135 spin->si_msg_count = 999999;
6136
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006137 /* Read and ignore the first line: word count. */
6138 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006139 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006140 EMSG2(_("E760: No word count in %s"), fname);
6141
6142 /*
6143 * Read all the lines in the file one by one.
6144 * The words are converted to 'encoding' here, before being added to
6145 * the hashtable.
6146 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006147 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006148 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006149 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006150 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00006151 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006152 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006153
Bram Moolenaar51485f02005-06-04 21:55:20 +00006154 /* Remove CR, LF and white space from the end. White space halfway
6155 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006156 l = STRLEN(line);
6157 while (l > 0 && line[l - 1] <= ' ')
6158 --l;
6159 if (l == 0)
6160 continue; /* empty line */
6161 line[l] = NUL;
6162
Bram Moolenaar66fa2712006-01-22 23:22:22 +00006163 /* Truncate the word at the "/", set "afflist" to what follows.
6164 * Replace "\/" by "/" and "\\" by "\". */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006165 afflist = NULL;
6166 for (p = line; *p != NUL; mb_ptr_adv(p))
6167 {
Bram Moolenaar66fa2712006-01-22 23:22:22 +00006168 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
6169 mch_memmove(p, p + 1, STRLEN(p));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006170 else if (*p == '/')
6171 {
6172 *p = NUL;
6173 afflist = p + 1;
6174 break;
6175 }
6176 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006177
6178 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6179 if (spin->si_ascii && has_non_ascii(line))
6180 {
6181 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00006182 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006183 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00006184
Bram Moolenaarb765d632005-06-07 21:00:02 +00006185#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006186 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006187 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006188 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006189 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006190 if (pc == NULL)
6191 {
6192 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6193 fname, lnum, line);
6194 continue;
6195 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006196 w = pc;
6197 }
6198 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006199#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006200 {
6201 pc = NULL;
6202 w = line;
6203 }
6204
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006205 /* This takes time, print a message every 10000 words. */
6206 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006207 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006208 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006209 vim_snprintf((char *)message, sizeof(message),
6210 _("line %6d, word %6d - %s"),
6211 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
6212 msg_start();
6213 msg_puts_long_attr(message, 0);
6214 msg_clr_eos();
6215 msg_didout = FALSE;
6216 msg_col = 0;
6217 out_flush();
6218 }
6219
Bram Moolenaar51485f02005-06-04 21:55:20 +00006220 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006221 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006222 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006223 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006224 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006225 if (retval == FAIL)
6226 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006227
Bram Moolenaar51485f02005-06-04 21:55:20 +00006228 hash = hash_hash(dw);
6229 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006230 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006231 {
6232 if (p_verbose > 0)
6233 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006234 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006235 else if (duplicate == 0)
6236 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
6237 fname, lnum, dw);
6238 ++duplicate;
6239 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006240 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006241 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006242
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006243 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006244 store_afflist[0] = NUL;
6245 pfxlen = 0;
6246 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006247 if (afflist != NULL)
6248 {
6249 /* Check for affix name that stands for keep-case word and stands
6250 * for rare word (if defined). */
Bram Moolenaar371baa92005-12-29 22:43:53 +00006251 if (affile->af_keepcase != 0 && flag_in_afflist(
6252 affile->af_flagtype, afflist, affile->af_keepcase))
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006253 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar371baa92005-12-29 22:43:53 +00006254 if (affile->af_rare != 0 && flag_in_afflist(
6255 affile->af_flagtype, afflist, affile->af_rare))
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006256 flags |= WF_RARE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006257 if (affile->af_bad != 0 && flag_in_afflist(
6258 affile->af_flagtype, afflist, affile->af_bad))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006259 flags |= WF_BANNED;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006260 if (affile->af_needaffix != 0 && flag_in_afflist(
6261 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00006262 need_affix = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006263 if (affile->af_needcomp != 0 && flag_in_afflist(
6264 affile->af_flagtype, afflist, affile->af_needcomp))
6265 flags |= WF_NEEDCOMP;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00006266 if (affile->af_nosuggest != 0 && flag_in_afflist(
6267 affile->af_flagtype, afflist, affile->af_nosuggest))
6268 flags |= WF_NOSUGGEST;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006269
6270 if (affile->af_pfxpostpone)
6271 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006272 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006273
Bram Moolenaar5195e452005-08-19 20:32:47 +00006274 if (spin->si_compflags != NULL)
6275 /* Need to store the list of compound flags with the word.
6276 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006277 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006278 }
6279
Bram Moolenaar51485f02005-06-04 21:55:20 +00006280 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006281 if (store_word(spin, dw, flags, spin->si_region,
6282 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006283 retval = FAIL;
6284
6285 if (afflist != NULL)
6286 {
6287 /* Find all matching suffixes and add the resulting words.
6288 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006289 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006290 &affile->af_suff, &affile->af_pref,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006291 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006292 retval = FAIL;
6293
6294 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006295 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006296 &affile->af_pref, NULL,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006297 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006298 retval = FAIL;
6299 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006300 }
6301
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006302 if (duplicate > 0)
6303 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006304 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006305 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
6306 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006307 hash_clear(&ht);
6308
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006309 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006310 return retval;
6311}
6312
6313/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006314 * Get the list of prefix IDs from the affix list "afflist".
6315 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006316 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
6317 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006318 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006319 static int
6320get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006321 afffile_T *affile;
6322 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006323 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006324{
6325 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006326 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006327 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006328 int id;
6329 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006330 hashitem_T *hi;
6331
Bram Moolenaar6de68532005-08-24 22:08:48 +00006332 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006333 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006334 prevp = p;
6335 if (get_affitem(affile->af_flagtype, &p) != 0)
6336 {
6337 /* A flag is a postponed prefix flag if it appears in "af_pref"
6338 * and it's ID is not zero. */
6339 vim_strncpy(key, prevp, p - prevp);
6340 hi = hash_find(&affile->af_pref, key);
6341 if (!HASHITEM_EMPTY(hi))
6342 {
6343 id = HI2AH(hi)->ah_newID;
6344 if (id != 0)
6345 store_afflist[cnt++] = id;
6346 }
6347 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006348 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006349 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006350 }
6351
Bram Moolenaar5195e452005-08-19 20:32:47 +00006352 store_afflist[cnt] = NUL;
6353 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006354}
6355
6356/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006357 * Get the list of compound IDs from the affix list "afflist" that are used
6358 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006359 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006360 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006361 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00006362get_compflags(affile, afflist, store_afflist)
6363 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006364 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006365 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006366{
6367 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006368 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006369 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006370 char_u key[AH_KEY_LEN];
6371 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006372
Bram Moolenaar6de68532005-08-24 22:08:48 +00006373 for (p = afflist; *p != NUL; )
6374 {
6375 prevp = p;
6376 if (get_affitem(affile->af_flagtype, &p) != 0)
6377 {
6378 /* A flag is a compound flag if it appears in "af_comp". */
6379 vim_strncpy(key, prevp, p - prevp);
6380 hi = hash_find(&affile->af_comp, key);
6381 if (!HASHITEM_EMPTY(hi))
6382 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
6383 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006384 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006385 ++p;
6386 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006387
Bram Moolenaar5195e452005-08-19 20:32:47 +00006388 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006389}
6390
6391/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006392 * Apply affixes to a word and store the resulting words.
6393 * "ht" is the hashtable with affentry_T that need to be applied, either
6394 * prefixes or suffixes.
6395 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
6396 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006397 *
6398 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006399 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006400 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00006401store_aff_word(spin, word, afflist, affile, ht, xht, comb, flags,
6402 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006403 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006404 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006405 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006406 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006407 hashtab_T *ht;
6408 hashtab_T *xht;
6409 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006410 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006411 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006412 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
6413 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006414{
6415 int todo;
6416 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006417 affheader_T *ah;
6418 affentry_T *ae;
6419 regmatch_T regmatch;
6420 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006421 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006422 int i;
6423 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006424 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006425 char_u *use_pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006426 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006427 size_t wordlen = STRLEN(word);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006428
Bram Moolenaar51485f02005-06-04 21:55:20 +00006429 todo = ht->ht_used;
6430 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006431 {
6432 if (!HASHITEM_EMPTY(hi))
6433 {
6434 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006435 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00006436
Bram Moolenaar51485f02005-06-04 21:55:20 +00006437 /* Check that the affix combines, if required, and that the word
6438 * supports this affix. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006439 if ((!comb || ah->ah_combine) && flag_in_afflist(
6440 affile->af_flagtype, afflist, ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00006441 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006442 /* Loop over all affix entries with this name. */
6443 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006444 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006445 /* Check the condition. It's not logical to match case
6446 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006447 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006448 * Another requirement from Myspell is that the chop
6449 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006450 * For prefixes, when "PFXPOSTPONE" was used, only do
6451 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006452 regmatch.regprog = ae->ae_prog;
6453 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006454 if ((xht != NULL || !affile->af_pfxpostpone
6455 || ae->ae_chop != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006456 && (ae->ae_chop == NULL
6457 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006458 && (ae->ae_prog == NULL
6459 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006460 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006461 /* Match. Remove the chop and add the affix. */
6462 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006463 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006464 /* prefix: chop/add at the start of the word */
6465 if (ae->ae_add == NULL)
6466 *newword = NUL;
6467 else
6468 STRCPY(newword, ae->ae_add);
6469 p = word;
6470 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006471 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006472 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006473#ifdef FEAT_MBYTE
6474 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006475 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006476 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006477 for ( ; i > 0; --i)
6478 mb_ptr_adv(p);
6479 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006480 else
6481#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006482 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006483 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006484 STRCAT(newword, p);
6485 }
6486 else
6487 {
6488 /* suffix: chop/add at the end of the word */
6489 STRCPY(newword, word);
6490 if (ae->ae_chop != NULL)
6491 {
6492 /* Remove chop string. */
6493 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006494 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006495 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006496 mb_ptr_back(newword, p);
6497 *p = NUL;
6498 }
6499 if (ae->ae_add != NULL)
6500 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006501 }
6502
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006503 /* Obey the "rare" flag of the affix. */
6504 if (ae->ae_rare)
6505 use_flags = flags | WF_RARE;
6506 else
6507 use_flags = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006508
6509 /* Obey the "nocomp" flag of the affix: don't use the
6510 * compound flags. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006511 use_pfxlist = pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006512 if (ae->ae_nocomp && pfxlist != NULL)
6513 {
6514 vim_strncpy(pfx_pfxlist, pfxlist, pfxlen);
6515 use_pfxlist = pfx_pfxlist;
6516 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006517
6518 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00006519 if (spin->si_prefroot != NULL
6520 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006521 {
6522 /* ... add a flag to indicate an affix was used. */
6523 use_flags |= WF_HAS_AFF;
6524
6525 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00006526 * affixes is not allowed. But do use the
6527 * compound flags after them. */
6528 if ((!ah->ah_combine || comb) && pfxlist != NULL)
6529 use_pfxlist += pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006530 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006531
Bram Moolenaar51485f02005-06-04 21:55:20 +00006532 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006533 if (store_word(spin, newword, use_flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006534 spin->si_region, use_pfxlist, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006535 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006536
Bram Moolenaar51485f02005-06-04 21:55:20 +00006537 /* When added a suffix and combining is allowed also
6538 * try adding prefixes additionally. */
6539 if (xht != NULL && ah->ah_combine)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006540 if (store_aff_word(spin, newword, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006541 xht, NULL, TRUE,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006542 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006543 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006544 }
6545 }
6546 }
6547 }
6548 }
6549
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006550 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006551}
6552
6553/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006554 * Read a file with a list of words.
6555 */
6556 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006557spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006558 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006559 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006560{
6561 FILE *fd;
6562 long lnum = 0;
6563 char_u rline[MAXLINELEN];
6564 char_u *line;
6565 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006566 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006567 int l;
6568 int retval = OK;
6569 int did_word = FALSE;
6570 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006571 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00006572 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006573
6574 /*
6575 * Open the file.
6576 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006577 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00006578 if (fd == NULL)
6579 {
6580 EMSG2(_(e_notopen), fname);
6581 return FAIL;
6582 }
6583
Bram Moolenaar4770d092006-01-12 23:22:24 +00006584 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname);
6585 spell_message(spin, IObuff);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006586
6587 /*
6588 * Read all the lines in the file one by one.
6589 */
6590 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
6591 {
6592 line_breakcheck();
6593 ++lnum;
6594
6595 /* Skip comment lines. */
6596 if (*rline == '#')
6597 continue;
6598
6599 /* Remove CR, LF and white space from the end. */
6600 l = STRLEN(rline);
6601 while (l > 0 && rline[l - 1] <= ' ')
6602 --l;
6603 if (l == 0)
6604 continue; /* empty or blank line */
6605 rline[l] = NUL;
6606
6607 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
6608 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006609#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00006610 if (spin->si_conv.vc_type != CONV_NONE)
6611 {
6612 pc = string_convert(&spin->si_conv, rline, NULL);
6613 if (pc == NULL)
6614 {
6615 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6616 fname, lnum, rline);
6617 continue;
6618 }
6619 line = pc;
6620 }
6621 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006622#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006623 {
6624 pc = NULL;
6625 line = rline;
6626 }
6627
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006628 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00006629 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006630 ++line;
6631 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006632 {
6633 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006634 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
6635 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006636 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006637 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
6638 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006639 else
6640 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006641#ifdef FEAT_MBYTE
6642 char_u *enc;
6643
Bram Moolenaar51485f02005-06-04 21:55:20 +00006644 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006645 line += 10;
6646 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006647 if (enc != NULL && !spin->si_ascii
6648 && convert_setup(&spin->si_conv, enc,
6649 p_enc) == FAIL)
6650 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00006651 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006652 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006653 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006654#else
6655 smsg((char_u *)_("Conversion in %s not supported"), fname);
6656#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006657 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006658 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006659 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006660
Bram Moolenaar3982c542005-06-08 21:56:31 +00006661 if (STRNCMP(line, "regions=", 8) == 0)
6662 {
6663 if (spin->si_region_count > 1)
6664 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
6665 fname, lnum, line);
6666 else
6667 {
6668 line += 8;
6669 if (STRLEN(line) > 16)
6670 smsg((char_u *)_("Too many regions in %s line %d: %s"),
6671 fname, lnum, line);
6672 else
6673 {
6674 spin->si_region_count = STRLEN(line) / 2;
6675 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006676
6677 /* Adjust the mask for a word valid in all regions. */
6678 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00006679 }
6680 }
6681 continue;
6682 }
6683
Bram Moolenaar7887d882005-07-01 22:33:52 +00006684 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
6685 fname, lnum, line - 1);
6686 continue;
6687 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006688
Bram Moolenaar7887d882005-07-01 22:33:52 +00006689 flags = 0;
6690 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006691
Bram Moolenaar7887d882005-07-01 22:33:52 +00006692 /* Check for flags and region after a slash. */
6693 p = vim_strchr(line, '/');
6694 if (p != NULL)
6695 {
6696 *p++ = NUL;
6697 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006698 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006699 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006700 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006701 else if (*p == '!') /* Bad, bad, wicked word. */
6702 flags |= WF_BANNED;
6703 else if (*p == '?') /* Rare word. */
6704 flags |= WF_RARE;
6705 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006706 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006707 if ((flags & WF_REGION) == 0) /* first one */
6708 regionmask = 0;
6709 flags |= WF_REGION;
6710
6711 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00006712 if (l > spin->si_region_count)
6713 {
6714 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00006715 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00006716 break;
6717 }
6718 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00006719 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00006720 else
6721 {
6722 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
6723 fname, lnum, p);
6724 break;
6725 }
6726 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006727 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006728 }
6729
6730 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6731 if (spin->si_ascii && has_non_ascii(line))
6732 {
6733 ++non_ascii;
6734 continue;
6735 }
6736
6737 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006738 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006739 {
6740 retval = FAIL;
6741 break;
6742 }
6743 did_word = TRUE;
6744 }
6745
6746 vim_free(pc);
6747 fclose(fd);
6748
Bram Moolenaar4770d092006-01-12 23:22:24 +00006749 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006750 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006751 vim_snprintf((char *)IObuff, IOSIZE,
6752 _("Ignored %d words with non-ASCII characters"), non_ascii);
6753 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006754 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006755
Bram Moolenaar51485f02005-06-04 21:55:20 +00006756 return retval;
6757}
6758
6759/*
6760 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006761 * This avoids calling free() for every little struct we use (and keeping
6762 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00006763 * The memory is cleared to all zeros.
6764 * Returns NULL when out of memory.
6765 */
6766 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006767getroom(spin, len, align)
6768 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006769 size_t len; /* length needed */
6770 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006771{
6772 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006773 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006774
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006775 if (align && bl != NULL)
6776 /* Round size up for alignment. On some systems structures need to be
6777 * aligned to the size of a pointer (e.g., SPARC). */
6778 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
6779 & ~(sizeof(char *) - 1);
6780
Bram Moolenaar51485f02005-06-04 21:55:20 +00006781 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
6782 {
6783 /* Allocate a block of memory. This is not freed until much later. */
6784 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
6785 if (bl == NULL)
6786 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006787 bl->sb_next = spin->si_blocks;
6788 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006789 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006790 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006791 }
6792
6793 p = bl->sb_data + bl->sb_used;
6794 bl->sb_used += len;
6795
6796 return p;
6797}
6798
6799/*
6800 * Make a copy of a string into memory allocated with getroom().
6801 */
6802 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006803getroom_save(spin, s)
6804 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006805 char_u *s;
6806{
6807 char_u *sc;
6808
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006809 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006810 if (sc != NULL)
6811 STRCPY(sc, s);
6812 return sc;
6813}
6814
6815
6816/*
6817 * Free the list of allocated sblock_T.
6818 */
6819 static void
6820free_blocks(bl)
6821 sblock_T *bl;
6822{
6823 sblock_T *next;
6824
6825 while (bl != NULL)
6826 {
6827 next = bl->sb_next;
6828 vim_free(bl);
6829 bl = next;
6830 }
6831}
6832
6833/*
6834 * Allocate the root of a word tree.
6835 */
6836 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006837wordtree_alloc(spin)
6838 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006839{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006840 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006841}
6842
6843/*
6844 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006845 * Always store it in the case-folded tree. For a keep-case word this is
6846 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
6847 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006848 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006849 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
6850 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006851 */
6852 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00006853store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006854 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006855 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006856 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006857 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006858 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006859 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006860{
6861 int len = STRLEN(word);
6862 int ct = captype(word, word + len);
6863 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006864 int res = OK;
6865 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006866
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006867 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006868 for (p = pfxlist; res == OK; ++p)
6869 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006870 if (!need_affix || (p != NULL && *p != NUL))
6871 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006872 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006873 if (p == NULL || *p == NUL)
6874 break;
6875 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00006876 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006877
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006878 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00006879 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006880 for (p = pfxlist; res == OK; ++p)
6881 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006882 if (!need_affix || (p != NULL && *p != NUL))
6883 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006884 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006885 if (p == NULL || *p == NUL)
6886 break;
6887 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00006888 ++spin->si_keepwcount;
6889 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006890 return res;
6891}
6892
6893/*
6894 * Add word "word" to a word tree at "root".
Bram Moolenaar4770d092006-01-12 23:22:24 +00006895 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006896 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006897 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006898 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006899 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006900tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006901 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006902 char_u *word;
6903 wordnode_T *root;
6904 int flags;
6905 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006906 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006907{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006908 wordnode_T *node = root;
6909 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006910 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006911 wordnode_T **prev = NULL;
6912 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006913
Bram Moolenaar51485f02005-06-04 21:55:20 +00006914 /* Add each byte of the word to the tree, including the NUL at the end. */
6915 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006916 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006917 /* When there is more than one reference to this node we need to make
6918 * a copy, so that we can modify it. Copy the whole list of siblings
6919 * (we don't optimize for a partly shared list of siblings). */
6920 if (node != NULL && node->wn_refs > 1)
6921 {
6922 --node->wn_refs;
6923 copyprev = prev;
6924 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
6925 {
6926 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006927 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006928 if (np == NULL)
6929 return FAIL;
6930 np->wn_child = copyp->wn_child;
6931 if (np->wn_child != NULL)
6932 ++np->wn_child->wn_refs; /* child gets extra ref */
6933 np->wn_byte = copyp->wn_byte;
6934 if (np->wn_byte == NUL)
6935 {
6936 np->wn_flags = copyp->wn_flags;
6937 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006938 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006939 }
6940
6941 /* Link the new node in the list, there will be one ref. */
6942 np->wn_refs = 1;
6943 *copyprev = np;
6944 copyprev = &np->wn_sibling;
6945
6946 /* Let "node" point to the head of the copied list. */
6947 if (copyp == node)
6948 node = np;
6949 }
6950 }
6951
Bram Moolenaar51485f02005-06-04 21:55:20 +00006952 /* Look for the sibling that has the same character. They are sorted
6953 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006954 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006955 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006956 while (node != NULL
6957 && (node->wn_byte < word[i]
6958 || (node->wn_byte == NUL
6959 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00006960 ? node->wn_affixID < (unsigned)affixID
6961 : (node->wn_flags < (unsigned)(flags & WN_MASK)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006962 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006963 && (spin->si_sugtree
6964 ? (node->wn_region & 0xffff) < region
6965 : node->wn_affixID
6966 < (unsigned)affixID)))))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006967 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006968 prev = &node->wn_sibling;
6969 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006970 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006971 if (node == NULL
6972 || node->wn_byte != word[i]
6973 || (word[i] == NUL
6974 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00006975 || spin->si_sugtree
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006976 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006977 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006978 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006979 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006980 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006981 if (np == NULL)
6982 return FAIL;
6983 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006984
6985 /* If "node" is NULL this is a new child or the end of the sibling
6986 * list: ref count is one. Otherwise use ref count of sibling and
6987 * make ref count of sibling one (matters when inserting in front
6988 * of the list of siblings). */
6989 if (node == NULL)
6990 np->wn_refs = 1;
6991 else
6992 {
6993 np->wn_refs = node->wn_refs;
6994 node->wn_refs = 1;
6995 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006996 *prev = np;
6997 np->wn_sibling = node;
6998 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006999 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007000
Bram Moolenaar51485f02005-06-04 21:55:20 +00007001 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007002 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007003 node->wn_flags = flags;
7004 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007005 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007006 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00007007 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007008 prev = &node->wn_child;
7009 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007010 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007011#ifdef SPELL_PRINTTREE
7012 smsg("Added \"%s\"", word);
7013 spell_print_tree(root->wn_sibling);
7014#endif
7015
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007016 /* count nr of words added since last message */
7017 ++spin->si_msg_count;
7018
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007019 if (spin->si_compress_cnt > 1)
7020 {
7021 if (--spin->si_compress_cnt == 1)
7022 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007023 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007024 }
7025
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007026 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007027 * When we have allocated lots of memory we need to compress the word tree
7028 * to free up some room. But compression is slow, and we might actually
7029 * need that room, thus only compress in the following situations:
7030 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00007031 * "compress_start" blocks.
7032 * 2. When compressed before and used "compress_inc" blocks before
7033 * adding "compress_added" words (si_compress_cnt > 1).
7034 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007035 * (si_compress_cnt == 1) and the number of free nodes drops below the
7036 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007037 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007038#ifndef SPELL_PRINTTREE
7039 if (spin->si_compress_cnt == 1
7040 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00007041 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007042#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007043 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007044 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00007045 * when the freed up room has been used and another "compress_inc"
7046 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007047 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007048 spin->si_blocks_cnt -= compress_inc;
7049 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007050
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007051 if (spin->si_verbose)
7052 {
7053 msg_start();
7054 msg_puts((char_u *)_(msg_compressing));
7055 msg_clr_eos();
7056 msg_didout = FALSE;
7057 msg_col = 0;
7058 out_flush();
7059 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007060
7061 /* Compress both trees. Either they both have many nodes, which makes
7062 * compression useful, or one of them is small, which means
Bram Moolenaar4770d092006-01-12 23:22:24 +00007063 * compression goes fast. But when filling the souldfold word tree
7064 * there is no keep-case tree. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007065 wordtree_compress(spin, spin->si_foldroot);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007066 if (affixID >= 0)
7067 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007068 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007069
7070 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007071}
7072
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007073/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007074 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
7075 * Sets "sps_flags".
7076 */
7077 int
7078spell_check_msm()
7079{
7080 char_u *p = p_msm;
7081 long start = 0;
7082 long inc = 0;
7083 long added = 0;
7084
7085 if (!VIM_ISDIGIT(*p))
7086 return FAIL;
7087 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
7088 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
7089 if (*p != ',')
7090 return FAIL;
7091 ++p;
7092 if (!VIM_ISDIGIT(*p))
7093 return FAIL;
7094 inc = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
7095 if (*p != ',')
7096 return FAIL;
7097 ++p;
7098 if (!VIM_ISDIGIT(*p))
7099 return FAIL;
7100 added = getdigits(&p) * 1024;
7101 if (*p != NUL)
7102 return FAIL;
7103
7104 if (start == 0 || inc == 0 || added == 0 || inc > start)
7105 return FAIL;
7106
7107 compress_start = start;
7108 compress_inc = inc;
7109 compress_added = added;
7110 return OK;
7111}
7112
7113
7114/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007115 * Get a wordnode_T, either from the list of previously freed nodes or
7116 * allocate a new one.
7117 */
7118 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007119get_wordnode(spin)
7120 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007121{
7122 wordnode_T *n;
7123
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007124 if (spin->si_first_free == NULL)
7125 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007126 else
7127 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007128 n = spin->si_first_free;
7129 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007130 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007131 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007132 }
7133#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007134 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007135#endif
7136 return n;
7137}
7138
7139/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007140 * Decrement the reference count on a node (which is the head of a list of
7141 * siblings). If the reference count becomes zero free the node and its
7142 * siblings.
Bram Moolenaar4770d092006-01-12 23:22:24 +00007143 * Returns the number of nodes actually freed.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007144 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007145 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007146deref_wordnode(spin, node)
7147 spellinfo_T *spin;
7148 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007149{
Bram Moolenaar4770d092006-01-12 23:22:24 +00007150 wordnode_T *np;
7151 int cnt = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007152
7153 if (--node->wn_refs == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007154 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007155 for (np = node; np != NULL; np = np->wn_sibling)
7156 {
7157 if (np->wn_child != NULL)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007158 cnt += deref_wordnode(spin, np->wn_child);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007159 free_wordnode(spin, np);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007160 ++cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007161 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007162 ++cnt; /* length field */
7163 }
7164 return cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007165}
7166
7167/*
7168 * Free a wordnode_T for re-use later.
7169 * Only the "wn_child" field becomes invalid.
7170 */
7171 static void
7172free_wordnode(spin, n)
7173 spellinfo_T *spin;
7174 wordnode_T *n;
7175{
7176 n->wn_child = spin->si_first_free;
7177 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007178 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007179}
7180
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007181/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007182 * Compress a tree: find tails that are identical and can be shared.
7183 */
7184 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007185wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007186 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007187 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007188{
7189 hashtab_T ht;
7190 int n;
7191 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007192 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007193
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007194 /* Skip the root itself, it's not actually used. The first sibling is the
7195 * start of the tree. */
7196 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007197 {
7198 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007199 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007200
7201#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00007202 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007203#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00007204 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007205 if (tot > 1000000)
7206 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007207 else if (tot == 0)
7208 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007209 else
7210 perc = (tot - n) * 100 / tot;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007211 vim_snprintf((char *)IObuff, IOSIZE,
7212 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
7213 n, tot, tot - n, perc);
7214 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007215 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007216#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007217 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007218#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007219 hash_clear(&ht);
7220 }
7221}
7222
7223/*
7224 * Compress a node, its siblings and its children, depth first.
7225 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007226 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007227 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007228node_compress(spin, node, ht, tot)
7229 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007230 wordnode_T *node;
7231 hashtab_T *ht;
7232 int *tot; /* total count of nodes before compressing,
7233 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007234{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007235 wordnode_T *np;
7236 wordnode_T *tp;
7237 wordnode_T *child;
7238 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007239 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007240 int len = 0;
7241 unsigned nr, n;
7242 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007243
Bram Moolenaar51485f02005-06-04 21:55:20 +00007244 /*
7245 * Go through the list of siblings. Compress each child and then try
7246 * finding an identical child to replace it.
7247 * Note that with "child" we mean not just the node that is pointed to,
Bram Moolenaar4770d092006-01-12 23:22:24 +00007248 * but the whole list of siblings of which the child node is the first.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007249 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007250 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007251 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007252 ++len;
7253 if ((child = np->wn_child) != NULL)
7254 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007255 /* Compress the child first. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007256 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007257
7258 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007259 hash = hash_hash(child->wn_u1.hashkey);
7260 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007261 if (!HASHITEM_EMPTY(hi))
7262 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007263 /* There are children we encountered before with a hash value
7264 * identical to the current child. Now check if there is one
7265 * that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007266 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007267 if (node_equal(child, tp))
7268 {
7269 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007270 * current one. This means the current child and all
7271 * its siblings is unlinked from the tree. */
7272 ++tp->wn_refs;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007273 compressed += deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007274 np->wn_child = tp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007275 break;
7276 }
7277 if (tp == NULL)
7278 {
7279 /* No other child with this hash value equals the child of
7280 * the node, add it to the linked list after the first
7281 * item. */
7282 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007283 child->wn_u2.next = tp->wn_u2.next;
7284 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007285 }
7286 }
7287 else
7288 /* No other child has this hash value, add it to the
7289 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007290 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007291 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007292 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007293 *tot += len + 1; /* add one for the node that stores the length */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007294
7295 /*
7296 * Make a hash key for the node and its siblings, so that we can quickly
7297 * find a lookalike node. This must be done after compressing the sibling
7298 * list, otherwise the hash key would become invalid by the compression.
7299 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007300 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007301 nr = 0;
7302 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007303 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007304 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007305 /* end node: use wn_flags, wn_region and wn_affixID */
7306 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007307 else
7308 /* byte node: use the byte value and the child pointer */
7309 n = np->wn_byte + ((long_u)np->wn_child << 8);
7310 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007311 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007312
7313 /* Avoid NUL bytes, it terminates the hash key. */
7314 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007315 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007316 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007317 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007318 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007319 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007320 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007321 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
7322 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007323
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007324 /* Check for CTRL-C pressed now and then. */
7325 fast_breakcheck();
7326
Bram Moolenaar51485f02005-06-04 21:55:20 +00007327 return compressed;
7328}
7329
7330/*
7331 * Return TRUE when two nodes have identical siblings and children.
7332 */
7333 static int
7334node_equal(n1, n2)
7335 wordnode_T *n1;
7336 wordnode_T *n2;
7337{
7338 wordnode_T *p1;
7339 wordnode_T *p2;
7340
7341 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
7342 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
7343 if (p1->wn_byte != p2->wn_byte
7344 || (p1->wn_byte == NUL
7345 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007346 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007347 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007348 : (p1->wn_child != p2->wn_child)))
7349 break;
7350
7351 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007352}
7353
7354/*
7355 * Write a number to file "fd", MSB first, in "len" bytes.
7356 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007357 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007358put_bytes(fd, nr, len)
7359 FILE *fd;
7360 long_u nr;
7361 int len;
7362{
7363 int i;
7364
7365 for (i = len - 1; i >= 0; --i)
7366 putc((int)(nr >> (i * 8)), fd);
7367}
7368
Bram Moolenaar4770d092006-01-12 23:22:24 +00007369/*
7370 * Write spin->si_sugtime to file "fd".
7371 */
7372 static void
7373put_sugtime(spin, fd)
7374 spellinfo_T *spin;
7375 FILE *fd;
7376{
7377 int c;
7378 int i;
7379
7380 /* time_t can be up to 8 bytes in size, more than long_u, thus we
7381 * can't use put_bytes() here. */
7382 for (i = 7; i >= 0; --i)
7383 if (i + 1 > sizeof(time_t))
7384 /* ">>" doesn't work well when shifting more bits than avail */
7385 putc(0, fd);
7386 else
7387 {
7388 c = (unsigned)spin->si_sugtime >> (i * 8);
7389 putc(c, fd);
7390 }
7391}
7392
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007393static int
7394#ifdef __BORLANDC__
7395_RTLENTRYF
7396#endif
7397rep_compare __ARGS((const void *s1, const void *s2));
7398
7399/*
7400 * Function given to qsort() to sort the REP items on "from" string.
7401 */
7402 static int
7403#ifdef __BORLANDC__
7404_RTLENTRYF
7405#endif
7406rep_compare(s1, s2)
7407 const void *s1;
7408 const void *s2;
7409{
7410 fromto_T *p1 = (fromto_T *)s1;
7411 fromto_T *p2 = (fromto_T *)s2;
7412
7413 return STRCMP(p1->ft_from, p2->ft_from);
7414}
7415
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007416/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007417 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007418 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007419 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007420 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007421write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007422 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007423 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007424{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007425 FILE *fd;
7426 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007427 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007428 wordnode_T *tree;
7429 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007430 int i;
7431 int l;
7432 garray_T *gap;
7433 fromto_T *ftp;
7434 char_u *p;
7435 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007436 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007437
Bram Moolenaarb765d632005-06-07 21:00:02 +00007438 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007439 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007440 {
7441 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007442 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007443 }
7444
Bram Moolenaar5195e452005-08-19 20:32:47 +00007445 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007446 /* <fileID> */
7447 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007448 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007449 EMSG(_(e_write));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007450 retval = FAIL;
7451 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007452 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007453
Bram Moolenaar5195e452005-08-19 20:32:47 +00007454 /*
7455 * <SECTIONS>: <section> ... <sectionend>
7456 */
7457
7458 /* SN_REGION: <regionname> ...
7459 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007460 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007461 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007462 putc(SN_REGION, fd); /* <sectionID> */
7463 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7464 l = spin->si_region_count * 2;
7465 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7466 fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
7467 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007468 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007469 }
7470 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00007471 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007472
Bram Moolenaar5195e452005-08-19 20:32:47 +00007473 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
7474 *
7475 * The table with character flags and the table for case folding.
7476 * This makes sure the same characters are recognized as word characters
7477 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007478 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007479 * 'encoding'.
7480 * Also skip this for an .add.spl file, the main spell file must contain
7481 * the table (avoids that it conflicts). File is shorter too.
7482 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007483 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007484 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007485 char_u folchars[128 * 8];
7486 int flags;
7487
Bram Moolenaard12a1322005-08-21 22:08:24 +00007488 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007489 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7490
7491 /* Form the <folchars> string first, we need to know its length. */
7492 l = 0;
7493 for (i = 128; i < 256; ++i)
7494 {
7495#ifdef FEAT_MBYTE
7496 if (has_mbyte)
7497 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
7498 else
7499#endif
7500 folchars[l++] = spelltab.st_fold[i];
7501 }
7502 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
7503
7504 fputc(128, fd); /* <charflagslen> */
7505 for (i = 128; i < 256; ++i)
7506 {
7507 flags = 0;
7508 if (spelltab.st_isw[i])
7509 flags |= CF_WORD;
7510 if (spelltab.st_isu[i])
7511 flags |= CF_UPPER;
7512 fputc(flags, fd); /* <charflags> */
7513 }
7514
7515 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
7516 fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007517 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007518
Bram Moolenaar5195e452005-08-19 20:32:47 +00007519 /* SN_MIDWORD: <midword> */
7520 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007521 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007522 putc(SN_MIDWORD, fd); /* <sectionID> */
7523 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7524
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007525 i = STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007526 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007527 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
7528 }
7529
Bram Moolenaar5195e452005-08-19 20:32:47 +00007530 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
7531 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007532 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007533 putc(SN_PREFCOND, fd); /* <sectionID> */
7534 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7535
7536 l = write_spell_prefcond(NULL, &spin->si_prefcond);
7537 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7538
7539 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007540 }
7541
Bram Moolenaar5195e452005-08-19 20:32:47 +00007542 /* SN_REP: <repcount> <rep> ...
Bram Moolenaar4770d092006-01-12 23:22:24 +00007543 * SN_SAL: <salflags> <salcount> <sal> ...
7544 * SN_REPSAL: <repcount> <rep> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007545
Bram Moolenaar5195e452005-08-19 20:32:47 +00007546 /* round 1: SN_REP section
Bram Moolenaar4770d092006-01-12 23:22:24 +00007547 * round 2: SN_SAL section (unless SN_SOFO is used)
7548 * round 3: SN_REPSAL section */
7549 for (round = 1; round <= 3; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007550 {
7551 if (round == 1)
7552 gap = &spin->si_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007553 else if (round == 2)
7554 {
7555 /* Don't write SN_SAL when using a SN_SOFO section */
7556 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
7557 continue;
7558 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007559 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007560 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00007561 gap = &spin->si_repsal;
7562
7563 /* Don't write the section if there are no items. */
7564 if (gap->ga_len == 0)
7565 continue;
7566
7567 /* Sort the REP/REPSAL items. */
7568 if (round != 2)
7569 qsort(gap->ga_data, (size_t)gap->ga_len,
7570 sizeof(fromto_T), rep_compare);
7571
7572 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
7573 putc(i, fd); /* <sectionID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007574
Bram Moolenaar5195e452005-08-19 20:32:47 +00007575 /* This is for making suggestions, section is not required. */
7576 putc(0, fd); /* <sectionflags> */
7577
7578 /* Compute the length of what follows. */
7579 l = 2; /* count <repcount> or <salcount> */
7580 for (i = 0; i < gap->ga_len; ++i)
7581 {
7582 ftp = &((fromto_T *)gap->ga_data)[i];
7583 l += 1 + STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
7584 l += 1 + STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
7585 }
7586 if (round == 2)
7587 ++l; /* count <salflags> */
7588 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7589
7590 if (round == 2)
7591 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007592 i = 0;
7593 if (spin->si_followup)
7594 i |= SAL_F0LLOWUP;
7595 if (spin->si_collapse)
7596 i |= SAL_COLLAPSE;
7597 if (spin->si_rem_accents)
7598 i |= SAL_REM_ACCENTS;
7599 putc(i, fd); /* <salflags> */
7600 }
7601
7602 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
7603 for (i = 0; i < gap->ga_len; ++i)
7604 {
7605 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
7606 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
7607 ftp = &((fromto_T *)gap->ga_data)[i];
7608 for (rr = 1; rr <= 2; ++rr)
7609 {
7610 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
7611 l = STRLEN(p);
7612 putc(l, fd);
7613 fwrite(p, l, (size_t)1, fd);
7614 }
7615 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007616
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007617 }
7618
Bram Moolenaar5195e452005-08-19 20:32:47 +00007619 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
7620 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007621 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
7622 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007623 putc(SN_SOFO, fd); /* <sectionID> */
7624 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007625
7626 l = STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007627 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
7628 /* <sectionlen> */
7629
7630 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
7631 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007632
7633 l = STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007634 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
7635 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007636 }
7637
Bram Moolenaar4770d092006-01-12 23:22:24 +00007638 /* SN_WORDS: <word> ...
7639 * This is for making suggestions, section is not required. */
7640 if (spin->si_commonwords.ht_used > 0)
7641 {
7642 putc(SN_WORDS, fd); /* <sectionID> */
7643 putc(0, fd); /* <sectionflags> */
7644
7645 /* round 1: count the bytes
7646 * round 2: write the bytes */
7647 for (round = 1; round <= 2; ++round)
7648 {
7649 int todo;
7650 int len = 0;
7651 hashitem_T *hi;
7652
7653 todo = spin->si_commonwords.ht_used;
7654 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
7655 if (!HASHITEM_EMPTY(hi))
7656 {
7657 l = STRLEN(hi->hi_key) + 1;
7658 len += l;
7659 if (round == 2) /* <word> */
7660 fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
7661 --todo;
7662 }
7663 if (round == 1)
7664 put_bytes(fd, (long_u)len, 4); /* <sectionlen> */
7665 }
7666 }
7667
Bram Moolenaar5195e452005-08-19 20:32:47 +00007668 /* SN_MAP: <mapstr>
7669 * This is for making suggestions, section is not required. */
7670 if (spin->si_map.ga_len > 0)
7671 {
7672 putc(SN_MAP, fd); /* <sectionID> */
7673 putc(0, fd); /* <sectionflags> */
7674 l = spin->si_map.ga_len;
7675 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7676 fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
7677 /* <mapstr> */
7678 }
7679
Bram Moolenaar4770d092006-01-12 23:22:24 +00007680 /* SN_SUGFILE: <timestamp>
7681 * This is used to notify that a .sug file may be available and at the
7682 * same time allows for checking that a .sug file that is found matches
7683 * with this .spl file. That's because the word numbers must be exactly
7684 * right. */
7685 if (!spin->si_nosugfile
7686 && (spin->si_sal.ga_len > 0
7687 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
7688 {
7689 putc(SN_SUGFILE, fd); /* <sectionID> */
7690 putc(0, fd); /* <sectionflags> */
7691 put_bytes(fd, (long_u)8, 4); /* <sectionlen> */
7692
7693 /* Set si_sugtime and write it to the file. */
7694 spin->si_sugtime = time(NULL);
7695 put_sugtime(spin, fd); /* <timestamp> */
7696 }
7697
Bram Moolenaare1438bb2006-03-01 22:01:55 +00007698 /* SN_NOSPLITSUGS: nothing
7699 * This is used to notify that no suggestions with word splits are to be
7700 * made. */
7701 if (spin->si_nosplitsugs)
7702 {
7703 putc(SN_NOSPLITSUGS, fd); /* <sectionID> */
7704 putc(0, fd); /* <sectionflags> */
7705 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
7706 }
7707
Bram Moolenaar5195e452005-08-19 20:32:47 +00007708 /* SN_COMPOUND: compound info.
7709 * We don't mark it required, when not supported all compound words will
7710 * be bad words. */
7711 if (spin->si_compflags != NULL)
7712 {
7713 putc(SN_COMPOUND, fd); /* <sectionID> */
7714 putc(0, fd); /* <sectionflags> */
7715
7716 l = STRLEN(spin->si_compflags);
7717 put_bytes(fd, (long_u)(l + 3), 4); /* <sectionlen> */
7718 putc(spin->si_compmax, fd); /* <compmax> */
7719 putc(spin->si_compminlen, fd); /* <compminlen> */
7720 putc(spin->si_compsylmax, fd); /* <compsylmax> */
7721 /* <compflags> */
7722 fwrite(spin->si_compflags, (size_t)l, (size_t)1, fd);
7723 }
7724
Bram Moolenaar78622822005-08-23 21:00:13 +00007725 /* SN_NOBREAK: NOBREAK flag */
7726 if (spin->si_nobreak)
7727 {
7728 putc(SN_NOBREAK, fd); /* <sectionID> */
7729 putc(0, fd); /* <sectionflags> */
7730
7731 /* It's empty, the precense of the section flags the feature. */
7732 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
7733 }
7734
Bram Moolenaar5195e452005-08-19 20:32:47 +00007735 /* SN_SYLLABLE: syllable info.
7736 * We don't mark it required, when not supported syllables will not be
7737 * counted. */
7738 if (spin->si_syllable != NULL)
7739 {
7740 putc(SN_SYLLABLE, fd); /* <sectionID> */
7741 putc(0, fd); /* <sectionflags> */
7742
7743 l = STRLEN(spin->si_syllable);
7744 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7745 fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd); /* <syllable> */
7746 }
7747
7748 /* end of <SECTIONS> */
7749 putc(SN_END, fd); /* <sectionend> */
7750
Bram Moolenaar50cde822005-06-05 21:54:54 +00007751
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007752 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007753 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007754 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007755 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007756 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007757 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007758 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007759 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007760 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007761 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007762 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007763 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007764
Bram Moolenaar0c405862005-06-22 22:26:26 +00007765 /* Clear the index and wnode fields in the tree. */
7766 clear_node(tree);
7767
Bram Moolenaar51485f02005-06-04 21:55:20 +00007768 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00007769 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00007770 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007771 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007772
Bram Moolenaar51485f02005-06-04 21:55:20 +00007773 /* number of nodes in 4 bytes */
7774 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00007775 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007776
Bram Moolenaar51485f02005-06-04 21:55:20 +00007777 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007778 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007779 }
7780
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007781 /* Write another byte to check for errors. */
7782 if (putc(0, fd) == EOF)
7783 retval = FAIL;
7784
7785 if (fclose(fd) == EOF)
7786 retval = FAIL;
7787
7788 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007789}
7790
7791/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007792 * Clear the index and wnode fields of "node", it siblings and its
7793 * children. This is needed because they are a union with other items to save
7794 * space.
7795 */
7796 static void
7797clear_node(node)
7798 wordnode_T *node;
7799{
7800 wordnode_T *np;
7801
7802 if (node != NULL)
7803 for (np = node; np != NULL; np = np->wn_sibling)
7804 {
7805 np->wn_u1.index = 0;
7806 np->wn_u2.wnode = NULL;
7807
7808 if (np->wn_byte != NUL)
7809 clear_node(np->wn_child);
7810 }
7811}
7812
7813
7814/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007815 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007816 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00007817 * This first writes the list of possible bytes (siblings). Then for each
7818 * byte recursively write the children.
7819 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00007820 * NOTE: The code here must match the code in read_tree_node(), since
7821 * assumptions are made about the indexes (so that we don't have to write them
7822 * in the file).
Bram Moolenaar51485f02005-06-04 21:55:20 +00007823 *
7824 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007825 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007826 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00007827put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007828 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007829 wordnode_T *node;
7830 int index;
7831 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007832 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007833{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007834 int newindex = index;
7835 int siblingcount = 0;
7836 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007837 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007838
Bram Moolenaar51485f02005-06-04 21:55:20 +00007839 /* If "node" is zero the tree is empty. */
7840 if (node == NULL)
7841 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007842
Bram Moolenaar51485f02005-06-04 21:55:20 +00007843 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007844 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007845
7846 /* Count the number of siblings. */
7847 for (np = node; np != NULL; np = np->wn_sibling)
7848 ++siblingcount;
7849
7850 /* Write the sibling count. */
7851 if (fd != NULL)
7852 putc(siblingcount, fd); /* <siblingcount> */
7853
7854 /* Write each sibling byte and optionally extra info. */
7855 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007856 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007857 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007858 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007859 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007860 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007861 /* For a NUL byte (end of word) write the flags etc. */
7862 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007863 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007864 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007865 * associated condition nr (stored in wn_region). The
7866 * byte value is misused to store the "rare" and "not
7867 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00007868 if (np->wn_flags == (short_u)PFX_FLAGS)
7869 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007870 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00007871 {
7872 putc(BY_FLAGS, fd); /* <byte> */
7873 putc(np->wn_flags, fd); /* <pflags> */
7874 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007875 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007876 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007877 }
7878 else
7879 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007880 /* For word trees we write the flag/region items. */
7881 flags = np->wn_flags;
7882 if (regionmask != 0 && np->wn_region != regionmask)
7883 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007884 if (np->wn_affixID != 0)
7885 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007886 if (flags == 0)
7887 {
7888 /* word without flags or region */
7889 putc(BY_NOFLAGS, fd); /* <byte> */
7890 }
7891 else
7892 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007893 if (np->wn_flags >= 0x100)
7894 {
7895 putc(BY_FLAGS2, fd); /* <byte> */
7896 putc(flags, fd); /* <flags> */
7897 putc((unsigned)flags >> 8, fd); /* <flags2> */
7898 }
7899 else
7900 {
7901 putc(BY_FLAGS, fd); /* <byte> */
7902 putc(flags, fd); /* <flags> */
7903 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007904 if (flags & WF_REGION)
7905 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007906 if (flags & WF_AFX)
7907 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007908 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007909 }
7910 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007911 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007912 else
7913 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00007914 if (np->wn_child->wn_u1.index != 0
7915 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007916 {
7917 /* The child is written elsewhere, write the reference. */
7918 if (fd != NULL)
7919 {
7920 putc(BY_INDEX, fd); /* <byte> */
7921 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007922 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007923 }
7924 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00007925 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007926 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007927 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007928
Bram Moolenaar51485f02005-06-04 21:55:20 +00007929 if (fd != NULL)
7930 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
7931 {
7932 EMSG(_(e_write));
7933 return 0;
7934 }
7935 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007936 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007937
7938 /* Space used in the array when reading: one for each sibling and one for
7939 * the count. */
7940 newindex += siblingcount + 1;
7941
7942 /* Recursively dump the children of each sibling. */
7943 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007944 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
7945 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007946 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007947
7948 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007949}
7950
7951
7952/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00007953 * ":mkspell [-ascii] outfile infile ..."
7954 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007955 */
7956 void
7957ex_mkspell(eap)
7958 exarg_T *eap;
7959{
7960 int fcount;
7961 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007962 char_u *arg = eap->arg;
7963 int ascii = FALSE;
7964
7965 if (STRNCMP(arg, "-ascii", 6) == 0)
7966 {
7967 ascii = TRUE;
7968 arg = skipwhite(arg + 6);
7969 }
7970
7971 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
7972 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
7973 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007974 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007975 FreeWild(fcount, fnames);
7976 }
7977}
7978
7979/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00007980 * Create the .sug file.
7981 * Uses the soundfold info in "spin".
7982 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
7983 */
7984 static void
7985spell_make_sugfile(spin, wfname)
7986 spellinfo_T *spin;
7987 char_u *wfname;
7988{
7989 char_u fname[MAXPATHL];
7990 int len;
7991 slang_T *slang;
7992 int free_slang = FALSE;
7993
7994 /*
7995 * Read back the .spl file that was written. This fills the required
7996 * info for soundfolding. This also uses less memory than the
7997 * pointer-linked version of the trie. And it avoids having two versions
7998 * of the code for the soundfolding stuff.
7999 * It might have been done already by spell_reload_one().
8000 */
8001 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
8002 if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME)
8003 break;
8004 if (slang == NULL)
8005 {
8006 spell_message(spin, (char_u *)_("Reading back spell file..."));
8007 slang = spell_load_file(wfname, NULL, NULL, FALSE);
8008 if (slang == NULL)
8009 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008010 free_slang = TRUE;
8011 }
8012
8013 /*
8014 * Clear the info in "spin" that is used.
8015 */
8016 spin->si_blocks = NULL;
8017 spin->si_blocks_cnt = 0;
8018 spin->si_compress_cnt = 0; /* will stay at 0 all the time*/
8019 spin->si_free_count = 0;
8020 spin->si_first_free = NULL;
8021 spin->si_foldwcount = 0;
8022
8023 /*
8024 * Go through the trie of good words, soundfold each word and add it to
8025 * the soundfold trie.
8026 */
8027 spell_message(spin, (char_u *)_("Performing soundfolding..."));
8028 if (sug_filltree(spin, slang) == FAIL)
8029 goto theend;
8030
8031 /*
8032 * Create the table which links each soundfold word with a list of the
8033 * good words it may come from. Creates buffer "spin->si_spellbuf".
8034 * This also removes the wordnr from the NUL byte entries to make
8035 * compression possible.
8036 */
8037 if (sug_maketable(spin) == FAIL)
8038 goto theend;
8039
8040 smsg((char_u *)_("Number of words after soundfolding: %ld"),
8041 (long)spin->si_spellbuf->b_ml.ml_line_count);
8042
8043 /*
8044 * Compress the soundfold trie.
8045 */
8046 spell_message(spin, (char_u *)_(msg_compressing));
8047 wordtree_compress(spin, spin->si_foldroot);
8048
8049 /*
8050 * Write the .sug file.
8051 * Make the file name by changing ".spl" to ".sug".
8052 */
8053 STRCPY(fname, wfname);
8054 len = STRLEN(fname);
8055 fname[len - 2] = 'u';
8056 fname[len - 1] = 'g';
8057 sug_write(spin, fname);
8058
8059theend:
8060 if (free_slang)
8061 slang_free(slang);
8062 free_blocks(spin->si_blocks);
8063 close_spellbuf(spin->si_spellbuf);
8064}
8065
8066/*
8067 * Build the soundfold trie for language "slang".
8068 */
8069 static int
8070sug_filltree(spin, slang)
8071 spellinfo_T *spin;
8072 slang_T *slang;
8073{
8074 char_u *byts;
8075 idx_T *idxs;
8076 int depth;
8077 idx_T arridx[MAXWLEN];
8078 int curi[MAXWLEN];
8079 char_u tword[MAXWLEN];
8080 char_u tsalword[MAXWLEN];
8081 int c;
8082 idx_T n;
8083 unsigned words_done = 0;
8084 int wordcount[MAXWLEN];
8085
8086 /* We use si_foldroot for the souldfolded trie. */
8087 spin->si_foldroot = wordtree_alloc(spin);
8088 if (spin->si_foldroot == NULL)
8089 return FAIL;
8090
8091 /* let tree_add_word() know we're adding to the soundfolded tree */
8092 spin->si_sugtree = TRUE;
8093
8094 /*
8095 * Go through the whole case-folded tree, soundfold each word and put it
8096 * in the trie.
8097 */
8098 byts = slang->sl_fbyts;
8099 idxs = slang->sl_fidxs;
8100
8101 arridx[0] = 0;
8102 curi[0] = 1;
8103 wordcount[0] = 0;
8104
8105 depth = 0;
8106 while (depth >= 0 && !got_int)
8107 {
8108 if (curi[depth] > byts[arridx[depth]])
8109 {
8110 /* Done all bytes at this node, go up one level. */
8111 idxs[arridx[depth]] = wordcount[depth];
8112 if (depth > 0)
8113 wordcount[depth - 1] += wordcount[depth];
8114
8115 --depth;
8116 line_breakcheck();
8117 }
8118 else
8119 {
8120
8121 /* Do one more byte at this node. */
8122 n = arridx[depth] + curi[depth];
8123 ++curi[depth];
8124
8125 c = byts[n];
8126 if (c == 0)
8127 {
8128 /* Sound-fold the word. */
8129 tword[depth] = NUL;
8130 spell_soundfold(slang, tword, TRUE, tsalword);
8131
8132 /* We use the "flags" field for the MSB of the wordnr,
8133 * "region" for the LSB of the wordnr. */
8134 if (tree_add_word(spin, tsalword, spin->si_foldroot,
8135 words_done >> 16, words_done & 0xffff,
8136 0) == FAIL)
8137 return FAIL;
8138
8139 ++words_done;
8140 ++wordcount[depth];
8141
8142 /* Reset the block count each time to avoid compression
8143 * kicking in. */
8144 spin->si_blocks_cnt = 0;
8145
8146 /* Skip over any other NUL bytes (same word with different
8147 * flags). */
8148 while (byts[n + 1] == 0)
8149 {
8150 ++n;
8151 ++curi[depth];
8152 }
8153 }
8154 else
8155 {
8156 /* Normal char, go one level deeper. */
8157 tword[depth++] = c;
8158 arridx[depth] = idxs[n];
8159 curi[depth] = 1;
8160 wordcount[depth] = 0;
8161 }
8162 }
8163 }
8164
8165 smsg((char_u *)_("Total number of words: %d"), words_done);
8166
8167 return OK;
8168}
8169
8170/*
8171 * Make the table that links each word in the soundfold trie to the words it
8172 * can be produced from.
8173 * This is not unlike lines in a file, thus use a memfile to be able to access
8174 * the table efficiently.
8175 * Returns FAIL when out of memory.
8176 */
8177 static int
8178sug_maketable(spin)
8179 spellinfo_T *spin;
8180{
8181 garray_T ga;
8182 int res = OK;
8183
8184 /* Allocate a buffer, open a memline for it and create the swap file
8185 * (uses a temp file, not a .swp file). */
8186 spin->si_spellbuf = open_spellbuf();
8187 if (spin->si_spellbuf == NULL)
8188 return FAIL;
8189
8190 /* Use a buffer to store the line info, avoids allocating many small
8191 * pieces of memory. */
8192 ga_init2(&ga, 1, 100);
8193
8194 /* recursively go through the tree */
8195 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
8196 res = FAIL;
8197
8198 ga_clear(&ga);
8199 return res;
8200}
8201
8202/*
8203 * Fill the table for one node and its children.
8204 * Returns the wordnr at the start of the node.
8205 * Returns -1 when out of memory.
8206 */
8207 static int
8208sug_filltable(spin, node, startwordnr, gap)
8209 spellinfo_T *spin;
8210 wordnode_T *node;
8211 int startwordnr;
8212 garray_T *gap; /* place to store line of numbers */
8213{
8214 wordnode_T *p, *np;
8215 int wordnr = startwordnr;
8216 int nr;
8217 int prev_nr;
8218
8219 for (p = node; p != NULL; p = p->wn_sibling)
8220 {
8221 if (p->wn_byte == NUL)
8222 {
8223 gap->ga_len = 0;
8224 prev_nr = 0;
8225 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
8226 {
8227 if (ga_grow(gap, 10) == FAIL)
8228 return -1;
8229
8230 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
8231 /* Compute the offset from the previous nr and store the
8232 * offset in a way that it takes a minimum number of bytes.
8233 * It's a bit like utf-8, but without the need to mark
8234 * following bytes. */
8235 nr -= prev_nr;
8236 prev_nr += nr;
8237 gap->ga_len += offset2bytes(nr,
8238 (char_u *)gap->ga_data + gap->ga_len);
8239 }
8240
8241 /* add the NUL byte */
8242 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
8243
8244 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
8245 gap->ga_data, gap->ga_len, TRUE) == FAIL)
8246 return -1;
8247 ++wordnr;
8248
8249 /* Remove extra NUL entries, we no longer need them. We don't
8250 * bother freeing the nodes, the won't be reused anyway. */
8251 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
8252 p->wn_sibling = p->wn_sibling->wn_sibling;
8253
8254 /* Clear the flags on the remaining NUL node, so that compression
8255 * works a lot better. */
8256 p->wn_flags = 0;
8257 p->wn_region = 0;
8258 }
8259 else
8260 {
8261 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
8262 if (wordnr == -1)
8263 return -1;
8264 }
8265 }
8266 return wordnr;
8267}
8268
8269/*
8270 * Convert an offset into a minimal number of bytes.
8271 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
8272 * bytes.
8273 */
8274 static int
8275offset2bytes(nr, buf)
8276 int nr;
8277 char_u *buf;
8278{
8279 int rem;
8280 int b1, b2, b3, b4;
8281
8282 /* Split the number in parts of base 255. We need to avoid NUL bytes. */
8283 b1 = nr % 255 + 1;
8284 rem = nr / 255;
8285 b2 = rem % 255 + 1;
8286 rem = rem / 255;
8287 b3 = rem % 255 + 1;
8288 b4 = rem / 255 + 1;
8289
8290 if (b4 > 1 || b3 > 0x1f) /* 4 bytes */
8291 {
8292 buf[0] = 0xe0 + b4;
8293 buf[1] = b3;
8294 buf[2] = b2;
8295 buf[3] = b1;
8296 return 4;
8297 }
8298 if (b3 > 1 || b2 > 0x3f ) /* 3 bytes */
8299 {
8300 buf[0] = 0xc0 + b3;
8301 buf[1] = b2;
8302 buf[2] = b1;
8303 return 3;
8304 }
8305 if (b2 > 1 || b1 > 0x7f ) /* 2 bytes */
8306 {
8307 buf[0] = 0x80 + b2;
8308 buf[1] = b1;
8309 return 2;
8310 }
8311 /* 1 byte */
8312 buf[0] = b1;
8313 return 1;
8314}
8315
8316/*
8317 * Opposite of offset2bytes().
8318 * "pp" points to the bytes and is advanced over it.
8319 * Returns the offset.
8320 */
8321 static int
8322bytes2offset(pp)
8323 char_u **pp;
8324{
8325 char_u *p = *pp;
8326 int nr;
8327 int c;
8328
8329 c = *p++;
8330 if ((c & 0x80) == 0x00) /* 1 byte */
8331 {
8332 nr = c - 1;
8333 }
8334 else if ((c & 0xc0) == 0x80) /* 2 bytes */
8335 {
8336 nr = (c & 0x3f) - 1;
8337 nr = nr * 255 + (*p++ - 1);
8338 }
8339 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
8340 {
8341 nr = (c & 0x1f) - 1;
8342 nr = nr * 255 + (*p++ - 1);
8343 nr = nr * 255 + (*p++ - 1);
8344 }
8345 else /* 4 bytes */
8346 {
8347 nr = (c & 0x0f) - 1;
8348 nr = nr * 255 + (*p++ - 1);
8349 nr = nr * 255 + (*p++ - 1);
8350 nr = nr * 255 + (*p++ - 1);
8351 }
8352
8353 *pp = p;
8354 return nr;
8355}
8356
8357/*
8358 * Write the .sug file in "fname".
8359 */
8360 static void
8361sug_write(spin, fname)
8362 spellinfo_T *spin;
8363 char_u *fname;
8364{
8365 FILE *fd;
8366 wordnode_T *tree;
8367 int nodecount;
8368 int wcount;
8369 char_u *line;
8370 linenr_T lnum;
8371 int len;
8372
8373 /* Create the file. Note that an existing file is silently overwritten! */
8374 fd = mch_fopen((char *)fname, "w");
8375 if (fd == NULL)
8376 {
8377 EMSG2(_(e_notopen), fname);
8378 return;
8379 }
8380
8381 vim_snprintf((char *)IObuff, IOSIZE,
8382 _("Writing suggestion file %s ..."), fname);
8383 spell_message(spin, IObuff);
8384
8385 /*
8386 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
8387 */
8388 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) /* <fileID> */
8389 {
8390 EMSG(_(e_write));
8391 goto theend;
8392 }
8393 putc(VIMSUGVERSION, fd); /* <versionnr> */
8394
8395 /* Write si_sugtime to the file. */
8396 put_sugtime(spin, fd); /* <timestamp> */
8397
8398 /*
8399 * <SUGWORDTREE>
8400 */
8401 spin->si_memtot = 0;
8402 tree = spin->si_foldroot->wn_sibling;
8403
8404 /* Clear the index and wnode fields in the tree. */
8405 clear_node(tree);
8406
8407 /* Count the number of nodes. Needed to be able to allocate the
8408 * memory when reading the nodes. Also fills in index for shared
8409 * nodes. */
8410 nodecount = put_node(NULL, tree, 0, 0, FALSE);
8411
8412 /* number of nodes in 4 bytes */
8413 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
8414 spin->si_memtot += nodecount + nodecount * sizeof(int);
8415
8416 /* Write the nodes. */
8417 (void)put_node(fd, tree, 0, 0, FALSE);
8418
8419 /*
8420 * <SUGTABLE>: <sugwcount> <sugline> ...
8421 */
8422 wcount = spin->si_spellbuf->b_ml.ml_line_count;
8423 put_bytes(fd, (long_u)wcount, 4); /* <sugwcount> */
8424
8425 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
8426 {
8427 /* <sugline>: <sugnr> ... NUL */
8428 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
8429 len = STRLEN(line) + 1;
8430 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
8431 {
8432 EMSG(_(e_write));
8433 goto theend;
8434 }
8435 spin->si_memtot += len;
8436 }
8437
8438 /* Write another byte to check for errors. */
8439 if (putc(0, fd) == EOF)
8440 EMSG(_(e_write));
8441
8442 vim_snprintf((char *)IObuff, IOSIZE,
8443 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
8444 spell_message(spin, IObuff);
8445
8446theend:
8447 /* close the file */
8448 fclose(fd);
8449}
8450
8451/*
8452 * Open a spell buffer. This is a nameless buffer that is not in the buffer
8453 * list and only contains text lines. Can use a swapfile to reduce memory
8454 * use.
8455 * Most other fields are invalid! Esp. watch out for string options being
8456 * NULL and there is no undo info.
8457 * Returns NULL when out of memory.
8458 */
8459 static buf_T *
8460open_spellbuf()
8461{
8462 buf_T *buf;
8463
8464 buf = (buf_T *)alloc_clear(sizeof(buf_T));
8465 if (buf != NULL)
8466 {
8467 buf->b_spell = TRUE;
8468 buf->b_p_swf = TRUE; /* may create a swap file */
8469 ml_open(buf);
8470 ml_open_file(buf); /* create swap file now */
8471 }
8472 return buf;
8473}
8474
8475/*
8476 * Close the buffer used for spell info.
8477 */
8478 static void
8479close_spellbuf(buf)
8480 buf_T *buf;
8481{
8482 if (buf != NULL)
8483 {
8484 ml_close(buf, TRUE);
8485 vim_free(buf);
8486 }
8487}
8488
8489
8490/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008491 * Create a Vim spell file from one or more word lists.
8492 * "fnames[0]" is the output file name.
8493 * "fnames[fcount - 1]" is the last input file name.
8494 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
8495 * and ".spl" is appended to make the output file name.
8496 */
8497 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008498mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00008499 int fcount;
8500 char_u **fnames;
8501 int ascii; /* -ascii argument given */
8502 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008503 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008504{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008505 char_u fname[MAXPATHL];
8506 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00008507 char_u **innames;
8508 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008509 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008510 int i;
8511 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008512 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008513 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008514 spellinfo_T spin;
8515
8516 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008517 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008518 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008519 spin.si_followup = TRUE;
8520 spin.si_rem_accents = TRUE;
8521 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008522 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008523 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
8524 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008525 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008526 hash_init(&spin.si_commonwords);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008527 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008528
Bram Moolenaarb765d632005-06-07 21:00:02 +00008529 /* default: fnames[0] is output file, following are input files */
8530 innames = &fnames[1];
8531 incount = fcount - 1;
8532
8533 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00008534 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008535 len = STRLEN(fnames[0]);
8536 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
8537 {
8538 /* For ":mkspell path/en.latin1.add" output file is
8539 * "path/en.latin1.add.spl". */
8540 innames = &fnames[0];
8541 incount = 1;
8542 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
8543 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008544 else if (fcount == 1)
8545 {
8546 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
8547 innames = &fnames[0];
8548 incount = 1;
8549 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
8550 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
8551 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00008552 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
8553 {
8554 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008555 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008556 }
8557 else
8558 /* Name should be language, make the file name from it. */
8559 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
8560 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
8561
8562 /* Check for .ascii.spl. */
8563 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
8564 spin.si_ascii = TRUE;
8565
8566 /* Check for .add.spl. */
8567 if (strstr((char *)gettail(wfname), ".add.") != NULL)
8568 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00008569 }
8570
Bram Moolenaarb765d632005-06-07 21:00:02 +00008571 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008572 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008573 else if (vim_strchr(gettail(wfname), '_') != NULL)
8574 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00008575 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008576 EMSG(_("E754: Only up to 8 regions supported"));
8577 else
8578 {
8579 /* Check for overwriting before doing things that may take a lot of
8580 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008581 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008582 {
8583 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00008584 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008585 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00008586 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008587 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008588 EMSG2(_(e_isadir2), wfname);
8589 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008590 }
8591
8592 /*
8593 * Init the aff and dic pointers.
8594 * Get the region names if there are more than 2 arguments.
8595 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008596 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008597 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008598 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008599
Bram Moolenaar3982c542005-06-08 21:56:31 +00008600 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008601 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008602 len = STRLEN(innames[i]);
8603 if (STRLEN(gettail(innames[i])) < 5
8604 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008605 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008606 EMSG2(_("E755: Invalid region in %s"), innames[i]);
8607 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008608 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00008609 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
8610 spin.si_region_name[i * 2 + 1] =
8611 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008612 }
8613 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00008614 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008615
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008616 spin.si_foldroot = wordtree_alloc(&spin);
8617 spin.si_keeproot = wordtree_alloc(&spin);
8618 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008619 if (spin.si_foldroot == NULL
8620 || spin.si_keeproot == NULL
8621 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008622 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008623 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008624 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008625 }
8626
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008627 /* When not producing a .add.spl file clear the character table when
8628 * we encounter one in the .aff file. This means we dump the current
8629 * one in the .spl file if the .aff file doesn't define one. That's
8630 * better than guessing the contents, the table will match a
8631 * previously loaded spell file. */
8632 if (!spin.si_add)
8633 spin.si_clear_chartab = TRUE;
8634
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008635 /*
8636 * Read all the .aff and .dic files.
8637 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00008638 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008639 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008640 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008641 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008642 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008643 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008644
Bram Moolenaarb765d632005-06-07 21:00:02 +00008645 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008646 if (mch_stat((char *)fname, &st) >= 0)
8647 {
8648 /* Read the .aff file. Will init "spin->si_conv" based on the
8649 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008650 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008651 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008652 error = TRUE;
8653 else
8654 {
8655 /* Read the .dic file and store the words in the trees. */
8656 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00008657 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008658 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008659 error = TRUE;
8660 }
8661 }
8662 else
8663 {
8664 /* No .aff file, try reading the file as a word list. Store
8665 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008666 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008667 error = TRUE;
8668 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008669
Bram Moolenaarb765d632005-06-07 21:00:02 +00008670#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008671 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008672 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008673#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008674 }
8675
Bram Moolenaar78622822005-08-23 21:00:13 +00008676 if (spin.si_compflags != NULL && spin.si_nobreak)
8677 MSG(_("Warning: both compounding and NOBREAK specified"));
8678
Bram Moolenaar4770d092006-01-12 23:22:24 +00008679 if (!error && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008680 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008681 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00008682 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008683 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008684 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008685 wordtree_compress(&spin, spin.si_foldroot);
8686 wordtree_compress(&spin, spin.si_keeproot);
8687 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008688 }
8689
Bram Moolenaar4770d092006-01-12 23:22:24 +00008690 if (!error && !got_int)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008691 {
8692 /*
8693 * Write the info in the spell file.
8694 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008695 vim_snprintf((char *)IObuff, IOSIZE,
8696 _("Writing spell file %s ..."), wfname);
8697 spell_message(&spin, IObuff);
Bram Moolenaar50cde822005-06-05 21:54:54 +00008698
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008699 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008700
Bram Moolenaar4770d092006-01-12 23:22:24 +00008701 spell_message(&spin, (char_u *)_("Done!"));
8702 vim_snprintf((char *)IObuff, IOSIZE,
8703 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
8704 spell_message(&spin, IObuff);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008705
Bram Moolenaar4770d092006-01-12 23:22:24 +00008706 /*
8707 * If the file is loaded need to reload it.
8708 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008709 if (!error)
8710 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008711 }
8712
8713 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008714 ga_clear(&spin.si_rep);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008715 ga_clear(&spin.si_repsal);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008716 ga_clear(&spin.si_sal);
8717 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008718 ga_clear(&spin.si_prefcond);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008719 hash_clear_all(&spin.si_commonwords, 0);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008720
8721 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008722 for (i = 0; i < incount; ++i)
8723 if (afile[i] != NULL)
8724 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008725
8726 /* Free all the bits and pieces at once. */
8727 free_blocks(spin.si_blocks);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008728
8729 /*
8730 * If there is soundfolding info and no NOSUGFILE item create the
8731 * .sug file with the soundfolded word trie.
8732 */
8733 if (spin.si_sugtime != 0 && !error && !got_int)
8734 spell_make_sugfile(&spin, wfname);
8735
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008736 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008737}
8738
Bram Moolenaar4770d092006-01-12 23:22:24 +00008739/*
8740 * Display a message for spell file processing when 'verbose' is set or using
8741 * ":mkspell". "str" can be IObuff.
8742 */
8743 static void
8744spell_message(spin, str)
8745 spellinfo_T *spin;
8746 char_u *str;
8747{
8748 if (spin->si_verbose || p_verbose > 2)
8749 {
8750 if (!spin->si_verbose)
8751 verbose_enter();
8752 MSG(str);
8753 out_flush();
8754 if (!spin->si_verbose)
8755 verbose_leave();
8756 }
8757}
Bram Moolenaarb765d632005-06-07 21:00:02 +00008758
8759/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008760 * ":[count]spellgood {word}"
8761 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00008762 */
8763 void
8764ex_spell(eap)
8765 exarg_T *eap;
8766{
Bram Moolenaar7887d882005-07-01 22:33:52 +00008767 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008768 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008769}
8770
8771/*
8772 * Add "word[len]" to 'spellfile' as a good or bad word.
8773 */
8774 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008775spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00008776 char_u *word;
8777 int len;
8778 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008779 int index; /* "zG" and "zW": zero, otherwise index in
8780 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008781{
8782 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008783 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008784 int new_spf = FALSE;
Bram Moolenaar7887d882005-07-01 22:33:52 +00008785 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008786 char_u fnamebuf[MAXPATHL];
8787 char_u line[MAXWLEN * 2];
8788 long fpos, fpos_next = 0;
8789 int i;
8790 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008791
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008792 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008793 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008794 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00008795 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008796 int_wordlist = vim_tempname('s');
8797 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00008798 return;
8799 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008800 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008801 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00008802 else
8803 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00008804 /* If 'spellfile' isn't set figure out a good default value. */
8805 if (*curbuf->b_p_spf == NUL)
8806 {
8807 init_spellfile();
8808 new_spf = TRUE;
8809 }
8810
8811 if (*curbuf->b_p_spf == NUL)
8812 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008813 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00008814 return;
8815 }
8816
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008817 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
8818 {
8819 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
8820 if (i == index)
8821 break;
8822 if (*spf == NUL)
8823 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00008824 EMSGN(_("E765: 'spellfile' does not have %ld entries"), index);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008825 return;
8826 }
8827 }
8828
Bram Moolenaarb765d632005-06-07 21:00:02 +00008829 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008830 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008831 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
8832 buf = NULL;
8833 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00008834 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00008835 EMSG(_(e_bufloaded));
8836 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008837 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00008838
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008839 fname = fnamebuf;
8840 }
8841
8842 if (bad)
8843 {
8844 /* When the word also appears as good word we need to remove that one,
8845 * since its flags sort before the one with WF_BANNED. */
8846 fd = mch_fopen((char *)fname, "r");
8847 if (fd != NULL)
8848 {
8849 while (!vim_fgets(line, MAXWLEN * 2, fd))
8850 {
8851 fpos = fpos_next;
8852 fpos_next = ftell(fd);
8853 if (STRNCMP(word, line, len) == 0
8854 && (line[len] == '/' || line[len] < ' '))
8855 {
8856 /* Found duplicate word. Remove it by writing a '#' at
8857 * the start of the line. Mixing reading and writing
8858 * doesn't work for all systems, close the file first. */
8859 fclose(fd);
8860 fd = mch_fopen((char *)fname, "r+");
8861 if (fd == NULL)
8862 break;
8863 if (fseek(fd, fpos, SEEK_SET) == 0)
8864 fputc('#', fd);
8865 fseek(fd, fpos_next, SEEK_SET);
8866 }
8867 }
8868 fclose(fd);
8869 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00008870 }
8871
8872 fd = mch_fopen((char *)fname, "a");
8873 if (fd == NULL && new_spf)
8874 {
8875 /* We just initialized the 'spellfile' option and can't open the file.
8876 * We may need to create the "spell" directory first. We already
8877 * checked the runtime directory is writable in init_spellfile(). */
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00008878 if (!dir_of_file_exists(fname))
Bram Moolenaar7887d882005-07-01 22:33:52 +00008879 {
8880 /* The directory doesn't exist. Try creating it and opening the
8881 * file again. */
8882 vim_mkdir(NameBuff, 0755);
8883 fd = mch_fopen((char *)fname, "a");
8884 }
8885 }
8886
8887 if (fd == NULL)
8888 EMSG2(_(e_notopen), fname);
8889 else
8890 {
8891 if (bad)
8892 fprintf(fd, "%.*s/!\n", len, word);
8893 else
8894 fprintf(fd, "%.*s\n", len, word);
8895 fclose(fd);
8896
8897 /* Update the .add.spl file. */
8898 mkspell(1, &fname, FALSE, TRUE, TRUE);
8899
8900 /* If the .add file is edited somewhere, reload it. */
8901 if (buf != NULL)
Bram Moolenaarea8bd732006-01-14 21:15:59 +00008902 buf_reload(buf, buf->b_orig_mode);
Bram Moolenaar7887d882005-07-01 22:33:52 +00008903
8904 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008905 }
8906}
8907
8908/*
8909 * Initialize 'spellfile' for the current buffer.
8910 */
8911 static void
8912init_spellfile()
8913{
8914 char_u buf[MAXPATHL];
8915 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008916 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008917 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008918 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00008919 int aspath = FALSE;
8920 char_u *lstart = curbuf->b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008921
8922 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
8923 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00008924 /* Find the end of the language name. Exclude the region. If there
8925 * is a path separator remember the start of the tail. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008926 for (lend = curbuf->b_p_spl; *lend != NUL
8927 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00008928 if (vim_ispathsep(*lend))
8929 {
8930 aspath = TRUE;
8931 lstart = lend + 1;
8932 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008933
8934 /* Loop over all entries in 'runtimepath'. Use the first one where we
8935 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008936 rtp = p_rtp;
8937 while (*rtp != NUL)
8938 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00008939 if (aspath)
8940 /* Use directory of an entry with path, e.g., for
8941 * "/dir/lg.utf-8.spl" use "/dir". */
8942 vim_strncpy(buf, curbuf->b_p_spl, lstart - curbuf->b_p_spl - 1);
8943 else
8944 /* Copy the path from 'runtimepath' to buf[]. */
8945 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00008946 if (filewritable(buf) == 2)
8947 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00008948 /* Use the first language name from 'spelllang' and the
8949 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00008950 if (aspath)
8951 vim_strncpy(buf, curbuf->b_p_spl, lend - curbuf->b_p_spl);
8952 else
8953 {
8954 l = STRLEN(buf);
8955 vim_snprintf((char *)buf + l, MAXPATHL - l,
8956 "/spell/%.*s", (int)(lend - lstart), lstart);
8957 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00008958 l = STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00008959 fname = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang->sl_fname;
8960 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
8961 fname != NULL
8962 && strstr((char *)gettail(fname), ".ascii.") != NULL
8963 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00008964 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
8965 break;
8966 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00008967 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008968 }
8969 }
8970}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008971
Bram Moolenaar51485f02005-06-04 21:55:20 +00008972
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008973/*
8974 * Init the chartab used for spelling for ASCII.
8975 * EBCDIC is not supported!
8976 */
8977 static void
8978clear_spell_chartab(sp)
8979 spelltab_T *sp;
8980{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008981 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008982
8983 /* Init everything to FALSE. */
8984 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
8985 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
8986 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008987 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008988 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008989 sp->st_upper[i] = i;
8990 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008991
8992 /* We include digits. A word shouldn't start with a digit, but handling
8993 * that is done separately. */
8994 for (i = '0'; i <= '9'; ++i)
8995 sp->st_isw[i] = TRUE;
8996 for (i = 'A'; i <= 'Z'; ++i)
8997 {
8998 sp->st_isw[i] = TRUE;
8999 sp->st_isu[i] = TRUE;
9000 sp->st_fold[i] = i + 0x20;
9001 }
9002 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009003 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009004 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009005 sp->st_upper[i] = i - 0x20;
9006 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009007}
9008
9009/*
9010 * Init the chartab used for spelling. Only depends on 'encoding'.
9011 * Called once while starting up and when 'encoding' changes.
9012 * The default is to use isalpha(), but the spell file should define the word
9013 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009014 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009015 */
9016 void
9017init_spell_chartab()
9018{
9019 int i;
9020
9021 did_set_spelltab = FALSE;
9022 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009023#ifdef FEAT_MBYTE
9024 if (enc_dbcs)
9025 {
9026 /* DBCS: assume double-wide characters are word characters. */
9027 for (i = 128; i <= 255; ++i)
9028 if (MB_BYTE2LEN(i) == 2)
9029 spelltab.st_isw[i] = TRUE;
9030 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009031 else if (enc_utf8)
9032 {
9033 for (i = 128; i < 256; ++i)
9034 {
9035 spelltab.st_isu[i] = utf_isupper(i);
9036 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
9037 spelltab.st_fold[i] = utf_fold(i);
9038 spelltab.st_upper[i] = utf_toupper(i);
9039 }
9040 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009041 else
9042#endif
9043 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009044 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009045 for (i = 128; i < 256; ++i)
9046 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009047 if (MB_ISUPPER(i))
9048 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009049 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009050 spelltab.st_isu[i] = TRUE;
9051 spelltab.st_fold[i] = MB_TOLOWER(i);
9052 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009053 else if (MB_ISLOWER(i))
9054 {
9055 spelltab.st_isw[i] = TRUE;
9056 spelltab.st_upper[i] = MB_TOUPPER(i);
9057 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009058 }
9059 }
9060}
9061
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009062/*
9063 * Set the spell character tables from strings in the affix file.
9064 */
9065 static int
9066set_spell_chartab(fol, low, upp)
9067 char_u *fol;
9068 char_u *low;
9069 char_u *upp;
9070{
9071 /* We build the new tables here first, so that we can compare with the
9072 * previous one. */
9073 spelltab_T new_st;
9074 char_u *pf = fol, *pl = low, *pu = upp;
9075 int f, l, u;
9076
9077 clear_spell_chartab(&new_st);
9078
9079 while (*pf != NUL)
9080 {
9081 if (*pl == NUL || *pu == NUL)
9082 {
9083 EMSG(_(e_affform));
9084 return FAIL;
9085 }
9086#ifdef FEAT_MBYTE
9087 f = mb_ptr2char_adv(&pf);
9088 l = mb_ptr2char_adv(&pl);
9089 u = mb_ptr2char_adv(&pu);
9090#else
9091 f = *pf++;
9092 l = *pl++;
9093 u = *pu++;
9094#endif
9095 /* Every character that appears is a word character. */
9096 if (f < 256)
9097 new_st.st_isw[f] = TRUE;
9098 if (l < 256)
9099 new_st.st_isw[l] = TRUE;
9100 if (u < 256)
9101 new_st.st_isw[u] = TRUE;
9102
9103 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9104 * case-folding */
9105 if (l < 256 && l != f)
9106 {
9107 if (f >= 256)
9108 {
9109 EMSG(_(e_affrange));
9110 return FAIL;
9111 }
9112 new_st.st_fold[l] = f;
9113 }
9114
9115 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009116 * case-folding, it's upper case and the "UPP" is the upper case of
9117 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009118 if (u < 256 && u != f)
9119 {
9120 if (f >= 256)
9121 {
9122 EMSG(_(e_affrange));
9123 return FAIL;
9124 }
9125 new_st.st_fold[u] = f;
9126 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009127 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009128 }
9129 }
9130
9131 if (*pl != NUL || *pu != NUL)
9132 {
9133 EMSG(_(e_affform));
9134 return FAIL;
9135 }
9136
9137 return set_spell_finish(&new_st);
9138}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009139
9140/*
9141 * Set the spell character tables from strings in the .spl file.
9142 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009143 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009144set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009145 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009146 int cnt; /* length of "flags" */
9147 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009148{
9149 /* We build the new tables here first, so that we can compare with the
9150 * previous one. */
9151 spelltab_T new_st;
9152 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009153 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009154 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009155
9156 clear_spell_chartab(&new_st);
9157
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009158 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009159 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009160 if (i < cnt)
9161 {
9162 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
9163 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
9164 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009165
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009166 if (*p != NUL)
9167 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009168#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009169 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009170#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009171 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009172#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009173 new_st.st_fold[i + 128] = c;
9174 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
9175 new_st.st_upper[c] = i + 128;
9176 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009177 }
9178
Bram Moolenaar5195e452005-08-19 20:32:47 +00009179 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009180}
9181
9182 static int
9183set_spell_finish(new_st)
9184 spelltab_T *new_st;
9185{
9186 int i;
9187
9188 if (did_set_spelltab)
9189 {
9190 /* check that it's the same table */
9191 for (i = 0; i < 256; ++i)
9192 {
9193 if (spelltab.st_isw[i] != new_st->st_isw[i]
9194 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009195 || spelltab.st_fold[i] != new_st->st_fold[i]
9196 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009197 {
9198 EMSG(_("E763: Word characters differ between spell files"));
9199 return FAIL;
9200 }
9201 }
9202 }
9203 else
9204 {
9205 /* copy the new spelltab into the one being used */
9206 spelltab = *new_st;
9207 did_set_spelltab = TRUE;
9208 }
9209
9210 return OK;
9211}
9212
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009213/*
Bram Moolenaarea408852005-06-25 22:49:46 +00009214 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009215 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00009216 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009217 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00009218 */
9219 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009220spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00009221 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009222 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00009223{
Bram Moolenaarea408852005-06-25 22:49:46 +00009224#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009225 char_u *s;
9226 int l;
9227 int c;
9228
9229 if (has_mbyte)
9230 {
9231 l = MB_BYTE2LEN(*p);
9232 s = p;
9233 if (l == 1)
9234 {
9235 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009236 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009237 {
9238 s = p + 1; /* skip a mid-word character */
9239 l = MB_BYTE2LEN(*s);
9240 }
9241 }
9242 else
9243 {
9244 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009245 if (c < 256 ? buf->b_spell_ismw[c]
9246 : (buf->b_spell_ismw_mb != NULL
9247 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009248 {
9249 s = p + l;
9250 l = MB_BYTE2LEN(*s);
9251 }
9252 }
9253
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009254 c = mb_ptr2char(s);
9255 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009256 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009257 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009258 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009259#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009260
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009261 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
9262}
9263
9264/*
9265 * Return TRUE if "p" points to a word character.
9266 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9267 */
9268 static int
9269spell_iswordp_nmw(p)
9270 char_u *p;
9271{
9272#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009273 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009274
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009275 if (has_mbyte)
9276 {
9277 c = mb_ptr2char(p);
9278 if (c > 255)
9279 return mb_get_class(p) >= 2;
9280 return spelltab.st_isw[c];
9281 }
9282#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009283 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00009284}
9285
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009286#ifdef FEAT_MBYTE
9287/*
9288 * Return TRUE if "p" points to a word character.
9289 * Wide version of spell_iswordp().
9290 */
9291 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009292spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009293 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009294 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009295{
9296 int *s;
9297
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009298 if (*p < 256 ? buf->b_spell_ismw[*p]
9299 : (buf->b_spell_ismw_mb != NULL
9300 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009301 s = p + 1;
9302 else
9303 s = p;
9304
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009305 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009306 {
9307 if (enc_utf8)
9308 return utf_class(*s) >= 2;
9309 if (enc_dbcs)
9310 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
9311 return 0;
9312 }
9313 return spelltab.st_isw[*s];
9314}
9315#endif
9316
Bram Moolenaarea408852005-06-25 22:49:46 +00009317/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009318 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009319 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009320 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009321 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009322write_spell_prefcond(fd, gap)
9323 FILE *fd;
9324 garray_T *gap;
9325{
9326 int i;
9327 char_u *p;
9328 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009329 int totlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009330
Bram Moolenaar5195e452005-08-19 20:32:47 +00009331 if (fd != NULL)
9332 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
9333
9334 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009335
9336 for (i = 0; i < gap->ga_len; ++i)
9337 {
9338 /* <prefcond> : <condlen> <condstr> */
9339 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009340 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009341 {
9342 len = STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009343 if (fd != NULL)
9344 {
9345 fputc(len, fd);
9346 fwrite(p, (size_t)len, (size_t)1, fd);
9347 }
9348 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009349 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00009350 else if (fd != NULL)
9351 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009352 }
9353
Bram Moolenaar5195e452005-08-19 20:32:47 +00009354 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009355}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009356
9357/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009358 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
9359 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009360 * When using a multi-byte 'encoding' the length may change!
9361 * Returns FAIL when something wrong.
9362 */
9363 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009364spell_casefold(str, len, buf, buflen)
9365 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009366 int len;
9367 char_u *buf;
9368 int buflen;
9369{
9370 int i;
9371
9372 if (len >= buflen)
9373 {
9374 buf[0] = NUL;
9375 return FAIL; /* result will not fit */
9376 }
9377
9378#ifdef FEAT_MBYTE
9379 if (has_mbyte)
9380 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009381 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009382 char_u *p;
9383 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009384
9385 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009386 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009387 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009388 if (outi + MB_MAXBYTES > buflen)
9389 {
9390 buf[outi] = NUL;
9391 return FAIL;
9392 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009393 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009394 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009395 }
9396 buf[outi] = NUL;
9397 }
9398 else
9399#endif
9400 {
9401 /* Be quick for non-multibyte encodings. */
9402 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009403 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009404 buf[i] = NUL;
9405 }
9406
9407 return OK;
9408}
9409
Bram Moolenaar4770d092006-01-12 23:22:24 +00009410/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009411#define SPS_BEST 1
9412#define SPS_FAST 2
9413#define SPS_DOUBLE 4
9414
Bram Moolenaar4770d092006-01-12 23:22:24 +00009415static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
9416static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009417
9418/*
9419 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009420 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009421 */
9422 int
9423spell_check_sps()
9424{
9425 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009426 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009427 char_u buf[MAXPATHL];
9428 int f;
9429
9430 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009431 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009432
9433 for (p = p_sps; *p != NUL; )
9434 {
9435 copy_option_part(&p, buf, MAXPATHL, ",");
9436
9437 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009438 if (VIM_ISDIGIT(*buf))
9439 {
9440 s = buf;
9441 sps_limit = getdigits(&s);
9442 if (*s != NUL && !VIM_ISDIGIT(*s))
9443 f = -1;
9444 }
9445 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009446 f = SPS_BEST;
9447 else if (STRCMP(buf, "fast") == 0)
9448 f = SPS_FAST;
9449 else if (STRCMP(buf, "double") == 0)
9450 f = SPS_DOUBLE;
9451 else if (STRNCMP(buf, "expr:", 5) != 0
9452 && STRNCMP(buf, "file:", 5) != 0)
9453 f = -1;
9454
9455 if (f == -1 || (sps_flags != 0 && f != 0))
9456 {
9457 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009458 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009459 return FAIL;
9460 }
9461 if (f != 0)
9462 sps_flags = f;
9463 }
9464
9465 if (sps_flags == 0)
9466 sps_flags = SPS_BEST;
9467
9468 return OK;
9469}
9470
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009471/*
9472 * "z?": Find badly spelled word under or after the cursor.
9473 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009474 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00009475 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009476 */
9477 void
Bram Moolenaard12a1322005-08-21 22:08:24 +00009478spell_suggest(count)
9479 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009480{
9481 char_u *line;
9482 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009483 char_u wcopy[MAXWLEN + 2];
9484 char_u *p;
9485 int i;
9486 int c;
9487 suginfo_T sug;
9488 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009489 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009490 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009491 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009492 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009493 int badlen = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009494
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009495 if (no_spell_checking(curwin))
9496 return;
9497
9498#ifdef FEAT_VISUAL
9499 if (VIsual_active)
9500 {
9501 /* Use the Visually selected text as the bad word. But reject
9502 * a multi-line selection. */
9503 if (curwin->w_cursor.lnum != VIsual.lnum)
9504 {
9505 vim_beep();
9506 return;
9507 }
9508 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
9509 if (badlen < 0)
9510 badlen = -badlen;
9511 else
9512 curwin->w_cursor.col = VIsual.col;
9513 ++badlen;
9514 end_visual_mode();
9515 }
9516 else
9517#endif
9518 /* Find the start of the badly spelled word. */
9519 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +00009520 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009521 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00009522 /* No bad word or it starts after the cursor: use the word under the
9523 * cursor. */
9524 curwin->w_cursor = prev_cursor;
9525 line = ml_get_curline();
9526 p = line + curwin->w_cursor.col;
9527 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009528 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00009529 mb_ptr_back(line, p);
9530 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009531 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00009532 mb_ptr_adv(p);
9533
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009534 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00009535 {
9536 beep_flush();
9537 return;
9538 }
9539 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009540 }
9541
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009542 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009543
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009544 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009545 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009546
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009547 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009548
Bram Moolenaar5195e452005-08-19 20:32:47 +00009549 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
9550 * 'spellsuggest', whatever is smaller. */
9551 if (sps_limit > (int)Rows - 2)
9552 limit = (int)Rows - 2;
9553 else
9554 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009555 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +00009556 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009557
9558 if (sug.su_ga.ga_len == 0)
9559 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +00009560 else if (count > 0)
9561 {
9562 if (count > sug.su_ga.ga_len)
9563 smsg((char_u *)_("Sorry, only %ld suggestions"),
9564 (long)sug.su_ga.ga_len);
9565 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009566 else
9567 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009568 vim_free(repl_from);
9569 repl_from = NULL;
9570 vim_free(repl_to);
9571 repl_to = NULL;
9572
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009573#ifdef FEAT_RIGHTLEFT
9574 /* When 'rightleft' is set the list is drawn right-left. */
9575 cmdmsg_rl = curwin->w_p_rl;
9576 if (cmdmsg_rl)
9577 msg_col = Columns - 1;
9578#endif
9579
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009580 /* List the suggestions. */
9581 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009582 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009583 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
9584 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009585#ifdef FEAT_RIGHTLEFT
9586 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
9587 {
9588 /* And now the rabbit from the high hat: Avoid showing the
9589 * untranslated message rightleft. */
9590 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
9591 sug.su_badlen, sug.su_badptr);
9592 }
9593#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009594 msg_puts(IObuff);
9595 msg_clr_eos();
9596 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00009597
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009598 msg_scroll = TRUE;
9599 for (i = 0; i < sug.su_ga.ga_len; ++i)
9600 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009601 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009602
9603 /* The suggested word may replace only part of the bad word, add
9604 * the not replaced part. */
9605 STRCPY(wcopy, stp->st_word);
9606 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +00009607 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009608 sug.su_badptr + stp->st_orglen,
9609 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009610 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
9611#ifdef FEAT_RIGHTLEFT
9612 if (cmdmsg_rl)
9613 rl_mirror(IObuff);
9614#endif
9615 msg_puts(IObuff);
9616
9617 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009618 msg_puts(IObuff);
9619
9620 /* The word may replace more than "su_badlen". */
9621 if (sug.su_badlen < stp->st_orglen)
9622 {
9623 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
9624 stp->st_orglen, sug.su_badptr);
9625 msg_puts(IObuff);
9626 }
9627
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009628 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009629 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00009630 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009631 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009632 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009633 stp->st_salscore ? "s " : "",
9634 stp->st_score, stp->st_altscore);
9635 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009636 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00009637 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009638#ifdef FEAT_RIGHTLEFT
9639 if (cmdmsg_rl)
9640 /* Mirror the numbers, but keep the leading space. */
9641 rl_mirror(IObuff + 1);
9642#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00009643 msg_advance(30);
9644 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009645 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009646 msg_putchar('\n');
9647 }
9648
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009649#ifdef FEAT_RIGHTLEFT
9650 cmdmsg_rl = FALSE;
9651 msg_col = 0;
9652#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009653 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00009654 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009655 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +00009656 selected -= lines_left;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009657 }
9658
Bram Moolenaard12a1322005-08-21 22:08:24 +00009659 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
9660 {
9661 /* Save the from and to text for :spellrepall. */
9662 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00009663 if (sug.su_badlen > stp->st_orglen)
9664 {
9665 /* Replacing less than "su_badlen", append the remainder to
9666 * repl_to. */
9667 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
9668 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
9669 sug.su_badlen - stp->st_orglen,
9670 sug.su_badptr + stp->st_orglen);
9671 repl_to = vim_strsave(IObuff);
9672 }
9673 else
9674 {
9675 /* Replacing su_badlen or more, use the whole word. */
9676 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
9677 repl_to = vim_strsave(stp->st_word);
9678 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00009679
9680 /* Replace the word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009681 p = alloc(STRLEN(line) - stp->st_orglen + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +00009682 if (p != NULL)
9683 {
9684 c = sug.su_badptr - line;
9685 mch_memmove(p, line, c);
9686 STRCPY(p + c, stp->st_word);
9687 STRCAT(p, sug.su_badptr + stp->st_orglen);
9688 ml_replace(curwin->w_cursor.lnum, p, FALSE);
9689 curwin->w_cursor.col = c;
9690 changed_bytes(curwin->w_cursor.lnum, c);
9691
9692 /* For redo we use a change-word command. */
9693 ResetRedobuff();
9694 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +00009695 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +00009696 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +00009697 AppendCharToRedobuff(ESC);
9698 }
9699 }
9700 else
9701 curwin->w_cursor = prev_cursor;
9702
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009703 spell_find_cleanup(&sug);
9704}
9705
9706/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009707 * Check if the word at line "lnum" column "col" is required to start with a
9708 * capital. This uses 'spellcapcheck' of the current buffer.
9709 */
9710 static int
9711check_need_cap(lnum, col)
9712 linenr_T lnum;
9713 colnr_T col;
9714{
9715 int need_cap = FALSE;
9716 char_u *line;
9717 char_u *line_copy = NULL;
9718 char_u *p;
9719 colnr_T endcol;
9720 regmatch_T regmatch;
9721
9722 if (curbuf->b_cap_prog == NULL)
9723 return FALSE;
9724
9725 line = ml_get_curline();
9726 endcol = 0;
9727 if ((int)(skipwhite(line) - line) >= (int)col)
9728 {
9729 /* At start of line, check if previous line is empty or sentence
9730 * ends there. */
9731 if (lnum == 1)
9732 need_cap = TRUE;
9733 else
9734 {
9735 line = ml_get(lnum - 1);
9736 if (*skipwhite(line) == NUL)
9737 need_cap = TRUE;
9738 else
9739 {
9740 /* Append a space in place of the line break. */
9741 line_copy = concat_str(line, (char_u *)" ");
9742 line = line_copy;
9743 endcol = STRLEN(line);
9744 }
9745 }
9746 }
9747 else
9748 endcol = col;
9749
9750 if (endcol > 0)
9751 {
9752 /* Check if sentence ends before the bad word. */
9753 regmatch.regprog = curbuf->b_cap_prog;
9754 regmatch.rm_ic = FALSE;
9755 p = line + endcol;
9756 for (;;)
9757 {
9758 mb_ptr_back(line, p);
9759 if (p == line || spell_iswordp_nmw(p))
9760 break;
9761 if (vim_regexec(&regmatch, p, 0)
9762 && regmatch.endp[0] == line + endcol)
9763 {
9764 need_cap = TRUE;
9765 break;
9766 }
9767 }
9768 }
9769
9770 vim_free(line_copy);
9771
9772 return need_cap;
9773}
9774
9775
9776/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009777 * ":spellrepall"
9778 */
9779/*ARGSUSED*/
9780 void
9781ex_spellrepall(eap)
9782 exarg_T *eap;
9783{
9784 pos_T pos = curwin->w_cursor;
9785 char_u *frompat;
9786 int addlen;
9787 char_u *line;
9788 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009789 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009790 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009791
9792 if (repl_from == NULL || repl_to == NULL)
9793 {
9794 EMSG(_("E752: No previous spell replacement"));
9795 return;
9796 }
9797 addlen = STRLEN(repl_to) - STRLEN(repl_from);
9798
9799 frompat = alloc(STRLEN(repl_from) + 7);
9800 if (frompat == NULL)
9801 return;
9802 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
9803 p_ws = FALSE;
9804
Bram Moolenaar5195e452005-08-19 20:32:47 +00009805 sub_nsubs = 0;
9806 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009807 curwin->w_cursor.lnum = 0;
9808 while (!got_int)
9809 {
9810 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
9811 || u_save_cursor() == FAIL)
9812 break;
9813
9814 /* Only replace when the right word isn't there yet. This happens
9815 * when changing "etc" to "etc.". */
9816 line = ml_get_curline();
9817 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
9818 repl_to, STRLEN(repl_to)) != 0)
9819 {
9820 p = alloc(STRLEN(line) + addlen + 1);
9821 if (p == NULL)
9822 break;
9823 mch_memmove(p, line, curwin->w_cursor.col);
9824 STRCPY(p + curwin->w_cursor.col, repl_to);
9825 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
9826 ml_replace(curwin->w_cursor.lnum, p, FALSE);
9827 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009828
9829 if (curwin->w_cursor.lnum != prev_lnum)
9830 {
9831 ++sub_nlines;
9832 prev_lnum = curwin->w_cursor.lnum;
9833 }
9834 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009835 }
9836 curwin->w_cursor.col += STRLEN(repl_to);
9837 }
9838
9839 p_ws = save_ws;
9840 curwin->w_cursor = pos;
9841 vim_free(frompat);
9842
Bram Moolenaar5195e452005-08-19 20:32:47 +00009843 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009844 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009845 else
9846 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009847}
9848
9849/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009850 * Find spell suggestions for "word". Return them in the growarray "*gap" as
9851 * a list of allocated strings.
9852 */
9853 void
Bram Moolenaar4770d092006-01-12 23:22:24 +00009854spell_suggest_list(gap, word, maxcount, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009855 garray_T *gap;
9856 char_u *word;
9857 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009858 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009859 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009860{
9861 suginfo_T sug;
9862 int i;
9863 suggest_T *stp;
9864 char_u *wcopy;
9865
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009866 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009867
9868 /* Make room in "gap". */
9869 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00009870 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009871 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00009872 for (i = 0; i < sug.su_ga.ga_len; ++i)
9873 {
9874 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009875
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00009876 /* The suggested word may replace only part of "word", add the not
9877 * replaced part. */
9878 wcopy = alloc(stp->st_wordlen
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009879 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00009880 if (wcopy == NULL)
9881 break;
9882 STRCPY(wcopy, stp->st_word);
9883 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
9884 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
9885 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009886 }
9887
9888 spell_find_cleanup(&sug);
9889}
9890
9891/*
9892 * Find spell suggestions for the word at the start of "badptr".
9893 * Return the suggestions in "su->su_ga".
9894 * The maximum number of suggestions is "maxcount".
9895 * Note: does use info for the current window.
9896 * This is based on the mechanisms of Aspell, but completely reimplemented.
9897 */
9898 static void
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009899spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009900 char_u *badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009901 int badlen; /* length of bad word or 0 if unknown */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009902 suginfo_T *su;
9903 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00009904 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009905 int need_cap; /* word should start with capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009906 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009907{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009908 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009909 char_u buf[MAXPATHL];
9910 char_u *p;
9911 int do_combine = FALSE;
9912 char_u *sps_copy;
9913#ifdef FEAT_EVAL
9914 static int expr_busy = FALSE;
9915#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009916 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00009917 int i;
9918 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009919
9920 /*
9921 * Set the info in "*su".
9922 */
9923 vim_memset(su, 0, sizeof(suginfo_T));
9924 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
9925 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009926 if (*badptr == NUL)
9927 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009928 hash_init(&su->su_banned);
9929
9930 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009931 if (badlen != 0)
9932 su->su_badlen = badlen;
9933 else
9934 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009935 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009936 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009937
9938 if (su->su_badlen >= MAXWLEN)
9939 su->su_badlen = MAXWLEN - 1; /* just in case */
9940 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
9941 (void)spell_casefold(su->su_badptr, su->su_badlen,
9942 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009943 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009944 su->su_badflags = badword_captype(su->su_badptr,
9945 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009946 if (need_cap)
9947 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009948
Bram Moolenaar8b96d642005-09-05 22:05:30 +00009949 /* Find the default language for sound folding. We simply use the first
9950 * one in 'spelllang' that supports sound folding. That's good for when
9951 * using multiple files for one language, it's not that bad when mixing
9952 * languages (e.g., "pl,en"). */
9953 for (i = 0; i < curbuf->b_langp.ga_len; ++i)
9954 {
9955 lp = LANGP_ENTRY(curbuf->b_langp, i);
9956 if (lp->lp_sallang != NULL)
9957 {
9958 su->su_sallang = lp->lp_sallang;
9959 break;
9960 }
9961 }
9962
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009963 /* Soundfold the bad word with the default sound folding, so that we don't
9964 * have to do this many times. */
9965 if (su->su_sallang != NULL)
9966 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
9967 su->su_sal_badword);
9968
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009969 /* If the word is not capitalised and spell_check() doesn't consider the
9970 * word to be bad then it might need to be capitalised. Add a suggestion
9971 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00009972 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009973 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009974 {
9975 make_case_word(su->su_badword, buf, WF_ONECAP);
9976 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +00009977 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009978 }
9979
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009980 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00009981 if (banbadword)
9982 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009983
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009984 /* Make a copy of 'spellsuggest', because the expression may change it. */
9985 sps_copy = vim_strsave(p_sps);
9986 if (sps_copy == NULL)
9987 return;
9988
9989 /* Loop over the items in 'spellsuggest'. */
9990 for (p = sps_copy; *p != NUL; )
9991 {
9992 copy_option_part(&p, buf, MAXPATHL, ",");
9993
9994 if (STRNCMP(buf, "expr:", 5) == 0)
9995 {
9996#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009997 /* Evaluate an expression. Skip this when called recursively,
9998 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009999 if (!expr_busy)
10000 {
10001 expr_busy = TRUE;
10002 spell_suggest_expr(su, buf + 5);
10003 expr_busy = FALSE;
10004 }
10005#endif
10006 }
10007 else if (STRNCMP(buf, "file:", 5) == 0)
10008 /* Use list of suggestions in a file. */
10009 spell_suggest_file(su, buf + 5);
10010 else
10011 {
10012 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010013 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010014 if (sps_flags & SPS_DOUBLE)
10015 do_combine = TRUE;
10016 }
10017 }
10018
10019 vim_free(sps_copy);
10020
10021 if (do_combine)
10022 /* Combine the two list of suggestions. This must be done last,
10023 * because sorting changes the order again. */
10024 score_combine(su);
10025}
10026
10027#ifdef FEAT_EVAL
10028/*
10029 * Find suggestions by evaluating expression "expr".
10030 */
10031 static void
10032spell_suggest_expr(su, expr)
10033 suginfo_T *su;
10034 char_u *expr;
10035{
10036 list_T *list;
10037 listitem_T *li;
10038 int score;
10039 char_u *p;
10040
10041 /* The work is split up in a few parts to avoid having to export
10042 * suginfo_T.
10043 * First evaluate the expression and get the resulting list. */
10044 list = eval_spell_expr(su->su_badword, expr);
10045 if (list != NULL)
10046 {
10047 /* Loop over the items in the list. */
10048 for (li = list->lv_first; li != NULL; li = li->li_next)
10049 if (li->li_tv.v_type == VAR_LIST)
10050 {
10051 /* Get the word and the score from the items. */
10052 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010053 if (score >= 0 && score <= su->su_maxscore)
10054 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10055 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010056 }
10057 list_unref(list);
10058 }
10059
Bram Moolenaar4770d092006-01-12 23:22:24 +000010060 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10061 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010062 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10063}
10064#endif
10065
10066/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010067 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010068 */
10069 static void
10070spell_suggest_file(su, fname)
10071 suginfo_T *su;
10072 char_u *fname;
10073{
10074 FILE *fd;
10075 char_u line[MAXWLEN * 2];
10076 char_u *p;
10077 int len;
10078 char_u cword[MAXWLEN];
10079
10080 /* Open the file. */
10081 fd = mch_fopen((char *)fname, "r");
10082 if (fd == NULL)
10083 {
10084 EMSG2(_(e_notopen), fname);
10085 return;
10086 }
10087
10088 /* Read it line by line. */
10089 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
10090 {
10091 line_breakcheck();
10092
10093 p = vim_strchr(line, '/');
10094 if (p == NULL)
10095 continue; /* No Tab found, just skip the line. */
10096 *p++ = NUL;
10097 if (STRICMP(su->su_badword, line) == 0)
10098 {
10099 /* Match! Isolate the good word, until CR or NL. */
10100 for (len = 0; p[len] >= ' '; ++len)
10101 ;
10102 p[len] = NUL;
10103
10104 /* If the suggestion doesn't have specific case duplicate the case
10105 * of the bad word. */
10106 if (captype(p, NULL) == 0)
10107 {
10108 make_case_word(p, cword, su->su_badflags);
10109 p = cword;
10110 }
10111
10112 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010113 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010114 }
10115 }
10116
10117 fclose(fd);
10118
Bram Moolenaar4770d092006-01-12 23:22:24 +000010119 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10120 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010121 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10122}
10123
10124/*
10125 * Find suggestions for the internal method indicated by "sps_flags".
10126 */
10127 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010128spell_suggest_intern(su, interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010129 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010130 int interactive;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010131{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010132 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010133 * Load the .sug file(s) that are available and not done yet.
10134 */
10135 suggest_load_files();
10136
10137 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010138 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010139 *
10140 * Set a maximum score to limit the combination of operations that is
10141 * tried.
10142 */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010143 suggest_try_special(su);
10144
10145 /*
10146 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10147 * from the .aff file and inserting a space (split the word).
10148 */
10149 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010150
10151 /* For the resulting top-scorers compute the sound-a-like score. */
10152 if (sps_flags & SPS_DOUBLE)
10153 score_comp_sal(su);
10154
10155 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010156 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010157 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010158 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010159 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010160 if (sps_flags & SPS_BEST)
10161 /* Adjust the word score for the suggestions found so far for how
10162 * they sounds like. */
10163 rescore_suggestions(su);
10164
10165 /*
10166 * While going throught the soundfold tree "su_maxscore" is the score
10167 * for the soundfold word, limits the changes that are being tried,
10168 * and "su_sfmaxscore" the rescored score, which is set by
10169 * cleanup_suggestions().
10170 * First find words with a small edit distance, because this is much
10171 * faster and often already finds the top-N suggestions. If we didn't
10172 * find many suggestions try again with a higher edit distance.
10173 * "sl_sounddone" is used to avoid doing the same word twice.
10174 */
10175 suggest_try_soundalike_prep();
10176 su->su_maxscore = SCORE_SFMAX1;
10177 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010178 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010179 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10180 {
10181 /* We didn't find enough matches, try again, allowing more
10182 * changes to the soundfold word. */
10183 su->su_maxscore = SCORE_SFMAX2;
10184 suggest_try_soundalike(su);
10185 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10186 {
10187 /* Still didn't find enough matches, try again, allowing even
10188 * more changes to the soundfold word. */
10189 su->su_maxscore = SCORE_SFMAX3;
10190 suggest_try_soundalike(su);
10191 }
10192 }
10193 su->su_maxscore = su->su_sfmaxscore;
10194 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010195 }
10196
Bram Moolenaar4770d092006-01-12 23:22:24 +000010197 /* When CTRL-C was hit while searching do show the results. Only clear
10198 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010199 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +000010200 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010201 {
10202 (void)vgetc();
10203 got_int = FALSE;
10204 }
10205
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010206 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010207 {
10208 if (sps_flags & SPS_BEST)
10209 /* Adjust the word score for how it sounds like. */
10210 rescore_suggestions(su);
10211
Bram Moolenaar4770d092006-01-12 23:22:24 +000010212 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10213 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010214 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010215 }
10216}
10217
10218/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010219 * Load the .sug files for languages that have one and weren't loaded yet.
10220 */
10221 static void
10222suggest_load_files()
10223{
10224 langp_T *lp;
10225 int lpi;
10226 slang_T *slang;
10227 char_u *dotp;
10228 FILE *fd;
10229 char_u buf[MAXWLEN];
10230 int i;
10231 time_t timestamp;
10232 int wcount;
10233 int wordnr;
10234 garray_T ga;
10235 int c;
10236
10237 /* Do this for all languages that support sound folding. */
10238 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
10239 {
10240 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
10241 slang = lp->lp_slang;
10242 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10243 {
10244 /* Change ".spl" to ".sug" and open the file. When the file isn't
10245 * found silently skip it. Do set "sl_sugloaded" so that we
10246 * don't try again and again. */
10247 slang->sl_sugloaded = TRUE;
10248
10249 dotp = vim_strrchr(slang->sl_fname, '.');
10250 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10251 continue;
10252 STRCPY(dotp, ".sug");
10253 fd = fopen((char *)slang->sl_fname, "r");
10254 if (fd == NULL)
10255 goto nextone;
10256
10257 /*
10258 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10259 */
10260 for (i = 0; i < VIMSUGMAGICL; ++i)
10261 buf[i] = getc(fd); /* <fileID> */
10262 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10263 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010264 EMSG2(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010265 slang->sl_fname);
10266 goto nextone;
10267 }
10268 c = getc(fd); /* <versionnr> */
10269 if (c < VIMSUGVERSION)
10270 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010271 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010272 slang->sl_fname);
10273 goto nextone;
10274 }
10275 else if (c > VIMSUGVERSION)
10276 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010277 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010278 slang->sl_fname);
10279 goto nextone;
10280 }
10281
10282 /* Check the timestamp, it must be exactly the same as the one in
10283 * the .spl file. Otherwise the word numbers won't match. */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010284 timestamp = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010285 if (timestamp != slang->sl_sugtime)
10286 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010287 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010288 slang->sl_fname);
10289 goto nextone;
10290 }
10291
10292 /*
10293 * <SUGWORDTREE>: <wordtree>
10294 * Read the trie with the soundfolded words.
10295 */
10296 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
10297 FALSE, 0) != 0)
10298 {
10299someerror:
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010300 EMSG2(_("E782: error while reading .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010301 slang->sl_fname);
10302 slang_clear_sug(slang);
10303 goto nextone;
10304 }
10305
10306 /*
10307 * <SUGTABLE>: <sugwcount> <sugline> ...
10308 *
10309 * Read the table with word numbers. We use a file buffer for
10310 * this, because it's so much like a file with lines. Makes it
10311 * possible to swap the info and save on memory use.
10312 */
10313 slang->sl_sugbuf = open_spellbuf();
10314 if (slang->sl_sugbuf == NULL)
10315 goto someerror;
10316 /* <sugwcount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010317 wcount = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010318 if (wcount < 0)
10319 goto someerror;
10320
10321 /* Read all the wordnr lists into the buffer, one NUL terminated
10322 * list per line. */
10323 ga_init2(&ga, 1, 100);
10324 for (wordnr = 0; wordnr < wcount; ++wordnr)
10325 {
10326 ga.ga_len = 0;
10327 for (;;)
10328 {
10329 c = getc(fd); /* <sugline> */
10330 if (c < 0 || ga_grow(&ga, 1) == FAIL)
10331 goto someerror;
10332 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
10333 if (c == NUL)
10334 break;
10335 }
10336 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
10337 ga.ga_data, ga.ga_len, TRUE) == FAIL)
10338 goto someerror;
10339 }
10340 ga_clear(&ga);
10341
10342 /*
10343 * Need to put word counts in the word tries, so that we can find
10344 * a word by its number.
10345 */
10346 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
10347 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
10348
10349nextone:
10350 if (fd != NULL)
10351 fclose(fd);
10352 STRCPY(dotp, ".spl");
10353 }
10354 }
10355}
10356
10357
10358/*
10359 * Fill in the wordcount fields for a trie.
10360 * Returns the total number of words.
10361 */
10362 static void
10363tree_count_words(byts, idxs)
10364 char_u *byts;
10365 idx_T *idxs;
10366{
10367 int depth;
10368 idx_T arridx[MAXWLEN];
10369 int curi[MAXWLEN];
10370 int c;
10371 idx_T n;
10372 int wordcount[MAXWLEN];
10373
10374 arridx[0] = 0;
10375 curi[0] = 1;
10376 wordcount[0] = 0;
10377 depth = 0;
10378 while (depth >= 0 && !got_int)
10379 {
10380 if (curi[depth] > byts[arridx[depth]])
10381 {
10382 /* Done all bytes at this node, go up one level. */
10383 idxs[arridx[depth]] = wordcount[depth];
10384 if (depth > 0)
10385 wordcount[depth - 1] += wordcount[depth];
10386
10387 --depth;
10388 fast_breakcheck();
10389 }
10390 else
10391 {
10392 /* Do one more byte at this node. */
10393 n = arridx[depth] + curi[depth];
10394 ++curi[depth];
10395
10396 c = byts[n];
10397 if (c == 0)
10398 {
10399 /* End of word, count it. */
10400 ++wordcount[depth];
10401
10402 /* Skip over any other NUL bytes (same word with different
10403 * flags). */
10404 while (byts[n + 1] == 0)
10405 {
10406 ++n;
10407 ++curi[depth];
10408 }
10409 }
10410 else
10411 {
10412 /* Normal char, go one level deeper to count the words. */
10413 ++depth;
10414 arridx[depth] = idxs[n];
10415 curi[depth] = 1;
10416 wordcount[depth] = 0;
10417 }
10418 }
10419 }
10420}
10421
10422/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010423 * Free the info put in "*su" by spell_find_suggest().
10424 */
10425 static void
10426spell_find_cleanup(su)
10427 suginfo_T *su;
10428{
10429 int i;
10430
10431 /* Free the suggestions. */
10432 for (i = 0; i < su->su_ga.ga_len; ++i)
10433 vim_free(SUG(su->su_ga, i).st_word);
10434 ga_clear(&su->su_ga);
10435 for (i = 0; i < su->su_sga.ga_len; ++i)
10436 vim_free(SUG(su->su_sga, i).st_word);
10437 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010438
10439 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010440 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010441}
10442
10443/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010444 * Make a copy of "word", with the first letter upper or lower cased, to
10445 * "wcopy[MAXWLEN]". "word" must not be empty.
10446 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010447 */
10448 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010449onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010450 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010451 char_u *wcopy;
10452 int upper; /* TRUE: first letter made upper case */
10453{
10454 char_u *p;
10455 int c;
10456 int l;
10457
10458 p = word;
10459#ifdef FEAT_MBYTE
10460 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010461 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010462 else
10463#endif
10464 c = *p++;
10465 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010466 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010467 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010468 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010469#ifdef FEAT_MBYTE
10470 if (has_mbyte)
10471 l = mb_char2bytes(c, wcopy);
10472 else
10473#endif
10474 {
10475 l = 1;
10476 wcopy[0] = c;
10477 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010478 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010479}
10480
10481/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010482 * Make a copy of "word" with all the letters upper cased into
10483 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010484 */
10485 static void
10486allcap_copy(word, wcopy)
10487 char_u *word;
10488 char_u *wcopy;
10489{
10490 char_u *s;
10491 char_u *d;
10492 int c;
10493
10494 d = wcopy;
10495 for (s = word; *s != NUL; )
10496 {
10497#ifdef FEAT_MBYTE
10498 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010499 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010500 else
10501#endif
10502 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +000010503
10504#ifdef FEAT_MBYTE
10505 /* We only change ß to SS when we are certain latin1 is used. It
10506 * would cause weird errors in other 8-bit encodings. */
10507 if (enc_latin1like && c == 0xdf)
10508 {
10509 c = 'S';
10510 if (d - wcopy >= MAXWLEN - 1)
10511 break;
10512 *d++ = c;
10513 }
10514 else
10515#endif
10516 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010517
10518#ifdef FEAT_MBYTE
10519 if (has_mbyte)
10520 {
10521 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
10522 break;
10523 d += mb_char2bytes(c, d);
10524 }
10525 else
10526#endif
10527 {
10528 if (d - wcopy >= MAXWLEN - 1)
10529 break;
10530 *d++ = c;
10531 }
10532 }
10533 *d = NUL;
10534}
10535
10536/*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010537 * Try finding suggestions by recognizing specific situations.
10538 */
10539 static void
10540suggest_try_special(su)
10541 suginfo_T *su;
10542{
10543 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010544 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010545 int c;
10546 char_u word[MAXWLEN];
10547
10548 /*
10549 * Recognize a word that is repeated: "the the".
10550 */
10551 p = skiptowhite(su->su_fbadword);
10552 len = p - su->su_fbadword;
10553 p = skipwhite(p);
10554 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
10555 {
10556 /* Include badflags: if the badword is onecap or allcap
10557 * use that for the goodword too: "The the" -> "The". */
10558 c = su->su_fbadword[len];
10559 su->su_fbadword[len] = NUL;
10560 make_case_word(su->su_fbadword, word, su->su_badflags);
10561 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010562
10563 /* Give a soundalike score of 0, compute the score as if deleting one
10564 * character. */
10565 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010566 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010567 }
10568}
10569
10570/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010571 * Try finding suggestions by adding/removing/swapping letters.
10572 */
10573 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000010574suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010575 suginfo_T *su;
10576{
10577 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010578 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010579 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010580 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010581 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010582
10583 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +000010584 * to find matches (esp. REP items). Append some more text, changing
10585 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010586 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010587 n = STRLEN(fword);
10588 p = su->su_badptr + su->su_badlen;
10589 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010590
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010591 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010592 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010593 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010594
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010595 /* If reloading a spell file fails it's still in the list but
10596 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010597 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010598 continue;
10599
Bram Moolenaar4770d092006-01-12 23:22:24 +000010600 /* Try it for this language. Will add possible suggestions. */
10601 suggest_trie_walk(su, lp, fword, FALSE);
10602 }
10603}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010604
Bram Moolenaar4770d092006-01-12 23:22:24 +000010605/* Check the maximum score, if we go over it we won't try this change. */
10606#define TRY_DEEPER(su, stack, depth, add) \
10607 (stack[depth].ts_score + (add) < su->su_maxscore)
10608
10609/*
10610 * Try finding suggestions by adding/removing/swapping letters.
10611 *
10612 * This uses a state machine. At each node in the tree we try various
10613 * operations. When trying if an operation works "depth" is increased and the
10614 * stack[] is used to store info. This allows combinations, thus insert one
10615 * character, replace one and delete another. The number of changes is
10616 * limited by su->su_maxscore.
10617 *
10618 * After implementing this I noticed an article by Kemal Oflazer that
10619 * describes something similar: "Error-tolerant Finite State Recognition with
10620 * Applications to Morphological Analysis and Spelling Correction" (1996).
10621 * The implementation in the article is simplified and requires a stack of
10622 * unknown depth. The implementation here only needs a stack depth equal to
10623 * the length of the word.
10624 *
10625 * This is also used for the sound-folded word, "soundfold" is TRUE then.
10626 * The mechanism is the same, but we find a match with a sound-folded word
10627 * that comes from one or more original words. Each of these words may be
10628 * added, this is done by add_sound_suggest().
10629 * Don't use:
10630 * the prefix tree or the keep-case tree
10631 * "su->su_badlen"
10632 * anything to do with upper and lower case
10633 * anything to do with word or non-word characters ("spell_iswordp()")
10634 * banned words
10635 * word flags (rare, region, compounding)
10636 * word splitting for now
10637 * "similar_chars()"
10638 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
10639 */
10640 static void
10641suggest_trie_walk(su, lp, fword, soundfold)
10642 suginfo_T *su;
10643 langp_T *lp;
10644 char_u *fword;
10645 int soundfold;
10646{
10647 char_u tword[MAXWLEN]; /* good word collected so far */
10648 trystate_T stack[MAXWLEN];
10649 char_u preword[MAXWLEN * 3]; /* word found with proper case;
10650 * concatanation of prefix compound
10651 * words and split word. NUL terminated
10652 * when going deeper but not when coming
10653 * back. */
10654 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
10655 trystate_T *sp;
10656 int newscore;
10657 int score;
10658 char_u *byts, *fbyts, *pbyts;
10659 idx_T *idxs, *fidxs, *pidxs;
10660 int depth;
10661 int c, c2, c3;
10662 int n = 0;
10663 int flags;
10664 garray_T *gap;
10665 idx_T arridx;
10666 int len;
10667 char_u *p;
10668 fromto_T *ftp;
10669 int fl = 0, tl;
10670 int repextra = 0; /* extra bytes in fword[] from REP item */
10671 slang_T *slang = lp->lp_slang;
10672 int fword_ends;
10673 int goodword_ends;
10674#ifdef DEBUG_TRIEWALK
10675 /* Stores the name of the change made at each level. */
10676 char_u changename[MAXWLEN][80];
10677#endif
10678 int breakcheckcount = 1000;
10679 int compound_ok;
10680
10681 /*
10682 * Go through the whole case-fold tree, try changes at each node.
10683 * "tword[]" contains the word collected from nodes in the tree.
10684 * "fword[]" the word we are trying to match with (initially the bad
10685 * word).
10686 */
10687 depth = 0;
10688 sp = &stack[0];
10689 vim_memset(sp, 0, sizeof(trystate_T));
10690 sp->ts_curi = 1;
10691
10692 if (soundfold)
10693 {
10694 /* Going through the soundfold tree. */
10695 byts = fbyts = slang->sl_sbyts;
10696 idxs = fidxs = slang->sl_sidxs;
10697 pbyts = NULL;
10698 pidxs = NULL;
10699 sp->ts_prefixdepth = PFD_NOPREFIX;
10700 sp->ts_state = STATE_START;
10701 }
10702 else
10703 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010704 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010705 * When there are postponed prefixes we need to use these first. At
10706 * the end of the prefix we continue in the case-fold tree.
10707 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010708 fbyts = slang->sl_fbyts;
10709 fidxs = slang->sl_fidxs;
10710 pbyts = slang->sl_pbyts;
10711 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010712 if (pbyts != NULL)
10713 {
10714 byts = pbyts;
10715 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010716 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010717 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
10718 }
10719 else
10720 {
10721 byts = fbyts;
10722 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010723 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010724 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010725 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000010726 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010727
Bram Moolenaar4770d092006-01-12 23:22:24 +000010728 /*
10729 * Loop to find all suggestions. At each round we either:
10730 * - For the current state try one operation, advance "ts_curi",
10731 * increase "depth".
10732 * - When a state is done go to the next, set "ts_state".
10733 * - When all states are tried decrease "depth".
10734 */
10735 while (depth >= 0 && !got_int)
10736 {
10737 sp = &stack[depth];
10738 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010739 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010740 case STATE_START:
10741 case STATE_NOPREFIX:
10742 /*
10743 * Start of node: Deal with NUL bytes, which means
10744 * tword[] may end here.
10745 */
10746 arridx = sp->ts_arridx; /* current node in the tree */
10747 len = byts[arridx]; /* bytes in this node */
10748 arridx += sp->ts_curi; /* index of current byte */
10749
10750 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010751 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010752 /* Skip over the NUL bytes, we use them later. */
10753 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
10754 ;
10755 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010756
Bram Moolenaar4770d092006-01-12 23:22:24 +000010757 /* Always past NUL bytes now. */
10758 n = (int)sp->ts_state;
10759 sp->ts_state = STATE_ENDNUL;
10760 sp->ts_save_badflags = su->su_badflags;
10761
10762 /* At end of a prefix or at start of prefixtree: check for
10763 * following word. */
10764 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010765 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010766 /* Set su->su_badflags to the caps type at this position.
10767 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010768#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000010769 if (has_mbyte)
10770 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
10771 else
Bram Moolenaar53805d12005-08-01 07:08:33 +000010772#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000010773 n = sp->ts_fidx;
10774 flags = badword_captype(su->su_badptr, su->su_badptr + n);
10775 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000010776 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010777#ifdef DEBUG_TRIEWALK
10778 sprintf(changename[depth], "prefix");
10779#endif
10780 go_deeper(stack, depth, 0);
10781 ++depth;
10782 sp = &stack[depth];
10783 sp->ts_prefixdepth = depth - 1;
10784 byts = fbyts;
10785 idxs = fidxs;
10786 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010787
Bram Moolenaar4770d092006-01-12 23:22:24 +000010788 /* Move the prefix to preword[] with the right case
10789 * and make find_keepcap_word() works. */
10790 tword[sp->ts_twordlen] = NUL;
10791 make_case_word(tword + sp->ts_splitoff,
10792 preword + sp->ts_prewordlen, flags);
10793 sp->ts_prewordlen = STRLEN(preword);
10794 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010795 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000010796 break;
10797 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010798
Bram Moolenaar4770d092006-01-12 23:22:24 +000010799 if (sp->ts_curi > len || byts[arridx] != 0)
10800 {
10801 /* Past bytes in node and/or past NUL bytes. */
10802 sp->ts_state = STATE_ENDNUL;
10803 sp->ts_save_badflags = su->su_badflags;
10804 break;
10805 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010806
Bram Moolenaar4770d092006-01-12 23:22:24 +000010807 /*
10808 * End of word in tree.
10809 */
10810 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010811
Bram Moolenaar4770d092006-01-12 23:22:24 +000010812 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010813
10814 /* Skip words with the NOSUGGEST flag. */
10815 if (flags & WF_NOSUGGEST)
10816 break;
10817
Bram Moolenaar4770d092006-01-12 23:22:24 +000010818 fword_ends = (fword[sp->ts_fidx] == NUL
10819 || (soundfold
10820 ? vim_iswhite(fword[sp->ts_fidx])
10821 : !spell_iswordp(fword + sp->ts_fidx, curbuf)));
10822 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010823
Bram Moolenaar4770d092006-01-12 23:22:24 +000010824 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +000010825 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010826 {
10827 /* There was a prefix before the word. Check that the prefix
10828 * can be used with this word. */
10829 /* Count the length of the NULs in the prefix. If there are
10830 * none this must be the first try without a prefix. */
10831 n = stack[sp->ts_prefixdepth].ts_arridx;
10832 len = pbyts[n++];
10833 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
10834 ;
10835 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010836 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010837 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000010838 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010839 if (c == 0)
10840 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010841
Bram Moolenaar4770d092006-01-12 23:22:24 +000010842 /* Use the WF_RARE flag for a rare prefix. */
10843 if (c & WF_RAREPFX)
10844 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010845
Bram Moolenaar4770d092006-01-12 23:22:24 +000010846 /* Tricky: when checking for both prefix and compounding
10847 * we run into the prefix flag first.
10848 * Remember that it's OK, so that we accept the prefix
10849 * when arriving at a compound flag. */
10850 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010851 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000010852 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010853
Bram Moolenaar4770d092006-01-12 23:22:24 +000010854 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
10855 * appending another compound word below. */
10856 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010857 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +000010858 goodword_ends = FALSE;
10859 else
10860 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010861
Bram Moolenaar4770d092006-01-12 23:22:24 +000010862 p = NULL;
10863 compound_ok = TRUE;
10864 if (sp->ts_complen > sp->ts_compsplit)
10865 {
10866 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +000010867 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010868 /* There was a word before this word. When there was no
10869 * change in this word (it was correct) add the first word
10870 * as a suggestion. If this word was corrected too, we
10871 * need to check if a correct word follows. */
10872 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +000010873 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +000010874 && STRNCMP(fword + sp->ts_splitfidx,
10875 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +000010876 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010877 {
10878 preword[sp->ts_prewordlen] = NUL;
10879 newscore = score_wordcount_adj(slang, sp->ts_score,
10880 preword + sp->ts_prewordlen,
10881 sp->ts_prewordlen > 0);
10882 /* Add the suggestion if the score isn't too bad. */
10883 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +000010884 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010885 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010886 newscore, 0, FALSE,
10887 lp->lp_sallang, FALSE);
10888 break;
Bram Moolenaar78622822005-08-23 21:00:13 +000010889 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000010890 }
Bram Moolenaare52325c2005-08-22 22:54:29 +000010891 else
Bram Moolenaar0c405862005-06-22 22:26:26 +000010892 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010893 /* There was a compound word before this word. If this
10894 * word does not support compounding then give up
10895 * (splitting is tried for the word without compound
10896 * flag). */
10897 if (((unsigned)flags >> 24) == 0
10898 || sp->ts_twordlen - sp->ts_splitoff
10899 < slang->sl_compminlen)
10900 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010901#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000010902 /* For multi-byte chars check character length against
10903 * COMPOUNDMIN. */
10904 if (has_mbyte
10905 && slang->sl_compminlen > 0
10906 && mb_charlen(tword + sp->ts_splitoff)
10907 < slang->sl_compminlen)
10908 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010909#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +000010910
Bram Moolenaar4770d092006-01-12 23:22:24 +000010911 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
10912 compflags[sp->ts_complen + 1] = NUL;
10913 vim_strncpy(preword + sp->ts_prewordlen,
10914 tword + sp->ts_splitoff,
10915 sp->ts_twordlen - sp->ts_splitoff);
10916 p = preword;
10917 while (*skiptowhite(p) != NUL)
10918 p = skipwhite(skiptowhite(p));
10919 if (fword_ends && !can_compound(slang, p,
10920 compflags + sp->ts_compsplit))
10921 /* Compound is not allowed. But it may still be
10922 * possible if we add another (short) word. */
10923 compound_ok = FALSE;
10924
10925 /* Get pointer to last char of previous word. */
10926 p = preword + sp->ts_prewordlen;
10927 mb_ptr_back(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010928 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000010929 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010930
Bram Moolenaar4770d092006-01-12 23:22:24 +000010931 /*
10932 * Form the word with proper case in preword.
10933 * If there is a word from a previous split, append.
10934 * For the soundfold tree don't change the case, simply append.
10935 */
10936 if (soundfold)
10937 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
10938 else if (flags & WF_KEEPCAP)
10939 /* Must find the word in the keep-case tree. */
10940 find_keepcap_word(slang, tword + sp->ts_splitoff,
10941 preword + sp->ts_prewordlen);
10942 else
10943 {
10944 /* Include badflags: If the badword is onecap or allcap
10945 * use that for the goodword too. But if the badword is
10946 * allcap and it's only one char long use onecap. */
10947 c = su->su_badflags;
10948 if ((c & WF_ALLCAP)
10949#ifdef FEAT_MBYTE
10950 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
10951#else
10952 && su->su_badlen == 1
10953#endif
10954 )
10955 c = WF_ONECAP;
10956 c |= flags;
10957
10958 /* When appending a compound word after a word character don't
10959 * use Onecap. */
10960 if (p != NULL && spell_iswordp_nmw(p))
10961 c &= ~WF_ONECAP;
10962 make_case_word(tword + sp->ts_splitoff,
10963 preword + sp->ts_prewordlen, c);
10964 }
10965
10966 if (!soundfold)
10967 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010968 /* Don't use a banned word. It may appear again as a good
10969 * word, thus remember it. */
10970 if (flags & WF_BANNED)
10971 {
Bram Moolenaar5195e452005-08-19 20:32:47 +000010972 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010973 break;
10974 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010975 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +000010976 && WAS_BANNED(su, preword + sp->ts_prewordlen))
10977 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010978 {
10979 if (slang->sl_compprog == NULL)
10980 break;
10981 /* the word so far was banned but we may try compounding */
10982 goodword_ends = FALSE;
10983 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000010984 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010985
Bram Moolenaar4770d092006-01-12 23:22:24 +000010986 newscore = 0;
10987 if (!soundfold) /* soundfold words don't have flags */
10988 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010989 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010990 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010991 newscore += SCORE_REGION;
10992 if (flags & WF_RARE)
10993 newscore += SCORE_RARE;
10994
Bram Moolenaar0c405862005-06-22 22:26:26 +000010995 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000010996 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010997 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010998 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010999
Bram Moolenaar4770d092006-01-12 23:22:24 +000011000 /* TODO: how about splitting in the soundfold tree? */
11001 if (fword_ends
11002 && goodword_ends
11003 && sp->ts_fidx >= sp->ts_fidxtry
11004 && compound_ok)
11005 {
11006 /* The badword also ends: add suggestions. */
11007#ifdef DEBUG_TRIEWALK
11008 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011009 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011010 int j;
11011
11012 /* print the stack of changes that brought us here */
11013 smsg("------ %s -------", fword);
11014 for (j = 0; j < depth; ++j)
11015 smsg("%s", changename[j]);
11016 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011017#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011018 if (soundfold)
11019 {
11020 /* For soundfolded words we need to find the original
11021 * words, the edit distrance and then add them. */
11022 add_sound_suggest(su, preword, sp->ts_score, lp);
11023 }
11024 else
11025 {
11026 /* Give a penalty when changing non-word char to word
11027 * char, e.g., "thes," -> "these". */
11028 p = fword + sp->ts_fidx;
11029 mb_ptr_back(fword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011030 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011031 {
11032 p = preword + STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011033 mb_ptr_back(preword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011034 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011035 newscore += SCORE_NONWORD;
11036 }
11037
Bram Moolenaar4770d092006-01-12 23:22:24 +000011038 /* Give a bonus to words seen before. */
11039 score = score_wordcount_adj(slang,
11040 sp->ts_score + newscore,
11041 preword + sp->ts_prewordlen,
11042 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011043
Bram Moolenaar4770d092006-01-12 23:22:24 +000011044 /* Add the suggestion if the score isn't too bad. */
11045 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011046 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011047 add_suggestion(su, &su->su_ga, preword,
11048 sp->ts_fidx - repextra,
11049 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011050
11051 if (su->su_badflags & WF_MIXCAP)
11052 {
11053 /* We really don't know if the word should be
11054 * upper or lower case, add both. */
11055 c = captype(preword, NULL);
11056 if (c == 0 || c == WF_ALLCAP)
11057 {
11058 make_case_word(tword + sp->ts_splitoff,
11059 preword + sp->ts_prewordlen,
11060 c == 0 ? WF_ALLCAP : 0);
11061
11062 add_suggestion(su, &su->su_ga, preword,
11063 sp->ts_fidx - repextra,
11064 score + SCORE_ICASE, 0, FALSE,
11065 lp->lp_sallang, FALSE);
11066 }
11067 }
11068 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011069 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011070 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011071
Bram Moolenaar4770d092006-01-12 23:22:24 +000011072 /*
11073 * Try word split and/or compounding.
11074 */
11075 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +000011076#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011077 /* Don't split halfway a character. */
11078 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011079#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011080 )
11081 {
11082 int try_compound;
11083 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011084
Bram Moolenaar4770d092006-01-12 23:22:24 +000011085 /* If past the end of the bad word don't try a split.
11086 * Otherwise try changing the next word. E.g., find
11087 * suggestions for "the the" where the second "the" is
11088 * different. It's done like a split.
11089 * TODO: word split for soundfold words */
11090 try_split = (sp->ts_fidx - repextra < su->su_badlen)
11091 && !soundfold;
11092
11093 /* Get here in several situations:
11094 * 1. The word in the tree ends:
11095 * If the word allows compounding try that. Otherwise try
11096 * a split by inserting a space. For both check that a
11097 * valid words starts at fword[sp->ts_fidx].
11098 * For NOBREAK do like compounding to be able to check if
11099 * the next word is valid.
11100 * 2. The badword does end, but it was due to a change (e.g.,
11101 * a swap). No need to split, but do check that the
11102 * following word is valid.
11103 * 3. The badword and the word in the tree end. It may still
11104 * be possible to compound another (short) word.
11105 */
11106 try_compound = FALSE;
11107 if (!soundfold
11108 && slang->sl_compprog != NULL
11109 && ((unsigned)flags >> 24) != 0
11110 && sp->ts_twordlen - sp->ts_splitoff
11111 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011112#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011113 && (!has_mbyte
11114 || slang->sl_compminlen == 0
11115 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011116 >= slang->sl_compminlen)
11117#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011118 && (slang->sl_compsylmax < MAXWLEN
11119 || sp->ts_complen + 1 - sp->ts_compsplit
11120 < slang->sl_compmax)
11121 && (byte_in_str(sp->ts_complen == sp->ts_compsplit
11122 ? slang->sl_compstartflags
11123 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +000011124 ((unsigned)flags >> 24))))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011125 {
11126 try_compound = TRUE;
11127 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11128 compflags[sp->ts_complen + 1] = NUL;
11129 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011130
Bram Moolenaar4770d092006-01-12 23:22:24 +000011131 /* For NOBREAK we never try splitting, it won't make any word
11132 * valid. */
11133 if (slang->sl_nobreak)
11134 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +000011135
Bram Moolenaar4770d092006-01-12 23:22:24 +000011136 /* If we could add a compound word, and it's also possible to
11137 * split at this point, do the split first and set
11138 * TSF_DIDSPLIT to avoid doing it again. */
11139 else if (!fword_ends
11140 && try_compound
11141 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11142 {
11143 try_compound = FALSE;
11144 sp->ts_flags |= TSF_DIDSPLIT;
11145 --sp->ts_curi; /* do the same NUL again */
11146 compflags[sp->ts_complen] = NUL;
11147 }
11148 else
11149 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011150
Bram Moolenaar4770d092006-01-12 23:22:24 +000011151 if (try_split || try_compound)
11152 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011153 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +000011154 {
11155 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011156 * words so far are valid for compounding. If there
11157 * is only one word it must not have the NEEDCOMPOUND
11158 * flag. */
11159 if (sp->ts_complen == sp->ts_compsplit
11160 && (flags & WF_NEEDCOMP))
11161 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +000011162 p = preword;
11163 while (*skiptowhite(p) != NUL)
11164 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +000011165 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +000011166 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +000011167 compflags + sp->ts_compsplit))
11168 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011169
11170 if (slang->sl_nosplitsugs)
11171 newscore += SCORE_SPLIT_NO;
11172 else
11173 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011174
11175 /* Give a bonus to words seen before. */
11176 newscore = score_wordcount_adj(slang, newscore,
11177 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011178 }
11179
Bram Moolenaar4770d092006-01-12 23:22:24 +000011180 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011181 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011182 go_deeper(stack, depth, newscore);
11183#ifdef DEBUG_TRIEWALK
11184 if (!try_compound && !fword_ends)
11185 sprintf(changename[depth], "%.*s-%s: split",
11186 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11187 else
11188 sprintf(changename[depth], "%.*s-%s: compound",
11189 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11190#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011191 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000011192 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011193 sp->ts_state = STATE_SPLITUNDO;
11194
11195 ++depth;
11196 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011197
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011198 /* Append a space to preword when splitting. */
11199 if (!try_compound && !fword_ends)
11200 STRCAT(preword, " ");
Bram Moolenaar5195e452005-08-19 20:32:47 +000011201 sp->ts_prewordlen = STRLEN(preword);
11202 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +000011203 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011204
11205 /* If the badword has a non-word character at this
11206 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011207 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011208 * character when the word ends. But only when the
11209 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011210 if (((!try_compound && !spell_iswordp_nmw(fword
11211 + sp->ts_fidx))
11212 || fword_ends)
11213 && fword[sp->ts_fidx] != NUL
11214 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011215 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011216 int l;
11217
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011218#ifdef FEAT_MBYTE
11219 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011220 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011221 else
11222#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011223 l = 1;
11224 if (fword_ends)
11225 {
11226 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +000011227 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011228 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011229 sp->ts_prewordlen += l;
11230 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011231 }
11232 else
11233 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
11234 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011235 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011236
Bram Moolenaard12a1322005-08-21 22:08:24 +000011237 /* When compounding include compound flag in
11238 * compflags[] (already set above). When splitting we
11239 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011240 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +000011241 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011242 else
Bram Moolenaard12a1322005-08-21 22:08:24 +000011243 sp->ts_compsplit = sp->ts_complen;
11244 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011245
Bram Moolenaar53805d12005-08-01 07:08:33 +000011246 /* set su->su_badflags to the caps type at this
11247 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011248#ifdef FEAT_MBYTE
11249 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +000011250 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011251 else
11252#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +000011253 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011254 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011255 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011256
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011257 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011258 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011259
11260 /* If there are postponed prefixes, try these too. */
11261 if (pbyts != NULL)
11262 {
11263 byts = pbyts;
11264 idxs = pidxs;
11265 sp->ts_prefixdepth = PFD_PREFIXTREE;
11266 sp->ts_state = STATE_NOPREFIX;
11267 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011268 }
11269 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011270 }
11271 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011272
Bram Moolenaar4770d092006-01-12 23:22:24 +000011273 case STATE_SPLITUNDO:
11274 /* Undo the changes done for word split or compound word. */
11275 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011276
Bram Moolenaar4770d092006-01-12 23:22:24 +000011277 /* Continue looking for NUL bytes. */
11278 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011279
Bram Moolenaar4770d092006-01-12 23:22:24 +000011280 /* In case we went into the prefix tree. */
11281 byts = fbyts;
11282 idxs = fidxs;
11283 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011284
Bram Moolenaar4770d092006-01-12 23:22:24 +000011285 case STATE_ENDNUL:
11286 /* Past the NUL bytes in the node. */
11287 su->su_badflags = sp->ts_save_badflags;
11288 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011289#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011290 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011291#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011292 )
11293 {
11294 /* The badword ends, can't use STATE_PLAIN. */
11295 sp->ts_state = STATE_DEL;
11296 break;
11297 }
11298 sp->ts_state = STATE_PLAIN;
11299 /*FALLTHROUGH*/
11300
11301 case STATE_PLAIN:
11302 /*
11303 * Go over all possible bytes at this node, add each to tword[]
11304 * and use child node. "ts_curi" is the index.
11305 */
11306 arridx = sp->ts_arridx;
11307 if (sp->ts_curi > byts[arridx])
11308 {
11309 /* Done all bytes at this node, do next state. When still at
11310 * already changed bytes skip the other tricks. */
11311 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011312 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011313 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011314 sp->ts_state = STATE_FINAL;
11315 }
11316 else
11317 {
11318 arridx += sp->ts_curi++;
11319 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011320
Bram Moolenaar4770d092006-01-12 23:22:24 +000011321 /* Normal byte, go one level deeper. If it's not equal to the
11322 * byte in the bad word adjust the score. But don't even try
11323 * when the byte was already changed. And don't try when we
11324 * just deleted this byte, accepting it is always cheaper then
11325 * delete + substitute. */
11326 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +000011327#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011328 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011329#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011330 )
11331 newscore = 0;
11332 else
11333 newscore = SCORE_SUBST;
11334 if ((newscore == 0
11335 || (sp->ts_fidx >= sp->ts_fidxtry
11336 && ((sp->ts_flags & TSF_DIDDEL) == 0
11337 || c != fword[sp->ts_delidx])))
11338 && TRY_DEEPER(su, stack, depth, newscore))
11339 {
11340 go_deeper(stack, depth, newscore);
11341#ifdef DEBUG_TRIEWALK
11342 if (newscore > 0)
11343 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
11344 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11345 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011346 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011347 sprintf(changename[depth], "%.*s-%s: accept %c",
11348 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11349 fword[sp->ts_fidx]);
11350#endif
11351 ++depth;
11352 sp = &stack[depth];
11353 ++sp->ts_fidx;
11354 tword[sp->ts_twordlen++] = c;
11355 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +000011356#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011357 if (newscore == SCORE_SUBST)
11358 sp->ts_isdiff = DIFF_YES;
11359 if (has_mbyte)
11360 {
11361 /* Multi-byte characters are a bit complicated to
11362 * handle: They differ when any of the bytes differ
11363 * and then their length may also differ. */
11364 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011365 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011366 /* First byte. */
11367 sp->ts_tcharidx = 0;
11368 sp->ts_tcharlen = MB_BYTE2LEN(c);
11369 sp->ts_fcharstart = sp->ts_fidx - 1;
11370 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011371 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011372 }
11373 else if (sp->ts_isdiff == DIFF_INSERT)
11374 /* When inserting trail bytes don't advance in the
11375 * bad word. */
11376 --sp->ts_fidx;
11377 if (++sp->ts_tcharidx == sp->ts_tcharlen)
11378 {
11379 /* Last byte of character. */
11380 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +000011381 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011382 /* Correct ts_fidx for the byte length of the
11383 * character (we didn't check that before). */
11384 sp->ts_fidx = sp->ts_fcharstart
11385 + MB_BYTE2LEN(
Bram Moolenaarea424162005-06-16 21:51:00 +000011386 fword[sp->ts_fcharstart]);
11387
Bram Moolenaar4770d092006-01-12 23:22:24 +000011388 /* For changing a composing character adjust
11389 * the score from SCORE_SUBST to
11390 * SCORE_SUBCOMP. */
11391 if (enc_utf8
11392 && utf_iscomposing(
11393 mb_ptr2char(tword
11394 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011395 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011396 && utf_iscomposing(
11397 mb_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011398 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011399 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011400 SCORE_SUBST - SCORE_SUBCOMP;
11401
Bram Moolenaar4770d092006-01-12 23:22:24 +000011402 /* For a similar character adjust score from
11403 * SCORE_SUBST to SCORE_SIMILAR. */
11404 else if (!soundfold
11405 && slang->sl_has_map
11406 && similar_chars(slang,
11407 mb_ptr2char(tword
11408 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +000011409 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +000011410 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +000011411 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011412 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +000011413 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +000011414 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011415 else if (sp->ts_isdiff == DIFF_INSERT
11416 && sp->ts_twordlen > sp->ts_tcharlen)
11417 {
11418 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
11419 c = mb_ptr2char(p);
11420 if (enc_utf8 && utf_iscomposing(c))
11421 {
11422 /* Inserting a composing char doesn't
11423 * count that much. */
11424 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
11425 }
11426 else
11427 {
11428 /* If the previous character was the same,
11429 * thus doubling a character, give a bonus
11430 * to the score. Also for the soundfold
11431 * tree (might seem illogical but does
11432 * give better scores). */
11433 mb_ptr_back(tword, p);
11434 if (c == mb_ptr2char(p))
11435 sp->ts_score -= SCORE_INS
11436 - SCORE_INSDUP;
11437 }
11438 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011439
Bram Moolenaar4770d092006-01-12 23:22:24 +000011440 /* Starting a new char, reset the length. */
11441 sp->ts_tcharlen = 0;
11442 }
Bram Moolenaarea408852005-06-25 22:49:46 +000011443 }
Bram Moolenaarea424162005-06-16 21:51:00 +000011444 else
11445#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000011446 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011447 /* If we found a similar char adjust the score.
11448 * We do this after calling go_deeper() because
11449 * it's slow. */
11450 if (newscore != 0
11451 && !soundfold
11452 && slang->sl_has_map
11453 && similar_chars(slang,
11454 c, fword[sp->ts_fidx - 1]))
11455 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +000011456 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011457 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011458 }
11459 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011460
Bram Moolenaar4770d092006-01-12 23:22:24 +000011461 case STATE_DEL:
11462#ifdef FEAT_MBYTE
11463 /* When past the first byte of a multi-byte char don't try
11464 * delete/insert/swap a character. */
11465 if (has_mbyte && sp->ts_tcharlen > 0)
11466 {
11467 sp->ts_state = STATE_FINAL;
11468 break;
11469 }
11470#endif
11471 /*
11472 * Try skipping one character in the bad word (delete it).
11473 */
11474 sp->ts_state = STATE_INS_PREP;
11475 sp->ts_curi = 1;
11476 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
11477 /* Deleting a vowel at the start of a word counts less, see
11478 * soundalike_score(). */
11479 newscore = 2 * SCORE_DEL / 3;
11480 else
11481 newscore = SCORE_DEL;
11482 if (fword[sp->ts_fidx] != NUL
11483 && TRY_DEEPER(su, stack, depth, newscore))
11484 {
11485 go_deeper(stack, depth, newscore);
11486#ifdef DEBUG_TRIEWALK
11487 sprintf(changename[depth], "%.*s-%s: delete %c",
11488 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11489 fword[sp->ts_fidx]);
11490#endif
11491 ++depth;
11492
11493 /* Remember what character we deleted, so that we can avoid
11494 * inserting it again. */
11495 stack[depth].ts_flags |= TSF_DIDDEL;
11496 stack[depth].ts_delidx = sp->ts_fidx;
11497
11498 /* Advance over the character in fword[]. Give a bonus to the
11499 * score if the same character is following "nn" -> "n". It's
11500 * a bit illogical for soundfold tree but it does give better
11501 * results. */
11502#ifdef FEAT_MBYTE
11503 if (has_mbyte)
11504 {
11505 c = mb_ptr2char(fword + sp->ts_fidx);
11506 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
11507 if (enc_utf8 && utf_iscomposing(c))
11508 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
11509 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
11510 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
11511 }
11512 else
11513#endif
11514 {
11515 ++stack[depth].ts_fidx;
11516 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
11517 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
11518 }
11519 break;
11520 }
11521 /*FALLTHROUGH*/
11522
11523 case STATE_INS_PREP:
11524 if (sp->ts_flags & TSF_DIDDEL)
11525 {
11526 /* If we just deleted a byte then inserting won't make sense,
11527 * a substitute is always cheaper. */
11528 sp->ts_state = STATE_SWAP;
11529 break;
11530 }
11531
11532 /* skip over NUL bytes */
11533 n = sp->ts_arridx;
11534 for (;;)
11535 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011536 if (sp->ts_curi > byts[n])
11537 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011538 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011539 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011540 break;
11541 }
11542 if (byts[n + sp->ts_curi] != NUL)
11543 {
11544 /* Found a byte to insert. */
11545 sp->ts_state = STATE_INS;
11546 break;
11547 }
11548 ++sp->ts_curi;
11549 }
11550 break;
11551
11552 /*FALLTHROUGH*/
11553
11554 case STATE_INS:
11555 /* Insert one byte. Repeat this for each possible byte at this
11556 * node. */
11557 n = sp->ts_arridx;
11558 if (sp->ts_curi > byts[n])
11559 {
11560 /* Done all bytes at this node, go to next state. */
11561 sp->ts_state = STATE_SWAP;
11562 break;
11563 }
11564
11565 /* Do one more byte at this node, but:
11566 * - Skip NUL bytes.
11567 * - Skip the byte if it's equal to the byte in the word,
11568 * accepting that byte is always better.
11569 */
11570 n += sp->ts_curi++;
11571 c = byts[n];
11572 if (soundfold && sp->ts_twordlen == 0 && c == '*')
11573 /* Inserting a vowel at the start of a word counts less,
11574 * see soundalike_score(). */
11575 newscore = 2 * SCORE_INS / 3;
11576 else
11577 newscore = SCORE_INS;
11578 if (c != fword[sp->ts_fidx]
11579 && TRY_DEEPER(su, stack, depth, newscore))
11580 {
11581 go_deeper(stack, depth, newscore);
11582#ifdef DEBUG_TRIEWALK
11583 sprintf(changename[depth], "%.*s-%s: insert %c",
11584 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11585 c);
11586#endif
11587 ++depth;
11588 sp = &stack[depth];
11589 tword[sp->ts_twordlen++] = c;
11590 sp->ts_arridx = idxs[n];
11591#ifdef FEAT_MBYTE
11592 if (has_mbyte)
11593 {
11594 fl = MB_BYTE2LEN(c);
11595 if (fl > 1)
11596 {
11597 /* There are following bytes for the same character.
11598 * We must find all bytes before trying
11599 * delete/insert/swap/etc. */
11600 sp->ts_tcharlen = fl;
11601 sp->ts_tcharidx = 1;
11602 sp->ts_isdiff = DIFF_INSERT;
11603 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011604 }
11605 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011606 fl = 1;
11607 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000011608#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011609 {
11610 /* If the previous character was the same, thus doubling a
11611 * character, give a bonus to the score. Also for
11612 * soundfold words (illogical but does give a better
11613 * score). */
11614 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +000011615 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011616 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011617 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011618 }
11619 break;
11620
11621 case STATE_SWAP:
11622 /*
11623 * Swap two bytes in the bad word: "12" -> "21".
11624 * We change "fword" here, it's changed back afterwards at
11625 * STATE_UNSWAP.
11626 */
11627 p = fword + sp->ts_fidx;
11628 c = *p;
11629 if (c == NUL)
11630 {
11631 /* End of word, can't swap or replace. */
11632 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011633 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011634 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011635
Bram Moolenaar4770d092006-01-12 23:22:24 +000011636 /* Don't swap if the first character is not a word character.
11637 * SWAP3 etc. also don't make sense then. */
11638 if (!soundfold && !spell_iswordp(p, curbuf))
11639 {
11640 sp->ts_state = STATE_REP_INI;
11641 break;
11642 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000011643
Bram Moolenaar4770d092006-01-12 23:22:24 +000011644#ifdef FEAT_MBYTE
11645 if (has_mbyte)
11646 {
11647 n = mb_cptr2len(p);
11648 c = mb_ptr2char(p);
11649 if (!soundfold && !spell_iswordp(p + n, curbuf))
11650 c2 = c; /* don't swap non-word char */
11651 else
11652 c2 = mb_ptr2char(p + n);
11653 }
11654 else
11655#endif
11656 {
11657 if (!soundfold && !spell_iswordp(p + 1, curbuf))
11658 c2 = c; /* don't swap non-word char */
11659 else
11660 c2 = p[1];
11661 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000011662
Bram Moolenaar4770d092006-01-12 23:22:24 +000011663 /* When characters are identical, swap won't do anything.
11664 * Also get here if the second char is not a word character. */
11665 if (c == c2)
11666 {
11667 sp->ts_state = STATE_SWAP3;
11668 break;
11669 }
11670 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
11671 {
11672 go_deeper(stack, depth, SCORE_SWAP);
11673#ifdef DEBUG_TRIEWALK
11674 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
11675 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11676 c, c2);
11677#endif
11678 sp->ts_state = STATE_UNSWAP;
11679 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000011680#ifdef FEAT_MBYTE
11681 if (has_mbyte)
11682 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011683 fl = mb_char2len(c2);
11684 mch_memmove(p, p + n, fl);
11685 mb_char2bytes(c, p + fl);
11686 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000011687 }
11688 else
11689#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000011690 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011691 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +000011692 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011693 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +000011694 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011695 }
11696 else
11697 /* If this swap doesn't work then SWAP3 won't either. */
11698 sp->ts_state = STATE_REP_INI;
11699 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000011700
Bram Moolenaar4770d092006-01-12 23:22:24 +000011701 case STATE_UNSWAP:
11702 /* Undo the STATE_SWAP swap: "21" -> "12". */
11703 p = fword + sp->ts_fidx;
11704#ifdef FEAT_MBYTE
11705 if (has_mbyte)
11706 {
11707 n = MB_BYTE2LEN(*p);
11708 c = mb_ptr2char(p + n);
11709 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
11710 mb_char2bytes(c, p);
11711 }
11712 else
11713#endif
11714 {
11715 c = *p;
11716 *p = p[1];
11717 p[1] = c;
11718 }
11719 /*FALLTHROUGH*/
11720
11721 case STATE_SWAP3:
11722 /* Swap two bytes, skipping one: "123" -> "321". We change
11723 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
11724 p = fword + sp->ts_fidx;
11725#ifdef FEAT_MBYTE
11726 if (has_mbyte)
11727 {
11728 n = mb_cptr2len(p);
11729 c = mb_ptr2char(p);
11730 fl = mb_cptr2len(p + n);
11731 c2 = mb_ptr2char(p + n);
11732 if (!soundfold && !spell_iswordp(p + n + fl, curbuf))
11733 c3 = c; /* don't swap non-word char */
11734 else
11735 c3 = mb_ptr2char(p + n + fl);
11736 }
11737 else
11738#endif
11739 {
11740 c = *p;
11741 c2 = p[1];
11742 if (!soundfold && !spell_iswordp(p + 2, curbuf))
11743 c3 = c; /* don't swap non-word char */
11744 else
11745 c3 = p[2];
11746 }
11747
11748 /* When characters are identical: "121" then SWAP3 result is
11749 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
11750 * same as SWAP on next char: "112". Thus skip all swapping.
11751 * Also skip when c3 is NUL.
11752 * Also get here when the third character is not a word character.
11753 * Second character may any char: "a.b" -> "b.a" */
11754 if (c == c3 || c3 == NUL)
11755 {
11756 sp->ts_state = STATE_REP_INI;
11757 break;
11758 }
11759 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
11760 {
11761 go_deeper(stack, depth, SCORE_SWAP3);
11762#ifdef DEBUG_TRIEWALK
11763 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
11764 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11765 c, c3);
11766#endif
11767 sp->ts_state = STATE_UNSWAP3;
11768 ++depth;
11769#ifdef FEAT_MBYTE
11770 if (has_mbyte)
11771 {
11772 tl = mb_char2len(c3);
11773 mch_memmove(p, p + n + fl, tl);
11774 mb_char2bytes(c2, p + tl);
11775 mb_char2bytes(c, p + fl + tl);
11776 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
11777 }
11778 else
11779#endif
11780 {
11781 p[0] = p[2];
11782 p[2] = c;
11783 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
11784 }
11785 }
11786 else
11787 sp->ts_state = STATE_REP_INI;
11788 break;
11789
11790 case STATE_UNSWAP3:
11791 /* Undo STATE_SWAP3: "321" -> "123" */
11792 p = fword + sp->ts_fidx;
11793#ifdef FEAT_MBYTE
11794 if (has_mbyte)
11795 {
11796 n = MB_BYTE2LEN(*p);
11797 c2 = mb_ptr2char(p + n);
11798 fl = MB_BYTE2LEN(p[n]);
11799 c = mb_ptr2char(p + n + fl);
11800 tl = MB_BYTE2LEN(p[n + fl]);
11801 mch_memmove(p + fl + tl, p, n);
11802 mb_char2bytes(c, p);
11803 mb_char2bytes(c2, p + tl);
11804 p = p + tl;
11805 }
11806 else
11807#endif
11808 {
11809 c = *p;
11810 *p = p[2];
11811 p[2] = c;
11812 ++p;
11813 }
11814
11815 if (!soundfold && !spell_iswordp(p, curbuf))
11816 {
11817 /* Middle char is not a word char, skip the rotate. First and
11818 * third char were already checked at swap and swap3. */
11819 sp->ts_state = STATE_REP_INI;
11820 break;
11821 }
11822
11823 /* Rotate three characters left: "123" -> "231". We change
11824 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
11825 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
11826 {
11827 go_deeper(stack, depth, SCORE_SWAP3);
11828#ifdef DEBUG_TRIEWALK
11829 p = fword + sp->ts_fidx;
11830 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
11831 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11832 p[0], p[1], p[2]);
11833#endif
11834 sp->ts_state = STATE_UNROT3L;
11835 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000011836 p = fword + sp->ts_fidx;
11837#ifdef FEAT_MBYTE
11838 if (has_mbyte)
11839 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011840 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000011841 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011842 fl = mb_cptr2len(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011843 fl += mb_cptr2len(p + n + fl);
11844 mch_memmove(p, p + n, fl);
11845 mb_char2bytes(c, p + fl);
11846 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000011847 }
11848 else
11849#endif
11850 {
11851 c = *p;
11852 *p = p[1];
11853 p[1] = p[2];
11854 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011855 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +000011856 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011857 }
11858 else
11859 sp->ts_state = STATE_REP_INI;
11860 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011861
Bram Moolenaar4770d092006-01-12 23:22:24 +000011862 case STATE_UNROT3L:
11863 /* Undo ROT3L: "231" -> "123" */
11864 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +000011865#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011866 if (has_mbyte)
11867 {
11868 n = MB_BYTE2LEN(*p);
11869 n += MB_BYTE2LEN(p[n]);
11870 c = mb_ptr2char(p + n);
11871 tl = MB_BYTE2LEN(p[n]);
11872 mch_memmove(p + tl, p, n);
11873 mb_char2bytes(c, p);
11874 }
11875 else
Bram Moolenaarea424162005-06-16 21:51:00 +000011876#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011877 {
11878 c = p[2];
11879 p[2] = p[1];
11880 p[1] = *p;
11881 *p = c;
11882 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011883
Bram Moolenaar4770d092006-01-12 23:22:24 +000011884 /* Rotate three bytes right: "123" -> "312". We change "fword"
11885 * here, it's changed back afterwards at STATE_UNROT3R. */
11886 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
11887 {
11888 go_deeper(stack, depth, SCORE_SWAP3);
11889#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011890 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011891 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
11892 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11893 p[0], p[1], p[2]);
11894#endif
11895 sp->ts_state = STATE_UNROT3R;
11896 ++depth;
11897 p = fword + sp->ts_fidx;
11898#ifdef FEAT_MBYTE
11899 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000011900 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011901 n = mb_cptr2len(p);
11902 n += mb_cptr2len(p + n);
11903 c = mb_ptr2char(p + n);
11904 tl = mb_cptr2len(p + n);
11905 mch_memmove(p + tl, p, n);
11906 mb_char2bytes(c, p);
11907 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011908 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011909 else
11910#endif
11911 {
11912 c = p[2];
11913 p[2] = p[1];
11914 p[1] = *p;
11915 *p = c;
11916 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
11917 }
11918 }
11919 else
11920 sp->ts_state = STATE_REP_INI;
11921 break;
11922
11923 case STATE_UNROT3R:
11924 /* Undo ROT3R: "312" -> "123" */
11925 p = fword + sp->ts_fidx;
11926#ifdef FEAT_MBYTE
11927 if (has_mbyte)
11928 {
11929 c = mb_ptr2char(p);
11930 tl = MB_BYTE2LEN(*p);
11931 n = MB_BYTE2LEN(p[tl]);
11932 n += MB_BYTE2LEN(p[tl + n]);
11933 mch_memmove(p, p + tl, n);
11934 mb_char2bytes(c, p + n);
11935 }
11936 else
11937#endif
11938 {
11939 c = *p;
11940 *p = p[1];
11941 p[1] = p[2];
11942 p[2] = c;
11943 }
11944 /*FALLTHROUGH*/
11945
11946 case STATE_REP_INI:
11947 /* Check if matching with REP items from the .aff file would work.
11948 * Quickly skip if:
11949 * - there are no REP items and we are not in the soundfold trie
11950 * - the score is going to be too high anyway
11951 * - already applied a REP item or swapped here */
11952 if ((lp->lp_replang == NULL && !soundfold)
11953 || sp->ts_score + SCORE_REP >= su->su_maxscore
11954 || sp->ts_fidx < sp->ts_fidxtry)
11955 {
11956 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011957 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011958 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011959
Bram Moolenaar4770d092006-01-12 23:22:24 +000011960 /* Use the first byte to quickly find the first entry that may
11961 * match. If the index is -1 there is none. */
11962 if (soundfold)
11963 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
11964 else
11965 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011966
Bram Moolenaar4770d092006-01-12 23:22:24 +000011967 if (sp->ts_curi < 0)
11968 {
11969 sp->ts_state = STATE_FINAL;
11970 break;
11971 }
11972
11973 sp->ts_state = STATE_REP;
11974 /*FALLTHROUGH*/
11975
11976 case STATE_REP:
11977 /* Try matching with REP items from the .aff file. For each match
11978 * replace the characters and check if the resulting word is
11979 * valid. */
11980 p = fword + sp->ts_fidx;
11981
11982 if (soundfold)
11983 gap = &slang->sl_repsal;
11984 else
11985 gap = &lp->lp_replang->sl_rep;
11986 while (sp->ts_curi < gap->ga_len)
11987 {
11988 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
11989 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011990 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011991 /* past possible matching entries */
11992 sp->ts_curi = gap->ga_len;
11993 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011994 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011995 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
11996 && TRY_DEEPER(su, stack, depth, SCORE_REP))
11997 {
11998 go_deeper(stack, depth, SCORE_REP);
11999#ifdef DEBUG_TRIEWALK
12000 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
12001 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12002 ftp->ft_from, ftp->ft_to);
12003#endif
12004 /* Need to undo this afterwards. */
12005 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012006
Bram Moolenaar4770d092006-01-12 23:22:24 +000012007 /* Change the "from" to the "to" string. */
12008 ++depth;
12009 fl = STRLEN(ftp->ft_from);
12010 tl = STRLEN(ftp->ft_to);
12011 if (fl != tl)
12012 {
12013 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
12014 repextra += tl - fl;
12015 }
12016 mch_memmove(p, ftp->ft_to, tl);
12017 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
12018#ifdef FEAT_MBYTE
12019 stack[depth].ts_tcharlen = 0;
12020#endif
12021 break;
12022 }
12023 }
12024
12025 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
12026 /* No (more) matches. */
12027 sp->ts_state = STATE_FINAL;
12028
12029 break;
12030
12031 case STATE_REP_UNDO:
12032 /* Undo a REP replacement and continue with the next one. */
12033 if (soundfold)
12034 gap = &slang->sl_repsal;
12035 else
12036 gap = &lp->lp_replang->sl_rep;
12037 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
12038 fl = STRLEN(ftp->ft_from);
12039 tl = STRLEN(ftp->ft_to);
12040 p = fword + sp->ts_fidx;
12041 if (fl != tl)
12042 {
12043 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
12044 repextra -= tl - fl;
12045 }
12046 mch_memmove(p, ftp->ft_from, fl);
12047 sp->ts_state = STATE_REP;
12048 break;
12049
12050 default:
12051 /* Did all possible states at this level, go up one level. */
12052 --depth;
12053
12054 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
12055 {
12056 /* Continue in or go back to the prefix tree. */
12057 byts = pbyts;
12058 idxs = pidxs;
12059 }
12060
12061 /* Don't check for CTRL-C too often, it takes time. */
12062 if (--breakcheckcount == 0)
12063 {
12064 ui_breakcheck();
12065 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012066 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012067 }
12068 }
12069}
12070
Bram Moolenaar4770d092006-01-12 23:22:24 +000012071
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012072/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012073 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012074 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012075 static void
12076go_deeper(stack, depth, score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012077 trystate_T *stack;
12078 int depth;
12079 int score_add;
12080{
Bram Moolenaarea424162005-06-16 21:51:00 +000012081 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012082 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012083 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012084 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000012085 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012086}
12087
Bram Moolenaar53805d12005-08-01 07:08:33 +000012088#ifdef FEAT_MBYTE
12089/*
12090 * Case-folding may change the number of bytes: Count nr of chars in
12091 * fword[flen] and return the byte length of that many chars in "word".
12092 */
12093 static int
12094nofold_len(fword, flen, word)
12095 char_u *fword;
12096 int flen;
12097 char_u *word;
12098{
12099 char_u *p;
12100 int i = 0;
12101
12102 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12103 ++i;
12104 for (p = word; i > 0; mb_ptr_adv(p))
12105 --i;
12106 return (int)(p - word);
12107}
12108#endif
12109
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012110/*
12111 * "fword" is a good word with case folded. Find the matching keep-case
12112 * words and put it in "kword".
12113 * Theoretically there could be several keep-case words that result in the
12114 * same case-folded word, but we only find one...
12115 */
12116 static void
12117find_keepcap_word(slang, fword, kword)
12118 slang_T *slang;
12119 char_u *fword;
12120 char_u *kword;
12121{
12122 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12123 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012124 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012125
12126 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012127 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012128 int round[MAXWLEN];
12129 int fwordidx[MAXWLEN];
12130 int uwordidx[MAXWLEN];
12131 int kwordlen[MAXWLEN];
12132
12133 int flen, ulen;
12134 int l;
12135 int len;
12136 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012137 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012138 char_u *p;
12139 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012140 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012141
12142 if (byts == NULL)
12143 {
12144 /* array is empty: "cannot happen" */
12145 *kword = NUL;
12146 return;
12147 }
12148
12149 /* Make an all-cap version of "fword". */
12150 allcap_copy(fword, uword);
12151
12152 /*
12153 * Each character needs to be tried both case-folded and upper-case.
12154 * All this gets very complicated if we keep in mind that changing case
12155 * may change the byte length of a multi-byte character...
12156 */
12157 depth = 0;
12158 arridx[0] = 0;
12159 round[0] = 0;
12160 fwordidx[0] = 0;
12161 uwordidx[0] = 0;
12162 kwordlen[0] = 0;
12163 while (depth >= 0)
12164 {
12165 if (fword[fwordidx[depth]] == NUL)
12166 {
12167 /* We are at the end of "fword". If the tree allows a word to end
12168 * here we have found a match. */
12169 if (byts[arridx[depth] + 1] == 0)
12170 {
12171 kword[kwordlen[depth]] = NUL;
12172 return;
12173 }
12174
12175 /* kword is getting too long, continue one level up */
12176 --depth;
12177 }
12178 else if (++round[depth] > 2)
12179 {
12180 /* tried both fold-case and upper-case character, continue one
12181 * level up */
12182 --depth;
12183 }
12184 else
12185 {
12186 /*
12187 * round[depth] == 1: Try using the folded-case character.
12188 * round[depth] == 2: Try using the upper-case character.
12189 */
12190#ifdef FEAT_MBYTE
12191 if (has_mbyte)
12192 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012193 flen = mb_cptr2len(fword + fwordidx[depth]);
12194 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012195 }
12196 else
12197#endif
12198 ulen = flen = 1;
12199 if (round[depth] == 1)
12200 {
12201 p = fword + fwordidx[depth];
12202 l = flen;
12203 }
12204 else
12205 {
12206 p = uword + uwordidx[depth];
12207 l = ulen;
12208 }
12209
12210 for (tryidx = arridx[depth]; l > 0; --l)
12211 {
12212 /* Perform a binary search in the list of accepted bytes. */
12213 len = byts[tryidx++];
12214 c = *p++;
12215 lo = tryidx;
12216 hi = tryidx + len - 1;
12217 while (lo < hi)
12218 {
12219 m = (lo + hi) / 2;
12220 if (byts[m] > c)
12221 hi = m - 1;
12222 else if (byts[m] < c)
12223 lo = m + 1;
12224 else
12225 {
12226 lo = hi = m;
12227 break;
12228 }
12229 }
12230
12231 /* Stop if there is no matching byte. */
12232 if (hi < lo || byts[lo] != c)
12233 break;
12234
12235 /* Continue at the child (if there is one). */
12236 tryidx = idxs[lo];
12237 }
12238
12239 if (l == 0)
12240 {
12241 /*
12242 * Found the matching char. Copy it to "kword" and go a
12243 * level deeper.
12244 */
12245 if (round[depth] == 1)
12246 {
12247 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
12248 flen);
12249 kwordlen[depth + 1] = kwordlen[depth] + flen;
12250 }
12251 else
12252 {
12253 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
12254 ulen);
12255 kwordlen[depth + 1] = kwordlen[depth] + ulen;
12256 }
12257 fwordidx[depth + 1] = fwordidx[depth] + flen;
12258 uwordidx[depth + 1] = uwordidx[depth] + ulen;
12259
12260 ++depth;
12261 arridx[depth] = tryidx;
12262 round[depth] = 0;
12263 }
12264 }
12265 }
12266
12267 /* Didn't find it: "cannot happen". */
12268 *kword = NUL;
12269}
12270
12271/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012272 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
12273 * su->su_sga.
12274 */
12275 static void
12276score_comp_sal(su)
12277 suginfo_T *su;
12278{
12279 langp_T *lp;
12280 char_u badsound[MAXWLEN];
12281 int i;
12282 suggest_T *stp;
12283 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012284 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012285 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012286
12287 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
12288 return;
12289
12290 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012291 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012292 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012293 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012294 if (lp->lp_slang->sl_sal.ga_len > 0)
12295 {
12296 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012297 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012298
12299 for (i = 0; i < su->su_ga.ga_len; ++i)
12300 {
12301 stp = &SUG(su->su_ga, i);
12302
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012303 /* Case-fold the suggested word, sound-fold it and compute the
12304 * sound-a-like score. */
12305 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012306 if (score < SCORE_MAXMAX)
12307 {
12308 /* Add the suggestion. */
12309 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
12310 sstp->st_word = vim_strsave(stp->st_word);
12311 if (sstp->st_word != NULL)
12312 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012313 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012314 sstp->st_score = score;
12315 sstp->st_altscore = 0;
12316 sstp->st_orglen = stp->st_orglen;
12317 ++su->su_sga.ga_len;
12318 }
12319 }
12320 }
12321 break;
12322 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012323 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012324}
12325
12326/*
12327 * Combine the list of suggestions in su->su_ga and su->su_sga.
12328 * They are intwined.
12329 */
12330 static void
12331score_combine(su)
12332 suginfo_T *su;
12333{
12334 int i;
12335 int j;
12336 garray_T ga;
12337 garray_T *gap;
12338 langp_T *lp;
12339 suggest_T *stp;
12340 char_u *p;
12341 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012342 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012343 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012344 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012345
12346 /* Add the alternate score to su_ga. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012347 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012348 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012349 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012350 if (lp->lp_slang->sl_sal.ga_len > 0)
12351 {
12352 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012353 slang = lp->lp_slang;
12354 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012355
12356 for (i = 0; i < su->su_ga.ga_len; ++i)
12357 {
12358 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012359 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012360 if (stp->st_altscore == SCORE_MAXMAX)
12361 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
12362 else
12363 stp->st_score = (stp->st_score * 3
12364 + stp->st_altscore) / 4;
12365 stp->st_salscore = FALSE;
12366 }
12367 break;
12368 }
12369 }
12370
Bram Moolenaar4770d092006-01-12 23:22:24 +000012371 if (slang == NULL) /* just in case */
12372 return;
12373
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012374 /* Add the alternate score to su_sga. */
12375 for (i = 0; i < su->su_sga.ga_len; ++i)
12376 {
12377 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012378 stp->st_altscore = spell_edit_score(slang,
12379 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012380 if (stp->st_score == SCORE_MAXMAX)
12381 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
12382 else
12383 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
12384 stp->st_salscore = TRUE;
12385 }
12386
Bram Moolenaar4770d092006-01-12 23:22:24 +000012387 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
12388 * for both lists. */
12389 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012390 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012391 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012392 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
12393
12394 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
12395 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
12396 return;
12397
12398 stp = &SUG(ga, 0);
12399 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
12400 {
12401 /* round 1: get a suggestion from su_ga
12402 * round 2: get a suggestion from su_sga */
12403 for (round = 1; round <= 2; ++round)
12404 {
12405 gap = round == 1 ? &su->su_ga : &su->su_sga;
12406 if (i < gap->ga_len)
12407 {
12408 /* Don't add a word if it's already there. */
12409 p = SUG(*gap, i).st_word;
12410 for (j = 0; j < ga.ga_len; ++j)
12411 if (STRCMP(stp[j].st_word, p) == 0)
12412 break;
12413 if (j == ga.ga_len)
12414 stp[ga.ga_len++] = SUG(*gap, i);
12415 else
12416 vim_free(p);
12417 }
12418 }
12419 }
12420
12421 ga_clear(&su->su_ga);
12422 ga_clear(&su->su_sga);
12423
12424 /* Truncate the list to the number of suggestions that will be displayed. */
12425 if (ga.ga_len > su->su_maxcount)
12426 {
12427 for (i = su->su_maxcount; i < ga.ga_len; ++i)
12428 vim_free(stp[i].st_word);
12429 ga.ga_len = su->su_maxcount;
12430 }
12431
12432 su->su_ga = ga;
12433}
12434
12435/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012436 * For the goodword in "stp" compute the soundalike score compared to the
12437 * badword.
12438 */
12439 static int
12440stp_sal_score(stp, su, slang, badsound)
12441 suggest_T *stp;
12442 suginfo_T *su;
12443 slang_T *slang;
12444 char_u *badsound; /* sound-folded badword */
12445{
12446 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012447 char_u *pbad;
12448 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012449 char_u badsound2[MAXWLEN];
12450 char_u fword[MAXWLEN];
12451 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012452 char_u goodword[MAXWLEN];
12453 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012454
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012455 lendiff = (int)(su->su_badlen - stp->st_orglen);
12456 if (lendiff >= 0)
12457 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012458 else
12459 {
12460 /* soundfold the bad word with more characters following */
12461 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
12462
12463 /* When joining two words the sound often changes a lot. E.g., "t he"
12464 * sounds like "t h" while "the" sounds like "@". Avoid that by
12465 * removing the space. Don't do it when the good word also contains a
12466 * space. */
12467 if (vim_iswhite(su->su_badptr[su->su_badlen])
12468 && *skiptowhite(stp->st_word) == NUL)
12469 for (p = fword; *(p = skiptowhite(p)) != NUL; )
12470 mch_memmove(p, p + 1, STRLEN(p));
12471
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012472 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012473 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012474 }
12475
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012476 if (lendiff > 0)
12477 {
12478 /* Add part of the bad word to the good word, so that we soundfold
12479 * what replaces the bad word. */
12480 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012481 vim_strncpy(goodword + stp->st_wordlen,
12482 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012483 pgood = goodword;
12484 }
12485 else
12486 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012487
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012488 /* Sound-fold the word and compute the score for the difference. */
12489 spell_soundfold(slang, pgood, FALSE, goodsound);
12490
12491 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012492}
12493
Bram Moolenaar4770d092006-01-12 23:22:24 +000012494/* structure used to store soundfolded words that add_sound_suggest() has
12495 * handled already. */
12496typedef struct
12497{
12498 short sft_score; /* lowest score used */
12499 char_u sft_word[1]; /* soundfolded word, actually longer */
12500} sftword_T;
12501
12502static sftword_T dumsft;
12503#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
12504#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
12505
12506/*
12507 * Prepare for calling suggest_try_soundalike().
12508 */
12509 static void
12510suggest_try_soundalike_prep()
12511{
12512 langp_T *lp;
12513 int lpi;
12514 slang_T *slang;
12515
12516 /* Do this for all languages that support sound folding and for which a
12517 * .sug file has been loaded. */
12518 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
12519 {
12520 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
12521 slang = lp->lp_slang;
12522 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
12523 /* prepare the hashtable used by add_sound_suggest() */
12524 hash_init(&slang->sl_sounddone);
12525 }
12526}
12527
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012528/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012529 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012530 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012531 */
12532 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000012533suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012534 suginfo_T *su;
12535{
12536 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012537 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012538 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012539 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012540
Bram Moolenaar4770d092006-01-12 23:22:24 +000012541 /* Do this for all languages that support sound folding and for which a
12542 * .sug file has been loaded. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012543 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012544 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012545 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
12546 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012547 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012548 {
12549 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012550 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012551
Bram Moolenaar4770d092006-01-12 23:22:24 +000012552 /* try all kinds of inserts/deletes/swaps/etc. */
12553 /* TODO: also soundfold the next words, so that we can try joining
12554 * and splitting */
12555 suggest_trie_walk(su, lp, salword, TRUE);
12556 }
12557 }
12558}
12559
12560/*
12561 * Finish up after calling suggest_try_soundalike().
12562 */
12563 static void
12564suggest_try_soundalike_finish()
12565{
12566 langp_T *lp;
12567 int lpi;
12568 slang_T *slang;
12569 int todo;
12570 hashitem_T *hi;
12571
12572 /* Do this for all languages that support sound folding and for which a
12573 * .sug file has been loaded. */
12574 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
12575 {
12576 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
12577 slang = lp->lp_slang;
12578 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
12579 {
12580 /* Free the info about handled words. */
12581 todo = slang->sl_sounddone.ht_used;
12582 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
12583 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012584 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012585 vim_free(HI2SFT(hi));
12586 --todo;
12587 }
12588 hash_clear(&slang->sl_sounddone);
12589 }
12590 }
12591}
12592
12593/*
12594 * A match with a soundfolded word is found. Add the good word(s) that
12595 * produce this soundfolded word.
12596 */
12597 static void
12598add_sound_suggest(su, goodword, score, lp)
12599 suginfo_T *su;
12600 char_u *goodword;
12601 int score; /* soundfold score */
12602 langp_T *lp;
12603{
12604 slang_T *slang = lp->lp_slang; /* language for sound folding */
12605 int sfwordnr;
12606 char_u *nrline;
12607 int orgnr;
12608 char_u theword[MAXWLEN];
12609 int i;
12610 int wlen;
12611 char_u *byts;
12612 idx_T *idxs;
12613 int n;
12614 int wordcount;
12615 int wc;
12616 int goodscore;
12617 hash_T hash;
12618 hashitem_T *hi;
12619 sftword_T *sft;
12620 int bc, gc;
12621 int limit;
12622
12623 /*
12624 * It's very well possible that the same soundfold word is found several
12625 * times with different scores. Since the following is quite slow only do
12626 * the words that have a better score than before. Use a hashtable to
12627 * remember the words that have been done.
12628 */
12629 hash = hash_hash(goodword);
12630 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
12631 if (HASHITEM_EMPTY(hi))
12632 {
12633 sft = (sftword_T *)alloc(sizeof(sftword_T) + STRLEN(goodword));
12634 if (sft != NULL)
12635 {
12636 sft->sft_score = score;
12637 STRCPY(sft->sft_word, goodword);
12638 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
12639 }
12640 }
12641 else
12642 {
12643 sft = HI2SFT(hi);
12644 if (score >= sft->sft_score)
12645 return;
12646 sft->sft_score = score;
12647 }
12648
12649 /*
12650 * Find the word nr in the soundfold tree.
12651 */
12652 sfwordnr = soundfold_find(slang, goodword);
12653 if (sfwordnr < 0)
12654 {
12655 EMSG2(_(e_intern2), "add_sound_suggest()");
12656 return;
12657 }
12658
12659 /*
12660 * go over the list of good words that produce this soundfold word
12661 */
12662 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
12663 orgnr = 0;
12664 while (*nrline != NUL)
12665 {
12666 /* The wordnr was stored in a minimal nr of bytes as an offset to the
12667 * previous wordnr. */
12668 orgnr += bytes2offset(&nrline);
12669
12670 byts = slang->sl_fbyts;
12671 idxs = slang->sl_fidxs;
12672
12673 /* Lookup the word "orgnr" one of the two tries. */
12674 n = 0;
12675 wlen = 0;
12676 wordcount = 0;
12677 for (;;)
12678 {
12679 i = 1;
12680 if (wordcount == orgnr && byts[n + 1] == NUL)
12681 break; /* found end of word */
12682
12683 if (byts[n + 1] == NUL)
12684 ++wordcount;
12685
12686 /* skip over the NUL bytes */
12687 for ( ; byts[n + i] == NUL; ++i)
12688 if (i > byts[n]) /* safety check */
12689 {
12690 STRCPY(theword + wlen, "BAD");
12691 goto badword;
12692 }
12693
12694 /* One of the siblings must have the word. */
12695 for ( ; i < byts[n]; ++i)
12696 {
12697 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
12698 if (wordcount + wc > orgnr)
12699 break;
12700 wordcount += wc;
12701 }
12702
12703 theword[wlen++] = byts[n + i];
12704 n = idxs[n + i];
12705 }
12706badword:
12707 theword[wlen] = NUL;
12708
12709 /* Go over the possible flags and regions. */
12710 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
12711 {
12712 char_u cword[MAXWLEN];
12713 char_u *p;
12714 int flags = (int)idxs[n + i];
12715
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012716 /* Skip words with the NOSUGGEST flag */
12717 if (flags & WF_NOSUGGEST)
12718 continue;
12719
Bram Moolenaar4770d092006-01-12 23:22:24 +000012720 if (flags & WF_KEEPCAP)
12721 {
12722 /* Must find the word in the keep-case tree. */
12723 find_keepcap_word(slang, theword, cword);
12724 p = cword;
12725 }
12726 else
12727 {
12728 flags |= su->su_badflags;
12729 if ((flags & WF_CAPMASK) != 0)
12730 {
12731 /* Need to fix case according to "flags". */
12732 make_case_word(theword, cword, flags);
12733 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012734 }
12735 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012736 p = theword;
12737 }
12738
12739 /* Add the suggestion. */
12740 if (sps_flags & SPS_DOUBLE)
12741 {
12742 /* Add the suggestion if the score isn't too bad. */
12743 if (score <= su->su_maxscore)
12744 add_suggestion(su, &su->su_sga, p, su->su_badlen,
12745 score, 0, FALSE, slang, FALSE);
12746 }
12747 else
12748 {
12749 /* Add a penalty for words in another region. */
12750 if ((flags & WF_REGION)
12751 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
12752 goodscore = SCORE_REGION;
12753 else
12754 goodscore = 0;
12755
12756 /* Add a small penalty for changing the first letter from
12757 * lower to upper case. Helps for "tath" -> "Kath", which is
12758 * less common thatn "tath" -> "path". Don't do it when the
12759 * letter is the same, that has already been counted. */
12760 gc = PTR2CHAR(p);
12761 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012762 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012763 bc = PTR2CHAR(su->su_badword);
12764 if (!SPELL_ISUPPER(bc)
12765 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
12766 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012767 }
12768
Bram Moolenaar4770d092006-01-12 23:22:24 +000012769 /* Compute the score for the good word. This only does letter
12770 * insert/delete/swap/replace. REP items are not considered,
12771 * which may make the score a bit higher.
12772 * Use a limit for the score to make it work faster. Use
12773 * MAXSCORE(), because RESCORE() will change the score.
12774 * If the limit is very high then the iterative method is
12775 * inefficient, using an array is quicker. */
12776 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
12777 if (limit > SCORE_LIMITMAX)
12778 goodscore += spell_edit_score(slang, su->su_badword, p);
12779 else
12780 goodscore += spell_edit_score_limit(slang, su->su_badword,
12781 p, limit);
12782
12783 /* When going over the limit don't bother to do the rest. */
12784 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012785 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012786 /* Give a bonus to words seen before. */
12787 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012788
Bram Moolenaar4770d092006-01-12 23:22:24 +000012789 /* Add the suggestion if the score isn't too bad. */
12790 goodscore = RESCORE(goodscore, score);
12791 if (goodscore <= su->su_sfmaxscore)
12792 add_suggestion(su, &su->su_ga, p, su->su_badlen,
12793 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012794 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012795 }
12796 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012797 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012798 }
12799}
12800
12801/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012802 * Find word "word" in fold-case tree for "slang" and return the word number.
12803 */
12804 static int
12805soundfold_find(slang, word)
12806 slang_T *slang;
12807 char_u *word;
12808{
12809 idx_T arridx = 0;
12810 int len;
12811 int wlen = 0;
12812 int c;
12813 char_u *ptr = word;
12814 char_u *byts;
12815 idx_T *idxs;
12816 int wordnr = 0;
12817
12818 byts = slang->sl_sbyts;
12819 idxs = slang->sl_sidxs;
12820
12821 for (;;)
12822 {
12823 /* First byte is the number of possible bytes. */
12824 len = byts[arridx++];
12825
12826 /* If the first possible byte is a zero the word could end here.
12827 * If the word ends we found the word. If not skip the NUL bytes. */
12828 c = ptr[wlen];
12829 if (byts[arridx] == NUL)
12830 {
12831 if (c == NUL)
12832 break;
12833
12834 /* Skip over the zeros, there can be several. */
12835 while (len > 0 && byts[arridx] == NUL)
12836 {
12837 ++arridx;
12838 --len;
12839 }
12840 if (len == 0)
12841 return -1; /* no children, word should have ended here */
12842 ++wordnr;
12843 }
12844
12845 /* If the word ends we didn't find it. */
12846 if (c == NUL)
12847 return -1;
12848
12849 /* Perform a binary search in the list of accepted bytes. */
12850 if (c == TAB) /* <Tab> is handled like <Space> */
12851 c = ' ';
12852 while (byts[arridx] < c)
12853 {
12854 /* The word count is in the first idxs[] entry of the child. */
12855 wordnr += idxs[idxs[arridx]];
12856 ++arridx;
12857 if (--len == 0) /* end of the bytes, didn't find it */
12858 return -1;
12859 }
12860 if (byts[arridx] != c) /* didn't find the byte */
12861 return -1;
12862
12863 /* Continue at the child (if there is one). */
12864 arridx = idxs[arridx];
12865 ++wlen;
12866
12867 /* One space in the good word may stand for several spaces in the
12868 * checked word. */
12869 if (c == ' ')
12870 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
12871 ++wlen;
12872 }
12873
12874 return wordnr;
12875}
12876
12877/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012878 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012879 */
12880 static void
12881make_case_word(fword, cword, flags)
12882 char_u *fword;
12883 char_u *cword;
12884 int flags;
12885{
12886 if (flags & WF_ALLCAP)
12887 /* Make it all upper-case */
12888 allcap_copy(fword, cword);
12889 else if (flags & WF_ONECAP)
12890 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012891 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012892 else
12893 /* Use goodword as-is. */
12894 STRCPY(cword, fword);
12895}
12896
Bram Moolenaarea424162005-06-16 21:51:00 +000012897/*
12898 * Use map string "map" for languages "lp".
12899 */
12900 static void
12901set_map_str(lp, map)
12902 slang_T *lp;
12903 char_u *map;
12904{
12905 char_u *p;
12906 int headc = 0;
12907 int c;
12908 int i;
12909
12910 if (*map == NUL)
12911 {
12912 lp->sl_has_map = FALSE;
12913 return;
12914 }
12915 lp->sl_has_map = TRUE;
12916
Bram Moolenaar4770d092006-01-12 23:22:24 +000012917 /* Init the array and hash tables empty. */
Bram Moolenaarea424162005-06-16 21:51:00 +000012918 for (i = 0; i < 256; ++i)
12919 lp->sl_map_array[i] = 0;
12920#ifdef FEAT_MBYTE
12921 hash_init(&lp->sl_map_hash);
12922#endif
12923
12924 /*
12925 * The similar characters are stored separated with slashes:
12926 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
12927 * before the same slash. For characters above 255 sl_map_hash is used.
12928 */
12929 for (p = map; *p != NUL; )
12930 {
12931#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012932 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000012933#else
12934 c = *p++;
12935#endif
12936 if (c == '/')
12937 headc = 0;
12938 else
12939 {
12940 if (headc == 0)
12941 headc = c;
12942
12943#ifdef FEAT_MBYTE
12944 /* Characters above 255 don't fit in sl_map_array[], put them in
12945 * the hash table. Each entry is the char, a NUL the headchar and
12946 * a NUL. */
12947 if (c >= 256)
12948 {
12949 int cl = mb_char2len(c);
12950 int headcl = mb_char2len(headc);
12951 char_u *b;
12952 hash_T hash;
12953 hashitem_T *hi;
12954
12955 b = alloc((unsigned)(cl + headcl + 2));
12956 if (b == NULL)
12957 return;
12958 mb_char2bytes(c, b);
12959 b[cl] = NUL;
12960 mb_char2bytes(headc, b + cl + 1);
12961 b[cl + 1 + headcl] = NUL;
12962 hash = hash_hash(b);
12963 hi = hash_lookup(&lp->sl_map_hash, b, hash);
12964 if (HASHITEM_EMPTY(hi))
12965 hash_add_item(&lp->sl_map_hash, hi, b, hash);
12966 else
12967 {
12968 /* This should have been checked when generating the .spl
12969 * file. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000012970 EMSG(_("E783: duplicate char in MAP entry"));
Bram Moolenaarea424162005-06-16 21:51:00 +000012971 vim_free(b);
12972 }
12973 }
12974 else
12975#endif
12976 lp->sl_map_array[c] = headc;
12977 }
12978 }
12979}
12980
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012981/*
12982 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
12983 * lines in the .aff file.
12984 */
12985 static int
12986similar_chars(slang, c1, c2)
12987 slang_T *slang;
12988 int c1;
12989 int c2;
12990{
Bram Moolenaarea424162005-06-16 21:51:00 +000012991 int m1, m2;
12992#ifdef FEAT_MBYTE
12993 char_u buf[MB_MAXBYTES];
12994 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012995
Bram Moolenaarea424162005-06-16 21:51:00 +000012996 if (c1 >= 256)
12997 {
12998 buf[mb_char2bytes(c1, buf)] = 0;
12999 hi = hash_find(&slang->sl_map_hash, buf);
13000 if (HASHITEM_EMPTY(hi))
13001 m1 = 0;
13002 else
13003 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13004 }
13005 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013006#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000013007 m1 = slang->sl_map_array[c1];
13008 if (m1 == 0)
13009 return FALSE;
13010
13011
13012#ifdef FEAT_MBYTE
13013 if (c2 >= 256)
13014 {
13015 buf[mb_char2bytes(c2, buf)] = 0;
13016 hi = hash_find(&slang->sl_map_hash, buf);
13017 if (HASHITEM_EMPTY(hi))
13018 m2 = 0;
13019 else
13020 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13021 }
13022 else
13023#endif
13024 m2 = slang->sl_map_array[c2];
13025
13026 return m1 == m2;
13027}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013028
13029/*
13030 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +000013031 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013032 */
13033 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000013034add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus,
13035 slang, maxsf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013036 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013037 garray_T *gap; /* either su_ga or su_sga */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013038 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013039 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013040 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013041 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013042 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013043 slang_T *slang; /* language for sound folding */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013044 int maxsf; /* su_maxscore applies to soundfold score,
13045 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013046{
Bram Moolenaar4770d092006-01-12 23:22:24 +000013047 int goodlen; /* len of goodword changed */
13048 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013049 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013050 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013051 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013052 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013053
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013054 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13055 * "thee the" is added next to changing the first "the" the "thee". */
13056 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013057 pbad = su->su_badptr + badlenarg;
13058 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013059 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013060 goodlen = pgood - goodword;
13061 badlen = pbad - su->su_badptr;
13062 if (goodlen <= 0 || badlen <= 0)
13063 break;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013064 mb_ptr_back(goodword, pgood);
13065 mb_ptr_back(su->su_badptr, pbad);
13066#ifdef FEAT_MBYTE
13067 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013068 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013069 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
13070 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013071 }
13072 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013073#endif
13074 if (*pgood != *pbad)
13075 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013076 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013077
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013078 if (badlen == 0 && goodlen == 0)
13079 /* goodword doesn't change anything; may happen for "the the" changing
13080 * the first "the" to itself. */
13081 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013082
Bram Moolenaar4770d092006-01-12 23:22:24 +000013083 /* Check if the word is already there. Also check the length that is
13084 * being replaced "thes," -> "these" is a different suggestion from
13085 * "thes" -> "these". */
13086 stp = &SUG(*gap, 0);
13087 for (i = gap->ga_len; --i >= 0; ++stp)
13088 if (stp->st_wordlen == goodlen
13089 && stp->st_orglen == badlen
13090 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
13091 {
13092 /*
13093 * Found it. Remember the word with the lowest score.
13094 */
13095 if (stp->st_slang == NULL)
13096 stp->st_slang = slang;
13097
13098 new_sug.st_score = score;
13099 new_sug.st_altscore = altscore;
13100 new_sug.st_had_bonus = had_bonus;
13101
13102 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013103 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013104 /* Only one of the two had the soundalike score computed.
13105 * Need to do that for the other one now, otherwise the
13106 * scores can't be compared. This happens because
13107 * suggest_try_change() doesn't compute the soundalike
13108 * word to keep it fast, while some special methods set
13109 * the soundalike score to zero. */
13110 if (had_bonus)
13111 rescore_one(su, stp);
13112 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013113 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013114 new_sug.st_word = stp->st_word;
13115 new_sug.st_wordlen = stp->st_wordlen;
13116 new_sug.st_slang = stp->st_slang;
13117 new_sug.st_orglen = badlen;
13118 rescore_one(su, &new_sug);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013119 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013120 }
13121
Bram Moolenaar4770d092006-01-12 23:22:24 +000013122 if (stp->st_score > new_sug.st_score)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013123 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013124 stp->st_score = new_sug.st_score;
13125 stp->st_altscore = new_sug.st_altscore;
13126 stp->st_had_bonus = new_sug.st_had_bonus;
13127 }
13128 break;
13129 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013130
Bram Moolenaar4770d092006-01-12 23:22:24 +000013131 if (i < 0 && ga_grow(gap, 1) == OK)
13132 {
13133 /* Add a suggestion. */
13134 stp = &SUG(*gap, gap->ga_len);
13135 stp->st_word = vim_strnsave(goodword, goodlen);
13136 if (stp->st_word != NULL)
13137 {
13138 stp->st_wordlen = goodlen;
13139 stp->st_score = score;
13140 stp->st_altscore = altscore;
13141 stp->st_had_bonus = had_bonus;
13142 stp->st_orglen = badlen;
13143 stp->st_slang = slang;
13144 ++gap->ga_len;
13145
13146 /* If we have too many suggestions now, sort the list and keep
13147 * the best suggestions. */
13148 if (gap->ga_len > SUG_MAX_COUNT(su))
13149 {
13150 if (maxsf)
13151 su->su_sfmaxscore = cleanup_suggestions(gap,
13152 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
13153 else
13154 {
13155 i = su->su_maxscore;
13156 su->su_maxscore = cleanup_suggestions(gap,
13157 su->su_maxscore, SUG_CLEAN_COUNT(su));
13158 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013159 }
13160 }
13161 }
13162}
13163
13164/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013165 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13166 * for split words, such as "the the". Remove these from the list here.
13167 */
13168 static void
13169check_suggestions(su, gap)
13170 suginfo_T *su;
13171 garray_T *gap; /* either su_ga or su_sga */
13172{
13173 suggest_T *stp;
13174 int i;
13175 char_u longword[MAXWLEN + 1];
13176 int len;
13177 hlf_T attr;
13178
13179 stp = &SUG(*gap, 0);
13180 for (i = gap->ga_len - 1; i >= 0; --i)
13181 {
13182 /* Need to append what follows to check for "the the". */
13183 STRCPY(longword, stp[i].st_word);
13184 len = stp[i].st_wordlen;
13185 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
13186 MAXWLEN - len);
13187 attr = HLF_COUNT;
13188 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
13189 if (attr != HLF_COUNT)
13190 {
13191 /* Remove this entry. */
13192 vim_free(stp[i].st_word);
13193 --gap->ga_len;
13194 if (i < gap->ga_len)
13195 mch_memmove(stp + i, stp + i + 1,
13196 sizeof(suggest_T) * (gap->ga_len - i));
13197 }
13198 }
13199}
13200
13201
13202/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013203 * Add a word to be banned.
13204 */
13205 static void
13206add_banned(su, word)
13207 suginfo_T *su;
13208 char_u *word;
13209{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013210 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013211 hash_T hash;
13212 hashitem_T *hi;
13213
Bram Moolenaar4770d092006-01-12 23:22:24 +000013214 hash = hash_hash(word);
13215 hi = hash_lookup(&su->su_banned, word, hash);
13216 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013217 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013218 s = vim_strsave(word);
13219 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013220 hash_add_item(&su->su_banned, hi, s, hash);
13221 }
13222}
13223
13224/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013225 * Recompute the score for all suggestions if sound-folding is possible. This
13226 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013227 */
13228 static void
13229rescore_suggestions(su)
13230 suginfo_T *su;
13231{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013232 int i;
13233
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013234 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013235 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013236 rescore_one(su, &SUG(su->su_ga, i));
13237}
13238
13239/*
13240 * Recompute the score for one suggestion if sound-folding is possible.
13241 */
13242 static void
13243rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013244 suginfo_T *su;
13245 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013246{
13247 slang_T *slang = stp->st_slang;
13248 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000013249 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013250
13251 /* Only rescore suggestions that have no sal score yet and do have a
13252 * language. */
13253 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
13254 {
13255 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013256 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013257 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013258 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013259 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000013260 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013261 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000013262
13263 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013264 if (stp->st_altscore == SCORE_MAXMAX)
13265 stp->st_altscore = SCORE_BIG;
13266 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
13267 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013268 }
13269}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013270
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013271static int
13272#ifdef __BORLANDC__
13273_RTLENTRYF
13274#endif
13275sug_compare __ARGS((const void *s1, const void *s2));
13276
13277/*
13278 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013279 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013280 */
13281 static int
13282#ifdef __BORLANDC__
13283_RTLENTRYF
13284#endif
13285sug_compare(s1, s2)
13286 const void *s1;
13287 const void *s2;
13288{
13289 suggest_T *p1 = (suggest_T *)s1;
13290 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013291 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013292
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013293 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013294 {
13295 n = p1->st_altscore - p2->st_altscore;
13296 if (n == 0)
13297 n = STRICMP(p1->st_word, p2->st_word);
13298 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013299 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013300}
13301
13302/*
13303 * Cleanup the suggestions:
13304 * - Sort on score.
13305 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013306 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013307 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013308 static int
13309cleanup_suggestions(gap, maxscore, keep)
13310 garray_T *gap;
13311 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013312 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013313{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013314 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013315 int i;
13316
13317 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013318 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013319
13320 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013321 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013322 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013323 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013324 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013325 gap->ga_len = keep;
13326 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013327 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013328 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013329}
13330
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013331#if defined(FEAT_EVAL) || defined(PROTO)
13332/*
13333 * Soundfold a string, for soundfold().
13334 * Result is in allocated memory, NULL for an error.
13335 */
13336 char_u *
13337eval_soundfold(word)
13338 char_u *word;
13339{
13340 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013341 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013342 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013343
13344 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13345 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013346 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013347 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013348 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013349 if (lp->lp_slang->sl_sal.ga_len > 0)
13350 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013351 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013352 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013353 return vim_strsave(sound);
13354 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013355 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013356
13357 /* No language with sound folding, return word as-is. */
13358 return vim_strsave(word);
13359}
13360#endif
13361
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013362/*
13363 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000013364 *
13365 * There are many ways to turn a word into a sound-a-like representation. The
13366 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
13367 * swedish name matching - survey and test of different algorithms" by Klas
13368 * Erikson.
13369 *
13370 * We support two methods:
13371 * 1. SOFOFROM/SOFOTO do a simple character mapping.
13372 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013373 */
13374 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013375spell_soundfold(slang, inword, folded, res)
13376 slang_T *slang;
13377 char_u *inword;
13378 int folded; /* "inword" is already case-folded */
13379 char_u *res;
13380{
13381 char_u fword[MAXWLEN];
13382 char_u *word;
13383
13384 if (slang->sl_sofo)
13385 /* SOFOFROM and SOFOTO used */
13386 spell_soundfold_sofo(slang, inword, res);
13387 else
13388 {
13389 /* SAL items used. Requires the word to be case-folded. */
13390 if (folded)
13391 word = inword;
13392 else
13393 {
13394 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
13395 word = fword;
13396 }
13397
13398#ifdef FEAT_MBYTE
13399 if (has_mbyte)
13400 spell_soundfold_wsal(slang, word, res);
13401 else
13402#endif
13403 spell_soundfold_sal(slang, word, res);
13404 }
13405}
13406
13407/*
13408 * Perform sound folding of "inword" into "res" according to SOFOFROM and
13409 * SOFOTO lines.
13410 */
13411 static void
13412spell_soundfold_sofo(slang, inword, res)
13413 slang_T *slang;
13414 char_u *inword;
13415 char_u *res;
13416{
13417 char_u *s;
13418 int ri = 0;
13419 int c;
13420
13421#ifdef FEAT_MBYTE
13422 if (has_mbyte)
13423 {
13424 int prevc = 0;
13425 int *ip;
13426
13427 /* The sl_sal_first[] table contains the translation for chars up to
13428 * 255, sl_sal the rest. */
13429 for (s = inword; *s != NUL; )
13430 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013431 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013432 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
13433 c = ' ';
13434 else if (c < 256)
13435 c = slang->sl_sal_first[c];
13436 else
13437 {
13438 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
13439 if (ip == NULL) /* empty list, can't match */
13440 c = NUL;
13441 else
13442 for (;;) /* find "c" in the list */
13443 {
13444 if (*ip == 0) /* not found */
13445 {
13446 c = NUL;
13447 break;
13448 }
13449 if (*ip == c) /* match! */
13450 {
13451 c = ip[1];
13452 break;
13453 }
13454 ip += 2;
13455 }
13456 }
13457
13458 if (c != NUL && c != prevc)
13459 {
13460 ri += mb_char2bytes(c, res + ri);
13461 if (ri + MB_MAXBYTES > MAXWLEN)
13462 break;
13463 prevc = c;
13464 }
13465 }
13466 }
13467 else
13468#endif
13469 {
13470 /* The sl_sal_first[] table contains the translation. */
13471 for (s = inword; (c = *s) != NUL; ++s)
13472 {
13473 if (vim_iswhite(c))
13474 c = ' ';
13475 else
13476 c = slang->sl_sal_first[c];
13477 if (c != NUL && (ri == 0 || res[ri - 1] != c))
13478 res[ri++] = c;
13479 }
13480 }
13481
13482 res[ri] = NUL;
13483}
13484
13485 static void
13486spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013487 slang_T *slang;
13488 char_u *inword;
13489 char_u *res;
13490{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013491 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013492 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013493 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013494 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013495 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013496 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013497 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013498 int n, k = 0;
13499 int z0;
13500 int k0;
13501 int n0;
13502 int c;
13503 int pri;
13504 int p0 = -333;
13505 int c0;
13506
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013507 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013508 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013509 if (slang->sl_rem_accents)
13510 {
13511 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013512 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013513 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013514 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013515 {
13516 *t++ = ' ';
13517 s = skipwhite(s);
13518 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013519 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013520 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013521 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013522 *t++ = *s;
13523 ++s;
13524 }
13525 }
13526 *t = NUL;
13527 }
13528 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013529 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013530
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013531 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013532
13533 /*
13534 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013535 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013536 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013537 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013538 while ((c = word[i]) != NUL)
13539 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013540 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013541 n = slang->sl_sal_first[c];
13542 z0 = 0;
13543
13544 if (n >= 0)
13545 {
13546 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013547 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013548 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013549 /* Quickly skip entries that don't match the word. Most
13550 * entries are less then three chars, optimize for that. */
13551 k = smp[n].sm_leadlen;
13552 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013553 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013554 if (word[i + 1] != s[1])
13555 continue;
13556 if (k > 2)
13557 {
13558 for (j = 2; j < k; ++j)
13559 if (word[i + j] != s[j])
13560 break;
13561 if (j < k)
13562 continue;
13563 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013564 }
13565
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013566 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013567 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013568 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013569 while (*pf != NUL && *pf != word[i + k])
13570 ++pf;
13571 if (*pf == NUL)
13572 continue;
13573 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013574 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013575 s = smp[n].sm_rules;
13576 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013577
13578 p0 = *s;
13579 k0 = k;
13580 while (*s == '-' && k > 1)
13581 {
13582 k--;
13583 s++;
13584 }
13585 if (*s == '<')
13586 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013587 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013588 {
13589 /* determine priority */
13590 pri = *s - '0';
13591 s++;
13592 }
13593 if (*s == '^' && *(s + 1) == '^')
13594 s++;
13595
13596 if (*s == NUL
13597 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013598 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013599 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013600 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013601 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013602 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013603 && spell_iswordp(word + i - 1, curbuf)
13604 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013605 {
13606 /* search for followup rules, if: */
13607 /* followup and k > 1 and NO '-' in searchstring */
13608 c0 = word[i + k - 1];
13609 n0 = slang->sl_sal_first[c0];
13610
13611 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013612 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013613 {
13614 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013615 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013616 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013617 /* Quickly skip entries that don't match the word.
13618 * */
13619 k0 = smp[n0].sm_leadlen;
13620 if (k0 > 1)
13621 {
13622 if (word[i + k] != s[1])
13623 continue;
13624 if (k0 > 2)
13625 {
13626 pf = word + i + k + 1;
13627 for (j = 2; j < k0; ++j)
13628 if (*pf++ != s[j])
13629 break;
13630 if (j < k0)
13631 continue;
13632 }
13633 }
13634 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013635
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013636 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013637 {
13638 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013639 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013640 while (*pf != NUL && *pf != word[i + k0])
13641 ++pf;
13642 if (*pf == NUL)
13643 continue;
13644 ++k0;
13645 }
13646
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013647 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013648 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013649 while (*s == '-')
13650 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013651 /* "k0" gets NOT reduced because
13652 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013653 s++;
13654 }
13655 if (*s == '<')
13656 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013657 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013658 {
13659 p0 = *s - '0';
13660 s++;
13661 }
13662
13663 if (*s == NUL
13664 /* *s == '^' cuts */
13665 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013666 && !spell_iswordp(word + i + k0,
13667 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013668 {
13669 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013670 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013671 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013672
13673 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013674 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013675 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013676 /* rule fits; stop search */
13677 break;
13678 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013679 }
13680
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013681 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013682 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013683 }
13684
13685 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013686 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000013687 if (s == NULL)
13688 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013689 pf = smp[n].sm_rules;
13690 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013691 if (p0 == 1 && z == 0)
13692 {
13693 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013694 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
13695 || res[reslen - 1] == *s))
13696 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013697 z0 = 1;
13698 z = 1;
13699 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013700 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013701 {
13702 word[i + k0] = *s;
13703 k0++;
13704 s++;
13705 }
13706 if (k > k0)
13707 mch_memmove(word + i + k0, word + i + k,
13708 STRLEN(word + i + k) + 1);
13709
13710 /* new "actual letter" */
13711 c = word[i];
13712 }
13713 else
13714 {
13715 /* no '<' rule used */
13716 i += k - 1;
13717 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013718 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013719 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013720 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013721 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013722 s++;
13723 }
13724 /* new "actual letter" */
13725 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013726 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013727 {
13728 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013729 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013730 mch_memmove(word, word + i + 1,
13731 STRLEN(word + i + 1) + 1);
13732 i = 0;
13733 z0 = 1;
13734 }
13735 }
13736 break;
13737 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013738 }
13739 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013740 else if (vim_iswhite(c))
13741 {
13742 c = ' ';
13743 k = 1;
13744 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013745
13746 if (z0 == 0)
13747 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013748 if (k && !p0 && reslen < MAXWLEN && c != NUL
13749 && (!slang->sl_collapse || reslen == 0
13750 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013751 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013752 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013753
13754 i++;
13755 z = 0;
13756 k = 0;
13757 }
13758 }
13759
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013760 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013761}
13762
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013763#ifdef FEAT_MBYTE
13764/*
13765 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
13766 * Multi-byte version of spell_soundfold().
13767 */
13768 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013769spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013770 slang_T *slang;
13771 char_u *inword;
13772 char_u *res;
13773{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013774 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013775 int word[MAXWLEN];
13776 int wres[MAXWLEN];
13777 int l;
13778 char_u *s;
13779 int *ws;
13780 char_u *t;
13781 int *pf;
13782 int i, j, z;
13783 int reslen;
13784 int n, k = 0;
13785 int z0;
13786 int k0;
13787 int n0;
13788 int c;
13789 int pri;
13790 int p0 = -333;
13791 int c0;
13792 int did_white = FALSE;
13793
13794 /*
13795 * Convert the multi-byte string to a wide-character string.
13796 * Remove accents, if wanted. We actually remove all non-word characters.
13797 * But keep white space.
13798 */
13799 n = 0;
13800 for (s = inword; *s != NUL; )
13801 {
13802 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013803 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013804 if (slang->sl_rem_accents)
13805 {
13806 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
13807 {
13808 if (did_white)
13809 continue;
13810 c = ' ';
13811 did_white = TRUE;
13812 }
13813 else
13814 {
13815 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013816 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013817 continue;
13818 }
13819 }
13820 word[n++] = c;
13821 }
13822 word[n] = NUL;
13823
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013824 /*
13825 * This comes from Aspell phonet.cpp.
13826 * Converted from C++ to C. Added support for multi-byte chars.
13827 * Changed to keep spaces.
13828 */
13829 i = reslen = z = 0;
13830 while ((c = word[i]) != NUL)
13831 {
13832 /* Start with the first rule that has the character in the word. */
13833 n = slang->sl_sal_first[c & 0xff];
13834 z0 = 0;
13835
13836 if (n >= 0)
13837 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013838 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013839 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
13840 {
13841 /* Quickly skip entries that don't match the word. Most
13842 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013843 if (c != ws[0])
13844 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013845 k = smp[n].sm_leadlen;
13846 if (k > 1)
13847 {
13848 if (word[i + 1] != ws[1])
13849 continue;
13850 if (k > 2)
13851 {
13852 for (j = 2; j < k; ++j)
13853 if (word[i + j] != ws[j])
13854 break;
13855 if (j < k)
13856 continue;
13857 }
13858 }
13859
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013860 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013861 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013862 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013863 while (*pf != NUL && *pf != word[i + k])
13864 ++pf;
13865 if (*pf == NUL)
13866 continue;
13867 ++k;
13868 }
13869 s = smp[n].sm_rules;
13870 pri = 5; /* default priority */
13871
13872 p0 = *s;
13873 k0 = k;
13874 while (*s == '-' && k > 1)
13875 {
13876 k--;
13877 s++;
13878 }
13879 if (*s == '<')
13880 s++;
13881 if (VIM_ISDIGIT(*s))
13882 {
13883 /* determine priority */
13884 pri = *s - '0';
13885 s++;
13886 }
13887 if (*s == '^' && *(s + 1) == '^')
13888 s++;
13889
13890 if (*s == NUL
13891 || (*s == '^'
13892 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013893 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013894 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013895 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013896 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013897 && spell_iswordp_w(word + i - 1, curbuf)
13898 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013899 {
13900 /* search for followup rules, if: */
13901 /* followup and k > 1 and NO '-' in searchstring */
13902 c0 = word[i + k - 1];
13903 n0 = slang->sl_sal_first[c0 & 0xff];
13904
13905 if (slang->sl_followup && k > 1 && n0 >= 0
13906 && p0 != '-' && word[i + k] != NUL)
13907 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013908 /* Test follow-up rule for "word[i + k]"; loop over
13909 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013910 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
13911 == (c0 & 0xff); ++n0)
13912 {
13913 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013914 */
13915 if (c0 != ws[0])
13916 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013917 k0 = smp[n0].sm_leadlen;
13918 if (k0 > 1)
13919 {
13920 if (word[i + k] != ws[1])
13921 continue;
13922 if (k0 > 2)
13923 {
13924 pf = word + i + k + 1;
13925 for (j = 2; j < k0; ++j)
13926 if (*pf++ != ws[j])
13927 break;
13928 if (j < k0)
13929 continue;
13930 }
13931 }
13932 k0 += k - 1;
13933
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013934 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013935 {
13936 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013937 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013938 while (*pf != NUL && *pf != word[i + k0])
13939 ++pf;
13940 if (*pf == NUL)
13941 continue;
13942 ++k0;
13943 }
13944
13945 p0 = 5;
13946 s = smp[n0].sm_rules;
13947 while (*s == '-')
13948 {
13949 /* "k0" gets NOT reduced because
13950 * "if (k0 == k)" */
13951 s++;
13952 }
13953 if (*s == '<')
13954 s++;
13955 if (VIM_ISDIGIT(*s))
13956 {
13957 p0 = *s - '0';
13958 s++;
13959 }
13960
13961 if (*s == NUL
13962 /* *s == '^' cuts */
13963 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013964 && !spell_iswordp_w(word + i + k0,
13965 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013966 {
13967 if (k0 == k)
13968 /* this is just a piece of the string */
13969 continue;
13970
13971 if (p0 < pri)
13972 /* priority too low */
13973 continue;
13974 /* rule fits; stop search */
13975 break;
13976 }
13977 }
13978
13979 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
13980 == (c0 & 0xff))
13981 continue;
13982 }
13983
13984 /* replace string */
13985 ws = smp[n].sm_to_w;
13986 s = smp[n].sm_rules;
13987 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
13988 if (p0 == 1 && z == 0)
13989 {
13990 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000013991 if (reslen > 0 && ws != NULL && *ws != NUL
13992 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013993 || wres[reslen - 1] == *ws))
13994 reslen--;
13995 z0 = 1;
13996 z = 1;
13997 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000013998 if (ws != NULL)
13999 while (*ws != NUL && word[i + k0] != NUL)
14000 {
14001 word[i + k0] = *ws;
14002 k0++;
14003 ws++;
14004 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014005 if (k > k0)
14006 mch_memmove(word + i + k0, word + i + k,
14007 sizeof(int) * (STRLEN(word + i + k) + 1));
14008
14009 /* new "actual letter" */
14010 c = word[i];
14011 }
14012 else
14013 {
14014 /* no '<' rule used */
14015 i += k - 1;
14016 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014017 if (ws != NULL)
14018 while (*ws != NUL && ws[1] != NUL
14019 && reslen < MAXWLEN)
14020 {
14021 if (reslen == 0 || wres[reslen - 1] != *ws)
14022 wres[reslen++] = *ws;
14023 ws++;
14024 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014025 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014026 if (ws == NULL)
14027 c = NUL;
14028 else
14029 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014030 if (strstr((char *)s, "^^") != NULL)
14031 {
14032 if (c != NUL)
14033 wres[reslen++] = c;
14034 mch_memmove(word, word + i + 1,
14035 sizeof(int) * (STRLEN(word + i + 1) + 1));
14036 i = 0;
14037 z0 = 1;
14038 }
14039 }
14040 break;
14041 }
14042 }
14043 }
14044 else if (vim_iswhite(c))
14045 {
14046 c = ' ';
14047 k = 1;
14048 }
14049
14050 if (z0 == 0)
14051 {
14052 if (k && !p0 && reslen < MAXWLEN && c != NUL
14053 && (!slang->sl_collapse || reslen == 0
14054 || wres[reslen - 1] != c))
14055 /* condense only double letters */
14056 wres[reslen++] = c;
14057
14058 i++;
14059 z = 0;
14060 k = 0;
14061 }
14062 }
14063
14064 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14065 l = 0;
14066 for (n = 0; n < reslen; ++n)
14067 {
14068 l += mb_char2bytes(wres[n], res + l);
14069 if (l + MB_MAXBYTES > MAXWLEN)
14070 break;
14071 }
14072 res[l] = NUL;
14073}
14074#endif
14075
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014076/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014077 * Compute a score for two sound-a-like words.
14078 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14079 * Instead of a generic loop we write out the code. That keeps it fast by
14080 * avoiding checks that will not be possible.
14081 */
14082 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014083soundalike_score(goodstart, badstart)
14084 char_u *goodstart; /* sound-folded good word */
14085 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014086{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014087 char_u *goodsound = goodstart;
14088 char_u *badsound = badstart;
14089 int goodlen;
14090 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014091 int n;
14092 char_u *pl, *ps;
14093 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014094 int score = 0;
14095
14096 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
14097 * counted so much, vowels halfway the word aren't counted at all. */
14098 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
14099 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014100 if (badsound[1] == goodsound[1]
14101 || (badsound[1] != NUL
14102 && goodsound[1] != NUL
14103 && badsound[2] == goodsound[2]))
14104 {
14105 /* handle like a substitute */
14106 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014107 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014108 {
14109 score = 2 * SCORE_DEL / 3;
14110 if (*badsound == '*')
14111 ++badsound;
14112 else
14113 ++goodsound;
14114 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014115 }
14116
14117 goodlen = STRLEN(goodsound);
14118 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014119
14120 /* Return quickly if the lenghts are too different to be fixed by two
14121 * changes. */
14122 n = goodlen - badlen;
14123 if (n < -2 || n > 2)
14124 return SCORE_MAXMAX;
14125
14126 if (n > 0)
14127 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014128 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014129 ps = badsound;
14130 }
14131 else
14132 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014133 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014134 ps = goodsound;
14135 }
14136
14137 /* Skip over the identical part. */
14138 while (*pl == *ps && *pl != NUL)
14139 {
14140 ++pl;
14141 ++ps;
14142 }
14143
14144 switch (n)
14145 {
14146 case -2:
14147 case 2:
14148 /*
14149 * Must delete two characters from "pl".
14150 */
14151 ++pl; /* first delete */
14152 while (*pl == *ps)
14153 {
14154 ++pl;
14155 ++ps;
14156 }
14157 /* strings must be equal after second delete */
14158 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014159 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014160
14161 /* Failed to compare. */
14162 break;
14163
14164 case -1:
14165 case 1:
14166 /*
14167 * Minimal one delete from "pl" required.
14168 */
14169
14170 /* 1: delete */
14171 pl2 = pl + 1;
14172 ps2 = ps;
14173 while (*pl2 == *ps2)
14174 {
14175 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014176 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014177 ++pl2;
14178 ++ps2;
14179 }
14180
14181 /* 2: delete then swap, then rest must be equal */
14182 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14183 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014184 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014185
14186 /* 3: delete then substitute, then the rest must be equal */
14187 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014188 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014189
14190 /* 4: first swap then delete */
14191 if (pl[0] == ps[1] && pl[1] == ps[0])
14192 {
14193 pl2 = pl + 2; /* swap, skip two chars */
14194 ps2 = ps + 2;
14195 while (*pl2 == *ps2)
14196 {
14197 ++pl2;
14198 ++ps2;
14199 }
14200 /* delete a char and then strings must be equal */
14201 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014202 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014203 }
14204
14205 /* 5: first substitute then delete */
14206 pl2 = pl + 1; /* substitute, skip one char */
14207 ps2 = ps + 1;
14208 while (*pl2 == *ps2)
14209 {
14210 ++pl2;
14211 ++ps2;
14212 }
14213 /* delete a char and then strings must be equal */
14214 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014215 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014216
14217 /* Failed to compare. */
14218 break;
14219
14220 case 0:
14221 /*
14222 * Lenghts are equal, thus changes must result in same length: An
14223 * insert is only possible in combination with a delete.
14224 * 1: check if for identical strings
14225 */
14226 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014227 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014228
14229 /* 2: swap */
14230 if (pl[0] == ps[1] && pl[1] == ps[0])
14231 {
14232 pl2 = pl + 2; /* swap, skip two chars */
14233 ps2 = ps + 2;
14234 while (*pl2 == *ps2)
14235 {
14236 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014237 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014238 ++pl2;
14239 ++ps2;
14240 }
14241 /* 3: swap and swap again */
14242 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14243 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014244 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014245
14246 /* 4: swap and substitute */
14247 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014248 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014249 }
14250
14251 /* 5: substitute */
14252 pl2 = pl + 1;
14253 ps2 = ps + 1;
14254 while (*pl2 == *ps2)
14255 {
14256 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014257 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014258 ++pl2;
14259 ++ps2;
14260 }
14261
14262 /* 6: substitute and swap */
14263 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14264 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014265 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014266
14267 /* 7: substitute and substitute */
14268 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014269 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014270
14271 /* 8: insert then delete */
14272 pl2 = pl;
14273 ps2 = ps + 1;
14274 while (*pl2 == *ps2)
14275 {
14276 ++pl2;
14277 ++ps2;
14278 }
14279 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014280 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014281
14282 /* 9: delete then insert */
14283 pl2 = pl + 1;
14284 ps2 = ps;
14285 while (*pl2 == *ps2)
14286 {
14287 ++pl2;
14288 ++ps2;
14289 }
14290 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014291 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014292
14293 /* Failed to compare. */
14294 break;
14295 }
14296
14297 return SCORE_MAXMAX;
14298}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014299
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014300/*
14301 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014302 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014303 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000014304 * The algorithm is described by Du and Chang, 1992.
14305 * The implementation of the algorithm comes from Aspell editdist.cpp,
14306 * edit_distance(). It has been converted from C++ to C and modified to
14307 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014308 */
14309 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +000014310spell_edit_score(slang, badword, goodword)
14311 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014312 char_u *badword;
14313 char_u *goodword;
14314{
14315 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014316 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014317 int j, i;
14318 int t;
14319 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014320 int pbc, pgc;
14321#ifdef FEAT_MBYTE
14322 char_u *p;
14323 int wbadword[MAXWLEN];
14324 int wgoodword[MAXWLEN];
14325
14326 if (has_mbyte)
14327 {
14328 /* Get the characters from the multi-byte strings and put them in an
14329 * int array for easy access. */
14330 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014331 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014332 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014333 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014334 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014335 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014336 }
14337 else
14338#endif
14339 {
14340 badlen = STRLEN(badword) + 1;
14341 goodlen = STRLEN(goodword) + 1;
14342 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014343
14344 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
14345#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014346 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
14347 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014348 if (cnt == NULL)
14349 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014350
14351 CNT(0, 0) = 0;
14352 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +000014353 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014354
14355 for (i = 1; i <= badlen; ++i)
14356 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014357 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014358 for (j = 1; j <= goodlen; ++j)
14359 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014360#ifdef FEAT_MBYTE
14361 if (has_mbyte)
14362 {
14363 bc = wbadword[i - 1];
14364 gc = wgoodword[j - 1];
14365 }
14366 else
14367#endif
14368 {
14369 bc = badword[i - 1];
14370 gc = goodword[j - 1];
14371 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014372 if (bc == gc)
14373 CNT(i, j) = CNT(i - 1, j - 1);
14374 else
14375 {
14376 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014377 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014378 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
14379 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014380 {
14381 /* For a similar character use SCORE_SIMILAR. */
14382 if (slang != NULL
14383 && slang->sl_has_map
14384 && similar_chars(slang, gc, bc))
14385 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
14386 else
14387 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
14388 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014389
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014390 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014391 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014392#ifdef FEAT_MBYTE
14393 if (has_mbyte)
14394 {
14395 pbc = wbadword[i - 2];
14396 pgc = wgoodword[j - 2];
14397 }
14398 else
14399#endif
14400 {
14401 pbc = badword[i - 2];
14402 pgc = goodword[j - 2];
14403 }
14404 if (bc == pgc && pbc == gc)
14405 {
14406 t = SCORE_SWAP + CNT(i - 2, j - 2);
14407 if (t < CNT(i, j))
14408 CNT(i, j) = t;
14409 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014410 }
14411 t = SCORE_DEL + CNT(i - 1, j);
14412 if (t < CNT(i, j))
14413 CNT(i, j) = t;
14414 t = SCORE_INS + CNT(i, j - 1);
14415 if (t < CNT(i, j))
14416 CNT(i, j) = t;
14417 }
14418 }
14419 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014420
14421 i = CNT(badlen - 1, goodlen - 1);
14422 vim_free(cnt);
14423 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014424}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000014425
Bram Moolenaar4770d092006-01-12 23:22:24 +000014426typedef struct
14427{
14428 int badi;
14429 int goodi;
14430 int score;
14431} limitscore_T;
14432
14433/*
14434 * Like spell_edit_score(), but with a limit on the score to make it faster.
14435 * May return SCORE_MAXMAX when the score is higher than "limit".
14436 *
14437 * This uses a stack for the edits still to be tried.
14438 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
14439 * for multi-byte characters.
14440 */
14441 static int
14442spell_edit_score_limit(slang, badword, goodword, limit)
14443 slang_T *slang;
14444 char_u *badword;
14445 char_u *goodword;
14446 int limit;
14447{
14448 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
14449 int stackidx;
14450 int bi, gi;
14451 int bi2, gi2;
14452 int bc, gc;
14453 int score;
14454 int score_off;
14455 int minscore;
14456 int round;
14457
14458#ifdef FEAT_MBYTE
14459 /* Multi-byte characters require a bit more work, use a different function
14460 * to avoid testing "has_mbyte" quite often. */
14461 if (has_mbyte)
14462 return spell_edit_score_limit_w(slang, badword, goodword, limit);
14463#endif
14464
14465 /*
14466 * The idea is to go from start to end over the words. So long as
14467 * characters are equal just continue, this always gives the lowest score.
14468 * When there is a difference try several alternatives. Each alternative
14469 * increases "score" for the edit distance. Some of the alternatives are
14470 * pushed unto a stack and tried later, some are tried right away. At the
14471 * end of the word the score for one alternative is known. The lowest
14472 * possible score is stored in "minscore".
14473 */
14474 stackidx = 0;
14475 bi = 0;
14476 gi = 0;
14477 score = 0;
14478 minscore = limit + 1;
14479
14480 for (;;)
14481 {
14482 /* Skip over an equal part, score remains the same. */
14483 for (;;)
14484 {
14485 bc = badword[bi];
14486 gc = goodword[gi];
14487 if (bc != gc) /* stop at a char that's different */
14488 break;
14489 if (bc == NUL) /* both words end */
14490 {
14491 if (score < minscore)
14492 minscore = score;
14493 goto pop; /* do next alternative */
14494 }
14495 ++bi;
14496 ++gi;
14497 }
14498
14499 if (gc == NUL) /* goodword ends, delete badword chars */
14500 {
14501 do
14502 {
14503 if ((score += SCORE_DEL) >= minscore)
14504 goto pop; /* do next alternative */
14505 } while (badword[++bi] != NUL);
14506 minscore = score;
14507 }
14508 else if (bc == NUL) /* badword ends, insert badword chars */
14509 {
14510 do
14511 {
14512 if ((score += SCORE_INS) >= minscore)
14513 goto pop; /* do next alternative */
14514 } while (goodword[++gi] != NUL);
14515 minscore = score;
14516 }
14517 else /* both words continue */
14518 {
14519 /* If not close to the limit, perform a change. Only try changes
14520 * that may lead to a lower score than "minscore".
14521 * round 0: try deleting a char from badword
14522 * round 1: try inserting a char in badword */
14523 for (round = 0; round <= 1; ++round)
14524 {
14525 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
14526 if (score_off < minscore)
14527 {
14528 if (score_off + SCORE_EDIT_MIN >= minscore)
14529 {
14530 /* Near the limit, rest of the words must match. We
14531 * can check that right now, no need to push an item
14532 * onto the stack. */
14533 bi2 = bi + 1 - round;
14534 gi2 = gi + round;
14535 while (goodword[gi2] == badword[bi2])
14536 {
14537 if (goodword[gi2] == NUL)
14538 {
14539 minscore = score_off;
14540 break;
14541 }
14542 ++bi2;
14543 ++gi2;
14544 }
14545 }
14546 else
14547 {
14548 /* try deleting/inserting a character later */
14549 stack[stackidx].badi = bi + 1 - round;
14550 stack[stackidx].goodi = gi + round;
14551 stack[stackidx].score = score_off;
14552 ++stackidx;
14553 }
14554 }
14555 }
14556
14557 if (score + SCORE_SWAP < minscore)
14558 {
14559 /* If swapping two characters makes a match then the
14560 * substitution is more expensive, thus there is no need to
14561 * try both. */
14562 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
14563 {
14564 /* Swap two characters, that is: skip them. */
14565 gi += 2;
14566 bi += 2;
14567 score += SCORE_SWAP;
14568 continue;
14569 }
14570 }
14571
14572 /* Substitute one character for another which is the same
14573 * thing as deleting a character from both goodword and badword.
14574 * Use a better score when there is only a case difference. */
14575 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
14576 score += SCORE_ICASE;
14577 else
14578 {
14579 /* For a similar character use SCORE_SIMILAR. */
14580 if (slang != NULL
14581 && slang->sl_has_map
14582 && similar_chars(slang, gc, bc))
14583 score += SCORE_SIMILAR;
14584 else
14585 score += SCORE_SUBST;
14586 }
14587
14588 if (score < minscore)
14589 {
14590 /* Do the substitution. */
14591 ++gi;
14592 ++bi;
14593 continue;
14594 }
14595 }
14596pop:
14597 /*
14598 * Get here to try the next alternative, pop it from the stack.
14599 */
14600 if (stackidx == 0) /* stack is empty, finished */
14601 break;
14602
14603 /* pop an item from the stack */
14604 --stackidx;
14605 gi = stack[stackidx].goodi;
14606 bi = stack[stackidx].badi;
14607 score = stack[stackidx].score;
14608 }
14609
14610 /* When the score goes over "limit" it may actually be much higher.
14611 * Return a very large number to avoid going below the limit when giving a
14612 * bonus. */
14613 if (minscore > limit)
14614 return SCORE_MAXMAX;
14615 return minscore;
14616}
14617
14618#ifdef FEAT_MBYTE
14619/*
14620 * Multi-byte version of spell_edit_score_limit().
14621 * Keep it in sync with the above!
14622 */
14623 static int
14624spell_edit_score_limit_w(slang, badword, goodword, limit)
14625 slang_T *slang;
14626 char_u *badword;
14627 char_u *goodword;
14628 int limit;
14629{
14630 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
14631 int stackidx;
14632 int bi, gi;
14633 int bi2, gi2;
14634 int bc, gc;
14635 int score;
14636 int score_off;
14637 int minscore;
14638 int round;
14639 char_u *p;
14640 int wbadword[MAXWLEN];
14641 int wgoodword[MAXWLEN];
14642
14643 /* Get the characters from the multi-byte strings and put them in an
14644 * int array for easy access. */
14645 bi = 0;
14646 for (p = badword; *p != NUL; )
14647 wbadword[bi++] = mb_cptr2char_adv(&p);
14648 wbadword[bi++] = 0;
14649 gi = 0;
14650 for (p = goodword; *p != NUL; )
14651 wgoodword[gi++] = mb_cptr2char_adv(&p);
14652 wgoodword[gi++] = 0;
14653
14654 /*
14655 * The idea is to go from start to end over the words. So long as
14656 * characters are equal just continue, this always gives the lowest score.
14657 * When there is a difference try several alternatives. Each alternative
14658 * increases "score" for the edit distance. Some of the alternatives are
14659 * pushed unto a stack and tried later, some are tried right away. At the
14660 * end of the word the score for one alternative is known. The lowest
14661 * possible score is stored in "minscore".
14662 */
14663 stackidx = 0;
14664 bi = 0;
14665 gi = 0;
14666 score = 0;
14667 minscore = limit + 1;
14668
14669 for (;;)
14670 {
14671 /* Skip over an equal part, score remains the same. */
14672 for (;;)
14673 {
14674 bc = wbadword[bi];
14675 gc = wgoodword[gi];
14676
14677 if (bc != gc) /* stop at a char that's different */
14678 break;
14679 if (bc == NUL) /* both words end */
14680 {
14681 if (score < minscore)
14682 minscore = score;
14683 goto pop; /* do next alternative */
14684 }
14685 ++bi;
14686 ++gi;
14687 }
14688
14689 if (gc == NUL) /* goodword ends, delete badword chars */
14690 {
14691 do
14692 {
14693 if ((score += SCORE_DEL) >= minscore)
14694 goto pop; /* do next alternative */
14695 } while (wbadword[++bi] != NUL);
14696 minscore = score;
14697 }
14698 else if (bc == NUL) /* badword ends, insert badword chars */
14699 {
14700 do
14701 {
14702 if ((score += SCORE_INS) >= minscore)
14703 goto pop; /* do next alternative */
14704 } while (wgoodword[++gi] != NUL);
14705 minscore = score;
14706 }
14707 else /* both words continue */
14708 {
14709 /* If not close to the limit, perform a change. Only try changes
14710 * that may lead to a lower score than "minscore".
14711 * round 0: try deleting a char from badword
14712 * round 1: try inserting a char in badword */
14713 for (round = 0; round <= 1; ++round)
14714 {
14715 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
14716 if (score_off < minscore)
14717 {
14718 if (score_off + SCORE_EDIT_MIN >= minscore)
14719 {
14720 /* Near the limit, rest of the words must match. We
14721 * can check that right now, no need to push an item
14722 * onto the stack. */
14723 bi2 = bi + 1 - round;
14724 gi2 = gi + round;
14725 while (wgoodword[gi2] == wbadword[bi2])
14726 {
14727 if (wgoodword[gi2] == NUL)
14728 {
14729 minscore = score_off;
14730 break;
14731 }
14732 ++bi2;
14733 ++gi2;
14734 }
14735 }
14736 else
14737 {
14738 /* try deleting a character from badword later */
14739 stack[stackidx].badi = bi + 1 - round;
14740 stack[stackidx].goodi = gi + round;
14741 stack[stackidx].score = score_off;
14742 ++stackidx;
14743 }
14744 }
14745 }
14746
14747 if (score + SCORE_SWAP < minscore)
14748 {
14749 /* If swapping two characters makes a match then the
14750 * substitution is more expensive, thus there is no need to
14751 * try both. */
14752 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
14753 {
14754 /* Swap two characters, that is: skip them. */
14755 gi += 2;
14756 bi += 2;
14757 score += SCORE_SWAP;
14758 continue;
14759 }
14760 }
14761
14762 /* Substitute one character for another which is the same
14763 * thing as deleting a character from both goodword and badword.
14764 * Use a better score when there is only a case difference. */
14765 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
14766 score += SCORE_ICASE;
14767 else
14768 {
14769 /* For a similar character use SCORE_SIMILAR. */
14770 if (slang != NULL
14771 && slang->sl_has_map
14772 && similar_chars(slang, gc, bc))
14773 score += SCORE_SIMILAR;
14774 else
14775 score += SCORE_SUBST;
14776 }
14777
14778 if (score < minscore)
14779 {
14780 /* Do the substitution. */
14781 ++gi;
14782 ++bi;
14783 continue;
14784 }
14785 }
14786pop:
14787 /*
14788 * Get here to try the next alternative, pop it from the stack.
14789 */
14790 if (stackidx == 0) /* stack is empty, finished */
14791 break;
14792
14793 /* pop an item from the stack */
14794 --stackidx;
14795 gi = stack[stackidx].goodi;
14796 bi = stack[stackidx].badi;
14797 score = stack[stackidx].score;
14798 }
14799
14800 /* When the score goes over "limit" it may actually be much higher.
14801 * Return a very large number to avoid going below the limit when giving a
14802 * bonus. */
14803 if (minscore > limit)
14804 return SCORE_MAXMAX;
14805 return minscore;
14806}
14807#endif
14808
14809#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
14810#define DUMPFLAG_COUNT 2 /* include word count */
14811
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014812/*
14813 * ":spelldump"
14814 */
14815/*ARGSUSED*/
14816 void
14817ex_spelldump(eap)
14818 exarg_T *eap;
14819{
14820 buf_T *buf = curbuf;
14821 langp_T *lp;
14822 slang_T *slang;
14823 idx_T arridx[MAXWLEN];
14824 int curi[MAXWLEN];
14825 char_u word[MAXWLEN];
14826 int c;
14827 char_u *byts;
14828 idx_T *idxs;
14829 linenr_T lnum = 0;
14830 int round;
14831 int depth;
14832 int n;
14833 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000014834 char_u *region_names = NULL; /* region names being used */
14835 int do_region = TRUE; /* dump region names and numbers */
14836 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014837 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000014838 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014839
Bram Moolenaar95529562005-08-25 21:21:38 +000014840 if (no_spell_checking(curwin))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014841 return;
14842
14843 /* Create a new empty buffer by splitting the window. */
14844 do_cmdline_cmd((char_u *)"new");
14845 if (!bufempty() || !buf_valid(buf))
14846 return;
14847
Bram Moolenaar7887d882005-07-01 22:33:52 +000014848 /* Find out if we can support regions: All languages must support the same
14849 * regions or none at all. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014850 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000014851 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014852 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000014853 p = lp->lp_slang->sl_regions;
14854 if (p[0] != 0)
14855 {
14856 if (region_names == NULL) /* first language with regions */
14857 region_names = p;
14858 else if (STRCMP(region_names, p) != 0)
14859 {
14860 do_region = FALSE; /* region names are different */
14861 break;
14862 }
14863 }
14864 }
14865
14866 if (do_region && region_names != NULL)
14867 {
14868 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
14869 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
14870 }
14871 else
14872 do_region = FALSE;
14873
14874 /*
14875 * Loop over all files loaded for the entries in 'spelllang'.
14876 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014877 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014878 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014879 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014880 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014881 if (slang->sl_fbyts == NULL) /* reloading failed */
14882 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014883
14884 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
14885 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
14886
14887 /* round 1: case-folded tree
14888 * round 2: keep-case tree */
14889 for (round = 1; round <= 2; ++round)
14890 {
14891 if (round == 1)
14892 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014893 dumpflags = 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014894 byts = slang->sl_fbyts;
14895 idxs = slang->sl_fidxs;
14896 }
14897 else
14898 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014899 dumpflags = DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014900 byts = slang->sl_kbyts;
14901 idxs = slang->sl_kidxs;
14902 }
14903 if (byts == NULL)
14904 continue; /* array is empty */
14905
Bram Moolenaar4770d092006-01-12 23:22:24 +000014906 if (eap->forceit)
14907 dumpflags |= DUMPFLAG_COUNT;
14908
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014909 depth = 0;
14910 arridx[0] = 0;
14911 curi[0] = 1;
14912 while (depth >= 0 && !got_int)
14913 {
14914 if (curi[depth] > byts[arridx[depth]])
14915 {
14916 /* Done all bytes at this node, go up one level. */
14917 --depth;
14918 line_breakcheck();
14919 }
14920 else
14921 {
14922 /* Do one more byte at this node. */
14923 n = arridx[depth] + curi[depth];
14924 ++curi[depth];
14925 c = byts[n];
14926 if (c == 0)
14927 {
14928 /* End of word, deal with the word.
14929 * Don't use keep-case words in the fold-case tree,
14930 * they will appear in the keep-case tree.
14931 * Only use the word when the region matches. */
14932 flags = (int)idxs[n];
14933 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014934 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000014935 && (do_region
14936 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000014937 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014938 & lp->lp_region) != 0))
14939 {
14940 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000014941 if (!do_region)
14942 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000014943
14944 /* Dump the basic word if there is no prefix or
14945 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000014946 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000014947 if (c == 0 || curi[depth] == 2)
Bram Moolenaar4770d092006-01-12 23:22:24 +000014948 dump_word(slang, word, dumpflags,
14949 flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014950
14951 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000014952 if (c != 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000014953 lnum = dump_prefixes(slang, word, dumpflags,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014954 flags, lnum);
14955 }
14956 }
14957 else
14958 {
14959 /* Normal char, go one level deeper. */
14960 word[depth++] = c;
14961 arridx[depth] = idxs[n];
14962 curi[depth] = 1;
14963 }
14964 }
14965 }
14966 }
14967 }
14968
14969 /* Delete the empty line that we started with. */
14970 if (curbuf->b_ml.ml_line_count > 1)
14971 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
14972
14973 redraw_later(NOT_VALID);
14974}
14975
14976/*
14977 * Dump one word: apply case modifications and append a line to the buffer.
14978 */
14979 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000014980dump_word(slang, word, dumpflags, flags, lnum)
14981 slang_T *slang;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014982 char_u *word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000014983 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014984 int flags;
14985 linenr_T lnum;
14986{
14987 int keepcap = FALSE;
14988 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +000014989 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014990 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000014991 char_u badword[MAXWLEN + 10];
14992 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014993
Bram Moolenaar4770d092006-01-12 23:22:24 +000014994 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014995 {
14996 /* Need to fix case according to "flags". */
14997 make_case_word(word, cword, flags);
14998 p = cword;
14999 }
15000 else
15001 {
15002 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015003 if ((dumpflags & DUMPFLAG_KEEPCASE)
15004 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000015005 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015006 keepcap = TRUE;
15007 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015008 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015009
Bram Moolenaar7887d882005-07-01 22:33:52 +000015010 /* Add flags and regions after a slash. */
15011 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015012 {
Bram Moolenaar7887d882005-07-01 22:33:52 +000015013 STRCPY(badword, p);
15014 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015015 if (keepcap)
15016 STRCAT(badword, "=");
15017 if (flags & WF_BANNED)
15018 STRCAT(badword, "!");
15019 else if (flags & WF_RARE)
15020 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +000015021 if (flags & WF_REGION)
15022 for (i = 0; i < 7; ++i)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015023 if (flags & (0x10000 << i))
Bram Moolenaar7887d882005-07-01 22:33:52 +000015024 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015025 p = badword;
15026 }
15027
Bram Moolenaar4770d092006-01-12 23:22:24 +000015028 if (dumpflags & DUMPFLAG_COUNT)
15029 {
15030 hashitem_T *hi;
15031
15032 /* Include the word count for ":spelldump!". */
15033 hi = hash_find(&slang->sl_wordcount, tw);
15034 if (!HASHITEM_EMPTY(hi))
15035 {
15036 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
15037 tw, HI2WC(hi)->wc_count);
15038 p = IObuff;
15039 }
15040 }
15041
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015042 ml_append(lnum, p, (colnr_T)0, FALSE);
15043}
15044
15045/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000015046 * For ":spelldump": Find matching prefixes for "word". Prepend each to
15047 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015048 * Return the updated line number.
15049 */
15050 static linenr_T
Bram Moolenaar4770d092006-01-12 23:22:24 +000015051dump_prefixes(slang, word, dumpflags, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015052 slang_T *slang;
15053 char_u *word; /* case-folded word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000015054 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015055 int flags; /* flags with prefix ID */
15056 linenr_T startlnum;
15057{
15058 idx_T arridx[MAXWLEN];
15059 int curi[MAXWLEN];
15060 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000015061 char_u word_up[MAXWLEN];
15062 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015063 int c;
15064 char_u *byts;
15065 idx_T *idxs;
15066 linenr_T lnum = startlnum;
15067 int depth;
15068 int n;
15069 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015070 int i;
15071
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015072 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +000015073 * upper-case letter in word_up[]. */
15074 c = PTR2CHAR(word);
15075 if (SPELL_TOUPPER(c) != c)
15076 {
15077 onecap_copy(word, word_up, TRUE);
15078 has_word_up = TRUE;
15079 }
15080
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015081 byts = slang->sl_pbyts;
15082 idxs = slang->sl_pidxs;
15083 if (byts != NULL) /* array not is empty */
15084 {
15085 /*
15086 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015087 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015088 */
15089 depth = 0;
15090 arridx[0] = 0;
15091 curi[0] = 1;
15092 while (depth >= 0 && !got_int)
15093 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015094 n = arridx[depth];
15095 len = byts[n];
15096 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015097 {
15098 /* Done all bytes at this node, go up one level. */
15099 --depth;
15100 line_breakcheck();
15101 }
15102 else
15103 {
15104 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015105 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015106 ++curi[depth];
15107 c = byts[n];
15108 if (c == 0)
15109 {
15110 /* End of prefix, find out how many IDs there are. */
15111 for (i = 1; i < len; ++i)
15112 if (byts[n + i] != 0)
15113 break;
15114 curi[depth] += i - 1;
15115
Bram Moolenaar53805d12005-08-01 07:08:33 +000015116 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
15117 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015118 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000015119 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaar4770d092006-01-12 23:22:24 +000015120 dump_word(slang, prefix, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015121 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000015122 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015123 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000015124
15125 /* Check for prefix that matches the word when the
15126 * first letter is upper-case, but only if the prefix has
15127 * a condition. */
15128 if (has_word_up)
15129 {
15130 c = valid_word_prefix(i, n, flags, word_up, slang,
15131 TRUE);
15132 if (c != 0)
15133 {
15134 vim_strncpy(prefix + depth, word_up,
15135 MAXWLEN - depth - 1);
Bram Moolenaar4770d092006-01-12 23:22:24 +000015136 dump_word(slang, prefix, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015137 (c & WF_RAREPFX) ? (flags | WF_RARE)
15138 : flags, lnum++);
15139 }
15140 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015141 }
15142 else
15143 {
15144 /* Normal char, go one level deeper. */
15145 prefix[depth++] = c;
15146 arridx[depth] = idxs[n];
15147 curi[depth] = 1;
15148 }
15149 }
15150 }
15151 }
15152
15153 return lnum;
15154}
15155
Bram Moolenaar95529562005-08-25 21:21:38 +000015156/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015157 * Move "p" to the end of word "start".
15158 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +000015159 */
15160 char_u *
15161spell_to_word_end(start, buf)
15162 char_u *start;
15163 buf_T *buf;
15164{
15165 char_u *p = start;
15166
15167 while (*p != NUL && spell_iswordp(p, buf))
15168 mb_ptr_adv(p);
15169 return p;
15170}
15171
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015172#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015173/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015174 * For Insert mode completion CTRL-X s:
15175 * Find start of the word in front of column "startcol".
15176 * We don't check if it is badly spelled, with completion we can only change
15177 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015178 * Returns the column number of the word.
15179 */
15180 int
15181spell_word_start(startcol)
15182 int startcol;
15183{
15184 char_u *line;
15185 char_u *p;
15186 int col = 0;
15187
Bram Moolenaar95529562005-08-25 21:21:38 +000015188 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015189 return startcol;
15190
15191 /* Find a word character before "startcol". */
15192 line = ml_get_curline();
15193 for (p = line + startcol; p > line; )
15194 {
15195 mb_ptr_back(line, p);
15196 if (spell_iswordp_nmw(p))
15197 break;
15198 }
15199
15200 /* Go back to start of the word. */
15201 while (p > line)
15202 {
15203 col = p - line;
15204 mb_ptr_back(line, p);
15205 if (!spell_iswordp(p, curbuf))
15206 break;
15207 col = 0;
15208 }
15209
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015210 return col;
15211}
15212
15213/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000015214 * Need to check for 'spellcapcheck' now, the word is removed before
15215 * expand_spelling() is called. Therefore the ugly global variable.
15216 */
15217static int spell_expand_need_cap;
15218
15219 void
15220spell_expand_check_cap(col)
15221 colnr_T col;
15222{
15223 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
15224}
15225
15226/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015227 * Get list of spelling suggestions.
15228 * Used for Insert mode completion CTRL-X ?.
15229 * Returns the number of matches. The matches are in "matchp[]", array of
15230 * allocated strings.
15231 */
15232/*ARGSUSED*/
15233 int
15234expand_spelling(lnum, col, pat, matchp)
15235 linenr_T lnum;
15236 int col;
15237 char_u *pat;
15238 char_u ***matchp;
15239{
15240 garray_T ga;
15241
Bram Moolenaar4770d092006-01-12 23:22:24 +000015242 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015243 *matchp = ga.ga_data;
15244 return ga.ga_len;
15245}
15246#endif
15247
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000015248#endif /* FEAT_SYN_HL */