blob: f8b8460339f82fdfdb4d96f69013bca1c40ad20e [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.
182 * BY_FLAGS: End of word, <flags> follow.
183 * For PREFIXTREE <prefixID> and
184 * <prefcondnr> follow for rare prefix.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000185 * BY_INDEX: Child of sibling is shared, <nodeidx>
186 * and <xbyte> follow.
187 *
188 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
189 *
190 * <xbyte> 1 byte byte value of the sibling.
191 *
192 * <flags> 1 byte bitmask of:
193 * WF_ALLCAP word must have only capitals
194 * WF_ONECAP first char of word must be capital
195 * WF_RARE rare word
196 * WF_REGION <region> follows
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000197 * WF_PFX <prefixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000198 *
199 * <region> 1 byte Bitmask for regions in which word is valid. When
200 * omitted it's valid in all regions.
201 * Lowest bit is for region 1.
202 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000203 * <prefixID> 1 byte ID of prefix that can be used with this word. For
204 * PREFIXTREE used for the required prefix ID.
205 *
206 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
207 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000208 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000209 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000210 */
211
Bram Moolenaare19defe2005-03-21 08:23:33 +0000212#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
213# include <io.h> /* for lseek(), must be before vim.h */
214#endif
215
216#include "vim.h"
217
218#if defined(FEAT_SYN_HL) || defined(PROTO)
219
220#ifdef HAVE_FCNTL_H
221# include <fcntl.h>
222#endif
223
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000224#define MAXWLEN 250 /* Assume max. word len is this many bytes.
225 Some places assume a word length fits in a
226 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000227
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000228/* Type used for indexes in the word tree need to be at least 3 bytes. If int
229 * is 8 bytes we could use something smaller, but what? */
230#if SIZEOF_INT > 2
231typedef int idx_T;
232#else
233typedef long idx_T;
234#endif
235
236/* Flags used for a word. Only the lowest byte can be used, the region byte
237 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000238#define WF_REGION 0x01 /* region byte follows */
239#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
240#define WF_ALLCAP 0x04 /* word must be all capitals */
241#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000242#define WF_BANNED 0x10 /* bad word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000243#define WF_PFX 0x20 /* prefix ID list follows */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000244#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000245
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000246#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000247
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000248#define WF_RAREPFX 0x1000000 /* in sl_pidxs: flag for rare postponed
249 prefix; must be above prefixID (one byte)
250 and prefcondnr (two bytes) */
251
Bram Moolenaar51485f02005-06-04 21:55:20 +0000252#define BY_NOFLAGS 0 /* end of word without flags or region */
253#define BY_FLAGS 1 /* end of word, flag byte follows */
254#define BY_INDEX 2 /* child is shared, index follows */
255#define BY_SPECIAL BY_INDEX /* hightest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000256
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000257/* Info from "REP" and "SAL" entries in ".aff" file used in si_rep, sl_rep,
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000258 * and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000259 * One replacement: from "ft_from" to "ft_to". */
260typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000261{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000262 char_u *ft_from;
263 char_u *ft_to;
264} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000265
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000266/* Info from "SAL" entries in ".aff" file used in sl_sal.
267 * The info is split for quick processing by spell_soundfold().
268 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
269typedef struct salitem_S
270{
271 char_u *sm_lead; /* leading letters */
272 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000273 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000274 char_u *sm_rules; /* rules like ^, $, priority */
275 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000276#ifdef FEAT_MBYTE
277 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000278 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000279 int *sm_to_w; /* wide character copy of "sm_to" */
280#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000281} salitem_T;
282
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000283#ifdef FEAT_MBYTE
284typedef int salfirst_T;
285#else
286typedef short salfirst_T;
287#endif
288
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000289/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000290 * Structure used to store words and other info for one language, loaded from
291 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000292 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
293 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
294 *
295 * The "byts" array stores the possible bytes in each tree node, preceded by
296 * the number of possible bytes, sorted on byte value:
297 * <len> <byte1> <byte2> ...
298 * The "idxs" array stores the index of the child node corresponding to the
299 * byte in "byts".
300 * Exception: when the byte is zero, the word may end here and "idxs" holds
301 * the flags and region for the word. There may be several zeros in sequence
302 * for alternative flag/region combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000303 */
304typedef struct slang_S slang_T;
305struct slang_S
306{
307 slang_T *sl_next; /* next language */
308 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000309 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000310 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000311
Bram Moolenaar51485f02005-06-04 21:55:20 +0000312 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000313 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000314 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000315 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000316 char_u *sl_pbyts; /* prefix tree word bytes */
317 idx_T *sl_pidxs; /* prefix tree word indexes */
318
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000319 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000320
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000321 char_u *sl_midword; /* MIDWORD string or NULL */
322
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000323 int sl_prefixcnt; /* number of items in "sl_prefprog" */
324 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
325
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000326 garray_T sl_rep; /* list of fromto_T entries from REP lines */
327 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
328 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000329 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000330 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000331 there is none */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000332 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
333 * "sl_sal_first" maps chars, when has_mbyte
334 * "sl_sal" is a list of wide char lists. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000335 int sl_followup; /* SAL followup */
336 int sl_collapse; /* SAL collapse_result */
337 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaarea424162005-06-16 21:51:00 +0000338 int sl_has_map; /* TRUE if there is a MAP line */
339#ifdef FEAT_MBYTE
340 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
341 int sl_map_array[256]; /* MAP for first 256 chars */
342#else
343 char_u sl_map_array[256]; /* MAP for first 256 chars */
344#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000345};
346
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000347/* First language that is loaded, start of the linked list of loaded
348 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000349static slang_T *first_lang = NULL;
350
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000351/* Flags used in .spl file for soundsalike flags. */
352#define SAL_F0LLOWUP 1
353#define SAL_COLLAPSE 2
354#define SAL_REM_ACCENTS 4
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000355#define SAL_SOFO 8 /* SOFOFROM and SOFOTO instead of SAL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000356
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000357/*
358 * Structure used in "b_langp", filled from 'spelllang'.
359 */
360typedef struct langp_S
361{
362 slang_T *lp_slang; /* info for this language (NULL for last one) */
363 int lp_region; /* bitmask for region or REGION_ALL */
364} langp_T;
365
366#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
367
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000368#define REGION_ALL 0xff /* word valid in all regions */
369
370/* Result values. Lower number is accepted over higher one. */
371#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000372#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000373#define SP_RARE 1
374#define SP_LOCAL 2
375#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000376
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000377#define VIMSPELLMAGIC "VIMspell08" /* string at start of Vim spell file */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000378#define VIMSPELLMAGICL 10
379
Bram Moolenaar7887d882005-07-01 22:33:52 +0000380/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000381static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000382
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000383/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000384 * Information used when looking for suggestions.
385 */
386typedef struct suginfo_S
387{
388 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000389 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000390 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000391 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000392 char_u *su_badptr; /* start of bad word in line */
393 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000394 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000395 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
396 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
397 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000398} suginfo_T;
399
400/* One word suggestion. Used in "si_ga". */
401typedef struct suggest_S
402{
403 char_u *st_word; /* suggested word, allocated string */
404 int st_orglen; /* length of replaced text */
405 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000406 int st_altscore; /* used when st_score compares equal */
407 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000408 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000409} suggest_T;
410
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000411#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000412
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000413/* Number of suggestions kept when cleaning up. When rescore_suggestions() is
414 * called the score may change, thus we need to keep more than what is
415 * displayed. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000416#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 50 ? 50 : (su)->su_maxcount)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000417
418/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
419 * of suggestions that are not going to be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000420#define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000421
422/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000423#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000424#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000425#define SCORE_REGION 70 /* word is for different region */
426#define SCORE_RARE 180 /* rare word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000427#define SCORE_SWAP 90 /* swap two characters */
428#define SCORE_SWAP3 110 /* swap two characters in three */
429#define SCORE_REP 87 /* REP replacement */
430#define SCORE_SUBST 93 /* substitute a character */
431#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000432#define SCORE_DEL 94 /* delete a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000433#define SCORE_DELDUP 64 /* delete a duplicated character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000434#define SCORE_INS 96 /* insert a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000435#define SCORE_INSDUP 66 /* insert a duplicate character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000436#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000437
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000438#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000439#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
440 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000441
442#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000443#define SCORE_MAXMAX 999999 /* accept any score */
444
445/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000446 * Structure to store info for word matching.
447 */
448typedef struct matchinf_S
449{
450 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000451
452 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000453 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000454 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000455 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000456 char_u *mi_cend; /* char after what was used for
457 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000458
459 /* case-folded text */
460 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000461 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000462
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000463 /* for when checking word after a prefix */
464 int mi_prefarridx; /* index in sl_pidxs with list of
465 prefixID/condition */
466 int mi_prefcnt; /* number of entries at mi_prefarridx */
467 int mi_prefixlen; /* byte length of prefix */
468
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000469 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000470 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000471 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000472 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000473} matchinf_T;
474
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000475/*
476 * The tables used for recognizing word characters according to spelling.
477 * These are only used for the first 256 characters of 'encoding'.
478 */
479typedef struct spelltab_S
480{
481 char_u st_isw[256]; /* flags: is word char */
482 char_u st_isu[256]; /* flags: is uppercase char */
483 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000484 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000485} spelltab_T;
486
487static spelltab_T spelltab;
488static int did_set_spelltab;
489
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000490#define CF_WORD 0x01
491#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000492
493static void clear_spell_chartab __ARGS((spelltab_T *sp));
494static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000495static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
496static int spell_iswordp_nmw __ARGS((char_u *p));
497#ifdef FEAT_MBYTE
498static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
499#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000500static void write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000501
502/*
Bram Moolenaarea408852005-06-25 22:49:46 +0000503 * Return TRUE if "p" points to a word character. Like spell_iswordp() but
504 * without the special handling of a single quote.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000505 * Checking for a word character is done very often, avoid the function call
506 * overhead.
507 */
508#ifdef FEAT_MBYTE
509# define SPELL_ISWORDP(p) ((has_mbyte && MB_BYTE2LEN(*(p)) > 1) \
510 ? (mb_get_class(p) >= 2) : spelltab.st_isw[*(p)])
511#else
512# define SPELL_ISWORDP(p) (spelltab.st_isw[*(p)])
513#endif
514
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000515/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000516 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000517 */
518typedef enum
519{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000520 STATE_START = 0, /* At start of node check for NUL bytes (goodword
521 * ends); if badword ends there is a match, otherwise
522 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000523 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000524 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000525 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
526 STATE_PLAIN, /* Use each byte of the node. */
527 STATE_DEL, /* Delete a byte from the bad word. */
528 STATE_INS, /* Insert a byte in the bad word. */
529 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000530 STATE_UNSWAP, /* Undo swap two characters. */
531 STATE_SWAP3, /* Swap two characters over three. */
532 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000533 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000534 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000535 STATE_REP_INI, /* Prepare for using REP items. */
536 STATE_REP, /* Use matching REP items from the .aff file. */
537 STATE_REP_UNDO, /* Undo a REP item replacement. */
538 STATE_FINAL /* End of this node. */
539} state_T;
540
541/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000542 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000543 */
544typedef struct trystate_S
545{
Bram Moolenaarea424162005-06-16 21:51:00 +0000546 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000547 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000548 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000549 short ts_curi; /* index in list of child nodes */
550 char_u ts_fidx; /* index in fword[], case-folded bad word */
551 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
552 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000553 char_u ts_prefixdepth; /* stack depth for end of prefix or PREFIXTREE
554 * or NOPREFIX */
Bram Moolenaarea424162005-06-16 21:51:00 +0000555#ifdef FEAT_MBYTE
556 char_u ts_tcharlen; /* number of bytes in tword character */
557 char_u ts_tcharidx; /* current byte index in tword character */
558 char_u ts_isdiff; /* DIFF_ values */
559 char_u ts_fcharstart; /* index in fword where badword char started */
560#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000561 char_u ts_save_prewordlen; /* saved "prewordlen" */
Bram Moolenaarea424162005-06-16 21:51:00 +0000562 char_u ts_save_splitoff; /* su_splitoff saved here */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000563 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000564} trystate_T;
565
Bram Moolenaarea424162005-06-16 21:51:00 +0000566/* values for ts_isdiff */
567#define DIFF_NONE 0 /* no different byte (yet) */
568#define DIFF_YES 1 /* different byte found */
569#define DIFF_INSERT 2 /* inserting character */
570
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000571/* special values ts_prefixdepth */
572#define PREFIXTREE 0xfe /* walking through the prefix tree */
573#define NOPREFIX 0xff /* not using prefixes */
574
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000575/* mode values for find_word */
576#define FIND_FOLDWORD 0 /* find word case-folded */
577#define FIND_KEEPWORD 1 /* find keep-case word */
578#define FIND_PREFIX 2 /* find word after prefix */
579
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000580static slang_T *slang_alloc __ARGS((char_u *lang));
581static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000582static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000583static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000584static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int prefid, char_u *word, slang_T *slang));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000585static void find_prefix __ARGS((matchinf_T *mip));
586static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000587static int spell_valid_case __ARGS((int origflags, int treeflags));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000588static int no_spell_checking __ARGS((void));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000589static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000590static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000591static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000592static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000593static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000594static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *errp));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000595static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
596static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000597#ifdef FEAT_MBYTE
598static int *mb_str2wide __ARGS((char_u *s));
599#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000600static 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 +0000601static void clear_midword __ARGS((buf_T *buf));
602static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000603static int find_region __ARGS((char_u *rp, char_u *region));
604static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000605static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000606static int set_spell_charflags __ARGS((char_u *flags, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000607static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
608static void write_spell_chartab __ARGS((FILE *fd));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000609static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaarea408852005-06-25 22:49:46 +0000610static void spell_find_suggest __ARGS((char_u *badptr, suginfo_T *su, int maxcount, int banbadword));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000611#ifdef FEAT_EVAL
612static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
613#endif
614static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
615static void spell_suggest_intern __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000616static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000617static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000618static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000619static void suggest_try_special __ARGS((suginfo_T *su));
620static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000621static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
622static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000623static void score_comp_sal __ARGS((suginfo_T *su));
624static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000625static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000626static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000627static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000628static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000629static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000630static 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 +0000631static void add_banned __ARGS((suginfo_T *su, char_u *word));
632static int was_banned __ARGS((suginfo_T *su, char_u *word));
633static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000634static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000635static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000636static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
637static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
638static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000639#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000640static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000641#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000642static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000643static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000644static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum));
645static 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 +0000646
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000647/*
648 * Use our own character-case definitions, because the current locale may
649 * differ from what the .spl file uses.
650 * These must not be called with negative number!
651 */
652#ifndef FEAT_MBYTE
653/* Non-multi-byte implementation. */
654# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
655# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
656# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
657#else
658/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
659 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
660 * the "w" library function for characters above 255 if available. */
661# ifdef HAVE_TOWLOWER
662# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
663 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
664# else
665# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
666 : (c) < 256 ? spelltab.st_fold[c] : (c))
667# endif
668
669# ifdef HAVE_TOWUPPER
670# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
671 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
672# else
673# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
674 : (c) < 256 ? spelltab.st_upper[c] : (c))
675# endif
676
677# ifdef HAVE_ISWUPPER
678# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
679 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
680# else
681# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
682 : (c) < 256 ? spelltab.st_isu[c] : (c))
683# endif
684#endif
685
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000686
687static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000688static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000689
690/*
691 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000692 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000693 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
694 * or when it's OK it remains unchanged.
695 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000696 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000697 * "capcol" is used to check for a Capitalised word after the end of a
698 * sentence. If it's zero then perform the check. Return the column where to
699 * check next, or -1 when no sentence end was found. If it's NULL then don't
700 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000701 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000702 * Returns the length of the word in bytes, also when it's OK, so that the
703 * caller can skip over the word.
704 */
705 int
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000706spell_check(wp, ptr, attrp, capcol)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000707 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000708 char_u *ptr;
709 int *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000710 int *capcol; /* column to check for Capital */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000711{
712 matchinf_T mi; /* Most things are put in "mi" so that it can
713 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000714 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000715 int c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000716
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000717 /* A word never starts at a space or a control character. Return quickly
718 * then, skipping over the character. */
719 if (*ptr <= ' ')
720 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000721
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000722 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar0c405862005-06-22 22:26:26 +0000723 * 0X99FF. But when a word character follows do check spelling to find
724 * "3GPP". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000725 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000726 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000727 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
728 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000729 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000730 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000731 mi.mi_end = skipdigits(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000732 nrlen = mi.mi_end - ptr;
733 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000734 if (!spell_iswordp(mi.mi_end, wp->w_buffer))
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000735 return (int)(mi.mi_end - ptr);
Bram Moolenaar0c405862005-06-22 22:26:26 +0000736
737 /* Try including the digits in the word. */
738 mi.mi_fend = ptr + nrlen;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000739 }
Bram Moolenaar0c405862005-06-22 22:26:26 +0000740 else
741 mi.mi_fend = ptr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000742
Bram Moolenaar0c405862005-06-22 22:26:26 +0000743 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000744 mi.mi_word = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000745 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000746 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000747 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000748 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000749 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000750 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000751
752 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
753 {
754 /* Check word starting with capital letter. */
755#ifdef FEAT_MBYTE
756 c = mb_ptr2char(ptr);
757#else
758 c = *ptr;
759#endif
760 if (!SPELL_ISUPPER(c))
761 {
762 *attrp = highlight_attr[HLF_SPC];
763 return (int)(mi.mi_fend - ptr);
764 }
765 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000766 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000767 if (capcol != NULL)
768 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000769
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000770 /* We always use the characters up to the next non-word character,
771 * also for bad words. */
772 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000773
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000774 /* Check caps type later. */
775 mi.mi_capflags = 0;
776 mi.mi_cend = NULL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000777 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000778
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000779 /* Include one non-word character so that we can check for the
780 * word end. */
781 if (*mi.mi_fend != NUL)
782 mb_ptr_adv(mi.mi_fend);
783
784 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
785 MAXWLEN + 1);
786 mi.mi_fwordlen = STRLEN(mi.mi_fword);
787
788 /* The word is bad unless we recognize it. */
789 mi.mi_result = SP_BAD;
790
791 /*
792 * Loop over the languages specified in 'spelllang'.
793 * We check them all, because a matching word may be longer than an
794 * already found matching word.
795 */
796 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
797 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
798 {
799 /* Check for a matching word in case-folded words. */
800 find_word(&mi, FIND_FOLDWORD);
801
802 /* Check for a matching word in keep-case words. */
803 find_word(&mi, FIND_KEEPWORD);
804
805 /* Check for matching prefixes. */
806 find_prefix(&mi);
807 }
808
809 if (mi.mi_result != SP_OK)
810 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000811 /* If we found a number skip over it. Allows for "42nd". Do flag
812 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000813 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000814 {
815 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
816 return nrlen;
817 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000818
819 /* When we are at a non-word character there is no error, just
820 * skip over the character (try looking for a word after it). */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000821 else if (!SPELL_ISWORDP(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000822 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000823 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
824 {
825 regmatch_T regmatch;
826
827 /* Check for end of sentence. */
828 regmatch.regprog = wp->w_buffer->b_cap_prog;
829 regmatch.rm_ic = FALSE;
830 if (vim_regexec(&regmatch, ptr, 0))
831 *capcol = (int)(regmatch.endp[0] - ptr);
832 }
833
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000834#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000835 if (has_mbyte)
836 return mb_ptr2len_check(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000837#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000838 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000839 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000840
841 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
842 *attrp = highlight_attr[HLF_SPB];
843 else if (mi.mi_result == SP_RARE)
844 *attrp = highlight_attr[HLF_SPR];
845 else
846 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000847 }
848
Bram Moolenaar51485f02005-06-04 21:55:20 +0000849 return (int)(mi.mi_end - ptr);
850}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000851
Bram Moolenaar51485f02005-06-04 21:55:20 +0000852/*
853 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000854 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
855 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
856 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
857 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000858 *
859 * For a match mip->mi_result is updated.
860 */
861 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000862find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000863 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000864 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000865{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000866 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000867 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000868 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000869 int endidxcnt = 0;
870 int len;
871 int wlen = 0;
872 int flen;
873 int c;
874 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000875 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000876#ifdef FEAT_MBYTE
877 char_u *s;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000878 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000879#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000880 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000881 slang_T *slang = mip->mi_lp->lp_slang;
882 unsigned flags;
883 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000884 idx_T *idxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000885 int prefid;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000886
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000887 if (mode == FIND_KEEPWORD)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000888 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000889 /* Check for word with matching case in keep-case tree. */
890 ptr = mip->mi_word;
891 flen = 9999; /* no case folding, always enough bytes */
892 byts = slang->sl_kbyts;
893 idxs = slang->sl_kidxs;
894 }
895 else
896 {
897 /* Check for case-folded in case-folded tree. */
898 ptr = mip->mi_fword;
899 flen = mip->mi_fwordlen; /* available case-folded bytes */
900 byts = slang->sl_fbyts;
901 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000902
903 if (mode == FIND_PREFIX)
904 {
905 /* Skip over the prefix. */
906 wlen = mip->mi_prefixlen;
907 flen -= mip->mi_prefixlen;
908 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000909 }
910
Bram Moolenaar51485f02005-06-04 21:55:20 +0000911 if (byts == NULL)
912 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000913
Bram Moolenaar51485f02005-06-04 21:55:20 +0000914 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000915 * Repeat advancing in the tree until:
916 * - there is a byte that doesn't match,
917 * - we reach the end of the tree,
918 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000919 */
920 for (;;)
921 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000922 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000923 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000924
925 len = byts[arridx++];
926
927 /* If the first possible byte is a zero the word could end here.
928 * Remember this index, we first check for the longest word. */
929 if (byts[arridx] == 0)
930 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000931 if (endidxcnt == MAXWLEN)
932 {
933 /* Must be a corrupted spell file. */
934 EMSG(_(e_format));
935 return;
936 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000937 endlen[endidxcnt] = wlen;
938 endidx[endidxcnt++] = arridx++;
939 --len;
940
941 /* Skip over the zeros, there can be several flag/region
942 * combinations. */
943 while (len > 0 && byts[arridx] == 0)
944 {
945 ++arridx;
946 --len;
947 }
948 if (len == 0)
949 break; /* no children, word must end here */
950 }
951
952 /* Stop looking at end of the line. */
953 if (ptr[wlen] == NUL)
954 break;
955
956 /* Perform a binary search in the list of accepted bytes. */
957 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +0000958 if (c == TAB) /* <Tab> is handled like <Space> */
959 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +0000960 lo = arridx;
961 hi = arridx + len - 1;
962 while (lo < hi)
963 {
964 m = (lo + hi) / 2;
965 if (byts[m] > c)
966 hi = m - 1;
967 else if (byts[m] < c)
968 lo = m + 1;
969 else
970 {
971 lo = hi = m;
972 break;
973 }
974 }
975
976 /* Stop if there is no matching byte. */
977 if (hi < lo || byts[lo] != c)
978 break;
979
980 /* Continue at the child (if there is one). */
981 arridx = idxs[lo];
982 ++wlen;
983 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +0000984
985 /* One space in the good word may stand for several spaces in the
986 * checked word. */
987 if (c == ' ')
988 {
989 for (;;)
990 {
991 if (flen <= 0 && *mip->mi_fend != NUL)
992 flen = fold_more(mip);
993 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
994 break;
995 ++wlen;
996 --flen;
997 }
998 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000999 }
1000
1001 /*
1002 * Verify that one of the possible endings is valid. Try the longest
1003 * first.
1004 */
1005 while (endidxcnt > 0)
1006 {
1007 --endidxcnt;
1008 arridx = endidx[endidxcnt];
1009 wlen = endlen[endidxcnt];
1010
1011#ifdef FEAT_MBYTE
1012 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1013 continue; /* not at first byte of character */
1014#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001015 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001016 continue; /* next char is a word character */
1017
1018#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001019 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001020 {
1021 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001022 * when folding case. This can be slow, take a shortcut when the
1023 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001024 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001025 if (STRNCMP(ptr, p, wlen) != 0)
1026 {
1027 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1028 mb_ptr_adv(p);
1029 wlen = p - mip->mi_word;
1030 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001031 }
1032#endif
1033
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001034 /* Check flags and region. For FIND_PREFIX check the condition and
1035 * prefix ID.
1036 * Repeat this if there are more flags/region alternatives until there
1037 * is a match. */
1038 res = SP_BAD;
1039 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1040 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001041 {
1042 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001043
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001044 /* For the fold-case tree check that the case of the checked word
1045 * matches with what the word in the tree requires.
1046 * For keep-case tree the case is always right. For prefixes we
1047 * don't bother to check. */
1048 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001049 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001050 if (mip->mi_cend != mip->mi_word + wlen)
1051 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001052 /* mi_capflags was set for a different word length, need
1053 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001054 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001055 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001056 }
1057
Bram Moolenaar0c405862005-06-22 22:26:26 +00001058 if (mip->mi_capflags == WF_KEEPCAP
1059 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001060 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001061 }
1062
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001063 /* When mode is FIND_PREFIX the word must support the prefix:
1064 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001065 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001066 if (mode == FIND_PREFIX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001067 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001068 /* The prefix ID is stored two bytes above the flags. */
1069 prefid = (unsigned)flags >> 16;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001070 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001071 prefid, mip->mi_fword + mip->mi_prefixlen,
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001072 slang);
1073 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001074 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001075
1076 /* Use the WF_RARE flag for a rare prefix. */
1077 if (c & WF_RAREPFX)
1078 flags |= WF_RARE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001079 }
1080
1081 if (flags & WF_BANNED)
1082 res = SP_BANNED;
1083 else if (flags & WF_REGION)
1084 {
1085 /* Check region. */
1086 if ((mip->mi_lp->lp_region & (flags >> 8)) != 0)
1087 res = SP_OK;
1088 else
1089 res = SP_LOCAL;
1090 }
1091 else if (flags & WF_RARE)
1092 res = SP_RARE;
1093 else
1094 res = SP_OK;
1095
1096 /* Always use the longest match and the best result. */
1097 if (mip->mi_result > res)
1098 {
1099 mip->mi_result = res;
1100 mip->mi_end = mip->mi_word + wlen;
1101 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001102 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001103 mip->mi_end = mip->mi_word + wlen;
1104
1105 if (res == SP_OK)
1106 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001107 }
1108
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001109 if (res == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001110 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001111 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001112}
1113
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001114/*
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001115 * Return non-zero if the prefix indicated by "mip->mi_prefarridx" matches
1116 * with the prefix ID "prefid" for the word "word".
1117 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001118 */
1119 static int
1120valid_word_prefix(totprefcnt, arridx, prefid, word, slang)
1121 int totprefcnt; /* nr of prefix IDs */
1122 int arridx; /* idx in sl_pidxs[] */
1123 int prefid;
1124 char_u *word;
1125 slang_T *slang;
1126{
1127 int prefcnt;
1128 int pidx;
1129 regprog_T *rp;
1130 regmatch_T regmatch;
1131
1132 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1133 {
1134 pidx = slang->sl_pidxs[arridx + prefcnt];
1135
1136 /* Check the prefix ID. */
1137 if (prefid != (pidx & 0xff))
1138 continue;
1139
1140 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001141 * stored in the two bytes above the prefix ID byte. */
1142 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001143 if (rp != NULL)
1144 {
1145 regmatch.regprog = rp;
1146 regmatch.rm_ic = FALSE;
1147 if (!vim_regexec(&regmatch, word, 0))
1148 continue;
1149 }
1150
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001151 /* It's a match! Return the WF_RAREPFX flag. */
1152 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001153 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001154 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001155}
1156
1157/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001158 * Check if the word at "mip->mi_word" has a matching prefix.
1159 * If it does, then check the following word.
1160 *
1161 * For a match mip->mi_result is updated.
1162 */
1163 static void
1164find_prefix(mip)
1165 matchinf_T *mip;
1166{
1167 idx_T arridx = 0;
1168 int len;
1169 int wlen = 0;
1170 int flen;
1171 int c;
1172 char_u *ptr;
1173 idx_T lo, hi, m;
1174 slang_T *slang = mip->mi_lp->lp_slang;
1175 char_u *byts;
1176 idx_T *idxs;
1177
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001178 byts = slang->sl_pbyts;
1179 if (byts == NULL)
1180 return; /* array is empty */
1181
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001182 /* We use the case-folded word here, since prefixes are always
1183 * case-folded. */
1184 ptr = mip->mi_fword;
1185 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001186 idxs = slang->sl_pidxs;
1187
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001188 /*
1189 * Repeat advancing in the tree until:
1190 * - there is a byte that doesn't match,
1191 * - we reach the end of the tree,
1192 * - or we reach the end of the line.
1193 */
1194 for (;;)
1195 {
1196 if (flen == 0 && *mip->mi_fend != NUL)
1197 flen = fold_more(mip);
1198
1199 len = byts[arridx++];
1200
1201 /* If the first possible byte is a zero the prefix could end here.
1202 * Check if the following word matches and supports the prefix. */
1203 if (byts[arridx] == 0)
1204 {
1205 /* There can be several prefixes with different conditions. We
1206 * try them all, since we don't know which one will give the
1207 * longest match. The word is the same each time, pass the list
1208 * of possible prefixes to find_word(). */
1209 mip->mi_prefarridx = arridx;
1210 mip->mi_prefcnt = len;
1211 while (len > 0 && byts[arridx] == 0)
1212 {
1213 ++arridx;
1214 --len;
1215 }
1216 mip->mi_prefcnt -= len;
1217
1218 /* Find the word that comes after the prefix. */
1219 mip->mi_prefixlen = wlen;
1220 find_word(mip, FIND_PREFIX);
1221
1222
1223 if (len == 0)
1224 break; /* no children, word must end here */
1225 }
1226
1227 /* Stop looking at end of the line. */
1228 if (ptr[wlen] == NUL)
1229 break;
1230
1231 /* Perform a binary search in the list of accepted bytes. */
1232 c = ptr[wlen];
1233 lo = arridx;
1234 hi = arridx + len - 1;
1235 while (lo < hi)
1236 {
1237 m = (lo + hi) / 2;
1238 if (byts[m] > c)
1239 hi = m - 1;
1240 else if (byts[m] < c)
1241 lo = m + 1;
1242 else
1243 {
1244 lo = hi = m;
1245 break;
1246 }
1247 }
1248
1249 /* Stop if there is no matching byte. */
1250 if (hi < lo || byts[lo] != c)
1251 break;
1252
1253 /* Continue at the child (if there is one). */
1254 arridx = idxs[lo];
1255 ++wlen;
1256 --flen;
1257 }
1258}
1259
1260/*
1261 * Need to fold at least one more character. Do until next non-word character
1262 * for efficiency.
1263 * Return the length of the folded chars in bytes.
1264 */
1265 static int
1266fold_more(mip)
1267 matchinf_T *mip;
1268{
1269 int flen;
1270 char_u *p;
1271
1272 p = mip->mi_fend;
1273 do
1274 {
1275 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001276 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001277
1278 /* Include the non-word character so that we can check for the
1279 * word end. */
1280 if (*mip->mi_fend != NUL)
1281 mb_ptr_adv(mip->mi_fend);
1282
1283 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1284 mip->mi_fword + mip->mi_fwordlen,
1285 MAXWLEN - mip->mi_fwordlen);
1286 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1287 mip->mi_fwordlen += flen;
1288 return flen;
1289}
1290
1291/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001292 * Check case flags for a word. Return TRUE if the word has the requested
1293 * case.
1294 */
1295 static int
1296spell_valid_case(origflags, treeflags)
1297 int origflags; /* flags for the checked word. */
1298 int treeflags; /* flags for the word in the spell tree */
1299{
1300 return (origflags == WF_ALLCAP
1301 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
1302 && ((treeflags & WF_ONECAP) == 0 || origflags == WF_ONECAP)));
1303}
1304
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001305/*
1306 * Return TRUE if spell checking is not enabled.
1307 */
1308 static int
1309no_spell_checking()
1310{
1311 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
1312 {
1313 EMSG(_("E756: Spell checking is not enabled"));
1314 return TRUE;
1315 }
1316 return FALSE;
1317}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001318
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001319/*
1320 * Move to next spell error.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001321 * "curline" is TRUE for "z?": find word under/after cursor in the same line.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001322 * Return OK if found, FAIL otherwise.
1323 */
1324 int
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001325spell_move_to(dir, allwords, curline)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001326 int dir; /* FORWARD or BACKWARD */
1327 int allwords; /* TRUE for "[s" and "]s" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001328 int curline;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001329{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001330 linenr_T lnum;
1331 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001332 char_u *line;
1333 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001334 char_u *endp;
1335 int attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001336 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001337 int has_syntax = syntax_present(curbuf);
1338 int col;
1339 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001340 char_u *buf = NULL;
1341 int buflen = 0;
1342 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001343 int capcol = -1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001344
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001345 if (no_spell_checking())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001346 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001347
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001348 /*
1349 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001350 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001351 *
1352 * When searching backwards, we continue in the line to find the last
1353 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001354 *
1355 * We concatenate the start of the next line, so that wrapped words work
1356 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1357 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001358 */
1359 lnum = curwin->w_cursor.lnum;
1360 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001361
1362 while (!got_int)
1363 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001364 line = ml_get(lnum);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001365
Bram Moolenaar0c405862005-06-22 22:26:26 +00001366 len = STRLEN(line);
1367 if (buflen < len + MAXWLEN + 2)
1368 {
1369 vim_free(buf);
1370 buflen = len + MAXWLEN + 2;
1371 buf = alloc(buflen);
1372 if (buf == NULL)
1373 break;
1374 }
1375
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001376 /* In first line check first word for Capital. */
1377 if (lnum == 1)
1378 capcol = 0;
1379
1380 /* For checking first word with a capital skip white space. */
1381 if (capcol == 0)
1382 capcol = skipwhite(line) - line;
1383
Bram Moolenaar0c405862005-06-22 22:26:26 +00001384 /* Copy the line into "buf" and append the start of the next line if
1385 * possible. */
1386 STRCPY(buf, line);
1387 if (lnum < curbuf->b_ml.ml_line_count)
1388 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1389
1390 p = buf + skip;
1391 endp = buf + len;
1392 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001393 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001394 /* When searching backward don't search after the cursor. */
1395 if (dir == BACKWARD
1396 && lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001397 && (colnr_T)(p - buf) >= curwin->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001398 break;
1399
1400 /* start of word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00001401 attr = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001402 len = spell_check(curwin, p, &attr, &capcol);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001403
1404 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001405 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001406 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001407 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001408 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001409 /* When searching forward only accept a bad word after
1410 * the cursor. */
1411 if (dir == BACKWARD
1412 || lnum > curwin->w_cursor.lnum
1413 || (lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001414 && (colnr_T)(curline ? p - buf + len
1415 : p - buf)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001416 > curwin->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001417 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001418 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001419 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001420 col = p - buf;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001421 (void)syn_get_id(lnum, (colnr_T)col,
1422 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001423 }
1424 else
1425 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001426
Bram Moolenaar51485f02005-06-04 21:55:20 +00001427 if (can_spell)
1428 {
1429 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001430 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001431#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001432 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001433#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001434 if (dir == FORWARD)
1435 {
1436 /* No need to search further. */
1437 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001438 vim_free(buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001439 return OK;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001440 }
1441 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001442 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001443 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001444 }
1445
Bram Moolenaar51485f02005-06-04 21:55:20 +00001446 /* advance to character after the word */
1447 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001448 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001449 }
1450
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001451 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001452 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001453
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001454 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001455 if (dir == BACKWARD)
1456 {
1457 if (found_pos.lnum != 0)
1458 {
1459 /* Use the last match in the line. */
1460 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001461 vim_free(buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001462 return OK;
1463 }
1464 if (lnum == 1)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001465 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001466 --lnum;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001467 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001468 }
1469 else
1470 {
1471 if (lnum == curbuf->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001472 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001473 ++lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001474
1475 /* Skip the characters at the start of the next line that were
1476 * included in a match crossing line boundaries. */
1477 if (attr == 0)
1478 skip = p - endp;
1479 else
1480 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001481
1482 /* Capscol skips over the inserted space. */
1483 --capcol;
1484
1485 /* But after empty line check first word in next line */
1486 if (*skipwhite(line) == NUL)
1487 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001488 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001489
1490 line_breakcheck();
1491 }
1492
Bram Moolenaar0c405862005-06-22 22:26:26 +00001493 vim_free(buf);
1494 return FAIL;
1495}
1496
1497/*
1498 * For spell checking: concatenate the start of the following line "line" into
1499 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
1500 */
1501 void
1502spell_cat_line(buf, line, maxlen)
1503 char_u *buf;
1504 char_u *line;
1505 int maxlen;
1506{
1507 char_u *p;
1508 int n;
1509
1510 p = skipwhite(line);
1511 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1512 p = skipwhite(p + 1);
1513
1514 if (*p != NUL)
1515 {
1516 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001517 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001518 n = p - line;
1519 if (n >= maxlen)
1520 n = maxlen - 1;
1521 vim_memset(buf + 1, ' ', n);
1522 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001523}
1524
1525/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001526 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001527 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001528 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001529 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001530spell_load_lang(lang)
1531 char_u *lang;
1532{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001533 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001534 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001535 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001536
Bram Moolenaarb765d632005-06-07 21:00:02 +00001537 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001538 * It's truncated when an error is detected. */
1539 STRCPY(langcp, lang);
1540
Bram Moolenaarb765d632005-06-07 21:00:02 +00001541 /*
1542 * Find the first spell file for "lang" in 'runtimepath' and load it.
1543 */
1544 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
1545 "spell/%s.%s.spl", lang, spell_enc());
1546 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001547
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001548 if (r == FAIL && *langcp != NUL)
1549 {
1550 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001551 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001552 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001553 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001554 }
1555
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001556 if (r == FAIL)
1557 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
1558 fname_enc + 6);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001559 else if (*langcp != NUL)
1560 {
1561 /* Load all the additions. */
1562 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
1563 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
1564 }
1565}
1566
1567/*
1568 * Return the encoding used for spell checking: Use 'encoding', except that we
1569 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1570 */
1571 static char_u *
1572spell_enc()
1573{
1574
1575#ifdef FEAT_MBYTE
1576 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1577 return p_enc;
1578#endif
1579 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001580}
1581
1582/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001583 * Get the name of the .spl file for the internal wordlist into
1584 * "fname[MAXPATHL]".
1585 */
1586 static void
1587int_wordlist_spl(fname)
1588 char_u *fname;
1589{
1590 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
1591 int_wordlist, spell_enc());
1592}
1593
1594/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001595 * Allocate a new slang_T.
1596 * Caller must fill "sl_next".
1597 */
1598 static slang_T *
1599slang_alloc(lang)
1600 char_u *lang;
1601{
1602 slang_T *lp;
1603
Bram Moolenaar51485f02005-06-04 21:55:20 +00001604 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001605 if (lp != NULL)
1606 {
1607 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001608 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001609 ga_init2(&lp->sl_sal, sizeof(salitem_T), 10);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001610 }
1611 return lp;
1612}
1613
1614/*
1615 * Free the contents of an slang_T and the structure itself.
1616 */
1617 static void
1618slang_free(lp)
1619 slang_T *lp;
1620{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001621 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001622 vim_free(lp->sl_fname);
1623 slang_clear(lp);
1624 vim_free(lp);
1625}
1626
1627/*
1628 * Clear an slang_T so that the file can be reloaded.
1629 */
1630 static void
1631slang_clear(lp)
1632 slang_T *lp;
1633{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001634 garray_T *gap;
1635 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001636 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001637 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001638
Bram Moolenaar51485f02005-06-04 21:55:20 +00001639 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001640 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001641 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001642 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001643 vim_free(lp->sl_pbyts);
1644 lp->sl_pbyts = NULL;
1645
Bram Moolenaar51485f02005-06-04 21:55:20 +00001646 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001647 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001648 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001649 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001650 vim_free(lp->sl_pidxs);
1651 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001652
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001653 gap = &lp->sl_rep;
1654 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001655 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001656 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1657 vim_free(ftp->ft_from);
1658 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001659 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001660 ga_clear(gap);
1661
1662 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001663 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001664 {
1665 /* "ga_len" is set to 1 without adding an item for latin1 */
1666 if (gap->ga_data != NULL)
1667 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
1668 for (i = 0; i < gap->ga_len; ++i)
1669 vim_free(((int **)gap->ga_data)[i]);
1670 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001671 else
1672 /* SAL items: free salitem_T items */
1673 while (gap->ga_len > 0)
1674 {
1675 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1676 vim_free(smp->sm_lead);
1677 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
1678 vim_free(smp->sm_to);
1679#ifdef FEAT_MBYTE
1680 vim_free(smp->sm_lead_w);
1681 vim_free(smp->sm_oneof_w);
1682 vim_free(smp->sm_to_w);
1683#endif
1684 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001685 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001686
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001687 for (i = 0; i < lp->sl_prefixcnt; ++i)
1688 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001689 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001690 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001691 lp->sl_prefprog = NULL;
1692
1693 vim_free(lp->sl_midword);
1694 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001695
Bram Moolenaarea424162005-06-16 21:51:00 +00001696#ifdef FEAT_MBYTE
1697 {
1698 int todo = lp->sl_map_hash.ht_used;
1699 hashitem_T *hi;
1700
1701 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
1702 if (!HASHITEM_EMPTY(hi))
1703 {
1704 --todo;
1705 vim_free(hi->hi_key);
1706 }
1707 }
1708 hash_clear(&lp->sl_map_hash);
1709#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001710}
1711
1712/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001713 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001714 * Invoked through do_in_runtimepath().
1715 */
1716 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00001717spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001718 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001719 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001720{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001721 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001722}
1723
1724/*
1725 * Load one spell file and store the info into a slang_T.
1726 *
1727 * This is invoked in two ways:
1728 * - From spell_load_cb() to load a spell file for the first time. "lang" is
1729 * the language name, "old_lp" is NULL. Will allocate an slang_T.
1730 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
1731 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001732 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00001733 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001734 static slang_T *
1735spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001736 char_u *fname;
1737 char_u *lang;
1738 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001739 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001740{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001741 FILE *fd;
1742 char_u buf[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001743 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001744 char_u *bp;
1745 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001746 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001747 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001748 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001749 int round;
1750 char_u *save_sourcing_name = sourcing_name;
1751 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001752 int cnt, ccnt;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001753 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001754 slang_T *lp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001755 garray_T *gap;
1756 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001757 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001758 short *first;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001759 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001760 int c = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001761
Bram Moolenaarb765d632005-06-07 21:00:02 +00001762 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001763 if (fd == NULL)
1764 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001765 if (!silent)
1766 EMSG2(_(e_notopen), fname);
1767 else if (p_verbose > 2)
1768 {
1769 verbose_enter();
1770 smsg((char_u *)e_notopen, fname);
1771 verbose_leave();
1772 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001773 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001774 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00001775 if (p_verbose > 2)
1776 {
1777 verbose_enter();
1778 smsg((char_u *)_("Reading spell file \"%s\""), fname);
1779 verbose_leave();
1780 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001781
Bram Moolenaarb765d632005-06-07 21:00:02 +00001782 if (old_lp == NULL)
1783 {
1784 lp = slang_alloc(lang);
1785 if (lp == NULL)
1786 goto endFAIL;
1787
1788 /* Remember the file name, used to reload the file when it's updated. */
1789 lp->sl_fname = vim_strsave(fname);
1790 if (lp->sl_fname == NULL)
1791 goto endFAIL;
1792
1793 /* Check for .add.spl. */
1794 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
1795 }
1796 else
1797 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001798
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001799 /* Set sourcing_name, so that error messages mention the file name. */
1800 sourcing_name = fname;
1801 sourcing_lnum = 0;
1802
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001803 /* <HEADER>: <fileID>
1804 * <regioncnt> <regionname> ...
1805 * <charflagslen> <charflags>
1806 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001807 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001808 * <prefcondcnt> <prefcond> ...
1809 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001810 for (i = 0; i < VIMSPELLMAGICL; ++i)
1811 buf[i] = getc(fd); /* <fileID> */
1812 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
1813 {
1814 EMSG(_("E757: Wrong file ID in spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001815 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001816 }
1817
1818 cnt = getc(fd); /* <regioncnt> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001819 if (cnt < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001820 {
1821truncerr:
Bram Moolenaar7887d882005-07-01 22:33:52 +00001822 EMSG(_(e_spell_trunc));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001823 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001824 }
1825 if (cnt > 8)
1826 {
1827formerr:
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001828 EMSG(_(e_format));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001829 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001830 }
1831 for (i = 0; i < cnt; ++i)
1832 {
1833 lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
1834 lp->sl_regions[i * 2 + 1] = getc(fd);
1835 }
1836 lp->sl_regions[cnt * 2] = NUL;
1837
Bram Moolenaar7887d882005-07-01 22:33:52 +00001838 /* <charflagslen> <charflags> */
1839 p = read_cnt_string(fd, 1, &cnt);
1840 if (cnt == FAIL)
1841 goto endFAIL;
1842
1843 /* <fcharslen> <fchars> */
1844 fol = read_cnt_string(fd, 2, &cnt);
1845 if (cnt == FAIL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001846 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001847 vim_free(p);
Bram Moolenaar7887d882005-07-01 22:33:52 +00001848 goto endFAIL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001849 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00001850
1851 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
1852 if (p != NULL && fol != NULL)
1853 i = set_spell_charflags(p, fol);
1854
1855 vim_free(p);
1856 vim_free(fol);
1857
1858 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
1859 if ((p == NULL) != (fol == NULL))
1860 goto formerr;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001861
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001862 /* <midwordlen> <midword> */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001863 lp->sl_midword = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar7887d882005-07-01 22:33:52 +00001864 if (cnt == FAIL)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001865 goto endFAIL;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001866
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001867 /* <prefcondcnt> <prefcond> ... */
1868 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
1869 if (cnt > 0)
1870 {
1871 lp->sl_prefprog = (regprog_T **)alloc_clear(
1872 (unsigned)sizeof(regprog_T *) * cnt);
1873 if (lp->sl_prefprog == NULL)
1874 goto endFAIL;
1875 lp->sl_prefixcnt = cnt;
1876
1877 for (i = 0; i < cnt; ++i)
1878 {
1879 /* <prefcond> : <condlen> <condstr> */
1880 n = getc(fd); /* <condlen> */
1881 if (n < 0)
1882 goto formerr;
1883 /* When <condlen> is zero we have an empty condition. Otherwise
1884 * compile the regexp program used to check for the condition. */
1885 if (n > 0)
1886 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001887 buf[0] = '^'; /* always match at one position only */
1888 p = buf + 1;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001889 while (n-- > 0)
1890 *p++ = getc(fd); /* <condstr> */
1891 *p = NUL;
1892 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
1893 }
1894 }
1895 }
1896
1897
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001898 /* <SUGGEST> : <repcount> <rep> ...
1899 * <salflags> <salcount> <sal> ...
1900 * <maplen> <mapstr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001901
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001902 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
1903 if (cnt < 0)
1904 goto formerr;
1905
1906 gap = &lp->sl_rep;
1907 if (ga_grow(gap, cnt) == FAIL)
1908 goto endFAIL;
1909
1910 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
1911 for (; gap->ga_len < cnt; ++gap->ga_len)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001912 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001913 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
Bram Moolenaar7887d882005-07-01 22:33:52 +00001914 ftp->ft_from = read_cnt_string(fd, 1, &i);
1915 if (i == FAIL)
1916 goto endFAIL;
1917 ftp->ft_to = read_cnt_string(fd, 1, &i);
1918 if (i == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001919 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00001920 vim_free(ftp->ft_from);
1921 goto endFAIL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001922 }
1923 }
1924
1925 /* Fill the first-index table. */
1926 first = lp->sl_rep_first;
1927 for (i = 0; i < 256; ++i)
1928 first[i] = -1;
1929 for (i = 0; i < gap->ga_len; ++i)
1930 {
1931 ftp = &((fromto_T *)gap->ga_data)[i];
1932 if (first[*ftp->ft_from] == -1)
1933 first[*ftp->ft_from] = i;
1934 }
1935
1936 i = getc(fd); /* <salflags> */
1937 if (i & SAL_F0LLOWUP)
1938 lp->sl_followup = TRUE;
1939 if (i & SAL_COLLAPSE)
1940 lp->sl_collapse = TRUE;
1941 if (i & SAL_REM_ACCENTS)
1942 lp->sl_rem_accents = TRUE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001943 if (i & SAL_SOFO)
1944 lp->sl_sofo = TRUE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001945
1946 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
1947 if (cnt < 0)
1948 goto formerr;
1949
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001950 if (lp->sl_sofo)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001951 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001952 /*
1953 * SOFOFROM and SOFOTO items come in one <salfrom> and <salto>
1954 */
1955 if (cnt != 1)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001956 goto formerr;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001957
Bram Moolenaar7887d882005-07-01 22:33:52 +00001958 /* <salfromlen> <salfrom> */
1959 bp = read_cnt_string(fd, 2, &cnt);
1960 if (cnt == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001961 goto endFAIL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001962
Bram Moolenaar7887d882005-07-01 22:33:52 +00001963 /* <saltolen> <salto> */
1964 fol = read_cnt_string(fd, 2, &cnt);
1965 if (cnt == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001966 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001967 vim_free(bp);
1968 goto endFAIL;
1969 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001970
Bram Moolenaar7887d882005-07-01 22:33:52 +00001971 /* Store the info in lp->sl_sal and/or lp->sl_sal_first. */
1972 i = set_sofo(lp, bp, fol);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001973
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001974 vim_free(bp);
1975 vim_free(fol);
Bram Moolenaar7887d882005-07-01 22:33:52 +00001976 if (i == FAIL)
1977 goto formerr;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001978 }
1979 else
1980 {
1981 /*
1982 * SAL items
1983 */
1984 gap = &lp->sl_sal;
1985 if (ga_grow(gap, cnt) == FAIL)
1986 goto endFAIL;
1987
1988 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
1989 for (; gap->ga_len < cnt; ++gap->ga_len)
1990 {
1991 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
1992 ccnt = getc(fd); /* <salfromlen> */
1993 if (ccnt < 0)
1994 goto formerr;
1995 if ((p = alloc(ccnt + 2)) == NULL)
1996 goto endFAIL;
1997 smp->sm_lead = p;
1998
1999 /* Read up to the first special char into sm_lead. */
2000 for (i = 0; i < ccnt; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002001 {
2002 c = getc(fd); /* <salfrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002003 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002004 break;
2005 *p++ = c;
2006 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002007 smp->sm_leadlen = p - smp->sm_lead;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002008 *p++ = NUL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002009
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002010 /* Put (abc) chars in sm_oneof, if any. */
2011 if (c == '(')
2012 {
2013 smp->sm_oneof = p;
2014 for (++i; i < ccnt; ++i)
2015 {
2016 c = getc(fd); /* <salfrom> */
2017 if (c == ')')
2018 break;
2019 *p++ = c;
2020 }
2021 *p++ = NUL;
2022 if (++i < ccnt)
2023 c = getc(fd);
2024 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002025 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002026 smp->sm_oneof = NULL;
2027
2028 /* Any following chars go in sm_rules. */
2029 smp->sm_rules = p;
2030 if (i < ccnt)
2031 /* store the char we got while checking for end of sm_lead */
2032 *p++ = c;
2033 for (++i; i < ccnt; ++i)
2034 *p++ = getc(fd); /* <salfrom> */
2035 *p++ = NUL;
2036
Bram Moolenaar7887d882005-07-01 22:33:52 +00002037 /* <saltolen> <salto> */
2038 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
2039 if (ccnt == FAIL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002040 {
2041 vim_free(smp->sm_lead);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002042 goto formerr;
2043 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002044
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002045#ifdef FEAT_MBYTE
2046 if (has_mbyte)
2047 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002048 /* convert the multi-byte strings to wide char strings */
2049 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2050 smp->sm_leadlen = mb_charlen(smp->sm_lead);
2051 if (smp->sm_oneof == NULL)
2052 smp->sm_oneof_w = NULL;
2053 else
2054 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
2055 smp->sm_to_w = mb_str2wide(smp->sm_to);
2056 if (smp->sm_lead_w == NULL
2057 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
2058 || smp->sm_to_w == NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002059 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002060 vim_free(smp->sm_lead);
2061 vim_free(smp->sm_to);
2062 vim_free(smp->sm_lead_w);
2063 vim_free(smp->sm_oneof_w);
2064 vim_free(smp->sm_to_w);
2065 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002066 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002067 }
2068#endif
2069 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002070
2071 /* Fill the first-index table. */
Bram Moolenaar7887d882005-07-01 22:33:52 +00002072 set_sal_first(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002073 }
2074
Bram Moolenaar7887d882005-07-01 22:33:52 +00002075 /* <maplen> <mapstr> */
2076 p = read_cnt_string(fd, 2, &cnt);
2077 if (cnt == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002078 goto endFAIL;
Bram Moolenaarea424162005-06-16 21:51:00 +00002079 set_map_str(lp, p);
2080 vim_free(p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002081
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002082
Bram Moolenaar51485f02005-06-04 21:55:20 +00002083 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002084 * round 2: <KWORDTREE>
2085 * round 3: <PREFIXTREE> */
2086 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002087 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002088 /* The tree size was computed when writing the file, so that we can
2089 * allocate it as one long block. <nodecount> */
2090 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2091 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002092 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002093 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002094 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002095 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002096 bp = lalloc((long_u)len, TRUE);
2097 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002098 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002099 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002100 lp->sl_fbyts = bp;
2101 else if (round == 2)
2102 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002103 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002104 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002105
2106 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002107 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
2108 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002109 goto endFAIL;
2110 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002111 lp->sl_fidxs = ip;
2112 else if (round == 2)
2113 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002114 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002115 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002116
2117 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002118 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002119 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002120 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002121 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002122 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002123 }
2124 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002125
Bram Moolenaarb765d632005-06-07 21:00:02 +00002126 /* For a new file link it in the list of spell files. */
2127 if (old_lp == NULL)
2128 {
2129 lp->sl_next = first_lang;
2130 first_lang = lp;
2131 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002132
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002133 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002134
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002135endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002136 if (lang != NULL)
2137 /* truncating the name signals the error to spell_load_lang() */
2138 *lang = NUL;
2139 if (lp != NULL && old_lp == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002140 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002141 slang_free(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002142 lp = NULL;
2143 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002144
2145endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002146 if (fd != NULL)
2147 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002148 sourcing_name = save_sourcing_name;
2149 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002150
2151 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002152}
2153
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002154/*
2155 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002156 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002157 * Returns NULL when the count is zero.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002158 * Sets "*errp" to FAIL when there is an error, OK otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002159 */
2160 static char_u *
2161read_cnt_string(fd, cnt_bytes, errp)
2162 FILE *fd;
2163 int cnt_bytes;
2164 int *errp;
2165{
2166 int cnt = 0;
2167 int i;
2168 char_u *str;
2169
2170 /* read the length bytes, MSB first */
2171 for (i = 0; i < cnt_bytes; ++i)
2172 cnt = (cnt << 8) + getc(fd);
2173 if (cnt < 0)
2174 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00002175 EMSG(_(e_spell_trunc));
2176 *errp = FAIL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002177 return NULL;
2178 }
2179
2180 /* allocate memory */
2181 str = alloc((unsigned)cnt + 1);
2182 if (str == NULL)
2183 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00002184 *errp = FAIL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002185 return NULL;
2186 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00002187 *errp = OK;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002188
2189 /* Read the string. Doesn't check for truncated file. */
2190 for (i = 0; i < cnt; ++i)
2191 str[i] = getc(fd);
2192 str[i] = NUL;
2193
2194 return str;
2195}
2196
Bram Moolenaar7887d882005-07-01 22:33:52 +00002197/*
2198 * Set the SOFOFROM and SOFOTO items in language "lp".
2199 * Returns FAIL when there is something wrong.
2200 */
2201 static int
2202set_sofo(lp, from, to)
2203 slang_T *lp;
2204 char_u *from;
2205 char_u *to;
2206{
2207 int i;
2208
2209#ifdef FEAT_MBYTE
2210 garray_T *gap;
2211 char_u *s;
2212 char_u *p;
2213 int c;
2214 int *inp;
2215
2216 if (has_mbyte)
2217 {
2218 /* Use "sl_sal" as an array with 256 pointers to a list of wide
2219 * characters. The index is the low byte of the character.
2220 * The list contains from-to pairs with a terminating NUL.
2221 * sl_sal_first[] is used for latin1 "from" characters. */
2222 gap = &lp->sl_sal;
2223 ga_init2(gap, sizeof(int *), 1);
2224 if (ga_grow(gap, 256) == FAIL)
2225 return FAIL;
2226 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
2227 gap->ga_len = 256;
2228
2229 /* First count the number of items for each list. Temporarily use
2230 * sl_sal_first[] for this. */
2231 for (p = from, s = to; *p != NUL && *s != NUL; )
2232 {
2233 c = mb_ptr2char_adv(&p);
2234 mb_ptr_adv(s);
2235 if (c >= 256)
2236 ++lp->sl_sal_first[c & 0xff];
2237 }
2238 if (*p != NUL || *s != NUL) /* lengths differ */
2239 return FAIL;
2240
2241 /* Allocate the lists. */
2242 for (i = 0; i < 256; ++i)
2243 if (lp->sl_sal_first[i] > 0)
2244 {
2245 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
2246 if (p == NULL)
2247 return FAIL;
2248 ((int **)gap->ga_data)[i] = (int *)p;
2249 *(int *)p = 0;
2250 }
2251
2252 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
2253 * list. */
2254 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
2255 for (p = from, s = to; *p != NUL && *s != NUL; )
2256 {
2257 c = mb_ptr2char_adv(&p);
2258 i = mb_ptr2char_adv(&s);
2259 if (c >= 256)
2260 {
2261 /* Append the from-to chars at the end of the list with
2262 * the low byte. */
2263 inp = ((int **)gap->ga_data)[c & 0xff];
2264 while (*inp != 0)
2265 ++inp;
2266 *inp++ = c; /* from char */
2267 *inp++ = i; /* to char */
2268 *inp++ = NUL; /* NUL at the end */
2269 }
2270 else
2271 /* mapping byte to char is done in sl_sal_first[] */
2272 lp->sl_sal_first[c] = i;
2273 }
2274 }
2275 else
2276#endif
2277 {
2278 /* mapping bytes to bytes is done in sl_sal_first[] */
2279 if (STRLEN(from) != STRLEN(to))
2280 return FAIL;
2281
2282 for (i = 0; to[i] != NUL; ++i)
2283 lp->sl_sal_first[from[i]] = to[i];
2284 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
2285 }
2286
2287 return OK;
2288}
2289
2290/*
2291 * Fill the first-index table for "lp".
2292 */
2293 static void
2294set_sal_first(lp)
2295 slang_T *lp;
2296{
2297 salfirst_T *sfirst;
2298 int i;
2299 salitem_T *smp;
2300 int c;
2301 garray_T *gap = &lp->sl_sal;
2302
2303 sfirst = lp->sl_sal_first;
2304 for (i = 0; i < 256; ++i)
2305 sfirst[i] = -1;
2306 smp = (salitem_T *)gap->ga_data;
2307 for (i = 0; i < gap->ga_len; ++i)
2308 {
2309#ifdef FEAT_MBYTE
2310 if (has_mbyte)
2311 /* Use the lowest byte of the first character. For latin1 it's
2312 * the character, for other encodings it should differ for most
2313 * characters. */
2314 c = *smp[i].sm_lead_w & 0xff;
2315 else
2316#endif
2317 c = *smp[i].sm_lead;
2318 if (sfirst[c] == -1)
2319 {
2320 sfirst[c] = i;
2321#ifdef FEAT_MBYTE
2322 if (has_mbyte)
2323 {
2324 int n;
2325
2326 /* Make sure all entries with this byte are following each
2327 * other. Move the ones that are in the wrong position. Do
2328 * keep the same ordering! */
2329 while (i + 1 < gap->ga_len
2330 && (*smp[i + 1].sm_lead_w & 0xff) == c)
2331 /* Skip over entry with same index byte. */
2332 ++i;
2333
2334 for (n = 1; i + n < gap->ga_len; ++n)
2335 if ((*smp[i + n].sm_lead_w & 0xff) == c)
2336 {
2337 salitem_T tsal;
2338
2339 /* Move entry with same index byte after the entries
2340 * we already found. */
2341 ++i;
2342 --n;
2343 tsal = smp[i + n];
2344 mch_memmove(smp + i + 1, smp + i,
2345 sizeof(salitem_T) * n);
2346 smp[i] = tsal;
2347 }
2348 }
2349#endif
2350 }
2351 }
2352}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002353
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002354#ifdef FEAT_MBYTE
2355/*
2356 * Turn a multi-byte string into a wide character string.
2357 * Return it in allocated memory (NULL for out-of-memory)
2358 */
2359 static int *
2360mb_str2wide(s)
2361 char_u *s;
2362{
2363 int *res;
2364 char_u *p;
2365 int i = 0;
2366
2367 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
2368 if (res != NULL)
2369 {
2370 for (p = s; *p != NUL; )
2371 res[i++] = mb_ptr2char_adv(&p);
2372 res[i] = NUL;
2373 }
2374 return res;
2375}
2376#endif
2377
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002378/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002379 * Read one row of siblings from the spell file and store it in the byte array
2380 * "byts" and index array "idxs". Recursively read the children.
2381 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00002382 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00002383 *
2384 * Returns the index follosing the siblings.
2385 * Returns -1 if the file is shorter than expected.
2386 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002387 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002388 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002389read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002390 FILE *fd;
2391 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002392 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002393 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002394 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002395 int prefixtree; /* TRUE for reading PREFIXTREE */
2396 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002397{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002398 int len;
2399 int i;
2400 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002401 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002402 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002403 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002404#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002405
Bram Moolenaar51485f02005-06-04 21:55:20 +00002406 len = getc(fd); /* <siblingcount> */
2407 if (len <= 0)
2408 return -1;
2409
2410 if (startidx + len >= maxidx)
2411 return -2;
2412 byts[idx++] = len;
2413
2414 /* Read the byte values, flag/region bytes and shared indexes. */
2415 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002416 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002417 c = getc(fd); /* <byte> */
2418 if (c < 0)
2419 return -1;
2420 if (c <= BY_SPECIAL)
2421 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002422 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002423 {
2424 /* No flags, all regions. */
2425 idxs[idx] = 0;
2426 c = 0;
2427 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002428 else if (c == BY_FLAGS || c == BY_NOFLAGS)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002429 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002430 if (prefixtree)
2431 {
2432 /* Read the prefix ID and the condition nr. In idxs[]
2433 * store the prefix ID in the low byte, the condition
2434 * index shifted up 8 bits. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002435 c2 = getc(fd); /* <prefixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002436 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
2437 if (n >= maxprefcondnr)
2438 return -2;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002439 c2 += (n << 8);
2440 if (c == BY_NOFLAGS)
2441 c = c2;
2442 else
2443 c = c2 | WF_RAREPFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002444 }
2445 else
2446 {
2447 /* Read flags and optional region and prefix ID. In
2448 * idxs[] the flags go in the low byte, region above that
2449 * and prefix ID above the region. */
2450 c = getc(fd); /* <flags> */
2451 if (c & WF_REGION)
2452 c = (getc(fd) << 8) + c; /* <region> */
2453 if (c & WF_PFX)
2454 c = (getc(fd) << 16) + c; /* <prefixID> */
2455 }
2456
Bram Moolenaar51485f02005-06-04 21:55:20 +00002457 idxs[idx] = c;
2458 c = 0;
2459 }
2460 else /* c == BY_INDEX */
2461 {
2462 /* <nodeidx> */
2463 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2464 if (n < 0 || n >= maxidx)
2465 return -2;
2466 idxs[idx] = n + SHARED_MASK;
2467 c = getc(fd); /* <xbyte> */
2468 }
2469 }
2470 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002471 }
2472
Bram Moolenaar51485f02005-06-04 21:55:20 +00002473 /* Recursively read the children for non-shared siblings.
2474 * Skip the end-of-word ones (zero byte value) and the shared ones (and
2475 * remove SHARED_MASK) */
2476 for (i = 1; i <= len; ++i)
2477 if (byts[startidx + i] != 0)
2478 {
2479 if (idxs[startidx + i] & SHARED_MASK)
2480 idxs[startidx + i] &= ~SHARED_MASK;
2481 else
2482 {
2483 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002484 idx = read_tree(fd, byts, idxs, maxidx, idx,
2485 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002486 if (idx < 0)
2487 break;
2488 }
2489 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002490
Bram Moolenaar51485f02005-06-04 21:55:20 +00002491 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002492}
2493
2494/*
2495 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002496 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002497 */
2498 char_u *
2499did_set_spelllang(buf)
2500 buf_T *buf;
2501{
2502 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002503 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002504 char_u *region;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002505 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002506 int region_mask;
2507 slang_T *lp;
2508 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002509 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002510 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002511 int len;
2512 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002513 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002514 char_u *spf;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002515
2516 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002517 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002518
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002519 /* loop over comma separated language names. */
2520 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002521 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002522 /* Get one language name. */
2523 copy_option_part(&splp, lang, MAXWLEN, ",");
2524
Bram Moolenaar5482f332005-04-17 20:18:43 +00002525 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002526 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002527
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002528 /* If the name ends in ".spl" use it as the name of the spell file.
2529 * If there is a region name let "region" point to it and remove it
2530 * from the name. */
2531 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2532 {
2533 filename = TRUE;
2534
2535 /* Check if we loaded this language before. */
2536 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2537 if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME)
2538 break;
2539 }
2540 else
2541 {
2542 filename = FALSE;
2543 if (len > 3 && lang[len - 3] == '_')
2544 {
2545 region = lang + len - 2;
2546 len -= 3;
2547 lang[len] = NUL;
2548 }
2549
2550 /* Check if we loaded this language before. */
2551 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2552 if (STRICMP(lang, lp->sl_name) == 0)
2553 break;
2554 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002555
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002556 /* If not found try loading the language now. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002557 if (lp == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002558 {
2559 if (filename)
2560 (void)spell_load_file(lang, lang, NULL, FALSE);
2561 else
2562 spell_load_lang(lang);
2563 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002564
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002565 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002566 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002567 */
2568 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002569 if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME
2570 : STRICMP(lang, lp->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002571 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002572 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002573 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002574 {
2575 /* find region in sl_regions */
2576 c = find_region(lp->sl_regions, region);
2577 if (c == REGION_ALL)
2578 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002579 if (!lp->sl_add)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002580 smsg((char_u *)
2581 _("Warning: region %s not supported"),
2582 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002583 }
2584 else
2585 region_mask = 1 << c;
2586 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002587
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002588 if (ga_grow(&ga, 1) == FAIL)
2589 {
2590 ga_clear(&ga);
2591 return e_outofmem;
2592 }
2593 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2594 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2595 ++ga.ga_len;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002596 use_midword(lp, buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002597 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002598 }
2599
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002600 /* round 0: load int_wordlist, if possible.
2601 * round 1: load first name in 'spellfile'.
2602 * round 2: load second name in 'spellfile.
2603 * etc. */
2604 spf = curbuf->b_p_spf;
2605 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002606 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002607 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002608 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002609 /* Internal wordlist, if there is one. */
2610 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002611 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002612 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002613 }
2614 else
2615 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002616 /* One entry in 'spellfile'. */
2617 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
2618 STRCAT(spf_name, ".spl");
2619
2620 /* If it was already found above then skip it. */
2621 for (c = 0; c < ga.ga_len; ++c)
2622 if (fullpathcmp(spf_name,
2623 LANGP_ENTRY(ga, c)->lp_slang->sl_fname,
2624 FALSE) == FPC_SAME)
2625 break;
2626 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002627 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002628 }
2629
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002630 /* Check if it was loaded already. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002631 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2632 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
2633 break;
2634 if (lp == NULL)
2635 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002636 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002637 * region name, the region is ignored otherwise. for int_wordlist
2638 * use an arbitrary name. */
2639 if (round == 0)
2640 STRCPY(lang, "internal wordlist");
2641 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00002642 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002643 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002644 p = vim_strchr(lang, '.');
2645 if (p != NULL)
2646 *p = NUL; /* truncate at ".encoding.add" */
2647 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002648 lp = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002649 }
2650 if (lp != NULL && ga_grow(&ga, 1) == OK)
2651 {
2652 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2653 LANGP_ENTRY(ga, ga.ga_len)->lp_region = REGION_ALL;
2654 ++ga.ga_len;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002655 use_midword(lp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002656 }
2657 }
2658
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002659 /* Add a NULL entry to mark the end of the list. */
2660 if (ga_grow(&ga, 1) == FAIL)
2661 {
2662 ga_clear(&ga);
2663 return e_outofmem;
2664 }
2665 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
2666 ++ga.ga_len;
2667
2668 /* Everything is fine, store the new b_langp value. */
2669 ga_clear(&buf->b_langp);
2670 buf->b_langp = ga;
2671
2672 return NULL;
2673}
2674
2675/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002676 * Clear the midword characters for buffer "buf".
2677 */
2678 static void
2679clear_midword(buf)
2680 buf_T *buf;
2681{
2682 vim_memset(buf->b_spell_ismw, 0, 256);
2683#ifdef FEAT_MBYTE
2684 vim_free(buf->b_spell_ismw_mb);
2685 buf->b_spell_ismw_mb = NULL;
2686#endif
2687}
2688
2689/*
2690 * Use the "sl_midword" field of language "lp" for buffer "buf".
2691 * They add up to any currently used midword characters.
2692 */
2693 static void
2694use_midword(lp, buf)
2695 slang_T *lp;
2696 buf_T *buf;
2697{
2698 char_u *p;
2699
2700 for (p = lp->sl_midword; *p != NUL; )
2701#ifdef FEAT_MBYTE
2702 if (has_mbyte)
2703 {
2704 int c, l, n;
2705 char_u *bp;
2706
2707 c = mb_ptr2char(p);
2708 l = mb_ptr2len_check(p);
2709 if (c < 256)
2710 buf->b_spell_ismw[c] = TRUE;
2711 else if (buf->b_spell_ismw_mb == NULL)
2712 /* First multi-byte char in "b_spell_ismw_mb". */
2713 buf->b_spell_ismw_mb = vim_strnsave(p, l);
2714 else
2715 {
2716 /* Append multi-byte chars to "b_spell_ismw_mb". */
2717 n = STRLEN(buf->b_spell_ismw_mb);
2718 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
2719 if (bp != NULL)
2720 {
2721 vim_free(buf->b_spell_ismw_mb);
2722 buf->b_spell_ismw_mb = bp;
2723 vim_strncpy(bp + n, p, l);
2724 }
2725 }
2726 p += l;
2727 }
2728 else
2729#endif
2730 buf->b_spell_ismw[*p++] = TRUE;
2731}
2732
2733/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002734 * Find the region "region[2]" in "rp" (points to "sl_regions").
2735 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002736 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002737 */
2738 static int
2739find_region(rp, region)
2740 char_u *rp;
2741 char_u *region;
2742{
2743 int i;
2744
2745 for (i = 0; ; i += 2)
2746 {
2747 if (rp[i] == NUL)
2748 return REGION_ALL;
2749 if (rp[i] == region[0] && rp[i + 1] == region[1])
2750 break;
2751 }
2752 return i / 2;
2753}
2754
2755/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002756 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002757 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002758 * Word WF_ONECAP
2759 * W WORD WF_ALLCAP
2760 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002761 */
2762 static int
2763captype(word, end)
2764 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002765 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002766{
2767 char_u *p;
2768 int c;
2769 int firstcap;
2770 int allcap;
2771 int past_second = FALSE; /* past second word char */
2772
2773 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002774 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002775 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002776 return 0; /* only non-word characters, illegal word */
2777#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00002778 if (has_mbyte)
2779 c = mb_ptr2char_adv(&p);
2780 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002781#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00002782 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002783 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002784
2785 /*
2786 * Need to check all letters to find a word with mixed upper/lower.
2787 * But a word with an upper char only at start is a ONECAP.
2788 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002789 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002790 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002791 {
2792#ifdef FEAT_MBYTE
2793 c = mb_ptr2char(p);
2794#else
2795 c = *p;
2796#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002797 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002798 {
2799 /* UUl -> KEEPCAP */
2800 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002801 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002802 allcap = FALSE;
2803 }
2804 else if (!allcap)
2805 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002806 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002807 past_second = TRUE;
2808 }
2809
2810 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002811 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002812 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002813 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002814 return 0;
2815}
2816
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002817# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
2818/*
2819 * Free all languages.
2820 */
2821 void
2822spell_free_all()
2823{
2824 slang_T *lp;
2825 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002826 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002827
2828 /* Go through all buffers and handle 'spelllang'. */
2829 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2830 ga_clear(&buf->b_langp);
2831
2832 while (first_lang != NULL)
2833 {
2834 lp = first_lang;
2835 first_lang = lp->sl_next;
2836 slang_free(lp);
2837 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002838
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002839 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002840 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002841 /* Delete the internal wordlist and its .spl file */
2842 mch_remove(int_wordlist);
2843 int_wordlist_spl(fname);
2844 mch_remove(fname);
2845 vim_free(int_wordlist);
2846 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002847 }
2848
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002849 init_spell_chartab();
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002850}
2851# endif
2852
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002853# if defined(FEAT_MBYTE) || defined(PROTO)
2854/*
2855 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002856 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002857 */
2858 void
2859spell_reload()
2860{
2861 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002862 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002863
Bram Moolenaarea408852005-06-25 22:49:46 +00002864 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002865 init_spell_chartab();
2866
2867 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002868 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002869
2870 /* Go through all buffers and handle 'spelllang'. */
2871 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2872 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002873 /* Only load the wordlists when 'spelllang' is set and there is a
2874 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002875 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002876 {
2877 FOR_ALL_WINDOWS(wp)
2878 if (wp->w_buffer == buf && wp->w_p_spell)
2879 {
2880 (void)did_set_spelllang(buf);
2881# ifdef FEAT_WINDOWS
2882 break;
2883# endif
2884 }
2885 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002886 }
2887}
2888# endif
2889
Bram Moolenaarb765d632005-06-07 21:00:02 +00002890/*
2891 * Reload the spell file "fname" if it's loaded.
2892 */
2893 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002894spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002895 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002896 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002897{
2898 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002899 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002900
Bram Moolenaarb765d632005-06-07 21:00:02 +00002901 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2902 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
2903 {
2904 slang_clear(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002905 (void)spell_load_file(fname, NULL, lp, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002906 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002907 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002908 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002909
2910 /* When "zg" was used and the file wasn't loaded yet, should redo
2911 * 'spelllang' to get it loaded. */
2912 if (added_word && !didit)
2913 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002914}
2915
2916
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002917/*
2918 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002919 */
2920
Bram Moolenaar51485f02005-06-04 21:55:20 +00002921#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002922 and .dic file. */
2923/*
2924 * Main structure to store the contents of a ".aff" file.
2925 */
2926typedef struct afffile_S
2927{
2928 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002929 int af_rar; /* RAR ID for rare word */
2930 int af_kep; /* KEP ID for keep-case word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00002931 int af_bad; /* BAD ID for banned word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002932 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002933 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
2934 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002935} afffile_T;
2936
2937typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002938/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
2939struct affentry_S
2940{
2941 affentry_T *ae_next; /* next affix with same name/number */
2942 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
2943 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002944 char_u *ae_cond; /* condition (NULL for ".") */
2945 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002946 int ae_rare; /* rare affix */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002947};
2948
2949/* Affix header from ".aff" file. Used for af_pref and af_suff. */
2950typedef struct affheader_S
2951{
2952 char_u ah_key[2]; /* key for hashtable == name of affix entry */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002953 int ah_newID; /* prefix ID after renumbering */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002954 int ah_combine; /* suffix may combine with prefix */
2955 affentry_T *ah_first; /* first affix entry */
2956} affheader_T;
2957
2958#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
2959
2960/*
2961 * Structure that is used to store the items in the word tree. This avoids
2962 * the need to keep track of each allocated thing, it's freed all at once
2963 * after ":mkspell" is done.
2964 */
2965#define SBLOCKSIZE 16000 /* size of sb_data */
2966typedef struct sblock_S sblock_T;
2967struct sblock_S
2968{
2969 sblock_T *sb_next; /* next block in list */
2970 int sb_used; /* nr of bytes already in use */
2971 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002972};
2973
2974/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002975 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002976 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002977typedef struct wordnode_S wordnode_T;
2978struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002979{
Bram Moolenaar0c405862005-06-22 22:26:26 +00002980 union /* shared to save space */
2981 {
2982 char_u hashkey[6]; /* room for the hash key */
2983 int index; /* index in written nodes (valid after first
2984 round) */
2985 } wn_u1;
2986 union /* shared to save space */
2987 {
2988 wordnode_T *next; /* next node with same hash key */
2989 wordnode_T *wnode; /* parent node that will write this node */
2990 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002991 wordnode_T *wn_child; /* child (next byte in word) */
2992 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
2993 always sorted) */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002994 char_u wn_byte; /* Byte for this node. NUL for word end */
2995 char_u wn_flags; /* when wn_byte is NUL: WF_ flags */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002996 short wn_region; /* when wn_byte is NUL: region mask; for
2997 PREFIXTREE it's the prefcondnr */
2998 char_u wn_prefixID; /* supported/required prefix ID or 0 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002999};
3000
Bram Moolenaar51485f02005-06-04 21:55:20 +00003001#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003002
Bram Moolenaar51485f02005-06-04 21:55:20 +00003003/*
3004 * Info used while reading the spell files.
3005 */
3006typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003007{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003008 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003009 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003010 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003011 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003012 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003013 sblock_T *si_blocks; /* memory blocks used */
3014 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003015 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003016 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003017 int si_region; /* region mask */
3018 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003019 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003020 int si_verbose; /* verbose messages */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003021 int si_region_count; /* number of regions supported (1 when there
3022 are no regions) */
3023 char_u si_region_name[16]; /* region names (if count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003024
3025 garray_T si_rep; /* list of fromto_T entries from REP lines */
3026 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003027 char_u *si_sofofr; /* SOFOFROM text */
3028 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003029 int si_followup; /* soundsalike: ? */
3030 int si_collapse; /* soundsalike: ? */
3031 int si_rem_accents; /* soundsalike: remove accents */
3032 garray_T si_map; /* MAP info concatenated */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003033 char_u *si_midword; /* MIDWORD chars, alloc'ed string or NULL */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003034 garray_T si_prefcond; /* table with conditions for postponed
3035 * prefixes, each stored as a string */
3036 int si_newID; /* current value for ah_newID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003037} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003038
Bram Moolenaar51485f02005-06-04 21:55:20 +00003039static afffile_T *spell_read_aff __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003040static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003041static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
3042static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00003043static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003044static void spell_free_aff __ARGS((afffile_T *aff));
3045static int spell_read_dic __ARGS((char_u *fname, spellinfo_T *spin, afffile_T *affile));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003046static char_u *get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, sblock_T **blp));
3047static 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 +00003048static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin));
3049static void *getroom __ARGS((sblock_T **blp, size_t len));
3050static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s));
3051static void free_blocks __ARGS((sblock_T *bl));
3052static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003053static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags, int region, char_u *pfxlist));
3054static 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 +00003055static void wordtree_compress __ARGS((wordnode_T *root, spellinfo_T *spin));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003056static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot));
3057static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar3982c542005-06-08 21:56:31 +00003058static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar0c405862005-06-22 22:26:26 +00003059static void clear_node __ARGS((wordnode_T *node));
3060static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003061static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00003062static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003063
3064/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003065 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00003066 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003067 */
3068 static afffile_T *
Bram Moolenaar51485f02005-06-04 21:55:20 +00003069spell_read_aff(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003070 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003071 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003072{
3073 FILE *fd;
3074 afffile_T *aff;
3075 char_u rline[MAXLINELEN];
3076 char_u *line;
3077 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003078#define MAXITEMCNT 7
3079 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003080 int itemcnt;
3081 char_u *p;
3082 int lnum = 0;
3083 affheader_T *cur_aff = NULL;
3084 int aff_todo = 0;
3085 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003086 char_u *low = NULL;
3087 char_u *fol = NULL;
3088 char_u *upp = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003089 static char *e_affname = N_("Affix name too long in %s line %d: %s");
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003090 int do_rep;
3091 int do_sal;
3092 int do_map;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003093 int do_midword;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003094 int do_sofo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003095 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003096 hashitem_T *hi;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003097
Bram Moolenaar51485f02005-06-04 21:55:20 +00003098 /*
3099 * Open the file.
3100 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003101 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003102 if (fd == NULL)
3103 {
3104 EMSG2(_(e_notopen), fname);
3105 return NULL;
3106 }
3107
Bram Moolenaarb765d632005-06-07 21:00:02 +00003108 if (spin->si_verbose || p_verbose > 2)
3109 {
3110 if (!spin->si_verbose)
3111 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003112 smsg((char_u *)_("Reading affix file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003113 out_flush();
3114 if (!spin->si_verbose)
3115 verbose_leave();
3116 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003117
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003118 /* Only do REP lines when not done in another .aff file already. */
3119 do_rep = spin->si_rep.ga_len == 0;
3120
3121 /* Only do SAL lines when not done in another .aff file already. */
3122 do_sal = spin->si_sal.ga_len == 0;
3123
3124 /* Only do MAP lines when not done in another .aff file already. */
3125 do_map = spin->si_map.ga_len == 0;
3126
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003127 /* Only do MIDWORD line when not done in another .aff file already */
3128 do_midword = spin->si_midword == NULL;
3129
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003130 /* Only do SOFOFROM and SOFOTO when not done in another .aff file already */
3131 do_sofo = spin->si_sofofr == NULL;
3132
Bram Moolenaar51485f02005-06-04 21:55:20 +00003133 /*
3134 * Allocate and init the afffile_T structure.
3135 */
3136 aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003137 if (aff == NULL)
3138 return NULL;
3139 hash_init(&aff->af_pref);
3140 hash_init(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003141
3142 /*
3143 * Read all the lines in the file one by one.
3144 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003145 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003146 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003147 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003148 ++lnum;
3149
3150 /* Skip comment lines. */
3151 if (*rline == '#')
3152 continue;
3153
3154 /* Convert from "SET" to 'encoding' when needed. */
3155 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003156#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003157 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003158 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003159 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003160 if (pc == NULL)
3161 {
3162 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3163 fname, lnum, rline);
3164 continue;
3165 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003166 line = pc;
3167 }
3168 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003169#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003170 {
3171 pc = NULL;
3172 line = rline;
3173 }
3174
3175 /* Split the line up in white separated items. Put a NUL after each
3176 * item. */
3177 itemcnt = 0;
3178 for (p = line; ; )
3179 {
3180 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
3181 ++p;
3182 if (*p == NUL)
3183 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003184 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003185 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003186 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003187 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003188 ++p;
3189 if (*p == NUL)
3190 break;
3191 *p++ = NUL;
3192 }
3193
3194 /* Handle non-empty lines. */
3195 if (itemcnt > 0)
3196 {
3197 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
3198 && aff->af_enc == NULL)
3199 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003200#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003201 /* Setup for conversion from "ENC" to 'encoding'. */
3202 aff->af_enc = enc_canonize(items[1]);
3203 if (aff->af_enc != NULL && !spin->si_ascii
3204 && convert_setup(&spin->si_conv, aff->af_enc,
3205 p_enc) == FAIL)
3206 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
3207 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003208 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003209#else
3210 smsg((char_u *)_("Conversion in %s not supported"), fname);
3211#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003212 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003213 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2)
3214 {
3215 if (do_midword)
3216 spin->si_midword = vim_strsave(items[1]);
3217 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00003218 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
3219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003220 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003221 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003222 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003223 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003224 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003225 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003226 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
3227 && aff->af_rar == 0)
3228 {
3229 aff->af_rar = items[1][0];
3230 if (items[1][1] != NUL)
3231 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3232 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00003233 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
3234 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003235 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003236 aff->af_kep = items[1][0];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003237 if (items[1][1] != NUL)
3238 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3239 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00003240 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
3241 && aff->af_bad == 0)
3242 {
3243 aff->af_bad = items[1][0];
3244 if (items[1][1] != NUL)
3245 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3246 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003247 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
3248 {
3249 aff->af_pfxpostpone = TRUE;
3250 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003251 else if ((STRCMP(items[0], "PFX") == 0
3252 || STRCMP(items[0], "SFX") == 0)
3253 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003254 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003255 {
Bram Moolenaar8db73182005-06-17 21:51:16 +00003256 /* Myspell allows extra text after the item, but that might
3257 * mean mistakes go unnoticed. Require a comment-starter. */
3258 if (itemcnt > 4 && *items[4] != '#')
3259 smsg((char_u *)_("Trailing text in %s line %d: %s"),
3260 fname, lnum, items[4]);
3261
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003262 /* New affix letter. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003263 cur_aff = (affheader_T *)getroom(&spin->si_blocks,
3264 sizeof(affheader_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003265 if (cur_aff == NULL)
3266 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003267 cur_aff->ah_key[0] = *items[1]; /* TODO: multi-byte? */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003268 cur_aff->ah_key[1] = NUL;
3269 if (items[1][1] != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003270 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003271 if (*items[2] == 'Y')
3272 cur_aff->ah_combine = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003273 else if (*items[2] != 'N')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003274 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
3275 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003276
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003277 if (*items[0] == 'P')
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003278 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003279 tp = &aff->af_pref;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003280 /* Use a new number in the .spl file later, to be able to
3281 * handle multiple .aff files. */
3282 if (aff->af_pfxpostpone)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003283 cur_aff->ah_newID = ++spin->si_newID;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003284 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003285 else
3286 tp = &aff->af_suff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003287 aff_todo = atoi((char *)items[3]);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003288 hi = hash_find(tp, cur_aff->ah_key);
3289 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar51485f02005-06-04 21:55:20 +00003290 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003291 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
3292 fname, lnum, items[1]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003293 aff_todo = 0;
3294 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003295 else
3296 hash_add(tp, cur_aff->ah_key);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003297 }
3298 else if ((STRCMP(items[0], "PFX") == 0
3299 || STRCMP(items[0], "SFX") == 0)
3300 && aff_todo > 0
3301 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003302 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003303 {
3304 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003305 int rare = FALSE;
3306 int lasti = 5;
3307
3308 /* Check for "rare" after the other info. */
3309 if (itemcnt > 5 && STRICMP(items[5], "rare") == 0)
3310 {
3311 rare = TRUE;
3312 lasti = 6;
3313 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003314
Bram Moolenaar8db73182005-06-17 21:51:16 +00003315 /* Myspell allows extra text after the item, but that might
3316 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003317 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar8db73182005-06-17 21:51:16 +00003318 smsg((char_u *)_("Trailing text in %s line %d: %s"),
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003319 fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00003320
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003321 /* New item for an affix letter. */
3322 --aff_todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003323 aff_entry = (affentry_T *)getroom(&spin->si_blocks,
3324 sizeof(affentry_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003325 if (aff_entry == NULL)
3326 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003327 aff_entry->ae_rare = rare;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003328
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003329 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003330 aff_entry->ae_chop = getroom_save(&spin->si_blocks,
3331 items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003332 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003333 aff_entry->ae_add = getroom_save(&spin->si_blocks,
3334 items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003335
Bram Moolenaar51485f02005-06-04 21:55:20 +00003336 /* Don't use an affix entry with non-ASCII characters when
3337 * "spin->si_ascii" is TRUE. */
3338 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00003339 || has_non_ascii(aff_entry->ae_add)))
3340 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003341 aff_entry->ae_next = cur_aff->ah_first;
3342 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003343
3344 if (STRCMP(items[4], ".") != 0)
3345 {
3346 char_u buf[MAXLINELEN];
3347
3348 aff_entry->ae_cond = getroom_save(&spin->si_blocks,
3349 items[4]);
3350 if (*items[0] == 'P')
3351 sprintf((char *)buf, "^%s", items[4]);
3352 else
3353 sprintf((char *)buf, "%s$", items[4]);
3354 aff_entry->ae_prog = vim_regcomp(buf,
3355 RE_MAGIC + RE_STRING);
3356 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003357
3358 /* For postponed prefixes we need an entry in si_prefcond
3359 * for the condition. Use an existing one if possible. */
3360 if (*items[0] == 'P' && aff->af_pfxpostpone
3361 && aff_entry->ae_chop == NULL)
3362 {
3363 int idx;
3364 char_u **pp;
3365
3366 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
3367 --idx)
3368 {
3369 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
3370 if (str_equal(p, aff_entry->ae_cond))
3371 break;
3372 }
3373 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
3374 {
3375 /* Not found, add a new condition. */
3376 idx = spin->si_prefcond.ga_len++;
3377 pp = ((char_u **)spin->si_prefcond.ga_data) + idx;
3378 if (aff_entry->ae_cond == NULL)
3379 *pp = NULL;
3380 else
3381 *pp = getroom_save(&spin->si_blocks,
3382 aff_entry->ae_cond);
3383 }
3384
3385 /* Add the prefix to the prefix tree. */
3386 if (aff_entry->ae_add == NULL)
3387 p = (char_u *)"";
3388 else
3389 p = aff_entry->ae_add;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003390 tree_add_word(p, spin->si_prefroot, rare ? -2 : -1,
3391 idx, cur_aff->ah_newID, &spin->si_blocks);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003392 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003393 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003394 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003395 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
3396 {
3397 if (fol != NULL)
3398 smsg((char_u *)_("Duplicate FOL in %s line %d"),
3399 fname, lnum);
3400 else
3401 fol = vim_strsave(items[1]);
3402 }
3403 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
3404 {
3405 if (low != NULL)
3406 smsg((char_u *)_("Duplicate LOW in %s line %d"),
3407 fname, lnum);
3408 else
3409 low = vim_strsave(items[1]);
3410 }
3411 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
3412 {
3413 if (upp != NULL)
3414 smsg((char_u *)_("Duplicate UPP in %s line %d"),
3415 fname, lnum);
3416 else
3417 upp = vim_strsave(items[1]);
3418 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003419 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003420 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003421 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003422 if (!isdigit(*items[1]))
3423 smsg((char_u *)_("Expected REP count in %s line %d"),
3424 fname, lnum);
3425 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003426 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
3427 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003428 /* REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003429 if (do_rep)
3430 add_fromto(spin, &spin->si_rep, items[1], items[2]);
3431 }
3432 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
3433 {
3434 /* MAP item or count */
3435 if (!found_map)
3436 {
3437 /* First line contains the count. */
3438 found_map = TRUE;
3439 if (!isdigit(*items[1]))
3440 smsg((char_u *)_("Expected MAP count in %s line %d"),
3441 fname, lnum);
3442 }
3443 else if (do_map)
3444 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003445 int c;
3446
3447 /* Check that every character appears only once. */
3448 for (p = items[1]; *p != NUL; )
3449 {
3450#ifdef FEAT_MBYTE
3451 c = mb_ptr2char_adv(&p);
3452#else
3453 c = *p++;
3454#endif
3455 if ((spin->si_map.ga_len > 0
3456 && vim_strchr(spin->si_map.ga_data, c)
3457 != NULL)
3458 || vim_strchr(p, c) != NULL)
3459 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
3460 fname, lnum);
3461 }
3462
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003463 /* We simply concatenate all the MAP strings, separated by
3464 * slashes. */
3465 ga_concat(&spin->si_map, items[1]);
3466 ga_append(&spin->si_map, '/');
3467 }
3468 }
3469 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
3470 {
3471 if (do_sal)
3472 {
3473 /* SAL item (sounds-a-like)
3474 * Either one of the known keys or a from-to pair. */
3475 if (STRCMP(items[1], "followup") == 0)
3476 spin->si_followup = sal_to_bool(items[2]);
3477 else if (STRCMP(items[1], "collapse_result") == 0)
3478 spin->si_collapse = sal_to_bool(items[2]);
3479 else if (STRCMP(items[1], "remove_accents") == 0)
3480 spin->si_rem_accents = sal_to_bool(items[2]);
3481 else
3482 /* when "to" is "_" it means empty */
3483 add_fromto(spin, &spin->si_sal, items[1],
3484 STRCMP(items[2], "_") == 0 ? (char_u *)""
3485 : items[2]);
3486 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003487 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003488 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
3489 && (!do_sofo || spin->si_sofofr == NULL))
3490 {
3491 if (do_sofo)
3492 spin->si_sofofr = vim_strsave(items[1]);
3493 }
3494 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
3495 && (!do_sofo || spin->si_sofoto == NULL))
3496 {
3497 if (do_sofo)
3498 spin->si_sofoto = vim_strsave(items[1]);
3499 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003500 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003501 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
3502 fname, lnum, items[0]);
3503 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003504 }
3505
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003506 if (do_sofo && (spin->si_sofofr == NULL) != (spin->si_sofoto == NULL))
3507 smsg((char_u *)_("Missing SOFO%s line in %s"),
3508 spin->si_sofofr == NULL ? "FROM" : "TO", fname);
3509 if (spin->si_sofofr != NULL && spin->si_sal.ga_len > 0)
3510 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
3511
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003512 if (fol != NULL || low != NULL || upp != NULL)
3513 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003514 if (spin->si_clear_chartab)
3515 {
3516 /* Clear the char type tables, don't want to use any of the
3517 * currently used spell properties. */
3518 init_spell_chartab();
3519 spin->si_clear_chartab = FALSE;
3520 }
3521
Bram Moolenaar3982c542005-06-08 21:56:31 +00003522 /*
3523 * Don't write a word table for an ASCII file, so that we don't check
3524 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003525 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00003526 * mb_get_class(), the list of chars in the file will be incomplete.
3527 */
3528 if (!spin->si_ascii
3529#ifdef FEAT_MBYTE
3530 && !enc_utf8
3531#endif
3532 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003533 {
3534 if (fol == NULL || low == NULL || upp == NULL)
3535 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
3536 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00003537 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003538 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003539
3540 vim_free(fol);
3541 vim_free(low);
3542 vim_free(upp);
3543 }
3544
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003545 vim_free(pc);
3546 fclose(fd);
3547 return aff;
3548}
3549
3550/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003551 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
3552 * NULL as equal.
3553 */
3554 static int
3555str_equal(s1, s2)
3556 char_u *s1;
3557 char_u *s2;
3558{
3559 if (s1 == NULL || s2 == NULL)
3560 return s1 == s2;
3561 return STRCMP(s1, s2) == 0;
3562}
3563
3564/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003565 * Add a from-to item to "gap". Used for REP and SAL items.
3566 * They are stored case-folded.
3567 */
3568 static void
3569add_fromto(spin, gap, from, to)
3570 spellinfo_T *spin;
3571 garray_T *gap;
3572 char_u *from;
3573 char_u *to;
3574{
3575 fromto_T *ftp;
3576 char_u word[MAXWLEN];
3577
3578 if (ga_grow(gap, 1) == OK)
3579 {
3580 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
3581 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
3582 ftp->ft_from = getroom_save(&spin->si_blocks, word);
3583 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
3584 ftp->ft_to = getroom_save(&spin->si_blocks, word);
3585 ++gap->ga_len;
3586 }
3587}
3588
3589/*
3590 * Convert a boolean argument in a SAL line to TRUE or FALSE;
3591 */
3592 static int
3593sal_to_bool(s)
3594 char_u *s;
3595{
3596 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
3597}
3598
3599/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00003600 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
3601 * When "s" is NULL FALSE is returned.
3602 */
3603 static int
3604has_non_ascii(s)
3605 char_u *s;
3606{
3607 char_u *p;
3608
3609 if (s != NULL)
3610 for (p = s; *p != NUL; ++p)
3611 if (*p >= 128)
3612 return TRUE;
3613 return FALSE;
3614}
3615
3616/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003617 * Free the structure filled by spell_read_aff().
3618 */
3619 static void
3620spell_free_aff(aff)
3621 afffile_T *aff;
3622{
3623 hashtab_T *ht;
3624 hashitem_T *hi;
3625 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003626 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003627 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003628
3629 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003630
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003631 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003632 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
3633 {
3634 todo = ht->ht_used;
3635 for (hi = ht->ht_array; todo > 0; ++hi)
3636 {
3637 if (!HASHITEM_EMPTY(hi))
3638 {
3639 --todo;
3640 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003641 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3642 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003643 }
3644 }
3645 if (ht == &aff->af_suff)
3646 break;
3647 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003648
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003649 hash_clear(&aff->af_pref);
3650 hash_clear(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003651}
3652
3653/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003654 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003655 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003656 */
3657 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00003658spell_read_dic(fname, spin, affile)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003659 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003660 spellinfo_T *spin;
3661 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003662{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003663 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003664 char_u line[MAXLINELEN];
Bram Moolenaar51485f02005-06-04 21:55:20 +00003665 char_u *afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003666 char_u *pfxlist;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003667 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003668 char_u *pc;
3669 char_u *w;
3670 int l;
3671 hash_T hash;
3672 hashitem_T *hi;
3673 FILE *fd;
3674 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003675 int non_ascii = 0;
3676 int retval = OK;
3677 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003678 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003679
Bram Moolenaar51485f02005-06-04 21:55:20 +00003680 /*
3681 * Open the file.
3682 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003683 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003684 if (fd == NULL)
3685 {
3686 EMSG2(_(e_notopen), fname);
3687 return FAIL;
3688 }
3689
Bram Moolenaar51485f02005-06-04 21:55:20 +00003690 /* The hashtable is only used to detect duplicated words. */
3691 hash_init(&ht);
3692
Bram Moolenaar8db73182005-06-17 21:51:16 +00003693 spin->si_foldwcount = 0;
3694 spin->si_keepwcount = 0;
3695
Bram Moolenaarb765d632005-06-07 21:00:02 +00003696 if (spin->si_verbose || p_verbose > 2)
3697 {
3698 if (!spin->si_verbose)
3699 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003700 smsg((char_u *)_("Reading dictionary file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003701 out_flush();
3702 if (!spin->si_verbose)
3703 verbose_leave();
3704 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003705
3706 /* Read and ignore the first line: word count. */
3707 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003708 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003709 EMSG2(_("E760: No word count in %s"), fname);
3710
3711 /*
3712 * Read all the lines in the file one by one.
3713 * The words are converted to 'encoding' here, before being added to
3714 * the hashtable.
3715 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003716 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003717 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003718 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003719 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003720 if (line[0] == '#')
3721 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003722
Bram Moolenaar51485f02005-06-04 21:55:20 +00003723 /* Remove CR, LF and white space from the end. White space halfway
3724 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003725 l = STRLEN(line);
3726 while (l > 0 && line[l - 1] <= ' ')
3727 --l;
3728 if (l == 0)
3729 continue; /* empty line */
3730 line[l] = NUL;
3731
Bram Moolenaar51485f02005-06-04 21:55:20 +00003732 /* Find the optional affix names. */
3733 afflist = vim_strchr(line, '/');
3734 if (afflist != NULL)
3735 *afflist++ = NUL;
3736
3737 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
3738 if (spin->si_ascii && has_non_ascii(line))
3739 {
3740 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003741 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003742 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003743
Bram Moolenaarb765d632005-06-07 21:00:02 +00003744#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003745 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003746 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003747 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003748 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003749 if (pc == NULL)
3750 {
3751 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3752 fname, lnum, line);
3753 continue;
3754 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003755 w = pc;
3756 }
3757 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003758#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003759 {
3760 pc = NULL;
3761 w = line;
3762 }
3763
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003764 /* This takes time, print a message now and then. */
3765 if (spin->si_verbose && (lnum & 0x3ff) == 0)
3766 {
3767 vim_snprintf((char *)message, sizeof(message),
3768 _("line %6d, word %6d - %s"),
3769 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
3770 msg_start();
3771 msg_puts_long_attr(message, 0);
3772 msg_clr_eos();
3773 msg_didout = FALSE;
3774 msg_col = 0;
3775 out_flush();
3776 }
3777
Bram Moolenaar51485f02005-06-04 21:55:20 +00003778 /* Store the word in the hashtable to be able to find duplicates. */
3779 dw = (char_u *)getroom_save(&spin->si_blocks, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003780 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003781 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003782 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003783 if (retval == FAIL)
3784 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003785
Bram Moolenaar51485f02005-06-04 21:55:20 +00003786 hash = hash_hash(dw);
3787 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003788 if (!HASHITEM_EMPTY(hi))
3789 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003790 fname, lnum, dw);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003791 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00003792 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003793
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003794 flags = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003795 pfxlist = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003796 if (afflist != NULL)
3797 {
3798 /* Check for affix name that stands for keep-case word and stands
3799 * for rare word (if defined). */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003800 if (affile->af_kep != NUL
3801 && vim_strchr(afflist, affile->af_kep) != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003802 flags |= WF_KEEPCAP;
3803 if (affile->af_rar != NUL
3804 && vim_strchr(afflist, affile->af_rar) != NULL)
3805 flags |= WF_RARE;
Bram Moolenaar0c405862005-06-22 22:26:26 +00003806 if (affile->af_bad != NUL
3807 && vim_strchr(afflist, affile->af_bad) != NULL)
3808 flags |= WF_BANNED;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003809
3810 if (affile->af_pfxpostpone)
3811 /* Need to store the list of prefix IDs with the word. */
3812 pfxlist = get_pfxlist(affile, afflist, &spin->si_blocks);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003813 }
3814
Bram Moolenaar51485f02005-06-04 21:55:20 +00003815 /* Add the word to the word tree(s). */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003816 if (store_word(dw, spin, flags, spin->si_region, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003817 retval = FAIL;
3818
3819 if (afflist != NULL)
3820 {
3821 /* Find all matching suffixes and add the resulting words.
3822 * Additionally do matching prefixes that combine. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003823 if (store_aff_word(dw, spin, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003824 &affile->af_suff, &affile->af_pref,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003825 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003826 retval = FAIL;
3827
3828 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003829 if (store_aff_word(dw, spin, afflist, affile,
3830 &affile->af_pref, NULL,
3831 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003832 retval = FAIL;
3833 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003834 }
3835
Bram Moolenaar51485f02005-06-04 21:55:20 +00003836 if (spin->si_ascii && non_ascii > 0)
3837 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
3838 non_ascii);
3839 hash_clear(&ht);
3840
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003841 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003842 return retval;
3843}
3844
3845/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003846 * Get the list of prefix IDs from the affix list "afflist".
3847 * Used for PFXPOSTPONE.
3848 * Returns a string allocated with getroom(). NULL when there are no prefixes
3849 * or when out of memory.
3850 */
3851 static char_u *
3852get_pfxlist(affile, afflist, blp)
3853 afffile_T *affile;
3854 char_u *afflist;
3855 sblock_T **blp;
3856{
3857 char_u *p;
3858 int cnt;
3859 int round;
3860 char_u *res = NULL;
3861 char_u key[2];
3862 hashitem_T *hi;
3863
3864 key[1] = NUL;
3865
3866 /* round 1: count the number of prefix IDs.
3867 * round 2: move prefix IDs to "res" */
3868 for (round = 1; round <= 2; ++round)
3869 {
3870 cnt = 0;
3871 for (p = afflist; *p != NUL; ++p)
3872 {
3873 key[0] = *p;
3874 hi = hash_find(&affile->af_pref, key);
3875 if (!HASHITEM_EMPTY(hi))
3876 {
3877 /* This is a prefix ID, use the new number. */
3878 if (round == 2)
3879 res[cnt] = HI2AH(hi)->ah_newID;
3880 ++cnt;
3881 }
3882 }
3883 if (round == 1 && cnt > 0)
3884 res = getroom(blp, cnt + 1);
3885 if (res == NULL)
3886 break;
3887 }
3888
3889 if (res != NULL)
3890 res[cnt] = NUL;
3891 return res;
3892}
3893
3894/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003895 * Apply affixes to a word and store the resulting words.
3896 * "ht" is the hashtable with affentry_T that need to be applied, either
3897 * prefixes or suffixes.
3898 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
3899 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003900 *
3901 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003902 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003903 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003904store_aff_word(word, spin, afflist, affile, ht, xht, comb, flags, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003905 char_u *word; /* basic word start */
3906 spellinfo_T *spin; /* spell info */
3907 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003908 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003909 hashtab_T *ht;
3910 hashtab_T *xht;
3911 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003912 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003913 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003914{
3915 int todo;
3916 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003917 affheader_T *ah;
3918 affentry_T *ae;
3919 regmatch_T regmatch;
3920 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003921 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003922 int i;
3923 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003924 int use_flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003925
Bram Moolenaar51485f02005-06-04 21:55:20 +00003926 todo = ht->ht_used;
3927 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003928 {
3929 if (!HASHITEM_EMPTY(hi))
3930 {
3931 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003932 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00003933
Bram Moolenaar51485f02005-06-04 21:55:20 +00003934 /* Check that the affix combines, if required, and that the word
3935 * supports this affix. */
3936 if ((!comb || ah->ah_combine)
3937 && vim_strchr(afflist, *ah->ah_key) != NULL)
Bram Moolenaar5482f332005-04-17 20:18:43 +00003938 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003939 /* Loop over all affix entries with this name. */
3940 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003941 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003942 /* Check the condition. It's not logical to match case
3943 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003944 * Myspell.
3945 * For prefixes, when "PFXPOSTPONE" was used, only do
3946 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003947 regmatch.regprog = ae->ae_prog;
3948 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003949 if ((xht != NULL || !affile->af_pfxpostpone
3950 || ae->ae_chop != NULL)
3951 && (ae->ae_prog == NULL
3952 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003953 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003954 /* Match. Remove the chop and add the affix. */
3955 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003956 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003957 /* prefix: chop/add at the start of the word */
3958 if (ae->ae_add == NULL)
3959 *newword = NUL;
3960 else
3961 STRCPY(newword, ae->ae_add);
3962 p = word;
3963 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003964 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003965 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003966#ifdef FEAT_MBYTE
3967 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003968 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003969 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003970 for ( ; i > 0; --i)
3971 mb_ptr_adv(p);
3972 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00003973 else
3974#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003975 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003976 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003977 STRCAT(newword, p);
3978 }
3979 else
3980 {
3981 /* suffix: chop/add at the end of the word */
3982 STRCPY(newword, word);
3983 if (ae->ae_chop != NULL)
3984 {
3985 /* Remove chop string. */
3986 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003987 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003988 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003989 mb_ptr_back(newword, p);
3990 *p = NUL;
3991 }
3992 if (ae->ae_add != NULL)
3993 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003994 }
3995
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003996 /* Obey the "rare" flag of the affix. */
3997 if (ae->ae_rare)
3998 use_flags = flags | WF_RARE;
3999 else
4000 use_flags = flags;
4001
Bram Moolenaar51485f02005-06-04 21:55:20 +00004002 /* Store the modified word. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004003 if (store_word(newword, spin, use_flags,
4004 spin->si_region, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004005 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004006
Bram Moolenaar51485f02005-06-04 21:55:20 +00004007 /* When added a suffix and combining is allowed also
4008 * try adding prefixes additionally. */
4009 if (xht != NULL && ah->ah_combine)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004010 if (store_aff_word(newword, spin, afflist, affile,
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004011 xht, NULL, TRUE, use_flags, pfxlist)
4012 == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004013 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004014 }
4015 }
4016 }
4017 }
4018 }
4019
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004020 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004021}
4022
4023/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004024 * Read a file with a list of words.
4025 */
4026 static int
4027spell_read_wordfile(fname, spin)
4028 char_u *fname;
4029 spellinfo_T *spin;
4030{
4031 FILE *fd;
4032 long lnum = 0;
4033 char_u rline[MAXLINELEN];
4034 char_u *line;
4035 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004036 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004037 int l;
4038 int retval = OK;
4039 int did_word = FALSE;
4040 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004041 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004042 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004043
4044 /*
4045 * Open the file.
4046 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004047 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00004048 if (fd == NULL)
4049 {
4050 EMSG2(_(e_notopen), fname);
4051 return FAIL;
4052 }
4053
Bram Moolenaarb765d632005-06-07 21:00:02 +00004054 if (spin->si_verbose || p_verbose > 2)
4055 {
4056 if (!spin->si_verbose)
4057 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004058 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004059 out_flush();
4060 if (!spin->si_verbose)
4061 verbose_leave();
4062 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004063
4064 /*
4065 * Read all the lines in the file one by one.
4066 */
4067 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
4068 {
4069 line_breakcheck();
4070 ++lnum;
4071
4072 /* Skip comment lines. */
4073 if (*rline == '#')
4074 continue;
4075
4076 /* Remove CR, LF and white space from the end. */
4077 l = STRLEN(rline);
4078 while (l > 0 && rline[l - 1] <= ' ')
4079 --l;
4080 if (l == 0)
4081 continue; /* empty or blank line */
4082 rline[l] = NUL;
4083
4084 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
4085 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004086#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004087 if (spin->si_conv.vc_type != CONV_NONE)
4088 {
4089 pc = string_convert(&spin->si_conv, rline, NULL);
4090 if (pc == NULL)
4091 {
4092 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4093 fname, lnum, rline);
4094 continue;
4095 }
4096 line = pc;
4097 }
4098 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004099#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004100 {
4101 pc = NULL;
4102 line = rline;
4103 }
4104
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004105 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00004106 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004107 ++line;
4108 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004109 {
4110 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004111 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
4112 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004113 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004114 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
4115 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004116 else
4117 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004118#ifdef FEAT_MBYTE
4119 char_u *enc;
4120
Bram Moolenaar51485f02005-06-04 21:55:20 +00004121 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004122 line += 10;
4123 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004124 if (enc != NULL && !spin->si_ascii
4125 && convert_setup(&spin->si_conv, enc,
4126 p_enc) == FAIL)
4127 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00004128 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004129 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004130 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004131#else
4132 smsg((char_u *)_("Conversion in %s not supported"), fname);
4133#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004134 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004135 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004136 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004137
Bram Moolenaar3982c542005-06-08 21:56:31 +00004138 if (STRNCMP(line, "regions=", 8) == 0)
4139 {
4140 if (spin->si_region_count > 1)
4141 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
4142 fname, lnum, line);
4143 else
4144 {
4145 line += 8;
4146 if (STRLEN(line) > 16)
4147 smsg((char_u *)_("Too many regions in %s line %d: %s"),
4148 fname, lnum, line);
4149 else
4150 {
4151 spin->si_region_count = STRLEN(line) / 2;
4152 STRCPY(spin->si_region_name, line);
4153 }
4154 }
4155 continue;
4156 }
4157
Bram Moolenaar7887d882005-07-01 22:33:52 +00004158 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
4159 fname, lnum, line - 1);
4160 continue;
4161 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004162
Bram Moolenaar7887d882005-07-01 22:33:52 +00004163 flags = 0;
4164 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004165
Bram Moolenaar7887d882005-07-01 22:33:52 +00004166 /* Check for flags and region after a slash. */
4167 p = vim_strchr(line, '/');
4168 if (p != NULL)
4169 {
4170 *p++ = NUL;
4171 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004172 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004173 if (*p == '=') /* keep-case word */
4174 flags |= WF_KEEPCAP;
4175 else if (*p == '!') /* Bad, bad, wicked word. */
4176 flags |= WF_BANNED;
4177 else if (*p == '?') /* Rare word. */
4178 flags |= WF_RARE;
4179 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004180 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004181 if ((flags & WF_REGION) == 0) /* first one */
4182 regionmask = 0;
4183 flags |= WF_REGION;
4184
4185 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00004186 if (l > spin->si_region_count)
4187 {
4188 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00004189 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004190 break;
4191 }
4192 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004193 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00004194 else
4195 {
4196 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
4197 fname, lnum, p);
4198 break;
4199 }
4200 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004201 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004202 }
4203
4204 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
4205 if (spin->si_ascii && has_non_ascii(line))
4206 {
4207 ++non_ascii;
4208 continue;
4209 }
4210
4211 /* Normal word: store it. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004212 if (store_word(line, spin, flags, regionmask, NULL) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004213 {
4214 retval = FAIL;
4215 break;
4216 }
4217 did_word = TRUE;
4218 }
4219
4220 vim_free(pc);
4221 fclose(fd);
4222
Bram Moolenaarb765d632005-06-07 21:00:02 +00004223 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
4224 {
4225 if (p_verbose > 2)
4226 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00004227 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
4228 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004229 if (p_verbose > 2)
4230 verbose_leave();
4231 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004232 return retval;
4233}
4234
4235/*
4236 * Get part of an sblock_T, "len" bytes long.
4237 * This avoids calling free() for every little struct we use.
4238 * The memory is cleared to all zeros.
4239 * Returns NULL when out of memory.
4240 */
4241 static void *
4242getroom(blp, len)
4243 sblock_T **blp;
4244 size_t len; /* length needed */
4245{
4246 char_u *p;
4247 sblock_T *bl = *blp;
4248
4249 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
4250 {
4251 /* Allocate a block of memory. This is not freed until much later. */
4252 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
4253 if (bl == NULL)
4254 return NULL;
4255 bl->sb_next = *blp;
4256 *blp = bl;
4257 bl->sb_used = 0;
4258 }
4259
4260 p = bl->sb_data + bl->sb_used;
4261 bl->sb_used += len;
4262
4263 return p;
4264}
4265
4266/*
4267 * Make a copy of a string into memory allocated with getroom().
4268 */
4269 static char_u *
4270getroom_save(blp, s)
4271 sblock_T **blp;
4272 char_u *s;
4273{
4274 char_u *sc;
4275
4276 sc = (char_u *)getroom(blp, STRLEN(s) + 1);
4277 if (sc != NULL)
4278 STRCPY(sc, s);
4279 return sc;
4280}
4281
4282
4283/*
4284 * Free the list of allocated sblock_T.
4285 */
4286 static void
4287free_blocks(bl)
4288 sblock_T *bl;
4289{
4290 sblock_T *next;
4291
4292 while (bl != NULL)
4293 {
4294 next = bl->sb_next;
4295 vim_free(bl);
4296 bl = next;
4297 }
4298}
4299
4300/*
4301 * Allocate the root of a word tree.
4302 */
4303 static wordnode_T *
4304wordtree_alloc(blp)
4305 sblock_T **blp;
4306{
4307 return (wordnode_T *)getroom(blp, sizeof(wordnode_T));
4308}
4309
4310/*
4311 * Store a word in the tree(s).
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004312 * Always store it in the case-folded tree. A keep-case word can also be used
4313 * with all caps.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004314 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004315 * When "pfxlist" is not NULL store the word for each prefix ID.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004316 */
4317 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004318store_word(word, spin, flags, region, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004319 char_u *word;
4320 spellinfo_T *spin;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004321 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004322 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004323 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004324{
4325 int len = STRLEN(word);
4326 int ct = captype(word, word + len);
4327 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004328 int res = OK;
4329 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004330
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004331 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004332 for (p = pfxlist; res == OK; ++p)
4333 {
4334 res = tree_add_word(foldword, spin->si_foldroot, ct | flags,
4335 region, p == NULL ? 0 : *p, &spin->si_blocks);
4336 if (p == NULL || *p == NUL)
4337 break;
4338 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00004339 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004340
4341 if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP))
Bram Moolenaar8db73182005-06-17 21:51:16 +00004342 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004343 for (p = pfxlist; res == OK; ++p)
4344 {
4345 res = tree_add_word(word, spin->si_keeproot, flags,
4346 region, p == NULL ? 0 : *p, &spin->si_blocks);
4347 if (p == NULL || *p == NUL)
4348 break;
4349 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00004350 ++spin->si_keepwcount;
4351 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004352 return res;
4353}
4354
4355/*
4356 * Add word "word" to a word tree at "root".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004357 * When "flags" < 0 we are adding to the prefix tree where flags is used for
4358 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004359 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004360 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004361 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004362tree_add_word(word, root, flags, region, prefixID, blp)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004363 char_u *word;
4364 wordnode_T *root;
4365 int flags;
4366 int region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004367 int prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004368 sblock_T **blp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004369{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004370 wordnode_T *node = root;
4371 wordnode_T *np;
4372 wordnode_T **prev = NULL;
4373 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004374
Bram Moolenaar51485f02005-06-04 21:55:20 +00004375 /* Add each byte of the word to the tree, including the NUL at the end. */
4376 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004377 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004378 /* Look for the sibling that has the same character. They are sorted
4379 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004380 * higher byte value. For zero bytes (end of word) the sorting is
4381 * done on flags and then on prefixID
Bram Moolenaar51485f02005-06-04 21:55:20 +00004382 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004383 while (node != NULL
4384 && (node->wn_byte < word[i]
4385 || (node->wn_byte == NUL
4386 && (flags < 0
4387 ? node->wn_prefixID < prefixID
4388 : node->wn_flags < (flags & 0xff)
4389 || (node->wn_flags == (flags & 0xff)
4390 && node->wn_prefixID < prefixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004391 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004392 prev = &node->wn_sibling;
4393 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004394 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004395 if (node == NULL
4396 || node->wn_byte != word[i]
4397 || (word[i] == NUL
4398 && (flags < 0
4399 || node->wn_flags != (flags & 0xff)
4400 || node->wn_prefixID != prefixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004401 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004402 /* Allocate a new node. */
4403 np = (wordnode_T *)getroom(blp, sizeof(wordnode_T));
4404 if (np == NULL)
4405 return FAIL;
4406 np->wn_byte = word[i];
4407 *prev = np;
4408 np->wn_sibling = node;
4409 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004410 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004411
Bram Moolenaar51485f02005-06-04 21:55:20 +00004412 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004413 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004414 node->wn_flags = flags;
4415 node->wn_region |= region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004416 node->wn_prefixID = prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004417 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004418 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004419 prev = &node->wn_child;
4420 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004421 }
4422
4423 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004424}
4425
4426/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004427 * Compress a tree: find tails that are identical and can be shared.
4428 */
4429 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00004430wordtree_compress(root, spin)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004431 wordnode_T *root;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004432 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004433{
4434 hashtab_T ht;
4435 int n;
4436 int tot = 0;
4437
4438 if (root != NULL)
4439 {
4440 hash_init(&ht);
4441 n = node_compress(root, &ht, &tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004442 if (spin->si_verbose || p_verbose > 2)
4443 {
4444 if (!spin->si_verbose)
4445 verbose_enter();
4446 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar51485f02005-06-04 21:55:20 +00004447 n, tot, (tot - n) * 100 / tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004448 if (p_verbose > 2)
4449 verbose_leave();
4450 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004451 hash_clear(&ht);
4452 }
4453}
4454
4455/*
4456 * Compress a node, its siblings and its children, depth first.
4457 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004458 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004459 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00004460node_compress(node, ht, tot)
4461 wordnode_T *node;
4462 hashtab_T *ht;
4463 int *tot; /* total count of nodes before compressing,
4464 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004465{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004466 wordnode_T *np;
4467 wordnode_T *tp;
4468 wordnode_T *child;
4469 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004470 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004471 int len = 0;
4472 unsigned nr, n;
4473 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004474
Bram Moolenaar51485f02005-06-04 21:55:20 +00004475 /*
4476 * Go through the list of siblings. Compress each child and then try
4477 * finding an identical child to replace it.
4478 * Note that with "child" we mean not just the node that is pointed to,
4479 * but the whole list of siblings, of which the node is the first.
4480 */
4481 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004482 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004483 ++len;
4484 if ((child = np->wn_child) != NULL)
4485 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00004486 /* Compress the child. This fills hashkey. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004487 compressed += node_compress(child, ht, tot);
4488
4489 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004490 hash = hash_hash(child->wn_u1.hashkey);
4491 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004492 tp = NULL;
4493 if (!HASHITEM_EMPTY(hi))
4494 {
4495 /* There are children with an identical hash value. Now check
4496 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004497 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004498 if (node_equal(child, tp))
4499 {
4500 /* Found one! Now use that child in place of the
4501 * current one. This means the current child is
4502 * dropped from the tree. */
4503 np->wn_child = tp;
4504 ++compressed;
4505 break;
4506 }
4507 if (tp == NULL)
4508 {
4509 /* No other child with this hash value equals the child of
4510 * the node, add it to the linked list after the first
4511 * item. */
4512 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004513 child->wn_u2.next = tp->wn_u2.next;
4514 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004515 }
4516 }
4517 else
4518 /* No other child has this hash value, add it to the
4519 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004520 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004521 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004522 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004523 *tot += len;
4524
4525 /*
4526 * Make a hash key for the node and its siblings, so that we can quickly
4527 * find a lookalike node. This must be done after compressing the sibling
4528 * list, otherwise the hash key would become invalid by the compression.
4529 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004530 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004531 nr = 0;
4532 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004533 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004534 if (np->wn_byte == NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004535 /* end node: use wn_flags, wn_region and wn_prefixID */
4536 n = np->wn_flags + (np->wn_region << 8) + (np->wn_prefixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004537 else
4538 /* byte node: use the byte value and the child pointer */
4539 n = np->wn_byte + ((long_u)np->wn_child << 8);
4540 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004541 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004542
4543 /* Avoid NUL bytes, it terminates the hash key. */
4544 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004545 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004546 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004547 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004548 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004549 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004550 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004551 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
4552 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004553
4554 return compressed;
4555}
4556
4557/*
4558 * Return TRUE when two nodes have identical siblings and children.
4559 */
4560 static int
4561node_equal(n1, n2)
4562 wordnode_T *n1;
4563 wordnode_T *n2;
4564{
4565 wordnode_T *p1;
4566 wordnode_T *p2;
4567
4568 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
4569 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
4570 if (p1->wn_byte != p2->wn_byte
4571 || (p1->wn_byte == NUL
4572 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004573 || p1->wn_region != p2->wn_region
4574 || p1->wn_prefixID != p2->wn_prefixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004575 : (p1->wn_child != p2->wn_child)))
4576 break;
4577
4578 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004579}
4580
4581/*
4582 * Write a number to file "fd", MSB first, in "len" bytes.
4583 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004584 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004585put_bytes(fd, nr, len)
4586 FILE *fd;
4587 long_u nr;
4588 int len;
4589{
4590 int i;
4591
4592 for (i = len - 1; i >= 0; --i)
4593 putc((int)(nr >> (i * 8)), fd);
4594}
4595
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004596static int
4597#ifdef __BORLANDC__
4598_RTLENTRYF
4599#endif
4600rep_compare __ARGS((const void *s1, const void *s2));
4601
4602/*
4603 * Function given to qsort() to sort the REP items on "from" string.
4604 */
4605 static int
4606#ifdef __BORLANDC__
4607_RTLENTRYF
4608#endif
4609rep_compare(s1, s2)
4610 const void *s1;
4611 const void *s2;
4612{
4613 fromto_T *p1 = (fromto_T *)s1;
4614 fromto_T *p2 = (fromto_T *)s2;
4615
4616 return STRCMP(p1->ft_from, p2->ft_from);
4617}
4618
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004619/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004620 * Write the Vim spell file "fname".
4621 */
4622 static void
Bram Moolenaar3982c542005-06-08 21:56:31 +00004623write_vim_spell(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004624 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004625 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004626{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004627 FILE *fd;
4628 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004629 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004630 wordnode_T *tree;
4631 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004632 int i;
4633 int l;
4634 garray_T *gap;
4635 fromto_T *ftp;
4636 char_u *p;
4637 int rr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004638
Bram Moolenaarb765d632005-06-07 21:00:02 +00004639 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00004640 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004641 {
4642 EMSG2(_(e_notopen), fname);
4643 return;
4644 }
4645
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004646 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004647 * <charflagslen> <charflags>
4648 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004649 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004650 * <prefcondcnt> <prefcond> ... */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004651
4652 /* <fileID> */
4653 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
4654 EMSG(_(e_write));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004655
4656 /* write the region names if there is more than one */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004657 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004658 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004659 putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */
4660 fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2),
4661 (size_t)1, fd);
4662 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004663 }
4664 else
4665 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004666 putc(0, fd);
4667 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004668 }
4669
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004670 /*
4671 * Write the table with character flags and table for case folding.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004672 * <charflagslen> <charflags> <fcharlen> <fchars>
4673 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004674 * 'encoding'.
4675 * Also skip this for an .add.spl file, the main spell file must contain
4676 * the table (avoids that it conflicts). File is shorter too.
4677 */
4678 if (spin->si_ascii || spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004679 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004680 putc(0, fd);
4681 putc(0, fd);
4682 putc(0, fd);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004683 }
4684 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00004685 write_spell_chartab(fd);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004686
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004687
4688 if (spin->si_midword == NULL)
4689 put_bytes(fd, 0L, 2); /* <midwordlen> */
4690 else
4691 {
4692 i = STRLEN(spin->si_midword);
4693 put_bytes(fd, (long_u)i, 2); /* <midwordlen> */
4694 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
4695 }
4696
4697
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004698 /* Write the prefix conditions. */
4699 write_spell_prefcond(fd, &spin->si_prefcond);
4700
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004701 /* <SUGGEST> : <repcount> <rep> ...
4702 * <salflags> <salcount> <sal> ...
4703 * <maplen> <mapstr> */
4704
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004705 /* Sort the REP items. */
4706 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
4707 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004708
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004709 /* round 1: REP items
4710 * round 2: SAL items (unless SOFO is used) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004711 for (round = 1; round <= 2; ++round)
4712 {
4713 if (round == 1)
4714 gap = &spin->si_rep;
4715 else
4716 {
4717 gap = &spin->si_sal;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004718
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004719 i = 0;
4720 if (spin->si_followup)
4721 i |= SAL_F0LLOWUP;
4722 if (spin->si_collapse)
4723 i |= SAL_COLLAPSE;
4724 if (spin->si_rem_accents)
4725 i |= SAL_REM_ACCENTS;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004726 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
4727 i |= SAL_SOFO;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004728 putc(i, fd); /* <salflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004729 if (i & SAL_SOFO)
4730 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004731 }
4732
4733 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
4734 for (i = 0; i < gap->ga_len; ++i)
4735 {
4736 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
4737 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
4738 ftp = &((fromto_T *)gap->ga_data)[i];
4739 for (rr = 1; rr <= 2; ++rr)
4740 {
4741 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
4742 l = STRLEN(p);
4743 putc(l, fd);
4744 fwrite(p, l, (size_t)1, fd);
4745 }
4746 }
4747 }
4748
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004749 /* SOFOFROM and SOFOTO */
4750 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
4751 {
4752 put_bytes(fd, 1L, 2); /* <salcount> */
4753
4754 l = STRLEN(spin->si_sofofr);
4755 put_bytes(fd, (long_u)l, 2); /* <salfromlen> */
4756 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <salfrom> */
4757
4758 l = STRLEN(spin->si_sofoto);
4759 put_bytes(fd, (long_u)l, 2); /* <saltolen> */
4760 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <salto> */
4761 }
4762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004763 put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */
4764 if (spin->si_map.ga_len > 0) /* <mapstr> */
4765 fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len,
4766 (size_t)1, fd);
Bram Moolenaar50cde822005-06-05 21:54:54 +00004767
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004768 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004769 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004770 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004771 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004772 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004773 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004774 if (round == 1)
4775 tree = spin->si_foldroot;
4776 else if (round == 2)
4777 tree = spin->si_keeproot;
4778 else
4779 tree = spin->si_prefroot;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004780
Bram Moolenaar0c405862005-06-22 22:26:26 +00004781 /* Clear the index and wnode fields in the tree. */
4782 clear_node(tree);
4783
Bram Moolenaar51485f02005-06-04 21:55:20 +00004784 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00004785 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00004786 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004787 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004788
Bram Moolenaar51485f02005-06-04 21:55:20 +00004789 /* number of nodes in 4 bytes */
4790 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004791 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004792
Bram Moolenaar51485f02005-06-04 21:55:20 +00004793 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004794 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004795 }
4796
Bram Moolenaar51485f02005-06-04 21:55:20 +00004797 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004798}
4799
4800/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00004801 * Clear the index and wnode fields of "node", it siblings and its
4802 * children. This is needed because they are a union with other items to save
4803 * space.
4804 */
4805 static void
4806clear_node(node)
4807 wordnode_T *node;
4808{
4809 wordnode_T *np;
4810
4811 if (node != NULL)
4812 for (np = node; np != NULL; np = np->wn_sibling)
4813 {
4814 np->wn_u1.index = 0;
4815 np->wn_u2.wnode = NULL;
4816
4817 if (np->wn_byte != NUL)
4818 clear_node(np->wn_child);
4819 }
4820}
4821
4822
4823/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004824 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004825 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00004826 * This first writes the list of possible bytes (siblings). Then for each
4827 * byte recursively write the children.
4828 *
4829 * NOTE: The code here must match the code in read_tree(), since assumptions
4830 * are made about the indexes (so that we don't have to write them in the
4831 * file).
4832 *
4833 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004834 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004835 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00004836put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004837 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004838 wordnode_T *node;
4839 int index;
4840 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004841 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004842{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004843 int newindex = index;
4844 int siblingcount = 0;
4845 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004846 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004847
Bram Moolenaar51485f02005-06-04 21:55:20 +00004848 /* If "node" is zero the tree is empty. */
4849 if (node == NULL)
4850 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004851
Bram Moolenaar51485f02005-06-04 21:55:20 +00004852 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004853 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004854
4855 /* Count the number of siblings. */
4856 for (np = node; np != NULL; np = np->wn_sibling)
4857 ++siblingcount;
4858
4859 /* Write the sibling count. */
4860 if (fd != NULL)
4861 putc(siblingcount, fd); /* <siblingcount> */
4862
4863 /* Write each sibling byte and optionally extra info. */
4864 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004865 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004866 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004867 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004868 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004869 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004870 /* For a NUL byte (end of word) write the flags etc. */
4871 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004872 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004873 /* In PREFIXTREE write the required prefixID and the
4874 * associated condition nr (stored in wn_region). */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004875 if (np->wn_flags == (char_u)-2)
4876 putc(BY_FLAGS, fd); /* <byte> rare */
4877 else
4878 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004879 putc(np->wn_prefixID, fd); /* <prefixID> */
4880 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004881 }
4882 else
4883 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004884 /* For word trees we write the flag/region items. */
4885 flags = np->wn_flags;
4886 if (regionmask != 0 && np->wn_region != regionmask)
4887 flags |= WF_REGION;
4888 if (np->wn_prefixID != 0)
4889 flags |= WF_PFX;
4890 if (flags == 0)
4891 {
4892 /* word without flags or region */
4893 putc(BY_NOFLAGS, fd); /* <byte> */
4894 }
4895 else
4896 {
4897 putc(BY_FLAGS, fd); /* <byte> */
4898 putc(flags, fd); /* <flags> */
4899 if (flags & WF_REGION)
4900 putc(np->wn_region, fd); /* <region> */
4901 if (flags & WF_PFX)
4902 putc(np->wn_prefixID, fd); /* <prefixID> */
4903 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004904 }
4905 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004906 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004907 else
4908 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00004909 if (np->wn_child->wn_u1.index != 0
4910 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004911 {
4912 /* The child is written elsewhere, write the reference. */
4913 if (fd != NULL)
4914 {
4915 putc(BY_INDEX, fd); /* <byte> */
4916 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004917 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004918 }
4919 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00004920 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004921 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004922 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004923
Bram Moolenaar51485f02005-06-04 21:55:20 +00004924 if (fd != NULL)
4925 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
4926 {
4927 EMSG(_(e_write));
4928 return 0;
4929 }
4930 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004931 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004932
4933 /* Space used in the array when reading: one for each sibling and one for
4934 * the count. */
4935 newindex += siblingcount + 1;
4936
4937 /* Recursively dump the children of each sibling. */
4938 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00004939 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
4940 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004941 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004942
4943 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004944}
4945
4946
4947/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00004948 * ":mkspell [-ascii] outfile infile ..."
4949 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004950 */
4951 void
4952ex_mkspell(eap)
4953 exarg_T *eap;
4954{
4955 int fcount;
4956 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004957 char_u *arg = eap->arg;
4958 int ascii = FALSE;
4959
4960 if (STRNCMP(arg, "-ascii", 6) == 0)
4961 {
4962 ascii = TRUE;
4963 arg = skipwhite(arg + 6);
4964 }
4965
4966 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
4967 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
4968 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004969 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004970 FreeWild(fcount, fnames);
4971 }
4972}
4973
4974/*
4975 * Create a Vim spell file from one or more word lists.
4976 * "fnames[0]" is the output file name.
4977 * "fnames[fcount - 1]" is the last input file name.
4978 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
4979 * and ".spl" is appended to make the output file name.
4980 */
4981 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004982mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004983 int fcount;
4984 char_u **fnames;
4985 int ascii; /* -ascii argument given */
4986 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004987 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004988{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004989 char_u fname[MAXPATHL];
4990 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00004991 char_u **innames;
4992 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004993 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004994 int i;
4995 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004996 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004997 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004998 spellinfo_T spin;
4999
5000 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005001 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005002 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005003 spin.si_followup = TRUE;
5004 spin.si_rem_accents = TRUE;
5005 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
5006 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
5007 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005008 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005009
Bram Moolenaarb765d632005-06-07 21:00:02 +00005010 /* default: fnames[0] is output file, following are input files */
5011 innames = &fnames[1];
5012 incount = fcount - 1;
5013
5014 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005015 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005016 len = STRLEN(fnames[0]);
5017 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
5018 {
5019 /* For ":mkspell path/en.latin1.add" output file is
5020 * "path/en.latin1.add.spl". */
5021 innames = &fnames[0];
5022 incount = 1;
5023 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
5024 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005025 else if (fcount == 1)
5026 {
5027 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
5028 innames = &fnames[0];
5029 incount = 1;
5030 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
5031 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5032 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005033 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
5034 {
5035 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005036 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005037 }
5038 else
5039 /* Name should be language, make the file name from it. */
5040 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
5041 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5042
5043 /* Check for .ascii.spl. */
5044 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
5045 spin.si_ascii = TRUE;
5046
5047 /* Check for .add.spl. */
5048 if (strstr((char *)gettail(wfname), ".add.") != NULL)
5049 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005050 }
5051
Bram Moolenaarb765d632005-06-07 21:00:02 +00005052 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005053 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005054 else if (vim_strchr(gettail(wfname), '_') != NULL)
5055 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005056 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005057 EMSG(_("E754: Only up to 8 regions supported"));
5058 else
5059 {
5060 /* Check for overwriting before doing things that may take a lot of
5061 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005062 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005063 {
5064 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005065 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005066 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005067 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005068 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005069 EMSG2(_(e_isadir2), wfname);
5070 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005071 }
5072
5073 /*
5074 * Init the aff and dic pointers.
5075 * Get the region names if there are more than 2 arguments.
5076 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005077 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005078 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005079 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005080
Bram Moolenaar3982c542005-06-08 21:56:31 +00005081 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005082 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005083 len = STRLEN(innames[i]);
5084 if (STRLEN(gettail(innames[i])) < 5
5085 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005086 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005087 EMSG2(_("E755: Invalid region in %s"), innames[i]);
5088 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005089 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005090 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
5091 spin.si_region_name[i * 2 + 1] =
5092 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005093 }
5094 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005095 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005096
Bram Moolenaar51485f02005-06-04 21:55:20 +00005097 spin.si_foldroot = wordtree_alloc(&spin.si_blocks);
5098 spin.si_keeproot = wordtree_alloc(&spin.si_blocks);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005099 spin.si_prefroot = wordtree_alloc(&spin.si_blocks);
5100 if (spin.si_foldroot == NULL
5101 || spin.si_keeproot == NULL
5102 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005103 {
5104 error = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005105 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005106 }
5107
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005108 /* When not producing a .add.spl file clear the character table when
5109 * we encounter one in the .aff file. This means we dump the current
5110 * one in the .spl file if the .aff file doesn't define one. That's
5111 * better than guessing the contents, the table will match a
5112 * previously loaded spell file. */
5113 if (!spin.si_add)
5114 spin.si_clear_chartab = TRUE;
5115
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005116 /*
5117 * Read all the .aff and .dic files.
5118 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00005119 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005120 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005121 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005122 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005123 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005124 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005125
Bram Moolenaarb765d632005-06-07 21:00:02 +00005126 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005127 if (mch_stat((char *)fname, &st) >= 0)
5128 {
5129 /* Read the .aff file. Will init "spin->si_conv" based on the
5130 * "SET" line. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005131 afile[i] = spell_read_aff(fname, &spin);
5132 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005133 error = TRUE;
5134 else
5135 {
5136 /* Read the .dic file and store the words in the trees. */
5137 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00005138 innames[i]);
5139 if (spell_read_dic(fname, &spin, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005140 error = TRUE;
5141 }
5142 }
5143 else
5144 {
5145 /* No .aff file, try reading the file as a word list. Store
5146 * the words in the trees. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005147 if (spell_read_wordfile(innames[i], &spin) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005148 error = TRUE;
5149 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005150
Bram Moolenaarb765d632005-06-07 21:00:02 +00005151#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005152 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005153 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005154#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005155 }
5156
Bram Moolenaar51485f02005-06-04 21:55:20 +00005157 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005158 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005159 /*
5160 * Remove the dummy NUL from the start of the tree root.
5161 */
5162 spin.si_foldroot = spin.si_foldroot->wn_sibling;
5163 spin.si_keeproot = spin.si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005164 spin.si_prefroot = spin.si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005165
5166 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005167 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005168 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005169 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005170 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005171 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005172 verbose_enter();
5173 MSG(_("Compressing word tree..."));
5174 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005175 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005176 verbose_leave();
5177 }
5178 wordtree_compress(spin.si_foldroot, &spin);
5179 wordtree_compress(spin.si_keeproot, &spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005180 wordtree_compress(spin.si_prefroot, &spin);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005181 }
5182
Bram Moolenaar51485f02005-06-04 21:55:20 +00005183 if (!error)
5184 {
5185 /*
5186 * Write the info in the spell file.
5187 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005188 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005189 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005190 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005191 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005192 smsg((char_u *)_("Writing spell file %s ..."), wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005193 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005194 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005195 verbose_leave();
5196 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00005197
Bram Moolenaar3982c542005-06-08 21:56:31 +00005198 write_vim_spell(wfname, &spin);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005199
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005200 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005201 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005202 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005203 verbose_enter();
5204 MSG(_("Done!"));
5205 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00005206 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005207 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005208 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005209 verbose_leave();
5210 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005211
Bram Moolenaarb765d632005-06-07 21:00:02 +00005212 /* If the file is loaded need to reload it. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005213 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005214 }
5215
5216 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005217 ga_clear(&spin.si_rep);
5218 ga_clear(&spin.si_sal);
5219 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005220 ga_clear(&spin.si_prefcond);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005221 vim_free(spin.si_midword);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005222 vim_free(spin.si_sofofr);
5223 vim_free(spin.si_sofoto);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005224
5225 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005226 for (i = 0; i < incount; ++i)
5227 if (afile[i] != NULL)
5228 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005229
5230 /* Free all the bits and pieces at once. */
5231 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005232 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005233}
5234
Bram Moolenaarb765d632005-06-07 21:00:02 +00005235
5236/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005237 * ":[count]spellgood {word}"
5238 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00005239 */
5240 void
5241ex_spell(eap)
5242 exarg_T *eap;
5243{
Bram Moolenaar7887d882005-07-01 22:33:52 +00005244 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005245 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005246}
5247
5248/*
5249 * Add "word[len]" to 'spellfile' as a good or bad word.
5250 */
5251 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005252spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005253 char_u *word;
5254 int len;
5255 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005256 int index; /* "zG" and "zW": zero, otherwise index in
5257 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005258{
5259 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005260 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005261 int new_spf = FALSE;
5262 struct stat st;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005263 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005264 char_u fnamebuf[MAXPATHL];
5265 char_u line[MAXWLEN * 2];
5266 long fpos, fpos_next = 0;
5267 int i;
5268 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005269
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005270 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005271 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005272 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00005273 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005274 int_wordlist = vim_tempname('s');
5275 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00005276 return;
5277 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005278 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005279 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005280 else
5281 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005282 /* If 'spellfile' isn't set figure out a good default value. */
5283 if (*curbuf->b_p_spf == NUL)
5284 {
5285 init_spellfile();
5286 new_spf = TRUE;
5287 }
5288
5289 if (*curbuf->b_p_spf == NUL)
5290 {
5291 EMSG(_("E764: 'spellfile' is not set"));
5292 return;
5293 }
5294
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005295 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
5296 {
5297 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
5298 if (i == index)
5299 break;
5300 if (*spf == NUL)
5301 {
5302 EMSGN(_("E765: 'spellfile' does not have %ld enties"), index);
5303 return;
5304 }
5305 }
5306
Bram Moolenaarb765d632005-06-07 21:00:02 +00005307 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005308 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005309 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
5310 buf = NULL;
5311 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00005312 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005313 EMSG(_(e_bufloaded));
5314 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005315 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00005316
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005317 fname = fnamebuf;
5318 }
5319
5320 if (bad)
5321 {
5322 /* When the word also appears as good word we need to remove that one,
5323 * since its flags sort before the one with WF_BANNED. */
5324 fd = mch_fopen((char *)fname, "r");
5325 if (fd != NULL)
5326 {
5327 while (!vim_fgets(line, MAXWLEN * 2, fd))
5328 {
5329 fpos = fpos_next;
5330 fpos_next = ftell(fd);
5331 if (STRNCMP(word, line, len) == 0
5332 && (line[len] == '/' || line[len] < ' '))
5333 {
5334 /* Found duplicate word. Remove it by writing a '#' at
5335 * the start of the line. Mixing reading and writing
5336 * doesn't work for all systems, close the file first. */
5337 fclose(fd);
5338 fd = mch_fopen((char *)fname, "r+");
5339 if (fd == NULL)
5340 break;
5341 if (fseek(fd, fpos, SEEK_SET) == 0)
5342 fputc('#', fd);
5343 fseek(fd, fpos_next, SEEK_SET);
5344 }
5345 }
5346 fclose(fd);
5347 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00005348 }
5349
5350 fd = mch_fopen((char *)fname, "a");
5351 if (fd == NULL && new_spf)
5352 {
5353 /* We just initialized the 'spellfile' option and can't open the file.
5354 * We may need to create the "spell" directory first. We already
5355 * checked the runtime directory is writable in init_spellfile(). */
5356 STRCPY(NameBuff, fname);
5357 *gettail_sep(NameBuff) = NUL;
5358 if (mch_stat((char *)NameBuff, &st) < 0)
5359 {
5360 /* The directory doesn't exist. Try creating it and opening the
5361 * file again. */
5362 vim_mkdir(NameBuff, 0755);
5363 fd = mch_fopen((char *)fname, "a");
5364 }
5365 }
5366
5367 if (fd == NULL)
5368 EMSG2(_(e_notopen), fname);
5369 else
5370 {
5371 if (bad)
5372 fprintf(fd, "%.*s/!\n", len, word);
5373 else
5374 fprintf(fd, "%.*s\n", len, word);
5375 fclose(fd);
5376
5377 /* Update the .add.spl file. */
5378 mkspell(1, &fname, FALSE, TRUE, TRUE);
5379
5380 /* If the .add file is edited somewhere, reload it. */
5381 if (buf != NULL)
5382 buf_reload(buf);
5383
5384 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005385 }
5386}
5387
5388/*
5389 * Initialize 'spellfile' for the current buffer.
5390 */
5391 static void
5392init_spellfile()
5393{
5394 char_u buf[MAXPATHL];
5395 int l;
5396 slang_T *sl;
5397 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005398 char_u *lend;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005399
5400 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
5401 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005402 /* Find the end of the language name. Exclude the region. */
5403 for (lend = curbuf->b_p_spl; *lend != NUL
5404 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
5405 ;
5406
5407 /* Loop over all entries in 'runtimepath'. Use the first one where we
5408 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005409 rtp = p_rtp;
5410 while (*rtp != NUL)
5411 {
5412 /* Copy the path from 'runtimepath' to buf[]. */
5413 copy_option_part(&rtp, buf, MAXPATHL, ",");
5414 if (filewritable(buf) == 2)
5415 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00005416 /* Use the first language name from 'spelllang' and the
5417 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005418 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
5419 l = STRLEN(buf);
5420 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00005421 "/spell/%.*s.%s.add",
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005422 (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00005423 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
5424 ? (char_u *)"ascii" : spell_enc());
5425 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
5426 break;
5427 }
5428 }
5429 }
5430}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005431
Bram Moolenaar51485f02005-06-04 21:55:20 +00005432
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005433/*
5434 * Init the chartab used for spelling for ASCII.
5435 * EBCDIC is not supported!
5436 */
5437 static void
5438clear_spell_chartab(sp)
5439 spelltab_T *sp;
5440{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005441 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005442
5443 /* Init everything to FALSE. */
5444 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
5445 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
5446 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005447 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005448 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005449 sp->st_upper[i] = i;
5450 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005451
5452 /* We include digits. A word shouldn't start with a digit, but handling
5453 * that is done separately. */
5454 for (i = '0'; i <= '9'; ++i)
5455 sp->st_isw[i] = TRUE;
5456 for (i = 'A'; i <= 'Z'; ++i)
5457 {
5458 sp->st_isw[i] = TRUE;
5459 sp->st_isu[i] = TRUE;
5460 sp->st_fold[i] = i + 0x20;
5461 }
5462 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005463 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005464 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005465 sp->st_upper[i] = i - 0x20;
5466 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005467}
5468
5469/*
5470 * Init the chartab used for spelling. Only depends on 'encoding'.
5471 * Called once while starting up and when 'encoding' changes.
5472 * The default is to use isalpha(), but the spell file should define the word
5473 * characters to make it possible that 'encoding' differs from the current
5474 * locale.
5475 */
5476 void
5477init_spell_chartab()
5478{
5479 int i;
5480
5481 did_set_spelltab = FALSE;
5482 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005483#ifdef FEAT_MBYTE
5484 if (enc_dbcs)
5485 {
5486 /* DBCS: assume double-wide characters are word characters. */
5487 for (i = 128; i <= 255; ++i)
5488 if (MB_BYTE2LEN(i) == 2)
5489 spelltab.st_isw[i] = TRUE;
5490 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005491 else if (enc_utf8)
5492 {
5493 for (i = 128; i < 256; ++i)
5494 {
5495 spelltab.st_isu[i] = utf_isupper(i);
5496 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
5497 spelltab.st_fold[i] = utf_fold(i);
5498 spelltab.st_upper[i] = utf_toupper(i);
5499 }
5500 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005501 else
5502#endif
5503 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005504 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005505 for (i = 128; i < 256; ++i)
5506 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005507 if (MB_ISUPPER(i))
5508 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005509 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005510 spelltab.st_isu[i] = TRUE;
5511 spelltab.st_fold[i] = MB_TOLOWER(i);
5512 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005513 else if (MB_ISLOWER(i))
5514 {
5515 spelltab.st_isw[i] = TRUE;
5516 spelltab.st_upper[i] = MB_TOUPPER(i);
5517 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005518 }
5519 }
5520}
5521
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005522static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
5523static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
5524
5525/*
5526 * Set the spell character tables from strings in the affix file.
5527 */
5528 static int
5529set_spell_chartab(fol, low, upp)
5530 char_u *fol;
5531 char_u *low;
5532 char_u *upp;
5533{
5534 /* We build the new tables here first, so that we can compare with the
5535 * previous one. */
5536 spelltab_T new_st;
5537 char_u *pf = fol, *pl = low, *pu = upp;
5538 int f, l, u;
5539
5540 clear_spell_chartab(&new_st);
5541
5542 while (*pf != NUL)
5543 {
5544 if (*pl == NUL || *pu == NUL)
5545 {
5546 EMSG(_(e_affform));
5547 return FAIL;
5548 }
5549#ifdef FEAT_MBYTE
5550 f = mb_ptr2char_adv(&pf);
5551 l = mb_ptr2char_adv(&pl);
5552 u = mb_ptr2char_adv(&pu);
5553#else
5554 f = *pf++;
5555 l = *pl++;
5556 u = *pu++;
5557#endif
5558 /* Every character that appears is a word character. */
5559 if (f < 256)
5560 new_st.st_isw[f] = TRUE;
5561 if (l < 256)
5562 new_st.st_isw[l] = TRUE;
5563 if (u < 256)
5564 new_st.st_isw[u] = TRUE;
5565
5566 /* if "LOW" and "FOL" are not the same the "LOW" char needs
5567 * case-folding */
5568 if (l < 256 && l != f)
5569 {
5570 if (f >= 256)
5571 {
5572 EMSG(_(e_affrange));
5573 return FAIL;
5574 }
5575 new_st.st_fold[l] = f;
5576 }
5577
5578 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005579 * case-folding, it's upper case and the "UPP" is the upper case of
5580 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005581 if (u < 256 && u != f)
5582 {
5583 if (f >= 256)
5584 {
5585 EMSG(_(e_affrange));
5586 return FAIL;
5587 }
5588 new_st.st_fold[u] = f;
5589 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005590 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005591 }
5592 }
5593
5594 if (*pl != NUL || *pu != NUL)
5595 {
5596 EMSG(_(e_affform));
5597 return FAIL;
5598 }
5599
5600 return set_spell_finish(&new_st);
5601}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005602
5603/*
5604 * Set the spell character tables from strings in the .spl file.
5605 */
5606 static int
Bram Moolenaar7887d882005-07-01 22:33:52 +00005607set_spell_charflags(flags, upp)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005608 char_u *flags;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005609 char_u *upp;
5610{
5611 /* We build the new tables here first, so that we can compare with the
5612 * previous one. */
5613 spelltab_T new_st;
5614 int i;
5615 char_u *p = upp;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005616 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005617
5618 clear_spell_chartab(&new_st);
5619
Bram Moolenaar7887d882005-07-01 22:33:52 +00005620 for (i = 0; flags[i] != NUL; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005621 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005622 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
5623 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005624
5625 if (*p == NUL)
5626 return FAIL;
5627#ifdef FEAT_MBYTE
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005628 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005629#else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005630 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005631#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005632 new_st.st_fold[i + 128] = c;
5633 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
5634 new_st.st_upper[c] = i + 128;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005635 }
5636
5637 return set_spell_finish(&new_st);
5638}
5639
5640 static int
5641set_spell_finish(new_st)
5642 spelltab_T *new_st;
5643{
5644 int i;
5645
5646 if (did_set_spelltab)
5647 {
5648 /* check that it's the same table */
5649 for (i = 0; i < 256; ++i)
5650 {
5651 if (spelltab.st_isw[i] != new_st->st_isw[i]
5652 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005653 || spelltab.st_fold[i] != new_st->st_fold[i]
5654 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005655 {
5656 EMSG(_("E763: Word characters differ between spell files"));
5657 return FAIL;
5658 }
5659 }
5660 }
5661 else
5662 {
5663 /* copy the new spelltab into the one being used */
5664 spelltab = *new_st;
5665 did_set_spelltab = TRUE;
5666 }
5667
5668 return OK;
5669}
5670
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005671/*
Bram Moolenaarea408852005-06-25 22:49:46 +00005672 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005673 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00005674 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005675 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00005676 */
5677 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005678spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00005679 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005680 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00005681{
Bram Moolenaarea408852005-06-25 22:49:46 +00005682#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005683 char_u *s;
5684 int l;
5685 int c;
5686
5687 if (has_mbyte)
5688 {
5689 l = MB_BYTE2LEN(*p);
5690 s = p;
5691 if (l == 1)
5692 {
5693 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005694 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005695 {
5696 s = p + 1; /* skip a mid-word character */
5697 l = MB_BYTE2LEN(*s);
5698 }
5699 }
5700 else
5701 {
5702 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005703 if (c < 256 ? buf->b_spell_ismw[c]
5704 : (buf->b_spell_ismw_mb != NULL
5705 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005706 {
5707 s = p + l;
5708 l = MB_BYTE2LEN(*s);
5709 }
5710 }
5711
5712 if (l > 1)
5713 return mb_get_class(s) >= 2;
5714 return spelltab.st_isw[*s];
5715 }
Bram Moolenaarea408852005-06-25 22:49:46 +00005716#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005717
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005718 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
5719}
5720
5721/*
5722 * Return TRUE if "p" points to a word character.
5723 * Unlike spell_iswordp() this doesn't check for "midword" characters.
5724 */
5725 static int
5726spell_iswordp_nmw(p)
5727 char_u *p;
5728{
5729#ifdef FEAT_MBYTE
5730 if (has_mbyte && MB_BYTE2LEN(*p) > 1)
5731 return mb_get_class(p) >= 2;
5732#endif
5733
5734 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00005735}
5736
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005737#ifdef FEAT_MBYTE
5738/*
5739 * Return TRUE if "p" points to a word character.
5740 * Wide version of spell_iswordp().
5741 */
5742 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005743spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005744 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005745 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005746{
5747 int *s;
5748
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005749 if (*p < 256 ? buf->b_spell_ismw[*p]
5750 : (buf->b_spell_ismw_mb != NULL
5751 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005752 s = p + 1;
5753 else
5754 s = p;
5755
5756 if (mb_char2len(*s) > 1)
5757 {
5758 if (enc_utf8)
5759 return utf_class(*s) >= 2;
5760 if (enc_dbcs)
5761 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
5762 return 0;
5763 }
5764 return spelltab.st_isw[*s];
5765}
5766#endif
5767
Bram Moolenaarea408852005-06-25 22:49:46 +00005768/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005769 * Write the table with prefix conditions to the .spl file.
5770 */
5771 static void
5772write_spell_prefcond(fd, gap)
5773 FILE *fd;
5774 garray_T *gap;
5775{
5776 int i;
5777 char_u *p;
5778 int len;
5779
5780 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
5781
5782 for (i = 0; i < gap->ga_len; ++i)
5783 {
5784 /* <prefcond> : <condlen> <condstr> */
5785 p = ((char_u **)gap->ga_data)[i];
5786 if (p == NULL)
5787 fputc(0, fd);
5788 else
5789 {
5790 len = STRLEN(p);
5791 fputc(len, fd);
5792 fwrite(p, (size_t)len, (size_t)1, fd);
5793 }
5794 }
5795}
5796
5797/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005798 * Write the current tables into the .spl file.
5799 * This makes sure the same characters are recognized as word characters when
5800 * generating an when using a spell file.
5801 */
5802 static void
5803write_spell_chartab(fd)
5804 FILE *fd;
5805{
5806 char_u charbuf[256 * 4];
5807 int len = 0;
5808 int flags;
5809 int i;
5810
5811 fputc(128, fd); /* <charflagslen> */
5812 for (i = 128; i < 256; ++i)
5813 {
5814 flags = 0;
5815 if (spelltab.st_isw[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005816 flags |= CF_WORD;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005817 if (spelltab.st_isu[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005818 flags |= CF_UPPER;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005819 fputc(flags, fd); /* <charflags> */
5820
Bram Moolenaarb765d632005-06-07 21:00:02 +00005821#ifdef FEAT_MBYTE
5822 if (has_mbyte)
5823 len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
5824 else
5825#endif
5826 charbuf[len++] = spelltab.st_fold[i];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005827 }
5828
5829 put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
5830 fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
5831}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005832
5833/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005834 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
5835 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005836 * When using a multi-byte 'encoding' the length may change!
5837 * Returns FAIL when something wrong.
5838 */
5839 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005840spell_casefold(str, len, buf, buflen)
5841 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005842 int len;
5843 char_u *buf;
5844 int buflen;
5845{
5846 int i;
5847
5848 if (len >= buflen)
5849 {
5850 buf[0] = NUL;
5851 return FAIL; /* result will not fit */
5852 }
5853
5854#ifdef FEAT_MBYTE
5855 if (has_mbyte)
5856 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005857 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005858 char_u *p;
5859 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005860
5861 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005862 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005863 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005864 if (outi + MB_MAXBYTES > buflen)
5865 {
5866 buf[outi] = NUL;
5867 return FAIL;
5868 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005869 c = mb_ptr2char_adv(&p);
5870 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005871 }
5872 buf[outi] = NUL;
5873 }
5874 else
5875#endif
5876 {
5877 /* Be quick for non-multibyte encodings. */
5878 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005879 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005880 buf[i] = NUL;
5881 }
5882
5883 return OK;
5884}
5885
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005886#define SPS_BEST 1
5887#define SPS_FAST 2
5888#define SPS_DOUBLE 4
5889
5890static int sps_flags = SPS_BEST;
5891
5892/*
5893 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
5894 * Sets "sps_flags".
5895 */
5896 int
5897spell_check_sps()
5898{
5899 char_u *p;
5900 char_u buf[MAXPATHL];
5901 int f;
5902
5903 sps_flags = 0;
5904
5905 for (p = p_sps; *p != NUL; )
5906 {
5907 copy_option_part(&p, buf, MAXPATHL, ",");
5908
5909 f = 0;
5910 if (STRCMP(buf, "best") == 0)
5911 f = SPS_BEST;
5912 else if (STRCMP(buf, "fast") == 0)
5913 f = SPS_FAST;
5914 else if (STRCMP(buf, "double") == 0)
5915 f = SPS_DOUBLE;
5916 else if (STRNCMP(buf, "expr:", 5) != 0
5917 && STRNCMP(buf, "file:", 5) != 0)
5918 f = -1;
5919
5920 if (f == -1 || (sps_flags != 0 && f != 0))
5921 {
5922 sps_flags = SPS_BEST;
5923 return FAIL;
5924 }
5925 if (f != 0)
5926 sps_flags = f;
5927 }
5928
5929 if (sps_flags == 0)
5930 sps_flags = SPS_BEST;
5931
5932 return OK;
5933}
5934
5935/* Remember what "z?" replaced. */
5936static char_u *repl_from = NULL;
5937static char_u *repl_to = NULL;
5938
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005939/*
5940 * "z?": Find badly spelled word under or after the cursor.
5941 * Give suggestions for the properly spelled word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005942 */
5943 void
5944spell_suggest()
5945{
5946 char_u *line;
5947 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005948 char_u wcopy[MAXWLEN + 2];
5949 char_u *p;
5950 int i;
5951 int c;
5952 suginfo_T sug;
5953 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005954 int mouse_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005955
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005956 /* Find the start of the badly spelled word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005957 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL
5958 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005959 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005960 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
5961 return;
5962
5963 /* No bad word or it starts after the cursor: use the word under the
5964 * cursor. */
5965 curwin->w_cursor = prev_cursor;
5966 line = ml_get_curline();
5967 p = line + curwin->w_cursor.col;
5968 /* Backup to before start of word. */
5969 while (p > line && SPELL_ISWORDP(p))
5970 mb_ptr_back(line, p);
5971 /* Forward to start of word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005972 while (*p != NUL && !SPELL_ISWORDP(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00005973 mb_ptr_adv(p);
5974
5975 if (!SPELL_ISWORDP(p)) /* No word found. */
5976 {
5977 beep_flush();
5978 return;
5979 }
5980 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005981 }
5982
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005983 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005984 line = ml_get_curline();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005985
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005986 /* Get the list of suggestions */
Bram Moolenaarea408852005-06-25 22:49:46 +00005987 spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005988
5989 if (sug.su_ga.ga_len == 0)
5990 MSG(_("Sorry, no suggestions"));
5991 else
5992 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005993 vim_free(repl_from);
5994 repl_from = NULL;
5995 vim_free(repl_to);
5996 repl_to = NULL;
5997
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005998 /* List the suggestions. */
5999 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006000 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006001 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
6002 sug.su_badlen, sug.su_badptr);
6003 msg_puts(IObuff);
6004 msg_clr_eos();
6005 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00006006
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006007 msg_scroll = TRUE;
6008 for (i = 0; i < sug.su_ga.ga_len; ++i)
6009 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006010 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006011
6012 /* The suggested word may replace only part of the bad word, add
6013 * the not replaced part. */
6014 STRCPY(wcopy, stp->st_word);
6015 if (sug.su_badlen > stp->st_orglen)
6016 vim_strncpy(wcopy + STRLEN(wcopy),
6017 sug.su_badptr + stp->st_orglen,
6018 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006019 vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\""), i + 1, wcopy);
6020 msg_puts(IObuff);
6021
6022 /* The word may replace more than "su_badlen". */
6023 if (sug.su_badlen < stp->st_orglen)
6024 {
6025 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
6026 stp->st_orglen, sug.su_badptr);
6027 msg_puts(IObuff);
6028 }
6029
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006030 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006031 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006032 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006033 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006034 vim_snprintf((char *)IObuff, IOSIZE, _(" (%s%d - %d)"),
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006035 stp->st_salscore ? "s " : "",
6036 stp->st_score, stp->st_altscore);
6037 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00006038 vim_snprintf((char *)IObuff, IOSIZE, _(" (%d)"),
6039 stp->st_score);
6040 msg_advance(30);
6041 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006042 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006043 msg_putchar('\n');
6044 }
6045
6046 /* Ask for choice. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006047 i = prompt_for_number(&mouse_used);
6048 if (mouse_used)
6049 i -= lines_left;
6050
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006051 if (i > 0 && i <= sug.su_ga.ga_len && u_save_cursor() == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006052 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006053 /* Save the from and to text for :spellrepall. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006054 stp = &SUG(sug.su_ga, i - 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006055 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
6056 repl_to = vim_strsave(stp->st_word);
6057
6058 /* Replace the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006059 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
6060 if (p != NULL)
6061 {
6062 c = sug.su_badptr - line;
6063 mch_memmove(p, line, c);
6064 STRCPY(p + c, stp->st_word);
6065 STRCAT(p, sug.su_badptr + stp->st_orglen);
6066 ml_replace(curwin->w_cursor.lnum, p, FALSE);
6067 curwin->w_cursor.col = c;
6068 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006069
6070 /* For redo we use a change-word command. */
6071 ResetRedobuff();
6072 AppendToRedobuff((char_u *)"ciw");
6073 AppendToRedobuff(stp->st_word);
6074 AppendCharToRedobuff(ESC);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006075 }
6076 }
6077 else
6078 curwin->w_cursor = prev_cursor;
6079 }
6080
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006081 spell_find_cleanup(&sug);
6082}
6083
6084/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006085 * ":spellrepall"
6086 */
6087/*ARGSUSED*/
6088 void
6089ex_spellrepall(eap)
6090 exarg_T *eap;
6091{
6092 pos_T pos = curwin->w_cursor;
6093 char_u *frompat;
6094 int addlen;
6095 char_u *line;
6096 char_u *p;
6097 int didone = FALSE;
6098 int save_ws = p_ws;
6099
6100 if (repl_from == NULL || repl_to == NULL)
6101 {
6102 EMSG(_("E752: No previous spell replacement"));
6103 return;
6104 }
6105 addlen = STRLEN(repl_to) - STRLEN(repl_from);
6106
6107 frompat = alloc(STRLEN(repl_from) + 7);
6108 if (frompat == NULL)
6109 return;
6110 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
6111 p_ws = FALSE;
6112
6113 curwin->w_cursor.lnum = 0;
6114 while (!got_int)
6115 {
6116 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
6117 || u_save_cursor() == FAIL)
6118 break;
6119
6120 /* Only replace when the right word isn't there yet. This happens
6121 * when changing "etc" to "etc.". */
6122 line = ml_get_curline();
6123 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
6124 repl_to, STRLEN(repl_to)) != 0)
6125 {
6126 p = alloc(STRLEN(line) + addlen + 1);
6127 if (p == NULL)
6128 break;
6129 mch_memmove(p, line, curwin->w_cursor.col);
6130 STRCPY(p + curwin->w_cursor.col, repl_to);
6131 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
6132 ml_replace(curwin->w_cursor.lnum, p, FALSE);
6133 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6134 didone = TRUE;
6135 }
6136 curwin->w_cursor.col += STRLEN(repl_to);
6137 }
6138
6139 p_ws = save_ws;
6140 curwin->w_cursor = pos;
6141 vim_free(frompat);
6142
6143 if (!didone)
6144 EMSG2(_("E753: Not found: %s"), repl_from);
6145}
6146
6147/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006148 * Find spell suggestions for "word". Return them in the growarray "*gap" as
6149 * a list of allocated strings.
6150 */
6151 void
6152spell_suggest_list(gap, word, maxcount)
6153 garray_T *gap;
6154 char_u *word;
6155 int maxcount; /* maximum nr of suggestions */
6156{
6157 suginfo_T sug;
6158 int i;
6159 suggest_T *stp;
6160 char_u *wcopy;
6161
Bram Moolenaarea408852005-06-25 22:49:46 +00006162 spell_find_suggest(word, &sug, maxcount, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006163
6164 /* Make room in "gap". */
6165 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
6166 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
6167 return;
6168
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006169 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006170 {
6171 stp = &SUG(sug.su_ga, i);
6172
6173 /* The suggested word may replace only part of "word", add the not
6174 * replaced part. */
6175 wcopy = alloc(STRLEN(stp->st_word)
6176 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
6177 if (wcopy == NULL)
6178 break;
6179 STRCPY(wcopy, stp->st_word);
6180 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
6181 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
6182 }
6183
6184 spell_find_cleanup(&sug);
6185}
6186
6187/*
6188 * Find spell suggestions for the word at the start of "badptr".
6189 * Return the suggestions in "su->su_ga".
6190 * The maximum number of suggestions is "maxcount".
6191 * Note: does use info for the current window.
6192 * This is based on the mechanisms of Aspell, but completely reimplemented.
6193 */
6194 static void
Bram Moolenaarea408852005-06-25 22:49:46 +00006195spell_find_suggest(badptr, su, maxcount, banbadword)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006196 char_u *badptr;
6197 suginfo_T *su;
6198 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00006199 int banbadword; /* don't include badword in suggestions */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006200{
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006201 int attr = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006202 char_u buf[MAXPATHL];
6203 char_u *p;
6204 int do_combine = FALSE;
6205 char_u *sps_copy;
6206#ifdef FEAT_EVAL
6207 static int expr_busy = FALSE;
6208#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006209 int c;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006210
6211 /*
6212 * Set the info in "*su".
6213 */
6214 vim_memset(su, 0, sizeof(suginfo_T));
6215 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
6216 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006217 if (*badptr == NUL)
6218 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006219 hash_init(&su->su_banned);
6220
6221 su->su_badptr = badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006222 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006223 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006224 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006225
6226 if (su->su_badlen >= MAXWLEN)
6227 su->su_badlen = MAXWLEN - 1; /* just in case */
6228 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
6229 (void)spell_casefold(su->su_badptr, su->su_badlen,
6230 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006231 /* get caps flags for bad word */
6232 su->su_badflags = captype(su->su_badptr, su->su_badptr + su->su_badlen);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006233
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006234 /* If the word is not capitalised and spell_check() doesn't consider the
6235 * word to be bad then it might need to be capitalised. Add a suggestion
6236 * for that. */
6237#ifdef FEAT_MBYTE
6238 c = mb_ptr2char(su->su_badptr);
6239#else
6240 c = *p;
6241#endif
6242 if (!SPELL_ISUPPER(c) && attr == 0)
6243 {
6244 make_case_word(su->su_badword, buf, WF_ONECAP);
6245 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
6246 0, TRUE);
6247 }
6248
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006249 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00006250 if (banbadword)
6251 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006252
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006253 /* Make a copy of 'spellsuggest', because the expression may change it. */
6254 sps_copy = vim_strsave(p_sps);
6255 if (sps_copy == NULL)
6256 return;
6257
6258 /* Loop over the items in 'spellsuggest'. */
6259 for (p = sps_copy; *p != NUL; )
6260 {
6261 copy_option_part(&p, buf, MAXPATHL, ",");
6262
6263 if (STRNCMP(buf, "expr:", 5) == 0)
6264 {
6265#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006266 /* Evaluate an expression. Skip this when called recursively,
6267 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006268 if (!expr_busy)
6269 {
6270 expr_busy = TRUE;
6271 spell_suggest_expr(su, buf + 5);
6272 expr_busy = FALSE;
6273 }
6274#endif
6275 }
6276 else if (STRNCMP(buf, "file:", 5) == 0)
6277 /* Use list of suggestions in a file. */
6278 spell_suggest_file(su, buf + 5);
6279 else
6280 {
6281 /* Use internal method. */
6282 spell_suggest_intern(su);
6283 if (sps_flags & SPS_DOUBLE)
6284 do_combine = TRUE;
6285 }
6286 }
6287
6288 vim_free(sps_copy);
6289
6290 if (do_combine)
6291 /* Combine the two list of suggestions. This must be done last,
6292 * because sorting changes the order again. */
6293 score_combine(su);
6294}
6295
6296#ifdef FEAT_EVAL
6297/*
6298 * Find suggestions by evaluating expression "expr".
6299 */
6300 static void
6301spell_suggest_expr(su, expr)
6302 suginfo_T *su;
6303 char_u *expr;
6304{
6305 list_T *list;
6306 listitem_T *li;
6307 int score;
6308 char_u *p;
6309
6310 /* The work is split up in a few parts to avoid having to export
6311 * suginfo_T.
6312 * First evaluate the expression and get the resulting list. */
6313 list = eval_spell_expr(su->su_badword, expr);
6314 if (list != NULL)
6315 {
6316 /* Loop over the items in the list. */
6317 for (li = list->lv_first; li != NULL; li = li->li_next)
6318 if (li->li_tv.v_type == VAR_LIST)
6319 {
6320 /* Get the word and the score from the items. */
6321 score = get_spellword(li->li_tv.vval.v_list, &p);
6322 if (score >= 0)
6323 add_suggestion(su, &su->su_ga, p,
6324 su->su_badlen, score, 0, TRUE);
6325 }
6326 list_unref(list);
6327 }
6328
6329 /* Sort the suggestions and truncate at "maxcount". */
6330 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
6331}
6332#endif
6333
6334/*
6335 * Find suggestions a file "fname".
6336 */
6337 static void
6338spell_suggest_file(su, fname)
6339 suginfo_T *su;
6340 char_u *fname;
6341{
6342 FILE *fd;
6343 char_u line[MAXWLEN * 2];
6344 char_u *p;
6345 int len;
6346 char_u cword[MAXWLEN];
6347
6348 /* Open the file. */
6349 fd = mch_fopen((char *)fname, "r");
6350 if (fd == NULL)
6351 {
6352 EMSG2(_(e_notopen), fname);
6353 return;
6354 }
6355
6356 /* Read it line by line. */
6357 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
6358 {
6359 line_breakcheck();
6360
6361 p = vim_strchr(line, '/');
6362 if (p == NULL)
6363 continue; /* No Tab found, just skip the line. */
6364 *p++ = NUL;
6365 if (STRICMP(su->su_badword, line) == 0)
6366 {
6367 /* Match! Isolate the good word, until CR or NL. */
6368 for (len = 0; p[len] >= ' '; ++len)
6369 ;
6370 p[len] = NUL;
6371
6372 /* If the suggestion doesn't have specific case duplicate the case
6373 * of the bad word. */
6374 if (captype(p, NULL) == 0)
6375 {
6376 make_case_word(p, cword, su->su_badflags);
6377 p = cword;
6378 }
6379
6380 add_suggestion(su, &su->su_ga, p, su->su_badlen,
6381 SCORE_FILE, 0, TRUE);
6382 }
6383 }
6384
6385 fclose(fd);
6386
6387 /* Sort the suggestions and truncate at "maxcount". */
6388 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
6389}
6390
6391/*
6392 * Find suggestions for the internal method indicated by "sps_flags".
6393 */
6394 static void
6395spell_suggest_intern(su)
6396 suginfo_T *su;
6397{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006398 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006399 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006400 *
6401 * Set a maximum score to limit the combination of operations that is
6402 * tried.
6403 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006404 suggest_try_special(su);
6405
6406 /*
6407 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
6408 * from the .aff file and inserting a space (split the word).
6409 */
6410 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006411
6412 /* For the resulting top-scorers compute the sound-a-like score. */
6413 if (sps_flags & SPS_DOUBLE)
6414 score_comp_sal(su);
6415
6416 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006417 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006418 *
6419 * Only do this when we don't have a lot of suggestions yet, because it's
6420 * very slow and often doesn't find new suggestions.
6421 */
6422 if ((sps_flags & SPS_DOUBLE)
6423 || (!(sps_flags & SPS_FAST)
6424 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
6425 {
6426 /* Allow a higher score now. */
6427 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006428 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006429 }
6430
6431 /* When CTRL-C was hit while searching do show the results. */
6432 ui_breakcheck();
6433 if (got_int)
6434 {
6435 (void)vgetc();
6436 got_int = FALSE;
6437 }
6438
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006439 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006440 {
6441 if (sps_flags & SPS_BEST)
6442 /* Adjust the word score for how it sounds like. */
6443 rescore_suggestions(su);
6444
6445 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006446 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006447 }
6448}
6449
6450/*
6451 * Free the info put in "*su" by spell_find_suggest().
6452 */
6453 static void
6454spell_find_cleanup(su)
6455 suginfo_T *su;
6456{
6457 int i;
6458
6459 /* Free the suggestions. */
6460 for (i = 0; i < su->su_ga.ga_len; ++i)
6461 vim_free(SUG(su->su_ga, i).st_word);
6462 ga_clear(&su->su_ga);
6463 for (i = 0; i < su->su_sga.ga_len; ++i)
6464 vim_free(SUG(su->su_sga, i).st_word);
6465 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006466
6467 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006468 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006469}
6470
6471/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006472 * Make a copy of "word", with the first letter upper or lower cased, to
6473 * "wcopy[MAXWLEN]". "word" must not be empty.
6474 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006475 */
6476 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006477onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006478 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006479 char_u *wcopy;
6480 int upper; /* TRUE: first letter made upper case */
6481{
6482 char_u *p;
6483 int c;
6484 int l;
6485
6486 p = word;
6487#ifdef FEAT_MBYTE
6488 if (has_mbyte)
6489 c = mb_ptr2char_adv(&p);
6490 else
6491#endif
6492 c = *p++;
6493 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006494 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006495 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006496 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006497#ifdef FEAT_MBYTE
6498 if (has_mbyte)
6499 l = mb_char2bytes(c, wcopy);
6500 else
6501#endif
6502 {
6503 l = 1;
6504 wcopy[0] = c;
6505 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006506 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006507}
6508
6509/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006510 * Make a copy of "word" with all the letters upper cased into
6511 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006512 */
6513 static void
6514allcap_copy(word, wcopy)
6515 char_u *word;
6516 char_u *wcopy;
6517{
6518 char_u *s;
6519 char_u *d;
6520 int c;
6521
6522 d = wcopy;
6523 for (s = word; *s != NUL; )
6524 {
6525#ifdef FEAT_MBYTE
6526 if (has_mbyte)
6527 c = mb_ptr2char_adv(&s);
6528 else
6529#endif
6530 c = *s++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006531 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006532
6533#ifdef FEAT_MBYTE
6534 if (has_mbyte)
6535 {
6536 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
6537 break;
6538 d += mb_char2bytes(c, d);
6539 }
6540 else
6541#endif
6542 {
6543 if (d - wcopy >= MAXWLEN - 1)
6544 break;
6545 *d++ = c;
6546 }
6547 }
6548 *d = NUL;
6549}
6550
6551/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006552 * Try finding suggestions by recognizing specific situations.
6553 */
6554 static void
6555suggest_try_special(su)
6556 suginfo_T *su;
6557{
6558 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006559 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006560 int c;
6561 char_u word[MAXWLEN];
6562
6563 /*
6564 * Recognize a word that is repeated: "the the".
6565 */
6566 p = skiptowhite(su->su_fbadword);
6567 len = p - su->su_fbadword;
6568 p = skipwhite(p);
6569 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
6570 {
6571 /* Include badflags: if the badword is onecap or allcap
6572 * use that for the goodword too: "The the" -> "The". */
6573 c = su->su_fbadword[len];
6574 su->su_fbadword[len] = NUL;
6575 make_case_word(su->su_fbadword, word, su->su_badflags);
6576 su->su_fbadword[len] = c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006577 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006578 }
6579}
6580
6581/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006582 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00006583 *
6584 * This uses a state machine. At each node in the tree we try various
6585 * operations. When trying if an operation work "depth" is increased and the
6586 * stack[] is used to store info. This allows combinations, thus insert one
6587 * character, replace one and delete another. The number of changes is
6588 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006589 */
6590 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00006591suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006592 suginfo_T *su;
6593{
6594 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
6595 char_u tword[MAXWLEN]; /* good word collected so far */
6596 trystate_T stack[MAXWLEN];
6597 char_u preword[MAXWLEN * 3]; /* word found with proper case (appended
6598 * to for word split) */
6599 char_u prewordlen = 0; /* length of word in "preword" */
6600 int splitoff = 0; /* index in tword after last split */
6601 trystate_T *sp;
6602 int newscore;
6603 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006604 char_u *byts, *fbyts, *pbyts;
6605 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006606 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00006607 int c, c2, c3;
6608 int n = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006609 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006610 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006611 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006612 int len;
6613 char_u *p;
6614 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00006615 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006616 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006617
6618 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00006619 * to find matches (esp. REP items). Append some more text, changing
6620 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006621 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006622 n = STRLEN(fword);
6623 p = su->su_badptr + su->su_badlen;
6624 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006625
6626 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
6627 lp->lp_slang != NULL; ++lp)
6628 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006629 /*
6630 * Go through the whole case-fold tree, try changes at each node.
6631 * "tword[]" contains the word collected from nodes in the tree.
6632 * "fword[]" the word we are trying to match with (initially the bad
6633 * word).
6634 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006635 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006636 sp = &stack[0];
6637 sp->ts_state = STATE_START;
6638 sp->ts_score = 0;
6639 sp->ts_curi = 1;
6640 sp->ts_fidx = 0;
6641 sp->ts_fidxtry = 0;
6642 sp->ts_twordlen = 0;
6643 sp->ts_arridx = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00006644#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006645 sp->ts_tcharlen = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00006646#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006647
Bram Moolenaarea424162005-06-16 21:51:00 +00006648 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006649 * When there are postponed prefixes we need to use these first. At
6650 * the end of the prefix we continue in the case-fold tree.
6651 */
6652 fbyts = lp->lp_slang->sl_fbyts;
6653 fidxs = lp->lp_slang->sl_fidxs;
6654 pbyts = lp->lp_slang->sl_pbyts;
6655 pidxs = lp->lp_slang->sl_pidxs;
6656 if (pbyts != NULL)
6657 {
6658 byts = pbyts;
6659 idxs = pidxs;
6660 sp->ts_prefixdepth = PREFIXTREE;
6661 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
6662 }
6663 else
6664 {
6665 byts = fbyts;
6666 idxs = fidxs;
6667 sp->ts_prefixdepth = NOPREFIX;
6668 }
6669
6670 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00006671 * Loop to find all suggestions. At each round we either:
6672 * - For the current state try one operation, advance "ts_curi",
6673 * increase "depth".
6674 * - When a state is done go to the next, set "ts_state".
6675 * - When all states are tried decrease "depth".
6676 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006677 while (depth >= 0 && !got_int)
6678 {
6679 sp = &stack[depth];
6680 switch (sp->ts_state)
6681 {
6682 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006683 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006684 /*
6685 * Start of node: Deal with NUL bytes, which means
6686 * tword[] may end here.
6687 */
6688 arridx = sp->ts_arridx; /* current node in the tree */
6689 len = byts[arridx]; /* bytes in this node */
6690 arridx += sp->ts_curi; /* index of current byte */
6691
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006692 if (sp->ts_prefixdepth == PREFIXTREE)
6693 {
6694 /* Skip over the NUL bytes, we use them later. */
6695 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
6696 ;
6697 sp->ts_curi += n;
6698
6699 /* At end of a prefix or at start of prefixtree: check for
6700 * following word. */
6701 if (byts[arridx] == 0 || sp->ts_state == STATE_NOPREFIX)
6702 {
6703 sp->ts_state = STATE_START;
6704 ++depth;
6705 stack[depth] = stack[depth - 1];
6706 sp = &stack[depth];
6707 sp->ts_prefixdepth = depth - 1;
6708 byts = fbyts;
6709 idxs = fidxs;
6710 sp->ts_state = STATE_START;
6711 sp->ts_curi = 1; /* start just after length byte */
6712 sp->ts_arridx = 0;
6713
6714 /* Move the prefix to preword[] so that
6715 * find_keepcap_word() works. */
6716 prewordlen = splitoff = sp->ts_twordlen;
6717 mch_memmove(preword, tword, splitoff);
6718 break;
6719 }
6720
6721 /* Always past NUL bytes now. */
6722 sp->ts_state = STATE_ENDNUL;
6723 break;
6724 }
6725
Bram Moolenaar0c405862005-06-22 22:26:26 +00006726 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006727 {
6728 /* Past bytes in node and/or past NUL bytes. */
6729 sp->ts_state = STATE_ENDNUL;
6730 break;
6731 }
6732
6733 /*
6734 * End of word in tree.
6735 */
6736 ++sp->ts_curi; /* eat one NUL byte */
6737
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006738 flags = (int)idxs[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006739
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006740 if (sp->ts_prefixdepth < MAXWLEN)
6741 {
6742 /* There was a prefix before the word. Check that the
6743 * prefix can be used with this word. */
6744 /* Count the length of the NULs in the prefix. If there
6745 * are none this must be the first try without a prefix.
6746 */
6747 n = stack[sp->ts_prefixdepth].ts_arridx;
6748 len = pbyts[n++];
6749 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
6750 ;
6751 if (c > 0)
6752 {
6753 /* The prefix ID is stored two bytes above the flags. */
6754 c = valid_word_prefix(c, n, (unsigned)flags >> 16,
6755 tword + splitoff, lp->lp_slang);
6756 if (c == 0)
6757 break;
6758
6759 /* Use the WF_RARE flag for a rare prefix. */
6760 if (c & WF_RAREPFX)
6761 flags |= WF_RARE;
6762 }
6763 }
6764
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006765 /*
6766 * Form the word with proper case in preword.
6767 * If there is a word from a previous split, append.
6768 */
6769 tword[sp->ts_twordlen] = NUL;
6770 if (flags & WF_KEEPCAP)
6771 /* Must find the word in the keep-case tree. */
6772 find_keepcap_word(lp->lp_slang, tword + splitoff,
6773 preword + prewordlen);
6774 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00006775 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006776 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00006777 * use that for the goodword too. But if the badword is
6778 * allcap and it's only one char long use onecap. */
6779 c = su->su_badflags;
6780 if ((c & WF_ALLCAP)
6781#ifdef FEAT_MBYTE
6782 && su->su_badlen == mb_ptr2len_check(su->su_badptr)
6783#else
6784 && su->su_badlen == 1
6785#endif
6786 )
6787 c = WF_ONECAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006788 make_case_word(tword + splitoff,
Bram Moolenaar0c405862005-06-22 22:26:26 +00006789 preword + prewordlen, flags | c);
6790 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006791
6792 /* Don't use a banned word. It may appear again as a good
6793 * word, thus remember it. */
6794 if (flags & WF_BANNED)
6795 {
6796 add_banned(su, preword + prewordlen);
6797 break;
6798 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006799 if (was_banned(su, preword + prewordlen)
6800 || was_banned(su, preword))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006801 break;
6802
6803 newscore = 0;
6804 if ((flags & WF_REGION)
6805 && (((unsigned)flags >> 8) & lp->lp_region) == 0)
6806 newscore += SCORE_REGION;
6807 if (flags & WF_RARE)
6808 newscore += SCORE_RARE;
6809
Bram Moolenaar0c405862005-06-22 22:26:26 +00006810 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006811 captype(preword + prewordlen, NULL)))
6812 newscore += SCORE_ICASE;
6813
Bram Moolenaar0c405862005-06-22 22:26:26 +00006814 if ((fword[sp->ts_fidx] == NUL
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006815 || !spell_iswordp(fword + sp->ts_fidx, curbuf))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006816 && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006817 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006818 /* The badword also ends: add suggestions. Give a penalty
6819 * when changing non-word char to word char, e.g., "thes,"
6820 * -> "these". */
6821 p = fword + sp->ts_fidx;
6822#ifdef FEAT_MBYTE
6823 if (has_mbyte)
6824 mb_ptr_back(fword, p);
6825 else
6826#endif
6827 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006828 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006829 {
6830 p = preword + STRLEN(preword);
6831#ifdef FEAT_MBYTE
6832 if (has_mbyte)
6833 mb_ptr_back(preword, p);
6834 else
6835#endif
6836 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006837 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006838 newscore += SCORE_NONWORD;
6839 }
6840
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006841 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00006842 sp->ts_fidx - repextra,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006843 sp->ts_score + newscore, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006844 }
Bram Moolenaarea424162005-06-16 21:51:00 +00006845 else if (sp->ts_fidx >= sp->ts_fidxtry
6846#ifdef FEAT_MBYTE
6847 /* Don't split halfway a character. */
6848 && (!has_mbyte || sp->ts_tcharlen == 0)
6849#endif
6850 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006851 {
6852 /* The word in the tree ends but the badword
6853 * continues: try inserting a space and check that a valid
6854 * words starts at fword[sp->ts_fidx]. */
6855 if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT))
6856 {
6857 /* Save things to be restored at STATE_SPLITUNDO. */
6858 sp->ts_save_prewordlen = prewordlen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006859 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006860 sp->ts_save_splitoff = splitoff;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006861 sp->ts_state = STATE_SPLITUNDO;
6862
6863 ++depth;
6864 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006865
6866 /* Append a space to preword. */
6867 STRCAT(preword, " ");
6868 prewordlen = STRLEN(preword);
6869 splitoff = sp->ts_twordlen;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006870
6871 /* If the badword has a non-word character at this
6872 * position skip it. That means replacing the
6873 * non-word character with a space. */
6874 if (!spell_iswordp_nmw(fword + sp->ts_fidx))
6875 {
6876 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
6877#ifdef FEAT_MBYTE
6878 if (has_mbyte)
6879 sp->ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
6880 else
6881#endif
6882 ++sp->ts_fidx;
6883 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006884#ifdef FEAT_MBYTE
6885 if (has_mbyte)
6886 {
6887 int i = 0;
6888
6889 /* Case-folding may change the number of bytes:
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006890 * Count nr of chars in fword[ts_fidx] and
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006891 * advance that many chars in su->su_badptr. */
6892 for (p = fword; p < fword + sp->ts_fidx;
6893 mb_ptr_adv(p))
6894 ++i;
6895 for (p = su->su_badptr; i > 0; mb_ptr_adv(p))
6896 --i;
6897 }
6898 else
6899#endif
6900 p = su->su_badptr + sp->ts_fidx;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006901 su->su_badflags = captype(p, su->su_badptr
6902 + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006903
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006904 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006905 sp->ts_arridx = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006906 }
6907 }
6908 break;
6909
6910 case STATE_SPLITUNDO:
Bram Moolenaar0c405862005-06-22 22:26:26 +00006911 /* Undo the changes done for word split. */
6912 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006913 splitoff = sp->ts_save_splitoff;
6914 prewordlen = sp->ts_save_prewordlen;
6915
6916 /* Continue looking for NUL bytes. */
6917 sp->ts_state = STATE_START;
6918 break;
6919
6920 case STATE_ENDNUL:
6921 /* Past the NUL bytes in the node. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006922 if (fword[sp->ts_fidx] == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006923 {
6924 /* The badword ends, can't use the bytes in this node. */
6925 sp->ts_state = STATE_DEL;
6926 break;
6927 }
6928 sp->ts_state = STATE_PLAIN;
6929 /*FALLTHROUGH*/
6930
6931 case STATE_PLAIN:
6932 /*
6933 * Go over all possible bytes at this node, add each to
6934 * tword[] and use child node. "ts_curi" is the index.
6935 */
6936 arridx = sp->ts_arridx;
6937 if (sp->ts_curi > byts[arridx])
6938 {
6939 /* Done all bytes at this node, do next state. When still
6940 * at already changed bytes skip the other tricks. */
6941 if (sp->ts_fidx >= sp->ts_fidxtry)
6942 sp->ts_state = STATE_DEL;
6943 else
6944 sp->ts_state = STATE_FINAL;
6945 }
6946 else
6947 {
6948 arridx += sp->ts_curi++;
6949 c = byts[arridx];
6950
6951 /* Normal byte, go one level deeper. If it's not equal to
6952 * the byte in the bad word adjust the score. But don't
6953 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00006954 if (c == fword[sp->ts_fidx]
6955#ifdef FEAT_MBYTE
6956 || (sp->ts_tcharlen > 0
6957 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006958#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00006959 )
6960 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006961 else
6962 newscore = SCORE_SUBST;
6963 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
6964 && try_deeper(su, stack, depth, newscore))
6965 {
6966 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00006967 sp = &stack[depth];
6968 ++sp->ts_fidx;
6969 tword[sp->ts_twordlen++] = c;
6970 sp->ts_arridx = idxs[arridx];
6971#ifdef FEAT_MBYTE
6972 if (newscore == SCORE_SUBST)
6973 sp->ts_isdiff = DIFF_YES;
6974 if (has_mbyte)
6975 {
6976 /* Multi-byte characters are a bit complicated to
6977 * handle: They differ when any of the bytes
6978 * differ and then their length may also differ. */
6979 if (sp->ts_tcharlen == 0)
6980 {
6981 /* First byte. */
6982 sp->ts_tcharidx = 0;
6983 sp->ts_tcharlen = MB_BYTE2LEN(c);
6984 sp->ts_fcharstart = sp->ts_fidx - 1;
6985 sp->ts_isdiff = (newscore != 0)
6986 ? DIFF_YES : DIFF_NONE;
6987 }
6988 else if (sp->ts_isdiff == DIFF_INSERT)
6989 /* When inserting trail bytes don't advance in
6990 * the bad word. */
6991 --sp->ts_fidx;
6992 if (++sp->ts_tcharidx == sp->ts_tcharlen)
6993 {
6994 /* Last byte of character. */
6995 if (sp->ts_isdiff == DIFF_YES)
6996 {
6997 /* Correct ts_fidx for the byte length of
6998 * the character (we didn't check that
6999 * before). */
7000 sp->ts_fidx = sp->ts_fcharstart
7001 + MB_BYTE2LEN(
7002 fword[sp->ts_fcharstart]);
7003
7004 /* For a similar character adjust score
7005 * from SCORE_SUBST to SCORE_SIMILAR. */
7006 if (lp->lp_slang->sl_has_map
7007 && similar_chars(lp->lp_slang,
7008 mb_ptr2char(tword
7009 + sp->ts_twordlen
7010 - sp->ts_tcharlen),
7011 mb_ptr2char(fword
7012 + sp->ts_fcharstart)))
7013 sp->ts_score -=
7014 SCORE_SUBST - SCORE_SIMILAR;
7015 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007016 else if (sp->ts_isdiff == DIFF_INSERT
7017 && sp->ts_twordlen > sp->ts_tcharlen)
7018 {
7019 /* If the previous character was the same,
7020 * thus doubling a character, give a bonus
7021 * to the score. */
7022 p = tword + sp->ts_twordlen
7023 - sp->ts_tcharlen;
7024 c = mb_ptr2char(p);
7025 mb_ptr_back(tword, p);
7026 if (c == mb_ptr2char(p))
7027 sp->ts_score -= SCORE_INS
7028 - SCORE_INSDUP;
7029 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007030
7031 /* Starting a new char, reset the length. */
7032 sp->ts_tcharlen = 0;
7033 }
7034 }
7035 else
7036#endif
7037 {
7038 /* If we found a similar char adjust the score.
7039 * We do this after calling try_deeper() because
7040 * it's slow. */
7041 if (newscore != 0
7042 && lp->lp_slang->sl_has_map
7043 && similar_chars(lp->lp_slang,
7044 c, fword[sp->ts_fidx - 1]))
7045 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
7046 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007047 }
7048 }
7049 break;
7050
7051 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00007052#ifdef FEAT_MBYTE
7053 /* When past the first byte of a multi-byte char don't try
7054 * delete/insert/swap a character. */
7055 if (has_mbyte && sp->ts_tcharlen > 0)
7056 {
7057 sp->ts_state = STATE_FINAL;
7058 break;
7059 }
7060#endif
7061 /*
7062 * Try skipping one character in the bad word (delete it).
7063 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007064 sp->ts_state = STATE_INS;
7065 sp->ts_curi = 1;
7066 if (fword[sp->ts_fidx] != NUL
7067 && try_deeper(su, stack, depth, SCORE_DEL))
7068 {
7069 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00007070
7071 /* Advance over the character in fword[]. Give a bonus to
7072 * the score if the same character is following "nn" ->
7073 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00007074#ifdef FEAT_MBYTE
7075 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00007076 {
7077 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00007078 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaarea408852005-06-25 22:49:46 +00007079 if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
7080 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
7081 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007082 else
7083#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00007084 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007085 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00007086 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
7087 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
7088 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007089 break;
7090 }
7091 /*FALLTHROUGH*/
7092
7093 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00007094 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007095 * node. */
7096 n = sp->ts_arridx;
7097 if (sp->ts_curi > byts[n])
7098 {
7099 /* Done all bytes at this node, do next state. */
7100 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007101 }
7102 else
7103 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007104 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007105 n += sp->ts_curi++;
7106 c = byts[n];
7107 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
7108 {
7109 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007110 sp = &stack[depth];
7111 tword[sp->ts_twordlen++] = c;
7112 sp->ts_arridx = idxs[n];
7113#ifdef FEAT_MBYTE
7114 if (has_mbyte)
7115 {
7116 fl = MB_BYTE2LEN(c);
7117 if (fl > 1)
7118 {
7119 /* There are following bytes for the same
7120 * character. We must find all bytes before
7121 * trying delete/insert/swap/etc. */
7122 sp->ts_tcharlen = fl;
7123 sp->ts_tcharidx = 1;
7124 sp->ts_isdiff = DIFF_INSERT;
7125 }
7126 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007127 else
7128 fl = 1;
7129 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00007130#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00007131 {
7132 /* If the previous character was the same, thus
7133 * doubling a character, give a bonus to the
7134 * score. */
7135 if (sp->ts_twordlen >= 2
7136 && tword[sp->ts_twordlen - 2] == c)
7137 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
7138 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007139 }
7140 }
7141 break;
7142
7143 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00007144 /*
7145 * Swap two bytes in the bad word: "12" -> "21".
7146 * We change "fword" here, it's changed back afterwards.
7147 */
7148 p = fword + sp->ts_fidx;
7149 c = *p;
7150 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007151 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007152 /* End of word, can't swap or replace. */
7153 sp->ts_state = STATE_FINAL;
7154 break;
7155 }
7156#ifdef FEAT_MBYTE
7157 if (has_mbyte)
7158 {
7159 n = mb_ptr2len_check(p);
7160 c = mb_ptr2char(p);
7161 c2 = mb_ptr2char(p + n);
7162 }
7163 else
7164#endif
7165 c2 = p[1];
7166 if (c == c2)
7167 {
7168 /* Characters are identical, swap won't do anything. */
7169 sp->ts_state = STATE_SWAP3;
7170 break;
7171 }
7172 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
7173 {
7174 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007175 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007176#ifdef FEAT_MBYTE
7177 if (has_mbyte)
7178 {
7179 fl = mb_char2len(c2);
7180 mch_memmove(p, p + n, fl);
7181 mb_char2bytes(c, p + fl);
7182 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
7183 }
7184 else
7185#endif
7186 {
7187 p[0] = c2;
7188 p[1] = c;
7189 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
7190 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007191 }
7192 else
7193 /* If this swap doesn't work then SWAP3 won't either. */
7194 sp->ts_state = STATE_REP_INI;
7195 break;
7196
Bram Moolenaarea424162005-06-16 21:51:00 +00007197 case STATE_UNSWAP:
7198 /* Undo the STATE_SWAP swap: "21" -> "12". */
7199 p = fword + sp->ts_fidx;
7200#ifdef FEAT_MBYTE
7201 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007202 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007203 n = MB_BYTE2LEN(*p);
7204 c = mb_ptr2char(p + n);
7205 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
7206 mb_char2bytes(c, p);
7207 }
7208 else
7209#endif
7210 {
7211 c = *p;
7212 *p = p[1];
7213 p[1] = c;
7214 }
7215 /*FALLTHROUGH*/
7216
7217 case STATE_SWAP3:
7218 /* Swap two bytes, skipping one: "123" -> "321". We change
7219 * "fword" here, it's changed back afterwards. */
7220 p = fword + sp->ts_fidx;
7221#ifdef FEAT_MBYTE
7222 if (has_mbyte)
7223 {
7224 n = mb_ptr2len_check(p);
7225 c = mb_ptr2char(p);
7226 fl = mb_ptr2len_check(p + n);
7227 c2 = mb_ptr2char(p + n);
7228 c3 = mb_ptr2char(p + n + fl);
7229 }
7230 else
7231#endif
7232 {
7233 c = *p;
7234 c2 = p[1];
7235 c3 = p[2];
7236 }
7237
7238 /* When characters are identical: "121" then SWAP3 result is
7239 * identical, ROT3L result is same as SWAP: "211", ROT3L
7240 * result is same as SWAP on next char: "112". Thus skip all
7241 * swapping. Also skip when c3 is NUL. */
7242 if (c == c3 || c3 == NUL)
7243 {
7244 sp->ts_state = STATE_REP_INI;
7245 break;
7246 }
7247 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7248 {
7249 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007250 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007251#ifdef FEAT_MBYTE
7252 if (has_mbyte)
7253 {
7254 tl = mb_char2len(c3);
7255 mch_memmove(p, p + n + fl, tl);
7256 mb_char2bytes(c2, p + tl);
7257 mb_char2bytes(c, p + fl + tl);
7258 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
7259 }
7260 else
7261#endif
7262 {
7263 p[0] = p[2];
7264 p[2] = c;
7265 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7266 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007267 }
7268 else
7269 sp->ts_state = STATE_REP_INI;
7270 break;
7271
Bram Moolenaarea424162005-06-16 21:51:00 +00007272 case STATE_UNSWAP3:
7273 /* Undo STATE_SWAP3: "321" -> "123" */
7274 p = fword + sp->ts_fidx;
7275#ifdef FEAT_MBYTE
7276 if (has_mbyte)
7277 {
7278 n = MB_BYTE2LEN(*p);
7279 c2 = mb_ptr2char(p + n);
7280 fl = MB_BYTE2LEN(p[n]);
7281 c = mb_ptr2char(p + n + fl);
7282 tl = MB_BYTE2LEN(p[n + fl]);
7283 mch_memmove(p + fl + tl, p, n);
7284 mb_char2bytes(c, p);
7285 mb_char2bytes(c2, p + tl);
7286 }
7287 else
7288#endif
7289 {
7290 c = *p;
7291 *p = p[2];
7292 p[2] = c;
7293 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007294
Bram Moolenaarea424162005-06-16 21:51:00 +00007295 /* Rotate three characters left: "123" -> "231". We change
7296 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007297 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7298 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007299 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007300 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007301 p = fword + sp->ts_fidx;
7302#ifdef FEAT_MBYTE
7303 if (has_mbyte)
7304 {
7305 n = mb_ptr2len_check(p);
7306 c = mb_ptr2char(p);
7307 fl = mb_ptr2len_check(p + n);
7308 fl += mb_ptr2len_check(p + n + fl);
7309 mch_memmove(p, p + n, fl);
7310 mb_char2bytes(c, p + fl);
7311 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
7312 }
7313 else
7314#endif
7315 {
7316 c = *p;
7317 *p = p[1];
7318 p[1] = p[2];
7319 p[2] = c;
7320 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7321 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007322 }
7323 else
7324 sp->ts_state = STATE_REP_INI;
7325 break;
7326
Bram Moolenaarea424162005-06-16 21:51:00 +00007327 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007328 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00007329 p = fword + sp->ts_fidx;
7330#ifdef FEAT_MBYTE
7331 if (has_mbyte)
7332 {
7333 n = MB_BYTE2LEN(*p);
7334 n += MB_BYTE2LEN(p[n]);
7335 c = mb_ptr2char(p + n);
7336 tl = MB_BYTE2LEN(p[n]);
7337 mch_memmove(p + tl, p, n);
7338 mb_char2bytes(c, p);
7339 }
7340 else
7341#endif
7342 {
7343 c = p[2];
7344 p[2] = p[1];
7345 p[1] = *p;
7346 *p = c;
7347 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007348
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007349 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00007350 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007351 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7352 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007353 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007354 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007355 p = fword + sp->ts_fidx;
7356#ifdef FEAT_MBYTE
7357 if (has_mbyte)
7358 {
7359 n = mb_ptr2len_check(p);
7360 n += mb_ptr2len_check(p + n);
7361 c = mb_ptr2char(p + n);
7362 tl = mb_ptr2len_check(p + n);
7363 mch_memmove(p + tl, p, n);
7364 mb_char2bytes(c, p);
7365 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
7366 }
7367 else
7368#endif
7369 {
7370 c = p[2];
7371 p[2] = p[1];
7372 p[1] = *p;
7373 *p = c;
7374 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7375 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007376 }
7377 else
7378 sp->ts_state = STATE_REP_INI;
7379 break;
7380
Bram Moolenaarea424162005-06-16 21:51:00 +00007381 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007382 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00007383 p = fword + sp->ts_fidx;
7384#ifdef FEAT_MBYTE
7385 if (has_mbyte)
7386 {
7387 c = mb_ptr2char(p);
7388 tl = MB_BYTE2LEN(*p);
7389 n = MB_BYTE2LEN(p[tl]);
7390 n += MB_BYTE2LEN(p[tl + n]);
7391 mch_memmove(p, p + tl, n);
7392 mb_char2bytes(c, p + n);
7393 }
7394 else
7395#endif
7396 {
7397 c = *p;
7398 *p = p[1];
7399 p[1] = p[2];
7400 p[2] = c;
7401 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007402 /*FALLTHROUGH*/
7403
7404 case STATE_REP_INI:
7405 /* Check if matching with REP items from the .aff file would
7406 * work. Quickly skip if there are no REP items or the score
7407 * is going to be too high anyway. */
7408 gap = &lp->lp_slang->sl_rep;
7409 if (gap->ga_len == 0
7410 || sp->ts_score + SCORE_REP >= su->su_maxscore)
7411 {
7412 sp->ts_state = STATE_FINAL;
7413 break;
7414 }
7415
7416 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00007417 * may match. If the index is -1 there is none. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007418 sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]];
7419 if (sp->ts_curi < 0)
7420 {
7421 sp->ts_state = STATE_FINAL;
7422 break;
7423 }
7424
7425 sp->ts_state = STATE_REP;
7426 /*FALLTHROUGH*/
7427
7428 case STATE_REP:
7429 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00007430 * match replace the characters and check if the resulting
7431 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007432 p = fword + sp->ts_fidx;
7433
7434 gap = &lp->lp_slang->sl_rep;
7435 while (sp->ts_curi < gap->ga_len)
7436 {
7437 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
7438 if (*ftp->ft_from != *p)
7439 {
7440 /* past possible matching entries */
7441 sp->ts_curi = gap->ga_len;
7442 break;
7443 }
7444 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
7445 && try_deeper(su, stack, depth, SCORE_REP))
7446 {
7447 /* Need to undo this afterwards. */
7448 sp->ts_state = STATE_REP_UNDO;
7449
7450 /* Change the "from" to the "to" string. */
7451 ++depth;
7452 fl = STRLEN(ftp->ft_from);
7453 tl = STRLEN(ftp->ft_to);
7454 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007455 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007456 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007457 repextra += tl - fl;
7458 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007459 mch_memmove(p, ftp->ft_to, tl);
7460 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +00007461#ifdef FEAT_MBYTE
7462 stack[depth].ts_tcharlen = 0;
7463#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007464 break;
7465 }
7466 }
7467
7468 if (sp->ts_curi >= gap->ga_len)
7469 /* No (more) matches. */
7470 sp->ts_state = STATE_FINAL;
7471
7472 break;
7473
7474 case STATE_REP_UNDO:
7475 /* Undo a REP replacement and continue with the next one. */
7476 ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data
7477 + sp->ts_curi - 1;
7478 fl = STRLEN(ftp->ft_from);
7479 tl = STRLEN(ftp->ft_to);
7480 p = fword + sp->ts_fidx;
7481 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007482 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007483 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007484 repextra -= tl - fl;
7485 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007486 mch_memmove(p, ftp->ft_from, fl);
7487 sp->ts_state = STATE_REP;
7488 break;
7489
7490 default:
7491 /* Did all possible states at this level, go up one level. */
7492 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007493
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007494 if (depth >= 0 && stack[depth].ts_prefixdepth == PREFIXTREE)
7495 {
7496 /* Continue in or go back to the prefix tree. */
7497 byts = pbyts;
7498 idxs = pidxs;
7499 splitoff = 0;
7500 }
7501
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007502 /* Don't check for CTRL-C too often, it takes time. */
7503 line_breakcheck();
7504 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007505 }
7506 }
7507}
7508
7509/*
7510 * Try going one level deeper in the tree.
7511 */
7512 static int
7513try_deeper(su, stack, depth, score_add)
7514 suginfo_T *su;
7515 trystate_T *stack;
7516 int depth;
7517 int score_add;
7518{
7519 int newscore;
7520
7521 /* Refuse to go deeper if the scrore is getting too big. */
7522 newscore = stack[depth].ts_score + score_add;
7523 if (newscore >= su->su_maxscore)
7524 return FALSE;
7525
Bram Moolenaarea424162005-06-16 21:51:00 +00007526 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007527 stack[depth + 1].ts_state = STATE_START;
7528 stack[depth + 1].ts_score = newscore;
7529 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007530 return TRUE;
7531}
7532
7533/*
7534 * "fword" is a good word with case folded. Find the matching keep-case
7535 * words and put it in "kword".
7536 * Theoretically there could be several keep-case words that result in the
7537 * same case-folded word, but we only find one...
7538 */
7539 static void
7540find_keepcap_word(slang, fword, kword)
7541 slang_T *slang;
7542 char_u *fword;
7543 char_u *kword;
7544{
7545 char_u uword[MAXWLEN]; /* "fword" in upper-case */
7546 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007547 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007548
7549 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007550 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007551 int round[MAXWLEN];
7552 int fwordidx[MAXWLEN];
7553 int uwordidx[MAXWLEN];
7554 int kwordlen[MAXWLEN];
7555
7556 int flen, ulen;
7557 int l;
7558 int len;
7559 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007560 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007561 char_u *p;
7562 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007563 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007564
7565 if (byts == NULL)
7566 {
7567 /* array is empty: "cannot happen" */
7568 *kword = NUL;
7569 return;
7570 }
7571
7572 /* Make an all-cap version of "fword". */
7573 allcap_copy(fword, uword);
7574
7575 /*
7576 * Each character needs to be tried both case-folded and upper-case.
7577 * All this gets very complicated if we keep in mind that changing case
7578 * may change the byte length of a multi-byte character...
7579 */
7580 depth = 0;
7581 arridx[0] = 0;
7582 round[0] = 0;
7583 fwordidx[0] = 0;
7584 uwordidx[0] = 0;
7585 kwordlen[0] = 0;
7586 while (depth >= 0)
7587 {
7588 if (fword[fwordidx[depth]] == NUL)
7589 {
7590 /* We are at the end of "fword". If the tree allows a word to end
7591 * here we have found a match. */
7592 if (byts[arridx[depth] + 1] == 0)
7593 {
7594 kword[kwordlen[depth]] = NUL;
7595 return;
7596 }
7597
7598 /* kword is getting too long, continue one level up */
7599 --depth;
7600 }
7601 else if (++round[depth] > 2)
7602 {
7603 /* tried both fold-case and upper-case character, continue one
7604 * level up */
7605 --depth;
7606 }
7607 else
7608 {
7609 /*
7610 * round[depth] == 1: Try using the folded-case character.
7611 * round[depth] == 2: Try using the upper-case character.
7612 */
7613#ifdef FEAT_MBYTE
7614 if (has_mbyte)
7615 {
7616 flen = mb_ptr2len_check(fword + fwordidx[depth]);
7617 ulen = mb_ptr2len_check(uword + uwordidx[depth]);
7618 }
7619 else
7620#endif
7621 ulen = flen = 1;
7622 if (round[depth] == 1)
7623 {
7624 p = fword + fwordidx[depth];
7625 l = flen;
7626 }
7627 else
7628 {
7629 p = uword + uwordidx[depth];
7630 l = ulen;
7631 }
7632
7633 for (tryidx = arridx[depth]; l > 0; --l)
7634 {
7635 /* Perform a binary search in the list of accepted bytes. */
7636 len = byts[tryidx++];
7637 c = *p++;
7638 lo = tryidx;
7639 hi = tryidx + len - 1;
7640 while (lo < hi)
7641 {
7642 m = (lo + hi) / 2;
7643 if (byts[m] > c)
7644 hi = m - 1;
7645 else if (byts[m] < c)
7646 lo = m + 1;
7647 else
7648 {
7649 lo = hi = m;
7650 break;
7651 }
7652 }
7653
7654 /* Stop if there is no matching byte. */
7655 if (hi < lo || byts[lo] != c)
7656 break;
7657
7658 /* Continue at the child (if there is one). */
7659 tryidx = idxs[lo];
7660 }
7661
7662 if (l == 0)
7663 {
7664 /*
7665 * Found the matching char. Copy it to "kword" and go a
7666 * level deeper.
7667 */
7668 if (round[depth] == 1)
7669 {
7670 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
7671 flen);
7672 kwordlen[depth + 1] = kwordlen[depth] + flen;
7673 }
7674 else
7675 {
7676 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
7677 ulen);
7678 kwordlen[depth + 1] = kwordlen[depth] + ulen;
7679 }
7680 fwordidx[depth + 1] = fwordidx[depth] + flen;
7681 uwordidx[depth + 1] = uwordidx[depth] + ulen;
7682
7683 ++depth;
7684 arridx[depth] = tryidx;
7685 round[depth] = 0;
7686 }
7687 }
7688 }
7689
7690 /* Didn't find it: "cannot happen". */
7691 *kword = NUL;
7692}
7693
7694/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007695 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
7696 * su->su_sga.
7697 */
7698 static void
7699score_comp_sal(su)
7700 suginfo_T *su;
7701{
7702 langp_T *lp;
7703 char_u badsound[MAXWLEN];
7704 int i;
7705 suggest_T *stp;
7706 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007707 int score;
7708
7709 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
7710 return;
7711
7712 /* Use the sound-folding of the first language that supports it. */
7713 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7714 lp->lp_slang != NULL; ++lp)
7715 if (lp->lp_slang->sl_sal.ga_len > 0)
7716 {
7717 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007718 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007719
7720 for (i = 0; i < su->su_ga.ga_len; ++i)
7721 {
7722 stp = &SUG(su->su_ga, i);
7723
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007724 /* Case-fold the suggested word, sound-fold it and compute the
7725 * sound-a-like score. */
7726 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007727 if (score < SCORE_MAXMAX)
7728 {
7729 /* Add the suggestion. */
7730 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
7731 sstp->st_word = vim_strsave(stp->st_word);
7732 if (sstp->st_word != NULL)
7733 {
7734 sstp->st_score = score;
7735 sstp->st_altscore = 0;
7736 sstp->st_orglen = stp->st_orglen;
7737 ++su->su_sga.ga_len;
7738 }
7739 }
7740 }
7741 break;
7742 }
7743}
7744
7745/*
7746 * Combine the list of suggestions in su->su_ga and su->su_sga.
7747 * They are intwined.
7748 */
7749 static void
7750score_combine(su)
7751 suginfo_T *su;
7752{
7753 int i;
7754 int j;
7755 garray_T ga;
7756 garray_T *gap;
7757 langp_T *lp;
7758 suggest_T *stp;
7759 char_u *p;
7760 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007761 int round;
7762
7763 /* Add the alternate score to su_ga. */
7764 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7765 lp->lp_slang != NULL; ++lp)
7766 {
7767 if (lp->lp_slang->sl_sal.ga_len > 0)
7768 {
7769 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007770 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007771
7772 for (i = 0; i < su->su_ga.ga_len; ++i)
7773 {
7774 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007775 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
7776 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007777 if (stp->st_altscore == SCORE_MAXMAX)
7778 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
7779 else
7780 stp->st_score = (stp->st_score * 3
7781 + stp->st_altscore) / 4;
7782 stp->st_salscore = FALSE;
7783 }
7784 break;
7785 }
7786 }
7787
7788 /* Add the alternate score to su_sga. */
7789 for (i = 0; i < su->su_sga.ga_len; ++i)
7790 {
7791 stp = &SUG(su->su_sga, i);
7792 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
7793 if (stp->st_score == SCORE_MAXMAX)
7794 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
7795 else
7796 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
7797 stp->st_salscore = TRUE;
7798 }
7799
7800 /* Sort the suggestions and truncate at "maxcount" for both lists. */
7801 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
7802 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
7803
7804 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
7805 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
7806 return;
7807
7808 stp = &SUG(ga, 0);
7809 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
7810 {
7811 /* round 1: get a suggestion from su_ga
7812 * round 2: get a suggestion from su_sga */
7813 for (round = 1; round <= 2; ++round)
7814 {
7815 gap = round == 1 ? &su->su_ga : &su->su_sga;
7816 if (i < gap->ga_len)
7817 {
7818 /* Don't add a word if it's already there. */
7819 p = SUG(*gap, i).st_word;
7820 for (j = 0; j < ga.ga_len; ++j)
7821 if (STRCMP(stp[j].st_word, p) == 0)
7822 break;
7823 if (j == ga.ga_len)
7824 stp[ga.ga_len++] = SUG(*gap, i);
7825 else
7826 vim_free(p);
7827 }
7828 }
7829 }
7830
7831 ga_clear(&su->su_ga);
7832 ga_clear(&su->su_sga);
7833
7834 /* Truncate the list to the number of suggestions that will be displayed. */
7835 if (ga.ga_len > su->su_maxcount)
7836 {
7837 for (i = su->su_maxcount; i < ga.ga_len; ++i)
7838 vim_free(stp[i].st_word);
7839 ga.ga_len = su->su_maxcount;
7840 }
7841
7842 su->su_ga = ga;
7843}
7844
7845/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007846 * For the goodword in "stp" compute the soundalike score compared to the
7847 * badword.
7848 */
7849 static int
7850stp_sal_score(stp, su, slang, badsound)
7851 suggest_T *stp;
7852 suginfo_T *su;
7853 slang_T *slang;
7854 char_u *badsound; /* sound-folded badword */
7855{
7856 char_u *p;
7857 char_u badsound2[MAXWLEN];
7858 char_u fword[MAXWLEN];
7859 char_u goodsound[MAXWLEN];
7860
7861 if (stp->st_orglen <= su->su_badlen)
7862 p = badsound;
7863 else
7864 {
7865 /* soundfold the bad word with more characters following */
7866 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
7867
7868 /* When joining two words the sound often changes a lot. E.g., "t he"
7869 * sounds like "t h" while "the" sounds like "@". Avoid that by
7870 * removing the space. Don't do it when the good word also contains a
7871 * space. */
7872 if (vim_iswhite(su->su_badptr[su->su_badlen])
7873 && *skiptowhite(stp->st_word) == NUL)
7874 for (p = fword; *(p = skiptowhite(p)) != NUL; )
7875 mch_memmove(p, p + 1, STRLEN(p));
7876
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007877 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007878 p = badsound2;
7879 }
7880
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007881 /* Sound-fold the word and compute the score for the difference. */
7882 spell_soundfold(slang, stp->st_word, FALSE, goodsound);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007883
7884 return soundalike_score(goodsound, p);
7885}
7886
7887/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007888 * Find suggestions by comparing the word in a sound-a-like form.
7889 */
7890 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00007891suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007892 suginfo_T *su;
7893{
7894 char_u salword[MAXWLEN];
7895 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007896 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007897 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007898 int curi[MAXWLEN];
7899 langp_T *lp;
7900 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007901 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007902 int depth;
7903 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007904 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007905 int round;
7906 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007907 int sound_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007908
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007909 /* Do this for all languages that support sound folding. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007910 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7911 lp->lp_slang != NULL; ++lp)
7912 {
7913 if (lp->lp_slang->sl_sal.ga_len > 0)
7914 {
7915 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007916 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007917
7918 /*
7919 * Go through the whole tree, soundfold each word and compare.
7920 * round 1: use the case-folded tree.
7921 * round 2: use the keep-case tree.
7922 */
7923 for (round = 1; round <= 2; ++round)
7924 {
7925 if (round == 1)
7926 {
7927 byts = lp->lp_slang->sl_fbyts;
7928 idxs = lp->lp_slang->sl_fidxs;
7929 }
7930 else
7931 {
7932 byts = lp->lp_slang->sl_kbyts;
7933 idxs = lp->lp_slang->sl_kidxs;
7934 }
7935
7936 depth = 0;
7937 arridx[0] = 0;
7938 curi[0] = 1;
7939 while (depth >= 0 && !got_int)
7940 {
7941 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007942 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007943 /* Done all bytes at this node, go up one level. */
7944 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007945 line_breakcheck();
7946 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007947 else
7948 {
7949 /* Do one more byte at this node. */
7950 n = arridx[depth] + curi[depth];
7951 ++curi[depth];
7952 c = byts[n];
7953 if (c == 0)
7954 {
7955 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007956 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007957 if (round == 2 || (flags & WF_KEEPCAP) == 0)
7958 {
7959 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007960 /* Sound-fold. Only in keep-case tree need to
7961 * case-fold the word. */
7962 spell_soundfold(lp->lp_slang, tword,
7963 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007964
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007965 /* Compute the edit distance between the
7966 * sound-a-like words. */
7967 sound_score = soundalike_score(salword,
7968 tsalword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007969 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007970 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007971 char_u cword[MAXWLEN];
7972 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007973 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007974
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007975 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007976 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007977 /* Need to fix case according to
7978 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007979 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007980 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007981 }
7982 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007983 p = tword;
7984
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007985 if (sps_flags & SPS_DOUBLE)
7986 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007987 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007988 sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007989 else
7990 {
7991 /* Compute the score. */
7992 score = spell_edit_score(
7993 su->su_badword, p);
7994 if (sps_flags & SPS_BEST)
7995 /* give a bonus for the good word
7996 * sounding the same as the bad
7997 * word */
7998 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007999 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008000 RESCORE(score, sound_score),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008001 sound_score, TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008002 else
8003 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008004 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008005 score + sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008006 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008007 }
8008 }
8009
8010 /* Skip over other NUL bytes. */
8011 while (byts[n + 1] == 0)
8012 {
8013 ++n;
8014 ++curi[depth];
8015 }
8016 }
8017 else
8018 {
8019 /* Normal char, go one level deeper. */
8020 tword[depth++] = c;
8021 arridx[depth] = idxs[n];
8022 curi[depth] = 1;
8023 }
8024 }
8025 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008026 }
8027 }
8028 }
8029}
8030
8031/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008032 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008033 */
8034 static void
8035make_case_word(fword, cword, flags)
8036 char_u *fword;
8037 char_u *cword;
8038 int flags;
8039{
8040 if (flags & WF_ALLCAP)
8041 /* Make it all upper-case */
8042 allcap_copy(fword, cword);
8043 else if (flags & WF_ONECAP)
8044 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008045 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008046 else
8047 /* Use goodword as-is. */
8048 STRCPY(cword, fword);
8049}
8050
Bram Moolenaarea424162005-06-16 21:51:00 +00008051/*
8052 * Use map string "map" for languages "lp".
8053 */
8054 static void
8055set_map_str(lp, map)
8056 slang_T *lp;
8057 char_u *map;
8058{
8059 char_u *p;
8060 int headc = 0;
8061 int c;
8062 int i;
8063
8064 if (*map == NUL)
8065 {
8066 lp->sl_has_map = FALSE;
8067 return;
8068 }
8069 lp->sl_has_map = TRUE;
8070
8071 /* Init the array and hash table empty. */
8072 for (i = 0; i < 256; ++i)
8073 lp->sl_map_array[i] = 0;
8074#ifdef FEAT_MBYTE
8075 hash_init(&lp->sl_map_hash);
8076#endif
8077
8078 /*
8079 * The similar characters are stored separated with slashes:
8080 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
8081 * before the same slash. For characters above 255 sl_map_hash is used.
8082 */
8083 for (p = map; *p != NUL; )
8084 {
8085#ifdef FEAT_MBYTE
8086 c = mb_ptr2char_adv(&p);
8087#else
8088 c = *p++;
8089#endif
8090 if (c == '/')
8091 headc = 0;
8092 else
8093 {
8094 if (headc == 0)
8095 headc = c;
8096
8097#ifdef FEAT_MBYTE
8098 /* Characters above 255 don't fit in sl_map_array[], put them in
8099 * the hash table. Each entry is the char, a NUL the headchar and
8100 * a NUL. */
8101 if (c >= 256)
8102 {
8103 int cl = mb_char2len(c);
8104 int headcl = mb_char2len(headc);
8105 char_u *b;
8106 hash_T hash;
8107 hashitem_T *hi;
8108
8109 b = alloc((unsigned)(cl + headcl + 2));
8110 if (b == NULL)
8111 return;
8112 mb_char2bytes(c, b);
8113 b[cl] = NUL;
8114 mb_char2bytes(headc, b + cl + 1);
8115 b[cl + 1 + headcl] = NUL;
8116 hash = hash_hash(b);
8117 hi = hash_lookup(&lp->sl_map_hash, b, hash);
8118 if (HASHITEM_EMPTY(hi))
8119 hash_add_item(&lp->sl_map_hash, hi, b, hash);
8120 else
8121 {
8122 /* This should have been checked when generating the .spl
8123 * file. */
8124 EMSG(_("E999: duplicate char in MAP entry"));
8125 vim_free(b);
8126 }
8127 }
8128 else
8129#endif
8130 lp->sl_map_array[c] = headc;
8131 }
8132 }
8133}
8134
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008135/*
8136 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
8137 * lines in the .aff file.
8138 */
8139 static int
8140similar_chars(slang, c1, c2)
8141 slang_T *slang;
8142 int c1;
8143 int c2;
8144{
Bram Moolenaarea424162005-06-16 21:51:00 +00008145 int m1, m2;
8146#ifdef FEAT_MBYTE
8147 char_u buf[MB_MAXBYTES];
8148 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008149
Bram Moolenaarea424162005-06-16 21:51:00 +00008150 if (c1 >= 256)
8151 {
8152 buf[mb_char2bytes(c1, buf)] = 0;
8153 hi = hash_find(&slang->sl_map_hash, buf);
8154 if (HASHITEM_EMPTY(hi))
8155 m1 = 0;
8156 else
8157 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8158 }
8159 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008160#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00008161 m1 = slang->sl_map_array[c1];
8162 if (m1 == 0)
8163 return FALSE;
8164
8165
8166#ifdef FEAT_MBYTE
8167 if (c2 >= 256)
8168 {
8169 buf[mb_char2bytes(c2, buf)] = 0;
8170 hi = hash_find(&slang->sl_map_hash, buf);
8171 if (HASHITEM_EMPTY(hi))
8172 m2 = 0;
8173 else
8174 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8175 }
8176 else
8177#endif
8178 m2 = slang->sl_map_array[c2];
8179
8180 return m1 == m2;
8181}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008182
8183/*
8184 * Add a suggestion to the list of suggestions.
8185 * Do not add a duplicate suggestion or suggestions with a bad score.
8186 * When "use_score" is not zero it's used, otherwise the score is computed
8187 * with spell_edit_score().
8188 */
8189 static void
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008190add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008191 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008192 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008193 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008194 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008195 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008196 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008197 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008198{
8199 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008200 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008201 char_u *p = NULL;
8202 int c = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008203
8204 /* Check that the word wasn't banned. */
8205 if (was_banned(su, goodword))
8206 return;
8207
Bram Moolenaar0c405862005-06-22 22:26:26 +00008208 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
8209 * Remove the common part from "goodword". */
8210 i = badlen - su->su_badlen;
8211 if (i > 0)
8212 {
8213 /* This assumes there was no case folding or it didn't change the
8214 * length... */
8215 p = goodword + STRLEN(goodword) - i;
8216 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
8217 {
8218 badlen = su->su_badlen;
8219 c = *p;
8220 *p = NUL;
8221 }
8222 else
8223 p = NULL;
8224 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008225 else if (i < 0)
8226 {
8227 /* When replacing part of the word check that we actually change
8228 * something. For "the the" a suggestion can be replacing the first
8229 * "the" with itself, since "the" wasn't banned. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008230 if (badlen == (int)STRLEN(goodword)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008231 && STRNCMP(su->su_badword, goodword, badlen) == 0)
8232 return;
8233 }
8234
Bram Moolenaar0c405862005-06-22 22:26:26 +00008235
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008236 if (score <= su->su_maxscore)
8237 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008238 /* Check if the word is already there. Also check the length that is
8239 * being replaced "thes," -> "these" is a different suggestion from
8240 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008241 stp = &SUG(*gap, 0);
8242 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008243 if (STRCMP(stp[i].st_word, goodword) == 0
8244 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008245 {
8246 /* Found it. Remember the lowest score. */
8247 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008248 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008249 stp[i].st_score = score;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008250 stp[i].st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008251 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008252 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008253 break;
8254 }
8255
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008256 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008257 {
8258 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008259 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008260 stp->st_word = vim_strsave(goodword);
8261 if (stp->st_word != NULL)
8262 {
8263 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008264 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008265 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008266 stp->st_orglen = badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008267 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008268
8269 /* If we have too many suggestions now, sort the list and keep
8270 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008271 if (gap->ga_len > SUG_MAX_COUNT(su))
8272 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
8273 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008274 }
8275 }
8276 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008277
8278 if (p != NULL)
8279 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008280}
8281
8282/*
8283 * Add a word to be banned.
8284 */
8285 static void
8286add_banned(su, word)
8287 suginfo_T *su;
8288 char_u *word;
8289{
8290 char_u *s = vim_strsave(word);
8291 hash_T hash;
8292 hashitem_T *hi;
8293
8294 if (s != NULL)
8295 {
8296 hash = hash_hash(s);
8297 hi = hash_lookup(&su->su_banned, s, hash);
8298 if (HASHITEM_EMPTY(hi))
8299 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008300 else
8301 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008302 }
8303}
8304
8305/*
8306 * Return TRUE if a word appears in the list of banned words.
8307 */
8308 static int
8309was_banned(su, word)
8310 suginfo_T *su;
8311 char_u *word;
8312{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008313 hashitem_T *hi = hash_find(&su->su_banned, word);
8314
8315 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008316}
8317
8318/*
8319 * Free the banned words in "su".
8320 */
8321 static void
8322free_banned(su)
8323 suginfo_T *su;
8324{
8325 int todo;
8326 hashitem_T *hi;
8327
8328 todo = su->su_banned.ht_used;
8329 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
8330 {
8331 if (!HASHITEM_EMPTY(hi))
8332 {
8333 vim_free(hi->hi_key);
8334 --todo;
8335 }
8336 }
8337 hash_clear(&su->su_banned);
8338}
8339
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008340/*
8341 * Recompute the score if sound-folding is possible. This is slow,
8342 * thus only done for the final results.
8343 */
8344 static void
8345rescore_suggestions(su)
8346 suginfo_T *su;
8347{
8348 langp_T *lp;
8349 suggest_T *stp;
8350 char_u sal_badword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008351 int i;
8352
8353 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8354 lp->lp_slang != NULL; ++lp)
8355 {
8356 if (lp->lp_slang->sl_sal.ga_len > 0)
8357 {
8358 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008359 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008360
8361 for (i = 0; i < su->su_ga.ga_len; ++i)
8362 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008363 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008364 if (!stp->st_had_bonus)
8365 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008366 stp->st_altscore = stp_sal_score(stp, su,
8367 lp->lp_slang, sal_badword);
8368 if (stp->st_altscore == SCORE_MAXMAX)
8369 stp->st_altscore = SCORE_BIG;
8370 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008371 }
8372 }
8373 break;
8374 }
8375 }
8376}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008377
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008378static int
8379#ifdef __BORLANDC__
8380_RTLENTRYF
8381#endif
8382sug_compare __ARGS((const void *s1, const void *s2));
8383
8384/*
8385 * Function given to qsort() to sort the suggestions on st_score.
8386 */
8387 static int
8388#ifdef __BORLANDC__
8389_RTLENTRYF
8390#endif
8391sug_compare(s1, s2)
8392 const void *s1;
8393 const void *s2;
8394{
8395 suggest_T *p1 = (suggest_T *)s1;
8396 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008397 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008398
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008399 if (n == 0)
8400 return p1->st_altscore - p2->st_altscore;
8401 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008402}
8403
8404/*
8405 * Cleanup the suggestions:
8406 * - Sort on score.
8407 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008408 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008409 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008410 static int
8411cleanup_suggestions(gap, maxscore, keep)
8412 garray_T *gap;
8413 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008414 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008415{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008416 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008417 int i;
8418
8419 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008420 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008421
8422 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008423 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008424 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008425 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008426 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008427 gap->ga_len = keep;
8428 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008429 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008430 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008431}
8432
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008433#if defined(FEAT_EVAL) || defined(PROTO)
8434/*
8435 * Soundfold a string, for soundfold().
8436 * Result is in allocated memory, NULL for an error.
8437 */
8438 char_u *
8439eval_soundfold(word)
8440 char_u *word;
8441{
8442 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008443 char_u sound[MAXWLEN];
8444
8445 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
8446 /* Use the sound-folding of the first language that supports it. */
8447 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8448 lp->lp_slang != NULL; ++lp)
8449 if (lp->lp_slang->sl_sal.ga_len > 0)
8450 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008451 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008452 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008453 return vim_strsave(sound);
8454 }
8455
8456 /* No language with sound folding, return word as-is. */
8457 return vim_strsave(word);
8458}
8459#endif
8460
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008461/*
8462 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
8463 */
8464 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008465spell_soundfold(slang, inword, folded, res)
8466 slang_T *slang;
8467 char_u *inword;
8468 int folded; /* "inword" is already case-folded */
8469 char_u *res;
8470{
8471 char_u fword[MAXWLEN];
8472 char_u *word;
8473
8474 if (slang->sl_sofo)
8475 /* SOFOFROM and SOFOTO used */
8476 spell_soundfold_sofo(slang, inword, res);
8477 else
8478 {
8479 /* SAL items used. Requires the word to be case-folded. */
8480 if (folded)
8481 word = inword;
8482 else
8483 {
8484 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
8485 word = fword;
8486 }
8487
8488#ifdef FEAT_MBYTE
8489 if (has_mbyte)
8490 spell_soundfold_wsal(slang, word, res);
8491 else
8492#endif
8493 spell_soundfold_sal(slang, word, res);
8494 }
8495}
8496
8497/*
8498 * Perform sound folding of "inword" into "res" according to SOFOFROM and
8499 * SOFOTO lines.
8500 */
8501 static void
8502spell_soundfold_sofo(slang, inword, res)
8503 slang_T *slang;
8504 char_u *inword;
8505 char_u *res;
8506{
8507 char_u *s;
8508 int ri = 0;
8509 int c;
8510
8511#ifdef FEAT_MBYTE
8512 if (has_mbyte)
8513 {
8514 int prevc = 0;
8515 int *ip;
8516
8517 /* The sl_sal_first[] table contains the translation for chars up to
8518 * 255, sl_sal the rest. */
8519 for (s = inword; *s != NUL; )
8520 {
8521 c = mb_ptr2char_adv(&s);
8522 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
8523 c = ' ';
8524 else if (c < 256)
8525 c = slang->sl_sal_first[c];
8526 else
8527 {
8528 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
8529 if (ip == NULL) /* empty list, can't match */
8530 c = NUL;
8531 else
8532 for (;;) /* find "c" in the list */
8533 {
8534 if (*ip == 0) /* not found */
8535 {
8536 c = NUL;
8537 break;
8538 }
8539 if (*ip == c) /* match! */
8540 {
8541 c = ip[1];
8542 break;
8543 }
8544 ip += 2;
8545 }
8546 }
8547
8548 if (c != NUL && c != prevc)
8549 {
8550 ri += mb_char2bytes(c, res + ri);
8551 if (ri + MB_MAXBYTES > MAXWLEN)
8552 break;
8553 prevc = c;
8554 }
8555 }
8556 }
8557 else
8558#endif
8559 {
8560 /* The sl_sal_first[] table contains the translation. */
8561 for (s = inword; (c = *s) != NUL; ++s)
8562 {
8563 if (vim_iswhite(c))
8564 c = ' ';
8565 else
8566 c = slang->sl_sal_first[c];
8567 if (c != NUL && (ri == 0 || res[ri - 1] != c))
8568 res[ri++] = c;
8569 }
8570 }
8571
8572 res[ri] = NUL;
8573}
8574
8575 static void
8576spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008577 slang_T *slang;
8578 char_u *inword;
8579 char_u *res;
8580{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008581 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008582 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008583 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008584 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008585 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008586 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008587 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008588 int n, k = 0;
8589 int z0;
8590 int k0;
8591 int n0;
8592 int c;
8593 int pri;
8594 int p0 = -333;
8595 int c0;
8596
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008597 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008598 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008599 if (slang->sl_rem_accents)
8600 {
8601 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008602 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008603 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008604 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008605 {
8606 *t++ = ' ';
8607 s = skipwhite(s);
8608 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008609 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008610 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008611 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008612 *t++ = *s;
8613 ++s;
8614 }
8615 }
8616 *t = NUL;
8617 }
8618 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008619 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008620
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008621 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008622
8623 /*
8624 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008625 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008626 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008627 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008628 while ((c = word[i]) != NUL)
8629 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008630 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008631 n = slang->sl_sal_first[c];
8632 z0 = 0;
8633
8634 if (n >= 0)
8635 {
8636 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008637 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008638 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008639 /* Quickly skip entries that don't match the word. Most
8640 * entries are less then three chars, optimize for that. */
8641 k = smp[n].sm_leadlen;
8642 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008643 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008644 if (word[i + 1] != s[1])
8645 continue;
8646 if (k > 2)
8647 {
8648 for (j = 2; j < k; ++j)
8649 if (word[i + j] != s[j])
8650 break;
8651 if (j < k)
8652 continue;
8653 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008654 }
8655
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008656 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008657 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008658 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008659 while (*pf != NUL && *pf != word[i + k])
8660 ++pf;
8661 if (*pf == NUL)
8662 continue;
8663 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008664 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008665 s = smp[n].sm_rules;
8666 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008667
8668 p0 = *s;
8669 k0 = k;
8670 while (*s == '-' && k > 1)
8671 {
8672 k--;
8673 s++;
8674 }
8675 if (*s == '<')
8676 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008677 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008678 {
8679 /* determine priority */
8680 pri = *s - '0';
8681 s++;
8682 }
8683 if (*s == '^' && *(s + 1) == '^')
8684 s++;
8685
8686 if (*s == NUL
8687 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008688 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008689 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008690 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008691 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008692 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008693 && spell_iswordp(word + i - 1, curbuf)
8694 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008695 {
8696 /* search for followup rules, if: */
8697 /* followup and k > 1 and NO '-' in searchstring */
8698 c0 = word[i + k - 1];
8699 n0 = slang->sl_sal_first[c0];
8700
8701 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008702 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008703 {
8704 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008705 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008706 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008707 /* Quickly skip entries that don't match the word.
8708 * */
8709 k0 = smp[n0].sm_leadlen;
8710 if (k0 > 1)
8711 {
8712 if (word[i + k] != s[1])
8713 continue;
8714 if (k0 > 2)
8715 {
8716 pf = word + i + k + 1;
8717 for (j = 2; j < k0; ++j)
8718 if (*pf++ != s[j])
8719 break;
8720 if (j < k0)
8721 continue;
8722 }
8723 }
8724 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008725
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008726 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008727 {
8728 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008729 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008730 while (*pf != NUL && *pf != word[i + k0])
8731 ++pf;
8732 if (*pf == NUL)
8733 continue;
8734 ++k0;
8735 }
8736
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008737 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008738 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008739 while (*s == '-')
8740 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008741 /* "k0" gets NOT reduced because
8742 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008743 s++;
8744 }
8745 if (*s == '<')
8746 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008747 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008748 {
8749 p0 = *s - '0';
8750 s++;
8751 }
8752
8753 if (*s == NUL
8754 /* *s == '^' cuts */
8755 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008756 && !spell_iswordp(word + i + k0,
8757 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008758 {
8759 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008760 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008761 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008762
8763 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008764 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008765 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008766 /* rule fits; stop search */
8767 break;
8768 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008769 }
8770
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008771 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008772 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008773 }
8774
8775 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008776 s = smp[n].sm_to;
8777 pf = smp[n].sm_rules;
8778 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008779 if (p0 == 1 && z == 0)
8780 {
8781 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008782 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
8783 || res[reslen - 1] == *s))
8784 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008785 z0 = 1;
8786 z = 1;
8787 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008788 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008789 {
8790 word[i + k0] = *s;
8791 k0++;
8792 s++;
8793 }
8794 if (k > k0)
8795 mch_memmove(word + i + k0, word + i + k,
8796 STRLEN(word + i + k) + 1);
8797
8798 /* new "actual letter" */
8799 c = word[i];
8800 }
8801 else
8802 {
8803 /* no '<' rule used */
8804 i += k - 1;
8805 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008806 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008807 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008808 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008809 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008810 s++;
8811 }
8812 /* new "actual letter" */
8813 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008814 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008815 {
8816 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008817 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008818 mch_memmove(word, word + i + 1,
8819 STRLEN(word + i + 1) + 1);
8820 i = 0;
8821 z0 = 1;
8822 }
8823 }
8824 break;
8825 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008826 }
8827 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008828 else if (vim_iswhite(c))
8829 {
8830 c = ' ';
8831 k = 1;
8832 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008833
8834 if (z0 == 0)
8835 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008836 if (k && !p0 && reslen < MAXWLEN && c != NUL
8837 && (!slang->sl_collapse || reslen == 0
8838 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008839 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008840 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008841
8842 i++;
8843 z = 0;
8844 k = 0;
8845 }
8846 }
8847
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008848 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008849}
8850
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008851#ifdef FEAT_MBYTE
8852/*
8853 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
8854 * Multi-byte version of spell_soundfold().
8855 */
8856 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008857spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008858 slang_T *slang;
8859 char_u *inword;
8860 char_u *res;
8861{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008862 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008863 int word[MAXWLEN];
8864 int wres[MAXWLEN];
8865 int l;
8866 char_u *s;
8867 int *ws;
8868 char_u *t;
8869 int *pf;
8870 int i, j, z;
8871 int reslen;
8872 int n, k = 0;
8873 int z0;
8874 int k0;
8875 int n0;
8876 int c;
8877 int pri;
8878 int p0 = -333;
8879 int c0;
8880 int did_white = FALSE;
8881
8882 /*
8883 * Convert the multi-byte string to a wide-character string.
8884 * Remove accents, if wanted. We actually remove all non-word characters.
8885 * But keep white space.
8886 */
8887 n = 0;
8888 for (s = inword; *s != NUL; )
8889 {
8890 t = s;
8891 c = mb_ptr2char_adv(&s);
8892 if (slang->sl_rem_accents)
8893 {
8894 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
8895 {
8896 if (did_white)
8897 continue;
8898 c = ' ';
8899 did_white = TRUE;
8900 }
8901 else
8902 {
8903 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008904 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008905 continue;
8906 }
8907 }
8908 word[n++] = c;
8909 }
8910 word[n] = NUL;
8911
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008912 /*
8913 * This comes from Aspell phonet.cpp.
8914 * Converted from C++ to C. Added support for multi-byte chars.
8915 * Changed to keep spaces.
8916 */
8917 i = reslen = z = 0;
8918 while ((c = word[i]) != NUL)
8919 {
8920 /* Start with the first rule that has the character in the word. */
8921 n = slang->sl_sal_first[c & 0xff];
8922 z0 = 0;
8923
8924 if (n >= 0)
8925 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008926 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008927 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
8928 {
8929 /* Quickly skip entries that don't match the word. Most
8930 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008931 if (c != ws[0])
8932 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008933 k = smp[n].sm_leadlen;
8934 if (k > 1)
8935 {
8936 if (word[i + 1] != ws[1])
8937 continue;
8938 if (k > 2)
8939 {
8940 for (j = 2; j < k; ++j)
8941 if (word[i + j] != ws[j])
8942 break;
8943 if (j < k)
8944 continue;
8945 }
8946 }
8947
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008948 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008949 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008950 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008951 while (*pf != NUL && *pf != word[i + k])
8952 ++pf;
8953 if (*pf == NUL)
8954 continue;
8955 ++k;
8956 }
8957 s = smp[n].sm_rules;
8958 pri = 5; /* default priority */
8959
8960 p0 = *s;
8961 k0 = k;
8962 while (*s == '-' && k > 1)
8963 {
8964 k--;
8965 s++;
8966 }
8967 if (*s == '<')
8968 s++;
8969 if (VIM_ISDIGIT(*s))
8970 {
8971 /* determine priority */
8972 pri = *s - '0';
8973 s++;
8974 }
8975 if (*s == '^' && *(s + 1) == '^')
8976 s++;
8977
8978 if (*s == NUL
8979 || (*s == '^'
8980 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008981 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008982 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008983 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008984 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008985 && spell_iswordp_w(word + i - 1, curbuf)
8986 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008987 {
8988 /* search for followup rules, if: */
8989 /* followup and k > 1 and NO '-' in searchstring */
8990 c0 = word[i + k - 1];
8991 n0 = slang->sl_sal_first[c0 & 0xff];
8992
8993 if (slang->sl_followup && k > 1 && n0 >= 0
8994 && p0 != '-' && word[i + k] != NUL)
8995 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008996 /* Test follow-up rule for "word[i + k]"; loop over
8997 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008998 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
8999 == (c0 & 0xff); ++n0)
9000 {
9001 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009002 */
9003 if (c0 != ws[0])
9004 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009005 k0 = smp[n0].sm_leadlen;
9006 if (k0 > 1)
9007 {
9008 if (word[i + k] != ws[1])
9009 continue;
9010 if (k0 > 2)
9011 {
9012 pf = word + i + k + 1;
9013 for (j = 2; j < k0; ++j)
9014 if (*pf++ != ws[j])
9015 break;
9016 if (j < k0)
9017 continue;
9018 }
9019 }
9020 k0 += k - 1;
9021
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009022 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009023 {
9024 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009025 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009026 while (*pf != NUL && *pf != word[i + k0])
9027 ++pf;
9028 if (*pf == NUL)
9029 continue;
9030 ++k0;
9031 }
9032
9033 p0 = 5;
9034 s = smp[n0].sm_rules;
9035 while (*s == '-')
9036 {
9037 /* "k0" gets NOT reduced because
9038 * "if (k0 == k)" */
9039 s++;
9040 }
9041 if (*s == '<')
9042 s++;
9043 if (VIM_ISDIGIT(*s))
9044 {
9045 p0 = *s - '0';
9046 s++;
9047 }
9048
9049 if (*s == NUL
9050 /* *s == '^' cuts */
9051 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009052 && !spell_iswordp_w(word + i + k0,
9053 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009054 {
9055 if (k0 == k)
9056 /* this is just a piece of the string */
9057 continue;
9058
9059 if (p0 < pri)
9060 /* priority too low */
9061 continue;
9062 /* rule fits; stop search */
9063 break;
9064 }
9065 }
9066
9067 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
9068 == (c0 & 0xff))
9069 continue;
9070 }
9071
9072 /* replace string */
9073 ws = smp[n].sm_to_w;
9074 s = smp[n].sm_rules;
9075 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
9076 if (p0 == 1 && z == 0)
9077 {
9078 /* rule with '<' is used */
9079 if (reslen > 0 && *ws != NUL && (wres[reslen - 1] == c
9080 || wres[reslen - 1] == *ws))
9081 reslen--;
9082 z0 = 1;
9083 z = 1;
9084 k0 = 0;
9085 while (*ws != NUL && word[i + k0] != NUL)
9086 {
9087 word[i + k0] = *ws;
9088 k0++;
9089 ws++;
9090 }
9091 if (k > k0)
9092 mch_memmove(word + i + k0, word + i + k,
9093 sizeof(int) * (STRLEN(word + i + k) + 1));
9094
9095 /* new "actual letter" */
9096 c = word[i];
9097 }
9098 else
9099 {
9100 /* no '<' rule used */
9101 i += k - 1;
9102 z = 0;
9103 while (*ws != NUL && ws[1] != NUL && reslen < MAXWLEN)
9104 {
9105 if (reslen == 0 || wres[reslen - 1] != *ws)
9106 wres[reslen++] = *ws;
9107 ws++;
9108 }
9109 /* new "actual letter" */
9110 c = *ws;
9111 if (strstr((char *)s, "^^") != NULL)
9112 {
9113 if (c != NUL)
9114 wres[reslen++] = c;
9115 mch_memmove(word, word + i + 1,
9116 sizeof(int) * (STRLEN(word + i + 1) + 1));
9117 i = 0;
9118 z0 = 1;
9119 }
9120 }
9121 break;
9122 }
9123 }
9124 }
9125 else if (vim_iswhite(c))
9126 {
9127 c = ' ';
9128 k = 1;
9129 }
9130
9131 if (z0 == 0)
9132 {
9133 if (k && !p0 && reslen < MAXWLEN && c != NUL
9134 && (!slang->sl_collapse || reslen == 0
9135 || wres[reslen - 1] != c))
9136 /* condense only double letters */
9137 wres[reslen++] = c;
9138
9139 i++;
9140 z = 0;
9141 k = 0;
9142 }
9143 }
9144
9145 /* Convert wide characters in "wres" to a multi-byte string in "res". */
9146 l = 0;
9147 for (n = 0; n < reslen; ++n)
9148 {
9149 l += mb_char2bytes(wres[n], res + l);
9150 if (l + MB_MAXBYTES > MAXWLEN)
9151 break;
9152 }
9153 res[l] = NUL;
9154}
9155#endif
9156
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009157/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009158 * Compute a score for two sound-a-like words.
9159 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
9160 * Instead of a generic loop we write out the code. That keeps it fast by
9161 * avoiding checks that will not be possible.
9162 */
9163 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009164soundalike_score(goodstart, badstart)
9165 char_u *goodstart; /* sound-folded good word */
9166 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009167{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009168 char_u *goodsound = goodstart;
9169 char_u *badsound = badstart;
9170 int goodlen;
9171 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009172 int n;
9173 char_u *pl, *ps;
9174 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009175 int score = 0;
9176
9177 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
9178 * counted so much, vowels halfway the word aren't counted at all. */
9179 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
9180 {
9181 score = SCORE_DEL / 2;
9182 if (*badsound == '*')
9183 ++badsound;
9184 else
9185 ++goodsound;
9186 }
9187
9188 goodlen = STRLEN(goodsound);
9189 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009190
9191 /* Return quickly if the lenghts are too different to be fixed by two
9192 * changes. */
9193 n = goodlen - badlen;
9194 if (n < -2 || n > 2)
9195 return SCORE_MAXMAX;
9196
9197 if (n > 0)
9198 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009199 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009200 ps = badsound;
9201 }
9202 else
9203 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009204 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009205 ps = goodsound;
9206 }
9207
9208 /* Skip over the identical part. */
9209 while (*pl == *ps && *pl != NUL)
9210 {
9211 ++pl;
9212 ++ps;
9213 }
9214
9215 switch (n)
9216 {
9217 case -2:
9218 case 2:
9219 /*
9220 * Must delete two characters from "pl".
9221 */
9222 ++pl; /* first delete */
9223 while (*pl == *ps)
9224 {
9225 ++pl;
9226 ++ps;
9227 }
9228 /* strings must be equal after second delete */
9229 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009230 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009231
9232 /* Failed to compare. */
9233 break;
9234
9235 case -1:
9236 case 1:
9237 /*
9238 * Minimal one delete from "pl" required.
9239 */
9240
9241 /* 1: delete */
9242 pl2 = pl + 1;
9243 ps2 = ps;
9244 while (*pl2 == *ps2)
9245 {
9246 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009247 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009248 ++pl2;
9249 ++ps2;
9250 }
9251
9252 /* 2: delete then swap, then rest must be equal */
9253 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9254 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009255 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009256
9257 /* 3: delete then substitute, then the rest must be equal */
9258 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009259 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009260
9261 /* 4: first swap then delete */
9262 if (pl[0] == ps[1] && pl[1] == ps[0])
9263 {
9264 pl2 = pl + 2; /* swap, skip two chars */
9265 ps2 = ps + 2;
9266 while (*pl2 == *ps2)
9267 {
9268 ++pl2;
9269 ++ps2;
9270 }
9271 /* delete a char and then strings must be equal */
9272 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009273 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009274 }
9275
9276 /* 5: first substitute then delete */
9277 pl2 = pl + 1; /* substitute, skip one char */
9278 ps2 = ps + 1;
9279 while (*pl2 == *ps2)
9280 {
9281 ++pl2;
9282 ++ps2;
9283 }
9284 /* delete a char and then strings must be equal */
9285 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009286 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009287
9288 /* Failed to compare. */
9289 break;
9290
9291 case 0:
9292 /*
9293 * Lenghts are equal, thus changes must result in same length: An
9294 * insert is only possible in combination with a delete.
9295 * 1: check if for identical strings
9296 */
9297 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009298 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009299
9300 /* 2: swap */
9301 if (pl[0] == ps[1] && pl[1] == ps[0])
9302 {
9303 pl2 = pl + 2; /* swap, skip two chars */
9304 ps2 = ps + 2;
9305 while (*pl2 == *ps2)
9306 {
9307 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009308 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009309 ++pl2;
9310 ++ps2;
9311 }
9312 /* 3: swap and swap again */
9313 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9314 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009315 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009316
9317 /* 4: swap and substitute */
9318 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009319 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009320 }
9321
9322 /* 5: substitute */
9323 pl2 = pl + 1;
9324 ps2 = ps + 1;
9325 while (*pl2 == *ps2)
9326 {
9327 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009328 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009329 ++pl2;
9330 ++ps2;
9331 }
9332
9333 /* 6: substitute and swap */
9334 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9335 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009336 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009337
9338 /* 7: substitute and substitute */
9339 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009340 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009341
9342 /* 8: insert then delete */
9343 pl2 = pl;
9344 ps2 = ps + 1;
9345 while (*pl2 == *ps2)
9346 {
9347 ++pl2;
9348 ++ps2;
9349 }
9350 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009351 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009352
9353 /* 9: delete then insert */
9354 pl2 = pl + 1;
9355 ps2 = ps;
9356 while (*pl2 == *ps2)
9357 {
9358 ++pl2;
9359 ++ps2;
9360 }
9361 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009362 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009363
9364 /* Failed to compare. */
9365 break;
9366 }
9367
9368 return SCORE_MAXMAX;
9369}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009370
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009371/*
9372 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009373 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009374 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009375 * The algorithm comes from Aspell editdist.cpp, edit_distance().
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009376 * It has been converted from C++ to C and modified to support multi-byte
9377 * characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009378 */
9379 static int
9380spell_edit_score(badword, goodword)
9381 char_u *badword;
9382 char_u *goodword;
9383{
9384 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009385 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009386 int j, i;
9387 int t;
9388 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009389 int pbc, pgc;
9390#ifdef FEAT_MBYTE
9391 char_u *p;
9392 int wbadword[MAXWLEN];
9393 int wgoodword[MAXWLEN];
9394
9395 if (has_mbyte)
9396 {
9397 /* Get the characters from the multi-byte strings and put them in an
9398 * int array for easy access. */
9399 for (p = badword, badlen = 0; *p != NUL; )
9400 wbadword[badlen++] = mb_ptr2char_adv(&p);
9401 ++badlen;
9402 for (p = goodword, goodlen = 0; *p != NUL; )
9403 wgoodword[goodlen++] = mb_ptr2char_adv(&p);
9404 ++goodlen;
9405 }
9406 else
9407#endif
9408 {
9409 badlen = STRLEN(badword) + 1;
9410 goodlen = STRLEN(goodword) + 1;
9411 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009412
9413 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
9414#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009415 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
9416 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009417 if (cnt == NULL)
9418 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009419
9420 CNT(0, 0) = 0;
9421 for (j = 1; j <= goodlen; ++j)
9422 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
9423
9424 for (i = 1; i <= badlen; ++i)
9425 {
9426 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
9427 for (j = 1; j <= goodlen; ++j)
9428 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009429#ifdef FEAT_MBYTE
9430 if (has_mbyte)
9431 {
9432 bc = wbadword[i - 1];
9433 gc = wgoodword[j - 1];
9434 }
9435 else
9436#endif
9437 {
9438 bc = badword[i - 1];
9439 gc = goodword[j - 1];
9440 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009441 if (bc == gc)
9442 CNT(i, j) = CNT(i - 1, j - 1);
9443 else
9444 {
9445 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009446 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009447 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
9448 else
9449 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
9450
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009451 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009452 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009453#ifdef FEAT_MBYTE
9454 if (has_mbyte)
9455 {
9456 pbc = wbadword[i - 2];
9457 pgc = wgoodword[j - 2];
9458 }
9459 else
9460#endif
9461 {
9462 pbc = badword[i - 2];
9463 pgc = goodword[j - 2];
9464 }
9465 if (bc == pgc && pbc == gc)
9466 {
9467 t = SCORE_SWAP + CNT(i - 2, j - 2);
9468 if (t < CNT(i, j))
9469 CNT(i, j) = t;
9470 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009471 }
9472 t = SCORE_DEL + CNT(i - 1, j);
9473 if (t < CNT(i, j))
9474 CNT(i, j) = t;
9475 t = SCORE_INS + CNT(i, j - 1);
9476 if (t < CNT(i, j))
9477 CNT(i, j) = t;
9478 }
9479 }
9480 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009481
9482 i = CNT(badlen - 1, goodlen - 1);
9483 vim_free(cnt);
9484 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009485}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009486
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009487/*
9488 * ":spelldump"
9489 */
9490/*ARGSUSED*/
9491 void
9492ex_spelldump(eap)
9493 exarg_T *eap;
9494{
9495 buf_T *buf = curbuf;
9496 langp_T *lp;
9497 slang_T *slang;
9498 idx_T arridx[MAXWLEN];
9499 int curi[MAXWLEN];
9500 char_u word[MAXWLEN];
9501 int c;
9502 char_u *byts;
9503 idx_T *idxs;
9504 linenr_T lnum = 0;
9505 int round;
9506 int depth;
9507 int n;
9508 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009509 char_u *region_names = NULL; /* region names being used */
9510 int do_region = TRUE; /* dump region names and numbers */
9511 char_u *p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009512
9513 if (no_spell_checking())
9514 return;
9515
9516 /* Create a new empty buffer by splitting the window. */
9517 do_cmdline_cmd((char_u *)"new");
9518 if (!bufempty() || !buf_valid(buf))
9519 return;
9520
Bram Moolenaar7887d882005-07-01 22:33:52 +00009521 /* Find out if we can support regions: All languages must support the same
9522 * regions or none at all. */
9523 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
9524 {
9525 p = lp->lp_slang->sl_regions;
9526 if (p[0] != 0)
9527 {
9528 if (region_names == NULL) /* first language with regions */
9529 region_names = p;
9530 else if (STRCMP(region_names, p) != 0)
9531 {
9532 do_region = FALSE; /* region names are different */
9533 break;
9534 }
9535 }
9536 }
9537
9538 if (do_region && region_names != NULL)
9539 {
9540 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
9541 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
9542 }
9543 else
9544 do_region = FALSE;
9545
9546 /*
9547 * Loop over all files loaded for the entries in 'spelllang'.
9548 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009549 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
9550 {
9551 slang = lp->lp_slang;
9552
9553 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
9554 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
9555
9556 /* round 1: case-folded tree
9557 * round 2: keep-case tree */
9558 for (round = 1; round <= 2; ++round)
9559 {
9560 if (round == 1)
9561 {
9562 byts = slang->sl_fbyts;
9563 idxs = slang->sl_fidxs;
9564 }
9565 else
9566 {
9567 byts = slang->sl_kbyts;
9568 idxs = slang->sl_kidxs;
9569 }
9570 if (byts == NULL)
9571 continue; /* array is empty */
9572
9573 depth = 0;
9574 arridx[0] = 0;
9575 curi[0] = 1;
9576 while (depth >= 0 && !got_int)
9577 {
9578 if (curi[depth] > byts[arridx[depth]])
9579 {
9580 /* Done all bytes at this node, go up one level. */
9581 --depth;
9582 line_breakcheck();
9583 }
9584 else
9585 {
9586 /* Do one more byte at this node. */
9587 n = arridx[depth] + curi[depth];
9588 ++curi[depth];
9589 c = byts[n];
9590 if (c == 0)
9591 {
9592 /* End of word, deal with the word.
9593 * Don't use keep-case words in the fold-case tree,
9594 * they will appear in the keep-case tree.
9595 * Only use the word when the region matches. */
9596 flags = (int)idxs[n];
9597 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009598 && (do_region
9599 || (flags & WF_REGION) == 0
9600 || (((unsigned)flags >> 8)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009601 & lp->lp_region) != 0))
9602 {
9603 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009604 if (!do_region)
9605 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009606
9607 /* Dump the basic word if there is no prefix or
9608 * when it's the first one. */
9609 c = (unsigned)flags >> 16;
9610 if (c == 0 || curi[depth] == 2)
9611 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009612
9613 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009614 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009615 lnum = apply_prefixes(slang, word, round,
9616 flags, lnum);
9617 }
9618 }
9619 else
9620 {
9621 /* Normal char, go one level deeper. */
9622 word[depth++] = c;
9623 arridx[depth] = idxs[n];
9624 curi[depth] = 1;
9625 }
9626 }
9627 }
9628 }
9629 }
9630
9631 /* Delete the empty line that we started with. */
9632 if (curbuf->b_ml.ml_line_count > 1)
9633 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
9634
9635 redraw_later(NOT_VALID);
9636}
9637
9638/*
9639 * Dump one word: apply case modifications and append a line to the buffer.
9640 */
9641 static void
9642dump_word(word, round, flags, lnum)
9643 char_u *word;
9644 int round;
9645 int flags;
9646 linenr_T lnum;
9647{
9648 int keepcap = FALSE;
9649 char_u *p;
9650 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +00009651 char_u badword[MAXWLEN + 10];
9652 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009653
9654 if (round == 1 && (flags & WF_CAPMASK) != 0)
9655 {
9656 /* Need to fix case according to "flags". */
9657 make_case_word(word, cword, flags);
9658 p = cword;
9659 }
9660 else
9661 {
9662 p = word;
9663 if (round == 2 && (captype(word, NULL) & WF_KEEPCAP) == 0)
9664 keepcap = TRUE;
9665 }
9666
Bram Moolenaar7887d882005-07-01 22:33:52 +00009667 /* Add flags and regions after a slash. */
9668 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009669 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009670 STRCPY(badword, p);
9671 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009672 if (keepcap)
9673 STRCAT(badword, "=");
9674 if (flags & WF_BANNED)
9675 STRCAT(badword, "!");
9676 else if (flags & WF_RARE)
9677 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009678 if (flags & WF_REGION)
9679 for (i = 0; i < 7; ++i)
9680 if (flags & (0x100 << i))
9681 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009682 p = badword;
9683 }
9684
9685 ml_append(lnum, p, (colnr_T)0, FALSE);
9686}
9687
9688/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009689 * For ":spelldump": Find matching prefixes for "word". Prepend each to
9690 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009691 * Return the updated line number.
9692 */
9693 static linenr_T
9694apply_prefixes(slang, word, round, flags, startlnum)
9695 slang_T *slang;
9696 char_u *word; /* case-folded word */
9697 int round;
9698 int flags; /* flags with prefix ID */
9699 linenr_T startlnum;
9700{
9701 idx_T arridx[MAXWLEN];
9702 int curi[MAXWLEN];
9703 char_u prefix[MAXWLEN];
9704 int c;
9705 char_u *byts;
9706 idx_T *idxs;
9707 linenr_T lnum = startlnum;
9708 int depth;
9709 int n;
9710 int len;
9711 int prefid = (unsigned)flags >> 16;
9712 int i;
9713
9714 byts = slang->sl_pbyts;
9715 idxs = slang->sl_pidxs;
9716 if (byts != NULL) /* array not is empty */
9717 {
9718 /*
9719 * Loop over all prefixes, building them byte-by-byte in prefix[].
9720 * When at the end of a prefix check that it supports "prefid".
9721 */
9722 depth = 0;
9723 arridx[0] = 0;
9724 curi[0] = 1;
9725 while (depth >= 0 && !got_int)
9726 {
9727 len = arridx[depth];
9728 if (curi[depth] > byts[len])
9729 {
9730 /* Done all bytes at this node, go up one level. */
9731 --depth;
9732 line_breakcheck();
9733 }
9734 else
9735 {
9736 /* Do one more byte at this node. */
9737 n = len + curi[depth];
9738 ++curi[depth];
9739 c = byts[n];
9740 if (c == 0)
9741 {
9742 /* End of prefix, find out how many IDs there are. */
9743 for (i = 1; i < len; ++i)
9744 if (byts[n + i] != 0)
9745 break;
9746 curi[depth] += i - 1;
9747
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009748 i = valid_word_prefix(i, n, prefid, word, slang);
9749 if (i != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009750 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009751 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009752 dump_word(prefix, round,
9753 (i & WF_RAREPFX) ? (flags | WF_RARE)
9754 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009755 }
9756 }
9757 else
9758 {
9759 /* Normal char, go one level deeper. */
9760 prefix[depth++] = c;
9761 arridx[depth] = idxs[n];
9762 curi[depth] = 1;
9763 }
9764 }
9765 }
9766 }
9767
9768 return lnum;
9769}
9770
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009771#endif /* FEAT_SYN_HL */