blob: 94991e734ee638cad36e27d33a350c6180eb5f83 [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... */
4110 if (spin->si_prefroot != NULL)
4111 {
4112 /* ... add a flag to indicate an affix was used. */
4113 use_flags |= WF_HAS_AFF;
4114
4115 /* ... don't use a prefix list if combining
4116 * affixes is not allowed */
4117 if (!ah->ah_combine || comb)
4118 use_pfxlist = NULL;
4119 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004120
Bram Moolenaar51485f02005-06-04 21:55:20 +00004121 /* Store the modified word. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004122 if (store_word(newword, spin, use_flags,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004123 spin->si_region, use_pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004124 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004125
Bram Moolenaar51485f02005-06-04 21:55:20 +00004126 /* When added a suffix and combining is allowed also
4127 * try adding prefixes additionally. */
4128 if (xht != NULL && ah->ah_combine)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004129 if (store_aff_word(newword, spin, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004130 xht, NULL, TRUE,
4131 use_flags, use_pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004132 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004133 }
4134 }
4135 }
4136 }
4137 }
4138
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004139 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004140}
4141
4142/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004143 * Read a file with a list of words.
4144 */
4145 static int
4146spell_read_wordfile(fname, spin)
4147 char_u *fname;
4148 spellinfo_T *spin;
4149{
4150 FILE *fd;
4151 long lnum = 0;
4152 char_u rline[MAXLINELEN];
4153 char_u *line;
4154 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004155 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004156 int l;
4157 int retval = OK;
4158 int did_word = FALSE;
4159 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004160 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004161 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004162
4163 /*
4164 * Open the file.
4165 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004166 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00004167 if (fd == NULL)
4168 {
4169 EMSG2(_(e_notopen), fname);
4170 return FAIL;
4171 }
4172
Bram Moolenaarb765d632005-06-07 21:00:02 +00004173 if (spin->si_verbose || p_verbose > 2)
4174 {
4175 if (!spin->si_verbose)
4176 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004177 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004178 out_flush();
4179 if (!spin->si_verbose)
4180 verbose_leave();
4181 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004182
4183 /*
4184 * Read all the lines in the file one by one.
4185 */
4186 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
4187 {
4188 line_breakcheck();
4189 ++lnum;
4190
4191 /* Skip comment lines. */
4192 if (*rline == '#')
4193 continue;
4194
4195 /* Remove CR, LF and white space from the end. */
4196 l = STRLEN(rline);
4197 while (l > 0 && rline[l - 1] <= ' ')
4198 --l;
4199 if (l == 0)
4200 continue; /* empty or blank line */
4201 rline[l] = NUL;
4202
4203 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
4204 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004205#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004206 if (spin->si_conv.vc_type != CONV_NONE)
4207 {
4208 pc = string_convert(&spin->si_conv, rline, NULL);
4209 if (pc == NULL)
4210 {
4211 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4212 fname, lnum, rline);
4213 continue;
4214 }
4215 line = pc;
4216 }
4217 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004218#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004219 {
4220 pc = NULL;
4221 line = rline;
4222 }
4223
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004224 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00004225 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004226 ++line;
4227 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004228 {
4229 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004230 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
4231 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004232 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004233 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
4234 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004235 else
4236 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004237#ifdef FEAT_MBYTE
4238 char_u *enc;
4239
Bram Moolenaar51485f02005-06-04 21:55:20 +00004240 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004241 line += 10;
4242 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004243 if (enc != NULL && !spin->si_ascii
4244 && convert_setup(&spin->si_conv, enc,
4245 p_enc) == FAIL)
4246 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00004247 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004248 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004249 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004250#else
4251 smsg((char_u *)_("Conversion in %s not supported"), fname);
4252#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004253 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004254 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004255 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004256
Bram Moolenaar3982c542005-06-08 21:56:31 +00004257 if (STRNCMP(line, "regions=", 8) == 0)
4258 {
4259 if (spin->si_region_count > 1)
4260 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
4261 fname, lnum, line);
4262 else
4263 {
4264 line += 8;
4265 if (STRLEN(line) > 16)
4266 smsg((char_u *)_("Too many regions in %s line %d: %s"),
4267 fname, lnum, line);
4268 else
4269 {
4270 spin->si_region_count = STRLEN(line) / 2;
4271 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004272
4273 /* Adjust the mask for a word valid in all regions. */
4274 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004275 }
4276 }
4277 continue;
4278 }
4279
Bram Moolenaar7887d882005-07-01 22:33:52 +00004280 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
4281 fname, lnum, line - 1);
4282 continue;
4283 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004284
Bram Moolenaar7887d882005-07-01 22:33:52 +00004285 flags = 0;
4286 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004287
Bram Moolenaar7887d882005-07-01 22:33:52 +00004288 /* Check for flags and region after a slash. */
4289 p = vim_strchr(line, '/');
4290 if (p != NULL)
4291 {
4292 *p++ = NUL;
4293 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004294 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004295 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004296 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004297 else if (*p == '!') /* Bad, bad, wicked word. */
4298 flags |= WF_BANNED;
4299 else if (*p == '?') /* Rare word. */
4300 flags |= WF_RARE;
4301 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004302 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004303 if ((flags & WF_REGION) == 0) /* first one */
4304 regionmask = 0;
4305 flags |= WF_REGION;
4306
4307 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00004308 if (l > spin->si_region_count)
4309 {
4310 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00004311 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004312 break;
4313 }
4314 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004315 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00004316 else
4317 {
4318 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
4319 fname, lnum, p);
4320 break;
4321 }
4322 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004323 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004324 }
4325
4326 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
4327 if (spin->si_ascii && has_non_ascii(line))
4328 {
4329 ++non_ascii;
4330 continue;
4331 }
4332
4333 /* Normal word: store it. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004334 if (store_word(line, spin, flags, regionmask, NULL) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004335 {
4336 retval = FAIL;
4337 break;
4338 }
4339 did_word = TRUE;
4340 }
4341
4342 vim_free(pc);
4343 fclose(fd);
4344
Bram Moolenaarb765d632005-06-07 21:00:02 +00004345 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
4346 {
4347 if (p_verbose > 2)
4348 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00004349 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
4350 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004351 if (p_verbose > 2)
4352 verbose_leave();
4353 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004354 return retval;
4355}
4356
4357/*
4358 * Get part of an sblock_T, "len" bytes long.
4359 * This avoids calling free() for every little struct we use.
4360 * The memory is cleared to all zeros.
4361 * Returns NULL when out of memory.
4362 */
4363 static void *
4364getroom(blp, len)
4365 sblock_T **blp;
4366 size_t len; /* length needed */
4367{
4368 char_u *p;
4369 sblock_T *bl = *blp;
4370
4371 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
4372 {
4373 /* Allocate a block of memory. This is not freed until much later. */
4374 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
4375 if (bl == NULL)
4376 return NULL;
4377 bl->sb_next = *blp;
4378 *blp = bl;
4379 bl->sb_used = 0;
4380 }
4381
4382 p = bl->sb_data + bl->sb_used;
4383 bl->sb_used += len;
4384
4385 return p;
4386}
4387
4388/*
4389 * Make a copy of a string into memory allocated with getroom().
4390 */
4391 static char_u *
4392getroom_save(blp, s)
4393 sblock_T **blp;
4394 char_u *s;
4395{
4396 char_u *sc;
4397
4398 sc = (char_u *)getroom(blp, STRLEN(s) + 1);
4399 if (sc != NULL)
4400 STRCPY(sc, s);
4401 return sc;
4402}
4403
4404
4405/*
4406 * Free the list of allocated sblock_T.
4407 */
4408 static void
4409free_blocks(bl)
4410 sblock_T *bl;
4411{
4412 sblock_T *next;
4413
4414 while (bl != NULL)
4415 {
4416 next = bl->sb_next;
4417 vim_free(bl);
4418 bl = next;
4419 }
4420}
4421
4422/*
4423 * Allocate the root of a word tree.
4424 */
4425 static wordnode_T *
4426wordtree_alloc(blp)
4427 sblock_T **blp;
4428{
4429 return (wordnode_T *)getroom(blp, sizeof(wordnode_T));
4430}
4431
4432/*
4433 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004434 * Always store it in the case-folded tree. For a keep-case word this is
4435 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
4436 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004437 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004438 * When "pfxlist" is not NULL store the word for each postponed prefix ID.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004439 */
4440 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004441store_word(word, spin, flags, region, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004442 char_u *word;
4443 spellinfo_T *spin;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004444 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004445 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004446 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004447{
4448 int len = STRLEN(word);
4449 int ct = captype(word, word + len);
4450 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004451 int res = OK;
4452 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004453
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004454 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004455 for (p = pfxlist; res == OK; ++p)
4456 {
4457 res = tree_add_word(foldword, spin->si_foldroot, ct | flags,
4458 region, p == NULL ? 0 : *p, &spin->si_blocks);
4459 if (p == NULL || *p == NUL)
4460 break;
4461 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00004462 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004463
4464 if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP))
Bram Moolenaar8db73182005-06-17 21:51:16 +00004465 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004466 for (p = pfxlist; res == OK; ++p)
4467 {
4468 res = tree_add_word(word, spin->si_keeproot, flags,
4469 region, p == NULL ? 0 : *p, &spin->si_blocks);
4470 if (p == NULL || *p == NUL)
4471 break;
4472 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00004473 ++spin->si_keepwcount;
4474 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004475 return res;
4476}
4477
4478/*
4479 * Add word "word" to a word tree at "root".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004480 * When "flags" < 0 we are adding to the prefix tree where flags is used for
4481 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004482 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004483 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004484 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004485tree_add_word(word, root, flags, region, prefixID, blp)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004486 char_u *word;
4487 wordnode_T *root;
4488 int flags;
4489 int region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004490 int prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004491 sblock_T **blp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004492{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004493 wordnode_T *node = root;
4494 wordnode_T *np;
4495 wordnode_T **prev = NULL;
4496 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004497
Bram Moolenaar51485f02005-06-04 21:55:20 +00004498 /* Add each byte of the word to the tree, including the NUL at the end. */
4499 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004500 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004501 /* Look for the sibling that has the same character. They are sorted
4502 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004503 * higher byte value. For zero bytes (end of word) the sorting is
4504 * done on flags and then on prefixID
Bram Moolenaar51485f02005-06-04 21:55:20 +00004505 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004506 while (node != NULL
4507 && (node->wn_byte < word[i]
4508 || (node->wn_byte == NUL
4509 && (flags < 0
4510 ? node->wn_prefixID < prefixID
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004511 : node->wn_flags < (flags & WN_MASK)
4512 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004513 && node->wn_prefixID < prefixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004514 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004515 prev = &node->wn_sibling;
4516 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004517 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004518 if (node == NULL
4519 || node->wn_byte != word[i]
4520 || (word[i] == NUL
4521 && (flags < 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004522 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004523 || node->wn_prefixID != prefixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004524 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004525 /* Allocate a new node. */
4526 np = (wordnode_T *)getroom(blp, sizeof(wordnode_T));
4527 if (np == NULL)
4528 return FAIL;
4529 np->wn_byte = word[i];
4530 *prev = np;
4531 np->wn_sibling = node;
4532 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004533 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004534
Bram Moolenaar51485f02005-06-04 21:55:20 +00004535 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004536 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004537 node->wn_flags = flags;
4538 node->wn_region |= region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004539 node->wn_prefixID = prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004540 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004541 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004542 prev = &node->wn_child;
4543 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004544 }
4545
4546 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004547}
4548
4549/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004550 * Compress a tree: find tails that are identical and can be shared.
4551 */
4552 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00004553wordtree_compress(root, spin)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004554 wordnode_T *root;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004555 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004556{
4557 hashtab_T ht;
4558 int n;
4559 int tot = 0;
4560
4561 if (root != NULL)
4562 {
4563 hash_init(&ht);
4564 n = node_compress(root, &ht, &tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004565 if (spin->si_verbose || p_verbose > 2)
4566 {
4567 if (!spin->si_verbose)
4568 verbose_enter();
4569 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar51485f02005-06-04 21:55:20 +00004570 n, tot, (tot - n) * 100 / tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004571 if (p_verbose > 2)
4572 verbose_leave();
4573 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004574 hash_clear(&ht);
4575 }
4576}
4577
4578/*
4579 * Compress a node, its siblings and its children, depth first.
4580 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004581 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004582 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00004583node_compress(node, ht, tot)
4584 wordnode_T *node;
4585 hashtab_T *ht;
4586 int *tot; /* total count of nodes before compressing,
4587 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004588{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004589 wordnode_T *np;
4590 wordnode_T *tp;
4591 wordnode_T *child;
4592 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004593 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004594 int len = 0;
4595 unsigned nr, n;
4596 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004597
Bram Moolenaar51485f02005-06-04 21:55:20 +00004598 /*
4599 * Go through the list of siblings. Compress each child and then try
4600 * finding an identical child to replace it.
4601 * Note that with "child" we mean not just the node that is pointed to,
4602 * but the whole list of siblings, of which the node is the first.
4603 */
4604 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004605 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004606 ++len;
4607 if ((child = np->wn_child) != NULL)
4608 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00004609 /* Compress the child. This fills hashkey. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004610 compressed += node_compress(child, ht, tot);
4611
4612 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004613 hash = hash_hash(child->wn_u1.hashkey);
4614 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004615 tp = NULL;
4616 if (!HASHITEM_EMPTY(hi))
4617 {
4618 /* There are children with an identical hash value. Now check
4619 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004620 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004621 if (node_equal(child, tp))
4622 {
4623 /* Found one! Now use that child in place of the
4624 * current one. This means the current child is
4625 * dropped from the tree. */
4626 np->wn_child = tp;
4627 ++compressed;
4628 break;
4629 }
4630 if (tp == NULL)
4631 {
4632 /* No other child with this hash value equals the child of
4633 * the node, add it to the linked list after the first
4634 * item. */
4635 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004636 child->wn_u2.next = tp->wn_u2.next;
4637 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004638 }
4639 }
4640 else
4641 /* No other child has this hash value, add it to the
4642 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004643 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004644 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004645 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004646 *tot += len;
4647
4648 /*
4649 * Make a hash key for the node and its siblings, so that we can quickly
4650 * find a lookalike node. This must be done after compressing the sibling
4651 * list, otherwise the hash key would become invalid by the compression.
4652 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004653 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004654 nr = 0;
4655 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004656 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004657 if (np->wn_byte == NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004658 /* end node: use wn_flags, wn_region and wn_prefixID */
4659 n = np->wn_flags + (np->wn_region << 8) + (np->wn_prefixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004660 else
4661 /* byte node: use the byte value and the child pointer */
4662 n = np->wn_byte + ((long_u)np->wn_child << 8);
4663 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004664 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004665
4666 /* Avoid NUL bytes, it terminates the hash key. */
4667 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004668 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004669 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004670 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004671 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004672 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004673 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004674 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
4675 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004676
4677 return compressed;
4678}
4679
4680/*
4681 * Return TRUE when two nodes have identical siblings and children.
4682 */
4683 static int
4684node_equal(n1, n2)
4685 wordnode_T *n1;
4686 wordnode_T *n2;
4687{
4688 wordnode_T *p1;
4689 wordnode_T *p2;
4690
4691 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
4692 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
4693 if (p1->wn_byte != p2->wn_byte
4694 || (p1->wn_byte == NUL
4695 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004696 || p1->wn_region != p2->wn_region
4697 || p1->wn_prefixID != p2->wn_prefixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004698 : (p1->wn_child != p2->wn_child)))
4699 break;
4700
4701 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004702}
4703
4704/*
4705 * Write a number to file "fd", MSB first, in "len" bytes.
4706 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004707 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004708put_bytes(fd, nr, len)
4709 FILE *fd;
4710 long_u nr;
4711 int len;
4712{
4713 int i;
4714
4715 for (i = len - 1; i >= 0; --i)
4716 putc((int)(nr >> (i * 8)), fd);
4717}
4718
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004719static int
4720#ifdef __BORLANDC__
4721_RTLENTRYF
4722#endif
4723rep_compare __ARGS((const void *s1, const void *s2));
4724
4725/*
4726 * Function given to qsort() to sort the REP items on "from" string.
4727 */
4728 static int
4729#ifdef __BORLANDC__
4730_RTLENTRYF
4731#endif
4732rep_compare(s1, s2)
4733 const void *s1;
4734 const void *s2;
4735{
4736 fromto_T *p1 = (fromto_T *)s1;
4737 fromto_T *p2 = (fromto_T *)s2;
4738
4739 return STRCMP(p1->ft_from, p2->ft_from);
4740}
4741
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004742/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004743 * Write the Vim spell file "fname".
4744 */
4745 static void
Bram Moolenaar3982c542005-06-08 21:56:31 +00004746write_vim_spell(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004747 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004748 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004749{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004750 FILE *fd;
4751 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004752 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004753 wordnode_T *tree;
4754 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004755 int i;
4756 int l;
4757 garray_T *gap;
4758 fromto_T *ftp;
4759 char_u *p;
4760 int rr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004761
Bram Moolenaarb765d632005-06-07 21:00:02 +00004762 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00004763 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004764 {
4765 EMSG2(_(e_notopen), fname);
4766 return;
4767 }
4768
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004769 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004770 * <charflagslen> <charflags>
4771 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004772 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004773 * <prefcondcnt> <prefcond> ... */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004774
4775 /* <fileID> */
4776 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
4777 EMSG(_(e_write));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004778
4779 /* write the region names if there is more than one */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004780 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004781 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004782 putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */
4783 fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2),
4784 (size_t)1, fd);
4785 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004786 }
4787 else
4788 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004789 putc(0, fd);
4790 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004791 }
4792
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004793 /*
4794 * Write the table with character flags and table for case folding.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004795 * <charflagslen> <charflags> <fcharlen> <fchars>
4796 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004797 * 'encoding'.
4798 * Also skip this for an .add.spl file, the main spell file must contain
4799 * the table (avoids that it conflicts). File is shorter too.
4800 */
4801 if (spin->si_ascii || spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004802 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004803 putc(0, fd);
4804 putc(0, fd);
4805 putc(0, fd);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004806 }
4807 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00004808 write_spell_chartab(fd);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004809
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004810
4811 if (spin->si_midword == NULL)
4812 put_bytes(fd, 0L, 2); /* <midwordlen> */
4813 else
4814 {
4815 i = STRLEN(spin->si_midword);
4816 put_bytes(fd, (long_u)i, 2); /* <midwordlen> */
4817 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
4818 }
4819
4820
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004821 /* Write the prefix conditions. */
4822 write_spell_prefcond(fd, &spin->si_prefcond);
4823
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004824 /* <SUGGEST> : <repcount> <rep> ...
4825 * <salflags> <salcount> <sal> ...
4826 * <maplen> <mapstr> */
4827
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004828 /* Sort the REP items. */
4829 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
4830 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004831
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004832 /* round 1: REP items
4833 * round 2: SAL items (unless SOFO is used) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004834 for (round = 1; round <= 2; ++round)
4835 {
4836 if (round == 1)
4837 gap = &spin->si_rep;
4838 else
4839 {
4840 gap = &spin->si_sal;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004841
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004842 i = 0;
4843 if (spin->si_followup)
4844 i |= SAL_F0LLOWUP;
4845 if (spin->si_collapse)
4846 i |= SAL_COLLAPSE;
4847 if (spin->si_rem_accents)
4848 i |= SAL_REM_ACCENTS;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004849 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
4850 i |= SAL_SOFO;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004851 putc(i, fd); /* <salflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004852 if (i & SAL_SOFO)
4853 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004854 }
4855
4856 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
4857 for (i = 0; i < gap->ga_len; ++i)
4858 {
4859 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
4860 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
4861 ftp = &((fromto_T *)gap->ga_data)[i];
4862 for (rr = 1; rr <= 2; ++rr)
4863 {
4864 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
4865 l = STRLEN(p);
4866 putc(l, fd);
4867 fwrite(p, l, (size_t)1, fd);
4868 }
4869 }
4870 }
4871
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004872 /* SOFOFROM and SOFOTO */
4873 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
4874 {
4875 put_bytes(fd, 1L, 2); /* <salcount> */
4876
4877 l = STRLEN(spin->si_sofofr);
4878 put_bytes(fd, (long_u)l, 2); /* <salfromlen> */
4879 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <salfrom> */
4880
4881 l = STRLEN(spin->si_sofoto);
4882 put_bytes(fd, (long_u)l, 2); /* <saltolen> */
4883 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <salto> */
4884 }
4885
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004886 put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */
4887 if (spin->si_map.ga_len > 0) /* <mapstr> */
4888 fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len,
4889 (size_t)1, fd);
Bram Moolenaar50cde822005-06-05 21:54:54 +00004890
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004891 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004892 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004893 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004894 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004895 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004896 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004897 if (round == 1)
4898 tree = spin->si_foldroot;
4899 else if (round == 2)
4900 tree = spin->si_keeproot;
4901 else
4902 tree = spin->si_prefroot;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004903
Bram Moolenaar0c405862005-06-22 22:26:26 +00004904 /* Clear the index and wnode fields in the tree. */
4905 clear_node(tree);
4906
Bram Moolenaar51485f02005-06-04 21:55:20 +00004907 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00004908 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00004909 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004910 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004911
Bram Moolenaar51485f02005-06-04 21:55:20 +00004912 /* number of nodes in 4 bytes */
4913 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004914 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004915
Bram Moolenaar51485f02005-06-04 21:55:20 +00004916 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004917 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004918 }
4919
Bram Moolenaar51485f02005-06-04 21:55:20 +00004920 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004921}
4922
4923/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00004924 * Clear the index and wnode fields of "node", it siblings and its
4925 * children. This is needed because they are a union with other items to save
4926 * space.
4927 */
4928 static void
4929clear_node(node)
4930 wordnode_T *node;
4931{
4932 wordnode_T *np;
4933
4934 if (node != NULL)
4935 for (np = node; np != NULL; np = np->wn_sibling)
4936 {
4937 np->wn_u1.index = 0;
4938 np->wn_u2.wnode = NULL;
4939
4940 if (np->wn_byte != NUL)
4941 clear_node(np->wn_child);
4942 }
4943}
4944
4945
4946/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004947 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004948 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00004949 * This first writes the list of possible bytes (siblings). Then for each
4950 * byte recursively write the children.
4951 *
4952 * NOTE: The code here must match the code in read_tree(), since assumptions
4953 * are made about the indexes (so that we don't have to write them in the
4954 * file).
4955 *
4956 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004957 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004958 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00004959put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004960 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004961 wordnode_T *node;
4962 int index;
4963 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004964 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004965{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004966 int newindex = index;
4967 int siblingcount = 0;
4968 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004969 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004970
Bram Moolenaar51485f02005-06-04 21:55:20 +00004971 /* If "node" is zero the tree is empty. */
4972 if (node == NULL)
4973 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004974
Bram Moolenaar51485f02005-06-04 21:55:20 +00004975 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004976 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004977
4978 /* Count the number of siblings. */
4979 for (np = node; np != NULL; np = np->wn_sibling)
4980 ++siblingcount;
4981
4982 /* Write the sibling count. */
4983 if (fd != NULL)
4984 putc(siblingcount, fd); /* <siblingcount> */
4985
4986 /* Write each sibling byte and optionally extra info. */
4987 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004988 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004989 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004990 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004991 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004992 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004993 /* For a NUL byte (end of word) write the flags etc. */
4994 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004995 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004996 /* In PREFIXTREE write the required prefixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004997 * associated condition nr (stored in wn_region). The
4998 * byte value is misused to store the "rare" and "not
4999 * combining" flags */
5000 if (np->wn_flags == (short_u)PFX_FLAGS_R)
5001 putc(BY_FLAGS, fd); /* <byte> */
5002 else if (np->wn_flags == (short_u)PFX_FLAGS_NC)
5003 putc(BY_PFX_NC, fd);
5004 else if (np->wn_flags == (short_u)PFX_FLAGS_RNC)
5005 putc(BY_PFX_RNC, fd);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005006 else
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005007 putc(BY_NOFLAGS, fd);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005008 putc(np->wn_prefixID, fd); /* <prefixID> */
5009 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005010 }
5011 else
5012 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005013 /* For word trees we write the flag/region items. */
5014 flags = np->wn_flags;
5015 if (regionmask != 0 && np->wn_region != regionmask)
5016 flags |= WF_REGION;
5017 if (np->wn_prefixID != 0)
5018 flags |= WF_PFX;
5019 if (flags == 0)
5020 {
5021 /* word without flags or region */
5022 putc(BY_NOFLAGS, fd); /* <byte> */
5023 }
5024 else
5025 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005026 if (np->wn_flags >= 0x100)
5027 {
5028 putc(BY_FLAGS2, fd); /* <byte> */
5029 putc(flags, fd); /* <flags> */
5030 putc((unsigned)flags >> 8, fd); /* <flags2> */
5031 }
5032 else
5033 {
5034 putc(BY_FLAGS, fd); /* <byte> */
5035 putc(flags, fd); /* <flags> */
5036 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005037 if (flags & WF_REGION)
5038 putc(np->wn_region, fd); /* <region> */
5039 if (flags & WF_PFX)
5040 putc(np->wn_prefixID, fd); /* <prefixID> */
5041 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005042 }
5043 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005044 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005045 else
5046 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005047 if (np->wn_child->wn_u1.index != 0
5048 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005049 {
5050 /* The child is written elsewhere, write the reference. */
5051 if (fd != NULL)
5052 {
5053 putc(BY_INDEX, fd); /* <byte> */
5054 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005055 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005056 }
5057 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00005058 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005059 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005060 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005061
Bram Moolenaar51485f02005-06-04 21:55:20 +00005062 if (fd != NULL)
5063 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
5064 {
5065 EMSG(_(e_write));
5066 return 0;
5067 }
5068 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005069 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005070
5071 /* Space used in the array when reading: one for each sibling and one for
5072 * the count. */
5073 newindex += siblingcount + 1;
5074
5075 /* Recursively dump the children of each sibling. */
5076 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005077 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
5078 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005079 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005080
5081 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005082}
5083
5084
5085/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00005086 * ":mkspell [-ascii] outfile infile ..."
5087 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005088 */
5089 void
5090ex_mkspell(eap)
5091 exarg_T *eap;
5092{
5093 int fcount;
5094 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005095 char_u *arg = eap->arg;
5096 int ascii = FALSE;
5097
5098 if (STRNCMP(arg, "-ascii", 6) == 0)
5099 {
5100 ascii = TRUE;
5101 arg = skipwhite(arg + 6);
5102 }
5103
5104 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
5105 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
5106 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005107 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005108 FreeWild(fcount, fnames);
5109 }
5110}
5111
5112/*
5113 * Create a Vim spell file from one or more word lists.
5114 * "fnames[0]" is the output file name.
5115 * "fnames[fcount - 1]" is the last input file name.
5116 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
5117 * and ".spl" is appended to make the output file name.
5118 */
5119 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005120mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005121 int fcount;
5122 char_u **fnames;
5123 int ascii; /* -ascii argument given */
5124 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005125 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005126{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005127 char_u fname[MAXPATHL];
5128 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00005129 char_u **innames;
5130 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005131 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005132 int i;
5133 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005134 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005135 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005136 spellinfo_T spin;
5137
5138 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005139 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005140 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005141 spin.si_followup = TRUE;
5142 spin.si_rem_accents = TRUE;
5143 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
5144 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
5145 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005146 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005147
Bram Moolenaarb765d632005-06-07 21:00:02 +00005148 /* default: fnames[0] is output file, following are input files */
5149 innames = &fnames[1];
5150 incount = fcount - 1;
5151
5152 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005153 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005154 len = STRLEN(fnames[0]);
5155 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
5156 {
5157 /* For ":mkspell path/en.latin1.add" output file is
5158 * "path/en.latin1.add.spl". */
5159 innames = &fnames[0];
5160 incount = 1;
5161 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
5162 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005163 else if (fcount == 1)
5164 {
5165 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
5166 innames = &fnames[0];
5167 incount = 1;
5168 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
5169 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5170 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005171 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
5172 {
5173 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005174 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005175 }
5176 else
5177 /* Name should be language, make the file name from it. */
5178 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
5179 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5180
5181 /* Check for .ascii.spl. */
5182 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
5183 spin.si_ascii = TRUE;
5184
5185 /* Check for .add.spl. */
5186 if (strstr((char *)gettail(wfname), ".add.") != NULL)
5187 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005188 }
5189
Bram Moolenaarb765d632005-06-07 21:00:02 +00005190 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005191 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005192 else if (vim_strchr(gettail(wfname), '_') != NULL)
5193 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005194 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005195 EMSG(_("E754: Only up to 8 regions supported"));
5196 else
5197 {
5198 /* Check for overwriting before doing things that may take a lot of
5199 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005200 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005201 {
5202 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005203 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005204 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005205 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005206 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005207 EMSG2(_(e_isadir2), wfname);
5208 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005209 }
5210
5211 /*
5212 * Init the aff and dic pointers.
5213 * Get the region names if there are more than 2 arguments.
5214 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005215 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005216 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005217 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005218
Bram Moolenaar3982c542005-06-08 21:56:31 +00005219 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005220 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005221 len = STRLEN(innames[i]);
5222 if (STRLEN(gettail(innames[i])) < 5
5223 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005224 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005225 EMSG2(_("E755: Invalid region in %s"), innames[i]);
5226 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005227 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005228 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
5229 spin.si_region_name[i * 2 + 1] =
5230 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005231 }
5232 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005233 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005234
Bram Moolenaar51485f02005-06-04 21:55:20 +00005235 spin.si_foldroot = wordtree_alloc(&spin.si_blocks);
5236 spin.si_keeproot = wordtree_alloc(&spin.si_blocks);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005237 spin.si_prefroot = wordtree_alloc(&spin.si_blocks);
5238 if (spin.si_foldroot == NULL
5239 || spin.si_keeproot == NULL
5240 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005241 {
5242 error = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005243 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005244 }
5245
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005246 /* When not producing a .add.spl file clear the character table when
5247 * we encounter one in the .aff file. This means we dump the current
5248 * one in the .spl file if the .aff file doesn't define one. That's
5249 * better than guessing the contents, the table will match a
5250 * previously loaded spell file. */
5251 if (!spin.si_add)
5252 spin.si_clear_chartab = TRUE;
5253
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005254 /*
5255 * Read all the .aff and .dic files.
5256 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00005257 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005258 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005259 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005260 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005261 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005262 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005263
Bram Moolenaarb765d632005-06-07 21:00:02 +00005264 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005265 if (mch_stat((char *)fname, &st) >= 0)
5266 {
5267 /* Read the .aff file. Will init "spin->si_conv" based on the
5268 * "SET" line. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005269 afile[i] = spell_read_aff(fname, &spin);
5270 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005271 error = TRUE;
5272 else
5273 {
5274 /* Read the .dic file and store the words in the trees. */
5275 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00005276 innames[i]);
5277 if (spell_read_dic(fname, &spin, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005278 error = TRUE;
5279 }
5280 }
5281 else
5282 {
5283 /* No .aff file, try reading the file as a word list. Store
5284 * the words in the trees. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005285 if (spell_read_wordfile(innames[i], &spin) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005286 error = TRUE;
5287 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005288
Bram Moolenaarb765d632005-06-07 21:00:02 +00005289#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005290 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005291 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005292#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005293 }
5294
Bram Moolenaar51485f02005-06-04 21:55:20 +00005295 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005296 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005297 /*
5298 * Remove the dummy NUL from the start of the tree root.
5299 */
5300 spin.si_foldroot = spin.si_foldroot->wn_sibling;
5301 spin.si_keeproot = spin.si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005302 spin.si_prefroot = spin.si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005303
5304 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005305 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005306 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005307 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005308 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005309 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005310 verbose_enter();
5311 MSG(_("Compressing word tree..."));
5312 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005313 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005314 verbose_leave();
5315 }
5316 wordtree_compress(spin.si_foldroot, &spin);
5317 wordtree_compress(spin.si_keeproot, &spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005318 wordtree_compress(spin.si_prefroot, &spin);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005319 }
5320
Bram Moolenaar51485f02005-06-04 21:55:20 +00005321 if (!error)
5322 {
5323 /*
5324 * Write the info in the spell file.
5325 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005326 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005328 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005329 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005330 smsg((char_u *)_("Writing spell file %s ..."), wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005331 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005332 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005333 verbose_leave();
5334 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00005335
Bram Moolenaar3982c542005-06-08 21:56:31 +00005336 write_vim_spell(wfname, &spin);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005337
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005338 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005340 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005341 verbose_enter();
5342 MSG(_("Done!"));
5343 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00005344 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005345 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005346 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005347 verbose_leave();
5348 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005349
Bram Moolenaarb765d632005-06-07 21:00:02 +00005350 /* If the file is loaded need to reload it. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005351 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005352 }
5353
5354 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005355 ga_clear(&spin.si_rep);
5356 ga_clear(&spin.si_sal);
5357 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005358 ga_clear(&spin.si_prefcond);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005359 vim_free(spin.si_midword);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005360 vim_free(spin.si_sofofr);
5361 vim_free(spin.si_sofoto);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005362
5363 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005364 for (i = 0; i < incount; ++i)
5365 if (afile[i] != NULL)
5366 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005367
5368 /* Free all the bits and pieces at once. */
5369 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005370 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005371}
5372
Bram Moolenaarb765d632005-06-07 21:00:02 +00005373
5374/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005375 * ":[count]spellgood {word}"
5376 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00005377 */
5378 void
5379ex_spell(eap)
5380 exarg_T *eap;
5381{
Bram Moolenaar7887d882005-07-01 22:33:52 +00005382 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005383 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005384}
5385
5386/*
5387 * Add "word[len]" to 'spellfile' as a good or bad word.
5388 */
5389 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005390spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005391 char_u *word;
5392 int len;
5393 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005394 int index; /* "zG" and "zW": zero, otherwise index in
5395 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005396{
5397 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005398 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005399 int new_spf = FALSE;
5400 struct stat st;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005401 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005402 char_u fnamebuf[MAXPATHL];
5403 char_u line[MAXWLEN * 2];
5404 long fpos, fpos_next = 0;
5405 int i;
5406 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005407
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005408 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005409 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005410 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00005411 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005412 int_wordlist = vim_tempname('s');
5413 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00005414 return;
5415 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005416 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005417 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005418 else
5419 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005420 /* If 'spellfile' isn't set figure out a good default value. */
5421 if (*curbuf->b_p_spf == NUL)
5422 {
5423 init_spellfile();
5424 new_spf = TRUE;
5425 }
5426
5427 if (*curbuf->b_p_spf == NUL)
5428 {
5429 EMSG(_("E764: 'spellfile' is not set"));
5430 return;
5431 }
5432
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005433 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
5434 {
5435 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
5436 if (i == index)
5437 break;
5438 if (*spf == NUL)
5439 {
5440 EMSGN(_("E765: 'spellfile' does not have %ld enties"), index);
5441 return;
5442 }
5443 }
5444
Bram Moolenaarb765d632005-06-07 21:00:02 +00005445 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005446 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005447 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
5448 buf = NULL;
5449 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00005450 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005451 EMSG(_(e_bufloaded));
5452 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005453 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00005454
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005455 fname = fnamebuf;
5456 }
5457
5458 if (bad)
5459 {
5460 /* When the word also appears as good word we need to remove that one,
5461 * since its flags sort before the one with WF_BANNED. */
5462 fd = mch_fopen((char *)fname, "r");
5463 if (fd != NULL)
5464 {
5465 while (!vim_fgets(line, MAXWLEN * 2, fd))
5466 {
5467 fpos = fpos_next;
5468 fpos_next = ftell(fd);
5469 if (STRNCMP(word, line, len) == 0
5470 && (line[len] == '/' || line[len] < ' '))
5471 {
5472 /* Found duplicate word. Remove it by writing a '#' at
5473 * the start of the line. Mixing reading and writing
5474 * doesn't work for all systems, close the file first. */
5475 fclose(fd);
5476 fd = mch_fopen((char *)fname, "r+");
5477 if (fd == NULL)
5478 break;
5479 if (fseek(fd, fpos, SEEK_SET) == 0)
5480 fputc('#', fd);
5481 fseek(fd, fpos_next, SEEK_SET);
5482 }
5483 }
5484 fclose(fd);
5485 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00005486 }
5487
5488 fd = mch_fopen((char *)fname, "a");
5489 if (fd == NULL && new_spf)
5490 {
5491 /* We just initialized the 'spellfile' option and can't open the file.
5492 * We may need to create the "spell" directory first. We already
5493 * checked the runtime directory is writable in init_spellfile(). */
5494 STRCPY(NameBuff, fname);
5495 *gettail_sep(NameBuff) = NUL;
5496 if (mch_stat((char *)NameBuff, &st) < 0)
5497 {
5498 /* The directory doesn't exist. Try creating it and opening the
5499 * file again. */
5500 vim_mkdir(NameBuff, 0755);
5501 fd = mch_fopen((char *)fname, "a");
5502 }
5503 }
5504
5505 if (fd == NULL)
5506 EMSG2(_(e_notopen), fname);
5507 else
5508 {
5509 if (bad)
5510 fprintf(fd, "%.*s/!\n", len, word);
5511 else
5512 fprintf(fd, "%.*s\n", len, word);
5513 fclose(fd);
5514
5515 /* Update the .add.spl file. */
5516 mkspell(1, &fname, FALSE, TRUE, TRUE);
5517
5518 /* If the .add file is edited somewhere, reload it. */
5519 if (buf != NULL)
5520 buf_reload(buf);
5521
5522 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005523 }
5524}
5525
5526/*
5527 * Initialize 'spellfile' for the current buffer.
5528 */
5529 static void
5530init_spellfile()
5531{
5532 char_u buf[MAXPATHL];
5533 int l;
5534 slang_T *sl;
5535 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005536 char_u *lend;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005537
5538 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
5539 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005540 /* Find the end of the language name. Exclude the region. */
5541 for (lend = curbuf->b_p_spl; *lend != NUL
5542 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
5543 ;
5544
5545 /* Loop over all entries in 'runtimepath'. Use the first one where we
5546 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005547 rtp = p_rtp;
5548 while (*rtp != NUL)
5549 {
5550 /* Copy the path from 'runtimepath' to buf[]. */
5551 copy_option_part(&rtp, buf, MAXPATHL, ",");
5552 if (filewritable(buf) == 2)
5553 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00005554 /* Use the first language name from 'spelllang' and the
5555 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005556 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
5557 l = STRLEN(buf);
5558 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00005559 "/spell/%.*s.%s.add",
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005560 (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00005561 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
5562 ? (char_u *)"ascii" : spell_enc());
5563 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
5564 break;
5565 }
5566 }
5567 }
5568}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005569
Bram Moolenaar51485f02005-06-04 21:55:20 +00005570
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005571/*
5572 * Init the chartab used for spelling for ASCII.
5573 * EBCDIC is not supported!
5574 */
5575 static void
5576clear_spell_chartab(sp)
5577 spelltab_T *sp;
5578{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005579 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005580
5581 /* Init everything to FALSE. */
5582 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
5583 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
5584 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005585 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005586 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005587 sp->st_upper[i] = i;
5588 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005589
5590 /* We include digits. A word shouldn't start with a digit, but handling
5591 * that is done separately. */
5592 for (i = '0'; i <= '9'; ++i)
5593 sp->st_isw[i] = TRUE;
5594 for (i = 'A'; i <= 'Z'; ++i)
5595 {
5596 sp->st_isw[i] = TRUE;
5597 sp->st_isu[i] = TRUE;
5598 sp->st_fold[i] = i + 0x20;
5599 }
5600 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005601 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005602 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005603 sp->st_upper[i] = i - 0x20;
5604 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005605}
5606
5607/*
5608 * Init the chartab used for spelling. Only depends on 'encoding'.
5609 * Called once while starting up and when 'encoding' changes.
5610 * The default is to use isalpha(), but the spell file should define the word
5611 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005612 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005613 */
5614 void
5615init_spell_chartab()
5616{
5617 int i;
5618
5619 did_set_spelltab = FALSE;
5620 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005621#ifdef FEAT_MBYTE
5622 if (enc_dbcs)
5623 {
5624 /* DBCS: assume double-wide characters are word characters. */
5625 for (i = 128; i <= 255; ++i)
5626 if (MB_BYTE2LEN(i) == 2)
5627 spelltab.st_isw[i] = TRUE;
5628 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005629 else if (enc_utf8)
5630 {
5631 for (i = 128; i < 256; ++i)
5632 {
5633 spelltab.st_isu[i] = utf_isupper(i);
5634 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
5635 spelltab.st_fold[i] = utf_fold(i);
5636 spelltab.st_upper[i] = utf_toupper(i);
5637 }
5638 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005639 else
5640#endif
5641 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005642 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005643 for (i = 128; i < 256; ++i)
5644 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005645 if (MB_ISUPPER(i))
5646 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005647 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005648 spelltab.st_isu[i] = TRUE;
5649 spelltab.st_fold[i] = MB_TOLOWER(i);
5650 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005651 else if (MB_ISLOWER(i))
5652 {
5653 spelltab.st_isw[i] = TRUE;
5654 spelltab.st_upper[i] = MB_TOUPPER(i);
5655 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005656 }
5657 }
5658}
5659
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005660static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
5661static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
5662
5663/*
5664 * Set the spell character tables from strings in the affix file.
5665 */
5666 static int
5667set_spell_chartab(fol, low, upp)
5668 char_u *fol;
5669 char_u *low;
5670 char_u *upp;
5671{
5672 /* We build the new tables here first, so that we can compare with the
5673 * previous one. */
5674 spelltab_T new_st;
5675 char_u *pf = fol, *pl = low, *pu = upp;
5676 int f, l, u;
5677
5678 clear_spell_chartab(&new_st);
5679
5680 while (*pf != NUL)
5681 {
5682 if (*pl == NUL || *pu == NUL)
5683 {
5684 EMSG(_(e_affform));
5685 return FAIL;
5686 }
5687#ifdef FEAT_MBYTE
5688 f = mb_ptr2char_adv(&pf);
5689 l = mb_ptr2char_adv(&pl);
5690 u = mb_ptr2char_adv(&pu);
5691#else
5692 f = *pf++;
5693 l = *pl++;
5694 u = *pu++;
5695#endif
5696 /* Every character that appears is a word character. */
5697 if (f < 256)
5698 new_st.st_isw[f] = TRUE;
5699 if (l < 256)
5700 new_st.st_isw[l] = TRUE;
5701 if (u < 256)
5702 new_st.st_isw[u] = TRUE;
5703
5704 /* if "LOW" and "FOL" are not the same the "LOW" char needs
5705 * case-folding */
5706 if (l < 256 && l != f)
5707 {
5708 if (f >= 256)
5709 {
5710 EMSG(_(e_affrange));
5711 return FAIL;
5712 }
5713 new_st.st_fold[l] = f;
5714 }
5715
5716 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005717 * case-folding, it's upper case and the "UPP" is the upper case of
5718 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005719 if (u < 256 && u != f)
5720 {
5721 if (f >= 256)
5722 {
5723 EMSG(_(e_affrange));
5724 return FAIL;
5725 }
5726 new_st.st_fold[u] = f;
5727 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005728 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005729 }
5730 }
5731
5732 if (*pl != NUL || *pu != NUL)
5733 {
5734 EMSG(_(e_affform));
5735 return FAIL;
5736 }
5737
5738 return set_spell_finish(&new_st);
5739}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005740
5741/*
5742 * Set the spell character tables from strings in the .spl file.
5743 */
5744 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005745set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005746 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005747 int cnt; /* length of "flags" */
5748 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005749{
5750 /* We build the new tables here first, so that we can compare with the
5751 * previous one. */
5752 spelltab_T new_st;
5753 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005754 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005755 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005756
5757 clear_spell_chartab(&new_st);
5758
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005759 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005760 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005761 if (i < cnt)
5762 {
5763 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
5764 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
5765 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005766
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005767 if (*p != NUL)
5768 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005769#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005770 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005771#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005772 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005773#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005774 new_st.st_fold[i + 128] = c;
5775 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
5776 new_st.st_upper[c] = i + 128;
5777 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005778 }
5779
5780 return set_spell_finish(&new_st);
5781}
5782
5783 static int
5784set_spell_finish(new_st)
5785 spelltab_T *new_st;
5786{
5787 int i;
5788
5789 if (did_set_spelltab)
5790 {
5791 /* check that it's the same table */
5792 for (i = 0; i < 256; ++i)
5793 {
5794 if (spelltab.st_isw[i] != new_st->st_isw[i]
5795 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005796 || spelltab.st_fold[i] != new_st->st_fold[i]
5797 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005798 {
5799 EMSG(_("E763: Word characters differ between spell files"));
5800 return FAIL;
5801 }
5802 }
5803 }
5804 else
5805 {
5806 /* copy the new spelltab into the one being used */
5807 spelltab = *new_st;
5808 did_set_spelltab = TRUE;
5809 }
5810
5811 return OK;
5812}
5813
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005814/*
Bram Moolenaarea408852005-06-25 22:49:46 +00005815 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005816 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00005817 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005818 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00005819 */
5820 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005821spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00005822 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005823 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00005824{
Bram Moolenaarea408852005-06-25 22:49:46 +00005825#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005826 char_u *s;
5827 int l;
5828 int c;
5829
5830 if (has_mbyte)
5831 {
5832 l = MB_BYTE2LEN(*p);
5833 s = p;
5834 if (l == 1)
5835 {
5836 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005837 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005838 {
5839 s = p + 1; /* skip a mid-word character */
5840 l = MB_BYTE2LEN(*s);
5841 }
5842 }
5843 else
5844 {
5845 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005846 if (c < 256 ? buf->b_spell_ismw[c]
5847 : (buf->b_spell_ismw_mb != NULL
5848 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005849 {
5850 s = p + l;
5851 l = MB_BYTE2LEN(*s);
5852 }
5853 }
5854
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005855 c = mb_ptr2char(s);
5856 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005857 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005858 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005859 }
Bram Moolenaarea408852005-06-25 22:49:46 +00005860#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005861
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005862 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
5863}
5864
5865/*
5866 * Return TRUE if "p" points to a word character.
5867 * Unlike spell_iswordp() this doesn't check for "midword" characters.
5868 */
5869 static int
5870spell_iswordp_nmw(p)
5871 char_u *p;
5872{
5873#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005874 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005875
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005876 if (has_mbyte)
5877 {
5878 c = mb_ptr2char(p);
5879 if (c > 255)
5880 return mb_get_class(p) >= 2;
5881 return spelltab.st_isw[c];
5882 }
5883#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005884 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00005885}
5886
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005887#ifdef FEAT_MBYTE
5888/*
5889 * Return TRUE if "p" points to a word character.
5890 * Wide version of spell_iswordp().
5891 */
5892 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005893spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005894 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005895 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005896{
5897 int *s;
5898
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005899 if (*p < 256 ? buf->b_spell_ismw[*p]
5900 : (buf->b_spell_ismw_mb != NULL
5901 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005902 s = p + 1;
5903 else
5904 s = p;
5905
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005906 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005907 {
5908 if (enc_utf8)
5909 return utf_class(*s) >= 2;
5910 if (enc_dbcs)
5911 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
5912 return 0;
5913 }
5914 return spelltab.st_isw[*s];
5915}
5916#endif
5917
Bram Moolenaarea408852005-06-25 22:49:46 +00005918/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005919 * Write the table with prefix conditions to the .spl file.
5920 */
5921 static void
5922write_spell_prefcond(fd, gap)
5923 FILE *fd;
5924 garray_T *gap;
5925{
5926 int i;
5927 char_u *p;
5928 int len;
5929
5930 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
5931
5932 for (i = 0; i < gap->ga_len; ++i)
5933 {
5934 /* <prefcond> : <condlen> <condstr> */
5935 p = ((char_u **)gap->ga_data)[i];
5936 if (p == NULL)
5937 fputc(0, fd);
5938 else
5939 {
5940 len = STRLEN(p);
5941 fputc(len, fd);
5942 fwrite(p, (size_t)len, (size_t)1, fd);
5943 }
5944 }
5945}
5946
5947/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005948 * Write the current tables into the .spl file.
5949 * This makes sure the same characters are recognized as word characters when
5950 * generating an when using a spell file.
5951 */
5952 static void
5953write_spell_chartab(fd)
5954 FILE *fd;
5955{
5956 char_u charbuf[256 * 4];
5957 int len = 0;
5958 int flags;
5959 int i;
5960
5961 fputc(128, fd); /* <charflagslen> */
5962 for (i = 128; i < 256; ++i)
5963 {
5964 flags = 0;
5965 if (spelltab.st_isw[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005966 flags |= CF_WORD;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005967 if (spelltab.st_isu[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005968 flags |= CF_UPPER;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005969 fputc(flags, fd); /* <charflags> */
5970
Bram Moolenaarb765d632005-06-07 21:00:02 +00005971#ifdef FEAT_MBYTE
5972 if (has_mbyte)
5973 len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
5974 else
5975#endif
5976 charbuf[len++] = spelltab.st_fold[i];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005977 }
5978
5979 put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
5980 fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
5981}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005982
5983/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005984 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
5985 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005986 * When using a multi-byte 'encoding' the length may change!
5987 * Returns FAIL when something wrong.
5988 */
5989 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005990spell_casefold(str, len, buf, buflen)
5991 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005992 int len;
5993 char_u *buf;
5994 int buflen;
5995{
5996 int i;
5997
5998 if (len >= buflen)
5999 {
6000 buf[0] = NUL;
6001 return FAIL; /* result will not fit */
6002 }
6003
6004#ifdef FEAT_MBYTE
6005 if (has_mbyte)
6006 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006007 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006008 char_u *p;
6009 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006010
6011 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006012 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006013 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006014 if (outi + MB_MAXBYTES > buflen)
6015 {
6016 buf[outi] = NUL;
6017 return FAIL;
6018 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006019 c = mb_ptr2char_adv(&p);
6020 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006021 }
6022 buf[outi] = NUL;
6023 }
6024 else
6025#endif
6026 {
6027 /* Be quick for non-multibyte encodings. */
6028 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006029 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006030 buf[i] = NUL;
6031 }
6032
6033 return OK;
6034}
6035
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006036#define SPS_BEST 1
6037#define SPS_FAST 2
6038#define SPS_DOUBLE 4
6039
6040static int sps_flags = SPS_BEST;
6041
6042/*
6043 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
6044 * Sets "sps_flags".
6045 */
6046 int
6047spell_check_sps()
6048{
6049 char_u *p;
6050 char_u buf[MAXPATHL];
6051 int f;
6052
6053 sps_flags = 0;
6054
6055 for (p = p_sps; *p != NUL; )
6056 {
6057 copy_option_part(&p, buf, MAXPATHL, ",");
6058
6059 f = 0;
6060 if (STRCMP(buf, "best") == 0)
6061 f = SPS_BEST;
6062 else if (STRCMP(buf, "fast") == 0)
6063 f = SPS_FAST;
6064 else if (STRCMP(buf, "double") == 0)
6065 f = SPS_DOUBLE;
6066 else if (STRNCMP(buf, "expr:", 5) != 0
6067 && STRNCMP(buf, "file:", 5) != 0)
6068 f = -1;
6069
6070 if (f == -1 || (sps_flags != 0 && f != 0))
6071 {
6072 sps_flags = SPS_BEST;
6073 return FAIL;
6074 }
6075 if (f != 0)
6076 sps_flags = f;
6077 }
6078
6079 if (sps_flags == 0)
6080 sps_flags = SPS_BEST;
6081
6082 return OK;
6083}
6084
6085/* Remember what "z?" replaced. */
6086static char_u *repl_from = NULL;
6087static char_u *repl_to = NULL;
6088
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006089/*
6090 * "z?": Find badly spelled word under or after the cursor.
6091 * Give suggestions for the properly spelled word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006092 */
6093 void
6094spell_suggest()
6095{
6096 char_u *line;
6097 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006098 char_u wcopy[MAXWLEN + 2];
6099 char_u *p;
6100 int i;
6101 int c;
6102 suginfo_T sug;
6103 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006104 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006105 int need_cap;
6106 regmatch_T regmatch;
6107 int endcol;
6108 char_u *line_copy = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006109
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006110 /* Find the start of the badly spelled word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006111 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL
6112 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006113 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006114 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
6115 return;
6116
6117 /* No bad word or it starts after the cursor: use the word under the
6118 * cursor. */
6119 curwin->w_cursor = prev_cursor;
6120 line = ml_get_curline();
6121 p = line + curwin->w_cursor.col;
6122 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006123 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006124 mb_ptr_back(line, p);
6125 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006126 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006127 mb_ptr_adv(p);
6128
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006129 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006130 {
6131 beep_flush();
6132 return;
6133 }
6134 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006135 }
6136
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006137 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006138 line = ml_get_curline();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006139
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006140 /* Figure out if the word should be capitalised. */
6141 need_cap = FALSE;
6142 if (curbuf->b_cap_prog != NULL)
6143 {
6144 endcol = 0;
6145 if (skipwhite(line) - line == curwin->w_cursor.col)
6146 {
6147 /* At start of line, check if previous line is empty or sentence
6148 * ends there. */
6149 if (curwin->w_cursor.lnum == 1)
6150 need_cap = TRUE;
6151 else
6152 {
6153 line = ml_get(curwin->w_cursor.lnum - 1);
6154 if (*skipwhite(line) == NUL)
6155 need_cap = TRUE;
6156 else
6157 {
6158 /* Append a space in place of the line break. */
6159 line_copy = concat_str(line, (char_u *)" ");
6160 line = line_copy;
6161 endcol = STRLEN(line);
6162 }
6163 }
6164 }
6165 else
6166 endcol = curwin->w_cursor.col;
6167
6168 if (endcol > 0)
6169 {
6170 /* Check if sentence ends before the bad word. */
6171 regmatch.regprog = curbuf->b_cap_prog;
6172 regmatch.rm_ic = FALSE;
6173 p = line + endcol;
6174 for (;;)
6175 {
6176 mb_ptr_back(line, p);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006177 if (p == line || spell_iswordp_nmw(p))
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006178 break;
6179 if (vim_regexec(&regmatch, p, 0)
6180 && regmatch.endp[0] == line + endcol)
6181 {
6182 need_cap = TRUE;
6183 break;
6184 }
6185 }
6186 }
6187
6188 /* get the line again, we may have been using the previous one */
6189 line = ml_get_curline();
6190 vim_free(line_copy);
6191 }
6192
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006193 /* Get the list of suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006194 spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2,
6195 TRUE, need_cap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006196
6197 if (sug.su_ga.ga_len == 0)
6198 MSG(_("Sorry, no suggestions"));
6199 else
6200 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006201 vim_free(repl_from);
6202 repl_from = NULL;
6203 vim_free(repl_to);
6204 repl_to = NULL;
6205
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006206 /* List the suggestions. */
6207 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006208 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006209 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
6210 sug.su_badlen, sug.su_badptr);
6211 msg_puts(IObuff);
6212 msg_clr_eos();
6213 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00006214
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006215 msg_scroll = TRUE;
6216 for (i = 0; i < sug.su_ga.ga_len; ++i)
6217 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006218 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006219
6220 /* The suggested word may replace only part of the bad word, add
6221 * the not replaced part. */
6222 STRCPY(wcopy, stp->st_word);
6223 if (sug.su_badlen > stp->st_orglen)
6224 vim_strncpy(wcopy + STRLEN(wcopy),
6225 sug.su_badptr + stp->st_orglen,
6226 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006227 vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\""), i + 1, wcopy);
6228 msg_puts(IObuff);
6229
6230 /* The word may replace more than "su_badlen". */
6231 if (sug.su_badlen < stp->st_orglen)
6232 {
6233 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
6234 stp->st_orglen, sug.su_badptr);
6235 msg_puts(IObuff);
6236 }
6237
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006238 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006239 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006240 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006241 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006242 vim_snprintf((char *)IObuff, IOSIZE, _(" (%s%d - %d)"),
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006243 stp->st_salscore ? "s " : "",
6244 stp->st_score, stp->st_altscore);
6245 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00006246 vim_snprintf((char *)IObuff, IOSIZE, _(" (%d)"),
6247 stp->st_score);
6248 msg_advance(30);
6249 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006250 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006251 msg_putchar('\n');
6252 }
6253
6254 /* Ask for choice. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006255 i = prompt_for_number(&mouse_used);
6256 if (mouse_used)
6257 i -= lines_left;
6258
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006259 if (i > 0 && i <= sug.su_ga.ga_len && u_save_cursor() == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006260 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006261 /* Save the from and to text for :spellrepall. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006262 stp = &SUG(sug.su_ga, i - 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006263 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
6264 repl_to = vim_strsave(stp->st_word);
6265
6266 /* Replace the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006267 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
6268 if (p != NULL)
6269 {
6270 c = sug.su_badptr - line;
6271 mch_memmove(p, line, c);
6272 STRCPY(p + c, stp->st_word);
6273 STRCAT(p, sug.su_badptr + stp->st_orglen);
6274 ml_replace(curwin->w_cursor.lnum, p, FALSE);
6275 curwin->w_cursor.col = c;
6276 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006277
6278 /* For redo we use a change-word command. */
6279 ResetRedobuff();
6280 AppendToRedobuff((char_u *)"ciw");
6281 AppendToRedobuff(stp->st_word);
6282 AppendCharToRedobuff(ESC);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006283 }
6284 }
6285 else
6286 curwin->w_cursor = prev_cursor;
6287 }
6288
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006289 spell_find_cleanup(&sug);
6290}
6291
6292/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006293 * ":spellrepall"
6294 */
6295/*ARGSUSED*/
6296 void
6297ex_spellrepall(eap)
6298 exarg_T *eap;
6299{
6300 pos_T pos = curwin->w_cursor;
6301 char_u *frompat;
6302 int addlen;
6303 char_u *line;
6304 char_u *p;
6305 int didone = FALSE;
6306 int save_ws = p_ws;
6307
6308 if (repl_from == NULL || repl_to == NULL)
6309 {
6310 EMSG(_("E752: No previous spell replacement"));
6311 return;
6312 }
6313 addlen = STRLEN(repl_to) - STRLEN(repl_from);
6314
6315 frompat = alloc(STRLEN(repl_from) + 7);
6316 if (frompat == NULL)
6317 return;
6318 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
6319 p_ws = FALSE;
6320
6321 curwin->w_cursor.lnum = 0;
6322 while (!got_int)
6323 {
6324 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
6325 || u_save_cursor() == FAIL)
6326 break;
6327
6328 /* Only replace when the right word isn't there yet. This happens
6329 * when changing "etc" to "etc.". */
6330 line = ml_get_curline();
6331 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
6332 repl_to, STRLEN(repl_to)) != 0)
6333 {
6334 p = alloc(STRLEN(line) + addlen + 1);
6335 if (p == NULL)
6336 break;
6337 mch_memmove(p, line, curwin->w_cursor.col);
6338 STRCPY(p + curwin->w_cursor.col, repl_to);
6339 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
6340 ml_replace(curwin->w_cursor.lnum, p, FALSE);
6341 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6342 didone = TRUE;
6343 }
6344 curwin->w_cursor.col += STRLEN(repl_to);
6345 }
6346
6347 p_ws = save_ws;
6348 curwin->w_cursor = pos;
6349 vim_free(frompat);
6350
6351 if (!didone)
6352 EMSG2(_("E753: Not found: %s"), repl_from);
6353}
6354
6355/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006356 * Find spell suggestions for "word". Return them in the growarray "*gap" as
6357 * a list of allocated strings.
6358 */
6359 void
6360spell_suggest_list(gap, word, maxcount)
6361 garray_T *gap;
6362 char_u *word;
6363 int maxcount; /* maximum nr of suggestions */
6364{
6365 suginfo_T sug;
6366 int i;
6367 suggest_T *stp;
6368 char_u *wcopy;
6369
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006370 spell_find_suggest(word, &sug, maxcount, FALSE, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006371
6372 /* Make room in "gap". */
6373 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
6374 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
6375 return;
6376
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006377 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006378 {
6379 stp = &SUG(sug.su_ga, i);
6380
6381 /* The suggested word may replace only part of "word", add the not
6382 * replaced part. */
6383 wcopy = alloc(STRLEN(stp->st_word)
6384 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
6385 if (wcopy == NULL)
6386 break;
6387 STRCPY(wcopy, stp->st_word);
6388 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
6389 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
6390 }
6391
6392 spell_find_cleanup(&sug);
6393}
6394
6395/*
6396 * Find spell suggestions for the word at the start of "badptr".
6397 * Return the suggestions in "su->su_ga".
6398 * The maximum number of suggestions is "maxcount".
6399 * Note: does use info for the current window.
6400 * This is based on the mechanisms of Aspell, but completely reimplemented.
6401 */
6402 static void
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006403spell_find_suggest(badptr, su, maxcount, banbadword, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006404 char_u *badptr;
6405 suginfo_T *su;
6406 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00006407 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006408 int need_cap; /* word should start with capital */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006409{
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006410 int attr = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006411 char_u buf[MAXPATHL];
6412 char_u *p;
6413 int do_combine = FALSE;
6414 char_u *sps_copy;
6415#ifdef FEAT_EVAL
6416 static int expr_busy = FALSE;
6417#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006418 int c;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006419
6420 /*
6421 * Set the info in "*su".
6422 */
6423 vim_memset(su, 0, sizeof(suginfo_T));
6424 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
6425 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006426 if (*badptr == NUL)
6427 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006428 hash_init(&su->su_banned);
6429
6430 su->su_badptr = badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006431 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006432 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006433 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006434
6435 if (su->su_badlen >= MAXWLEN)
6436 su->su_badlen = MAXWLEN - 1; /* just in case */
6437 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
6438 (void)spell_casefold(su->su_badptr, su->su_badlen,
6439 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006440 /* get caps flags for bad word */
6441 su->su_badflags = captype(su->su_badptr, su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006442 if (need_cap)
6443 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006444
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006445 /* If the word is not capitalised and spell_check() doesn't consider the
6446 * word to be bad then it might need to be capitalised. Add a suggestion
6447 * for that. */
6448#ifdef FEAT_MBYTE
6449 c = mb_ptr2char(su->su_badptr);
6450#else
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006451 c = *su->su_badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006452#endif
6453 if (!SPELL_ISUPPER(c) && attr == 0)
6454 {
6455 make_case_word(su->su_badword, buf, WF_ONECAP);
6456 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
6457 0, TRUE);
6458 }
6459
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006460 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00006461 if (banbadword)
6462 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006463
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006464 /* Make a copy of 'spellsuggest', because the expression may change it. */
6465 sps_copy = vim_strsave(p_sps);
6466 if (sps_copy == NULL)
6467 return;
6468
6469 /* Loop over the items in 'spellsuggest'. */
6470 for (p = sps_copy; *p != NUL; )
6471 {
6472 copy_option_part(&p, buf, MAXPATHL, ",");
6473
6474 if (STRNCMP(buf, "expr:", 5) == 0)
6475 {
6476#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006477 /* Evaluate an expression. Skip this when called recursively,
6478 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006479 if (!expr_busy)
6480 {
6481 expr_busy = TRUE;
6482 spell_suggest_expr(su, buf + 5);
6483 expr_busy = FALSE;
6484 }
6485#endif
6486 }
6487 else if (STRNCMP(buf, "file:", 5) == 0)
6488 /* Use list of suggestions in a file. */
6489 spell_suggest_file(su, buf + 5);
6490 else
6491 {
6492 /* Use internal method. */
6493 spell_suggest_intern(su);
6494 if (sps_flags & SPS_DOUBLE)
6495 do_combine = TRUE;
6496 }
6497 }
6498
6499 vim_free(sps_copy);
6500
6501 if (do_combine)
6502 /* Combine the two list of suggestions. This must be done last,
6503 * because sorting changes the order again. */
6504 score_combine(su);
6505}
6506
6507#ifdef FEAT_EVAL
6508/*
6509 * Find suggestions by evaluating expression "expr".
6510 */
6511 static void
6512spell_suggest_expr(su, expr)
6513 suginfo_T *su;
6514 char_u *expr;
6515{
6516 list_T *list;
6517 listitem_T *li;
6518 int score;
6519 char_u *p;
6520
6521 /* The work is split up in a few parts to avoid having to export
6522 * suginfo_T.
6523 * First evaluate the expression and get the resulting list. */
6524 list = eval_spell_expr(su->su_badword, expr);
6525 if (list != NULL)
6526 {
6527 /* Loop over the items in the list. */
6528 for (li = list->lv_first; li != NULL; li = li->li_next)
6529 if (li->li_tv.v_type == VAR_LIST)
6530 {
6531 /* Get the word and the score from the items. */
6532 score = get_spellword(li->li_tv.vval.v_list, &p);
6533 if (score >= 0)
6534 add_suggestion(su, &su->su_ga, p,
6535 su->su_badlen, score, 0, TRUE);
6536 }
6537 list_unref(list);
6538 }
6539
6540 /* Sort the suggestions and truncate at "maxcount". */
6541 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
6542}
6543#endif
6544
6545/*
6546 * Find suggestions a file "fname".
6547 */
6548 static void
6549spell_suggest_file(su, fname)
6550 suginfo_T *su;
6551 char_u *fname;
6552{
6553 FILE *fd;
6554 char_u line[MAXWLEN * 2];
6555 char_u *p;
6556 int len;
6557 char_u cword[MAXWLEN];
6558
6559 /* Open the file. */
6560 fd = mch_fopen((char *)fname, "r");
6561 if (fd == NULL)
6562 {
6563 EMSG2(_(e_notopen), fname);
6564 return;
6565 }
6566
6567 /* Read it line by line. */
6568 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
6569 {
6570 line_breakcheck();
6571
6572 p = vim_strchr(line, '/');
6573 if (p == NULL)
6574 continue; /* No Tab found, just skip the line. */
6575 *p++ = NUL;
6576 if (STRICMP(su->su_badword, line) == 0)
6577 {
6578 /* Match! Isolate the good word, until CR or NL. */
6579 for (len = 0; p[len] >= ' '; ++len)
6580 ;
6581 p[len] = NUL;
6582
6583 /* If the suggestion doesn't have specific case duplicate the case
6584 * of the bad word. */
6585 if (captype(p, NULL) == 0)
6586 {
6587 make_case_word(p, cword, su->su_badflags);
6588 p = cword;
6589 }
6590
6591 add_suggestion(su, &su->su_ga, p, su->su_badlen,
6592 SCORE_FILE, 0, TRUE);
6593 }
6594 }
6595
6596 fclose(fd);
6597
6598 /* Sort the suggestions and truncate at "maxcount". */
6599 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
6600}
6601
6602/*
6603 * Find suggestions for the internal method indicated by "sps_flags".
6604 */
6605 static void
6606spell_suggest_intern(su)
6607 suginfo_T *su;
6608{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006609 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006610 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006611 *
6612 * Set a maximum score to limit the combination of operations that is
6613 * tried.
6614 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006615 suggest_try_special(su);
6616
6617 /*
6618 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
6619 * from the .aff file and inserting a space (split the word).
6620 */
6621 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006622
6623 /* For the resulting top-scorers compute the sound-a-like score. */
6624 if (sps_flags & SPS_DOUBLE)
6625 score_comp_sal(su);
6626
6627 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006628 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006629 *
6630 * Only do this when we don't have a lot of suggestions yet, because it's
6631 * very slow and often doesn't find new suggestions.
6632 */
6633 if ((sps_flags & SPS_DOUBLE)
6634 || (!(sps_flags & SPS_FAST)
6635 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
6636 {
6637 /* Allow a higher score now. */
6638 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006639 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006640 }
6641
6642 /* When CTRL-C was hit while searching do show the results. */
6643 ui_breakcheck();
6644 if (got_int)
6645 {
6646 (void)vgetc();
6647 got_int = FALSE;
6648 }
6649
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006650 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006651 {
6652 if (sps_flags & SPS_BEST)
6653 /* Adjust the word score for how it sounds like. */
6654 rescore_suggestions(su);
6655
6656 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006657 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006658 }
6659}
6660
6661/*
6662 * Free the info put in "*su" by spell_find_suggest().
6663 */
6664 static void
6665spell_find_cleanup(su)
6666 suginfo_T *su;
6667{
6668 int i;
6669
6670 /* Free the suggestions. */
6671 for (i = 0; i < su->su_ga.ga_len; ++i)
6672 vim_free(SUG(su->su_ga, i).st_word);
6673 ga_clear(&su->su_ga);
6674 for (i = 0; i < su->su_sga.ga_len; ++i)
6675 vim_free(SUG(su->su_sga, i).st_word);
6676 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006677
6678 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006679 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006680}
6681
6682/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006683 * Make a copy of "word", with the first letter upper or lower cased, to
6684 * "wcopy[MAXWLEN]". "word" must not be empty.
6685 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006686 */
6687 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006688onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006689 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006690 char_u *wcopy;
6691 int upper; /* TRUE: first letter made upper case */
6692{
6693 char_u *p;
6694 int c;
6695 int l;
6696
6697 p = word;
6698#ifdef FEAT_MBYTE
6699 if (has_mbyte)
6700 c = mb_ptr2char_adv(&p);
6701 else
6702#endif
6703 c = *p++;
6704 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006705 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006706 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006707 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006708#ifdef FEAT_MBYTE
6709 if (has_mbyte)
6710 l = mb_char2bytes(c, wcopy);
6711 else
6712#endif
6713 {
6714 l = 1;
6715 wcopy[0] = c;
6716 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006717 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006718}
6719
6720/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006721 * Make a copy of "word" with all the letters upper cased into
6722 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006723 */
6724 static void
6725allcap_copy(word, wcopy)
6726 char_u *word;
6727 char_u *wcopy;
6728{
6729 char_u *s;
6730 char_u *d;
6731 int c;
6732
6733 d = wcopy;
6734 for (s = word; *s != NUL; )
6735 {
6736#ifdef FEAT_MBYTE
6737 if (has_mbyte)
6738 c = mb_ptr2char_adv(&s);
6739 else
6740#endif
6741 c = *s++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006742 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006743
6744#ifdef FEAT_MBYTE
6745 if (has_mbyte)
6746 {
6747 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
6748 break;
6749 d += mb_char2bytes(c, d);
6750 }
6751 else
6752#endif
6753 {
6754 if (d - wcopy >= MAXWLEN - 1)
6755 break;
6756 *d++ = c;
6757 }
6758 }
6759 *d = NUL;
6760}
6761
6762/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006763 * Try finding suggestions by recognizing specific situations.
6764 */
6765 static void
6766suggest_try_special(su)
6767 suginfo_T *su;
6768{
6769 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006770 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006771 int c;
6772 char_u word[MAXWLEN];
6773
6774 /*
6775 * Recognize a word that is repeated: "the the".
6776 */
6777 p = skiptowhite(su->su_fbadword);
6778 len = p - su->su_fbadword;
6779 p = skipwhite(p);
6780 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
6781 {
6782 /* Include badflags: if the badword is onecap or allcap
6783 * use that for the goodword too: "The the" -> "The". */
6784 c = su->su_fbadword[len];
6785 su->su_fbadword[len] = NUL;
6786 make_case_word(su->su_fbadword, word, su->su_badflags);
6787 su->su_fbadword[len] = c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006788 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006789 }
6790}
6791
6792/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006793 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00006794 *
6795 * This uses a state machine. At each node in the tree we try various
6796 * operations. When trying if an operation work "depth" is increased and the
6797 * stack[] is used to store info. This allows combinations, thus insert one
6798 * character, replace one and delete another. The number of changes is
6799 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006800 */
6801 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00006802suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006803 suginfo_T *su;
6804{
6805 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
6806 char_u tword[MAXWLEN]; /* good word collected so far */
6807 trystate_T stack[MAXWLEN];
6808 char_u preword[MAXWLEN * 3]; /* word found with proper case (appended
6809 * to for word split) */
6810 char_u prewordlen = 0; /* length of word in "preword" */
6811 int splitoff = 0; /* index in tword after last split */
6812 trystate_T *sp;
6813 int newscore;
6814 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006815 char_u *byts, *fbyts, *pbyts;
6816 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006817 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00006818 int c, c2, c3;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006819 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006820 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006821 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006822 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006823 int len;
6824 char_u *p;
6825 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00006826 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006827 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006828
6829 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00006830 * to find matches (esp. REP items). Append some more text, changing
6831 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006832 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006833 n = STRLEN(fword);
6834 p = su->su_badptr + su->su_badlen;
6835 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006836
6837 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
6838 lp->lp_slang != NULL; ++lp)
6839 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006840 /*
6841 * Go through the whole case-fold tree, try changes at each node.
6842 * "tword[]" contains the word collected from nodes in the tree.
6843 * "fword[]" the word we are trying to match with (initially the bad
6844 * word).
6845 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006846 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006847 sp = &stack[0];
6848 sp->ts_state = STATE_START;
6849 sp->ts_score = 0;
6850 sp->ts_curi = 1;
6851 sp->ts_fidx = 0;
6852 sp->ts_fidxtry = 0;
6853 sp->ts_twordlen = 0;
6854 sp->ts_arridx = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00006855#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006856 sp->ts_tcharlen = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00006857#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006858
Bram Moolenaarea424162005-06-16 21:51:00 +00006859 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006860 * When there are postponed prefixes we need to use these first. At
6861 * the end of the prefix we continue in the case-fold tree.
6862 */
6863 fbyts = lp->lp_slang->sl_fbyts;
6864 fidxs = lp->lp_slang->sl_fidxs;
6865 pbyts = lp->lp_slang->sl_pbyts;
6866 pidxs = lp->lp_slang->sl_pidxs;
6867 if (pbyts != NULL)
6868 {
6869 byts = pbyts;
6870 idxs = pidxs;
6871 sp->ts_prefixdepth = PREFIXTREE;
6872 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
6873 }
6874 else
6875 {
6876 byts = fbyts;
6877 idxs = fidxs;
6878 sp->ts_prefixdepth = NOPREFIX;
6879 }
6880
6881 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00006882 * Loop to find all suggestions. At each round we either:
6883 * - For the current state try one operation, advance "ts_curi",
6884 * increase "depth".
6885 * - When a state is done go to the next, set "ts_state".
6886 * - When all states are tried decrease "depth".
6887 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006888 while (depth >= 0 && !got_int)
6889 {
6890 sp = &stack[depth];
6891 switch (sp->ts_state)
6892 {
6893 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006894 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006895 /*
6896 * Start of node: Deal with NUL bytes, which means
6897 * tword[] may end here.
6898 */
6899 arridx = sp->ts_arridx; /* current node in the tree */
6900 len = byts[arridx]; /* bytes in this node */
6901 arridx += sp->ts_curi; /* index of current byte */
6902
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006903 if (sp->ts_prefixdepth == PREFIXTREE)
6904 {
6905 /* Skip over the NUL bytes, we use them later. */
6906 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
6907 ;
6908 sp->ts_curi += n;
6909
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006910 /* Always past NUL bytes now. */
6911 n = (int)sp->ts_state;
6912 sp->ts_state = STATE_ENDNUL;
6913
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006914 /* At end of a prefix or at start of prefixtree: check for
6915 * following word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006916 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006917 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006918 ++depth;
6919 stack[depth] = stack[depth - 1];
6920 sp = &stack[depth];
6921 sp->ts_prefixdepth = depth - 1;
6922 byts = fbyts;
6923 idxs = fidxs;
6924 sp->ts_state = STATE_START;
6925 sp->ts_curi = 1; /* start just after length byte */
6926 sp->ts_arridx = 0;
6927
6928 /* Move the prefix to preword[] so that
6929 * find_keepcap_word() works. */
6930 prewordlen = splitoff = sp->ts_twordlen;
6931 mch_memmove(preword, tword, splitoff);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006932 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006933 break;
6934 }
6935
Bram Moolenaar0c405862005-06-22 22:26:26 +00006936 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006937 {
6938 /* Past bytes in node and/or past NUL bytes. */
6939 sp->ts_state = STATE_ENDNUL;
6940 break;
6941 }
6942
6943 /*
6944 * End of word in tree.
6945 */
6946 ++sp->ts_curi; /* eat one NUL byte */
6947
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006948 flags = (int)idxs[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006949
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006950 if (sp->ts_prefixdepth < MAXWLEN)
6951 {
6952 /* There was a prefix before the word. Check that the
6953 * prefix can be used with this word. */
6954 /* Count the length of the NULs in the prefix. If there
6955 * are none this must be the first try without a prefix.
6956 */
6957 n = stack[sp->ts_prefixdepth].ts_arridx;
6958 len = pbyts[n++];
6959 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
6960 ;
6961 if (c > 0)
6962 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006963 /* The prefix ID is stored three bytes above the
6964 * flags. */
6965 c = valid_word_prefix(c, n, flags,
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006966 tword + splitoff, lp->lp_slang);
6967 if (c == 0)
6968 break;
6969
6970 /* Use the WF_RARE flag for a rare prefix. */
6971 if (c & WF_RAREPFX)
6972 flags |= WF_RARE;
6973 }
6974 }
6975
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006976 /*
6977 * Form the word with proper case in preword.
6978 * If there is a word from a previous split, append.
6979 */
6980 tword[sp->ts_twordlen] = NUL;
6981 if (flags & WF_KEEPCAP)
6982 /* Must find the word in the keep-case tree. */
6983 find_keepcap_word(lp->lp_slang, tword + splitoff,
6984 preword + prewordlen);
6985 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00006986 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006987 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00006988 * use that for the goodword too. But if the badword is
6989 * allcap and it's only one char long use onecap. */
6990 c = su->su_badflags;
6991 if ((c & WF_ALLCAP)
6992#ifdef FEAT_MBYTE
6993 && su->su_badlen == mb_ptr2len_check(su->su_badptr)
6994#else
6995 && su->su_badlen == 1
6996#endif
6997 )
6998 c = WF_ONECAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006999 make_case_word(tword + splitoff,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007000 preword + prewordlen, flags | c);
7001 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007002
7003 /* Don't use a banned word. It may appear again as a good
7004 * word, thus remember it. */
7005 if (flags & WF_BANNED)
7006 {
7007 add_banned(su, preword + prewordlen);
7008 break;
7009 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007010 if (was_banned(su, preword + prewordlen)
7011 || was_banned(su, preword))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007012 break;
7013
7014 newscore = 0;
7015 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007016 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007017 newscore += SCORE_REGION;
7018 if (flags & WF_RARE)
7019 newscore += SCORE_RARE;
7020
Bram Moolenaar0c405862005-06-22 22:26:26 +00007021 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007022 captype(preword + prewordlen, NULL)))
7023 newscore += SCORE_ICASE;
7024
Bram Moolenaar0c405862005-06-22 22:26:26 +00007025 if ((fword[sp->ts_fidx] == NUL
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007026 || !spell_iswordp(fword + sp->ts_fidx, curbuf))
Bram Moolenaar0c405862005-06-22 22:26:26 +00007027 && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007028 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007029 /* The badword also ends: add suggestions. Give a penalty
7030 * when changing non-word char to word char, e.g., "thes,"
7031 * -> "these". */
7032 p = fword + sp->ts_fidx;
7033#ifdef FEAT_MBYTE
7034 if (has_mbyte)
7035 mb_ptr_back(fword, p);
7036 else
7037#endif
7038 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007039 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007040 {
7041 p = preword + STRLEN(preword);
7042#ifdef FEAT_MBYTE
7043 if (has_mbyte)
7044 mb_ptr_back(preword, p);
7045 else
7046#endif
7047 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007048 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007049 newscore += SCORE_NONWORD;
7050 }
7051
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007052 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007053 sp->ts_fidx - repextra,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007054 sp->ts_score + newscore, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007055 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007056 else if (sp->ts_fidx >= sp->ts_fidxtry
7057#ifdef FEAT_MBYTE
7058 /* Don't split halfway a character. */
7059 && (!has_mbyte || sp->ts_tcharlen == 0)
7060#endif
7061 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007062 {
7063 /* The word in the tree ends but the badword
7064 * continues: try inserting a space and check that a valid
7065 * words starts at fword[sp->ts_fidx]. */
7066 if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT))
7067 {
7068 /* Save things to be restored at STATE_SPLITUNDO. */
7069 sp->ts_save_prewordlen = prewordlen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007070 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007071 sp->ts_save_splitoff = splitoff;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007072 sp->ts_state = STATE_SPLITUNDO;
7073
7074 ++depth;
7075 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007076
7077 /* Append a space to preword. */
7078 STRCAT(preword, " ");
7079 prewordlen = STRLEN(preword);
7080 splitoff = sp->ts_twordlen;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007081
7082 /* If the badword has a non-word character at this
7083 * position skip it. That means replacing the
7084 * non-word character with a space. */
7085 if (!spell_iswordp_nmw(fword + sp->ts_fidx))
7086 {
7087 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
7088#ifdef FEAT_MBYTE
7089 if (has_mbyte)
7090 sp->ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
7091 else
7092#endif
7093 ++sp->ts_fidx;
7094 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007095#ifdef FEAT_MBYTE
7096 if (has_mbyte)
7097 {
7098 int i = 0;
7099
7100 /* Case-folding may change the number of bytes:
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007101 * Count nr of chars in fword[ts_fidx] and
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007102 * advance that many chars in su->su_badptr. */
7103 for (p = fword; p < fword + sp->ts_fidx;
7104 mb_ptr_adv(p))
7105 ++i;
7106 for (p = su->su_badptr; i > 0; mb_ptr_adv(p))
7107 --i;
7108 }
7109 else
7110#endif
7111 p = su->su_badptr + sp->ts_fidx;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007112 su->su_badflags = captype(p, su->su_badptr
7113 + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007114
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007115 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007116 sp->ts_arridx = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007117 }
7118 }
7119 break;
7120
7121 case STATE_SPLITUNDO:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007122 /* Undo the changes done for word split. */
7123 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007124 splitoff = sp->ts_save_splitoff;
7125 prewordlen = sp->ts_save_prewordlen;
7126
7127 /* Continue looking for NUL bytes. */
7128 sp->ts_state = STATE_START;
7129 break;
7130
7131 case STATE_ENDNUL:
7132 /* Past the NUL bytes in the node. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007133 if (fword[sp->ts_fidx] == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007134 {
7135 /* The badword ends, can't use the bytes in this node. */
7136 sp->ts_state = STATE_DEL;
7137 break;
7138 }
7139 sp->ts_state = STATE_PLAIN;
7140 /*FALLTHROUGH*/
7141
7142 case STATE_PLAIN:
7143 /*
7144 * Go over all possible bytes at this node, add each to
7145 * tword[] and use child node. "ts_curi" is the index.
7146 */
7147 arridx = sp->ts_arridx;
7148 if (sp->ts_curi > byts[arridx])
7149 {
7150 /* Done all bytes at this node, do next state. When still
7151 * at already changed bytes skip the other tricks. */
7152 if (sp->ts_fidx >= sp->ts_fidxtry)
7153 sp->ts_state = STATE_DEL;
7154 else
7155 sp->ts_state = STATE_FINAL;
7156 }
7157 else
7158 {
7159 arridx += sp->ts_curi++;
7160 c = byts[arridx];
7161
7162 /* Normal byte, go one level deeper. If it's not equal to
7163 * the byte in the bad word adjust the score. But don't
7164 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00007165 if (c == fword[sp->ts_fidx]
7166#ifdef FEAT_MBYTE
7167 || (sp->ts_tcharlen > 0
7168 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007169#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00007170 )
7171 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007172 else
7173 newscore = SCORE_SUBST;
7174 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
7175 && try_deeper(su, stack, depth, newscore))
7176 {
7177 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007178 sp = &stack[depth];
7179 ++sp->ts_fidx;
7180 tword[sp->ts_twordlen++] = c;
7181 sp->ts_arridx = idxs[arridx];
7182#ifdef FEAT_MBYTE
7183 if (newscore == SCORE_SUBST)
7184 sp->ts_isdiff = DIFF_YES;
7185 if (has_mbyte)
7186 {
7187 /* Multi-byte characters are a bit complicated to
7188 * handle: They differ when any of the bytes
7189 * differ and then their length may also differ. */
7190 if (sp->ts_tcharlen == 0)
7191 {
7192 /* First byte. */
7193 sp->ts_tcharidx = 0;
7194 sp->ts_tcharlen = MB_BYTE2LEN(c);
7195 sp->ts_fcharstart = sp->ts_fidx - 1;
7196 sp->ts_isdiff = (newscore != 0)
7197 ? DIFF_YES : DIFF_NONE;
7198 }
7199 else if (sp->ts_isdiff == DIFF_INSERT)
7200 /* When inserting trail bytes don't advance in
7201 * the bad word. */
7202 --sp->ts_fidx;
7203 if (++sp->ts_tcharidx == sp->ts_tcharlen)
7204 {
7205 /* Last byte of character. */
7206 if (sp->ts_isdiff == DIFF_YES)
7207 {
7208 /* Correct ts_fidx for the byte length of
7209 * the character (we didn't check that
7210 * before). */
7211 sp->ts_fidx = sp->ts_fcharstart
7212 + MB_BYTE2LEN(
7213 fword[sp->ts_fcharstart]);
7214
7215 /* For a similar character adjust score
7216 * from SCORE_SUBST to SCORE_SIMILAR. */
7217 if (lp->lp_slang->sl_has_map
7218 && similar_chars(lp->lp_slang,
7219 mb_ptr2char(tword
7220 + sp->ts_twordlen
7221 - sp->ts_tcharlen),
7222 mb_ptr2char(fword
7223 + sp->ts_fcharstart)))
7224 sp->ts_score -=
7225 SCORE_SUBST - SCORE_SIMILAR;
7226 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007227 else if (sp->ts_isdiff == DIFF_INSERT
7228 && sp->ts_twordlen > sp->ts_tcharlen)
7229 {
7230 /* If the previous character was the same,
7231 * thus doubling a character, give a bonus
7232 * to the score. */
7233 p = tword + sp->ts_twordlen
7234 - sp->ts_tcharlen;
7235 c = mb_ptr2char(p);
7236 mb_ptr_back(tword, p);
7237 if (c == mb_ptr2char(p))
7238 sp->ts_score -= SCORE_INS
7239 - SCORE_INSDUP;
7240 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007241
7242 /* Starting a new char, reset the length. */
7243 sp->ts_tcharlen = 0;
7244 }
7245 }
7246 else
7247#endif
7248 {
7249 /* If we found a similar char adjust the score.
7250 * We do this after calling try_deeper() because
7251 * it's slow. */
7252 if (newscore != 0
7253 && lp->lp_slang->sl_has_map
7254 && similar_chars(lp->lp_slang,
7255 c, fword[sp->ts_fidx - 1]))
7256 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
7257 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007258 }
7259 }
7260 break;
7261
7262 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00007263#ifdef FEAT_MBYTE
7264 /* When past the first byte of a multi-byte char don't try
7265 * delete/insert/swap a character. */
7266 if (has_mbyte && sp->ts_tcharlen > 0)
7267 {
7268 sp->ts_state = STATE_FINAL;
7269 break;
7270 }
7271#endif
7272 /*
7273 * Try skipping one character in the bad word (delete it).
7274 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007275 sp->ts_state = STATE_INS;
7276 sp->ts_curi = 1;
7277 if (fword[sp->ts_fidx] != NUL
7278 && try_deeper(su, stack, depth, SCORE_DEL))
7279 {
7280 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00007281
7282 /* Advance over the character in fword[]. Give a bonus to
7283 * the score if the same character is following "nn" ->
7284 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00007285#ifdef FEAT_MBYTE
7286 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00007287 {
7288 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00007289 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaarea408852005-06-25 22:49:46 +00007290 if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
7291 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
7292 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007293 else
7294#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00007295 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007296 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00007297 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
7298 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
7299 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007300 break;
7301 }
7302 /*FALLTHROUGH*/
7303
7304 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00007305 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007306 * node. */
7307 n = sp->ts_arridx;
7308 if (sp->ts_curi > byts[n])
7309 {
7310 /* Done all bytes at this node, do next state. */
7311 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007312 }
7313 else
7314 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007315 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007316 n += sp->ts_curi++;
7317 c = byts[n];
7318 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
7319 {
7320 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007321 sp = &stack[depth];
7322 tword[sp->ts_twordlen++] = c;
7323 sp->ts_arridx = idxs[n];
7324#ifdef FEAT_MBYTE
7325 if (has_mbyte)
7326 {
7327 fl = MB_BYTE2LEN(c);
7328 if (fl > 1)
7329 {
7330 /* There are following bytes for the same
7331 * character. We must find all bytes before
7332 * trying delete/insert/swap/etc. */
7333 sp->ts_tcharlen = fl;
7334 sp->ts_tcharidx = 1;
7335 sp->ts_isdiff = DIFF_INSERT;
7336 }
7337 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007338 else
7339 fl = 1;
7340 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00007341#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00007342 {
7343 /* If the previous character was the same, thus
7344 * doubling a character, give a bonus to the
7345 * score. */
7346 if (sp->ts_twordlen >= 2
7347 && tword[sp->ts_twordlen - 2] == c)
7348 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
7349 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007350 }
7351 }
7352 break;
7353
7354 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00007355 /*
7356 * Swap two bytes in the bad word: "12" -> "21".
7357 * We change "fword" here, it's changed back afterwards.
7358 */
7359 p = fword + sp->ts_fidx;
7360 c = *p;
7361 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007362 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007363 /* End of word, can't swap or replace. */
7364 sp->ts_state = STATE_FINAL;
7365 break;
7366 }
7367#ifdef FEAT_MBYTE
7368 if (has_mbyte)
7369 {
7370 n = mb_ptr2len_check(p);
7371 c = mb_ptr2char(p);
7372 c2 = mb_ptr2char(p + n);
7373 }
7374 else
7375#endif
7376 c2 = p[1];
7377 if (c == c2)
7378 {
7379 /* Characters are identical, swap won't do anything. */
7380 sp->ts_state = STATE_SWAP3;
7381 break;
7382 }
7383 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
7384 {
7385 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007386 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007387#ifdef FEAT_MBYTE
7388 if (has_mbyte)
7389 {
7390 fl = mb_char2len(c2);
7391 mch_memmove(p, p + n, fl);
7392 mb_char2bytes(c, p + fl);
7393 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
7394 }
7395 else
7396#endif
7397 {
7398 p[0] = c2;
7399 p[1] = c;
7400 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
7401 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007402 }
7403 else
7404 /* If this swap doesn't work then SWAP3 won't either. */
7405 sp->ts_state = STATE_REP_INI;
7406 break;
7407
Bram Moolenaarea424162005-06-16 21:51:00 +00007408 case STATE_UNSWAP:
7409 /* Undo the STATE_SWAP swap: "21" -> "12". */
7410 p = fword + sp->ts_fidx;
7411#ifdef FEAT_MBYTE
7412 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007413 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007414 n = MB_BYTE2LEN(*p);
7415 c = mb_ptr2char(p + n);
7416 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
7417 mb_char2bytes(c, p);
7418 }
7419 else
7420#endif
7421 {
7422 c = *p;
7423 *p = p[1];
7424 p[1] = c;
7425 }
7426 /*FALLTHROUGH*/
7427
7428 case STATE_SWAP3:
7429 /* Swap two bytes, skipping one: "123" -> "321". We change
7430 * "fword" here, it's changed back afterwards. */
7431 p = fword + sp->ts_fidx;
7432#ifdef FEAT_MBYTE
7433 if (has_mbyte)
7434 {
7435 n = mb_ptr2len_check(p);
7436 c = mb_ptr2char(p);
7437 fl = mb_ptr2len_check(p + n);
7438 c2 = mb_ptr2char(p + n);
7439 c3 = mb_ptr2char(p + n + fl);
7440 }
7441 else
7442#endif
7443 {
7444 c = *p;
7445 c2 = p[1];
7446 c3 = p[2];
7447 }
7448
7449 /* When characters are identical: "121" then SWAP3 result is
7450 * identical, ROT3L result is same as SWAP: "211", ROT3L
7451 * result is same as SWAP on next char: "112". Thus skip all
7452 * swapping. Also skip when c3 is NUL. */
7453 if (c == c3 || c3 == NUL)
7454 {
7455 sp->ts_state = STATE_REP_INI;
7456 break;
7457 }
7458 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7459 {
7460 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007461 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007462#ifdef FEAT_MBYTE
7463 if (has_mbyte)
7464 {
7465 tl = mb_char2len(c3);
7466 mch_memmove(p, p + n + fl, tl);
7467 mb_char2bytes(c2, p + tl);
7468 mb_char2bytes(c, p + fl + tl);
7469 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
7470 }
7471 else
7472#endif
7473 {
7474 p[0] = p[2];
7475 p[2] = c;
7476 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7477 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007478 }
7479 else
7480 sp->ts_state = STATE_REP_INI;
7481 break;
7482
Bram Moolenaarea424162005-06-16 21:51:00 +00007483 case STATE_UNSWAP3:
7484 /* Undo STATE_SWAP3: "321" -> "123" */
7485 p = fword + sp->ts_fidx;
7486#ifdef FEAT_MBYTE
7487 if (has_mbyte)
7488 {
7489 n = MB_BYTE2LEN(*p);
7490 c2 = mb_ptr2char(p + n);
7491 fl = MB_BYTE2LEN(p[n]);
7492 c = mb_ptr2char(p + n + fl);
7493 tl = MB_BYTE2LEN(p[n + fl]);
7494 mch_memmove(p + fl + tl, p, n);
7495 mb_char2bytes(c, p);
7496 mb_char2bytes(c2, p + tl);
7497 }
7498 else
7499#endif
7500 {
7501 c = *p;
7502 *p = p[2];
7503 p[2] = c;
7504 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007505
Bram Moolenaarea424162005-06-16 21:51:00 +00007506 /* Rotate three characters left: "123" -> "231". We change
7507 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007508 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7509 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007510 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007511 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007512 p = fword + sp->ts_fidx;
7513#ifdef FEAT_MBYTE
7514 if (has_mbyte)
7515 {
7516 n = mb_ptr2len_check(p);
7517 c = mb_ptr2char(p);
7518 fl = mb_ptr2len_check(p + n);
7519 fl += mb_ptr2len_check(p + n + fl);
7520 mch_memmove(p, p + n, fl);
7521 mb_char2bytes(c, p + fl);
7522 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
7523 }
7524 else
7525#endif
7526 {
7527 c = *p;
7528 *p = p[1];
7529 p[1] = p[2];
7530 p[2] = c;
7531 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7532 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007533 }
7534 else
7535 sp->ts_state = STATE_REP_INI;
7536 break;
7537
Bram Moolenaarea424162005-06-16 21:51:00 +00007538 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007539 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00007540 p = fword + sp->ts_fidx;
7541#ifdef FEAT_MBYTE
7542 if (has_mbyte)
7543 {
7544 n = MB_BYTE2LEN(*p);
7545 n += MB_BYTE2LEN(p[n]);
7546 c = mb_ptr2char(p + n);
7547 tl = MB_BYTE2LEN(p[n]);
7548 mch_memmove(p + tl, p, n);
7549 mb_char2bytes(c, p);
7550 }
7551 else
7552#endif
7553 {
7554 c = p[2];
7555 p[2] = p[1];
7556 p[1] = *p;
7557 *p = c;
7558 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007559
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007560 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00007561 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007562 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7563 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007564 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007565 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007566 p = fword + sp->ts_fidx;
7567#ifdef FEAT_MBYTE
7568 if (has_mbyte)
7569 {
7570 n = mb_ptr2len_check(p);
7571 n += mb_ptr2len_check(p + n);
7572 c = mb_ptr2char(p + n);
7573 tl = mb_ptr2len_check(p + n);
7574 mch_memmove(p + tl, p, n);
7575 mb_char2bytes(c, p);
7576 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
7577 }
7578 else
7579#endif
7580 {
7581 c = p[2];
7582 p[2] = p[1];
7583 p[1] = *p;
7584 *p = c;
7585 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7586 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007587 }
7588 else
7589 sp->ts_state = STATE_REP_INI;
7590 break;
7591
Bram Moolenaarea424162005-06-16 21:51:00 +00007592 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007593 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00007594 p = fword + sp->ts_fidx;
7595#ifdef FEAT_MBYTE
7596 if (has_mbyte)
7597 {
7598 c = mb_ptr2char(p);
7599 tl = MB_BYTE2LEN(*p);
7600 n = MB_BYTE2LEN(p[tl]);
7601 n += MB_BYTE2LEN(p[tl + n]);
7602 mch_memmove(p, p + tl, n);
7603 mb_char2bytes(c, p + n);
7604 }
7605 else
7606#endif
7607 {
7608 c = *p;
7609 *p = p[1];
7610 p[1] = p[2];
7611 p[2] = c;
7612 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007613 /*FALLTHROUGH*/
7614
7615 case STATE_REP_INI:
7616 /* Check if matching with REP items from the .aff file would
7617 * work. Quickly skip if there are no REP items or the score
7618 * is going to be too high anyway. */
7619 gap = &lp->lp_slang->sl_rep;
7620 if (gap->ga_len == 0
7621 || sp->ts_score + SCORE_REP >= su->su_maxscore)
7622 {
7623 sp->ts_state = STATE_FINAL;
7624 break;
7625 }
7626
7627 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00007628 * may match. If the index is -1 there is none. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007629 sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]];
7630 if (sp->ts_curi < 0)
7631 {
7632 sp->ts_state = STATE_FINAL;
7633 break;
7634 }
7635
7636 sp->ts_state = STATE_REP;
7637 /*FALLTHROUGH*/
7638
7639 case STATE_REP:
7640 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00007641 * match replace the characters and check if the resulting
7642 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007643 p = fword + sp->ts_fidx;
7644
7645 gap = &lp->lp_slang->sl_rep;
7646 while (sp->ts_curi < gap->ga_len)
7647 {
7648 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
7649 if (*ftp->ft_from != *p)
7650 {
7651 /* past possible matching entries */
7652 sp->ts_curi = gap->ga_len;
7653 break;
7654 }
7655 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
7656 && try_deeper(su, stack, depth, SCORE_REP))
7657 {
7658 /* Need to undo this afterwards. */
7659 sp->ts_state = STATE_REP_UNDO;
7660
7661 /* Change the "from" to the "to" string. */
7662 ++depth;
7663 fl = STRLEN(ftp->ft_from);
7664 tl = STRLEN(ftp->ft_to);
7665 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007666 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007667 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007668 repextra += tl - fl;
7669 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007670 mch_memmove(p, ftp->ft_to, tl);
7671 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +00007672#ifdef FEAT_MBYTE
7673 stack[depth].ts_tcharlen = 0;
7674#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007675 break;
7676 }
7677 }
7678
7679 if (sp->ts_curi >= gap->ga_len)
7680 /* No (more) matches. */
7681 sp->ts_state = STATE_FINAL;
7682
7683 break;
7684
7685 case STATE_REP_UNDO:
7686 /* Undo a REP replacement and continue with the next one. */
7687 ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data
7688 + sp->ts_curi - 1;
7689 fl = STRLEN(ftp->ft_from);
7690 tl = STRLEN(ftp->ft_to);
7691 p = fword + sp->ts_fidx;
7692 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007693 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007694 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007695 repextra -= tl - fl;
7696 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007697 mch_memmove(p, ftp->ft_from, fl);
7698 sp->ts_state = STATE_REP;
7699 break;
7700
7701 default:
7702 /* Did all possible states at this level, go up one level. */
7703 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007704
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007705 if (depth >= 0 && stack[depth].ts_prefixdepth == PREFIXTREE)
7706 {
7707 /* Continue in or go back to the prefix tree. */
7708 byts = pbyts;
7709 idxs = pidxs;
7710 splitoff = 0;
7711 }
7712
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007713 /* Don't check for CTRL-C too often, it takes time. */
7714 line_breakcheck();
7715 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007716 }
7717 }
7718}
7719
7720/*
7721 * Try going one level deeper in the tree.
7722 */
7723 static int
7724try_deeper(su, stack, depth, score_add)
7725 suginfo_T *su;
7726 trystate_T *stack;
7727 int depth;
7728 int score_add;
7729{
7730 int newscore;
7731
7732 /* Refuse to go deeper if the scrore is getting too big. */
7733 newscore = stack[depth].ts_score + score_add;
7734 if (newscore >= su->su_maxscore)
7735 return FALSE;
7736
Bram Moolenaarea424162005-06-16 21:51:00 +00007737 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007738 stack[depth + 1].ts_state = STATE_START;
7739 stack[depth + 1].ts_score = newscore;
7740 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007741 return TRUE;
7742}
7743
7744/*
7745 * "fword" is a good word with case folded. Find the matching keep-case
7746 * words and put it in "kword".
7747 * Theoretically there could be several keep-case words that result in the
7748 * same case-folded word, but we only find one...
7749 */
7750 static void
7751find_keepcap_word(slang, fword, kword)
7752 slang_T *slang;
7753 char_u *fword;
7754 char_u *kword;
7755{
7756 char_u uword[MAXWLEN]; /* "fword" in upper-case */
7757 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007758 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007759
7760 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007761 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007762 int round[MAXWLEN];
7763 int fwordidx[MAXWLEN];
7764 int uwordidx[MAXWLEN];
7765 int kwordlen[MAXWLEN];
7766
7767 int flen, ulen;
7768 int l;
7769 int len;
7770 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007771 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007772 char_u *p;
7773 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007774 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007775
7776 if (byts == NULL)
7777 {
7778 /* array is empty: "cannot happen" */
7779 *kword = NUL;
7780 return;
7781 }
7782
7783 /* Make an all-cap version of "fword". */
7784 allcap_copy(fword, uword);
7785
7786 /*
7787 * Each character needs to be tried both case-folded and upper-case.
7788 * All this gets very complicated if we keep in mind that changing case
7789 * may change the byte length of a multi-byte character...
7790 */
7791 depth = 0;
7792 arridx[0] = 0;
7793 round[0] = 0;
7794 fwordidx[0] = 0;
7795 uwordidx[0] = 0;
7796 kwordlen[0] = 0;
7797 while (depth >= 0)
7798 {
7799 if (fword[fwordidx[depth]] == NUL)
7800 {
7801 /* We are at the end of "fword". If the tree allows a word to end
7802 * here we have found a match. */
7803 if (byts[arridx[depth] + 1] == 0)
7804 {
7805 kword[kwordlen[depth]] = NUL;
7806 return;
7807 }
7808
7809 /* kword is getting too long, continue one level up */
7810 --depth;
7811 }
7812 else if (++round[depth] > 2)
7813 {
7814 /* tried both fold-case and upper-case character, continue one
7815 * level up */
7816 --depth;
7817 }
7818 else
7819 {
7820 /*
7821 * round[depth] == 1: Try using the folded-case character.
7822 * round[depth] == 2: Try using the upper-case character.
7823 */
7824#ifdef FEAT_MBYTE
7825 if (has_mbyte)
7826 {
7827 flen = mb_ptr2len_check(fword + fwordidx[depth]);
7828 ulen = mb_ptr2len_check(uword + uwordidx[depth]);
7829 }
7830 else
7831#endif
7832 ulen = flen = 1;
7833 if (round[depth] == 1)
7834 {
7835 p = fword + fwordidx[depth];
7836 l = flen;
7837 }
7838 else
7839 {
7840 p = uword + uwordidx[depth];
7841 l = ulen;
7842 }
7843
7844 for (tryidx = arridx[depth]; l > 0; --l)
7845 {
7846 /* Perform a binary search in the list of accepted bytes. */
7847 len = byts[tryidx++];
7848 c = *p++;
7849 lo = tryidx;
7850 hi = tryidx + len - 1;
7851 while (lo < hi)
7852 {
7853 m = (lo + hi) / 2;
7854 if (byts[m] > c)
7855 hi = m - 1;
7856 else if (byts[m] < c)
7857 lo = m + 1;
7858 else
7859 {
7860 lo = hi = m;
7861 break;
7862 }
7863 }
7864
7865 /* Stop if there is no matching byte. */
7866 if (hi < lo || byts[lo] != c)
7867 break;
7868
7869 /* Continue at the child (if there is one). */
7870 tryidx = idxs[lo];
7871 }
7872
7873 if (l == 0)
7874 {
7875 /*
7876 * Found the matching char. Copy it to "kword" and go a
7877 * level deeper.
7878 */
7879 if (round[depth] == 1)
7880 {
7881 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
7882 flen);
7883 kwordlen[depth + 1] = kwordlen[depth] + flen;
7884 }
7885 else
7886 {
7887 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
7888 ulen);
7889 kwordlen[depth + 1] = kwordlen[depth] + ulen;
7890 }
7891 fwordidx[depth + 1] = fwordidx[depth] + flen;
7892 uwordidx[depth + 1] = uwordidx[depth] + ulen;
7893
7894 ++depth;
7895 arridx[depth] = tryidx;
7896 round[depth] = 0;
7897 }
7898 }
7899 }
7900
7901 /* Didn't find it: "cannot happen". */
7902 *kword = NUL;
7903}
7904
7905/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007906 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
7907 * su->su_sga.
7908 */
7909 static void
7910score_comp_sal(su)
7911 suginfo_T *su;
7912{
7913 langp_T *lp;
7914 char_u badsound[MAXWLEN];
7915 int i;
7916 suggest_T *stp;
7917 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007918 int score;
7919
7920 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
7921 return;
7922
7923 /* Use the sound-folding of the first language that supports it. */
7924 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7925 lp->lp_slang != NULL; ++lp)
7926 if (lp->lp_slang->sl_sal.ga_len > 0)
7927 {
7928 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007929 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007930
7931 for (i = 0; i < su->su_ga.ga_len; ++i)
7932 {
7933 stp = &SUG(su->su_ga, i);
7934
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007935 /* Case-fold the suggested word, sound-fold it and compute the
7936 * sound-a-like score. */
7937 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007938 if (score < SCORE_MAXMAX)
7939 {
7940 /* Add the suggestion. */
7941 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
7942 sstp->st_word = vim_strsave(stp->st_word);
7943 if (sstp->st_word != NULL)
7944 {
7945 sstp->st_score = score;
7946 sstp->st_altscore = 0;
7947 sstp->st_orglen = stp->st_orglen;
7948 ++su->su_sga.ga_len;
7949 }
7950 }
7951 }
7952 break;
7953 }
7954}
7955
7956/*
7957 * Combine the list of suggestions in su->su_ga and su->su_sga.
7958 * They are intwined.
7959 */
7960 static void
7961score_combine(su)
7962 suginfo_T *su;
7963{
7964 int i;
7965 int j;
7966 garray_T ga;
7967 garray_T *gap;
7968 langp_T *lp;
7969 suggest_T *stp;
7970 char_u *p;
7971 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007972 int round;
7973
7974 /* Add the alternate score to su_ga. */
7975 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7976 lp->lp_slang != NULL; ++lp)
7977 {
7978 if (lp->lp_slang->sl_sal.ga_len > 0)
7979 {
7980 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007981 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007982
7983 for (i = 0; i < su->su_ga.ga_len; ++i)
7984 {
7985 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007986 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
7987 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007988 if (stp->st_altscore == SCORE_MAXMAX)
7989 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
7990 else
7991 stp->st_score = (stp->st_score * 3
7992 + stp->st_altscore) / 4;
7993 stp->st_salscore = FALSE;
7994 }
7995 break;
7996 }
7997 }
7998
7999 /* Add the alternate score to su_sga. */
8000 for (i = 0; i < su->su_sga.ga_len; ++i)
8001 {
8002 stp = &SUG(su->su_sga, i);
8003 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
8004 if (stp->st_score == SCORE_MAXMAX)
8005 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
8006 else
8007 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
8008 stp->st_salscore = TRUE;
8009 }
8010
8011 /* Sort the suggestions and truncate at "maxcount" for both lists. */
8012 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8013 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
8014
8015 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
8016 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
8017 return;
8018
8019 stp = &SUG(ga, 0);
8020 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
8021 {
8022 /* round 1: get a suggestion from su_ga
8023 * round 2: get a suggestion from su_sga */
8024 for (round = 1; round <= 2; ++round)
8025 {
8026 gap = round == 1 ? &su->su_ga : &su->su_sga;
8027 if (i < gap->ga_len)
8028 {
8029 /* Don't add a word if it's already there. */
8030 p = SUG(*gap, i).st_word;
8031 for (j = 0; j < ga.ga_len; ++j)
8032 if (STRCMP(stp[j].st_word, p) == 0)
8033 break;
8034 if (j == ga.ga_len)
8035 stp[ga.ga_len++] = SUG(*gap, i);
8036 else
8037 vim_free(p);
8038 }
8039 }
8040 }
8041
8042 ga_clear(&su->su_ga);
8043 ga_clear(&su->su_sga);
8044
8045 /* Truncate the list to the number of suggestions that will be displayed. */
8046 if (ga.ga_len > su->su_maxcount)
8047 {
8048 for (i = su->su_maxcount; i < ga.ga_len; ++i)
8049 vim_free(stp[i].st_word);
8050 ga.ga_len = su->su_maxcount;
8051 }
8052
8053 su->su_ga = ga;
8054}
8055
8056/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008057 * For the goodword in "stp" compute the soundalike score compared to the
8058 * badword.
8059 */
8060 static int
8061stp_sal_score(stp, su, slang, badsound)
8062 suggest_T *stp;
8063 suginfo_T *su;
8064 slang_T *slang;
8065 char_u *badsound; /* sound-folded badword */
8066{
8067 char_u *p;
8068 char_u badsound2[MAXWLEN];
8069 char_u fword[MAXWLEN];
8070 char_u goodsound[MAXWLEN];
8071
8072 if (stp->st_orglen <= su->su_badlen)
8073 p = badsound;
8074 else
8075 {
8076 /* soundfold the bad word with more characters following */
8077 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
8078
8079 /* When joining two words the sound often changes a lot. E.g., "t he"
8080 * sounds like "t h" while "the" sounds like "@". Avoid that by
8081 * removing the space. Don't do it when the good word also contains a
8082 * space. */
8083 if (vim_iswhite(su->su_badptr[su->su_badlen])
8084 && *skiptowhite(stp->st_word) == NUL)
8085 for (p = fword; *(p = skiptowhite(p)) != NUL; )
8086 mch_memmove(p, p + 1, STRLEN(p));
8087
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008088 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008089 p = badsound2;
8090 }
8091
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008092 /* Sound-fold the word and compute the score for the difference. */
8093 spell_soundfold(slang, stp->st_word, FALSE, goodsound);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008094
8095 return soundalike_score(goodsound, p);
8096}
8097
8098/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008099 * Find suggestions by comparing the word in a sound-a-like form.
8100 */
8101 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00008102suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008103 suginfo_T *su;
8104{
8105 char_u salword[MAXWLEN];
8106 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008107 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008108 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008109 int curi[MAXWLEN];
8110 langp_T *lp;
8111 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008112 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008113 int depth;
8114 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008115 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008116 int round;
8117 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008118 int sound_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008119
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008120 /* Do this for all languages that support sound folding. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008121 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8122 lp->lp_slang != NULL; ++lp)
8123 {
8124 if (lp->lp_slang->sl_sal.ga_len > 0)
8125 {
8126 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008127 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008128
8129 /*
8130 * Go through the whole tree, soundfold each word and compare.
8131 * round 1: use the case-folded tree.
8132 * round 2: use the keep-case tree.
8133 */
8134 for (round = 1; round <= 2; ++round)
8135 {
8136 if (round == 1)
8137 {
8138 byts = lp->lp_slang->sl_fbyts;
8139 idxs = lp->lp_slang->sl_fidxs;
8140 }
8141 else
8142 {
8143 byts = lp->lp_slang->sl_kbyts;
8144 idxs = lp->lp_slang->sl_kidxs;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008145 if (byts == NULL) /* no keep-case words */
8146 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008147 }
8148
8149 depth = 0;
8150 arridx[0] = 0;
8151 curi[0] = 1;
8152 while (depth >= 0 && !got_int)
8153 {
8154 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008155 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008156 /* Done all bytes at this node, go up one level. */
8157 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008158 line_breakcheck();
8159 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008160 else
8161 {
8162 /* Do one more byte at this node. */
8163 n = arridx[depth] + curi[depth];
8164 ++curi[depth];
8165 c = byts[n];
8166 if (c == 0)
8167 {
8168 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008169 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008170 if (round == 2 || (flags & WF_KEEPCAP) == 0)
8171 {
8172 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008173 /* Sound-fold. Only in keep-case tree need to
8174 * case-fold the word. */
8175 spell_soundfold(lp->lp_slang, tword,
8176 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008177
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008178 /* Compute the edit distance between the
8179 * sound-a-like words. */
8180 sound_score = soundalike_score(salword,
8181 tsalword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008182 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008183 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008184 char_u cword[MAXWLEN];
8185 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008186 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008187
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008188 flags |= su->su_badflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008189 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008190 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008191 /* Need to fix case according to
8192 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008193 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008194 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008195 }
8196 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008197 p = tword;
8198
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008199 if (sps_flags & SPS_DOUBLE)
8200 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008201 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008202 sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008203 else
8204 {
8205 /* Compute the score. */
8206 score = spell_edit_score(
8207 su->su_badword, p);
8208 if (sps_flags & SPS_BEST)
8209 /* give a bonus for the good word
8210 * sounding the same as the bad
8211 * word */
8212 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008213 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008214 RESCORE(score, sound_score),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008215 sound_score, TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008216 else
8217 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008218 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008219 score + sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008220 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008221 }
8222 }
8223
8224 /* Skip over other NUL bytes. */
8225 while (byts[n + 1] == 0)
8226 {
8227 ++n;
8228 ++curi[depth];
8229 }
8230 }
8231 else
8232 {
8233 /* Normal char, go one level deeper. */
8234 tword[depth++] = c;
8235 arridx[depth] = idxs[n];
8236 curi[depth] = 1;
8237 }
8238 }
8239 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008240 }
8241 }
8242 }
8243}
8244
8245/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008246 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008247 */
8248 static void
8249make_case_word(fword, cword, flags)
8250 char_u *fword;
8251 char_u *cword;
8252 int flags;
8253{
8254 if (flags & WF_ALLCAP)
8255 /* Make it all upper-case */
8256 allcap_copy(fword, cword);
8257 else if (flags & WF_ONECAP)
8258 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008259 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008260 else
8261 /* Use goodword as-is. */
8262 STRCPY(cword, fword);
8263}
8264
Bram Moolenaarea424162005-06-16 21:51:00 +00008265/*
8266 * Use map string "map" for languages "lp".
8267 */
8268 static void
8269set_map_str(lp, map)
8270 slang_T *lp;
8271 char_u *map;
8272{
8273 char_u *p;
8274 int headc = 0;
8275 int c;
8276 int i;
8277
8278 if (*map == NUL)
8279 {
8280 lp->sl_has_map = FALSE;
8281 return;
8282 }
8283 lp->sl_has_map = TRUE;
8284
8285 /* Init the array and hash table empty. */
8286 for (i = 0; i < 256; ++i)
8287 lp->sl_map_array[i] = 0;
8288#ifdef FEAT_MBYTE
8289 hash_init(&lp->sl_map_hash);
8290#endif
8291
8292 /*
8293 * The similar characters are stored separated with slashes:
8294 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
8295 * before the same slash. For characters above 255 sl_map_hash is used.
8296 */
8297 for (p = map; *p != NUL; )
8298 {
8299#ifdef FEAT_MBYTE
8300 c = mb_ptr2char_adv(&p);
8301#else
8302 c = *p++;
8303#endif
8304 if (c == '/')
8305 headc = 0;
8306 else
8307 {
8308 if (headc == 0)
8309 headc = c;
8310
8311#ifdef FEAT_MBYTE
8312 /* Characters above 255 don't fit in sl_map_array[], put them in
8313 * the hash table. Each entry is the char, a NUL the headchar and
8314 * a NUL. */
8315 if (c >= 256)
8316 {
8317 int cl = mb_char2len(c);
8318 int headcl = mb_char2len(headc);
8319 char_u *b;
8320 hash_T hash;
8321 hashitem_T *hi;
8322
8323 b = alloc((unsigned)(cl + headcl + 2));
8324 if (b == NULL)
8325 return;
8326 mb_char2bytes(c, b);
8327 b[cl] = NUL;
8328 mb_char2bytes(headc, b + cl + 1);
8329 b[cl + 1 + headcl] = NUL;
8330 hash = hash_hash(b);
8331 hi = hash_lookup(&lp->sl_map_hash, b, hash);
8332 if (HASHITEM_EMPTY(hi))
8333 hash_add_item(&lp->sl_map_hash, hi, b, hash);
8334 else
8335 {
8336 /* This should have been checked when generating the .spl
8337 * file. */
8338 EMSG(_("E999: duplicate char in MAP entry"));
8339 vim_free(b);
8340 }
8341 }
8342 else
8343#endif
8344 lp->sl_map_array[c] = headc;
8345 }
8346 }
8347}
8348
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008349/*
8350 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
8351 * lines in the .aff file.
8352 */
8353 static int
8354similar_chars(slang, c1, c2)
8355 slang_T *slang;
8356 int c1;
8357 int c2;
8358{
Bram Moolenaarea424162005-06-16 21:51:00 +00008359 int m1, m2;
8360#ifdef FEAT_MBYTE
8361 char_u buf[MB_MAXBYTES];
8362 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008363
Bram Moolenaarea424162005-06-16 21:51:00 +00008364 if (c1 >= 256)
8365 {
8366 buf[mb_char2bytes(c1, buf)] = 0;
8367 hi = hash_find(&slang->sl_map_hash, buf);
8368 if (HASHITEM_EMPTY(hi))
8369 m1 = 0;
8370 else
8371 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8372 }
8373 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008374#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00008375 m1 = slang->sl_map_array[c1];
8376 if (m1 == 0)
8377 return FALSE;
8378
8379
8380#ifdef FEAT_MBYTE
8381 if (c2 >= 256)
8382 {
8383 buf[mb_char2bytes(c2, buf)] = 0;
8384 hi = hash_find(&slang->sl_map_hash, buf);
8385 if (HASHITEM_EMPTY(hi))
8386 m2 = 0;
8387 else
8388 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8389 }
8390 else
8391#endif
8392 m2 = slang->sl_map_array[c2];
8393
8394 return m1 == m2;
8395}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008396
8397/*
8398 * Add a suggestion to the list of suggestions.
8399 * Do not add a duplicate suggestion or suggestions with a bad score.
8400 * When "use_score" is not zero it's used, otherwise the score is computed
8401 * with spell_edit_score().
8402 */
8403 static void
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008404add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008405 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008406 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008407 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008408 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008409 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008410 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008411 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008412{
8413 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008414 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008415 char_u *p = NULL;
8416 int c = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008417
8418 /* Check that the word wasn't banned. */
8419 if (was_banned(su, goodword))
8420 return;
8421
Bram Moolenaar0c405862005-06-22 22:26:26 +00008422 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
8423 * Remove the common part from "goodword". */
8424 i = badlen - su->su_badlen;
8425 if (i > 0)
8426 {
8427 /* This assumes there was no case folding or it didn't change the
8428 * length... */
8429 p = goodword + STRLEN(goodword) - i;
8430 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
8431 {
8432 badlen = su->su_badlen;
8433 c = *p;
8434 *p = NUL;
8435 }
8436 else
8437 p = NULL;
8438 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008439 else if (i < 0)
8440 {
8441 /* When replacing part of the word check that we actually change
8442 * something. For "the the" a suggestion can be replacing the first
8443 * "the" with itself, since "the" wasn't banned. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008444 if (badlen == (int)STRLEN(goodword)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008445 && STRNCMP(su->su_badword, goodword, badlen) == 0)
8446 return;
8447 }
8448
Bram Moolenaar0c405862005-06-22 22:26:26 +00008449
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008450 if (score <= su->su_maxscore)
8451 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008452 /* Check if the word is already there. Also check the length that is
8453 * being replaced "thes," -> "these" is a different suggestion from
8454 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008455 stp = &SUG(*gap, 0);
8456 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008457 if (STRCMP(stp[i].st_word, goodword) == 0
8458 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008459 {
8460 /* Found it. Remember the lowest score. */
8461 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008462 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008463 stp[i].st_score = score;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008464 stp[i].st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008465 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008466 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008467 break;
8468 }
8469
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008470 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008471 {
8472 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008473 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008474 stp->st_word = vim_strsave(goodword);
8475 if (stp->st_word != NULL)
8476 {
8477 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008478 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008479 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008480 stp->st_orglen = badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008481 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008482
8483 /* If we have too many suggestions now, sort the list and keep
8484 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008485 if (gap->ga_len > SUG_MAX_COUNT(su))
8486 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
8487 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008488 }
8489 }
8490 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008491
8492 if (p != NULL)
8493 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008494}
8495
8496/*
8497 * Add a word to be banned.
8498 */
8499 static void
8500add_banned(su, word)
8501 suginfo_T *su;
8502 char_u *word;
8503{
8504 char_u *s = vim_strsave(word);
8505 hash_T hash;
8506 hashitem_T *hi;
8507
8508 if (s != NULL)
8509 {
8510 hash = hash_hash(s);
8511 hi = hash_lookup(&su->su_banned, s, hash);
8512 if (HASHITEM_EMPTY(hi))
8513 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008514 else
8515 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008516 }
8517}
8518
8519/*
8520 * Return TRUE if a word appears in the list of banned words.
8521 */
8522 static int
8523was_banned(su, word)
8524 suginfo_T *su;
8525 char_u *word;
8526{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008527 hashitem_T *hi = hash_find(&su->su_banned, word);
8528
8529 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008530}
8531
8532/*
8533 * Free the banned words in "su".
8534 */
8535 static void
8536free_banned(su)
8537 suginfo_T *su;
8538{
8539 int todo;
8540 hashitem_T *hi;
8541
8542 todo = su->su_banned.ht_used;
8543 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
8544 {
8545 if (!HASHITEM_EMPTY(hi))
8546 {
8547 vim_free(hi->hi_key);
8548 --todo;
8549 }
8550 }
8551 hash_clear(&su->su_banned);
8552}
8553
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008554/*
8555 * Recompute the score if sound-folding is possible. This is slow,
8556 * thus only done for the final results.
8557 */
8558 static void
8559rescore_suggestions(su)
8560 suginfo_T *su;
8561{
8562 langp_T *lp;
8563 suggest_T *stp;
8564 char_u sal_badword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008565 int i;
8566
8567 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8568 lp->lp_slang != NULL; ++lp)
8569 {
8570 if (lp->lp_slang->sl_sal.ga_len > 0)
8571 {
8572 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008573 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008574
8575 for (i = 0; i < su->su_ga.ga_len; ++i)
8576 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008577 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008578 if (!stp->st_had_bonus)
8579 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008580 stp->st_altscore = stp_sal_score(stp, su,
8581 lp->lp_slang, sal_badword);
8582 if (stp->st_altscore == SCORE_MAXMAX)
8583 stp->st_altscore = SCORE_BIG;
8584 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008585 }
8586 }
8587 break;
8588 }
8589 }
8590}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008591
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008592static int
8593#ifdef __BORLANDC__
8594_RTLENTRYF
8595#endif
8596sug_compare __ARGS((const void *s1, const void *s2));
8597
8598/*
8599 * Function given to qsort() to sort the suggestions on st_score.
8600 */
8601 static int
8602#ifdef __BORLANDC__
8603_RTLENTRYF
8604#endif
8605sug_compare(s1, s2)
8606 const void *s1;
8607 const void *s2;
8608{
8609 suggest_T *p1 = (suggest_T *)s1;
8610 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008611 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008612
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008613 if (n == 0)
8614 return p1->st_altscore - p2->st_altscore;
8615 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008616}
8617
8618/*
8619 * Cleanup the suggestions:
8620 * - Sort on score.
8621 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008622 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008623 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008624 static int
8625cleanup_suggestions(gap, maxscore, keep)
8626 garray_T *gap;
8627 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008628 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008629{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008630 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008631 int i;
8632
8633 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008634 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008635
8636 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008637 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008638 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008639 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008640 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008641 gap->ga_len = keep;
8642 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008643 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008644 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008645}
8646
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008647#if defined(FEAT_EVAL) || defined(PROTO)
8648/*
8649 * Soundfold a string, for soundfold().
8650 * Result is in allocated memory, NULL for an error.
8651 */
8652 char_u *
8653eval_soundfold(word)
8654 char_u *word;
8655{
8656 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008657 char_u sound[MAXWLEN];
8658
8659 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
8660 /* Use the sound-folding of the first language that supports it. */
8661 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8662 lp->lp_slang != NULL; ++lp)
8663 if (lp->lp_slang->sl_sal.ga_len > 0)
8664 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008665 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008666 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008667 return vim_strsave(sound);
8668 }
8669
8670 /* No language with sound folding, return word as-is. */
8671 return vim_strsave(word);
8672}
8673#endif
8674
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008675/*
8676 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
8677 */
8678 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008679spell_soundfold(slang, inword, folded, res)
8680 slang_T *slang;
8681 char_u *inword;
8682 int folded; /* "inword" is already case-folded */
8683 char_u *res;
8684{
8685 char_u fword[MAXWLEN];
8686 char_u *word;
8687
8688 if (slang->sl_sofo)
8689 /* SOFOFROM and SOFOTO used */
8690 spell_soundfold_sofo(slang, inword, res);
8691 else
8692 {
8693 /* SAL items used. Requires the word to be case-folded. */
8694 if (folded)
8695 word = inword;
8696 else
8697 {
8698 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
8699 word = fword;
8700 }
8701
8702#ifdef FEAT_MBYTE
8703 if (has_mbyte)
8704 spell_soundfold_wsal(slang, word, res);
8705 else
8706#endif
8707 spell_soundfold_sal(slang, word, res);
8708 }
8709}
8710
8711/*
8712 * Perform sound folding of "inword" into "res" according to SOFOFROM and
8713 * SOFOTO lines.
8714 */
8715 static void
8716spell_soundfold_sofo(slang, inword, res)
8717 slang_T *slang;
8718 char_u *inword;
8719 char_u *res;
8720{
8721 char_u *s;
8722 int ri = 0;
8723 int c;
8724
8725#ifdef FEAT_MBYTE
8726 if (has_mbyte)
8727 {
8728 int prevc = 0;
8729 int *ip;
8730
8731 /* The sl_sal_first[] table contains the translation for chars up to
8732 * 255, sl_sal the rest. */
8733 for (s = inword; *s != NUL; )
8734 {
8735 c = mb_ptr2char_adv(&s);
8736 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
8737 c = ' ';
8738 else if (c < 256)
8739 c = slang->sl_sal_first[c];
8740 else
8741 {
8742 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
8743 if (ip == NULL) /* empty list, can't match */
8744 c = NUL;
8745 else
8746 for (;;) /* find "c" in the list */
8747 {
8748 if (*ip == 0) /* not found */
8749 {
8750 c = NUL;
8751 break;
8752 }
8753 if (*ip == c) /* match! */
8754 {
8755 c = ip[1];
8756 break;
8757 }
8758 ip += 2;
8759 }
8760 }
8761
8762 if (c != NUL && c != prevc)
8763 {
8764 ri += mb_char2bytes(c, res + ri);
8765 if (ri + MB_MAXBYTES > MAXWLEN)
8766 break;
8767 prevc = c;
8768 }
8769 }
8770 }
8771 else
8772#endif
8773 {
8774 /* The sl_sal_first[] table contains the translation. */
8775 for (s = inword; (c = *s) != NUL; ++s)
8776 {
8777 if (vim_iswhite(c))
8778 c = ' ';
8779 else
8780 c = slang->sl_sal_first[c];
8781 if (c != NUL && (ri == 0 || res[ri - 1] != c))
8782 res[ri++] = c;
8783 }
8784 }
8785
8786 res[ri] = NUL;
8787}
8788
8789 static void
8790spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008791 slang_T *slang;
8792 char_u *inword;
8793 char_u *res;
8794{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008795 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008796 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008797 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008798 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008799 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008800 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008801 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008802 int n, k = 0;
8803 int z0;
8804 int k0;
8805 int n0;
8806 int c;
8807 int pri;
8808 int p0 = -333;
8809 int c0;
8810
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008811 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008812 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008813 if (slang->sl_rem_accents)
8814 {
8815 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008816 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008817 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008818 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008819 {
8820 *t++ = ' ';
8821 s = skipwhite(s);
8822 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008823 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008824 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008825 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008826 *t++ = *s;
8827 ++s;
8828 }
8829 }
8830 *t = NUL;
8831 }
8832 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008833 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008834
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008835 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008836
8837 /*
8838 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008839 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008840 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008841 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008842 while ((c = word[i]) != NUL)
8843 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008844 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008845 n = slang->sl_sal_first[c];
8846 z0 = 0;
8847
8848 if (n >= 0)
8849 {
8850 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008851 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008852 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008853 /* Quickly skip entries that don't match the word. Most
8854 * entries are less then three chars, optimize for that. */
8855 k = smp[n].sm_leadlen;
8856 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008857 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008858 if (word[i + 1] != s[1])
8859 continue;
8860 if (k > 2)
8861 {
8862 for (j = 2; j < k; ++j)
8863 if (word[i + j] != s[j])
8864 break;
8865 if (j < k)
8866 continue;
8867 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008868 }
8869
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008870 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008871 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008872 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008873 while (*pf != NUL && *pf != word[i + k])
8874 ++pf;
8875 if (*pf == NUL)
8876 continue;
8877 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008878 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008879 s = smp[n].sm_rules;
8880 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008881
8882 p0 = *s;
8883 k0 = k;
8884 while (*s == '-' && k > 1)
8885 {
8886 k--;
8887 s++;
8888 }
8889 if (*s == '<')
8890 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008891 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008892 {
8893 /* determine priority */
8894 pri = *s - '0';
8895 s++;
8896 }
8897 if (*s == '^' && *(s + 1) == '^')
8898 s++;
8899
8900 if (*s == NUL
8901 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008902 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008903 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008904 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008905 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008906 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008907 && spell_iswordp(word + i - 1, curbuf)
8908 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008909 {
8910 /* search for followup rules, if: */
8911 /* followup and k > 1 and NO '-' in searchstring */
8912 c0 = word[i + k - 1];
8913 n0 = slang->sl_sal_first[c0];
8914
8915 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008916 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008917 {
8918 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008919 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008920 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008921 /* Quickly skip entries that don't match the word.
8922 * */
8923 k0 = smp[n0].sm_leadlen;
8924 if (k0 > 1)
8925 {
8926 if (word[i + k] != s[1])
8927 continue;
8928 if (k0 > 2)
8929 {
8930 pf = word + i + k + 1;
8931 for (j = 2; j < k0; ++j)
8932 if (*pf++ != s[j])
8933 break;
8934 if (j < k0)
8935 continue;
8936 }
8937 }
8938 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008939
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008940 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008941 {
8942 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008943 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008944 while (*pf != NUL && *pf != word[i + k0])
8945 ++pf;
8946 if (*pf == NUL)
8947 continue;
8948 ++k0;
8949 }
8950
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008951 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008952 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008953 while (*s == '-')
8954 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008955 /* "k0" gets NOT reduced because
8956 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008957 s++;
8958 }
8959 if (*s == '<')
8960 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008961 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008962 {
8963 p0 = *s - '0';
8964 s++;
8965 }
8966
8967 if (*s == NUL
8968 /* *s == '^' cuts */
8969 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008970 && !spell_iswordp(word + i + k0,
8971 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008972 {
8973 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008974 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008975 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008976
8977 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008978 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008979 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008980 /* rule fits; stop search */
8981 break;
8982 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008983 }
8984
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008985 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008986 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008987 }
8988
8989 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008990 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008991 if (s == NULL)
8992 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008993 pf = smp[n].sm_rules;
8994 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008995 if (p0 == 1 && z == 0)
8996 {
8997 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008998 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
8999 || res[reslen - 1] == *s))
9000 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009001 z0 = 1;
9002 z = 1;
9003 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009004 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009005 {
9006 word[i + k0] = *s;
9007 k0++;
9008 s++;
9009 }
9010 if (k > k0)
9011 mch_memmove(word + i + k0, word + i + k,
9012 STRLEN(word + i + k) + 1);
9013
9014 /* new "actual letter" */
9015 c = word[i];
9016 }
9017 else
9018 {
9019 /* no '<' rule used */
9020 i += k - 1;
9021 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009022 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009023 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009024 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009025 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009026 s++;
9027 }
9028 /* new "actual letter" */
9029 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009030 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009031 {
9032 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009033 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009034 mch_memmove(word, word + i + 1,
9035 STRLEN(word + i + 1) + 1);
9036 i = 0;
9037 z0 = 1;
9038 }
9039 }
9040 break;
9041 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009042 }
9043 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009044 else if (vim_iswhite(c))
9045 {
9046 c = ' ';
9047 k = 1;
9048 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009049
9050 if (z0 == 0)
9051 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009052 if (k && !p0 && reslen < MAXWLEN && c != NUL
9053 && (!slang->sl_collapse || reslen == 0
9054 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009055 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009056 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009057
9058 i++;
9059 z = 0;
9060 k = 0;
9061 }
9062 }
9063
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009064 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009065}
9066
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009067#ifdef FEAT_MBYTE
9068/*
9069 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
9070 * Multi-byte version of spell_soundfold().
9071 */
9072 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009073spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009074 slang_T *slang;
9075 char_u *inword;
9076 char_u *res;
9077{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009078 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009079 int word[MAXWLEN];
9080 int wres[MAXWLEN];
9081 int l;
9082 char_u *s;
9083 int *ws;
9084 char_u *t;
9085 int *pf;
9086 int i, j, z;
9087 int reslen;
9088 int n, k = 0;
9089 int z0;
9090 int k0;
9091 int n0;
9092 int c;
9093 int pri;
9094 int p0 = -333;
9095 int c0;
9096 int did_white = FALSE;
9097
9098 /*
9099 * Convert the multi-byte string to a wide-character string.
9100 * Remove accents, if wanted. We actually remove all non-word characters.
9101 * But keep white space.
9102 */
9103 n = 0;
9104 for (s = inword; *s != NUL; )
9105 {
9106 t = s;
9107 c = mb_ptr2char_adv(&s);
9108 if (slang->sl_rem_accents)
9109 {
9110 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
9111 {
9112 if (did_white)
9113 continue;
9114 c = ' ';
9115 did_white = TRUE;
9116 }
9117 else
9118 {
9119 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009120 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009121 continue;
9122 }
9123 }
9124 word[n++] = c;
9125 }
9126 word[n] = NUL;
9127
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009128 /*
9129 * This comes from Aspell phonet.cpp.
9130 * Converted from C++ to C. Added support for multi-byte chars.
9131 * Changed to keep spaces.
9132 */
9133 i = reslen = z = 0;
9134 while ((c = word[i]) != NUL)
9135 {
9136 /* Start with the first rule that has the character in the word. */
9137 n = slang->sl_sal_first[c & 0xff];
9138 z0 = 0;
9139
9140 if (n >= 0)
9141 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009142 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009143 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
9144 {
9145 /* Quickly skip entries that don't match the word. Most
9146 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009147 if (c != ws[0])
9148 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009149 k = smp[n].sm_leadlen;
9150 if (k > 1)
9151 {
9152 if (word[i + 1] != ws[1])
9153 continue;
9154 if (k > 2)
9155 {
9156 for (j = 2; j < k; ++j)
9157 if (word[i + j] != ws[j])
9158 break;
9159 if (j < k)
9160 continue;
9161 }
9162 }
9163
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009164 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009165 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009166 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009167 while (*pf != NUL && *pf != word[i + k])
9168 ++pf;
9169 if (*pf == NUL)
9170 continue;
9171 ++k;
9172 }
9173 s = smp[n].sm_rules;
9174 pri = 5; /* default priority */
9175
9176 p0 = *s;
9177 k0 = k;
9178 while (*s == '-' && k > 1)
9179 {
9180 k--;
9181 s++;
9182 }
9183 if (*s == '<')
9184 s++;
9185 if (VIM_ISDIGIT(*s))
9186 {
9187 /* determine priority */
9188 pri = *s - '0';
9189 s++;
9190 }
9191 if (*s == '^' && *(s + 1) == '^')
9192 s++;
9193
9194 if (*s == NUL
9195 || (*s == '^'
9196 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009197 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009198 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009199 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009200 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009201 && spell_iswordp_w(word + i - 1, curbuf)
9202 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009203 {
9204 /* search for followup rules, if: */
9205 /* followup and k > 1 and NO '-' in searchstring */
9206 c0 = word[i + k - 1];
9207 n0 = slang->sl_sal_first[c0 & 0xff];
9208
9209 if (slang->sl_followup && k > 1 && n0 >= 0
9210 && p0 != '-' && word[i + k] != NUL)
9211 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009212 /* Test follow-up rule for "word[i + k]"; loop over
9213 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009214 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
9215 == (c0 & 0xff); ++n0)
9216 {
9217 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009218 */
9219 if (c0 != ws[0])
9220 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009221 k0 = smp[n0].sm_leadlen;
9222 if (k0 > 1)
9223 {
9224 if (word[i + k] != ws[1])
9225 continue;
9226 if (k0 > 2)
9227 {
9228 pf = word + i + k + 1;
9229 for (j = 2; j < k0; ++j)
9230 if (*pf++ != ws[j])
9231 break;
9232 if (j < k0)
9233 continue;
9234 }
9235 }
9236 k0 += k - 1;
9237
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009238 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009239 {
9240 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009241 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009242 while (*pf != NUL && *pf != word[i + k0])
9243 ++pf;
9244 if (*pf == NUL)
9245 continue;
9246 ++k0;
9247 }
9248
9249 p0 = 5;
9250 s = smp[n0].sm_rules;
9251 while (*s == '-')
9252 {
9253 /* "k0" gets NOT reduced because
9254 * "if (k0 == k)" */
9255 s++;
9256 }
9257 if (*s == '<')
9258 s++;
9259 if (VIM_ISDIGIT(*s))
9260 {
9261 p0 = *s - '0';
9262 s++;
9263 }
9264
9265 if (*s == NUL
9266 /* *s == '^' cuts */
9267 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009268 && !spell_iswordp_w(word + i + k0,
9269 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009270 {
9271 if (k0 == k)
9272 /* this is just a piece of the string */
9273 continue;
9274
9275 if (p0 < pri)
9276 /* priority too low */
9277 continue;
9278 /* rule fits; stop search */
9279 break;
9280 }
9281 }
9282
9283 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
9284 == (c0 & 0xff))
9285 continue;
9286 }
9287
9288 /* replace string */
9289 ws = smp[n].sm_to_w;
9290 s = smp[n].sm_rules;
9291 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
9292 if (p0 == 1 && z == 0)
9293 {
9294 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009295 if (reslen > 0 && ws != NULL && *ws != NUL
9296 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009297 || wres[reslen - 1] == *ws))
9298 reslen--;
9299 z0 = 1;
9300 z = 1;
9301 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009302 if (ws != NULL)
9303 while (*ws != NUL && word[i + k0] != NUL)
9304 {
9305 word[i + k0] = *ws;
9306 k0++;
9307 ws++;
9308 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009309 if (k > k0)
9310 mch_memmove(word + i + k0, word + i + k,
9311 sizeof(int) * (STRLEN(word + i + k) + 1));
9312
9313 /* new "actual letter" */
9314 c = word[i];
9315 }
9316 else
9317 {
9318 /* no '<' rule used */
9319 i += k - 1;
9320 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009321 if (ws != NULL)
9322 while (*ws != NUL && ws[1] != NUL
9323 && reslen < MAXWLEN)
9324 {
9325 if (reslen == 0 || wres[reslen - 1] != *ws)
9326 wres[reslen++] = *ws;
9327 ws++;
9328 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009329 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009330 if (ws == NULL)
9331 c = NUL;
9332 else
9333 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009334 if (strstr((char *)s, "^^") != NULL)
9335 {
9336 if (c != NUL)
9337 wres[reslen++] = c;
9338 mch_memmove(word, word + i + 1,
9339 sizeof(int) * (STRLEN(word + i + 1) + 1));
9340 i = 0;
9341 z0 = 1;
9342 }
9343 }
9344 break;
9345 }
9346 }
9347 }
9348 else if (vim_iswhite(c))
9349 {
9350 c = ' ';
9351 k = 1;
9352 }
9353
9354 if (z0 == 0)
9355 {
9356 if (k && !p0 && reslen < MAXWLEN && c != NUL
9357 && (!slang->sl_collapse || reslen == 0
9358 || wres[reslen - 1] != c))
9359 /* condense only double letters */
9360 wres[reslen++] = c;
9361
9362 i++;
9363 z = 0;
9364 k = 0;
9365 }
9366 }
9367
9368 /* Convert wide characters in "wres" to a multi-byte string in "res". */
9369 l = 0;
9370 for (n = 0; n < reslen; ++n)
9371 {
9372 l += mb_char2bytes(wres[n], res + l);
9373 if (l + MB_MAXBYTES > MAXWLEN)
9374 break;
9375 }
9376 res[l] = NUL;
9377}
9378#endif
9379
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009380/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009381 * Compute a score for two sound-a-like words.
9382 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
9383 * Instead of a generic loop we write out the code. That keeps it fast by
9384 * avoiding checks that will not be possible.
9385 */
9386 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009387soundalike_score(goodstart, badstart)
9388 char_u *goodstart; /* sound-folded good word */
9389 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009390{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009391 char_u *goodsound = goodstart;
9392 char_u *badsound = badstart;
9393 int goodlen;
9394 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009395 int n;
9396 char_u *pl, *ps;
9397 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009398 int score = 0;
9399
9400 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
9401 * counted so much, vowels halfway the word aren't counted at all. */
9402 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
9403 {
9404 score = SCORE_DEL / 2;
9405 if (*badsound == '*')
9406 ++badsound;
9407 else
9408 ++goodsound;
9409 }
9410
9411 goodlen = STRLEN(goodsound);
9412 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009413
9414 /* Return quickly if the lenghts are too different to be fixed by two
9415 * changes. */
9416 n = goodlen - badlen;
9417 if (n < -2 || n > 2)
9418 return SCORE_MAXMAX;
9419
9420 if (n > 0)
9421 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009422 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009423 ps = badsound;
9424 }
9425 else
9426 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009427 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009428 ps = goodsound;
9429 }
9430
9431 /* Skip over the identical part. */
9432 while (*pl == *ps && *pl != NUL)
9433 {
9434 ++pl;
9435 ++ps;
9436 }
9437
9438 switch (n)
9439 {
9440 case -2:
9441 case 2:
9442 /*
9443 * Must delete two characters from "pl".
9444 */
9445 ++pl; /* first delete */
9446 while (*pl == *ps)
9447 {
9448 ++pl;
9449 ++ps;
9450 }
9451 /* strings must be equal after second delete */
9452 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009453 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009454
9455 /* Failed to compare. */
9456 break;
9457
9458 case -1:
9459 case 1:
9460 /*
9461 * Minimal one delete from "pl" required.
9462 */
9463
9464 /* 1: delete */
9465 pl2 = pl + 1;
9466 ps2 = ps;
9467 while (*pl2 == *ps2)
9468 {
9469 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009470 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009471 ++pl2;
9472 ++ps2;
9473 }
9474
9475 /* 2: delete then swap, then rest must be equal */
9476 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9477 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009478 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009479
9480 /* 3: delete then substitute, then the rest must be equal */
9481 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009482 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009483
9484 /* 4: first swap then delete */
9485 if (pl[0] == ps[1] && pl[1] == ps[0])
9486 {
9487 pl2 = pl + 2; /* swap, skip two chars */
9488 ps2 = ps + 2;
9489 while (*pl2 == *ps2)
9490 {
9491 ++pl2;
9492 ++ps2;
9493 }
9494 /* delete a char and then strings must be equal */
9495 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009496 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009497 }
9498
9499 /* 5: first substitute then delete */
9500 pl2 = pl + 1; /* substitute, skip one char */
9501 ps2 = ps + 1;
9502 while (*pl2 == *ps2)
9503 {
9504 ++pl2;
9505 ++ps2;
9506 }
9507 /* delete a char and then strings must be equal */
9508 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009509 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009510
9511 /* Failed to compare. */
9512 break;
9513
9514 case 0:
9515 /*
9516 * Lenghts are equal, thus changes must result in same length: An
9517 * insert is only possible in combination with a delete.
9518 * 1: check if for identical strings
9519 */
9520 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009521 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009522
9523 /* 2: swap */
9524 if (pl[0] == ps[1] && pl[1] == ps[0])
9525 {
9526 pl2 = pl + 2; /* swap, skip two chars */
9527 ps2 = ps + 2;
9528 while (*pl2 == *ps2)
9529 {
9530 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009531 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009532 ++pl2;
9533 ++ps2;
9534 }
9535 /* 3: swap and swap again */
9536 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9537 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009538 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009539
9540 /* 4: swap and substitute */
9541 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009542 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009543 }
9544
9545 /* 5: substitute */
9546 pl2 = pl + 1;
9547 ps2 = ps + 1;
9548 while (*pl2 == *ps2)
9549 {
9550 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009551 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009552 ++pl2;
9553 ++ps2;
9554 }
9555
9556 /* 6: substitute and swap */
9557 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9558 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009559 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009560
9561 /* 7: substitute and substitute */
9562 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009563 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009564
9565 /* 8: insert then delete */
9566 pl2 = pl;
9567 ps2 = ps + 1;
9568 while (*pl2 == *ps2)
9569 {
9570 ++pl2;
9571 ++ps2;
9572 }
9573 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009574 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009575
9576 /* 9: delete then insert */
9577 pl2 = pl + 1;
9578 ps2 = ps;
9579 while (*pl2 == *ps2)
9580 {
9581 ++pl2;
9582 ++ps2;
9583 }
9584 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009585 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009586
9587 /* Failed to compare. */
9588 break;
9589 }
9590
9591 return SCORE_MAXMAX;
9592}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009593
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009594/*
9595 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009596 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009597 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009598 * The algorithm comes from Aspell editdist.cpp, edit_distance().
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009599 * It has been converted from C++ to C and modified to support multi-byte
9600 * characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009601 */
9602 static int
9603spell_edit_score(badword, goodword)
9604 char_u *badword;
9605 char_u *goodword;
9606{
9607 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009608 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009609 int j, i;
9610 int t;
9611 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009612 int pbc, pgc;
9613#ifdef FEAT_MBYTE
9614 char_u *p;
9615 int wbadword[MAXWLEN];
9616 int wgoodword[MAXWLEN];
9617
9618 if (has_mbyte)
9619 {
9620 /* Get the characters from the multi-byte strings and put them in an
9621 * int array for easy access. */
9622 for (p = badword, badlen = 0; *p != NUL; )
9623 wbadword[badlen++] = mb_ptr2char_adv(&p);
9624 ++badlen;
9625 for (p = goodword, goodlen = 0; *p != NUL; )
9626 wgoodword[goodlen++] = mb_ptr2char_adv(&p);
9627 ++goodlen;
9628 }
9629 else
9630#endif
9631 {
9632 badlen = STRLEN(badword) + 1;
9633 goodlen = STRLEN(goodword) + 1;
9634 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009635
9636 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
9637#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009638 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
9639 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009640 if (cnt == NULL)
9641 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009642
9643 CNT(0, 0) = 0;
9644 for (j = 1; j <= goodlen; ++j)
9645 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
9646
9647 for (i = 1; i <= badlen; ++i)
9648 {
9649 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
9650 for (j = 1; j <= goodlen; ++j)
9651 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009652#ifdef FEAT_MBYTE
9653 if (has_mbyte)
9654 {
9655 bc = wbadword[i - 1];
9656 gc = wgoodword[j - 1];
9657 }
9658 else
9659#endif
9660 {
9661 bc = badword[i - 1];
9662 gc = goodword[j - 1];
9663 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009664 if (bc == gc)
9665 CNT(i, j) = CNT(i - 1, j - 1);
9666 else
9667 {
9668 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009669 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009670 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
9671 else
9672 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
9673
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009674 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009675 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009676#ifdef FEAT_MBYTE
9677 if (has_mbyte)
9678 {
9679 pbc = wbadword[i - 2];
9680 pgc = wgoodword[j - 2];
9681 }
9682 else
9683#endif
9684 {
9685 pbc = badword[i - 2];
9686 pgc = goodword[j - 2];
9687 }
9688 if (bc == pgc && pbc == gc)
9689 {
9690 t = SCORE_SWAP + CNT(i - 2, j - 2);
9691 if (t < CNT(i, j))
9692 CNT(i, j) = t;
9693 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009694 }
9695 t = SCORE_DEL + CNT(i - 1, j);
9696 if (t < CNT(i, j))
9697 CNT(i, j) = t;
9698 t = SCORE_INS + CNT(i, j - 1);
9699 if (t < CNT(i, j))
9700 CNT(i, j) = t;
9701 }
9702 }
9703 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009704
9705 i = CNT(badlen - 1, goodlen - 1);
9706 vim_free(cnt);
9707 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009708}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009709
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009710/*
9711 * ":spelldump"
9712 */
9713/*ARGSUSED*/
9714 void
9715ex_spelldump(eap)
9716 exarg_T *eap;
9717{
9718 buf_T *buf = curbuf;
9719 langp_T *lp;
9720 slang_T *slang;
9721 idx_T arridx[MAXWLEN];
9722 int curi[MAXWLEN];
9723 char_u word[MAXWLEN];
9724 int c;
9725 char_u *byts;
9726 idx_T *idxs;
9727 linenr_T lnum = 0;
9728 int round;
9729 int depth;
9730 int n;
9731 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009732 char_u *region_names = NULL; /* region names being used */
9733 int do_region = TRUE; /* dump region names and numbers */
9734 char_u *p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009735
9736 if (no_spell_checking())
9737 return;
9738
9739 /* Create a new empty buffer by splitting the window. */
9740 do_cmdline_cmd((char_u *)"new");
9741 if (!bufempty() || !buf_valid(buf))
9742 return;
9743
Bram Moolenaar7887d882005-07-01 22:33:52 +00009744 /* Find out if we can support regions: All languages must support the same
9745 * regions or none at all. */
9746 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
9747 {
9748 p = lp->lp_slang->sl_regions;
9749 if (p[0] != 0)
9750 {
9751 if (region_names == NULL) /* first language with regions */
9752 region_names = p;
9753 else if (STRCMP(region_names, p) != 0)
9754 {
9755 do_region = FALSE; /* region names are different */
9756 break;
9757 }
9758 }
9759 }
9760
9761 if (do_region && region_names != NULL)
9762 {
9763 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
9764 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
9765 }
9766 else
9767 do_region = FALSE;
9768
9769 /*
9770 * Loop over all files loaded for the entries in 'spelllang'.
9771 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009772 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
9773 {
9774 slang = lp->lp_slang;
9775
9776 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
9777 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
9778
9779 /* round 1: case-folded tree
9780 * round 2: keep-case tree */
9781 for (round = 1; round <= 2; ++round)
9782 {
9783 if (round == 1)
9784 {
9785 byts = slang->sl_fbyts;
9786 idxs = slang->sl_fidxs;
9787 }
9788 else
9789 {
9790 byts = slang->sl_kbyts;
9791 idxs = slang->sl_kidxs;
9792 }
9793 if (byts == NULL)
9794 continue; /* array is empty */
9795
9796 depth = 0;
9797 arridx[0] = 0;
9798 curi[0] = 1;
9799 while (depth >= 0 && !got_int)
9800 {
9801 if (curi[depth] > byts[arridx[depth]])
9802 {
9803 /* Done all bytes at this node, go up one level. */
9804 --depth;
9805 line_breakcheck();
9806 }
9807 else
9808 {
9809 /* Do one more byte at this node. */
9810 n = arridx[depth] + curi[depth];
9811 ++curi[depth];
9812 c = byts[n];
9813 if (c == 0)
9814 {
9815 /* End of word, deal with the word.
9816 * Don't use keep-case words in the fold-case tree,
9817 * they will appear in the keep-case tree.
9818 * Only use the word when the region matches. */
9819 flags = (int)idxs[n];
9820 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009821 && (do_region
9822 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009823 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009824 & lp->lp_region) != 0))
9825 {
9826 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009827 if (!do_region)
9828 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009829
9830 /* Dump the basic word if there is no prefix or
9831 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009832 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009833 if (c == 0 || curi[depth] == 2)
9834 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009835
9836 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009837 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009838 lnum = apply_prefixes(slang, word, round,
9839 flags, lnum);
9840 }
9841 }
9842 else
9843 {
9844 /* Normal char, go one level deeper. */
9845 word[depth++] = c;
9846 arridx[depth] = idxs[n];
9847 curi[depth] = 1;
9848 }
9849 }
9850 }
9851 }
9852 }
9853
9854 /* Delete the empty line that we started with. */
9855 if (curbuf->b_ml.ml_line_count > 1)
9856 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
9857
9858 redraw_later(NOT_VALID);
9859}
9860
9861/*
9862 * Dump one word: apply case modifications and append a line to the buffer.
9863 */
9864 static void
9865dump_word(word, round, flags, lnum)
9866 char_u *word;
9867 int round;
9868 int flags;
9869 linenr_T lnum;
9870{
9871 int keepcap = FALSE;
9872 char_u *p;
9873 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +00009874 char_u badword[MAXWLEN + 10];
9875 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009876
9877 if (round == 1 && (flags & WF_CAPMASK) != 0)
9878 {
9879 /* Need to fix case according to "flags". */
9880 make_case_word(word, cword, flags);
9881 p = cword;
9882 }
9883 else
9884 {
9885 p = word;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009886 if (round == 2 && ((captype(word, NULL) & WF_KEEPCAP) == 0
9887 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009888 keepcap = TRUE;
9889 }
9890
Bram Moolenaar7887d882005-07-01 22:33:52 +00009891 /* Add flags and regions after a slash. */
9892 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009893 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009894 STRCPY(badword, p);
9895 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009896 if (keepcap)
9897 STRCAT(badword, "=");
9898 if (flags & WF_BANNED)
9899 STRCAT(badword, "!");
9900 else if (flags & WF_RARE)
9901 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009902 if (flags & WF_REGION)
9903 for (i = 0; i < 7; ++i)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009904 if (flags & (0x10000 << i))
Bram Moolenaar7887d882005-07-01 22:33:52 +00009905 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009906 p = badword;
9907 }
9908
9909 ml_append(lnum, p, (colnr_T)0, FALSE);
9910}
9911
9912/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009913 * For ":spelldump": Find matching prefixes for "word". Prepend each to
9914 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009915 * Return the updated line number.
9916 */
9917 static linenr_T
9918apply_prefixes(slang, word, round, flags, startlnum)
9919 slang_T *slang;
9920 char_u *word; /* case-folded word */
9921 int round;
9922 int flags; /* flags with prefix ID */
9923 linenr_T startlnum;
9924{
9925 idx_T arridx[MAXWLEN];
9926 int curi[MAXWLEN];
9927 char_u prefix[MAXWLEN];
9928 int c;
9929 char_u *byts;
9930 idx_T *idxs;
9931 linenr_T lnum = startlnum;
9932 int depth;
9933 int n;
9934 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009935 int i;
9936
9937 byts = slang->sl_pbyts;
9938 idxs = slang->sl_pidxs;
9939 if (byts != NULL) /* array not is empty */
9940 {
9941 /*
9942 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009943 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009944 */
9945 depth = 0;
9946 arridx[0] = 0;
9947 curi[0] = 1;
9948 while (depth >= 0 && !got_int)
9949 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009950 n = arridx[depth];
9951 len = byts[n];
9952 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009953 {
9954 /* Done all bytes at this node, go up one level. */
9955 --depth;
9956 line_breakcheck();
9957 }
9958 else
9959 {
9960 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009961 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009962 ++curi[depth];
9963 c = byts[n];
9964 if (c == 0)
9965 {
9966 /* End of prefix, find out how many IDs there are. */
9967 for (i = 1; i < len; ++i)
9968 if (byts[n + i] != 0)
9969 break;
9970 curi[depth] += i - 1;
9971
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009972 i = valid_word_prefix(i, n, flags, word, slang);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009973 if (i != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009974 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009975 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009976 dump_word(prefix, round,
9977 (i & WF_RAREPFX) ? (flags | WF_RARE)
9978 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009979 }
9980 }
9981 else
9982 {
9983 /* Normal char, go one level deeper. */
9984 prefix[depth++] = c;
9985 arridx[depth] = idxs[n];
9986 curi[depth] = 1;
9987 }
9988 }
9989 }
9990 }
9991
9992 return lnum;
9993}
9994
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009995#endif /* FEAT_SYN_HL */