blob: 77d65020982450056f045b48c8eff61c67fa0fc3 [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 Moolenaar1d73c882005-06-19 22:48:47 +000038 * There is one additional tree for when prefixes are not applied when
39 * 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 Moolenaar51485f02005-06-04 21:55:20 +000053/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000054 * Use this to adjust the score after finding suggestions, based on the
55 * suggested word sounding like the bad word. This is much faster than doing
56 * it for every possible suggestion.
57 * Disadvantage: When "the" is typed as "hte" it sounds different and goes
58 * down in the list.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000059 * Used when 'spellsuggest' is set to "best".
60 */
61#define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4)
62
63/*
64 * The double scoring mechanism is based on the principle that there are two
65 * kinds of spelling mistakes:
66 * 1. You know how to spell the word, but mistype something. This results in
67 * a small editing distance (character swapped/omitted/inserted) and
68 * possibly a word that sounds completely different.
69 * 2. You don't know how to spell the word and type something that sounds
70 * right. The edit distance can be big but the word is similar after
71 * sound-folding.
72 * Since scores for these two mistakes will be very different we use a list
73 * for each.
74 * The sound-folding is slow, only do double scoring when 'spellsuggest' is
75 * "double".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000076 */
77
78/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +000079 * Vim spell file format: <HEADER>
80 * <SUGGEST>
81 * <LWORDTREE>
82 * <KWORDTREE>
83 * <PREFIXTREE>
Bram Moolenaar51485f02005-06-04 21:55:20 +000084 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000085 * <HEADER>: <fileID>
86 * <regioncnt> <regionname> ...
87 * <charflagslen> <charflags>
88 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000089 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +000090 * <prefcondcnt> <prefcond> ...
Bram Moolenaar51485f02005-06-04 21:55:20 +000091 *
Bram Moolenaar53805d12005-08-01 07:08:33 +000092 * <fileID> 10 bytes "VIMspell09"
Bram Moolenaar51485f02005-06-04 21:55:20 +000093 * <regioncnt> 1 byte number of regions following (8 supported)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000094 * <regionname> 2 bytes Region name: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +000095 * First <regionname> is region 1.
96 *
97 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
98 * <charflags> N bytes List of flags (first one is for character 128):
Bram Moolenaar9f30f502005-06-14 22:01:04 +000099 * 0x01 word character CF_WORD
100 * 0x02 upper-case character CF_UPPER
Bram Moolenaar51485f02005-06-04 21:55:20 +0000101 * <fcharslen> 2 bytes Number of bytes in <fchars>.
102 * <fchars> N bytes Folded characters, first one is for character 128.
103 *
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000104 * <midwordlen> 2 bytes Number of bytes in <midword>.
105 * <midword> N bytes Characters that are word characters only when used
106 * in the middle of a word.
107 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000108 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
109 *
110 * <prefcond> : <condlen> <condstr>
111 *
112 * <condlen> 1 byte Length of <condstr>.
113 *
114 * <condstr> N bytes Condition for the prefix.
115 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000116 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000117 * <SUGGEST> : <repcount> <rep> ...
118 * <salflags> <salcount> <sal> ...
119 * <maplen> <mapstr>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000120 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000121 * <repcount> 2 bytes number of <rep> items, MSB first.
122 *
123 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
124 *
125 * <repfromlen> 1 byte length of <repfrom>
126 *
127 * <repfrom> N bytes "from" part of replacement
128 *
129 * <reptolen> 1 byte length of <repto>
130 *
131 * <repto> N bytes "to" part of replacement
132 *
133 * <salflags> 1 byte flags for soundsalike conversion:
134 * SAL_F0LLOWUP
135 * SAL_COLLAPSE
136 * SAL_REM_ACCENTS
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000137 * SAL_SOFO: SOFOFROM and SOFOTO used instead of SAL
138 *
139 * <salcount> 2 bytes number of <sal> items following
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000140 *
141 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
142 *
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000143 * <salfromlen> 1-2 bytes length of <salfrom> (2 bytes for SAL_SOFO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000144 *
145 * <salfrom> N bytes "from" part of soundsalike
146 *
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000147 * <saltolen> 1-2 bytes length of <salto> (2 bytes for SAL_SOFO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000148 *
149 * <salto> N bytes "to" part of soundsalike
150 *
151 * <maplen> 2 bytes length of <mapstr>, MSB first
152 *
153 * <mapstr> N bytes String with sequences of similar characters,
154 * separated by slashes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000155 *
156 *
157 * <LWORDTREE>: <wordtree>
158 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000159 * <KWORDTREE>: <wordtree>
160 *
161 * <PREFIXTREE>: <wordtree>
162 *
163 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000164 * <wordtree>: <nodecount> <nodedata> ...
165 *
166 * <nodecount> 4 bytes Number of nodes following. MSB first.
167 *
168 * <nodedata>: <siblingcount> <sibling> ...
169 *
170 * <siblingcount> 1 byte Number of siblings in this node. The siblings
171 * follow in sorted order.
172 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000173 * <sibling>: <byte> [ <nodeidx> <xbyte>
Bram Moolenaar53805d12005-08-01 07:08:33 +0000174 * | <flags> [<flags2>] [<region>] [<prefixID>]
175 * | [<pflags>] <prefixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000176 *
177 * <byte> 1 byte Byte value of the sibling. Special cases:
178 * BY_NOFLAGS: End of word without flags and for all
179 * regions.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000180 * For PREFIXTREE <prefixID> and
181 * <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000182 * BY_FLAGS: End of word, <flags> follow.
Bram Moolenaar53805d12005-08-01 07:08:33 +0000183 * For PREFIXTREE <pflags>, <prefixID>
184 * and <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000185 * BY_FLAGS2: End of word, <flags> and <flags2>
186 * follow. Not used in PREFIXTREE.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000187 * BY_INDEX: Child of sibling is shared, <nodeidx>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000188 * and <xbyte> follow.
189 *
190 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
191 *
192 * <xbyte> 1 byte byte value of the sibling.
193 *
194 * <flags> 1 byte bitmask of:
195 * WF_ALLCAP word must have only capitals
196 * WF_ONECAP first char of word must be capital
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000197 * WF_KEEPCAP keep-case word
198 * WF_FIXCAP keep-case word, all caps not allowed
Bram Moolenaar51485f02005-06-04 21:55:20 +0000199 * WF_RARE rare word
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000200 * WF_BANNED bad word
Bram Moolenaar51485f02005-06-04 21:55:20 +0000201 * WF_REGION <region> follows
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000202 * WF_PFX <prefixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000203 *
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000204 * <flags2> 1 byte Only used when there are postponed prefixes.
205 * Bitmask of:
206 * WF_HAS_AFF >> 8 word includes affix
207 *
Bram Moolenaar53805d12005-08-01 07:08:33 +0000208 * <pflags> 1 byte bitmask of:
209 * WFP_RARE rare prefix
210 * WFP_NC non-combining prefix
211 * WFP_UP letter after prefix made upper case
212 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000213 * <region> 1 byte Bitmask for regions in which word is valid. When
214 * omitted it's valid in all regions.
215 * Lowest bit is for region 1.
216 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000217 * <prefixID> 1 byte ID of prefix that can be used with this word. For
218 * PREFIXTREE used for the required prefix ID.
219 *
220 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
221 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000222 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000223 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000224 */
225
Bram Moolenaare19defe2005-03-21 08:23:33 +0000226#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
227# include <io.h> /* for lseek(), must be before vim.h */
228#endif
229
230#include "vim.h"
231
232#if defined(FEAT_SYN_HL) || defined(PROTO)
233
234#ifdef HAVE_FCNTL_H
235# include <fcntl.h>
236#endif
237
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000238#define MAXWLEN 250 /* Assume max. word len is this many bytes.
239 Some places assume a word length fits in a
240 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000241
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000242/* Type used for indexes in the word tree need to be at least 3 bytes. If int
243 * is 8 bytes we could use something smaller, but what? */
244#if SIZEOF_INT > 2
245typedef int idx_T;
246#else
247typedef long idx_T;
248#endif
249
250/* Flags used for a word. Only the lowest byte can be used, the region byte
251 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000252#define WF_REGION 0x01 /* region byte follows */
253#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
254#define WF_ALLCAP 0x04 /* word must be all capitals */
255#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000256#define WF_BANNED 0x10 /* bad word */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000257#define WF_PFX 0x20 /* prefix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000258#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000259#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000260
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000261/* for <flags2>, shifted up one byte to be used in wn_flags */
262#define WF_HAS_AFF 0x0100 /* word includes affix */
263
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000264#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000265
Bram Moolenaar53805d12005-08-01 07:08:33 +0000266/* flags for <pflags> */
267#define WFP_RARE 0x01 /* rare prefix */
268#define WFP_NC 0x02 /* prefix is not combining */
269#define WFP_UP 0x04 /* to-upper prefix */
270
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000271/* flags for postponed prefixes. Must be above prefixID (one byte)
272 * and prefcondnr (two bytes). */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000273#define WF_RAREPFX (WFP_RARE << 24) /* in sl_pidxs: flag for rare
274 * postponed prefix */
275#define WF_PFX_NC (WFP_NC << 24) /* in sl_pidxs: flag for non-combining
276 * postponed prefix */
277#define WF_PFX_UP (WFP_UP << 24) /* in sl_pidxs: flag for to-upper
278 * postponed prefix */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000279
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000280/* Special byte values for <byte>. Some are only used in the tree for
281 * postponed prefixes, some only in the other trees. This is a bit messy... */
282#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000283 * postponed prefix: no <pflags> */
284#define BY_INDEX 1 /* child is shared, index follows */
285#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
286 * postponed prefix: <pflags> follows */
287#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
288 * follow; never used in prefix tree */
289#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000290
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000291/* Info from "REP" and "SAL" entries in ".aff" file used in si_rep, sl_rep,
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000292 * and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000293 * One replacement: from "ft_from" to "ft_to". */
294typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000295{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000296 char_u *ft_from;
297 char_u *ft_to;
298} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000299
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000300/* Info from "SAL" entries in ".aff" file used in sl_sal.
301 * The info is split for quick processing by spell_soundfold().
302 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
303typedef struct salitem_S
304{
305 char_u *sm_lead; /* leading letters */
306 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000307 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000308 char_u *sm_rules; /* rules like ^, $, priority */
309 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000310#ifdef FEAT_MBYTE
311 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000312 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000313 int *sm_to_w; /* wide character copy of "sm_to" */
314#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000315} salitem_T;
316
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000317#ifdef FEAT_MBYTE
318typedef int salfirst_T;
319#else
320typedef short salfirst_T;
321#endif
322
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000323/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000324 * Structure used to store words and other info for one language, loaded from
325 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000326 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
327 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
328 *
329 * The "byts" array stores the possible bytes in each tree node, preceded by
330 * the number of possible bytes, sorted on byte value:
331 * <len> <byte1> <byte2> ...
332 * The "idxs" array stores the index of the child node corresponding to the
333 * byte in "byts".
334 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000335 * the flags, region mask and prefixID for the word. There may be several
336 * zeros in sequence for alternative flag/region combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000337 */
338typedef struct slang_S slang_T;
339struct slang_S
340{
341 slang_T *sl_next; /* next language */
342 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000343 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000344 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000345
Bram Moolenaar51485f02005-06-04 21:55:20 +0000346 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000347 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000348 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000349 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000350 char_u *sl_pbyts; /* prefix tree word bytes */
351 idx_T *sl_pidxs; /* prefix tree word indexes */
352
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000353 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000354
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000355 char_u *sl_midword; /* MIDWORD string or NULL */
356
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000357 int sl_prefixcnt; /* number of items in "sl_prefprog" */
358 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
359
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000360 garray_T sl_rep; /* list of fromto_T entries from REP lines */
361 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
362 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000363 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000364 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000365 there is none */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000366 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
367 * "sl_sal_first" maps chars, when has_mbyte
368 * "sl_sal" is a list of wide char lists. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000369 int sl_followup; /* SAL followup */
370 int sl_collapse; /* SAL collapse_result */
371 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaarea424162005-06-16 21:51:00 +0000372 int sl_has_map; /* TRUE if there is a MAP line */
373#ifdef FEAT_MBYTE
374 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
375 int sl_map_array[256]; /* MAP for first 256 chars */
376#else
377 char_u sl_map_array[256]; /* MAP for first 256 chars */
378#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000379};
380
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000381/* First language that is loaded, start of the linked list of loaded
382 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000383static slang_T *first_lang = NULL;
384
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000385/* Flags used in .spl file for soundsalike flags. */
386#define SAL_F0LLOWUP 1
387#define SAL_COLLAPSE 2
388#define SAL_REM_ACCENTS 4
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000389#define SAL_SOFO 8 /* SOFOFROM and SOFOTO instead of SAL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000390
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000391/*
392 * Structure used in "b_langp", filled from 'spelllang'.
393 */
394typedef struct langp_S
395{
396 slang_T *lp_slang; /* info for this language (NULL for last one) */
397 int lp_region; /* bitmask for region or REGION_ALL */
398} langp_T;
399
400#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
401
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000402#define REGION_ALL 0xff /* word valid in all regions */
403
404/* Result values. Lower number is accepted over higher one. */
405#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000406#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000407#define SP_RARE 1
408#define SP_LOCAL 2
409#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000410
Bram Moolenaar53805d12005-08-01 07:08:33 +0000411#define VIMSPELLMAGIC "VIMspell09" /* string at start of Vim spell file */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000412#define VIMSPELLMAGICL 10
413
Bram Moolenaar7887d882005-07-01 22:33:52 +0000414/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000415static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000416
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000417/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000418 * Information used when looking for suggestions.
419 */
420typedef struct suginfo_S
421{
422 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000423 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000424 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000425 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000426 char_u *su_badptr; /* start of bad word in line */
427 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000428 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000429 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
430 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
431 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000432} suginfo_T;
433
434/* One word suggestion. Used in "si_ga". */
435typedef struct suggest_S
436{
437 char_u *st_word; /* suggested word, allocated string */
438 int st_orglen; /* length of replaced text */
439 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000440 int st_altscore; /* used when st_score compares equal */
441 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000442 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000443} suggest_T;
444
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000445#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000446
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000447/* Number of suggestions kept when cleaning up. When rescore_suggestions() is
448 * called the score may change, thus we need to keep more than what is
449 * displayed. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000450#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 50 ? 50 : (su)->su_maxcount)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000451
452/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
453 * of suggestions that are not going to be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000454#define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000455
456/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000457#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000458#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000459#define SCORE_REGION 70 /* word is for different region */
460#define SCORE_RARE 180 /* rare word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000461#define SCORE_SWAP 90 /* swap two characters */
462#define SCORE_SWAP3 110 /* swap two characters in three */
463#define SCORE_REP 87 /* REP replacement */
464#define SCORE_SUBST 93 /* substitute a character */
465#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000466#define SCORE_DEL 94 /* delete a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000467#define SCORE_DELDUP 64 /* delete a duplicated character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000468#define SCORE_INS 96 /* insert a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000469#define SCORE_INSDUP 66 /* insert a duplicate character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000470#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000471
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000472#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000473#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
474 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000475
476#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000477#define SCORE_MAXMAX 999999 /* accept any score */
478
479/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000480 * Structure to store info for word matching.
481 */
482typedef struct matchinf_S
483{
484 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000485
486 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000487 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000488 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000489 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000490 char_u *mi_cend; /* char after what was used for
491 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000492
493 /* case-folded text */
494 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000495 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000496
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000497 /* for when checking word after a prefix */
498 int mi_prefarridx; /* index in sl_pidxs with list of
499 prefixID/condition */
500 int mi_prefcnt; /* number of entries at mi_prefarridx */
501 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000502#ifdef FEAT_MBYTE
503 int mi_cprefixlen; /* byte length of prefix in original
504 case */
505#else
506# define mi_cprefixlen mi_prefixlen /* it's the same value */
507#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000508
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000509 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000510 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000511 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000512 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000513} matchinf_T;
514
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000515/*
516 * The tables used for recognizing word characters according to spelling.
517 * These are only used for the first 256 characters of 'encoding'.
518 */
519typedef struct spelltab_S
520{
521 char_u st_isw[256]; /* flags: is word char */
522 char_u st_isu[256]; /* flags: is uppercase char */
523 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000524 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000525} spelltab_T;
526
527static spelltab_T spelltab;
528static int did_set_spelltab;
529
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000530#define CF_WORD 0x01
531#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000532
533static void clear_spell_chartab __ARGS((spelltab_T *sp));
534static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000535static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
536static int spell_iswordp_nmw __ARGS((char_u *p));
537#ifdef FEAT_MBYTE
538static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
539#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000540static void write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000541
542/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000543 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000544 */
545typedef enum
546{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000547 STATE_START = 0, /* At start of node check for NUL bytes (goodword
548 * ends); if badword ends there is a match, otherwise
549 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000550 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000551 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000552 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
553 STATE_PLAIN, /* Use each byte of the node. */
554 STATE_DEL, /* Delete a byte from the bad word. */
555 STATE_INS, /* Insert a byte in the bad word. */
556 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000557 STATE_UNSWAP, /* Undo swap two characters. */
558 STATE_SWAP3, /* Swap two characters over three. */
559 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000560 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000561 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000562 STATE_REP_INI, /* Prepare for using REP items. */
563 STATE_REP, /* Use matching REP items from the .aff file. */
564 STATE_REP_UNDO, /* Undo a REP item replacement. */
565 STATE_FINAL /* End of this node. */
566} state_T;
567
568/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000569 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000570 */
571typedef struct trystate_S
572{
Bram Moolenaarea424162005-06-16 21:51:00 +0000573 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000574 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000575 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000576 short ts_curi; /* index in list of child nodes */
577 char_u ts_fidx; /* index in fword[], case-folded bad word */
578 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
579 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000580 char_u ts_prefixdepth; /* stack depth for end of prefix or PREFIXTREE
581 * or NOPREFIX */
Bram Moolenaarea424162005-06-16 21:51:00 +0000582#ifdef FEAT_MBYTE
583 char_u ts_tcharlen; /* number of bytes in tword character */
584 char_u ts_tcharidx; /* current byte index in tword character */
585 char_u ts_isdiff; /* DIFF_ values */
586 char_u ts_fcharstart; /* index in fword where badword char started */
587#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000588 char_u ts_save_prewordlen; /* saved "prewordlen" */
Bram Moolenaarea424162005-06-16 21:51:00 +0000589 char_u ts_save_splitoff; /* su_splitoff saved here */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000590 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000591} trystate_T;
592
Bram Moolenaarea424162005-06-16 21:51:00 +0000593/* values for ts_isdiff */
594#define DIFF_NONE 0 /* no different byte (yet) */
595#define DIFF_YES 1 /* different byte found */
596#define DIFF_INSERT 2 /* inserting character */
597
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000598/* special values ts_prefixdepth */
599#define PREFIXTREE 0xfe /* walking through the prefix tree */
600#define NOPREFIX 0xff /* not using prefixes */
601
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000602/* mode values for find_word */
603#define FIND_FOLDWORD 0 /* find word case-folded */
604#define FIND_KEEPWORD 1 /* find keep-case word */
605#define FIND_PREFIX 2 /* find word after prefix */
606
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000607static slang_T *slang_alloc __ARGS((char_u *lang));
608static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000609static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000610static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000611static 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 +0000612static void find_prefix __ARGS((matchinf_T *mip));
613static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000614static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000615static int no_spell_checking __ARGS((void));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000616static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000617static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000618static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000619static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000620static 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 +0000621static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000622static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
623static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000624#ifdef FEAT_MBYTE
625static int *mb_str2wide __ARGS((char_u *s));
626#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000627static 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 +0000628static void clear_midword __ARGS((buf_T *buf));
629static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000630static int find_region __ARGS((char_u *rp, char_u *region));
631static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000632static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000633static int set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000634static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
635static void write_spell_chartab __ARGS((FILE *fd));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000636static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +0000637static 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 +0000638#ifdef FEAT_EVAL
639static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
640#endif
641static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
642static void spell_suggest_intern __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000643static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000644static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000645static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000646static void suggest_try_special __ARGS((suginfo_T *su));
647static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000648static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000649#ifdef FEAT_MBYTE
650static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
651#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000652static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000653static void score_comp_sal __ARGS((suginfo_T *su));
654static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000655static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000656static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000657static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000658static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000659static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000660static 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 +0000661static void add_banned __ARGS((suginfo_T *su, char_u *word));
662static int was_banned __ARGS((suginfo_T *su, char_u *word));
663static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000664static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000665static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000666static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
667static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
668static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000669#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000670static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000671#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000672static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000673static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000674static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum));
675static 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 +0000676
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000677/*
678 * Use our own character-case definitions, because the current locale may
679 * differ from what the .spl file uses.
680 * These must not be called with negative number!
681 */
682#ifndef FEAT_MBYTE
683/* Non-multi-byte implementation. */
684# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
685# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
686# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
687#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000688# if defined(HAVE_WCHAR_H)
689# include <wchar.h> /* for towupper() and towlower() */
690# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000691/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
692 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
693 * the "w" library function for characters above 255 if available. */
694# ifdef HAVE_TOWLOWER
695# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
696 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
697# else
698# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
699 : (c) < 256 ? spelltab.st_fold[c] : (c))
700# endif
701
702# ifdef HAVE_TOWUPPER
703# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
704 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
705# else
706# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
707 : (c) < 256 ? spelltab.st_upper[c] : (c))
708# endif
709
710# ifdef HAVE_ISWUPPER
711# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
712 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
713# else
714# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
715 : (c) < 256 ? spelltab.st_isu[c] : (c))
716# endif
717#endif
718
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000719
720static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000721static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000722
723/*
724 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000725 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000726 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
727 * or when it's OK it remains unchanged.
728 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000729 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000730 * "capcol" is used to check for a Capitalised word after the end of a
731 * sentence. If it's zero then perform the check. Return the column where to
732 * check next, or -1 when no sentence end was found. If it's NULL then don't
733 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000734 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000735 * Returns the length of the word in bytes, also when it's OK, so that the
736 * caller can skip over the word.
737 */
738 int
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000739spell_check(wp, ptr, attrp, capcol)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000740 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000741 char_u *ptr;
742 int *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000743 int *capcol; /* column to check for Capital */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000744{
745 matchinf_T mi; /* Most things are put in "mi" so that it can
746 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000747 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000748 int c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000749
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000750 /* A word never starts at a space or a control character. Return quickly
751 * then, skipping over the character. */
752 if (*ptr <= ' ')
753 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000754
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000755 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar0c405862005-06-22 22:26:26 +0000756 * 0X99FF. But when a word character follows do check spelling to find
757 * "3GPP". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000758 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000759 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000760 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
761 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000762 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000763 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000764 mi.mi_end = skipdigits(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000765 nrlen = mi.mi_end - ptr;
766 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000767 if (!spell_iswordp(mi.mi_end, wp->w_buffer))
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000768 return (int)(mi.mi_end - ptr);
Bram Moolenaar0c405862005-06-22 22:26:26 +0000769
770 /* Try including the digits in the word. */
771 mi.mi_fend = ptr + nrlen;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000772 }
Bram Moolenaar0c405862005-06-22 22:26:26 +0000773 else
774 mi.mi_fend = ptr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000775
Bram Moolenaar0c405862005-06-22 22:26:26 +0000776 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000777 mi.mi_word = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000778 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000779 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000780 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000781 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000782 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000783 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000784
785 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
786 {
787 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000788 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000789 if (!SPELL_ISUPPER(c))
790 {
791 *attrp = highlight_attr[HLF_SPC];
792 return (int)(mi.mi_fend - ptr);
793 }
794 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000795 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000796 if (capcol != NULL)
797 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000798
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000799 /* We always use the characters up to the next non-word character,
800 * also for bad words. */
801 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000802
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000803 /* Check caps type later. */
804 mi.mi_capflags = 0;
805 mi.mi_cend = NULL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000806 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000807
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000808 /* Include one non-word character so that we can check for the
809 * word end. */
810 if (*mi.mi_fend != NUL)
811 mb_ptr_adv(mi.mi_fend);
812
813 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
814 MAXWLEN + 1);
815 mi.mi_fwordlen = STRLEN(mi.mi_fword);
816
817 /* The word is bad unless we recognize it. */
818 mi.mi_result = SP_BAD;
819
820 /*
821 * Loop over the languages specified in 'spelllang'.
822 * We check them all, because a matching word may be longer than an
823 * already found matching word.
824 */
825 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
826 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
827 {
828 /* Check for a matching word in case-folded words. */
829 find_word(&mi, FIND_FOLDWORD);
830
831 /* Check for a matching word in keep-case words. */
832 find_word(&mi, FIND_KEEPWORD);
833
834 /* Check for matching prefixes. */
835 find_prefix(&mi);
836 }
837
838 if (mi.mi_result != SP_OK)
839 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000840 /* If we found a number skip over it. Allows for "42nd". Do flag
841 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000842 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000843 {
844 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
845 return nrlen;
846 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000847
848 /* When we are at a non-word character there is no error, just
849 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000850 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000851 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000852 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
853 {
854 regmatch_T regmatch;
855
856 /* Check for end of sentence. */
857 regmatch.regprog = wp->w_buffer->b_cap_prog;
858 regmatch.rm_ic = FALSE;
859 if (vim_regexec(&regmatch, ptr, 0))
860 *capcol = (int)(regmatch.endp[0] - ptr);
861 }
862
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000863#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000864 if (has_mbyte)
865 return mb_ptr2len_check(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000866#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000867 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000868 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000869
870 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
871 *attrp = highlight_attr[HLF_SPB];
872 else if (mi.mi_result == SP_RARE)
873 *attrp = highlight_attr[HLF_SPR];
874 else
875 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000876 }
877
Bram Moolenaar51485f02005-06-04 21:55:20 +0000878 return (int)(mi.mi_end - ptr);
879}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000880
Bram Moolenaar51485f02005-06-04 21:55:20 +0000881/*
882 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000883 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
884 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
885 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
886 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000887 *
888 * For a match mip->mi_result is updated.
889 */
890 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000891find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000892 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000893 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000894{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000895 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000896 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000897 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000898 int endidxcnt = 0;
899 int len;
900 int wlen = 0;
901 int flen;
902 int c;
903 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000904 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000905#ifdef FEAT_MBYTE
906 char_u *s;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000907 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000908#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000909 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000910 slang_T *slang = mip->mi_lp->lp_slang;
911 unsigned flags;
912 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000913 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000914
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000915 if (mode == FIND_KEEPWORD)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000916 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000917 /* Check for word with matching case in keep-case tree. */
918 ptr = mip->mi_word;
919 flen = 9999; /* no case folding, always enough bytes */
920 byts = slang->sl_kbyts;
921 idxs = slang->sl_kidxs;
922 }
923 else
924 {
925 /* Check for case-folded in case-folded tree. */
926 ptr = mip->mi_fword;
927 flen = mip->mi_fwordlen; /* available case-folded bytes */
928 byts = slang->sl_fbyts;
929 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000930
931 if (mode == FIND_PREFIX)
932 {
933 /* Skip over the prefix. */
934 wlen = mip->mi_prefixlen;
935 flen -= mip->mi_prefixlen;
936 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000937 }
938
Bram Moolenaar51485f02005-06-04 21:55:20 +0000939 if (byts == NULL)
940 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000941
Bram Moolenaar51485f02005-06-04 21:55:20 +0000942 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000943 * Repeat advancing in the tree until:
944 * - there is a byte that doesn't match,
945 * - we reach the end of the tree,
946 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000947 */
948 for (;;)
949 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000950 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000951 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000952
953 len = byts[arridx++];
954
955 /* If the first possible byte is a zero the word could end here.
956 * Remember this index, we first check for the longest word. */
957 if (byts[arridx] == 0)
958 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000959 if (endidxcnt == MAXWLEN)
960 {
961 /* Must be a corrupted spell file. */
962 EMSG(_(e_format));
963 return;
964 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000965 endlen[endidxcnt] = wlen;
966 endidx[endidxcnt++] = arridx++;
967 --len;
968
969 /* Skip over the zeros, there can be several flag/region
970 * combinations. */
971 while (len > 0 && byts[arridx] == 0)
972 {
973 ++arridx;
974 --len;
975 }
976 if (len == 0)
977 break; /* no children, word must end here */
978 }
979
980 /* Stop looking at end of the line. */
981 if (ptr[wlen] == NUL)
982 break;
983
984 /* Perform a binary search in the list of accepted bytes. */
985 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +0000986 if (c == TAB) /* <Tab> is handled like <Space> */
987 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +0000988 lo = arridx;
989 hi = arridx + len - 1;
990 while (lo < hi)
991 {
992 m = (lo + hi) / 2;
993 if (byts[m] > c)
994 hi = m - 1;
995 else if (byts[m] < c)
996 lo = m + 1;
997 else
998 {
999 lo = hi = m;
1000 break;
1001 }
1002 }
1003
1004 /* Stop if there is no matching byte. */
1005 if (hi < lo || byts[lo] != c)
1006 break;
1007
1008 /* Continue at the child (if there is one). */
1009 arridx = idxs[lo];
1010 ++wlen;
1011 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001012
1013 /* One space in the good word may stand for several spaces in the
1014 * checked word. */
1015 if (c == ' ')
1016 {
1017 for (;;)
1018 {
1019 if (flen <= 0 && *mip->mi_fend != NUL)
1020 flen = fold_more(mip);
1021 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1022 break;
1023 ++wlen;
1024 --flen;
1025 }
1026 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001027 }
1028
1029 /*
1030 * Verify that one of the possible endings is valid. Try the longest
1031 * first.
1032 */
1033 while (endidxcnt > 0)
1034 {
1035 --endidxcnt;
1036 arridx = endidx[endidxcnt];
1037 wlen = endlen[endidxcnt];
1038
1039#ifdef FEAT_MBYTE
1040 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1041 continue; /* not at first byte of character */
1042#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001043 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001044 continue; /* next char is a word character */
1045
1046#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001047 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001048 {
1049 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001050 * when folding case. This can be slow, take a shortcut when the
1051 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001052 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001053 if (STRNCMP(ptr, p, wlen) != 0)
1054 {
1055 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1056 mb_ptr_adv(p);
1057 wlen = p - mip->mi_word;
1058 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001059 }
1060#endif
1061
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001062 /* Check flags and region. For FIND_PREFIX check the condition and
1063 * prefix ID.
1064 * Repeat this if there are more flags/region alternatives until there
1065 * is a match. */
1066 res = SP_BAD;
1067 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1068 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001069 {
1070 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001071
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001072 /* For the fold-case tree check that the case of the checked word
1073 * matches with what the word in the tree requires.
1074 * For keep-case tree the case is always right. For prefixes we
1075 * don't bother to check. */
1076 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001077 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001078 if (mip->mi_cend != mip->mi_word + wlen)
1079 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001080 /* mi_capflags was set for a different word length, need
1081 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001082 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001083 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001084 }
1085
Bram Moolenaar0c405862005-06-22 22:26:26 +00001086 if (mip->mi_capflags == WF_KEEPCAP
1087 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001088 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001089 }
1090
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001091 /* When mode is FIND_PREFIX the word must support the prefix:
1092 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001093 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001094 if (mode == FIND_PREFIX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001095 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001096 /* The prefix ID is stored two bytes above the flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001097 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001098 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001099 mip->mi_word + mip->mi_cprefixlen, slang,
1100 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001101 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001102 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001103
1104 /* Use the WF_RARE flag for a rare prefix. */
1105 if (c & WF_RAREPFX)
1106 flags |= WF_RARE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001107 }
1108
1109 if (flags & WF_BANNED)
1110 res = SP_BANNED;
1111 else if (flags & WF_REGION)
1112 {
1113 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001114 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001115 res = SP_OK;
1116 else
1117 res = SP_LOCAL;
1118 }
1119 else if (flags & WF_RARE)
1120 res = SP_RARE;
1121 else
1122 res = SP_OK;
1123
1124 /* Always use the longest match and the best result. */
1125 if (mip->mi_result > res)
1126 {
1127 mip->mi_result = res;
1128 mip->mi_end = mip->mi_word + wlen;
1129 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001130 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001131 mip->mi_end = mip->mi_word + wlen;
1132
1133 if (res == SP_OK)
1134 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001135 }
1136
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001137 if (res == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001138 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001139 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001140}
1141
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001142/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001143 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1144 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001145 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001146 */
1147 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001148valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001149 int totprefcnt; /* nr of prefix IDs */
1150 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001151 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001152 char_u *word;
1153 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001154 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001155{
1156 int prefcnt;
1157 int pidx;
1158 regprog_T *rp;
1159 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001160 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001161
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001162 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001163 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1164 {
1165 pidx = slang->sl_pidxs[arridx + prefcnt];
1166
1167 /* Check the prefix ID. */
1168 if (prefid != (pidx & 0xff))
1169 continue;
1170
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001171 /* Check if the prefix doesn't combine and the word already has a
1172 * suffix. */
1173 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1174 continue;
1175
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001176 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001177 * stored in the two bytes above the prefix ID byte. */
1178 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001179 if (rp != NULL)
1180 {
1181 regmatch.regprog = rp;
1182 regmatch.rm_ic = FALSE;
1183 if (!vim_regexec(&regmatch, word, 0))
1184 continue;
1185 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001186 else if (cond_req)
1187 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001188
Bram Moolenaar53805d12005-08-01 07:08:33 +00001189 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001190 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001191 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001192 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001193}
1194
1195/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001196 * Check if the word at "mip->mi_word" has a matching prefix.
1197 * If it does, then check the following word.
1198 *
1199 * For a match mip->mi_result is updated.
1200 */
1201 static void
1202find_prefix(mip)
1203 matchinf_T *mip;
1204{
1205 idx_T arridx = 0;
1206 int len;
1207 int wlen = 0;
1208 int flen;
1209 int c;
1210 char_u *ptr;
1211 idx_T lo, hi, m;
1212 slang_T *slang = mip->mi_lp->lp_slang;
1213 char_u *byts;
1214 idx_T *idxs;
1215
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001216 byts = slang->sl_pbyts;
1217 if (byts == NULL)
1218 return; /* array is empty */
1219
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001220 /* We use the case-folded word here, since prefixes are always
1221 * case-folded. */
1222 ptr = mip->mi_fword;
1223 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001224 idxs = slang->sl_pidxs;
1225
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001226 /*
1227 * Repeat advancing in the tree until:
1228 * - there is a byte that doesn't match,
1229 * - we reach the end of the tree,
1230 * - or we reach the end of the line.
1231 */
1232 for (;;)
1233 {
1234 if (flen == 0 && *mip->mi_fend != NUL)
1235 flen = fold_more(mip);
1236
1237 len = byts[arridx++];
1238
1239 /* If the first possible byte is a zero the prefix could end here.
1240 * Check if the following word matches and supports the prefix. */
1241 if (byts[arridx] == 0)
1242 {
1243 /* There can be several prefixes with different conditions. We
1244 * try them all, since we don't know which one will give the
1245 * longest match. The word is the same each time, pass the list
1246 * of possible prefixes to find_word(). */
1247 mip->mi_prefarridx = arridx;
1248 mip->mi_prefcnt = len;
1249 while (len > 0 && byts[arridx] == 0)
1250 {
1251 ++arridx;
1252 --len;
1253 }
1254 mip->mi_prefcnt -= len;
1255
1256 /* Find the word that comes after the prefix. */
1257 mip->mi_prefixlen = wlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001258#ifdef FEAT_MBYTE
1259 if (has_mbyte)
1260 {
1261 /* Case-folded length may differ from original length. */
1262 mip->mi_cprefixlen = nofold_len(mip->mi_fword, wlen,
1263 mip->mi_word);
1264 }
1265 else
1266 mip->mi_cprefixlen = wlen;
1267#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001268 find_word(mip, FIND_PREFIX);
1269
1270
1271 if (len == 0)
1272 break; /* no children, word must end here */
1273 }
1274
1275 /* Stop looking at end of the line. */
1276 if (ptr[wlen] == NUL)
1277 break;
1278
1279 /* Perform a binary search in the list of accepted bytes. */
1280 c = ptr[wlen];
1281 lo = arridx;
1282 hi = arridx + len - 1;
1283 while (lo < hi)
1284 {
1285 m = (lo + hi) / 2;
1286 if (byts[m] > c)
1287 hi = m - 1;
1288 else if (byts[m] < c)
1289 lo = m + 1;
1290 else
1291 {
1292 lo = hi = m;
1293 break;
1294 }
1295 }
1296
1297 /* Stop if there is no matching byte. */
1298 if (hi < lo || byts[lo] != c)
1299 break;
1300
1301 /* Continue at the child (if there is one). */
1302 arridx = idxs[lo];
1303 ++wlen;
1304 --flen;
1305 }
1306}
1307
1308/*
1309 * Need to fold at least one more character. Do until next non-word character
1310 * for efficiency.
1311 * Return the length of the folded chars in bytes.
1312 */
1313 static int
1314fold_more(mip)
1315 matchinf_T *mip;
1316{
1317 int flen;
1318 char_u *p;
1319
1320 p = mip->mi_fend;
1321 do
1322 {
1323 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001324 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001325
1326 /* Include the non-word character so that we can check for the
1327 * word end. */
1328 if (*mip->mi_fend != NUL)
1329 mb_ptr_adv(mip->mi_fend);
1330
1331 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1332 mip->mi_fword + mip->mi_fwordlen,
1333 MAXWLEN - mip->mi_fwordlen);
1334 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1335 mip->mi_fwordlen += flen;
1336 return flen;
1337}
1338
1339/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001340 * Check case flags for a word. Return TRUE if the word has the requested
1341 * case.
1342 */
1343 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001344spell_valid_case(wordflags, treeflags)
1345 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001346 int treeflags; /* flags for the word in the spell tree */
1347{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001348 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001349 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001350 && ((treeflags & WF_ONECAP) == 0 || wordflags == WF_ONECAP)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001351}
1352
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001353/*
1354 * Return TRUE if spell checking is not enabled.
1355 */
1356 static int
1357no_spell_checking()
1358{
1359 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
1360 {
1361 EMSG(_("E756: Spell checking is not enabled"));
1362 return TRUE;
1363 }
1364 return FALSE;
1365}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001366
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001367/*
1368 * Move to next spell error.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001369 * "curline" is TRUE for "z?": find word under/after cursor in the same line.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001370 * Return OK if found, FAIL otherwise.
1371 */
1372 int
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001373spell_move_to(dir, allwords, curline)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001374 int dir; /* FORWARD or BACKWARD */
1375 int allwords; /* TRUE for "[s" and "]s" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001376 int curline;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001377{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001378 linenr_T lnum;
1379 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001380 char_u *line;
1381 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001382 char_u *endp;
1383 int attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001384 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001385 int has_syntax = syntax_present(curbuf);
1386 int col;
1387 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001388 char_u *buf = NULL;
1389 int buflen = 0;
1390 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001391 int capcol = -1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001392
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001393 if (no_spell_checking())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001394 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001395
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001396 /*
1397 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001398 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001399 *
1400 * When searching backwards, we continue in the line to find the last
1401 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001402 *
1403 * We concatenate the start of the next line, so that wrapped words work
1404 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1405 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001406 */
1407 lnum = curwin->w_cursor.lnum;
1408 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001409
1410 while (!got_int)
1411 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001412 line = ml_get(lnum);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001413
Bram Moolenaar0c405862005-06-22 22:26:26 +00001414 len = STRLEN(line);
1415 if (buflen < len + MAXWLEN + 2)
1416 {
1417 vim_free(buf);
1418 buflen = len + MAXWLEN + 2;
1419 buf = alloc(buflen);
1420 if (buf == NULL)
1421 break;
1422 }
1423
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001424 /* In first line check first word for Capital. */
1425 if (lnum == 1)
1426 capcol = 0;
1427
1428 /* For checking first word with a capital skip white space. */
1429 if (capcol == 0)
1430 capcol = skipwhite(line) - line;
1431
Bram Moolenaar0c405862005-06-22 22:26:26 +00001432 /* Copy the line into "buf" and append the start of the next line if
1433 * possible. */
1434 STRCPY(buf, line);
1435 if (lnum < curbuf->b_ml.ml_line_count)
1436 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1437
1438 p = buf + skip;
1439 endp = buf + len;
1440 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001441 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001442 /* When searching backward don't search after the cursor. */
1443 if (dir == BACKWARD
1444 && lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001445 && (colnr_T)(p - buf) >= curwin->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001446 break;
1447
1448 /* start of word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00001449 attr = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001450 len = spell_check(curwin, p, &attr, &capcol);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001451
1452 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001453 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001454 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001455 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001456 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001457 /* When searching forward only accept a bad word after
1458 * the cursor. */
1459 if (dir == BACKWARD
1460 || lnum > curwin->w_cursor.lnum
1461 || (lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001462 && (colnr_T)(curline ? p - buf + len
1463 : p - buf)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001464 > curwin->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001465 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001466 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001467 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001468 col = p - buf;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001469 (void)syn_get_id(lnum, (colnr_T)col,
1470 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001471 }
1472 else
1473 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001474
Bram Moolenaar51485f02005-06-04 21:55:20 +00001475 if (can_spell)
1476 {
1477 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001478 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001479#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001480 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001481#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001482 if (dir == FORWARD)
1483 {
1484 /* No need to search further. */
1485 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001486 vim_free(buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001487 return OK;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001488 }
1489 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001490 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001491 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001492 }
1493
Bram Moolenaar51485f02005-06-04 21:55:20 +00001494 /* advance to character after the word */
1495 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001496 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001497 }
1498
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001499 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001500 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001501
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001502 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001503 if (dir == BACKWARD)
1504 {
1505 if (found_pos.lnum != 0)
1506 {
1507 /* Use the last match in the line. */
1508 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001509 vim_free(buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001510 return OK;
1511 }
1512 if (lnum == 1)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001513 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001514 --lnum;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001515 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001516 }
1517 else
1518 {
1519 if (lnum == curbuf->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001520 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001521 ++lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001522
1523 /* Skip the characters at the start of the next line that were
1524 * included in a match crossing line boundaries. */
1525 if (attr == 0)
1526 skip = p - endp;
1527 else
1528 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001529
1530 /* Capscol skips over the inserted space. */
1531 --capcol;
1532
1533 /* But after empty line check first word in next line */
1534 if (*skipwhite(line) == NUL)
1535 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001536 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001537
1538 line_breakcheck();
1539 }
1540
Bram Moolenaar0c405862005-06-22 22:26:26 +00001541 vim_free(buf);
1542 return FAIL;
1543}
1544
1545/*
1546 * For spell checking: concatenate the start of the following line "line" into
1547 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
1548 */
1549 void
1550spell_cat_line(buf, line, maxlen)
1551 char_u *buf;
1552 char_u *line;
1553 int maxlen;
1554{
1555 char_u *p;
1556 int n;
1557
1558 p = skipwhite(line);
1559 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1560 p = skipwhite(p + 1);
1561
1562 if (*p != NUL)
1563 {
1564 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001565 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001566 n = p - line;
1567 if (n >= maxlen)
1568 n = maxlen - 1;
1569 vim_memset(buf + 1, ' ', n);
1570 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001571}
1572
1573/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001574 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001575 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001576 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001577 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001578spell_load_lang(lang)
1579 char_u *lang;
1580{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001581 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001582 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001583 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001584
Bram Moolenaarb765d632005-06-07 21:00:02 +00001585 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001586 * It's truncated when an error is detected. */
1587 STRCPY(langcp, lang);
1588
Bram Moolenaarb765d632005-06-07 21:00:02 +00001589 /*
1590 * Find the first spell file for "lang" in 'runtimepath' and load it.
1591 */
1592 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
1593 "spell/%s.%s.spl", lang, spell_enc());
1594 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001595
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001596 if (r == FAIL && *langcp != NUL)
1597 {
1598 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001599 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001600 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001601 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001602 }
1603
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001604 if (r == FAIL)
1605 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
1606 fname_enc + 6);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001607 else if (*langcp != NUL)
1608 {
1609 /* Load all the additions. */
1610 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
1611 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
1612 }
1613}
1614
1615/*
1616 * Return the encoding used for spell checking: Use 'encoding', except that we
1617 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1618 */
1619 static char_u *
1620spell_enc()
1621{
1622
1623#ifdef FEAT_MBYTE
1624 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1625 return p_enc;
1626#endif
1627 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001628}
1629
1630/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001631 * Get the name of the .spl file for the internal wordlist into
1632 * "fname[MAXPATHL]".
1633 */
1634 static void
1635int_wordlist_spl(fname)
1636 char_u *fname;
1637{
1638 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
1639 int_wordlist, spell_enc());
1640}
1641
1642/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001643 * Allocate a new slang_T.
1644 * Caller must fill "sl_next".
1645 */
1646 static slang_T *
1647slang_alloc(lang)
1648 char_u *lang;
1649{
1650 slang_T *lp;
1651
Bram Moolenaar51485f02005-06-04 21:55:20 +00001652 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001653 if (lp != NULL)
1654 {
1655 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001656 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001657 }
1658 return lp;
1659}
1660
1661/*
1662 * Free the contents of an slang_T and the structure itself.
1663 */
1664 static void
1665slang_free(lp)
1666 slang_T *lp;
1667{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001668 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001669 vim_free(lp->sl_fname);
1670 slang_clear(lp);
1671 vim_free(lp);
1672}
1673
1674/*
1675 * Clear an slang_T so that the file can be reloaded.
1676 */
1677 static void
1678slang_clear(lp)
1679 slang_T *lp;
1680{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001681 garray_T *gap;
1682 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001683 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001684 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001685
Bram Moolenaar51485f02005-06-04 21:55:20 +00001686 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001687 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001688 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001689 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001690 vim_free(lp->sl_pbyts);
1691 lp->sl_pbyts = NULL;
1692
Bram Moolenaar51485f02005-06-04 21:55:20 +00001693 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001694 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001695 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001696 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001697 vim_free(lp->sl_pidxs);
1698 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001699
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001700 gap = &lp->sl_rep;
1701 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001702 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001703 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1704 vim_free(ftp->ft_from);
1705 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001706 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001707 ga_clear(gap);
1708
1709 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001710 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001711 {
1712 /* "ga_len" is set to 1 without adding an item for latin1 */
1713 if (gap->ga_data != NULL)
1714 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
1715 for (i = 0; i < gap->ga_len; ++i)
1716 vim_free(((int **)gap->ga_data)[i]);
1717 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001718 else
1719 /* SAL items: free salitem_T items */
1720 while (gap->ga_len > 0)
1721 {
1722 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1723 vim_free(smp->sm_lead);
1724 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
1725 vim_free(smp->sm_to);
1726#ifdef FEAT_MBYTE
1727 vim_free(smp->sm_lead_w);
1728 vim_free(smp->sm_oneof_w);
1729 vim_free(smp->sm_to_w);
1730#endif
1731 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001732 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001733
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001734 for (i = 0; i < lp->sl_prefixcnt; ++i)
1735 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001736 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001737 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001738 lp->sl_prefprog = NULL;
1739
1740 vim_free(lp->sl_midword);
1741 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001742
Bram Moolenaarea424162005-06-16 21:51:00 +00001743#ifdef FEAT_MBYTE
1744 {
1745 int todo = lp->sl_map_hash.ht_used;
1746 hashitem_T *hi;
1747
1748 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
1749 if (!HASHITEM_EMPTY(hi))
1750 {
1751 --todo;
1752 vim_free(hi->hi_key);
1753 }
1754 }
1755 hash_clear(&lp->sl_map_hash);
1756#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001757}
1758
1759/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001760 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001761 * Invoked through do_in_runtimepath().
1762 */
1763 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00001764spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001765 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001766 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001767{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001768 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001769}
1770
1771/*
1772 * Load one spell file and store the info into a slang_T.
1773 *
1774 * This is invoked in two ways:
1775 * - From spell_load_cb() to load a spell file for the first time. "lang" is
1776 * the language name, "old_lp" is NULL. Will allocate an slang_T.
1777 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
1778 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001779 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00001780 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001781 static slang_T *
1782spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001783 char_u *fname;
1784 char_u *lang;
1785 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001786 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001787{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001788 FILE *fd;
1789 char_u buf[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001790 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001791 char_u *bp;
1792 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001793 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001794 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001795 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001796 int round;
1797 char_u *save_sourcing_name = sourcing_name;
1798 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001799 int cnt, ccnt;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001800 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001801 slang_T *lp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001802 garray_T *gap;
1803 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001804 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001805 short *first;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001806 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001807 int c = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001808
Bram Moolenaarb765d632005-06-07 21:00:02 +00001809 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001810 if (fd == NULL)
1811 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001812 if (!silent)
1813 EMSG2(_(e_notopen), fname);
1814 else if (p_verbose > 2)
1815 {
1816 verbose_enter();
1817 smsg((char_u *)e_notopen, fname);
1818 verbose_leave();
1819 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001820 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001821 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00001822 if (p_verbose > 2)
1823 {
1824 verbose_enter();
1825 smsg((char_u *)_("Reading spell file \"%s\""), fname);
1826 verbose_leave();
1827 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001828
Bram Moolenaarb765d632005-06-07 21:00:02 +00001829 if (old_lp == NULL)
1830 {
1831 lp = slang_alloc(lang);
1832 if (lp == NULL)
1833 goto endFAIL;
1834
1835 /* Remember the file name, used to reload the file when it's updated. */
1836 lp->sl_fname = vim_strsave(fname);
1837 if (lp->sl_fname == NULL)
1838 goto endFAIL;
1839
1840 /* Check for .add.spl. */
1841 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
1842 }
1843 else
1844 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001845
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001846 /* Set sourcing_name, so that error messages mention the file name. */
1847 sourcing_name = fname;
1848 sourcing_lnum = 0;
1849
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001850 /* <HEADER>: <fileID>
1851 * <regioncnt> <regionname> ...
1852 * <charflagslen> <charflags>
1853 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001854 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001855 * <prefcondcnt> <prefcond> ...
1856 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001857 for (i = 0; i < VIMSPELLMAGICL; ++i)
1858 buf[i] = getc(fd); /* <fileID> */
1859 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
1860 {
1861 EMSG(_("E757: Wrong file ID in spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001862 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001863 }
1864
1865 cnt = getc(fd); /* <regioncnt> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001866 if (cnt < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001867 {
1868truncerr:
Bram Moolenaar7887d882005-07-01 22:33:52 +00001869 EMSG(_(e_spell_trunc));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001870 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001871 }
1872 if (cnt > 8)
1873 {
1874formerr:
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001875 EMSG(_(e_format));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001876 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001877 }
1878 for (i = 0; i < cnt; ++i)
1879 {
1880 lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
1881 lp->sl_regions[i * 2 + 1] = getc(fd);
1882 }
1883 lp->sl_regions[cnt * 2] = NUL;
1884
Bram Moolenaar7887d882005-07-01 22:33:52 +00001885 /* <charflagslen> <charflags> */
1886 p = read_cnt_string(fd, 1, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001887 if (cnt < 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00001888 goto endFAIL;
1889
1890 /* <fcharslen> <fchars> */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001891 fol = read_cnt_string(fd, 2, &ccnt);
1892 if (ccnt < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001893 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001894 vim_free(p);
Bram Moolenaar7887d882005-07-01 22:33:52 +00001895 goto endFAIL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001896 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00001897
1898 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
1899 if (p != NULL && fol != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001900 i = set_spell_charflags(p, cnt, fol);
Bram Moolenaar7887d882005-07-01 22:33:52 +00001901
1902 vim_free(p);
1903 vim_free(fol);
1904
1905 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
1906 if ((p == NULL) != (fol == NULL))
1907 goto formerr;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001908
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001909 /* <midwordlen> <midword> */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001910 lp->sl_midword = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001911 if (cnt < 0)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001912 goto endFAIL;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001913
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001914 /* <prefcondcnt> <prefcond> ... */
1915 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
1916 if (cnt > 0)
1917 {
1918 lp->sl_prefprog = (regprog_T **)alloc_clear(
1919 (unsigned)sizeof(regprog_T *) * cnt);
1920 if (lp->sl_prefprog == NULL)
1921 goto endFAIL;
1922 lp->sl_prefixcnt = cnt;
1923
1924 for (i = 0; i < cnt; ++i)
1925 {
1926 /* <prefcond> : <condlen> <condstr> */
1927 n = getc(fd); /* <condlen> */
1928 if (n < 0)
1929 goto formerr;
1930 /* When <condlen> is zero we have an empty condition. Otherwise
1931 * compile the regexp program used to check for the condition. */
1932 if (n > 0)
1933 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001934 buf[0] = '^'; /* always match at one position only */
1935 p = buf + 1;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001936 while (n-- > 0)
1937 *p++ = getc(fd); /* <condstr> */
1938 *p = NUL;
1939 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
1940 }
1941 }
1942 }
1943
1944
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001945 /* <SUGGEST> : <repcount> <rep> ...
1946 * <salflags> <salcount> <sal> ...
1947 * <maplen> <mapstr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001948
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001949 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
1950 if (cnt < 0)
1951 goto formerr;
1952
1953 gap = &lp->sl_rep;
1954 if (ga_grow(gap, cnt) == FAIL)
1955 goto endFAIL;
1956
1957 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
1958 for (; gap->ga_len < cnt; ++gap->ga_len)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001959 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001960 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
Bram Moolenaar7887d882005-07-01 22:33:52 +00001961 ftp->ft_from = read_cnt_string(fd, 1, &i);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001962 if (i <= 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00001963 goto endFAIL;
1964 ftp->ft_to = read_cnt_string(fd, 1, &i);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001965 if (i <= 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001966 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00001967 vim_free(ftp->ft_from);
1968 goto endFAIL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001969 }
1970 }
1971
1972 /* Fill the first-index table. */
1973 first = lp->sl_rep_first;
1974 for (i = 0; i < 256; ++i)
1975 first[i] = -1;
1976 for (i = 0; i < gap->ga_len; ++i)
1977 {
1978 ftp = &((fromto_T *)gap->ga_data)[i];
1979 if (first[*ftp->ft_from] == -1)
1980 first[*ftp->ft_from] = i;
1981 }
1982
1983 i = getc(fd); /* <salflags> */
1984 if (i & SAL_F0LLOWUP)
1985 lp->sl_followup = TRUE;
1986 if (i & SAL_COLLAPSE)
1987 lp->sl_collapse = TRUE;
1988 if (i & SAL_REM_ACCENTS)
1989 lp->sl_rem_accents = TRUE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001990 if (i & SAL_SOFO)
1991 lp->sl_sofo = TRUE;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001992 else
1993 lp->sl_sofo = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001994
1995 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
1996 if (cnt < 0)
1997 goto formerr;
1998
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001999 if (lp->sl_sofo)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002000 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002001 /*
2002 * SOFOFROM and SOFOTO items come in one <salfrom> and <salto>
2003 */
2004 if (cnt != 1)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002005 goto formerr;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002006
Bram Moolenaar7887d882005-07-01 22:33:52 +00002007 /* <salfromlen> <salfrom> */
2008 bp = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002009 if (cnt < 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002010 goto endFAIL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002011
Bram Moolenaar7887d882005-07-01 22:33:52 +00002012 /* <saltolen> <salto> */
2013 fol = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002014 if (cnt < 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002015 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002016 vim_free(bp);
2017 goto endFAIL;
2018 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002019
Bram Moolenaar7887d882005-07-01 22:33:52 +00002020 /* Store the info in lp->sl_sal and/or lp->sl_sal_first. */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002021 if (bp != NULL && fol != NULL)
2022 i = set_sofo(lp, bp, fol);
2023 else if (bp != NULL || fol != NULL)
2024 i = FAIL; /* only one of two strings is an error */
2025 else
2026 i = OK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002027
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002028 vim_free(bp);
2029 vim_free(fol);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002030 if (i == FAIL)
2031 goto formerr;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002032 }
2033 else
2034 {
2035 /*
2036 * SAL items
2037 */
2038 gap = &lp->sl_sal;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002039 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002040 if (ga_grow(gap, cnt) == FAIL)
2041 goto endFAIL;
2042
2043 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
2044 for (; gap->ga_len < cnt; ++gap->ga_len)
2045 {
2046 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
2047 ccnt = getc(fd); /* <salfromlen> */
2048 if (ccnt < 0)
2049 goto formerr;
2050 if ((p = alloc(ccnt + 2)) == NULL)
2051 goto endFAIL;
2052 smp->sm_lead = p;
2053
2054 /* Read up to the first special char into sm_lead. */
2055 for (i = 0; i < ccnt; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002056 {
2057 c = getc(fd); /* <salfrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002058 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002059 break;
2060 *p++ = c;
2061 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002062 smp->sm_leadlen = p - smp->sm_lead;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002063 *p++ = NUL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002064
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002065 /* Put (abc) chars in sm_oneof, if any. */
2066 if (c == '(')
2067 {
2068 smp->sm_oneof = p;
2069 for (++i; i < ccnt; ++i)
2070 {
2071 c = getc(fd); /* <salfrom> */
2072 if (c == ')')
2073 break;
2074 *p++ = c;
2075 }
2076 *p++ = NUL;
2077 if (++i < ccnt)
2078 c = getc(fd);
2079 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002080 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002081 smp->sm_oneof = NULL;
2082
2083 /* Any following chars go in sm_rules. */
2084 smp->sm_rules = p;
2085 if (i < ccnt)
2086 /* store the char we got while checking for end of sm_lead */
2087 *p++ = c;
2088 for (++i; i < ccnt; ++i)
2089 *p++ = getc(fd); /* <salfrom> */
2090 *p++ = NUL;
2091
Bram Moolenaar7887d882005-07-01 22:33:52 +00002092 /* <saltolen> <salto> */
2093 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002094 if (ccnt < 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002095 {
2096 vim_free(smp->sm_lead);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002097 goto formerr;
2098 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002099
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002100#ifdef FEAT_MBYTE
2101 if (has_mbyte)
2102 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002103 /* convert the multi-byte strings to wide char strings */
2104 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2105 smp->sm_leadlen = mb_charlen(smp->sm_lead);
2106 if (smp->sm_oneof == NULL)
2107 smp->sm_oneof_w = NULL;
2108 else
2109 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002110 if (smp->sm_to == NULL)
2111 smp->sm_to_w = NULL;
2112 else
2113 smp->sm_to_w = mb_str2wide(smp->sm_to);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002114 if (smp->sm_lead_w == NULL
2115 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002116 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002117 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002118 vim_free(smp->sm_lead);
2119 vim_free(smp->sm_to);
2120 vim_free(smp->sm_lead_w);
2121 vim_free(smp->sm_oneof_w);
2122 vim_free(smp->sm_to_w);
2123 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002124 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002125 }
2126#endif
2127 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002128
2129 /* Fill the first-index table. */
Bram Moolenaar7887d882005-07-01 22:33:52 +00002130 set_sal_first(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002131 }
2132
Bram Moolenaar7887d882005-07-01 22:33:52 +00002133 /* <maplen> <mapstr> */
2134 p = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002135 if (cnt < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002136 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002137 if (p != NULL)
2138 {
2139 set_map_str(lp, p);
2140 vim_free(p);
2141 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002142
Bram Moolenaar51485f02005-06-04 21:55:20 +00002143 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002144 * round 2: <KWORDTREE>
2145 * round 3: <PREFIXTREE> */
2146 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002147 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002148 /* The tree size was computed when writing the file, so that we can
2149 * allocate it as one long block. <nodecount> */
2150 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2151 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002152 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002153 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002154 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002155 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002156 bp = lalloc((long_u)len, TRUE);
2157 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002158 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002159 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002160 lp->sl_fbyts = bp;
2161 else if (round == 2)
2162 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002163 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002164 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002165
2166 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002167 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
2168 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002169 goto endFAIL;
2170 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002171 lp->sl_fidxs = ip;
2172 else if (round == 2)
2173 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002174 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002175 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002176
2177 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002178 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002179 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002180 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002181 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002182 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002183 }
2184 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002185
Bram Moolenaarb765d632005-06-07 21:00:02 +00002186 /* For a new file link it in the list of spell files. */
2187 if (old_lp == NULL)
2188 {
2189 lp->sl_next = first_lang;
2190 first_lang = lp;
2191 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002192
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002193 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002194
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002195endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002196 if (lang != NULL)
2197 /* truncating the name signals the error to spell_load_lang() */
2198 *lang = NUL;
2199 if (lp != NULL && old_lp == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002200 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002201 slang_free(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002202 lp = NULL;
2203 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002204
2205endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002206 if (fd != NULL)
2207 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002208 sourcing_name = save_sourcing_name;
2209 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002210
2211 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002212}
2213
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002214/*
2215 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002216 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002217 * Returns NULL when the count is zero.
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002218 * Sets "*cntp" to -1 when there is an error, length of the result otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002219 */
2220 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002221read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002222 FILE *fd;
2223 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002224 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002225{
2226 int cnt = 0;
2227 int i;
2228 char_u *str;
2229
2230 /* read the length bytes, MSB first */
2231 for (i = 0; i < cnt_bytes; ++i)
2232 cnt = (cnt << 8) + getc(fd);
2233 if (cnt < 0)
2234 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00002235 EMSG(_(e_spell_trunc));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002236 *cntp = -1;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002237 return NULL;
2238 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002239 *cntp = cnt;
2240 if (cnt == 0)
2241 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002242
2243 /* allocate memory */
2244 str = alloc((unsigned)cnt + 1);
2245 if (str == NULL)
2246 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002247 *cntp = -1;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002248 return NULL;
2249 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002250
2251 /* Read the string. Doesn't check for truncated file. */
2252 for (i = 0; i < cnt; ++i)
2253 str[i] = getc(fd);
2254 str[i] = NUL;
2255
2256 return str;
2257}
2258
Bram Moolenaar7887d882005-07-01 22:33:52 +00002259/*
2260 * Set the SOFOFROM and SOFOTO items in language "lp".
2261 * Returns FAIL when there is something wrong.
2262 */
2263 static int
2264set_sofo(lp, from, to)
2265 slang_T *lp;
2266 char_u *from;
2267 char_u *to;
2268{
2269 int i;
2270
2271#ifdef FEAT_MBYTE
2272 garray_T *gap;
2273 char_u *s;
2274 char_u *p;
2275 int c;
2276 int *inp;
2277
2278 if (has_mbyte)
2279 {
2280 /* Use "sl_sal" as an array with 256 pointers to a list of wide
2281 * characters. The index is the low byte of the character.
2282 * The list contains from-to pairs with a terminating NUL.
2283 * sl_sal_first[] is used for latin1 "from" characters. */
2284 gap = &lp->sl_sal;
2285 ga_init2(gap, sizeof(int *), 1);
2286 if (ga_grow(gap, 256) == FAIL)
2287 return FAIL;
2288 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
2289 gap->ga_len = 256;
2290
2291 /* First count the number of items for each list. Temporarily use
2292 * sl_sal_first[] for this. */
2293 for (p = from, s = to; *p != NUL && *s != NUL; )
2294 {
2295 c = mb_ptr2char_adv(&p);
2296 mb_ptr_adv(s);
2297 if (c >= 256)
2298 ++lp->sl_sal_first[c & 0xff];
2299 }
2300 if (*p != NUL || *s != NUL) /* lengths differ */
2301 return FAIL;
2302
2303 /* Allocate the lists. */
2304 for (i = 0; i < 256; ++i)
2305 if (lp->sl_sal_first[i] > 0)
2306 {
2307 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
2308 if (p == NULL)
2309 return FAIL;
2310 ((int **)gap->ga_data)[i] = (int *)p;
2311 *(int *)p = 0;
2312 }
2313
2314 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
2315 * list. */
2316 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
2317 for (p = from, s = to; *p != NUL && *s != NUL; )
2318 {
2319 c = mb_ptr2char_adv(&p);
2320 i = mb_ptr2char_adv(&s);
2321 if (c >= 256)
2322 {
2323 /* Append the from-to chars at the end of the list with
2324 * the low byte. */
2325 inp = ((int **)gap->ga_data)[c & 0xff];
2326 while (*inp != 0)
2327 ++inp;
2328 *inp++ = c; /* from char */
2329 *inp++ = i; /* to char */
2330 *inp++ = NUL; /* NUL at the end */
2331 }
2332 else
2333 /* mapping byte to char is done in sl_sal_first[] */
2334 lp->sl_sal_first[c] = i;
2335 }
2336 }
2337 else
2338#endif
2339 {
2340 /* mapping bytes to bytes is done in sl_sal_first[] */
2341 if (STRLEN(from) != STRLEN(to))
2342 return FAIL;
2343
2344 for (i = 0; to[i] != NUL; ++i)
2345 lp->sl_sal_first[from[i]] = to[i];
2346 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
2347 }
2348
2349 return OK;
2350}
2351
2352/*
2353 * Fill the first-index table for "lp".
2354 */
2355 static void
2356set_sal_first(lp)
2357 slang_T *lp;
2358{
2359 salfirst_T *sfirst;
2360 int i;
2361 salitem_T *smp;
2362 int c;
2363 garray_T *gap = &lp->sl_sal;
2364
2365 sfirst = lp->sl_sal_first;
2366 for (i = 0; i < 256; ++i)
2367 sfirst[i] = -1;
2368 smp = (salitem_T *)gap->ga_data;
2369 for (i = 0; i < gap->ga_len; ++i)
2370 {
2371#ifdef FEAT_MBYTE
2372 if (has_mbyte)
2373 /* Use the lowest byte of the first character. For latin1 it's
2374 * the character, for other encodings it should differ for most
2375 * characters. */
2376 c = *smp[i].sm_lead_w & 0xff;
2377 else
2378#endif
2379 c = *smp[i].sm_lead;
2380 if (sfirst[c] == -1)
2381 {
2382 sfirst[c] = i;
2383#ifdef FEAT_MBYTE
2384 if (has_mbyte)
2385 {
2386 int n;
2387
2388 /* Make sure all entries with this byte are following each
2389 * other. Move the ones that are in the wrong position. Do
2390 * keep the same ordering! */
2391 while (i + 1 < gap->ga_len
2392 && (*smp[i + 1].sm_lead_w & 0xff) == c)
2393 /* Skip over entry with same index byte. */
2394 ++i;
2395
2396 for (n = 1; i + n < gap->ga_len; ++n)
2397 if ((*smp[i + n].sm_lead_w & 0xff) == c)
2398 {
2399 salitem_T tsal;
2400
2401 /* Move entry with same index byte after the entries
2402 * we already found. */
2403 ++i;
2404 --n;
2405 tsal = smp[i + n];
2406 mch_memmove(smp + i + 1, smp + i,
2407 sizeof(salitem_T) * n);
2408 smp[i] = tsal;
2409 }
2410 }
2411#endif
2412 }
2413 }
2414}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002415
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002416#ifdef FEAT_MBYTE
2417/*
2418 * Turn a multi-byte string into a wide character string.
2419 * Return it in allocated memory (NULL for out-of-memory)
2420 */
2421 static int *
2422mb_str2wide(s)
2423 char_u *s;
2424{
2425 int *res;
2426 char_u *p;
2427 int i = 0;
2428
2429 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
2430 if (res != NULL)
2431 {
2432 for (p = s; *p != NUL; )
2433 res[i++] = mb_ptr2char_adv(&p);
2434 res[i] = NUL;
2435 }
2436 return res;
2437}
2438#endif
2439
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002440/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002441 * Read one row of siblings from the spell file and store it in the byte array
2442 * "byts" and index array "idxs". Recursively read the children.
2443 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00002444 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00002445 *
2446 * Returns the index follosing the siblings.
2447 * Returns -1 if the file is shorter than expected.
2448 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002449 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002450 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002451read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002452 FILE *fd;
2453 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002454 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002455 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002456 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002457 int prefixtree; /* TRUE for reading PREFIXTREE */
2458 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002459{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002460 int len;
2461 int i;
2462 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002463 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002464 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002465 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002466#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002467
Bram Moolenaar51485f02005-06-04 21:55:20 +00002468 len = getc(fd); /* <siblingcount> */
2469 if (len <= 0)
2470 return -1;
2471
2472 if (startidx + len >= maxidx)
2473 return -2;
2474 byts[idx++] = len;
2475
2476 /* Read the byte values, flag/region bytes and shared indexes. */
2477 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002478 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002479 c = getc(fd); /* <byte> */
2480 if (c < 0)
2481 return -1;
2482 if (c <= BY_SPECIAL)
2483 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002484 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002485 {
2486 /* No flags, all regions. */
2487 idxs[idx] = 0;
2488 c = 0;
2489 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002490 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002491 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002492 if (prefixtree)
2493 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002494 /* Read the optional pflags byte, the prefix ID and the
2495 * condition nr. In idxs[] store the prefix ID in the low
2496 * byte, the condition index shifted up 8 bits, the flags
2497 * shifted up 24 bits. */
2498 if (c == BY_FLAGS)
2499 c = getc(fd) << 24; /* <pflags> */
2500 else
2501 c = 0;
2502
2503 c |= getc(fd); /* <prefixID> */
2504
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002505 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
2506 if (n >= maxprefcondnr)
2507 return -2;
Bram Moolenaar53805d12005-08-01 07:08:33 +00002508 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002509 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002510 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002511 {
2512 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002513 * idxs[] the flags go in the low two bytes, region above
2514 * that and prefix ID above the region. */
2515 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002516 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002517 if (c2 == BY_FLAGS2)
2518 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002519 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002520 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002521 if (c & WF_PFX)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002522 c = (getc(fd) << 24) + c; /* <prefixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002523 }
2524
Bram Moolenaar51485f02005-06-04 21:55:20 +00002525 idxs[idx] = c;
2526 c = 0;
2527 }
2528 else /* c == BY_INDEX */
2529 {
2530 /* <nodeidx> */
2531 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2532 if (n < 0 || n >= maxidx)
2533 return -2;
2534 idxs[idx] = n + SHARED_MASK;
2535 c = getc(fd); /* <xbyte> */
2536 }
2537 }
2538 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002539 }
2540
Bram Moolenaar51485f02005-06-04 21:55:20 +00002541 /* Recursively read the children for non-shared siblings.
2542 * Skip the end-of-word ones (zero byte value) and the shared ones (and
2543 * remove SHARED_MASK) */
2544 for (i = 1; i <= len; ++i)
2545 if (byts[startidx + i] != 0)
2546 {
2547 if (idxs[startidx + i] & SHARED_MASK)
2548 idxs[startidx + i] &= ~SHARED_MASK;
2549 else
2550 {
2551 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002552 idx = read_tree(fd, byts, idxs, maxidx, idx,
2553 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002554 if (idx < 0)
2555 break;
2556 }
2557 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002558
Bram Moolenaar51485f02005-06-04 21:55:20 +00002559 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002560}
2561
2562/*
2563 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002564 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002565 */
2566 char_u *
2567did_set_spelllang(buf)
2568 buf_T *buf;
2569{
2570 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002571 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002572 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00002573 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002574 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002575 int region_mask;
2576 slang_T *lp;
2577 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002578 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002579 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002580 int len;
2581 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002582 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002583 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002584 char_u *use_region = NULL;
2585 int dont_use_region = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002586
2587 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002588 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002589
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002590 /* loop over comma separated language names. */
2591 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002592 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002593 /* Get one language name. */
2594 copy_option_part(&splp, lang, MAXWLEN, ",");
2595
Bram Moolenaar5482f332005-04-17 20:18:43 +00002596 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002597 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002598
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002599 /* If the name ends in ".spl" use it as the name of the spell file.
2600 * If there is a region name let "region" point to it and remove it
2601 * from the name. */
2602 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2603 {
2604 filename = TRUE;
2605
Bram Moolenaarb6356332005-07-18 21:40:44 +00002606 /* Locate a region and remove it from the file name. */
2607 p = vim_strchr(gettail(lang), '_');
2608 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
2609 && !ASCII_ISALPHA(p[3]))
2610 {
2611 vim_strncpy(region_cp, p + 1, 2);
2612 mch_memmove(p, p + 3, len - (p - lang) - 2);
2613 len -= 3;
2614 region = region_cp;
2615 }
2616 else
2617 dont_use_region = TRUE;
2618
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002619 /* Check if we loaded this language before. */
2620 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2621 if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME)
2622 break;
2623 }
2624 else
2625 {
2626 filename = FALSE;
2627 if (len > 3 && lang[len - 3] == '_')
2628 {
2629 region = lang + len - 2;
2630 len -= 3;
2631 lang[len] = NUL;
2632 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002633 else
2634 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002635
2636 /* Check if we loaded this language before. */
2637 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2638 if (STRICMP(lang, lp->sl_name) == 0)
2639 break;
2640 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002641
Bram Moolenaarb6356332005-07-18 21:40:44 +00002642 if (region != NULL)
2643 {
2644 /* If the region differs from what was used before then don't
2645 * use it for 'spellfile'. */
2646 if (use_region != NULL && STRCMP(region, use_region) != 0)
2647 dont_use_region = TRUE;
2648 use_region = region;
2649 }
2650
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002651 /* If not found try loading the language now. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002652 if (lp == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002653 {
2654 if (filename)
2655 (void)spell_load_file(lang, lang, NULL, FALSE);
2656 else
2657 spell_load_lang(lang);
2658 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002659
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002660 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002661 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002662 */
2663 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002664 if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME
2665 : STRICMP(lang, lp->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002666 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002667 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002668 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002669 {
2670 /* find region in sl_regions */
2671 c = find_region(lp->sl_regions, region);
2672 if (c == REGION_ALL)
2673 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002674 if (lp->sl_add)
2675 {
2676 if (*lp->sl_regions != NUL)
2677 /* This addition file is for other regions. */
2678 region_mask = 0;
2679 }
2680 else
2681 /* This is probably an error. Give a warning and
2682 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002683 smsg((char_u *)
2684 _("Warning: region %s not supported"),
2685 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002686 }
2687 else
2688 region_mask = 1 << c;
2689 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002690
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002691 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002692 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002693 if (ga_grow(&ga, 1) == FAIL)
2694 {
2695 ga_clear(&ga);
2696 return e_outofmem;
2697 }
2698 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2699 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2700 ++ga.ga_len;
2701 use_midword(lp, buf);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002702 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002703 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002704 }
2705
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002706 /* round 0: load int_wordlist, if possible.
2707 * round 1: load first name in 'spellfile'.
2708 * round 2: load second name in 'spellfile.
2709 * etc. */
2710 spf = curbuf->b_p_spf;
2711 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002712 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002713 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002714 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002715 /* Internal wordlist, if there is one. */
2716 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002717 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002718 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002719 }
2720 else
2721 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002722 /* One entry in 'spellfile'. */
2723 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
2724 STRCAT(spf_name, ".spl");
2725
2726 /* If it was already found above then skip it. */
2727 for (c = 0; c < ga.ga_len; ++c)
2728 if (fullpathcmp(spf_name,
2729 LANGP_ENTRY(ga, c)->lp_slang->sl_fname,
2730 FALSE) == FPC_SAME)
2731 break;
2732 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002733 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002734 }
2735
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002736 /* Check if it was loaded already. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002737 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2738 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
2739 break;
2740 if (lp == NULL)
2741 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002742 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002743 * region name, the region is ignored otherwise. for int_wordlist
2744 * use an arbitrary name. */
2745 if (round == 0)
2746 STRCPY(lang, "internal wordlist");
2747 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00002748 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002749 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002750 p = vim_strchr(lang, '.');
2751 if (p != NULL)
2752 *p = NUL; /* truncate at ".encoding.add" */
2753 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002754 lp = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002755 }
2756 if (lp != NULL && ga_grow(&ga, 1) == OK)
2757 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002758 region_mask = REGION_ALL;
2759 if (use_region != NULL && !dont_use_region)
2760 {
2761 /* find region in sl_regions */
2762 c = find_region(lp->sl_regions, use_region);
2763 if (c != REGION_ALL)
2764 region_mask = 1 << c;
2765 else if (*lp->sl_regions != NUL)
2766 /* This spell file is for other regions. */
2767 region_mask = 0;
2768 }
2769
2770 if (region_mask != 0)
2771 {
2772 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2773 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2774 ++ga.ga_len;
2775 use_midword(lp, buf);
2776 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002777 }
2778 }
2779
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002780 /* Add a NULL entry to mark the end of the list. */
2781 if (ga_grow(&ga, 1) == FAIL)
2782 {
2783 ga_clear(&ga);
2784 return e_outofmem;
2785 }
2786 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
2787 ++ga.ga_len;
2788
2789 /* Everything is fine, store the new b_langp value. */
2790 ga_clear(&buf->b_langp);
2791 buf->b_langp = ga;
2792
2793 return NULL;
2794}
2795
2796/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002797 * Clear the midword characters for buffer "buf".
2798 */
2799 static void
2800clear_midword(buf)
2801 buf_T *buf;
2802{
2803 vim_memset(buf->b_spell_ismw, 0, 256);
2804#ifdef FEAT_MBYTE
2805 vim_free(buf->b_spell_ismw_mb);
2806 buf->b_spell_ismw_mb = NULL;
2807#endif
2808}
2809
2810/*
2811 * Use the "sl_midword" field of language "lp" for buffer "buf".
2812 * They add up to any currently used midword characters.
2813 */
2814 static void
2815use_midword(lp, buf)
2816 slang_T *lp;
2817 buf_T *buf;
2818{
2819 char_u *p;
2820
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002821 if (lp->sl_midword == NULL) /* there aren't any */
2822 return;
2823
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002824 for (p = lp->sl_midword; *p != NUL; )
2825#ifdef FEAT_MBYTE
2826 if (has_mbyte)
2827 {
2828 int c, l, n;
2829 char_u *bp;
2830
2831 c = mb_ptr2char(p);
2832 l = mb_ptr2len_check(p);
2833 if (c < 256)
2834 buf->b_spell_ismw[c] = TRUE;
2835 else if (buf->b_spell_ismw_mb == NULL)
2836 /* First multi-byte char in "b_spell_ismw_mb". */
2837 buf->b_spell_ismw_mb = vim_strnsave(p, l);
2838 else
2839 {
2840 /* Append multi-byte chars to "b_spell_ismw_mb". */
2841 n = STRLEN(buf->b_spell_ismw_mb);
2842 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
2843 if (bp != NULL)
2844 {
2845 vim_free(buf->b_spell_ismw_mb);
2846 buf->b_spell_ismw_mb = bp;
2847 vim_strncpy(bp + n, p, l);
2848 }
2849 }
2850 p += l;
2851 }
2852 else
2853#endif
2854 buf->b_spell_ismw[*p++] = TRUE;
2855}
2856
2857/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002858 * Find the region "region[2]" in "rp" (points to "sl_regions").
2859 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002860 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002861 */
2862 static int
2863find_region(rp, region)
2864 char_u *rp;
2865 char_u *region;
2866{
2867 int i;
2868
2869 for (i = 0; ; i += 2)
2870 {
2871 if (rp[i] == NUL)
2872 return REGION_ALL;
2873 if (rp[i] == region[0] && rp[i + 1] == region[1])
2874 break;
2875 }
2876 return i / 2;
2877}
2878
2879/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002880 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002881 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002882 * Word WF_ONECAP
2883 * W WORD WF_ALLCAP
2884 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002885 */
2886 static int
2887captype(word, end)
2888 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002889 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002890{
2891 char_u *p;
2892 int c;
2893 int firstcap;
2894 int allcap;
2895 int past_second = FALSE; /* past second word char */
2896
2897 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002898 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002899 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002900 return 0; /* only non-word characters, illegal word */
2901#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00002902 if (has_mbyte)
2903 c = mb_ptr2char_adv(&p);
2904 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002905#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00002906 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002907 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002908
2909 /*
2910 * Need to check all letters to find a word with mixed upper/lower.
2911 * But a word with an upper char only at start is a ONECAP.
2912 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002913 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002914 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002915 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002916 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002917 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002918 {
2919 /* UUl -> KEEPCAP */
2920 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002921 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002922 allcap = FALSE;
2923 }
2924 else if (!allcap)
2925 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002926 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002927 past_second = TRUE;
2928 }
2929
2930 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002931 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002932 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002933 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002934 return 0;
2935}
2936
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002937# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
2938/*
2939 * Free all languages.
2940 */
2941 void
2942spell_free_all()
2943{
2944 slang_T *lp;
2945 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002946 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002947
2948 /* Go through all buffers and handle 'spelllang'. */
2949 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2950 ga_clear(&buf->b_langp);
2951
2952 while (first_lang != NULL)
2953 {
2954 lp = first_lang;
2955 first_lang = lp->sl_next;
2956 slang_free(lp);
2957 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002958
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002959 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002960 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002961 /* Delete the internal wordlist and its .spl file */
2962 mch_remove(int_wordlist);
2963 int_wordlist_spl(fname);
2964 mch_remove(fname);
2965 vim_free(int_wordlist);
2966 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002967 }
2968
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002969 init_spell_chartab();
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002970}
2971# endif
2972
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002973# if defined(FEAT_MBYTE) || defined(PROTO)
2974/*
2975 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002976 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002977 */
2978 void
2979spell_reload()
2980{
2981 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002982 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002983
Bram Moolenaarea408852005-06-25 22:49:46 +00002984 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002985 init_spell_chartab();
2986
2987 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002988 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002989
2990 /* Go through all buffers and handle 'spelllang'. */
2991 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2992 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002993 /* Only load the wordlists when 'spelllang' is set and there is a
2994 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002995 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002996 {
2997 FOR_ALL_WINDOWS(wp)
2998 if (wp->w_buffer == buf && wp->w_p_spell)
2999 {
3000 (void)did_set_spelllang(buf);
3001# ifdef FEAT_WINDOWS
3002 break;
3003# endif
3004 }
3005 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003006 }
3007}
3008# endif
3009
Bram Moolenaarb765d632005-06-07 21:00:02 +00003010/*
3011 * Reload the spell file "fname" if it's loaded.
3012 */
3013 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003014spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003015 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003016 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003017{
3018 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003019 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003020
Bram Moolenaarb765d632005-06-07 21:00:02 +00003021 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3022 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
3023 {
3024 slang_clear(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003025 (void)spell_load_file(fname, NULL, lp, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003026 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003027 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003028 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003029
3030 /* When "zg" was used and the file wasn't loaded yet, should redo
3031 * 'spelllang' to get it loaded. */
3032 if (added_word && !didit)
3033 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003034}
3035
3036
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003037/*
3038 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003039 */
3040
Bram Moolenaar51485f02005-06-04 21:55:20 +00003041#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003042 and .dic file. */
3043/*
3044 * Main structure to store the contents of a ".aff" file.
3045 */
3046typedef struct afffile_S
3047{
3048 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003049 int af_rar; /* RAR ID for rare word */
3050 int af_kep; /* KEP ID for keep-case word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003051 int af_bad; /* BAD ID for banned word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003052 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003053 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
3054 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003055} afffile_T;
3056
3057typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003058/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
3059struct affentry_S
3060{
3061 affentry_T *ae_next; /* next affix with same name/number */
3062 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
3063 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003064 char_u *ae_cond; /* condition (NULL for ".") */
3065 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003066 int ae_rare; /* rare affix */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003067};
3068
Bram Moolenaar53805d12005-08-01 07:08:33 +00003069#define AH_KEY_LEN 10
3070
Bram Moolenaar51485f02005-06-04 21:55:20 +00003071/* Affix header from ".aff" file. Used for af_pref and af_suff. */
3072typedef struct affheader_S
3073{
Bram Moolenaar53805d12005-08-01 07:08:33 +00003074 /* key for hashtable == name of affix entry */
3075#ifdef FEAT_MBYTE
3076 char_u ah_key[AH_KEY_LEN]; /* multi-byte char plus NUL */
3077#else
3078 char_u ah_key[2]; /* one byte char plus NUL */
3079#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003080 int ah_newID; /* prefix ID after renumbering */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003081 int ah_combine; /* suffix may combine with prefix */
3082 affentry_T *ah_first; /* first affix entry */
3083} affheader_T;
3084
3085#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
3086
3087/*
3088 * Structure that is used to store the items in the word tree. This avoids
3089 * the need to keep track of each allocated thing, it's freed all at once
3090 * after ":mkspell" is done.
3091 */
3092#define SBLOCKSIZE 16000 /* size of sb_data */
3093typedef struct sblock_S sblock_T;
3094struct sblock_S
3095{
3096 sblock_T *sb_next; /* next block in list */
3097 int sb_used; /* nr of bytes already in use */
3098 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003099};
3100
3101/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003102 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003103 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003104typedef struct wordnode_S wordnode_T;
3105struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003106{
Bram Moolenaar0c405862005-06-22 22:26:26 +00003107 union /* shared to save space */
3108 {
3109 char_u hashkey[6]; /* room for the hash key */
3110 int index; /* index in written nodes (valid after first
3111 round) */
3112 } wn_u1;
3113 union /* shared to save space */
3114 {
3115 wordnode_T *next; /* next node with same hash key */
3116 wordnode_T *wnode; /* parent node that will write this node */
3117 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003118 wordnode_T *wn_child; /* child (next byte in word) */
3119 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
3120 always sorted) */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003121 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003122 short_u wn_flags; /* when wn_byte is NUL: WF_ flags */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003123 short wn_region; /* when wn_byte is NUL: region mask; for
3124 PREFIXTREE it's the prefcondnr */
3125 char_u wn_prefixID; /* supported/required prefix ID or 0 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003126};
3127
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003128#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
3129
Bram Moolenaar51485f02005-06-04 21:55:20 +00003130#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003131
Bram Moolenaar51485f02005-06-04 21:55:20 +00003132/*
3133 * Info used while reading the spell files.
3134 */
3135typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003136{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003137 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003138 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003139 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003140 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003141 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003142 sblock_T *si_blocks; /* memory blocks used */
3143 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003144 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003145 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003146 int si_region; /* region mask */
3147 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003148 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003149 int si_verbose; /* verbose messages */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003150 int si_region_count; /* number of regions supported (1 when there
3151 are no regions) */
3152 char_u si_region_name[16]; /* region names (if count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003153
3154 garray_T si_rep; /* list of fromto_T entries from REP lines */
3155 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003156 char_u *si_sofofr; /* SOFOFROM text */
3157 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003158 int si_followup; /* soundsalike: ? */
3159 int si_collapse; /* soundsalike: ? */
3160 int si_rem_accents; /* soundsalike: remove accents */
3161 garray_T si_map; /* MAP info concatenated */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003162 char_u *si_midword; /* MIDWORD chars, alloc'ed string or NULL */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003163 garray_T si_prefcond; /* table with conditions for postponed
3164 * prefixes, each stored as a string */
3165 int si_newID; /* current value for ah_newID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003166} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003167
Bram Moolenaar51485f02005-06-04 21:55:20 +00003168static afffile_T *spell_read_aff __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003169static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003170static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
3171static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00003172static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003173static void spell_free_aff __ARGS((afffile_T *aff));
3174static int spell_read_dic __ARGS((char_u *fname, spellinfo_T *spin, afffile_T *affile));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003175static char_u *get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, sblock_T **blp));
3176static int store_aff_word __ARGS((char_u *word, spellinfo_T *spin, char_u *afflist, afffile_T *affile, hashtab_T *ht, hashtab_T *xht, int comb, int flags, char_u *pfxlist));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003177static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003178static void *getroom __ARGS((sblock_T **blp, size_t len, int align));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003179static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s));
3180static void free_blocks __ARGS((sblock_T *bl));
3181static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003182static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags, int region, char_u *pfxlist));
3183static int tree_add_word __ARGS((char_u *word, wordnode_T *tree, int flags, int region, int prefixID, sblock_T **blp));
Bram Moolenaarb765d632005-06-07 21:00:02 +00003184static void wordtree_compress __ARGS((wordnode_T *root, spellinfo_T *spin));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003185static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot));
3186static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar3982c542005-06-08 21:56:31 +00003187static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar0c405862005-06-22 22:26:26 +00003188static void clear_node __ARGS((wordnode_T *node));
3189static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003190static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00003191static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003192
Bram Moolenaar53805d12005-08-01 07:08:33 +00003193/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
3194 * but it must be negative to indicate the prefix tree to tree_add_word().
3195 * Use a negative number with the lower 8 bits zero. */
3196#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003197
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003198/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003199 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00003200 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003201 */
3202 static afffile_T *
Bram Moolenaar51485f02005-06-04 21:55:20 +00003203spell_read_aff(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003204 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003205 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003206{
3207 FILE *fd;
3208 afffile_T *aff;
3209 char_u rline[MAXLINELEN];
3210 char_u *line;
3211 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003212#define MAXITEMCNT 7
3213 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003214 int itemcnt;
3215 char_u *p;
3216 int lnum = 0;
3217 affheader_T *cur_aff = NULL;
3218 int aff_todo = 0;
3219 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003220 char_u *low = NULL;
3221 char_u *fol = NULL;
3222 char_u *upp = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003223 static char *e_affname = N_("Affix name too long in %s line %d: %s");
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003224 int do_rep;
3225 int do_sal;
3226 int do_map;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003227 int do_midword;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003228 int do_sofo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003229 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003230 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003231 int l;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003232
Bram Moolenaar51485f02005-06-04 21:55:20 +00003233 /*
3234 * Open the file.
3235 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003236 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003237 if (fd == NULL)
3238 {
3239 EMSG2(_(e_notopen), fname);
3240 return NULL;
3241 }
3242
Bram Moolenaarb765d632005-06-07 21:00:02 +00003243 if (spin->si_verbose || p_verbose > 2)
3244 {
3245 if (!spin->si_verbose)
3246 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003247 smsg((char_u *)_("Reading affix file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003248 out_flush();
3249 if (!spin->si_verbose)
3250 verbose_leave();
3251 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003252
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003253 /* Only do REP lines when not done in another .aff file already. */
3254 do_rep = spin->si_rep.ga_len == 0;
3255
3256 /* Only do SAL lines when not done in another .aff file already. */
3257 do_sal = spin->si_sal.ga_len == 0;
3258
3259 /* Only do MAP lines when not done in another .aff file already. */
3260 do_map = spin->si_map.ga_len == 0;
3261
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003262 /* Only do MIDWORD line when not done in another .aff file already */
3263 do_midword = spin->si_midword == NULL;
3264
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003265 /* Only do SOFOFROM and SOFOTO when not done in another .aff file already */
3266 do_sofo = spin->si_sofofr == NULL;
3267
Bram Moolenaar51485f02005-06-04 21:55:20 +00003268 /*
3269 * Allocate and init the afffile_T structure.
3270 */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003271 aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003272 if (aff == NULL)
3273 return NULL;
3274 hash_init(&aff->af_pref);
3275 hash_init(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003276
3277 /*
3278 * Read all the lines in the file one by one.
3279 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003280 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003281 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003282 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003283 ++lnum;
3284
3285 /* Skip comment lines. */
3286 if (*rline == '#')
3287 continue;
3288
3289 /* Convert from "SET" to 'encoding' when needed. */
3290 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003291#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003292 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003293 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003294 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003295 if (pc == NULL)
3296 {
3297 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3298 fname, lnum, rline);
3299 continue;
3300 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003301 line = pc;
3302 }
3303 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003304#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003305 {
3306 pc = NULL;
3307 line = rline;
3308 }
3309
3310 /* Split the line up in white separated items. Put a NUL after each
3311 * item. */
3312 itemcnt = 0;
3313 for (p = line; ; )
3314 {
3315 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
3316 ++p;
3317 if (*p == NUL)
3318 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003319 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003320 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003321 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003322 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003323 ++p;
3324 if (*p == NUL)
3325 break;
3326 *p++ = NUL;
3327 }
3328
3329 /* Handle non-empty lines. */
3330 if (itemcnt > 0)
3331 {
3332 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
3333 && aff->af_enc == NULL)
3334 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003335#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003336 /* Setup for conversion from "ENC" to 'encoding'. */
3337 aff->af_enc = enc_canonize(items[1]);
3338 if (aff->af_enc != NULL && !spin->si_ascii
3339 && convert_setup(&spin->si_conv, aff->af_enc,
3340 p_enc) == FAIL)
3341 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
3342 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003343 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003344#else
3345 smsg((char_u *)_("Conversion in %s not supported"), fname);
3346#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003347 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003348 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2)
3349 {
3350 if (do_midword)
3351 spin->si_midword = vim_strsave(items[1]);
3352 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00003353 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
3354 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003355 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003356 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003357 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003358 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003359 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003360 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003361 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
3362 && aff->af_rar == 0)
3363 {
3364 aff->af_rar = items[1][0];
3365 if (items[1][1] != NUL)
3366 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3367 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00003368 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
3369 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003370 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003371 aff->af_kep = items[1][0];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003372 if (items[1][1] != NUL)
3373 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3374 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00003375 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
3376 && aff->af_bad == 0)
3377 {
3378 aff->af_bad = items[1][0];
3379 if (items[1][1] != NUL)
3380 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3381 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003382 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
3383 {
3384 aff->af_pfxpostpone = TRUE;
3385 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003386 else if ((STRCMP(items[0], "PFX") == 0
3387 || STRCMP(items[0], "SFX") == 0)
3388 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003389 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003390 {
Bram Moolenaar8db73182005-06-17 21:51:16 +00003391 /* Myspell allows extra text after the item, but that might
3392 * mean mistakes go unnoticed. Require a comment-starter. */
3393 if (itemcnt > 4 && *items[4] != '#')
3394 smsg((char_u *)_("Trailing text in %s line %d: %s"),
3395 fname, lnum, items[4]);
3396
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003397 /* New affix letter. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003398 cur_aff = (affheader_T *)getroom(&spin->si_blocks,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003399 sizeof(affheader_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003400 if (cur_aff == NULL)
3401 break;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003402#ifdef FEAT_MBYTE
3403 if (has_mbyte)
3404 {
3405 l = mb_ptr2len_check(items[1]);
3406 if (l >= AH_KEY_LEN)
3407 l = 1; /* too long, must be an overlong sequence */
3408 else
3409 mch_memmove(cur_aff->ah_key, items[1], l);
3410 }
3411 else
3412#endif
3413 {
3414 *cur_aff->ah_key = *items[1];
3415 l = 1;
3416 }
3417 cur_aff->ah_key[l] = NUL;
3418 if (items[1][l] != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003419 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003420 if (*items[2] == 'Y')
3421 cur_aff->ah_combine = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003422 else if (*items[2] != 'N')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003423 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
3424 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003425
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003426 if (*items[0] == 'P')
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003427 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003428 tp = &aff->af_pref;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003429 /* Use a new number in the .spl file later, to be able to
3430 * handle multiple .aff files. */
3431 if (aff->af_pfxpostpone)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003432 cur_aff->ah_newID = ++spin->si_newID;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003433 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003434 else
3435 tp = &aff->af_suff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003436 aff_todo = atoi((char *)items[3]);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003437 hi = hash_find(tp, cur_aff->ah_key);
3438 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar51485f02005-06-04 21:55:20 +00003439 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003440 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
3441 fname, lnum, items[1]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003442 aff_todo = 0;
3443 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003444 else
3445 hash_add(tp, cur_aff->ah_key);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003446 }
3447 else if ((STRCMP(items[0], "PFX") == 0
3448 || STRCMP(items[0], "SFX") == 0)
3449 && aff_todo > 0
3450 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003451 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003452 {
3453 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003454 int rare = FALSE;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003455 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003456 int lasti = 5;
3457
3458 /* Check for "rare" after the other info. */
3459 if (itemcnt > 5 && STRICMP(items[5], "rare") == 0)
3460 {
3461 rare = TRUE;
3462 lasti = 6;
3463 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003464
Bram Moolenaar8db73182005-06-17 21:51:16 +00003465 /* Myspell allows extra text after the item, but that might
3466 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003467 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar8db73182005-06-17 21:51:16 +00003468 smsg((char_u *)_("Trailing text in %s line %d: %s"),
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003469 fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00003470
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003471 /* New item for an affix letter. */
3472 --aff_todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003473 aff_entry = (affentry_T *)getroom(&spin->si_blocks,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003474 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003475 if (aff_entry == NULL)
3476 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003477 aff_entry->ae_rare = rare;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003478
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003479 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003480 aff_entry->ae_chop = getroom_save(&spin->si_blocks,
3481 items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003482 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003483 aff_entry->ae_add = getroom_save(&spin->si_blocks,
3484 items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003485
Bram Moolenaar51485f02005-06-04 21:55:20 +00003486 /* Don't use an affix entry with non-ASCII characters when
3487 * "spin->si_ascii" is TRUE. */
3488 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00003489 || has_non_ascii(aff_entry->ae_add)))
3490 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003491 aff_entry->ae_next = cur_aff->ah_first;
3492 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003493
3494 if (STRCMP(items[4], ".") != 0)
3495 {
3496 char_u buf[MAXLINELEN];
3497
3498 aff_entry->ae_cond = getroom_save(&spin->si_blocks,
3499 items[4]);
3500 if (*items[0] == 'P')
3501 sprintf((char *)buf, "^%s", items[4]);
3502 else
3503 sprintf((char *)buf, "%s$", items[4]);
3504 aff_entry->ae_prog = vim_regcomp(buf,
3505 RE_MAGIC + RE_STRING);
3506 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003507
3508 /* For postponed prefixes we need an entry in si_prefcond
3509 * for the condition. Use an existing one if possible. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003510 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003511 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003512 /* When the chop string is one lower-case letter and
3513 * the add string ends in the upper-case letter we set
3514 * the "upper" flag, clear "ae_chop" and remove the
3515 * letters from "ae_add". The condition must either
3516 * be empty or start with the same letter. */
3517 if (aff_entry->ae_chop != NULL
3518 && aff_entry->ae_add != NULL
3519#ifdef FEAT_MBYTE
3520 && aff_entry->ae_chop[mb_ptr2len_check(
3521 aff_entry->ae_chop)] == NUL
3522#else
3523 && aff_entry->ae_chop[1] == NUL
3524#endif
3525 )
3526 {
3527 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003528
Bram Moolenaar53805d12005-08-01 07:08:33 +00003529 c = PTR2CHAR(aff_entry->ae_chop);
3530 c_up = SPELL_TOUPPER(c);
3531 if (c_up != c
3532 && (aff_entry->ae_cond == NULL
3533 || PTR2CHAR(aff_entry->ae_cond) == c))
3534 {
3535 p = aff_entry->ae_add
3536 + STRLEN(aff_entry->ae_add);
3537 mb_ptr_back(aff_entry->ae_add, p);
3538 if (PTR2CHAR(p) == c_up)
3539 {
3540 upper = TRUE;
3541 aff_entry->ae_chop = NULL;
3542 *p = NUL;
3543
3544 /* The condition is matched with the
3545 * actual word, thus must check for the
3546 * upper-case letter. */
3547 if (aff_entry->ae_cond != NULL)
3548 {
3549 char_u buf[MAXLINELEN];
3550#ifdef FEAT_MBYTE
3551 if (has_mbyte)
3552 {
3553 onecap_copy(items[4], buf, TRUE);
3554 aff_entry->ae_cond = getroom_save(
3555 &spin->si_blocks, buf);
3556 }
3557 else
3558#endif
3559 *aff_entry->ae_cond = c_up;
3560 if (aff_entry->ae_cond != NULL)
3561 {
3562 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003563 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00003564 vim_free(aff_entry->ae_prog);
3565 aff_entry->ae_prog = vim_regcomp(
3566 buf, RE_MAGIC + RE_STRING);
3567 }
3568 }
3569 }
3570 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003571 }
3572
Bram Moolenaar53805d12005-08-01 07:08:33 +00003573 if (aff_entry->ae_chop == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003574 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003575 int idx;
3576 char_u **pp;
3577 int n;
3578
3579 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
3580 --idx)
3581 {
3582 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
3583 if (str_equal(p, aff_entry->ae_cond))
3584 break;
3585 }
3586 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
3587 {
3588 /* Not found, add a new condition. */
3589 idx = spin->si_prefcond.ga_len++;
3590 pp = ((char_u **)spin->si_prefcond.ga_data)
3591 + idx;
3592 if (aff_entry->ae_cond == NULL)
3593 *pp = NULL;
3594 else
3595 *pp = getroom_save(&spin->si_blocks,
3596 aff_entry->ae_cond);
3597 }
3598
3599 /* Add the prefix to the prefix tree. */
3600 if (aff_entry->ae_add == NULL)
3601 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003602 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00003603 p = aff_entry->ae_add;
3604 /* PFX_FLAGS is a negative number, so that
3605 * tree_add_word() knows this is the prefix tree. */
3606 n = PFX_FLAGS;
3607 if (rare)
3608 n |= WFP_RARE;
3609 if (!cur_aff->ah_combine)
3610 n |= WFP_NC;
3611 if (upper)
3612 n |= WFP_UP;
3613 tree_add_word(p, spin->si_prefroot, n,
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003614 idx, cur_aff->ah_newID, &spin->si_blocks);
Bram Moolenaar53805d12005-08-01 07:08:33 +00003615 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003616 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003617 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003618 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003619 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
3620 {
3621 if (fol != NULL)
3622 smsg((char_u *)_("Duplicate FOL in %s line %d"),
3623 fname, lnum);
3624 else
3625 fol = vim_strsave(items[1]);
3626 }
3627 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
3628 {
3629 if (low != NULL)
3630 smsg((char_u *)_("Duplicate LOW in %s line %d"),
3631 fname, lnum);
3632 else
3633 low = vim_strsave(items[1]);
3634 }
3635 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
3636 {
3637 if (upp != NULL)
3638 smsg((char_u *)_("Duplicate UPP in %s line %d"),
3639 fname, lnum);
3640 else
3641 upp = vim_strsave(items[1]);
3642 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003643 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003644 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003645 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003646 if (!isdigit(*items[1]))
3647 smsg((char_u *)_("Expected REP count in %s line %d"),
3648 fname, lnum);
3649 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003650 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
3651 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003652 /* REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003653 if (do_rep)
3654 add_fromto(spin, &spin->si_rep, items[1], items[2]);
3655 }
3656 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
3657 {
3658 /* MAP item or count */
3659 if (!found_map)
3660 {
3661 /* First line contains the count. */
3662 found_map = TRUE;
3663 if (!isdigit(*items[1]))
3664 smsg((char_u *)_("Expected MAP count in %s line %d"),
3665 fname, lnum);
3666 }
3667 else if (do_map)
3668 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003669 int c;
3670
3671 /* Check that every character appears only once. */
3672 for (p = items[1]; *p != NUL; )
3673 {
3674#ifdef FEAT_MBYTE
3675 c = mb_ptr2char_adv(&p);
3676#else
3677 c = *p++;
3678#endif
3679 if ((spin->si_map.ga_len > 0
3680 && vim_strchr(spin->si_map.ga_data, c)
3681 != NULL)
3682 || vim_strchr(p, c) != NULL)
3683 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
3684 fname, lnum);
3685 }
3686
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003687 /* We simply concatenate all the MAP strings, separated by
3688 * slashes. */
3689 ga_concat(&spin->si_map, items[1]);
3690 ga_append(&spin->si_map, '/');
3691 }
3692 }
3693 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
3694 {
3695 if (do_sal)
3696 {
3697 /* SAL item (sounds-a-like)
3698 * Either one of the known keys or a from-to pair. */
3699 if (STRCMP(items[1], "followup") == 0)
3700 spin->si_followup = sal_to_bool(items[2]);
3701 else if (STRCMP(items[1], "collapse_result") == 0)
3702 spin->si_collapse = sal_to_bool(items[2]);
3703 else if (STRCMP(items[1], "remove_accents") == 0)
3704 spin->si_rem_accents = sal_to_bool(items[2]);
3705 else
3706 /* when "to" is "_" it means empty */
3707 add_fromto(spin, &spin->si_sal, items[1],
3708 STRCMP(items[2], "_") == 0 ? (char_u *)""
3709 : items[2]);
3710 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003711 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003712 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
3713 && (!do_sofo || spin->si_sofofr == NULL))
3714 {
3715 if (do_sofo)
3716 spin->si_sofofr = vim_strsave(items[1]);
3717 }
3718 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
3719 && (!do_sofo || spin->si_sofoto == NULL))
3720 {
3721 if (do_sofo)
3722 spin->si_sofoto = vim_strsave(items[1]);
3723 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003724 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003725 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
3726 fname, lnum, items[0]);
3727 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003728 }
3729
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003730 if (do_sofo && (spin->si_sofofr == NULL) != (spin->si_sofoto == NULL))
3731 smsg((char_u *)_("Missing SOFO%s line in %s"),
3732 spin->si_sofofr == NULL ? "FROM" : "TO", fname);
3733 if (spin->si_sofofr != NULL && spin->si_sal.ga_len > 0)
3734 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
3735
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003736 if (fol != NULL || low != NULL || upp != NULL)
3737 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003738 if (spin->si_clear_chartab)
3739 {
3740 /* Clear the char type tables, don't want to use any of the
3741 * currently used spell properties. */
3742 init_spell_chartab();
3743 spin->si_clear_chartab = FALSE;
3744 }
3745
Bram Moolenaar3982c542005-06-08 21:56:31 +00003746 /*
3747 * Don't write a word table for an ASCII file, so that we don't check
3748 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003749 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00003750 * mb_get_class(), the list of chars in the file will be incomplete.
3751 */
3752 if (!spin->si_ascii
3753#ifdef FEAT_MBYTE
3754 && !enc_utf8
3755#endif
3756 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003757 {
3758 if (fol == NULL || low == NULL || upp == NULL)
3759 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
3760 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00003761 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003762 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003763
3764 vim_free(fol);
3765 vim_free(low);
3766 vim_free(upp);
3767 }
3768
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003769 vim_free(pc);
3770 fclose(fd);
3771 return aff;
3772}
3773
3774/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003775 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
3776 * NULL as equal.
3777 */
3778 static int
3779str_equal(s1, s2)
3780 char_u *s1;
3781 char_u *s2;
3782{
3783 if (s1 == NULL || s2 == NULL)
3784 return s1 == s2;
3785 return STRCMP(s1, s2) == 0;
3786}
3787
3788/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003789 * Add a from-to item to "gap". Used for REP and SAL items.
3790 * They are stored case-folded.
3791 */
3792 static void
3793add_fromto(spin, gap, from, to)
3794 spellinfo_T *spin;
3795 garray_T *gap;
3796 char_u *from;
3797 char_u *to;
3798{
3799 fromto_T *ftp;
3800 char_u word[MAXWLEN];
3801
3802 if (ga_grow(gap, 1) == OK)
3803 {
3804 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
3805 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
3806 ftp->ft_from = getroom_save(&spin->si_blocks, word);
3807 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
3808 ftp->ft_to = getroom_save(&spin->si_blocks, word);
3809 ++gap->ga_len;
3810 }
3811}
3812
3813/*
3814 * Convert a boolean argument in a SAL line to TRUE or FALSE;
3815 */
3816 static int
3817sal_to_bool(s)
3818 char_u *s;
3819{
3820 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
3821}
3822
3823/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00003824 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
3825 * When "s" is NULL FALSE is returned.
3826 */
3827 static int
3828has_non_ascii(s)
3829 char_u *s;
3830{
3831 char_u *p;
3832
3833 if (s != NULL)
3834 for (p = s; *p != NUL; ++p)
3835 if (*p >= 128)
3836 return TRUE;
3837 return FALSE;
3838}
3839
3840/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003841 * Free the structure filled by spell_read_aff().
3842 */
3843 static void
3844spell_free_aff(aff)
3845 afffile_T *aff;
3846{
3847 hashtab_T *ht;
3848 hashitem_T *hi;
3849 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003850 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003851 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003852
3853 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003854
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003855 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003856 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
3857 {
3858 todo = ht->ht_used;
3859 for (hi = ht->ht_array; todo > 0; ++hi)
3860 {
3861 if (!HASHITEM_EMPTY(hi))
3862 {
3863 --todo;
3864 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003865 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3866 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003867 }
3868 }
3869 if (ht == &aff->af_suff)
3870 break;
3871 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003872
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003873 hash_clear(&aff->af_pref);
3874 hash_clear(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003875}
3876
3877/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003878 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003879 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003880 */
3881 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00003882spell_read_dic(fname, spin, affile)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003883 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003884 spellinfo_T *spin;
3885 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003886{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003887 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003888 char_u line[MAXLINELEN];
Bram Moolenaar51485f02005-06-04 21:55:20 +00003889 char_u *afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003890 char_u *pfxlist;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003891 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003892 char_u *pc;
3893 char_u *w;
3894 int l;
3895 hash_T hash;
3896 hashitem_T *hi;
3897 FILE *fd;
3898 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003899 int non_ascii = 0;
3900 int retval = OK;
3901 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003902 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003903
Bram Moolenaar51485f02005-06-04 21:55:20 +00003904 /*
3905 * Open the file.
3906 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003907 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003908 if (fd == NULL)
3909 {
3910 EMSG2(_(e_notopen), fname);
3911 return FAIL;
3912 }
3913
Bram Moolenaar51485f02005-06-04 21:55:20 +00003914 /* The hashtable is only used to detect duplicated words. */
3915 hash_init(&ht);
3916
Bram Moolenaar8db73182005-06-17 21:51:16 +00003917 spin->si_foldwcount = 0;
3918 spin->si_keepwcount = 0;
3919
Bram Moolenaarb765d632005-06-07 21:00:02 +00003920 if (spin->si_verbose || p_verbose > 2)
3921 {
3922 if (!spin->si_verbose)
3923 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003924 smsg((char_u *)_("Reading dictionary file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003925 out_flush();
3926 if (!spin->si_verbose)
3927 verbose_leave();
3928 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003929
3930 /* Read and ignore the first line: word count. */
3931 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003932 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003933 EMSG2(_("E760: No word count in %s"), fname);
3934
3935 /*
3936 * Read all the lines in the file one by one.
3937 * The words are converted to 'encoding' here, before being added to
3938 * the hashtable.
3939 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003940 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003941 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003942 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003943 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003944 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003945 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003946
Bram Moolenaar51485f02005-06-04 21:55:20 +00003947 /* Remove CR, LF and white space from the end. White space halfway
3948 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003949 l = STRLEN(line);
3950 while (l > 0 && line[l - 1] <= ' ')
3951 --l;
3952 if (l == 0)
3953 continue; /* empty line */
3954 line[l] = NUL;
3955
Bram Moolenaar51485f02005-06-04 21:55:20 +00003956 /* Find the optional affix names. */
3957 afflist = vim_strchr(line, '/');
3958 if (afflist != NULL)
3959 *afflist++ = NUL;
3960
3961 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
3962 if (spin->si_ascii && has_non_ascii(line))
3963 {
3964 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003965 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003966 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003967
Bram Moolenaarb765d632005-06-07 21:00:02 +00003968#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003969 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003970 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003971 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003972 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003973 if (pc == NULL)
3974 {
3975 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3976 fname, lnum, line);
3977 continue;
3978 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003979 w = pc;
3980 }
3981 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003982#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003983 {
3984 pc = NULL;
3985 w = line;
3986 }
3987
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003988 /* This takes time, print a message now and then. */
3989 if (spin->si_verbose && (lnum & 0x3ff) == 0)
3990 {
3991 vim_snprintf((char *)message, sizeof(message),
3992 _("line %6d, word %6d - %s"),
3993 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
3994 msg_start();
3995 msg_puts_long_attr(message, 0);
3996 msg_clr_eos();
3997 msg_didout = FALSE;
3998 msg_col = 0;
3999 out_flush();
4000 }
4001
Bram Moolenaar51485f02005-06-04 21:55:20 +00004002 /* Store the word in the hashtable to be able to find duplicates. */
4003 dw = (char_u *)getroom_save(&spin->si_blocks, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004004 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004005 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004006 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004007 if (retval == FAIL)
4008 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004009
Bram Moolenaar51485f02005-06-04 21:55:20 +00004010 hash = hash_hash(dw);
4011 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004012 if (!HASHITEM_EMPTY(hi))
4013 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004014 fname, lnum, dw);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004015 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00004016 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004017
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004018 flags = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004019 pfxlist = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004020 if (afflist != NULL)
4021 {
4022 /* Check for affix name that stands for keep-case word and stands
4023 * for rare word (if defined). */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004024 if (affile->af_kep != NUL
4025 && vim_strchr(afflist, affile->af_kep) != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004026 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004027 if (affile->af_rar != NUL
4028 && vim_strchr(afflist, affile->af_rar) != NULL)
4029 flags |= WF_RARE;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004030 if (affile->af_bad != NUL
4031 && vim_strchr(afflist, affile->af_bad) != NULL)
4032 flags |= WF_BANNED;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004033
4034 if (affile->af_pfxpostpone)
4035 /* Need to store the list of prefix IDs with the word. */
4036 pfxlist = get_pfxlist(affile, afflist, &spin->si_blocks);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004037 }
4038
Bram Moolenaar51485f02005-06-04 21:55:20 +00004039 /* Add the word to the word tree(s). */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004040 if (store_word(dw, spin, flags, spin->si_region, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004041 retval = FAIL;
4042
4043 if (afflist != NULL)
4044 {
4045 /* Find all matching suffixes and add the resulting words.
4046 * Additionally do matching prefixes that combine. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004047 if (store_aff_word(dw, spin, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004048 &affile->af_suff, &affile->af_pref,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004049 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004050 retval = FAIL;
4051
4052 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004053 if (store_aff_word(dw, spin, afflist, affile,
4054 &affile->af_pref, NULL,
4055 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004056 retval = FAIL;
4057 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004058 }
4059
Bram Moolenaar51485f02005-06-04 21:55:20 +00004060 if (spin->si_ascii && non_ascii > 0)
4061 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
4062 non_ascii);
4063 hash_clear(&ht);
4064
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004065 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004066 return retval;
4067}
4068
4069/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004070 * Get the list of prefix IDs from the affix list "afflist".
4071 * Used for PFXPOSTPONE.
4072 * Returns a string allocated with getroom(). NULL when there are no prefixes
4073 * or when out of memory.
4074 */
4075 static char_u *
4076get_pfxlist(affile, afflist, blp)
4077 afffile_T *affile;
4078 char_u *afflist;
4079 sblock_T **blp;
4080{
4081 char_u *p;
4082 int cnt;
4083 int round;
4084 char_u *res = NULL;
4085 char_u key[2];
4086 hashitem_T *hi;
4087
4088 key[1] = NUL;
4089
4090 /* round 1: count the number of prefix IDs.
4091 * round 2: move prefix IDs to "res" */
4092 for (round = 1; round <= 2; ++round)
4093 {
4094 cnt = 0;
4095 for (p = afflist; *p != NUL; ++p)
4096 {
4097 key[0] = *p;
4098 hi = hash_find(&affile->af_pref, key);
4099 if (!HASHITEM_EMPTY(hi))
4100 {
4101 /* This is a prefix ID, use the new number. */
4102 if (round == 2)
4103 res[cnt] = HI2AH(hi)->ah_newID;
4104 ++cnt;
4105 }
4106 }
4107 if (round == 1 && cnt > 0)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004108 res = getroom(blp, cnt + 1, FALSE);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004109 if (res == NULL)
4110 break;
4111 }
4112
4113 if (res != NULL)
4114 res[cnt] = NUL;
4115 return res;
4116}
4117
4118/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004119 * Apply affixes to a word and store the resulting words.
4120 * "ht" is the hashtable with affentry_T that need to be applied, either
4121 * prefixes or suffixes.
4122 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
4123 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004124 *
4125 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004126 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004127 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004128store_aff_word(word, spin, afflist, affile, ht, xht, comb, flags, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004129 char_u *word; /* basic word start */
4130 spellinfo_T *spin; /* spell info */
4131 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004132 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004133 hashtab_T *ht;
4134 hashtab_T *xht;
4135 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004136 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004137 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004138{
4139 int todo;
4140 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004141 affheader_T *ah;
4142 affentry_T *ae;
4143 regmatch_T regmatch;
4144 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004145 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004146 int i;
4147 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004148 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004149 char_u *use_pfxlist;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004150 int c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004151
Bram Moolenaar51485f02005-06-04 21:55:20 +00004152 todo = ht->ht_used;
4153 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004154 {
4155 if (!HASHITEM_EMPTY(hi))
4156 {
4157 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004158 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00004159
Bram Moolenaar51485f02005-06-04 21:55:20 +00004160 /* Check that the affix combines, if required, and that the word
4161 * supports this affix. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004162 c = PTR2CHAR(ah->ah_key);
4163 if ((!comb || ah->ah_combine) && vim_strchr(afflist, c) != NULL)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004164 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004165 /* Loop over all affix entries with this name. */
4166 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004167 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004168 /* Check the condition. It's not logical to match case
4169 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004170 * Myspell.
4171 * For prefixes, when "PFXPOSTPONE" was used, only do
4172 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004173 regmatch.regprog = ae->ae_prog;
4174 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004175 if ((xht != NULL || !affile->af_pfxpostpone
4176 || ae->ae_chop != NULL)
4177 && (ae->ae_prog == NULL
4178 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004179 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004180 /* Match. Remove the chop and add the affix. */
4181 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004182 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004183 /* prefix: chop/add at the start of the word */
4184 if (ae->ae_add == NULL)
4185 *newword = NUL;
4186 else
4187 STRCPY(newword, ae->ae_add);
4188 p = word;
4189 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004190 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004191 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004192#ifdef FEAT_MBYTE
4193 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004194 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004195 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004196 for ( ; i > 0; --i)
4197 mb_ptr_adv(p);
4198 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004199 else
4200#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004201 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004202 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004203 STRCAT(newword, p);
4204 }
4205 else
4206 {
4207 /* suffix: chop/add at the end of the word */
4208 STRCPY(newword, word);
4209 if (ae->ae_chop != NULL)
4210 {
4211 /* Remove chop string. */
4212 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004213 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004214 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004215 mb_ptr_back(newword, p);
4216 *p = NUL;
4217 }
4218 if (ae->ae_add != NULL)
4219 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004220 }
4221
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004222 /* Obey the "rare" flag of the affix. */
4223 if (ae->ae_rare)
4224 use_flags = flags | WF_RARE;
4225 else
4226 use_flags = flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004227 use_pfxlist = pfxlist;
4228
4229 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00004230 if (spin->si_prefroot != NULL
4231 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004232 {
4233 /* ... add a flag to indicate an affix was used. */
4234 use_flags |= WF_HAS_AFF;
4235
4236 /* ... don't use a prefix list if combining
4237 * affixes is not allowed */
4238 if (!ah->ah_combine || comb)
4239 use_pfxlist = NULL;
4240 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004241
Bram Moolenaar51485f02005-06-04 21:55:20 +00004242 /* Store the modified word. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004243 if (store_word(newword, spin, use_flags,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004244 spin->si_region, use_pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004245 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004246
Bram Moolenaar51485f02005-06-04 21:55:20 +00004247 /* When added a suffix and combining is allowed also
4248 * try adding prefixes additionally. */
4249 if (xht != NULL && ah->ah_combine)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004250 if (store_aff_word(newword, spin, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004251 xht, NULL, TRUE,
4252 use_flags, use_pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004253 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004254 }
4255 }
4256 }
4257 }
4258 }
4259
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004260 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004261}
4262
4263/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004264 * Read a file with a list of words.
4265 */
4266 static int
4267spell_read_wordfile(fname, spin)
4268 char_u *fname;
4269 spellinfo_T *spin;
4270{
4271 FILE *fd;
4272 long lnum = 0;
4273 char_u rline[MAXLINELEN];
4274 char_u *line;
4275 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004276 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004277 int l;
4278 int retval = OK;
4279 int did_word = FALSE;
4280 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004281 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004282 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004283
4284 /*
4285 * Open the file.
4286 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004287 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00004288 if (fd == NULL)
4289 {
4290 EMSG2(_(e_notopen), fname);
4291 return FAIL;
4292 }
4293
Bram Moolenaarb765d632005-06-07 21:00:02 +00004294 if (spin->si_verbose || p_verbose > 2)
4295 {
4296 if (!spin->si_verbose)
4297 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004298 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004299 out_flush();
4300 if (!spin->si_verbose)
4301 verbose_leave();
4302 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004303
4304 /*
4305 * Read all the lines in the file one by one.
4306 */
4307 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
4308 {
4309 line_breakcheck();
4310 ++lnum;
4311
4312 /* Skip comment lines. */
4313 if (*rline == '#')
4314 continue;
4315
4316 /* Remove CR, LF and white space from the end. */
4317 l = STRLEN(rline);
4318 while (l > 0 && rline[l - 1] <= ' ')
4319 --l;
4320 if (l == 0)
4321 continue; /* empty or blank line */
4322 rline[l] = NUL;
4323
4324 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
4325 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004326#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004327 if (spin->si_conv.vc_type != CONV_NONE)
4328 {
4329 pc = string_convert(&spin->si_conv, rline, NULL);
4330 if (pc == NULL)
4331 {
4332 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4333 fname, lnum, rline);
4334 continue;
4335 }
4336 line = pc;
4337 }
4338 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004339#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004340 {
4341 pc = NULL;
4342 line = rline;
4343 }
4344
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004345 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00004346 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004347 ++line;
4348 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004349 {
4350 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004351 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
4352 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004353 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004354 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
4355 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004356 else
4357 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004358#ifdef FEAT_MBYTE
4359 char_u *enc;
4360
Bram Moolenaar51485f02005-06-04 21:55:20 +00004361 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004362 line += 10;
4363 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004364 if (enc != NULL && !spin->si_ascii
4365 && convert_setup(&spin->si_conv, enc,
4366 p_enc) == FAIL)
4367 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00004368 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004369 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004370 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004371#else
4372 smsg((char_u *)_("Conversion in %s not supported"), fname);
4373#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004374 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004375 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004376 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004377
Bram Moolenaar3982c542005-06-08 21:56:31 +00004378 if (STRNCMP(line, "regions=", 8) == 0)
4379 {
4380 if (spin->si_region_count > 1)
4381 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
4382 fname, lnum, line);
4383 else
4384 {
4385 line += 8;
4386 if (STRLEN(line) > 16)
4387 smsg((char_u *)_("Too many regions in %s line %d: %s"),
4388 fname, lnum, line);
4389 else
4390 {
4391 spin->si_region_count = STRLEN(line) / 2;
4392 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004393
4394 /* Adjust the mask for a word valid in all regions. */
4395 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004396 }
4397 }
4398 continue;
4399 }
4400
Bram Moolenaar7887d882005-07-01 22:33:52 +00004401 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
4402 fname, lnum, line - 1);
4403 continue;
4404 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004405
Bram Moolenaar7887d882005-07-01 22:33:52 +00004406 flags = 0;
4407 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004408
Bram Moolenaar7887d882005-07-01 22:33:52 +00004409 /* Check for flags and region after a slash. */
4410 p = vim_strchr(line, '/');
4411 if (p != NULL)
4412 {
4413 *p++ = NUL;
4414 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004415 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004416 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004417 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004418 else if (*p == '!') /* Bad, bad, wicked word. */
4419 flags |= WF_BANNED;
4420 else if (*p == '?') /* Rare word. */
4421 flags |= WF_RARE;
4422 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004423 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004424 if ((flags & WF_REGION) == 0) /* first one */
4425 regionmask = 0;
4426 flags |= WF_REGION;
4427
4428 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00004429 if (l > spin->si_region_count)
4430 {
4431 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00004432 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004433 break;
4434 }
4435 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004436 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00004437 else
4438 {
4439 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
4440 fname, lnum, p);
4441 break;
4442 }
4443 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004444 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004445 }
4446
4447 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
4448 if (spin->si_ascii && has_non_ascii(line))
4449 {
4450 ++non_ascii;
4451 continue;
4452 }
4453
4454 /* Normal word: store it. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004455 if (store_word(line, spin, flags, regionmask, NULL) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004456 {
4457 retval = FAIL;
4458 break;
4459 }
4460 did_word = TRUE;
4461 }
4462
4463 vim_free(pc);
4464 fclose(fd);
4465
Bram Moolenaarb765d632005-06-07 21:00:02 +00004466 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
4467 {
4468 if (p_verbose > 2)
4469 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00004470 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
4471 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004472 if (p_verbose > 2)
4473 verbose_leave();
4474 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004475 return retval;
4476}
4477
4478/*
4479 * Get part of an sblock_T, "len" bytes long.
4480 * This avoids calling free() for every little struct we use.
4481 * The memory is cleared to all zeros.
4482 * Returns NULL when out of memory.
4483 */
4484 static void *
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004485getroom(blp, len, align)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004486 sblock_T **blp;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004487 size_t len; /* length needed */
4488 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004489{
4490 char_u *p;
4491 sblock_T *bl = *blp;
4492
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004493 if (align && bl != NULL)
4494 /* Round size up for alignment. On some systems structures need to be
4495 * aligned to the size of a pointer (e.g., SPARC). */
4496 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
4497 & ~(sizeof(char *) - 1);
4498
Bram Moolenaar51485f02005-06-04 21:55:20 +00004499 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
4500 {
4501 /* Allocate a block of memory. This is not freed until much later. */
4502 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
4503 if (bl == NULL)
4504 return NULL;
4505 bl->sb_next = *blp;
4506 *blp = bl;
4507 bl->sb_used = 0;
4508 }
4509
4510 p = bl->sb_data + bl->sb_used;
4511 bl->sb_used += len;
4512
4513 return p;
4514}
4515
4516/*
4517 * Make a copy of a string into memory allocated with getroom().
4518 */
4519 static char_u *
4520getroom_save(blp, s)
4521 sblock_T **blp;
4522 char_u *s;
4523{
4524 char_u *sc;
4525
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004526 sc = (char_u *)getroom(blp, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004527 if (sc != NULL)
4528 STRCPY(sc, s);
4529 return sc;
4530}
4531
4532
4533/*
4534 * Free the list of allocated sblock_T.
4535 */
4536 static void
4537free_blocks(bl)
4538 sblock_T *bl;
4539{
4540 sblock_T *next;
4541
4542 while (bl != NULL)
4543 {
4544 next = bl->sb_next;
4545 vim_free(bl);
4546 bl = next;
4547 }
4548}
4549
4550/*
4551 * Allocate the root of a word tree.
4552 */
4553 static wordnode_T *
4554wordtree_alloc(blp)
4555 sblock_T **blp;
4556{
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004557 return (wordnode_T *)getroom(blp, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004558}
4559
4560/*
4561 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004562 * Always store it in the case-folded tree. For a keep-case word this is
4563 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
4564 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004565 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004566 * When "pfxlist" is not NULL store the word for each postponed prefix ID.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004567 */
4568 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004569store_word(word, spin, flags, region, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004570 char_u *word;
4571 spellinfo_T *spin;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004572 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004573 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004574 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004575{
4576 int len = STRLEN(word);
4577 int ct = captype(word, word + len);
4578 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004579 int res = OK;
4580 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004581
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004582 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004583 for (p = pfxlist; res == OK; ++p)
4584 {
4585 res = tree_add_word(foldword, spin->si_foldroot, ct | flags,
4586 region, p == NULL ? 0 : *p, &spin->si_blocks);
4587 if (p == NULL || *p == NUL)
4588 break;
4589 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00004590 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004591
4592 if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP))
Bram Moolenaar8db73182005-06-17 21:51:16 +00004593 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004594 for (p = pfxlist; res == OK; ++p)
4595 {
4596 res = tree_add_word(word, spin->si_keeproot, flags,
4597 region, p == NULL ? 0 : *p, &spin->si_blocks);
4598 if (p == NULL || *p == NUL)
4599 break;
4600 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00004601 ++spin->si_keepwcount;
4602 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004603 return res;
4604}
4605
4606/*
4607 * Add word "word" to a word tree at "root".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004608 * When "flags" < 0 we are adding to the prefix tree where flags is used for
4609 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004610 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004611 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004612 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004613tree_add_word(word, root, flags, region, prefixID, blp)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004614 char_u *word;
4615 wordnode_T *root;
4616 int flags;
4617 int region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004618 int prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004619 sblock_T **blp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004620{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004621 wordnode_T *node = root;
4622 wordnode_T *np;
4623 wordnode_T **prev = NULL;
4624 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004625
Bram Moolenaar51485f02005-06-04 21:55:20 +00004626 /* Add each byte of the word to the tree, including the NUL at the end. */
4627 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004628 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004629 /* Look for the sibling that has the same character. They are sorted
4630 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004631 * higher byte value. For zero bytes (end of word) the sorting is
4632 * done on flags and then on prefixID
Bram Moolenaar51485f02005-06-04 21:55:20 +00004633 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004634 while (node != NULL
4635 && (node->wn_byte < word[i]
4636 || (node->wn_byte == NUL
4637 && (flags < 0
4638 ? node->wn_prefixID < prefixID
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004639 : node->wn_flags < (flags & WN_MASK)
4640 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004641 && node->wn_prefixID < prefixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004642 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004643 prev = &node->wn_sibling;
4644 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004645 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004646 if (node == NULL
4647 || node->wn_byte != word[i]
4648 || (word[i] == NUL
4649 && (flags < 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004650 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004651 || node->wn_prefixID != prefixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004652 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004653 /* Allocate a new node. */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004654 np = (wordnode_T *)getroom(blp, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004655 if (np == NULL)
4656 return FAIL;
4657 np->wn_byte = word[i];
4658 *prev = np;
4659 np->wn_sibling = node;
4660 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004661 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004662
Bram Moolenaar51485f02005-06-04 21:55:20 +00004663 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004664 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004665 node->wn_flags = flags;
4666 node->wn_region |= region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004667 node->wn_prefixID = prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004668 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004669 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004670 prev = &node->wn_child;
4671 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004672 }
4673
4674 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004675}
4676
4677/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004678 * Compress a tree: find tails that are identical and can be shared.
4679 */
4680 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00004681wordtree_compress(root, spin)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004682 wordnode_T *root;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004683 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004684{
4685 hashtab_T ht;
4686 int n;
4687 int tot = 0;
4688
4689 if (root != NULL)
4690 {
4691 hash_init(&ht);
4692 n = node_compress(root, &ht, &tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004693 if (spin->si_verbose || p_verbose > 2)
4694 {
4695 if (!spin->si_verbose)
4696 verbose_enter();
4697 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar51485f02005-06-04 21:55:20 +00004698 n, tot, (tot - n) * 100 / tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004699 if (p_verbose > 2)
4700 verbose_leave();
4701 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004702 hash_clear(&ht);
4703 }
4704}
4705
4706/*
4707 * Compress a node, its siblings and its children, depth first.
4708 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004709 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004710 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00004711node_compress(node, ht, tot)
4712 wordnode_T *node;
4713 hashtab_T *ht;
4714 int *tot; /* total count of nodes before compressing,
4715 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004716{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004717 wordnode_T *np;
4718 wordnode_T *tp;
4719 wordnode_T *child;
4720 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004721 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004722 int len = 0;
4723 unsigned nr, n;
4724 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004725
Bram Moolenaar51485f02005-06-04 21:55:20 +00004726 /*
4727 * Go through the list of siblings. Compress each child and then try
4728 * finding an identical child to replace it.
4729 * Note that with "child" we mean not just the node that is pointed to,
4730 * but the whole list of siblings, of which the node is the first.
4731 */
4732 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004733 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004734 ++len;
4735 if ((child = np->wn_child) != NULL)
4736 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00004737 /* Compress the child. This fills hashkey. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004738 compressed += node_compress(child, ht, tot);
4739
4740 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004741 hash = hash_hash(child->wn_u1.hashkey);
4742 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004743 tp = NULL;
4744 if (!HASHITEM_EMPTY(hi))
4745 {
4746 /* There are children with an identical hash value. Now check
4747 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004748 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004749 if (node_equal(child, tp))
4750 {
4751 /* Found one! Now use that child in place of the
4752 * current one. This means the current child is
4753 * dropped from the tree. */
4754 np->wn_child = tp;
4755 ++compressed;
4756 break;
4757 }
4758 if (tp == NULL)
4759 {
4760 /* No other child with this hash value equals the child of
4761 * the node, add it to the linked list after the first
4762 * item. */
4763 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004764 child->wn_u2.next = tp->wn_u2.next;
4765 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004766 }
4767 }
4768 else
4769 /* No other child has this hash value, add it to the
4770 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004771 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004772 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004773 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004774 *tot += len;
4775
4776 /*
4777 * Make a hash key for the node and its siblings, so that we can quickly
4778 * find a lookalike node. This must be done after compressing the sibling
4779 * list, otherwise the hash key would become invalid by the compression.
4780 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004781 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004782 nr = 0;
4783 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004784 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004785 if (np->wn_byte == NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004786 /* end node: use wn_flags, wn_region and wn_prefixID */
4787 n = np->wn_flags + (np->wn_region << 8) + (np->wn_prefixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004788 else
4789 /* byte node: use the byte value and the child pointer */
4790 n = np->wn_byte + ((long_u)np->wn_child << 8);
4791 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004792 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004793
4794 /* Avoid NUL bytes, it terminates the hash key. */
4795 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004796 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004797 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004798 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004799 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004800 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004801 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004802 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
4803 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004804
4805 return compressed;
4806}
4807
4808/*
4809 * Return TRUE when two nodes have identical siblings and children.
4810 */
4811 static int
4812node_equal(n1, n2)
4813 wordnode_T *n1;
4814 wordnode_T *n2;
4815{
4816 wordnode_T *p1;
4817 wordnode_T *p2;
4818
4819 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
4820 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
4821 if (p1->wn_byte != p2->wn_byte
4822 || (p1->wn_byte == NUL
4823 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004824 || p1->wn_region != p2->wn_region
4825 || p1->wn_prefixID != p2->wn_prefixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004826 : (p1->wn_child != p2->wn_child)))
4827 break;
4828
4829 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004830}
4831
4832/*
4833 * Write a number to file "fd", MSB first, in "len" bytes.
4834 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004835 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004836put_bytes(fd, nr, len)
4837 FILE *fd;
4838 long_u nr;
4839 int len;
4840{
4841 int i;
4842
4843 for (i = len - 1; i >= 0; --i)
4844 putc((int)(nr >> (i * 8)), fd);
4845}
4846
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004847static int
4848#ifdef __BORLANDC__
4849_RTLENTRYF
4850#endif
4851rep_compare __ARGS((const void *s1, const void *s2));
4852
4853/*
4854 * Function given to qsort() to sort the REP items on "from" string.
4855 */
4856 static int
4857#ifdef __BORLANDC__
4858_RTLENTRYF
4859#endif
4860rep_compare(s1, s2)
4861 const void *s1;
4862 const void *s2;
4863{
4864 fromto_T *p1 = (fromto_T *)s1;
4865 fromto_T *p2 = (fromto_T *)s2;
4866
4867 return STRCMP(p1->ft_from, p2->ft_from);
4868}
4869
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004870/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004871 * Write the Vim spell file "fname".
4872 */
4873 static void
Bram Moolenaar3982c542005-06-08 21:56:31 +00004874write_vim_spell(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004875 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004876 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004877{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004878 FILE *fd;
4879 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004880 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004881 wordnode_T *tree;
4882 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004883 int i;
4884 int l;
4885 garray_T *gap;
4886 fromto_T *ftp;
4887 char_u *p;
4888 int rr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004889
Bram Moolenaarb765d632005-06-07 21:00:02 +00004890 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00004891 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004892 {
4893 EMSG2(_(e_notopen), fname);
4894 return;
4895 }
4896
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004897 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004898 * <charflagslen> <charflags>
4899 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004900 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004901 * <prefcondcnt> <prefcond> ... */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004902
4903 /* <fileID> */
4904 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
4905 EMSG(_(e_write));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004906
4907 /* write the region names if there is more than one */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004908 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004909 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004910 putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */
4911 fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2),
4912 (size_t)1, fd);
4913 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004914 }
4915 else
4916 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004917 putc(0, fd);
4918 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004919 }
4920
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004921 /*
4922 * Write the table with character flags and table for case folding.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004923 * <charflagslen> <charflags> <fcharlen> <fchars>
4924 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004925 * 'encoding'.
4926 * Also skip this for an .add.spl file, the main spell file must contain
4927 * the table (avoids that it conflicts). File is shorter too.
4928 */
4929 if (spin->si_ascii || spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004930 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004931 putc(0, fd);
4932 putc(0, fd);
4933 putc(0, fd);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004934 }
4935 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00004936 write_spell_chartab(fd);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004937
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004938
4939 if (spin->si_midword == NULL)
4940 put_bytes(fd, 0L, 2); /* <midwordlen> */
4941 else
4942 {
4943 i = STRLEN(spin->si_midword);
4944 put_bytes(fd, (long_u)i, 2); /* <midwordlen> */
4945 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
4946 }
4947
4948
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004949 /* Write the prefix conditions. */
4950 write_spell_prefcond(fd, &spin->si_prefcond);
4951
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004952 /* <SUGGEST> : <repcount> <rep> ...
4953 * <salflags> <salcount> <sal> ...
4954 * <maplen> <mapstr> */
4955
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004956 /* Sort the REP items. */
4957 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
4958 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004959
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004960 /* round 1: REP items
4961 * round 2: SAL items (unless SOFO is used) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004962 for (round = 1; round <= 2; ++round)
4963 {
4964 if (round == 1)
4965 gap = &spin->si_rep;
4966 else
4967 {
4968 gap = &spin->si_sal;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004969
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004970 i = 0;
4971 if (spin->si_followup)
4972 i |= SAL_F0LLOWUP;
4973 if (spin->si_collapse)
4974 i |= SAL_COLLAPSE;
4975 if (spin->si_rem_accents)
4976 i |= SAL_REM_ACCENTS;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004977 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
4978 i |= SAL_SOFO;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004979 putc(i, fd); /* <salflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004980 if (i & SAL_SOFO)
4981 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004982 }
4983
4984 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
4985 for (i = 0; i < gap->ga_len; ++i)
4986 {
4987 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
4988 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
4989 ftp = &((fromto_T *)gap->ga_data)[i];
4990 for (rr = 1; rr <= 2; ++rr)
4991 {
4992 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
4993 l = STRLEN(p);
4994 putc(l, fd);
4995 fwrite(p, l, (size_t)1, fd);
4996 }
4997 }
4998 }
4999
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005000 /* SOFOFROM and SOFOTO */
5001 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
5002 {
5003 put_bytes(fd, 1L, 2); /* <salcount> */
5004
5005 l = STRLEN(spin->si_sofofr);
5006 put_bytes(fd, (long_u)l, 2); /* <salfromlen> */
5007 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <salfrom> */
5008
5009 l = STRLEN(spin->si_sofoto);
5010 put_bytes(fd, (long_u)l, 2); /* <saltolen> */
5011 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <salto> */
5012 }
5013
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005014 put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */
5015 if (spin->si_map.ga_len > 0) /* <mapstr> */
5016 fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len,
5017 (size_t)1, fd);
Bram Moolenaar50cde822005-06-05 21:54:54 +00005018
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005019 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005020 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005021 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005022 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005023 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005024 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005025 if (round == 1)
5026 tree = spin->si_foldroot;
5027 else if (round == 2)
5028 tree = spin->si_keeproot;
5029 else
5030 tree = spin->si_prefroot;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005031
Bram Moolenaar0c405862005-06-22 22:26:26 +00005032 /* Clear the index and wnode fields in the tree. */
5033 clear_node(tree);
5034
Bram Moolenaar51485f02005-06-04 21:55:20 +00005035 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00005036 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00005037 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005038 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005039
Bram Moolenaar51485f02005-06-04 21:55:20 +00005040 /* number of nodes in 4 bytes */
5041 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00005042 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005043
Bram Moolenaar51485f02005-06-04 21:55:20 +00005044 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005045 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005046 }
5047
Bram Moolenaar51485f02005-06-04 21:55:20 +00005048 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005049}
5050
5051/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00005052 * Clear the index and wnode fields of "node", it siblings and its
5053 * children. This is needed because they are a union with other items to save
5054 * space.
5055 */
5056 static void
5057clear_node(node)
5058 wordnode_T *node;
5059{
5060 wordnode_T *np;
5061
5062 if (node != NULL)
5063 for (np = node; np != NULL; np = np->wn_sibling)
5064 {
5065 np->wn_u1.index = 0;
5066 np->wn_u2.wnode = NULL;
5067
5068 if (np->wn_byte != NUL)
5069 clear_node(np->wn_child);
5070 }
5071}
5072
5073
5074/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005075 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005076 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00005077 * This first writes the list of possible bytes (siblings). Then for each
5078 * byte recursively write the children.
5079 *
5080 * NOTE: The code here must match the code in read_tree(), since assumptions
5081 * are made about the indexes (so that we don't have to write them in the
5082 * file).
5083 *
5084 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005085 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005086 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00005087put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005088 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005089 wordnode_T *node;
5090 int index;
5091 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005092 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005093{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005094 int newindex = index;
5095 int siblingcount = 0;
5096 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005097 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005098
Bram Moolenaar51485f02005-06-04 21:55:20 +00005099 /* If "node" is zero the tree is empty. */
5100 if (node == NULL)
5101 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005102
Bram Moolenaar51485f02005-06-04 21:55:20 +00005103 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005104 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005105
5106 /* Count the number of siblings. */
5107 for (np = node; np != NULL; np = np->wn_sibling)
5108 ++siblingcount;
5109
5110 /* Write the sibling count. */
5111 if (fd != NULL)
5112 putc(siblingcount, fd); /* <siblingcount> */
5113
5114 /* Write each sibling byte and optionally extra info. */
5115 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005116 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005117 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005118 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005119 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005120 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005121 /* For a NUL byte (end of word) write the flags etc. */
5122 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005123 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005124 /* In PREFIXTREE write the required prefixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005125 * associated condition nr (stored in wn_region). The
5126 * byte value is misused to store the "rare" and "not
5127 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005128 if (np->wn_flags == (short_u)PFX_FLAGS)
5129 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005130 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005131 {
5132 putc(BY_FLAGS, fd); /* <byte> */
5133 putc(np->wn_flags, fd); /* <pflags> */
5134 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005135 putc(np->wn_prefixID, fd); /* <prefixID> */
5136 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005137 }
5138 else
5139 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005140 /* For word trees we write the flag/region items. */
5141 flags = np->wn_flags;
5142 if (regionmask != 0 && np->wn_region != regionmask)
5143 flags |= WF_REGION;
5144 if (np->wn_prefixID != 0)
5145 flags |= WF_PFX;
5146 if (flags == 0)
5147 {
5148 /* word without flags or region */
5149 putc(BY_NOFLAGS, fd); /* <byte> */
5150 }
5151 else
5152 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005153 if (np->wn_flags >= 0x100)
5154 {
5155 putc(BY_FLAGS2, fd); /* <byte> */
5156 putc(flags, fd); /* <flags> */
5157 putc((unsigned)flags >> 8, fd); /* <flags2> */
5158 }
5159 else
5160 {
5161 putc(BY_FLAGS, fd); /* <byte> */
5162 putc(flags, fd); /* <flags> */
5163 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005164 if (flags & WF_REGION)
5165 putc(np->wn_region, fd); /* <region> */
5166 if (flags & WF_PFX)
5167 putc(np->wn_prefixID, fd); /* <prefixID> */
5168 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005169 }
5170 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005171 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005172 else
5173 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005174 if (np->wn_child->wn_u1.index != 0
5175 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005176 {
5177 /* The child is written elsewhere, write the reference. */
5178 if (fd != NULL)
5179 {
5180 putc(BY_INDEX, fd); /* <byte> */
5181 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005182 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005183 }
5184 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00005185 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005186 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005187 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005188
Bram Moolenaar51485f02005-06-04 21:55:20 +00005189 if (fd != NULL)
5190 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
5191 {
5192 EMSG(_(e_write));
5193 return 0;
5194 }
5195 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005196 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005197
5198 /* Space used in the array when reading: one for each sibling and one for
5199 * the count. */
5200 newindex += siblingcount + 1;
5201
5202 /* Recursively dump the children of each sibling. */
5203 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005204 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
5205 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005206 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005207
5208 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005209}
5210
5211
5212/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00005213 * ":mkspell [-ascii] outfile infile ..."
5214 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005215 */
5216 void
5217ex_mkspell(eap)
5218 exarg_T *eap;
5219{
5220 int fcount;
5221 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005222 char_u *arg = eap->arg;
5223 int ascii = FALSE;
5224
5225 if (STRNCMP(arg, "-ascii", 6) == 0)
5226 {
5227 ascii = TRUE;
5228 arg = skipwhite(arg + 6);
5229 }
5230
5231 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
5232 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
5233 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005234 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005235 FreeWild(fcount, fnames);
5236 }
5237}
5238
5239/*
5240 * Create a Vim spell file from one or more word lists.
5241 * "fnames[0]" is the output file name.
5242 * "fnames[fcount - 1]" is the last input file name.
5243 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
5244 * and ".spl" is appended to make the output file name.
5245 */
5246 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005247mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005248 int fcount;
5249 char_u **fnames;
5250 int ascii; /* -ascii argument given */
5251 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005252 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005253{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005254 char_u fname[MAXPATHL];
5255 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00005256 char_u **innames;
5257 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005258 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005259 int i;
5260 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005261 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005262 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005263 spellinfo_T spin;
5264
5265 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005266 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005267 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005268 spin.si_followup = TRUE;
5269 spin.si_rem_accents = TRUE;
5270 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
5271 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
5272 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005273 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005274
Bram Moolenaarb765d632005-06-07 21:00:02 +00005275 /* default: fnames[0] is output file, following are input files */
5276 innames = &fnames[1];
5277 incount = fcount - 1;
5278
5279 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005280 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005281 len = STRLEN(fnames[0]);
5282 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
5283 {
5284 /* For ":mkspell path/en.latin1.add" output file is
5285 * "path/en.latin1.add.spl". */
5286 innames = &fnames[0];
5287 incount = 1;
5288 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
5289 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005290 else if (fcount == 1)
5291 {
5292 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
5293 innames = &fnames[0];
5294 incount = 1;
5295 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
5296 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5297 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005298 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
5299 {
5300 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005301 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005302 }
5303 else
5304 /* Name should be language, make the file name from it. */
5305 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
5306 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5307
5308 /* Check for .ascii.spl. */
5309 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
5310 spin.si_ascii = TRUE;
5311
5312 /* Check for .add.spl. */
5313 if (strstr((char *)gettail(wfname), ".add.") != NULL)
5314 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005315 }
5316
Bram Moolenaarb765d632005-06-07 21:00:02 +00005317 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005318 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005319 else if (vim_strchr(gettail(wfname), '_') != NULL)
5320 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005321 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005322 EMSG(_("E754: Only up to 8 regions supported"));
5323 else
5324 {
5325 /* Check for overwriting before doing things that may take a lot of
5326 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005327 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005328 {
5329 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005330 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005331 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005332 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005333 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005334 EMSG2(_(e_isadir2), wfname);
5335 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005336 }
5337
5338 /*
5339 * Init the aff and dic pointers.
5340 * Get the region names if there are more than 2 arguments.
5341 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005342 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005343 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005344 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005345
Bram Moolenaar3982c542005-06-08 21:56:31 +00005346 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005347 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005348 len = STRLEN(innames[i]);
5349 if (STRLEN(gettail(innames[i])) < 5
5350 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005351 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005352 EMSG2(_("E755: Invalid region in %s"), innames[i]);
5353 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005354 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005355 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
5356 spin.si_region_name[i * 2 + 1] =
5357 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005358 }
5359 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005360 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005361
Bram Moolenaar51485f02005-06-04 21:55:20 +00005362 spin.si_foldroot = wordtree_alloc(&spin.si_blocks);
5363 spin.si_keeproot = wordtree_alloc(&spin.si_blocks);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005364 spin.si_prefroot = wordtree_alloc(&spin.si_blocks);
5365 if (spin.si_foldroot == NULL
5366 || spin.si_keeproot == NULL
5367 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005368 {
5369 error = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005370 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005371 }
5372
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005373 /* When not producing a .add.spl file clear the character table when
5374 * we encounter one in the .aff file. This means we dump the current
5375 * one in the .spl file if the .aff file doesn't define one. That's
5376 * better than guessing the contents, the table will match a
5377 * previously loaded spell file. */
5378 if (!spin.si_add)
5379 spin.si_clear_chartab = TRUE;
5380
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005381 /*
5382 * Read all the .aff and .dic files.
5383 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00005384 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005385 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005386 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005387 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005388 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005389 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005390
Bram Moolenaarb765d632005-06-07 21:00:02 +00005391 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005392 if (mch_stat((char *)fname, &st) >= 0)
5393 {
5394 /* Read the .aff file. Will init "spin->si_conv" based on the
5395 * "SET" line. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005396 afile[i] = spell_read_aff(fname, &spin);
5397 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005398 error = TRUE;
5399 else
5400 {
5401 /* Read the .dic file and store the words in the trees. */
5402 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00005403 innames[i]);
5404 if (spell_read_dic(fname, &spin, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005405 error = TRUE;
5406 }
5407 }
5408 else
5409 {
5410 /* No .aff file, try reading the file as a word list. Store
5411 * the words in the trees. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005412 if (spell_read_wordfile(innames[i], &spin) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005413 error = TRUE;
5414 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005415
Bram Moolenaarb765d632005-06-07 21:00:02 +00005416#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005417 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005418 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005419#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005420 }
5421
Bram Moolenaar51485f02005-06-04 21:55:20 +00005422 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005423 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005424 /*
5425 * Remove the dummy NUL from the start of the tree root.
5426 */
5427 spin.si_foldroot = spin.si_foldroot->wn_sibling;
5428 spin.si_keeproot = spin.si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005429 spin.si_prefroot = spin.si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005430
5431 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005432 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005433 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005434 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005436 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005437 verbose_enter();
5438 MSG(_("Compressing word tree..."));
5439 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005440 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005441 verbose_leave();
5442 }
5443 wordtree_compress(spin.si_foldroot, &spin);
5444 wordtree_compress(spin.si_keeproot, &spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005445 wordtree_compress(spin.si_prefroot, &spin);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005446 }
5447
Bram Moolenaar51485f02005-06-04 21:55:20 +00005448 if (!error)
5449 {
5450 /*
5451 * Write the info in the spell file.
5452 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005453 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005454 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005455 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005456 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005457 smsg((char_u *)_("Writing spell file %s ..."), wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005458 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005459 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005460 verbose_leave();
5461 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00005462
Bram Moolenaar3982c542005-06-08 21:56:31 +00005463 write_vim_spell(wfname, &spin);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005464
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005465 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005466 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005467 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005468 verbose_enter();
5469 MSG(_("Done!"));
5470 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00005471 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005472 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005473 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005474 verbose_leave();
5475 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005476
Bram Moolenaarb765d632005-06-07 21:00:02 +00005477 /* If the file is loaded need to reload it. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005478 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005479 }
5480
5481 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005482 ga_clear(&spin.si_rep);
5483 ga_clear(&spin.si_sal);
5484 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005485 ga_clear(&spin.si_prefcond);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005486 vim_free(spin.si_midword);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005487 vim_free(spin.si_sofofr);
5488 vim_free(spin.si_sofoto);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005489
5490 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005491 for (i = 0; i < incount; ++i)
5492 if (afile[i] != NULL)
5493 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005494
5495 /* Free all the bits and pieces at once. */
5496 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005497 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005498}
5499
Bram Moolenaarb765d632005-06-07 21:00:02 +00005500
5501/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005502 * ":[count]spellgood {word}"
5503 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00005504 */
5505 void
5506ex_spell(eap)
5507 exarg_T *eap;
5508{
Bram Moolenaar7887d882005-07-01 22:33:52 +00005509 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005510 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005511}
5512
5513/*
5514 * Add "word[len]" to 'spellfile' as a good or bad word.
5515 */
5516 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005517spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005518 char_u *word;
5519 int len;
5520 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005521 int index; /* "zG" and "zW": zero, otherwise index in
5522 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005523{
5524 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005525 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005526 int new_spf = FALSE;
5527 struct stat st;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005528 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005529 char_u fnamebuf[MAXPATHL];
5530 char_u line[MAXWLEN * 2];
5531 long fpos, fpos_next = 0;
5532 int i;
5533 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005534
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005535 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005536 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005537 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00005538 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005539 int_wordlist = vim_tempname('s');
5540 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00005541 return;
5542 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005543 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005544 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005545 else
5546 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005547 /* If 'spellfile' isn't set figure out a good default value. */
5548 if (*curbuf->b_p_spf == NUL)
5549 {
5550 init_spellfile();
5551 new_spf = TRUE;
5552 }
5553
5554 if (*curbuf->b_p_spf == NUL)
5555 {
5556 EMSG(_("E764: 'spellfile' is not set"));
5557 return;
5558 }
5559
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005560 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
5561 {
5562 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
5563 if (i == index)
5564 break;
5565 if (*spf == NUL)
5566 {
5567 EMSGN(_("E765: 'spellfile' does not have %ld enties"), index);
5568 return;
5569 }
5570 }
5571
Bram Moolenaarb765d632005-06-07 21:00:02 +00005572 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005573 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005574 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
5575 buf = NULL;
5576 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00005577 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005578 EMSG(_(e_bufloaded));
5579 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005580 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00005581
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005582 fname = fnamebuf;
5583 }
5584
5585 if (bad)
5586 {
5587 /* When the word also appears as good word we need to remove that one,
5588 * since its flags sort before the one with WF_BANNED. */
5589 fd = mch_fopen((char *)fname, "r");
5590 if (fd != NULL)
5591 {
5592 while (!vim_fgets(line, MAXWLEN * 2, fd))
5593 {
5594 fpos = fpos_next;
5595 fpos_next = ftell(fd);
5596 if (STRNCMP(word, line, len) == 0
5597 && (line[len] == '/' || line[len] < ' '))
5598 {
5599 /* Found duplicate word. Remove it by writing a '#' at
5600 * the start of the line. Mixing reading and writing
5601 * doesn't work for all systems, close the file first. */
5602 fclose(fd);
5603 fd = mch_fopen((char *)fname, "r+");
5604 if (fd == NULL)
5605 break;
5606 if (fseek(fd, fpos, SEEK_SET) == 0)
5607 fputc('#', fd);
5608 fseek(fd, fpos_next, SEEK_SET);
5609 }
5610 }
5611 fclose(fd);
5612 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00005613 }
5614
5615 fd = mch_fopen((char *)fname, "a");
5616 if (fd == NULL && new_spf)
5617 {
5618 /* We just initialized the 'spellfile' option and can't open the file.
5619 * We may need to create the "spell" directory first. We already
5620 * checked the runtime directory is writable in init_spellfile(). */
5621 STRCPY(NameBuff, fname);
5622 *gettail_sep(NameBuff) = NUL;
5623 if (mch_stat((char *)NameBuff, &st) < 0)
5624 {
5625 /* The directory doesn't exist. Try creating it and opening the
5626 * file again. */
5627 vim_mkdir(NameBuff, 0755);
5628 fd = mch_fopen((char *)fname, "a");
5629 }
5630 }
5631
5632 if (fd == NULL)
5633 EMSG2(_(e_notopen), fname);
5634 else
5635 {
5636 if (bad)
5637 fprintf(fd, "%.*s/!\n", len, word);
5638 else
5639 fprintf(fd, "%.*s\n", len, word);
5640 fclose(fd);
5641
5642 /* Update the .add.spl file. */
5643 mkspell(1, &fname, FALSE, TRUE, TRUE);
5644
5645 /* If the .add file is edited somewhere, reload it. */
5646 if (buf != NULL)
5647 buf_reload(buf);
5648
5649 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005650 }
5651}
5652
5653/*
5654 * Initialize 'spellfile' for the current buffer.
5655 */
5656 static void
5657init_spellfile()
5658{
5659 char_u buf[MAXPATHL];
5660 int l;
5661 slang_T *sl;
5662 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005663 char_u *lend;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005664
5665 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
5666 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005667 /* Find the end of the language name. Exclude the region. */
5668 for (lend = curbuf->b_p_spl; *lend != NUL
5669 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
5670 ;
5671
5672 /* Loop over all entries in 'runtimepath'. Use the first one where we
5673 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005674 rtp = p_rtp;
5675 while (*rtp != NUL)
5676 {
5677 /* Copy the path from 'runtimepath' to buf[]. */
5678 copy_option_part(&rtp, buf, MAXPATHL, ",");
5679 if (filewritable(buf) == 2)
5680 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00005681 /* Use the first language name from 'spelllang' and the
5682 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005683 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
5684 l = STRLEN(buf);
5685 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00005686 "/spell/%.*s.%s.add",
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005687 (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00005688 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
5689 ? (char_u *)"ascii" : spell_enc());
5690 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
5691 break;
5692 }
5693 }
5694 }
5695}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005696
Bram Moolenaar51485f02005-06-04 21:55:20 +00005697
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005698/*
5699 * Init the chartab used for spelling for ASCII.
5700 * EBCDIC is not supported!
5701 */
5702 static void
5703clear_spell_chartab(sp)
5704 spelltab_T *sp;
5705{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005706 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005707
5708 /* Init everything to FALSE. */
5709 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
5710 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
5711 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005712 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005713 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005714 sp->st_upper[i] = i;
5715 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005716
5717 /* We include digits. A word shouldn't start with a digit, but handling
5718 * that is done separately. */
5719 for (i = '0'; i <= '9'; ++i)
5720 sp->st_isw[i] = TRUE;
5721 for (i = 'A'; i <= 'Z'; ++i)
5722 {
5723 sp->st_isw[i] = TRUE;
5724 sp->st_isu[i] = TRUE;
5725 sp->st_fold[i] = i + 0x20;
5726 }
5727 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005728 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005729 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005730 sp->st_upper[i] = i - 0x20;
5731 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005732}
5733
5734/*
5735 * Init the chartab used for spelling. Only depends on 'encoding'.
5736 * Called once while starting up and when 'encoding' changes.
5737 * The default is to use isalpha(), but the spell file should define the word
5738 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005739 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005740 */
5741 void
5742init_spell_chartab()
5743{
5744 int i;
5745
5746 did_set_spelltab = FALSE;
5747 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005748#ifdef FEAT_MBYTE
5749 if (enc_dbcs)
5750 {
5751 /* DBCS: assume double-wide characters are word characters. */
5752 for (i = 128; i <= 255; ++i)
5753 if (MB_BYTE2LEN(i) == 2)
5754 spelltab.st_isw[i] = TRUE;
5755 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005756 else if (enc_utf8)
5757 {
5758 for (i = 128; i < 256; ++i)
5759 {
5760 spelltab.st_isu[i] = utf_isupper(i);
5761 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
5762 spelltab.st_fold[i] = utf_fold(i);
5763 spelltab.st_upper[i] = utf_toupper(i);
5764 }
5765 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005766 else
5767#endif
5768 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005769 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005770 for (i = 128; i < 256; ++i)
5771 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005772 if (MB_ISUPPER(i))
5773 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005774 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005775 spelltab.st_isu[i] = TRUE;
5776 spelltab.st_fold[i] = MB_TOLOWER(i);
5777 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005778 else if (MB_ISLOWER(i))
5779 {
5780 spelltab.st_isw[i] = TRUE;
5781 spelltab.st_upper[i] = MB_TOUPPER(i);
5782 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005783 }
5784 }
5785}
5786
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005787static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
5788static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
5789
5790/*
5791 * Set the spell character tables from strings in the affix file.
5792 */
5793 static int
5794set_spell_chartab(fol, low, upp)
5795 char_u *fol;
5796 char_u *low;
5797 char_u *upp;
5798{
5799 /* We build the new tables here first, so that we can compare with the
5800 * previous one. */
5801 spelltab_T new_st;
5802 char_u *pf = fol, *pl = low, *pu = upp;
5803 int f, l, u;
5804
5805 clear_spell_chartab(&new_st);
5806
5807 while (*pf != NUL)
5808 {
5809 if (*pl == NUL || *pu == NUL)
5810 {
5811 EMSG(_(e_affform));
5812 return FAIL;
5813 }
5814#ifdef FEAT_MBYTE
5815 f = mb_ptr2char_adv(&pf);
5816 l = mb_ptr2char_adv(&pl);
5817 u = mb_ptr2char_adv(&pu);
5818#else
5819 f = *pf++;
5820 l = *pl++;
5821 u = *pu++;
5822#endif
5823 /* Every character that appears is a word character. */
5824 if (f < 256)
5825 new_st.st_isw[f] = TRUE;
5826 if (l < 256)
5827 new_st.st_isw[l] = TRUE;
5828 if (u < 256)
5829 new_st.st_isw[u] = TRUE;
5830
5831 /* if "LOW" and "FOL" are not the same the "LOW" char needs
5832 * case-folding */
5833 if (l < 256 && l != f)
5834 {
5835 if (f >= 256)
5836 {
5837 EMSG(_(e_affrange));
5838 return FAIL;
5839 }
5840 new_st.st_fold[l] = f;
5841 }
5842
5843 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005844 * case-folding, it's upper case and the "UPP" is the upper case of
5845 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005846 if (u < 256 && u != f)
5847 {
5848 if (f >= 256)
5849 {
5850 EMSG(_(e_affrange));
5851 return FAIL;
5852 }
5853 new_st.st_fold[u] = f;
5854 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005855 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005856 }
5857 }
5858
5859 if (*pl != NUL || *pu != NUL)
5860 {
5861 EMSG(_(e_affform));
5862 return FAIL;
5863 }
5864
5865 return set_spell_finish(&new_st);
5866}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005867
5868/*
5869 * Set the spell character tables from strings in the .spl file.
5870 */
5871 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005872set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005873 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005874 int cnt; /* length of "flags" */
5875 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005876{
5877 /* We build the new tables here first, so that we can compare with the
5878 * previous one. */
5879 spelltab_T new_st;
5880 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005881 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005882 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005883
5884 clear_spell_chartab(&new_st);
5885
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005886 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005887 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005888 if (i < cnt)
5889 {
5890 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
5891 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
5892 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005893
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005894 if (*p != NUL)
5895 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005896#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005897 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005898#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005899 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005900#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005901 new_st.st_fold[i + 128] = c;
5902 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
5903 new_st.st_upper[c] = i + 128;
5904 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005905 }
5906
5907 return set_spell_finish(&new_st);
5908}
5909
5910 static int
5911set_spell_finish(new_st)
5912 spelltab_T *new_st;
5913{
5914 int i;
5915
5916 if (did_set_spelltab)
5917 {
5918 /* check that it's the same table */
5919 for (i = 0; i < 256; ++i)
5920 {
5921 if (spelltab.st_isw[i] != new_st->st_isw[i]
5922 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005923 || spelltab.st_fold[i] != new_st->st_fold[i]
5924 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005925 {
5926 EMSG(_("E763: Word characters differ between spell files"));
5927 return FAIL;
5928 }
5929 }
5930 }
5931 else
5932 {
5933 /* copy the new spelltab into the one being used */
5934 spelltab = *new_st;
5935 did_set_spelltab = TRUE;
5936 }
5937
5938 return OK;
5939}
5940
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005941/*
Bram Moolenaarea408852005-06-25 22:49:46 +00005942 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005943 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00005944 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005945 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00005946 */
5947 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005948spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00005949 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005950 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00005951{
Bram Moolenaarea408852005-06-25 22:49:46 +00005952#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005953 char_u *s;
5954 int l;
5955 int c;
5956
5957 if (has_mbyte)
5958 {
5959 l = MB_BYTE2LEN(*p);
5960 s = p;
5961 if (l == 1)
5962 {
5963 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005964 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005965 {
5966 s = p + 1; /* skip a mid-word character */
5967 l = MB_BYTE2LEN(*s);
5968 }
5969 }
5970 else
5971 {
5972 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005973 if (c < 256 ? buf->b_spell_ismw[c]
5974 : (buf->b_spell_ismw_mb != NULL
5975 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005976 {
5977 s = p + l;
5978 l = MB_BYTE2LEN(*s);
5979 }
5980 }
5981
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005982 c = mb_ptr2char(s);
5983 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005984 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005985 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005986 }
Bram Moolenaarea408852005-06-25 22:49:46 +00005987#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005988
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005989 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
5990}
5991
5992/*
5993 * Return TRUE if "p" points to a word character.
5994 * Unlike spell_iswordp() this doesn't check for "midword" characters.
5995 */
5996 static int
5997spell_iswordp_nmw(p)
5998 char_u *p;
5999{
6000#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006001 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006002
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006003 if (has_mbyte)
6004 {
6005 c = mb_ptr2char(p);
6006 if (c > 255)
6007 return mb_get_class(p) >= 2;
6008 return spelltab.st_isw[c];
6009 }
6010#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006011 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00006012}
6013
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006014#ifdef FEAT_MBYTE
6015/*
6016 * Return TRUE if "p" points to a word character.
6017 * Wide version of spell_iswordp().
6018 */
6019 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006020spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006021 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006022 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006023{
6024 int *s;
6025
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006026 if (*p < 256 ? buf->b_spell_ismw[*p]
6027 : (buf->b_spell_ismw_mb != NULL
6028 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006029 s = p + 1;
6030 else
6031 s = p;
6032
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006033 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006034 {
6035 if (enc_utf8)
6036 return utf_class(*s) >= 2;
6037 if (enc_dbcs)
6038 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
6039 return 0;
6040 }
6041 return spelltab.st_isw[*s];
6042}
6043#endif
6044
Bram Moolenaarea408852005-06-25 22:49:46 +00006045/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006046 * Write the table with prefix conditions to the .spl file.
6047 */
6048 static void
6049write_spell_prefcond(fd, gap)
6050 FILE *fd;
6051 garray_T *gap;
6052{
6053 int i;
6054 char_u *p;
6055 int len;
6056
6057 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
6058
6059 for (i = 0; i < gap->ga_len; ++i)
6060 {
6061 /* <prefcond> : <condlen> <condstr> */
6062 p = ((char_u **)gap->ga_data)[i];
6063 if (p == NULL)
6064 fputc(0, fd);
6065 else
6066 {
6067 len = STRLEN(p);
6068 fputc(len, fd);
6069 fwrite(p, (size_t)len, (size_t)1, fd);
6070 }
6071 }
6072}
6073
6074/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006075 * Write the current tables into the .spl file.
6076 * This makes sure the same characters are recognized as word characters when
6077 * generating an when using a spell file.
6078 */
6079 static void
6080write_spell_chartab(fd)
6081 FILE *fd;
6082{
6083 char_u charbuf[256 * 4];
6084 int len = 0;
6085 int flags;
6086 int i;
6087
6088 fputc(128, fd); /* <charflagslen> */
6089 for (i = 128; i < 256; ++i)
6090 {
6091 flags = 0;
6092 if (spelltab.st_isw[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006093 flags |= CF_WORD;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006094 if (spelltab.st_isu[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006095 flags |= CF_UPPER;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006096 fputc(flags, fd); /* <charflags> */
6097
Bram Moolenaarb765d632005-06-07 21:00:02 +00006098#ifdef FEAT_MBYTE
6099 if (has_mbyte)
6100 len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
6101 else
6102#endif
6103 charbuf[len++] = spelltab.st_fold[i];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006104 }
6105
6106 put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
6107 fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
6108}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006109
6110/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006111 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
6112 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006113 * When using a multi-byte 'encoding' the length may change!
6114 * Returns FAIL when something wrong.
6115 */
6116 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006117spell_casefold(str, len, buf, buflen)
6118 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006119 int len;
6120 char_u *buf;
6121 int buflen;
6122{
6123 int i;
6124
6125 if (len >= buflen)
6126 {
6127 buf[0] = NUL;
6128 return FAIL; /* result will not fit */
6129 }
6130
6131#ifdef FEAT_MBYTE
6132 if (has_mbyte)
6133 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006134 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006135 char_u *p;
6136 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006137
6138 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006139 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006140 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006141 if (outi + MB_MAXBYTES > buflen)
6142 {
6143 buf[outi] = NUL;
6144 return FAIL;
6145 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006146 c = mb_ptr2char_adv(&p);
6147 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006148 }
6149 buf[outi] = NUL;
6150 }
6151 else
6152#endif
6153 {
6154 /* Be quick for non-multibyte encodings. */
6155 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006156 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006157 buf[i] = NUL;
6158 }
6159
6160 return OK;
6161}
6162
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006163#define SPS_BEST 1
6164#define SPS_FAST 2
6165#define SPS_DOUBLE 4
6166
6167static int sps_flags = SPS_BEST;
6168
6169/*
6170 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
6171 * Sets "sps_flags".
6172 */
6173 int
6174spell_check_sps()
6175{
6176 char_u *p;
6177 char_u buf[MAXPATHL];
6178 int f;
6179
6180 sps_flags = 0;
6181
6182 for (p = p_sps; *p != NUL; )
6183 {
6184 copy_option_part(&p, buf, MAXPATHL, ",");
6185
6186 f = 0;
6187 if (STRCMP(buf, "best") == 0)
6188 f = SPS_BEST;
6189 else if (STRCMP(buf, "fast") == 0)
6190 f = SPS_FAST;
6191 else if (STRCMP(buf, "double") == 0)
6192 f = SPS_DOUBLE;
6193 else if (STRNCMP(buf, "expr:", 5) != 0
6194 && STRNCMP(buf, "file:", 5) != 0)
6195 f = -1;
6196
6197 if (f == -1 || (sps_flags != 0 && f != 0))
6198 {
6199 sps_flags = SPS_BEST;
6200 return FAIL;
6201 }
6202 if (f != 0)
6203 sps_flags = f;
6204 }
6205
6206 if (sps_flags == 0)
6207 sps_flags = SPS_BEST;
6208
6209 return OK;
6210}
6211
6212/* Remember what "z?" replaced. */
6213static char_u *repl_from = NULL;
6214static char_u *repl_to = NULL;
6215
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006216/*
6217 * "z?": Find badly spelled word under or after the cursor.
6218 * Give suggestions for the properly spelled word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006219 */
6220 void
6221spell_suggest()
6222{
6223 char_u *line;
6224 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006225 char_u wcopy[MAXWLEN + 2];
6226 char_u *p;
6227 int i;
6228 int c;
6229 suginfo_T sug;
6230 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006231 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006232 int need_cap;
6233 regmatch_T regmatch;
6234 int endcol;
6235 char_u *line_copy = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006236
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006237 /* Find the start of the badly spelled word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006238 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL
6239 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006240 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006241 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
6242 return;
6243
6244 /* No bad word or it starts after the cursor: use the word under the
6245 * cursor. */
6246 curwin->w_cursor = prev_cursor;
6247 line = ml_get_curline();
6248 p = line + curwin->w_cursor.col;
6249 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006250 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006251 mb_ptr_back(line, p);
6252 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006253 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006254 mb_ptr_adv(p);
6255
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006256 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006257 {
6258 beep_flush();
6259 return;
6260 }
6261 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006262 }
6263
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006264 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006265 line = ml_get_curline();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006266
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006267 /* Figure out if the word should be capitalised. */
6268 need_cap = FALSE;
6269 if (curbuf->b_cap_prog != NULL)
6270 {
6271 endcol = 0;
Bram Moolenaar97409f12005-07-08 22:17:29 +00006272 if ((int)(skipwhite(line) - line) == (int)curwin->w_cursor.col)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006273 {
6274 /* At start of line, check if previous line is empty or sentence
6275 * ends there. */
6276 if (curwin->w_cursor.lnum == 1)
6277 need_cap = TRUE;
6278 else
6279 {
6280 line = ml_get(curwin->w_cursor.lnum - 1);
6281 if (*skipwhite(line) == NUL)
6282 need_cap = TRUE;
6283 else
6284 {
6285 /* Append a space in place of the line break. */
6286 line_copy = concat_str(line, (char_u *)" ");
6287 line = line_copy;
6288 endcol = STRLEN(line);
6289 }
6290 }
6291 }
6292 else
6293 endcol = curwin->w_cursor.col;
6294
6295 if (endcol > 0)
6296 {
6297 /* Check if sentence ends before the bad word. */
6298 regmatch.regprog = curbuf->b_cap_prog;
6299 regmatch.rm_ic = FALSE;
6300 p = line + endcol;
6301 for (;;)
6302 {
6303 mb_ptr_back(line, p);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006304 if (p == line || spell_iswordp_nmw(p))
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006305 break;
6306 if (vim_regexec(&regmatch, p, 0)
6307 && regmatch.endp[0] == line + endcol)
6308 {
6309 need_cap = TRUE;
6310 break;
6311 }
6312 }
6313 }
6314
6315 /* get the line again, we may have been using the previous one */
6316 line = ml_get_curline();
6317 vim_free(line_copy);
6318 }
6319
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006320 /* Get the list of suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006321 spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2,
6322 TRUE, need_cap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006323
6324 if (sug.su_ga.ga_len == 0)
6325 MSG(_("Sorry, no suggestions"));
6326 else
6327 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006328 vim_free(repl_from);
6329 repl_from = NULL;
6330 vim_free(repl_to);
6331 repl_to = NULL;
6332
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006333 /* List the suggestions. */
6334 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006335 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006336 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
6337 sug.su_badlen, sug.su_badptr);
6338 msg_puts(IObuff);
6339 msg_clr_eos();
6340 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00006341
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006342 msg_scroll = TRUE;
6343 for (i = 0; i < sug.su_ga.ga_len; ++i)
6344 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006345 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006346
6347 /* The suggested word may replace only part of the bad word, add
6348 * the not replaced part. */
6349 STRCPY(wcopy, stp->st_word);
6350 if (sug.su_badlen > stp->st_orglen)
6351 vim_strncpy(wcopy + STRLEN(wcopy),
6352 sug.su_badptr + stp->st_orglen,
6353 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006354 vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\""), i + 1, wcopy);
6355 msg_puts(IObuff);
6356
6357 /* The word may replace more than "su_badlen". */
6358 if (sug.su_badlen < stp->st_orglen)
6359 {
6360 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
6361 stp->st_orglen, sug.su_badptr);
6362 msg_puts(IObuff);
6363 }
6364
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006365 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006366 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006367 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006368 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006369 vim_snprintf((char *)IObuff, IOSIZE, _(" (%s%d - %d)"),
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006370 stp->st_salscore ? "s " : "",
6371 stp->st_score, stp->st_altscore);
6372 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00006373 vim_snprintf((char *)IObuff, IOSIZE, _(" (%d)"),
6374 stp->st_score);
6375 msg_advance(30);
6376 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006377 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006378 msg_putchar('\n');
6379 }
6380
6381 /* Ask for choice. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006382 i = prompt_for_number(&mouse_used);
6383 if (mouse_used)
6384 i -= lines_left;
6385
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006386 if (i > 0 && i <= sug.su_ga.ga_len && u_save_cursor() == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006387 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006388 /* Save the from and to text for :spellrepall. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006389 stp = &SUG(sug.su_ga, i - 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006390 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
6391 repl_to = vim_strsave(stp->st_word);
6392
6393 /* Replace the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006394 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
6395 if (p != NULL)
6396 {
6397 c = sug.su_badptr - line;
6398 mch_memmove(p, line, c);
6399 STRCPY(p + c, stp->st_word);
6400 STRCAT(p, sug.su_badptr + stp->st_orglen);
6401 ml_replace(curwin->w_cursor.lnum, p, FALSE);
6402 curwin->w_cursor.col = c;
6403 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006404
6405 /* For redo we use a change-word command. */
6406 ResetRedobuff();
6407 AppendToRedobuff((char_u *)"ciw");
6408 AppendToRedobuff(stp->st_word);
6409 AppendCharToRedobuff(ESC);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006410 }
6411 }
6412 else
6413 curwin->w_cursor = prev_cursor;
6414 }
6415
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006416 spell_find_cleanup(&sug);
6417}
6418
6419/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006420 * ":spellrepall"
6421 */
6422/*ARGSUSED*/
6423 void
6424ex_spellrepall(eap)
6425 exarg_T *eap;
6426{
6427 pos_T pos = curwin->w_cursor;
6428 char_u *frompat;
6429 int addlen;
6430 char_u *line;
6431 char_u *p;
6432 int didone = FALSE;
6433 int save_ws = p_ws;
6434
6435 if (repl_from == NULL || repl_to == NULL)
6436 {
6437 EMSG(_("E752: No previous spell replacement"));
6438 return;
6439 }
6440 addlen = STRLEN(repl_to) - STRLEN(repl_from);
6441
6442 frompat = alloc(STRLEN(repl_from) + 7);
6443 if (frompat == NULL)
6444 return;
6445 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
6446 p_ws = FALSE;
6447
6448 curwin->w_cursor.lnum = 0;
6449 while (!got_int)
6450 {
6451 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
6452 || u_save_cursor() == FAIL)
6453 break;
6454
6455 /* Only replace when the right word isn't there yet. This happens
6456 * when changing "etc" to "etc.". */
6457 line = ml_get_curline();
6458 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
6459 repl_to, STRLEN(repl_to)) != 0)
6460 {
6461 p = alloc(STRLEN(line) + addlen + 1);
6462 if (p == NULL)
6463 break;
6464 mch_memmove(p, line, curwin->w_cursor.col);
6465 STRCPY(p + curwin->w_cursor.col, repl_to);
6466 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
6467 ml_replace(curwin->w_cursor.lnum, p, FALSE);
6468 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6469 didone = TRUE;
6470 }
6471 curwin->w_cursor.col += STRLEN(repl_to);
6472 }
6473
6474 p_ws = save_ws;
6475 curwin->w_cursor = pos;
6476 vim_free(frompat);
6477
6478 if (!didone)
6479 EMSG2(_("E753: Not found: %s"), repl_from);
6480}
6481
6482/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006483 * Find spell suggestions for "word". Return them in the growarray "*gap" as
6484 * a list of allocated strings.
6485 */
6486 void
6487spell_suggest_list(gap, word, maxcount)
6488 garray_T *gap;
6489 char_u *word;
6490 int maxcount; /* maximum nr of suggestions */
6491{
6492 suginfo_T sug;
6493 int i;
6494 suggest_T *stp;
6495 char_u *wcopy;
6496
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006497 spell_find_suggest(word, &sug, maxcount, FALSE, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006498
6499 /* Make room in "gap". */
6500 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
6501 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
6502 return;
6503
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006504 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006505 {
6506 stp = &SUG(sug.su_ga, i);
6507
6508 /* The suggested word may replace only part of "word", add the not
6509 * replaced part. */
6510 wcopy = alloc(STRLEN(stp->st_word)
6511 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
6512 if (wcopy == NULL)
6513 break;
6514 STRCPY(wcopy, stp->st_word);
6515 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
6516 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
6517 }
6518
6519 spell_find_cleanup(&sug);
6520}
6521
6522/*
6523 * Find spell suggestions for the word at the start of "badptr".
6524 * Return the suggestions in "su->su_ga".
6525 * The maximum number of suggestions is "maxcount".
6526 * Note: does use info for the current window.
6527 * This is based on the mechanisms of Aspell, but completely reimplemented.
6528 */
6529 static void
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006530spell_find_suggest(badptr, su, maxcount, banbadword, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006531 char_u *badptr;
6532 suginfo_T *su;
6533 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00006534 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006535 int need_cap; /* word should start with capital */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006536{
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006537 int attr = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006538 char_u buf[MAXPATHL];
6539 char_u *p;
6540 int do_combine = FALSE;
6541 char_u *sps_copy;
6542#ifdef FEAT_EVAL
6543 static int expr_busy = FALSE;
6544#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006545 int c;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006546
6547 /*
6548 * Set the info in "*su".
6549 */
6550 vim_memset(su, 0, sizeof(suginfo_T));
6551 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
6552 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006553 if (*badptr == NUL)
6554 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006555 hash_init(&su->su_banned);
6556
6557 su->su_badptr = badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006558 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006559 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006560 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006561
6562 if (su->su_badlen >= MAXWLEN)
6563 su->su_badlen = MAXWLEN - 1; /* just in case */
6564 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
6565 (void)spell_casefold(su->su_badptr, su->su_badlen,
6566 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006567 /* get caps flags for bad word */
6568 su->su_badflags = captype(su->su_badptr, su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006569 if (need_cap)
6570 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006571
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006572 /* If the word is not capitalised and spell_check() doesn't consider the
6573 * word to be bad then it might need to be capitalised. Add a suggestion
6574 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00006575 c = PTR2CHAR(su->su_badptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006576 if (!SPELL_ISUPPER(c) && attr == 0)
6577 {
6578 make_case_word(su->su_badword, buf, WF_ONECAP);
6579 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
6580 0, TRUE);
6581 }
6582
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006583 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00006584 if (banbadword)
6585 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006586
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006587 /* Make a copy of 'spellsuggest', because the expression may change it. */
6588 sps_copy = vim_strsave(p_sps);
6589 if (sps_copy == NULL)
6590 return;
6591
6592 /* Loop over the items in 'spellsuggest'. */
6593 for (p = sps_copy; *p != NUL; )
6594 {
6595 copy_option_part(&p, buf, MAXPATHL, ",");
6596
6597 if (STRNCMP(buf, "expr:", 5) == 0)
6598 {
6599#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006600 /* Evaluate an expression. Skip this when called recursively,
6601 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006602 if (!expr_busy)
6603 {
6604 expr_busy = TRUE;
6605 spell_suggest_expr(su, buf + 5);
6606 expr_busy = FALSE;
6607 }
6608#endif
6609 }
6610 else if (STRNCMP(buf, "file:", 5) == 0)
6611 /* Use list of suggestions in a file. */
6612 spell_suggest_file(su, buf + 5);
6613 else
6614 {
6615 /* Use internal method. */
6616 spell_suggest_intern(su);
6617 if (sps_flags & SPS_DOUBLE)
6618 do_combine = TRUE;
6619 }
6620 }
6621
6622 vim_free(sps_copy);
6623
6624 if (do_combine)
6625 /* Combine the two list of suggestions. This must be done last,
6626 * because sorting changes the order again. */
6627 score_combine(su);
6628}
6629
6630#ifdef FEAT_EVAL
6631/*
6632 * Find suggestions by evaluating expression "expr".
6633 */
6634 static void
6635spell_suggest_expr(su, expr)
6636 suginfo_T *su;
6637 char_u *expr;
6638{
6639 list_T *list;
6640 listitem_T *li;
6641 int score;
6642 char_u *p;
6643
6644 /* The work is split up in a few parts to avoid having to export
6645 * suginfo_T.
6646 * First evaluate the expression and get the resulting list. */
6647 list = eval_spell_expr(su->su_badword, expr);
6648 if (list != NULL)
6649 {
6650 /* Loop over the items in the list. */
6651 for (li = list->lv_first; li != NULL; li = li->li_next)
6652 if (li->li_tv.v_type == VAR_LIST)
6653 {
6654 /* Get the word and the score from the items. */
6655 score = get_spellword(li->li_tv.vval.v_list, &p);
6656 if (score >= 0)
6657 add_suggestion(su, &su->su_ga, p,
6658 su->su_badlen, score, 0, TRUE);
6659 }
6660 list_unref(list);
6661 }
6662
6663 /* Sort the suggestions and truncate at "maxcount". */
6664 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
6665}
6666#endif
6667
6668/*
6669 * Find suggestions a file "fname".
6670 */
6671 static void
6672spell_suggest_file(su, fname)
6673 suginfo_T *su;
6674 char_u *fname;
6675{
6676 FILE *fd;
6677 char_u line[MAXWLEN * 2];
6678 char_u *p;
6679 int len;
6680 char_u cword[MAXWLEN];
6681
6682 /* Open the file. */
6683 fd = mch_fopen((char *)fname, "r");
6684 if (fd == NULL)
6685 {
6686 EMSG2(_(e_notopen), fname);
6687 return;
6688 }
6689
6690 /* Read it line by line. */
6691 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
6692 {
6693 line_breakcheck();
6694
6695 p = vim_strchr(line, '/');
6696 if (p == NULL)
6697 continue; /* No Tab found, just skip the line. */
6698 *p++ = NUL;
6699 if (STRICMP(su->su_badword, line) == 0)
6700 {
6701 /* Match! Isolate the good word, until CR or NL. */
6702 for (len = 0; p[len] >= ' '; ++len)
6703 ;
6704 p[len] = NUL;
6705
6706 /* If the suggestion doesn't have specific case duplicate the case
6707 * of the bad word. */
6708 if (captype(p, NULL) == 0)
6709 {
6710 make_case_word(p, cword, su->su_badflags);
6711 p = cword;
6712 }
6713
6714 add_suggestion(su, &su->su_ga, p, su->su_badlen,
6715 SCORE_FILE, 0, TRUE);
6716 }
6717 }
6718
6719 fclose(fd);
6720
6721 /* Sort the suggestions and truncate at "maxcount". */
6722 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
6723}
6724
6725/*
6726 * Find suggestions for the internal method indicated by "sps_flags".
6727 */
6728 static void
6729spell_suggest_intern(su)
6730 suginfo_T *su;
6731{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006732 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006733 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006734 *
6735 * Set a maximum score to limit the combination of operations that is
6736 * tried.
6737 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006738 suggest_try_special(su);
6739
6740 /*
6741 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
6742 * from the .aff file and inserting a space (split the word).
6743 */
6744 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006745
6746 /* For the resulting top-scorers compute the sound-a-like score. */
6747 if (sps_flags & SPS_DOUBLE)
6748 score_comp_sal(su);
6749
6750 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006751 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006752 *
6753 * Only do this when we don't have a lot of suggestions yet, because it's
6754 * very slow and often doesn't find new suggestions.
6755 */
6756 if ((sps_flags & SPS_DOUBLE)
6757 || (!(sps_flags & SPS_FAST)
6758 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
6759 {
6760 /* Allow a higher score now. */
6761 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006762 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006763 }
6764
6765 /* When CTRL-C was hit while searching do show the results. */
6766 ui_breakcheck();
6767 if (got_int)
6768 {
6769 (void)vgetc();
6770 got_int = FALSE;
6771 }
6772
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006773 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006774 {
6775 if (sps_flags & SPS_BEST)
6776 /* Adjust the word score for how it sounds like. */
6777 rescore_suggestions(su);
6778
6779 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006780 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006781 }
6782}
6783
6784/*
6785 * Free the info put in "*su" by spell_find_suggest().
6786 */
6787 static void
6788spell_find_cleanup(su)
6789 suginfo_T *su;
6790{
6791 int i;
6792
6793 /* Free the suggestions. */
6794 for (i = 0; i < su->su_ga.ga_len; ++i)
6795 vim_free(SUG(su->su_ga, i).st_word);
6796 ga_clear(&su->su_ga);
6797 for (i = 0; i < su->su_sga.ga_len; ++i)
6798 vim_free(SUG(su->su_sga, i).st_word);
6799 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006800
6801 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006802 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006803}
6804
6805/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006806 * Make a copy of "word", with the first letter upper or lower cased, to
6807 * "wcopy[MAXWLEN]". "word" must not be empty.
6808 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006809 */
6810 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006811onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006812 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006813 char_u *wcopy;
6814 int upper; /* TRUE: first letter made upper case */
6815{
6816 char_u *p;
6817 int c;
6818 int l;
6819
6820 p = word;
6821#ifdef FEAT_MBYTE
6822 if (has_mbyte)
6823 c = mb_ptr2char_adv(&p);
6824 else
6825#endif
6826 c = *p++;
6827 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006828 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006829 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006830 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006831#ifdef FEAT_MBYTE
6832 if (has_mbyte)
6833 l = mb_char2bytes(c, wcopy);
6834 else
6835#endif
6836 {
6837 l = 1;
6838 wcopy[0] = c;
6839 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006840 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006841}
6842
6843/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006844 * Make a copy of "word" with all the letters upper cased into
6845 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006846 */
6847 static void
6848allcap_copy(word, wcopy)
6849 char_u *word;
6850 char_u *wcopy;
6851{
6852 char_u *s;
6853 char_u *d;
6854 int c;
6855
6856 d = wcopy;
6857 for (s = word; *s != NUL; )
6858 {
6859#ifdef FEAT_MBYTE
6860 if (has_mbyte)
6861 c = mb_ptr2char_adv(&s);
6862 else
6863#endif
6864 c = *s++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006865 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006866
6867#ifdef FEAT_MBYTE
6868 if (has_mbyte)
6869 {
6870 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
6871 break;
6872 d += mb_char2bytes(c, d);
6873 }
6874 else
6875#endif
6876 {
6877 if (d - wcopy >= MAXWLEN - 1)
6878 break;
6879 *d++ = c;
6880 }
6881 }
6882 *d = NUL;
6883}
6884
6885/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006886 * Try finding suggestions by recognizing specific situations.
6887 */
6888 static void
6889suggest_try_special(su)
6890 suginfo_T *su;
6891{
6892 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006893 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006894 int c;
6895 char_u word[MAXWLEN];
6896
6897 /*
6898 * Recognize a word that is repeated: "the the".
6899 */
6900 p = skiptowhite(su->su_fbadword);
6901 len = p - su->su_fbadword;
6902 p = skipwhite(p);
6903 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
6904 {
6905 /* Include badflags: if the badword is onecap or allcap
6906 * use that for the goodword too: "The the" -> "The". */
6907 c = su->su_fbadword[len];
6908 su->su_fbadword[len] = NUL;
6909 make_case_word(su->su_fbadword, word, su->su_badflags);
6910 su->su_fbadword[len] = c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006911 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006912 }
6913}
6914
6915/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006916 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00006917 *
6918 * This uses a state machine. At each node in the tree we try various
6919 * operations. When trying if an operation work "depth" is increased and the
6920 * stack[] is used to store info. This allows combinations, thus insert one
6921 * character, replace one and delete another. The number of changes is
6922 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006923 */
6924 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00006925suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006926 suginfo_T *su;
6927{
6928 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
6929 char_u tword[MAXWLEN]; /* good word collected so far */
6930 trystate_T stack[MAXWLEN];
6931 char_u preword[MAXWLEN * 3]; /* word found with proper case (appended
6932 * to for word split) */
6933 char_u prewordlen = 0; /* length of word in "preword" */
6934 int splitoff = 0; /* index in tword after last split */
6935 trystate_T *sp;
6936 int newscore;
6937 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006938 char_u *byts, *fbyts, *pbyts;
6939 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006940 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00006941 int c, c2, c3;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006942 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006943 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006944 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006945 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006946 int len;
6947 char_u *p;
6948 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00006949 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006950 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006951
6952 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00006953 * to find matches (esp. REP items). Append some more text, changing
6954 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006955 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006956 n = STRLEN(fword);
6957 p = su->su_badptr + su->su_badlen;
6958 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006959
6960 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
6961 lp->lp_slang != NULL; ++lp)
6962 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006963 /*
6964 * Go through the whole case-fold tree, try changes at each node.
6965 * "tword[]" contains the word collected from nodes in the tree.
6966 * "fword[]" the word we are trying to match with (initially the bad
6967 * word).
6968 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006969 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006970 sp = &stack[0];
6971 sp->ts_state = STATE_START;
6972 sp->ts_score = 0;
6973 sp->ts_curi = 1;
6974 sp->ts_fidx = 0;
6975 sp->ts_fidxtry = 0;
6976 sp->ts_twordlen = 0;
6977 sp->ts_arridx = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00006978#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006979 sp->ts_tcharlen = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00006980#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006981
Bram Moolenaarea424162005-06-16 21:51:00 +00006982 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006983 * When there are postponed prefixes we need to use these first. At
6984 * the end of the prefix we continue in the case-fold tree.
6985 */
6986 fbyts = lp->lp_slang->sl_fbyts;
6987 fidxs = lp->lp_slang->sl_fidxs;
6988 pbyts = lp->lp_slang->sl_pbyts;
6989 pidxs = lp->lp_slang->sl_pidxs;
6990 if (pbyts != NULL)
6991 {
6992 byts = pbyts;
6993 idxs = pidxs;
6994 sp->ts_prefixdepth = PREFIXTREE;
6995 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
6996 }
6997 else
6998 {
6999 byts = fbyts;
7000 idxs = fidxs;
7001 sp->ts_prefixdepth = NOPREFIX;
7002 }
7003
7004 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00007005 * Loop to find all suggestions. At each round we either:
7006 * - For the current state try one operation, advance "ts_curi",
7007 * increase "depth".
7008 * - When a state is done go to the next, set "ts_state".
7009 * - When all states are tried decrease "depth".
7010 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007011 while (depth >= 0 && !got_int)
7012 {
7013 sp = &stack[depth];
7014 switch (sp->ts_state)
7015 {
7016 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007017 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007018 /*
7019 * Start of node: Deal with NUL bytes, which means
7020 * tword[] may end here.
7021 */
7022 arridx = sp->ts_arridx; /* current node in the tree */
7023 len = byts[arridx]; /* bytes in this node */
7024 arridx += sp->ts_curi; /* index of current byte */
7025
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007026 if (sp->ts_prefixdepth == PREFIXTREE)
7027 {
7028 /* Skip over the NUL bytes, we use them later. */
7029 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
7030 ;
7031 sp->ts_curi += n;
7032
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007033 /* Always past NUL bytes now. */
7034 n = (int)sp->ts_state;
7035 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00007036 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007037
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007038 /* At end of a prefix or at start of prefixtree: check for
7039 * following word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007040 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007041 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00007042 /* Set su->su_badflags to the caps type at this
7043 * position. Use the caps type until here for the
7044 * prefix itself. */
7045#ifdef FEAT_MBYTE
7046 if (has_mbyte)
7047 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
7048 else
7049#endif
7050 n = sp->ts_fidx;
7051 flags = captype(su->su_badptr, su->su_badptr + n);
7052 su->su_badflags = captype(su->su_badptr + n,
7053 su->su_badptr + su->su_badlen);
7054
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007055 ++depth;
7056 stack[depth] = stack[depth - 1];
7057 sp = &stack[depth];
7058 sp->ts_prefixdepth = depth - 1;
7059 byts = fbyts;
7060 idxs = fidxs;
7061 sp->ts_state = STATE_START;
7062 sp->ts_curi = 1; /* start just after length byte */
7063 sp->ts_arridx = 0;
7064
Bram Moolenaar53805d12005-08-01 07:08:33 +00007065 /* Move the prefix to preword[] with the right case
7066 * and make find_keepcap_word() works. */
7067 splitoff = sp->ts_twordlen;
7068 tword[splitoff] = NUL;
7069 make_case_word(tword, preword, flags);
7070 prewordlen = STRLEN(preword);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007071 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007072 break;
7073 }
7074
Bram Moolenaar0c405862005-06-22 22:26:26 +00007075 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007076 {
7077 /* Past bytes in node and/or past NUL bytes. */
7078 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00007079 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007080 break;
7081 }
7082
7083 /*
7084 * End of word in tree.
7085 */
7086 ++sp->ts_curi; /* eat one NUL byte */
7087
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007088 flags = (int)idxs[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007089
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007090 if (sp->ts_prefixdepth < MAXWLEN)
7091 {
7092 /* There was a prefix before the word. Check that the
7093 * prefix can be used with this word. */
7094 /* Count the length of the NULs in the prefix. If there
7095 * are none this must be the first try without a prefix.
7096 */
7097 n = stack[sp->ts_prefixdepth].ts_arridx;
7098 len = pbyts[n++];
7099 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
7100 ;
7101 if (c > 0)
7102 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007103 /* The prefix ID is stored three bytes above the
7104 * flags. */
7105 c = valid_word_prefix(c, n, flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00007106 tword + splitoff, lp->lp_slang, FALSE);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007107 if (c == 0)
7108 break;
7109
7110 /* Use the WF_RARE flag for a rare prefix. */
7111 if (c & WF_RAREPFX)
7112 flags |= WF_RARE;
7113 }
7114 }
7115
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007116 /*
7117 * Form the word with proper case in preword.
7118 * If there is a word from a previous split, append.
7119 */
7120 tword[sp->ts_twordlen] = NUL;
7121 if (flags & WF_KEEPCAP)
7122 /* Must find the word in the keep-case tree. */
7123 find_keepcap_word(lp->lp_slang, tword + splitoff,
7124 preword + prewordlen);
7125 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00007126 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007127 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00007128 * use that for the goodword too. But if the badword is
7129 * allcap and it's only one char long use onecap. */
7130 c = su->su_badflags;
7131 if ((c & WF_ALLCAP)
7132#ifdef FEAT_MBYTE
7133 && su->su_badlen == mb_ptr2len_check(su->su_badptr)
7134#else
7135 && su->su_badlen == 1
7136#endif
7137 )
7138 c = WF_ONECAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007139 make_case_word(tword + splitoff,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007140 preword + prewordlen, flags | c);
7141 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007142
7143 /* Don't use a banned word. It may appear again as a good
7144 * word, thus remember it. */
7145 if (flags & WF_BANNED)
7146 {
7147 add_banned(su, preword + prewordlen);
7148 break;
7149 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007150 if (was_banned(su, preword + prewordlen)
7151 || was_banned(su, preword))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007152 break;
7153
7154 newscore = 0;
7155 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007156 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007157 newscore += SCORE_REGION;
7158 if (flags & WF_RARE)
7159 newscore += SCORE_RARE;
7160
Bram Moolenaar0c405862005-06-22 22:26:26 +00007161 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007162 captype(preword + prewordlen, NULL)))
7163 newscore += SCORE_ICASE;
7164
Bram Moolenaar0c405862005-06-22 22:26:26 +00007165 if ((fword[sp->ts_fidx] == NUL
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007166 || !spell_iswordp(fword + sp->ts_fidx, curbuf))
Bram Moolenaar0c405862005-06-22 22:26:26 +00007167 && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007168 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007169 /* The badword also ends: add suggestions. Give a penalty
7170 * when changing non-word char to word char, e.g., "thes,"
7171 * -> "these". */
7172 p = fword + sp->ts_fidx;
7173#ifdef FEAT_MBYTE
7174 if (has_mbyte)
7175 mb_ptr_back(fword, p);
7176 else
7177#endif
7178 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007179 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007180 {
7181 p = preword + STRLEN(preword);
7182#ifdef FEAT_MBYTE
7183 if (has_mbyte)
7184 mb_ptr_back(preword, p);
7185 else
7186#endif
7187 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007188 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007189 newscore += SCORE_NONWORD;
7190 }
7191
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007192 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007193 sp->ts_fidx - repextra,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007194 sp->ts_score + newscore, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007195 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007196 else if (sp->ts_fidx >= sp->ts_fidxtry
7197#ifdef FEAT_MBYTE
7198 /* Don't split halfway a character. */
7199 && (!has_mbyte || sp->ts_tcharlen == 0)
7200#endif
7201 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007202 {
7203 /* The word in the tree ends but the badword
7204 * continues: try inserting a space and check that a valid
7205 * words starts at fword[sp->ts_fidx]. */
7206 if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT))
7207 {
7208 /* Save things to be restored at STATE_SPLITUNDO. */
7209 sp->ts_save_prewordlen = prewordlen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007210 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007211 sp->ts_save_splitoff = splitoff;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007212 sp->ts_state = STATE_SPLITUNDO;
7213
7214 ++depth;
7215 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007216
7217 /* Append a space to preword. */
7218 STRCAT(preword, " ");
7219 prewordlen = STRLEN(preword);
7220 splitoff = sp->ts_twordlen;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007221
7222 /* If the badword has a non-word character at this
7223 * position skip it. That means replacing the
7224 * non-word character with a space. */
7225 if (!spell_iswordp_nmw(fword + sp->ts_fidx))
7226 {
7227 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
7228#ifdef FEAT_MBYTE
7229 if (has_mbyte)
7230 sp->ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
7231 else
7232#endif
7233 ++sp->ts_fidx;
7234 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00007235
7236 /* set su->su_badflags to the caps type at this
7237 * position */
7238
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007239#ifdef FEAT_MBYTE
7240 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00007241 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007242 else
7243#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00007244 n = sp->ts_fidx;
7245 su->su_badflags = captype(su->su_badptr + n,
7246 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007247
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007248 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007249 sp->ts_arridx = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007250 }
7251 }
7252 break;
7253
7254 case STATE_SPLITUNDO:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007255 /* Undo the changes done for word split. */
7256 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007257 splitoff = sp->ts_save_splitoff;
7258 prewordlen = sp->ts_save_prewordlen;
7259
7260 /* Continue looking for NUL bytes. */
7261 sp->ts_state = STATE_START;
7262 break;
7263
7264 case STATE_ENDNUL:
7265 /* Past the NUL bytes in the node. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00007266 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007267 if (fword[sp->ts_fidx] == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007268 {
7269 /* The badword ends, can't use the bytes in this node. */
7270 sp->ts_state = STATE_DEL;
7271 break;
7272 }
7273 sp->ts_state = STATE_PLAIN;
7274 /*FALLTHROUGH*/
7275
7276 case STATE_PLAIN:
7277 /*
7278 * Go over all possible bytes at this node, add each to
7279 * tword[] and use child node. "ts_curi" is the index.
7280 */
7281 arridx = sp->ts_arridx;
7282 if (sp->ts_curi > byts[arridx])
7283 {
7284 /* Done all bytes at this node, do next state. When still
7285 * at already changed bytes skip the other tricks. */
7286 if (sp->ts_fidx >= sp->ts_fidxtry)
7287 sp->ts_state = STATE_DEL;
7288 else
7289 sp->ts_state = STATE_FINAL;
7290 }
7291 else
7292 {
7293 arridx += sp->ts_curi++;
7294 c = byts[arridx];
7295
7296 /* Normal byte, go one level deeper. If it's not equal to
7297 * the byte in the bad word adjust the score. But don't
7298 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00007299 if (c == fword[sp->ts_fidx]
7300#ifdef FEAT_MBYTE
7301 || (sp->ts_tcharlen > 0
7302 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007303#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00007304 )
7305 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007306 else
7307 newscore = SCORE_SUBST;
7308 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
7309 && try_deeper(su, stack, depth, newscore))
7310 {
7311 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007312 sp = &stack[depth];
7313 ++sp->ts_fidx;
7314 tword[sp->ts_twordlen++] = c;
7315 sp->ts_arridx = idxs[arridx];
7316#ifdef FEAT_MBYTE
7317 if (newscore == SCORE_SUBST)
7318 sp->ts_isdiff = DIFF_YES;
7319 if (has_mbyte)
7320 {
7321 /* Multi-byte characters are a bit complicated to
7322 * handle: They differ when any of the bytes
7323 * differ and then their length may also differ. */
7324 if (sp->ts_tcharlen == 0)
7325 {
7326 /* First byte. */
7327 sp->ts_tcharidx = 0;
7328 sp->ts_tcharlen = MB_BYTE2LEN(c);
7329 sp->ts_fcharstart = sp->ts_fidx - 1;
7330 sp->ts_isdiff = (newscore != 0)
7331 ? DIFF_YES : DIFF_NONE;
7332 }
7333 else if (sp->ts_isdiff == DIFF_INSERT)
7334 /* When inserting trail bytes don't advance in
7335 * the bad word. */
7336 --sp->ts_fidx;
7337 if (++sp->ts_tcharidx == sp->ts_tcharlen)
7338 {
7339 /* Last byte of character. */
7340 if (sp->ts_isdiff == DIFF_YES)
7341 {
7342 /* Correct ts_fidx for the byte length of
7343 * the character (we didn't check that
7344 * before). */
7345 sp->ts_fidx = sp->ts_fcharstart
7346 + MB_BYTE2LEN(
7347 fword[sp->ts_fcharstart]);
7348
7349 /* For a similar character adjust score
7350 * from SCORE_SUBST to SCORE_SIMILAR. */
7351 if (lp->lp_slang->sl_has_map
7352 && similar_chars(lp->lp_slang,
7353 mb_ptr2char(tword
7354 + sp->ts_twordlen
7355 - sp->ts_tcharlen),
7356 mb_ptr2char(fword
7357 + sp->ts_fcharstart)))
7358 sp->ts_score -=
7359 SCORE_SUBST - SCORE_SIMILAR;
7360 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007361 else if (sp->ts_isdiff == DIFF_INSERT
7362 && sp->ts_twordlen > sp->ts_tcharlen)
7363 {
7364 /* If the previous character was the same,
7365 * thus doubling a character, give a bonus
7366 * to the score. */
7367 p = tword + sp->ts_twordlen
7368 - sp->ts_tcharlen;
7369 c = mb_ptr2char(p);
7370 mb_ptr_back(tword, p);
7371 if (c == mb_ptr2char(p))
7372 sp->ts_score -= SCORE_INS
7373 - SCORE_INSDUP;
7374 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007375
7376 /* Starting a new char, reset the length. */
7377 sp->ts_tcharlen = 0;
7378 }
7379 }
7380 else
7381#endif
7382 {
7383 /* If we found a similar char adjust the score.
7384 * We do this after calling try_deeper() because
7385 * it's slow. */
7386 if (newscore != 0
7387 && lp->lp_slang->sl_has_map
7388 && similar_chars(lp->lp_slang,
7389 c, fword[sp->ts_fidx - 1]))
7390 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
7391 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007392 }
7393 }
7394 break;
7395
7396 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00007397#ifdef FEAT_MBYTE
7398 /* When past the first byte of a multi-byte char don't try
7399 * delete/insert/swap a character. */
7400 if (has_mbyte && sp->ts_tcharlen > 0)
7401 {
7402 sp->ts_state = STATE_FINAL;
7403 break;
7404 }
7405#endif
7406 /*
7407 * Try skipping one character in the bad word (delete it).
7408 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007409 sp->ts_state = STATE_INS;
7410 sp->ts_curi = 1;
7411 if (fword[sp->ts_fidx] != NUL
7412 && try_deeper(su, stack, depth, SCORE_DEL))
7413 {
7414 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00007415
7416 /* Advance over the character in fword[]. Give a bonus to
7417 * the score if the same character is following "nn" ->
7418 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00007419#ifdef FEAT_MBYTE
7420 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00007421 {
7422 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00007423 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaarea408852005-06-25 22:49:46 +00007424 if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
7425 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
7426 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007427 else
7428#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00007429 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007430 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00007431 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
7432 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
7433 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007434 break;
7435 }
7436 /*FALLTHROUGH*/
7437
7438 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00007439 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007440 * node. */
7441 n = sp->ts_arridx;
7442 if (sp->ts_curi > byts[n])
7443 {
7444 /* Done all bytes at this node, do next state. */
7445 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007446 }
7447 else
7448 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007449 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007450 n += sp->ts_curi++;
7451 c = byts[n];
7452 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
7453 {
7454 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007455 sp = &stack[depth];
7456 tword[sp->ts_twordlen++] = c;
7457 sp->ts_arridx = idxs[n];
7458#ifdef FEAT_MBYTE
7459 if (has_mbyte)
7460 {
7461 fl = MB_BYTE2LEN(c);
7462 if (fl > 1)
7463 {
7464 /* There are following bytes for the same
7465 * character. We must find all bytes before
7466 * trying delete/insert/swap/etc. */
7467 sp->ts_tcharlen = fl;
7468 sp->ts_tcharidx = 1;
7469 sp->ts_isdiff = DIFF_INSERT;
7470 }
7471 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007472 else
7473 fl = 1;
7474 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00007475#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00007476 {
7477 /* If the previous character was the same, thus
7478 * doubling a character, give a bonus to the
7479 * score. */
7480 if (sp->ts_twordlen >= 2
7481 && tword[sp->ts_twordlen - 2] == c)
7482 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
7483 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007484 }
7485 }
7486 break;
7487
7488 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00007489 /*
7490 * Swap two bytes in the bad word: "12" -> "21".
7491 * We change "fword" here, it's changed back afterwards.
7492 */
7493 p = fword + sp->ts_fidx;
7494 c = *p;
7495 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007496 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007497 /* End of word, can't swap or replace. */
7498 sp->ts_state = STATE_FINAL;
7499 break;
7500 }
7501#ifdef FEAT_MBYTE
7502 if (has_mbyte)
7503 {
7504 n = mb_ptr2len_check(p);
7505 c = mb_ptr2char(p);
7506 c2 = mb_ptr2char(p + n);
7507 }
7508 else
7509#endif
7510 c2 = p[1];
7511 if (c == c2)
7512 {
7513 /* Characters are identical, swap won't do anything. */
7514 sp->ts_state = STATE_SWAP3;
7515 break;
7516 }
7517 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
7518 {
7519 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007520 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007521#ifdef FEAT_MBYTE
7522 if (has_mbyte)
7523 {
7524 fl = mb_char2len(c2);
7525 mch_memmove(p, p + n, fl);
7526 mb_char2bytes(c, p + fl);
7527 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
7528 }
7529 else
7530#endif
7531 {
7532 p[0] = c2;
7533 p[1] = c;
7534 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
7535 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007536 }
7537 else
7538 /* If this swap doesn't work then SWAP3 won't either. */
7539 sp->ts_state = STATE_REP_INI;
7540 break;
7541
Bram Moolenaarea424162005-06-16 21:51:00 +00007542 case STATE_UNSWAP:
7543 /* Undo the STATE_SWAP swap: "21" -> "12". */
7544 p = fword + sp->ts_fidx;
7545#ifdef FEAT_MBYTE
7546 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007547 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007548 n = MB_BYTE2LEN(*p);
7549 c = mb_ptr2char(p + n);
7550 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
7551 mb_char2bytes(c, p);
7552 }
7553 else
7554#endif
7555 {
7556 c = *p;
7557 *p = p[1];
7558 p[1] = c;
7559 }
7560 /*FALLTHROUGH*/
7561
7562 case STATE_SWAP3:
7563 /* Swap two bytes, skipping one: "123" -> "321". We change
7564 * "fword" here, it's changed back afterwards. */
7565 p = fword + sp->ts_fidx;
7566#ifdef FEAT_MBYTE
7567 if (has_mbyte)
7568 {
7569 n = mb_ptr2len_check(p);
7570 c = mb_ptr2char(p);
7571 fl = mb_ptr2len_check(p + n);
7572 c2 = mb_ptr2char(p + n);
7573 c3 = mb_ptr2char(p + n + fl);
7574 }
7575 else
7576#endif
7577 {
7578 c = *p;
7579 c2 = p[1];
7580 c3 = p[2];
7581 }
7582
7583 /* When characters are identical: "121" then SWAP3 result is
7584 * identical, ROT3L result is same as SWAP: "211", ROT3L
7585 * result is same as SWAP on next char: "112". Thus skip all
7586 * swapping. Also skip when c3 is NUL. */
7587 if (c == c3 || c3 == NUL)
7588 {
7589 sp->ts_state = STATE_REP_INI;
7590 break;
7591 }
7592 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7593 {
7594 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007595 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007596#ifdef FEAT_MBYTE
7597 if (has_mbyte)
7598 {
7599 tl = mb_char2len(c3);
7600 mch_memmove(p, p + n + fl, tl);
7601 mb_char2bytes(c2, p + tl);
7602 mb_char2bytes(c, p + fl + tl);
7603 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
7604 }
7605 else
7606#endif
7607 {
7608 p[0] = p[2];
7609 p[2] = c;
7610 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7611 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007612 }
7613 else
7614 sp->ts_state = STATE_REP_INI;
7615 break;
7616
Bram Moolenaarea424162005-06-16 21:51:00 +00007617 case STATE_UNSWAP3:
7618 /* Undo STATE_SWAP3: "321" -> "123" */
7619 p = fword + sp->ts_fidx;
7620#ifdef FEAT_MBYTE
7621 if (has_mbyte)
7622 {
7623 n = MB_BYTE2LEN(*p);
7624 c2 = mb_ptr2char(p + n);
7625 fl = MB_BYTE2LEN(p[n]);
7626 c = mb_ptr2char(p + n + fl);
7627 tl = MB_BYTE2LEN(p[n + fl]);
7628 mch_memmove(p + fl + tl, p, n);
7629 mb_char2bytes(c, p);
7630 mb_char2bytes(c2, p + tl);
7631 }
7632 else
7633#endif
7634 {
7635 c = *p;
7636 *p = p[2];
7637 p[2] = c;
7638 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007639
Bram Moolenaarea424162005-06-16 21:51:00 +00007640 /* Rotate three characters left: "123" -> "231". We change
7641 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007642 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7643 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007644 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007645 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007646 p = fword + sp->ts_fidx;
7647#ifdef FEAT_MBYTE
7648 if (has_mbyte)
7649 {
7650 n = mb_ptr2len_check(p);
7651 c = mb_ptr2char(p);
7652 fl = mb_ptr2len_check(p + n);
7653 fl += mb_ptr2len_check(p + n + fl);
7654 mch_memmove(p, p + n, fl);
7655 mb_char2bytes(c, p + fl);
7656 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
7657 }
7658 else
7659#endif
7660 {
7661 c = *p;
7662 *p = p[1];
7663 p[1] = p[2];
7664 p[2] = c;
7665 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7666 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007667 }
7668 else
7669 sp->ts_state = STATE_REP_INI;
7670 break;
7671
Bram Moolenaarea424162005-06-16 21:51:00 +00007672 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007673 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00007674 p = fword + sp->ts_fidx;
7675#ifdef FEAT_MBYTE
7676 if (has_mbyte)
7677 {
7678 n = MB_BYTE2LEN(*p);
7679 n += MB_BYTE2LEN(p[n]);
7680 c = mb_ptr2char(p + n);
7681 tl = MB_BYTE2LEN(p[n]);
7682 mch_memmove(p + tl, p, n);
7683 mb_char2bytes(c, p);
7684 }
7685 else
7686#endif
7687 {
7688 c = p[2];
7689 p[2] = p[1];
7690 p[1] = *p;
7691 *p = c;
7692 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007693
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007694 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00007695 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007696 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7697 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007698 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007699 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007700 p = fword + sp->ts_fidx;
7701#ifdef FEAT_MBYTE
7702 if (has_mbyte)
7703 {
7704 n = mb_ptr2len_check(p);
7705 n += mb_ptr2len_check(p + n);
7706 c = mb_ptr2char(p + n);
7707 tl = mb_ptr2len_check(p + n);
7708 mch_memmove(p + tl, p, n);
7709 mb_char2bytes(c, p);
7710 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
7711 }
7712 else
7713#endif
7714 {
7715 c = p[2];
7716 p[2] = p[1];
7717 p[1] = *p;
7718 *p = c;
7719 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7720 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007721 }
7722 else
7723 sp->ts_state = STATE_REP_INI;
7724 break;
7725
Bram Moolenaarea424162005-06-16 21:51:00 +00007726 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007727 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00007728 p = fword + sp->ts_fidx;
7729#ifdef FEAT_MBYTE
7730 if (has_mbyte)
7731 {
7732 c = mb_ptr2char(p);
7733 tl = MB_BYTE2LEN(*p);
7734 n = MB_BYTE2LEN(p[tl]);
7735 n += MB_BYTE2LEN(p[tl + n]);
7736 mch_memmove(p, p + tl, n);
7737 mb_char2bytes(c, p + n);
7738 }
7739 else
7740#endif
7741 {
7742 c = *p;
7743 *p = p[1];
7744 p[1] = p[2];
7745 p[2] = c;
7746 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007747 /*FALLTHROUGH*/
7748
7749 case STATE_REP_INI:
7750 /* Check if matching with REP items from the .aff file would
7751 * work. Quickly skip if there are no REP items or the score
7752 * is going to be too high anyway. */
7753 gap = &lp->lp_slang->sl_rep;
7754 if (gap->ga_len == 0
7755 || sp->ts_score + SCORE_REP >= su->su_maxscore)
7756 {
7757 sp->ts_state = STATE_FINAL;
7758 break;
7759 }
7760
7761 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00007762 * may match. If the index is -1 there is none. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007763 sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]];
7764 if (sp->ts_curi < 0)
7765 {
7766 sp->ts_state = STATE_FINAL;
7767 break;
7768 }
7769
7770 sp->ts_state = STATE_REP;
7771 /*FALLTHROUGH*/
7772
7773 case STATE_REP:
7774 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00007775 * match replace the characters and check if the resulting
7776 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007777 p = fword + sp->ts_fidx;
7778
7779 gap = &lp->lp_slang->sl_rep;
7780 while (sp->ts_curi < gap->ga_len)
7781 {
7782 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
7783 if (*ftp->ft_from != *p)
7784 {
7785 /* past possible matching entries */
7786 sp->ts_curi = gap->ga_len;
7787 break;
7788 }
7789 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
7790 && try_deeper(su, stack, depth, SCORE_REP))
7791 {
7792 /* Need to undo this afterwards. */
7793 sp->ts_state = STATE_REP_UNDO;
7794
7795 /* Change the "from" to the "to" string. */
7796 ++depth;
7797 fl = STRLEN(ftp->ft_from);
7798 tl = STRLEN(ftp->ft_to);
7799 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007800 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007801 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007802 repextra += tl - fl;
7803 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007804 mch_memmove(p, ftp->ft_to, tl);
7805 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +00007806#ifdef FEAT_MBYTE
7807 stack[depth].ts_tcharlen = 0;
7808#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007809 break;
7810 }
7811 }
7812
7813 if (sp->ts_curi >= gap->ga_len)
7814 /* No (more) matches. */
7815 sp->ts_state = STATE_FINAL;
7816
7817 break;
7818
7819 case STATE_REP_UNDO:
7820 /* Undo a REP replacement and continue with the next one. */
7821 ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data
7822 + sp->ts_curi - 1;
7823 fl = STRLEN(ftp->ft_from);
7824 tl = STRLEN(ftp->ft_to);
7825 p = fword + sp->ts_fidx;
7826 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007827 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007828 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007829 repextra -= tl - fl;
7830 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007831 mch_memmove(p, ftp->ft_from, fl);
7832 sp->ts_state = STATE_REP;
7833 break;
7834
7835 default:
7836 /* Did all possible states at this level, go up one level. */
7837 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007838
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007839 if (depth >= 0 && stack[depth].ts_prefixdepth == PREFIXTREE)
7840 {
7841 /* Continue in or go back to the prefix tree. */
7842 byts = pbyts;
7843 idxs = pidxs;
7844 splitoff = 0;
7845 }
7846
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007847 /* Don't check for CTRL-C too often, it takes time. */
7848 line_breakcheck();
7849 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007850 }
7851 }
7852}
7853
7854/*
7855 * Try going one level deeper in the tree.
7856 */
7857 static int
7858try_deeper(su, stack, depth, score_add)
7859 suginfo_T *su;
7860 trystate_T *stack;
7861 int depth;
7862 int score_add;
7863{
7864 int newscore;
7865
7866 /* Refuse to go deeper if the scrore is getting too big. */
7867 newscore = stack[depth].ts_score + score_add;
7868 if (newscore >= su->su_maxscore)
7869 return FALSE;
7870
Bram Moolenaarea424162005-06-16 21:51:00 +00007871 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007872 stack[depth + 1].ts_state = STATE_START;
7873 stack[depth + 1].ts_score = newscore;
7874 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007875 return TRUE;
7876}
7877
Bram Moolenaar53805d12005-08-01 07:08:33 +00007878#ifdef FEAT_MBYTE
7879/*
7880 * Case-folding may change the number of bytes: Count nr of chars in
7881 * fword[flen] and return the byte length of that many chars in "word".
7882 */
7883 static int
7884nofold_len(fword, flen, word)
7885 char_u *fword;
7886 int flen;
7887 char_u *word;
7888{
7889 char_u *p;
7890 int i = 0;
7891
7892 for (p = fword; p < fword + flen; mb_ptr_adv(p))
7893 ++i;
7894 for (p = word; i > 0; mb_ptr_adv(p))
7895 --i;
7896 return (int)(p - word);
7897}
7898#endif
7899
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007900/*
7901 * "fword" is a good word with case folded. Find the matching keep-case
7902 * words and put it in "kword".
7903 * Theoretically there could be several keep-case words that result in the
7904 * same case-folded word, but we only find one...
7905 */
7906 static void
7907find_keepcap_word(slang, fword, kword)
7908 slang_T *slang;
7909 char_u *fword;
7910 char_u *kword;
7911{
7912 char_u uword[MAXWLEN]; /* "fword" in upper-case */
7913 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007914 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007915
7916 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007917 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007918 int round[MAXWLEN];
7919 int fwordidx[MAXWLEN];
7920 int uwordidx[MAXWLEN];
7921 int kwordlen[MAXWLEN];
7922
7923 int flen, ulen;
7924 int l;
7925 int len;
7926 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007927 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007928 char_u *p;
7929 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007930 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007931
7932 if (byts == NULL)
7933 {
7934 /* array is empty: "cannot happen" */
7935 *kword = NUL;
7936 return;
7937 }
7938
7939 /* Make an all-cap version of "fword". */
7940 allcap_copy(fword, uword);
7941
7942 /*
7943 * Each character needs to be tried both case-folded and upper-case.
7944 * All this gets very complicated if we keep in mind that changing case
7945 * may change the byte length of a multi-byte character...
7946 */
7947 depth = 0;
7948 arridx[0] = 0;
7949 round[0] = 0;
7950 fwordidx[0] = 0;
7951 uwordidx[0] = 0;
7952 kwordlen[0] = 0;
7953 while (depth >= 0)
7954 {
7955 if (fword[fwordidx[depth]] == NUL)
7956 {
7957 /* We are at the end of "fword". If the tree allows a word to end
7958 * here we have found a match. */
7959 if (byts[arridx[depth] + 1] == 0)
7960 {
7961 kword[kwordlen[depth]] = NUL;
7962 return;
7963 }
7964
7965 /* kword is getting too long, continue one level up */
7966 --depth;
7967 }
7968 else if (++round[depth] > 2)
7969 {
7970 /* tried both fold-case and upper-case character, continue one
7971 * level up */
7972 --depth;
7973 }
7974 else
7975 {
7976 /*
7977 * round[depth] == 1: Try using the folded-case character.
7978 * round[depth] == 2: Try using the upper-case character.
7979 */
7980#ifdef FEAT_MBYTE
7981 if (has_mbyte)
7982 {
7983 flen = mb_ptr2len_check(fword + fwordidx[depth]);
7984 ulen = mb_ptr2len_check(uword + uwordidx[depth]);
7985 }
7986 else
7987#endif
7988 ulen = flen = 1;
7989 if (round[depth] == 1)
7990 {
7991 p = fword + fwordidx[depth];
7992 l = flen;
7993 }
7994 else
7995 {
7996 p = uword + uwordidx[depth];
7997 l = ulen;
7998 }
7999
8000 for (tryidx = arridx[depth]; l > 0; --l)
8001 {
8002 /* Perform a binary search in the list of accepted bytes. */
8003 len = byts[tryidx++];
8004 c = *p++;
8005 lo = tryidx;
8006 hi = tryidx + len - 1;
8007 while (lo < hi)
8008 {
8009 m = (lo + hi) / 2;
8010 if (byts[m] > c)
8011 hi = m - 1;
8012 else if (byts[m] < c)
8013 lo = m + 1;
8014 else
8015 {
8016 lo = hi = m;
8017 break;
8018 }
8019 }
8020
8021 /* Stop if there is no matching byte. */
8022 if (hi < lo || byts[lo] != c)
8023 break;
8024
8025 /* Continue at the child (if there is one). */
8026 tryidx = idxs[lo];
8027 }
8028
8029 if (l == 0)
8030 {
8031 /*
8032 * Found the matching char. Copy it to "kword" and go a
8033 * level deeper.
8034 */
8035 if (round[depth] == 1)
8036 {
8037 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
8038 flen);
8039 kwordlen[depth + 1] = kwordlen[depth] + flen;
8040 }
8041 else
8042 {
8043 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
8044 ulen);
8045 kwordlen[depth + 1] = kwordlen[depth] + ulen;
8046 }
8047 fwordidx[depth + 1] = fwordidx[depth] + flen;
8048 uwordidx[depth + 1] = uwordidx[depth] + ulen;
8049
8050 ++depth;
8051 arridx[depth] = tryidx;
8052 round[depth] = 0;
8053 }
8054 }
8055 }
8056
8057 /* Didn't find it: "cannot happen". */
8058 *kword = NUL;
8059}
8060
8061/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008062 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
8063 * su->su_sga.
8064 */
8065 static void
8066score_comp_sal(su)
8067 suginfo_T *su;
8068{
8069 langp_T *lp;
8070 char_u badsound[MAXWLEN];
8071 int i;
8072 suggest_T *stp;
8073 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008074 int score;
8075
8076 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
8077 return;
8078
8079 /* Use the sound-folding of the first language that supports it. */
8080 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8081 lp->lp_slang != NULL; ++lp)
8082 if (lp->lp_slang->sl_sal.ga_len > 0)
8083 {
8084 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008085 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008086
8087 for (i = 0; i < su->su_ga.ga_len; ++i)
8088 {
8089 stp = &SUG(su->su_ga, i);
8090
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008091 /* Case-fold the suggested word, sound-fold it and compute the
8092 * sound-a-like score. */
8093 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008094 if (score < SCORE_MAXMAX)
8095 {
8096 /* Add the suggestion. */
8097 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
8098 sstp->st_word = vim_strsave(stp->st_word);
8099 if (sstp->st_word != NULL)
8100 {
8101 sstp->st_score = score;
8102 sstp->st_altscore = 0;
8103 sstp->st_orglen = stp->st_orglen;
8104 ++su->su_sga.ga_len;
8105 }
8106 }
8107 }
8108 break;
8109 }
8110}
8111
8112/*
8113 * Combine the list of suggestions in su->su_ga and su->su_sga.
8114 * They are intwined.
8115 */
8116 static void
8117score_combine(su)
8118 suginfo_T *su;
8119{
8120 int i;
8121 int j;
8122 garray_T ga;
8123 garray_T *gap;
8124 langp_T *lp;
8125 suggest_T *stp;
8126 char_u *p;
8127 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008128 int round;
8129
8130 /* Add the alternate score to su_ga. */
8131 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8132 lp->lp_slang != NULL; ++lp)
8133 {
8134 if (lp->lp_slang->sl_sal.ga_len > 0)
8135 {
8136 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008137 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008138
8139 for (i = 0; i < su->su_ga.ga_len; ++i)
8140 {
8141 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008142 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
8143 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008144 if (stp->st_altscore == SCORE_MAXMAX)
8145 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
8146 else
8147 stp->st_score = (stp->st_score * 3
8148 + stp->st_altscore) / 4;
8149 stp->st_salscore = FALSE;
8150 }
8151 break;
8152 }
8153 }
8154
8155 /* Add the alternate score to su_sga. */
8156 for (i = 0; i < su->su_sga.ga_len; ++i)
8157 {
8158 stp = &SUG(su->su_sga, i);
8159 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
8160 if (stp->st_score == SCORE_MAXMAX)
8161 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
8162 else
8163 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
8164 stp->st_salscore = TRUE;
8165 }
8166
8167 /* Sort the suggestions and truncate at "maxcount" for both lists. */
8168 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8169 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
8170
8171 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
8172 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
8173 return;
8174
8175 stp = &SUG(ga, 0);
8176 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
8177 {
8178 /* round 1: get a suggestion from su_ga
8179 * round 2: get a suggestion from su_sga */
8180 for (round = 1; round <= 2; ++round)
8181 {
8182 gap = round == 1 ? &su->su_ga : &su->su_sga;
8183 if (i < gap->ga_len)
8184 {
8185 /* Don't add a word if it's already there. */
8186 p = SUG(*gap, i).st_word;
8187 for (j = 0; j < ga.ga_len; ++j)
8188 if (STRCMP(stp[j].st_word, p) == 0)
8189 break;
8190 if (j == ga.ga_len)
8191 stp[ga.ga_len++] = SUG(*gap, i);
8192 else
8193 vim_free(p);
8194 }
8195 }
8196 }
8197
8198 ga_clear(&su->su_ga);
8199 ga_clear(&su->su_sga);
8200
8201 /* Truncate the list to the number of suggestions that will be displayed. */
8202 if (ga.ga_len > su->su_maxcount)
8203 {
8204 for (i = su->su_maxcount; i < ga.ga_len; ++i)
8205 vim_free(stp[i].st_word);
8206 ga.ga_len = su->su_maxcount;
8207 }
8208
8209 su->su_ga = ga;
8210}
8211
8212/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008213 * For the goodword in "stp" compute the soundalike score compared to the
8214 * badword.
8215 */
8216 static int
8217stp_sal_score(stp, su, slang, badsound)
8218 suggest_T *stp;
8219 suginfo_T *su;
8220 slang_T *slang;
8221 char_u *badsound; /* sound-folded badword */
8222{
8223 char_u *p;
8224 char_u badsound2[MAXWLEN];
8225 char_u fword[MAXWLEN];
8226 char_u goodsound[MAXWLEN];
8227
8228 if (stp->st_orglen <= su->su_badlen)
8229 p = badsound;
8230 else
8231 {
8232 /* soundfold the bad word with more characters following */
8233 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
8234
8235 /* When joining two words the sound often changes a lot. E.g., "t he"
8236 * sounds like "t h" while "the" sounds like "@". Avoid that by
8237 * removing the space. Don't do it when the good word also contains a
8238 * space. */
8239 if (vim_iswhite(su->su_badptr[su->su_badlen])
8240 && *skiptowhite(stp->st_word) == NUL)
8241 for (p = fword; *(p = skiptowhite(p)) != NUL; )
8242 mch_memmove(p, p + 1, STRLEN(p));
8243
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008244 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008245 p = badsound2;
8246 }
8247
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008248 /* Sound-fold the word and compute the score for the difference. */
8249 spell_soundfold(slang, stp->st_word, FALSE, goodsound);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008250
8251 return soundalike_score(goodsound, p);
8252}
8253
8254/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008255 * Find suggestions by comparing the word in a sound-a-like form.
8256 */
8257 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00008258suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008259 suginfo_T *su;
8260{
8261 char_u salword[MAXWLEN];
8262 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008263 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008264 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008265 int curi[MAXWLEN];
8266 langp_T *lp;
8267 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008268 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008269 int depth;
8270 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008271 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008272 int round;
8273 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008274 int sound_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008275
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008276 /* Do this for all languages that support sound folding. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008277 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8278 lp->lp_slang != NULL; ++lp)
8279 {
8280 if (lp->lp_slang->sl_sal.ga_len > 0)
8281 {
8282 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008283 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008284
8285 /*
8286 * Go through the whole tree, soundfold each word and compare.
8287 * round 1: use the case-folded tree.
8288 * round 2: use the keep-case tree.
8289 */
8290 for (round = 1; round <= 2; ++round)
8291 {
8292 if (round == 1)
8293 {
8294 byts = lp->lp_slang->sl_fbyts;
8295 idxs = lp->lp_slang->sl_fidxs;
8296 }
8297 else
8298 {
8299 byts = lp->lp_slang->sl_kbyts;
8300 idxs = lp->lp_slang->sl_kidxs;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008301 if (byts == NULL) /* no keep-case words */
8302 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008303 }
8304
8305 depth = 0;
8306 arridx[0] = 0;
8307 curi[0] = 1;
8308 while (depth >= 0 && !got_int)
8309 {
8310 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008311 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008312 /* Done all bytes at this node, go up one level. */
8313 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008314 line_breakcheck();
8315 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008316 else
8317 {
8318 /* Do one more byte at this node. */
8319 n = arridx[depth] + curi[depth];
8320 ++curi[depth];
8321 c = byts[n];
8322 if (c == 0)
8323 {
8324 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008325 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008326 if (round == 2 || (flags & WF_KEEPCAP) == 0)
8327 {
8328 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008329 /* Sound-fold. Only in keep-case tree need to
8330 * case-fold the word. */
8331 spell_soundfold(lp->lp_slang, tword,
8332 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008333
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008334 /* Compute the edit distance between the
8335 * sound-a-like words. */
8336 sound_score = soundalike_score(salword,
8337 tsalword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008338 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008339 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008340 char_u cword[MAXWLEN];
8341 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008342 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008343
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008344 flags |= su->su_badflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008345 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008346 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008347 /* Need to fix case according to
8348 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008349 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008350 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008351 }
8352 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008353 p = tword;
8354
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008355 if (sps_flags & SPS_DOUBLE)
8356 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008357 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008358 sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008359 else
8360 {
8361 /* Compute the score. */
8362 score = spell_edit_score(
8363 su->su_badword, p);
8364 if (sps_flags & SPS_BEST)
8365 /* give a bonus for the good word
8366 * sounding the same as the bad
8367 * word */
8368 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008369 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008370 RESCORE(score, sound_score),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008371 sound_score, TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008372 else
8373 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008374 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008375 score + sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008376 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008377 }
8378 }
8379
8380 /* Skip over other NUL bytes. */
8381 while (byts[n + 1] == 0)
8382 {
8383 ++n;
8384 ++curi[depth];
8385 }
8386 }
8387 else
8388 {
8389 /* Normal char, go one level deeper. */
8390 tword[depth++] = c;
8391 arridx[depth] = idxs[n];
8392 curi[depth] = 1;
8393 }
8394 }
8395 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008396 }
8397 }
8398 }
8399}
8400
8401/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008402 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008403 */
8404 static void
8405make_case_word(fword, cword, flags)
8406 char_u *fword;
8407 char_u *cword;
8408 int flags;
8409{
8410 if (flags & WF_ALLCAP)
8411 /* Make it all upper-case */
8412 allcap_copy(fword, cword);
8413 else if (flags & WF_ONECAP)
8414 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008415 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008416 else
8417 /* Use goodword as-is. */
8418 STRCPY(cword, fword);
8419}
8420
Bram Moolenaarea424162005-06-16 21:51:00 +00008421/*
8422 * Use map string "map" for languages "lp".
8423 */
8424 static void
8425set_map_str(lp, map)
8426 slang_T *lp;
8427 char_u *map;
8428{
8429 char_u *p;
8430 int headc = 0;
8431 int c;
8432 int i;
8433
8434 if (*map == NUL)
8435 {
8436 lp->sl_has_map = FALSE;
8437 return;
8438 }
8439 lp->sl_has_map = TRUE;
8440
8441 /* Init the array and hash table empty. */
8442 for (i = 0; i < 256; ++i)
8443 lp->sl_map_array[i] = 0;
8444#ifdef FEAT_MBYTE
8445 hash_init(&lp->sl_map_hash);
8446#endif
8447
8448 /*
8449 * The similar characters are stored separated with slashes:
8450 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
8451 * before the same slash. For characters above 255 sl_map_hash is used.
8452 */
8453 for (p = map; *p != NUL; )
8454 {
8455#ifdef FEAT_MBYTE
8456 c = mb_ptr2char_adv(&p);
8457#else
8458 c = *p++;
8459#endif
8460 if (c == '/')
8461 headc = 0;
8462 else
8463 {
8464 if (headc == 0)
8465 headc = c;
8466
8467#ifdef FEAT_MBYTE
8468 /* Characters above 255 don't fit in sl_map_array[], put them in
8469 * the hash table. Each entry is the char, a NUL the headchar and
8470 * a NUL. */
8471 if (c >= 256)
8472 {
8473 int cl = mb_char2len(c);
8474 int headcl = mb_char2len(headc);
8475 char_u *b;
8476 hash_T hash;
8477 hashitem_T *hi;
8478
8479 b = alloc((unsigned)(cl + headcl + 2));
8480 if (b == NULL)
8481 return;
8482 mb_char2bytes(c, b);
8483 b[cl] = NUL;
8484 mb_char2bytes(headc, b + cl + 1);
8485 b[cl + 1 + headcl] = NUL;
8486 hash = hash_hash(b);
8487 hi = hash_lookup(&lp->sl_map_hash, b, hash);
8488 if (HASHITEM_EMPTY(hi))
8489 hash_add_item(&lp->sl_map_hash, hi, b, hash);
8490 else
8491 {
8492 /* This should have been checked when generating the .spl
8493 * file. */
8494 EMSG(_("E999: duplicate char in MAP entry"));
8495 vim_free(b);
8496 }
8497 }
8498 else
8499#endif
8500 lp->sl_map_array[c] = headc;
8501 }
8502 }
8503}
8504
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008505/*
8506 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
8507 * lines in the .aff file.
8508 */
8509 static int
8510similar_chars(slang, c1, c2)
8511 slang_T *slang;
8512 int c1;
8513 int c2;
8514{
Bram Moolenaarea424162005-06-16 21:51:00 +00008515 int m1, m2;
8516#ifdef FEAT_MBYTE
8517 char_u buf[MB_MAXBYTES];
8518 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008519
Bram Moolenaarea424162005-06-16 21:51:00 +00008520 if (c1 >= 256)
8521 {
8522 buf[mb_char2bytes(c1, buf)] = 0;
8523 hi = hash_find(&slang->sl_map_hash, buf);
8524 if (HASHITEM_EMPTY(hi))
8525 m1 = 0;
8526 else
8527 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8528 }
8529 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008530#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00008531 m1 = slang->sl_map_array[c1];
8532 if (m1 == 0)
8533 return FALSE;
8534
8535
8536#ifdef FEAT_MBYTE
8537 if (c2 >= 256)
8538 {
8539 buf[mb_char2bytes(c2, buf)] = 0;
8540 hi = hash_find(&slang->sl_map_hash, buf);
8541 if (HASHITEM_EMPTY(hi))
8542 m2 = 0;
8543 else
8544 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8545 }
8546 else
8547#endif
8548 m2 = slang->sl_map_array[c2];
8549
8550 return m1 == m2;
8551}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008552
8553/*
8554 * Add a suggestion to the list of suggestions.
8555 * Do not add a duplicate suggestion or suggestions with a bad score.
8556 * When "use_score" is not zero it's used, otherwise the score is computed
8557 * with spell_edit_score().
8558 */
8559 static void
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008560add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008561 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008562 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008563 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008564 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008565 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008566 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008567 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008568{
8569 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008570 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008571 char_u *p = NULL;
8572 int c = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008573
8574 /* Check that the word wasn't banned. */
8575 if (was_banned(su, goodword))
8576 return;
8577
Bram Moolenaar0c405862005-06-22 22:26:26 +00008578 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
8579 * Remove the common part from "goodword". */
8580 i = badlen - su->su_badlen;
8581 if (i > 0)
8582 {
8583 /* This assumes there was no case folding or it didn't change the
8584 * length... */
8585 p = goodword + STRLEN(goodword) - i;
8586 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
8587 {
8588 badlen = su->su_badlen;
8589 c = *p;
8590 *p = NUL;
8591 }
8592 else
8593 p = NULL;
8594 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008595 else if (i < 0)
8596 {
8597 /* When replacing part of the word check that we actually change
8598 * something. For "the the" a suggestion can be replacing the first
8599 * "the" with itself, since "the" wasn't banned. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008600 if (badlen == (int)STRLEN(goodword)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008601 && STRNCMP(su->su_badword, goodword, badlen) == 0)
8602 return;
8603 }
8604
Bram Moolenaar0c405862005-06-22 22:26:26 +00008605
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008606 if (score <= su->su_maxscore)
8607 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008608 /* Check if the word is already there. Also check the length that is
8609 * being replaced "thes," -> "these" is a different suggestion from
8610 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008611 stp = &SUG(*gap, 0);
8612 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008613 if (STRCMP(stp[i].st_word, goodword) == 0
8614 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008615 {
8616 /* Found it. Remember the lowest score. */
8617 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008618 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008619 stp[i].st_score = score;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008620 stp[i].st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008621 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008622 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008623 break;
8624 }
8625
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008626 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008627 {
8628 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008629 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008630 stp->st_word = vim_strsave(goodword);
8631 if (stp->st_word != NULL)
8632 {
8633 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008634 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008635 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008636 stp->st_orglen = badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008637 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008638
8639 /* If we have too many suggestions now, sort the list and keep
8640 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008641 if (gap->ga_len > SUG_MAX_COUNT(su))
8642 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
8643 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008644 }
8645 }
8646 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008647
8648 if (p != NULL)
8649 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008650}
8651
8652/*
8653 * Add a word to be banned.
8654 */
8655 static void
8656add_banned(su, word)
8657 suginfo_T *su;
8658 char_u *word;
8659{
8660 char_u *s = vim_strsave(word);
8661 hash_T hash;
8662 hashitem_T *hi;
8663
8664 if (s != NULL)
8665 {
8666 hash = hash_hash(s);
8667 hi = hash_lookup(&su->su_banned, s, hash);
8668 if (HASHITEM_EMPTY(hi))
8669 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008670 else
8671 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008672 }
8673}
8674
8675/*
8676 * Return TRUE if a word appears in the list of banned words.
8677 */
8678 static int
8679was_banned(su, word)
8680 suginfo_T *su;
8681 char_u *word;
8682{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008683 hashitem_T *hi = hash_find(&su->su_banned, word);
8684
8685 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008686}
8687
8688/*
8689 * Free the banned words in "su".
8690 */
8691 static void
8692free_banned(su)
8693 suginfo_T *su;
8694{
8695 int todo;
8696 hashitem_T *hi;
8697
8698 todo = su->su_banned.ht_used;
8699 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
8700 {
8701 if (!HASHITEM_EMPTY(hi))
8702 {
8703 vim_free(hi->hi_key);
8704 --todo;
8705 }
8706 }
8707 hash_clear(&su->su_banned);
8708}
8709
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008710/*
8711 * Recompute the score if sound-folding is possible. This is slow,
8712 * thus only done for the final results.
8713 */
8714 static void
8715rescore_suggestions(su)
8716 suginfo_T *su;
8717{
8718 langp_T *lp;
8719 suggest_T *stp;
8720 char_u sal_badword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008721 int i;
8722
8723 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8724 lp->lp_slang != NULL; ++lp)
8725 {
8726 if (lp->lp_slang->sl_sal.ga_len > 0)
8727 {
8728 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008729 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008730
8731 for (i = 0; i < su->su_ga.ga_len; ++i)
8732 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008733 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008734 if (!stp->st_had_bonus)
8735 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008736 stp->st_altscore = stp_sal_score(stp, su,
8737 lp->lp_slang, sal_badword);
8738 if (stp->st_altscore == SCORE_MAXMAX)
8739 stp->st_altscore = SCORE_BIG;
8740 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008741 }
8742 }
8743 break;
8744 }
8745 }
8746}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008747
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008748static int
8749#ifdef __BORLANDC__
8750_RTLENTRYF
8751#endif
8752sug_compare __ARGS((const void *s1, const void *s2));
8753
8754/*
8755 * Function given to qsort() to sort the suggestions on st_score.
8756 */
8757 static int
8758#ifdef __BORLANDC__
8759_RTLENTRYF
8760#endif
8761sug_compare(s1, s2)
8762 const void *s1;
8763 const void *s2;
8764{
8765 suggest_T *p1 = (suggest_T *)s1;
8766 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008767 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008768
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008769 if (n == 0)
8770 return p1->st_altscore - p2->st_altscore;
8771 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008772}
8773
8774/*
8775 * Cleanup the suggestions:
8776 * - Sort on score.
8777 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008778 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008779 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008780 static int
8781cleanup_suggestions(gap, maxscore, keep)
8782 garray_T *gap;
8783 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008784 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008785{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008786 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008787 int i;
8788
8789 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008790 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008791
8792 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008793 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008794 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008795 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008796 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008797 gap->ga_len = keep;
8798 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008799 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008800 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008801}
8802
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008803#if defined(FEAT_EVAL) || defined(PROTO)
8804/*
8805 * Soundfold a string, for soundfold().
8806 * Result is in allocated memory, NULL for an error.
8807 */
8808 char_u *
8809eval_soundfold(word)
8810 char_u *word;
8811{
8812 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008813 char_u sound[MAXWLEN];
8814
8815 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
8816 /* Use the sound-folding of the first language that supports it. */
8817 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8818 lp->lp_slang != NULL; ++lp)
8819 if (lp->lp_slang->sl_sal.ga_len > 0)
8820 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008821 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008822 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008823 return vim_strsave(sound);
8824 }
8825
8826 /* No language with sound folding, return word as-is. */
8827 return vim_strsave(word);
8828}
8829#endif
8830
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008831/*
8832 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
8833 */
8834 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008835spell_soundfold(slang, inword, folded, res)
8836 slang_T *slang;
8837 char_u *inword;
8838 int folded; /* "inword" is already case-folded */
8839 char_u *res;
8840{
8841 char_u fword[MAXWLEN];
8842 char_u *word;
8843
8844 if (slang->sl_sofo)
8845 /* SOFOFROM and SOFOTO used */
8846 spell_soundfold_sofo(slang, inword, res);
8847 else
8848 {
8849 /* SAL items used. Requires the word to be case-folded. */
8850 if (folded)
8851 word = inword;
8852 else
8853 {
8854 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
8855 word = fword;
8856 }
8857
8858#ifdef FEAT_MBYTE
8859 if (has_mbyte)
8860 spell_soundfold_wsal(slang, word, res);
8861 else
8862#endif
8863 spell_soundfold_sal(slang, word, res);
8864 }
8865}
8866
8867/*
8868 * Perform sound folding of "inword" into "res" according to SOFOFROM and
8869 * SOFOTO lines.
8870 */
8871 static void
8872spell_soundfold_sofo(slang, inword, res)
8873 slang_T *slang;
8874 char_u *inword;
8875 char_u *res;
8876{
8877 char_u *s;
8878 int ri = 0;
8879 int c;
8880
8881#ifdef FEAT_MBYTE
8882 if (has_mbyte)
8883 {
8884 int prevc = 0;
8885 int *ip;
8886
8887 /* The sl_sal_first[] table contains the translation for chars up to
8888 * 255, sl_sal the rest. */
8889 for (s = inword; *s != NUL; )
8890 {
8891 c = mb_ptr2char_adv(&s);
8892 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
8893 c = ' ';
8894 else if (c < 256)
8895 c = slang->sl_sal_first[c];
8896 else
8897 {
8898 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
8899 if (ip == NULL) /* empty list, can't match */
8900 c = NUL;
8901 else
8902 for (;;) /* find "c" in the list */
8903 {
8904 if (*ip == 0) /* not found */
8905 {
8906 c = NUL;
8907 break;
8908 }
8909 if (*ip == c) /* match! */
8910 {
8911 c = ip[1];
8912 break;
8913 }
8914 ip += 2;
8915 }
8916 }
8917
8918 if (c != NUL && c != prevc)
8919 {
8920 ri += mb_char2bytes(c, res + ri);
8921 if (ri + MB_MAXBYTES > MAXWLEN)
8922 break;
8923 prevc = c;
8924 }
8925 }
8926 }
8927 else
8928#endif
8929 {
8930 /* The sl_sal_first[] table contains the translation. */
8931 for (s = inword; (c = *s) != NUL; ++s)
8932 {
8933 if (vim_iswhite(c))
8934 c = ' ';
8935 else
8936 c = slang->sl_sal_first[c];
8937 if (c != NUL && (ri == 0 || res[ri - 1] != c))
8938 res[ri++] = c;
8939 }
8940 }
8941
8942 res[ri] = NUL;
8943}
8944
8945 static void
8946spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008947 slang_T *slang;
8948 char_u *inword;
8949 char_u *res;
8950{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008951 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008952 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008953 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008954 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008955 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008956 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008957 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008958 int n, k = 0;
8959 int z0;
8960 int k0;
8961 int n0;
8962 int c;
8963 int pri;
8964 int p0 = -333;
8965 int c0;
8966
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008967 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008968 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008969 if (slang->sl_rem_accents)
8970 {
8971 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008972 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008973 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008974 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008975 {
8976 *t++ = ' ';
8977 s = skipwhite(s);
8978 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008979 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008980 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008981 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008982 *t++ = *s;
8983 ++s;
8984 }
8985 }
8986 *t = NUL;
8987 }
8988 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008989 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008990
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008991 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008992
8993 /*
8994 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008995 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008996 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008997 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008998 while ((c = word[i]) != NUL)
8999 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009000 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009001 n = slang->sl_sal_first[c];
9002 z0 = 0;
9003
9004 if (n >= 0)
9005 {
9006 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009007 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009008 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009009 /* Quickly skip entries that don't match the word. Most
9010 * entries are less then three chars, optimize for that. */
9011 k = smp[n].sm_leadlen;
9012 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009013 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009014 if (word[i + 1] != s[1])
9015 continue;
9016 if (k > 2)
9017 {
9018 for (j = 2; j < k; ++j)
9019 if (word[i + j] != s[j])
9020 break;
9021 if (j < k)
9022 continue;
9023 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009024 }
9025
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009026 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009027 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009028 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009029 while (*pf != NUL && *pf != word[i + k])
9030 ++pf;
9031 if (*pf == NUL)
9032 continue;
9033 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009034 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009035 s = smp[n].sm_rules;
9036 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009037
9038 p0 = *s;
9039 k0 = k;
9040 while (*s == '-' && k > 1)
9041 {
9042 k--;
9043 s++;
9044 }
9045 if (*s == '<')
9046 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009047 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009048 {
9049 /* determine priority */
9050 pri = *s - '0';
9051 s++;
9052 }
9053 if (*s == '^' && *(s + 1) == '^')
9054 s++;
9055
9056 if (*s == NUL
9057 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009058 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009059 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009060 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009061 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009062 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009063 && spell_iswordp(word + i - 1, curbuf)
9064 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009065 {
9066 /* search for followup rules, if: */
9067 /* followup and k > 1 and NO '-' in searchstring */
9068 c0 = word[i + k - 1];
9069 n0 = slang->sl_sal_first[c0];
9070
9071 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009072 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009073 {
9074 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009075 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009076 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009077 /* Quickly skip entries that don't match the word.
9078 * */
9079 k0 = smp[n0].sm_leadlen;
9080 if (k0 > 1)
9081 {
9082 if (word[i + k] != s[1])
9083 continue;
9084 if (k0 > 2)
9085 {
9086 pf = word + i + k + 1;
9087 for (j = 2; j < k0; ++j)
9088 if (*pf++ != s[j])
9089 break;
9090 if (j < k0)
9091 continue;
9092 }
9093 }
9094 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009095
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009096 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009097 {
9098 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009099 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009100 while (*pf != NUL && *pf != word[i + k0])
9101 ++pf;
9102 if (*pf == NUL)
9103 continue;
9104 ++k0;
9105 }
9106
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009107 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009108 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009109 while (*s == '-')
9110 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009111 /* "k0" gets NOT reduced because
9112 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009113 s++;
9114 }
9115 if (*s == '<')
9116 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009117 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009118 {
9119 p0 = *s - '0';
9120 s++;
9121 }
9122
9123 if (*s == NUL
9124 /* *s == '^' cuts */
9125 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009126 && !spell_iswordp(word + i + k0,
9127 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009128 {
9129 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009130 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009131 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009132
9133 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009134 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009135 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009136 /* rule fits; stop search */
9137 break;
9138 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009139 }
9140
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009141 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009142 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009143 }
9144
9145 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009146 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009147 if (s == NULL)
9148 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009149 pf = smp[n].sm_rules;
9150 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009151 if (p0 == 1 && z == 0)
9152 {
9153 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009154 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
9155 || res[reslen - 1] == *s))
9156 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009157 z0 = 1;
9158 z = 1;
9159 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009160 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009161 {
9162 word[i + k0] = *s;
9163 k0++;
9164 s++;
9165 }
9166 if (k > k0)
9167 mch_memmove(word + i + k0, word + i + k,
9168 STRLEN(word + i + k) + 1);
9169
9170 /* new "actual letter" */
9171 c = word[i];
9172 }
9173 else
9174 {
9175 /* no '<' rule used */
9176 i += k - 1;
9177 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009178 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009179 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009180 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009181 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009182 s++;
9183 }
9184 /* new "actual letter" */
9185 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009186 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009187 {
9188 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009189 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009190 mch_memmove(word, word + i + 1,
9191 STRLEN(word + i + 1) + 1);
9192 i = 0;
9193 z0 = 1;
9194 }
9195 }
9196 break;
9197 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009198 }
9199 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009200 else if (vim_iswhite(c))
9201 {
9202 c = ' ';
9203 k = 1;
9204 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009205
9206 if (z0 == 0)
9207 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009208 if (k && !p0 && reslen < MAXWLEN && c != NUL
9209 && (!slang->sl_collapse || reslen == 0
9210 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009211 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009212 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009213
9214 i++;
9215 z = 0;
9216 k = 0;
9217 }
9218 }
9219
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009220 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009221}
9222
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009223#ifdef FEAT_MBYTE
9224/*
9225 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
9226 * Multi-byte version of spell_soundfold().
9227 */
9228 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009229spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009230 slang_T *slang;
9231 char_u *inword;
9232 char_u *res;
9233{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009234 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009235 int word[MAXWLEN];
9236 int wres[MAXWLEN];
9237 int l;
9238 char_u *s;
9239 int *ws;
9240 char_u *t;
9241 int *pf;
9242 int i, j, z;
9243 int reslen;
9244 int n, k = 0;
9245 int z0;
9246 int k0;
9247 int n0;
9248 int c;
9249 int pri;
9250 int p0 = -333;
9251 int c0;
9252 int did_white = FALSE;
9253
9254 /*
9255 * Convert the multi-byte string to a wide-character string.
9256 * Remove accents, if wanted. We actually remove all non-word characters.
9257 * But keep white space.
9258 */
9259 n = 0;
9260 for (s = inword; *s != NUL; )
9261 {
9262 t = s;
9263 c = mb_ptr2char_adv(&s);
9264 if (slang->sl_rem_accents)
9265 {
9266 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
9267 {
9268 if (did_white)
9269 continue;
9270 c = ' ';
9271 did_white = TRUE;
9272 }
9273 else
9274 {
9275 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009276 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009277 continue;
9278 }
9279 }
9280 word[n++] = c;
9281 }
9282 word[n] = NUL;
9283
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009284 /*
9285 * This comes from Aspell phonet.cpp.
9286 * Converted from C++ to C. Added support for multi-byte chars.
9287 * Changed to keep spaces.
9288 */
9289 i = reslen = z = 0;
9290 while ((c = word[i]) != NUL)
9291 {
9292 /* Start with the first rule that has the character in the word. */
9293 n = slang->sl_sal_first[c & 0xff];
9294 z0 = 0;
9295
9296 if (n >= 0)
9297 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009298 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009299 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
9300 {
9301 /* Quickly skip entries that don't match the word. Most
9302 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009303 if (c != ws[0])
9304 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009305 k = smp[n].sm_leadlen;
9306 if (k > 1)
9307 {
9308 if (word[i + 1] != ws[1])
9309 continue;
9310 if (k > 2)
9311 {
9312 for (j = 2; j < k; ++j)
9313 if (word[i + j] != ws[j])
9314 break;
9315 if (j < k)
9316 continue;
9317 }
9318 }
9319
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009320 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009321 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009322 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009323 while (*pf != NUL && *pf != word[i + k])
9324 ++pf;
9325 if (*pf == NUL)
9326 continue;
9327 ++k;
9328 }
9329 s = smp[n].sm_rules;
9330 pri = 5; /* default priority */
9331
9332 p0 = *s;
9333 k0 = k;
9334 while (*s == '-' && k > 1)
9335 {
9336 k--;
9337 s++;
9338 }
9339 if (*s == '<')
9340 s++;
9341 if (VIM_ISDIGIT(*s))
9342 {
9343 /* determine priority */
9344 pri = *s - '0';
9345 s++;
9346 }
9347 if (*s == '^' && *(s + 1) == '^')
9348 s++;
9349
9350 if (*s == NUL
9351 || (*s == '^'
9352 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009353 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009354 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009355 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009356 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009357 && spell_iswordp_w(word + i - 1, curbuf)
9358 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009359 {
9360 /* search for followup rules, if: */
9361 /* followup and k > 1 and NO '-' in searchstring */
9362 c0 = word[i + k - 1];
9363 n0 = slang->sl_sal_first[c0 & 0xff];
9364
9365 if (slang->sl_followup && k > 1 && n0 >= 0
9366 && p0 != '-' && word[i + k] != NUL)
9367 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009368 /* Test follow-up rule for "word[i + k]"; loop over
9369 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009370 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
9371 == (c0 & 0xff); ++n0)
9372 {
9373 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009374 */
9375 if (c0 != ws[0])
9376 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009377 k0 = smp[n0].sm_leadlen;
9378 if (k0 > 1)
9379 {
9380 if (word[i + k] != ws[1])
9381 continue;
9382 if (k0 > 2)
9383 {
9384 pf = word + i + k + 1;
9385 for (j = 2; j < k0; ++j)
9386 if (*pf++ != ws[j])
9387 break;
9388 if (j < k0)
9389 continue;
9390 }
9391 }
9392 k0 += k - 1;
9393
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009394 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009395 {
9396 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009397 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009398 while (*pf != NUL && *pf != word[i + k0])
9399 ++pf;
9400 if (*pf == NUL)
9401 continue;
9402 ++k0;
9403 }
9404
9405 p0 = 5;
9406 s = smp[n0].sm_rules;
9407 while (*s == '-')
9408 {
9409 /* "k0" gets NOT reduced because
9410 * "if (k0 == k)" */
9411 s++;
9412 }
9413 if (*s == '<')
9414 s++;
9415 if (VIM_ISDIGIT(*s))
9416 {
9417 p0 = *s - '0';
9418 s++;
9419 }
9420
9421 if (*s == NUL
9422 /* *s == '^' cuts */
9423 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009424 && !spell_iswordp_w(word + i + k0,
9425 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009426 {
9427 if (k0 == k)
9428 /* this is just a piece of the string */
9429 continue;
9430
9431 if (p0 < pri)
9432 /* priority too low */
9433 continue;
9434 /* rule fits; stop search */
9435 break;
9436 }
9437 }
9438
9439 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
9440 == (c0 & 0xff))
9441 continue;
9442 }
9443
9444 /* replace string */
9445 ws = smp[n].sm_to_w;
9446 s = smp[n].sm_rules;
9447 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
9448 if (p0 == 1 && z == 0)
9449 {
9450 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009451 if (reslen > 0 && ws != NULL && *ws != NUL
9452 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009453 || wres[reslen - 1] == *ws))
9454 reslen--;
9455 z0 = 1;
9456 z = 1;
9457 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009458 if (ws != NULL)
9459 while (*ws != NUL && word[i + k0] != NUL)
9460 {
9461 word[i + k0] = *ws;
9462 k0++;
9463 ws++;
9464 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009465 if (k > k0)
9466 mch_memmove(word + i + k0, word + i + k,
9467 sizeof(int) * (STRLEN(word + i + k) + 1));
9468
9469 /* new "actual letter" */
9470 c = word[i];
9471 }
9472 else
9473 {
9474 /* no '<' rule used */
9475 i += k - 1;
9476 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009477 if (ws != NULL)
9478 while (*ws != NUL && ws[1] != NUL
9479 && reslen < MAXWLEN)
9480 {
9481 if (reslen == 0 || wres[reslen - 1] != *ws)
9482 wres[reslen++] = *ws;
9483 ws++;
9484 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009485 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009486 if (ws == NULL)
9487 c = NUL;
9488 else
9489 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009490 if (strstr((char *)s, "^^") != NULL)
9491 {
9492 if (c != NUL)
9493 wres[reslen++] = c;
9494 mch_memmove(word, word + i + 1,
9495 sizeof(int) * (STRLEN(word + i + 1) + 1));
9496 i = 0;
9497 z0 = 1;
9498 }
9499 }
9500 break;
9501 }
9502 }
9503 }
9504 else if (vim_iswhite(c))
9505 {
9506 c = ' ';
9507 k = 1;
9508 }
9509
9510 if (z0 == 0)
9511 {
9512 if (k && !p0 && reslen < MAXWLEN && c != NUL
9513 && (!slang->sl_collapse || reslen == 0
9514 || wres[reslen - 1] != c))
9515 /* condense only double letters */
9516 wres[reslen++] = c;
9517
9518 i++;
9519 z = 0;
9520 k = 0;
9521 }
9522 }
9523
9524 /* Convert wide characters in "wres" to a multi-byte string in "res". */
9525 l = 0;
9526 for (n = 0; n < reslen; ++n)
9527 {
9528 l += mb_char2bytes(wres[n], res + l);
9529 if (l + MB_MAXBYTES > MAXWLEN)
9530 break;
9531 }
9532 res[l] = NUL;
9533}
9534#endif
9535
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009536/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009537 * Compute a score for two sound-a-like words.
9538 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
9539 * Instead of a generic loop we write out the code. That keeps it fast by
9540 * avoiding checks that will not be possible.
9541 */
9542 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009543soundalike_score(goodstart, badstart)
9544 char_u *goodstart; /* sound-folded good word */
9545 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009546{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009547 char_u *goodsound = goodstart;
9548 char_u *badsound = badstart;
9549 int goodlen;
9550 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009551 int n;
9552 char_u *pl, *ps;
9553 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009554 int score = 0;
9555
9556 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
9557 * counted so much, vowels halfway the word aren't counted at all. */
9558 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
9559 {
9560 score = SCORE_DEL / 2;
9561 if (*badsound == '*')
9562 ++badsound;
9563 else
9564 ++goodsound;
9565 }
9566
9567 goodlen = STRLEN(goodsound);
9568 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009569
9570 /* Return quickly if the lenghts are too different to be fixed by two
9571 * changes. */
9572 n = goodlen - badlen;
9573 if (n < -2 || n > 2)
9574 return SCORE_MAXMAX;
9575
9576 if (n > 0)
9577 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009578 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009579 ps = badsound;
9580 }
9581 else
9582 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009583 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009584 ps = goodsound;
9585 }
9586
9587 /* Skip over the identical part. */
9588 while (*pl == *ps && *pl != NUL)
9589 {
9590 ++pl;
9591 ++ps;
9592 }
9593
9594 switch (n)
9595 {
9596 case -2:
9597 case 2:
9598 /*
9599 * Must delete two characters from "pl".
9600 */
9601 ++pl; /* first delete */
9602 while (*pl == *ps)
9603 {
9604 ++pl;
9605 ++ps;
9606 }
9607 /* strings must be equal after second delete */
9608 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009609 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009610
9611 /* Failed to compare. */
9612 break;
9613
9614 case -1:
9615 case 1:
9616 /*
9617 * Minimal one delete from "pl" required.
9618 */
9619
9620 /* 1: delete */
9621 pl2 = pl + 1;
9622 ps2 = ps;
9623 while (*pl2 == *ps2)
9624 {
9625 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009626 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009627 ++pl2;
9628 ++ps2;
9629 }
9630
9631 /* 2: delete then swap, then rest must be equal */
9632 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9633 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009634 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009635
9636 /* 3: delete then substitute, then the rest must be equal */
9637 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009638 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009639
9640 /* 4: first swap then delete */
9641 if (pl[0] == ps[1] && pl[1] == ps[0])
9642 {
9643 pl2 = pl + 2; /* swap, skip two chars */
9644 ps2 = ps + 2;
9645 while (*pl2 == *ps2)
9646 {
9647 ++pl2;
9648 ++ps2;
9649 }
9650 /* delete a char and then strings must be equal */
9651 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009652 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009653 }
9654
9655 /* 5: first substitute then delete */
9656 pl2 = pl + 1; /* substitute, skip one char */
9657 ps2 = ps + 1;
9658 while (*pl2 == *ps2)
9659 {
9660 ++pl2;
9661 ++ps2;
9662 }
9663 /* delete a char and then strings must be equal */
9664 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009665 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009666
9667 /* Failed to compare. */
9668 break;
9669
9670 case 0:
9671 /*
9672 * Lenghts are equal, thus changes must result in same length: An
9673 * insert is only possible in combination with a delete.
9674 * 1: check if for identical strings
9675 */
9676 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009677 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009678
9679 /* 2: swap */
9680 if (pl[0] == ps[1] && pl[1] == ps[0])
9681 {
9682 pl2 = pl + 2; /* swap, skip two chars */
9683 ps2 = ps + 2;
9684 while (*pl2 == *ps2)
9685 {
9686 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009687 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009688 ++pl2;
9689 ++ps2;
9690 }
9691 /* 3: swap and swap again */
9692 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9693 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009694 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009695
9696 /* 4: swap and substitute */
9697 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009698 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009699 }
9700
9701 /* 5: substitute */
9702 pl2 = pl + 1;
9703 ps2 = ps + 1;
9704 while (*pl2 == *ps2)
9705 {
9706 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009707 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009708 ++pl2;
9709 ++ps2;
9710 }
9711
9712 /* 6: substitute and swap */
9713 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9714 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009715 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009716
9717 /* 7: substitute and substitute */
9718 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009719 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009720
9721 /* 8: insert then delete */
9722 pl2 = pl;
9723 ps2 = ps + 1;
9724 while (*pl2 == *ps2)
9725 {
9726 ++pl2;
9727 ++ps2;
9728 }
9729 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009730 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009731
9732 /* 9: delete then insert */
9733 pl2 = pl + 1;
9734 ps2 = ps;
9735 while (*pl2 == *ps2)
9736 {
9737 ++pl2;
9738 ++ps2;
9739 }
9740 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009741 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009742
9743 /* Failed to compare. */
9744 break;
9745 }
9746
9747 return SCORE_MAXMAX;
9748}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009749
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009750/*
9751 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009752 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009753 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009754 * The algorithm comes from Aspell editdist.cpp, edit_distance().
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009755 * It has been converted from C++ to C and modified to support multi-byte
9756 * characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009757 */
9758 static int
9759spell_edit_score(badword, goodword)
9760 char_u *badword;
9761 char_u *goodword;
9762{
9763 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009764 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009765 int j, i;
9766 int t;
9767 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009768 int pbc, pgc;
9769#ifdef FEAT_MBYTE
9770 char_u *p;
9771 int wbadword[MAXWLEN];
9772 int wgoodword[MAXWLEN];
9773
9774 if (has_mbyte)
9775 {
9776 /* Get the characters from the multi-byte strings and put them in an
9777 * int array for easy access. */
9778 for (p = badword, badlen = 0; *p != NUL; )
9779 wbadword[badlen++] = mb_ptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +00009780 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009781 for (p = goodword, goodlen = 0; *p != NUL; )
9782 wgoodword[goodlen++] = mb_ptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +00009783 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009784 }
9785 else
9786#endif
9787 {
9788 badlen = STRLEN(badword) + 1;
9789 goodlen = STRLEN(goodword) + 1;
9790 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009791
9792 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
9793#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009794 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
9795 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009796 if (cnt == NULL)
9797 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009798
9799 CNT(0, 0) = 0;
9800 for (j = 1; j <= goodlen; ++j)
9801 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
9802
9803 for (i = 1; i <= badlen; ++i)
9804 {
9805 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
9806 for (j = 1; j <= goodlen; ++j)
9807 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009808#ifdef FEAT_MBYTE
9809 if (has_mbyte)
9810 {
9811 bc = wbadword[i - 1];
9812 gc = wgoodword[j - 1];
9813 }
9814 else
9815#endif
9816 {
9817 bc = badword[i - 1];
9818 gc = goodword[j - 1];
9819 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009820 if (bc == gc)
9821 CNT(i, j) = CNT(i - 1, j - 1);
9822 else
9823 {
9824 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009825 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009826 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
9827 else
9828 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
9829
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009830 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009831 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009832#ifdef FEAT_MBYTE
9833 if (has_mbyte)
9834 {
9835 pbc = wbadword[i - 2];
9836 pgc = wgoodword[j - 2];
9837 }
9838 else
9839#endif
9840 {
9841 pbc = badword[i - 2];
9842 pgc = goodword[j - 2];
9843 }
9844 if (bc == pgc && pbc == gc)
9845 {
9846 t = SCORE_SWAP + CNT(i - 2, j - 2);
9847 if (t < CNT(i, j))
9848 CNT(i, j) = t;
9849 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009850 }
9851 t = SCORE_DEL + CNT(i - 1, j);
9852 if (t < CNT(i, j))
9853 CNT(i, j) = t;
9854 t = SCORE_INS + CNT(i, j - 1);
9855 if (t < CNT(i, j))
9856 CNT(i, j) = t;
9857 }
9858 }
9859 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009860
9861 i = CNT(badlen - 1, goodlen - 1);
9862 vim_free(cnt);
9863 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009864}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009865
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009866/*
9867 * ":spelldump"
9868 */
9869/*ARGSUSED*/
9870 void
9871ex_spelldump(eap)
9872 exarg_T *eap;
9873{
9874 buf_T *buf = curbuf;
9875 langp_T *lp;
9876 slang_T *slang;
9877 idx_T arridx[MAXWLEN];
9878 int curi[MAXWLEN];
9879 char_u word[MAXWLEN];
9880 int c;
9881 char_u *byts;
9882 idx_T *idxs;
9883 linenr_T lnum = 0;
9884 int round;
9885 int depth;
9886 int n;
9887 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009888 char_u *region_names = NULL; /* region names being used */
9889 int do_region = TRUE; /* dump region names and numbers */
9890 char_u *p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009891
9892 if (no_spell_checking())
9893 return;
9894
9895 /* Create a new empty buffer by splitting the window. */
9896 do_cmdline_cmd((char_u *)"new");
9897 if (!bufempty() || !buf_valid(buf))
9898 return;
9899
Bram Moolenaar7887d882005-07-01 22:33:52 +00009900 /* Find out if we can support regions: All languages must support the same
9901 * regions or none at all. */
9902 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
9903 {
9904 p = lp->lp_slang->sl_regions;
9905 if (p[0] != 0)
9906 {
9907 if (region_names == NULL) /* first language with regions */
9908 region_names = p;
9909 else if (STRCMP(region_names, p) != 0)
9910 {
9911 do_region = FALSE; /* region names are different */
9912 break;
9913 }
9914 }
9915 }
9916
9917 if (do_region && region_names != NULL)
9918 {
9919 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
9920 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
9921 }
9922 else
9923 do_region = FALSE;
9924
9925 /*
9926 * Loop over all files loaded for the entries in 'spelllang'.
9927 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009928 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
9929 {
9930 slang = lp->lp_slang;
9931
9932 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
9933 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
9934
9935 /* round 1: case-folded tree
9936 * round 2: keep-case tree */
9937 for (round = 1; round <= 2; ++round)
9938 {
9939 if (round == 1)
9940 {
9941 byts = slang->sl_fbyts;
9942 idxs = slang->sl_fidxs;
9943 }
9944 else
9945 {
9946 byts = slang->sl_kbyts;
9947 idxs = slang->sl_kidxs;
9948 }
9949 if (byts == NULL)
9950 continue; /* array is empty */
9951
9952 depth = 0;
9953 arridx[0] = 0;
9954 curi[0] = 1;
9955 while (depth >= 0 && !got_int)
9956 {
9957 if (curi[depth] > byts[arridx[depth]])
9958 {
9959 /* Done all bytes at this node, go up one level. */
9960 --depth;
9961 line_breakcheck();
9962 }
9963 else
9964 {
9965 /* Do one more byte at this node. */
9966 n = arridx[depth] + curi[depth];
9967 ++curi[depth];
9968 c = byts[n];
9969 if (c == 0)
9970 {
9971 /* End of word, deal with the word.
9972 * Don't use keep-case words in the fold-case tree,
9973 * they will appear in the keep-case tree.
9974 * Only use the word when the region matches. */
9975 flags = (int)idxs[n];
9976 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009977 && (do_region
9978 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009979 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009980 & lp->lp_region) != 0))
9981 {
9982 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009983 if (!do_region)
9984 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009985
9986 /* Dump the basic word if there is no prefix or
9987 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009988 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009989 if (c == 0 || curi[depth] == 2)
9990 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009991
9992 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009993 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009994 lnum = apply_prefixes(slang, word, round,
9995 flags, lnum);
9996 }
9997 }
9998 else
9999 {
10000 /* Normal char, go one level deeper. */
10001 word[depth++] = c;
10002 arridx[depth] = idxs[n];
10003 curi[depth] = 1;
10004 }
10005 }
10006 }
10007 }
10008 }
10009
10010 /* Delete the empty line that we started with. */
10011 if (curbuf->b_ml.ml_line_count > 1)
10012 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
10013
10014 redraw_later(NOT_VALID);
10015}
10016
10017/*
10018 * Dump one word: apply case modifications and append a line to the buffer.
10019 */
10020 static void
10021dump_word(word, round, flags, lnum)
10022 char_u *word;
10023 int round;
10024 int flags;
10025 linenr_T lnum;
10026{
10027 int keepcap = FALSE;
10028 char_u *p;
10029 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000010030 char_u badword[MAXWLEN + 10];
10031 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010032
10033 if (round == 1 && (flags & WF_CAPMASK) != 0)
10034 {
10035 /* Need to fix case according to "flags". */
10036 make_case_word(word, cword, flags);
10037 p = cword;
10038 }
10039 else
10040 {
10041 p = word;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010042 if (round == 2 && ((captype(word, NULL) & WF_KEEPCAP) == 0
10043 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010044 keepcap = TRUE;
10045 }
10046
Bram Moolenaar7887d882005-07-01 22:33:52 +000010047 /* Add flags and regions after a slash. */
10048 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010049 {
Bram Moolenaar7887d882005-07-01 22:33:52 +000010050 STRCPY(badword, p);
10051 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010052 if (keepcap)
10053 STRCAT(badword, "=");
10054 if (flags & WF_BANNED)
10055 STRCAT(badword, "!");
10056 else if (flags & WF_RARE)
10057 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +000010058 if (flags & WF_REGION)
10059 for (i = 0; i < 7; ++i)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010060 if (flags & (0x10000 << i))
Bram Moolenaar7887d882005-07-01 22:33:52 +000010061 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010062 p = badword;
10063 }
10064
10065 ml_append(lnum, p, (colnr_T)0, FALSE);
10066}
10067
10068/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010069 * For ":spelldump": Find matching prefixes for "word". Prepend each to
10070 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010071 * Return the updated line number.
10072 */
10073 static linenr_T
10074apply_prefixes(slang, word, round, flags, startlnum)
10075 slang_T *slang;
10076 char_u *word; /* case-folded word */
10077 int round;
10078 int flags; /* flags with prefix ID */
10079 linenr_T startlnum;
10080{
10081 idx_T arridx[MAXWLEN];
10082 int curi[MAXWLEN];
10083 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000010084 char_u word_up[MAXWLEN];
10085 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010086 int c;
10087 char_u *byts;
10088 idx_T *idxs;
10089 linenr_T lnum = startlnum;
10090 int depth;
10091 int n;
10092 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010093 int i;
10094
Bram Moolenaar53805d12005-08-01 07:08:33 +000010095 /* if the word starts with a lower-case letter make the word with an
10096 * upper-case letter in word_up[]. */
10097 c = PTR2CHAR(word);
10098 if (SPELL_TOUPPER(c) != c)
10099 {
10100 onecap_copy(word, word_up, TRUE);
10101 has_word_up = TRUE;
10102 }
10103
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010104 byts = slang->sl_pbyts;
10105 idxs = slang->sl_pidxs;
10106 if (byts != NULL) /* array not is empty */
10107 {
10108 /*
10109 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010110 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010111 */
10112 depth = 0;
10113 arridx[0] = 0;
10114 curi[0] = 1;
10115 while (depth >= 0 && !got_int)
10116 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010117 n = arridx[depth];
10118 len = byts[n];
10119 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010120 {
10121 /* Done all bytes at this node, go up one level. */
10122 --depth;
10123 line_breakcheck();
10124 }
10125 else
10126 {
10127 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010128 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010129 ++curi[depth];
10130 c = byts[n];
10131 if (c == 0)
10132 {
10133 /* End of prefix, find out how many IDs there are. */
10134 for (i = 1; i < len; ++i)
10135 if (byts[n + i] != 0)
10136 break;
10137 curi[depth] += i - 1;
10138
Bram Moolenaar53805d12005-08-01 07:08:33 +000010139 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
10140 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010141 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010142 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010143 dump_word(prefix, round,
Bram Moolenaar53805d12005-08-01 07:08:33 +000010144 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010145 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010146 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000010147
10148 /* Check for prefix that matches the word when the
10149 * first letter is upper-case, but only if the prefix has
10150 * a condition. */
10151 if (has_word_up)
10152 {
10153 c = valid_word_prefix(i, n, flags, word_up, slang,
10154 TRUE);
10155 if (c != 0)
10156 {
10157 vim_strncpy(prefix + depth, word_up,
10158 MAXWLEN - depth - 1);
10159 dump_word(prefix, round,
10160 (c & WF_RAREPFX) ? (flags | WF_RARE)
10161 : flags, lnum++);
10162 }
10163 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010164 }
10165 else
10166 {
10167 /* Normal char, go one level deeper. */
10168 prefix[depth++] = c;
10169 arridx[depth] = idxs[n];
10170 curi[depth] = 1;
10171 }
10172 }
10173 }
10174 }
10175
10176 return lnum;
10177}
10178
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000010179#endif /* FEAT_SYN_HL */