blob: f93027d036dbf8a38d3c805be348aae08b568f13 [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 Moolenaarcf6bf392005-06-27 22:27:46 +000092 * <fileID> 10 bytes "VIMspell08"
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>
174 * | <flags> [<region>] [<prefixID>]
175 * | <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 Moolenaarcf6bf392005-06-27 22:27:46 +0000183 * For PREFIXTREE <prefixID> and
184 * <prefcondnr> follow for rare prefix.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000185 * BY_FLAGS2: End of word, <flags> and <flags2>
186 * follow. Not used in PREFIXTREE.
187 * BY_PFX_NC: For PREFIXTREE <prefixID> and
188 * <prefcondnr> follow for non-combining
189 * prefix.
190 * BY_PFX_RNC: For PREFIXTREE <prefixID> and
191 * <prefcondnr> follow for non-combining
192 * rare prefix.
193 * BY_INDEX: Child of sibling is shared, <nodeidx>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000194 * and <xbyte> follow.
195 *
196 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
197 *
198 * <xbyte> 1 byte byte value of the sibling.
199 *
200 * <flags> 1 byte bitmask of:
201 * WF_ALLCAP word must have only capitals
202 * WF_ONECAP first char of word must be capital
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000203 * WF_KEEPCAP keep-case word
204 * WF_FIXCAP keep-case word, all caps not allowed
Bram Moolenaar51485f02005-06-04 21:55:20 +0000205 * WF_RARE rare word
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000206 * WF_BANNED bad word
Bram Moolenaar51485f02005-06-04 21:55:20 +0000207 * WF_REGION <region> follows
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000208 * WF_PFX <prefixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000209 *
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000210 * <flags2> 1 byte Only used when there are postponed prefixes.
211 * Bitmask of:
212 * WF_HAS_AFF >> 8 word includes affix
213 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000214 * <region> 1 byte Bitmask for regions in which word is valid. When
215 * omitted it's valid in all regions.
216 * Lowest bit is for region 1.
217 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000218 * <prefixID> 1 byte ID of prefix that can be used with this word. For
219 * PREFIXTREE used for the required prefix ID.
220 *
221 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
222 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000223 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000224 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000225 */
226
Bram Moolenaare19defe2005-03-21 08:23:33 +0000227#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
228# include <io.h> /* for lseek(), must be before vim.h */
229#endif
230
231#include "vim.h"
232
233#if defined(FEAT_SYN_HL) || defined(PROTO)
234
235#ifdef HAVE_FCNTL_H
236# include <fcntl.h>
237#endif
238
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000239#define MAXWLEN 250 /* Assume max. word len is this many bytes.
240 Some places assume a word length fits in a
241 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000242
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000243/* Type used for indexes in the word tree need to be at least 3 bytes. If int
244 * is 8 bytes we could use something smaller, but what? */
245#if SIZEOF_INT > 2
246typedef int idx_T;
247#else
248typedef long idx_T;
249#endif
250
251/* Flags used for a word. Only the lowest byte can be used, the region byte
252 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000253#define WF_REGION 0x01 /* region byte follows */
254#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
255#define WF_ALLCAP 0x04 /* word must be all capitals */
256#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000257#define WF_BANNED 0x10 /* bad word */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000258#define WF_PFX 0x20 /* prefix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000259#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000260#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000261
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000262/* for <flags2>, shifted up one byte to be used in wn_flags */
263#define WF_HAS_AFF 0x0100 /* word includes affix */
264
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000265#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000266
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000267/* flags for postponed prefixes. Must be above prefixID (one byte)
268 * and prefcondnr (two bytes). */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000269#define WF_RAREPFX 0x1000000 /* in sl_pidxs: flag for rare postponed
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000270 * prefix */
271#define WF_PFX_NC 0x2000000 /* in sl_pidxs: flag for non-combining
272 * postponed prefix */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000273
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000274/* Special byte values for <byte>. Some are only used in the tree for
275 * postponed prefixes, some only in the other trees. This is a bit messy... */
276#define BY_NOFLAGS 0 /* end of word without flags or region; for
277 * postponed prefix: not rare, combining */
278#define BY_FLAGS 1 /* end of word, flags byte follows; for
279 * postponed prefix: rare, combining */
280#define BY_INDEX 2 /* child is shared, index follows */
281#define BY_FLAGS2 3 /* end of word, two flags bytes follow; never
282 * used in prefix tree */
283#define BY_PFX_NC 3 /* postponed prefix: not rare, not combining */
284#define BY_PFX_RNC 4 /* postponed prefix: rare, not combining */
285#define BY_SPECIAL BY_PFX_RNC /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000286
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000287/* Info from "REP" and "SAL" entries in ".aff" file used in si_rep, sl_rep,
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000288 * and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000289 * One replacement: from "ft_from" to "ft_to". */
290typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000291{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000292 char_u *ft_from;
293 char_u *ft_to;
294} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000295
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000296/* Info from "SAL" entries in ".aff" file used in sl_sal.
297 * The info is split for quick processing by spell_soundfold().
298 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
299typedef struct salitem_S
300{
301 char_u *sm_lead; /* leading letters */
302 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000303 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000304 char_u *sm_rules; /* rules like ^, $, priority */
305 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000306#ifdef FEAT_MBYTE
307 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000308 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000309 int *sm_to_w; /* wide character copy of "sm_to" */
310#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000311} salitem_T;
312
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000313#ifdef FEAT_MBYTE
314typedef int salfirst_T;
315#else
316typedef short salfirst_T;
317#endif
318
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000319/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000320 * Structure used to store words and other info for one language, loaded from
321 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000322 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
323 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
324 *
325 * The "byts" array stores the possible bytes in each tree node, preceded by
326 * the number of possible bytes, sorted on byte value:
327 * <len> <byte1> <byte2> ...
328 * The "idxs" array stores the index of the child node corresponding to the
329 * byte in "byts".
330 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000331 * the flags, region mask and prefixID for the word. There may be several
332 * zeros in sequence for alternative flag/region combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000333 */
334typedef struct slang_S slang_T;
335struct slang_S
336{
337 slang_T *sl_next; /* next language */
338 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000339 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000340 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000341
Bram Moolenaar51485f02005-06-04 21:55:20 +0000342 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000343 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000344 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000345 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000346 char_u *sl_pbyts; /* prefix tree word bytes */
347 idx_T *sl_pidxs; /* prefix tree word indexes */
348
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000349 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000350
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000351 char_u *sl_midword; /* MIDWORD string or NULL */
352
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000353 int sl_prefixcnt; /* number of items in "sl_prefprog" */
354 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
355
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000356 garray_T sl_rep; /* list of fromto_T entries from REP lines */
357 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
358 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000359 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000360 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000361 there is none */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000362 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
363 * "sl_sal_first" maps chars, when has_mbyte
364 * "sl_sal" is a list of wide char lists. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000365 int sl_followup; /* SAL followup */
366 int sl_collapse; /* SAL collapse_result */
367 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaarea424162005-06-16 21:51:00 +0000368 int sl_has_map; /* TRUE if there is a MAP line */
369#ifdef FEAT_MBYTE
370 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
371 int sl_map_array[256]; /* MAP for first 256 chars */
372#else
373 char_u sl_map_array[256]; /* MAP for first 256 chars */
374#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000375};
376
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000377/* First language that is loaded, start of the linked list of loaded
378 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000379static slang_T *first_lang = NULL;
380
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000381/* Flags used in .spl file for soundsalike flags. */
382#define SAL_F0LLOWUP 1
383#define SAL_COLLAPSE 2
384#define SAL_REM_ACCENTS 4
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000385#define SAL_SOFO 8 /* SOFOFROM and SOFOTO instead of SAL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000386
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000387/*
388 * Structure used in "b_langp", filled from 'spelllang'.
389 */
390typedef struct langp_S
391{
392 slang_T *lp_slang; /* info for this language (NULL for last one) */
393 int lp_region; /* bitmask for region or REGION_ALL */
394} langp_T;
395
396#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
397
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000398#define REGION_ALL 0xff /* word valid in all regions */
399
400/* Result values. Lower number is accepted over higher one. */
401#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000402#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000403#define SP_RARE 1
404#define SP_LOCAL 2
405#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000406
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000407#define VIMSPELLMAGIC "VIMspell08" /* string at start of Vim spell file */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000408#define VIMSPELLMAGICL 10
409
Bram Moolenaar7887d882005-07-01 22:33:52 +0000410/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000411static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000412
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000413/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000414 * Information used when looking for suggestions.
415 */
416typedef struct suginfo_S
417{
418 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000419 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000420 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000421 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000422 char_u *su_badptr; /* start of bad word in line */
423 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000424 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000425 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
426 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
427 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000428} suginfo_T;
429
430/* One word suggestion. Used in "si_ga". */
431typedef struct suggest_S
432{
433 char_u *st_word; /* suggested word, allocated string */
434 int st_orglen; /* length of replaced text */
435 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000436 int st_altscore; /* used when st_score compares equal */
437 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000438 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000439} suggest_T;
440
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000441#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000442
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000443/* Number of suggestions kept when cleaning up. When rescore_suggestions() is
444 * called the score may change, thus we need to keep more than what is
445 * displayed. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000446#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 50 ? 50 : (su)->su_maxcount)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000447
448/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
449 * of suggestions that are not going to be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000450#define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000451
452/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000453#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000454#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000455#define SCORE_REGION 70 /* word is for different region */
456#define SCORE_RARE 180 /* rare word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000457#define SCORE_SWAP 90 /* swap two characters */
458#define SCORE_SWAP3 110 /* swap two characters in three */
459#define SCORE_REP 87 /* REP replacement */
460#define SCORE_SUBST 93 /* substitute a character */
461#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000462#define SCORE_DEL 94 /* delete a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000463#define SCORE_DELDUP 64 /* delete a duplicated character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000464#define SCORE_INS 96 /* insert a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000465#define SCORE_INSDUP 66 /* insert a duplicate character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000466#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000467
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000468#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000469#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
470 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000471
472#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000473#define SCORE_MAXMAX 999999 /* accept any score */
474
475/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000476 * Structure to store info for word matching.
477 */
478typedef struct matchinf_S
479{
480 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000481
482 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000483 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000484 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000485 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000486 char_u *mi_cend; /* char after what was used for
487 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000488
489 /* case-folded text */
490 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000491 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000492
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000493 /* for when checking word after a prefix */
494 int mi_prefarridx; /* index in sl_pidxs with list of
495 prefixID/condition */
496 int mi_prefcnt; /* number of entries at mi_prefarridx */
497 int mi_prefixlen; /* byte length of prefix */
498
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000499 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000500 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000501 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000502 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000503} matchinf_T;
504
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000505/*
506 * The tables used for recognizing word characters according to spelling.
507 * These are only used for the first 256 characters of 'encoding'.
508 */
509typedef struct spelltab_S
510{
511 char_u st_isw[256]; /* flags: is word char */
512 char_u st_isu[256]; /* flags: is uppercase char */
513 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000514 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000515} spelltab_T;
516
517static spelltab_T spelltab;
518static int did_set_spelltab;
519
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000520#define CF_WORD 0x01
521#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000522
523static void clear_spell_chartab __ARGS((spelltab_T *sp));
524static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000525static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
526static int spell_iswordp_nmw __ARGS((char_u *p));
527#ifdef FEAT_MBYTE
528static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
529#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000530static void write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000531
532/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000533 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000534 */
535typedef enum
536{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000537 STATE_START = 0, /* At start of node check for NUL bytes (goodword
538 * ends); if badword ends there is a match, otherwise
539 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000540 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000541 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000542 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
543 STATE_PLAIN, /* Use each byte of the node. */
544 STATE_DEL, /* Delete a byte from the bad word. */
545 STATE_INS, /* Insert a byte in the bad word. */
546 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000547 STATE_UNSWAP, /* Undo swap two characters. */
548 STATE_SWAP3, /* Swap two characters over three. */
549 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000550 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000551 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000552 STATE_REP_INI, /* Prepare for using REP items. */
553 STATE_REP, /* Use matching REP items from the .aff file. */
554 STATE_REP_UNDO, /* Undo a REP item replacement. */
555 STATE_FINAL /* End of this node. */
556} state_T;
557
558/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000559 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000560 */
561typedef struct trystate_S
562{
Bram Moolenaarea424162005-06-16 21:51:00 +0000563 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000564 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000565 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000566 short ts_curi; /* index in list of child nodes */
567 char_u ts_fidx; /* index in fword[], case-folded bad word */
568 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
569 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000570 char_u ts_prefixdepth; /* stack depth for end of prefix or PREFIXTREE
571 * or NOPREFIX */
Bram Moolenaarea424162005-06-16 21:51:00 +0000572#ifdef FEAT_MBYTE
573 char_u ts_tcharlen; /* number of bytes in tword character */
574 char_u ts_tcharidx; /* current byte index in tword character */
575 char_u ts_isdiff; /* DIFF_ values */
576 char_u ts_fcharstart; /* index in fword where badword char started */
577#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000578 char_u ts_save_prewordlen; /* saved "prewordlen" */
Bram Moolenaarea424162005-06-16 21:51:00 +0000579 char_u ts_save_splitoff; /* su_splitoff saved here */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000580 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000581} trystate_T;
582
Bram Moolenaarea424162005-06-16 21:51:00 +0000583/* values for ts_isdiff */
584#define DIFF_NONE 0 /* no different byte (yet) */
585#define DIFF_YES 1 /* different byte found */
586#define DIFF_INSERT 2 /* inserting character */
587
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000588/* special values ts_prefixdepth */
589#define PREFIXTREE 0xfe /* walking through the prefix tree */
590#define NOPREFIX 0xff /* not using prefixes */
591
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000592/* mode values for find_word */
593#define FIND_FOLDWORD 0 /* find word case-folded */
594#define FIND_KEEPWORD 1 /* find keep-case word */
595#define FIND_PREFIX 2 /* find word after prefix */
596
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000597static slang_T *slang_alloc __ARGS((char_u *lang));
598static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000599static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000600static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000601static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000602static void find_prefix __ARGS((matchinf_T *mip));
603static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000604static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000605static int no_spell_checking __ARGS((void));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000606static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000607static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000608static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000609static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000610static 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 +0000611static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000612static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
613static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000614#ifdef FEAT_MBYTE
615static int *mb_str2wide __ARGS((char_u *s));
616#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000617static 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 +0000618static void clear_midword __ARGS((buf_T *buf));
619static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000620static int find_region __ARGS((char_u *rp, char_u *region));
621static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000622static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000623static int set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000624static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
625static void write_spell_chartab __ARGS((FILE *fd));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000626static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +0000627static 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 +0000628#ifdef FEAT_EVAL
629static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
630#endif
631static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
632static void spell_suggest_intern __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000633static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000634static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000635static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000636static void suggest_try_special __ARGS((suginfo_T *su));
637static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000638static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
639static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000640static void score_comp_sal __ARGS((suginfo_T *su));
641static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000642static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000643static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000644static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000645static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000646static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000647static 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 +0000648static void add_banned __ARGS((suginfo_T *su, char_u *word));
649static int was_banned __ARGS((suginfo_T *su, char_u *word));
650static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000651static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000652static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000653static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
654static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
655static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000656#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000657static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000658#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000659static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000660static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000661static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum));
662static 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 +0000663
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000664/*
665 * Use our own character-case definitions, because the current locale may
666 * differ from what the .spl file uses.
667 * These must not be called with negative number!
668 */
669#ifndef FEAT_MBYTE
670/* Non-multi-byte implementation. */
671# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
672# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
673# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
674#else
675/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
676 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
677 * the "w" library function for characters above 255 if available. */
678# ifdef HAVE_TOWLOWER
679# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
680 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
681# else
682# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
683 : (c) < 256 ? spelltab.st_fold[c] : (c))
684# endif
685
686# ifdef HAVE_TOWUPPER
687# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
688 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
689# else
690# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
691 : (c) < 256 ? spelltab.st_upper[c] : (c))
692# endif
693
694# ifdef HAVE_ISWUPPER
695# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
696 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
697# else
698# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
699 : (c) < 256 ? spelltab.st_isu[c] : (c))
700# endif
701#endif
702
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000703
704static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000705static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000706
707/*
708 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000709 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000710 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
711 * or when it's OK it remains unchanged.
712 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000713 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000714 * "capcol" is used to check for a Capitalised word after the end of a
715 * sentence. If it's zero then perform the check. Return the column where to
716 * check next, or -1 when no sentence end was found. If it's NULL then don't
717 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000718 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000719 * Returns the length of the word in bytes, also when it's OK, so that the
720 * caller can skip over the word.
721 */
722 int
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000723spell_check(wp, ptr, attrp, capcol)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000724 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000725 char_u *ptr;
726 int *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000727 int *capcol; /* column to check for Capital */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000728{
729 matchinf_T mi; /* Most things are put in "mi" so that it can
730 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000731 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000732 int c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000733
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000734 /* A word never starts at a space or a control character. Return quickly
735 * then, skipping over the character. */
736 if (*ptr <= ' ')
737 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000738
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000739 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar0c405862005-06-22 22:26:26 +0000740 * 0X99FF. But when a word character follows do check spelling to find
741 * "3GPP". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000742 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000743 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000744 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
745 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000746 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000747 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000748 mi.mi_end = skipdigits(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000749 nrlen = mi.mi_end - ptr;
750 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000751 if (!spell_iswordp(mi.mi_end, wp->w_buffer))
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000752 return (int)(mi.mi_end - ptr);
Bram Moolenaar0c405862005-06-22 22:26:26 +0000753
754 /* Try including the digits in the word. */
755 mi.mi_fend = ptr + nrlen;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000756 }
Bram Moolenaar0c405862005-06-22 22:26:26 +0000757 else
758 mi.mi_fend = ptr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000759
Bram Moolenaar0c405862005-06-22 22:26:26 +0000760 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000761 mi.mi_word = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000762 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000763 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000764 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000765 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000766 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000767 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000768
769 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
770 {
771 /* Check word starting with capital letter. */
772#ifdef FEAT_MBYTE
773 c = mb_ptr2char(ptr);
774#else
775 c = *ptr;
776#endif
777 if (!SPELL_ISUPPER(c))
778 {
779 *attrp = highlight_attr[HLF_SPC];
780 return (int)(mi.mi_fend - ptr);
781 }
782 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000783 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000784 if (capcol != NULL)
785 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000786
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000787 /* We always use the characters up to the next non-word character,
788 * also for bad words. */
789 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000790
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000791 /* Check caps type later. */
792 mi.mi_capflags = 0;
793 mi.mi_cend = NULL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000794 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000795
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000796 /* Include one non-word character so that we can check for the
797 * word end. */
798 if (*mi.mi_fend != NUL)
799 mb_ptr_adv(mi.mi_fend);
800
801 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
802 MAXWLEN + 1);
803 mi.mi_fwordlen = STRLEN(mi.mi_fword);
804
805 /* The word is bad unless we recognize it. */
806 mi.mi_result = SP_BAD;
807
808 /*
809 * Loop over the languages specified in 'spelllang'.
810 * We check them all, because a matching word may be longer than an
811 * already found matching word.
812 */
813 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
814 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
815 {
816 /* Check for a matching word in case-folded words. */
817 find_word(&mi, FIND_FOLDWORD);
818
819 /* Check for a matching word in keep-case words. */
820 find_word(&mi, FIND_KEEPWORD);
821
822 /* Check for matching prefixes. */
823 find_prefix(&mi);
824 }
825
826 if (mi.mi_result != SP_OK)
827 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000828 /* If we found a number skip over it. Allows for "42nd". Do flag
829 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000830 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000831 {
832 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
833 return nrlen;
834 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000835
836 /* When we are at a non-word character there is no error, just
837 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000838 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000839 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000840 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
841 {
842 regmatch_T regmatch;
843
844 /* Check for end of sentence. */
845 regmatch.regprog = wp->w_buffer->b_cap_prog;
846 regmatch.rm_ic = FALSE;
847 if (vim_regexec(&regmatch, ptr, 0))
848 *capcol = (int)(regmatch.endp[0] - ptr);
849 }
850
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000851#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000852 if (has_mbyte)
853 return mb_ptr2len_check(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000854#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000855 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000856 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000857
858 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
859 *attrp = highlight_attr[HLF_SPB];
860 else if (mi.mi_result == SP_RARE)
861 *attrp = highlight_attr[HLF_SPR];
862 else
863 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000864 }
865
Bram Moolenaar51485f02005-06-04 21:55:20 +0000866 return (int)(mi.mi_end - ptr);
867}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000868
Bram Moolenaar51485f02005-06-04 21:55:20 +0000869/*
870 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000871 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
872 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
873 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
874 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000875 *
876 * For a match mip->mi_result is updated.
877 */
878 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000879find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000880 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000881 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000882{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000883 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000884 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000885 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000886 int endidxcnt = 0;
887 int len;
888 int wlen = 0;
889 int flen;
890 int c;
891 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000892 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000893#ifdef FEAT_MBYTE
894 char_u *s;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000895 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000896#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000897 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000898 slang_T *slang = mip->mi_lp->lp_slang;
899 unsigned flags;
900 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000901 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000902
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000903 if (mode == FIND_KEEPWORD)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000904 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000905 /* Check for word with matching case in keep-case tree. */
906 ptr = mip->mi_word;
907 flen = 9999; /* no case folding, always enough bytes */
908 byts = slang->sl_kbyts;
909 idxs = slang->sl_kidxs;
910 }
911 else
912 {
913 /* Check for case-folded in case-folded tree. */
914 ptr = mip->mi_fword;
915 flen = mip->mi_fwordlen; /* available case-folded bytes */
916 byts = slang->sl_fbyts;
917 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000918
919 if (mode == FIND_PREFIX)
920 {
921 /* Skip over the prefix. */
922 wlen = mip->mi_prefixlen;
923 flen -= mip->mi_prefixlen;
924 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000925 }
926
Bram Moolenaar51485f02005-06-04 21:55:20 +0000927 if (byts == NULL)
928 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000929
Bram Moolenaar51485f02005-06-04 21:55:20 +0000930 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000931 * Repeat advancing in the tree until:
932 * - there is a byte that doesn't match,
933 * - we reach the end of the tree,
934 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000935 */
936 for (;;)
937 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000938 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000939 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000940
941 len = byts[arridx++];
942
943 /* If the first possible byte is a zero the word could end here.
944 * Remember this index, we first check for the longest word. */
945 if (byts[arridx] == 0)
946 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000947 if (endidxcnt == MAXWLEN)
948 {
949 /* Must be a corrupted spell file. */
950 EMSG(_(e_format));
951 return;
952 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000953 endlen[endidxcnt] = wlen;
954 endidx[endidxcnt++] = arridx++;
955 --len;
956
957 /* Skip over the zeros, there can be several flag/region
958 * combinations. */
959 while (len > 0 && byts[arridx] == 0)
960 {
961 ++arridx;
962 --len;
963 }
964 if (len == 0)
965 break; /* no children, word must end here */
966 }
967
968 /* Stop looking at end of the line. */
969 if (ptr[wlen] == NUL)
970 break;
971
972 /* Perform a binary search in the list of accepted bytes. */
973 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +0000974 if (c == TAB) /* <Tab> is handled like <Space> */
975 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +0000976 lo = arridx;
977 hi = arridx + len - 1;
978 while (lo < hi)
979 {
980 m = (lo + hi) / 2;
981 if (byts[m] > c)
982 hi = m - 1;
983 else if (byts[m] < c)
984 lo = m + 1;
985 else
986 {
987 lo = hi = m;
988 break;
989 }
990 }
991
992 /* Stop if there is no matching byte. */
993 if (hi < lo || byts[lo] != c)
994 break;
995
996 /* Continue at the child (if there is one). */
997 arridx = idxs[lo];
998 ++wlen;
999 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001000
1001 /* One space in the good word may stand for several spaces in the
1002 * checked word. */
1003 if (c == ' ')
1004 {
1005 for (;;)
1006 {
1007 if (flen <= 0 && *mip->mi_fend != NUL)
1008 flen = fold_more(mip);
1009 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1010 break;
1011 ++wlen;
1012 --flen;
1013 }
1014 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001015 }
1016
1017 /*
1018 * Verify that one of the possible endings is valid. Try the longest
1019 * first.
1020 */
1021 while (endidxcnt > 0)
1022 {
1023 --endidxcnt;
1024 arridx = endidx[endidxcnt];
1025 wlen = endlen[endidxcnt];
1026
1027#ifdef FEAT_MBYTE
1028 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1029 continue; /* not at first byte of character */
1030#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001031 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001032 continue; /* next char is a word character */
1033
1034#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001035 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001036 {
1037 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001038 * when folding case. This can be slow, take a shortcut when the
1039 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001040 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001041 if (STRNCMP(ptr, p, wlen) != 0)
1042 {
1043 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1044 mb_ptr_adv(p);
1045 wlen = p - mip->mi_word;
1046 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001047 }
1048#endif
1049
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001050 /* Check flags and region. For FIND_PREFIX check the condition and
1051 * prefix ID.
1052 * Repeat this if there are more flags/region alternatives until there
1053 * is a match. */
1054 res = SP_BAD;
1055 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1056 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001057 {
1058 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001059
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001060 /* For the fold-case tree check that the case of the checked word
1061 * matches with what the word in the tree requires.
1062 * For keep-case tree the case is always right. For prefixes we
1063 * don't bother to check. */
1064 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001065 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001066 if (mip->mi_cend != mip->mi_word + wlen)
1067 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001068 /* mi_capflags was set for a different word length, need
1069 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001070 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001071 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001072 }
1073
Bram Moolenaar0c405862005-06-22 22:26:26 +00001074 if (mip->mi_capflags == WF_KEEPCAP
1075 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001076 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001077 }
1078
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001079 /* When mode is FIND_PREFIX the word must support the prefix:
1080 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001081 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001082 if (mode == FIND_PREFIX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001083 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001084 /* The prefix ID is stored two bytes above the flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001085 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001086 flags,
1087 mip->mi_fword + mip->mi_prefixlen, slang);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001088 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001089 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001090
1091 /* Use the WF_RARE flag for a rare prefix. */
1092 if (c & WF_RAREPFX)
1093 flags |= WF_RARE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001094 }
1095
1096 if (flags & WF_BANNED)
1097 res = SP_BANNED;
1098 else if (flags & WF_REGION)
1099 {
1100 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001101 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001102 res = SP_OK;
1103 else
1104 res = SP_LOCAL;
1105 }
1106 else if (flags & WF_RARE)
1107 res = SP_RARE;
1108 else
1109 res = SP_OK;
1110
1111 /* Always use the longest match and the best result. */
1112 if (mip->mi_result > res)
1113 {
1114 mip->mi_result = res;
1115 mip->mi_end = mip->mi_word + wlen;
1116 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001117 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001118 mip->mi_end = mip->mi_word + wlen;
1119
1120 if (res == SP_OK)
1121 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001122 }
1123
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001124 if (res == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001125 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001126 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001127}
1128
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001129/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001130 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1131 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001132 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001133 */
1134 static int
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001135valid_word_prefix(totprefcnt, arridx, flags, word, slang)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001136 int totprefcnt; /* nr of prefix IDs */
1137 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001138 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001139 char_u *word;
1140 slang_T *slang;
1141{
1142 int prefcnt;
1143 int pidx;
1144 regprog_T *rp;
1145 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001146 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001147
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001148 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001149 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1150 {
1151 pidx = slang->sl_pidxs[arridx + prefcnt];
1152
1153 /* Check the prefix ID. */
1154 if (prefid != (pidx & 0xff))
1155 continue;
1156
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001157 /* Check if the prefix doesn't combine and the word already has a
1158 * suffix. */
1159 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1160 continue;
1161
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001162 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001163 * stored in the two bytes above the prefix ID byte. */
1164 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001165 if (rp != NULL)
1166 {
1167 regmatch.regprog = rp;
1168 regmatch.rm_ic = FALSE;
1169 if (!vim_regexec(&regmatch, word, 0))
1170 continue;
1171 }
1172
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001173 /* It's a match! Return the WF_RAREPFX flag. */
1174 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001175 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001176 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001177}
1178
1179/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001180 * Check if the word at "mip->mi_word" has a matching prefix.
1181 * If it does, then check the following word.
1182 *
1183 * For a match mip->mi_result is updated.
1184 */
1185 static void
1186find_prefix(mip)
1187 matchinf_T *mip;
1188{
1189 idx_T arridx = 0;
1190 int len;
1191 int wlen = 0;
1192 int flen;
1193 int c;
1194 char_u *ptr;
1195 idx_T lo, hi, m;
1196 slang_T *slang = mip->mi_lp->lp_slang;
1197 char_u *byts;
1198 idx_T *idxs;
1199
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001200 byts = slang->sl_pbyts;
1201 if (byts == NULL)
1202 return; /* array is empty */
1203
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001204 /* We use the case-folded word here, since prefixes are always
1205 * case-folded. */
1206 ptr = mip->mi_fword;
1207 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001208 idxs = slang->sl_pidxs;
1209
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001210 /*
1211 * Repeat advancing in the tree until:
1212 * - there is a byte that doesn't match,
1213 * - we reach the end of the tree,
1214 * - or we reach the end of the line.
1215 */
1216 for (;;)
1217 {
1218 if (flen == 0 && *mip->mi_fend != NUL)
1219 flen = fold_more(mip);
1220
1221 len = byts[arridx++];
1222
1223 /* If the first possible byte is a zero the prefix could end here.
1224 * Check if the following word matches and supports the prefix. */
1225 if (byts[arridx] == 0)
1226 {
1227 /* There can be several prefixes with different conditions. We
1228 * try them all, since we don't know which one will give the
1229 * longest match. The word is the same each time, pass the list
1230 * of possible prefixes to find_word(). */
1231 mip->mi_prefarridx = arridx;
1232 mip->mi_prefcnt = len;
1233 while (len > 0 && byts[arridx] == 0)
1234 {
1235 ++arridx;
1236 --len;
1237 }
1238 mip->mi_prefcnt -= len;
1239
1240 /* Find the word that comes after the prefix. */
1241 mip->mi_prefixlen = wlen;
1242 find_word(mip, FIND_PREFIX);
1243
1244
1245 if (len == 0)
1246 break; /* no children, word must end here */
1247 }
1248
1249 /* Stop looking at end of the line. */
1250 if (ptr[wlen] == NUL)
1251 break;
1252
1253 /* Perform a binary search in the list of accepted bytes. */
1254 c = ptr[wlen];
1255 lo = arridx;
1256 hi = arridx + len - 1;
1257 while (lo < hi)
1258 {
1259 m = (lo + hi) / 2;
1260 if (byts[m] > c)
1261 hi = m - 1;
1262 else if (byts[m] < c)
1263 lo = m + 1;
1264 else
1265 {
1266 lo = hi = m;
1267 break;
1268 }
1269 }
1270
1271 /* Stop if there is no matching byte. */
1272 if (hi < lo || byts[lo] != c)
1273 break;
1274
1275 /* Continue at the child (if there is one). */
1276 arridx = idxs[lo];
1277 ++wlen;
1278 --flen;
1279 }
1280}
1281
1282/*
1283 * Need to fold at least one more character. Do until next non-word character
1284 * for efficiency.
1285 * Return the length of the folded chars in bytes.
1286 */
1287 static int
1288fold_more(mip)
1289 matchinf_T *mip;
1290{
1291 int flen;
1292 char_u *p;
1293
1294 p = mip->mi_fend;
1295 do
1296 {
1297 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001298 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001299
1300 /* Include the non-word character so that we can check for the
1301 * word end. */
1302 if (*mip->mi_fend != NUL)
1303 mb_ptr_adv(mip->mi_fend);
1304
1305 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1306 mip->mi_fword + mip->mi_fwordlen,
1307 MAXWLEN - mip->mi_fwordlen);
1308 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1309 mip->mi_fwordlen += flen;
1310 return flen;
1311}
1312
1313/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001314 * Check case flags for a word. Return TRUE if the word has the requested
1315 * case.
1316 */
1317 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001318spell_valid_case(wordflags, treeflags)
1319 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001320 int treeflags; /* flags for the word in the spell tree */
1321{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001322 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001323 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001324 && ((treeflags & WF_ONECAP) == 0 || wordflags == WF_ONECAP)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001325}
1326
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001327/*
1328 * Return TRUE if spell checking is not enabled.
1329 */
1330 static int
1331no_spell_checking()
1332{
1333 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
1334 {
1335 EMSG(_("E756: Spell checking is not enabled"));
1336 return TRUE;
1337 }
1338 return FALSE;
1339}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001340
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001341/*
1342 * Move to next spell error.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001343 * "curline" is TRUE for "z?": find word under/after cursor in the same line.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001344 * Return OK if found, FAIL otherwise.
1345 */
1346 int
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001347spell_move_to(dir, allwords, curline)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001348 int dir; /* FORWARD or BACKWARD */
1349 int allwords; /* TRUE for "[s" and "]s" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001350 int curline;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001351{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001352 linenr_T lnum;
1353 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001354 char_u *line;
1355 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001356 char_u *endp;
1357 int attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001358 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001359 int has_syntax = syntax_present(curbuf);
1360 int col;
1361 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001362 char_u *buf = NULL;
1363 int buflen = 0;
1364 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001365 int capcol = -1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001366
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001367 if (no_spell_checking())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001368 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001369
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001370 /*
1371 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001372 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001373 *
1374 * When searching backwards, we continue in the line to find the last
1375 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001376 *
1377 * We concatenate the start of the next line, so that wrapped words work
1378 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1379 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001380 */
1381 lnum = curwin->w_cursor.lnum;
1382 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001383
1384 while (!got_int)
1385 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001386 line = ml_get(lnum);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001387
Bram Moolenaar0c405862005-06-22 22:26:26 +00001388 len = STRLEN(line);
1389 if (buflen < len + MAXWLEN + 2)
1390 {
1391 vim_free(buf);
1392 buflen = len + MAXWLEN + 2;
1393 buf = alloc(buflen);
1394 if (buf == NULL)
1395 break;
1396 }
1397
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001398 /* In first line check first word for Capital. */
1399 if (lnum == 1)
1400 capcol = 0;
1401
1402 /* For checking first word with a capital skip white space. */
1403 if (capcol == 0)
1404 capcol = skipwhite(line) - line;
1405
Bram Moolenaar0c405862005-06-22 22:26:26 +00001406 /* Copy the line into "buf" and append the start of the next line if
1407 * possible. */
1408 STRCPY(buf, line);
1409 if (lnum < curbuf->b_ml.ml_line_count)
1410 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1411
1412 p = buf + skip;
1413 endp = buf + len;
1414 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001415 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001416 /* When searching backward don't search after the cursor. */
1417 if (dir == BACKWARD
1418 && lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001419 && (colnr_T)(p - buf) >= curwin->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001420 break;
1421
1422 /* start of word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00001423 attr = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001424 len = spell_check(curwin, p, &attr, &capcol);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001425
1426 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001427 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001428 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001429 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001430 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001431 /* When searching forward only accept a bad word after
1432 * the cursor. */
1433 if (dir == BACKWARD
1434 || lnum > curwin->w_cursor.lnum
1435 || (lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001436 && (colnr_T)(curline ? p - buf + len
1437 : p - buf)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001438 > curwin->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001439 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001440 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001441 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001442 col = p - buf;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001443 (void)syn_get_id(lnum, (colnr_T)col,
1444 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001445 }
1446 else
1447 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001448
Bram Moolenaar51485f02005-06-04 21:55:20 +00001449 if (can_spell)
1450 {
1451 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001452 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001453#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001454 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001455#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001456 if (dir == FORWARD)
1457 {
1458 /* No need to search further. */
1459 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001460 vim_free(buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001461 return OK;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001462 }
1463 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001464 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001465 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001466 }
1467
Bram Moolenaar51485f02005-06-04 21:55:20 +00001468 /* advance to character after the word */
1469 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001470 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001471 }
1472
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001473 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001474 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001475
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001476 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001477 if (dir == BACKWARD)
1478 {
1479 if (found_pos.lnum != 0)
1480 {
1481 /* Use the last match in the line. */
1482 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001483 vim_free(buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001484 return OK;
1485 }
1486 if (lnum == 1)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001487 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001488 --lnum;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001489 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001490 }
1491 else
1492 {
1493 if (lnum == curbuf->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001494 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001495 ++lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001496
1497 /* Skip the characters at the start of the next line that were
1498 * included in a match crossing line boundaries. */
1499 if (attr == 0)
1500 skip = p - endp;
1501 else
1502 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001503
1504 /* Capscol skips over the inserted space. */
1505 --capcol;
1506
1507 /* But after empty line check first word in next line */
1508 if (*skipwhite(line) == NUL)
1509 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001510 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001511
1512 line_breakcheck();
1513 }
1514
Bram Moolenaar0c405862005-06-22 22:26:26 +00001515 vim_free(buf);
1516 return FAIL;
1517}
1518
1519/*
1520 * For spell checking: concatenate the start of the following line "line" into
1521 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
1522 */
1523 void
1524spell_cat_line(buf, line, maxlen)
1525 char_u *buf;
1526 char_u *line;
1527 int maxlen;
1528{
1529 char_u *p;
1530 int n;
1531
1532 p = skipwhite(line);
1533 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1534 p = skipwhite(p + 1);
1535
1536 if (*p != NUL)
1537 {
1538 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001539 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001540 n = p - line;
1541 if (n >= maxlen)
1542 n = maxlen - 1;
1543 vim_memset(buf + 1, ' ', n);
1544 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001545}
1546
1547/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001548 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001549 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001550 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001551 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001552spell_load_lang(lang)
1553 char_u *lang;
1554{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001555 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001556 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001557 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001558
Bram Moolenaarb765d632005-06-07 21:00:02 +00001559 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001560 * It's truncated when an error is detected. */
1561 STRCPY(langcp, lang);
1562
Bram Moolenaarb765d632005-06-07 21:00:02 +00001563 /*
1564 * Find the first spell file for "lang" in 'runtimepath' and load it.
1565 */
1566 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
1567 "spell/%s.%s.spl", lang, spell_enc());
1568 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001569
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001570 if (r == FAIL && *langcp != NUL)
1571 {
1572 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001573 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001574 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001575 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001576 }
1577
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001578 if (r == FAIL)
1579 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
1580 fname_enc + 6);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001581 else if (*langcp != NUL)
1582 {
1583 /* Load all the additions. */
1584 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
1585 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
1586 }
1587}
1588
1589/*
1590 * Return the encoding used for spell checking: Use 'encoding', except that we
1591 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1592 */
1593 static char_u *
1594spell_enc()
1595{
1596
1597#ifdef FEAT_MBYTE
1598 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1599 return p_enc;
1600#endif
1601 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001602}
1603
1604/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001605 * Get the name of the .spl file for the internal wordlist into
1606 * "fname[MAXPATHL]".
1607 */
1608 static void
1609int_wordlist_spl(fname)
1610 char_u *fname;
1611{
1612 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
1613 int_wordlist, spell_enc());
1614}
1615
1616/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001617 * Allocate a new slang_T.
1618 * Caller must fill "sl_next".
1619 */
1620 static slang_T *
1621slang_alloc(lang)
1622 char_u *lang;
1623{
1624 slang_T *lp;
1625
Bram Moolenaar51485f02005-06-04 21:55:20 +00001626 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001627 if (lp != NULL)
1628 {
1629 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001630 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001631 }
1632 return lp;
1633}
1634
1635/*
1636 * Free the contents of an slang_T and the structure itself.
1637 */
1638 static void
1639slang_free(lp)
1640 slang_T *lp;
1641{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001642 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001643 vim_free(lp->sl_fname);
1644 slang_clear(lp);
1645 vim_free(lp);
1646}
1647
1648/*
1649 * Clear an slang_T so that the file can be reloaded.
1650 */
1651 static void
1652slang_clear(lp)
1653 slang_T *lp;
1654{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001655 garray_T *gap;
1656 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001657 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001658 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001659
Bram Moolenaar51485f02005-06-04 21:55:20 +00001660 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001661 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001662 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001663 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001664 vim_free(lp->sl_pbyts);
1665 lp->sl_pbyts = NULL;
1666
Bram Moolenaar51485f02005-06-04 21:55:20 +00001667 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001668 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001669 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001670 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001671 vim_free(lp->sl_pidxs);
1672 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001673
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001674 gap = &lp->sl_rep;
1675 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001676 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001677 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1678 vim_free(ftp->ft_from);
1679 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001680 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001681 ga_clear(gap);
1682
1683 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001684 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001685 {
1686 /* "ga_len" is set to 1 without adding an item for latin1 */
1687 if (gap->ga_data != NULL)
1688 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
1689 for (i = 0; i < gap->ga_len; ++i)
1690 vim_free(((int **)gap->ga_data)[i]);
1691 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001692 else
1693 /* SAL items: free salitem_T items */
1694 while (gap->ga_len > 0)
1695 {
1696 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1697 vim_free(smp->sm_lead);
1698 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
1699 vim_free(smp->sm_to);
1700#ifdef FEAT_MBYTE
1701 vim_free(smp->sm_lead_w);
1702 vim_free(smp->sm_oneof_w);
1703 vim_free(smp->sm_to_w);
1704#endif
1705 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001706 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001707
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001708 for (i = 0; i < lp->sl_prefixcnt; ++i)
1709 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001710 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001711 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001712 lp->sl_prefprog = NULL;
1713
1714 vim_free(lp->sl_midword);
1715 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001716
Bram Moolenaarea424162005-06-16 21:51:00 +00001717#ifdef FEAT_MBYTE
1718 {
1719 int todo = lp->sl_map_hash.ht_used;
1720 hashitem_T *hi;
1721
1722 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
1723 if (!HASHITEM_EMPTY(hi))
1724 {
1725 --todo;
1726 vim_free(hi->hi_key);
1727 }
1728 }
1729 hash_clear(&lp->sl_map_hash);
1730#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001731}
1732
1733/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001734 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001735 * Invoked through do_in_runtimepath().
1736 */
1737 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00001738spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001739 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001740 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001741{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001742 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001743}
1744
1745/*
1746 * Load one spell file and store the info into a slang_T.
1747 *
1748 * This is invoked in two ways:
1749 * - From spell_load_cb() to load a spell file for the first time. "lang" is
1750 * the language name, "old_lp" is NULL. Will allocate an slang_T.
1751 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
1752 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001753 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00001754 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001755 static slang_T *
1756spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001757 char_u *fname;
1758 char_u *lang;
1759 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001760 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001761{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001762 FILE *fd;
1763 char_u buf[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001764 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001765 char_u *bp;
1766 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001767 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001768 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001769 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001770 int round;
1771 char_u *save_sourcing_name = sourcing_name;
1772 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001773 int cnt, ccnt;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001774 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001775 slang_T *lp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001776 garray_T *gap;
1777 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001778 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001779 short *first;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001780 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001781 int c = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001782
Bram Moolenaarb765d632005-06-07 21:00:02 +00001783 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001784 if (fd == NULL)
1785 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001786 if (!silent)
1787 EMSG2(_(e_notopen), fname);
1788 else if (p_verbose > 2)
1789 {
1790 verbose_enter();
1791 smsg((char_u *)e_notopen, fname);
1792 verbose_leave();
1793 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001794 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001795 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00001796 if (p_verbose > 2)
1797 {
1798 verbose_enter();
1799 smsg((char_u *)_("Reading spell file \"%s\""), fname);
1800 verbose_leave();
1801 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001802
Bram Moolenaarb765d632005-06-07 21:00:02 +00001803 if (old_lp == NULL)
1804 {
1805 lp = slang_alloc(lang);
1806 if (lp == NULL)
1807 goto endFAIL;
1808
1809 /* Remember the file name, used to reload the file when it's updated. */
1810 lp->sl_fname = vim_strsave(fname);
1811 if (lp->sl_fname == NULL)
1812 goto endFAIL;
1813
1814 /* Check for .add.spl. */
1815 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
1816 }
1817 else
1818 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001819
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001820 /* Set sourcing_name, so that error messages mention the file name. */
1821 sourcing_name = fname;
1822 sourcing_lnum = 0;
1823
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001824 /* <HEADER>: <fileID>
1825 * <regioncnt> <regionname> ...
1826 * <charflagslen> <charflags>
1827 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001828 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001829 * <prefcondcnt> <prefcond> ...
1830 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001831 for (i = 0; i < VIMSPELLMAGICL; ++i)
1832 buf[i] = getc(fd); /* <fileID> */
1833 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
1834 {
1835 EMSG(_("E757: Wrong file ID in spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001836 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001837 }
1838
1839 cnt = getc(fd); /* <regioncnt> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001840 if (cnt < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001841 {
1842truncerr:
Bram Moolenaar7887d882005-07-01 22:33:52 +00001843 EMSG(_(e_spell_trunc));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001844 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001845 }
1846 if (cnt > 8)
1847 {
1848formerr:
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001849 EMSG(_(e_format));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001850 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001851 }
1852 for (i = 0; i < cnt; ++i)
1853 {
1854 lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
1855 lp->sl_regions[i * 2 + 1] = getc(fd);
1856 }
1857 lp->sl_regions[cnt * 2] = NUL;
1858
Bram Moolenaar7887d882005-07-01 22:33:52 +00001859 /* <charflagslen> <charflags> */
1860 p = read_cnt_string(fd, 1, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001861 if (cnt < 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00001862 goto endFAIL;
1863
1864 /* <fcharslen> <fchars> */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001865 fol = read_cnt_string(fd, 2, &ccnt);
1866 if (ccnt < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001867 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001868 vim_free(p);
Bram Moolenaar7887d882005-07-01 22:33:52 +00001869 goto endFAIL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001870 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00001871
1872 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
1873 if (p != NULL && fol != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001874 i = set_spell_charflags(p, cnt, fol);
Bram Moolenaar7887d882005-07-01 22:33:52 +00001875
1876 vim_free(p);
1877 vim_free(fol);
1878
1879 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
1880 if ((p == NULL) != (fol == NULL))
1881 goto formerr;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001882
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001883 /* <midwordlen> <midword> */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001884 lp->sl_midword = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001885 if (cnt < 0)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001886 goto endFAIL;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001887
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001888 /* <prefcondcnt> <prefcond> ... */
1889 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
1890 if (cnt > 0)
1891 {
1892 lp->sl_prefprog = (regprog_T **)alloc_clear(
1893 (unsigned)sizeof(regprog_T *) * cnt);
1894 if (lp->sl_prefprog == NULL)
1895 goto endFAIL;
1896 lp->sl_prefixcnt = cnt;
1897
1898 for (i = 0; i < cnt; ++i)
1899 {
1900 /* <prefcond> : <condlen> <condstr> */
1901 n = getc(fd); /* <condlen> */
1902 if (n < 0)
1903 goto formerr;
1904 /* When <condlen> is zero we have an empty condition. Otherwise
1905 * compile the regexp program used to check for the condition. */
1906 if (n > 0)
1907 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001908 buf[0] = '^'; /* always match at one position only */
1909 p = buf + 1;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001910 while (n-- > 0)
1911 *p++ = getc(fd); /* <condstr> */
1912 *p = NUL;
1913 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
1914 }
1915 }
1916 }
1917
1918
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001919 /* <SUGGEST> : <repcount> <rep> ...
1920 * <salflags> <salcount> <sal> ...
1921 * <maplen> <mapstr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001922
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001923 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
1924 if (cnt < 0)
1925 goto formerr;
1926
1927 gap = &lp->sl_rep;
1928 if (ga_grow(gap, cnt) == FAIL)
1929 goto endFAIL;
1930
1931 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
1932 for (; gap->ga_len < cnt; ++gap->ga_len)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001933 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001934 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
Bram Moolenaar7887d882005-07-01 22:33:52 +00001935 ftp->ft_from = read_cnt_string(fd, 1, &i);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001936 if (i <= 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00001937 goto endFAIL;
1938 ftp->ft_to = read_cnt_string(fd, 1, &i);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001939 if (i <= 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001940 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00001941 vim_free(ftp->ft_from);
1942 goto endFAIL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001943 }
1944 }
1945
1946 /* Fill the first-index table. */
1947 first = lp->sl_rep_first;
1948 for (i = 0; i < 256; ++i)
1949 first[i] = -1;
1950 for (i = 0; i < gap->ga_len; ++i)
1951 {
1952 ftp = &((fromto_T *)gap->ga_data)[i];
1953 if (first[*ftp->ft_from] == -1)
1954 first[*ftp->ft_from] = i;
1955 }
1956
1957 i = getc(fd); /* <salflags> */
1958 if (i & SAL_F0LLOWUP)
1959 lp->sl_followup = TRUE;
1960 if (i & SAL_COLLAPSE)
1961 lp->sl_collapse = TRUE;
1962 if (i & SAL_REM_ACCENTS)
1963 lp->sl_rem_accents = TRUE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001964 if (i & SAL_SOFO)
1965 lp->sl_sofo = TRUE;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001966 else
1967 lp->sl_sofo = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001968
1969 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
1970 if (cnt < 0)
1971 goto formerr;
1972
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001973 if (lp->sl_sofo)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001974 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001975 /*
1976 * SOFOFROM and SOFOTO items come in one <salfrom> and <salto>
1977 */
1978 if (cnt != 1)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001979 goto formerr;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001980
Bram Moolenaar7887d882005-07-01 22:33:52 +00001981 /* <salfromlen> <salfrom> */
1982 bp = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001983 if (cnt < 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001984 goto endFAIL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001985
Bram Moolenaar7887d882005-07-01 22:33:52 +00001986 /* <saltolen> <salto> */
1987 fol = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001988 if (cnt < 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001989 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001990 vim_free(bp);
1991 goto endFAIL;
1992 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001993
Bram Moolenaar7887d882005-07-01 22:33:52 +00001994 /* Store the info in lp->sl_sal and/or lp->sl_sal_first. */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001995 if (bp != NULL && fol != NULL)
1996 i = set_sofo(lp, bp, fol);
1997 else if (bp != NULL || fol != NULL)
1998 i = FAIL; /* only one of two strings is an error */
1999 else
2000 i = OK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002001
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002002 vim_free(bp);
2003 vim_free(fol);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002004 if (i == FAIL)
2005 goto formerr;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002006 }
2007 else
2008 {
2009 /*
2010 * SAL items
2011 */
2012 gap = &lp->sl_sal;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002013 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002014 if (ga_grow(gap, cnt) == FAIL)
2015 goto endFAIL;
2016
2017 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
2018 for (; gap->ga_len < cnt; ++gap->ga_len)
2019 {
2020 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
2021 ccnt = getc(fd); /* <salfromlen> */
2022 if (ccnt < 0)
2023 goto formerr;
2024 if ((p = alloc(ccnt + 2)) == NULL)
2025 goto endFAIL;
2026 smp->sm_lead = p;
2027
2028 /* Read up to the first special char into sm_lead. */
2029 for (i = 0; i < ccnt; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002030 {
2031 c = getc(fd); /* <salfrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002032 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002033 break;
2034 *p++ = c;
2035 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002036 smp->sm_leadlen = p - smp->sm_lead;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002037 *p++ = NUL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002038
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002039 /* Put (abc) chars in sm_oneof, if any. */
2040 if (c == '(')
2041 {
2042 smp->sm_oneof = p;
2043 for (++i; i < ccnt; ++i)
2044 {
2045 c = getc(fd); /* <salfrom> */
2046 if (c == ')')
2047 break;
2048 *p++ = c;
2049 }
2050 *p++ = NUL;
2051 if (++i < ccnt)
2052 c = getc(fd);
2053 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002054 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002055 smp->sm_oneof = NULL;
2056
2057 /* Any following chars go in sm_rules. */
2058 smp->sm_rules = p;
2059 if (i < ccnt)
2060 /* store the char we got while checking for end of sm_lead */
2061 *p++ = c;
2062 for (++i; i < ccnt; ++i)
2063 *p++ = getc(fd); /* <salfrom> */
2064 *p++ = NUL;
2065
Bram Moolenaar7887d882005-07-01 22:33:52 +00002066 /* <saltolen> <salto> */
2067 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002068 if (ccnt < 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002069 {
2070 vim_free(smp->sm_lead);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002071 goto formerr;
2072 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002073
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002074#ifdef FEAT_MBYTE
2075 if (has_mbyte)
2076 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002077 /* convert the multi-byte strings to wide char strings */
2078 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2079 smp->sm_leadlen = mb_charlen(smp->sm_lead);
2080 if (smp->sm_oneof == NULL)
2081 smp->sm_oneof_w = NULL;
2082 else
2083 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002084 if (smp->sm_to == NULL)
2085 smp->sm_to_w = NULL;
2086 else
2087 smp->sm_to_w = mb_str2wide(smp->sm_to);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002088 if (smp->sm_lead_w == NULL
2089 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002090 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002091 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002092 vim_free(smp->sm_lead);
2093 vim_free(smp->sm_to);
2094 vim_free(smp->sm_lead_w);
2095 vim_free(smp->sm_oneof_w);
2096 vim_free(smp->sm_to_w);
2097 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002098 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002099 }
2100#endif
2101 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002102
2103 /* Fill the first-index table. */
Bram Moolenaar7887d882005-07-01 22:33:52 +00002104 set_sal_first(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002105 }
2106
Bram Moolenaar7887d882005-07-01 22:33:52 +00002107 /* <maplen> <mapstr> */
2108 p = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002109 if (cnt < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002110 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002111 if (p != NULL)
2112 {
2113 set_map_str(lp, p);
2114 vim_free(p);
2115 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002116
Bram Moolenaar51485f02005-06-04 21:55:20 +00002117 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002118 * round 2: <KWORDTREE>
2119 * round 3: <PREFIXTREE> */
2120 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002121 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002122 /* The tree size was computed when writing the file, so that we can
2123 * allocate it as one long block. <nodecount> */
2124 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2125 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002126 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002127 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002128 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002129 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002130 bp = lalloc((long_u)len, TRUE);
2131 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002132 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002133 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002134 lp->sl_fbyts = bp;
2135 else if (round == 2)
2136 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002137 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002138 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002139
2140 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002141 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
2142 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002143 goto endFAIL;
2144 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002145 lp->sl_fidxs = ip;
2146 else if (round == 2)
2147 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002148 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002149 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002150
2151 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002152 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002153 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002154 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002155 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002156 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002157 }
2158 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002159
Bram Moolenaarb765d632005-06-07 21:00:02 +00002160 /* For a new file link it in the list of spell files. */
2161 if (old_lp == NULL)
2162 {
2163 lp->sl_next = first_lang;
2164 first_lang = lp;
2165 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002166
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002167 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002168
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002169endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002170 if (lang != NULL)
2171 /* truncating the name signals the error to spell_load_lang() */
2172 *lang = NUL;
2173 if (lp != NULL && old_lp == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002174 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002175 slang_free(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002176 lp = NULL;
2177 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002178
2179endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002180 if (fd != NULL)
2181 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002182 sourcing_name = save_sourcing_name;
2183 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002184
2185 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002186}
2187
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002188/*
2189 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002190 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002191 * Returns NULL when the count is zero.
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002192 * Sets "*cntp" to -1 when there is an error, length of the result otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002193 */
2194 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002195read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002196 FILE *fd;
2197 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002198 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002199{
2200 int cnt = 0;
2201 int i;
2202 char_u *str;
2203
2204 /* read the length bytes, MSB first */
2205 for (i = 0; i < cnt_bytes; ++i)
2206 cnt = (cnt << 8) + getc(fd);
2207 if (cnt < 0)
2208 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00002209 EMSG(_(e_spell_trunc));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002210 *cntp = -1;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002211 return NULL;
2212 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002213 *cntp = cnt;
2214 if (cnt == 0)
2215 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002216
2217 /* allocate memory */
2218 str = alloc((unsigned)cnt + 1);
2219 if (str == NULL)
2220 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002221 *cntp = -1;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002222 return NULL;
2223 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002224
2225 /* Read the string. Doesn't check for truncated file. */
2226 for (i = 0; i < cnt; ++i)
2227 str[i] = getc(fd);
2228 str[i] = NUL;
2229
2230 return str;
2231}
2232
Bram Moolenaar7887d882005-07-01 22:33:52 +00002233/*
2234 * Set the SOFOFROM and SOFOTO items in language "lp".
2235 * Returns FAIL when there is something wrong.
2236 */
2237 static int
2238set_sofo(lp, from, to)
2239 slang_T *lp;
2240 char_u *from;
2241 char_u *to;
2242{
2243 int i;
2244
2245#ifdef FEAT_MBYTE
2246 garray_T *gap;
2247 char_u *s;
2248 char_u *p;
2249 int c;
2250 int *inp;
2251
2252 if (has_mbyte)
2253 {
2254 /* Use "sl_sal" as an array with 256 pointers to a list of wide
2255 * characters. The index is the low byte of the character.
2256 * The list contains from-to pairs with a terminating NUL.
2257 * sl_sal_first[] is used for latin1 "from" characters. */
2258 gap = &lp->sl_sal;
2259 ga_init2(gap, sizeof(int *), 1);
2260 if (ga_grow(gap, 256) == FAIL)
2261 return FAIL;
2262 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
2263 gap->ga_len = 256;
2264
2265 /* First count the number of items for each list. Temporarily use
2266 * sl_sal_first[] for this. */
2267 for (p = from, s = to; *p != NUL && *s != NUL; )
2268 {
2269 c = mb_ptr2char_adv(&p);
2270 mb_ptr_adv(s);
2271 if (c >= 256)
2272 ++lp->sl_sal_first[c & 0xff];
2273 }
2274 if (*p != NUL || *s != NUL) /* lengths differ */
2275 return FAIL;
2276
2277 /* Allocate the lists. */
2278 for (i = 0; i < 256; ++i)
2279 if (lp->sl_sal_first[i] > 0)
2280 {
2281 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
2282 if (p == NULL)
2283 return FAIL;
2284 ((int **)gap->ga_data)[i] = (int *)p;
2285 *(int *)p = 0;
2286 }
2287
2288 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
2289 * list. */
2290 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
2291 for (p = from, s = to; *p != NUL && *s != NUL; )
2292 {
2293 c = mb_ptr2char_adv(&p);
2294 i = mb_ptr2char_adv(&s);
2295 if (c >= 256)
2296 {
2297 /* Append the from-to chars at the end of the list with
2298 * the low byte. */
2299 inp = ((int **)gap->ga_data)[c & 0xff];
2300 while (*inp != 0)
2301 ++inp;
2302 *inp++ = c; /* from char */
2303 *inp++ = i; /* to char */
2304 *inp++ = NUL; /* NUL at the end */
2305 }
2306 else
2307 /* mapping byte to char is done in sl_sal_first[] */
2308 lp->sl_sal_first[c] = i;
2309 }
2310 }
2311 else
2312#endif
2313 {
2314 /* mapping bytes to bytes is done in sl_sal_first[] */
2315 if (STRLEN(from) != STRLEN(to))
2316 return FAIL;
2317
2318 for (i = 0; to[i] != NUL; ++i)
2319 lp->sl_sal_first[from[i]] = to[i];
2320 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
2321 }
2322
2323 return OK;
2324}
2325
2326/*
2327 * Fill the first-index table for "lp".
2328 */
2329 static void
2330set_sal_first(lp)
2331 slang_T *lp;
2332{
2333 salfirst_T *sfirst;
2334 int i;
2335 salitem_T *smp;
2336 int c;
2337 garray_T *gap = &lp->sl_sal;
2338
2339 sfirst = lp->sl_sal_first;
2340 for (i = 0; i < 256; ++i)
2341 sfirst[i] = -1;
2342 smp = (salitem_T *)gap->ga_data;
2343 for (i = 0; i < gap->ga_len; ++i)
2344 {
2345#ifdef FEAT_MBYTE
2346 if (has_mbyte)
2347 /* Use the lowest byte of the first character. For latin1 it's
2348 * the character, for other encodings it should differ for most
2349 * characters. */
2350 c = *smp[i].sm_lead_w & 0xff;
2351 else
2352#endif
2353 c = *smp[i].sm_lead;
2354 if (sfirst[c] == -1)
2355 {
2356 sfirst[c] = i;
2357#ifdef FEAT_MBYTE
2358 if (has_mbyte)
2359 {
2360 int n;
2361
2362 /* Make sure all entries with this byte are following each
2363 * other. Move the ones that are in the wrong position. Do
2364 * keep the same ordering! */
2365 while (i + 1 < gap->ga_len
2366 && (*smp[i + 1].sm_lead_w & 0xff) == c)
2367 /* Skip over entry with same index byte. */
2368 ++i;
2369
2370 for (n = 1; i + n < gap->ga_len; ++n)
2371 if ((*smp[i + n].sm_lead_w & 0xff) == c)
2372 {
2373 salitem_T tsal;
2374
2375 /* Move entry with same index byte after the entries
2376 * we already found. */
2377 ++i;
2378 --n;
2379 tsal = smp[i + n];
2380 mch_memmove(smp + i + 1, smp + i,
2381 sizeof(salitem_T) * n);
2382 smp[i] = tsal;
2383 }
2384 }
2385#endif
2386 }
2387 }
2388}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002389
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002390#ifdef FEAT_MBYTE
2391/*
2392 * Turn a multi-byte string into a wide character string.
2393 * Return it in allocated memory (NULL for out-of-memory)
2394 */
2395 static int *
2396mb_str2wide(s)
2397 char_u *s;
2398{
2399 int *res;
2400 char_u *p;
2401 int i = 0;
2402
2403 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
2404 if (res != NULL)
2405 {
2406 for (p = s; *p != NUL; )
2407 res[i++] = mb_ptr2char_adv(&p);
2408 res[i] = NUL;
2409 }
2410 return res;
2411}
2412#endif
2413
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002414/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002415 * Read one row of siblings from the spell file and store it in the byte array
2416 * "byts" and index array "idxs". Recursively read the children.
2417 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00002418 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00002419 *
2420 * Returns the index follosing the siblings.
2421 * Returns -1 if the file is shorter than expected.
2422 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002423 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002424 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002425read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002426 FILE *fd;
2427 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002428 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002429 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002430 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002431 int prefixtree; /* TRUE for reading PREFIXTREE */
2432 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002433{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002434 int len;
2435 int i;
2436 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002437 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002438 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002439 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002440#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002441
Bram Moolenaar51485f02005-06-04 21:55:20 +00002442 len = getc(fd); /* <siblingcount> */
2443 if (len <= 0)
2444 return -1;
2445
2446 if (startidx + len >= maxidx)
2447 return -2;
2448 byts[idx++] = len;
2449
2450 /* Read the byte values, flag/region bytes and shared indexes. */
2451 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002452 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002453 c = getc(fd); /* <byte> */
2454 if (c < 0)
2455 return -1;
2456 if (c <= BY_SPECIAL)
2457 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002458 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002459 {
2460 /* No flags, all regions. */
2461 idxs[idx] = 0;
2462 c = 0;
2463 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002464 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002465 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002466 if (prefixtree)
2467 {
2468 /* Read the prefix ID and the condition nr. In idxs[]
2469 * store the prefix ID in the low byte, the condition
2470 * index shifted up 8 bits. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002471 c2 = getc(fd); /* <prefixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002472 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
2473 if (n >= maxprefcondnr)
2474 return -2;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002475 c2 += (n << 8);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002476
2477 /* the byte value is misused to pass two flags: rare
2478 * and non-combining. */
2479 if (c == BY_FLAGS || c == BY_PFX_RNC)
2480 c2 |= WF_RAREPFX;
2481 if (c == BY_PFX_NC || c == BY_PFX_RNC)
2482 c2 |= WF_PFX_NC;
2483 c = c2;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002484 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002485 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002486 {
2487 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002488 * idxs[] the flags go in the low two bytes, region above
2489 * that and prefix ID above the region. */
2490 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002491 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002492 if (c2 == BY_FLAGS2)
2493 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002494 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002495 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002496 if (c & WF_PFX)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002497 c = (getc(fd) << 24) + c; /* <prefixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002498 }
2499
Bram Moolenaar51485f02005-06-04 21:55:20 +00002500 idxs[idx] = c;
2501 c = 0;
2502 }
2503 else /* c == BY_INDEX */
2504 {
2505 /* <nodeidx> */
2506 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2507 if (n < 0 || n >= maxidx)
2508 return -2;
2509 idxs[idx] = n + SHARED_MASK;
2510 c = getc(fd); /* <xbyte> */
2511 }
2512 }
2513 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002514 }
2515
Bram Moolenaar51485f02005-06-04 21:55:20 +00002516 /* Recursively read the children for non-shared siblings.
2517 * Skip the end-of-word ones (zero byte value) and the shared ones (and
2518 * remove SHARED_MASK) */
2519 for (i = 1; i <= len; ++i)
2520 if (byts[startidx + i] != 0)
2521 {
2522 if (idxs[startidx + i] & SHARED_MASK)
2523 idxs[startidx + i] &= ~SHARED_MASK;
2524 else
2525 {
2526 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002527 idx = read_tree(fd, byts, idxs, maxidx, idx,
2528 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002529 if (idx < 0)
2530 break;
2531 }
2532 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002533
Bram Moolenaar51485f02005-06-04 21:55:20 +00002534 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002535}
2536
2537/*
2538 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002539 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002540 */
2541 char_u *
2542did_set_spelllang(buf)
2543 buf_T *buf;
2544{
2545 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002546 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002547 char_u *region;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002548 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002549 int region_mask;
2550 slang_T *lp;
2551 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002552 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002553 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002554 int len;
2555 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002556 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002557 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002558 char_u *use_region = NULL;
2559 int dont_use_region = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002560
2561 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002562 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002563
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002564 /* loop over comma separated language names. */
2565 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002566 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002567 /* Get one language name. */
2568 copy_option_part(&splp, lang, MAXWLEN, ",");
2569
Bram Moolenaar5482f332005-04-17 20:18:43 +00002570 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002571 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002572
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002573 /* If the name ends in ".spl" use it as the name of the spell file.
2574 * If there is a region name let "region" point to it and remove it
2575 * from the name. */
2576 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2577 {
2578 filename = TRUE;
2579
2580 /* Check if we loaded this language before. */
2581 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2582 if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME)
2583 break;
2584 }
2585 else
2586 {
2587 filename = FALSE;
2588 if (len > 3 && lang[len - 3] == '_')
2589 {
2590 region = lang + len - 2;
2591 len -= 3;
2592 lang[len] = NUL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002593
2594 /* If the region differs from what was used before then don't
2595 * use it for 'spellfile'. */
2596 if (use_region != NULL && STRCMP(region, use_region) != 0)
2597 dont_use_region = TRUE;
2598 use_region = region;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002599 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002600 else
2601 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002602
2603 /* Check if we loaded this language before. */
2604 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2605 if (STRICMP(lang, lp->sl_name) == 0)
2606 break;
2607 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002608
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002609 /* If not found try loading the language now. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002610 if (lp == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002611 {
2612 if (filename)
2613 (void)spell_load_file(lang, lang, NULL, FALSE);
2614 else
2615 spell_load_lang(lang);
2616 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002617
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002618 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002619 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002620 */
2621 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002622 if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME
2623 : STRICMP(lang, lp->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002624 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002625 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002626 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002627 {
2628 /* find region in sl_regions */
2629 c = find_region(lp->sl_regions, region);
2630 if (c == REGION_ALL)
2631 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002632 if (lp->sl_add)
2633 {
2634 if (*lp->sl_regions != NUL)
2635 /* This addition file is for other regions. */
2636 region_mask = 0;
2637 }
2638 else
2639 /* This is probably an error. Give a warning and
2640 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002641 smsg((char_u *)
2642 _("Warning: region %s not supported"),
2643 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002644 }
2645 else
2646 region_mask = 1 << c;
2647 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002648
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002649 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002650 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002651 if (ga_grow(&ga, 1) == FAIL)
2652 {
2653 ga_clear(&ga);
2654 return e_outofmem;
2655 }
2656 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2657 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2658 ++ga.ga_len;
2659 use_midword(lp, buf);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002660 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002661 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002662 }
2663
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002664 /* round 0: load int_wordlist, if possible.
2665 * round 1: load first name in 'spellfile'.
2666 * round 2: load second name in 'spellfile.
2667 * etc. */
2668 spf = curbuf->b_p_spf;
2669 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002670 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002671 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002672 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002673 /* Internal wordlist, if there is one. */
2674 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002675 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002676 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002677 }
2678 else
2679 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002680 /* One entry in 'spellfile'. */
2681 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
2682 STRCAT(spf_name, ".spl");
2683
2684 /* If it was already found above then skip it. */
2685 for (c = 0; c < ga.ga_len; ++c)
2686 if (fullpathcmp(spf_name,
2687 LANGP_ENTRY(ga, c)->lp_slang->sl_fname,
2688 FALSE) == FPC_SAME)
2689 break;
2690 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002691 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002692 }
2693
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002694 /* Check if it was loaded already. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002695 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2696 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
2697 break;
2698 if (lp == NULL)
2699 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002700 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002701 * region name, the region is ignored otherwise. for int_wordlist
2702 * use an arbitrary name. */
2703 if (round == 0)
2704 STRCPY(lang, "internal wordlist");
2705 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00002706 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002707 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002708 p = vim_strchr(lang, '.');
2709 if (p != NULL)
2710 *p = NUL; /* truncate at ".encoding.add" */
2711 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002712 lp = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002713 }
2714 if (lp != NULL && ga_grow(&ga, 1) == OK)
2715 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002716 region_mask = REGION_ALL;
2717 if (use_region != NULL && !dont_use_region)
2718 {
2719 /* find region in sl_regions */
2720 c = find_region(lp->sl_regions, use_region);
2721 if (c != REGION_ALL)
2722 region_mask = 1 << c;
2723 else if (*lp->sl_regions != NUL)
2724 /* This spell file is for other regions. */
2725 region_mask = 0;
2726 }
2727
2728 if (region_mask != 0)
2729 {
2730 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2731 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2732 ++ga.ga_len;
2733 use_midword(lp, buf);
2734 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002735 }
2736 }
2737
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002738 /* Add a NULL entry to mark the end of the list. */
2739 if (ga_grow(&ga, 1) == FAIL)
2740 {
2741 ga_clear(&ga);
2742 return e_outofmem;
2743 }
2744 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
2745 ++ga.ga_len;
2746
2747 /* Everything is fine, store the new b_langp value. */
2748 ga_clear(&buf->b_langp);
2749 buf->b_langp = ga;
2750
2751 return NULL;
2752}
2753
2754/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002755 * Clear the midword characters for buffer "buf".
2756 */
2757 static void
2758clear_midword(buf)
2759 buf_T *buf;
2760{
2761 vim_memset(buf->b_spell_ismw, 0, 256);
2762#ifdef FEAT_MBYTE
2763 vim_free(buf->b_spell_ismw_mb);
2764 buf->b_spell_ismw_mb = NULL;
2765#endif
2766}
2767
2768/*
2769 * Use the "sl_midword" field of language "lp" for buffer "buf".
2770 * They add up to any currently used midword characters.
2771 */
2772 static void
2773use_midword(lp, buf)
2774 slang_T *lp;
2775 buf_T *buf;
2776{
2777 char_u *p;
2778
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002779 if (lp->sl_midword == NULL) /* there aren't any */
2780 return;
2781
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002782 for (p = lp->sl_midword; *p != NUL; )
2783#ifdef FEAT_MBYTE
2784 if (has_mbyte)
2785 {
2786 int c, l, n;
2787 char_u *bp;
2788
2789 c = mb_ptr2char(p);
2790 l = mb_ptr2len_check(p);
2791 if (c < 256)
2792 buf->b_spell_ismw[c] = TRUE;
2793 else if (buf->b_spell_ismw_mb == NULL)
2794 /* First multi-byte char in "b_spell_ismw_mb". */
2795 buf->b_spell_ismw_mb = vim_strnsave(p, l);
2796 else
2797 {
2798 /* Append multi-byte chars to "b_spell_ismw_mb". */
2799 n = STRLEN(buf->b_spell_ismw_mb);
2800 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
2801 if (bp != NULL)
2802 {
2803 vim_free(buf->b_spell_ismw_mb);
2804 buf->b_spell_ismw_mb = bp;
2805 vim_strncpy(bp + n, p, l);
2806 }
2807 }
2808 p += l;
2809 }
2810 else
2811#endif
2812 buf->b_spell_ismw[*p++] = TRUE;
2813}
2814
2815/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002816 * Find the region "region[2]" in "rp" (points to "sl_regions").
2817 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002818 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002819 */
2820 static int
2821find_region(rp, region)
2822 char_u *rp;
2823 char_u *region;
2824{
2825 int i;
2826
2827 for (i = 0; ; i += 2)
2828 {
2829 if (rp[i] == NUL)
2830 return REGION_ALL;
2831 if (rp[i] == region[0] && rp[i + 1] == region[1])
2832 break;
2833 }
2834 return i / 2;
2835}
2836
2837/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002838 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002839 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002840 * Word WF_ONECAP
2841 * W WORD WF_ALLCAP
2842 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002843 */
2844 static int
2845captype(word, end)
2846 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002847 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002848{
2849 char_u *p;
2850 int c;
2851 int firstcap;
2852 int allcap;
2853 int past_second = FALSE; /* past second word char */
2854
2855 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002856 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002857 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002858 return 0; /* only non-word characters, illegal word */
2859#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00002860 if (has_mbyte)
2861 c = mb_ptr2char_adv(&p);
2862 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002863#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00002864 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002865 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002866
2867 /*
2868 * Need to check all letters to find a word with mixed upper/lower.
2869 * But a word with an upper char only at start is a ONECAP.
2870 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002871 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002872 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002873 {
2874#ifdef FEAT_MBYTE
2875 c = mb_ptr2char(p);
2876#else
2877 c = *p;
2878#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002879 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002880 {
2881 /* UUl -> KEEPCAP */
2882 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002883 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002884 allcap = FALSE;
2885 }
2886 else if (!allcap)
2887 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002888 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002889 past_second = TRUE;
2890 }
2891
2892 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002893 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002894 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002895 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002896 return 0;
2897}
2898
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002899# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
2900/*
2901 * Free all languages.
2902 */
2903 void
2904spell_free_all()
2905{
2906 slang_T *lp;
2907 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002908 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002909
2910 /* Go through all buffers and handle 'spelllang'. */
2911 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2912 ga_clear(&buf->b_langp);
2913
2914 while (first_lang != NULL)
2915 {
2916 lp = first_lang;
2917 first_lang = lp->sl_next;
2918 slang_free(lp);
2919 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002920
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002921 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002922 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002923 /* Delete the internal wordlist and its .spl file */
2924 mch_remove(int_wordlist);
2925 int_wordlist_spl(fname);
2926 mch_remove(fname);
2927 vim_free(int_wordlist);
2928 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002929 }
2930
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002931 init_spell_chartab();
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002932}
2933# endif
2934
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002935# if defined(FEAT_MBYTE) || defined(PROTO)
2936/*
2937 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002938 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002939 */
2940 void
2941spell_reload()
2942{
2943 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002944 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002945
Bram Moolenaarea408852005-06-25 22:49:46 +00002946 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002947 init_spell_chartab();
2948
2949 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002950 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002951
2952 /* Go through all buffers and handle 'spelllang'. */
2953 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2954 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002955 /* Only load the wordlists when 'spelllang' is set and there is a
2956 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002957 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002958 {
2959 FOR_ALL_WINDOWS(wp)
2960 if (wp->w_buffer == buf && wp->w_p_spell)
2961 {
2962 (void)did_set_spelllang(buf);
2963# ifdef FEAT_WINDOWS
2964 break;
2965# endif
2966 }
2967 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002968 }
2969}
2970# endif
2971
Bram Moolenaarb765d632005-06-07 21:00:02 +00002972/*
2973 * Reload the spell file "fname" if it's loaded.
2974 */
2975 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002976spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002977 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002978 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002979{
2980 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002981 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002982
Bram Moolenaarb765d632005-06-07 21:00:02 +00002983 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2984 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
2985 {
2986 slang_clear(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002987 (void)spell_load_file(fname, NULL, lp, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002988 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002989 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002990 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002991
2992 /* When "zg" was used and the file wasn't loaded yet, should redo
2993 * 'spelllang' to get it loaded. */
2994 if (added_word && !didit)
2995 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002996}
2997
2998
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002999/*
3000 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003001 */
3002
Bram Moolenaar51485f02005-06-04 21:55:20 +00003003#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003004 and .dic file. */
3005/*
3006 * Main structure to store the contents of a ".aff" file.
3007 */
3008typedef struct afffile_S
3009{
3010 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003011 int af_rar; /* RAR ID for rare word */
3012 int af_kep; /* KEP ID for keep-case word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003013 int af_bad; /* BAD ID for banned word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003014 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003015 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
3016 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003017} afffile_T;
3018
3019typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003020/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
3021struct affentry_S
3022{
3023 affentry_T *ae_next; /* next affix with same name/number */
3024 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
3025 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003026 char_u *ae_cond; /* condition (NULL for ".") */
3027 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003028 int ae_rare; /* rare affix */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003029};
3030
3031/* Affix header from ".aff" file. Used for af_pref and af_suff. */
3032typedef struct affheader_S
3033{
3034 char_u ah_key[2]; /* key for hashtable == name of affix entry */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003035 int ah_newID; /* prefix ID after renumbering */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003036 int ah_combine; /* suffix may combine with prefix */
3037 affentry_T *ah_first; /* first affix entry */
3038} affheader_T;
3039
3040#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
3041
3042/*
3043 * Structure that is used to store the items in the word tree. This avoids
3044 * the need to keep track of each allocated thing, it's freed all at once
3045 * after ":mkspell" is done.
3046 */
3047#define SBLOCKSIZE 16000 /* size of sb_data */
3048typedef struct sblock_S sblock_T;
3049struct sblock_S
3050{
3051 sblock_T *sb_next; /* next block in list */
3052 int sb_used; /* nr of bytes already in use */
3053 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003054};
3055
3056/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003057 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003058 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003059typedef struct wordnode_S wordnode_T;
3060struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003061{
Bram Moolenaar0c405862005-06-22 22:26:26 +00003062 union /* shared to save space */
3063 {
3064 char_u hashkey[6]; /* room for the hash key */
3065 int index; /* index in written nodes (valid after first
3066 round) */
3067 } wn_u1;
3068 union /* shared to save space */
3069 {
3070 wordnode_T *next; /* next node with same hash key */
3071 wordnode_T *wnode; /* parent node that will write this node */
3072 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003073 wordnode_T *wn_child; /* child (next byte in word) */
3074 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
3075 always sorted) */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003076 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003077 short_u wn_flags; /* when wn_byte is NUL: WF_ flags */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003078 short wn_region; /* when wn_byte is NUL: region mask; for
3079 PREFIXTREE it's the prefcondnr */
3080 char_u wn_prefixID; /* supported/required prefix ID or 0 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003081};
3082
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003083#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
3084
Bram Moolenaar51485f02005-06-04 21:55:20 +00003085#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003086
Bram Moolenaar51485f02005-06-04 21:55:20 +00003087/*
3088 * Info used while reading the spell files.
3089 */
3090typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003091{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003092 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003093 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003094 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003095 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003096 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003097 sblock_T *si_blocks; /* memory blocks used */
3098 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003099 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003100 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003101 int si_region; /* region mask */
3102 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003103 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003104 int si_verbose; /* verbose messages */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003105 int si_region_count; /* number of regions supported (1 when there
3106 are no regions) */
3107 char_u si_region_name[16]; /* region names (if count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003108
3109 garray_T si_rep; /* list of fromto_T entries from REP lines */
3110 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003111 char_u *si_sofofr; /* SOFOFROM text */
3112 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003113 int si_followup; /* soundsalike: ? */
3114 int si_collapse; /* soundsalike: ? */
3115 int si_rem_accents; /* soundsalike: remove accents */
3116 garray_T si_map; /* MAP info concatenated */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003117 char_u *si_midword; /* MIDWORD chars, alloc'ed string or NULL */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003118 garray_T si_prefcond; /* table with conditions for postponed
3119 * prefixes, each stored as a string */
3120 int si_newID; /* current value for ah_newID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003121} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003122
Bram Moolenaar51485f02005-06-04 21:55:20 +00003123static afffile_T *spell_read_aff __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003124static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003125static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
3126static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00003127static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003128static void spell_free_aff __ARGS((afffile_T *aff));
3129static int spell_read_dic __ARGS((char_u *fname, spellinfo_T *spin, afffile_T *affile));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003130static char_u *get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, sblock_T **blp));
3131static 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 +00003132static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin));
3133static void *getroom __ARGS((sblock_T **blp, size_t len));
3134static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s));
3135static void free_blocks __ARGS((sblock_T *bl));
3136static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003137static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags, int region, char_u *pfxlist));
3138static 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 +00003139static void wordtree_compress __ARGS((wordnode_T *root, spellinfo_T *spin));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003140static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot));
3141static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar3982c542005-06-08 21:56:31 +00003142static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar0c405862005-06-22 22:26:26 +00003143static void clear_node __ARGS((wordnode_T *node));
3144static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003145static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00003146static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003147
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003148/* values for wn_flags used in the postponed prefixes tree */
3149#define PFX_FLAGS -1 /* not rare, combining */
3150#define PFX_FLAGS_R -2 /* rare, combining */
3151#define PFX_FLAGS_NC -3 /* not rare, not combining */
3152#define PFX_FLAGS_RNC -4 /* rare, not combining */
3153
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003154/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003155 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00003156 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003157 */
3158 static afffile_T *
Bram Moolenaar51485f02005-06-04 21:55:20 +00003159spell_read_aff(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003160 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003161 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003162{
3163 FILE *fd;
3164 afffile_T *aff;
3165 char_u rline[MAXLINELEN];
3166 char_u *line;
3167 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003168#define MAXITEMCNT 7
3169 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003170 int itemcnt;
3171 char_u *p;
3172 int lnum = 0;
3173 affheader_T *cur_aff = NULL;
3174 int aff_todo = 0;
3175 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003176 char_u *low = NULL;
3177 char_u *fol = NULL;
3178 char_u *upp = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003179 static char *e_affname = N_("Affix name too long in %s line %d: %s");
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003180 int do_rep;
3181 int do_sal;
3182 int do_map;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003183 int do_midword;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003184 int do_sofo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003185 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003186 hashitem_T *hi;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003187
Bram Moolenaar51485f02005-06-04 21:55:20 +00003188 /*
3189 * Open the file.
3190 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003191 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003192 if (fd == NULL)
3193 {
3194 EMSG2(_(e_notopen), fname);
3195 return NULL;
3196 }
3197
Bram Moolenaarb765d632005-06-07 21:00:02 +00003198 if (spin->si_verbose || p_verbose > 2)
3199 {
3200 if (!spin->si_verbose)
3201 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003202 smsg((char_u *)_("Reading affix file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003203 out_flush();
3204 if (!spin->si_verbose)
3205 verbose_leave();
3206 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003207
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003208 /* Only do REP lines when not done in another .aff file already. */
3209 do_rep = spin->si_rep.ga_len == 0;
3210
3211 /* Only do SAL lines when not done in another .aff file already. */
3212 do_sal = spin->si_sal.ga_len == 0;
3213
3214 /* Only do MAP lines when not done in another .aff file already. */
3215 do_map = spin->si_map.ga_len == 0;
3216
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003217 /* Only do MIDWORD line when not done in another .aff file already */
3218 do_midword = spin->si_midword == NULL;
3219
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003220 /* Only do SOFOFROM and SOFOTO when not done in another .aff file already */
3221 do_sofo = spin->si_sofofr == NULL;
3222
Bram Moolenaar51485f02005-06-04 21:55:20 +00003223 /*
3224 * Allocate and init the afffile_T structure.
3225 */
3226 aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003227 if (aff == NULL)
3228 return NULL;
3229 hash_init(&aff->af_pref);
3230 hash_init(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003231
3232 /*
3233 * Read all the lines in the file one by one.
3234 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003235 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003236 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003237 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003238 ++lnum;
3239
3240 /* Skip comment lines. */
3241 if (*rline == '#')
3242 continue;
3243
3244 /* Convert from "SET" to 'encoding' when needed. */
3245 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003246#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003247 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003248 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003249 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003250 if (pc == NULL)
3251 {
3252 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3253 fname, lnum, rline);
3254 continue;
3255 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003256 line = pc;
3257 }
3258 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003259#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003260 {
3261 pc = NULL;
3262 line = rline;
3263 }
3264
3265 /* Split the line up in white separated items. Put a NUL after each
3266 * item. */
3267 itemcnt = 0;
3268 for (p = line; ; )
3269 {
3270 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
3271 ++p;
3272 if (*p == NUL)
3273 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003274 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003275 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003276 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003277 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003278 ++p;
3279 if (*p == NUL)
3280 break;
3281 *p++ = NUL;
3282 }
3283
3284 /* Handle non-empty lines. */
3285 if (itemcnt > 0)
3286 {
3287 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
3288 && aff->af_enc == NULL)
3289 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003290#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003291 /* Setup for conversion from "ENC" to 'encoding'. */
3292 aff->af_enc = enc_canonize(items[1]);
3293 if (aff->af_enc != NULL && !spin->si_ascii
3294 && convert_setup(&spin->si_conv, aff->af_enc,
3295 p_enc) == FAIL)
3296 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
3297 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003298 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003299#else
3300 smsg((char_u *)_("Conversion in %s not supported"), fname);
3301#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003302 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003303 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2)
3304 {
3305 if (do_midword)
3306 spin->si_midword = vim_strsave(items[1]);
3307 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00003308 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
3309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003310 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003311 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003312 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003313 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003314 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003315 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003316 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
3317 && aff->af_rar == 0)
3318 {
3319 aff->af_rar = items[1][0];
3320 if (items[1][1] != NUL)
3321 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3322 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00003323 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
3324 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003325 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003326 aff->af_kep = items[1][0];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003327 if (items[1][1] != NUL)
3328 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3329 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00003330 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
3331 && aff->af_bad == 0)
3332 {
3333 aff->af_bad = items[1][0];
3334 if (items[1][1] != NUL)
3335 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3336 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003337 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
3338 {
3339 aff->af_pfxpostpone = TRUE;
3340 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003341 else if ((STRCMP(items[0], "PFX") == 0
3342 || STRCMP(items[0], "SFX") == 0)
3343 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003344 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003345 {
Bram Moolenaar8db73182005-06-17 21:51:16 +00003346 /* Myspell allows extra text after the item, but that might
3347 * mean mistakes go unnoticed. Require a comment-starter. */
3348 if (itemcnt > 4 && *items[4] != '#')
3349 smsg((char_u *)_("Trailing text in %s line %d: %s"),
3350 fname, lnum, items[4]);
3351
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003352 /* New affix letter. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003353 cur_aff = (affheader_T *)getroom(&spin->si_blocks,
3354 sizeof(affheader_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003355 if (cur_aff == NULL)
3356 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003357 cur_aff->ah_key[0] = *items[1]; /* TODO: multi-byte? */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003358 cur_aff->ah_key[1] = NUL;
3359 if (items[1][1] != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003360 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003361 if (*items[2] == 'Y')
3362 cur_aff->ah_combine = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003363 else if (*items[2] != 'N')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003364 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
3365 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003366
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003367 if (*items[0] == 'P')
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003368 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003369 tp = &aff->af_pref;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003370 /* Use a new number in the .spl file later, to be able to
3371 * handle multiple .aff files. */
3372 if (aff->af_pfxpostpone)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003373 cur_aff->ah_newID = ++spin->si_newID;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003374 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003375 else
3376 tp = &aff->af_suff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003377 aff_todo = atoi((char *)items[3]);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003378 hi = hash_find(tp, cur_aff->ah_key);
3379 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar51485f02005-06-04 21:55:20 +00003380 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003381 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
3382 fname, lnum, items[1]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003383 aff_todo = 0;
3384 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003385 else
3386 hash_add(tp, cur_aff->ah_key);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003387 }
3388 else if ((STRCMP(items[0], "PFX") == 0
3389 || STRCMP(items[0], "SFX") == 0)
3390 && aff_todo > 0
3391 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003392 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003393 {
3394 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003395 int rare = FALSE;
3396 int lasti = 5;
3397
3398 /* Check for "rare" after the other info. */
3399 if (itemcnt > 5 && STRICMP(items[5], "rare") == 0)
3400 {
3401 rare = TRUE;
3402 lasti = 6;
3403 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003404
Bram Moolenaar8db73182005-06-17 21:51:16 +00003405 /* Myspell allows extra text after the item, but that might
3406 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003407 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar8db73182005-06-17 21:51:16 +00003408 smsg((char_u *)_("Trailing text in %s line %d: %s"),
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003409 fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00003410
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003411 /* New item for an affix letter. */
3412 --aff_todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003413 aff_entry = (affentry_T *)getroom(&spin->si_blocks,
3414 sizeof(affentry_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003415 if (aff_entry == NULL)
3416 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003417 aff_entry->ae_rare = rare;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003418
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003419 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003420 aff_entry->ae_chop = getroom_save(&spin->si_blocks,
3421 items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003422 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003423 aff_entry->ae_add = getroom_save(&spin->si_blocks,
3424 items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003425
Bram Moolenaar51485f02005-06-04 21:55:20 +00003426 /* Don't use an affix entry with non-ASCII characters when
3427 * "spin->si_ascii" is TRUE. */
3428 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00003429 || has_non_ascii(aff_entry->ae_add)))
3430 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003431 aff_entry->ae_next = cur_aff->ah_first;
3432 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003433
3434 if (STRCMP(items[4], ".") != 0)
3435 {
3436 char_u buf[MAXLINELEN];
3437
3438 aff_entry->ae_cond = getroom_save(&spin->si_blocks,
3439 items[4]);
3440 if (*items[0] == 'P')
3441 sprintf((char *)buf, "^%s", items[4]);
3442 else
3443 sprintf((char *)buf, "%s$", items[4]);
3444 aff_entry->ae_prog = vim_regcomp(buf,
3445 RE_MAGIC + RE_STRING);
3446 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003447
3448 /* For postponed prefixes we need an entry in si_prefcond
3449 * for the condition. Use an existing one if possible. */
3450 if (*items[0] == 'P' && aff->af_pfxpostpone
3451 && aff_entry->ae_chop == NULL)
3452 {
3453 int idx;
3454 char_u **pp;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003455 int n;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003456
3457 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
3458 --idx)
3459 {
3460 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
3461 if (str_equal(p, aff_entry->ae_cond))
3462 break;
3463 }
3464 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
3465 {
3466 /* Not found, add a new condition. */
3467 idx = spin->si_prefcond.ga_len++;
3468 pp = ((char_u **)spin->si_prefcond.ga_data) + idx;
3469 if (aff_entry->ae_cond == NULL)
3470 *pp = NULL;
3471 else
3472 *pp = getroom_save(&spin->si_blocks,
3473 aff_entry->ae_cond);
3474 }
3475
3476 /* Add the prefix to the prefix tree. */
3477 if (aff_entry->ae_add == NULL)
3478 p = (char_u *)"";
3479 else
3480 p = aff_entry->ae_add;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003481 if (rare)
3482 {
3483 if (cur_aff->ah_combine)
3484 n = PFX_FLAGS_R;
3485 else
3486 n = PFX_FLAGS_RNC;
3487 }
3488 else
3489 {
3490 if (cur_aff->ah_combine)
3491 n = PFX_FLAGS;
3492 else
3493 n = PFX_FLAGS_NC;
3494 }
3495 tree_add_word(p, spin->si_prefroot, n,
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003496 idx, cur_aff->ah_newID, &spin->si_blocks);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003497 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003498 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003499 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003500 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
3501 {
3502 if (fol != NULL)
3503 smsg((char_u *)_("Duplicate FOL in %s line %d"),
3504 fname, lnum);
3505 else
3506 fol = vim_strsave(items[1]);
3507 }
3508 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
3509 {
3510 if (low != NULL)
3511 smsg((char_u *)_("Duplicate LOW in %s line %d"),
3512 fname, lnum);
3513 else
3514 low = vim_strsave(items[1]);
3515 }
3516 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
3517 {
3518 if (upp != NULL)
3519 smsg((char_u *)_("Duplicate UPP in %s line %d"),
3520 fname, lnum);
3521 else
3522 upp = vim_strsave(items[1]);
3523 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003524 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003525 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003526 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003527 if (!isdigit(*items[1]))
3528 smsg((char_u *)_("Expected REP count in %s line %d"),
3529 fname, lnum);
3530 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003531 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
3532 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003533 /* REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003534 if (do_rep)
3535 add_fromto(spin, &spin->si_rep, items[1], items[2]);
3536 }
3537 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
3538 {
3539 /* MAP item or count */
3540 if (!found_map)
3541 {
3542 /* First line contains the count. */
3543 found_map = TRUE;
3544 if (!isdigit(*items[1]))
3545 smsg((char_u *)_("Expected MAP count in %s line %d"),
3546 fname, lnum);
3547 }
3548 else if (do_map)
3549 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003550 int c;
3551
3552 /* Check that every character appears only once. */
3553 for (p = items[1]; *p != NUL; )
3554 {
3555#ifdef FEAT_MBYTE
3556 c = mb_ptr2char_adv(&p);
3557#else
3558 c = *p++;
3559#endif
3560 if ((spin->si_map.ga_len > 0
3561 && vim_strchr(spin->si_map.ga_data, c)
3562 != NULL)
3563 || vim_strchr(p, c) != NULL)
3564 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
3565 fname, lnum);
3566 }
3567
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003568 /* We simply concatenate all the MAP strings, separated by
3569 * slashes. */
3570 ga_concat(&spin->si_map, items[1]);
3571 ga_append(&spin->si_map, '/');
3572 }
3573 }
3574 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
3575 {
3576 if (do_sal)
3577 {
3578 /* SAL item (sounds-a-like)
3579 * Either one of the known keys or a from-to pair. */
3580 if (STRCMP(items[1], "followup") == 0)
3581 spin->si_followup = sal_to_bool(items[2]);
3582 else if (STRCMP(items[1], "collapse_result") == 0)
3583 spin->si_collapse = sal_to_bool(items[2]);
3584 else if (STRCMP(items[1], "remove_accents") == 0)
3585 spin->si_rem_accents = sal_to_bool(items[2]);
3586 else
3587 /* when "to" is "_" it means empty */
3588 add_fromto(spin, &spin->si_sal, items[1],
3589 STRCMP(items[2], "_") == 0 ? (char_u *)""
3590 : items[2]);
3591 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003592 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003593 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
3594 && (!do_sofo || spin->si_sofofr == NULL))
3595 {
3596 if (do_sofo)
3597 spin->si_sofofr = vim_strsave(items[1]);
3598 }
3599 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
3600 && (!do_sofo || spin->si_sofoto == NULL))
3601 {
3602 if (do_sofo)
3603 spin->si_sofoto = vim_strsave(items[1]);
3604 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003605 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003606 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
3607 fname, lnum, items[0]);
3608 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003609 }
3610
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003611 if (do_sofo && (spin->si_sofofr == NULL) != (spin->si_sofoto == NULL))
3612 smsg((char_u *)_("Missing SOFO%s line in %s"),
3613 spin->si_sofofr == NULL ? "FROM" : "TO", fname);
3614 if (spin->si_sofofr != NULL && spin->si_sal.ga_len > 0)
3615 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
3616
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003617 if (fol != NULL || low != NULL || upp != NULL)
3618 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003619 if (spin->si_clear_chartab)
3620 {
3621 /* Clear the char type tables, don't want to use any of the
3622 * currently used spell properties. */
3623 init_spell_chartab();
3624 spin->si_clear_chartab = FALSE;
3625 }
3626
Bram Moolenaar3982c542005-06-08 21:56:31 +00003627 /*
3628 * Don't write a word table for an ASCII file, so that we don't check
3629 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003630 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00003631 * mb_get_class(), the list of chars in the file will be incomplete.
3632 */
3633 if (!spin->si_ascii
3634#ifdef FEAT_MBYTE
3635 && !enc_utf8
3636#endif
3637 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003638 {
3639 if (fol == NULL || low == NULL || upp == NULL)
3640 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
3641 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00003642 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003643 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003644
3645 vim_free(fol);
3646 vim_free(low);
3647 vim_free(upp);
3648 }
3649
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003650 vim_free(pc);
3651 fclose(fd);
3652 return aff;
3653}
3654
3655/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003656 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
3657 * NULL as equal.
3658 */
3659 static int
3660str_equal(s1, s2)
3661 char_u *s1;
3662 char_u *s2;
3663{
3664 if (s1 == NULL || s2 == NULL)
3665 return s1 == s2;
3666 return STRCMP(s1, s2) == 0;
3667}
3668
3669/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003670 * Add a from-to item to "gap". Used for REP and SAL items.
3671 * They are stored case-folded.
3672 */
3673 static void
3674add_fromto(spin, gap, from, to)
3675 spellinfo_T *spin;
3676 garray_T *gap;
3677 char_u *from;
3678 char_u *to;
3679{
3680 fromto_T *ftp;
3681 char_u word[MAXWLEN];
3682
3683 if (ga_grow(gap, 1) == OK)
3684 {
3685 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
3686 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
3687 ftp->ft_from = getroom_save(&spin->si_blocks, word);
3688 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
3689 ftp->ft_to = getroom_save(&spin->si_blocks, word);
3690 ++gap->ga_len;
3691 }
3692}
3693
3694/*
3695 * Convert a boolean argument in a SAL line to TRUE or FALSE;
3696 */
3697 static int
3698sal_to_bool(s)
3699 char_u *s;
3700{
3701 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
3702}
3703
3704/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00003705 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
3706 * When "s" is NULL FALSE is returned.
3707 */
3708 static int
3709has_non_ascii(s)
3710 char_u *s;
3711{
3712 char_u *p;
3713
3714 if (s != NULL)
3715 for (p = s; *p != NUL; ++p)
3716 if (*p >= 128)
3717 return TRUE;
3718 return FALSE;
3719}
3720
3721/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003722 * Free the structure filled by spell_read_aff().
3723 */
3724 static void
3725spell_free_aff(aff)
3726 afffile_T *aff;
3727{
3728 hashtab_T *ht;
3729 hashitem_T *hi;
3730 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003731 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003732 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003733
3734 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003735
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003736 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003737 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
3738 {
3739 todo = ht->ht_used;
3740 for (hi = ht->ht_array; todo > 0; ++hi)
3741 {
3742 if (!HASHITEM_EMPTY(hi))
3743 {
3744 --todo;
3745 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003746 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3747 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003748 }
3749 }
3750 if (ht == &aff->af_suff)
3751 break;
3752 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003753
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003754 hash_clear(&aff->af_pref);
3755 hash_clear(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003756}
3757
3758/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003759 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003760 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003761 */
3762 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00003763spell_read_dic(fname, spin, affile)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003764 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003765 spellinfo_T *spin;
3766 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003767{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003768 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003769 char_u line[MAXLINELEN];
Bram Moolenaar51485f02005-06-04 21:55:20 +00003770 char_u *afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003771 char_u *pfxlist;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003772 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003773 char_u *pc;
3774 char_u *w;
3775 int l;
3776 hash_T hash;
3777 hashitem_T *hi;
3778 FILE *fd;
3779 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003780 int non_ascii = 0;
3781 int retval = OK;
3782 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003783 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003784
Bram Moolenaar51485f02005-06-04 21:55:20 +00003785 /*
3786 * Open the file.
3787 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003788 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003789 if (fd == NULL)
3790 {
3791 EMSG2(_(e_notopen), fname);
3792 return FAIL;
3793 }
3794
Bram Moolenaar51485f02005-06-04 21:55:20 +00003795 /* The hashtable is only used to detect duplicated words. */
3796 hash_init(&ht);
3797
Bram Moolenaar8db73182005-06-17 21:51:16 +00003798 spin->si_foldwcount = 0;
3799 spin->si_keepwcount = 0;
3800
Bram Moolenaarb765d632005-06-07 21:00:02 +00003801 if (spin->si_verbose || p_verbose > 2)
3802 {
3803 if (!spin->si_verbose)
3804 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003805 smsg((char_u *)_("Reading dictionary file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003806 out_flush();
3807 if (!spin->si_verbose)
3808 verbose_leave();
3809 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003810
3811 /* Read and ignore the first line: word count. */
3812 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003813 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003814 EMSG2(_("E760: No word count in %s"), fname);
3815
3816 /*
3817 * Read all the lines in the file one by one.
3818 * The words are converted to 'encoding' here, before being added to
3819 * the hashtable.
3820 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003821 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003822 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003823 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003824 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003825 if (line[0] == '#')
3826 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003827
Bram Moolenaar51485f02005-06-04 21:55:20 +00003828 /* Remove CR, LF and white space from the end. White space halfway
3829 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003830 l = STRLEN(line);
3831 while (l > 0 && line[l - 1] <= ' ')
3832 --l;
3833 if (l == 0)
3834 continue; /* empty line */
3835 line[l] = NUL;
3836
Bram Moolenaar51485f02005-06-04 21:55:20 +00003837 /* Find the optional affix names. */
3838 afflist = vim_strchr(line, '/');
3839 if (afflist != NULL)
3840 *afflist++ = NUL;
3841
3842 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
3843 if (spin->si_ascii && has_non_ascii(line))
3844 {
3845 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003846 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003847 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003848
Bram Moolenaarb765d632005-06-07 21:00:02 +00003849#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003850 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003851 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003852 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003853 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003854 if (pc == NULL)
3855 {
3856 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3857 fname, lnum, line);
3858 continue;
3859 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003860 w = pc;
3861 }
3862 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003863#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003864 {
3865 pc = NULL;
3866 w = line;
3867 }
3868
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003869 /* This takes time, print a message now and then. */
3870 if (spin->si_verbose && (lnum & 0x3ff) == 0)
3871 {
3872 vim_snprintf((char *)message, sizeof(message),
3873 _("line %6d, word %6d - %s"),
3874 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
3875 msg_start();
3876 msg_puts_long_attr(message, 0);
3877 msg_clr_eos();
3878 msg_didout = FALSE;
3879 msg_col = 0;
3880 out_flush();
3881 }
3882
Bram Moolenaar51485f02005-06-04 21:55:20 +00003883 /* Store the word in the hashtable to be able to find duplicates. */
3884 dw = (char_u *)getroom_save(&spin->si_blocks, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003885 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003886 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003887 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003888 if (retval == FAIL)
3889 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003890
Bram Moolenaar51485f02005-06-04 21:55:20 +00003891 hash = hash_hash(dw);
3892 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003893 if (!HASHITEM_EMPTY(hi))
3894 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003895 fname, lnum, dw);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003896 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00003897 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003898
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003899 flags = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003900 pfxlist = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003901 if (afflist != NULL)
3902 {
3903 /* Check for affix name that stands for keep-case word and stands
3904 * for rare word (if defined). */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003905 if (affile->af_kep != NUL
3906 && vim_strchr(afflist, affile->af_kep) != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003907 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003908 if (affile->af_rar != NUL
3909 && vim_strchr(afflist, affile->af_rar) != NULL)
3910 flags |= WF_RARE;
Bram Moolenaar0c405862005-06-22 22:26:26 +00003911 if (affile->af_bad != NUL
3912 && vim_strchr(afflist, affile->af_bad) != NULL)
3913 flags |= WF_BANNED;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003914
3915 if (affile->af_pfxpostpone)
3916 /* Need to store the list of prefix IDs with the word. */
3917 pfxlist = get_pfxlist(affile, afflist, &spin->si_blocks);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003918 }
3919
Bram Moolenaar51485f02005-06-04 21:55:20 +00003920 /* Add the word to the word tree(s). */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003921 if (store_word(dw, spin, flags, spin->si_region, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003922 retval = FAIL;
3923
3924 if (afflist != NULL)
3925 {
3926 /* Find all matching suffixes and add the resulting words.
3927 * Additionally do matching prefixes that combine. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003928 if (store_aff_word(dw, spin, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003929 &affile->af_suff, &affile->af_pref,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003930 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003931 retval = FAIL;
3932
3933 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003934 if (store_aff_word(dw, spin, afflist, affile,
3935 &affile->af_pref, NULL,
3936 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003937 retval = FAIL;
3938 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003939 }
3940
Bram Moolenaar51485f02005-06-04 21:55:20 +00003941 if (spin->si_ascii && non_ascii > 0)
3942 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
3943 non_ascii);
3944 hash_clear(&ht);
3945
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003946 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003947 return retval;
3948}
3949
3950/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003951 * Get the list of prefix IDs from the affix list "afflist".
3952 * Used for PFXPOSTPONE.
3953 * Returns a string allocated with getroom(). NULL when there are no prefixes
3954 * or when out of memory.
3955 */
3956 static char_u *
3957get_pfxlist(affile, afflist, blp)
3958 afffile_T *affile;
3959 char_u *afflist;
3960 sblock_T **blp;
3961{
3962 char_u *p;
3963 int cnt;
3964 int round;
3965 char_u *res = NULL;
3966 char_u key[2];
3967 hashitem_T *hi;
3968
3969 key[1] = NUL;
3970
3971 /* round 1: count the number of prefix IDs.
3972 * round 2: move prefix IDs to "res" */
3973 for (round = 1; round <= 2; ++round)
3974 {
3975 cnt = 0;
3976 for (p = afflist; *p != NUL; ++p)
3977 {
3978 key[0] = *p;
3979 hi = hash_find(&affile->af_pref, key);
3980 if (!HASHITEM_EMPTY(hi))
3981 {
3982 /* This is a prefix ID, use the new number. */
3983 if (round == 2)
3984 res[cnt] = HI2AH(hi)->ah_newID;
3985 ++cnt;
3986 }
3987 }
3988 if (round == 1 && cnt > 0)
3989 res = getroom(blp, cnt + 1);
3990 if (res == NULL)
3991 break;
3992 }
3993
3994 if (res != NULL)
3995 res[cnt] = NUL;
3996 return res;
3997}
3998
3999/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004000 * Apply affixes to a word and store the resulting words.
4001 * "ht" is the hashtable with affentry_T that need to be applied, either
4002 * prefixes or suffixes.
4003 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
4004 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004005 *
4006 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004007 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004008 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004009store_aff_word(word, spin, afflist, affile, ht, xht, comb, flags, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004010 char_u *word; /* basic word start */
4011 spellinfo_T *spin; /* spell info */
4012 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004013 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004014 hashtab_T *ht;
4015 hashtab_T *xht;
4016 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004017 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004018 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004019{
4020 int todo;
4021 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004022 affheader_T *ah;
4023 affentry_T *ae;
4024 regmatch_T regmatch;
4025 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004026 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004027 int i;
4028 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004029 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004030 char_u *use_pfxlist;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004031
Bram Moolenaar51485f02005-06-04 21:55:20 +00004032 todo = ht->ht_used;
4033 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004034 {
4035 if (!HASHITEM_EMPTY(hi))
4036 {
4037 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004038 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00004039
Bram Moolenaar51485f02005-06-04 21:55:20 +00004040 /* Check that the affix combines, if required, and that the word
4041 * supports this affix. */
4042 if ((!comb || ah->ah_combine)
4043 && vim_strchr(afflist, *ah->ah_key) != NULL)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004044 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004045 /* Loop over all affix entries with this name. */
4046 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004047 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004048 /* Check the condition. It's not logical to match case
4049 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004050 * Myspell.
4051 * For prefixes, when "PFXPOSTPONE" was used, only do
4052 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004053 regmatch.regprog = ae->ae_prog;
4054 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004055 if ((xht != NULL || !affile->af_pfxpostpone
4056 || ae->ae_chop != NULL)
4057 && (ae->ae_prog == NULL
4058 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004059 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004060 /* Match. Remove the chop and add the affix. */
4061 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004062 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004063 /* prefix: chop/add at the start of the word */
4064 if (ae->ae_add == NULL)
4065 *newword = NUL;
4066 else
4067 STRCPY(newword, ae->ae_add);
4068 p = word;
4069 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004070 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004071 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004072#ifdef FEAT_MBYTE
4073 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004074 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004075 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004076 for ( ; i > 0; --i)
4077 mb_ptr_adv(p);
4078 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004079 else
4080#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004081 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004082 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004083 STRCAT(newword, p);
4084 }
4085 else
4086 {
4087 /* suffix: chop/add at the end of the word */
4088 STRCPY(newword, word);
4089 if (ae->ae_chop != NULL)
4090 {
4091 /* Remove chop string. */
4092 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004093 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004094 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004095 mb_ptr_back(newword, p);
4096 *p = NUL;
4097 }
4098 if (ae->ae_add != NULL)
4099 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004100 }
4101
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004102 /* Obey the "rare" flag of the affix. */
4103 if (ae->ae_rare)
4104 use_flags = flags | WF_RARE;
4105 else
4106 use_flags = flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004107 use_pfxlist = pfxlist;
4108
4109 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00004110 if (spin->si_prefroot != NULL
4111 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004112 {
4113 /* ... add a flag to indicate an affix was used. */
4114 use_flags |= WF_HAS_AFF;
4115
4116 /* ... don't use a prefix list if combining
4117 * affixes is not allowed */
4118 if (!ah->ah_combine || comb)
4119 use_pfxlist = NULL;
4120 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004121
Bram Moolenaar51485f02005-06-04 21:55:20 +00004122 /* Store the modified word. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004123 if (store_word(newword, spin, use_flags,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004124 spin->si_region, use_pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004125 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004126
Bram Moolenaar51485f02005-06-04 21:55:20 +00004127 /* When added a suffix and combining is allowed also
4128 * try adding prefixes additionally. */
4129 if (xht != NULL && ah->ah_combine)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004130 if (store_aff_word(newword, spin, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004131 xht, NULL, TRUE,
4132 use_flags, use_pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004133 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004134 }
4135 }
4136 }
4137 }
4138 }
4139
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004140 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004141}
4142
4143/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004144 * Read a file with a list of words.
4145 */
4146 static int
4147spell_read_wordfile(fname, spin)
4148 char_u *fname;
4149 spellinfo_T *spin;
4150{
4151 FILE *fd;
4152 long lnum = 0;
4153 char_u rline[MAXLINELEN];
4154 char_u *line;
4155 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004156 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004157 int l;
4158 int retval = OK;
4159 int did_word = FALSE;
4160 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004161 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004162 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004163
4164 /*
4165 * Open the file.
4166 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004167 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00004168 if (fd == NULL)
4169 {
4170 EMSG2(_(e_notopen), fname);
4171 return FAIL;
4172 }
4173
Bram Moolenaarb765d632005-06-07 21:00:02 +00004174 if (spin->si_verbose || p_verbose > 2)
4175 {
4176 if (!spin->si_verbose)
4177 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004178 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004179 out_flush();
4180 if (!spin->si_verbose)
4181 verbose_leave();
4182 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004183
4184 /*
4185 * Read all the lines in the file one by one.
4186 */
4187 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
4188 {
4189 line_breakcheck();
4190 ++lnum;
4191
4192 /* Skip comment lines. */
4193 if (*rline == '#')
4194 continue;
4195
4196 /* Remove CR, LF and white space from the end. */
4197 l = STRLEN(rline);
4198 while (l > 0 && rline[l - 1] <= ' ')
4199 --l;
4200 if (l == 0)
4201 continue; /* empty or blank line */
4202 rline[l] = NUL;
4203
4204 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
4205 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004206#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004207 if (spin->si_conv.vc_type != CONV_NONE)
4208 {
4209 pc = string_convert(&spin->si_conv, rline, NULL);
4210 if (pc == NULL)
4211 {
4212 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4213 fname, lnum, rline);
4214 continue;
4215 }
4216 line = pc;
4217 }
4218 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004219#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004220 {
4221 pc = NULL;
4222 line = rline;
4223 }
4224
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004225 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00004226 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004227 ++line;
4228 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004229 {
4230 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004231 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
4232 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004233 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004234 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
4235 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004236 else
4237 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004238#ifdef FEAT_MBYTE
4239 char_u *enc;
4240
Bram Moolenaar51485f02005-06-04 21:55:20 +00004241 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004242 line += 10;
4243 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004244 if (enc != NULL && !spin->si_ascii
4245 && convert_setup(&spin->si_conv, enc,
4246 p_enc) == FAIL)
4247 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00004248 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004249 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004250 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004251#else
4252 smsg((char_u *)_("Conversion in %s not supported"), fname);
4253#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004254 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004255 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004256 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004257
Bram Moolenaar3982c542005-06-08 21:56:31 +00004258 if (STRNCMP(line, "regions=", 8) == 0)
4259 {
4260 if (spin->si_region_count > 1)
4261 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
4262 fname, lnum, line);
4263 else
4264 {
4265 line += 8;
4266 if (STRLEN(line) > 16)
4267 smsg((char_u *)_("Too many regions in %s line %d: %s"),
4268 fname, lnum, line);
4269 else
4270 {
4271 spin->si_region_count = STRLEN(line) / 2;
4272 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004273
4274 /* Adjust the mask for a word valid in all regions. */
4275 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004276 }
4277 }
4278 continue;
4279 }
4280
Bram Moolenaar7887d882005-07-01 22:33:52 +00004281 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
4282 fname, lnum, line - 1);
4283 continue;
4284 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004285
Bram Moolenaar7887d882005-07-01 22:33:52 +00004286 flags = 0;
4287 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004288
Bram Moolenaar7887d882005-07-01 22:33:52 +00004289 /* Check for flags and region after a slash. */
4290 p = vim_strchr(line, '/');
4291 if (p != NULL)
4292 {
4293 *p++ = NUL;
4294 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004295 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004296 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004297 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004298 else if (*p == '!') /* Bad, bad, wicked word. */
4299 flags |= WF_BANNED;
4300 else if (*p == '?') /* Rare word. */
4301 flags |= WF_RARE;
4302 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004303 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004304 if ((flags & WF_REGION) == 0) /* first one */
4305 regionmask = 0;
4306 flags |= WF_REGION;
4307
4308 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00004309 if (l > spin->si_region_count)
4310 {
4311 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00004312 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004313 break;
4314 }
4315 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004316 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00004317 else
4318 {
4319 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
4320 fname, lnum, p);
4321 break;
4322 }
4323 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004324 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004325 }
4326
4327 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
4328 if (spin->si_ascii && has_non_ascii(line))
4329 {
4330 ++non_ascii;
4331 continue;
4332 }
4333
4334 /* Normal word: store it. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004335 if (store_word(line, spin, flags, regionmask, NULL) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004336 {
4337 retval = FAIL;
4338 break;
4339 }
4340 did_word = TRUE;
4341 }
4342
4343 vim_free(pc);
4344 fclose(fd);
4345
Bram Moolenaarb765d632005-06-07 21:00:02 +00004346 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
4347 {
4348 if (p_verbose > 2)
4349 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00004350 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
4351 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004352 if (p_verbose > 2)
4353 verbose_leave();
4354 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004355 return retval;
4356}
4357
4358/*
4359 * Get part of an sblock_T, "len" bytes long.
4360 * This avoids calling free() for every little struct we use.
4361 * The memory is cleared to all zeros.
4362 * Returns NULL when out of memory.
4363 */
4364 static void *
4365getroom(blp, len)
4366 sblock_T **blp;
4367 size_t len; /* length needed */
4368{
4369 char_u *p;
4370 sblock_T *bl = *blp;
4371
4372 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
4373 {
4374 /* Allocate a block of memory. This is not freed until much later. */
4375 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
4376 if (bl == NULL)
4377 return NULL;
4378 bl->sb_next = *blp;
4379 *blp = bl;
4380 bl->sb_used = 0;
4381 }
4382
4383 p = bl->sb_data + bl->sb_used;
4384 bl->sb_used += len;
4385
4386 return p;
4387}
4388
4389/*
4390 * Make a copy of a string into memory allocated with getroom().
4391 */
4392 static char_u *
4393getroom_save(blp, s)
4394 sblock_T **blp;
4395 char_u *s;
4396{
4397 char_u *sc;
4398
4399 sc = (char_u *)getroom(blp, STRLEN(s) + 1);
4400 if (sc != NULL)
4401 STRCPY(sc, s);
4402 return sc;
4403}
4404
4405
4406/*
4407 * Free the list of allocated sblock_T.
4408 */
4409 static void
4410free_blocks(bl)
4411 sblock_T *bl;
4412{
4413 sblock_T *next;
4414
4415 while (bl != NULL)
4416 {
4417 next = bl->sb_next;
4418 vim_free(bl);
4419 bl = next;
4420 }
4421}
4422
4423/*
4424 * Allocate the root of a word tree.
4425 */
4426 static wordnode_T *
4427wordtree_alloc(blp)
4428 sblock_T **blp;
4429{
4430 return (wordnode_T *)getroom(blp, sizeof(wordnode_T));
4431}
4432
4433/*
4434 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004435 * Always store it in the case-folded tree. For a keep-case word this is
4436 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
4437 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004438 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004439 * When "pfxlist" is not NULL store the word for each postponed prefix ID.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004440 */
4441 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004442store_word(word, spin, flags, region, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004443 char_u *word;
4444 spellinfo_T *spin;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004445 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004446 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004447 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004448{
4449 int len = STRLEN(word);
4450 int ct = captype(word, word + len);
4451 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004452 int res = OK;
4453 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004454
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004455 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004456 for (p = pfxlist; res == OK; ++p)
4457 {
4458 res = tree_add_word(foldword, spin->si_foldroot, ct | flags,
4459 region, p == NULL ? 0 : *p, &spin->si_blocks);
4460 if (p == NULL || *p == NUL)
4461 break;
4462 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00004463 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004464
4465 if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP))
Bram Moolenaar8db73182005-06-17 21:51:16 +00004466 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004467 for (p = pfxlist; res == OK; ++p)
4468 {
4469 res = tree_add_word(word, spin->si_keeproot, flags,
4470 region, p == NULL ? 0 : *p, &spin->si_blocks);
4471 if (p == NULL || *p == NUL)
4472 break;
4473 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00004474 ++spin->si_keepwcount;
4475 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004476 return res;
4477}
4478
4479/*
4480 * Add word "word" to a word tree at "root".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004481 * When "flags" < 0 we are adding to the prefix tree where flags is used for
4482 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004483 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004484 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004485 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004486tree_add_word(word, root, flags, region, prefixID, blp)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004487 char_u *word;
4488 wordnode_T *root;
4489 int flags;
4490 int region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004491 int prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004492 sblock_T **blp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004493{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004494 wordnode_T *node = root;
4495 wordnode_T *np;
4496 wordnode_T **prev = NULL;
4497 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004498
Bram Moolenaar51485f02005-06-04 21:55:20 +00004499 /* Add each byte of the word to the tree, including the NUL at the end. */
4500 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004501 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004502 /* Look for the sibling that has the same character. They are sorted
4503 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004504 * higher byte value. For zero bytes (end of word) the sorting is
4505 * done on flags and then on prefixID
Bram Moolenaar51485f02005-06-04 21:55:20 +00004506 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004507 while (node != NULL
4508 && (node->wn_byte < word[i]
4509 || (node->wn_byte == NUL
4510 && (flags < 0
4511 ? node->wn_prefixID < prefixID
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004512 : node->wn_flags < (flags & WN_MASK)
4513 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004514 && node->wn_prefixID < prefixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004515 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004516 prev = &node->wn_sibling;
4517 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004518 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004519 if (node == NULL
4520 || node->wn_byte != word[i]
4521 || (word[i] == NUL
4522 && (flags < 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004523 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004524 || node->wn_prefixID != prefixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004525 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004526 /* Allocate a new node. */
4527 np = (wordnode_T *)getroom(blp, sizeof(wordnode_T));
4528 if (np == NULL)
4529 return FAIL;
4530 np->wn_byte = word[i];
4531 *prev = np;
4532 np->wn_sibling = node;
4533 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004534 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004535
Bram Moolenaar51485f02005-06-04 21:55:20 +00004536 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004537 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004538 node->wn_flags = flags;
4539 node->wn_region |= region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004540 node->wn_prefixID = prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004541 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004542 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004543 prev = &node->wn_child;
4544 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004545 }
4546
4547 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004548}
4549
4550/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004551 * Compress a tree: find tails that are identical and can be shared.
4552 */
4553 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00004554wordtree_compress(root, spin)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004555 wordnode_T *root;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004556 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004557{
4558 hashtab_T ht;
4559 int n;
4560 int tot = 0;
4561
4562 if (root != NULL)
4563 {
4564 hash_init(&ht);
4565 n = node_compress(root, &ht, &tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004566 if (spin->si_verbose || p_verbose > 2)
4567 {
4568 if (!spin->si_verbose)
4569 verbose_enter();
4570 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar51485f02005-06-04 21:55:20 +00004571 n, tot, (tot - n) * 100 / tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004572 if (p_verbose > 2)
4573 verbose_leave();
4574 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004575 hash_clear(&ht);
4576 }
4577}
4578
4579/*
4580 * Compress a node, its siblings and its children, depth first.
4581 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004582 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004583 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00004584node_compress(node, ht, tot)
4585 wordnode_T *node;
4586 hashtab_T *ht;
4587 int *tot; /* total count of nodes before compressing,
4588 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004589{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004590 wordnode_T *np;
4591 wordnode_T *tp;
4592 wordnode_T *child;
4593 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004594 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004595 int len = 0;
4596 unsigned nr, n;
4597 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004598
Bram Moolenaar51485f02005-06-04 21:55:20 +00004599 /*
4600 * Go through the list of siblings. Compress each child and then try
4601 * finding an identical child to replace it.
4602 * Note that with "child" we mean not just the node that is pointed to,
4603 * but the whole list of siblings, of which the node is the first.
4604 */
4605 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004606 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004607 ++len;
4608 if ((child = np->wn_child) != NULL)
4609 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00004610 /* Compress the child. This fills hashkey. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004611 compressed += node_compress(child, ht, tot);
4612
4613 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004614 hash = hash_hash(child->wn_u1.hashkey);
4615 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004616 tp = NULL;
4617 if (!HASHITEM_EMPTY(hi))
4618 {
4619 /* There are children with an identical hash value. Now check
4620 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004621 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004622 if (node_equal(child, tp))
4623 {
4624 /* Found one! Now use that child in place of the
4625 * current one. This means the current child is
4626 * dropped from the tree. */
4627 np->wn_child = tp;
4628 ++compressed;
4629 break;
4630 }
4631 if (tp == NULL)
4632 {
4633 /* No other child with this hash value equals the child of
4634 * the node, add it to the linked list after the first
4635 * item. */
4636 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004637 child->wn_u2.next = tp->wn_u2.next;
4638 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004639 }
4640 }
4641 else
4642 /* No other child has this hash value, add it to the
4643 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004644 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004645 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004646 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004647 *tot += len;
4648
4649 /*
4650 * Make a hash key for the node and its siblings, so that we can quickly
4651 * find a lookalike node. This must be done after compressing the sibling
4652 * list, otherwise the hash key would become invalid by the compression.
4653 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004654 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004655 nr = 0;
4656 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004657 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004658 if (np->wn_byte == NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004659 /* end node: use wn_flags, wn_region and wn_prefixID */
4660 n = np->wn_flags + (np->wn_region << 8) + (np->wn_prefixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004661 else
4662 /* byte node: use the byte value and the child pointer */
4663 n = np->wn_byte + ((long_u)np->wn_child << 8);
4664 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004665 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004666
4667 /* Avoid NUL bytes, it terminates the hash key. */
4668 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004669 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004670 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004671 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004672 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004673 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004674 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004675 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
4676 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004677
4678 return compressed;
4679}
4680
4681/*
4682 * Return TRUE when two nodes have identical siblings and children.
4683 */
4684 static int
4685node_equal(n1, n2)
4686 wordnode_T *n1;
4687 wordnode_T *n2;
4688{
4689 wordnode_T *p1;
4690 wordnode_T *p2;
4691
4692 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
4693 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
4694 if (p1->wn_byte != p2->wn_byte
4695 || (p1->wn_byte == NUL
4696 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004697 || p1->wn_region != p2->wn_region
4698 || p1->wn_prefixID != p2->wn_prefixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004699 : (p1->wn_child != p2->wn_child)))
4700 break;
4701
4702 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004703}
4704
4705/*
4706 * Write a number to file "fd", MSB first, in "len" bytes.
4707 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004708 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004709put_bytes(fd, nr, len)
4710 FILE *fd;
4711 long_u nr;
4712 int len;
4713{
4714 int i;
4715
4716 for (i = len - 1; i >= 0; --i)
4717 putc((int)(nr >> (i * 8)), fd);
4718}
4719
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004720static int
4721#ifdef __BORLANDC__
4722_RTLENTRYF
4723#endif
4724rep_compare __ARGS((const void *s1, const void *s2));
4725
4726/*
4727 * Function given to qsort() to sort the REP items on "from" string.
4728 */
4729 static int
4730#ifdef __BORLANDC__
4731_RTLENTRYF
4732#endif
4733rep_compare(s1, s2)
4734 const void *s1;
4735 const void *s2;
4736{
4737 fromto_T *p1 = (fromto_T *)s1;
4738 fromto_T *p2 = (fromto_T *)s2;
4739
4740 return STRCMP(p1->ft_from, p2->ft_from);
4741}
4742
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004743/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004744 * Write the Vim spell file "fname".
4745 */
4746 static void
Bram Moolenaar3982c542005-06-08 21:56:31 +00004747write_vim_spell(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004748 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004749 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004750{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004751 FILE *fd;
4752 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004753 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004754 wordnode_T *tree;
4755 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004756 int i;
4757 int l;
4758 garray_T *gap;
4759 fromto_T *ftp;
4760 char_u *p;
4761 int rr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004762
Bram Moolenaarb765d632005-06-07 21:00:02 +00004763 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00004764 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004765 {
4766 EMSG2(_(e_notopen), fname);
4767 return;
4768 }
4769
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004770 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004771 * <charflagslen> <charflags>
4772 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004773 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004774 * <prefcondcnt> <prefcond> ... */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004775
4776 /* <fileID> */
4777 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
4778 EMSG(_(e_write));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004779
4780 /* write the region names if there is more than one */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004781 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004782 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004783 putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */
4784 fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2),
4785 (size_t)1, fd);
4786 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004787 }
4788 else
4789 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004790 putc(0, fd);
4791 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004792 }
4793
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004794 /*
4795 * Write the table with character flags and table for case folding.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004796 * <charflagslen> <charflags> <fcharlen> <fchars>
4797 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004798 * 'encoding'.
4799 * Also skip this for an .add.spl file, the main spell file must contain
4800 * the table (avoids that it conflicts). File is shorter too.
4801 */
4802 if (spin->si_ascii || spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004803 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004804 putc(0, fd);
4805 putc(0, fd);
4806 putc(0, fd);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004807 }
4808 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00004809 write_spell_chartab(fd);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004810
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004811
4812 if (spin->si_midword == NULL)
4813 put_bytes(fd, 0L, 2); /* <midwordlen> */
4814 else
4815 {
4816 i = STRLEN(spin->si_midword);
4817 put_bytes(fd, (long_u)i, 2); /* <midwordlen> */
4818 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
4819 }
4820
4821
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004822 /* Write the prefix conditions. */
4823 write_spell_prefcond(fd, &spin->si_prefcond);
4824
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004825 /* <SUGGEST> : <repcount> <rep> ...
4826 * <salflags> <salcount> <sal> ...
4827 * <maplen> <mapstr> */
4828
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004829 /* Sort the REP items. */
4830 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
4831 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004832
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004833 /* round 1: REP items
4834 * round 2: SAL items (unless SOFO is used) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004835 for (round = 1; round <= 2; ++round)
4836 {
4837 if (round == 1)
4838 gap = &spin->si_rep;
4839 else
4840 {
4841 gap = &spin->si_sal;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004842
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004843 i = 0;
4844 if (spin->si_followup)
4845 i |= SAL_F0LLOWUP;
4846 if (spin->si_collapse)
4847 i |= SAL_COLLAPSE;
4848 if (spin->si_rem_accents)
4849 i |= SAL_REM_ACCENTS;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004850 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
4851 i |= SAL_SOFO;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004852 putc(i, fd); /* <salflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004853 if (i & SAL_SOFO)
4854 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004855 }
4856
4857 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
4858 for (i = 0; i < gap->ga_len; ++i)
4859 {
4860 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
4861 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
4862 ftp = &((fromto_T *)gap->ga_data)[i];
4863 for (rr = 1; rr <= 2; ++rr)
4864 {
4865 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
4866 l = STRLEN(p);
4867 putc(l, fd);
4868 fwrite(p, l, (size_t)1, fd);
4869 }
4870 }
4871 }
4872
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004873 /* SOFOFROM and SOFOTO */
4874 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
4875 {
4876 put_bytes(fd, 1L, 2); /* <salcount> */
4877
4878 l = STRLEN(spin->si_sofofr);
4879 put_bytes(fd, (long_u)l, 2); /* <salfromlen> */
4880 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <salfrom> */
4881
4882 l = STRLEN(spin->si_sofoto);
4883 put_bytes(fd, (long_u)l, 2); /* <saltolen> */
4884 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <salto> */
4885 }
4886
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004887 put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */
4888 if (spin->si_map.ga_len > 0) /* <mapstr> */
4889 fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len,
4890 (size_t)1, fd);
Bram Moolenaar50cde822005-06-05 21:54:54 +00004891
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004892 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004893 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004894 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004895 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004896 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004897 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004898 if (round == 1)
4899 tree = spin->si_foldroot;
4900 else if (round == 2)
4901 tree = spin->si_keeproot;
4902 else
4903 tree = spin->si_prefroot;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004904
Bram Moolenaar0c405862005-06-22 22:26:26 +00004905 /* Clear the index and wnode fields in the tree. */
4906 clear_node(tree);
4907
Bram Moolenaar51485f02005-06-04 21:55:20 +00004908 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00004909 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00004910 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004911 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004912
Bram Moolenaar51485f02005-06-04 21:55:20 +00004913 /* number of nodes in 4 bytes */
4914 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004915 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004916
Bram Moolenaar51485f02005-06-04 21:55:20 +00004917 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004918 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004919 }
4920
Bram Moolenaar51485f02005-06-04 21:55:20 +00004921 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004922}
4923
4924/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00004925 * Clear the index and wnode fields of "node", it siblings and its
4926 * children. This is needed because they are a union with other items to save
4927 * space.
4928 */
4929 static void
4930clear_node(node)
4931 wordnode_T *node;
4932{
4933 wordnode_T *np;
4934
4935 if (node != NULL)
4936 for (np = node; np != NULL; np = np->wn_sibling)
4937 {
4938 np->wn_u1.index = 0;
4939 np->wn_u2.wnode = NULL;
4940
4941 if (np->wn_byte != NUL)
4942 clear_node(np->wn_child);
4943 }
4944}
4945
4946
4947/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004948 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004949 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00004950 * This first writes the list of possible bytes (siblings). Then for each
4951 * byte recursively write the children.
4952 *
4953 * NOTE: The code here must match the code in read_tree(), since assumptions
4954 * are made about the indexes (so that we don't have to write them in the
4955 * file).
4956 *
4957 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004958 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004959 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00004960put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004961 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004962 wordnode_T *node;
4963 int index;
4964 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004965 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004966{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004967 int newindex = index;
4968 int siblingcount = 0;
4969 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004970 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004971
Bram Moolenaar51485f02005-06-04 21:55:20 +00004972 /* If "node" is zero the tree is empty. */
4973 if (node == NULL)
4974 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004975
Bram Moolenaar51485f02005-06-04 21:55:20 +00004976 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004977 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004978
4979 /* Count the number of siblings. */
4980 for (np = node; np != NULL; np = np->wn_sibling)
4981 ++siblingcount;
4982
4983 /* Write the sibling count. */
4984 if (fd != NULL)
4985 putc(siblingcount, fd); /* <siblingcount> */
4986
4987 /* Write each sibling byte and optionally extra info. */
4988 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004989 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004990 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004991 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004992 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004993 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004994 /* For a NUL byte (end of word) write the flags etc. */
4995 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004996 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004997 /* In PREFIXTREE write the required prefixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004998 * associated condition nr (stored in wn_region). The
4999 * byte value is misused to store the "rare" and "not
5000 * combining" flags */
5001 if (np->wn_flags == (short_u)PFX_FLAGS_R)
5002 putc(BY_FLAGS, fd); /* <byte> */
5003 else if (np->wn_flags == (short_u)PFX_FLAGS_NC)
5004 putc(BY_PFX_NC, fd);
5005 else if (np->wn_flags == (short_u)PFX_FLAGS_RNC)
5006 putc(BY_PFX_RNC, fd);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005007 else
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005008 putc(BY_NOFLAGS, fd);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005009 putc(np->wn_prefixID, fd); /* <prefixID> */
5010 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005011 }
5012 else
5013 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005014 /* For word trees we write the flag/region items. */
5015 flags = np->wn_flags;
5016 if (regionmask != 0 && np->wn_region != regionmask)
5017 flags |= WF_REGION;
5018 if (np->wn_prefixID != 0)
5019 flags |= WF_PFX;
5020 if (flags == 0)
5021 {
5022 /* word without flags or region */
5023 putc(BY_NOFLAGS, fd); /* <byte> */
5024 }
5025 else
5026 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005027 if (np->wn_flags >= 0x100)
5028 {
5029 putc(BY_FLAGS2, fd); /* <byte> */
5030 putc(flags, fd); /* <flags> */
5031 putc((unsigned)flags >> 8, fd); /* <flags2> */
5032 }
5033 else
5034 {
5035 putc(BY_FLAGS, fd); /* <byte> */
5036 putc(flags, fd); /* <flags> */
5037 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005038 if (flags & WF_REGION)
5039 putc(np->wn_region, fd); /* <region> */
5040 if (flags & WF_PFX)
5041 putc(np->wn_prefixID, fd); /* <prefixID> */
5042 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005043 }
5044 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005045 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005046 else
5047 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005048 if (np->wn_child->wn_u1.index != 0
5049 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005050 {
5051 /* The child is written elsewhere, write the reference. */
5052 if (fd != NULL)
5053 {
5054 putc(BY_INDEX, fd); /* <byte> */
5055 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005056 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005057 }
5058 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00005059 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005060 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005061 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005062
Bram Moolenaar51485f02005-06-04 21:55:20 +00005063 if (fd != NULL)
5064 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
5065 {
5066 EMSG(_(e_write));
5067 return 0;
5068 }
5069 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005070 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005071
5072 /* Space used in the array when reading: one for each sibling and one for
5073 * the count. */
5074 newindex += siblingcount + 1;
5075
5076 /* Recursively dump the children of each sibling. */
5077 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005078 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
5079 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005080 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005081
5082 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005083}
5084
5085
5086/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00005087 * ":mkspell [-ascii] outfile infile ..."
5088 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005089 */
5090 void
5091ex_mkspell(eap)
5092 exarg_T *eap;
5093{
5094 int fcount;
5095 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005096 char_u *arg = eap->arg;
5097 int ascii = FALSE;
5098
5099 if (STRNCMP(arg, "-ascii", 6) == 0)
5100 {
5101 ascii = TRUE;
5102 arg = skipwhite(arg + 6);
5103 }
5104
5105 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
5106 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
5107 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005108 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005109 FreeWild(fcount, fnames);
5110 }
5111}
5112
5113/*
5114 * Create a Vim spell file from one or more word lists.
5115 * "fnames[0]" is the output file name.
5116 * "fnames[fcount - 1]" is the last input file name.
5117 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
5118 * and ".spl" is appended to make the output file name.
5119 */
5120 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005121mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005122 int fcount;
5123 char_u **fnames;
5124 int ascii; /* -ascii argument given */
5125 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005126 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005127{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005128 char_u fname[MAXPATHL];
5129 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00005130 char_u **innames;
5131 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005132 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005133 int i;
5134 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005135 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005136 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005137 spellinfo_T spin;
5138
5139 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005140 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005141 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005142 spin.si_followup = TRUE;
5143 spin.si_rem_accents = TRUE;
5144 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
5145 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
5146 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005147 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005148
Bram Moolenaarb765d632005-06-07 21:00:02 +00005149 /* default: fnames[0] is output file, following are input files */
5150 innames = &fnames[1];
5151 incount = fcount - 1;
5152
5153 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005154 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005155 len = STRLEN(fnames[0]);
5156 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
5157 {
5158 /* For ":mkspell path/en.latin1.add" output file is
5159 * "path/en.latin1.add.spl". */
5160 innames = &fnames[0];
5161 incount = 1;
5162 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
5163 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005164 else if (fcount == 1)
5165 {
5166 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
5167 innames = &fnames[0];
5168 incount = 1;
5169 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
5170 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5171 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005172 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
5173 {
5174 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005175 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005176 }
5177 else
5178 /* Name should be language, make the file name from it. */
5179 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
5180 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5181
5182 /* Check for .ascii.spl. */
5183 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
5184 spin.si_ascii = TRUE;
5185
5186 /* Check for .add.spl. */
5187 if (strstr((char *)gettail(wfname), ".add.") != NULL)
5188 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005189 }
5190
Bram Moolenaarb765d632005-06-07 21:00:02 +00005191 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005192 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005193 else if (vim_strchr(gettail(wfname), '_') != NULL)
5194 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005195 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005196 EMSG(_("E754: Only up to 8 regions supported"));
5197 else
5198 {
5199 /* Check for overwriting before doing things that may take a lot of
5200 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005201 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005202 {
5203 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005204 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005205 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005206 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005207 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005208 EMSG2(_(e_isadir2), wfname);
5209 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005210 }
5211
5212 /*
5213 * Init the aff and dic pointers.
5214 * Get the region names if there are more than 2 arguments.
5215 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005216 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005217 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005218 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005219
Bram Moolenaar3982c542005-06-08 21:56:31 +00005220 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005221 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005222 len = STRLEN(innames[i]);
5223 if (STRLEN(gettail(innames[i])) < 5
5224 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005225 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005226 EMSG2(_("E755: Invalid region in %s"), innames[i]);
5227 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005228 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005229 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
5230 spin.si_region_name[i * 2 + 1] =
5231 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005232 }
5233 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005234 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005235
Bram Moolenaar51485f02005-06-04 21:55:20 +00005236 spin.si_foldroot = wordtree_alloc(&spin.si_blocks);
5237 spin.si_keeproot = wordtree_alloc(&spin.si_blocks);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005238 spin.si_prefroot = wordtree_alloc(&spin.si_blocks);
5239 if (spin.si_foldroot == NULL
5240 || spin.si_keeproot == NULL
5241 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005242 {
5243 error = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005244 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005245 }
5246
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005247 /* When not producing a .add.spl file clear the character table when
5248 * we encounter one in the .aff file. This means we dump the current
5249 * one in the .spl file if the .aff file doesn't define one. That's
5250 * better than guessing the contents, the table will match a
5251 * previously loaded spell file. */
5252 if (!spin.si_add)
5253 spin.si_clear_chartab = TRUE;
5254
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005255 /*
5256 * Read all the .aff and .dic files.
5257 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00005258 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005259 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005260 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005261 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005262 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005263 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005264
Bram Moolenaarb765d632005-06-07 21:00:02 +00005265 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005266 if (mch_stat((char *)fname, &st) >= 0)
5267 {
5268 /* Read the .aff file. Will init "spin->si_conv" based on the
5269 * "SET" line. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005270 afile[i] = spell_read_aff(fname, &spin);
5271 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005272 error = TRUE;
5273 else
5274 {
5275 /* Read the .dic file and store the words in the trees. */
5276 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00005277 innames[i]);
5278 if (spell_read_dic(fname, &spin, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005279 error = TRUE;
5280 }
5281 }
5282 else
5283 {
5284 /* No .aff file, try reading the file as a word list. Store
5285 * the words in the trees. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005286 if (spell_read_wordfile(innames[i], &spin) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005287 error = TRUE;
5288 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005289
Bram Moolenaarb765d632005-06-07 21:00:02 +00005290#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005291 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005292 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005293#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005294 }
5295
Bram Moolenaar51485f02005-06-04 21:55:20 +00005296 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005297 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005298 /*
5299 * Remove the dummy NUL from the start of the tree root.
5300 */
5301 spin.si_foldroot = spin.si_foldroot->wn_sibling;
5302 spin.si_keeproot = spin.si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005303 spin.si_prefroot = spin.si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005304
5305 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005306 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005307 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005308 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005310 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005311 verbose_enter();
5312 MSG(_("Compressing word tree..."));
5313 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005314 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005315 verbose_leave();
5316 }
5317 wordtree_compress(spin.si_foldroot, &spin);
5318 wordtree_compress(spin.si_keeproot, &spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005319 wordtree_compress(spin.si_prefroot, &spin);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005320 }
5321
Bram Moolenaar51485f02005-06-04 21:55:20 +00005322 if (!error)
5323 {
5324 /*
5325 * Write the info in the spell file.
5326 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005327 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005329 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005330 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005331 smsg((char_u *)_("Writing spell file %s ..."), wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005332 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005333 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005334 verbose_leave();
5335 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00005336
Bram Moolenaar3982c542005-06-08 21:56:31 +00005337 write_vim_spell(wfname, &spin);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005338
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005339 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005341 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005342 verbose_enter();
5343 MSG(_("Done!"));
5344 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00005345 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005346 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005347 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005348 verbose_leave();
5349 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005350
Bram Moolenaarb765d632005-06-07 21:00:02 +00005351 /* If the file is loaded need to reload it. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005352 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005353 }
5354
5355 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005356 ga_clear(&spin.si_rep);
5357 ga_clear(&spin.si_sal);
5358 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005359 ga_clear(&spin.si_prefcond);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005360 vim_free(spin.si_midword);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005361 vim_free(spin.si_sofofr);
5362 vim_free(spin.si_sofoto);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005363
5364 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005365 for (i = 0; i < incount; ++i)
5366 if (afile[i] != NULL)
5367 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005368
5369 /* Free all the bits and pieces at once. */
5370 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005371 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005372}
5373
Bram Moolenaarb765d632005-06-07 21:00:02 +00005374
5375/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005376 * ":[count]spellgood {word}"
5377 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00005378 */
5379 void
5380ex_spell(eap)
5381 exarg_T *eap;
5382{
Bram Moolenaar7887d882005-07-01 22:33:52 +00005383 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005384 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005385}
5386
5387/*
5388 * Add "word[len]" to 'spellfile' as a good or bad word.
5389 */
5390 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005391spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005392 char_u *word;
5393 int len;
5394 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005395 int index; /* "zG" and "zW": zero, otherwise index in
5396 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005397{
5398 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005399 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005400 int new_spf = FALSE;
5401 struct stat st;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005402 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005403 char_u fnamebuf[MAXPATHL];
5404 char_u line[MAXWLEN * 2];
5405 long fpos, fpos_next = 0;
5406 int i;
5407 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005408
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005409 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005410 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005411 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00005412 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005413 int_wordlist = vim_tempname('s');
5414 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00005415 return;
5416 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005417 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005418 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005419 else
5420 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005421 /* If 'spellfile' isn't set figure out a good default value. */
5422 if (*curbuf->b_p_spf == NUL)
5423 {
5424 init_spellfile();
5425 new_spf = TRUE;
5426 }
5427
5428 if (*curbuf->b_p_spf == NUL)
5429 {
5430 EMSG(_("E764: 'spellfile' is not set"));
5431 return;
5432 }
5433
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005434 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
5435 {
5436 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
5437 if (i == index)
5438 break;
5439 if (*spf == NUL)
5440 {
5441 EMSGN(_("E765: 'spellfile' does not have %ld enties"), index);
5442 return;
5443 }
5444 }
5445
Bram Moolenaarb765d632005-06-07 21:00:02 +00005446 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005447 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005448 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
5449 buf = NULL;
5450 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00005451 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005452 EMSG(_(e_bufloaded));
5453 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005454 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00005455
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005456 fname = fnamebuf;
5457 }
5458
5459 if (bad)
5460 {
5461 /* When the word also appears as good word we need to remove that one,
5462 * since its flags sort before the one with WF_BANNED. */
5463 fd = mch_fopen((char *)fname, "r");
5464 if (fd != NULL)
5465 {
5466 while (!vim_fgets(line, MAXWLEN * 2, fd))
5467 {
5468 fpos = fpos_next;
5469 fpos_next = ftell(fd);
5470 if (STRNCMP(word, line, len) == 0
5471 && (line[len] == '/' || line[len] < ' '))
5472 {
5473 /* Found duplicate word. Remove it by writing a '#' at
5474 * the start of the line. Mixing reading and writing
5475 * doesn't work for all systems, close the file first. */
5476 fclose(fd);
5477 fd = mch_fopen((char *)fname, "r+");
5478 if (fd == NULL)
5479 break;
5480 if (fseek(fd, fpos, SEEK_SET) == 0)
5481 fputc('#', fd);
5482 fseek(fd, fpos_next, SEEK_SET);
5483 }
5484 }
5485 fclose(fd);
5486 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00005487 }
5488
5489 fd = mch_fopen((char *)fname, "a");
5490 if (fd == NULL && new_spf)
5491 {
5492 /* We just initialized the 'spellfile' option and can't open the file.
5493 * We may need to create the "spell" directory first. We already
5494 * checked the runtime directory is writable in init_spellfile(). */
5495 STRCPY(NameBuff, fname);
5496 *gettail_sep(NameBuff) = NUL;
5497 if (mch_stat((char *)NameBuff, &st) < 0)
5498 {
5499 /* The directory doesn't exist. Try creating it and opening the
5500 * file again. */
5501 vim_mkdir(NameBuff, 0755);
5502 fd = mch_fopen((char *)fname, "a");
5503 }
5504 }
5505
5506 if (fd == NULL)
5507 EMSG2(_(e_notopen), fname);
5508 else
5509 {
5510 if (bad)
5511 fprintf(fd, "%.*s/!\n", len, word);
5512 else
5513 fprintf(fd, "%.*s\n", len, word);
5514 fclose(fd);
5515
5516 /* Update the .add.spl file. */
5517 mkspell(1, &fname, FALSE, TRUE, TRUE);
5518
5519 /* If the .add file is edited somewhere, reload it. */
5520 if (buf != NULL)
5521 buf_reload(buf);
5522
5523 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005524 }
5525}
5526
5527/*
5528 * Initialize 'spellfile' for the current buffer.
5529 */
5530 static void
5531init_spellfile()
5532{
5533 char_u buf[MAXPATHL];
5534 int l;
5535 slang_T *sl;
5536 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005537 char_u *lend;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005538
5539 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
5540 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005541 /* Find the end of the language name. Exclude the region. */
5542 for (lend = curbuf->b_p_spl; *lend != NUL
5543 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
5544 ;
5545
5546 /* Loop over all entries in 'runtimepath'. Use the first one where we
5547 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005548 rtp = p_rtp;
5549 while (*rtp != NUL)
5550 {
5551 /* Copy the path from 'runtimepath' to buf[]. */
5552 copy_option_part(&rtp, buf, MAXPATHL, ",");
5553 if (filewritable(buf) == 2)
5554 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00005555 /* Use the first language name from 'spelllang' and the
5556 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005557 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
5558 l = STRLEN(buf);
5559 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00005560 "/spell/%.*s.%s.add",
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005561 (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00005562 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
5563 ? (char_u *)"ascii" : spell_enc());
5564 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
5565 break;
5566 }
5567 }
5568 }
5569}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005570
Bram Moolenaar51485f02005-06-04 21:55:20 +00005571
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005572/*
5573 * Init the chartab used for spelling for ASCII.
5574 * EBCDIC is not supported!
5575 */
5576 static void
5577clear_spell_chartab(sp)
5578 spelltab_T *sp;
5579{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005580 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005581
5582 /* Init everything to FALSE. */
5583 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
5584 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
5585 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005586 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005587 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005588 sp->st_upper[i] = i;
5589 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005590
5591 /* We include digits. A word shouldn't start with a digit, but handling
5592 * that is done separately. */
5593 for (i = '0'; i <= '9'; ++i)
5594 sp->st_isw[i] = TRUE;
5595 for (i = 'A'; i <= 'Z'; ++i)
5596 {
5597 sp->st_isw[i] = TRUE;
5598 sp->st_isu[i] = TRUE;
5599 sp->st_fold[i] = i + 0x20;
5600 }
5601 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005602 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005603 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005604 sp->st_upper[i] = i - 0x20;
5605 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005606}
5607
5608/*
5609 * Init the chartab used for spelling. Only depends on 'encoding'.
5610 * Called once while starting up and when 'encoding' changes.
5611 * The default is to use isalpha(), but the spell file should define the word
5612 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005613 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005614 */
5615 void
5616init_spell_chartab()
5617{
5618 int i;
5619
5620 did_set_spelltab = FALSE;
5621 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005622#ifdef FEAT_MBYTE
5623 if (enc_dbcs)
5624 {
5625 /* DBCS: assume double-wide characters are word characters. */
5626 for (i = 128; i <= 255; ++i)
5627 if (MB_BYTE2LEN(i) == 2)
5628 spelltab.st_isw[i] = TRUE;
5629 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005630 else if (enc_utf8)
5631 {
5632 for (i = 128; i < 256; ++i)
5633 {
5634 spelltab.st_isu[i] = utf_isupper(i);
5635 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
5636 spelltab.st_fold[i] = utf_fold(i);
5637 spelltab.st_upper[i] = utf_toupper(i);
5638 }
5639 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005640 else
5641#endif
5642 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005643 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005644 for (i = 128; i < 256; ++i)
5645 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005646 if (MB_ISUPPER(i))
5647 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005648 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005649 spelltab.st_isu[i] = TRUE;
5650 spelltab.st_fold[i] = MB_TOLOWER(i);
5651 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005652 else if (MB_ISLOWER(i))
5653 {
5654 spelltab.st_isw[i] = TRUE;
5655 spelltab.st_upper[i] = MB_TOUPPER(i);
5656 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005657 }
5658 }
5659}
5660
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005661static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
5662static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
5663
5664/*
5665 * Set the spell character tables from strings in the affix file.
5666 */
5667 static int
5668set_spell_chartab(fol, low, upp)
5669 char_u *fol;
5670 char_u *low;
5671 char_u *upp;
5672{
5673 /* We build the new tables here first, so that we can compare with the
5674 * previous one. */
5675 spelltab_T new_st;
5676 char_u *pf = fol, *pl = low, *pu = upp;
5677 int f, l, u;
5678
5679 clear_spell_chartab(&new_st);
5680
5681 while (*pf != NUL)
5682 {
5683 if (*pl == NUL || *pu == NUL)
5684 {
5685 EMSG(_(e_affform));
5686 return FAIL;
5687 }
5688#ifdef FEAT_MBYTE
5689 f = mb_ptr2char_adv(&pf);
5690 l = mb_ptr2char_adv(&pl);
5691 u = mb_ptr2char_adv(&pu);
5692#else
5693 f = *pf++;
5694 l = *pl++;
5695 u = *pu++;
5696#endif
5697 /* Every character that appears is a word character. */
5698 if (f < 256)
5699 new_st.st_isw[f] = TRUE;
5700 if (l < 256)
5701 new_st.st_isw[l] = TRUE;
5702 if (u < 256)
5703 new_st.st_isw[u] = TRUE;
5704
5705 /* if "LOW" and "FOL" are not the same the "LOW" char needs
5706 * case-folding */
5707 if (l < 256 && l != f)
5708 {
5709 if (f >= 256)
5710 {
5711 EMSG(_(e_affrange));
5712 return FAIL;
5713 }
5714 new_st.st_fold[l] = f;
5715 }
5716
5717 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005718 * case-folding, it's upper case and the "UPP" is the upper case of
5719 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005720 if (u < 256 && u != f)
5721 {
5722 if (f >= 256)
5723 {
5724 EMSG(_(e_affrange));
5725 return FAIL;
5726 }
5727 new_st.st_fold[u] = f;
5728 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005729 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005730 }
5731 }
5732
5733 if (*pl != NUL || *pu != NUL)
5734 {
5735 EMSG(_(e_affform));
5736 return FAIL;
5737 }
5738
5739 return set_spell_finish(&new_st);
5740}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005741
5742/*
5743 * Set the spell character tables from strings in the .spl file.
5744 */
5745 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005746set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005747 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005748 int cnt; /* length of "flags" */
5749 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005750{
5751 /* We build the new tables here first, so that we can compare with the
5752 * previous one. */
5753 spelltab_T new_st;
5754 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005755 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005756 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005757
5758 clear_spell_chartab(&new_st);
5759
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005760 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005761 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005762 if (i < cnt)
5763 {
5764 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
5765 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
5766 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005767
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005768 if (*p != NUL)
5769 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005770#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005771 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005772#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005773 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005774#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005775 new_st.st_fold[i + 128] = c;
5776 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
5777 new_st.st_upper[c] = i + 128;
5778 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005779 }
5780
5781 return set_spell_finish(&new_st);
5782}
5783
5784 static int
5785set_spell_finish(new_st)
5786 spelltab_T *new_st;
5787{
5788 int i;
5789
5790 if (did_set_spelltab)
5791 {
5792 /* check that it's the same table */
5793 for (i = 0; i < 256; ++i)
5794 {
5795 if (spelltab.st_isw[i] != new_st->st_isw[i]
5796 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005797 || spelltab.st_fold[i] != new_st->st_fold[i]
5798 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005799 {
5800 EMSG(_("E763: Word characters differ between spell files"));
5801 return FAIL;
5802 }
5803 }
5804 }
5805 else
5806 {
5807 /* copy the new spelltab into the one being used */
5808 spelltab = *new_st;
5809 did_set_spelltab = TRUE;
5810 }
5811
5812 return OK;
5813}
5814
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005815/*
Bram Moolenaarea408852005-06-25 22:49:46 +00005816 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005817 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00005818 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005819 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00005820 */
5821 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005822spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00005823 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005824 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00005825{
Bram Moolenaarea408852005-06-25 22:49:46 +00005826#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005827 char_u *s;
5828 int l;
5829 int c;
5830
5831 if (has_mbyte)
5832 {
5833 l = MB_BYTE2LEN(*p);
5834 s = p;
5835 if (l == 1)
5836 {
5837 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005838 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005839 {
5840 s = p + 1; /* skip a mid-word character */
5841 l = MB_BYTE2LEN(*s);
5842 }
5843 }
5844 else
5845 {
5846 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005847 if (c < 256 ? buf->b_spell_ismw[c]
5848 : (buf->b_spell_ismw_mb != NULL
5849 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005850 {
5851 s = p + l;
5852 l = MB_BYTE2LEN(*s);
5853 }
5854 }
5855
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005856 c = mb_ptr2char(s);
5857 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005858 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005859 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005860 }
Bram Moolenaarea408852005-06-25 22:49:46 +00005861#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005862
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005863 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
5864}
5865
5866/*
5867 * Return TRUE if "p" points to a word character.
5868 * Unlike spell_iswordp() this doesn't check for "midword" characters.
5869 */
5870 static int
5871spell_iswordp_nmw(p)
5872 char_u *p;
5873{
5874#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005875 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005876
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005877 if (has_mbyte)
5878 {
5879 c = mb_ptr2char(p);
5880 if (c > 255)
5881 return mb_get_class(p) >= 2;
5882 return spelltab.st_isw[c];
5883 }
5884#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005885 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00005886}
5887
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005888#ifdef FEAT_MBYTE
5889/*
5890 * Return TRUE if "p" points to a word character.
5891 * Wide version of spell_iswordp().
5892 */
5893 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005894spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005895 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005896 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005897{
5898 int *s;
5899
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005900 if (*p < 256 ? buf->b_spell_ismw[*p]
5901 : (buf->b_spell_ismw_mb != NULL
5902 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005903 s = p + 1;
5904 else
5905 s = p;
5906
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005907 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005908 {
5909 if (enc_utf8)
5910 return utf_class(*s) >= 2;
5911 if (enc_dbcs)
5912 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
5913 return 0;
5914 }
5915 return spelltab.st_isw[*s];
5916}
5917#endif
5918
Bram Moolenaarea408852005-06-25 22:49:46 +00005919/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005920 * Write the table with prefix conditions to the .spl file.
5921 */
5922 static void
5923write_spell_prefcond(fd, gap)
5924 FILE *fd;
5925 garray_T *gap;
5926{
5927 int i;
5928 char_u *p;
5929 int len;
5930
5931 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
5932
5933 for (i = 0; i < gap->ga_len; ++i)
5934 {
5935 /* <prefcond> : <condlen> <condstr> */
5936 p = ((char_u **)gap->ga_data)[i];
5937 if (p == NULL)
5938 fputc(0, fd);
5939 else
5940 {
5941 len = STRLEN(p);
5942 fputc(len, fd);
5943 fwrite(p, (size_t)len, (size_t)1, fd);
5944 }
5945 }
5946}
5947
5948/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005949 * Write the current tables into the .spl file.
5950 * This makes sure the same characters are recognized as word characters when
5951 * generating an when using a spell file.
5952 */
5953 static void
5954write_spell_chartab(fd)
5955 FILE *fd;
5956{
5957 char_u charbuf[256 * 4];
5958 int len = 0;
5959 int flags;
5960 int i;
5961
5962 fputc(128, fd); /* <charflagslen> */
5963 for (i = 128; i < 256; ++i)
5964 {
5965 flags = 0;
5966 if (spelltab.st_isw[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005967 flags |= CF_WORD;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005968 if (spelltab.st_isu[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005969 flags |= CF_UPPER;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005970 fputc(flags, fd); /* <charflags> */
5971
Bram Moolenaarb765d632005-06-07 21:00:02 +00005972#ifdef FEAT_MBYTE
5973 if (has_mbyte)
5974 len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
5975 else
5976#endif
5977 charbuf[len++] = spelltab.st_fold[i];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005978 }
5979
5980 put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
5981 fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
5982}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005983
5984/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005985 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
5986 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005987 * When using a multi-byte 'encoding' the length may change!
5988 * Returns FAIL when something wrong.
5989 */
5990 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005991spell_casefold(str, len, buf, buflen)
5992 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005993 int len;
5994 char_u *buf;
5995 int buflen;
5996{
5997 int i;
5998
5999 if (len >= buflen)
6000 {
6001 buf[0] = NUL;
6002 return FAIL; /* result will not fit */
6003 }
6004
6005#ifdef FEAT_MBYTE
6006 if (has_mbyte)
6007 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006008 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006009 char_u *p;
6010 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006011
6012 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006013 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006014 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006015 if (outi + MB_MAXBYTES > buflen)
6016 {
6017 buf[outi] = NUL;
6018 return FAIL;
6019 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006020 c = mb_ptr2char_adv(&p);
6021 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006022 }
6023 buf[outi] = NUL;
6024 }
6025 else
6026#endif
6027 {
6028 /* Be quick for non-multibyte encodings. */
6029 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006030 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006031 buf[i] = NUL;
6032 }
6033
6034 return OK;
6035}
6036
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006037#define SPS_BEST 1
6038#define SPS_FAST 2
6039#define SPS_DOUBLE 4
6040
6041static int sps_flags = SPS_BEST;
6042
6043/*
6044 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
6045 * Sets "sps_flags".
6046 */
6047 int
6048spell_check_sps()
6049{
6050 char_u *p;
6051 char_u buf[MAXPATHL];
6052 int f;
6053
6054 sps_flags = 0;
6055
6056 for (p = p_sps; *p != NUL; )
6057 {
6058 copy_option_part(&p, buf, MAXPATHL, ",");
6059
6060 f = 0;
6061 if (STRCMP(buf, "best") == 0)
6062 f = SPS_BEST;
6063 else if (STRCMP(buf, "fast") == 0)
6064 f = SPS_FAST;
6065 else if (STRCMP(buf, "double") == 0)
6066 f = SPS_DOUBLE;
6067 else if (STRNCMP(buf, "expr:", 5) != 0
6068 && STRNCMP(buf, "file:", 5) != 0)
6069 f = -1;
6070
6071 if (f == -1 || (sps_flags != 0 && f != 0))
6072 {
6073 sps_flags = SPS_BEST;
6074 return FAIL;
6075 }
6076 if (f != 0)
6077 sps_flags = f;
6078 }
6079
6080 if (sps_flags == 0)
6081 sps_flags = SPS_BEST;
6082
6083 return OK;
6084}
6085
6086/* Remember what "z?" replaced. */
6087static char_u *repl_from = NULL;
6088static char_u *repl_to = NULL;
6089
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006090/*
6091 * "z?": Find badly spelled word under or after the cursor.
6092 * Give suggestions for the properly spelled word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006093 */
6094 void
6095spell_suggest()
6096{
6097 char_u *line;
6098 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006099 char_u wcopy[MAXWLEN + 2];
6100 char_u *p;
6101 int i;
6102 int c;
6103 suginfo_T sug;
6104 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006105 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006106 int need_cap;
6107 regmatch_T regmatch;
6108 int endcol;
6109 char_u *line_copy = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006110
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006111 /* Find the start of the badly spelled word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006112 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL
6113 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006114 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006115 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
6116 return;
6117
6118 /* No bad word or it starts after the cursor: use the word under the
6119 * cursor. */
6120 curwin->w_cursor = prev_cursor;
6121 line = ml_get_curline();
6122 p = line + curwin->w_cursor.col;
6123 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006124 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006125 mb_ptr_back(line, p);
6126 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006127 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006128 mb_ptr_adv(p);
6129
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006130 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006131 {
6132 beep_flush();
6133 return;
6134 }
6135 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006136 }
6137
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006138 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006139 line = ml_get_curline();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006140
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006141 /* Figure out if the word should be capitalised. */
6142 need_cap = FALSE;
6143 if (curbuf->b_cap_prog != NULL)
6144 {
6145 endcol = 0;
Bram Moolenaar97409f12005-07-08 22:17:29 +00006146 if ((int)(skipwhite(line) - line) == (int)curwin->w_cursor.col)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006147 {
6148 /* At start of line, check if previous line is empty or sentence
6149 * ends there. */
6150 if (curwin->w_cursor.lnum == 1)
6151 need_cap = TRUE;
6152 else
6153 {
6154 line = ml_get(curwin->w_cursor.lnum - 1);
6155 if (*skipwhite(line) == NUL)
6156 need_cap = TRUE;
6157 else
6158 {
6159 /* Append a space in place of the line break. */
6160 line_copy = concat_str(line, (char_u *)" ");
6161 line = line_copy;
6162 endcol = STRLEN(line);
6163 }
6164 }
6165 }
6166 else
6167 endcol = curwin->w_cursor.col;
6168
6169 if (endcol > 0)
6170 {
6171 /* Check if sentence ends before the bad word. */
6172 regmatch.regprog = curbuf->b_cap_prog;
6173 regmatch.rm_ic = FALSE;
6174 p = line + endcol;
6175 for (;;)
6176 {
6177 mb_ptr_back(line, p);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006178 if (p == line || spell_iswordp_nmw(p))
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006179 break;
6180 if (vim_regexec(&regmatch, p, 0)
6181 && regmatch.endp[0] == line + endcol)
6182 {
6183 need_cap = TRUE;
6184 break;
6185 }
6186 }
6187 }
6188
6189 /* get the line again, we may have been using the previous one */
6190 line = ml_get_curline();
6191 vim_free(line_copy);
6192 }
6193
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006194 /* Get the list of suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006195 spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2,
6196 TRUE, need_cap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006197
6198 if (sug.su_ga.ga_len == 0)
6199 MSG(_("Sorry, no suggestions"));
6200 else
6201 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006202 vim_free(repl_from);
6203 repl_from = NULL;
6204 vim_free(repl_to);
6205 repl_to = NULL;
6206
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006207 /* List the suggestions. */
6208 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006209 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006210 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
6211 sug.su_badlen, sug.su_badptr);
6212 msg_puts(IObuff);
6213 msg_clr_eos();
6214 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00006215
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006216 msg_scroll = TRUE;
6217 for (i = 0; i < sug.su_ga.ga_len; ++i)
6218 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006219 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006220
6221 /* The suggested word may replace only part of the bad word, add
6222 * the not replaced part. */
6223 STRCPY(wcopy, stp->st_word);
6224 if (sug.su_badlen > stp->st_orglen)
6225 vim_strncpy(wcopy + STRLEN(wcopy),
6226 sug.su_badptr + stp->st_orglen,
6227 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006228 vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\""), i + 1, wcopy);
6229 msg_puts(IObuff);
6230
6231 /* The word may replace more than "su_badlen". */
6232 if (sug.su_badlen < stp->st_orglen)
6233 {
6234 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
6235 stp->st_orglen, sug.su_badptr);
6236 msg_puts(IObuff);
6237 }
6238
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006239 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006240 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006241 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006242 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006243 vim_snprintf((char *)IObuff, IOSIZE, _(" (%s%d - %d)"),
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006244 stp->st_salscore ? "s " : "",
6245 stp->st_score, stp->st_altscore);
6246 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00006247 vim_snprintf((char *)IObuff, IOSIZE, _(" (%d)"),
6248 stp->st_score);
6249 msg_advance(30);
6250 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006251 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006252 msg_putchar('\n');
6253 }
6254
6255 /* Ask for choice. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006256 i = prompt_for_number(&mouse_used);
6257 if (mouse_used)
6258 i -= lines_left;
6259
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006260 if (i > 0 && i <= sug.su_ga.ga_len && u_save_cursor() == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006261 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006262 /* Save the from and to text for :spellrepall. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006263 stp = &SUG(sug.su_ga, i - 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006264 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
6265 repl_to = vim_strsave(stp->st_word);
6266
6267 /* Replace the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006268 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
6269 if (p != NULL)
6270 {
6271 c = sug.su_badptr - line;
6272 mch_memmove(p, line, c);
6273 STRCPY(p + c, stp->st_word);
6274 STRCAT(p, sug.su_badptr + stp->st_orglen);
6275 ml_replace(curwin->w_cursor.lnum, p, FALSE);
6276 curwin->w_cursor.col = c;
6277 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006278
6279 /* For redo we use a change-word command. */
6280 ResetRedobuff();
6281 AppendToRedobuff((char_u *)"ciw");
6282 AppendToRedobuff(stp->st_word);
6283 AppendCharToRedobuff(ESC);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006284 }
6285 }
6286 else
6287 curwin->w_cursor = prev_cursor;
6288 }
6289
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006290 spell_find_cleanup(&sug);
6291}
6292
6293/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006294 * ":spellrepall"
6295 */
6296/*ARGSUSED*/
6297 void
6298ex_spellrepall(eap)
6299 exarg_T *eap;
6300{
6301 pos_T pos = curwin->w_cursor;
6302 char_u *frompat;
6303 int addlen;
6304 char_u *line;
6305 char_u *p;
6306 int didone = FALSE;
6307 int save_ws = p_ws;
6308
6309 if (repl_from == NULL || repl_to == NULL)
6310 {
6311 EMSG(_("E752: No previous spell replacement"));
6312 return;
6313 }
6314 addlen = STRLEN(repl_to) - STRLEN(repl_from);
6315
6316 frompat = alloc(STRLEN(repl_from) + 7);
6317 if (frompat == NULL)
6318 return;
6319 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
6320 p_ws = FALSE;
6321
6322 curwin->w_cursor.lnum = 0;
6323 while (!got_int)
6324 {
6325 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
6326 || u_save_cursor() == FAIL)
6327 break;
6328
6329 /* Only replace when the right word isn't there yet. This happens
6330 * when changing "etc" to "etc.". */
6331 line = ml_get_curline();
6332 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
6333 repl_to, STRLEN(repl_to)) != 0)
6334 {
6335 p = alloc(STRLEN(line) + addlen + 1);
6336 if (p == NULL)
6337 break;
6338 mch_memmove(p, line, curwin->w_cursor.col);
6339 STRCPY(p + curwin->w_cursor.col, repl_to);
6340 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
6341 ml_replace(curwin->w_cursor.lnum, p, FALSE);
6342 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6343 didone = TRUE;
6344 }
6345 curwin->w_cursor.col += STRLEN(repl_to);
6346 }
6347
6348 p_ws = save_ws;
6349 curwin->w_cursor = pos;
6350 vim_free(frompat);
6351
6352 if (!didone)
6353 EMSG2(_("E753: Not found: %s"), repl_from);
6354}
6355
6356/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006357 * Find spell suggestions for "word". Return them in the growarray "*gap" as
6358 * a list of allocated strings.
6359 */
6360 void
6361spell_suggest_list(gap, word, maxcount)
6362 garray_T *gap;
6363 char_u *word;
6364 int maxcount; /* maximum nr of suggestions */
6365{
6366 suginfo_T sug;
6367 int i;
6368 suggest_T *stp;
6369 char_u *wcopy;
6370
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006371 spell_find_suggest(word, &sug, maxcount, FALSE, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006372
6373 /* Make room in "gap". */
6374 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
6375 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
6376 return;
6377
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006378 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006379 {
6380 stp = &SUG(sug.su_ga, i);
6381
6382 /* The suggested word may replace only part of "word", add the not
6383 * replaced part. */
6384 wcopy = alloc(STRLEN(stp->st_word)
6385 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
6386 if (wcopy == NULL)
6387 break;
6388 STRCPY(wcopy, stp->st_word);
6389 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
6390 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
6391 }
6392
6393 spell_find_cleanup(&sug);
6394}
6395
6396/*
6397 * Find spell suggestions for the word at the start of "badptr".
6398 * Return the suggestions in "su->su_ga".
6399 * The maximum number of suggestions is "maxcount".
6400 * Note: does use info for the current window.
6401 * This is based on the mechanisms of Aspell, but completely reimplemented.
6402 */
6403 static void
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006404spell_find_suggest(badptr, su, maxcount, banbadword, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006405 char_u *badptr;
6406 suginfo_T *su;
6407 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00006408 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006409 int need_cap; /* word should start with capital */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006410{
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006411 int attr = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006412 char_u buf[MAXPATHL];
6413 char_u *p;
6414 int do_combine = FALSE;
6415 char_u *sps_copy;
6416#ifdef FEAT_EVAL
6417 static int expr_busy = FALSE;
6418#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006419 int c;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006420
6421 /*
6422 * Set the info in "*su".
6423 */
6424 vim_memset(su, 0, sizeof(suginfo_T));
6425 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
6426 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006427 if (*badptr == NUL)
6428 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006429 hash_init(&su->su_banned);
6430
6431 su->su_badptr = badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006432 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006433 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006434 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006435
6436 if (su->su_badlen >= MAXWLEN)
6437 su->su_badlen = MAXWLEN - 1; /* just in case */
6438 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
6439 (void)spell_casefold(su->su_badptr, su->su_badlen,
6440 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006441 /* get caps flags for bad word */
6442 su->su_badflags = captype(su->su_badptr, su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006443 if (need_cap)
6444 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006445
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006446 /* If the word is not capitalised and spell_check() doesn't consider the
6447 * word to be bad then it might need to be capitalised. Add a suggestion
6448 * for that. */
6449#ifdef FEAT_MBYTE
6450 c = mb_ptr2char(su->su_badptr);
6451#else
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006452 c = *su->su_badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006453#endif
6454 if (!SPELL_ISUPPER(c) && attr == 0)
6455 {
6456 make_case_word(su->su_badword, buf, WF_ONECAP);
6457 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
6458 0, TRUE);
6459 }
6460
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006461 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00006462 if (banbadword)
6463 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006464
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006465 /* Make a copy of 'spellsuggest', because the expression may change it. */
6466 sps_copy = vim_strsave(p_sps);
6467 if (sps_copy == NULL)
6468 return;
6469
6470 /* Loop over the items in 'spellsuggest'. */
6471 for (p = sps_copy; *p != NUL; )
6472 {
6473 copy_option_part(&p, buf, MAXPATHL, ",");
6474
6475 if (STRNCMP(buf, "expr:", 5) == 0)
6476 {
6477#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006478 /* Evaluate an expression. Skip this when called recursively,
6479 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006480 if (!expr_busy)
6481 {
6482 expr_busy = TRUE;
6483 spell_suggest_expr(su, buf + 5);
6484 expr_busy = FALSE;
6485 }
6486#endif
6487 }
6488 else if (STRNCMP(buf, "file:", 5) == 0)
6489 /* Use list of suggestions in a file. */
6490 spell_suggest_file(su, buf + 5);
6491 else
6492 {
6493 /* Use internal method. */
6494 spell_suggest_intern(su);
6495 if (sps_flags & SPS_DOUBLE)
6496 do_combine = TRUE;
6497 }
6498 }
6499
6500 vim_free(sps_copy);
6501
6502 if (do_combine)
6503 /* Combine the two list of suggestions. This must be done last,
6504 * because sorting changes the order again. */
6505 score_combine(su);
6506}
6507
6508#ifdef FEAT_EVAL
6509/*
6510 * Find suggestions by evaluating expression "expr".
6511 */
6512 static void
6513spell_suggest_expr(su, expr)
6514 suginfo_T *su;
6515 char_u *expr;
6516{
6517 list_T *list;
6518 listitem_T *li;
6519 int score;
6520 char_u *p;
6521
6522 /* The work is split up in a few parts to avoid having to export
6523 * suginfo_T.
6524 * First evaluate the expression and get the resulting list. */
6525 list = eval_spell_expr(su->su_badword, expr);
6526 if (list != NULL)
6527 {
6528 /* Loop over the items in the list. */
6529 for (li = list->lv_first; li != NULL; li = li->li_next)
6530 if (li->li_tv.v_type == VAR_LIST)
6531 {
6532 /* Get the word and the score from the items. */
6533 score = get_spellword(li->li_tv.vval.v_list, &p);
6534 if (score >= 0)
6535 add_suggestion(su, &su->su_ga, p,
6536 su->su_badlen, score, 0, TRUE);
6537 }
6538 list_unref(list);
6539 }
6540
6541 /* Sort the suggestions and truncate at "maxcount". */
6542 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
6543}
6544#endif
6545
6546/*
6547 * Find suggestions a file "fname".
6548 */
6549 static void
6550spell_suggest_file(su, fname)
6551 suginfo_T *su;
6552 char_u *fname;
6553{
6554 FILE *fd;
6555 char_u line[MAXWLEN * 2];
6556 char_u *p;
6557 int len;
6558 char_u cword[MAXWLEN];
6559
6560 /* Open the file. */
6561 fd = mch_fopen((char *)fname, "r");
6562 if (fd == NULL)
6563 {
6564 EMSG2(_(e_notopen), fname);
6565 return;
6566 }
6567
6568 /* Read it line by line. */
6569 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
6570 {
6571 line_breakcheck();
6572
6573 p = vim_strchr(line, '/');
6574 if (p == NULL)
6575 continue; /* No Tab found, just skip the line. */
6576 *p++ = NUL;
6577 if (STRICMP(su->su_badword, line) == 0)
6578 {
6579 /* Match! Isolate the good word, until CR or NL. */
6580 for (len = 0; p[len] >= ' '; ++len)
6581 ;
6582 p[len] = NUL;
6583
6584 /* If the suggestion doesn't have specific case duplicate the case
6585 * of the bad word. */
6586 if (captype(p, NULL) == 0)
6587 {
6588 make_case_word(p, cword, su->su_badflags);
6589 p = cword;
6590 }
6591
6592 add_suggestion(su, &su->su_ga, p, su->su_badlen,
6593 SCORE_FILE, 0, TRUE);
6594 }
6595 }
6596
6597 fclose(fd);
6598
6599 /* Sort the suggestions and truncate at "maxcount". */
6600 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
6601}
6602
6603/*
6604 * Find suggestions for the internal method indicated by "sps_flags".
6605 */
6606 static void
6607spell_suggest_intern(su)
6608 suginfo_T *su;
6609{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006610 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006611 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006612 *
6613 * Set a maximum score to limit the combination of operations that is
6614 * tried.
6615 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006616 suggest_try_special(su);
6617
6618 /*
6619 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
6620 * from the .aff file and inserting a space (split the word).
6621 */
6622 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006623
6624 /* For the resulting top-scorers compute the sound-a-like score. */
6625 if (sps_flags & SPS_DOUBLE)
6626 score_comp_sal(su);
6627
6628 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006629 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006630 *
6631 * Only do this when we don't have a lot of suggestions yet, because it's
6632 * very slow and often doesn't find new suggestions.
6633 */
6634 if ((sps_flags & SPS_DOUBLE)
6635 || (!(sps_flags & SPS_FAST)
6636 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
6637 {
6638 /* Allow a higher score now. */
6639 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006640 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006641 }
6642
6643 /* When CTRL-C was hit while searching do show the results. */
6644 ui_breakcheck();
6645 if (got_int)
6646 {
6647 (void)vgetc();
6648 got_int = FALSE;
6649 }
6650
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006651 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006652 {
6653 if (sps_flags & SPS_BEST)
6654 /* Adjust the word score for how it sounds like. */
6655 rescore_suggestions(su);
6656
6657 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006658 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006659 }
6660}
6661
6662/*
6663 * Free the info put in "*su" by spell_find_suggest().
6664 */
6665 static void
6666spell_find_cleanup(su)
6667 suginfo_T *su;
6668{
6669 int i;
6670
6671 /* Free the suggestions. */
6672 for (i = 0; i < su->su_ga.ga_len; ++i)
6673 vim_free(SUG(su->su_ga, i).st_word);
6674 ga_clear(&su->su_ga);
6675 for (i = 0; i < su->su_sga.ga_len; ++i)
6676 vim_free(SUG(su->su_sga, i).st_word);
6677 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006678
6679 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006680 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006681}
6682
6683/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006684 * Make a copy of "word", with the first letter upper or lower cased, to
6685 * "wcopy[MAXWLEN]". "word" must not be empty.
6686 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006687 */
6688 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006689onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006690 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006691 char_u *wcopy;
6692 int upper; /* TRUE: first letter made upper case */
6693{
6694 char_u *p;
6695 int c;
6696 int l;
6697
6698 p = word;
6699#ifdef FEAT_MBYTE
6700 if (has_mbyte)
6701 c = mb_ptr2char_adv(&p);
6702 else
6703#endif
6704 c = *p++;
6705 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006706 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006707 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006708 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006709#ifdef FEAT_MBYTE
6710 if (has_mbyte)
6711 l = mb_char2bytes(c, wcopy);
6712 else
6713#endif
6714 {
6715 l = 1;
6716 wcopy[0] = c;
6717 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006718 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006719}
6720
6721/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006722 * Make a copy of "word" with all the letters upper cased into
6723 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006724 */
6725 static void
6726allcap_copy(word, wcopy)
6727 char_u *word;
6728 char_u *wcopy;
6729{
6730 char_u *s;
6731 char_u *d;
6732 int c;
6733
6734 d = wcopy;
6735 for (s = word; *s != NUL; )
6736 {
6737#ifdef FEAT_MBYTE
6738 if (has_mbyte)
6739 c = mb_ptr2char_adv(&s);
6740 else
6741#endif
6742 c = *s++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006743 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006744
6745#ifdef FEAT_MBYTE
6746 if (has_mbyte)
6747 {
6748 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
6749 break;
6750 d += mb_char2bytes(c, d);
6751 }
6752 else
6753#endif
6754 {
6755 if (d - wcopy >= MAXWLEN - 1)
6756 break;
6757 *d++ = c;
6758 }
6759 }
6760 *d = NUL;
6761}
6762
6763/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006764 * Try finding suggestions by recognizing specific situations.
6765 */
6766 static void
6767suggest_try_special(su)
6768 suginfo_T *su;
6769{
6770 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006771 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006772 int c;
6773 char_u word[MAXWLEN];
6774
6775 /*
6776 * Recognize a word that is repeated: "the the".
6777 */
6778 p = skiptowhite(su->su_fbadword);
6779 len = p - su->su_fbadword;
6780 p = skipwhite(p);
6781 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
6782 {
6783 /* Include badflags: if the badword is onecap or allcap
6784 * use that for the goodword too: "The the" -> "The". */
6785 c = su->su_fbadword[len];
6786 su->su_fbadword[len] = NUL;
6787 make_case_word(su->su_fbadword, word, su->su_badflags);
6788 su->su_fbadword[len] = c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006789 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006790 }
6791}
6792
6793/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006794 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00006795 *
6796 * This uses a state machine. At each node in the tree we try various
6797 * operations. When trying if an operation work "depth" is increased and the
6798 * stack[] is used to store info. This allows combinations, thus insert one
6799 * character, replace one and delete another. The number of changes is
6800 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006801 */
6802 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00006803suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006804 suginfo_T *su;
6805{
6806 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
6807 char_u tword[MAXWLEN]; /* good word collected so far */
6808 trystate_T stack[MAXWLEN];
6809 char_u preword[MAXWLEN * 3]; /* word found with proper case (appended
6810 * to for word split) */
6811 char_u prewordlen = 0; /* length of word in "preword" */
6812 int splitoff = 0; /* index in tword after last split */
6813 trystate_T *sp;
6814 int newscore;
6815 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006816 char_u *byts, *fbyts, *pbyts;
6817 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006818 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00006819 int c, c2, c3;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006820 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006821 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006822 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006823 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006824 int len;
6825 char_u *p;
6826 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00006827 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006828 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006829
6830 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00006831 * to find matches (esp. REP items). Append some more text, changing
6832 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006833 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006834 n = STRLEN(fword);
6835 p = su->su_badptr + su->su_badlen;
6836 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006837
6838 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
6839 lp->lp_slang != NULL; ++lp)
6840 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006841 /*
6842 * Go through the whole case-fold tree, try changes at each node.
6843 * "tword[]" contains the word collected from nodes in the tree.
6844 * "fword[]" the word we are trying to match with (initially the bad
6845 * word).
6846 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006847 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006848 sp = &stack[0];
6849 sp->ts_state = STATE_START;
6850 sp->ts_score = 0;
6851 sp->ts_curi = 1;
6852 sp->ts_fidx = 0;
6853 sp->ts_fidxtry = 0;
6854 sp->ts_twordlen = 0;
6855 sp->ts_arridx = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00006856#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006857 sp->ts_tcharlen = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00006858#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006859
Bram Moolenaarea424162005-06-16 21:51:00 +00006860 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006861 * When there are postponed prefixes we need to use these first. At
6862 * the end of the prefix we continue in the case-fold tree.
6863 */
6864 fbyts = lp->lp_slang->sl_fbyts;
6865 fidxs = lp->lp_slang->sl_fidxs;
6866 pbyts = lp->lp_slang->sl_pbyts;
6867 pidxs = lp->lp_slang->sl_pidxs;
6868 if (pbyts != NULL)
6869 {
6870 byts = pbyts;
6871 idxs = pidxs;
6872 sp->ts_prefixdepth = PREFIXTREE;
6873 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
6874 }
6875 else
6876 {
6877 byts = fbyts;
6878 idxs = fidxs;
6879 sp->ts_prefixdepth = NOPREFIX;
6880 }
6881
6882 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00006883 * Loop to find all suggestions. At each round we either:
6884 * - For the current state try one operation, advance "ts_curi",
6885 * increase "depth".
6886 * - When a state is done go to the next, set "ts_state".
6887 * - When all states are tried decrease "depth".
6888 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006889 while (depth >= 0 && !got_int)
6890 {
6891 sp = &stack[depth];
6892 switch (sp->ts_state)
6893 {
6894 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006895 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006896 /*
6897 * Start of node: Deal with NUL bytes, which means
6898 * tword[] may end here.
6899 */
6900 arridx = sp->ts_arridx; /* current node in the tree */
6901 len = byts[arridx]; /* bytes in this node */
6902 arridx += sp->ts_curi; /* index of current byte */
6903
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006904 if (sp->ts_prefixdepth == PREFIXTREE)
6905 {
6906 /* Skip over the NUL bytes, we use them later. */
6907 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
6908 ;
6909 sp->ts_curi += n;
6910
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006911 /* Always past NUL bytes now. */
6912 n = (int)sp->ts_state;
6913 sp->ts_state = STATE_ENDNUL;
6914
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006915 /* At end of a prefix or at start of prefixtree: check for
6916 * following word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006917 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006918 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006919 ++depth;
6920 stack[depth] = stack[depth - 1];
6921 sp = &stack[depth];
6922 sp->ts_prefixdepth = depth - 1;
6923 byts = fbyts;
6924 idxs = fidxs;
6925 sp->ts_state = STATE_START;
6926 sp->ts_curi = 1; /* start just after length byte */
6927 sp->ts_arridx = 0;
6928
6929 /* Move the prefix to preword[] so that
6930 * find_keepcap_word() works. */
6931 prewordlen = splitoff = sp->ts_twordlen;
6932 mch_memmove(preword, tword, splitoff);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006933 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006934 break;
6935 }
6936
Bram Moolenaar0c405862005-06-22 22:26:26 +00006937 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006938 {
6939 /* Past bytes in node and/or past NUL bytes. */
6940 sp->ts_state = STATE_ENDNUL;
6941 break;
6942 }
6943
6944 /*
6945 * End of word in tree.
6946 */
6947 ++sp->ts_curi; /* eat one NUL byte */
6948
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006949 flags = (int)idxs[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006950
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006951 if (sp->ts_prefixdepth < MAXWLEN)
6952 {
6953 /* There was a prefix before the word. Check that the
6954 * prefix can be used with this word. */
6955 /* Count the length of the NULs in the prefix. If there
6956 * are none this must be the first try without a prefix.
6957 */
6958 n = stack[sp->ts_prefixdepth].ts_arridx;
6959 len = pbyts[n++];
6960 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
6961 ;
6962 if (c > 0)
6963 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006964 /* The prefix ID is stored three bytes above the
6965 * flags. */
6966 c = valid_word_prefix(c, n, flags,
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006967 tword + splitoff, lp->lp_slang);
6968 if (c == 0)
6969 break;
6970
6971 /* Use the WF_RARE flag for a rare prefix. */
6972 if (c & WF_RAREPFX)
6973 flags |= WF_RARE;
6974 }
6975 }
6976
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006977 /*
6978 * Form the word with proper case in preword.
6979 * If there is a word from a previous split, append.
6980 */
6981 tword[sp->ts_twordlen] = NUL;
6982 if (flags & WF_KEEPCAP)
6983 /* Must find the word in the keep-case tree. */
6984 find_keepcap_word(lp->lp_slang, tword + splitoff,
6985 preword + prewordlen);
6986 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00006987 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006988 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00006989 * use that for the goodword too. But if the badword is
6990 * allcap and it's only one char long use onecap. */
6991 c = su->su_badflags;
6992 if ((c & WF_ALLCAP)
6993#ifdef FEAT_MBYTE
6994 && su->su_badlen == mb_ptr2len_check(su->su_badptr)
6995#else
6996 && su->su_badlen == 1
6997#endif
6998 )
6999 c = WF_ONECAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007000 make_case_word(tword + splitoff,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007001 preword + prewordlen, flags | c);
7002 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007003
7004 /* Don't use a banned word. It may appear again as a good
7005 * word, thus remember it. */
7006 if (flags & WF_BANNED)
7007 {
7008 add_banned(su, preword + prewordlen);
7009 break;
7010 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007011 if (was_banned(su, preword + prewordlen)
7012 || was_banned(su, preword))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007013 break;
7014
7015 newscore = 0;
7016 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007017 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007018 newscore += SCORE_REGION;
7019 if (flags & WF_RARE)
7020 newscore += SCORE_RARE;
7021
Bram Moolenaar0c405862005-06-22 22:26:26 +00007022 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007023 captype(preword + prewordlen, NULL)))
7024 newscore += SCORE_ICASE;
7025
Bram Moolenaar0c405862005-06-22 22:26:26 +00007026 if ((fword[sp->ts_fidx] == NUL
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007027 || !spell_iswordp(fword + sp->ts_fidx, curbuf))
Bram Moolenaar0c405862005-06-22 22:26:26 +00007028 && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007029 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007030 /* The badword also ends: add suggestions. Give a penalty
7031 * when changing non-word char to word char, e.g., "thes,"
7032 * -> "these". */
7033 p = fword + sp->ts_fidx;
7034#ifdef FEAT_MBYTE
7035 if (has_mbyte)
7036 mb_ptr_back(fword, p);
7037 else
7038#endif
7039 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007040 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007041 {
7042 p = preword + STRLEN(preword);
7043#ifdef FEAT_MBYTE
7044 if (has_mbyte)
7045 mb_ptr_back(preword, p);
7046 else
7047#endif
7048 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007049 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007050 newscore += SCORE_NONWORD;
7051 }
7052
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007053 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007054 sp->ts_fidx - repextra,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007055 sp->ts_score + newscore, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007056 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007057 else if (sp->ts_fidx >= sp->ts_fidxtry
7058#ifdef FEAT_MBYTE
7059 /* Don't split halfway a character. */
7060 && (!has_mbyte || sp->ts_tcharlen == 0)
7061#endif
7062 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007063 {
7064 /* The word in the tree ends but the badword
7065 * continues: try inserting a space and check that a valid
7066 * words starts at fword[sp->ts_fidx]. */
7067 if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT))
7068 {
7069 /* Save things to be restored at STATE_SPLITUNDO. */
7070 sp->ts_save_prewordlen = prewordlen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007071 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007072 sp->ts_save_splitoff = splitoff;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007073 sp->ts_state = STATE_SPLITUNDO;
7074
7075 ++depth;
7076 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007077
7078 /* Append a space to preword. */
7079 STRCAT(preword, " ");
7080 prewordlen = STRLEN(preword);
7081 splitoff = sp->ts_twordlen;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007082
7083 /* If the badword has a non-word character at this
7084 * position skip it. That means replacing the
7085 * non-word character with a space. */
7086 if (!spell_iswordp_nmw(fword + sp->ts_fidx))
7087 {
7088 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
7089#ifdef FEAT_MBYTE
7090 if (has_mbyte)
7091 sp->ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
7092 else
7093#endif
7094 ++sp->ts_fidx;
7095 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007096#ifdef FEAT_MBYTE
7097 if (has_mbyte)
7098 {
7099 int i = 0;
7100
7101 /* Case-folding may change the number of bytes:
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007102 * Count nr of chars in fword[ts_fidx] and
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007103 * advance that many chars in su->su_badptr. */
7104 for (p = fword; p < fword + sp->ts_fidx;
7105 mb_ptr_adv(p))
7106 ++i;
7107 for (p = su->su_badptr; i > 0; mb_ptr_adv(p))
7108 --i;
7109 }
7110 else
7111#endif
7112 p = su->su_badptr + sp->ts_fidx;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007113 su->su_badflags = captype(p, su->su_badptr
7114 + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007115
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007116 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007117 sp->ts_arridx = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007118 }
7119 }
7120 break;
7121
7122 case STATE_SPLITUNDO:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007123 /* Undo the changes done for word split. */
7124 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007125 splitoff = sp->ts_save_splitoff;
7126 prewordlen = sp->ts_save_prewordlen;
7127
7128 /* Continue looking for NUL bytes. */
7129 sp->ts_state = STATE_START;
7130 break;
7131
7132 case STATE_ENDNUL:
7133 /* Past the NUL bytes in the node. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007134 if (fword[sp->ts_fidx] == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007135 {
7136 /* The badword ends, can't use the bytes in this node. */
7137 sp->ts_state = STATE_DEL;
7138 break;
7139 }
7140 sp->ts_state = STATE_PLAIN;
7141 /*FALLTHROUGH*/
7142
7143 case STATE_PLAIN:
7144 /*
7145 * Go over all possible bytes at this node, add each to
7146 * tword[] and use child node. "ts_curi" is the index.
7147 */
7148 arridx = sp->ts_arridx;
7149 if (sp->ts_curi > byts[arridx])
7150 {
7151 /* Done all bytes at this node, do next state. When still
7152 * at already changed bytes skip the other tricks. */
7153 if (sp->ts_fidx >= sp->ts_fidxtry)
7154 sp->ts_state = STATE_DEL;
7155 else
7156 sp->ts_state = STATE_FINAL;
7157 }
7158 else
7159 {
7160 arridx += sp->ts_curi++;
7161 c = byts[arridx];
7162
7163 /* Normal byte, go one level deeper. If it's not equal to
7164 * the byte in the bad word adjust the score. But don't
7165 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00007166 if (c == fword[sp->ts_fidx]
7167#ifdef FEAT_MBYTE
7168 || (sp->ts_tcharlen > 0
7169 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007170#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00007171 )
7172 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007173 else
7174 newscore = SCORE_SUBST;
7175 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
7176 && try_deeper(su, stack, depth, newscore))
7177 {
7178 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007179 sp = &stack[depth];
7180 ++sp->ts_fidx;
7181 tword[sp->ts_twordlen++] = c;
7182 sp->ts_arridx = idxs[arridx];
7183#ifdef FEAT_MBYTE
7184 if (newscore == SCORE_SUBST)
7185 sp->ts_isdiff = DIFF_YES;
7186 if (has_mbyte)
7187 {
7188 /* Multi-byte characters are a bit complicated to
7189 * handle: They differ when any of the bytes
7190 * differ and then their length may also differ. */
7191 if (sp->ts_tcharlen == 0)
7192 {
7193 /* First byte. */
7194 sp->ts_tcharidx = 0;
7195 sp->ts_tcharlen = MB_BYTE2LEN(c);
7196 sp->ts_fcharstart = sp->ts_fidx - 1;
7197 sp->ts_isdiff = (newscore != 0)
7198 ? DIFF_YES : DIFF_NONE;
7199 }
7200 else if (sp->ts_isdiff == DIFF_INSERT)
7201 /* When inserting trail bytes don't advance in
7202 * the bad word. */
7203 --sp->ts_fidx;
7204 if (++sp->ts_tcharidx == sp->ts_tcharlen)
7205 {
7206 /* Last byte of character. */
7207 if (sp->ts_isdiff == DIFF_YES)
7208 {
7209 /* Correct ts_fidx for the byte length of
7210 * the character (we didn't check that
7211 * before). */
7212 sp->ts_fidx = sp->ts_fcharstart
7213 + MB_BYTE2LEN(
7214 fword[sp->ts_fcharstart]);
7215
7216 /* For a similar character adjust score
7217 * from SCORE_SUBST to SCORE_SIMILAR. */
7218 if (lp->lp_slang->sl_has_map
7219 && similar_chars(lp->lp_slang,
7220 mb_ptr2char(tword
7221 + sp->ts_twordlen
7222 - sp->ts_tcharlen),
7223 mb_ptr2char(fword
7224 + sp->ts_fcharstart)))
7225 sp->ts_score -=
7226 SCORE_SUBST - SCORE_SIMILAR;
7227 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007228 else if (sp->ts_isdiff == DIFF_INSERT
7229 && sp->ts_twordlen > sp->ts_tcharlen)
7230 {
7231 /* If the previous character was the same,
7232 * thus doubling a character, give a bonus
7233 * to the score. */
7234 p = tword + sp->ts_twordlen
7235 - sp->ts_tcharlen;
7236 c = mb_ptr2char(p);
7237 mb_ptr_back(tword, p);
7238 if (c == mb_ptr2char(p))
7239 sp->ts_score -= SCORE_INS
7240 - SCORE_INSDUP;
7241 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007242
7243 /* Starting a new char, reset the length. */
7244 sp->ts_tcharlen = 0;
7245 }
7246 }
7247 else
7248#endif
7249 {
7250 /* If we found a similar char adjust the score.
7251 * We do this after calling try_deeper() because
7252 * it's slow. */
7253 if (newscore != 0
7254 && lp->lp_slang->sl_has_map
7255 && similar_chars(lp->lp_slang,
7256 c, fword[sp->ts_fidx - 1]))
7257 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
7258 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007259 }
7260 }
7261 break;
7262
7263 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00007264#ifdef FEAT_MBYTE
7265 /* When past the first byte of a multi-byte char don't try
7266 * delete/insert/swap a character. */
7267 if (has_mbyte && sp->ts_tcharlen > 0)
7268 {
7269 sp->ts_state = STATE_FINAL;
7270 break;
7271 }
7272#endif
7273 /*
7274 * Try skipping one character in the bad word (delete it).
7275 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007276 sp->ts_state = STATE_INS;
7277 sp->ts_curi = 1;
7278 if (fword[sp->ts_fidx] != NUL
7279 && try_deeper(su, stack, depth, SCORE_DEL))
7280 {
7281 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00007282
7283 /* Advance over the character in fword[]. Give a bonus to
7284 * the score if the same character is following "nn" ->
7285 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00007286#ifdef FEAT_MBYTE
7287 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00007288 {
7289 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00007290 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaarea408852005-06-25 22:49:46 +00007291 if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
7292 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
7293 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007294 else
7295#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00007296 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007297 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00007298 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
7299 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
7300 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007301 break;
7302 }
7303 /*FALLTHROUGH*/
7304
7305 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00007306 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007307 * node. */
7308 n = sp->ts_arridx;
7309 if (sp->ts_curi > byts[n])
7310 {
7311 /* Done all bytes at this node, do next state. */
7312 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007313 }
7314 else
7315 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007316 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007317 n += sp->ts_curi++;
7318 c = byts[n];
7319 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
7320 {
7321 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007322 sp = &stack[depth];
7323 tword[sp->ts_twordlen++] = c;
7324 sp->ts_arridx = idxs[n];
7325#ifdef FEAT_MBYTE
7326 if (has_mbyte)
7327 {
7328 fl = MB_BYTE2LEN(c);
7329 if (fl > 1)
7330 {
7331 /* There are following bytes for the same
7332 * character. We must find all bytes before
7333 * trying delete/insert/swap/etc. */
7334 sp->ts_tcharlen = fl;
7335 sp->ts_tcharidx = 1;
7336 sp->ts_isdiff = DIFF_INSERT;
7337 }
7338 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007339 else
7340 fl = 1;
7341 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00007342#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00007343 {
7344 /* If the previous character was the same, thus
7345 * doubling a character, give a bonus to the
7346 * score. */
7347 if (sp->ts_twordlen >= 2
7348 && tword[sp->ts_twordlen - 2] == c)
7349 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
7350 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007351 }
7352 }
7353 break;
7354
7355 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00007356 /*
7357 * Swap two bytes in the bad word: "12" -> "21".
7358 * We change "fword" here, it's changed back afterwards.
7359 */
7360 p = fword + sp->ts_fidx;
7361 c = *p;
7362 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007363 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007364 /* End of word, can't swap or replace. */
7365 sp->ts_state = STATE_FINAL;
7366 break;
7367 }
7368#ifdef FEAT_MBYTE
7369 if (has_mbyte)
7370 {
7371 n = mb_ptr2len_check(p);
7372 c = mb_ptr2char(p);
7373 c2 = mb_ptr2char(p + n);
7374 }
7375 else
7376#endif
7377 c2 = p[1];
7378 if (c == c2)
7379 {
7380 /* Characters are identical, swap won't do anything. */
7381 sp->ts_state = STATE_SWAP3;
7382 break;
7383 }
7384 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
7385 {
7386 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007387 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007388#ifdef FEAT_MBYTE
7389 if (has_mbyte)
7390 {
7391 fl = mb_char2len(c2);
7392 mch_memmove(p, p + n, fl);
7393 mb_char2bytes(c, p + fl);
7394 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
7395 }
7396 else
7397#endif
7398 {
7399 p[0] = c2;
7400 p[1] = c;
7401 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
7402 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007403 }
7404 else
7405 /* If this swap doesn't work then SWAP3 won't either. */
7406 sp->ts_state = STATE_REP_INI;
7407 break;
7408
Bram Moolenaarea424162005-06-16 21:51:00 +00007409 case STATE_UNSWAP:
7410 /* Undo the STATE_SWAP swap: "21" -> "12". */
7411 p = fword + sp->ts_fidx;
7412#ifdef FEAT_MBYTE
7413 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007414 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007415 n = MB_BYTE2LEN(*p);
7416 c = mb_ptr2char(p + n);
7417 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
7418 mb_char2bytes(c, p);
7419 }
7420 else
7421#endif
7422 {
7423 c = *p;
7424 *p = p[1];
7425 p[1] = c;
7426 }
7427 /*FALLTHROUGH*/
7428
7429 case STATE_SWAP3:
7430 /* Swap two bytes, skipping one: "123" -> "321". We change
7431 * "fword" here, it's changed back afterwards. */
7432 p = fword + sp->ts_fidx;
7433#ifdef FEAT_MBYTE
7434 if (has_mbyte)
7435 {
7436 n = mb_ptr2len_check(p);
7437 c = mb_ptr2char(p);
7438 fl = mb_ptr2len_check(p + n);
7439 c2 = mb_ptr2char(p + n);
7440 c3 = mb_ptr2char(p + n + fl);
7441 }
7442 else
7443#endif
7444 {
7445 c = *p;
7446 c2 = p[1];
7447 c3 = p[2];
7448 }
7449
7450 /* When characters are identical: "121" then SWAP3 result is
7451 * identical, ROT3L result is same as SWAP: "211", ROT3L
7452 * result is same as SWAP on next char: "112". Thus skip all
7453 * swapping. Also skip when c3 is NUL. */
7454 if (c == c3 || c3 == NUL)
7455 {
7456 sp->ts_state = STATE_REP_INI;
7457 break;
7458 }
7459 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7460 {
7461 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007462 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007463#ifdef FEAT_MBYTE
7464 if (has_mbyte)
7465 {
7466 tl = mb_char2len(c3);
7467 mch_memmove(p, p + n + fl, tl);
7468 mb_char2bytes(c2, p + tl);
7469 mb_char2bytes(c, p + fl + tl);
7470 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
7471 }
7472 else
7473#endif
7474 {
7475 p[0] = p[2];
7476 p[2] = c;
7477 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7478 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007479 }
7480 else
7481 sp->ts_state = STATE_REP_INI;
7482 break;
7483
Bram Moolenaarea424162005-06-16 21:51:00 +00007484 case STATE_UNSWAP3:
7485 /* Undo STATE_SWAP3: "321" -> "123" */
7486 p = fword + sp->ts_fidx;
7487#ifdef FEAT_MBYTE
7488 if (has_mbyte)
7489 {
7490 n = MB_BYTE2LEN(*p);
7491 c2 = mb_ptr2char(p + n);
7492 fl = MB_BYTE2LEN(p[n]);
7493 c = mb_ptr2char(p + n + fl);
7494 tl = MB_BYTE2LEN(p[n + fl]);
7495 mch_memmove(p + fl + tl, p, n);
7496 mb_char2bytes(c, p);
7497 mb_char2bytes(c2, p + tl);
7498 }
7499 else
7500#endif
7501 {
7502 c = *p;
7503 *p = p[2];
7504 p[2] = c;
7505 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007506
Bram Moolenaarea424162005-06-16 21:51:00 +00007507 /* Rotate three characters left: "123" -> "231". We change
7508 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007509 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7510 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007511 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007512 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007513 p = fword + sp->ts_fidx;
7514#ifdef FEAT_MBYTE
7515 if (has_mbyte)
7516 {
7517 n = mb_ptr2len_check(p);
7518 c = mb_ptr2char(p);
7519 fl = mb_ptr2len_check(p + n);
7520 fl += mb_ptr2len_check(p + n + fl);
7521 mch_memmove(p, p + n, fl);
7522 mb_char2bytes(c, p + fl);
7523 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
7524 }
7525 else
7526#endif
7527 {
7528 c = *p;
7529 *p = p[1];
7530 p[1] = p[2];
7531 p[2] = c;
7532 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7533 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007534 }
7535 else
7536 sp->ts_state = STATE_REP_INI;
7537 break;
7538
Bram Moolenaarea424162005-06-16 21:51:00 +00007539 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007540 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00007541 p = fword + sp->ts_fidx;
7542#ifdef FEAT_MBYTE
7543 if (has_mbyte)
7544 {
7545 n = MB_BYTE2LEN(*p);
7546 n += MB_BYTE2LEN(p[n]);
7547 c = mb_ptr2char(p + n);
7548 tl = MB_BYTE2LEN(p[n]);
7549 mch_memmove(p + tl, p, n);
7550 mb_char2bytes(c, p);
7551 }
7552 else
7553#endif
7554 {
7555 c = p[2];
7556 p[2] = p[1];
7557 p[1] = *p;
7558 *p = c;
7559 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007560
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007561 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00007562 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007563 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7564 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007565 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007566 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007567 p = fword + sp->ts_fidx;
7568#ifdef FEAT_MBYTE
7569 if (has_mbyte)
7570 {
7571 n = mb_ptr2len_check(p);
7572 n += mb_ptr2len_check(p + n);
7573 c = mb_ptr2char(p + n);
7574 tl = mb_ptr2len_check(p + n);
7575 mch_memmove(p + tl, p, n);
7576 mb_char2bytes(c, p);
7577 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
7578 }
7579 else
7580#endif
7581 {
7582 c = p[2];
7583 p[2] = p[1];
7584 p[1] = *p;
7585 *p = c;
7586 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7587 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007588 }
7589 else
7590 sp->ts_state = STATE_REP_INI;
7591 break;
7592
Bram Moolenaarea424162005-06-16 21:51:00 +00007593 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007594 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00007595 p = fword + sp->ts_fidx;
7596#ifdef FEAT_MBYTE
7597 if (has_mbyte)
7598 {
7599 c = mb_ptr2char(p);
7600 tl = MB_BYTE2LEN(*p);
7601 n = MB_BYTE2LEN(p[tl]);
7602 n += MB_BYTE2LEN(p[tl + n]);
7603 mch_memmove(p, p + tl, n);
7604 mb_char2bytes(c, p + n);
7605 }
7606 else
7607#endif
7608 {
7609 c = *p;
7610 *p = p[1];
7611 p[1] = p[2];
7612 p[2] = c;
7613 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007614 /*FALLTHROUGH*/
7615
7616 case STATE_REP_INI:
7617 /* Check if matching with REP items from the .aff file would
7618 * work. Quickly skip if there are no REP items or the score
7619 * is going to be too high anyway. */
7620 gap = &lp->lp_slang->sl_rep;
7621 if (gap->ga_len == 0
7622 || sp->ts_score + SCORE_REP >= su->su_maxscore)
7623 {
7624 sp->ts_state = STATE_FINAL;
7625 break;
7626 }
7627
7628 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00007629 * may match. If the index is -1 there is none. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007630 sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]];
7631 if (sp->ts_curi < 0)
7632 {
7633 sp->ts_state = STATE_FINAL;
7634 break;
7635 }
7636
7637 sp->ts_state = STATE_REP;
7638 /*FALLTHROUGH*/
7639
7640 case STATE_REP:
7641 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00007642 * match replace the characters and check if the resulting
7643 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007644 p = fword + sp->ts_fidx;
7645
7646 gap = &lp->lp_slang->sl_rep;
7647 while (sp->ts_curi < gap->ga_len)
7648 {
7649 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
7650 if (*ftp->ft_from != *p)
7651 {
7652 /* past possible matching entries */
7653 sp->ts_curi = gap->ga_len;
7654 break;
7655 }
7656 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
7657 && try_deeper(su, stack, depth, SCORE_REP))
7658 {
7659 /* Need to undo this afterwards. */
7660 sp->ts_state = STATE_REP_UNDO;
7661
7662 /* Change the "from" to the "to" string. */
7663 ++depth;
7664 fl = STRLEN(ftp->ft_from);
7665 tl = STRLEN(ftp->ft_to);
7666 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007667 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007668 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007669 repextra += tl - fl;
7670 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007671 mch_memmove(p, ftp->ft_to, tl);
7672 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +00007673#ifdef FEAT_MBYTE
7674 stack[depth].ts_tcharlen = 0;
7675#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007676 break;
7677 }
7678 }
7679
7680 if (sp->ts_curi >= gap->ga_len)
7681 /* No (more) matches. */
7682 sp->ts_state = STATE_FINAL;
7683
7684 break;
7685
7686 case STATE_REP_UNDO:
7687 /* Undo a REP replacement and continue with the next one. */
7688 ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data
7689 + sp->ts_curi - 1;
7690 fl = STRLEN(ftp->ft_from);
7691 tl = STRLEN(ftp->ft_to);
7692 p = fword + sp->ts_fidx;
7693 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007694 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007695 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007696 repextra -= tl - fl;
7697 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007698 mch_memmove(p, ftp->ft_from, fl);
7699 sp->ts_state = STATE_REP;
7700 break;
7701
7702 default:
7703 /* Did all possible states at this level, go up one level. */
7704 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007705
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007706 if (depth >= 0 && stack[depth].ts_prefixdepth == PREFIXTREE)
7707 {
7708 /* Continue in or go back to the prefix tree. */
7709 byts = pbyts;
7710 idxs = pidxs;
7711 splitoff = 0;
7712 }
7713
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007714 /* Don't check for CTRL-C too often, it takes time. */
7715 line_breakcheck();
7716 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007717 }
7718 }
7719}
7720
7721/*
7722 * Try going one level deeper in the tree.
7723 */
7724 static int
7725try_deeper(su, stack, depth, score_add)
7726 suginfo_T *su;
7727 trystate_T *stack;
7728 int depth;
7729 int score_add;
7730{
7731 int newscore;
7732
7733 /* Refuse to go deeper if the scrore is getting too big. */
7734 newscore = stack[depth].ts_score + score_add;
7735 if (newscore >= su->su_maxscore)
7736 return FALSE;
7737
Bram Moolenaarea424162005-06-16 21:51:00 +00007738 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007739 stack[depth + 1].ts_state = STATE_START;
7740 stack[depth + 1].ts_score = newscore;
7741 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007742 return TRUE;
7743}
7744
7745/*
7746 * "fword" is a good word with case folded. Find the matching keep-case
7747 * words and put it in "kword".
7748 * Theoretically there could be several keep-case words that result in the
7749 * same case-folded word, but we only find one...
7750 */
7751 static void
7752find_keepcap_word(slang, fword, kword)
7753 slang_T *slang;
7754 char_u *fword;
7755 char_u *kword;
7756{
7757 char_u uword[MAXWLEN]; /* "fword" in upper-case */
7758 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007759 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007760
7761 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007762 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007763 int round[MAXWLEN];
7764 int fwordidx[MAXWLEN];
7765 int uwordidx[MAXWLEN];
7766 int kwordlen[MAXWLEN];
7767
7768 int flen, ulen;
7769 int l;
7770 int len;
7771 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007772 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007773 char_u *p;
7774 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007775 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007776
7777 if (byts == NULL)
7778 {
7779 /* array is empty: "cannot happen" */
7780 *kword = NUL;
7781 return;
7782 }
7783
7784 /* Make an all-cap version of "fword". */
7785 allcap_copy(fword, uword);
7786
7787 /*
7788 * Each character needs to be tried both case-folded and upper-case.
7789 * All this gets very complicated if we keep in mind that changing case
7790 * may change the byte length of a multi-byte character...
7791 */
7792 depth = 0;
7793 arridx[0] = 0;
7794 round[0] = 0;
7795 fwordidx[0] = 0;
7796 uwordidx[0] = 0;
7797 kwordlen[0] = 0;
7798 while (depth >= 0)
7799 {
7800 if (fword[fwordidx[depth]] == NUL)
7801 {
7802 /* We are at the end of "fword". If the tree allows a word to end
7803 * here we have found a match. */
7804 if (byts[arridx[depth] + 1] == 0)
7805 {
7806 kword[kwordlen[depth]] = NUL;
7807 return;
7808 }
7809
7810 /* kword is getting too long, continue one level up */
7811 --depth;
7812 }
7813 else if (++round[depth] > 2)
7814 {
7815 /* tried both fold-case and upper-case character, continue one
7816 * level up */
7817 --depth;
7818 }
7819 else
7820 {
7821 /*
7822 * round[depth] == 1: Try using the folded-case character.
7823 * round[depth] == 2: Try using the upper-case character.
7824 */
7825#ifdef FEAT_MBYTE
7826 if (has_mbyte)
7827 {
7828 flen = mb_ptr2len_check(fword + fwordidx[depth]);
7829 ulen = mb_ptr2len_check(uword + uwordidx[depth]);
7830 }
7831 else
7832#endif
7833 ulen = flen = 1;
7834 if (round[depth] == 1)
7835 {
7836 p = fword + fwordidx[depth];
7837 l = flen;
7838 }
7839 else
7840 {
7841 p = uword + uwordidx[depth];
7842 l = ulen;
7843 }
7844
7845 for (tryidx = arridx[depth]; l > 0; --l)
7846 {
7847 /* Perform a binary search in the list of accepted bytes. */
7848 len = byts[tryidx++];
7849 c = *p++;
7850 lo = tryidx;
7851 hi = tryidx + len - 1;
7852 while (lo < hi)
7853 {
7854 m = (lo + hi) / 2;
7855 if (byts[m] > c)
7856 hi = m - 1;
7857 else if (byts[m] < c)
7858 lo = m + 1;
7859 else
7860 {
7861 lo = hi = m;
7862 break;
7863 }
7864 }
7865
7866 /* Stop if there is no matching byte. */
7867 if (hi < lo || byts[lo] != c)
7868 break;
7869
7870 /* Continue at the child (if there is one). */
7871 tryidx = idxs[lo];
7872 }
7873
7874 if (l == 0)
7875 {
7876 /*
7877 * Found the matching char. Copy it to "kword" and go a
7878 * level deeper.
7879 */
7880 if (round[depth] == 1)
7881 {
7882 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
7883 flen);
7884 kwordlen[depth + 1] = kwordlen[depth] + flen;
7885 }
7886 else
7887 {
7888 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
7889 ulen);
7890 kwordlen[depth + 1] = kwordlen[depth] + ulen;
7891 }
7892 fwordidx[depth + 1] = fwordidx[depth] + flen;
7893 uwordidx[depth + 1] = uwordidx[depth] + ulen;
7894
7895 ++depth;
7896 arridx[depth] = tryidx;
7897 round[depth] = 0;
7898 }
7899 }
7900 }
7901
7902 /* Didn't find it: "cannot happen". */
7903 *kword = NUL;
7904}
7905
7906/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007907 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
7908 * su->su_sga.
7909 */
7910 static void
7911score_comp_sal(su)
7912 suginfo_T *su;
7913{
7914 langp_T *lp;
7915 char_u badsound[MAXWLEN];
7916 int i;
7917 suggest_T *stp;
7918 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007919 int score;
7920
7921 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
7922 return;
7923
7924 /* Use the sound-folding of the first language that supports it. */
7925 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7926 lp->lp_slang != NULL; ++lp)
7927 if (lp->lp_slang->sl_sal.ga_len > 0)
7928 {
7929 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007930 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007931
7932 for (i = 0; i < su->su_ga.ga_len; ++i)
7933 {
7934 stp = &SUG(su->su_ga, i);
7935
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007936 /* Case-fold the suggested word, sound-fold it and compute the
7937 * sound-a-like score. */
7938 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007939 if (score < SCORE_MAXMAX)
7940 {
7941 /* Add the suggestion. */
7942 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
7943 sstp->st_word = vim_strsave(stp->st_word);
7944 if (sstp->st_word != NULL)
7945 {
7946 sstp->st_score = score;
7947 sstp->st_altscore = 0;
7948 sstp->st_orglen = stp->st_orglen;
7949 ++su->su_sga.ga_len;
7950 }
7951 }
7952 }
7953 break;
7954 }
7955}
7956
7957/*
7958 * Combine the list of suggestions in su->su_ga and su->su_sga.
7959 * They are intwined.
7960 */
7961 static void
7962score_combine(su)
7963 suginfo_T *su;
7964{
7965 int i;
7966 int j;
7967 garray_T ga;
7968 garray_T *gap;
7969 langp_T *lp;
7970 suggest_T *stp;
7971 char_u *p;
7972 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007973 int round;
7974
7975 /* Add the alternate score to su_ga. */
7976 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7977 lp->lp_slang != NULL; ++lp)
7978 {
7979 if (lp->lp_slang->sl_sal.ga_len > 0)
7980 {
7981 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007982 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007983
7984 for (i = 0; i < su->su_ga.ga_len; ++i)
7985 {
7986 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007987 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
7988 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007989 if (stp->st_altscore == SCORE_MAXMAX)
7990 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
7991 else
7992 stp->st_score = (stp->st_score * 3
7993 + stp->st_altscore) / 4;
7994 stp->st_salscore = FALSE;
7995 }
7996 break;
7997 }
7998 }
7999
8000 /* Add the alternate score to su_sga. */
8001 for (i = 0; i < su->su_sga.ga_len; ++i)
8002 {
8003 stp = &SUG(su->su_sga, i);
8004 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
8005 if (stp->st_score == SCORE_MAXMAX)
8006 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
8007 else
8008 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
8009 stp->st_salscore = TRUE;
8010 }
8011
8012 /* Sort the suggestions and truncate at "maxcount" for both lists. */
8013 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8014 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
8015
8016 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
8017 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
8018 return;
8019
8020 stp = &SUG(ga, 0);
8021 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
8022 {
8023 /* round 1: get a suggestion from su_ga
8024 * round 2: get a suggestion from su_sga */
8025 for (round = 1; round <= 2; ++round)
8026 {
8027 gap = round == 1 ? &su->su_ga : &su->su_sga;
8028 if (i < gap->ga_len)
8029 {
8030 /* Don't add a word if it's already there. */
8031 p = SUG(*gap, i).st_word;
8032 for (j = 0; j < ga.ga_len; ++j)
8033 if (STRCMP(stp[j].st_word, p) == 0)
8034 break;
8035 if (j == ga.ga_len)
8036 stp[ga.ga_len++] = SUG(*gap, i);
8037 else
8038 vim_free(p);
8039 }
8040 }
8041 }
8042
8043 ga_clear(&su->su_ga);
8044 ga_clear(&su->su_sga);
8045
8046 /* Truncate the list to the number of suggestions that will be displayed. */
8047 if (ga.ga_len > su->su_maxcount)
8048 {
8049 for (i = su->su_maxcount; i < ga.ga_len; ++i)
8050 vim_free(stp[i].st_word);
8051 ga.ga_len = su->su_maxcount;
8052 }
8053
8054 su->su_ga = ga;
8055}
8056
8057/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008058 * For the goodword in "stp" compute the soundalike score compared to the
8059 * badword.
8060 */
8061 static int
8062stp_sal_score(stp, su, slang, badsound)
8063 suggest_T *stp;
8064 suginfo_T *su;
8065 slang_T *slang;
8066 char_u *badsound; /* sound-folded badword */
8067{
8068 char_u *p;
8069 char_u badsound2[MAXWLEN];
8070 char_u fword[MAXWLEN];
8071 char_u goodsound[MAXWLEN];
8072
8073 if (stp->st_orglen <= su->su_badlen)
8074 p = badsound;
8075 else
8076 {
8077 /* soundfold the bad word with more characters following */
8078 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
8079
8080 /* When joining two words the sound often changes a lot. E.g., "t he"
8081 * sounds like "t h" while "the" sounds like "@". Avoid that by
8082 * removing the space. Don't do it when the good word also contains a
8083 * space. */
8084 if (vim_iswhite(su->su_badptr[su->su_badlen])
8085 && *skiptowhite(stp->st_word) == NUL)
8086 for (p = fword; *(p = skiptowhite(p)) != NUL; )
8087 mch_memmove(p, p + 1, STRLEN(p));
8088
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008089 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008090 p = badsound2;
8091 }
8092
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008093 /* Sound-fold the word and compute the score for the difference. */
8094 spell_soundfold(slang, stp->st_word, FALSE, goodsound);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008095
8096 return soundalike_score(goodsound, p);
8097}
8098
8099/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008100 * Find suggestions by comparing the word in a sound-a-like form.
8101 */
8102 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00008103suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008104 suginfo_T *su;
8105{
8106 char_u salword[MAXWLEN];
8107 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008108 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008109 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008110 int curi[MAXWLEN];
8111 langp_T *lp;
8112 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008113 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008114 int depth;
8115 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008116 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008117 int round;
8118 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008119 int sound_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008120
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008121 /* Do this for all languages that support sound folding. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008122 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8123 lp->lp_slang != NULL; ++lp)
8124 {
8125 if (lp->lp_slang->sl_sal.ga_len > 0)
8126 {
8127 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008128 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008129
8130 /*
8131 * Go through the whole tree, soundfold each word and compare.
8132 * round 1: use the case-folded tree.
8133 * round 2: use the keep-case tree.
8134 */
8135 for (round = 1; round <= 2; ++round)
8136 {
8137 if (round == 1)
8138 {
8139 byts = lp->lp_slang->sl_fbyts;
8140 idxs = lp->lp_slang->sl_fidxs;
8141 }
8142 else
8143 {
8144 byts = lp->lp_slang->sl_kbyts;
8145 idxs = lp->lp_slang->sl_kidxs;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008146 if (byts == NULL) /* no keep-case words */
8147 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008148 }
8149
8150 depth = 0;
8151 arridx[0] = 0;
8152 curi[0] = 1;
8153 while (depth >= 0 && !got_int)
8154 {
8155 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008156 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008157 /* Done all bytes at this node, go up one level. */
8158 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008159 line_breakcheck();
8160 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008161 else
8162 {
8163 /* Do one more byte at this node. */
8164 n = arridx[depth] + curi[depth];
8165 ++curi[depth];
8166 c = byts[n];
8167 if (c == 0)
8168 {
8169 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008170 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008171 if (round == 2 || (flags & WF_KEEPCAP) == 0)
8172 {
8173 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008174 /* Sound-fold. Only in keep-case tree need to
8175 * case-fold the word. */
8176 spell_soundfold(lp->lp_slang, tword,
8177 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008178
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008179 /* Compute the edit distance between the
8180 * sound-a-like words. */
8181 sound_score = soundalike_score(salword,
8182 tsalword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008183 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008184 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008185 char_u cword[MAXWLEN];
8186 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008187 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008188
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008189 flags |= su->su_badflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008190 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008191 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008192 /* Need to fix case according to
8193 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008194 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008195 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008196 }
8197 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008198 p = tword;
8199
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008200 if (sps_flags & SPS_DOUBLE)
8201 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008202 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008203 sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008204 else
8205 {
8206 /* Compute the score. */
8207 score = spell_edit_score(
8208 su->su_badword, p);
8209 if (sps_flags & SPS_BEST)
8210 /* give a bonus for the good word
8211 * sounding the same as the bad
8212 * word */
8213 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008214 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008215 RESCORE(score, sound_score),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008216 sound_score, TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008217 else
8218 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008219 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008220 score + sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008221 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008222 }
8223 }
8224
8225 /* Skip over other NUL bytes. */
8226 while (byts[n + 1] == 0)
8227 {
8228 ++n;
8229 ++curi[depth];
8230 }
8231 }
8232 else
8233 {
8234 /* Normal char, go one level deeper. */
8235 tword[depth++] = c;
8236 arridx[depth] = idxs[n];
8237 curi[depth] = 1;
8238 }
8239 }
8240 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008241 }
8242 }
8243 }
8244}
8245
8246/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008247 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008248 */
8249 static void
8250make_case_word(fword, cword, flags)
8251 char_u *fword;
8252 char_u *cword;
8253 int flags;
8254{
8255 if (flags & WF_ALLCAP)
8256 /* Make it all upper-case */
8257 allcap_copy(fword, cword);
8258 else if (flags & WF_ONECAP)
8259 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008260 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008261 else
8262 /* Use goodword as-is. */
8263 STRCPY(cword, fword);
8264}
8265
Bram Moolenaarea424162005-06-16 21:51:00 +00008266/*
8267 * Use map string "map" for languages "lp".
8268 */
8269 static void
8270set_map_str(lp, map)
8271 slang_T *lp;
8272 char_u *map;
8273{
8274 char_u *p;
8275 int headc = 0;
8276 int c;
8277 int i;
8278
8279 if (*map == NUL)
8280 {
8281 lp->sl_has_map = FALSE;
8282 return;
8283 }
8284 lp->sl_has_map = TRUE;
8285
8286 /* Init the array and hash table empty. */
8287 for (i = 0; i < 256; ++i)
8288 lp->sl_map_array[i] = 0;
8289#ifdef FEAT_MBYTE
8290 hash_init(&lp->sl_map_hash);
8291#endif
8292
8293 /*
8294 * The similar characters are stored separated with slashes:
8295 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
8296 * before the same slash. For characters above 255 sl_map_hash is used.
8297 */
8298 for (p = map; *p != NUL; )
8299 {
8300#ifdef FEAT_MBYTE
8301 c = mb_ptr2char_adv(&p);
8302#else
8303 c = *p++;
8304#endif
8305 if (c == '/')
8306 headc = 0;
8307 else
8308 {
8309 if (headc == 0)
8310 headc = c;
8311
8312#ifdef FEAT_MBYTE
8313 /* Characters above 255 don't fit in sl_map_array[], put them in
8314 * the hash table. Each entry is the char, a NUL the headchar and
8315 * a NUL. */
8316 if (c >= 256)
8317 {
8318 int cl = mb_char2len(c);
8319 int headcl = mb_char2len(headc);
8320 char_u *b;
8321 hash_T hash;
8322 hashitem_T *hi;
8323
8324 b = alloc((unsigned)(cl + headcl + 2));
8325 if (b == NULL)
8326 return;
8327 mb_char2bytes(c, b);
8328 b[cl] = NUL;
8329 mb_char2bytes(headc, b + cl + 1);
8330 b[cl + 1 + headcl] = NUL;
8331 hash = hash_hash(b);
8332 hi = hash_lookup(&lp->sl_map_hash, b, hash);
8333 if (HASHITEM_EMPTY(hi))
8334 hash_add_item(&lp->sl_map_hash, hi, b, hash);
8335 else
8336 {
8337 /* This should have been checked when generating the .spl
8338 * file. */
8339 EMSG(_("E999: duplicate char in MAP entry"));
8340 vim_free(b);
8341 }
8342 }
8343 else
8344#endif
8345 lp->sl_map_array[c] = headc;
8346 }
8347 }
8348}
8349
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008350/*
8351 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
8352 * lines in the .aff file.
8353 */
8354 static int
8355similar_chars(slang, c1, c2)
8356 slang_T *slang;
8357 int c1;
8358 int c2;
8359{
Bram Moolenaarea424162005-06-16 21:51:00 +00008360 int m1, m2;
8361#ifdef FEAT_MBYTE
8362 char_u buf[MB_MAXBYTES];
8363 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008364
Bram Moolenaarea424162005-06-16 21:51:00 +00008365 if (c1 >= 256)
8366 {
8367 buf[mb_char2bytes(c1, buf)] = 0;
8368 hi = hash_find(&slang->sl_map_hash, buf);
8369 if (HASHITEM_EMPTY(hi))
8370 m1 = 0;
8371 else
8372 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8373 }
8374 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008375#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00008376 m1 = slang->sl_map_array[c1];
8377 if (m1 == 0)
8378 return FALSE;
8379
8380
8381#ifdef FEAT_MBYTE
8382 if (c2 >= 256)
8383 {
8384 buf[mb_char2bytes(c2, buf)] = 0;
8385 hi = hash_find(&slang->sl_map_hash, buf);
8386 if (HASHITEM_EMPTY(hi))
8387 m2 = 0;
8388 else
8389 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8390 }
8391 else
8392#endif
8393 m2 = slang->sl_map_array[c2];
8394
8395 return m1 == m2;
8396}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008397
8398/*
8399 * Add a suggestion to the list of suggestions.
8400 * Do not add a duplicate suggestion or suggestions with a bad score.
8401 * When "use_score" is not zero it's used, otherwise the score is computed
8402 * with spell_edit_score().
8403 */
8404 static void
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008405add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008406 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008407 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008408 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008409 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008410 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008411 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008412 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008413{
8414 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008415 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008416 char_u *p = NULL;
8417 int c = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008418
8419 /* Check that the word wasn't banned. */
8420 if (was_banned(su, goodword))
8421 return;
8422
Bram Moolenaar0c405862005-06-22 22:26:26 +00008423 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
8424 * Remove the common part from "goodword". */
8425 i = badlen - su->su_badlen;
8426 if (i > 0)
8427 {
8428 /* This assumes there was no case folding or it didn't change the
8429 * length... */
8430 p = goodword + STRLEN(goodword) - i;
8431 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
8432 {
8433 badlen = su->su_badlen;
8434 c = *p;
8435 *p = NUL;
8436 }
8437 else
8438 p = NULL;
8439 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008440 else if (i < 0)
8441 {
8442 /* When replacing part of the word check that we actually change
8443 * something. For "the the" a suggestion can be replacing the first
8444 * "the" with itself, since "the" wasn't banned. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008445 if (badlen == (int)STRLEN(goodword)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008446 && STRNCMP(su->su_badword, goodword, badlen) == 0)
8447 return;
8448 }
8449
Bram Moolenaar0c405862005-06-22 22:26:26 +00008450
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008451 if (score <= su->su_maxscore)
8452 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008453 /* Check if the word is already there. Also check the length that is
8454 * being replaced "thes," -> "these" is a different suggestion from
8455 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008456 stp = &SUG(*gap, 0);
8457 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008458 if (STRCMP(stp[i].st_word, goodword) == 0
8459 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008460 {
8461 /* Found it. Remember the lowest score. */
8462 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008463 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008464 stp[i].st_score = score;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008465 stp[i].st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008466 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008467 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008468 break;
8469 }
8470
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008471 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008472 {
8473 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008474 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008475 stp->st_word = vim_strsave(goodword);
8476 if (stp->st_word != NULL)
8477 {
8478 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008479 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008480 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008481 stp->st_orglen = badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008482 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008483
8484 /* If we have too many suggestions now, sort the list and keep
8485 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008486 if (gap->ga_len > SUG_MAX_COUNT(su))
8487 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
8488 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008489 }
8490 }
8491 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008492
8493 if (p != NULL)
8494 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008495}
8496
8497/*
8498 * Add a word to be banned.
8499 */
8500 static void
8501add_banned(su, word)
8502 suginfo_T *su;
8503 char_u *word;
8504{
8505 char_u *s = vim_strsave(word);
8506 hash_T hash;
8507 hashitem_T *hi;
8508
8509 if (s != NULL)
8510 {
8511 hash = hash_hash(s);
8512 hi = hash_lookup(&su->su_banned, s, hash);
8513 if (HASHITEM_EMPTY(hi))
8514 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008515 else
8516 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008517 }
8518}
8519
8520/*
8521 * Return TRUE if a word appears in the list of banned words.
8522 */
8523 static int
8524was_banned(su, word)
8525 suginfo_T *su;
8526 char_u *word;
8527{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008528 hashitem_T *hi = hash_find(&su->su_banned, word);
8529
8530 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008531}
8532
8533/*
8534 * Free the banned words in "su".
8535 */
8536 static void
8537free_banned(su)
8538 suginfo_T *su;
8539{
8540 int todo;
8541 hashitem_T *hi;
8542
8543 todo = su->su_banned.ht_used;
8544 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
8545 {
8546 if (!HASHITEM_EMPTY(hi))
8547 {
8548 vim_free(hi->hi_key);
8549 --todo;
8550 }
8551 }
8552 hash_clear(&su->su_banned);
8553}
8554
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008555/*
8556 * Recompute the score if sound-folding is possible. This is slow,
8557 * thus only done for the final results.
8558 */
8559 static void
8560rescore_suggestions(su)
8561 suginfo_T *su;
8562{
8563 langp_T *lp;
8564 suggest_T *stp;
8565 char_u sal_badword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008566 int i;
8567
8568 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8569 lp->lp_slang != NULL; ++lp)
8570 {
8571 if (lp->lp_slang->sl_sal.ga_len > 0)
8572 {
8573 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008574 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008575
8576 for (i = 0; i < su->su_ga.ga_len; ++i)
8577 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008578 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008579 if (!stp->st_had_bonus)
8580 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008581 stp->st_altscore = stp_sal_score(stp, su,
8582 lp->lp_slang, sal_badword);
8583 if (stp->st_altscore == SCORE_MAXMAX)
8584 stp->st_altscore = SCORE_BIG;
8585 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008586 }
8587 }
8588 break;
8589 }
8590 }
8591}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008592
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008593static int
8594#ifdef __BORLANDC__
8595_RTLENTRYF
8596#endif
8597sug_compare __ARGS((const void *s1, const void *s2));
8598
8599/*
8600 * Function given to qsort() to sort the suggestions on st_score.
8601 */
8602 static int
8603#ifdef __BORLANDC__
8604_RTLENTRYF
8605#endif
8606sug_compare(s1, s2)
8607 const void *s1;
8608 const void *s2;
8609{
8610 suggest_T *p1 = (suggest_T *)s1;
8611 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008612 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008613
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008614 if (n == 0)
8615 return p1->st_altscore - p2->st_altscore;
8616 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008617}
8618
8619/*
8620 * Cleanup the suggestions:
8621 * - Sort on score.
8622 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008623 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008624 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008625 static int
8626cleanup_suggestions(gap, maxscore, keep)
8627 garray_T *gap;
8628 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008629 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008630{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008631 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008632 int i;
8633
8634 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008635 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008636
8637 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008638 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008639 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008640 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008641 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008642 gap->ga_len = keep;
8643 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008644 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008645 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008646}
8647
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008648#if defined(FEAT_EVAL) || defined(PROTO)
8649/*
8650 * Soundfold a string, for soundfold().
8651 * Result is in allocated memory, NULL for an error.
8652 */
8653 char_u *
8654eval_soundfold(word)
8655 char_u *word;
8656{
8657 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008658 char_u sound[MAXWLEN];
8659
8660 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
8661 /* Use the sound-folding of the first language that supports it. */
8662 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8663 lp->lp_slang != NULL; ++lp)
8664 if (lp->lp_slang->sl_sal.ga_len > 0)
8665 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008666 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008667 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008668 return vim_strsave(sound);
8669 }
8670
8671 /* No language with sound folding, return word as-is. */
8672 return vim_strsave(word);
8673}
8674#endif
8675
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008676/*
8677 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
8678 */
8679 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008680spell_soundfold(slang, inword, folded, res)
8681 slang_T *slang;
8682 char_u *inword;
8683 int folded; /* "inword" is already case-folded */
8684 char_u *res;
8685{
8686 char_u fword[MAXWLEN];
8687 char_u *word;
8688
8689 if (slang->sl_sofo)
8690 /* SOFOFROM and SOFOTO used */
8691 spell_soundfold_sofo(slang, inword, res);
8692 else
8693 {
8694 /* SAL items used. Requires the word to be case-folded. */
8695 if (folded)
8696 word = inword;
8697 else
8698 {
8699 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
8700 word = fword;
8701 }
8702
8703#ifdef FEAT_MBYTE
8704 if (has_mbyte)
8705 spell_soundfold_wsal(slang, word, res);
8706 else
8707#endif
8708 spell_soundfold_sal(slang, word, res);
8709 }
8710}
8711
8712/*
8713 * Perform sound folding of "inword" into "res" according to SOFOFROM and
8714 * SOFOTO lines.
8715 */
8716 static void
8717spell_soundfold_sofo(slang, inword, res)
8718 slang_T *slang;
8719 char_u *inword;
8720 char_u *res;
8721{
8722 char_u *s;
8723 int ri = 0;
8724 int c;
8725
8726#ifdef FEAT_MBYTE
8727 if (has_mbyte)
8728 {
8729 int prevc = 0;
8730 int *ip;
8731
8732 /* The sl_sal_first[] table contains the translation for chars up to
8733 * 255, sl_sal the rest. */
8734 for (s = inword; *s != NUL; )
8735 {
8736 c = mb_ptr2char_adv(&s);
8737 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
8738 c = ' ';
8739 else if (c < 256)
8740 c = slang->sl_sal_first[c];
8741 else
8742 {
8743 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
8744 if (ip == NULL) /* empty list, can't match */
8745 c = NUL;
8746 else
8747 for (;;) /* find "c" in the list */
8748 {
8749 if (*ip == 0) /* not found */
8750 {
8751 c = NUL;
8752 break;
8753 }
8754 if (*ip == c) /* match! */
8755 {
8756 c = ip[1];
8757 break;
8758 }
8759 ip += 2;
8760 }
8761 }
8762
8763 if (c != NUL && c != prevc)
8764 {
8765 ri += mb_char2bytes(c, res + ri);
8766 if (ri + MB_MAXBYTES > MAXWLEN)
8767 break;
8768 prevc = c;
8769 }
8770 }
8771 }
8772 else
8773#endif
8774 {
8775 /* The sl_sal_first[] table contains the translation. */
8776 for (s = inword; (c = *s) != NUL; ++s)
8777 {
8778 if (vim_iswhite(c))
8779 c = ' ';
8780 else
8781 c = slang->sl_sal_first[c];
8782 if (c != NUL && (ri == 0 || res[ri - 1] != c))
8783 res[ri++] = c;
8784 }
8785 }
8786
8787 res[ri] = NUL;
8788}
8789
8790 static void
8791spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008792 slang_T *slang;
8793 char_u *inword;
8794 char_u *res;
8795{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008796 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008797 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008798 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008799 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008800 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008801 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008802 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008803 int n, k = 0;
8804 int z0;
8805 int k0;
8806 int n0;
8807 int c;
8808 int pri;
8809 int p0 = -333;
8810 int c0;
8811
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008812 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008813 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008814 if (slang->sl_rem_accents)
8815 {
8816 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008817 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008818 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008819 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008820 {
8821 *t++ = ' ';
8822 s = skipwhite(s);
8823 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008824 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008825 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008826 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008827 *t++ = *s;
8828 ++s;
8829 }
8830 }
8831 *t = NUL;
8832 }
8833 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008834 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008835
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008836 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008837
8838 /*
8839 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008840 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008841 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008842 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008843 while ((c = word[i]) != NUL)
8844 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008845 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008846 n = slang->sl_sal_first[c];
8847 z0 = 0;
8848
8849 if (n >= 0)
8850 {
8851 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008852 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008853 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008854 /* Quickly skip entries that don't match the word. Most
8855 * entries are less then three chars, optimize for that. */
8856 k = smp[n].sm_leadlen;
8857 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008858 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008859 if (word[i + 1] != s[1])
8860 continue;
8861 if (k > 2)
8862 {
8863 for (j = 2; j < k; ++j)
8864 if (word[i + j] != s[j])
8865 break;
8866 if (j < k)
8867 continue;
8868 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008869 }
8870
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008871 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008872 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008873 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008874 while (*pf != NUL && *pf != word[i + k])
8875 ++pf;
8876 if (*pf == NUL)
8877 continue;
8878 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008879 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008880 s = smp[n].sm_rules;
8881 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008882
8883 p0 = *s;
8884 k0 = k;
8885 while (*s == '-' && k > 1)
8886 {
8887 k--;
8888 s++;
8889 }
8890 if (*s == '<')
8891 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008892 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008893 {
8894 /* determine priority */
8895 pri = *s - '0';
8896 s++;
8897 }
8898 if (*s == '^' && *(s + 1) == '^')
8899 s++;
8900
8901 if (*s == NUL
8902 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008903 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008904 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008905 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008906 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008907 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008908 && spell_iswordp(word + i - 1, curbuf)
8909 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008910 {
8911 /* search for followup rules, if: */
8912 /* followup and k > 1 and NO '-' in searchstring */
8913 c0 = word[i + k - 1];
8914 n0 = slang->sl_sal_first[c0];
8915
8916 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008917 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008918 {
8919 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008920 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008921 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008922 /* Quickly skip entries that don't match the word.
8923 * */
8924 k0 = smp[n0].sm_leadlen;
8925 if (k0 > 1)
8926 {
8927 if (word[i + k] != s[1])
8928 continue;
8929 if (k0 > 2)
8930 {
8931 pf = word + i + k + 1;
8932 for (j = 2; j < k0; ++j)
8933 if (*pf++ != s[j])
8934 break;
8935 if (j < k0)
8936 continue;
8937 }
8938 }
8939 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008940
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008941 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008942 {
8943 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008944 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008945 while (*pf != NUL && *pf != word[i + k0])
8946 ++pf;
8947 if (*pf == NUL)
8948 continue;
8949 ++k0;
8950 }
8951
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008952 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008953 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008954 while (*s == '-')
8955 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008956 /* "k0" gets NOT reduced because
8957 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008958 s++;
8959 }
8960 if (*s == '<')
8961 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008962 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008963 {
8964 p0 = *s - '0';
8965 s++;
8966 }
8967
8968 if (*s == NUL
8969 /* *s == '^' cuts */
8970 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008971 && !spell_iswordp(word + i + k0,
8972 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008973 {
8974 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008975 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008976 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008977
8978 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008979 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008980 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008981 /* rule fits; stop search */
8982 break;
8983 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008984 }
8985
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008986 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008987 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008988 }
8989
8990 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008991 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008992 if (s == NULL)
8993 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008994 pf = smp[n].sm_rules;
8995 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008996 if (p0 == 1 && z == 0)
8997 {
8998 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008999 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
9000 || res[reslen - 1] == *s))
9001 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009002 z0 = 1;
9003 z = 1;
9004 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009005 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009006 {
9007 word[i + k0] = *s;
9008 k0++;
9009 s++;
9010 }
9011 if (k > k0)
9012 mch_memmove(word + i + k0, word + i + k,
9013 STRLEN(word + i + k) + 1);
9014
9015 /* new "actual letter" */
9016 c = word[i];
9017 }
9018 else
9019 {
9020 /* no '<' rule used */
9021 i += k - 1;
9022 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009023 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009024 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009025 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009026 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009027 s++;
9028 }
9029 /* new "actual letter" */
9030 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009031 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009032 {
9033 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009034 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009035 mch_memmove(word, word + i + 1,
9036 STRLEN(word + i + 1) + 1);
9037 i = 0;
9038 z0 = 1;
9039 }
9040 }
9041 break;
9042 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009043 }
9044 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009045 else if (vim_iswhite(c))
9046 {
9047 c = ' ';
9048 k = 1;
9049 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009050
9051 if (z0 == 0)
9052 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009053 if (k && !p0 && reslen < MAXWLEN && c != NUL
9054 && (!slang->sl_collapse || reslen == 0
9055 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009056 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009057 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009058
9059 i++;
9060 z = 0;
9061 k = 0;
9062 }
9063 }
9064
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009065 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009066}
9067
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009068#ifdef FEAT_MBYTE
9069/*
9070 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
9071 * Multi-byte version of spell_soundfold().
9072 */
9073 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009074spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009075 slang_T *slang;
9076 char_u *inword;
9077 char_u *res;
9078{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009079 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009080 int word[MAXWLEN];
9081 int wres[MAXWLEN];
9082 int l;
9083 char_u *s;
9084 int *ws;
9085 char_u *t;
9086 int *pf;
9087 int i, j, z;
9088 int reslen;
9089 int n, k = 0;
9090 int z0;
9091 int k0;
9092 int n0;
9093 int c;
9094 int pri;
9095 int p0 = -333;
9096 int c0;
9097 int did_white = FALSE;
9098
9099 /*
9100 * Convert the multi-byte string to a wide-character string.
9101 * Remove accents, if wanted. We actually remove all non-word characters.
9102 * But keep white space.
9103 */
9104 n = 0;
9105 for (s = inword; *s != NUL; )
9106 {
9107 t = s;
9108 c = mb_ptr2char_adv(&s);
9109 if (slang->sl_rem_accents)
9110 {
9111 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
9112 {
9113 if (did_white)
9114 continue;
9115 c = ' ';
9116 did_white = TRUE;
9117 }
9118 else
9119 {
9120 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009121 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009122 continue;
9123 }
9124 }
9125 word[n++] = c;
9126 }
9127 word[n] = NUL;
9128
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009129 /*
9130 * This comes from Aspell phonet.cpp.
9131 * Converted from C++ to C. Added support for multi-byte chars.
9132 * Changed to keep spaces.
9133 */
9134 i = reslen = z = 0;
9135 while ((c = word[i]) != NUL)
9136 {
9137 /* Start with the first rule that has the character in the word. */
9138 n = slang->sl_sal_first[c & 0xff];
9139 z0 = 0;
9140
9141 if (n >= 0)
9142 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009143 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009144 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
9145 {
9146 /* Quickly skip entries that don't match the word. Most
9147 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009148 if (c != ws[0])
9149 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009150 k = smp[n].sm_leadlen;
9151 if (k > 1)
9152 {
9153 if (word[i + 1] != ws[1])
9154 continue;
9155 if (k > 2)
9156 {
9157 for (j = 2; j < k; ++j)
9158 if (word[i + j] != ws[j])
9159 break;
9160 if (j < k)
9161 continue;
9162 }
9163 }
9164
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009165 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009166 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009167 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009168 while (*pf != NUL && *pf != word[i + k])
9169 ++pf;
9170 if (*pf == NUL)
9171 continue;
9172 ++k;
9173 }
9174 s = smp[n].sm_rules;
9175 pri = 5; /* default priority */
9176
9177 p0 = *s;
9178 k0 = k;
9179 while (*s == '-' && k > 1)
9180 {
9181 k--;
9182 s++;
9183 }
9184 if (*s == '<')
9185 s++;
9186 if (VIM_ISDIGIT(*s))
9187 {
9188 /* determine priority */
9189 pri = *s - '0';
9190 s++;
9191 }
9192 if (*s == '^' && *(s + 1) == '^')
9193 s++;
9194
9195 if (*s == NUL
9196 || (*s == '^'
9197 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009198 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009199 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009200 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009201 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009202 && spell_iswordp_w(word + i - 1, curbuf)
9203 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009204 {
9205 /* search for followup rules, if: */
9206 /* followup and k > 1 and NO '-' in searchstring */
9207 c0 = word[i + k - 1];
9208 n0 = slang->sl_sal_first[c0 & 0xff];
9209
9210 if (slang->sl_followup && k > 1 && n0 >= 0
9211 && p0 != '-' && word[i + k] != NUL)
9212 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009213 /* Test follow-up rule for "word[i + k]"; loop over
9214 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009215 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
9216 == (c0 & 0xff); ++n0)
9217 {
9218 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009219 */
9220 if (c0 != ws[0])
9221 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009222 k0 = smp[n0].sm_leadlen;
9223 if (k0 > 1)
9224 {
9225 if (word[i + k] != ws[1])
9226 continue;
9227 if (k0 > 2)
9228 {
9229 pf = word + i + k + 1;
9230 for (j = 2; j < k0; ++j)
9231 if (*pf++ != ws[j])
9232 break;
9233 if (j < k0)
9234 continue;
9235 }
9236 }
9237 k0 += k - 1;
9238
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009239 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009240 {
9241 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009242 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009243 while (*pf != NUL && *pf != word[i + k0])
9244 ++pf;
9245 if (*pf == NUL)
9246 continue;
9247 ++k0;
9248 }
9249
9250 p0 = 5;
9251 s = smp[n0].sm_rules;
9252 while (*s == '-')
9253 {
9254 /* "k0" gets NOT reduced because
9255 * "if (k0 == k)" */
9256 s++;
9257 }
9258 if (*s == '<')
9259 s++;
9260 if (VIM_ISDIGIT(*s))
9261 {
9262 p0 = *s - '0';
9263 s++;
9264 }
9265
9266 if (*s == NUL
9267 /* *s == '^' cuts */
9268 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009269 && !spell_iswordp_w(word + i + k0,
9270 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009271 {
9272 if (k0 == k)
9273 /* this is just a piece of the string */
9274 continue;
9275
9276 if (p0 < pri)
9277 /* priority too low */
9278 continue;
9279 /* rule fits; stop search */
9280 break;
9281 }
9282 }
9283
9284 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
9285 == (c0 & 0xff))
9286 continue;
9287 }
9288
9289 /* replace string */
9290 ws = smp[n].sm_to_w;
9291 s = smp[n].sm_rules;
9292 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
9293 if (p0 == 1 && z == 0)
9294 {
9295 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009296 if (reslen > 0 && ws != NULL && *ws != NUL
9297 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009298 || wres[reslen - 1] == *ws))
9299 reslen--;
9300 z0 = 1;
9301 z = 1;
9302 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009303 if (ws != NULL)
9304 while (*ws != NUL && word[i + k0] != NUL)
9305 {
9306 word[i + k0] = *ws;
9307 k0++;
9308 ws++;
9309 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009310 if (k > k0)
9311 mch_memmove(word + i + k0, word + i + k,
9312 sizeof(int) * (STRLEN(word + i + k) + 1));
9313
9314 /* new "actual letter" */
9315 c = word[i];
9316 }
9317 else
9318 {
9319 /* no '<' rule used */
9320 i += k - 1;
9321 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009322 if (ws != NULL)
9323 while (*ws != NUL && ws[1] != NUL
9324 && reslen < MAXWLEN)
9325 {
9326 if (reslen == 0 || wres[reslen - 1] != *ws)
9327 wres[reslen++] = *ws;
9328 ws++;
9329 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009330 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009331 if (ws == NULL)
9332 c = NUL;
9333 else
9334 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009335 if (strstr((char *)s, "^^") != NULL)
9336 {
9337 if (c != NUL)
9338 wres[reslen++] = c;
9339 mch_memmove(word, word + i + 1,
9340 sizeof(int) * (STRLEN(word + i + 1) + 1));
9341 i = 0;
9342 z0 = 1;
9343 }
9344 }
9345 break;
9346 }
9347 }
9348 }
9349 else if (vim_iswhite(c))
9350 {
9351 c = ' ';
9352 k = 1;
9353 }
9354
9355 if (z0 == 0)
9356 {
9357 if (k && !p0 && reslen < MAXWLEN && c != NUL
9358 && (!slang->sl_collapse || reslen == 0
9359 || wres[reslen - 1] != c))
9360 /* condense only double letters */
9361 wres[reslen++] = c;
9362
9363 i++;
9364 z = 0;
9365 k = 0;
9366 }
9367 }
9368
9369 /* Convert wide characters in "wres" to a multi-byte string in "res". */
9370 l = 0;
9371 for (n = 0; n < reslen; ++n)
9372 {
9373 l += mb_char2bytes(wres[n], res + l);
9374 if (l + MB_MAXBYTES > MAXWLEN)
9375 break;
9376 }
9377 res[l] = NUL;
9378}
9379#endif
9380
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009381/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009382 * Compute a score for two sound-a-like words.
9383 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
9384 * Instead of a generic loop we write out the code. That keeps it fast by
9385 * avoiding checks that will not be possible.
9386 */
9387 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009388soundalike_score(goodstart, badstart)
9389 char_u *goodstart; /* sound-folded good word */
9390 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009391{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009392 char_u *goodsound = goodstart;
9393 char_u *badsound = badstart;
9394 int goodlen;
9395 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009396 int n;
9397 char_u *pl, *ps;
9398 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009399 int score = 0;
9400
9401 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
9402 * counted so much, vowels halfway the word aren't counted at all. */
9403 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
9404 {
9405 score = SCORE_DEL / 2;
9406 if (*badsound == '*')
9407 ++badsound;
9408 else
9409 ++goodsound;
9410 }
9411
9412 goodlen = STRLEN(goodsound);
9413 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009414
9415 /* Return quickly if the lenghts are too different to be fixed by two
9416 * changes. */
9417 n = goodlen - badlen;
9418 if (n < -2 || n > 2)
9419 return SCORE_MAXMAX;
9420
9421 if (n > 0)
9422 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009423 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009424 ps = badsound;
9425 }
9426 else
9427 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009428 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009429 ps = goodsound;
9430 }
9431
9432 /* Skip over the identical part. */
9433 while (*pl == *ps && *pl != NUL)
9434 {
9435 ++pl;
9436 ++ps;
9437 }
9438
9439 switch (n)
9440 {
9441 case -2:
9442 case 2:
9443 /*
9444 * Must delete two characters from "pl".
9445 */
9446 ++pl; /* first delete */
9447 while (*pl == *ps)
9448 {
9449 ++pl;
9450 ++ps;
9451 }
9452 /* strings must be equal after second delete */
9453 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009454 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009455
9456 /* Failed to compare. */
9457 break;
9458
9459 case -1:
9460 case 1:
9461 /*
9462 * Minimal one delete from "pl" required.
9463 */
9464
9465 /* 1: delete */
9466 pl2 = pl + 1;
9467 ps2 = ps;
9468 while (*pl2 == *ps2)
9469 {
9470 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009471 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009472 ++pl2;
9473 ++ps2;
9474 }
9475
9476 /* 2: delete then swap, then rest must be equal */
9477 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9478 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009479 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009480
9481 /* 3: delete then substitute, then the rest must be equal */
9482 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009483 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009484
9485 /* 4: first swap then delete */
9486 if (pl[0] == ps[1] && pl[1] == ps[0])
9487 {
9488 pl2 = pl + 2; /* swap, skip two chars */
9489 ps2 = ps + 2;
9490 while (*pl2 == *ps2)
9491 {
9492 ++pl2;
9493 ++ps2;
9494 }
9495 /* delete a char and then strings must be equal */
9496 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009497 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009498 }
9499
9500 /* 5: first substitute then delete */
9501 pl2 = pl + 1; /* substitute, skip one char */
9502 ps2 = ps + 1;
9503 while (*pl2 == *ps2)
9504 {
9505 ++pl2;
9506 ++ps2;
9507 }
9508 /* delete a char and then strings must be equal */
9509 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009510 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009511
9512 /* Failed to compare. */
9513 break;
9514
9515 case 0:
9516 /*
9517 * Lenghts are equal, thus changes must result in same length: An
9518 * insert is only possible in combination with a delete.
9519 * 1: check if for identical strings
9520 */
9521 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009522 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009523
9524 /* 2: swap */
9525 if (pl[0] == ps[1] && pl[1] == ps[0])
9526 {
9527 pl2 = pl + 2; /* swap, skip two chars */
9528 ps2 = ps + 2;
9529 while (*pl2 == *ps2)
9530 {
9531 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009532 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009533 ++pl2;
9534 ++ps2;
9535 }
9536 /* 3: swap and swap again */
9537 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9538 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009539 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009540
9541 /* 4: swap and substitute */
9542 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009543 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009544 }
9545
9546 /* 5: substitute */
9547 pl2 = pl + 1;
9548 ps2 = ps + 1;
9549 while (*pl2 == *ps2)
9550 {
9551 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009552 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009553 ++pl2;
9554 ++ps2;
9555 }
9556
9557 /* 6: substitute and swap */
9558 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9559 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009560 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009561
9562 /* 7: substitute and substitute */
9563 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009564 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009565
9566 /* 8: insert then delete */
9567 pl2 = pl;
9568 ps2 = ps + 1;
9569 while (*pl2 == *ps2)
9570 {
9571 ++pl2;
9572 ++ps2;
9573 }
9574 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009575 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009576
9577 /* 9: delete then insert */
9578 pl2 = pl + 1;
9579 ps2 = ps;
9580 while (*pl2 == *ps2)
9581 {
9582 ++pl2;
9583 ++ps2;
9584 }
9585 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009586 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009587
9588 /* Failed to compare. */
9589 break;
9590 }
9591
9592 return SCORE_MAXMAX;
9593}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009594
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009595/*
9596 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009597 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009598 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009599 * The algorithm comes from Aspell editdist.cpp, edit_distance().
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009600 * It has been converted from C++ to C and modified to support multi-byte
9601 * characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009602 */
9603 static int
9604spell_edit_score(badword, goodword)
9605 char_u *badword;
9606 char_u *goodword;
9607{
9608 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009609 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009610 int j, i;
9611 int t;
9612 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009613 int pbc, pgc;
9614#ifdef FEAT_MBYTE
9615 char_u *p;
9616 int wbadword[MAXWLEN];
9617 int wgoodword[MAXWLEN];
9618
9619 if (has_mbyte)
9620 {
9621 /* Get the characters from the multi-byte strings and put them in an
9622 * int array for easy access. */
9623 for (p = badword, badlen = 0; *p != NUL; )
9624 wbadword[badlen++] = mb_ptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +00009625 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009626 for (p = goodword, goodlen = 0; *p != NUL; )
9627 wgoodword[goodlen++] = mb_ptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +00009628 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009629 }
9630 else
9631#endif
9632 {
9633 badlen = STRLEN(badword) + 1;
9634 goodlen = STRLEN(goodword) + 1;
9635 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009636
9637 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
9638#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009639 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
9640 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009641 if (cnt == NULL)
9642 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009643
9644 CNT(0, 0) = 0;
9645 for (j = 1; j <= goodlen; ++j)
9646 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
9647
9648 for (i = 1; i <= badlen; ++i)
9649 {
9650 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
9651 for (j = 1; j <= goodlen; ++j)
9652 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009653#ifdef FEAT_MBYTE
9654 if (has_mbyte)
9655 {
9656 bc = wbadword[i - 1];
9657 gc = wgoodword[j - 1];
9658 }
9659 else
9660#endif
9661 {
9662 bc = badword[i - 1];
9663 gc = goodword[j - 1];
9664 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009665 if (bc == gc)
9666 CNT(i, j) = CNT(i - 1, j - 1);
9667 else
9668 {
9669 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009670 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009671 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
9672 else
9673 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
9674
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009675 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009676 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009677#ifdef FEAT_MBYTE
9678 if (has_mbyte)
9679 {
9680 pbc = wbadword[i - 2];
9681 pgc = wgoodword[j - 2];
9682 }
9683 else
9684#endif
9685 {
9686 pbc = badword[i - 2];
9687 pgc = goodword[j - 2];
9688 }
9689 if (bc == pgc && pbc == gc)
9690 {
9691 t = SCORE_SWAP + CNT(i - 2, j - 2);
9692 if (t < CNT(i, j))
9693 CNT(i, j) = t;
9694 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009695 }
9696 t = SCORE_DEL + CNT(i - 1, j);
9697 if (t < CNT(i, j))
9698 CNT(i, j) = t;
9699 t = SCORE_INS + CNT(i, j - 1);
9700 if (t < CNT(i, j))
9701 CNT(i, j) = t;
9702 }
9703 }
9704 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009705
9706 i = CNT(badlen - 1, goodlen - 1);
9707 vim_free(cnt);
9708 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009709}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009710
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009711/*
9712 * ":spelldump"
9713 */
9714/*ARGSUSED*/
9715 void
9716ex_spelldump(eap)
9717 exarg_T *eap;
9718{
9719 buf_T *buf = curbuf;
9720 langp_T *lp;
9721 slang_T *slang;
9722 idx_T arridx[MAXWLEN];
9723 int curi[MAXWLEN];
9724 char_u word[MAXWLEN];
9725 int c;
9726 char_u *byts;
9727 idx_T *idxs;
9728 linenr_T lnum = 0;
9729 int round;
9730 int depth;
9731 int n;
9732 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009733 char_u *region_names = NULL; /* region names being used */
9734 int do_region = TRUE; /* dump region names and numbers */
9735 char_u *p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009736
9737 if (no_spell_checking())
9738 return;
9739
9740 /* Create a new empty buffer by splitting the window. */
9741 do_cmdline_cmd((char_u *)"new");
9742 if (!bufempty() || !buf_valid(buf))
9743 return;
9744
Bram Moolenaar7887d882005-07-01 22:33:52 +00009745 /* Find out if we can support regions: All languages must support the same
9746 * regions or none at all. */
9747 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
9748 {
9749 p = lp->lp_slang->sl_regions;
9750 if (p[0] != 0)
9751 {
9752 if (region_names == NULL) /* first language with regions */
9753 region_names = p;
9754 else if (STRCMP(region_names, p) != 0)
9755 {
9756 do_region = FALSE; /* region names are different */
9757 break;
9758 }
9759 }
9760 }
9761
9762 if (do_region && region_names != NULL)
9763 {
9764 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
9765 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
9766 }
9767 else
9768 do_region = FALSE;
9769
9770 /*
9771 * Loop over all files loaded for the entries in 'spelllang'.
9772 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009773 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
9774 {
9775 slang = lp->lp_slang;
9776
9777 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
9778 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
9779
9780 /* round 1: case-folded tree
9781 * round 2: keep-case tree */
9782 for (round = 1; round <= 2; ++round)
9783 {
9784 if (round == 1)
9785 {
9786 byts = slang->sl_fbyts;
9787 idxs = slang->sl_fidxs;
9788 }
9789 else
9790 {
9791 byts = slang->sl_kbyts;
9792 idxs = slang->sl_kidxs;
9793 }
9794 if (byts == NULL)
9795 continue; /* array is empty */
9796
9797 depth = 0;
9798 arridx[0] = 0;
9799 curi[0] = 1;
9800 while (depth >= 0 && !got_int)
9801 {
9802 if (curi[depth] > byts[arridx[depth]])
9803 {
9804 /* Done all bytes at this node, go up one level. */
9805 --depth;
9806 line_breakcheck();
9807 }
9808 else
9809 {
9810 /* Do one more byte at this node. */
9811 n = arridx[depth] + curi[depth];
9812 ++curi[depth];
9813 c = byts[n];
9814 if (c == 0)
9815 {
9816 /* End of word, deal with the word.
9817 * Don't use keep-case words in the fold-case tree,
9818 * they will appear in the keep-case tree.
9819 * Only use the word when the region matches. */
9820 flags = (int)idxs[n];
9821 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009822 && (do_region
9823 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009824 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009825 & lp->lp_region) != 0))
9826 {
9827 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009828 if (!do_region)
9829 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009830
9831 /* Dump the basic word if there is no prefix or
9832 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009833 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009834 if (c == 0 || curi[depth] == 2)
9835 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009836
9837 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009838 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009839 lnum = apply_prefixes(slang, word, round,
9840 flags, lnum);
9841 }
9842 }
9843 else
9844 {
9845 /* Normal char, go one level deeper. */
9846 word[depth++] = c;
9847 arridx[depth] = idxs[n];
9848 curi[depth] = 1;
9849 }
9850 }
9851 }
9852 }
9853 }
9854
9855 /* Delete the empty line that we started with. */
9856 if (curbuf->b_ml.ml_line_count > 1)
9857 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
9858
9859 redraw_later(NOT_VALID);
9860}
9861
9862/*
9863 * Dump one word: apply case modifications and append a line to the buffer.
9864 */
9865 static void
9866dump_word(word, round, flags, lnum)
9867 char_u *word;
9868 int round;
9869 int flags;
9870 linenr_T lnum;
9871{
9872 int keepcap = FALSE;
9873 char_u *p;
9874 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +00009875 char_u badword[MAXWLEN + 10];
9876 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009877
9878 if (round == 1 && (flags & WF_CAPMASK) != 0)
9879 {
9880 /* Need to fix case according to "flags". */
9881 make_case_word(word, cword, flags);
9882 p = cword;
9883 }
9884 else
9885 {
9886 p = word;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009887 if (round == 2 && ((captype(word, NULL) & WF_KEEPCAP) == 0
9888 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009889 keepcap = TRUE;
9890 }
9891
Bram Moolenaar7887d882005-07-01 22:33:52 +00009892 /* Add flags and regions after a slash. */
9893 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009894 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009895 STRCPY(badword, p);
9896 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009897 if (keepcap)
9898 STRCAT(badword, "=");
9899 if (flags & WF_BANNED)
9900 STRCAT(badword, "!");
9901 else if (flags & WF_RARE)
9902 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009903 if (flags & WF_REGION)
9904 for (i = 0; i < 7; ++i)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009905 if (flags & (0x10000 << i))
Bram Moolenaar7887d882005-07-01 22:33:52 +00009906 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009907 p = badword;
9908 }
9909
9910 ml_append(lnum, p, (colnr_T)0, FALSE);
9911}
9912
9913/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009914 * For ":spelldump": Find matching prefixes for "word". Prepend each to
9915 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009916 * Return the updated line number.
9917 */
9918 static linenr_T
9919apply_prefixes(slang, word, round, flags, startlnum)
9920 slang_T *slang;
9921 char_u *word; /* case-folded word */
9922 int round;
9923 int flags; /* flags with prefix ID */
9924 linenr_T startlnum;
9925{
9926 idx_T arridx[MAXWLEN];
9927 int curi[MAXWLEN];
9928 char_u prefix[MAXWLEN];
9929 int c;
9930 char_u *byts;
9931 idx_T *idxs;
9932 linenr_T lnum = startlnum;
9933 int depth;
9934 int n;
9935 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009936 int i;
9937
9938 byts = slang->sl_pbyts;
9939 idxs = slang->sl_pidxs;
9940 if (byts != NULL) /* array not is empty */
9941 {
9942 /*
9943 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009944 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009945 */
9946 depth = 0;
9947 arridx[0] = 0;
9948 curi[0] = 1;
9949 while (depth >= 0 && !got_int)
9950 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009951 n = arridx[depth];
9952 len = byts[n];
9953 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009954 {
9955 /* Done all bytes at this node, go up one level. */
9956 --depth;
9957 line_breakcheck();
9958 }
9959 else
9960 {
9961 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009962 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009963 ++curi[depth];
9964 c = byts[n];
9965 if (c == 0)
9966 {
9967 /* End of prefix, find out how many IDs there are. */
9968 for (i = 1; i < len; ++i)
9969 if (byts[n + i] != 0)
9970 break;
9971 curi[depth] += i - 1;
9972
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009973 i = valid_word_prefix(i, n, flags, word, slang);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009974 if (i != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009975 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009976 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009977 dump_word(prefix, round,
9978 (i & WF_RAREPFX) ? (flags | WF_RARE)
9979 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009980 }
9981 }
9982 else
9983 {
9984 /* Normal char, go one level deeper. */
9985 prefix[depth++] = c;
9986 arridx[depth] = idxs[n];
9987 curi[depth] = 1;
9988 }
9989 }
9990 }
9991 }
9992
9993 return lnum;
9994}
9995
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009996#endif /* FEAT_SYN_HL */