blob: a3fc24f72089c4a402ddde3739eff1f175b90661 [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 Moolenaar63d5a1e2005-04-19 21:30:25 +000046 *
47 * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000048 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000049 * Why doesn't Vim use aspell/ispell/myspell/etc.?
50 * See ":help develop-spell".
51 */
52
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000053/* Use SPELL_PRINTTREE for debugging: dump the word tree after adding a word.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000054 * Only use it for small word lists! */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000055#if 0
56# define SPELL_PRINTTREE
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000057#endif
58
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000059/* SPELL_COMPRESS_CNT is after how many allocated blocks we compress the tree
60 * to limit the amount of memory used (esp. for Italian and Hungarian). The
61 * amount of memory used for nodes then is SPELL_COMPRESS_CNT times
62 * SBLOCKSIZE.
63 * Then compress again after allocating SPELL_COMPRESS_INC more blocks or
64 * adding SPELL_COMPRESS_ADDED words and running out of memory again. */
65#define SPELL_COMPRESS_CNT 30000
66#define SPELL_COMPRESS_INC 100
67#define SPELL_COMPRESS_ADDED 500000
68
Bram Moolenaar51485f02005-06-04 21:55:20 +000069/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000070 * Use this to adjust the score after finding suggestions, based on the
71 * suggested word sounding like the bad word. This is much faster than doing
72 * it for every possible suggestion.
73 * Disadvantage: When "the" is typed as "hte" it sounds different and goes
74 * down in the list.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000075 * Used when 'spellsuggest' is set to "best".
76 */
77#define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4)
78
79/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +000080 * Vim spell file format: <HEADER>
81 * <SUGGEST>
82 * <LWORDTREE>
83 * <KWORDTREE>
84 * <PREFIXTREE>
Bram Moolenaar51485f02005-06-04 21:55:20 +000085 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000086 * <HEADER>: <fileID>
87 * <regioncnt> <regionname> ...
88 * <charflagslen> <charflags>
89 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000090 * <midwordlen> <midword>
Bram Moolenaarae5bce12005-08-15 21:41:48 +000091 * <compoundlen> <compoundtype> <compoundinfo>
Bram Moolenaar1d73c882005-06-19 22:48:47 +000092 * <prefcondcnt> <prefcond> ...
Bram Moolenaar51485f02005-06-04 21:55:20 +000093 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +000094 * <fileID> 10 bytes "VIMspell10"
Bram Moolenaar51485f02005-06-04 21:55:20 +000095 * <regioncnt> 1 byte number of regions following (8 supported)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000096 * <regionname> 2 bytes Region name: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +000097 * First <regionname> is region 1.
98 *
99 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
100 * <charflags> N bytes List of flags (first one is for character 128):
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000101 * 0x01 word character CF_WORD
102 * 0x02 upper-case character CF_UPPER
Bram Moolenaar51485f02005-06-04 21:55:20 +0000103 * <fcharslen> 2 bytes Number of bytes in <fchars>.
104 * <fchars> N bytes Folded characters, first one is for character 128.
105 *
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000106 * <midwordlen> 2 bytes Number of bytes in <midword>.
107 * <midword> N bytes Characters that are word characters only when used
108 * in the middle of a word.
109 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000110 * <compoundlen> 2 bytes Number of bytes following for compound info (can
111 * be used to skip it when it's not understood).
112 *
113 * <compoundtype 1 byte 1: compound words using <comp1minlen> and
114 * <comp1flags>
115 *
116 * <comp1minlen> 1 byte minimal word length for compounding
117 *
118 * <comp1flags> N bytes flags used for compounding words
119 *
120 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000121 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
122 *
123 * <prefcond> : <condlen> <condstr>
124 *
125 * <condlen> 1 byte Length of <condstr>.
126 *
127 * <condstr> N bytes Condition for the prefix.
128 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000129 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000130 * <SUGGEST> : <repcount> <rep> ...
131 * <salflags> <salcount> <sal> ...
132 * <maplen> <mapstr>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000133 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000134 * <repcount> 2 bytes number of <rep> items, MSB first.
135 *
136 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
137 *
138 * <repfromlen> 1 byte length of <repfrom>
139 *
140 * <repfrom> N bytes "from" part of replacement
141 *
142 * <reptolen> 1 byte length of <repto>
143 *
144 * <repto> N bytes "to" part of replacement
145 *
146 * <salflags> 1 byte flags for soundsalike conversion:
147 * SAL_F0LLOWUP
148 * SAL_COLLAPSE
149 * SAL_REM_ACCENTS
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000150 * SAL_SOFO: SOFOFROM and SOFOTO used instead of SAL
151 *
152 * <salcount> 2 bytes number of <sal> items following
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000153 *
154 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
155 *
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000156 * <salfromlen> 1-2 bytes length of <salfrom> (2 bytes for SAL_SOFO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000157 *
158 * <salfrom> N bytes "from" part of soundsalike
159 *
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000160 * <saltolen> 1-2 bytes length of <salto> (2 bytes for SAL_SOFO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000161 *
162 * <salto> N bytes "to" part of soundsalike
163 *
164 * <maplen> 2 bytes length of <mapstr>, MSB first
165 *
166 * <mapstr> N bytes String with sequences of similar characters,
167 * separated by slashes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000168 *
169 *
170 * <LWORDTREE>: <wordtree>
171 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000172 * <KWORDTREE>: <wordtree>
173 *
174 * <PREFIXTREE>: <wordtree>
175 *
176 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000177 * <wordtree>: <nodecount> <nodedata> ...
178 *
179 * <nodecount> 4 bytes Number of nodes following. MSB first.
180 *
181 * <nodedata>: <siblingcount> <sibling> ...
182 *
183 * <siblingcount> 1 byte Number of siblings in this node. The siblings
184 * follow in sorted order.
185 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000186 * <sibling>: <byte> [ <nodeidx> <xbyte>
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000187 * | <flags> [<flags2>] [<region>] [<affixID>]
188 * | [<pflags>] <affixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000189 *
190 * <byte> 1 byte Byte value of the sibling. Special cases:
191 * BY_NOFLAGS: End of word without flags and for all
192 * regions.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000193 * For PREFIXTREE <affixID> and
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000194 * <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000195 * BY_FLAGS: End of word, <flags> follow.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000196 * For PREFIXTREE <pflags>, <affixID>
Bram Moolenaar53805d12005-08-01 07:08:33 +0000197 * and <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000198 * BY_FLAGS2: End of word, <flags> and <flags2>
199 * follow. Not used in PREFIXTREE.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000200 * BY_INDEX: Child of sibling is shared, <nodeidx>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000201 * and <xbyte> follow.
202 *
203 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
204 *
205 * <xbyte> 1 byte byte value of the sibling.
206 *
207 * <flags> 1 byte bitmask of:
208 * WF_ALLCAP word must have only capitals
209 * WF_ONECAP first char of word must be capital
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000210 * WF_KEEPCAP keep-case word
211 * WF_FIXCAP keep-case word, all caps not allowed
Bram Moolenaar51485f02005-06-04 21:55:20 +0000212 * WF_RARE rare word
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000213 * WF_BANNED bad word
Bram Moolenaar51485f02005-06-04 21:55:20 +0000214 * WF_REGION <region> follows
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000215 * WF_AFX <affixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000216 *
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000217 * <flags2> 1 byte Only used when there are postponed prefixes.
218 * Bitmask of:
219 * WF_HAS_AFF >> 8 word includes affix
220 *
Bram Moolenaar53805d12005-08-01 07:08:33 +0000221 * <pflags> 1 byte bitmask of:
222 * WFP_RARE rare prefix
223 * WFP_NC non-combining prefix
224 * WFP_UP letter after prefix made upper case
225 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000226 * <region> 1 byte Bitmask for regions in which word is valid. When
227 * omitted it's valid in all regions.
228 * Lowest bit is for region 1.
229 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000230 * <affixID> 1 byte ID of affix that can be used with this word. In
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000231 * PREFIXTREE used for the required prefix ID.
232 *
233 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
234 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000235 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000236 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000237 */
238
Bram Moolenaare19defe2005-03-21 08:23:33 +0000239#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
240# include <io.h> /* for lseek(), must be before vim.h */
241#endif
242
243#include "vim.h"
244
245#if defined(FEAT_SYN_HL) || defined(PROTO)
246
247#ifdef HAVE_FCNTL_H
248# include <fcntl.h>
249#endif
250
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000251#define MAXWLEN 250 /* Assume max. word len is this many bytes.
252 Some places assume a word length fits in a
253 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000254
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000255/* Type used for indexes in the word tree need to be at least 3 bytes. If int
256 * is 8 bytes we could use something smaller, but what? */
257#if SIZEOF_INT > 2
258typedef int idx_T;
259#else
260typedef long idx_T;
261#endif
262
263/* Flags used for a word. Only the lowest byte can be used, the region byte
264 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000265#define WF_REGION 0x01 /* region byte follows */
266#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
267#define WF_ALLCAP 0x04 /* word must be all capitals */
268#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000269#define WF_BANNED 0x10 /* bad word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000270#define WF_AFX 0x20 /* affix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000271#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000272#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000273
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000274/* for <flags2>, shifted up one byte to be used in wn_flags */
275#define WF_HAS_AFF 0x0100 /* word includes affix */
276
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000277#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000278
Bram Moolenaar53805d12005-08-01 07:08:33 +0000279/* flags for <pflags> */
280#define WFP_RARE 0x01 /* rare prefix */
281#define WFP_NC 0x02 /* prefix is not combining */
282#define WFP_UP 0x04 /* to-upper prefix */
283
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000284/* Flags for postponed prefixes. Must be above affixID (one byte)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000285 * and prefcondnr (two bytes). */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000286#define WF_RAREPFX (WFP_RARE << 24) /* in sl_pidxs: flag for rare
287 * postponed prefix */
288#define WF_PFX_NC (WFP_NC << 24) /* in sl_pidxs: flag for non-combining
289 * postponed prefix */
290#define WF_PFX_UP (WFP_UP << 24) /* in sl_pidxs: flag for to-upper
291 * postponed prefix */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000292
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000293/* Special byte values for <byte>. Some are only used in the tree for
294 * postponed prefixes, some only in the other trees. This is a bit messy... */
295#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000296 * postponed prefix: no <pflags> */
297#define BY_INDEX 1 /* child is shared, index follows */
298#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
299 * postponed prefix: <pflags> follows */
300#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
301 * follow; never used in prefix tree */
302#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000303
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000304/* Info from "REP" and "SAL" entries in ".aff" file used in si_rep, sl_rep,
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000305 * and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000306 * One replacement: from "ft_from" to "ft_to". */
307typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000308{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000309 char_u *ft_from;
310 char_u *ft_to;
311} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000312
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000313/* Info from "SAL" entries in ".aff" file used in sl_sal.
314 * The info is split for quick processing by spell_soundfold().
315 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
316typedef struct salitem_S
317{
318 char_u *sm_lead; /* leading letters */
319 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000320 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000321 char_u *sm_rules; /* rules like ^, $, priority */
322 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000323#ifdef FEAT_MBYTE
324 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000325 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000326 int *sm_to_w; /* wide character copy of "sm_to" */
327#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000328} salitem_T;
329
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000330#ifdef FEAT_MBYTE
331typedef int salfirst_T;
332#else
333typedef short salfirst_T;
334#endif
335
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000336/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000337 * Structure used to store words and other info for one language, loaded from
338 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000339 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
340 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
341 *
342 * The "byts" array stores the possible bytes in each tree node, preceded by
343 * the number of possible bytes, sorted on byte value:
344 * <len> <byte1> <byte2> ...
345 * The "idxs" array stores the index of the child node corresponding to the
346 * byte in "byts".
347 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000348 * the flags, region mask and affixID for the word. There may be several
349 * zeros in sequence for alternative flag/region/affixID combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000350 */
351typedef struct slang_S slang_T;
352struct slang_S
353{
354 slang_T *sl_next; /* next language */
355 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000356 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000357 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000358
Bram Moolenaar51485f02005-06-04 21:55:20 +0000359 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000360 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000361 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000362 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000363 char_u *sl_pbyts; /* prefix tree word bytes */
364 idx_T *sl_pidxs; /* prefix tree word indexes */
365
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000366 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000367
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000368 char_u *sl_midword; /* MIDWORD string or NULL */
369
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000370 int sl_compminlen; /* COMPOUNDMIN */
371 char_u *sl_compflags; /* COMPOUNDFLAGS (NULL when no compounding) */
372
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000373 int sl_prefixcnt; /* number of items in "sl_prefprog" */
374 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
375
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000376 garray_T sl_rep; /* list of fromto_T entries from REP lines */
377 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
378 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000379 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000380 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000381 there is none */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000382 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
383 * "sl_sal_first" maps chars, when has_mbyte
384 * "sl_sal" is a list of wide char lists. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000385 int sl_followup; /* SAL followup */
386 int sl_collapse; /* SAL collapse_result */
387 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaarea424162005-06-16 21:51:00 +0000388 int sl_has_map; /* TRUE if there is a MAP line */
389#ifdef FEAT_MBYTE
390 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
391 int sl_map_array[256]; /* MAP for first 256 chars */
392#else
393 char_u sl_map_array[256]; /* MAP for first 256 chars */
394#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000395};
396
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000397/* First language that is loaded, start of the linked list of loaded
398 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000399static slang_T *first_lang = NULL;
400
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000401/* Flags used in .spl file for soundsalike flags. */
402#define SAL_F0LLOWUP 1
403#define SAL_COLLAPSE 2
404#define SAL_REM_ACCENTS 4
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000405#define SAL_SOFO 8 /* SOFOFROM and SOFOTO instead of SAL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000406
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000407/*
408 * Structure used in "b_langp", filled from 'spelllang'.
409 */
410typedef struct langp_S
411{
412 slang_T *lp_slang; /* info for this language (NULL for last one) */
413 int lp_region; /* bitmask for region or REGION_ALL */
414} langp_T;
415
416#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
417
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000418#define REGION_ALL 0xff /* word valid in all regions */
419
420/* Result values. Lower number is accepted over higher one. */
421#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000422#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000423#define SP_RARE 1
424#define SP_LOCAL 2
425#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000426
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000427#define VIMSPELLMAGIC "VIMspell10" /* string at start of Vim spell file */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000428#define VIMSPELLMAGICL 10
429
Bram Moolenaar7887d882005-07-01 22:33:52 +0000430/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000431static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000432
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000433/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000434 * Information used when looking for suggestions.
435 */
436typedef struct suginfo_S
437{
438 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000439 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000440 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000441 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000442 char_u *su_badptr; /* start of bad word in line */
443 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000444 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000445 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
446 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
447 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000448} suginfo_T;
449
450/* One word suggestion. Used in "si_ga". */
451typedef struct suggest_S
452{
453 char_u *st_word; /* suggested word, allocated string */
454 int st_orglen; /* length of replaced text */
455 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000456 int st_altscore; /* used when st_score compares equal */
457 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000458 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000459} suggest_T;
460
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000461#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000462
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000463/* Number of suggestions kept when cleaning up. When rescore_suggestions() is
464 * called the score may change, thus we need to keep more than what is
465 * displayed. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000466#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 50 ? 50 : (su)->su_maxcount)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000467
468/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
469 * of suggestions that are not going to be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000470#define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000471
472/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000473#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000474#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000475#define SCORE_REGION 70 /* word is for different region */
476#define SCORE_RARE 180 /* rare word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000477#define SCORE_SWAP 90 /* swap two characters */
478#define SCORE_SWAP3 110 /* swap two characters in three */
479#define SCORE_REP 87 /* REP replacement */
480#define SCORE_SUBST 93 /* substitute a character */
481#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000482#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000483#define SCORE_DEL 94 /* delete a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000484#define SCORE_DELDUP 64 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000485#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000486#define SCORE_INS 96 /* insert a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000487#define SCORE_INSDUP 66 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000488#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000489#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000490
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000491#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000492#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
493 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000494
495#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000496#define SCORE_MAXMAX 999999 /* accept any score */
497
498/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000499 * Structure to store info for word matching.
500 */
501typedef struct matchinf_S
502{
503 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000504
505 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000506 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000507 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000508 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000509 char_u *mi_cend; /* char after what was used for
510 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000511
512 /* case-folded text */
513 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000514 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000515
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000516 /* for when checking word after a prefix */
517 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000518 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000519 int mi_prefcnt; /* number of entries at mi_prefarridx */
520 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000521#ifdef FEAT_MBYTE
522 int mi_cprefixlen; /* byte length of prefix in original
523 case */
524#else
525# define mi_cprefixlen mi_prefixlen /* it's the same value */
526#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000527
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000528 /* for when checking a compound word */
529 int mi_compoff; /* start of following word offset */
530
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000531 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000532 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000533 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000534 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000535} matchinf_T;
536
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000537/*
538 * The tables used for recognizing word characters according to spelling.
539 * These are only used for the first 256 characters of 'encoding'.
540 */
541typedef struct spelltab_S
542{
543 char_u st_isw[256]; /* flags: is word char */
544 char_u st_isu[256]; /* flags: is uppercase char */
545 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000546 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000547} spelltab_T;
548
549static spelltab_T spelltab;
550static int did_set_spelltab;
551
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000552#define CF_WORD 0x01
553#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000554
555static void clear_spell_chartab __ARGS((spelltab_T *sp));
556static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000557static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
558static int spell_iswordp_nmw __ARGS((char_u *p));
559#ifdef FEAT_MBYTE
560static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
561#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000562static void write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000563
564/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000565 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000566 */
567typedef enum
568{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000569 STATE_START = 0, /* At start of node check for NUL bytes (goodword
570 * ends); if badword ends there is a match, otherwise
571 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000572 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000573 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000574 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
575 STATE_PLAIN, /* Use each byte of the node. */
576 STATE_DEL, /* Delete a byte from the bad word. */
577 STATE_INS, /* Insert a byte in the bad word. */
578 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000579 STATE_UNSWAP, /* Undo swap two characters. */
580 STATE_SWAP3, /* Swap two characters over three. */
581 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000582 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000583 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000584 STATE_REP_INI, /* Prepare for using REP items. */
585 STATE_REP, /* Use matching REP items from the .aff file. */
586 STATE_REP_UNDO, /* Undo a REP item replacement. */
587 STATE_FINAL /* End of this node. */
588} state_T;
589
590/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000591 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000592 */
593typedef struct trystate_S
594{
Bram Moolenaarea424162005-06-16 21:51:00 +0000595 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000596 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000597 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000598 short ts_curi; /* index in list of child nodes */
599 char_u ts_fidx; /* index in fword[], case-folded bad word */
600 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
601 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000602 char_u ts_prefixdepth; /* stack depth for end of prefix or
603 * PFD_PREFIXTREE or PFD_NOPREFIX or
604 * PFD_COMPOUND */
Bram Moolenaarea424162005-06-16 21:51:00 +0000605#ifdef FEAT_MBYTE
606 char_u ts_tcharlen; /* number of bytes in tword character */
607 char_u ts_tcharidx; /* current byte index in tword character */
608 char_u ts_isdiff; /* DIFF_ values */
609 char_u ts_fcharstart; /* index in fword where badword char started */
610#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000611 char_u ts_save_prewordlen; /* saved "prewordlen" */
Bram Moolenaarea424162005-06-16 21:51:00 +0000612 char_u ts_save_splitoff; /* su_splitoff saved here */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000613 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000614} trystate_T;
615
Bram Moolenaarea424162005-06-16 21:51:00 +0000616/* values for ts_isdiff */
617#define DIFF_NONE 0 /* no different byte (yet) */
618#define DIFF_YES 1 /* different byte found */
619#define DIFF_INSERT 2 /* inserting character */
620
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000621/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000622#define PFD_COMPOUND 0xfd /* prefixed is a compound word */
623#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
624#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000625
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000626/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000627#define FIND_FOLDWORD 0 /* find word case-folded */
628#define FIND_KEEPWORD 1 /* find keep-case word */
629#define FIND_PREFIX 2 /* find word after prefix */
630#define FIND_COMPOUND 3 /* find case-folded compound word */
631#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000632
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000633static slang_T *slang_alloc __ARGS((char_u *lang));
634static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000635static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000636static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000637static int can_compound __ARGS((slang_T *slang, int flags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000638static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang, int cond_req));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000639static void find_prefix __ARGS((matchinf_T *mip));
640static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000641static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000642static int no_spell_checking __ARGS((void));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000643static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000644static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000645static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000646static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000647static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000648static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000649static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
650static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000651#ifdef FEAT_MBYTE
652static int *mb_str2wide __ARGS((char_u *s));
653#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000654static idx_T read_tree __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, int startidx, int prefixtree, int maxprefcondnr));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000655static void clear_midword __ARGS((buf_T *buf));
656static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000657static int find_region __ARGS((char_u *rp, char_u *region));
658static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000659static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000660static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000661static int set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000662static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
663static void write_spell_chartab __ARGS((FILE *fd));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000664static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000665static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +0000666static void spell_find_suggest __ARGS((char_u *badptr, suginfo_T *su, int maxcount, int banbadword, int need_cap));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000667#ifdef FEAT_EVAL
668static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
669#endif
670static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
671static void spell_suggest_intern __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000672static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000673static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000674static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000675static void suggest_try_special __ARGS((suginfo_T *su));
676static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000677static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000678#ifdef FEAT_MBYTE
679static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
680#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000681static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000682static void score_comp_sal __ARGS((suginfo_T *su));
683static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000684static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000685static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000686static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000687static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000688static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000689static void add_suggestion __ARGS((suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int score, int altscore, int had_bonus));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000690static void add_banned __ARGS((suginfo_T *su, char_u *word));
691static int was_banned __ARGS((suginfo_T *su, char_u *word));
692static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000693static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000694static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000695static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
696static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
697static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000698#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000699static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000700#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000701static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000702static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000703static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum));
704static linenr_T apply_prefixes __ARGS((slang_T *slang, char_u *word, int round, int flags, linenr_T startlnum));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000705
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000706/*
707 * Use our own character-case definitions, because the current locale may
708 * differ from what the .spl file uses.
709 * These must not be called with negative number!
710 */
711#ifndef FEAT_MBYTE
712/* Non-multi-byte implementation. */
713# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
714# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
715# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
716#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000717# if defined(HAVE_WCHAR_H)
718# include <wchar.h> /* for towupper() and towlower() */
719# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000720/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
721 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
722 * the "w" library function for characters above 255 if available. */
723# ifdef HAVE_TOWLOWER
724# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
725 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
726# else
727# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
728 : (c) < 256 ? spelltab.st_fold[c] : (c))
729# endif
730
731# ifdef HAVE_TOWUPPER
732# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
733 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
734# else
735# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
736 : (c) < 256 ? spelltab.st_upper[c] : (c))
737# endif
738
739# ifdef HAVE_ISWUPPER
740# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
741 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
742# else
743# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000744 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000745# endif
746#endif
747
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000748
749static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000750static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000751static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000752static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000753
754/*
755 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000756 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000757 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
758 * or when it's OK it remains unchanged.
759 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000760 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000761 * "capcol" is used to check for a Capitalised word after the end of a
762 * sentence. If it's zero then perform the check. Return the column where to
763 * check next, or -1 when no sentence end was found. If it's NULL then don't
764 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000765 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000766 * Returns the length of the word in bytes, also when it's OK, so that the
767 * caller can skip over the word.
768 */
769 int
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000770spell_check(wp, ptr, attrp, capcol)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000771 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000772 char_u *ptr;
773 int *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000774 int *capcol; /* column to check for Capital */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000775{
776 matchinf_T mi; /* Most things are put in "mi" so that it can
777 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000778 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000779 int c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000780
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000781 /* A word never starts at a space or a control character. Return quickly
782 * then, skipping over the character. */
783 if (*ptr <= ' ')
784 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000785
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000786 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar0c405862005-06-22 22:26:26 +0000787 * 0X99FF. But when a word character follows do check spelling to find
788 * "3GPP". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000789 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000790 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000791 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
792 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000793 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000794 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000795 mi.mi_end = skipdigits(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000796 nrlen = mi.mi_end - ptr;
797 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000798 if (!spell_iswordp(mi.mi_end, wp->w_buffer))
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000799 return (int)(mi.mi_end - ptr);
Bram Moolenaar0c405862005-06-22 22:26:26 +0000800
801 /* Try including the digits in the word. */
802 mi.mi_fend = ptr + nrlen;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000803 }
Bram Moolenaar0c405862005-06-22 22:26:26 +0000804 else
805 mi.mi_fend = ptr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000806
Bram Moolenaar0c405862005-06-22 22:26:26 +0000807 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000808 mi.mi_word = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000809 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000810 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000811 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000812 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000813 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000814 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000815
816 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
817 {
818 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000819 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000820 if (!SPELL_ISUPPER(c))
821 {
822 *attrp = highlight_attr[HLF_SPC];
823 return (int)(mi.mi_fend - ptr);
824 }
825 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000826 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000827 if (capcol != NULL)
828 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000829
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000830 /* We always use the characters up to the next non-word character,
831 * also for bad words. */
832 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000833
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000834 /* Check caps type later. */
835 mi.mi_capflags = 0;
836 mi.mi_cend = NULL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000837 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000838
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000839 /* Include one non-word character so that we can check for the
840 * word end. */
841 if (*mi.mi_fend != NUL)
842 mb_ptr_adv(mi.mi_fend);
843
844 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
845 MAXWLEN + 1);
846 mi.mi_fwordlen = STRLEN(mi.mi_fword);
847
848 /* The word is bad unless we recognize it. */
849 mi.mi_result = SP_BAD;
850
851 /*
852 * Loop over the languages specified in 'spelllang'.
853 * We check them all, because a matching word may be longer than an
854 * already found matching word.
855 */
856 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
857 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
858 {
859 /* Check for a matching word in case-folded words. */
860 find_word(&mi, FIND_FOLDWORD);
861
862 /* Check for a matching word in keep-case words. */
863 find_word(&mi, FIND_KEEPWORD);
864
865 /* Check for matching prefixes. */
866 find_prefix(&mi);
867 }
868
869 if (mi.mi_result != SP_OK)
870 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000871 /* If we found a number skip over it. Allows for "42nd". Do flag
872 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000873 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000874 {
875 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
876 return nrlen;
877 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000878
879 /* When we are at a non-word character there is no error, just
880 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000881 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000882 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000883 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
884 {
885 regmatch_T regmatch;
886
887 /* Check for end of sentence. */
888 regmatch.regprog = wp->w_buffer->b_cap_prog;
889 regmatch.rm_ic = FALSE;
890 if (vim_regexec(&regmatch, ptr, 0))
891 *capcol = (int)(regmatch.endp[0] - ptr);
892 }
893
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000894#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000895 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000896 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000897#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000898 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000899 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000900
901 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
902 *attrp = highlight_attr[HLF_SPB];
903 else if (mi.mi_result == SP_RARE)
904 *attrp = highlight_attr[HLF_SPR];
905 else
906 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000907 }
908
Bram Moolenaar51485f02005-06-04 21:55:20 +0000909 return (int)(mi.mi_end - ptr);
910}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000911
Bram Moolenaar51485f02005-06-04 21:55:20 +0000912/*
913 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000914 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
915 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
916 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
917 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000918 *
919 * For a match mip->mi_result is updated.
920 */
921 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000922find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000923 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000924 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000925{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000926 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000927 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000928 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000929 int endidxcnt = 0;
930 int len;
931 int wlen = 0;
932 int flen;
933 int c;
934 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000935 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000936#ifdef FEAT_MBYTE
937 char_u *s;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000938 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000939#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000940 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000941 slang_T *slang = mip->mi_lp->lp_slang;
942 unsigned flags;
943 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000944 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000945 int word_ends;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000946
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000947 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000948 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000949 /* Check for word with matching case in keep-case tree. */
950 ptr = mip->mi_word;
951 flen = 9999; /* no case folding, always enough bytes */
952 byts = slang->sl_kbyts;
953 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000954
955 if (mode == FIND_KEEPCOMPOUND)
956 /* Skip over the previously found word(s). */
957 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000958 }
959 else
960 {
961 /* Check for case-folded in case-folded tree. */
962 ptr = mip->mi_fword;
963 flen = mip->mi_fwordlen; /* available case-folded bytes */
964 byts = slang->sl_fbyts;
965 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000966
967 if (mode == FIND_PREFIX)
968 {
969 /* Skip over the prefix. */
970 wlen = mip->mi_prefixlen;
971 flen -= mip->mi_prefixlen;
972 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000973 else if (mode == FIND_COMPOUND)
974 {
975 /* Skip over the previously found word(s). */
976 wlen = mip->mi_compoff;
977 flen -= mip->mi_compoff;
978 }
979
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000980 }
981
Bram Moolenaar51485f02005-06-04 21:55:20 +0000982 if (byts == NULL)
983 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000984
Bram Moolenaar51485f02005-06-04 21:55:20 +0000985 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000986 * Repeat advancing in the tree until:
987 * - there is a byte that doesn't match,
988 * - we reach the end of the tree,
989 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000990 */
991 for (;;)
992 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000993 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000994 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000995
996 len = byts[arridx++];
997
998 /* If the first possible byte is a zero the word could end here.
999 * Remember this index, we first check for the longest word. */
1000 if (byts[arridx] == 0)
1001 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001002 if (endidxcnt == MAXWLEN)
1003 {
1004 /* Must be a corrupted spell file. */
1005 EMSG(_(e_format));
1006 return;
1007 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001008 endlen[endidxcnt] = wlen;
1009 endidx[endidxcnt++] = arridx++;
1010 --len;
1011
1012 /* Skip over the zeros, there can be several flag/region
1013 * combinations. */
1014 while (len > 0 && byts[arridx] == 0)
1015 {
1016 ++arridx;
1017 --len;
1018 }
1019 if (len == 0)
1020 break; /* no children, word must end here */
1021 }
1022
1023 /* Stop looking at end of the line. */
1024 if (ptr[wlen] == NUL)
1025 break;
1026
1027 /* Perform a binary search in the list of accepted bytes. */
1028 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001029 if (c == TAB) /* <Tab> is handled like <Space> */
1030 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001031 lo = arridx;
1032 hi = arridx + len - 1;
1033 while (lo < hi)
1034 {
1035 m = (lo + hi) / 2;
1036 if (byts[m] > c)
1037 hi = m - 1;
1038 else if (byts[m] < c)
1039 lo = m + 1;
1040 else
1041 {
1042 lo = hi = m;
1043 break;
1044 }
1045 }
1046
1047 /* Stop if there is no matching byte. */
1048 if (hi < lo || byts[lo] != c)
1049 break;
1050
1051 /* Continue at the child (if there is one). */
1052 arridx = idxs[lo];
1053 ++wlen;
1054 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001055
1056 /* One space in the good word may stand for several spaces in the
1057 * checked word. */
1058 if (c == ' ')
1059 {
1060 for (;;)
1061 {
1062 if (flen <= 0 && *mip->mi_fend != NUL)
1063 flen = fold_more(mip);
1064 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1065 break;
1066 ++wlen;
1067 --flen;
1068 }
1069 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001070 }
1071
1072 /*
1073 * Verify that one of the possible endings is valid. Try the longest
1074 * first.
1075 */
1076 while (endidxcnt > 0)
1077 {
1078 --endidxcnt;
1079 arridx = endidx[endidxcnt];
1080 wlen = endlen[endidxcnt];
1081
1082#ifdef FEAT_MBYTE
1083 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1084 continue; /* not at first byte of character */
1085#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001086 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001087 {
1088 if (slang->sl_compflags == NULL)
1089 continue; /* next char is a word character */
1090 word_ends = FALSE;
1091 }
1092 else
1093 word_ends = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001094
1095#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001096 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001097 {
1098 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001099 * when folding case. This can be slow, take a shortcut when the
1100 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001101 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001102 if (STRNCMP(ptr, p, wlen) != 0)
1103 {
1104 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1105 mb_ptr_adv(p);
1106 wlen = p - mip->mi_word;
1107 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001108 }
1109#endif
1110
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001111 /* Check flags and region. For FIND_PREFIX check the condition and
1112 * prefix ID.
1113 * Repeat this if there are more flags/region alternatives until there
1114 * is a match. */
1115 res = SP_BAD;
1116 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1117 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001118 {
1119 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001120
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001121 /* For the fold-case tree check that the case of the checked word
1122 * matches with what the word in the tree requires.
1123 * For keep-case tree the case is always right. For prefixes we
1124 * don't bother to check. */
1125 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001126 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001127 if (mip->mi_cend != mip->mi_word + wlen)
1128 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001129 /* mi_capflags was set for a different word length, need
1130 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001131 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001132 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001133 }
1134
Bram Moolenaar0c405862005-06-22 22:26:26 +00001135 if (mip->mi_capflags == WF_KEEPCAP
1136 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001137 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001138 }
1139
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001140 /* When mode is FIND_PREFIX the word must support the prefix:
1141 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001142 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001143 else if (mode == FIND_PREFIX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001144 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001145 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001146 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001147 mip->mi_word + mip->mi_cprefixlen, slang,
1148 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001149 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001150 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001151
1152 /* Use the WF_RARE flag for a rare prefix. */
1153 if (c & WF_RAREPFX)
1154 flags |= WF_RARE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001155 }
1156
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001157 if (mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1158 || !word_ends)
1159 {
1160 /* Makes you wonder why someone puts a compound flag on a word
1161 * that's too short... Myspell compatibility requires this
1162 * anyway. */
1163 if (wlen < slang->sl_compminlen)
1164 continue;
1165
1166 /* The word doesn't end or it comes after another: it must
1167 * have a compound flag. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001168 if (!can_compound(slang, flags))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001169 continue;
1170 }
1171
1172 if (!word_ends)
1173 {
1174 /* Check that a valid word follows. If there is one, it will
1175 * set "mi_result", thus we are always finished here.
1176 * Recursive! */
1177
1178 /* Find following word in case-folded tree. */
1179 mip->mi_compoff = endlen[endidxcnt];
1180#ifdef FEAT_MBYTE
1181 if (has_mbyte && mode == FIND_KEEPWORD)
1182 {
1183 /* Compute byte length in case-folded word from "wlen":
1184 * byte length in keep-case word. Length may change when
1185 * folding case. This can be slow, take a shortcut when
1186 * the case-folded word is equal to the keep-case word. */
1187 p = mip->mi_fword;
1188 if (STRNCMP(ptr, p, wlen) != 0)
1189 {
1190 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1191 mb_ptr_adv(p);
1192 mip->mi_compoff = p - mip->mi_fword;
1193 }
1194 }
1195#endif
1196 find_word(mip, FIND_COMPOUND);
1197 if (mip->mi_result == SP_OK)
1198 break;
1199
1200 /* Find following word in keep-case tree. */
1201 mip->mi_compoff = wlen;
1202 find_word(mip, FIND_KEEPCOMPOUND);
1203 if (mip->mi_result == SP_OK)
1204 break;
1205 continue;
1206 }
1207
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001208 if (flags & WF_BANNED)
1209 res = SP_BANNED;
1210 else if (flags & WF_REGION)
1211 {
1212 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001213 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001214 res = SP_OK;
1215 else
1216 res = SP_LOCAL;
1217 }
1218 else if (flags & WF_RARE)
1219 res = SP_RARE;
1220 else
1221 res = SP_OK;
1222
1223 /* Always use the longest match and the best result. */
1224 if (mip->mi_result > res)
1225 {
1226 mip->mi_result = res;
1227 mip->mi_end = mip->mi_word + wlen;
1228 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001229 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001230 mip->mi_end = mip->mi_word + wlen;
1231
1232 if (res == SP_OK)
1233 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001234 }
1235
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001236 if (res == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001237 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001238 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001239}
1240
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001241/*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001242 * Return TRUE if "flags" has a valid compound flag.
1243 * TODO: check flags in a more advanced way.
1244 */
1245 static int
1246can_compound(slang, flags)
1247 slang_T *slang;
1248 int flags;
1249{
1250 return slang->sl_compflags != NULL
1251 && *slang->sl_compflags == ((unsigned)flags >> 24);
1252}
1253
1254/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001255 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1256 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001257 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001258 */
1259 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001260valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001261 int totprefcnt; /* nr of prefix IDs */
1262 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001263 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001264 char_u *word;
1265 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001266 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001267{
1268 int prefcnt;
1269 int pidx;
1270 regprog_T *rp;
1271 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001272 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001273
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001274 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001275 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1276 {
1277 pidx = slang->sl_pidxs[arridx + prefcnt];
1278
1279 /* Check the prefix ID. */
1280 if (prefid != (pidx & 0xff))
1281 continue;
1282
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001283 /* Check if the prefix doesn't combine and the word already has a
1284 * suffix. */
1285 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1286 continue;
1287
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001288 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001289 * stored in the two bytes above the prefix ID byte. */
1290 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001291 if (rp != NULL)
1292 {
1293 regmatch.regprog = rp;
1294 regmatch.rm_ic = FALSE;
1295 if (!vim_regexec(&regmatch, word, 0))
1296 continue;
1297 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001298 else if (cond_req)
1299 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001300
Bram Moolenaar53805d12005-08-01 07:08:33 +00001301 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001302 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001303 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001304 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001305}
1306
1307/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001308 * Check if the word at "mip->mi_word" has a matching prefix.
1309 * If it does, then check the following word.
1310 *
1311 * For a match mip->mi_result is updated.
1312 */
1313 static void
1314find_prefix(mip)
1315 matchinf_T *mip;
1316{
1317 idx_T arridx = 0;
1318 int len;
1319 int wlen = 0;
1320 int flen;
1321 int c;
1322 char_u *ptr;
1323 idx_T lo, hi, m;
1324 slang_T *slang = mip->mi_lp->lp_slang;
1325 char_u *byts;
1326 idx_T *idxs;
1327
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001328 byts = slang->sl_pbyts;
1329 if (byts == NULL)
1330 return; /* array is empty */
1331
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001332 /* We use the case-folded word here, since prefixes are always
1333 * case-folded. */
1334 ptr = mip->mi_fword;
1335 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001336 idxs = slang->sl_pidxs;
1337
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001338 /*
1339 * Repeat advancing in the tree until:
1340 * - there is a byte that doesn't match,
1341 * - we reach the end of the tree,
1342 * - or we reach the end of the line.
1343 */
1344 for (;;)
1345 {
1346 if (flen == 0 && *mip->mi_fend != NUL)
1347 flen = fold_more(mip);
1348
1349 len = byts[arridx++];
1350
1351 /* If the first possible byte is a zero the prefix could end here.
1352 * Check if the following word matches and supports the prefix. */
1353 if (byts[arridx] == 0)
1354 {
1355 /* There can be several prefixes with different conditions. We
1356 * try them all, since we don't know which one will give the
1357 * longest match. The word is the same each time, pass the list
1358 * of possible prefixes to find_word(). */
1359 mip->mi_prefarridx = arridx;
1360 mip->mi_prefcnt = len;
1361 while (len > 0 && byts[arridx] == 0)
1362 {
1363 ++arridx;
1364 --len;
1365 }
1366 mip->mi_prefcnt -= len;
1367
1368 /* Find the word that comes after the prefix. */
1369 mip->mi_prefixlen = wlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001370#ifdef FEAT_MBYTE
1371 if (has_mbyte)
1372 {
1373 /* Case-folded length may differ from original length. */
1374 mip->mi_cprefixlen = nofold_len(mip->mi_fword, wlen,
1375 mip->mi_word);
1376 }
1377 else
1378 mip->mi_cprefixlen = wlen;
1379#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001380 find_word(mip, FIND_PREFIX);
1381
1382
1383 if (len == 0)
1384 break; /* no children, word must end here */
1385 }
1386
1387 /* Stop looking at end of the line. */
1388 if (ptr[wlen] == NUL)
1389 break;
1390
1391 /* Perform a binary search in the list of accepted bytes. */
1392 c = ptr[wlen];
1393 lo = arridx;
1394 hi = arridx + len - 1;
1395 while (lo < hi)
1396 {
1397 m = (lo + hi) / 2;
1398 if (byts[m] > c)
1399 hi = m - 1;
1400 else if (byts[m] < c)
1401 lo = m + 1;
1402 else
1403 {
1404 lo = hi = m;
1405 break;
1406 }
1407 }
1408
1409 /* Stop if there is no matching byte. */
1410 if (hi < lo || byts[lo] != c)
1411 break;
1412
1413 /* Continue at the child (if there is one). */
1414 arridx = idxs[lo];
1415 ++wlen;
1416 --flen;
1417 }
1418}
1419
1420/*
1421 * Need to fold at least one more character. Do until next non-word character
1422 * for efficiency.
1423 * Return the length of the folded chars in bytes.
1424 */
1425 static int
1426fold_more(mip)
1427 matchinf_T *mip;
1428{
1429 int flen;
1430 char_u *p;
1431
1432 p = mip->mi_fend;
1433 do
1434 {
1435 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001436 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001437
1438 /* Include the non-word character so that we can check for the
1439 * word end. */
1440 if (*mip->mi_fend != NUL)
1441 mb_ptr_adv(mip->mi_fend);
1442
1443 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1444 mip->mi_fword + mip->mi_fwordlen,
1445 MAXWLEN - mip->mi_fwordlen);
1446 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1447 mip->mi_fwordlen += flen;
1448 return flen;
1449}
1450
1451/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001452 * Check case flags for a word. Return TRUE if the word has the requested
1453 * case.
1454 */
1455 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001456spell_valid_case(wordflags, treeflags)
1457 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001458 int treeflags; /* flags for the word in the spell tree */
1459{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001460 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001461 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001462 && ((treeflags & WF_ONECAP) == 0
1463 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001464}
1465
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001466/*
1467 * Return TRUE if spell checking is not enabled.
1468 */
1469 static int
1470no_spell_checking()
1471{
1472 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
1473 {
1474 EMSG(_("E756: Spell checking is not enabled"));
1475 return TRUE;
1476 }
1477 return FALSE;
1478}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001479
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001480/*
1481 * Move to next spell error.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001482 * "curline" is TRUE for "z?": find word under/after cursor in the same line.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001483 * Return OK if found, FAIL otherwise.
1484 */
1485 int
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001486spell_move_to(dir, allwords, curline)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001487 int dir; /* FORWARD or BACKWARD */
1488 int allwords; /* TRUE for "[s" and "]s" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001489 int curline;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001490{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001491 linenr_T lnum;
1492 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001493 char_u *line;
1494 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001495 char_u *endp;
1496 int attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001497 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001498 int has_syntax = syntax_present(curbuf);
1499 int col;
1500 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001501 char_u *buf = NULL;
1502 int buflen = 0;
1503 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001504 int capcol = -1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001505
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001506 if (no_spell_checking())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001507 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001508
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001509 /*
1510 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001511 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001512 *
1513 * When searching backwards, we continue in the line to find the last
1514 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001515 *
1516 * We concatenate the start of the next line, so that wrapped words work
1517 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1518 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001519 */
1520 lnum = curwin->w_cursor.lnum;
1521 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001522
1523 while (!got_int)
1524 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001525 line = ml_get(lnum);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001526
Bram Moolenaar0c405862005-06-22 22:26:26 +00001527 len = STRLEN(line);
1528 if (buflen < len + MAXWLEN + 2)
1529 {
1530 vim_free(buf);
1531 buflen = len + MAXWLEN + 2;
1532 buf = alloc(buflen);
1533 if (buf == NULL)
1534 break;
1535 }
1536
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001537 /* In first line check first word for Capital. */
1538 if (lnum == 1)
1539 capcol = 0;
1540
1541 /* For checking first word with a capital skip white space. */
1542 if (capcol == 0)
1543 capcol = skipwhite(line) - line;
1544
Bram Moolenaar0c405862005-06-22 22:26:26 +00001545 /* Copy the line into "buf" and append the start of the next line if
1546 * possible. */
1547 STRCPY(buf, line);
1548 if (lnum < curbuf->b_ml.ml_line_count)
1549 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1550
1551 p = buf + skip;
1552 endp = buf + len;
1553 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001554 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001555 /* When searching backward don't search after the cursor. */
1556 if (dir == BACKWARD
1557 && lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001558 && (colnr_T)(p - buf) >= curwin->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001559 break;
1560
1561 /* start of word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00001562 attr = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001563 len = spell_check(curwin, p, &attr, &capcol);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001564
1565 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001566 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001567 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001568 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001569 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001570 /* When searching forward only accept a bad word after
1571 * the cursor. */
1572 if (dir == BACKWARD
1573 || lnum > curwin->w_cursor.lnum
1574 || (lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001575 && (colnr_T)(curline ? p - buf + len
1576 : p - buf)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001577 > curwin->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001578 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001579 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001580 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001581 col = p - buf;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001582 (void)syn_get_id(lnum, (colnr_T)col,
1583 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001584 }
1585 else
1586 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001587
Bram Moolenaar51485f02005-06-04 21:55:20 +00001588 if (can_spell)
1589 {
1590 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001591 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001592#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001593 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001594#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001595 if (dir == FORWARD)
1596 {
1597 /* No need to search further. */
1598 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001599 vim_free(buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001600 return OK;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001601 }
1602 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001603 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001604 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001605 }
1606
Bram Moolenaar51485f02005-06-04 21:55:20 +00001607 /* advance to character after the word */
1608 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001609 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001610 }
1611
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001612 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001613 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001614
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001615 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001616 if (dir == BACKWARD)
1617 {
1618 if (found_pos.lnum != 0)
1619 {
1620 /* Use the last match in the line. */
1621 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001622 vim_free(buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001623 return OK;
1624 }
1625 if (lnum == 1)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001626 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001627 --lnum;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001628 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001629 }
1630 else
1631 {
1632 if (lnum == curbuf->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001633 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001634 ++lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001635
1636 /* Skip the characters at the start of the next line that were
1637 * included in a match crossing line boundaries. */
1638 if (attr == 0)
1639 skip = p - endp;
1640 else
1641 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001642
1643 /* Capscol skips over the inserted space. */
1644 --capcol;
1645
1646 /* But after empty line check first word in next line */
1647 if (*skipwhite(line) == NUL)
1648 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001649 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001650
1651 line_breakcheck();
1652 }
1653
Bram Moolenaar0c405862005-06-22 22:26:26 +00001654 vim_free(buf);
1655 return FAIL;
1656}
1657
1658/*
1659 * For spell checking: concatenate the start of the following line "line" into
1660 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
1661 */
1662 void
1663spell_cat_line(buf, line, maxlen)
1664 char_u *buf;
1665 char_u *line;
1666 int maxlen;
1667{
1668 char_u *p;
1669 int n;
1670
1671 p = skipwhite(line);
1672 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1673 p = skipwhite(p + 1);
1674
1675 if (*p != NUL)
1676 {
1677 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001678 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001679 n = p - line;
1680 if (n >= maxlen)
1681 n = maxlen - 1;
1682 vim_memset(buf + 1, ' ', n);
1683 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001684}
1685
1686/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001687 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001688 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001689 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001690 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001691spell_load_lang(lang)
1692 char_u *lang;
1693{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001694 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001695 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001696 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001697
Bram Moolenaarb765d632005-06-07 21:00:02 +00001698 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001699 * It's truncated when an error is detected. */
1700 STRCPY(langcp, lang);
1701
Bram Moolenaarb765d632005-06-07 21:00:02 +00001702 /*
1703 * Find the first spell file for "lang" in 'runtimepath' and load it.
1704 */
1705 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
1706 "spell/%s.%s.spl", lang, spell_enc());
1707 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001708
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001709 if (r == FAIL && *langcp != NUL)
1710 {
1711 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001712 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001713 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001714 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001715 }
1716
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001717 if (r == FAIL)
1718 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
1719 fname_enc + 6);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001720 else if (*langcp != NUL)
1721 {
1722 /* Load all the additions. */
1723 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
1724 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
1725 }
1726}
1727
1728/*
1729 * Return the encoding used for spell checking: Use 'encoding', except that we
1730 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1731 */
1732 static char_u *
1733spell_enc()
1734{
1735
1736#ifdef FEAT_MBYTE
1737 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1738 return p_enc;
1739#endif
1740 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001741}
1742
1743/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001744 * Get the name of the .spl file for the internal wordlist into
1745 * "fname[MAXPATHL]".
1746 */
1747 static void
1748int_wordlist_spl(fname)
1749 char_u *fname;
1750{
1751 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
1752 int_wordlist, spell_enc());
1753}
1754
1755/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001756 * Allocate a new slang_T.
1757 * Caller must fill "sl_next".
1758 */
1759 static slang_T *
1760slang_alloc(lang)
1761 char_u *lang;
1762{
1763 slang_T *lp;
1764
Bram Moolenaar51485f02005-06-04 21:55:20 +00001765 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001766 if (lp != NULL)
1767 {
1768 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001769 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001770 }
1771 return lp;
1772}
1773
1774/*
1775 * Free the contents of an slang_T and the structure itself.
1776 */
1777 static void
1778slang_free(lp)
1779 slang_T *lp;
1780{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001781 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001782 vim_free(lp->sl_fname);
1783 slang_clear(lp);
1784 vim_free(lp);
1785}
1786
1787/*
1788 * Clear an slang_T so that the file can be reloaded.
1789 */
1790 static void
1791slang_clear(lp)
1792 slang_T *lp;
1793{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001794 garray_T *gap;
1795 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001796 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001797 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001798
Bram Moolenaar51485f02005-06-04 21:55:20 +00001799 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001800 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001801 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001802 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001803 vim_free(lp->sl_pbyts);
1804 lp->sl_pbyts = NULL;
1805
Bram Moolenaar51485f02005-06-04 21:55:20 +00001806 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001807 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001808 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001809 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001810 vim_free(lp->sl_pidxs);
1811 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001812
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001813 gap = &lp->sl_rep;
1814 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001815 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001816 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1817 vim_free(ftp->ft_from);
1818 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001819 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001820 ga_clear(gap);
1821
1822 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001823 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001824 {
1825 /* "ga_len" is set to 1 without adding an item for latin1 */
1826 if (gap->ga_data != NULL)
1827 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
1828 for (i = 0; i < gap->ga_len; ++i)
1829 vim_free(((int **)gap->ga_data)[i]);
1830 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001831 else
1832 /* SAL items: free salitem_T items */
1833 while (gap->ga_len > 0)
1834 {
1835 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1836 vim_free(smp->sm_lead);
1837 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
1838 vim_free(smp->sm_to);
1839#ifdef FEAT_MBYTE
1840 vim_free(smp->sm_lead_w);
1841 vim_free(smp->sm_oneof_w);
1842 vim_free(smp->sm_to_w);
1843#endif
1844 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001845 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001846
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001847 for (i = 0; i < lp->sl_prefixcnt; ++i)
1848 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001849 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001850 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001851 lp->sl_prefprog = NULL;
1852
1853 vim_free(lp->sl_midword);
1854 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001855
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001856 vim_free(lp->sl_compflags);
1857 lp->sl_compflags = NULL;
1858
Bram Moolenaarea424162005-06-16 21:51:00 +00001859#ifdef FEAT_MBYTE
1860 {
1861 int todo = lp->sl_map_hash.ht_used;
1862 hashitem_T *hi;
1863
1864 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
1865 if (!HASHITEM_EMPTY(hi))
1866 {
1867 --todo;
1868 vim_free(hi->hi_key);
1869 }
1870 }
1871 hash_clear(&lp->sl_map_hash);
1872#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001873}
1874
1875/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001876 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001877 * Invoked through do_in_runtimepath().
1878 */
1879 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00001880spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001881 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001882 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001883{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001884 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001885}
1886
1887/*
1888 * Load one spell file and store the info into a slang_T.
1889 *
1890 * This is invoked in two ways:
1891 * - From spell_load_cb() to load a spell file for the first time. "lang" is
1892 * the language name, "old_lp" is NULL. Will allocate an slang_T.
1893 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
1894 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001895 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00001896 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001897 static slang_T *
1898spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001899 char_u *fname;
1900 char_u *lang;
1901 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001902 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001903{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001904 FILE *fd;
1905 char_u buf[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001906 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001907 char_u *bp;
1908 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001909 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001910 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001911 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001912 int round;
1913 char_u *save_sourcing_name = sourcing_name;
1914 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001915 int cnt, ccnt;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001916 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001917 slang_T *lp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001918 garray_T *gap;
1919 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001920 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001921 short *first;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001922 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001923 int c = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001924
Bram Moolenaarb765d632005-06-07 21:00:02 +00001925 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001926 if (fd == NULL)
1927 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001928 if (!silent)
1929 EMSG2(_(e_notopen), fname);
1930 else if (p_verbose > 2)
1931 {
1932 verbose_enter();
1933 smsg((char_u *)e_notopen, fname);
1934 verbose_leave();
1935 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001936 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001937 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00001938 if (p_verbose > 2)
1939 {
1940 verbose_enter();
1941 smsg((char_u *)_("Reading spell file \"%s\""), fname);
1942 verbose_leave();
1943 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001944
Bram Moolenaarb765d632005-06-07 21:00:02 +00001945 if (old_lp == NULL)
1946 {
1947 lp = slang_alloc(lang);
1948 if (lp == NULL)
1949 goto endFAIL;
1950
1951 /* Remember the file name, used to reload the file when it's updated. */
1952 lp->sl_fname = vim_strsave(fname);
1953 if (lp->sl_fname == NULL)
1954 goto endFAIL;
1955
1956 /* Check for .add.spl. */
1957 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
1958 }
1959 else
1960 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001961
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001962 /* Set sourcing_name, so that error messages mention the file name. */
1963 sourcing_name = fname;
1964 sourcing_lnum = 0;
1965
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001966 /* <HEADER>: <fileID>
1967 * <regioncnt> <regionname> ...
1968 * <charflagslen> <charflags>
1969 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001970 * <midwordlen> <midword>
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001971 * <compoundlen> <compoundtype> <compoundinfo>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001972 * <prefcondcnt> <prefcond> ...
1973 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001974 for (i = 0; i < VIMSPELLMAGICL; ++i)
1975 buf[i] = getc(fd); /* <fileID> */
1976 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
1977 {
1978 EMSG(_("E757: Wrong file ID in spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001979 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001980 }
1981
1982 cnt = getc(fd); /* <regioncnt> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001983 if (cnt < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001984 {
1985truncerr:
Bram Moolenaar7887d882005-07-01 22:33:52 +00001986 EMSG(_(e_spell_trunc));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001987 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001988 }
1989 if (cnt > 8)
1990 {
1991formerr:
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001992 EMSG(_(e_format));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001993 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001994 }
1995 for (i = 0; i < cnt; ++i)
1996 {
1997 lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
1998 lp->sl_regions[i * 2 + 1] = getc(fd);
1999 }
2000 lp->sl_regions[cnt * 2] = NUL;
2001
Bram Moolenaar7887d882005-07-01 22:33:52 +00002002 /* <charflagslen> <charflags> */
2003 p = read_cnt_string(fd, 1, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002004 if (cnt < 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002005 goto endFAIL;
2006
2007 /* <fcharslen> <fchars> */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002008 fol = read_cnt_string(fd, 2, &ccnt);
2009 if (ccnt < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002010 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002011 vim_free(p);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002012 goto endFAIL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002013 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00002014
2015 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
2016 if (p != NULL && fol != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002017 i = set_spell_charflags(p, cnt, fol);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002018
2019 vim_free(p);
2020 vim_free(fol);
2021
2022 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
2023 if ((p == NULL) != (fol == NULL))
2024 goto formerr;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002025
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002026 /* <midwordlen> <midword> */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002027 lp->sl_midword = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002028 if (cnt < 0)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002029 goto endFAIL;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002030
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002031 /* <compoundlen> <compoundtype> <compoundinfo> */
2032 cnt = (getc(fd) << 8) + getc(fd); /* <compoundlen> */
2033 if (cnt < 0)
2034 goto endFAIL;
2035 if (cnt > 0)
2036 {
2037 --cnt;
2038 c = getc(fd); /* <compoundtype> */
2039 if (c != 1)
2040 {
2041 /* Unknown kind of compound words, skip the info. */
2042 while (cnt-- > 0)
2043 getc(fd);
2044 }
2045 else if (cnt < 2)
2046 goto formerr;
2047 else
2048 {
2049 --cnt;
2050 c = getc(fd); /* <comp1minlen> */
2051 if (c < 1 || c > 50)
2052 c = 3;
2053 lp->sl_compminlen = c;
2054
2055 p = alloc(cnt + 1);
2056 if (p == NULL)
2057 goto endFAIL;
2058 lp->sl_compflags = p;
2059 while (cnt-- > 0)
2060 *p++ = getc(fd); /* <comp1flags> */
2061 *p = NUL;
2062 }
2063 }
2064
2065
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002066 /* <prefcondcnt> <prefcond> ... */
2067 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
2068 if (cnt > 0)
2069 {
2070 lp->sl_prefprog = (regprog_T **)alloc_clear(
2071 (unsigned)sizeof(regprog_T *) * cnt);
2072 if (lp->sl_prefprog == NULL)
2073 goto endFAIL;
2074 lp->sl_prefixcnt = cnt;
2075
2076 for (i = 0; i < cnt; ++i)
2077 {
2078 /* <prefcond> : <condlen> <condstr> */
2079 n = getc(fd); /* <condlen> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002080 if (n < 0 || n >= MAXWLEN)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002081 goto formerr;
2082 /* When <condlen> is zero we have an empty condition. Otherwise
2083 * compile the regexp program used to check for the condition. */
2084 if (n > 0)
2085 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002086 buf[0] = '^'; /* always match at one position only */
2087 p = buf + 1;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002088 while (n-- > 0)
2089 *p++ = getc(fd); /* <condstr> */
2090 *p = NUL;
2091 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
2092 }
2093 }
2094 }
2095
2096
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002097 /* <SUGGEST> : <repcount> <rep> ...
2098 * <salflags> <salcount> <sal> ...
2099 * <maplen> <mapstr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002100
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002101 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
2102 if (cnt < 0)
2103 goto formerr;
2104
2105 gap = &lp->sl_rep;
2106 if (ga_grow(gap, cnt) == FAIL)
2107 goto endFAIL;
2108
2109 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
2110 for (; gap->ga_len < cnt; ++gap->ga_len)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002111 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002112 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
Bram Moolenaar7887d882005-07-01 22:33:52 +00002113 ftp->ft_from = read_cnt_string(fd, 1, &i);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002114 if (i <= 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002115 goto endFAIL;
2116 ftp->ft_to = read_cnt_string(fd, 1, &i);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002117 if (i <= 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002118 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00002119 vim_free(ftp->ft_from);
2120 goto endFAIL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002121 }
2122 }
2123
2124 /* Fill the first-index table. */
2125 first = lp->sl_rep_first;
2126 for (i = 0; i < 256; ++i)
2127 first[i] = -1;
2128 for (i = 0; i < gap->ga_len; ++i)
2129 {
2130 ftp = &((fromto_T *)gap->ga_data)[i];
2131 if (first[*ftp->ft_from] == -1)
2132 first[*ftp->ft_from] = i;
2133 }
2134
2135 i = getc(fd); /* <salflags> */
2136 if (i & SAL_F0LLOWUP)
2137 lp->sl_followup = TRUE;
2138 if (i & SAL_COLLAPSE)
2139 lp->sl_collapse = TRUE;
2140 if (i & SAL_REM_ACCENTS)
2141 lp->sl_rem_accents = TRUE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002142 if (i & SAL_SOFO)
2143 lp->sl_sofo = TRUE;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002144 else
2145 lp->sl_sofo = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002146
2147 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
2148 if (cnt < 0)
2149 goto formerr;
2150
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002151 if (lp->sl_sofo)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002152 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002153 /*
2154 * SOFOFROM and SOFOTO items come in one <salfrom> and <salto>
2155 */
2156 if (cnt != 1)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002157 goto formerr;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002158
Bram Moolenaar7887d882005-07-01 22:33:52 +00002159 /* <salfromlen> <salfrom> */
2160 bp = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002161 if (cnt < 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002162 goto endFAIL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002163
Bram Moolenaar7887d882005-07-01 22:33:52 +00002164 /* <saltolen> <salto> */
2165 fol = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002166 if (cnt < 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002167 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002168 vim_free(bp);
2169 goto endFAIL;
2170 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002171
Bram Moolenaar7887d882005-07-01 22:33:52 +00002172 /* Store the info in lp->sl_sal and/or lp->sl_sal_first. */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002173 if (bp != NULL && fol != NULL)
2174 i = set_sofo(lp, bp, fol);
2175 else if (bp != NULL || fol != NULL)
2176 i = FAIL; /* only one of two strings is an error */
2177 else
2178 i = OK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002179
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002180 vim_free(bp);
2181 vim_free(fol);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002182 if (i == FAIL)
2183 goto formerr;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002184 }
2185 else
2186 {
2187 /*
2188 * SAL items
2189 */
2190 gap = &lp->sl_sal;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002191 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002192 if (ga_grow(gap, cnt) == FAIL)
2193 goto endFAIL;
2194
2195 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
2196 for (; gap->ga_len < cnt; ++gap->ga_len)
2197 {
2198 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
2199 ccnt = getc(fd); /* <salfromlen> */
2200 if (ccnt < 0)
2201 goto formerr;
2202 if ((p = alloc(ccnt + 2)) == NULL)
2203 goto endFAIL;
2204 smp->sm_lead = p;
2205
2206 /* Read up to the first special char into sm_lead. */
2207 for (i = 0; i < ccnt; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002208 {
2209 c = getc(fd); /* <salfrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002210 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002211 break;
2212 *p++ = c;
2213 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002214 smp->sm_leadlen = p - smp->sm_lead;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002215 *p++ = NUL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002216
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002217 /* Put (abc) chars in sm_oneof, if any. */
2218 if (c == '(')
2219 {
2220 smp->sm_oneof = p;
2221 for (++i; i < ccnt; ++i)
2222 {
2223 c = getc(fd); /* <salfrom> */
2224 if (c == ')')
2225 break;
2226 *p++ = c;
2227 }
2228 *p++ = NUL;
2229 if (++i < ccnt)
2230 c = getc(fd);
2231 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002232 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002233 smp->sm_oneof = NULL;
2234
2235 /* Any following chars go in sm_rules. */
2236 smp->sm_rules = p;
2237 if (i < ccnt)
2238 /* store the char we got while checking for end of sm_lead */
2239 *p++ = c;
2240 for (++i; i < ccnt; ++i)
2241 *p++ = getc(fd); /* <salfrom> */
2242 *p++ = NUL;
2243
Bram Moolenaar7887d882005-07-01 22:33:52 +00002244 /* <saltolen> <salto> */
2245 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002246 if (ccnt < 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002247 {
2248 vim_free(smp->sm_lead);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002249 goto formerr;
2250 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002251
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002252#ifdef FEAT_MBYTE
2253 if (has_mbyte)
2254 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002255 /* convert the multi-byte strings to wide char strings */
2256 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2257 smp->sm_leadlen = mb_charlen(smp->sm_lead);
2258 if (smp->sm_oneof == NULL)
2259 smp->sm_oneof_w = NULL;
2260 else
2261 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002262 if (smp->sm_to == NULL)
2263 smp->sm_to_w = NULL;
2264 else
2265 smp->sm_to_w = mb_str2wide(smp->sm_to);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002266 if (smp->sm_lead_w == NULL
2267 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002268 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002269 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002270 vim_free(smp->sm_lead);
2271 vim_free(smp->sm_to);
2272 vim_free(smp->sm_lead_w);
2273 vim_free(smp->sm_oneof_w);
2274 vim_free(smp->sm_to_w);
2275 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002276 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002277 }
2278#endif
2279 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002280
2281 /* Fill the first-index table. */
Bram Moolenaar7887d882005-07-01 22:33:52 +00002282 set_sal_first(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002283 }
2284
Bram Moolenaar7887d882005-07-01 22:33:52 +00002285 /* <maplen> <mapstr> */
2286 p = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002287 if (cnt < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002288 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002289 if (p != NULL)
2290 {
2291 set_map_str(lp, p);
2292 vim_free(p);
2293 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002294
Bram Moolenaar51485f02005-06-04 21:55:20 +00002295 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002296 * round 2: <KWORDTREE>
2297 * round 3: <PREFIXTREE> */
2298 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002299 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002300 /* The tree size was computed when writing the file, so that we can
2301 * allocate it as one long block. <nodecount> */
2302 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2303 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002304 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002305 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002306 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002307 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002308 bp = lalloc((long_u)len, TRUE);
2309 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002310 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002311 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002312 lp->sl_fbyts = bp;
2313 else if (round == 2)
2314 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002315 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002316 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002317
2318 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002319 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
2320 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002321 goto endFAIL;
2322 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002323 lp->sl_fidxs = ip;
2324 else if (round == 2)
2325 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002326 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002327 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002328
2329 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002330 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002331 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002332 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002333 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002334 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002335 }
2336 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002337
Bram Moolenaarb765d632005-06-07 21:00:02 +00002338 /* For a new file link it in the list of spell files. */
2339 if (old_lp == NULL)
2340 {
2341 lp->sl_next = first_lang;
2342 first_lang = lp;
2343 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002344
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002345 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002346
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002347endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002348 if (lang != NULL)
2349 /* truncating the name signals the error to spell_load_lang() */
2350 *lang = NUL;
2351 if (lp != NULL && old_lp == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002352 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002353 slang_free(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002354 lp = NULL;
2355 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002356
2357endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002358 if (fd != NULL)
2359 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002360 sourcing_name = save_sourcing_name;
2361 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002362
2363 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002364}
2365
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002366/*
2367 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002368 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002369 * Returns NULL when the count is zero.
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002370 * Sets "*cntp" to -1 when there is an error, length of the result otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002371 */
2372 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002373read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002374 FILE *fd;
2375 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002376 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002377{
2378 int cnt = 0;
2379 int i;
2380 char_u *str;
2381
2382 /* read the length bytes, MSB first */
2383 for (i = 0; i < cnt_bytes; ++i)
2384 cnt = (cnt << 8) + getc(fd);
2385 if (cnt < 0)
2386 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00002387 EMSG(_(e_spell_trunc));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002388 *cntp = -1;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002389 return NULL;
2390 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002391 *cntp = cnt;
2392 if (cnt == 0)
2393 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002394
2395 /* allocate memory */
2396 str = alloc((unsigned)cnt + 1);
2397 if (str == NULL)
2398 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002399 *cntp = -1;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002400 return NULL;
2401 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002402
2403 /* Read the string. Doesn't check for truncated file. */
2404 for (i = 0; i < cnt; ++i)
2405 str[i] = getc(fd);
2406 str[i] = NUL;
2407
2408 return str;
2409}
2410
Bram Moolenaar7887d882005-07-01 22:33:52 +00002411/*
2412 * Set the SOFOFROM and SOFOTO items in language "lp".
2413 * Returns FAIL when there is something wrong.
2414 */
2415 static int
2416set_sofo(lp, from, to)
2417 slang_T *lp;
2418 char_u *from;
2419 char_u *to;
2420{
2421 int i;
2422
2423#ifdef FEAT_MBYTE
2424 garray_T *gap;
2425 char_u *s;
2426 char_u *p;
2427 int c;
2428 int *inp;
2429
2430 if (has_mbyte)
2431 {
2432 /* Use "sl_sal" as an array with 256 pointers to a list of wide
2433 * characters. The index is the low byte of the character.
2434 * The list contains from-to pairs with a terminating NUL.
2435 * sl_sal_first[] is used for latin1 "from" characters. */
2436 gap = &lp->sl_sal;
2437 ga_init2(gap, sizeof(int *), 1);
2438 if (ga_grow(gap, 256) == FAIL)
2439 return FAIL;
2440 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
2441 gap->ga_len = 256;
2442
2443 /* First count the number of items for each list. Temporarily use
2444 * sl_sal_first[] for this. */
2445 for (p = from, s = to; *p != NUL && *s != NUL; )
2446 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002447 c = mb_cptr2char_adv(&p);
2448 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002449 if (c >= 256)
2450 ++lp->sl_sal_first[c & 0xff];
2451 }
2452 if (*p != NUL || *s != NUL) /* lengths differ */
2453 return FAIL;
2454
2455 /* Allocate the lists. */
2456 for (i = 0; i < 256; ++i)
2457 if (lp->sl_sal_first[i] > 0)
2458 {
2459 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
2460 if (p == NULL)
2461 return FAIL;
2462 ((int **)gap->ga_data)[i] = (int *)p;
2463 *(int *)p = 0;
2464 }
2465
2466 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
2467 * list. */
2468 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
2469 for (p = from, s = to; *p != NUL && *s != NUL; )
2470 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002471 c = mb_cptr2char_adv(&p);
2472 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002473 if (c >= 256)
2474 {
2475 /* Append the from-to chars at the end of the list with
2476 * the low byte. */
2477 inp = ((int **)gap->ga_data)[c & 0xff];
2478 while (*inp != 0)
2479 ++inp;
2480 *inp++ = c; /* from char */
2481 *inp++ = i; /* to char */
2482 *inp++ = NUL; /* NUL at the end */
2483 }
2484 else
2485 /* mapping byte to char is done in sl_sal_first[] */
2486 lp->sl_sal_first[c] = i;
2487 }
2488 }
2489 else
2490#endif
2491 {
2492 /* mapping bytes to bytes is done in sl_sal_first[] */
2493 if (STRLEN(from) != STRLEN(to))
2494 return FAIL;
2495
2496 for (i = 0; to[i] != NUL; ++i)
2497 lp->sl_sal_first[from[i]] = to[i];
2498 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
2499 }
2500
2501 return OK;
2502}
2503
2504/*
2505 * Fill the first-index table for "lp".
2506 */
2507 static void
2508set_sal_first(lp)
2509 slang_T *lp;
2510{
2511 salfirst_T *sfirst;
2512 int i;
2513 salitem_T *smp;
2514 int c;
2515 garray_T *gap = &lp->sl_sal;
2516
2517 sfirst = lp->sl_sal_first;
2518 for (i = 0; i < 256; ++i)
2519 sfirst[i] = -1;
2520 smp = (salitem_T *)gap->ga_data;
2521 for (i = 0; i < gap->ga_len; ++i)
2522 {
2523#ifdef FEAT_MBYTE
2524 if (has_mbyte)
2525 /* Use the lowest byte of the first character. For latin1 it's
2526 * the character, for other encodings it should differ for most
2527 * characters. */
2528 c = *smp[i].sm_lead_w & 0xff;
2529 else
2530#endif
2531 c = *smp[i].sm_lead;
2532 if (sfirst[c] == -1)
2533 {
2534 sfirst[c] = i;
2535#ifdef FEAT_MBYTE
2536 if (has_mbyte)
2537 {
2538 int n;
2539
2540 /* Make sure all entries with this byte are following each
2541 * other. Move the ones that are in the wrong position. Do
2542 * keep the same ordering! */
2543 while (i + 1 < gap->ga_len
2544 && (*smp[i + 1].sm_lead_w & 0xff) == c)
2545 /* Skip over entry with same index byte. */
2546 ++i;
2547
2548 for (n = 1; i + n < gap->ga_len; ++n)
2549 if ((*smp[i + n].sm_lead_w & 0xff) == c)
2550 {
2551 salitem_T tsal;
2552
2553 /* Move entry with same index byte after the entries
2554 * we already found. */
2555 ++i;
2556 --n;
2557 tsal = smp[i + n];
2558 mch_memmove(smp + i + 1, smp + i,
2559 sizeof(salitem_T) * n);
2560 smp[i] = tsal;
2561 }
2562 }
2563#endif
2564 }
2565 }
2566}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002567
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002568#ifdef FEAT_MBYTE
2569/*
2570 * Turn a multi-byte string into a wide character string.
2571 * Return it in allocated memory (NULL for out-of-memory)
2572 */
2573 static int *
2574mb_str2wide(s)
2575 char_u *s;
2576{
2577 int *res;
2578 char_u *p;
2579 int i = 0;
2580
2581 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
2582 if (res != NULL)
2583 {
2584 for (p = s; *p != NUL; )
2585 res[i++] = mb_ptr2char_adv(&p);
2586 res[i] = NUL;
2587 }
2588 return res;
2589}
2590#endif
2591
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002592/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002593 * Read one row of siblings from the spell file and store it in the byte array
2594 * "byts" and index array "idxs". Recursively read the children.
2595 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00002596 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00002597 *
2598 * Returns the index follosing the siblings.
2599 * Returns -1 if the file is shorter than expected.
2600 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002601 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002602 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002603read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002604 FILE *fd;
2605 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002606 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002607 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002608 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002609 int prefixtree; /* TRUE for reading PREFIXTREE */
2610 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002611{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002612 int len;
2613 int i;
2614 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002615 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002616 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002617 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002618#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002619
Bram Moolenaar51485f02005-06-04 21:55:20 +00002620 len = getc(fd); /* <siblingcount> */
2621 if (len <= 0)
2622 return -1;
2623
2624 if (startidx + len >= maxidx)
2625 return -2;
2626 byts[idx++] = len;
2627
2628 /* Read the byte values, flag/region bytes and shared indexes. */
2629 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002630 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002631 c = getc(fd); /* <byte> */
2632 if (c < 0)
2633 return -1;
2634 if (c <= BY_SPECIAL)
2635 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002636 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002637 {
2638 /* No flags, all regions. */
2639 idxs[idx] = 0;
2640 c = 0;
2641 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002642 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002643 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002644 if (prefixtree)
2645 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002646 /* Read the optional pflags byte, the prefix ID and the
2647 * condition nr. In idxs[] store the prefix ID in the low
2648 * byte, the condition index shifted up 8 bits, the flags
2649 * shifted up 24 bits. */
2650 if (c == BY_FLAGS)
2651 c = getc(fd) << 24; /* <pflags> */
2652 else
2653 c = 0;
2654
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002655 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00002656
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002657 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
2658 if (n >= maxprefcondnr)
2659 return -2;
Bram Moolenaar53805d12005-08-01 07:08:33 +00002660 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002661 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002662 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002663 {
2664 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002665 * idxs[] the flags go in the low two bytes, region above
2666 * that and prefix ID above the region. */
2667 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002668 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002669 if (c2 == BY_FLAGS2)
2670 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002671 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002672 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002673 if (c & WF_AFX)
2674 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002675 }
2676
Bram Moolenaar51485f02005-06-04 21:55:20 +00002677 idxs[idx] = c;
2678 c = 0;
2679 }
2680 else /* c == BY_INDEX */
2681 {
2682 /* <nodeidx> */
2683 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2684 if (n < 0 || n >= maxidx)
2685 return -2;
2686 idxs[idx] = n + SHARED_MASK;
2687 c = getc(fd); /* <xbyte> */
2688 }
2689 }
2690 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002691 }
2692
Bram Moolenaar51485f02005-06-04 21:55:20 +00002693 /* Recursively read the children for non-shared siblings.
2694 * Skip the end-of-word ones (zero byte value) and the shared ones (and
2695 * remove SHARED_MASK) */
2696 for (i = 1; i <= len; ++i)
2697 if (byts[startidx + i] != 0)
2698 {
2699 if (idxs[startidx + i] & SHARED_MASK)
2700 idxs[startidx + i] &= ~SHARED_MASK;
2701 else
2702 {
2703 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002704 idx = read_tree(fd, byts, idxs, maxidx, idx,
2705 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002706 if (idx < 0)
2707 break;
2708 }
2709 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002710
Bram Moolenaar51485f02005-06-04 21:55:20 +00002711 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002712}
2713
2714/*
2715 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002716 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002717 */
2718 char_u *
2719did_set_spelllang(buf)
2720 buf_T *buf;
2721{
2722 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002723 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002724 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00002725 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002726 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002727 int region_mask;
2728 slang_T *lp;
2729 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002730 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002731 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002732 int len;
2733 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002734 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002735 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002736 char_u *use_region = NULL;
2737 int dont_use_region = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002738
2739 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002740 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002741
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002742 /* loop over comma separated language names. */
2743 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002744 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002745 /* Get one language name. */
2746 copy_option_part(&splp, lang, MAXWLEN, ",");
2747
Bram Moolenaar5482f332005-04-17 20:18:43 +00002748 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002749 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002750
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002751 /* If the name ends in ".spl" use it as the name of the spell file.
2752 * If there is a region name let "region" point to it and remove it
2753 * from the name. */
2754 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2755 {
2756 filename = TRUE;
2757
Bram Moolenaarb6356332005-07-18 21:40:44 +00002758 /* Locate a region and remove it from the file name. */
2759 p = vim_strchr(gettail(lang), '_');
2760 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
2761 && !ASCII_ISALPHA(p[3]))
2762 {
2763 vim_strncpy(region_cp, p + 1, 2);
2764 mch_memmove(p, p + 3, len - (p - lang) - 2);
2765 len -= 3;
2766 region = region_cp;
2767 }
2768 else
2769 dont_use_region = TRUE;
2770
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002771 /* Check if we loaded this language before. */
2772 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2773 if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME)
2774 break;
2775 }
2776 else
2777 {
2778 filename = FALSE;
2779 if (len > 3 && lang[len - 3] == '_')
2780 {
2781 region = lang + len - 2;
2782 len -= 3;
2783 lang[len] = NUL;
2784 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002785 else
2786 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002787
2788 /* Check if we loaded this language before. */
2789 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2790 if (STRICMP(lang, lp->sl_name) == 0)
2791 break;
2792 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002793
Bram Moolenaarb6356332005-07-18 21:40:44 +00002794 if (region != NULL)
2795 {
2796 /* If the region differs from what was used before then don't
2797 * use it for 'spellfile'. */
2798 if (use_region != NULL && STRCMP(region, use_region) != 0)
2799 dont_use_region = TRUE;
2800 use_region = region;
2801 }
2802
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002803 /* If not found try loading the language now. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002804 if (lp == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002805 {
2806 if (filename)
2807 (void)spell_load_file(lang, lang, NULL, FALSE);
2808 else
2809 spell_load_lang(lang);
2810 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002811
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002812 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002813 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002814 */
2815 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002816 if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME
2817 : STRICMP(lang, lp->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002818 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002819 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002820 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002821 {
2822 /* find region in sl_regions */
2823 c = find_region(lp->sl_regions, region);
2824 if (c == REGION_ALL)
2825 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002826 if (lp->sl_add)
2827 {
2828 if (*lp->sl_regions != NUL)
2829 /* This addition file is for other regions. */
2830 region_mask = 0;
2831 }
2832 else
2833 /* This is probably an error. Give a warning and
2834 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002835 smsg((char_u *)
2836 _("Warning: region %s not supported"),
2837 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002838 }
2839 else
2840 region_mask = 1 << c;
2841 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002842
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002843 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002844 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002845 if (ga_grow(&ga, 1) == FAIL)
2846 {
2847 ga_clear(&ga);
2848 return e_outofmem;
2849 }
2850 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2851 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2852 ++ga.ga_len;
2853 use_midword(lp, buf);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002854 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002855 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002856 }
2857
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002858 /* round 0: load int_wordlist, if possible.
2859 * round 1: load first name in 'spellfile'.
2860 * round 2: load second name in 'spellfile.
2861 * etc. */
2862 spf = curbuf->b_p_spf;
2863 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002864 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002865 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002866 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002867 /* Internal wordlist, if there is one. */
2868 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002869 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002870 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002871 }
2872 else
2873 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002874 /* One entry in 'spellfile'. */
2875 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
2876 STRCAT(spf_name, ".spl");
2877
2878 /* If it was already found above then skip it. */
2879 for (c = 0; c < ga.ga_len; ++c)
2880 if (fullpathcmp(spf_name,
2881 LANGP_ENTRY(ga, c)->lp_slang->sl_fname,
2882 FALSE) == FPC_SAME)
2883 break;
2884 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002885 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002886 }
2887
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002888 /* Check if it was loaded already. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002889 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2890 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
2891 break;
2892 if (lp == NULL)
2893 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002894 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002895 * region name, the region is ignored otherwise. for int_wordlist
2896 * use an arbitrary name. */
2897 if (round == 0)
2898 STRCPY(lang, "internal wordlist");
2899 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00002900 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002901 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002902 p = vim_strchr(lang, '.');
2903 if (p != NULL)
2904 *p = NUL; /* truncate at ".encoding.add" */
2905 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002906 lp = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002907 }
2908 if (lp != NULL && ga_grow(&ga, 1) == OK)
2909 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002910 region_mask = REGION_ALL;
2911 if (use_region != NULL && !dont_use_region)
2912 {
2913 /* find region in sl_regions */
2914 c = find_region(lp->sl_regions, use_region);
2915 if (c != REGION_ALL)
2916 region_mask = 1 << c;
2917 else if (*lp->sl_regions != NUL)
2918 /* This spell file is for other regions. */
2919 region_mask = 0;
2920 }
2921
2922 if (region_mask != 0)
2923 {
2924 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2925 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2926 ++ga.ga_len;
2927 use_midword(lp, buf);
2928 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002929 }
2930 }
2931
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002932 /* Add a NULL entry to mark the end of the list. */
2933 if (ga_grow(&ga, 1) == FAIL)
2934 {
2935 ga_clear(&ga);
2936 return e_outofmem;
2937 }
2938 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
2939 ++ga.ga_len;
2940
2941 /* Everything is fine, store the new b_langp value. */
2942 ga_clear(&buf->b_langp);
2943 buf->b_langp = ga;
2944
2945 return NULL;
2946}
2947
2948/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002949 * Clear the midword characters for buffer "buf".
2950 */
2951 static void
2952clear_midword(buf)
2953 buf_T *buf;
2954{
2955 vim_memset(buf->b_spell_ismw, 0, 256);
2956#ifdef FEAT_MBYTE
2957 vim_free(buf->b_spell_ismw_mb);
2958 buf->b_spell_ismw_mb = NULL;
2959#endif
2960}
2961
2962/*
2963 * Use the "sl_midword" field of language "lp" for buffer "buf".
2964 * They add up to any currently used midword characters.
2965 */
2966 static void
2967use_midword(lp, buf)
2968 slang_T *lp;
2969 buf_T *buf;
2970{
2971 char_u *p;
2972
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002973 if (lp->sl_midword == NULL) /* there aren't any */
2974 return;
2975
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002976 for (p = lp->sl_midword; *p != NUL; )
2977#ifdef FEAT_MBYTE
2978 if (has_mbyte)
2979 {
2980 int c, l, n;
2981 char_u *bp;
2982
2983 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002984 l = (*mb_ptr2len)(p);
2985 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002986 buf->b_spell_ismw[c] = TRUE;
2987 else if (buf->b_spell_ismw_mb == NULL)
2988 /* First multi-byte char in "b_spell_ismw_mb". */
2989 buf->b_spell_ismw_mb = vim_strnsave(p, l);
2990 else
2991 {
2992 /* Append multi-byte chars to "b_spell_ismw_mb". */
2993 n = STRLEN(buf->b_spell_ismw_mb);
2994 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
2995 if (bp != NULL)
2996 {
2997 vim_free(buf->b_spell_ismw_mb);
2998 buf->b_spell_ismw_mb = bp;
2999 vim_strncpy(bp + n, p, l);
3000 }
3001 }
3002 p += l;
3003 }
3004 else
3005#endif
3006 buf->b_spell_ismw[*p++] = TRUE;
3007}
3008
3009/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003010 * Find the region "region[2]" in "rp" (points to "sl_regions").
3011 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003012 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003013 */
3014 static int
3015find_region(rp, region)
3016 char_u *rp;
3017 char_u *region;
3018{
3019 int i;
3020
3021 for (i = 0; ; i += 2)
3022 {
3023 if (rp[i] == NUL)
3024 return REGION_ALL;
3025 if (rp[i] == region[0] && rp[i + 1] == region[1])
3026 break;
3027 }
3028 return i / 2;
3029}
3030
3031/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003032 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003033 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00003034 * Word WF_ONECAP
3035 * W WORD WF_ALLCAP
3036 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003037 */
3038 static int
3039captype(word, end)
3040 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003041 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003042{
3043 char_u *p;
3044 int c;
3045 int firstcap;
3046 int allcap;
3047 int past_second = FALSE; /* past second word char */
3048
3049 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003050 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003051 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003052 return 0; /* only non-word characters, illegal word */
3053#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00003054 if (has_mbyte)
3055 c = mb_ptr2char_adv(&p);
3056 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003057#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00003058 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003059 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003060
3061 /*
3062 * Need to check all letters to find a word with mixed upper/lower.
3063 * But a word with an upper char only at start is a ONECAP.
3064 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003065 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003066 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003067 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003068 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003069 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003070 {
3071 /* UUl -> KEEPCAP */
3072 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003073 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003074 allcap = FALSE;
3075 }
3076 else if (!allcap)
3077 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003078 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003079 past_second = TRUE;
3080 }
3081
3082 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003083 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003084 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003085 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003086 return 0;
3087}
3088
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003089/*
3090 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
3091 * capital. So that make_case_word() can turn WOrd into Word.
3092 * Add ALLCAP for "WOrD".
3093 */
3094 static int
3095badword_captype(word, end)
3096 char_u *word;
3097 char_u *end;
3098{
3099 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003100 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003101 int l, u;
3102 int first;
3103 char_u *p;
3104
3105 if (flags & WF_KEEPCAP)
3106 {
3107 /* Count the number of UPPER and lower case letters. */
3108 l = u = 0;
3109 first = FALSE;
3110 for (p = word; p < end; mb_ptr_adv(p))
3111 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003112 c = PTR2CHAR(p);
3113 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003114 {
3115 ++u;
3116 if (p == word)
3117 first = TRUE;
3118 }
3119 else
3120 ++l;
3121 }
3122
3123 /* If there are more UPPER than lower case letters suggest an
3124 * ALLCAP word. Otherwise, if the first letter is UPPER then
3125 * suggest ONECAP. Exception: "ALl" most likely should be "All",
3126 * require three upper case letters. */
3127 if (u > l && u > 2)
3128 flags |= WF_ALLCAP;
3129 else if (first)
3130 flags |= WF_ONECAP;
3131 }
3132 return flags;
3133}
3134
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003135# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
3136/*
3137 * Free all languages.
3138 */
3139 void
3140spell_free_all()
3141{
3142 slang_T *lp;
3143 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003144 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003145
3146 /* Go through all buffers and handle 'spelllang'. */
3147 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3148 ga_clear(&buf->b_langp);
3149
3150 while (first_lang != NULL)
3151 {
3152 lp = first_lang;
3153 first_lang = lp->sl_next;
3154 slang_free(lp);
3155 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003156
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003157 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003158 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003159 /* Delete the internal wordlist and its .spl file */
3160 mch_remove(int_wordlist);
3161 int_wordlist_spl(fname);
3162 mch_remove(fname);
3163 vim_free(int_wordlist);
3164 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003165 }
3166
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003167 init_spell_chartab();
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003168}
3169# endif
3170
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003171# if defined(FEAT_MBYTE) || defined(PROTO)
3172/*
3173 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003174 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003175 */
3176 void
3177spell_reload()
3178{
3179 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003180 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003181
Bram Moolenaarea408852005-06-25 22:49:46 +00003182 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003183 init_spell_chartab();
3184
3185 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003186 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003187
3188 /* Go through all buffers and handle 'spelllang'. */
3189 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3190 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003191 /* Only load the wordlists when 'spelllang' is set and there is a
3192 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003193 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00003194 {
3195 FOR_ALL_WINDOWS(wp)
3196 if (wp->w_buffer == buf && wp->w_p_spell)
3197 {
3198 (void)did_set_spelllang(buf);
3199# ifdef FEAT_WINDOWS
3200 break;
3201# endif
3202 }
3203 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003204 }
3205}
3206# endif
3207
Bram Moolenaarb765d632005-06-07 21:00:02 +00003208/*
3209 * Reload the spell file "fname" if it's loaded.
3210 */
3211 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003212spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003213 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003214 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003215{
3216 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003217 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003218
Bram Moolenaarb765d632005-06-07 21:00:02 +00003219 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3220 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
3221 {
3222 slang_clear(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003223 (void)spell_load_file(fname, NULL, lp, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003224 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003225 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003226 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003227
3228 /* When "zg" was used and the file wasn't loaded yet, should redo
3229 * 'spelllang' to get it loaded. */
3230 if (added_word && !didit)
3231 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003232}
3233
3234
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003235/*
3236 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003237 */
3238
Bram Moolenaar51485f02005-06-04 21:55:20 +00003239#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003240 and .dic file. */
3241/*
3242 * Main structure to store the contents of a ".aff" file.
3243 */
3244typedef struct afffile_S
3245{
3246 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003247 int af_slash; /* character used in word for slash */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003248 int af_rar; /* RAR ID for rare word */
3249 int af_kep; /* KEP ID for keep-case word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003250 int af_bad; /* BAD ID for banned word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003251 char_u *af_compflags; /* COMPOUNDFLAG or COMPOUNDFLAGS */
3252 int af_compminlen; /* COMPOUNDMIN */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003253 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003254 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
3255 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003256} afffile_T;
3257
3258typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003259/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
3260struct affentry_S
3261{
3262 affentry_T *ae_next; /* next affix with same name/number */
3263 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
3264 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003265 char_u *ae_cond; /* condition (NULL for ".") */
3266 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003267 int ae_rare; /* rare affix */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003268};
3269
Bram Moolenaar53805d12005-08-01 07:08:33 +00003270#define AH_KEY_LEN 10
3271
Bram Moolenaar51485f02005-06-04 21:55:20 +00003272/* Affix header from ".aff" file. Used for af_pref and af_suff. */
3273typedef struct affheader_S
3274{
Bram Moolenaar53805d12005-08-01 07:08:33 +00003275 /* key for hashtable == name of affix entry */
3276#ifdef FEAT_MBYTE
3277 char_u ah_key[AH_KEY_LEN]; /* multi-byte char plus NUL */
3278#else
3279 char_u ah_key[2]; /* one byte char plus NUL */
3280#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003281 int ah_newID; /* prefix ID after renumbering */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003282 int ah_combine; /* suffix may combine with prefix */
3283 affentry_T *ah_first; /* first affix entry */
3284} affheader_T;
3285
3286#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
3287
3288/*
3289 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003290 * the need to keep track of each allocated thing, everything is freed all at
3291 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00003292 */
3293#define SBLOCKSIZE 16000 /* size of sb_data */
3294typedef struct sblock_S sblock_T;
3295struct sblock_S
3296{
3297 sblock_T *sb_next; /* next block in list */
3298 int sb_used; /* nr of bytes already in use */
3299 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003300};
3301
3302/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003303 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003304 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003305typedef struct wordnode_S wordnode_T;
3306struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003307{
Bram Moolenaar0c405862005-06-22 22:26:26 +00003308 union /* shared to save space */
3309 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003310 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003311 int index; /* index in written nodes (valid after first
3312 round) */
3313 } wn_u1;
3314 union /* shared to save space */
3315 {
3316 wordnode_T *next; /* next node with same hash key */
3317 wordnode_T *wnode; /* parent node that will write this node */
3318 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003319 wordnode_T *wn_child; /* child (next byte in word) */
3320 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
3321 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003322 int wn_refs; /* Nr. of references to this node. Only
3323 relevant for first node in a list of
3324 siblings, in following siblings it is
3325 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003326 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003327 char_u wn_affixID; /* when "wn_byte" is NUL: supported/required
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003328 prefix ID or 0 */
3329 short_u wn_flags; /* when "wn_byte" is NUL: WF_ flags */
3330 short wn_region; /* when "wn_byte" is NUL: region mask; for
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003331 PREFIXTREE it's the prefcondnr */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003332#ifdef SPELL_PRINTTREE
3333 int wn_nr; /* sequence nr for printing */
3334#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003335};
3336
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003337#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
3338
Bram Moolenaar51485f02005-06-04 21:55:20 +00003339#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003340
Bram Moolenaar51485f02005-06-04 21:55:20 +00003341/*
3342 * Info used while reading the spell files.
3343 */
3344typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003345{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003346 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003347 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003348
Bram Moolenaar51485f02005-06-04 21:55:20 +00003349 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003350 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003351
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003352 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003353
Bram Moolenaar51485f02005-06-04 21:55:20 +00003354 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003355 long si_blocks_cnt; /* memory blocks allocated */
3356 long si_compress_cnt; /* words to add before lowering
3357 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003358 wordnode_T *si_first_free; /* List of nodes that have been freed during
3359 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003360 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003361#ifdef SPELL_PRINTTREE
3362 int si_wordnode_nr; /* sequence nr for nodes */
3363#endif
3364
3365
Bram Moolenaar51485f02005-06-04 21:55:20 +00003366 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003367 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003368 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003369 int si_region; /* region mask */
3370 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003371 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003372 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003373 int si_msg_count; /* number of words added since last message */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003374 int si_region_count; /* number of regions supported (1 when there
3375 are no regions) */
3376 char_u si_region_name[16]; /* region names (if count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003377
3378 garray_T si_rep; /* list of fromto_T entries from REP lines */
3379 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003380 char_u *si_sofofr; /* SOFOFROM text */
3381 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003382 int si_followup; /* soundsalike: ? */
3383 int si_collapse; /* soundsalike: ? */
3384 int si_rem_accents; /* soundsalike: remove accents */
3385 garray_T si_map; /* MAP info concatenated */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003386 char_u *si_midword; /* MIDWORD chars, alloc'ed string or NULL */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003387 int si_compminlen; /* minimal length for compounding */
3388 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003389 garray_T si_prefcond; /* table with conditions for postponed
3390 * prefixes, each stored as a string */
3391 int si_newID; /* current value for ah_newID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003392} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003393
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003394static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003395static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003396static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
3397static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00003398static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003399static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003400static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
3401static char_u *get_pfxlist __ARGS((spellinfo_T *spin, afffile_T *affile, char_u *afflist));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003402static char_u *get_compflags __ARGS((spellinfo_T *spin, char_u *afflist));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003403static 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));
3404static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
3405static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
3406static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003407static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003408static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
3409static int store_word __ARGS((spellinfo_T *spin, char_u *word, int flags, int region, char_u *pfxlist));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003410static 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 +00003411static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
3412static void deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
3413static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
3414static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
3415static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003416static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003417static void write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00003418static void clear_node __ARGS((wordnode_T *node));
3419static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003420static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00003421static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003422
Bram Moolenaar53805d12005-08-01 07:08:33 +00003423/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
3424 * but it must be negative to indicate the prefix tree to tree_add_word().
3425 * Use a negative number with the lower 8 bits zero. */
3426#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003427
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003428#ifdef SPELL_PRINTTREE
3429/*
3430 * For debugging the tree code: print the current tree in a (more or less)
3431 * readable format, so that we can see what happens when adding a word and/or
3432 * compressing the tree.
3433 * Based on code from Olaf Seibert.
3434 */
3435#define PRINTLINESIZE 1000
3436#define PRINTWIDTH 6
3437
3438#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
3439 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
3440
3441static char line1[PRINTLINESIZE];
3442static char line2[PRINTLINESIZE];
3443static char line3[PRINTLINESIZE];
3444
3445 static void
3446spell_clear_flags(wordnode_T *node)
3447{
3448 wordnode_T *np;
3449
3450 for (np = node; np != NULL; np = np->wn_sibling)
3451 {
3452 np->wn_u1.index = FALSE;
3453 spell_clear_flags(np->wn_child);
3454 }
3455}
3456
3457 static void
3458spell_print_node(wordnode_T *node, int depth)
3459{
3460 if (node->wn_u1.index)
3461 {
3462 /* Done this node before, print the reference. */
3463 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
3464 PRINTSOME(line2, depth, " ", 0, 0);
3465 PRINTSOME(line3, depth, " ", 0, 0);
3466 msg(line1);
3467 msg(line2);
3468 msg(line3);
3469 }
3470 else
3471 {
3472 node->wn_u1.index = TRUE;
3473
3474 if (node->wn_byte != NUL)
3475 {
3476 if (node->wn_child != NULL)
3477 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
3478 else
3479 /* Cannot happen? */
3480 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
3481 }
3482 else
3483 PRINTSOME(line1, depth, " $ ", 0, 0);
3484
3485 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
3486
3487 if (node->wn_sibling != NULL)
3488 PRINTSOME(line3, depth, " | ", 0, 0);
3489 else
3490 PRINTSOME(line3, depth, " ", 0, 0);
3491
3492 if (node->wn_byte == NUL)
3493 {
3494 msg(line1);
3495 msg(line2);
3496 msg(line3);
3497 }
3498
3499 /* do the children */
3500 if (node->wn_byte != NUL && node->wn_child != NULL)
3501 spell_print_node(node->wn_child, depth + 1);
3502
3503 /* do the siblings */
3504 if (node->wn_sibling != NULL)
3505 {
3506 /* get rid of all parent details except | */
3507 STRCPY(line1, line3);
3508 STRCPY(line2, line3);
3509 spell_print_node(node->wn_sibling, depth);
3510 }
3511 }
3512}
3513
3514 static void
3515spell_print_tree(wordnode_T *root)
3516{
3517 if (root != NULL)
3518 {
3519 /* Clear the "wn_u1.index" fields, used to remember what has been
3520 * done. */
3521 spell_clear_flags(root);
3522
3523 /* Recursively print the tree. */
3524 spell_print_node(root, 0);
3525 }
3526}
3527#endif /* SPELL_PRINTTREE */
3528
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003529/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003530 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00003531 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003532 */
3533 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003534spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003535 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003536 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003537{
3538 FILE *fd;
3539 afffile_T *aff;
3540 char_u rline[MAXLINELEN];
3541 char_u *line;
3542 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003543#define MAXITEMCNT 7
3544 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003545 int itemcnt;
3546 char_u *p;
3547 int lnum = 0;
3548 affheader_T *cur_aff = NULL;
3549 int aff_todo = 0;
3550 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003551 char_u *low = NULL;
3552 char_u *fol = NULL;
3553 char_u *upp = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003554 static char *e_affname = N_("Affix name too long in %s line %d: %s");
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003555 int do_rep;
3556 int do_sal;
3557 int do_map;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003558 int do_midword;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003559 int do_sofo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003560 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003561 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003562 int l;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003563
Bram Moolenaar51485f02005-06-04 21:55:20 +00003564 /*
3565 * Open the file.
3566 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003567 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003568 if (fd == NULL)
3569 {
3570 EMSG2(_(e_notopen), fname);
3571 return NULL;
3572 }
3573
Bram Moolenaarb765d632005-06-07 21:00:02 +00003574 if (spin->si_verbose || p_verbose > 2)
3575 {
3576 if (!spin->si_verbose)
3577 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003578 smsg((char_u *)_("Reading affix file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003579 out_flush();
3580 if (!spin->si_verbose)
3581 verbose_leave();
3582 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003583
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003584 /* Only do REP lines when not done in another .aff file already. */
3585 do_rep = spin->si_rep.ga_len == 0;
3586
3587 /* Only do SAL lines when not done in another .aff file already. */
3588 do_sal = spin->si_sal.ga_len == 0;
3589
3590 /* Only do MAP lines when not done in another .aff file already. */
3591 do_map = spin->si_map.ga_len == 0;
3592
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003593 /* Only do MIDWORD line when not done in another .aff file already */
3594 do_midword = spin->si_midword == NULL;
3595
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003596 /* Only do SOFOFROM and SOFOTO when not done in another .aff file already */
3597 do_sofo = spin->si_sofofr == NULL;
3598
Bram Moolenaar51485f02005-06-04 21:55:20 +00003599 /*
3600 * Allocate and init the afffile_T structure.
3601 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003602 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003603 if (aff == NULL)
3604 return NULL;
3605 hash_init(&aff->af_pref);
3606 hash_init(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003607
3608 /*
3609 * Read all the lines in the file one by one.
3610 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003611 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003612 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003613 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003614 ++lnum;
3615
3616 /* Skip comment lines. */
3617 if (*rline == '#')
3618 continue;
3619
3620 /* Convert from "SET" to 'encoding' when needed. */
3621 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003622#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003623 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003624 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003625 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003626 if (pc == NULL)
3627 {
3628 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3629 fname, lnum, rline);
3630 continue;
3631 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003632 line = pc;
3633 }
3634 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003635#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003636 {
3637 pc = NULL;
3638 line = rline;
3639 }
3640
3641 /* Split the line up in white separated items. Put a NUL after each
3642 * item. */
3643 itemcnt = 0;
3644 for (p = line; ; )
3645 {
3646 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
3647 ++p;
3648 if (*p == NUL)
3649 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003650 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003651 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003652 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003653 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003654 ++p;
3655 if (*p == NUL)
3656 break;
3657 *p++ = NUL;
3658 }
3659
3660 /* Handle non-empty lines. */
3661 if (itemcnt > 0)
3662 {
3663 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
3664 && aff->af_enc == NULL)
3665 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003666#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003667 /* Setup for conversion from "ENC" to 'encoding'. */
3668 aff->af_enc = enc_canonize(items[1]);
3669 if (aff->af_enc != NULL && !spin->si_ascii
3670 && convert_setup(&spin->si_conv, aff->af_enc,
3671 p_enc) == FAIL)
3672 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
3673 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003674 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003675#else
3676 smsg((char_u *)_("Conversion in %s not supported"), fname);
3677#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003678 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003679 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2)
3680 {
3681 if (do_midword)
3682 spin->si_midword = vim_strsave(items[1]);
3683 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00003684 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
3685 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003686 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003687 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003688 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003689 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003690 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003691 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003692 else if (STRCMP(items[0], "SLASH") == 0 && itemcnt == 2
3693 && aff->af_slash == 0)
3694 {
3695 aff->af_slash = items[1][0];
3696 if (items[1][1] != NUL)
3697 smsg((char_u *)_("Character used for SLASH must be ASCII; in %s line %d: %s"),
3698 fname, lnum, items[1]);
3699 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003700 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
3701 && aff->af_rar == 0)
3702 {
3703 aff->af_rar = items[1][0];
3704 if (items[1][1] != NUL)
3705 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3706 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00003707 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
3708 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003709 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003710 aff->af_kep = items[1][0];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003711 if (items[1][1] != NUL)
3712 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3713 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00003714 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
3715 && aff->af_bad == 0)
3716 {
3717 aff->af_bad = items[1][0];
3718 if (items[1][1] != NUL)
3719 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3720 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003721 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
3722 && aff->af_compflags == 0)
3723 {
3724 aff->af_compflags = getroom_save(spin, items[1]);
3725 if (items[1][1] != NUL)
3726 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3727 }
3728 else if (STRCMP(items[0], "COMPOUNDFLAGS") == 0 && itemcnt == 2
3729 && aff->af_compflags == 0)
3730 {
3731 aff->af_compflags = getroom_save(spin, items[1]);
3732 }
3733 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
3734 && aff->af_compminlen == 0)
3735 {
3736 aff->af_compminlen = atoi((char *)items[1]);
3737 if (aff->af_compminlen == 0)
3738 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
3739 fname, lnum, items[1]);
3740 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003741 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
3742 {
3743 aff->af_pfxpostpone = TRUE;
3744 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003745 else if ((STRCMP(items[0], "PFX") == 0
3746 || STRCMP(items[0], "SFX") == 0)
3747 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003748 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003749 {
Bram Moolenaar8db73182005-06-17 21:51:16 +00003750 /* Myspell allows extra text after the item, but that might
3751 * mean mistakes go unnoticed. Require a comment-starter. */
3752 if (itemcnt > 4 && *items[4] != '#')
3753 smsg((char_u *)_("Trailing text in %s line %d: %s"),
3754 fname, lnum, items[4]);
3755
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003756 /* New affix letter. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003757 cur_aff = (affheader_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003758 sizeof(affheader_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003759 if (cur_aff == NULL)
3760 break;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003761#ifdef FEAT_MBYTE
3762 if (has_mbyte)
3763 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003764 l = (*mb_ptr2len)(items[1]);
Bram Moolenaar53805d12005-08-01 07:08:33 +00003765 if (l >= AH_KEY_LEN)
3766 l = 1; /* too long, must be an overlong sequence */
3767 else
3768 mch_memmove(cur_aff->ah_key, items[1], l);
3769 }
3770 else
3771#endif
3772 {
3773 *cur_aff->ah_key = *items[1];
3774 l = 1;
3775 }
3776 cur_aff->ah_key[l] = NUL;
3777 if (items[1][l] != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003778 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003779 if (*items[2] == 'Y')
3780 cur_aff->ah_combine = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003781 else if (*items[2] != 'N')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003782 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
3783 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003784
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003785 if (*items[0] == 'P')
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003786 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003787 tp = &aff->af_pref;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003788 /* Use a new number in the .spl file later, to be able to
3789 * handle multiple .aff files. */
3790 if (aff->af_pfxpostpone)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003791 cur_aff->ah_newID = ++spin->si_newID;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003792 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003793 else
3794 tp = &aff->af_suff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003795 aff_todo = atoi((char *)items[3]);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003796 hi = hash_find(tp, cur_aff->ah_key);
3797 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar51485f02005-06-04 21:55:20 +00003798 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003799 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
3800 fname, lnum, items[1]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003801 aff_todo = 0;
3802 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003803 else
3804 hash_add(tp, cur_aff->ah_key);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003805 }
3806 else if ((STRCMP(items[0], "PFX") == 0
3807 || STRCMP(items[0], "SFX") == 0)
3808 && aff_todo > 0
3809 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003810 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003811 {
3812 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003813 int rare = FALSE;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003814 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003815 int lasti = 5;
3816
3817 /* Check for "rare" after the other info. */
3818 if (itemcnt > 5 && STRICMP(items[5], "rare") == 0)
3819 {
3820 rare = TRUE;
3821 lasti = 6;
3822 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003823
Bram Moolenaar8db73182005-06-17 21:51:16 +00003824 /* Myspell allows extra text after the item, but that might
3825 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003826 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003827 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00003828
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003829 /* New item for an affix letter. */
3830 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003831 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003832 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003833 if (aff_entry == NULL)
3834 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003835 aff_entry->ae_rare = rare;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003836
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003837 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003838 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003839 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003840 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003841
Bram Moolenaar51485f02005-06-04 21:55:20 +00003842 /* Don't use an affix entry with non-ASCII characters when
3843 * "spin->si_ascii" is TRUE. */
3844 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00003845 || has_non_ascii(aff_entry->ae_add)))
3846 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003847 aff_entry->ae_next = cur_aff->ah_first;
3848 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003849
3850 if (STRCMP(items[4], ".") != 0)
3851 {
3852 char_u buf[MAXLINELEN];
3853
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003854 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003855 if (*items[0] == 'P')
3856 sprintf((char *)buf, "^%s", items[4]);
3857 else
3858 sprintf((char *)buf, "%s$", items[4]);
3859 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003860 RE_MAGIC + RE_STRING + RE_STRICT);
3861 if (aff_entry->ae_prog == NULL)
3862 smsg((char_u *)_("Broken condition in %s line %d: %s"),
3863 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003864 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003865
3866 /* For postponed prefixes we need an entry in si_prefcond
3867 * for the condition. Use an existing one if possible. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003868 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003869 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003870 /* When the chop string is one lower-case letter and
3871 * the add string ends in the upper-case letter we set
3872 * the "upper" flag, clear "ae_chop" and remove the
3873 * letters from "ae_add". The condition must either
3874 * be empty or start with the same letter. */
3875 if (aff_entry->ae_chop != NULL
3876 && aff_entry->ae_add != NULL
3877#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003878 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00003879 aff_entry->ae_chop)] == NUL
3880#else
3881 && aff_entry->ae_chop[1] == NUL
3882#endif
3883 )
3884 {
3885 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003886
Bram Moolenaar53805d12005-08-01 07:08:33 +00003887 c = PTR2CHAR(aff_entry->ae_chop);
3888 c_up = SPELL_TOUPPER(c);
3889 if (c_up != c
3890 && (aff_entry->ae_cond == NULL
3891 || PTR2CHAR(aff_entry->ae_cond) == c))
3892 {
3893 p = aff_entry->ae_add
3894 + STRLEN(aff_entry->ae_add);
3895 mb_ptr_back(aff_entry->ae_add, p);
3896 if (PTR2CHAR(p) == c_up)
3897 {
3898 upper = TRUE;
3899 aff_entry->ae_chop = NULL;
3900 *p = NUL;
3901
3902 /* The condition is matched with the
3903 * actual word, thus must check for the
3904 * upper-case letter. */
3905 if (aff_entry->ae_cond != NULL)
3906 {
3907 char_u buf[MAXLINELEN];
3908#ifdef FEAT_MBYTE
3909 if (has_mbyte)
3910 {
3911 onecap_copy(items[4], buf, TRUE);
3912 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003913 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00003914 }
3915 else
3916#endif
3917 *aff_entry->ae_cond = c_up;
3918 if (aff_entry->ae_cond != NULL)
3919 {
3920 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003921 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00003922 vim_free(aff_entry->ae_prog);
3923 aff_entry->ae_prog = vim_regcomp(
3924 buf, RE_MAGIC + RE_STRING);
3925 }
3926 }
3927 }
3928 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003929 }
3930
Bram Moolenaar53805d12005-08-01 07:08:33 +00003931 if (aff_entry->ae_chop == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003932 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003933 int idx;
3934 char_u **pp;
3935 int n;
3936
3937 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
3938 --idx)
3939 {
3940 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
3941 if (str_equal(p, aff_entry->ae_cond))
3942 break;
3943 }
3944 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
3945 {
3946 /* Not found, add a new condition. */
3947 idx = spin->si_prefcond.ga_len++;
3948 pp = ((char_u **)spin->si_prefcond.ga_data)
3949 + idx;
3950 if (aff_entry->ae_cond == NULL)
3951 *pp = NULL;
3952 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003953 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00003954 aff_entry->ae_cond);
3955 }
3956
3957 /* Add the prefix to the prefix tree. */
3958 if (aff_entry->ae_add == NULL)
3959 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003960 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00003961 p = aff_entry->ae_add;
3962 /* PFX_FLAGS is a negative number, so that
3963 * tree_add_word() knows this is the prefix tree. */
3964 n = PFX_FLAGS;
3965 if (rare)
3966 n |= WFP_RARE;
3967 if (!cur_aff->ah_combine)
3968 n |= WFP_NC;
3969 if (upper)
3970 n |= WFP_UP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003971 tree_add_word(spin, p, spin->si_prefroot, n,
3972 idx, cur_aff->ah_newID);
Bram Moolenaar53805d12005-08-01 07:08:33 +00003973 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003974 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003975 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003976 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003977 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
3978 {
3979 if (fol != NULL)
3980 smsg((char_u *)_("Duplicate FOL in %s line %d"),
3981 fname, lnum);
3982 else
3983 fol = vim_strsave(items[1]);
3984 }
3985 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
3986 {
3987 if (low != NULL)
3988 smsg((char_u *)_("Duplicate LOW in %s line %d"),
3989 fname, lnum);
3990 else
3991 low = vim_strsave(items[1]);
3992 }
3993 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
3994 {
3995 if (upp != NULL)
3996 smsg((char_u *)_("Duplicate UPP in %s line %d"),
3997 fname, lnum);
3998 else
3999 upp = vim_strsave(items[1]);
4000 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004001 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004002 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004003 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004004 if (!isdigit(*items[1]))
4005 smsg((char_u *)_("Expected REP count in %s line %d"),
4006 fname, lnum);
4007 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004008 else if (STRCMP(items[0], "REP") == 0 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004009 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004010 /* REP item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004011 /* Myspell ignores extra arguments, we require it starts with
4012 * # to detect mistakes. */
4013 if (itemcnt > 3 && items[3][0] != '#')
4014 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004015 if (do_rep)
4016 add_fromto(spin, &spin->si_rep, items[1], items[2]);
4017 }
4018 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
4019 {
4020 /* MAP item or count */
4021 if (!found_map)
4022 {
4023 /* First line contains the count. */
4024 found_map = TRUE;
4025 if (!isdigit(*items[1]))
4026 smsg((char_u *)_("Expected MAP count in %s line %d"),
4027 fname, lnum);
4028 }
4029 else if (do_map)
4030 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00004031 int c;
4032
4033 /* Check that every character appears only once. */
4034 for (p = items[1]; *p != NUL; )
4035 {
4036#ifdef FEAT_MBYTE
4037 c = mb_ptr2char_adv(&p);
4038#else
4039 c = *p++;
4040#endif
4041 if ((spin->si_map.ga_len > 0
4042 && vim_strchr(spin->si_map.ga_data, c)
4043 != NULL)
4044 || vim_strchr(p, c) != NULL)
4045 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
4046 fname, lnum);
4047 }
4048
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004049 /* We simply concatenate all the MAP strings, separated by
4050 * slashes. */
4051 ga_concat(&spin->si_map, items[1]);
4052 ga_append(&spin->si_map, '/');
4053 }
4054 }
4055 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
4056 {
4057 if (do_sal)
4058 {
4059 /* SAL item (sounds-a-like)
4060 * Either one of the known keys or a from-to pair. */
4061 if (STRCMP(items[1], "followup") == 0)
4062 spin->si_followup = sal_to_bool(items[2]);
4063 else if (STRCMP(items[1], "collapse_result") == 0)
4064 spin->si_collapse = sal_to_bool(items[2]);
4065 else if (STRCMP(items[1], "remove_accents") == 0)
4066 spin->si_rem_accents = sal_to_bool(items[2]);
4067 else
4068 /* when "to" is "_" it means empty */
4069 add_fromto(spin, &spin->si_sal, items[1],
4070 STRCMP(items[2], "_") == 0 ? (char_u *)""
4071 : items[2]);
4072 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004073 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004074 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
4075 && (!do_sofo || spin->si_sofofr == NULL))
4076 {
4077 if (do_sofo)
4078 spin->si_sofofr = vim_strsave(items[1]);
4079 }
4080 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
4081 && (!do_sofo || spin->si_sofoto == NULL))
4082 {
4083 if (do_sofo)
4084 spin->si_sofoto = vim_strsave(items[1]);
4085 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004086 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004087 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004088 fname, lnum, items[0]);
4089 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004090 }
4091
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004092 if (do_sofo && (spin->si_sofofr == NULL) != (spin->si_sofoto == NULL))
4093 smsg((char_u *)_("Missing SOFO%s line in %s"),
4094 spin->si_sofofr == NULL ? "FROM" : "TO", fname);
4095 if (spin->si_sofofr != NULL && spin->si_sal.ga_len > 0)
4096 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
4097
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004098 if (fol != NULL || low != NULL || upp != NULL)
4099 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004100 if (spin->si_clear_chartab)
4101 {
4102 /* Clear the char type tables, don't want to use any of the
4103 * currently used spell properties. */
4104 init_spell_chartab();
4105 spin->si_clear_chartab = FALSE;
4106 }
4107
Bram Moolenaar3982c542005-06-08 21:56:31 +00004108 /*
4109 * Don't write a word table for an ASCII file, so that we don't check
4110 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004111 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00004112 * mb_get_class(), the list of chars in the file will be incomplete.
4113 */
4114 if (!spin->si_ascii
4115#ifdef FEAT_MBYTE
4116 && !enc_utf8
4117#endif
4118 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004119 {
4120 if (fol == NULL || low == NULL || upp == NULL)
4121 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
4122 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00004123 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004124 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004125
4126 vim_free(fol);
4127 vim_free(low);
4128 vim_free(upp);
4129 }
4130
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004131 /* Use compound specifications of the .aff file for the spell info. */
4132 if (aff->af_compminlen != 0)
4133 {
4134 if (spin->si_compminlen != 0
4135 && spin->si_compminlen != aff->af_compminlen)
4136 smsg((char_u *)_("COMPOUNDMIN value differs from what is used in another .aff file"));
4137 else
4138 spin->si_compminlen = aff->af_compminlen;
4139 }
4140
4141 if (aff->af_compflags != NULL)
4142 {
4143 if (spin->si_compflags != NULL
4144 && STRCMP(spin->si_compflags, aff->af_compflags) != 0)
4145 smsg((char_u *)_("COMPOUNDFLAG(S) value differs from what is used in another .aff file"));
4146 else
4147 spin->si_compflags = aff->af_compflags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004148 }
4149
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004150 vim_free(pc);
4151 fclose(fd);
4152 return aff;
4153}
4154
4155/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004156 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
4157 * NULL as equal.
4158 */
4159 static int
4160str_equal(s1, s2)
4161 char_u *s1;
4162 char_u *s2;
4163{
4164 if (s1 == NULL || s2 == NULL)
4165 return s1 == s2;
4166 return STRCMP(s1, s2) == 0;
4167}
4168
4169/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004170 * Add a from-to item to "gap". Used for REP and SAL items.
4171 * They are stored case-folded.
4172 */
4173 static void
4174add_fromto(spin, gap, from, to)
4175 spellinfo_T *spin;
4176 garray_T *gap;
4177 char_u *from;
4178 char_u *to;
4179{
4180 fromto_T *ftp;
4181 char_u word[MAXWLEN];
4182
4183 if (ga_grow(gap, 1) == OK)
4184 {
4185 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
4186 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004187 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004188 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004189 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004190 ++gap->ga_len;
4191 }
4192}
4193
4194/*
4195 * Convert a boolean argument in a SAL line to TRUE or FALSE;
4196 */
4197 static int
4198sal_to_bool(s)
4199 char_u *s;
4200{
4201 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
4202}
4203
4204/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00004205 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
4206 * When "s" is NULL FALSE is returned.
4207 */
4208 static int
4209has_non_ascii(s)
4210 char_u *s;
4211{
4212 char_u *p;
4213
4214 if (s != NULL)
4215 for (p = s; *p != NUL; ++p)
4216 if (*p >= 128)
4217 return TRUE;
4218 return FALSE;
4219}
4220
4221/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004222 * Free the structure filled by spell_read_aff().
4223 */
4224 static void
4225spell_free_aff(aff)
4226 afffile_T *aff;
4227{
4228 hashtab_T *ht;
4229 hashitem_T *hi;
4230 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004231 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004232 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004233
4234 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004235
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004236 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004237 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
4238 {
4239 todo = ht->ht_used;
4240 for (hi = ht->ht_array; todo > 0; ++hi)
4241 {
4242 if (!HASHITEM_EMPTY(hi))
4243 {
4244 --todo;
4245 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004246 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
4247 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004248 }
4249 }
4250 if (ht == &aff->af_suff)
4251 break;
4252 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004253
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004254 hash_clear(&aff->af_pref);
4255 hash_clear(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004256}
4257
4258/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004259 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004260 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004261 */
4262 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004263spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004264 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004265 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004266 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004267{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004268 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004269 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004270 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004271 char_u *afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004272 char_u *store_afflist;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004273 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004274 char_u *pc;
4275 char_u *w;
4276 int l;
4277 hash_T hash;
4278 hashitem_T *hi;
4279 FILE *fd;
4280 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004281 int non_ascii = 0;
4282 int retval = OK;
4283 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004284 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004285 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004286
Bram Moolenaar51485f02005-06-04 21:55:20 +00004287 /*
4288 * Open the file.
4289 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004290 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004291 if (fd == NULL)
4292 {
4293 EMSG2(_(e_notopen), fname);
4294 return FAIL;
4295 }
4296
Bram Moolenaar51485f02005-06-04 21:55:20 +00004297 /* The hashtable is only used to detect duplicated words. */
4298 hash_init(&ht);
4299
Bram Moolenaarb765d632005-06-07 21:00:02 +00004300 if (spin->si_verbose || p_verbose > 2)
4301 {
4302 if (!spin->si_verbose)
4303 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004304 smsg((char_u *)_("Reading dictionary file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004305 out_flush();
4306 if (!spin->si_verbose)
4307 verbose_leave();
4308 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004309
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004310 /* start with a message for the first line */
4311 spin->si_msg_count = 999999;
4312
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004313 /* Read and ignore the first line: word count. */
4314 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004315 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004316 EMSG2(_("E760: No word count in %s"), fname);
4317
4318 /*
4319 * Read all the lines in the file one by one.
4320 * The words are converted to 'encoding' here, before being added to
4321 * the hashtable.
4322 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004323 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004324 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004325 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004326 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004327 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004328 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004329
Bram Moolenaar51485f02005-06-04 21:55:20 +00004330 /* Remove CR, LF and white space from the end. White space halfway
4331 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004332 l = STRLEN(line);
4333 while (l > 0 && line[l - 1] <= ' ')
4334 --l;
4335 if (l == 0)
4336 continue; /* empty line */
4337 line[l] = NUL;
4338
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004339 /* Find the optional affix names. Replace the SLASH character by a
4340 * slash. */
4341 afflist = NULL;
4342 for (p = line; *p != NUL; mb_ptr_adv(p))
4343 {
4344 if (*p == affile->af_slash)
4345 *p = '/';
4346 else if (*p == '/')
4347 {
4348 *p = NUL;
4349 afflist = p + 1;
4350 break;
4351 }
4352 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004353
4354 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
4355 if (spin->si_ascii && has_non_ascii(line))
4356 {
4357 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004358 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004359 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00004360
Bram Moolenaarb765d632005-06-07 21:00:02 +00004361#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004362 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004363 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004364 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004365 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004366 if (pc == NULL)
4367 {
4368 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4369 fname, lnum, line);
4370 continue;
4371 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004372 w = pc;
4373 }
4374 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004375#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004376 {
4377 pc = NULL;
4378 w = line;
4379 }
4380
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004381 /* This takes time, print a message every 10000 words. */
4382 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004383 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004384 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004385 vim_snprintf((char *)message, sizeof(message),
4386 _("line %6d, word %6d - %s"),
4387 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
4388 msg_start();
4389 msg_puts_long_attr(message, 0);
4390 msg_clr_eos();
4391 msg_didout = FALSE;
4392 msg_col = 0;
4393 out_flush();
4394 }
4395
Bram Moolenaar51485f02005-06-04 21:55:20 +00004396 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004397 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004398 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004399 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004400 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004401 if (retval == FAIL)
4402 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004403
Bram Moolenaar51485f02005-06-04 21:55:20 +00004404 hash = hash_hash(dw);
4405 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004406 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004407 {
4408 if (p_verbose > 0)
4409 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004410 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004411 else if (duplicate == 0)
4412 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
4413 fname, lnum, dw);
4414 ++duplicate;
4415 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004416 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00004417 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004418
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004419 flags = 0;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004420 store_afflist = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004421 if (afflist != NULL)
4422 {
4423 /* Check for affix name that stands for keep-case word and stands
4424 * for rare word (if defined). */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004425 if (affile->af_kep != NUL
4426 && vim_strchr(afflist, affile->af_kep) != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004427 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004428 if (affile->af_rar != NUL
4429 && vim_strchr(afflist, affile->af_rar) != NULL)
4430 flags |= WF_RARE;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004431 if (affile->af_bad != NUL
4432 && vim_strchr(afflist, affile->af_bad) != NULL)
4433 flags |= WF_BANNED;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004434
4435 if (affile->af_pfxpostpone)
4436 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004437 store_afflist = get_pfxlist(spin, affile, afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004438
4439 if (spin->si_compflags)
4440 {
4441 /* Need to store the list of compound flags with the word. */
4442 p = get_compflags(spin, afflist);
4443 if (p != NULL)
4444 {
4445 if (store_afflist != NULL)
4446 {
4447 char_u *s;
4448
4449 /* Concatenate the prefix IDs with the compound flags.
4450 */
4451 s = getroom(spin, STRLEN(store_afflist)
4452 + STRLEN(p) + 1, FALSE);
4453 if (s != NULL)
4454 {
4455 STRCPY(s, store_afflist);
4456 STRCAT(s, p);
4457 store_afflist = s;
4458 }
4459 }
4460 else
4461 store_afflist = p;
4462 }
4463 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004464 }
4465
Bram Moolenaar51485f02005-06-04 21:55:20 +00004466 /* Add the word to the word tree(s). */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004467 if (store_word(spin, dw, flags, spin->si_region, store_afflist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004468 retval = FAIL;
4469
4470 if (afflist != NULL)
4471 {
4472 /* Find all matching suffixes and add the resulting words.
4473 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004474 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004475 &affile->af_suff, &affile->af_pref,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004476 FALSE, flags, store_afflist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004477 retval = FAIL;
4478
4479 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004480 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004481 &affile->af_pref, NULL,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004482 FALSE, flags, store_afflist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004483 retval = FAIL;
4484 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004485 }
4486
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004487 if (duplicate > 0)
4488 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004489 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004490 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
4491 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004492 hash_clear(&ht);
4493
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004494 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004495 return retval;
4496}
4497
4498/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004499 * Get the list of prefix IDs from the affix list "afflist".
4500 * Used for PFXPOSTPONE.
4501 * Returns a string allocated with getroom(). NULL when there are no prefixes
4502 * or when out of memory.
4503 */
4504 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004505get_pfxlist(spin, affile, afflist)
4506 spellinfo_T *spin;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004507 afffile_T *affile;
4508 char_u *afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004509{
4510 char_u *p;
4511 int cnt;
4512 int round;
4513 char_u *res = NULL;
4514 char_u key[2];
4515 hashitem_T *hi;
4516
4517 key[1] = NUL;
4518
4519 /* round 1: count the number of prefix IDs.
4520 * round 2: move prefix IDs to "res" */
4521 for (round = 1; round <= 2; ++round)
4522 {
4523 cnt = 0;
4524 for (p = afflist; *p != NUL; ++p)
4525 {
4526 key[0] = *p;
4527 hi = hash_find(&affile->af_pref, key);
4528 if (!HASHITEM_EMPTY(hi))
4529 {
4530 /* This is a prefix ID, use the new number. */
4531 if (round == 2)
4532 res[cnt] = HI2AH(hi)->ah_newID;
4533 ++cnt;
4534 }
4535 }
4536 if (round == 1 && cnt > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004537 res = getroom(spin, cnt + 1, FALSE);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004538 if (res == NULL)
4539 break;
4540 }
4541
4542 if (res != NULL)
4543 res[cnt] = NUL;
4544 return res;
4545}
4546
4547/*
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004548 * Get the list of affix IDs from the affix list "afflist" that are used for
4549 * compound words.
4550 * Returns a string allocated with getroom(). NULL when there are no relevant
4551 * affixes or when out of memory.
4552 */
4553 static char_u *
4554get_compflags(spin, afflist)
4555 spellinfo_T *spin;
4556 char_u *afflist;
4557{
4558 char_u *p;
4559 int cnt;
4560 int round;
4561 char_u *res = NULL;
4562
4563 /* round 1: count the number of affix IDs.
4564 * round 2: move affix IDs to "res" */
4565 for (round = 1; round <= 2; ++round)
4566 {
4567 cnt = 0;
4568 for (p = afflist; *p != NUL; ++p)
4569 {
4570 if (*p != ',' && *p != '-'
4571 && vim_strchr(spin->si_compflags, *p) != NULL)
4572 {
4573 /* This is a compount affix ID. */
4574 if (round == 2)
4575 res[cnt] = *p;
4576 ++cnt;
4577 }
4578 }
4579 if (round == 1 && cnt > 0)
4580 res = getroom(spin, cnt + 1, FALSE);
4581 if (res == NULL)
4582 break;
4583 }
4584
4585 if (res != NULL)
4586 res[cnt] = NUL;
4587 return res;
4588}
4589
4590/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004591 * Apply affixes to a word and store the resulting words.
4592 * "ht" is the hashtable with affentry_T that need to be applied, either
4593 * prefixes or suffixes.
4594 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
4595 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004596 *
4597 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004598 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004599 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004600store_aff_word(spin, word, afflist, affile, ht, xht, comb, flags, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004601 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004602 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004603 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004604 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004605 hashtab_T *ht;
4606 hashtab_T *xht;
4607 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004608 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004609 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004610{
4611 int todo;
4612 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004613 affheader_T *ah;
4614 affentry_T *ae;
4615 regmatch_T regmatch;
4616 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004617 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004618 int i;
4619 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004620 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004621 char_u *use_pfxlist;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004622 int c;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004623 int wordlen = STRLEN(word);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004624
Bram Moolenaar51485f02005-06-04 21:55:20 +00004625 todo = ht->ht_used;
4626 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004627 {
4628 if (!HASHITEM_EMPTY(hi))
4629 {
4630 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004631 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00004632
Bram Moolenaar51485f02005-06-04 21:55:20 +00004633 /* Check that the affix combines, if required, and that the word
4634 * supports this affix. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004635 c = PTR2CHAR(ah->ah_key);
4636 if ((!comb || ah->ah_combine) && vim_strchr(afflist, c) != NULL)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004637 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004638 /* Loop over all affix entries with this name. */
4639 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004640 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004641 /* Check the condition. It's not logical to match case
4642 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004643 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004644 * Another requirement from Myspell is that the chop
4645 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004646 * For prefixes, when "PFXPOSTPONE" was used, only do
4647 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004648 regmatch.regprog = ae->ae_prog;
4649 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004650 if ((xht != NULL || !affile->af_pfxpostpone
4651 || ae->ae_chop != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004652 && (ae->ae_chop == NULL
4653 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004654 && (ae->ae_prog == NULL
4655 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004656 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004657 /* Match. Remove the chop and add the affix. */
4658 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004659 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004660 /* prefix: chop/add at the start of the word */
4661 if (ae->ae_add == NULL)
4662 *newword = NUL;
4663 else
4664 STRCPY(newword, ae->ae_add);
4665 p = word;
4666 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004667 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004668 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004669#ifdef FEAT_MBYTE
4670 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004671 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004672 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004673 for ( ; i > 0; --i)
4674 mb_ptr_adv(p);
4675 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004676 else
4677#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004678 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004679 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004680 STRCAT(newword, p);
4681 }
4682 else
4683 {
4684 /* suffix: chop/add at the end of the word */
4685 STRCPY(newword, word);
4686 if (ae->ae_chop != NULL)
4687 {
4688 /* Remove chop string. */
4689 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004690 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004691 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004692 mb_ptr_back(newword, p);
4693 *p = NUL;
4694 }
4695 if (ae->ae_add != NULL)
4696 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004697 }
4698
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004699 /* Obey the "rare" flag of the affix. */
4700 if (ae->ae_rare)
4701 use_flags = flags | WF_RARE;
4702 else
4703 use_flags = flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004704 use_pfxlist = pfxlist;
4705
4706 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00004707 if (spin->si_prefroot != NULL
4708 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004709 {
4710 /* ... add a flag to indicate an affix was used. */
4711 use_flags |= WF_HAS_AFF;
4712
4713 /* ... don't use a prefix list if combining
4714 * affixes is not allowed */
4715 if (!ah->ah_combine || comb)
4716 use_pfxlist = NULL;
4717 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004718
Bram Moolenaar51485f02005-06-04 21:55:20 +00004719 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004720 if (store_word(spin, newword, use_flags,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004721 spin->si_region, use_pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004722 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004723
Bram Moolenaar51485f02005-06-04 21:55:20 +00004724 /* When added a suffix and combining is allowed also
4725 * try adding prefixes additionally. */
4726 if (xht != NULL && ah->ah_combine)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004727 if (store_aff_word(spin, newword, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004728 xht, NULL, TRUE,
4729 use_flags, use_pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004730 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004731 }
4732 }
4733 }
4734 }
4735 }
4736
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004737 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004738}
4739
4740/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004741 * Read a file with a list of words.
4742 */
4743 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004744spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004745 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004746 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004747{
4748 FILE *fd;
4749 long lnum = 0;
4750 char_u rline[MAXLINELEN];
4751 char_u *line;
4752 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004753 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004754 int l;
4755 int retval = OK;
4756 int did_word = FALSE;
4757 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004758 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004759 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004760
4761 /*
4762 * Open the file.
4763 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004764 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00004765 if (fd == NULL)
4766 {
4767 EMSG2(_(e_notopen), fname);
4768 return FAIL;
4769 }
4770
Bram Moolenaarb765d632005-06-07 21:00:02 +00004771 if (spin->si_verbose || p_verbose > 2)
4772 {
4773 if (!spin->si_verbose)
4774 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004775 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004776 out_flush();
4777 if (!spin->si_verbose)
4778 verbose_leave();
4779 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004780
4781 /*
4782 * Read all the lines in the file one by one.
4783 */
4784 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
4785 {
4786 line_breakcheck();
4787 ++lnum;
4788
4789 /* Skip comment lines. */
4790 if (*rline == '#')
4791 continue;
4792
4793 /* Remove CR, LF and white space from the end. */
4794 l = STRLEN(rline);
4795 while (l > 0 && rline[l - 1] <= ' ')
4796 --l;
4797 if (l == 0)
4798 continue; /* empty or blank line */
4799 rline[l] = NUL;
4800
4801 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
4802 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004803#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004804 if (spin->si_conv.vc_type != CONV_NONE)
4805 {
4806 pc = string_convert(&spin->si_conv, rline, NULL);
4807 if (pc == NULL)
4808 {
4809 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4810 fname, lnum, rline);
4811 continue;
4812 }
4813 line = pc;
4814 }
4815 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004816#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004817 {
4818 pc = NULL;
4819 line = rline;
4820 }
4821
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004822 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00004823 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004824 ++line;
4825 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004826 {
4827 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004828 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
4829 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004830 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004831 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
4832 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004833 else
4834 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004835#ifdef FEAT_MBYTE
4836 char_u *enc;
4837
Bram Moolenaar51485f02005-06-04 21:55:20 +00004838 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004839 line += 10;
4840 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004841 if (enc != NULL && !spin->si_ascii
4842 && convert_setup(&spin->si_conv, enc,
4843 p_enc) == FAIL)
4844 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00004845 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004846 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004847 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004848#else
4849 smsg((char_u *)_("Conversion in %s not supported"), fname);
4850#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004851 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004852 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004853 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004854
Bram Moolenaar3982c542005-06-08 21:56:31 +00004855 if (STRNCMP(line, "regions=", 8) == 0)
4856 {
4857 if (spin->si_region_count > 1)
4858 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
4859 fname, lnum, line);
4860 else
4861 {
4862 line += 8;
4863 if (STRLEN(line) > 16)
4864 smsg((char_u *)_("Too many regions in %s line %d: %s"),
4865 fname, lnum, line);
4866 else
4867 {
4868 spin->si_region_count = STRLEN(line) / 2;
4869 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004870
4871 /* Adjust the mask for a word valid in all regions. */
4872 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004873 }
4874 }
4875 continue;
4876 }
4877
Bram Moolenaar7887d882005-07-01 22:33:52 +00004878 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
4879 fname, lnum, line - 1);
4880 continue;
4881 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004882
Bram Moolenaar7887d882005-07-01 22:33:52 +00004883 flags = 0;
4884 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004885
Bram Moolenaar7887d882005-07-01 22:33:52 +00004886 /* Check for flags and region after a slash. */
4887 p = vim_strchr(line, '/');
4888 if (p != NULL)
4889 {
4890 *p++ = NUL;
4891 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004892 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004893 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004894 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004895 else if (*p == '!') /* Bad, bad, wicked word. */
4896 flags |= WF_BANNED;
4897 else if (*p == '?') /* Rare word. */
4898 flags |= WF_RARE;
4899 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004900 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004901 if ((flags & WF_REGION) == 0) /* first one */
4902 regionmask = 0;
4903 flags |= WF_REGION;
4904
4905 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00004906 if (l > spin->si_region_count)
4907 {
4908 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00004909 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004910 break;
4911 }
4912 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004913 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00004914 else
4915 {
4916 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
4917 fname, lnum, p);
4918 break;
4919 }
4920 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004921 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004922 }
4923
4924 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
4925 if (spin->si_ascii && has_non_ascii(line))
4926 {
4927 ++non_ascii;
4928 continue;
4929 }
4930
4931 /* Normal word: store it. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004932 if (store_word(spin, line, flags, regionmask, NULL) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004933 {
4934 retval = FAIL;
4935 break;
4936 }
4937 did_word = TRUE;
4938 }
4939
4940 vim_free(pc);
4941 fclose(fd);
4942
Bram Moolenaarb765d632005-06-07 21:00:02 +00004943 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
4944 {
4945 if (p_verbose > 2)
4946 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00004947 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
4948 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004949 if (p_verbose > 2)
4950 verbose_leave();
4951 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004952 return retval;
4953}
4954
4955/*
4956 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004957 * This avoids calling free() for every little struct we use (and keeping
4958 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00004959 * The memory is cleared to all zeros.
4960 * Returns NULL when out of memory.
4961 */
4962 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004963getroom(spin, len, align)
4964 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004965 size_t len; /* length needed */
4966 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004967{
4968 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004969 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004970
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004971 if (align && bl != NULL)
4972 /* Round size up for alignment. On some systems structures need to be
4973 * aligned to the size of a pointer (e.g., SPARC). */
4974 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
4975 & ~(sizeof(char *) - 1);
4976
Bram Moolenaar51485f02005-06-04 21:55:20 +00004977 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
4978 {
4979 /* Allocate a block of memory. This is not freed until much later. */
4980 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
4981 if (bl == NULL)
4982 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004983 bl->sb_next = spin->si_blocks;
4984 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004985 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004986 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004987 }
4988
4989 p = bl->sb_data + bl->sb_used;
4990 bl->sb_used += len;
4991
4992 return p;
4993}
4994
4995/*
4996 * Make a copy of a string into memory allocated with getroom().
4997 */
4998 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004999getroom_save(spin, s)
5000 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005001 char_u *s;
5002{
5003 char_u *sc;
5004
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005005 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005006 if (sc != NULL)
5007 STRCPY(sc, s);
5008 return sc;
5009}
5010
5011
5012/*
5013 * Free the list of allocated sblock_T.
5014 */
5015 static void
5016free_blocks(bl)
5017 sblock_T *bl;
5018{
5019 sblock_T *next;
5020
5021 while (bl != NULL)
5022 {
5023 next = bl->sb_next;
5024 vim_free(bl);
5025 bl = next;
5026 }
5027}
5028
5029/*
5030 * Allocate the root of a word tree.
5031 */
5032 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005033wordtree_alloc(spin)
5034 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005035{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005036 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005037}
5038
5039/*
5040 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005041 * Always store it in the case-folded tree. For a keep-case word this is
5042 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
5043 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00005044 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005045 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
5046 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00005047 */
5048 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005049store_word(spin, word, flags, region, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005050 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005051 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005052 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00005053 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005054 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005055{
5056 int len = STRLEN(word);
5057 int ct = captype(word, word + len);
5058 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005059 int res = OK;
5060 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005061
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005062 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005063 for (p = pfxlist; res == OK; ++p)
5064 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005065 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
5066 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005067 if (p == NULL || *p == NUL)
5068 break;
5069 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00005070 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005071
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005072 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00005073 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005074 for (p = pfxlist; res == OK; ++p)
5075 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005076 res = tree_add_word(spin, word, spin->si_keeproot, flags,
5077 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005078 if (p == NULL || *p == NUL)
5079 break;
5080 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00005081 ++spin->si_keepwcount;
5082 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005083 return res;
5084}
5085
5086/*
5087 * Add word "word" to a word tree at "root".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005088 * When "flags" < 0 we are adding to the prefix tree where flags is used for
5089 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005090 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005091 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005092 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005093tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005094 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005095 char_u *word;
5096 wordnode_T *root;
5097 int flags;
5098 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005099 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005100{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005101 wordnode_T *node = root;
5102 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005103 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005104 wordnode_T **prev = NULL;
5105 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005106
Bram Moolenaar51485f02005-06-04 21:55:20 +00005107 /* Add each byte of the word to the tree, including the NUL at the end. */
5108 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005109 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005110 /* When there is more than one reference to this node we need to make
5111 * a copy, so that we can modify it. Copy the whole list of siblings
5112 * (we don't optimize for a partly shared list of siblings). */
5113 if (node != NULL && node->wn_refs > 1)
5114 {
5115 --node->wn_refs;
5116 copyprev = prev;
5117 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
5118 {
5119 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005120 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005121 if (np == NULL)
5122 return FAIL;
5123 np->wn_child = copyp->wn_child;
5124 if (np->wn_child != NULL)
5125 ++np->wn_child->wn_refs; /* child gets extra ref */
5126 np->wn_byte = copyp->wn_byte;
5127 if (np->wn_byte == NUL)
5128 {
5129 np->wn_flags = copyp->wn_flags;
5130 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005131 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005132 }
5133
5134 /* Link the new node in the list, there will be one ref. */
5135 np->wn_refs = 1;
5136 *copyprev = np;
5137 copyprev = &np->wn_sibling;
5138
5139 /* Let "node" point to the head of the copied list. */
5140 if (copyp == node)
5141 node = np;
5142 }
5143 }
5144
Bram Moolenaar51485f02005-06-04 21:55:20 +00005145 /* Look for the sibling that has the same character. They are sorted
5146 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005147 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005148 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005149 while (node != NULL
5150 && (node->wn_byte < word[i]
5151 || (node->wn_byte == NUL
5152 && (flags < 0
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005153 ? node->wn_affixID < affixID
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005154 : node->wn_flags < (flags & WN_MASK)
5155 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005156 && node->wn_affixID < affixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005157 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005158 prev = &node->wn_sibling;
5159 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005160 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005161 if (node == NULL
5162 || node->wn_byte != word[i]
5163 || (word[i] == NUL
5164 && (flags < 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005165 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005166 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005167 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005168 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005169 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005170 if (np == NULL)
5171 return FAIL;
5172 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005173
5174 /* If "node" is NULL this is a new child or the end of the sibling
5175 * list: ref count is one. Otherwise use ref count of sibling and
5176 * make ref count of sibling one (matters when inserting in front
5177 * of the list of siblings). */
5178 if (node == NULL)
5179 np->wn_refs = 1;
5180 else
5181 {
5182 np->wn_refs = node->wn_refs;
5183 node->wn_refs = 1;
5184 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005185 *prev = np;
5186 np->wn_sibling = node;
5187 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005188 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005189
Bram Moolenaar51485f02005-06-04 21:55:20 +00005190 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005191 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005192 node->wn_flags = flags;
5193 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005194 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005195 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00005196 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005197 prev = &node->wn_child;
5198 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005199 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005200#ifdef SPELL_PRINTTREE
5201 smsg("Added \"%s\"", word);
5202 spell_print_tree(root->wn_sibling);
5203#endif
5204
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005205 /* count nr of words added since last message */
5206 ++spin->si_msg_count;
5207
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005208 if (spin->si_compress_cnt > 1)
5209 {
5210 if (--spin->si_compress_cnt == 1)
5211 /* Did enough words to lower the block count limit. */
5212 spin->si_blocks_cnt += SPELL_COMPRESS_INC;
5213 }
5214
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005215 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005216 * When we have allocated lots of memory we need to compress the word tree
5217 * to free up some room. But compression is slow, and we might actually
5218 * need that room, thus only compress in the following situations:
5219 * 1. When not compressed before (si_compress_cnt == 0): when using
5220 * SPELL_COMPRESS_CNT blocks.
5221 * 2. When compressed before and used SPELL_COMPRESS_INC blocks before
5222 * adding SPELL_COMPRESS_ADDED words (si_compress_cnt > 1).
5223 * 3. When compressed before, added SPELL_COMPRESS_ADDED words
5224 * (si_compress_cnt == 1) and the number of free nodes drops below the
5225 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005226 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005227#ifndef SPELL_PRINTTREE
5228 if (spin->si_compress_cnt == 1
5229 ? spin->si_free_count < MAXWLEN
5230 : spin->si_blocks_cnt >= SPELL_COMPRESS_CNT)
5231#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005232 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005233 /* Decrement the block counter. The effect is that we compress again
5234 * when the freed up room has been used and another SPELL_COMPRESS_INC
5235 * blocks have been allocated. Unless SPELL_COMPRESS_ADDED words have
5236 * been added, then the limit is put back again. */
5237 spin->si_blocks_cnt -= SPELL_COMPRESS_INC;
5238 spin->si_compress_cnt = SPELL_COMPRESS_ADDED;
5239
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005240 if (spin->si_verbose)
5241 {
5242 msg_start();
5243 msg_puts((char_u *)_(msg_compressing));
5244 msg_clr_eos();
5245 msg_didout = FALSE;
5246 msg_col = 0;
5247 out_flush();
5248 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005249
5250 /* Compress both trees. Either they both have many nodes, which makes
5251 * compression useful, or one of them is small, which means
5252 * compression goes fast. */
5253 wordtree_compress(spin, spin->si_foldroot);
5254 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005255 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005256
5257 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005258}
5259
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005260/*
5261 * Get a wordnode_T, either from the list of previously freed nodes or
5262 * allocate a new one.
5263 */
5264 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005265get_wordnode(spin)
5266 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005267{
5268 wordnode_T *n;
5269
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005270 if (spin->si_first_free == NULL)
5271 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005272 else
5273 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005274 n = spin->si_first_free;
5275 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005276 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005277 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005278 }
5279#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005280 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005281#endif
5282 return n;
5283}
5284
5285/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005286 * Decrement the reference count on a node (which is the head of a list of
5287 * siblings). If the reference count becomes zero free the node and its
5288 * siblings.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005289 */
5290 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005291deref_wordnode(spin, node)
5292 spellinfo_T *spin;
5293 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005294{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005295 wordnode_T *np;
5296
5297 if (--node->wn_refs == 0)
5298 for (np = node; np != NULL; np = np->wn_sibling)
5299 {
5300 if (np->wn_child != NULL)
5301 deref_wordnode(spin, np->wn_child);
5302 free_wordnode(spin, np);
5303 }
5304}
5305
5306/*
5307 * Free a wordnode_T for re-use later.
5308 * Only the "wn_child" field becomes invalid.
5309 */
5310 static void
5311free_wordnode(spin, n)
5312 spellinfo_T *spin;
5313 wordnode_T *n;
5314{
5315 n->wn_child = spin->si_first_free;
5316 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005317 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005318}
5319
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005320/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005321 * Compress a tree: find tails that are identical and can be shared.
5322 */
5323 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005324wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005325 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005326 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005327{
5328 hashtab_T ht;
5329 int n;
5330 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005331 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005332
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005333 /* Skip the root itself, it's not actually used. The first sibling is the
5334 * start of the tree. */
5335 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005336 {
5337 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005338 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005339
5340#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00005341 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005342#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00005343 {
5344 if (!spin->si_verbose)
5345 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005346 if (tot > 1000000)
5347 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005348 else if (tot == 0)
5349 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005350 else
5351 perc = (tot - n) * 100 / tot;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005352 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005353 n, tot, perc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005354 if (p_verbose > 2)
5355 verbose_leave();
5356 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005357#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005358 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005359#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00005360 hash_clear(&ht);
5361 }
5362}
5363
5364/*
5365 * Compress a node, its siblings and its children, depth first.
5366 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005367 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005368 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005369node_compress(spin, node, ht, tot)
5370 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005371 wordnode_T *node;
5372 hashtab_T *ht;
5373 int *tot; /* total count of nodes before compressing,
5374 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005375{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005376 wordnode_T *np;
5377 wordnode_T *tp;
5378 wordnode_T *child;
5379 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005380 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005381 int len = 0;
5382 unsigned nr, n;
5383 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005384
Bram Moolenaar51485f02005-06-04 21:55:20 +00005385 /*
5386 * Go through the list of siblings. Compress each child and then try
5387 * finding an identical child to replace it.
5388 * Note that with "child" we mean not just the node that is pointed to,
5389 * but the whole list of siblings, of which the node is the first.
5390 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005391 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005392 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005393 ++len;
5394 if ((child = np->wn_child) != NULL)
5395 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005396 /* Compress the child. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005397 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005398
5399 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005400 hash = hash_hash(child->wn_u1.hashkey);
5401 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005402 tp = NULL;
5403 if (!HASHITEM_EMPTY(hi))
5404 {
5405 /* There are children with an identical hash value. Now check
5406 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005407 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005408 if (node_equal(child, tp))
5409 {
5410 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005411 * current one. This means the current child and all
5412 * its siblings is unlinked from the tree. */
5413 ++tp->wn_refs;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005414 deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005415 np->wn_child = tp;
5416 ++compressed;
5417 break;
5418 }
5419 if (tp == NULL)
5420 {
5421 /* No other child with this hash value equals the child of
5422 * the node, add it to the linked list after the first
5423 * item. */
5424 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005425 child->wn_u2.next = tp->wn_u2.next;
5426 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005427 }
5428 }
5429 else
5430 /* No other child has this hash value, add it to the
5431 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005432 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005433 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005434 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005435 *tot += len;
5436
5437 /*
5438 * Make a hash key for the node and its siblings, so that we can quickly
5439 * find a lookalike node. This must be done after compressing the sibling
5440 * list, otherwise the hash key would become invalid by the compression.
5441 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005442 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005443 nr = 0;
5444 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005445 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005446 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005447 /* end node: use wn_flags, wn_region and wn_affixID */
5448 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005449 else
5450 /* byte node: use the byte value and the child pointer */
5451 n = np->wn_byte + ((long_u)np->wn_child << 8);
5452 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005453 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005454
5455 /* Avoid NUL bytes, it terminates the hash key. */
5456 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005457 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005458 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005459 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005460 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005461 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005462 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005463 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
5464 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005465
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005466 /* Check for CTRL-C pressed now and then. */
5467 fast_breakcheck();
5468
Bram Moolenaar51485f02005-06-04 21:55:20 +00005469 return compressed;
5470}
5471
5472/*
5473 * Return TRUE when two nodes have identical siblings and children.
5474 */
5475 static int
5476node_equal(n1, n2)
5477 wordnode_T *n1;
5478 wordnode_T *n2;
5479{
5480 wordnode_T *p1;
5481 wordnode_T *p2;
5482
5483 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
5484 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
5485 if (p1->wn_byte != p2->wn_byte
5486 || (p1->wn_byte == NUL
5487 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005488 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005489 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005490 : (p1->wn_child != p2->wn_child)))
5491 break;
5492
5493 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005494}
5495
5496/*
5497 * Write a number to file "fd", MSB first, in "len" bytes.
5498 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005499 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005500put_bytes(fd, nr, len)
5501 FILE *fd;
5502 long_u nr;
5503 int len;
5504{
5505 int i;
5506
5507 for (i = len - 1; i >= 0; --i)
5508 putc((int)(nr >> (i * 8)), fd);
5509}
5510
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005511static int
5512#ifdef __BORLANDC__
5513_RTLENTRYF
5514#endif
5515rep_compare __ARGS((const void *s1, const void *s2));
5516
5517/*
5518 * Function given to qsort() to sort the REP items on "from" string.
5519 */
5520 static int
5521#ifdef __BORLANDC__
5522_RTLENTRYF
5523#endif
5524rep_compare(s1, s2)
5525 const void *s1;
5526 const void *s2;
5527{
5528 fromto_T *p1 = (fromto_T *)s1;
5529 fromto_T *p2 = (fromto_T *)s2;
5530
5531 return STRCMP(p1->ft_from, p2->ft_from);
5532}
5533
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005534/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005535 * Write the Vim spell file "fname".
5536 */
5537 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005538write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005539 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005540 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005541{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005542 FILE *fd;
5543 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005544 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005545 wordnode_T *tree;
5546 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005547 int i;
5548 int l;
5549 garray_T *gap;
5550 fromto_T *ftp;
5551 char_u *p;
5552 int rr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005553
Bram Moolenaarb765d632005-06-07 21:00:02 +00005554 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00005555 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005556 {
5557 EMSG2(_(e_notopen), fname);
5558 return;
5559 }
5560
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005561 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005562 * <charflagslen> <charflags>
5563 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005564 * <midwordlen> <midword>
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005565 * <compoundlen> <compoundtype> <compoundinfo>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005566 * <prefcondcnt> <prefcond> ... */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005567
5568 /* <fileID> */
5569 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
5570 EMSG(_(e_write));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005571
5572 /* write the region names if there is more than one */
Bram Moolenaar3982c542005-06-08 21:56:31 +00005573 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005574 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00005575 putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */
5576 fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2),
5577 (size_t)1, fd);
5578 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005579 }
5580 else
5581 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005582 putc(0, fd);
5583 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005584 }
5585
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005586 /*
5587 * Write the table with character flags and table for case folding.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005588 * <charflagslen> <charflags> <fcharlen> <fchars>
5589 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005590 * 'encoding'.
5591 * Also skip this for an .add.spl file, the main spell file must contain
5592 * the table (avoids that it conflicts). File is shorter too.
5593 */
5594 if (spin->si_ascii || spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005595 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005596 putc(0, fd);
5597 putc(0, fd);
5598 putc(0, fd);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005599 }
5600 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00005601 write_spell_chartab(fd);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005602
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005603
5604 if (spin->si_midword == NULL)
5605 put_bytes(fd, 0L, 2); /* <midwordlen> */
5606 else
5607 {
5608 i = STRLEN(spin->si_midword);
5609 put_bytes(fd, (long_u)i, 2); /* <midwordlen> */
5610 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
5611 }
5612
5613
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005614 /* Write the compound info. */
5615 if (spin->si_compflags == NULL)
5616 put_bytes(fd, 0L, 2); /* <compoundlen> */
5617 else
5618 {
5619 l = STRLEN(spin->si_compflags);
5620 put_bytes(fd, (long_u)(l + 2), 2); /* <compoundlen> */
5621 putc(1, fd); /* <compoundtype> */
5622 putc(spin->si_compminlen, fd); /* <comp1minlen> */
5623 fwrite(spin->si_compflags, (size_t)l, (size_t)1, fd);
5624 /* <comp1flags> */
5625 }
5626
5627
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005628 /* Write the prefix conditions. */
5629 write_spell_prefcond(fd, &spin->si_prefcond);
5630
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005631 /* <SUGGEST> : <repcount> <rep> ...
5632 * <salflags> <salcount> <sal> ...
5633 * <maplen> <mapstr> */
5634
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005635 /* Sort the REP items. */
5636 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
5637 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005638
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005639 /* round 1: REP items
5640 * round 2: SAL items (unless SOFO is used) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005641 for (round = 1; round <= 2; ++round)
5642 {
5643 if (round == 1)
5644 gap = &spin->si_rep;
5645 else
5646 {
5647 gap = &spin->si_sal;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005648
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005649 i = 0;
5650 if (spin->si_followup)
5651 i |= SAL_F0LLOWUP;
5652 if (spin->si_collapse)
5653 i |= SAL_COLLAPSE;
5654 if (spin->si_rem_accents)
5655 i |= SAL_REM_ACCENTS;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005656 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
5657 i |= SAL_SOFO;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005658 putc(i, fd); /* <salflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005659 if (i & SAL_SOFO)
5660 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005661 }
5662
5663 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
5664 for (i = 0; i < gap->ga_len; ++i)
5665 {
5666 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
5667 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
5668 ftp = &((fromto_T *)gap->ga_data)[i];
5669 for (rr = 1; rr <= 2; ++rr)
5670 {
5671 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
5672 l = STRLEN(p);
5673 putc(l, fd);
5674 fwrite(p, l, (size_t)1, fd);
5675 }
5676 }
5677 }
5678
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005679 /* SOFOFROM and SOFOTO */
5680 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
5681 {
5682 put_bytes(fd, 1L, 2); /* <salcount> */
5683
5684 l = STRLEN(spin->si_sofofr);
5685 put_bytes(fd, (long_u)l, 2); /* <salfromlen> */
5686 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <salfrom> */
5687
5688 l = STRLEN(spin->si_sofoto);
5689 put_bytes(fd, (long_u)l, 2); /* <saltolen> */
5690 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <salto> */
5691 }
5692
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005693 put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */
5694 if (spin->si_map.ga_len > 0) /* <mapstr> */
5695 fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len,
5696 (size_t)1, fd);
Bram Moolenaar50cde822005-06-05 21:54:54 +00005697
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005698 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005699 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005700 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005701 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005702 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005703 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005704 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005705 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005706 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005707 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005708 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005709 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005710
Bram Moolenaar0c405862005-06-22 22:26:26 +00005711 /* Clear the index and wnode fields in the tree. */
5712 clear_node(tree);
5713
Bram Moolenaar51485f02005-06-04 21:55:20 +00005714 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00005715 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00005716 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005717 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005718
Bram Moolenaar51485f02005-06-04 21:55:20 +00005719 /* number of nodes in 4 bytes */
5720 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00005721 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005722
Bram Moolenaar51485f02005-06-04 21:55:20 +00005723 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005724 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005725 }
5726
Bram Moolenaar51485f02005-06-04 21:55:20 +00005727 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005728}
5729
5730/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00005731 * Clear the index and wnode fields of "node", it siblings and its
5732 * children. This is needed because they are a union with other items to save
5733 * space.
5734 */
5735 static void
5736clear_node(node)
5737 wordnode_T *node;
5738{
5739 wordnode_T *np;
5740
5741 if (node != NULL)
5742 for (np = node; np != NULL; np = np->wn_sibling)
5743 {
5744 np->wn_u1.index = 0;
5745 np->wn_u2.wnode = NULL;
5746
5747 if (np->wn_byte != NUL)
5748 clear_node(np->wn_child);
5749 }
5750}
5751
5752
5753/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005754 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005755 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00005756 * This first writes the list of possible bytes (siblings). Then for each
5757 * byte recursively write the children.
5758 *
5759 * NOTE: The code here must match the code in read_tree(), since assumptions
5760 * are made about the indexes (so that we don't have to write them in the
5761 * file).
5762 *
5763 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005764 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005765 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00005766put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005767 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005768 wordnode_T *node;
5769 int index;
5770 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005771 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005772{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005773 int newindex = index;
5774 int siblingcount = 0;
5775 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005776 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005777
Bram Moolenaar51485f02005-06-04 21:55:20 +00005778 /* If "node" is zero the tree is empty. */
5779 if (node == NULL)
5780 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005781
Bram Moolenaar51485f02005-06-04 21:55:20 +00005782 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005783 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005784
5785 /* Count the number of siblings. */
5786 for (np = node; np != NULL; np = np->wn_sibling)
5787 ++siblingcount;
5788
5789 /* Write the sibling count. */
5790 if (fd != NULL)
5791 putc(siblingcount, fd); /* <siblingcount> */
5792
5793 /* Write each sibling byte and optionally extra info. */
5794 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005795 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005796 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005797 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005798 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005799 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005800 /* For a NUL byte (end of word) write the flags etc. */
5801 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005802 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005803 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005804 * associated condition nr (stored in wn_region). The
5805 * byte value is misused to store the "rare" and "not
5806 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005807 if (np->wn_flags == (short_u)PFX_FLAGS)
5808 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005809 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005810 {
5811 putc(BY_FLAGS, fd); /* <byte> */
5812 putc(np->wn_flags, fd); /* <pflags> */
5813 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005814 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005815 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005816 }
5817 else
5818 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005819 /* For word trees we write the flag/region items. */
5820 flags = np->wn_flags;
5821 if (regionmask != 0 && np->wn_region != regionmask)
5822 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005823 if (np->wn_affixID != 0)
5824 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005825 if (flags == 0)
5826 {
5827 /* word without flags or region */
5828 putc(BY_NOFLAGS, fd); /* <byte> */
5829 }
5830 else
5831 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005832 if (np->wn_flags >= 0x100)
5833 {
5834 putc(BY_FLAGS2, fd); /* <byte> */
5835 putc(flags, fd); /* <flags> */
5836 putc((unsigned)flags >> 8, fd); /* <flags2> */
5837 }
5838 else
5839 {
5840 putc(BY_FLAGS, fd); /* <byte> */
5841 putc(flags, fd); /* <flags> */
5842 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005843 if (flags & WF_REGION)
5844 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005845 if (flags & WF_AFX)
5846 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005847 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005848 }
5849 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005850 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005851 else
5852 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005853 if (np->wn_child->wn_u1.index != 0
5854 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005855 {
5856 /* The child is written elsewhere, write the reference. */
5857 if (fd != NULL)
5858 {
5859 putc(BY_INDEX, fd); /* <byte> */
5860 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005861 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005862 }
5863 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00005864 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005865 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005866 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005867
Bram Moolenaar51485f02005-06-04 21:55:20 +00005868 if (fd != NULL)
5869 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
5870 {
5871 EMSG(_(e_write));
5872 return 0;
5873 }
5874 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005875 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005876
5877 /* Space used in the array when reading: one for each sibling and one for
5878 * the count. */
5879 newindex += siblingcount + 1;
5880
5881 /* Recursively dump the children of each sibling. */
5882 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005883 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
5884 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005885 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005886
5887 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005888}
5889
5890
5891/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00005892 * ":mkspell [-ascii] outfile infile ..."
5893 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005894 */
5895 void
5896ex_mkspell(eap)
5897 exarg_T *eap;
5898{
5899 int fcount;
5900 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005901 char_u *arg = eap->arg;
5902 int ascii = FALSE;
5903
5904 if (STRNCMP(arg, "-ascii", 6) == 0)
5905 {
5906 ascii = TRUE;
5907 arg = skipwhite(arg + 6);
5908 }
5909
5910 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
5911 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
5912 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005913 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005914 FreeWild(fcount, fnames);
5915 }
5916}
5917
5918/*
5919 * Create a Vim spell file from one or more word lists.
5920 * "fnames[0]" is the output file name.
5921 * "fnames[fcount - 1]" is the last input file name.
5922 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
5923 * and ".spl" is appended to make the output file name.
5924 */
5925 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005926mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005927 int fcount;
5928 char_u **fnames;
5929 int ascii; /* -ascii argument given */
5930 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005931 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005932{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005933 char_u fname[MAXPATHL];
5934 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00005935 char_u **innames;
5936 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005937 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005938 int i;
5939 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005940 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005941 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005942 spellinfo_T spin;
5943
5944 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005945 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005946 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005947 spin.si_followup = TRUE;
5948 spin.si_rem_accents = TRUE;
5949 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
5950 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
5951 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005952 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005953
Bram Moolenaarb765d632005-06-07 21:00:02 +00005954 /* default: fnames[0] is output file, following are input files */
5955 innames = &fnames[1];
5956 incount = fcount - 1;
5957
5958 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005959 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005960 len = STRLEN(fnames[0]);
5961 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
5962 {
5963 /* For ":mkspell path/en.latin1.add" output file is
5964 * "path/en.latin1.add.spl". */
5965 innames = &fnames[0];
5966 incount = 1;
5967 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
5968 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005969 else if (fcount == 1)
5970 {
5971 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
5972 innames = &fnames[0];
5973 incount = 1;
5974 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
5975 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5976 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005977 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
5978 {
5979 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005980 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005981 }
5982 else
5983 /* Name should be language, make the file name from it. */
5984 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
5985 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5986
5987 /* Check for .ascii.spl. */
5988 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
5989 spin.si_ascii = TRUE;
5990
5991 /* Check for .add.spl. */
5992 if (strstr((char *)gettail(wfname), ".add.") != NULL)
5993 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005994 }
5995
Bram Moolenaarb765d632005-06-07 21:00:02 +00005996 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005997 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005998 else if (vim_strchr(gettail(wfname), '_') != NULL)
5999 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00006000 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006001 EMSG(_("E754: Only up to 8 regions supported"));
6002 else
6003 {
6004 /* Check for overwriting before doing things that may take a lot of
6005 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006006 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006007 {
6008 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00006009 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006010 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006011 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006012 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006013 EMSG2(_(e_isadir2), wfname);
6014 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006015 }
6016
6017 /*
6018 * Init the aff and dic pointers.
6019 * Get the region names if there are more than 2 arguments.
6020 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006021 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006022 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006023 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006024
Bram Moolenaar3982c542005-06-08 21:56:31 +00006025 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006026 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006027 len = STRLEN(innames[i]);
6028 if (STRLEN(gettail(innames[i])) < 5
6029 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006030 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006031 EMSG2(_("E755: Invalid region in %s"), innames[i]);
6032 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006033 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00006034 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
6035 spin.si_region_name[i * 2 + 1] =
6036 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006037 }
6038 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00006039 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006040
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006041 spin.si_foldroot = wordtree_alloc(&spin);
6042 spin.si_keeproot = wordtree_alloc(&spin);
6043 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006044 if (spin.si_foldroot == NULL
6045 || spin.si_keeproot == NULL
6046 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006047 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006048 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006049 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006050 }
6051
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006052 /* When not producing a .add.spl file clear the character table when
6053 * we encounter one in the .aff file. This means we dump the current
6054 * one in the .spl file if the .aff file doesn't define one. That's
6055 * better than guessing the contents, the table will match a
6056 * previously loaded spell file. */
6057 if (!spin.si_add)
6058 spin.si_clear_chartab = TRUE;
6059
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006060 /*
6061 * Read all the .aff and .dic files.
6062 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006063 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006064 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006065 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006066 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006067 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006068 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006069
Bram Moolenaarb765d632005-06-07 21:00:02 +00006070 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006071 if (mch_stat((char *)fname, &st) >= 0)
6072 {
6073 /* Read the .aff file. Will init "spin->si_conv" based on the
6074 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006075 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006076 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006077 error = TRUE;
6078 else
6079 {
6080 /* Read the .dic file and store the words in the trees. */
6081 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00006082 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006083 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006084 error = TRUE;
6085 }
6086 }
6087 else
6088 {
6089 /* No .aff file, try reading the file as a word list. Store
6090 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006091 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006092 error = TRUE;
6093 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006094
Bram Moolenaarb765d632005-06-07 21:00:02 +00006095#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006096 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006097 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006098#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006099 }
6100
Bram Moolenaar51485f02005-06-04 21:55:20 +00006101 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006102 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006103 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006104 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006105 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006106 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006107 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006108 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006109 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006110 MSG(_(msg_compressing));
Bram Moolenaarb765d632005-06-07 21:00:02 +00006111 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006112 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006113 verbose_leave();
6114 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006115 wordtree_compress(&spin, spin.si_foldroot);
6116 wordtree_compress(&spin, spin.si_keeproot);
6117 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006118 }
6119
Bram Moolenaar51485f02005-06-04 21:55:20 +00006120 if (!error)
6121 {
6122 /*
6123 * Write the info in the spell file.
6124 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006125 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006126 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006127 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006128 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006129 smsg((char_u *)_("Writing spell file %s ..."), wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006130 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006131 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006132 verbose_leave();
6133 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00006134
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006135 write_vim_spell(&spin, wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006136
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006137 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006138 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006139 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006140 verbose_enter();
6141 MSG(_("Done!"));
6142 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00006143 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006144 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006145 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006146 verbose_leave();
6147 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006148
Bram Moolenaarb765d632005-06-07 21:00:02 +00006149 /* If the file is loaded need to reload it. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006150 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006151 }
6152
6153 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006154 ga_clear(&spin.si_rep);
6155 ga_clear(&spin.si_sal);
6156 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006157 ga_clear(&spin.si_prefcond);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006158 vim_free(spin.si_midword);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006159 vim_free(spin.si_sofofr);
6160 vim_free(spin.si_sofoto);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006161
6162 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006163 for (i = 0; i < incount; ++i)
6164 if (afile[i] != NULL)
6165 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006166
6167 /* Free all the bits and pieces at once. */
6168 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006169 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006170}
6171
Bram Moolenaarb765d632005-06-07 21:00:02 +00006172
6173/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006174 * ":[count]spellgood {word}"
6175 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00006176 */
6177 void
6178ex_spell(eap)
6179 exarg_T *eap;
6180{
Bram Moolenaar7887d882005-07-01 22:33:52 +00006181 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006182 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006183}
6184
6185/*
6186 * Add "word[len]" to 'spellfile' as a good or bad word.
6187 */
6188 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006189spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006190 char_u *word;
6191 int len;
6192 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006193 int index; /* "zG" and "zW": zero, otherwise index in
6194 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006195{
6196 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006197 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006198 int new_spf = FALSE;
6199 struct stat st;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006200 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006201 char_u fnamebuf[MAXPATHL];
6202 char_u line[MAXWLEN * 2];
6203 long fpos, fpos_next = 0;
6204 int i;
6205 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006206
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006207 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006208 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006209 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00006210 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006211 int_wordlist = vim_tempname('s');
6212 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00006213 return;
6214 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006215 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006216 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006217 else
6218 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006219 /* If 'spellfile' isn't set figure out a good default value. */
6220 if (*curbuf->b_p_spf == NUL)
6221 {
6222 init_spellfile();
6223 new_spf = TRUE;
6224 }
6225
6226 if (*curbuf->b_p_spf == NUL)
6227 {
6228 EMSG(_("E764: 'spellfile' is not set"));
6229 return;
6230 }
6231
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006232 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
6233 {
6234 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
6235 if (i == index)
6236 break;
6237 if (*spf == NUL)
6238 {
6239 EMSGN(_("E765: 'spellfile' does not have %ld enties"), index);
6240 return;
6241 }
6242 }
6243
Bram Moolenaarb765d632005-06-07 21:00:02 +00006244 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006245 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006246 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
6247 buf = NULL;
6248 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00006249 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006250 EMSG(_(e_bufloaded));
6251 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006252 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00006253
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006254 fname = fnamebuf;
6255 }
6256
6257 if (bad)
6258 {
6259 /* When the word also appears as good word we need to remove that one,
6260 * since its flags sort before the one with WF_BANNED. */
6261 fd = mch_fopen((char *)fname, "r");
6262 if (fd != NULL)
6263 {
6264 while (!vim_fgets(line, MAXWLEN * 2, fd))
6265 {
6266 fpos = fpos_next;
6267 fpos_next = ftell(fd);
6268 if (STRNCMP(word, line, len) == 0
6269 && (line[len] == '/' || line[len] < ' '))
6270 {
6271 /* Found duplicate word. Remove it by writing a '#' at
6272 * the start of the line. Mixing reading and writing
6273 * doesn't work for all systems, close the file first. */
6274 fclose(fd);
6275 fd = mch_fopen((char *)fname, "r+");
6276 if (fd == NULL)
6277 break;
6278 if (fseek(fd, fpos, SEEK_SET) == 0)
6279 fputc('#', fd);
6280 fseek(fd, fpos_next, SEEK_SET);
6281 }
6282 }
6283 fclose(fd);
6284 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00006285 }
6286
6287 fd = mch_fopen((char *)fname, "a");
6288 if (fd == NULL && new_spf)
6289 {
6290 /* We just initialized the 'spellfile' option and can't open the file.
6291 * We may need to create the "spell" directory first. We already
6292 * checked the runtime directory is writable in init_spellfile(). */
6293 STRCPY(NameBuff, fname);
6294 *gettail_sep(NameBuff) = NUL;
6295 if (mch_stat((char *)NameBuff, &st) < 0)
6296 {
6297 /* The directory doesn't exist. Try creating it and opening the
6298 * file again. */
6299 vim_mkdir(NameBuff, 0755);
6300 fd = mch_fopen((char *)fname, "a");
6301 }
6302 }
6303
6304 if (fd == NULL)
6305 EMSG2(_(e_notopen), fname);
6306 else
6307 {
6308 if (bad)
6309 fprintf(fd, "%.*s/!\n", len, word);
6310 else
6311 fprintf(fd, "%.*s\n", len, word);
6312 fclose(fd);
6313
6314 /* Update the .add.spl file. */
6315 mkspell(1, &fname, FALSE, TRUE, TRUE);
6316
6317 /* If the .add file is edited somewhere, reload it. */
6318 if (buf != NULL)
6319 buf_reload(buf);
6320
6321 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006322 }
6323}
6324
6325/*
6326 * Initialize 'spellfile' for the current buffer.
6327 */
6328 static void
6329init_spellfile()
6330{
6331 char_u buf[MAXPATHL];
6332 int l;
6333 slang_T *sl;
6334 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006335 char_u *lend;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006336
6337 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
6338 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006339 /* Find the end of the language name. Exclude the region. */
6340 for (lend = curbuf->b_p_spl; *lend != NUL
6341 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
6342 ;
6343
6344 /* Loop over all entries in 'runtimepath'. Use the first one where we
6345 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006346 rtp = p_rtp;
6347 while (*rtp != NUL)
6348 {
6349 /* Copy the path from 'runtimepath' to buf[]. */
6350 copy_option_part(&rtp, buf, MAXPATHL, ",");
6351 if (filewritable(buf) == 2)
6352 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00006353 /* Use the first language name from 'spelllang' and the
6354 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006355 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
6356 l = STRLEN(buf);
6357 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00006358 "/spell/%.*s.%s.add",
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006359 (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00006360 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
6361 ? (char_u *)"ascii" : spell_enc());
6362 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
6363 break;
6364 }
6365 }
6366 }
6367}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006368
Bram Moolenaar51485f02005-06-04 21:55:20 +00006369
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006370/*
6371 * Init the chartab used for spelling for ASCII.
6372 * EBCDIC is not supported!
6373 */
6374 static void
6375clear_spell_chartab(sp)
6376 spelltab_T *sp;
6377{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006378 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006379
6380 /* Init everything to FALSE. */
6381 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
6382 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
6383 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006384 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006385 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006386 sp->st_upper[i] = i;
6387 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006388
6389 /* We include digits. A word shouldn't start with a digit, but handling
6390 * that is done separately. */
6391 for (i = '0'; i <= '9'; ++i)
6392 sp->st_isw[i] = TRUE;
6393 for (i = 'A'; i <= 'Z'; ++i)
6394 {
6395 sp->st_isw[i] = TRUE;
6396 sp->st_isu[i] = TRUE;
6397 sp->st_fold[i] = i + 0x20;
6398 }
6399 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006400 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006401 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006402 sp->st_upper[i] = i - 0x20;
6403 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006404}
6405
6406/*
6407 * Init the chartab used for spelling. Only depends on 'encoding'.
6408 * Called once while starting up and when 'encoding' changes.
6409 * The default is to use isalpha(), but the spell file should define the word
6410 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006411 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006412 */
6413 void
6414init_spell_chartab()
6415{
6416 int i;
6417
6418 did_set_spelltab = FALSE;
6419 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006420#ifdef FEAT_MBYTE
6421 if (enc_dbcs)
6422 {
6423 /* DBCS: assume double-wide characters are word characters. */
6424 for (i = 128; i <= 255; ++i)
6425 if (MB_BYTE2LEN(i) == 2)
6426 spelltab.st_isw[i] = TRUE;
6427 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006428 else if (enc_utf8)
6429 {
6430 for (i = 128; i < 256; ++i)
6431 {
6432 spelltab.st_isu[i] = utf_isupper(i);
6433 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
6434 spelltab.st_fold[i] = utf_fold(i);
6435 spelltab.st_upper[i] = utf_toupper(i);
6436 }
6437 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006438 else
6439#endif
6440 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006441 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006442 for (i = 128; i < 256; ++i)
6443 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006444 if (MB_ISUPPER(i))
6445 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006446 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006447 spelltab.st_isu[i] = TRUE;
6448 spelltab.st_fold[i] = MB_TOLOWER(i);
6449 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006450 else if (MB_ISLOWER(i))
6451 {
6452 spelltab.st_isw[i] = TRUE;
6453 spelltab.st_upper[i] = MB_TOUPPER(i);
6454 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006455 }
6456 }
6457}
6458
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006459static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
6460static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
6461
6462/*
6463 * Set the spell character tables from strings in the affix file.
6464 */
6465 static int
6466set_spell_chartab(fol, low, upp)
6467 char_u *fol;
6468 char_u *low;
6469 char_u *upp;
6470{
6471 /* We build the new tables here first, so that we can compare with the
6472 * previous one. */
6473 spelltab_T new_st;
6474 char_u *pf = fol, *pl = low, *pu = upp;
6475 int f, l, u;
6476
6477 clear_spell_chartab(&new_st);
6478
6479 while (*pf != NUL)
6480 {
6481 if (*pl == NUL || *pu == NUL)
6482 {
6483 EMSG(_(e_affform));
6484 return FAIL;
6485 }
6486#ifdef FEAT_MBYTE
6487 f = mb_ptr2char_adv(&pf);
6488 l = mb_ptr2char_adv(&pl);
6489 u = mb_ptr2char_adv(&pu);
6490#else
6491 f = *pf++;
6492 l = *pl++;
6493 u = *pu++;
6494#endif
6495 /* Every character that appears is a word character. */
6496 if (f < 256)
6497 new_st.st_isw[f] = TRUE;
6498 if (l < 256)
6499 new_st.st_isw[l] = TRUE;
6500 if (u < 256)
6501 new_st.st_isw[u] = TRUE;
6502
6503 /* if "LOW" and "FOL" are not the same the "LOW" char needs
6504 * case-folding */
6505 if (l < 256 && l != f)
6506 {
6507 if (f >= 256)
6508 {
6509 EMSG(_(e_affrange));
6510 return FAIL;
6511 }
6512 new_st.st_fold[l] = f;
6513 }
6514
6515 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006516 * case-folding, it's upper case and the "UPP" is the upper case of
6517 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006518 if (u < 256 && u != f)
6519 {
6520 if (f >= 256)
6521 {
6522 EMSG(_(e_affrange));
6523 return FAIL;
6524 }
6525 new_st.st_fold[u] = f;
6526 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006527 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006528 }
6529 }
6530
6531 if (*pl != NUL || *pu != NUL)
6532 {
6533 EMSG(_(e_affform));
6534 return FAIL;
6535 }
6536
6537 return set_spell_finish(&new_st);
6538}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006539
6540/*
6541 * Set the spell character tables from strings in the .spl file.
6542 */
6543 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006544set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006545 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006546 int cnt; /* length of "flags" */
6547 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006548{
6549 /* We build the new tables here first, so that we can compare with the
6550 * previous one. */
6551 spelltab_T new_st;
6552 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006553 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006554 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006555
6556 clear_spell_chartab(&new_st);
6557
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006558 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006559 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006560 if (i < cnt)
6561 {
6562 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
6563 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
6564 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006565
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006566 if (*p != NUL)
6567 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006568#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006569 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006570#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006571 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006572#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006573 new_st.st_fold[i + 128] = c;
6574 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
6575 new_st.st_upper[c] = i + 128;
6576 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006577 }
6578
6579 return set_spell_finish(&new_st);
6580}
6581
6582 static int
6583set_spell_finish(new_st)
6584 spelltab_T *new_st;
6585{
6586 int i;
6587
6588 if (did_set_spelltab)
6589 {
6590 /* check that it's the same table */
6591 for (i = 0; i < 256; ++i)
6592 {
6593 if (spelltab.st_isw[i] != new_st->st_isw[i]
6594 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006595 || spelltab.st_fold[i] != new_st->st_fold[i]
6596 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006597 {
6598 EMSG(_("E763: Word characters differ between spell files"));
6599 return FAIL;
6600 }
6601 }
6602 }
6603 else
6604 {
6605 /* copy the new spelltab into the one being used */
6606 spelltab = *new_st;
6607 did_set_spelltab = TRUE;
6608 }
6609
6610 return OK;
6611}
6612
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006613/*
Bram Moolenaarea408852005-06-25 22:49:46 +00006614 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006615 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00006616 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006617 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00006618 */
6619 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006620spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00006621 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006622 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00006623{
Bram Moolenaarea408852005-06-25 22:49:46 +00006624#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006625 char_u *s;
6626 int l;
6627 int c;
6628
6629 if (has_mbyte)
6630 {
6631 l = MB_BYTE2LEN(*p);
6632 s = p;
6633 if (l == 1)
6634 {
6635 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006636 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006637 {
6638 s = p + 1; /* skip a mid-word character */
6639 l = MB_BYTE2LEN(*s);
6640 }
6641 }
6642 else
6643 {
6644 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006645 if (c < 256 ? buf->b_spell_ismw[c]
6646 : (buf->b_spell_ismw_mb != NULL
6647 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006648 {
6649 s = p + l;
6650 l = MB_BYTE2LEN(*s);
6651 }
6652 }
6653
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006654 c = mb_ptr2char(s);
6655 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006656 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006657 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006658 }
Bram Moolenaarea408852005-06-25 22:49:46 +00006659#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006660
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006661 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
6662}
6663
6664/*
6665 * Return TRUE if "p" points to a word character.
6666 * Unlike spell_iswordp() this doesn't check for "midword" characters.
6667 */
6668 static int
6669spell_iswordp_nmw(p)
6670 char_u *p;
6671{
6672#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006673 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006674
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006675 if (has_mbyte)
6676 {
6677 c = mb_ptr2char(p);
6678 if (c > 255)
6679 return mb_get_class(p) >= 2;
6680 return spelltab.st_isw[c];
6681 }
6682#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006683 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00006684}
6685
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006686#ifdef FEAT_MBYTE
6687/*
6688 * Return TRUE if "p" points to a word character.
6689 * Wide version of spell_iswordp().
6690 */
6691 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006692spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006693 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006694 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006695{
6696 int *s;
6697
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006698 if (*p < 256 ? buf->b_spell_ismw[*p]
6699 : (buf->b_spell_ismw_mb != NULL
6700 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006701 s = p + 1;
6702 else
6703 s = p;
6704
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006705 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006706 {
6707 if (enc_utf8)
6708 return utf_class(*s) >= 2;
6709 if (enc_dbcs)
6710 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
6711 return 0;
6712 }
6713 return spelltab.st_isw[*s];
6714}
6715#endif
6716
Bram Moolenaarea408852005-06-25 22:49:46 +00006717/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006718 * Write the table with prefix conditions to the .spl file.
6719 */
6720 static void
6721write_spell_prefcond(fd, gap)
6722 FILE *fd;
6723 garray_T *gap;
6724{
6725 int i;
6726 char_u *p;
6727 int len;
6728
6729 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
6730
6731 for (i = 0; i < gap->ga_len; ++i)
6732 {
6733 /* <prefcond> : <condlen> <condstr> */
6734 p = ((char_u **)gap->ga_data)[i];
6735 if (p == NULL)
6736 fputc(0, fd);
6737 else
6738 {
6739 len = STRLEN(p);
6740 fputc(len, fd);
6741 fwrite(p, (size_t)len, (size_t)1, fd);
6742 }
6743 }
6744}
6745
6746/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006747 * Write the current tables into the .spl file.
6748 * This makes sure the same characters are recognized as word characters when
6749 * generating an when using a spell file.
6750 */
6751 static void
6752write_spell_chartab(fd)
6753 FILE *fd;
6754{
6755 char_u charbuf[256 * 4];
6756 int len = 0;
6757 int flags;
6758 int i;
6759
6760 fputc(128, fd); /* <charflagslen> */
6761 for (i = 128; i < 256; ++i)
6762 {
6763 flags = 0;
6764 if (spelltab.st_isw[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006765 flags |= CF_WORD;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006766 if (spelltab.st_isu[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006767 flags |= CF_UPPER;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006768 fputc(flags, fd); /* <charflags> */
6769
Bram Moolenaarb765d632005-06-07 21:00:02 +00006770#ifdef FEAT_MBYTE
6771 if (has_mbyte)
6772 len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
6773 else
6774#endif
6775 charbuf[len++] = spelltab.st_fold[i];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006776 }
6777
6778 put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
6779 fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
6780}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006781
6782/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006783 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
6784 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006785 * When using a multi-byte 'encoding' the length may change!
6786 * Returns FAIL when something wrong.
6787 */
6788 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006789spell_casefold(str, len, buf, buflen)
6790 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006791 int len;
6792 char_u *buf;
6793 int buflen;
6794{
6795 int i;
6796
6797 if (len >= buflen)
6798 {
6799 buf[0] = NUL;
6800 return FAIL; /* result will not fit */
6801 }
6802
6803#ifdef FEAT_MBYTE
6804 if (has_mbyte)
6805 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006806 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006807 char_u *p;
6808 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006809
6810 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006811 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006812 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006813 if (outi + MB_MAXBYTES > buflen)
6814 {
6815 buf[outi] = NUL;
6816 return FAIL;
6817 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006818 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006819 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006820 }
6821 buf[outi] = NUL;
6822 }
6823 else
6824#endif
6825 {
6826 /* Be quick for non-multibyte encodings. */
6827 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006828 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006829 buf[i] = NUL;
6830 }
6831
6832 return OK;
6833}
6834
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006835#define SPS_BEST 1
6836#define SPS_FAST 2
6837#define SPS_DOUBLE 4
6838
6839static int sps_flags = SPS_BEST;
6840
6841/*
6842 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
6843 * Sets "sps_flags".
6844 */
6845 int
6846spell_check_sps()
6847{
6848 char_u *p;
6849 char_u buf[MAXPATHL];
6850 int f;
6851
6852 sps_flags = 0;
6853
6854 for (p = p_sps; *p != NUL; )
6855 {
6856 copy_option_part(&p, buf, MAXPATHL, ",");
6857
6858 f = 0;
6859 if (STRCMP(buf, "best") == 0)
6860 f = SPS_BEST;
6861 else if (STRCMP(buf, "fast") == 0)
6862 f = SPS_FAST;
6863 else if (STRCMP(buf, "double") == 0)
6864 f = SPS_DOUBLE;
6865 else if (STRNCMP(buf, "expr:", 5) != 0
6866 && STRNCMP(buf, "file:", 5) != 0)
6867 f = -1;
6868
6869 if (f == -1 || (sps_flags != 0 && f != 0))
6870 {
6871 sps_flags = SPS_BEST;
6872 return FAIL;
6873 }
6874 if (f != 0)
6875 sps_flags = f;
6876 }
6877
6878 if (sps_flags == 0)
6879 sps_flags = SPS_BEST;
6880
6881 return OK;
6882}
6883
6884/* Remember what "z?" replaced. */
6885static char_u *repl_from = NULL;
6886static char_u *repl_to = NULL;
6887
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006888/*
6889 * "z?": Find badly spelled word under or after the cursor.
6890 * Give suggestions for the properly spelled word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006891 */
6892 void
6893spell_suggest()
6894{
6895 char_u *line;
6896 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006897 char_u wcopy[MAXWLEN + 2];
6898 char_u *p;
6899 int i;
6900 int c;
6901 suginfo_T sug;
6902 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006903 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006904 int need_cap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006905
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006906 /* Find the start of the badly spelled word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006907 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL
6908 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006909 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006910 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
6911 return;
6912
6913 /* No bad word or it starts after the cursor: use the word under the
6914 * cursor. */
6915 curwin->w_cursor = prev_cursor;
6916 line = ml_get_curline();
6917 p = line + curwin->w_cursor.col;
6918 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006919 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006920 mb_ptr_back(line, p);
6921 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006922 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006923 mb_ptr_adv(p);
6924
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006925 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006926 {
6927 beep_flush();
6928 return;
6929 }
6930 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006931 }
6932
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006933 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006934
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006935 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00006936 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006937
Bram Moolenaar8b59de92005-08-11 19:59:29 +00006938 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006939
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006940 /* Get the list of suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006941 spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2,
6942 TRUE, need_cap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006943
6944 if (sug.su_ga.ga_len == 0)
6945 MSG(_("Sorry, no suggestions"));
6946 else
6947 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006948 vim_free(repl_from);
6949 repl_from = NULL;
6950 vim_free(repl_to);
6951 repl_to = NULL;
6952
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006953#ifdef FEAT_RIGHTLEFT
6954 /* When 'rightleft' is set the list is drawn right-left. */
6955 cmdmsg_rl = curwin->w_p_rl;
6956 if (cmdmsg_rl)
6957 msg_col = Columns - 1;
6958#endif
6959
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006960 /* List the suggestions. */
6961 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006962 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006963 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
6964 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006965#ifdef FEAT_RIGHTLEFT
6966 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
6967 {
6968 /* And now the rabbit from the high hat: Avoid showing the
6969 * untranslated message rightleft. */
6970 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
6971 sug.su_badlen, sug.su_badptr);
6972 }
6973#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006974 msg_puts(IObuff);
6975 msg_clr_eos();
6976 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00006977
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006978 msg_scroll = TRUE;
6979 for (i = 0; i < sug.su_ga.ga_len; ++i)
6980 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006981 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006982
6983 /* The suggested word may replace only part of the bad word, add
6984 * the not replaced part. */
6985 STRCPY(wcopy, stp->st_word);
6986 if (sug.su_badlen > stp->st_orglen)
6987 vim_strncpy(wcopy + STRLEN(wcopy),
6988 sug.su_badptr + stp->st_orglen,
6989 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006990 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
6991#ifdef FEAT_RIGHTLEFT
6992 if (cmdmsg_rl)
6993 rl_mirror(IObuff);
6994#endif
6995 msg_puts(IObuff);
6996
6997 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006998 msg_puts(IObuff);
6999
7000 /* The word may replace more than "su_badlen". */
7001 if (sug.su_badlen < stp->st_orglen)
7002 {
7003 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
7004 stp->st_orglen, sug.su_badptr);
7005 msg_puts(IObuff);
7006 }
7007
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007008 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007009 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00007010 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007011 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007012 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007013 stp->st_salscore ? "s " : "",
7014 stp->st_score, stp->st_altscore);
7015 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007016 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00007017 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007018#ifdef FEAT_RIGHTLEFT
7019 if (cmdmsg_rl)
7020 /* Mirror the numbers, but keep the leading space. */
7021 rl_mirror(IObuff + 1);
7022#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00007023 msg_advance(30);
7024 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007025 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007026 msg_putchar('\n');
7027 }
7028
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007029#ifdef FEAT_RIGHTLEFT
7030 cmdmsg_rl = FALSE;
7031 msg_col = 0;
7032#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007033 /* Ask for choice. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007034 i = prompt_for_number(&mouse_used);
7035 if (mouse_used)
7036 i -= lines_left;
7037
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007038 if (i > 0 && i <= sug.su_ga.ga_len && u_save_cursor() == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007039 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007040 /* Save the from and to text for :spellrepall. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007041 stp = &SUG(sug.su_ga, i - 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007042 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
7043 repl_to = vim_strsave(stp->st_word);
7044
7045 /* Replace the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007046 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
7047 if (p != NULL)
7048 {
7049 c = sug.su_badptr - line;
7050 mch_memmove(p, line, c);
7051 STRCPY(p + c, stp->st_word);
7052 STRCAT(p, sug.su_badptr + stp->st_orglen);
7053 ml_replace(curwin->w_cursor.lnum, p, FALSE);
7054 curwin->w_cursor.col = c;
7055 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007056
7057 /* For redo we use a change-word command. */
7058 ResetRedobuff();
7059 AppendToRedobuff((char_u *)"ciw");
7060 AppendToRedobuff(stp->st_word);
7061 AppendCharToRedobuff(ESC);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007062 }
7063 }
7064 else
7065 curwin->w_cursor = prev_cursor;
7066 }
7067
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007068 spell_find_cleanup(&sug);
7069}
7070
7071/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007072 * Check if the word at line "lnum" column "col" is required to start with a
7073 * capital. This uses 'spellcapcheck' of the current buffer.
7074 */
7075 static int
7076check_need_cap(lnum, col)
7077 linenr_T lnum;
7078 colnr_T col;
7079{
7080 int need_cap = FALSE;
7081 char_u *line;
7082 char_u *line_copy = NULL;
7083 char_u *p;
7084 colnr_T endcol;
7085 regmatch_T regmatch;
7086
7087 if (curbuf->b_cap_prog == NULL)
7088 return FALSE;
7089
7090 line = ml_get_curline();
7091 endcol = 0;
7092 if ((int)(skipwhite(line) - line) >= (int)col)
7093 {
7094 /* At start of line, check if previous line is empty or sentence
7095 * ends there. */
7096 if (lnum == 1)
7097 need_cap = TRUE;
7098 else
7099 {
7100 line = ml_get(lnum - 1);
7101 if (*skipwhite(line) == NUL)
7102 need_cap = TRUE;
7103 else
7104 {
7105 /* Append a space in place of the line break. */
7106 line_copy = concat_str(line, (char_u *)" ");
7107 line = line_copy;
7108 endcol = STRLEN(line);
7109 }
7110 }
7111 }
7112 else
7113 endcol = col;
7114
7115 if (endcol > 0)
7116 {
7117 /* Check if sentence ends before the bad word. */
7118 regmatch.regprog = curbuf->b_cap_prog;
7119 regmatch.rm_ic = FALSE;
7120 p = line + endcol;
7121 for (;;)
7122 {
7123 mb_ptr_back(line, p);
7124 if (p == line || spell_iswordp_nmw(p))
7125 break;
7126 if (vim_regexec(&regmatch, p, 0)
7127 && regmatch.endp[0] == line + endcol)
7128 {
7129 need_cap = TRUE;
7130 break;
7131 }
7132 }
7133 }
7134
7135 vim_free(line_copy);
7136
7137 return need_cap;
7138}
7139
7140
7141/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007142 * ":spellrepall"
7143 */
7144/*ARGSUSED*/
7145 void
7146ex_spellrepall(eap)
7147 exarg_T *eap;
7148{
7149 pos_T pos = curwin->w_cursor;
7150 char_u *frompat;
7151 int addlen;
7152 char_u *line;
7153 char_u *p;
7154 int didone = FALSE;
7155 int save_ws = p_ws;
7156
7157 if (repl_from == NULL || repl_to == NULL)
7158 {
7159 EMSG(_("E752: No previous spell replacement"));
7160 return;
7161 }
7162 addlen = STRLEN(repl_to) - STRLEN(repl_from);
7163
7164 frompat = alloc(STRLEN(repl_from) + 7);
7165 if (frompat == NULL)
7166 return;
7167 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
7168 p_ws = FALSE;
7169
7170 curwin->w_cursor.lnum = 0;
7171 while (!got_int)
7172 {
7173 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
7174 || u_save_cursor() == FAIL)
7175 break;
7176
7177 /* Only replace when the right word isn't there yet. This happens
7178 * when changing "etc" to "etc.". */
7179 line = ml_get_curline();
7180 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
7181 repl_to, STRLEN(repl_to)) != 0)
7182 {
7183 p = alloc(STRLEN(line) + addlen + 1);
7184 if (p == NULL)
7185 break;
7186 mch_memmove(p, line, curwin->w_cursor.col);
7187 STRCPY(p + curwin->w_cursor.col, repl_to);
7188 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
7189 ml_replace(curwin->w_cursor.lnum, p, FALSE);
7190 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7191 didone = TRUE;
7192 }
7193 curwin->w_cursor.col += STRLEN(repl_to);
7194 }
7195
7196 p_ws = save_ws;
7197 curwin->w_cursor = pos;
7198 vim_free(frompat);
7199
7200 if (!didone)
7201 EMSG2(_("E753: Not found: %s"), repl_from);
7202}
7203
7204/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007205 * Find spell suggestions for "word". Return them in the growarray "*gap" as
7206 * a list of allocated strings.
7207 */
7208 void
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007209spell_suggest_list(gap, word, maxcount, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007210 garray_T *gap;
7211 char_u *word;
7212 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007213 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007214{
7215 suginfo_T sug;
7216 int i;
7217 suggest_T *stp;
7218 char_u *wcopy;
7219
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007220 spell_find_suggest(word, &sug, maxcount, FALSE, need_cap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007221
7222 /* Make room in "gap". */
7223 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
7224 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
7225 return;
7226
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007227 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007228 {
7229 stp = &SUG(sug.su_ga, i);
7230
7231 /* The suggested word may replace only part of "word", add the not
7232 * replaced part. */
7233 wcopy = alloc(STRLEN(stp->st_word)
7234 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
7235 if (wcopy == NULL)
7236 break;
7237 STRCPY(wcopy, stp->st_word);
7238 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
7239 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
7240 }
7241
7242 spell_find_cleanup(&sug);
7243}
7244
7245/*
7246 * Find spell suggestions for the word at the start of "badptr".
7247 * Return the suggestions in "su->su_ga".
7248 * The maximum number of suggestions is "maxcount".
7249 * Note: does use info for the current window.
7250 * This is based on the mechanisms of Aspell, but completely reimplemented.
7251 */
7252 static void
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007253spell_find_suggest(badptr, su, maxcount, banbadword, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007254 char_u *badptr;
7255 suginfo_T *su;
7256 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00007257 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007258 int need_cap; /* word should start with capital */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007259{
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007260 int attr = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007261 char_u buf[MAXPATHL];
7262 char_u *p;
7263 int do_combine = FALSE;
7264 char_u *sps_copy;
7265#ifdef FEAT_EVAL
7266 static int expr_busy = FALSE;
7267#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007268 int c;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007269
7270 /*
7271 * Set the info in "*su".
7272 */
7273 vim_memset(su, 0, sizeof(suginfo_T));
7274 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
7275 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00007276 if (*badptr == NUL)
7277 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007278 hash_init(&su->su_banned);
7279
7280 su->su_badptr = badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007281 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007282 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007283 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007284
7285 if (su->su_badlen >= MAXWLEN)
7286 su->su_badlen = MAXWLEN - 1; /* just in case */
7287 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
7288 (void)spell_casefold(su->su_badptr, su->su_badlen,
7289 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007290 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007291 su->su_badflags = badword_captype(su->su_badptr,
7292 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007293 if (need_cap)
7294 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007295
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007296 /* If the word is not capitalised and spell_check() doesn't consider the
7297 * word to be bad then it might need to be capitalised. Add a suggestion
7298 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00007299 c = PTR2CHAR(su->su_badptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007300 if (!SPELL_ISUPPER(c) && attr == 0)
7301 {
7302 make_case_word(su->su_badword, buf, WF_ONECAP);
7303 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
7304 0, TRUE);
7305 }
7306
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007307 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00007308 if (banbadword)
7309 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007310
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007311 /* Make a copy of 'spellsuggest', because the expression may change it. */
7312 sps_copy = vim_strsave(p_sps);
7313 if (sps_copy == NULL)
7314 return;
7315
7316 /* Loop over the items in 'spellsuggest'. */
7317 for (p = sps_copy; *p != NUL; )
7318 {
7319 copy_option_part(&p, buf, MAXPATHL, ",");
7320
7321 if (STRNCMP(buf, "expr:", 5) == 0)
7322 {
7323#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007324 /* Evaluate an expression. Skip this when called recursively,
7325 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007326 if (!expr_busy)
7327 {
7328 expr_busy = TRUE;
7329 spell_suggest_expr(su, buf + 5);
7330 expr_busy = FALSE;
7331 }
7332#endif
7333 }
7334 else if (STRNCMP(buf, "file:", 5) == 0)
7335 /* Use list of suggestions in a file. */
7336 spell_suggest_file(su, buf + 5);
7337 else
7338 {
7339 /* Use internal method. */
7340 spell_suggest_intern(su);
7341 if (sps_flags & SPS_DOUBLE)
7342 do_combine = TRUE;
7343 }
7344 }
7345
7346 vim_free(sps_copy);
7347
7348 if (do_combine)
7349 /* Combine the two list of suggestions. This must be done last,
7350 * because sorting changes the order again. */
7351 score_combine(su);
7352}
7353
7354#ifdef FEAT_EVAL
7355/*
7356 * Find suggestions by evaluating expression "expr".
7357 */
7358 static void
7359spell_suggest_expr(su, expr)
7360 suginfo_T *su;
7361 char_u *expr;
7362{
7363 list_T *list;
7364 listitem_T *li;
7365 int score;
7366 char_u *p;
7367
7368 /* The work is split up in a few parts to avoid having to export
7369 * suginfo_T.
7370 * First evaluate the expression and get the resulting list. */
7371 list = eval_spell_expr(su->su_badword, expr);
7372 if (list != NULL)
7373 {
7374 /* Loop over the items in the list. */
7375 for (li = list->lv_first; li != NULL; li = li->li_next)
7376 if (li->li_tv.v_type == VAR_LIST)
7377 {
7378 /* Get the word and the score from the items. */
7379 score = get_spellword(li->li_tv.vval.v_list, &p);
7380 if (score >= 0)
7381 add_suggestion(su, &su->su_ga, p,
7382 su->su_badlen, score, 0, TRUE);
7383 }
7384 list_unref(list);
7385 }
7386
7387 /* Sort the suggestions and truncate at "maxcount". */
7388 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
7389}
7390#endif
7391
7392/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007393 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007394 */
7395 static void
7396spell_suggest_file(su, fname)
7397 suginfo_T *su;
7398 char_u *fname;
7399{
7400 FILE *fd;
7401 char_u line[MAXWLEN * 2];
7402 char_u *p;
7403 int len;
7404 char_u cword[MAXWLEN];
7405
7406 /* Open the file. */
7407 fd = mch_fopen((char *)fname, "r");
7408 if (fd == NULL)
7409 {
7410 EMSG2(_(e_notopen), fname);
7411 return;
7412 }
7413
7414 /* Read it line by line. */
7415 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
7416 {
7417 line_breakcheck();
7418
7419 p = vim_strchr(line, '/');
7420 if (p == NULL)
7421 continue; /* No Tab found, just skip the line. */
7422 *p++ = NUL;
7423 if (STRICMP(su->su_badword, line) == 0)
7424 {
7425 /* Match! Isolate the good word, until CR or NL. */
7426 for (len = 0; p[len] >= ' '; ++len)
7427 ;
7428 p[len] = NUL;
7429
7430 /* If the suggestion doesn't have specific case duplicate the case
7431 * of the bad word. */
7432 if (captype(p, NULL) == 0)
7433 {
7434 make_case_word(p, cword, su->su_badflags);
7435 p = cword;
7436 }
7437
7438 add_suggestion(su, &su->su_ga, p, su->su_badlen,
7439 SCORE_FILE, 0, TRUE);
7440 }
7441 }
7442
7443 fclose(fd);
7444
7445 /* Sort the suggestions and truncate at "maxcount". */
7446 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
7447}
7448
7449/*
7450 * Find suggestions for the internal method indicated by "sps_flags".
7451 */
7452 static void
7453spell_suggest_intern(su)
7454 suginfo_T *su;
7455{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007456 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007457 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007458 *
7459 * Set a maximum score to limit the combination of operations that is
7460 * tried.
7461 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007462 suggest_try_special(su);
7463
7464 /*
7465 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
7466 * from the .aff file and inserting a space (split the word).
7467 */
7468 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007469
7470 /* For the resulting top-scorers compute the sound-a-like score. */
7471 if (sps_flags & SPS_DOUBLE)
7472 score_comp_sal(su);
7473
7474 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007475 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007476 *
7477 * Only do this when we don't have a lot of suggestions yet, because it's
7478 * very slow and often doesn't find new suggestions.
7479 */
7480 if ((sps_flags & SPS_DOUBLE)
7481 || (!(sps_flags & SPS_FAST)
7482 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
7483 {
7484 /* Allow a higher score now. */
7485 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007486 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007487 }
7488
7489 /* When CTRL-C was hit while searching do show the results. */
7490 ui_breakcheck();
7491 if (got_int)
7492 {
7493 (void)vgetc();
7494 got_int = FALSE;
7495 }
7496
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007497 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007498 {
7499 if (sps_flags & SPS_BEST)
7500 /* Adjust the word score for how it sounds like. */
7501 rescore_suggestions(su);
7502
7503 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007504 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007505 }
7506}
7507
7508/*
7509 * Free the info put in "*su" by spell_find_suggest().
7510 */
7511 static void
7512spell_find_cleanup(su)
7513 suginfo_T *su;
7514{
7515 int i;
7516
7517 /* Free the suggestions. */
7518 for (i = 0; i < su->su_ga.ga_len; ++i)
7519 vim_free(SUG(su->su_ga, i).st_word);
7520 ga_clear(&su->su_ga);
7521 for (i = 0; i < su->su_sga.ga_len; ++i)
7522 vim_free(SUG(su->su_sga, i).st_word);
7523 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007524
7525 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007526 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007527}
7528
7529/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007530 * Make a copy of "word", with the first letter upper or lower cased, to
7531 * "wcopy[MAXWLEN]". "word" must not be empty.
7532 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007533 */
7534 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007535onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007536 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007537 char_u *wcopy;
7538 int upper; /* TRUE: first letter made upper case */
7539{
7540 char_u *p;
7541 int c;
7542 int l;
7543
7544 p = word;
7545#ifdef FEAT_MBYTE
7546 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007547 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007548 else
7549#endif
7550 c = *p++;
7551 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007552 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007553 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007554 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007555#ifdef FEAT_MBYTE
7556 if (has_mbyte)
7557 l = mb_char2bytes(c, wcopy);
7558 else
7559#endif
7560 {
7561 l = 1;
7562 wcopy[0] = c;
7563 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007564 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007565}
7566
7567/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007568 * Make a copy of "word" with all the letters upper cased into
7569 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007570 */
7571 static void
7572allcap_copy(word, wcopy)
7573 char_u *word;
7574 char_u *wcopy;
7575{
7576 char_u *s;
7577 char_u *d;
7578 int c;
7579
7580 d = wcopy;
7581 for (s = word; *s != NUL; )
7582 {
7583#ifdef FEAT_MBYTE
7584 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007585 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007586 else
7587#endif
7588 c = *s++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007589 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007590
7591#ifdef FEAT_MBYTE
7592 if (has_mbyte)
7593 {
7594 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
7595 break;
7596 d += mb_char2bytes(c, d);
7597 }
7598 else
7599#endif
7600 {
7601 if (d - wcopy >= MAXWLEN - 1)
7602 break;
7603 *d++ = c;
7604 }
7605 }
7606 *d = NUL;
7607}
7608
7609/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007610 * Try finding suggestions by recognizing specific situations.
7611 */
7612 static void
7613suggest_try_special(su)
7614 suginfo_T *su;
7615{
7616 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007617 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007618 int c;
7619 char_u word[MAXWLEN];
7620
7621 /*
7622 * Recognize a word that is repeated: "the the".
7623 */
7624 p = skiptowhite(su->su_fbadword);
7625 len = p - su->su_fbadword;
7626 p = skipwhite(p);
7627 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
7628 {
7629 /* Include badflags: if the badword is onecap or allcap
7630 * use that for the goodword too: "The the" -> "The". */
7631 c = su->su_fbadword[len];
7632 su->su_fbadword[len] = NUL;
7633 make_case_word(su->su_fbadword, word, su->su_badflags);
7634 su->su_fbadword[len] = c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007635 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007636 }
7637}
7638
7639/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007640 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00007641 *
7642 * This uses a state machine. At each node in the tree we try various
7643 * operations. When trying if an operation work "depth" is increased and the
7644 * stack[] is used to store info. This allows combinations, thus insert one
7645 * character, replace one and delete another. The number of changes is
7646 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007647 */
7648 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00007649suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007650 suginfo_T *su;
7651{
7652 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
7653 char_u tword[MAXWLEN]; /* good word collected so far */
7654 trystate_T stack[MAXWLEN];
7655 char_u preword[MAXWLEN * 3]; /* word found with proper case (appended
7656 * to for word split) */
7657 char_u prewordlen = 0; /* length of word in "preword" */
7658 int splitoff = 0; /* index in tword after last split */
7659 trystate_T *sp;
7660 int newscore;
7661 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007662 char_u *byts, *fbyts, *pbyts;
7663 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007664 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007665 int c, c2, c3;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007666 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007667 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007668 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007669 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007670 int len;
7671 char_u *p;
7672 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00007673 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007674 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007675 slang_T *slang;
7676 int fword_ends;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007677
7678 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00007679 * to find matches (esp. REP items). Append some more text, changing
7680 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007681 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007682 n = STRLEN(fword);
7683 p = su->su_badptr + su->su_badlen;
7684 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007685
7686 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7687 lp->lp_slang != NULL; ++lp)
7688 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007689 slang = lp->lp_slang;
7690
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007691 /*
7692 * Go through the whole case-fold tree, try changes at each node.
7693 * "tword[]" contains the word collected from nodes in the tree.
7694 * "fword[]" the word we are trying to match with (initially the bad
7695 * word).
7696 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007697 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007698 sp = &stack[0];
7699 sp->ts_state = STATE_START;
7700 sp->ts_score = 0;
7701 sp->ts_curi = 1;
7702 sp->ts_fidx = 0;
7703 sp->ts_fidxtry = 0;
7704 sp->ts_twordlen = 0;
7705 sp->ts_arridx = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00007706#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007707 sp->ts_tcharlen = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00007708#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007709
Bram Moolenaarea424162005-06-16 21:51:00 +00007710 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007711 * When there are postponed prefixes we need to use these first. At
7712 * the end of the prefix we continue in the case-fold tree.
7713 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007714 fbyts = slang->sl_fbyts;
7715 fidxs = slang->sl_fidxs;
7716 pbyts = slang->sl_pbyts;
7717 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007718 if (pbyts != NULL)
7719 {
7720 byts = pbyts;
7721 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007722 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007723 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
7724 }
7725 else
7726 {
7727 byts = fbyts;
7728 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007729 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007730 }
7731
7732 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00007733 * Loop to find all suggestions. At each round we either:
7734 * - For the current state try one operation, advance "ts_curi",
7735 * increase "depth".
7736 * - When a state is done go to the next, set "ts_state".
7737 * - When all states are tried decrease "depth".
7738 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007739 while (depth >= 0 && !got_int)
7740 {
7741 sp = &stack[depth];
7742 switch (sp->ts_state)
7743 {
7744 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007745 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007746 /*
7747 * Start of node: Deal with NUL bytes, which means
7748 * tword[] may end here.
7749 */
7750 arridx = sp->ts_arridx; /* current node in the tree */
7751 len = byts[arridx]; /* bytes in this node */
7752 arridx += sp->ts_curi; /* index of current byte */
7753
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007754 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007755 {
7756 /* Skip over the NUL bytes, we use them later. */
7757 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
7758 ;
7759 sp->ts_curi += n;
7760
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007761 /* Always past NUL bytes now. */
7762 n = (int)sp->ts_state;
7763 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00007764 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007765
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007766 /* At end of a prefix or at start of prefixtree: check for
7767 * following word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007768 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007769 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00007770 /* Set su->su_badflags to the caps type at this
7771 * position. Use the caps type until here for the
7772 * prefix itself. */
7773#ifdef FEAT_MBYTE
7774 if (has_mbyte)
7775 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
7776 else
7777#endif
7778 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007779 flags = badword_captype(su->su_badptr,
7780 su->su_badptr + n);
7781 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00007782 su->su_badptr + su->su_badlen);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007783 ++depth;
7784 stack[depth] = stack[depth - 1];
7785 sp = &stack[depth];
7786 sp->ts_prefixdepth = depth - 1;
7787 byts = fbyts;
7788 idxs = fidxs;
7789 sp->ts_state = STATE_START;
7790 sp->ts_curi = 1; /* start just after length byte */
7791 sp->ts_arridx = 0;
7792
Bram Moolenaar53805d12005-08-01 07:08:33 +00007793 /* Move the prefix to preword[] with the right case
7794 * and make find_keepcap_word() works. */
7795 splitoff = sp->ts_twordlen;
7796 tword[splitoff] = NUL;
7797 make_case_word(tword, preword, flags);
7798 prewordlen = STRLEN(preword);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007799 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007800 break;
7801 }
7802
Bram Moolenaar0c405862005-06-22 22:26:26 +00007803 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007804 {
7805 /* Past bytes in node and/or past NUL bytes. */
7806 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00007807 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007808 break;
7809 }
7810
7811 /*
7812 * End of word in tree.
7813 */
7814 ++sp->ts_curi; /* eat one NUL byte */
7815
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007816 flags = (int)idxs[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007817
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007818 if (sp->ts_prefixdepth == PFD_COMPOUND)
7819 {
7820 /* There was a compound word before this word. If this
7821 * word does not support compounding then give up
7822 * (splitting is tried for the word without compound
7823 * flag). */
7824 if (sp->ts_twordlen - splitoff < slang->sl_compminlen
7825 || !can_compound(slang, flags))
7826 break;
7827 }
7828 else if (sp->ts_prefixdepth < MAXWLEN)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007829 {
7830 /* There was a prefix before the word. Check that the
7831 * prefix can be used with this word. */
7832 /* Count the length of the NULs in the prefix. If there
7833 * are none this must be the first try without a prefix.
7834 */
7835 n = stack[sp->ts_prefixdepth].ts_arridx;
7836 len = pbyts[n++];
7837 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
7838 ;
7839 if (c > 0)
7840 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007841 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007842 tword + splitoff, slang, FALSE);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007843 if (c == 0)
7844 break;
7845
7846 /* Use the WF_RARE flag for a rare prefix. */
7847 if (c & WF_RAREPFX)
7848 flags |= WF_RARE;
7849 }
7850 }
7851
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007852 /*
7853 * Form the word with proper case in preword.
7854 * If there is a word from a previous split, append.
7855 */
7856 tword[sp->ts_twordlen] = NUL;
7857 if (flags & WF_KEEPCAP)
7858 /* Must find the word in the keep-case tree. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007859 find_keepcap_word(slang, tword + splitoff,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007860 preword + prewordlen);
7861 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00007862 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007863 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00007864 * use that for the goodword too. But if the badword is
7865 * allcap and it's only one char long use onecap. */
7866 c = su->su_badflags;
7867 if ((c & WF_ALLCAP)
7868#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007869 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007870#else
7871 && su->su_badlen == 1
7872#endif
7873 )
7874 c = WF_ONECAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007875 make_case_word(tword + splitoff,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007876 preword + prewordlen, flags | c);
7877 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007878
7879 /* Don't use a banned word. It may appear again as a good
7880 * word, thus remember it. */
7881 if (flags & WF_BANNED)
7882 {
7883 add_banned(su, preword + prewordlen);
7884 break;
7885 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007886 if (was_banned(su, preword + prewordlen)
7887 || was_banned(su, preword))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007888 break;
7889
7890 newscore = 0;
7891 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007892 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007893 newscore += SCORE_REGION;
7894 if (flags & WF_RARE)
7895 newscore += SCORE_RARE;
7896
Bram Moolenaar0c405862005-06-22 22:26:26 +00007897 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007898 captype(preword + prewordlen, NULL)))
7899 newscore += SCORE_ICASE;
7900
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007901 fword_ends = (fword[sp->ts_fidx] == NUL
7902 || !spell_iswordp(fword + sp->ts_fidx, curbuf));
7903 if (fword_ends && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007904 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007905 /* The badword also ends: add suggestions. Give a penalty
7906 * when changing non-word char to word char, e.g., "thes,"
7907 * -> "these". */
7908 p = fword + sp->ts_fidx;
7909#ifdef FEAT_MBYTE
7910 if (has_mbyte)
7911 mb_ptr_back(fword, p);
7912 else
7913#endif
7914 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007915 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007916 {
7917 p = preword + STRLEN(preword);
7918#ifdef FEAT_MBYTE
7919 if (has_mbyte)
7920 mb_ptr_back(preword, p);
7921 else
7922#endif
7923 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007924 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007925 newscore += SCORE_NONWORD;
7926 }
7927
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007928 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007929 sp->ts_fidx - repextra,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007930 sp->ts_score + newscore, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007931 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007932 else if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +00007933#ifdef FEAT_MBYTE
7934 /* Don't split halfway a character. */
7935 && (!has_mbyte || sp->ts_tcharlen == 0)
7936#endif
7937 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007938 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007939 int try_compound;
7940
7941 /* Get here in two situations:
7942 * 1. The word in the tree ends but the badword continues:
7943 * If the word allows compounding try that. Otherwise
7944 * try a split by inserting a space. For both check
7945 * that a valid words starts at fword[sp->ts_fidx].
7946 * 2. The badword does end, but it was due to a change
7947 * (e.g., a swap). No need to split, but do check that
7948 * the following word is valid.
7949 */
7950 if (!fword_ends
7951 && spell_iswordp(fword + sp->ts_fidx, curbuf)
7952 && sp->ts_twordlen - splitoff
7953 >= slang->sl_compminlen
7954 && can_compound(slang, flags))
7955 try_compound = TRUE;
7956 else
7957 {
7958 try_compound = FALSE;
7959 if (!fword_ends)
7960 newscore += SCORE_SPLIT;
7961 }
7962
7963 if (try_deeper(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007964 {
7965 /* Save things to be restored at STATE_SPLITUNDO. */
7966 sp->ts_save_prewordlen = prewordlen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007967 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007968 sp->ts_save_splitoff = splitoff;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007969 sp->ts_state = STATE_SPLITUNDO;
7970
7971 ++depth;
7972 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007973
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007974 /* Append a space to preword when splitting. */
7975 if (!try_compound && !fword_ends)
7976 STRCAT(preword, " ");
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007977 prewordlen = STRLEN(preword);
7978 splitoff = sp->ts_twordlen;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007979
7980 /* If the badword has a non-word character at this
7981 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007982 * non-word character with a space. Always skip a
7983 * character when the word ends. */
7984 if ((!try_compound
7985 && !spell_iswordp_nmw(fword + sp->ts_fidx))
7986 || fword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007987 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007988 int l;
7989
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007990#ifdef FEAT_MBYTE
7991 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007992 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007993 else
7994#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007995 l = 1;
7996 if (fword_ends)
7997 {
7998 /* Copy the skipped character to preword. */
7999 mch_memmove(preword + prewordlen,
8000 fword + sp->ts_fidx, l);
8001 prewordlen += l;
8002 preword[prewordlen] = NUL;
8003 }
8004 else
8005 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
8006 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008007 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00008008
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008009 /* set flag to check compound flag on following word */
8010 if (try_compound)
8011 sp->ts_prefixdepth = PFD_COMPOUND;
8012 else
8013 sp->ts_prefixdepth = PFD_NOPREFIX;
8014
Bram Moolenaar53805d12005-08-01 07:08:33 +00008015 /* set su->su_badflags to the caps type at this
8016 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008017#ifdef FEAT_MBYTE
8018 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00008019 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008020 else
8021#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00008022 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008023 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00008024 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008025
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008026 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008027 sp->ts_arridx = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008028 }
8029 }
8030 break;
8031
8032 case STATE_SPLITUNDO:
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008033 /* Undo the changes done for word split or compound word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008034 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008035 splitoff = sp->ts_save_splitoff;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008036 prewordlen = sp->ts_save_prewordlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008037
8038 /* Continue looking for NUL bytes. */
8039 sp->ts_state = STATE_START;
8040 break;
8041
8042 case STATE_ENDNUL:
8043 /* Past the NUL bytes in the node. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008044 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008045 if (fword[sp->ts_fidx] == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008046 {
8047 /* The badword ends, can't use the bytes in this node. */
8048 sp->ts_state = STATE_DEL;
8049 break;
8050 }
8051 sp->ts_state = STATE_PLAIN;
8052 /*FALLTHROUGH*/
8053
8054 case STATE_PLAIN:
8055 /*
8056 * Go over all possible bytes at this node, add each to
8057 * tword[] and use child node. "ts_curi" is the index.
8058 */
8059 arridx = sp->ts_arridx;
8060 if (sp->ts_curi > byts[arridx])
8061 {
8062 /* Done all bytes at this node, do next state. When still
8063 * at already changed bytes skip the other tricks. */
8064 if (sp->ts_fidx >= sp->ts_fidxtry)
8065 sp->ts_state = STATE_DEL;
8066 else
8067 sp->ts_state = STATE_FINAL;
8068 }
8069 else
8070 {
8071 arridx += sp->ts_curi++;
8072 c = byts[arridx];
8073
8074 /* Normal byte, go one level deeper. If it's not equal to
8075 * the byte in the bad word adjust the score. But don't
8076 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00008077 if (c == fword[sp->ts_fidx]
8078#ifdef FEAT_MBYTE
8079 || (sp->ts_tcharlen > 0
8080 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008081#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00008082 )
8083 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008084 else
8085 newscore = SCORE_SUBST;
8086 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
8087 && try_deeper(su, stack, depth, newscore))
8088 {
8089 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008090 sp = &stack[depth];
8091 ++sp->ts_fidx;
8092 tword[sp->ts_twordlen++] = c;
8093 sp->ts_arridx = idxs[arridx];
8094#ifdef FEAT_MBYTE
8095 if (newscore == SCORE_SUBST)
8096 sp->ts_isdiff = DIFF_YES;
8097 if (has_mbyte)
8098 {
8099 /* Multi-byte characters are a bit complicated to
8100 * handle: They differ when any of the bytes
8101 * differ and then their length may also differ. */
8102 if (sp->ts_tcharlen == 0)
8103 {
8104 /* First byte. */
8105 sp->ts_tcharidx = 0;
8106 sp->ts_tcharlen = MB_BYTE2LEN(c);
8107 sp->ts_fcharstart = sp->ts_fidx - 1;
8108 sp->ts_isdiff = (newscore != 0)
8109 ? DIFF_YES : DIFF_NONE;
8110 }
8111 else if (sp->ts_isdiff == DIFF_INSERT)
8112 /* When inserting trail bytes don't advance in
8113 * the bad word. */
8114 --sp->ts_fidx;
8115 if (++sp->ts_tcharidx == sp->ts_tcharlen)
8116 {
8117 /* Last byte of character. */
8118 if (sp->ts_isdiff == DIFF_YES)
8119 {
8120 /* Correct ts_fidx for the byte length of
8121 * the character (we didn't check that
8122 * before). */
8123 sp->ts_fidx = sp->ts_fcharstart
8124 + MB_BYTE2LEN(
8125 fword[sp->ts_fcharstart]);
8126
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00008127 /* For changing a composing character
8128 * adjust the score from SCORE_SUBST to
8129 * SCORE_SUBCOMP. */
8130 if (enc_utf8
8131 && utf_iscomposing(
8132 mb_ptr2char(tword
8133 + sp->ts_twordlen
8134 - sp->ts_tcharlen))
8135 && utf_iscomposing(
8136 mb_ptr2char(fword
8137 + sp->ts_fcharstart)))
8138 sp->ts_score -=
8139 SCORE_SUBST - SCORE_SUBCOMP;
8140
Bram Moolenaarea424162005-06-16 21:51:00 +00008141 /* For a similar character adjust score
8142 * from SCORE_SUBST to SCORE_SIMILAR. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008143 else if (slang->sl_has_map
8144 && similar_chars(slang,
Bram Moolenaarea424162005-06-16 21:51:00 +00008145 mb_ptr2char(tword
8146 + sp->ts_twordlen
8147 - sp->ts_tcharlen),
8148 mb_ptr2char(fword
8149 + sp->ts_fcharstart)))
8150 sp->ts_score -=
8151 SCORE_SUBST - SCORE_SIMILAR;
8152 }
Bram Moolenaarea408852005-06-25 22:49:46 +00008153 else if (sp->ts_isdiff == DIFF_INSERT
8154 && sp->ts_twordlen > sp->ts_tcharlen)
8155 {
Bram Moolenaarea408852005-06-25 22:49:46 +00008156 p = tword + sp->ts_twordlen
8157 - sp->ts_tcharlen;
8158 c = mb_ptr2char(p);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008159 if (enc_utf8 && utf_iscomposing(c))
8160 {
8161 /* Inserting a composing char doesn't
8162 * count that much. */
Bram Moolenaarea408852005-06-25 22:49:46 +00008163 sp->ts_score -= SCORE_INS
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008164 - SCORE_INSCOMP;
8165 }
8166 else
8167 {
8168 /* If the previous character was the
8169 * same, thus doubling a character,
8170 * give a bonus to the score. */
8171 mb_ptr_back(tword, p);
8172 if (c == mb_ptr2char(p))
8173 sp->ts_score -= SCORE_INS
Bram Moolenaarea408852005-06-25 22:49:46 +00008174 - SCORE_INSDUP;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008175 }
Bram Moolenaarea408852005-06-25 22:49:46 +00008176 }
Bram Moolenaarea424162005-06-16 21:51:00 +00008177
8178 /* Starting a new char, reset the length. */
8179 sp->ts_tcharlen = 0;
8180 }
8181 }
8182 else
8183#endif
8184 {
8185 /* If we found a similar char adjust the score.
8186 * We do this after calling try_deeper() because
8187 * it's slow. */
8188 if (newscore != 0
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008189 && slang->sl_has_map
8190 && similar_chars(slang,
Bram Moolenaarea424162005-06-16 21:51:00 +00008191 c, fword[sp->ts_fidx - 1]))
8192 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
8193 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008194 }
8195 }
8196 break;
8197
8198 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00008199#ifdef FEAT_MBYTE
8200 /* When past the first byte of a multi-byte char don't try
8201 * delete/insert/swap a character. */
8202 if (has_mbyte && sp->ts_tcharlen > 0)
8203 {
8204 sp->ts_state = STATE_FINAL;
8205 break;
8206 }
8207#endif
8208 /*
8209 * Try skipping one character in the bad word (delete it).
8210 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008211 sp->ts_state = STATE_INS;
8212 sp->ts_curi = 1;
8213 if (fword[sp->ts_fidx] != NUL
8214 && try_deeper(su, stack, depth, SCORE_DEL))
8215 {
8216 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00008217
8218 /* Advance over the character in fword[]. Give a bonus to
8219 * the score if the same character is following "nn" ->
8220 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00008221#ifdef FEAT_MBYTE
8222 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00008223 {
8224 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00008225 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00008226 if (enc_utf8 && utf_iscomposing(c))
8227 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
8228 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
Bram Moolenaarea408852005-06-25 22:49:46 +00008229 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
8230 }
Bram Moolenaarea424162005-06-16 21:51:00 +00008231 else
8232#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00008233 {
Bram Moolenaarea424162005-06-16 21:51:00 +00008234 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00008235 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
8236 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
8237 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008238 break;
8239 }
8240 /*FALLTHROUGH*/
8241
8242 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00008243 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008244 * node. */
8245 n = sp->ts_arridx;
8246 if (sp->ts_curi > byts[n])
8247 {
8248 /* Done all bytes at this node, do next state. */
8249 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008250 }
8251 else
8252 {
Bram Moolenaarea424162005-06-16 21:51:00 +00008253 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008254 n += sp->ts_curi++;
8255 c = byts[n];
8256 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
8257 {
8258 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008259 sp = &stack[depth];
8260 tword[sp->ts_twordlen++] = c;
8261 sp->ts_arridx = idxs[n];
8262#ifdef FEAT_MBYTE
8263 if (has_mbyte)
8264 {
8265 fl = MB_BYTE2LEN(c);
8266 if (fl > 1)
8267 {
8268 /* There are following bytes for the same
8269 * character. We must find all bytes before
8270 * trying delete/insert/swap/etc. */
8271 sp->ts_tcharlen = fl;
8272 sp->ts_tcharidx = 1;
8273 sp->ts_isdiff = DIFF_INSERT;
8274 }
8275 }
Bram Moolenaarea408852005-06-25 22:49:46 +00008276 else
8277 fl = 1;
8278 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00008279#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00008280 {
8281 /* If the previous character was the same, thus
8282 * doubling a character, give a bonus to the
8283 * score. */
8284 if (sp->ts_twordlen >= 2
8285 && tword[sp->ts_twordlen - 2] == c)
8286 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
8287 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008288 }
8289 }
8290 break;
8291
8292 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00008293 /*
8294 * Swap two bytes in the bad word: "12" -> "21".
8295 * We change "fword" here, it's changed back afterwards.
8296 */
8297 p = fword + sp->ts_fidx;
8298 c = *p;
8299 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008300 {
Bram Moolenaarea424162005-06-16 21:51:00 +00008301 /* End of word, can't swap or replace. */
8302 sp->ts_state = STATE_FINAL;
8303 break;
8304 }
8305#ifdef FEAT_MBYTE
8306 if (has_mbyte)
8307 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008308 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00008309 c = mb_ptr2char(p);
8310 c2 = mb_ptr2char(p + n);
8311 }
8312 else
8313#endif
8314 c2 = p[1];
8315 if (c == c2)
8316 {
8317 /* Characters are identical, swap won't do anything. */
8318 sp->ts_state = STATE_SWAP3;
8319 break;
8320 }
8321 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
8322 {
8323 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008324 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008325#ifdef FEAT_MBYTE
8326 if (has_mbyte)
8327 {
8328 fl = mb_char2len(c2);
8329 mch_memmove(p, p + n, fl);
8330 mb_char2bytes(c, p + fl);
8331 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
8332 }
8333 else
8334#endif
8335 {
8336 p[0] = c2;
8337 p[1] = c;
8338 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
8339 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008340 }
8341 else
8342 /* If this swap doesn't work then SWAP3 won't either. */
8343 sp->ts_state = STATE_REP_INI;
8344 break;
8345
Bram Moolenaarea424162005-06-16 21:51:00 +00008346 case STATE_UNSWAP:
8347 /* Undo the STATE_SWAP swap: "21" -> "12". */
8348 p = fword + sp->ts_fidx;
8349#ifdef FEAT_MBYTE
8350 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008351 {
Bram Moolenaarea424162005-06-16 21:51:00 +00008352 n = MB_BYTE2LEN(*p);
8353 c = mb_ptr2char(p + n);
8354 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
8355 mb_char2bytes(c, p);
8356 }
8357 else
8358#endif
8359 {
8360 c = *p;
8361 *p = p[1];
8362 p[1] = c;
8363 }
8364 /*FALLTHROUGH*/
8365
8366 case STATE_SWAP3:
8367 /* Swap two bytes, skipping one: "123" -> "321". We change
8368 * "fword" here, it's changed back afterwards. */
8369 p = fword + sp->ts_fidx;
8370#ifdef FEAT_MBYTE
8371 if (has_mbyte)
8372 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008373 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00008374 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008375 fl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00008376 c2 = mb_ptr2char(p + n);
8377 c3 = mb_ptr2char(p + n + fl);
8378 }
8379 else
8380#endif
8381 {
8382 c = *p;
8383 c2 = p[1];
8384 c3 = p[2];
8385 }
8386
8387 /* When characters are identical: "121" then SWAP3 result is
8388 * identical, ROT3L result is same as SWAP: "211", ROT3L
8389 * result is same as SWAP on next char: "112". Thus skip all
8390 * swapping. Also skip when c3 is NUL. */
8391 if (c == c3 || c3 == NUL)
8392 {
8393 sp->ts_state = STATE_REP_INI;
8394 break;
8395 }
8396 if (try_deeper(su, stack, depth, SCORE_SWAP3))
8397 {
8398 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008399 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008400#ifdef FEAT_MBYTE
8401 if (has_mbyte)
8402 {
8403 tl = mb_char2len(c3);
8404 mch_memmove(p, p + n + fl, tl);
8405 mb_char2bytes(c2, p + tl);
8406 mb_char2bytes(c, p + fl + tl);
8407 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
8408 }
8409 else
8410#endif
8411 {
8412 p[0] = p[2];
8413 p[2] = c;
8414 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
8415 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008416 }
8417 else
8418 sp->ts_state = STATE_REP_INI;
8419 break;
8420
Bram Moolenaarea424162005-06-16 21:51:00 +00008421 case STATE_UNSWAP3:
8422 /* Undo STATE_SWAP3: "321" -> "123" */
8423 p = fword + sp->ts_fidx;
8424#ifdef FEAT_MBYTE
8425 if (has_mbyte)
8426 {
8427 n = MB_BYTE2LEN(*p);
8428 c2 = mb_ptr2char(p + n);
8429 fl = MB_BYTE2LEN(p[n]);
8430 c = mb_ptr2char(p + n + fl);
8431 tl = MB_BYTE2LEN(p[n + fl]);
8432 mch_memmove(p + fl + tl, p, n);
8433 mb_char2bytes(c, p);
8434 mb_char2bytes(c2, p + tl);
8435 }
8436 else
8437#endif
8438 {
8439 c = *p;
8440 *p = p[2];
8441 p[2] = c;
8442 }
Bram Moolenaarea424162005-06-16 21:51:00 +00008443
Bram Moolenaarea424162005-06-16 21:51:00 +00008444 /* Rotate three characters left: "123" -> "231". We change
8445 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008446 if (try_deeper(su, stack, depth, SCORE_SWAP3))
8447 {
Bram Moolenaarea424162005-06-16 21:51:00 +00008448 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008449 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008450 p = fword + sp->ts_fidx;
8451#ifdef FEAT_MBYTE
8452 if (has_mbyte)
8453 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008454 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00008455 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008456 fl = mb_cptr2len(p + n);
8457 fl += mb_cptr2len(p + n + fl);
Bram Moolenaarea424162005-06-16 21:51:00 +00008458 mch_memmove(p, p + n, fl);
8459 mb_char2bytes(c, p + fl);
8460 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
8461 }
8462 else
8463#endif
8464 {
8465 c = *p;
8466 *p = p[1];
8467 p[1] = p[2];
8468 p[2] = c;
8469 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
8470 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008471 }
8472 else
8473 sp->ts_state = STATE_REP_INI;
8474 break;
8475
Bram Moolenaarea424162005-06-16 21:51:00 +00008476 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +00008477 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00008478 p = fword + sp->ts_fidx;
8479#ifdef FEAT_MBYTE
8480 if (has_mbyte)
8481 {
8482 n = MB_BYTE2LEN(*p);
8483 n += MB_BYTE2LEN(p[n]);
8484 c = mb_ptr2char(p + n);
8485 tl = MB_BYTE2LEN(p[n]);
8486 mch_memmove(p + tl, p, n);
8487 mb_char2bytes(c, p);
8488 }
8489 else
8490#endif
8491 {
8492 c = p[2];
8493 p[2] = p[1];
8494 p[1] = *p;
8495 *p = c;
8496 }
Bram Moolenaarea424162005-06-16 21:51:00 +00008497
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008498 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00008499 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008500 if (try_deeper(su, stack, depth, SCORE_SWAP3))
8501 {
Bram Moolenaarea424162005-06-16 21:51:00 +00008502 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008503 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008504 p = fword + sp->ts_fidx;
8505#ifdef FEAT_MBYTE
8506 if (has_mbyte)
8507 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008508 n = mb_cptr2len(p);
8509 n += mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00008510 c = mb_ptr2char(p + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008511 tl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00008512 mch_memmove(p + tl, p, n);
8513 mb_char2bytes(c, p);
8514 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
8515 }
8516 else
8517#endif
8518 {
8519 c = p[2];
8520 p[2] = p[1];
8521 p[1] = *p;
8522 *p = c;
8523 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
8524 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008525 }
8526 else
8527 sp->ts_state = STATE_REP_INI;
8528 break;
8529
Bram Moolenaarea424162005-06-16 21:51:00 +00008530 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +00008531 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00008532 p = fword + sp->ts_fidx;
8533#ifdef FEAT_MBYTE
8534 if (has_mbyte)
8535 {
8536 c = mb_ptr2char(p);
8537 tl = MB_BYTE2LEN(*p);
8538 n = MB_BYTE2LEN(p[tl]);
8539 n += MB_BYTE2LEN(p[tl + n]);
8540 mch_memmove(p, p + tl, n);
8541 mb_char2bytes(c, p + n);
8542 }
8543 else
8544#endif
8545 {
8546 c = *p;
8547 *p = p[1];
8548 p[1] = p[2];
8549 p[2] = c;
8550 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008551 /*FALLTHROUGH*/
8552
8553 case STATE_REP_INI:
8554 /* Check if matching with REP items from the .aff file would
8555 * work. Quickly skip if there are no REP items or the score
8556 * is going to be too high anyway. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008557 gap = &slang->sl_rep;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008558 if (gap->ga_len == 0
8559 || sp->ts_score + SCORE_REP >= su->su_maxscore)
8560 {
8561 sp->ts_state = STATE_FINAL;
8562 break;
8563 }
8564
8565 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00008566 * may match. If the index is -1 there is none. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008567 sp->ts_curi = slang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008568 if (sp->ts_curi < 0)
8569 {
8570 sp->ts_state = STATE_FINAL;
8571 break;
8572 }
8573
8574 sp->ts_state = STATE_REP;
8575 /*FALLTHROUGH*/
8576
8577 case STATE_REP:
8578 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00008579 * match replace the characters and check if the resulting
8580 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008581 p = fword + sp->ts_fidx;
8582
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008583 gap = &slang->sl_rep;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008584 while (sp->ts_curi < gap->ga_len)
8585 {
8586 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
8587 if (*ftp->ft_from != *p)
8588 {
8589 /* past possible matching entries */
8590 sp->ts_curi = gap->ga_len;
8591 break;
8592 }
8593 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
8594 && try_deeper(su, stack, depth, SCORE_REP))
8595 {
8596 /* Need to undo this afterwards. */
8597 sp->ts_state = STATE_REP_UNDO;
8598
8599 /* Change the "from" to the "to" string. */
8600 ++depth;
8601 fl = STRLEN(ftp->ft_from);
8602 tl = STRLEN(ftp->ft_to);
8603 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008604 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008605 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008606 repextra += tl - fl;
8607 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008608 mch_memmove(p, ftp->ft_to, tl);
8609 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +00008610#ifdef FEAT_MBYTE
8611 stack[depth].ts_tcharlen = 0;
8612#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008613 break;
8614 }
8615 }
8616
8617 if (sp->ts_curi >= gap->ga_len)
8618 /* No (more) matches. */
8619 sp->ts_state = STATE_FINAL;
8620
8621 break;
8622
8623 case STATE_REP_UNDO:
8624 /* Undo a REP replacement and continue with the next one. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008625 ftp = (fromto_T *)slang->sl_rep.ga_data + sp->ts_curi - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008626 fl = STRLEN(ftp->ft_from);
8627 tl = STRLEN(ftp->ft_to);
8628 p = fword + sp->ts_fidx;
8629 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008630 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008631 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008632 repextra -= tl - fl;
8633 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008634 mch_memmove(p, ftp->ft_from, fl);
8635 sp->ts_state = STATE_REP;
8636 break;
8637
8638 default:
8639 /* Did all possible states at this level, go up one level. */
8640 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008641
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008642 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008643 {
8644 /* Continue in or go back to the prefix tree. */
8645 byts = pbyts;
8646 idxs = pidxs;
8647 splitoff = 0;
8648 }
8649
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008650 /* Don't check for CTRL-C too often, it takes time. */
8651 line_breakcheck();
8652 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008653 }
8654 }
8655}
8656
8657/*
8658 * Try going one level deeper in the tree.
8659 */
8660 static int
8661try_deeper(su, stack, depth, score_add)
8662 suginfo_T *su;
8663 trystate_T *stack;
8664 int depth;
8665 int score_add;
8666{
8667 int newscore;
8668
8669 /* Refuse to go deeper if the scrore is getting too big. */
8670 newscore = stack[depth].ts_score + score_add;
8671 if (newscore >= su->su_maxscore)
8672 return FALSE;
8673
Bram Moolenaarea424162005-06-16 21:51:00 +00008674 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008675 stack[depth + 1].ts_state = STATE_START;
8676 stack[depth + 1].ts_score = newscore;
8677 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008678 return TRUE;
8679}
8680
Bram Moolenaar53805d12005-08-01 07:08:33 +00008681#ifdef FEAT_MBYTE
8682/*
8683 * Case-folding may change the number of bytes: Count nr of chars in
8684 * fword[flen] and return the byte length of that many chars in "word".
8685 */
8686 static int
8687nofold_len(fword, flen, word)
8688 char_u *fword;
8689 int flen;
8690 char_u *word;
8691{
8692 char_u *p;
8693 int i = 0;
8694
8695 for (p = fword; p < fword + flen; mb_ptr_adv(p))
8696 ++i;
8697 for (p = word; i > 0; mb_ptr_adv(p))
8698 --i;
8699 return (int)(p - word);
8700}
8701#endif
8702
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008703/*
8704 * "fword" is a good word with case folded. Find the matching keep-case
8705 * words and put it in "kword".
8706 * Theoretically there could be several keep-case words that result in the
8707 * same case-folded word, but we only find one...
8708 */
8709 static void
8710find_keepcap_word(slang, fword, kword)
8711 slang_T *slang;
8712 char_u *fword;
8713 char_u *kword;
8714{
8715 char_u uword[MAXWLEN]; /* "fword" in upper-case */
8716 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008717 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008718
8719 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008720 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008721 int round[MAXWLEN];
8722 int fwordidx[MAXWLEN];
8723 int uwordidx[MAXWLEN];
8724 int kwordlen[MAXWLEN];
8725
8726 int flen, ulen;
8727 int l;
8728 int len;
8729 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008730 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008731 char_u *p;
8732 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008733 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008734
8735 if (byts == NULL)
8736 {
8737 /* array is empty: "cannot happen" */
8738 *kword = NUL;
8739 return;
8740 }
8741
8742 /* Make an all-cap version of "fword". */
8743 allcap_copy(fword, uword);
8744
8745 /*
8746 * Each character needs to be tried both case-folded and upper-case.
8747 * All this gets very complicated if we keep in mind that changing case
8748 * may change the byte length of a multi-byte character...
8749 */
8750 depth = 0;
8751 arridx[0] = 0;
8752 round[0] = 0;
8753 fwordidx[0] = 0;
8754 uwordidx[0] = 0;
8755 kwordlen[0] = 0;
8756 while (depth >= 0)
8757 {
8758 if (fword[fwordidx[depth]] == NUL)
8759 {
8760 /* We are at the end of "fword". If the tree allows a word to end
8761 * here we have found a match. */
8762 if (byts[arridx[depth] + 1] == 0)
8763 {
8764 kword[kwordlen[depth]] = NUL;
8765 return;
8766 }
8767
8768 /* kword is getting too long, continue one level up */
8769 --depth;
8770 }
8771 else if (++round[depth] > 2)
8772 {
8773 /* tried both fold-case and upper-case character, continue one
8774 * level up */
8775 --depth;
8776 }
8777 else
8778 {
8779 /*
8780 * round[depth] == 1: Try using the folded-case character.
8781 * round[depth] == 2: Try using the upper-case character.
8782 */
8783#ifdef FEAT_MBYTE
8784 if (has_mbyte)
8785 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008786 flen = mb_cptr2len(fword + fwordidx[depth]);
8787 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008788 }
8789 else
8790#endif
8791 ulen = flen = 1;
8792 if (round[depth] == 1)
8793 {
8794 p = fword + fwordidx[depth];
8795 l = flen;
8796 }
8797 else
8798 {
8799 p = uword + uwordidx[depth];
8800 l = ulen;
8801 }
8802
8803 for (tryidx = arridx[depth]; l > 0; --l)
8804 {
8805 /* Perform a binary search in the list of accepted bytes. */
8806 len = byts[tryidx++];
8807 c = *p++;
8808 lo = tryidx;
8809 hi = tryidx + len - 1;
8810 while (lo < hi)
8811 {
8812 m = (lo + hi) / 2;
8813 if (byts[m] > c)
8814 hi = m - 1;
8815 else if (byts[m] < c)
8816 lo = m + 1;
8817 else
8818 {
8819 lo = hi = m;
8820 break;
8821 }
8822 }
8823
8824 /* Stop if there is no matching byte. */
8825 if (hi < lo || byts[lo] != c)
8826 break;
8827
8828 /* Continue at the child (if there is one). */
8829 tryidx = idxs[lo];
8830 }
8831
8832 if (l == 0)
8833 {
8834 /*
8835 * Found the matching char. Copy it to "kword" and go a
8836 * level deeper.
8837 */
8838 if (round[depth] == 1)
8839 {
8840 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
8841 flen);
8842 kwordlen[depth + 1] = kwordlen[depth] + flen;
8843 }
8844 else
8845 {
8846 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
8847 ulen);
8848 kwordlen[depth + 1] = kwordlen[depth] + ulen;
8849 }
8850 fwordidx[depth + 1] = fwordidx[depth] + flen;
8851 uwordidx[depth + 1] = uwordidx[depth] + ulen;
8852
8853 ++depth;
8854 arridx[depth] = tryidx;
8855 round[depth] = 0;
8856 }
8857 }
8858 }
8859
8860 /* Didn't find it: "cannot happen". */
8861 *kword = NUL;
8862}
8863
8864/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008865 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
8866 * su->su_sga.
8867 */
8868 static void
8869score_comp_sal(su)
8870 suginfo_T *su;
8871{
8872 langp_T *lp;
8873 char_u badsound[MAXWLEN];
8874 int i;
8875 suggest_T *stp;
8876 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008877 int score;
8878
8879 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
8880 return;
8881
8882 /* Use the sound-folding of the first language that supports it. */
8883 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8884 lp->lp_slang != NULL; ++lp)
8885 if (lp->lp_slang->sl_sal.ga_len > 0)
8886 {
8887 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008888 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008889
8890 for (i = 0; i < su->su_ga.ga_len; ++i)
8891 {
8892 stp = &SUG(su->su_ga, i);
8893
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008894 /* Case-fold the suggested word, sound-fold it and compute the
8895 * sound-a-like score. */
8896 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008897 if (score < SCORE_MAXMAX)
8898 {
8899 /* Add the suggestion. */
8900 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
8901 sstp->st_word = vim_strsave(stp->st_word);
8902 if (sstp->st_word != NULL)
8903 {
8904 sstp->st_score = score;
8905 sstp->st_altscore = 0;
8906 sstp->st_orglen = stp->st_orglen;
8907 ++su->su_sga.ga_len;
8908 }
8909 }
8910 }
8911 break;
8912 }
8913}
8914
8915/*
8916 * Combine the list of suggestions in su->su_ga and su->su_sga.
8917 * They are intwined.
8918 */
8919 static void
8920score_combine(su)
8921 suginfo_T *su;
8922{
8923 int i;
8924 int j;
8925 garray_T ga;
8926 garray_T *gap;
8927 langp_T *lp;
8928 suggest_T *stp;
8929 char_u *p;
8930 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008931 int round;
8932
8933 /* Add the alternate score to su_ga. */
8934 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8935 lp->lp_slang != NULL; ++lp)
8936 {
8937 if (lp->lp_slang->sl_sal.ga_len > 0)
8938 {
8939 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008940 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008941
8942 for (i = 0; i < su->su_ga.ga_len; ++i)
8943 {
8944 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008945 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
8946 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008947 if (stp->st_altscore == SCORE_MAXMAX)
8948 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
8949 else
8950 stp->st_score = (stp->st_score * 3
8951 + stp->st_altscore) / 4;
8952 stp->st_salscore = FALSE;
8953 }
8954 break;
8955 }
8956 }
8957
8958 /* Add the alternate score to su_sga. */
8959 for (i = 0; i < su->su_sga.ga_len; ++i)
8960 {
8961 stp = &SUG(su->su_sga, i);
8962 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
8963 if (stp->st_score == SCORE_MAXMAX)
8964 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
8965 else
8966 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
8967 stp->st_salscore = TRUE;
8968 }
8969
8970 /* Sort the suggestions and truncate at "maxcount" for both lists. */
8971 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8972 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
8973
8974 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
8975 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
8976 return;
8977
8978 stp = &SUG(ga, 0);
8979 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
8980 {
8981 /* round 1: get a suggestion from su_ga
8982 * round 2: get a suggestion from su_sga */
8983 for (round = 1; round <= 2; ++round)
8984 {
8985 gap = round == 1 ? &su->su_ga : &su->su_sga;
8986 if (i < gap->ga_len)
8987 {
8988 /* Don't add a word if it's already there. */
8989 p = SUG(*gap, i).st_word;
8990 for (j = 0; j < ga.ga_len; ++j)
8991 if (STRCMP(stp[j].st_word, p) == 0)
8992 break;
8993 if (j == ga.ga_len)
8994 stp[ga.ga_len++] = SUG(*gap, i);
8995 else
8996 vim_free(p);
8997 }
8998 }
8999 }
9000
9001 ga_clear(&su->su_ga);
9002 ga_clear(&su->su_sga);
9003
9004 /* Truncate the list to the number of suggestions that will be displayed. */
9005 if (ga.ga_len > su->su_maxcount)
9006 {
9007 for (i = su->su_maxcount; i < ga.ga_len; ++i)
9008 vim_free(stp[i].st_word);
9009 ga.ga_len = su->su_maxcount;
9010 }
9011
9012 su->su_ga = ga;
9013}
9014
9015/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009016 * For the goodword in "stp" compute the soundalike score compared to the
9017 * badword.
9018 */
9019 static int
9020stp_sal_score(stp, su, slang, badsound)
9021 suggest_T *stp;
9022 suginfo_T *su;
9023 slang_T *slang;
9024 char_u *badsound; /* sound-folded badword */
9025{
9026 char_u *p;
9027 char_u badsound2[MAXWLEN];
9028 char_u fword[MAXWLEN];
9029 char_u goodsound[MAXWLEN];
9030
9031 if (stp->st_orglen <= su->su_badlen)
9032 p = badsound;
9033 else
9034 {
9035 /* soundfold the bad word with more characters following */
9036 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
9037
9038 /* When joining two words the sound often changes a lot. E.g., "t he"
9039 * sounds like "t h" while "the" sounds like "@". Avoid that by
9040 * removing the space. Don't do it when the good word also contains a
9041 * space. */
9042 if (vim_iswhite(su->su_badptr[su->su_badlen])
9043 && *skiptowhite(stp->st_word) == NUL)
9044 for (p = fword; *(p = skiptowhite(p)) != NUL; )
9045 mch_memmove(p, p + 1, STRLEN(p));
9046
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009047 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009048 p = badsound2;
9049 }
9050
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009051 /* Sound-fold the word and compute the score for the difference. */
9052 spell_soundfold(slang, stp->st_word, FALSE, goodsound);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009053
9054 return soundalike_score(goodsound, p);
9055}
9056
9057/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009058 * Find suggestions by comparing the word in a sound-a-like form.
9059 */
9060 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00009061suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009062 suginfo_T *su;
9063{
9064 char_u salword[MAXWLEN];
9065 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009066 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009067 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009068 int curi[MAXWLEN];
9069 langp_T *lp;
9070 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009071 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009072 int depth;
9073 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009074 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009075 int round;
9076 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009077 int sound_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009078
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009079 /* Do this for all languages that support sound folding. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009080 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
9081 lp->lp_slang != NULL; ++lp)
9082 {
9083 if (lp->lp_slang->sl_sal.ga_len > 0)
9084 {
9085 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009086 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009087
9088 /*
9089 * Go through the whole tree, soundfold each word and compare.
9090 * round 1: use the case-folded tree.
9091 * round 2: use the keep-case tree.
9092 */
9093 for (round = 1; round <= 2; ++round)
9094 {
9095 if (round == 1)
9096 {
9097 byts = lp->lp_slang->sl_fbyts;
9098 idxs = lp->lp_slang->sl_fidxs;
9099 }
9100 else
9101 {
9102 byts = lp->lp_slang->sl_kbyts;
9103 idxs = lp->lp_slang->sl_kidxs;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009104 if (byts == NULL) /* no keep-case words */
9105 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009106 }
9107
9108 depth = 0;
9109 arridx[0] = 0;
9110 curi[0] = 1;
9111 while (depth >= 0 && !got_int)
9112 {
9113 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009114 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009115 /* Done all bytes at this node, go up one level. */
9116 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009117 line_breakcheck();
9118 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009119 else
9120 {
9121 /* Do one more byte at this node. */
9122 n = arridx[depth] + curi[depth];
9123 ++curi[depth];
9124 c = byts[n];
9125 if (c == 0)
9126 {
9127 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009128 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009129 if (round == 2 || (flags & WF_KEEPCAP) == 0)
9130 {
9131 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009132 /* Sound-fold. Only in keep-case tree need to
9133 * case-fold the word. */
9134 spell_soundfold(lp->lp_slang, tword,
9135 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009136
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009137 /* Compute the edit distance between the
9138 * sound-a-like words. */
9139 sound_score = soundalike_score(salword,
9140 tsalword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009141 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009142 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009143 char_u cword[MAXWLEN];
9144 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009145 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009146
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009147 flags |= su->su_badflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009148 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009149 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009150 /* Need to fix case according to
9151 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009152 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009153 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009154 }
9155 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009156 p = tword;
9157
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009158 if (sps_flags & SPS_DOUBLE)
9159 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00009160 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009161 sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009162 else
9163 {
9164 /* Compute the score. */
9165 score = spell_edit_score(
9166 su->su_badword, p);
9167 if (sps_flags & SPS_BEST)
9168 /* give a bonus for the good word
9169 * sounding the same as the bad
9170 * word */
9171 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00009172 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009173 RESCORE(score, sound_score),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009174 sound_score, TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009175 else
9176 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00009177 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009178 score + sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009179 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009180 }
9181 }
9182
9183 /* Skip over other NUL bytes. */
9184 while (byts[n + 1] == 0)
9185 {
9186 ++n;
9187 ++curi[depth];
9188 }
9189 }
9190 else
9191 {
9192 /* Normal char, go one level deeper. */
9193 tword[depth++] = c;
9194 arridx[depth] = idxs[n];
9195 curi[depth] = 1;
9196 }
9197 }
9198 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009199 }
9200 }
9201 }
9202}
9203
9204/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009205 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009206 */
9207 static void
9208make_case_word(fword, cword, flags)
9209 char_u *fword;
9210 char_u *cword;
9211 int flags;
9212{
9213 if (flags & WF_ALLCAP)
9214 /* Make it all upper-case */
9215 allcap_copy(fword, cword);
9216 else if (flags & WF_ONECAP)
9217 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009218 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009219 else
9220 /* Use goodword as-is. */
9221 STRCPY(cword, fword);
9222}
9223
Bram Moolenaarea424162005-06-16 21:51:00 +00009224/*
9225 * Use map string "map" for languages "lp".
9226 */
9227 static void
9228set_map_str(lp, map)
9229 slang_T *lp;
9230 char_u *map;
9231{
9232 char_u *p;
9233 int headc = 0;
9234 int c;
9235 int i;
9236
9237 if (*map == NUL)
9238 {
9239 lp->sl_has_map = FALSE;
9240 return;
9241 }
9242 lp->sl_has_map = TRUE;
9243
9244 /* Init the array and hash table empty. */
9245 for (i = 0; i < 256; ++i)
9246 lp->sl_map_array[i] = 0;
9247#ifdef FEAT_MBYTE
9248 hash_init(&lp->sl_map_hash);
9249#endif
9250
9251 /*
9252 * The similar characters are stored separated with slashes:
9253 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
9254 * before the same slash. For characters above 255 sl_map_hash is used.
9255 */
9256 for (p = map; *p != NUL; )
9257 {
9258#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009259 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +00009260#else
9261 c = *p++;
9262#endif
9263 if (c == '/')
9264 headc = 0;
9265 else
9266 {
9267 if (headc == 0)
9268 headc = c;
9269
9270#ifdef FEAT_MBYTE
9271 /* Characters above 255 don't fit in sl_map_array[], put them in
9272 * the hash table. Each entry is the char, a NUL the headchar and
9273 * a NUL. */
9274 if (c >= 256)
9275 {
9276 int cl = mb_char2len(c);
9277 int headcl = mb_char2len(headc);
9278 char_u *b;
9279 hash_T hash;
9280 hashitem_T *hi;
9281
9282 b = alloc((unsigned)(cl + headcl + 2));
9283 if (b == NULL)
9284 return;
9285 mb_char2bytes(c, b);
9286 b[cl] = NUL;
9287 mb_char2bytes(headc, b + cl + 1);
9288 b[cl + 1 + headcl] = NUL;
9289 hash = hash_hash(b);
9290 hi = hash_lookup(&lp->sl_map_hash, b, hash);
9291 if (HASHITEM_EMPTY(hi))
9292 hash_add_item(&lp->sl_map_hash, hi, b, hash);
9293 else
9294 {
9295 /* This should have been checked when generating the .spl
9296 * file. */
9297 EMSG(_("E999: duplicate char in MAP entry"));
9298 vim_free(b);
9299 }
9300 }
9301 else
9302#endif
9303 lp->sl_map_array[c] = headc;
9304 }
9305 }
9306}
9307
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009308/*
9309 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
9310 * lines in the .aff file.
9311 */
9312 static int
9313similar_chars(slang, c1, c2)
9314 slang_T *slang;
9315 int c1;
9316 int c2;
9317{
Bram Moolenaarea424162005-06-16 21:51:00 +00009318 int m1, m2;
9319#ifdef FEAT_MBYTE
9320 char_u buf[MB_MAXBYTES];
9321 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009322
Bram Moolenaarea424162005-06-16 21:51:00 +00009323 if (c1 >= 256)
9324 {
9325 buf[mb_char2bytes(c1, buf)] = 0;
9326 hi = hash_find(&slang->sl_map_hash, buf);
9327 if (HASHITEM_EMPTY(hi))
9328 m1 = 0;
9329 else
9330 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
9331 }
9332 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009333#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00009334 m1 = slang->sl_map_array[c1];
9335 if (m1 == 0)
9336 return FALSE;
9337
9338
9339#ifdef FEAT_MBYTE
9340 if (c2 >= 256)
9341 {
9342 buf[mb_char2bytes(c2, buf)] = 0;
9343 hi = hash_find(&slang->sl_map_hash, buf);
9344 if (HASHITEM_EMPTY(hi))
9345 m2 = 0;
9346 else
9347 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
9348 }
9349 else
9350#endif
9351 m2 = slang->sl_map_array[c2];
9352
9353 return m1 == m2;
9354}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009355
9356/*
9357 * Add a suggestion to the list of suggestions.
9358 * Do not add a duplicate suggestion or suggestions with a bad score.
9359 * When "use_score" is not zero it's used, otherwise the score is computed
9360 * with spell_edit_score().
9361 */
9362 static void
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009363add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009364 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009365 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009366 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009367 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009368 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009369 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009370 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009371{
9372 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009373 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009374 char_u *p = NULL;
9375 int c = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009376
9377 /* Check that the word wasn't banned. */
9378 if (was_banned(su, goodword))
9379 return;
9380
Bram Moolenaar0c405862005-06-22 22:26:26 +00009381 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
9382 * Remove the common part from "goodword". */
9383 i = badlen - su->su_badlen;
9384 if (i > 0)
9385 {
9386 /* This assumes there was no case folding or it didn't change the
9387 * length... */
9388 p = goodword + STRLEN(goodword) - i;
9389 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
9390 {
9391 badlen = su->su_badlen;
9392 c = *p;
9393 *p = NUL;
9394 }
9395 else
9396 p = NULL;
9397 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009398 else if (i < 0)
9399 {
9400 /* When replacing part of the word check that we actually change
9401 * something. For "the the" a suggestion can be replacing the first
9402 * "the" with itself, since "the" wasn't banned. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009403 if (badlen == (int)STRLEN(goodword)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009404 && STRNCMP(su->su_badword, goodword, badlen) == 0)
9405 return;
9406 }
9407
Bram Moolenaar0c405862005-06-22 22:26:26 +00009408
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009409 if (score <= su->su_maxscore)
9410 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009411 /* Check if the word is already there. Also check the length that is
9412 * being replaced "thes," -> "these" is a different suggestion from
9413 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009414 stp = &SUG(*gap, 0);
9415 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009416 if (STRCMP(stp[i].st_word, goodword) == 0
9417 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009418 {
9419 /* Found it. Remember the lowest score. */
9420 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009422 stp[i].st_score = score;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009423 stp[i].st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009424 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009425 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009426 break;
9427 }
9428
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009429 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009430 {
9431 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009432 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009433 stp->st_word = vim_strsave(goodword);
9434 if (stp->st_word != NULL)
9435 {
9436 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009437 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009438 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009439 stp->st_orglen = badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009440 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009441
9442 /* If we have too many suggestions now, sort the list and keep
9443 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009444 if (gap->ga_len > SUG_MAX_COUNT(su))
9445 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
9446 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009447 }
9448 }
9449 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00009450
9451 if (p != NULL)
9452 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009453}
9454
9455/*
9456 * Add a word to be banned.
9457 */
9458 static void
9459add_banned(su, word)
9460 suginfo_T *su;
9461 char_u *word;
9462{
9463 char_u *s = vim_strsave(word);
9464 hash_T hash;
9465 hashitem_T *hi;
9466
9467 if (s != NULL)
9468 {
9469 hash = hash_hash(s);
9470 hi = hash_lookup(&su->su_banned, s, hash);
9471 if (HASHITEM_EMPTY(hi))
9472 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009473 else
9474 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009475 }
9476}
9477
9478/*
9479 * Return TRUE if a word appears in the list of banned words.
9480 */
9481 static int
9482was_banned(su, word)
9483 suginfo_T *su;
9484 char_u *word;
9485{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009486 hashitem_T *hi = hash_find(&su->su_banned, word);
9487
9488 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009489}
9490
9491/*
9492 * Free the banned words in "su".
9493 */
9494 static void
9495free_banned(su)
9496 suginfo_T *su;
9497{
9498 int todo;
9499 hashitem_T *hi;
9500
9501 todo = su->su_banned.ht_used;
9502 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
9503 {
9504 if (!HASHITEM_EMPTY(hi))
9505 {
9506 vim_free(hi->hi_key);
9507 --todo;
9508 }
9509 }
9510 hash_clear(&su->su_banned);
9511}
9512
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009513/*
9514 * Recompute the score if sound-folding is possible. This is slow,
9515 * thus only done for the final results.
9516 */
9517 static void
9518rescore_suggestions(su)
9519 suginfo_T *su;
9520{
9521 langp_T *lp;
9522 suggest_T *stp;
9523 char_u sal_badword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009524 int i;
9525
9526 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
9527 lp->lp_slang != NULL; ++lp)
9528 {
9529 if (lp->lp_slang->sl_sal.ga_len > 0)
9530 {
9531 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009532 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009533
9534 for (i = 0; i < su->su_ga.ga_len; ++i)
9535 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009536 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009537 if (!stp->st_had_bonus)
9538 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009539 stp->st_altscore = stp_sal_score(stp, su,
9540 lp->lp_slang, sal_badword);
9541 if (stp->st_altscore == SCORE_MAXMAX)
9542 stp->st_altscore = SCORE_BIG;
9543 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009544 }
9545 }
9546 break;
9547 }
9548 }
9549}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009550
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009551static int
9552#ifdef __BORLANDC__
9553_RTLENTRYF
9554#endif
9555sug_compare __ARGS((const void *s1, const void *s2));
9556
9557/*
9558 * Function given to qsort() to sort the suggestions on st_score.
9559 */
9560 static int
9561#ifdef __BORLANDC__
9562_RTLENTRYF
9563#endif
9564sug_compare(s1, s2)
9565 const void *s1;
9566 const void *s2;
9567{
9568 suggest_T *p1 = (suggest_T *)s1;
9569 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009570 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009571
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009572 if (n == 0)
9573 return p1->st_altscore - p2->st_altscore;
9574 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009575}
9576
9577/*
9578 * Cleanup the suggestions:
9579 * - Sort on score.
9580 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009581 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009582 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009583 static int
9584cleanup_suggestions(gap, maxscore, keep)
9585 garray_T *gap;
9586 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009587 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009588{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009589 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009590 int i;
9591
9592 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009593 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009594
9595 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009596 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009597 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009598 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009599 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009600 gap->ga_len = keep;
9601 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009602 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009603 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009604}
9605
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009606#if defined(FEAT_EVAL) || defined(PROTO)
9607/*
9608 * Soundfold a string, for soundfold().
9609 * Result is in allocated memory, NULL for an error.
9610 */
9611 char_u *
9612eval_soundfold(word)
9613 char_u *word;
9614{
9615 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009616 char_u sound[MAXWLEN];
9617
9618 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
9619 /* Use the sound-folding of the first language that supports it. */
9620 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
9621 lp->lp_slang != NULL; ++lp)
9622 if (lp->lp_slang->sl_sal.ga_len > 0)
9623 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009624 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009625 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009626 return vim_strsave(sound);
9627 }
9628
9629 /* No language with sound folding, return word as-is. */
9630 return vim_strsave(word);
9631}
9632#endif
9633
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009634/*
9635 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
9636 */
9637 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009638spell_soundfold(slang, inword, folded, res)
9639 slang_T *slang;
9640 char_u *inword;
9641 int folded; /* "inword" is already case-folded */
9642 char_u *res;
9643{
9644 char_u fword[MAXWLEN];
9645 char_u *word;
9646
9647 if (slang->sl_sofo)
9648 /* SOFOFROM and SOFOTO used */
9649 spell_soundfold_sofo(slang, inword, res);
9650 else
9651 {
9652 /* SAL items used. Requires the word to be case-folded. */
9653 if (folded)
9654 word = inword;
9655 else
9656 {
9657 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
9658 word = fword;
9659 }
9660
9661#ifdef FEAT_MBYTE
9662 if (has_mbyte)
9663 spell_soundfold_wsal(slang, word, res);
9664 else
9665#endif
9666 spell_soundfold_sal(slang, word, res);
9667 }
9668}
9669
9670/*
9671 * Perform sound folding of "inword" into "res" according to SOFOFROM and
9672 * SOFOTO lines.
9673 */
9674 static void
9675spell_soundfold_sofo(slang, inword, res)
9676 slang_T *slang;
9677 char_u *inword;
9678 char_u *res;
9679{
9680 char_u *s;
9681 int ri = 0;
9682 int c;
9683
9684#ifdef FEAT_MBYTE
9685 if (has_mbyte)
9686 {
9687 int prevc = 0;
9688 int *ip;
9689
9690 /* The sl_sal_first[] table contains the translation for chars up to
9691 * 255, sl_sal the rest. */
9692 for (s = inword; *s != NUL; )
9693 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009694 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009695 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
9696 c = ' ';
9697 else if (c < 256)
9698 c = slang->sl_sal_first[c];
9699 else
9700 {
9701 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
9702 if (ip == NULL) /* empty list, can't match */
9703 c = NUL;
9704 else
9705 for (;;) /* find "c" in the list */
9706 {
9707 if (*ip == 0) /* not found */
9708 {
9709 c = NUL;
9710 break;
9711 }
9712 if (*ip == c) /* match! */
9713 {
9714 c = ip[1];
9715 break;
9716 }
9717 ip += 2;
9718 }
9719 }
9720
9721 if (c != NUL && c != prevc)
9722 {
9723 ri += mb_char2bytes(c, res + ri);
9724 if (ri + MB_MAXBYTES > MAXWLEN)
9725 break;
9726 prevc = c;
9727 }
9728 }
9729 }
9730 else
9731#endif
9732 {
9733 /* The sl_sal_first[] table contains the translation. */
9734 for (s = inword; (c = *s) != NUL; ++s)
9735 {
9736 if (vim_iswhite(c))
9737 c = ' ';
9738 else
9739 c = slang->sl_sal_first[c];
9740 if (c != NUL && (ri == 0 || res[ri - 1] != c))
9741 res[ri++] = c;
9742 }
9743 }
9744
9745 res[ri] = NUL;
9746}
9747
9748 static void
9749spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009750 slang_T *slang;
9751 char_u *inword;
9752 char_u *res;
9753{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009754 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009755 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009756 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009757 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009758 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009759 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009760 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009761 int n, k = 0;
9762 int z0;
9763 int k0;
9764 int n0;
9765 int c;
9766 int pri;
9767 int p0 = -333;
9768 int c0;
9769
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009770 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009771 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009772 if (slang->sl_rem_accents)
9773 {
9774 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009775 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009776 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009777 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009778 {
9779 *t++ = ' ';
9780 s = skipwhite(s);
9781 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009782 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009783 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009784 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009785 *t++ = *s;
9786 ++s;
9787 }
9788 }
9789 *t = NUL;
9790 }
9791 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009792 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009793
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009794 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009795
9796 /*
9797 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009798 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009799 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009800 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009801 while ((c = word[i]) != NUL)
9802 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009803 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009804 n = slang->sl_sal_first[c];
9805 z0 = 0;
9806
9807 if (n >= 0)
9808 {
9809 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009810 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009811 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009812 /* Quickly skip entries that don't match the word. Most
9813 * entries are less then three chars, optimize for that. */
9814 k = smp[n].sm_leadlen;
9815 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009816 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009817 if (word[i + 1] != s[1])
9818 continue;
9819 if (k > 2)
9820 {
9821 for (j = 2; j < k; ++j)
9822 if (word[i + j] != s[j])
9823 break;
9824 if (j < k)
9825 continue;
9826 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009827 }
9828
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009829 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009830 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009831 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009832 while (*pf != NUL && *pf != word[i + k])
9833 ++pf;
9834 if (*pf == NUL)
9835 continue;
9836 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009837 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009838 s = smp[n].sm_rules;
9839 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009840
9841 p0 = *s;
9842 k0 = k;
9843 while (*s == '-' && k > 1)
9844 {
9845 k--;
9846 s++;
9847 }
9848 if (*s == '<')
9849 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009850 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009851 {
9852 /* determine priority */
9853 pri = *s - '0';
9854 s++;
9855 }
9856 if (*s == '^' && *(s + 1) == '^')
9857 s++;
9858
9859 if (*s == NUL
9860 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009861 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009862 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009863 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009864 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009865 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009866 && spell_iswordp(word + i - 1, curbuf)
9867 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009868 {
9869 /* search for followup rules, if: */
9870 /* followup and k > 1 and NO '-' in searchstring */
9871 c0 = word[i + k - 1];
9872 n0 = slang->sl_sal_first[c0];
9873
9874 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009875 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009876 {
9877 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009878 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009879 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009880 /* Quickly skip entries that don't match the word.
9881 * */
9882 k0 = smp[n0].sm_leadlen;
9883 if (k0 > 1)
9884 {
9885 if (word[i + k] != s[1])
9886 continue;
9887 if (k0 > 2)
9888 {
9889 pf = word + i + k + 1;
9890 for (j = 2; j < k0; ++j)
9891 if (*pf++ != s[j])
9892 break;
9893 if (j < k0)
9894 continue;
9895 }
9896 }
9897 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009898
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009899 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009900 {
9901 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009902 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009903 while (*pf != NUL && *pf != word[i + k0])
9904 ++pf;
9905 if (*pf == NUL)
9906 continue;
9907 ++k0;
9908 }
9909
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009910 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009911 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009912 while (*s == '-')
9913 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009914 /* "k0" gets NOT reduced because
9915 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009916 s++;
9917 }
9918 if (*s == '<')
9919 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009920 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009921 {
9922 p0 = *s - '0';
9923 s++;
9924 }
9925
9926 if (*s == NUL
9927 /* *s == '^' cuts */
9928 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009929 && !spell_iswordp(word + i + k0,
9930 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009931 {
9932 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009933 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009934 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009935
9936 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009937 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009938 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009939 /* rule fits; stop search */
9940 break;
9941 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009942 }
9943
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009944 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009945 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009946 }
9947
9948 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009949 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009950 if (s == NULL)
9951 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009952 pf = smp[n].sm_rules;
9953 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009954 if (p0 == 1 && z == 0)
9955 {
9956 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009957 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
9958 || res[reslen - 1] == *s))
9959 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009960 z0 = 1;
9961 z = 1;
9962 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009963 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009964 {
9965 word[i + k0] = *s;
9966 k0++;
9967 s++;
9968 }
9969 if (k > k0)
9970 mch_memmove(word + i + k0, word + i + k,
9971 STRLEN(word + i + k) + 1);
9972
9973 /* new "actual letter" */
9974 c = word[i];
9975 }
9976 else
9977 {
9978 /* no '<' rule used */
9979 i += k - 1;
9980 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009981 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009982 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009983 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009984 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009985 s++;
9986 }
9987 /* new "actual letter" */
9988 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009989 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009990 {
9991 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009992 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009993 mch_memmove(word, word + i + 1,
9994 STRLEN(word + i + 1) + 1);
9995 i = 0;
9996 z0 = 1;
9997 }
9998 }
9999 break;
10000 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010001 }
10002 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010003 else if (vim_iswhite(c))
10004 {
10005 c = ' ';
10006 k = 1;
10007 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010008
10009 if (z0 == 0)
10010 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010011 if (k && !p0 && reslen < MAXWLEN && c != NUL
10012 && (!slang->sl_collapse || reslen == 0
10013 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010014 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010015 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010016
10017 i++;
10018 z = 0;
10019 k = 0;
10020 }
10021 }
10022
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010023 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010024}
10025
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010026#ifdef FEAT_MBYTE
10027/*
10028 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
10029 * Multi-byte version of spell_soundfold().
10030 */
10031 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010032spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010033 slang_T *slang;
10034 char_u *inword;
10035 char_u *res;
10036{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010037 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010038 int word[MAXWLEN];
10039 int wres[MAXWLEN];
10040 int l;
10041 char_u *s;
10042 int *ws;
10043 char_u *t;
10044 int *pf;
10045 int i, j, z;
10046 int reslen;
10047 int n, k = 0;
10048 int z0;
10049 int k0;
10050 int n0;
10051 int c;
10052 int pri;
10053 int p0 = -333;
10054 int c0;
10055 int did_white = FALSE;
10056
10057 /*
10058 * Convert the multi-byte string to a wide-character string.
10059 * Remove accents, if wanted. We actually remove all non-word characters.
10060 * But keep white space.
10061 */
10062 n = 0;
10063 for (s = inword; *s != NUL; )
10064 {
10065 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010066 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010067 if (slang->sl_rem_accents)
10068 {
10069 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
10070 {
10071 if (did_white)
10072 continue;
10073 c = ' ';
10074 did_white = TRUE;
10075 }
10076 else
10077 {
10078 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010079 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010080 continue;
10081 }
10082 }
10083 word[n++] = c;
10084 }
10085 word[n] = NUL;
10086
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010087 /*
10088 * This comes from Aspell phonet.cpp.
10089 * Converted from C++ to C. Added support for multi-byte chars.
10090 * Changed to keep spaces.
10091 */
10092 i = reslen = z = 0;
10093 while ((c = word[i]) != NUL)
10094 {
10095 /* Start with the first rule that has the character in the word. */
10096 n = slang->sl_sal_first[c & 0xff];
10097 z0 = 0;
10098
10099 if (n >= 0)
10100 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010101 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010102 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
10103 {
10104 /* Quickly skip entries that don't match the word. Most
10105 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010106 if (c != ws[0])
10107 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010108 k = smp[n].sm_leadlen;
10109 if (k > 1)
10110 {
10111 if (word[i + 1] != ws[1])
10112 continue;
10113 if (k > 2)
10114 {
10115 for (j = 2; j < k; ++j)
10116 if (word[i + j] != ws[j])
10117 break;
10118 if (j < k)
10119 continue;
10120 }
10121 }
10122
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010123 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010124 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010125 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010126 while (*pf != NUL && *pf != word[i + k])
10127 ++pf;
10128 if (*pf == NUL)
10129 continue;
10130 ++k;
10131 }
10132 s = smp[n].sm_rules;
10133 pri = 5; /* default priority */
10134
10135 p0 = *s;
10136 k0 = k;
10137 while (*s == '-' && k > 1)
10138 {
10139 k--;
10140 s++;
10141 }
10142 if (*s == '<')
10143 s++;
10144 if (VIM_ISDIGIT(*s))
10145 {
10146 /* determine priority */
10147 pri = *s - '0';
10148 s++;
10149 }
10150 if (*s == '^' && *(s + 1) == '^')
10151 s++;
10152
10153 if (*s == NUL
10154 || (*s == '^'
10155 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010156 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010157 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010158 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010159 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010160 && spell_iswordp_w(word + i - 1, curbuf)
10161 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010162 {
10163 /* search for followup rules, if: */
10164 /* followup and k > 1 and NO '-' in searchstring */
10165 c0 = word[i + k - 1];
10166 n0 = slang->sl_sal_first[c0 & 0xff];
10167
10168 if (slang->sl_followup && k > 1 && n0 >= 0
10169 && p0 != '-' && word[i + k] != NUL)
10170 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010171 /* Test follow-up rule for "word[i + k]"; loop over
10172 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010173 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
10174 == (c0 & 0xff); ++n0)
10175 {
10176 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010177 */
10178 if (c0 != ws[0])
10179 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010180 k0 = smp[n0].sm_leadlen;
10181 if (k0 > 1)
10182 {
10183 if (word[i + k] != ws[1])
10184 continue;
10185 if (k0 > 2)
10186 {
10187 pf = word + i + k + 1;
10188 for (j = 2; j < k0; ++j)
10189 if (*pf++ != ws[j])
10190 break;
10191 if (j < k0)
10192 continue;
10193 }
10194 }
10195 k0 += k - 1;
10196
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010197 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010198 {
10199 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010200 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010201 while (*pf != NUL && *pf != word[i + k0])
10202 ++pf;
10203 if (*pf == NUL)
10204 continue;
10205 ++k0;
10206 }
10207
10208 p0 = 5;
10209 s = smp[n0].sm_rules;
10210 while (*s == '-')
10211 {
10212 /* "k0" gets NOT reduced because
10213 * "if (k0 == k)" */
10214 s++;
10215 }
10216 if (*s == '<')
10217 s++;
10218 if (VIM_ISDIGIT(*s))
10219 {
10220 p0 = *s - '0';
10221 s++;
10222 }
10223
10224 if (*s == NUL
10225 /* *s == '^' cuts */
10226 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010227 && !spell_iswordp_w(word + i + k0,
10228 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010229 {
10230 if (k0 == k)
10231 /* this is just a piece of the string */
10232 continue;
10233
10234 if (p0 < pri)
10235 /* priority too low */
10236 continue;
10237 /* rule fits; stop search */
10238 break;
10239 }
10240 }
10241
10242 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
10243 == (c0 & 0xff))
10244 continue;
10245 }
10246
10247 /* replace string */
10248 ws = smp[n].sm_to_w;
10249 s = smp[n].sm_rules;
10250 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
10251 if (p0 == 1 && z == 0)
10252 {
10253 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010254 if (reslen > 0 && ws != NULL && *ws != NUL
10255 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010256 || wres[reslen - 1] == *ws))
10257 reslen--;
10258 z0 = 1;
10259 z = 1;
10260 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010261 if (ws != NULL)
10262 while (*ws != NUL && word[i + k0] != NUL)
10263 {
10264 word[i + k0] = *ws;
10265 k0++;
10266 ws++;
10267 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010268 if (k > k0)
10269 mch_memmove(word + i + k0, word + i + k,
10270 sizeof(int) * (STRLEN(word + i + k) + 1));
10271
10272 /* new "actual letter" */
10273 c = word[i];
10274 }
10275 else
10276 {
10277 /* no '<' rule used */
10278 i += k - 1;
10279 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010280 if (ws != NULL)
10281 while (*ws != NUL && ws[1] != NUL
10282 && reslen < MAXWLEN)
10283 {
10284 if (reslen == 0 || wres[reslen - 1] != *ws)
10285 wres[reslen++] = *ws;
10286 ws++;
10287 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010288 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010289 if (ws == NULL)
10290 c = NUL;
10291 else
10292 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010293 if (strstr((char *)s, "^^") != NULL)
10294 {
10295 if (c != NUL)
10296 wres[reslen++] = c;
10297 mch_memmove(word, word + i + 1,
10298 sizeof(int) * (STRLEN(word + i + 1) + 1));
10299 i = 0;
10300 z0 = 1;
10301 }
10302 }
10303 break;
10304 }
10305 }
10306 }
10307 else if (vim_iswhite(c))
10308 {
10309 c = ' ';
10310 k = 1;
10311 }
10312
10313 if (z0 == 0)
10314 {
10315 if (k && !p0 && reslen < MAXWLEN && c != NUL
10316 && (!slang->sl_collapse || reslen == 0
10317 || wres[reslen - 1] != c))
10318 /* condense only double letters */
10319 wres[reslen++] = c;
10320
10321 i++;
10322 z = 0;
10323 k = 0;
10324 }
10325 }
10326
10327 /* Convert wide characters in "wres" to a multi-byte string in "res". */
10328 l = 0;
10329 for (n = 0; n < reslen; ++n)
10330 {
10331 l += mb_char2bytes(wres[n], res + l);
10332 if (l + MB_MAXBYTES > MAXWLEN)
10333 break;
10334 }
10335 res[l] = NUL;
10336}
10337#endif
10338
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010339/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010340 * Compute a score for two sound-a-like words.
10341 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
10342 * Instead of a generic loop we write out the code. That keeps it fast by
10343 * avoiding checks that will not be possible.
10344 */
10345 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010346soundalike_score(goodstart, badstart)
10347 char_u *goodstart; /* sound-folded good word */
10348 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010349{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010350 char_u *goodsound = goodstart;
10351 char_u *badsound = badstart;
10352 int goodlen;
10353 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010354 int n;
10355 char_u *pl, *ps;
10356 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010357 int score = 0;
10358
10359 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
10360 * counted so much, vowels halfway the word aren't counted at all. */
10361 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
10362 {
10363 score = SCORE_DEL / 2;
10364 if (*badsound == '*')
10365 ++badsound;
10366 else
10367 ++goodsound;
10368 }
10369
10370 goodlen = STRLEN(goodsound);
10371 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010372
10373 /* Return quickly if the lenghts are too different to be fixed by two
10374 * changes. */
10375 n = goodlen - badlen;
10376 if (n < -2 || n > 2)
10377 return SCORE_MAXMAX;
10378
10379 if (n > 0)
10380 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010381 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010382 ps = badsound;
10383 }
10384 else
10385 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010386 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010387 ps = goodsound;
10388 }
10389
10390 /* Skip over the identical part. */
10391 while (*pl == *ps && *pl != NUL)
10392 {
10393 ++pl;
10394 ++ps;
10395 }
10396
10397 switch (n)
10398 {
10399 case -2:
10400 case 2:
10401 /*
10402 * Must delete two characters from "pl".
10403 */
10404 ++pl; /* first delete */
10405 while (*pl == *ps)
10406 {
10407 ++pl;
10408 ++ps;
10409 }
10410 /* strings must be equal after second delete */
10411 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010412 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010413
10414 /* Failed to compare. */
10415 break;
10416
10417 case -1:
10418 case 1:
10419 /*
10420 * Minimal one delete from "pl" required.
10421 */
10422
10423 /* 1: delete */
10424 pl2 = pl + 1;
10425 ps2 = ps;
10426 while (*pl2 == *ps2)
10427 {
10428 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010429 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010430 ++pl2;
10431 ++ps2;
10432 }
10433
10434 /* 2: delete then swap, then rest must be equal */
10435 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
10436 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010437 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010438
10439 /* 3: delete then substitute, then the rest must be equal */
10440 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010441 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010442
10443 /* 4: first swap then delete */
10444 if (pl[0] == ps[1] && pl[1] == ps[0])
10445 {
10446 pl2 = pl + 2; /* swap, skip two chars */
10447 ps2 = ps + 2;
10448 while (*pl2 == *ps2)
10449 {
10450 ++pl2;
10451 ++ps2;
10452 }
10453 /* delete a char and then strings must be equal */
10454 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010455 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010456 }
10457
10458 /* 5: first substitute then delete */
10459 pl2 = pl + 1; /* substitute, skip one char */
10460 ps2 = ps + 1;
10461 while (*pl2 == *ps2)
10462 {
10463 ++pl2;
10464 ++ps2;
10465 }
10466 /* delete a char and then strings must be equal */
10467 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010468 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010469
10470 /* Failed to compare. */
10471 break;
10472
10473 case 0:
10474 /*
10475 * Lenghts are equal, thus changes must result in same length: An
10476 * insert is only possible in combination with a delete.
10477 * 1: check if for identical strings
10478 */
10479 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010480 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010481
10482 /* 2: swap */
10483 if (pl[0] == ps[1] && pl[1] == ps[0])
10484 {
10485 pl2 = pl + 2; /* swap, skip two chars */
10486 ps2 = ps + 2;
10487 while (*pl2 == *ps2)
10488 {
10489 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010490 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010491 ++pl2;
10492 ++ps2;
10493 }
10494 /* 3: swap and swap again */
10495 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
10496 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010497 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010498
10499 /* 4: swap and substitute */
10500 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010501 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010502 }
10503
10504 /* 5: substitute */
10505 pl2 = pl + 1;
10506 ps2 = ps + 1;
10507 while (*pl2 == *ps2)
10508 {
10509 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010510 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010511 ++pl2;
10512 ++ps2;
10513 }
10514
10515 /* 6: substitute and swap */
10516 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
10517 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010518 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010519
10520 /* 7: substitute and substitute */
10521 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010522 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010523
10524 /* 8: insert then delete */
10525 pl2 = pl;
10526 ps2 = ps + 1;
10527 while (*pl2 == *ps2)
10528 {
10529 ++pl2;
10530 ++ps2;
10531 }
10532 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010533 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010534
10535 /* 9: delete then insert */
10536 pl2 = pl + 1;
10537 ps2 = ps;
10538 while (*pl2 == *ps2)
10539 {
10540 ++pl2;
10541 ++ps2;
10542 }
10543 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010544 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010545
10546 /* Failed to compare. */
10547 break;
10548 }
10549
10550 return SCORE_MAXMAX;
10551}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010552
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010553/*
10554 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010555 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010556 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010557 * The algorithm comes from Aspell editdist.cpp, edit_distance().
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010558 * It has been converted from C++ to C and modified to support multi-byte
10559 * characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010560 */
10561 static int
10562spell_edit_score(badword, goodword)
10563 char_u *badword;
10564 char_u *goodword;
10565{
10566 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010567 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010568 int j, i;
10569 int t;
10570 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010571 int pbc, pgc;
10572#ifdef FEAT_MBYTE
10573 char_u *p;
10574 int wbadword[MAXWLEN];
10575 int wgoodword[MAXWLEN];
10576
10577 if (has_mbyte)
10578 {
10579 /* Get the characters from the multi-byte strings and put them in an
10580 * int array for easy access. */
10581 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010582 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000010583 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010584 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010585 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000010586 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010587 }
10588 else
10589#endif
10590 {
10591 badlen = STRLEN(badword) + 1;
10592 goodlen = STRLEN(goodword) + 1;
10593 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010594
10595 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
10596#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010597 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
10598 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010599 if (cnt == NULL)
10600 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010601
10602 CNT(0, 0) = 0;
10603 for (j = 1; j <= goodlen; ++j)
10604 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
10605
10606 for (i = 1; i <= badlen; ++i)
10607 {
10608 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
10609 for (j = 1; j <= goodlen; ++j)
10610 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010611#ifdef FEAT_MBYTE
10612 if (has_mbyte)
10613 {
10614 bc = wbadword[i - 1];
10615 gc = wgoodword[j - 1];
10616 }
10617 else
10618#endif
10619 {
10620 bc = badword[i - 1];
10621 gc = goodword[j - 1];
10622 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010623 if (bc == gc)
10624 CNT(i, j) = CNT(i - 1, j - 1);
10625 else
10626 {
10627 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010628 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010629 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
10630 else
10631 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
10632
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010633 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010634 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010635#ifdef FEAT_MBYTE
10636 if (has_mbyte)
10637 {
10638 pbc = wbadword[i - 2];
10639 pgc = wgoodword[j - 2];
10640 }
10641 else
10642#endif
10643 {
10644 pbc = badword[i - 2];
10645 pgc = goodword[j - 2];
10646 }
10647 if (bc == pgc && pbc == gc)
10648 {
10649 t = SCORE_SWAP + CNT(i - 2, j - 2);
10650 if (t < CNT(i, j))
10651 CNT(i, j) = t;
10652 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010653 }
10654 t = SCORE_DEL + CNT(i - 1, j);
10655 if (t < CNT(i, j))
10656 CNT(i, j) = t;
10657 t = SCORE_INS + CNT(i, j - 1);
10658 if (t < CNT(i, j))
10659 CNT(i, j) = t;
10660 }
10661 }
10662 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010663
10664 i = CNT(badlen - 1, goodlen - 1);
10665 vim_free(cnt);
10666 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010667}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010668
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010669/*
10670 * ":spelldump"
10671 */
10672/*ARGSUSED*/
10673 void
10674ex_spelldump(eap)
10675 exarg_T *eap;
10676{
10677 buf_T *buf = curbuf;
10678 langp_T *lp;
10679 slang_T *slang;
10680 idx_T arridx[MAXWLEN];
10681 int curi[MAXWLEN];
10682 char_u word[MAXWLEN];
10683 int c;
10684 char_u *byts;
10685 idx_T *idxs;
10686 linenr_T lnum = 0;
10687 int round;
10688 int depth;
10689 int n;
10690 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000010691 char_u *region_names = NULL; /* region names being used */
10692 int do_region = TRUE; /* dump region names and numbers */
10693 char_u *p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010694
10695 if (no_spell_checking())
10696 return;
10697
10698 /* Create a new empty buffer by splitting the window. */
10699 do_cmdline_cmd((char_u *)"new");
10700 if (!bufempty() || !buf_valid(buf))
10701 return;
10702
Bram Moolenaar7887d882005-07-01 22:33:52 +000010703 /* Find out if we can support regions: All languages must support the same
10704 * regions or none at all. */
10705 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
10706 {
10707 p = lp->lp_slang->sl_regions;
10708 if (p[0] != 0)
10709 {
10710 if (region_names == NULL) /* first language with regions */
10711 region_names = p;
10712 else if (STRCMP(region_names, p) != 0)
10713 {
10714 do_region = FALSE; /* region names are different */
10715 break;
10716 }
10717 }
10718 }
10719
10720 if (do_region && region_names != NULL)
10721 {
10722 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
10723 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
10724 }
10725 else
10726 do_region = FALSE;
10727
10728 /*
10729 * Loop over all files loaded for the entries in 'spelllang'.
10730 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010731 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
10732 {
10733 slang = lp->lp_slang;
10734
10735 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
10736 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
10737
10738 /* round 1: case-folded tree
10739 * round 2: keep-case tree */
10740 for (round = 1; round <= 2; ++round)
10741 {
10742 if (round == 1)
10743 {
10744 byts = slang->sl_fbyts;
10745 idxs = slang->sl_fidxs;
10746 }
10747 else
10748 {
10749 byts = slang->sl_kbyts;
10750 idxs = slang->sl_kidxs;
10751 }
10752 if (byts == NULL)
10753 continue; /* array is empty */
10754
10755 depth = 0;
10756 arridx[0] = 0;
10757 curi[0] = 1;
10758 while (depth >= 0 && !got_int)
10759 {
10760 if (curi[depth] > byts[arridx[depth]])
10761 {
10762 /* Done all bytes at this node, go up one level. */
10763 --depth;
10764 line_breakcheck();
10765 }
10766 else
10767 {
10768 /* Do one more byte at this node. */
10769 n = arridx[depth] + curi[depth];
10770 ++curi[depth];
10771 c = byts[n];
10772 if (c == 0)
10773 {
10774 /* End of word, deal with the word.
10775 * Don't use keep-case words in the fold-case tree,
10776 * they will appear in the keep-case tree.
10777 * Only use the word when the region matches. */
10778 flags = (int)idxs[n];
10779 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +000010780 && (do_region
10781 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010782 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010783 & lp->lp_region) != 0))
10784 {
10785 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000010786 if (!do_region)
10787 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010788
10789 /* Dump the basic word if there is no prefix or
10790 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010791 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010792 if (c == 0 || curi[depth] == 2)
10793 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010794
10795 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010796 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010797 lnum = apply_prefixes(slang, word, round,
10798 flags, lnum);
10799 }
10800 }
10801 else
10802 {
10803 /* Normal char, go one level deeper. */
10804 word[depth++] = c;
10805 arridx[depth] = idxs[n];
10806 curi[depth] = 1;
10807 }
10808 }
10809 }
10810 }
10811 }
10812
10813 /* Delete the empty line that we started with. */
10814 if (curbuf->b_ml.ml_line_count > 1)
10815 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
10816
10817 redraw_later(NOT_VALID);
10818}
10819
10820/*
10821 * Dump one word: apply case modifications and append a line to the buffer.
10822 */
10823 static void
10824dump_word(word, round, flags, lnum)
10825 char_u *word;
10826 int round;
10827 int flags;
10828 linenr_T lnum;
10829{
10830 int keepcap = FALSE;
10831 char_u *p;
10832 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000010833 char_u badword[MAXWLEN + 10];
10834 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010835
10836 if (round == 1 && (flags & WF_CAPMASK) != 0)
10837 {
10838 /* Need to fix case according to "flags". */
10839 make_case_word(word, cword, flags);
10840 p = cword;
10841 }
10842 else
10843 {
10844 p = word;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010845 if (round == 2 && ((captype(word, NULL) & WF_KEEPCAP) == 0
10846 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010847 keepcap = TRUE;
10848 }
10849
Bram Moolenaar7887d882005-07-01 22:33:52 +000010850 /* Add flags and regions after a slash. */
10851 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010852 {
Bram Moolenaar7887d882005-07-01 22:33:52 +000010853 STRCPY(badword, p);
10854 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010855 if (keepcap)
10856 STRCAT(badword, "=");
10857 if (flags & WF_BANNED)
10858 STRCAT(badword, "!");
10859 else if (flags & WF_RARE)
10860 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +000010861 if (flags & WF_REGION)
10862 for (i = 0; i < 7; ++i)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010863 if (flags & (0x10000 << i))
Bram Moolenaar7887d882005-07-01 22:33:52 +000010864 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010865 p = badword;
10866 }
10867
10868 ml_append(lnum, p, (colnr_T)0, FALSE);
10869}
10870
10871/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010872 * For ":spelldump": Find matching prefixes for "word". Prepend each to
10873 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010874 * Return the updated line number.
10875 */
10876 static linenr_T
10877apply_prefixes(slang, word, round, flags, startlnum)
10878 slang_T *slang;
10879 char_u *word; /* case-folded word */
10880 int round;
10881 int flags; /* flags with prefix ID */
10882 linenr_T startlnum;
10883{
10884 idx_T arridx[MAXWLEN];
10885 int curi[MAXWLEN];
10886 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000010887 char_u word_up[MAXWLEN];
10888 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010889 int c;
10890 char_u *byts;
10891 idx_T *idxs;
10892 linenr_T lnum = startlnum;
10893 int depth;
10894 int n;
10895 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010896 int i;
10897
Bram Moolenaar53805d12005-08-01 07:08:33 +000010898 /* if the word starts with a lower-case letter make the word with an
10899 * upper-case letter in word_up[]. */
10900 c = PTR2CHAR(word);
10901 if (SPELL_TOUPPER(c) != c)
10902 {
10903 onecap_copy(word, word_up, TRUE);
10904 has_word_up = TRUE;
10905 }
10906
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010907 byts = slang->sl_pbyts;
10908 idxs = slang->sl_pidxs;
10909 if (byts != NULL) /* array not is empty */
10910 {
10911 /*
10912 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010913 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010914 */
10915 depth = 0;
10916 arridx[0] = 0;
10917 curi[0] = 1;
10918 while (depth >= 0 && !got_int)
10919 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010920 n = arridx[depth];
10921 len = byts[n];
10922 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010923 {
10924 /* Done all bytes at this node, go up one level. */
10925 --depth;
10926 line_breakcheck();
10927 }
10928 else
10929 {
10930 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010931 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010932 ++curi[depth];
10933 c = byts[n];
10934 if (c == 0)
10935 {
10936 /* End of prefix, find out how many IDs there are. */
10937 for (i = 1; i < len; ++i)
10938 if (byts[n + i] != 0)
10939 break;
10940 curi[depth] += i - 1;
10941
Bram Moolenaar53805d12005-08-01 07:08:33 +000010942 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
10943 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010944 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010945 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010946 dump_word(prefix, round,
Bram Moolenaar53805d12005-08-01 07:08:33 +000010947 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010948 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010949 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000010950
10951 /* Check for prefix that matches the word when the
10952 * first letter is upper-case, but only if the prefix has
10953 * a condition. */
10954 if (has_word_up)
10955 {
10956 c = valid_word_prefix(i, n, flags, word_up, slang,
10957 TRUE);
10958 if (c != 0)
10959 {
10960 vim_strncpy(prefix + depth, word_up,
10961 MAXWLEN - depth - 1);
10962 dump_word(prefix, round,
10963 (c & WF_RAREPFX) ? (flags | WF_RARE)
10964 : flags, lnum++);
10965 }
10966 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010967 }
10968 else
10969 {
10970 /* Normal char, go one level deeper. */
10971 prefix[depth++] = c;
10972 arridx[depth] = idxs[n];
10973 curi[depth] = 1;
10974 }
10975 }
10976 }
10977 }
10978
10979 return lnum;
10980}
10981
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010982#if defined(FEAT_INS_EXPAND) || defined(PROTO)
10983static int spell_expand_need_cap;
10984
10985/*
10986 * Find start of the word in front of the cursor. We don't check if it is
10987 * badly spelled, with completion we can only change the word in front of the
10988 * cursor.
10989 * Used for Insert mode completion CTRL-X ?.
10990 * Returns the column number of the word.
10991 */
10992 int
10993spell_word_start(startcol)
10994 int startcol;
10995{
10996 char_u *line;
10997 char_u *p;
10998 int col = 0;
10999
11000 if (no_spell_checking())
11001 return startcol;
11002
11003 /* Find a word character before "startcol". */
11004 line = ml_get_curline();
11005 for (p = line + startcol; p > line; )
11006 {
11007 mb_ptr_back(line, p);
11008 if (spell_iswordp_nmw(p))
11009 break;
11010 }
11011
11012 /* Go back to start of the word. */
11013 while (p > line)
11014 {
11015 col = p - line;
11016 mb_ptr_back(line, p);
11017 if (!spell_iswordp(p, curbuf))
11018 break;
11019 col = 0;
11020 }
11021
11022 /* Need to check for 'spellcapcheck' now, the word is removed before
11023 * expand_spelling() is called. Therefore the ugly global variable. */
11024 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
11025
11026 return col;
11027}
11028
11029/*
11030 * Get list of spelling suggestions.
11031 * Used for Insert mode completion CTRL-X ?.
11032 * Returns the number of matches. The matches are in "matchp[]", array of
11033 * allocated strings.
11034 */
11035/*ARGSUSED*/
11036 int
11037expand_spelling(lnum, col, pat, matchp)
11038 linenr_T lnum;
11039 int col;
11040 char_u *pat;
11041 char_u ***matchp;
11042{
11043 garray_T ga;
11044
11045 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap);
11046 *matchp = ga.ga_data;
11047 return ga.ga_len;
11048}
11049#endif
11050
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000011051#endif /* FEAT_SYN_HL */