blob: 015a7108b9ddaa1ea9aeb8c75cd77cd5cba6c235 [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 Moolenaar7d1f5db2005-07-03 21:39:27 +0000610static void spell_find_suggest __ARGS((char_u *badptr, suginfo_T *su, int maxcount, int banbadword, int need_cap));
Bram Moolenaara1ba8112005-06-28 23:23:32 +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 Moolenaar7d1f5db2005-07-03 21:39:27 +00005955 int need_cap;
5956 regmatch_T regmatch;
5957 int endcol;
5958 char_u *line_copy = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005959
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005960 /* Find the start of the badly spelled word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005961 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL
5962 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005963 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005964 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
5965 return;
5966
5967 /* No bad word or it starts after the cursor: use the word under the
5968 * cursor. */
5969 curwin->w_cursor = prev_cursor;
5970 line = ml_get_curline();
5971 p = line + curwin->w_cursor.col;
5972 /* Backup to before start of word. */
5973 while (p > line && SPELL_ISWORDP(p))
5974 mb_ptr_back(line, p);
5975 /* Forward to start of word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005976 while (*p != NUL && !SPELL_ISWORDP(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00005977 mb_ptr_adv(p);
5978
5979 if (!SPELL_ISWORDP(p)) /* No word found. */
5980 {
5981 beep_flush();
5982 return;
5983 }
5984 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005985 }
5986
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005987 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005988 line = ml_get_curline();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005989
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005990 /* Figure out if the word should be capitalised. */
5991 need_cap = FALSE;
5992 if (curbuf->b_cap_prog != NULL)
5993 {
5994 endcol = 0;
5995 if (skipwhite(line) - line == curwin->w_cursor.col)
5996 {
5997 /* At start of line, check if previous line is empty or sentence
5998 * ends there. */
5999 if (curwin->w_cursor.lnum == 1)
6000 need_cap = TRUE;
6001 else
6002 {
6003 line = ml_get(curwin->w_cursor.lnum - 1);
6004 if (*skipwhite(line) == NUL)
6005 need_cap = TRUE;
6006 else
6007 {
6008 /* Append a space in place of the line break. */
6009 line_copy = concat_str(line, (char_u *)" ");
6010 line = line_copy;
6011 endcol = STRLEN(line);
6012 }
6013 }
6014 }
6015 else
6016 endcol = curwin->w_cursor.col;
6017
6018 if (endcol > 0)
6019 {
6020 /* Check if sentence ends before the bad word. */
6021 regmatch.regprog = curbuf->b_cap_prog;
6022 regmatch.rm_ic = FALSE;
6023 p = line + endcol;
6024 for (;;)
6025 {
6026 mb_ptr_back(line, p);
6027 if (p == line || SPELL_ISWORDP(p))
6028 break;
6029 if (vim_regexec(&regmatch, p, 0)
6030 && regmatch.endp[0] == line + endcol)
6031 {
6032 need_cap = TRUE;
6033 break;
6034 }
6035 }
6036 }
6037
6038 /* get the line again, we may have been using the previous one */
6039 line = ml_get_curline();
6040 vim_free(line_copy);
6041 }
6042
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006043 /* Get the list of suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006044 spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2,
6045 TRUE, need_cap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006046
6047 if (sug.su_ga.ga_len == 0)
6048 MSG(_("Sorry, no suggestions"));
6049 else
6050 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006051 vim_free(repl_from);
6052 repl_from = NULL;
6053 vim_free(repl_to);
6054 repl_to = NULL;
6055
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006056 /* List the suggestions. */
6057 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006058 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006059 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
6060 sug.su_badlen, sug.su_badptr);
6061 msg_puts(IObuff);
6062 msg_clr_eos();
6063 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00006064
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006065 msg_scroll = TRUE;
6066 for (i = 0; i < sug.su_ga.ga_len; ++i)
6067 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006068 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006069
6070 /* The suggested word may replace only part of the bad word, add
6071 * the not replaced part. */
6072 STRCPY(wcopy, stp->st_word);
6073 if (sug.su_badlen > stp->st_orglen)
6074 vim_strncpy(wcopy + STRLEN(wcopy),
6075 sug.su_badptr + stp->st_orglen,
6076 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006077 vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\""), i + 1, wcopy);
6078 msg_puts(IObuff);
6079
6080 /* The word may replace more than "su_badlen". */
6081 if (sug.su_badlen < stp->st_orglen)
6082 {
6083 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
6084 stp->st_orglen, sug.su_badptr);
6085 msg_puts(IObuff);
6086 }
6087
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006088 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006089 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006090 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006091 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006092 vim_snprintf((char *)IObuff, IOSIZE, _(" (%s%d - %d)"),
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006093 stp->st_salscore ? "s " : "",
6094 stp->st_score, stp->st_altscore);
6095 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00006096 vim_snprintf((char *)IObuff, IOSIZE, _(" (%d)"),
6097 stp->st_score);
6098 msg_advance(30);
6099 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006100 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006101 msg_putchar('\n');
6102 }
6103
6104 /* Ask for choice. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006105 i = prompt_for_number(&mouse_used);
6106 if (mouse_used)
6107 i -= lines_left;
6108
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006109 if (i > 0 && i <= sug.su_ga.ga_len && u_save_cursor() == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006110 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006111 /* Save the from and to text for :spellrepall. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006112 stp = &SUG(sug.su_ga, i - 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006113 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
6114 repl_to = vim_strsave(stp->st_word);
6115
6116 /* Replace the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006117 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
6118 if (p != NULL)
6119 {
6120 c = sug.su_badptr - line;
6121 mch_memmove(p, line, c);
6122 STRCPY(p + c, stp->st_word);
6123 STRCAT(p, sug.su_badptr + stp->st_orglen);
6124 ml_replace(curwin->w_cursor.lnum, p, FALSE);
6125 curwin->w_cursor.col = c;
6126 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006127
6128 /* For redo we use a change-word command. */
6129 ResetRedobuff();
6130 AppendToRedobuff((char_u *)"ciw");
6131 AppendToRedobuff(stp->st_word);
6132 AppendCharToRedobuff(ESC);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006133 }
6134 }
6135 else
6136 curwin->w_cursor = prev_cursor;
6137 }
6138
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006139 spell_find_cleanup(&sug);
6140}
6141
6142/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006143 * ":spellrepall"
6144 */
6145/*ARGSUSED*/
6146 void
6147ex_spellrepall(eap)
6148 exarg_T *eap;
6149{
6150 pos_T pos = curwin->w_cursor;
6151 char_u *frompat;
6152 int addlen;
6153 char_u *line;
6154 char_u *p;
6155 int didone = FALSE;
6156 int save_ws = p_ws;
6157
6158 if (repl_from == NULL || repl_to == NULL)
6159 {
6160 EMSG(_("E752: No previous spell replacement"));
6161 return;
6162 }
6163 addlen = STRLEN(repl_to) - STRLEN(repl_from);
6164
6165 frompat = alloc(STRLEN(repl_from) + 7);
6166 if (frompat == NULL)
6167 return;
6168 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
6169 p_ws = FALSE;
6170
6171 curwin->w_cursor.lnum = 0;
6172 while (!got_int)
6173 {
6174 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
6175 || u_save_cursor() == FAIL)
6176 break;
6177
6178 /* Only replace when the right word isn't there yet. This happens
6179 * when changing "etc" to "etc.". */
6180 line = ml_get_curline();
6181 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
6182 repl_to, STRLEN(repl_to)) != 0)
6183 {
6184 p = alloc(STRLEN(line) + addlen + 1);
6185 if (p == NULL)
6186 break;
6187 mch_memmove(p, line, curwin->w_cursor.col);
6188 STRCPY(p + curwin->w_cursor.col, repl_to);
6189 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
6190 ml_replace(curwin->w_cursor.lnum, p, FALSE);
6191 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6192 didone = TRUE;
6193 }
6194 curwin->w_cursor.col += STRLEN(repl_to);
6195 }
6196
6197 p_ws = save_ws;
6198 curwin->w_cursor = pos;
6199 vim_free(frompat);
6200
6201 if (!didone)
6202 EMSG2(_("E753: Not found: %s"), repl_from);
6203}
6204
6205/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006206 * Find spell suggestions for "word". Return them in the growarray "*gap" as
6207 * a list of allocated strings.
6208 */
6209 void
6210spell_suggest_list(gap, word, maxcount)
6211 garray_T *gap;
6212 char_u *word;
6213 int maxcount; /* maximum nr of suggestions */
6214{
6215 suginfo_T sug;
6216 int i;
6217 suggest_T *stp;
6218 char_u *wcopy;
6219
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006220 spell_find_suggest(word, &sug, maxcount, FALSE, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006221
6222 /* Make room in "gap". */
6223 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
6224 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
6225 return;
6226
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006227 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006228 {
6229 stp = &SUG(sug.su_ga, i);
6230
6231 /* The suggested word may replace only part of "word", add the not
6232 * replaced part. */
6233 wcopy = alloc(STRLEN(stp->st_word)
6234 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
6235 if (wcopy == NULL)
6236 break;
6237 STRCPY(wcopy, stp->st_word);
6238 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
6239 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
6240 }
6241
6242 spell_find_cleanup(&sug);
6243}
6244
6245/*
6246 * Find spell suggestions for the word at the start of "badptr".
6247 * Return the suggestions in "su->su_ga".
6248 * The maximum number of suggestions is "maxcount".
6249 * Note: does use info for the current window.
6250 * This is based on the mechanisms of Aspell, but completely reimplemented.
6251 */
6252 static void
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006253spell_find_suggest(badptr, su, maxcount, banbadword, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006254 char_u *badptr;
6255 suginfo_T *su;
6256 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00006257 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006258 int need_cap; /* word should start with capital */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006259{
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006260 int attr = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006261 char_u buf[MAXPATHL];
6262 char_u *p;
6263 int do_combine = FALSE;
6264 char_u *sps_copy;
6265#ifdef FEAT_EVAL
6266 static int expr_busy = FALSE;
6267#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006268 int c;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006269
6270 /*
6271 * Set the info in "*su".
6272 */
6273 vim_memset(su, 0, sizeof(suginfo_T));
6274 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
6275 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006276 if (*badptr == NUL)
6277 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006278 hash_init(&su->su_banned);
6279
6280 su->su_badptr = badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006281 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006282 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006283 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006284
6285 if (su->su_badlen >= MAXWLEN)
6286 su->su_badlen = MAXWLEN - 1; /* just in case */
6287 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
6288 (void)spell_casefold(su->su_badptr, su->su_badlen,
6289 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006290 /* get caps flags for bad word */
6291 su->su_badflags = captype(su->su_badptr, su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006292 if (need_cap)
6293 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006294
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006295 /* If the word is not capitalised and spell_check() doesn't consider the
6296 * word to be bad then it might need to be capitalised. Add a suggestion
6297 * for that. */
6298#ifdef FEAT_MBYTE
6299 c = mb_ptr2char(su->su_badptr);
6300#else
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006301 c = *su->su_badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006302#endif
6303 if (!SPELL_ISUPPER(c) && attr == 0)
6304 {
6305 make_case_word(su->su_badword, buf, WF_ONECAP);
6306 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
6307 0, TRUE);
6308 }
6309
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006310 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00006311 if (banbadword)
6312 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006313
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006314 /* Make a copy of 'spellsuggest', because the expression may change it. */
6315 sps_copy = vim_strsave(p_sps);
6316 if (sps_copy == NULL)
6317 return;
6318
6319 /* Loop over the items in 'spellsuggest'. */
6320 for (p = sps_copy; *p != NUL; )
6321 {
6322 copy_option_part(&p, buf, MAXPATHL, ",");
6323
6324 if (STRNCMP(buf, "expr:", 5) == 0)
6325 {
6326#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006327 /* Evaluate an expression. Skip this when called recursively,
6328 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006329 if (!expr_busy)
6330 {
6331 expr_busy = TRUE;
6332 spell_suggest_expr(su, buf + 5);
6333 expr_busy = FALSE;
6334 }
6335#endif
6336 }
6337 else if (STRNCMP(buf, "file:", 5) == 0)
6338 /* Use list of suggestions in a file. */
6339 spell_suggest_file(su, buf + 5);
6340 else
6341 {
6342 /* Use internal method. */
6343 spell_suggest_intern(su);
6344 if (sps_flags & SPS_DOUBLE)
6345 do_combine = TRUE;
6346 }
6347 }
6348
6349 vim_free(sps_copy);
6350
6351 if (do_combine)
6352 /* Combine the two list of suggestions. This must be done last,
6353 * because sorting changes the order again. */
6354 score_combine(su);
6355}
6356
6357#ifdef FEAT_EVAL
6358/*
6359 * Find suggestions by evaluating expression "expr".
6360 */
6361 static void
6362spell_suggest_expr(su, expr)
6363 suginfo_T *su;
6364 char_u *expr;
6365{
6366 list_T *list;
6367 listitem_T *li;
6368 int score;
6369 char_u *p;
6370
6371 /* The work is split up in a few parts to avoid having to export
6372 * suginfo_T.
6373 * First evaluate the expression and get the resulting list. */
6374 list = eval_spell_expr(su->su_badword, expr);
6375 if (list != NULL)
6376 {
6377 /* Loop over the items in the list. */
6378 for (li = list->lv_first; li != NULL; li = li->li_next)
6379 if (li->li_tv.v_type == VAR_LIST)
6380 {
6381 /* Get the word and the score from the items. */
6382 score = get_spellword(li->li_tv.vval.v_list, &p);
6383 if (score >= 0)
6384 add_suggestion(su, &su->su_ga, p,
6385 su->su_badlen, score, 0, TRUE);
6386 }
6387 list_unref(list);
6388 }
6389
6390 /* Sort the suggestions and truncate at "maxcount". */
6391 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
6392}
6393#endif
6394
6395/*
6396 * Find suggestions a file "fname".
6397 */
6398 static void
6399spell_suggest_file(su, fname)
6400 suginfo_T *su;
6401 char_u *fname;
6402{
6403 FILE *fd;
6404 char_u line[MAXWLEN * 2];
6405 char_u *p;
6406 int len;
6407 char_u cword[MAXWLEN];
6408
6409 /* Open the file. */
6410 fd = mch_fopen((char *)fname, "r");
6411 if (fd == NULL)
6412 {
6413 EMSG2(_(e_notopen), fname);
6414 return;
6415 }
6416
6417 /* Read it line by line. */
6418 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
6419 {
6420 line_breakcheck();
6421
6422 p = vim_strchr(line, '/');
6423 if (p == NULL)
6424 continue; /* No Tab found, just skip the line. */
6425 *p++ = NUL;
6426 if (STRICMP(su->su_badword, line) == 0)
6427 {
6428 /* Match! Isolate the good word, until CR or NL. */
6429 for (len = 0; p[len] >= ' '; ++len)
6430 ;
6431 p[len] = NUL;
6432
6433 /* If the suggestion doesn't have specific case duplicate the case
6434 * of the bad word. */
6435 if (captype(p, NULL) == 0)
6436 {
6437 make_case_word(p, cword, su->su_badflags);
6438 p = cword;
6439 }
6440
6441 add_suggestion(su, &su->su_ga, p, su->su_badlen,
6442 SCORE_FILE, 0, TRUE);
6443 }
6444 }
6445
6446 fclose(fd);
6447
6448 /* Sort the suggestions and truncate at "maxcount". */
6449 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
6450}
6451
6452/*
6453 * Find suggestions for the internal method indicated by "sps_flags".
6454 */
6455 static void
6456spell_suggest_intern(su)
6457 suginfo_T *su;
6458{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006459 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006460 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006461 *
6462 * Set a maximum score to limit the combination of operations that is
6463 * tried.
6464 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006465 suggest_try_special(su);
6466
6467 /*
6468 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
6469 * from the .aff file and inserting a space (split the word).
6470 */
6471 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006472
6473 /* For the resulting top-scorers compute the sound-a-like score. */
6474 if (sps_flags & SPS_DOUBLE)
6475 score_comp_sal(su);
6476
6477 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006478 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006479 *
6480 * Only do this when we don't have a lot of suggestions yet, because it's
6481 * very slow and often doesn't find new suggestions.
6482 */
6483 if ((sps_flags & SPS_DOUBLE)
6484 || (!(sps_flags & SPS_FAST)
6485 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
6486 {
6487 /* Allow a higher score now. */
6488 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006489 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006490 }
6491
6492 /* When CTRL-C was hit while searching do show the results. */
6493 ui_breakcheck();
6494 if (got_int)
6495 {
6496 (void)vgetc();
6497 got_int = FALSE;
6498 }
6499
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006500 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006501 {
6502 if (sps_flags & SPS_BEST)
6503 /* Adjust the word score for how it sounds like. */
6504 rescore_suggestions(su);
6505
6506 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006507 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006508 }
6509}
6510
6511/*
6512 * Free the info put in "*su" by spell_find_suggest().
6513 */
6514 static void
6515spell_find_cleanup(su)
6516 suginfo_T *su;
6517{
6518 int i;
6519
6520 /* Free the suggestions. */
6521 for (i = 0; i < su->su_ga.ga_len; ++i)
6522 vim_free(SUG(su->su_ga, i).st_word);
6523 ga_clear(&su->su_ga);
6524 for (i = 0; i < su->su_sga.ga_len; ++i)
6525 vim_free(SUG(su->su_sga, i).st_word);
6526 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006527
6528 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006529 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006530}
6531
6532/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006533 * Make a copy of "word", with the first letter upper or lower cased, to
6534 * "wcopy[MAXWLEN]". "word" must not be empty.
6535 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006536 */
6537 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006538onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006539 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006540 char_u *wcopy;
6541 int upper; /* TRUE: first letter made upper case */
6542{
6543 char_u *p;
6544 int c;
6545 int l;
6546
6547 p = word;
6548#ifdef FEAT_MBYTE
6549 if (has_mbyte)
6550 c = mb_ptr2char_adv(&p);
6551 else
6552#endif
6553 c = *p++;
6554 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006555 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006556 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006557 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006558#ifdef FEAT_MBYTE
6559 if (has_mbyte)
6560 l = mb_char2bytes(c, wcopy);
6561 else
6562#endif
6563 {
6564 l = 1;
6565 wcopy[0] = c;
6566 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006567 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006568}
6569
6570/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006571 * Make a copy of "word" with all the letters upper cased into
6572 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006573 */
6574 static void
6575allcap_copy(word, wcopy)
6576 char_u *word;
6577 char_u *wcopy;
6578{
6579 char_u *s;
6580 char_u *d;
6581 int c;
6582
6583 d = wcopy;
6584 for (s = word; *s != NUL; )
6585 {
6586#ifdef FEAT_MBYTE
6587 if (has_mbyte)
6588 c = mb_ptr2char_adv(&s);
6589 else
6590#endif
6591 c = *s++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006592 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006593
6594#ifdef FEAT_MBYTE
6595 if (has_mbyte)
6596 {
6597 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
6598 break;
6599 d += mb_char2bytes(c, d);
6600 }
6601 else
6602#endif
6603 {
6604 if (d - wcopy >= MAXWLEN - 1)
6605 break;
6606 *d++ = c;
6607 }
6608 }
6609 *d = NUL;
6610}
6611
6612/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006613 * Try finding suggestions by recognizing specific situations.
6614 */
6615 static void
6616suggest_try_special(su)
6617 suginfo_T *su;
6618{
6619 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006620 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006621 int c;
6622 char_u word[MAXWLEN];
6623
6624 /*
6625 * Recognize a word that is repeated: "the the".
6626 */
6627 p = skiptowhite(su->su_fbadword);
6628 len = p - su->su_fbadword;
6629 p = skipwhite(p);
6630 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
6631 {
6632 /* Include badflags: if the badword is onecap or allcap
6633 * use that for the goodword too: "The the" -> "The". */
6634 c = su->su_fbadword[len];
6635 su->su_fbadword[len] = NUL;
6636 make_case_word(su->su_fbadword, word, su->su_badflags);
6637 su->su_fbadword[len] = c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006638 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006639 }
6640}
6641
6642/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006643 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00006644 *
6645 * This uses a state machine. At each node in the tree we try various
6646 * operations. When trying if an operation work "depth" is increased and the
6647 * stack[] is used to store info. This allows combinations, thus insert one
6648 * character, replace one and delete another. The number of changes is
6649 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006650 */
6651 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00006652suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006653 suginfo_T *su;
6654{
6655 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
6656 char_u tword[MAXWLEN]; /* good word collected so far */
6657 trystate_T stack[MAXWLEN];
6658 char_u preword[MAXWLEN * 3]; /* word found with proper case (appended
6659 * to for word split) */
6660 char_u prewordlen = 0; /* length of word in "preword" */
6661 int splitoff = 0; /* index in tword after last split */
6662 trystate_T *sp;
6663 int newscore;
6664 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006665 char_u *byts, *fbyts, *pbyts;
6666 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006667 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00006668 int c, c2, c3;
6669 int n = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006670 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006671 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006672 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006673 int len;
6674 char_u *p;
6675 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00006676 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006677 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006678
6679 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00006680 * to find matches (esp. REP items). Append some more text, changing
6681 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006682 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006683 n = STRLEN(fword);
6684 p = su->su_badptr + su->su_badlen;
6685 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006686
6687 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
6688 lp->lp_slang != NULL; ++lp)
6689 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006690 /*
6691 * Go through the whole case-fold tree, try changes at each node.
6692 * "tword[]" contains the word collected from nodes in the tree.
6693 * "fword[]" the word we are trying to match with (initially the bad
6694 * word).
6695 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006696 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006697 sp = &stack[0];
6698 sp->ts_state = STATE_START;
6699 sp->ts_score = 0;
6700 sp->ts_curi = 1;
6701 sp->ts_fidx = 0;
6702 sp->ts_fidxtry = 0;
6703 sp->ts_twordlen = 0;
6704 sp->ts_arridx = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00006705#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006706 sp->ts_tcharlen = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00006707#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006708
Bram Moolenaarea424162005-06-16 21:51:00 +00006709 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006710 * When there are postponed prefixes we need to use these first. At
6711 * the end of the prefix we continue in the case-fold tree.
6712 */
6713 fbyts = lp->lp_slang->sl_fbyts;
6714 fidxs = lp->lp_slang->sl_fidxs;
6715 pbyts = lp->lp_slang->sl_pbyts;
6716 pidxs = lp->lp_slang->sl_pidxs;
6717 if (pbyts != NULL)
6718 {
6719 byts = pbyts;
6720 idxs = pidxs;
6721 sp->ts_prefixdepth = PREFIXTREE;
6722 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
6723 }
6724 else
6725 {
6726 byts = fbyts;
6727 idxs = fidxs;
6728 sp->ts_prefixdepth = NOPREFIX;
6729 }
6730
6731 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00006732 * Loop to find all suggestions. At each round we either:
6733 * - For the current state try one operation, advance "ts_curi",
6734 * increase "depth".
6735 * - When a state is done go to the next, set "ts_state".
6736 * - When all states are tried decrease "depth".
6737 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006738 while (depth >= 0 && !got_int)
6739 {
6740 sp = &stack[depth];
6741 switch (sp->ts_state)
6742 {
6743 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006744 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006745 /*
6746 * Start of node: Deal with NUL bytes, which means
6747 * tword[] may end here.
6748 */
6749 arridx = sp->ts_arridx; /* current node in the tree */
6750 len = byts[arridx]; /* bytes in this node */
6751 arridx += sp->ts_curi; /* index of current byte */
6752
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006753 if (sp->ts_prefixdepth == PREFIXTREE)
6754 {
6755 /* Skip over the NUL bytes, we use them later. */
6756 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
6757 ;
6758 sp->ts_curi += n;
6759
6760 /* At end of a prefix or at start of prefixtree: check for
6761 * following word. */
6762 if (byts[arridx] == 0 || sp->ts_state == STATE_NOPREFIX)
6763 {
6764 sp->ts_state = STATE_START;
6765 ++depth;
6766 stack[depth] = stack[depth - 1];
6767 sp = &stack[depth];
6768 sp->ts_prefixdepth = depth - 1;
6769 byts = fbyts;
6770 idxs = fidxs;
6771 sp->ts_state = STATE_START;
6772 sp->ts_curi = 1; /* start just after length byte */
6773 sp->ts_arridx = 0;
6774
6775 /* Move the prefix to preword[] so that
6776 * find_keepcap_word() works. */
6777 prewordlen = splitoff = sp->ts_twordlen;
6778 mch_memmove(preword, tword, splitoff);
6779 break;
6780 }
6781
6782 /* Always past NUL bytes now. */
6783 sp->ts_state = STATE_ENDNUL;
6784 break;
6785 }
6786
Bram Moolenaar0c405862005-06-22 22:26:26 +00006787 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006788 {
6789 /* Past bytes in node and/or past NUL bytes. */
6790 sp->ts_state = STATE_ENDNUL;
6791 break;
6792 }
6793
6794 /*
6795 * End of word in tree.
6796 */
6797 ++sp->ts_curi; /* eat one NUL byte */
6798
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006799 flags = (int)idxs[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006800
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006801 if (sp->ts_prefixdepth < MAXWLEN)
6802 {
6803 /* There was a prefix before the word. Check that the
6804 * prefix can be used with this word. */
6805 /* Count the length of the NULs in the prefix. If there
6806 * are none this must be the first try without a prefix.
6807 */
6808 n = stack[sp->ts_prefixdepth].ts_arridx;
6809 len = pbyts[n++];
6810 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
6811 ;
6812 if (c > 0)
6813 {
6814 /* The prefix ID is stored two bytes above the flags. */
6815 c = valid_word_prefix(c, n, (unsigned)flags >> 16,
6816 tword + splitoff, lp->lp_slang);
6817 if (c == 0)
6818 break;
6819
6820 /* Use the WF_RARE flag for a rare prefix. */
6821 if (c & WF_RAREPFX)
6822 flags |= WF_RARE;
6823 }
6824 }
6825
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006826 /*
6827 * Form the word with proper case in preword.
6828 * If there is a word from a previous split, append.
6829 */
6830 tword[sp->ts_twordlen] = NUL;
6831 if (flags & WF_KEEPCAP)
6832 /* Must find the word in the keep-case tree. */
6833 find_keepcap_word(lp->lp_slang, tword + splitoff,
6834 preword + prewordlen);
6835 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00006836 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006837 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00006838 * use that for the goodword too. But if the badword is
6839 * allcap and it's only one char long use onecap. */
6840 c = su->su_badflags;
6841 if ((c & WF_ALLCAP)
6842#ifdef FEAT_MBYTE
6843 && su->su_badlen == mb_ptr2len_check(su->su_badptr)
6844#else
6845 && su->su_badlen == 1
6846#endif
6847 )
6848 c = WF_ONECAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006849 make_case_word(tword + splitoff,
Bram Moolenaar0c405862005-06-22 22:26:26 +00006850 preword + prewordlen, flags | c);
6851 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006852
6853 /* Don't use a banned word. It may appear again as a good
6854 * word, thus remember it. */
6855 if (flags & WF_BANNED)
6856 {
6857 add_banned(su, preword + prewordlen);
6858 break;
6859 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006860 if (was_banned(su, preword + prewordlen)
6861 || was_banned(su, preword))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006862 break;
6863
6864 newscore = 0;
6865 if ((flags & WF_REGION)
6866 && (((unsigned)flags >> 8) & lp->lp_region) == 0)
6867 newscore += SCORE_REGION;
6868 if (flags & WF_RARE)
6869 newscore += SCORE_RARE;
6870
Bram Moolenaar0c405862005-06-22 22:26:26 +00006871 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006872 captype(preword + prewordlen, NULL)))
6873 newscore += SCORE_ICASE;
6874
Bram Moolenaar0c405862005-06-22 22:26:26 +00006875 if ((fword[sp->ts_fidx] == NUL
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006876 || !spell_iswordp(fword + sp->ts_fidx, curbuf))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006877 && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006878 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006879 /* The badword also ends: add suggestions. Give a penalty
6880 * when changing non-word char to word char, e.g., "thes,"
6881 * -> "these". */
6882 p = fword + sp->ts_fidx;
6883#ifdef FEAT_MBYTE
6884 if (has_mbyte)
6885 mb_ptr_back(fword, p);
6886 else
6887#endif
6888 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006889 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006890 {
6891 p = preword + STRLEN(preword);
6892#ifdef FEAT_MBYTE
6893 if (has_mbyte)
6894 mb_ptr_back(preword, p);
6895 else
6896#endif
6897 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006898 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006899 newscore += SCORE_NONWORD;
6900 }
6901
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006902 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00006903 sp->ts_fidx - repextra,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006904 sp->ts_score + newscore, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006905 }
Bram Moolenaarea424162005-06-16 21:51:00 +00006906 else if (sp->ts_fidx >= sp->ts_fidxtry
6907#ifdef FEAT_MBYTE
6908 /* Don't split halfway a character. */
6909 && (!has_mbyte || sp->ts_tcharlen == 0)
6910#endif
6911 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006912 {
6913 /* The word in the tree ends but the badword
6914 * continues: try inserting a space and check that a valid
6915 * words starts at fword[sp->ts_fidx]. */
6916 if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT))
6917 {
6918 /* Save things to be restored at STATE_SPLITUNDO. */
6919 sp->ts_save_prewordlen = prewordlen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006920 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006921 sp->ts_save_splitoff = splitoff;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006922 sp->ts_state = STATE_SPLITUNDO;
6923
6924 ++depth;
6925 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006926
6927 /* Append a space to preword. */
6928 STRCAT(preword, " ");
6929 prewordlen = STRLEN(preword);
6930 splitoff = sp->ts_twordlen;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006931
6932 /* If the badword has a non-word character at this
6933 * position skip it. That means replacing the
6934 * non-word character with a space. */
6935 if (!spell_iswordp_nmw(fword + sp->ts_fidx))
6936 {
6937 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
6938#ifdef FEAT_MBYTE
6939 if (has_mbyte)
6940 sp->ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
6941 else
6942#endif
6943 ++sp->ts_fidx;
6944 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006945#ifdef FEAT_MBYTE
6946 if (has_mbyte)
6947 {
6948 int i = 0;
6949
6950 /* Case-folding may change the number of bytes:
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006951 * Count nr of chars in fword[ts_fidx] and
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006952 * advance that many chars in su->su_badptr. */
6953 for (p = fword; p < fword + sp->ts_fidx;
6954 mb_ptr_adv(p))
6955 ++i;
6956 for (p = su->su_badptr; i > 0; mb_ptr_adv(p))
6957 --i;
6958 }
6959 else
6960#endif
6961 p = su->su_badptr + sp->ts_fidx;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006962 su->su_badflags = captype(p, su->su_badptr
6963 + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006964
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006965 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006966 sp->ts_arridx = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006967 }
6968 }
6969 break;
6970
6971 case STATE_SPLITUNDO:
Bram Moolenaar0c405862005-06-22 22:26:26 +00006972 /* Undo the changes done for word split. */
6973 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006974 splitoff = sp->ts_save_splitoff;
6975 prewordlen = sp->ts_save_prewordlen;
6976
6977 /* Continue looking for NUL bytes. */
6978 sp->ts_state = STATE_START;
6979 break;
6980
6981 case STATE_ENDNUL:
6982 /* Past the NUL bytes in the node. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006983 if (fword[sp->ts_fidx] == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006984 {
6985 /* The badword ends, can't use the bytes in this node. */
6986 sp->ts_state = STATE_DEL;
6987 break;
6988 }
6989 sp->ts_state = STATE_PLAIN;
6990 /*FALLTHROUGH*/
6991
6992 case STATE_PLAIN:
6993 /*
6994 * Go over all possible bytes at this node, add each to
6995 * tword[] and use child node. "ts_curi" is the index.
6996 */
6997 arridx = sp->ts_arridx;
6998 if (sp->ts_curi > byts[arridx])
6999 {
7000 /* Done all bytes at this node, do next state. When still
7001 * at already changed bytes skip the other tricks. */
7002 if (sp->ts_fidx >= sp->ts_fidxtry)
7003 sp->ts_state = STATE_DEL;
7004 else
7005 sp->ts_state = STATE_FINAL;
7006 }
7007 else
7008 {
7009 arridx += sp->ts_curi++;
7010 c = byts[arridx];
7011
7012 /* Normal byte, go one level deeper. If it's not equal to
7013 * the byte in the bad word adjust the score. But don't
7014 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00007015 if (c == fword[sp->ts_fidx]
7016#ifdef FEAT_MBYTE
7017 || (sp->ts_tcharlen > 0
7018 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007019#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00007020 )
7021 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007022 else
7023 newscore = SCORE_SUBST;
7024 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
7025 && try_deeper(su, stack, depth, newscore))
7026 {
7027 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007028 sp = &stack[depth];
7029 ++sp->ts_fidx;
7030 tword[sp->ts_twordlen++] = c;
7031 sp->ts_arridx = idxs[arridx];
7032#ifdef FEAT_MBYTE
7033 if (newscore == SCORE_SUBST)
7034 sp->ts_isdiff = DIFF_YES;
7035 if (has_mbyte)
7036 {
7037 /* Multi-byte characters are a bit complicated to
7038 * handle: They differ when any of the bytes
7039 * differ and then their length may also differ. */
7040 if (sp->ts_tcharlen == 0)
7041 {
7042 /* First byte. */
7043 sp->ts_tcharidx = 0;
7044 sp->ts_tcharlen = MB_BYTE2LEN(c);
7045 sp->ts_fcharstart = sp->ts_fidx - 1;
7046 sp->ts_isdiff = (newscore != 0)
7047 ? DIFF_YES : DIFF_NONE;
7048 }
7049 else if (sp->ts_isdiff == DIFF_INSERT)
7050 /* When inserting trail bytes don't advance in
7051 * the bad word. */
7052 --sp->ts_fidx;
7053 if (++sp->ts_tcharidx == sp->ts_tcharlen)
7054 {
7055 /* Last byte of character. */
7056 if (sp->ts_isdiff == DIFF_YES)
7057 {
7058 /* Correct ts_fidx for the byte length of
7059 * the character (we didn't check that
7060 * before). */
7061 sp->ts_fidx = sp->ts_fcharstart
7062 + MB_BYTE2LEN(
7063 fword[sp->ts_fcharstart]);
7064
7065 /* For a similar character adjust score
7066 * from SCORE_SUBST to SCORE_SIMILAR. */
7067 if (lp->lp_slang->sl_has_map
7068 && similar_chars(lp->lp_slang,
7069 mb_ptr2char(tword
7070 + sp->ts_twordlen
7071 - sp->ts_tcharlen),
7072 mb_ptr2char(fword
7073 + sp->ts_fcharstart)))
7074 sp->ts_score -=
7075 SCORE_SUBST - SCORE_SIMILAR;
7076 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007077 else if (sp->ts_isdiff == DIFF_INSERT
7078 && sp->ts_twordlen > sp->ts_tcharlen)
7079 {
7080 /* If the previous character was the same,
7081 * thus doubling a character, give a bonus
7082 * to the score. */
7083 p = tword + sp->ts_twordlen
7084 - sp->ts_tcharlen;
7085 c = mb_ptr2char(p);
7086 mb_ptr_back(tword, p);
7087 if (c == mb_ptr2char(p))
7088 sp->ts_score -= SCORE_INS
7089 - SCORE_INSDUP;
7090 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007091
7092 /* Starting a new char, reset the length. */
7093 sp->ts_tcharlen = 0;
7094 }
7095 }
7096 else
7097#endif
7098 {
7099 /* If we found a similar char adjust the score.
7100 * We do this after calling try_deeper() because
7101 * it's slow. */
7102 if (newscore != 0
7103 && lp->lp_slang->sl_has_map
7104 && similar_chars(lp->lp_slang,
7105 c, fword[sp->ts_fidx - 1]))
7106 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
7107 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007108 }
7109 }
7110 break;
7111
7112 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00007113#ifdef FEAT_MBYTE
7114 /* When past the first byte of a multi-byte char don't try
7115 * delete/insert/swap a character. */
7116 if (has_mbyte && sp->ts_tcharlen > 0)
7117 {
7118 sp->ts_state = STATE_FINAL;
7119 break;
7120 }
7121#endif
7122 /*
7123 * Try skipping one character in the bad word (delete it).
7124 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007125 sp->ts_state = STATE_INS;
7126 sp->ts_curi = 1;
7127 if (fword[sp->ts_fidx] != NUL
7128 && try_deeper(su, stack, depth, SCORE_DEL))
7129 {
7130 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00007131
7132 /* Advance over the character in fword[]. Give a bonus to
7133 * the score if the same character is following "nn" ->
7134 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00007135#ifdef FEAT_MBYTE
7136 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00007137 {
7138 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00007139 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaarea408852005-06-25 22:49:46 +00007140 if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
7141 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
7142 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007143 else
7144#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00007145 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007146 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00007147 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
7148 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
7149 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007150 break;
7151 }
7152 /*FALLTHROUGH*/
7153
7154 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00007155 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007156 * node. */
7157 n = sp->ts_arridx;
7158 if (sp->ts_curi > byts[n])
7159 {
7160 /* Done all bytes at this node, do next state. */
7161 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007162 }
7163 else
7164 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007165 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007166 n += sp->ts_curi++;
7167 c = byts[n];
7168 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
7169 {
7170 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007171 sp = &stack[depth];
7172 tword[sp->ts_twordlen++] = c;
7173 sp->ts_arridx = idxs[n];
7174#ifdef FEAT_MBYTE
7175 if (has_mbyte)
7176 {
7177 fl = MB_BYTE2LEN(c);
7178 if (fl > 1)
7179 {
7180 /* There are following bytes for the same
7181 * character. We must find all bytes before
7182 * trying delete/insert/swap/etc. */
7183 sp->ts_tcharlen = fl;
7184 sp->ts_tcharidx = 1;
7185 sp->ts_isdiff = DIFF_INSERT;
7186 }
7187 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007188 else
7189 fl = 1;
7190 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00007191#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00007192 {
7193 /* If the previous character was the same, thus
7194 * doubling a character, give a bonus to the
7195 * score. */
7196 if (sp->ts_twordlen >= 2
7197 && tword[sp->ts_twordlen - 2] == c)
7198 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
7199 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007200 }
7201 }
7202 break;
7203
7204 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00007205 /*
7206 * Swap two bytes in the bad word: "12" -> "21".
7207 * We change "fword" here, it's changed back afterwards.
7208 */
7209 p = fword + sp->ts_fidx;
7210 c = *p;
7211 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007212 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007213 /* End of word, can't swap or replace. */
7214 sp->ts_state = STATE_FINAL;
7215 break;
7216 }
7217#ifdef FEAT_MBYTE
7218 if (has_mbyte)
7219 {
7220 n = mb_ptr2len_check(p);
7221 c = mb_ptr2char(p);
7222 c2 = mb_ptr2char(p + n);
7223 }
7224 else
7225#endif
7226 c2 = p[1];
7227 if (c == c2)
7228 {
7229 /* Characters are identical, swap won't do anything. */
7230 sp->ts_state = STATE_SWAP3;
7231 break;
7232 }
7233 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
7234 {
7235 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007236 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007237#ifdef FEAT_MBYTE
7238 if (has_mbyte)
7239 {
7240 fl = mb_char2len(c2);
7241 mch_memmove(p, p + n, fl);
7242 mb_char2bytes(c, p + fl);
7243 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
7244 }
7245 else
7246#endif
7247 {
7248 p[0] = c2;
7249 p[1] = c;
7250 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
7251 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007252 }
7253 else
7254 /* If this swap doesn't work then SWAP3 won't either. */
7255 sp->ts_state = STATE_REP_INI;
7256 break;
7257
Bram Moolenaarea424162005-06-16 21:51:00 +00007258 case STATE_UNSWAP:
7259 /* Undo the STATE_SWAP swap: "21" -> "12". */
7260 p = fword + sp->ts_fidx;
7261#ifdef FEAT_MBYTE
7262 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007263 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007264 n = MB_BYTE2LEN(*p);
7265 c = mb_ptr2char(p + n);
7266 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
7267 mb_char2bytes(c, p);
7268 }
7269 else
7270#endif
7271 {
7272 c = *p;
7273 *p = p[1];
7274 p[1] = c;
7275 }
7276 /*FALLTHROUGH*/
7277
7278 case STATE_SWAP3:
7279 /* Swap two bytes, skipping one: "123" -> "321". We change
7280 * "fword" here, it's changed back afterwards. */
7281 p = fword + sp->ts_fidx;
7282#ifdef FEAT_MBYTE
7283 if (has_mbyte)
7284 {
7285 n = mb_ptr2len_check(p);
7286 c = mb_ptr2char(p);
7287 fl = mb_ptr2len_check(p + n);
7288 c2 = mb_ptr2char(p + n);
7289 c3 = mb_ptr2char(p + n + fl);
7290 }
7291 else
7292#endif
7293 {
7294 c = *p;
7295 c2 = p[1];
7296 c3 = p[2];
7297 }
7298
7299 /* When characters are identical: "121" then SWAP3 result is
7300 * identical, ROT3L result is same as SWAP: "211", ROT3L
7301 * result is same as SWAP on next char: "112". Thus skip all
7302 * swapping. Also skip when c3 is NUL. */
7303 if (c == c3 || c3 == NUL)
7304 {
7305 sp->ts_state = STATE_REP_INI;
7306 break;
7307 }
7308 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7309 {
7310 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007311 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007312#ifdef FEAT_MBYTE
7313 if (has_mbyte)
7314 {
7315 tl = mb_char2len(c3);
7316 mch_memmove(p, p + n + fl, tl);
7317 mb_char2bytes(c2, p + tl);
7318 mb_char2bytes(c, p + fl + tl);
7319 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
7320 }
7321 else
7322#endif
7323 {
7324 p[0] = p[2];
7325 p[2] = c;
7326 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7327 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007328 }
7329 else
7330 sp->ts_state = STATE_REP_INI;
7331 break;
7332
Bram Moolenaarea424162005-06-16 21:51:00 +00007333 case STATE_UNSWAP3:
7334 /* Undo STATE_SWAP3: "321" -> "123" */
7335 p = fword + sp->ts_fidx;
7336#ifdef FEAT_MBYTE
7337 if (has_mbyte)
7338 {
7339 n = MB_BYTE2LEN(*p);
7340 c2 = mb_ptr2char(p + n);
7341 fl = MB_BYTE2LEN(p[n]);
7342 c = mb_ptr2char(p + n + fl);
7343 tl = MB_BYTE2LEN(p[n + fl]);
7344 mch_memmove(p + fl + tl, p, n);
7345 mb_char2bytes(c, p);
7346 mb_char2bytes(c2, p + tl);
7347 }
7348 else
7349#endif
7350 {
7351 c = *p;
7352 *p = p[2];
7353 p[2] = c;
7354 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007355
Bram Moolenaarea424162005-06-16 21:51:00 +00007356 /* Rotate three characters left: "123" -> "231". We change
7357 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007358 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7359 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007360 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007361 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007362 p = fword + sp->ts_fidx;
7363#ifdef FEAT_MBYTE
7364 if (has_mbyte)
7365 {
7366 n = mb_ptr2len_check(p);
7367 c = mb_ptr2char(p);
7368 fl = mb_ptr2len_check(p + n);
7369 fl += mb_ptr2len_check(p + n + fl);
7370 mch_memmove(p, p + n, fl);
7371 mb_char2bytes(c, p + fl);
7372 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
7373 }
7374 else
7375#endif
7376 {
7377 c = *p;
7378 *p = p[1];
7379 p[1] = p[2];
7380 p[2] = c;
7381 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7382 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007383 }
7384 else
7385 sp->ts_state = STATE_REP_INI;
7386 break;
7387
Bram Moolenaarea424162005-06-16 21:51:00 +00007388 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007389 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00007390 p = fword + sp->ts_fidx;
7391#ifdef FEAT_MBYTE
7392 if (has_mbyte)
7393 {
7394 n = MB_BYTE2LEN(*p);
7395 n += MB_BYTE2LEN(p[n]);
7396 c = mb_ptr2char(p + n);
7397 tl = MB_BYTE2LEN(p[n]);
7398 mch_memmove(p + tl, p, n);
7399 mb_char2bytes(c, p);
7400 }
7401 else
7402#endif
7403 {
7404 c = p[2];
7405 p[2] = p[1];
7406 p[1] = *p;
7407 *p = c;
7408 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007409
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007410 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00007411 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007412 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7413 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007414 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007415 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007416 p = fword + sp->ts_fidx;
7417#ifdef FEAT_MBYTE
7418 if (has_mbyte)
7419 {
7420 n = mb_ptr2len_check(p);
7421 n += mb_ptr2len_check(p + n);
7422 c = mb_ptr2char(p + n);
7423 tl = mb_ptr2len_check(p + n);
7424 mch_memmove(p + tl, p, n);
7425 mb_char2bytes(c, p);
7426 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
7427 }
7428 else
7429#endif
7430 {
7431 c = p[2];
7432 p[2] = p[1];
7433 p[1] = *p;
7434 *p = c;
7435 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7436 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007437 }
7438 else
7439 sp->ts_state = STATE_REP_INI;
7440 break;
7441
Bram Moolenaarea424162005-06-16 21:51:00 +00007442 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007443 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00007444 p = fword + sp->ts_fidx;
7445#ifdef FEAT_MBYTE
7446 if (has_mbyte)
7447 {
7448 c = mb_ptr2char(p);
7449 tl = MB_BYTE2LEN(*p);
7450 n = MB_BYTE2LEN(p[tl]);
7451 n += MB_BYTE2LEN(p[tl + n]);
7452 mch_memmove(p, p + tl, n);
7453 mb_char2bytes(c, p + n);
7454 }
7455 else
7456#endif
7457 {
7458 c = *p;
7459 *p = p[1];
7460 p[1] = p[2];
7461 p[2] = c;
7462 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007463 /*FALLTHROUGH*/
7464
7465 case STATE_REP_INI:
7466 /* Check if matching with REP items from the .aff file would
7467 * work. Quickly skip if there are no REP items or the score
7468 * is going to be too high anyway. */
7469 gap = &lp->lp_slang->sl_rep;
7470 if (gap->ga_len == 0
7471 || sp->ts_score + SCORE_REP >= su->su_maxscore)
7472 {
7473 sp->ts_state = STATE_FINAL;
7474 break;
7475 }
7476
7477 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00007478 * may match. If the index is -1 there is none. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007479 sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]];
7480 if (sp->ts_curi < 0)
7481 {
7482 sp->ts_state = STATE_FINAL;
7483 break;
7484 }
7485
7486 sp->ts_state = STATE_REP;
7487 /*FALLTHROUGH*/
7488
7489 case STATE_REP:
7490 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00007491 * match replace the characters and check if the resulting
7492 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007493 p = fword + sp->ts_fidx;
7494
7495 gap = &lp->lp_slang->sl_rep;
7496 while (sp->ts_curi < gap->ga_len)
7497 {
7498 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
7499 if (*ftp->ft_from != *p)
7500 {
7501 /* past possible matching entries */
7502 sp->ts_curi = gap->ga_len;
7503 break;
7504 }
7505 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
7506 && try_deeper(su, stack, depth, SCORE_REP))
7507 {
7508 /* Need to undo this afterwards. */
7509 sp->ts_state = STATE_REP_UNDO;
7510
7511 /* Change the "from" to the "to" string. */
7512 ++depth;
7513 fl = STRLEN(ftp->ft_from);
7514 tl = STRLEN(ftp->ft_to);
7515 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007517 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007518 repextra += tl - fl;
7519 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007520 mch_memmove(p, ftp->ft_to, tl);
7521 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +00007522#ifdef FEAT_MBYTE
7523 stack[depth].ts_tcharlen = 0;
7524#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007525 break;
7526 }
7527 }
7528
7529 if (sp->ts_curi >= gap->ga_len)
7530 /* No (more) matches. */
7531 sp->ts_state = STATE_FINAL;
7532
7533 break;
7534
7535 case STATE_REP_UNDO:
7536 /* Undo a REP replacement and continue with the next one. */
7537 ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data
7538 + sp->ts_curi - 1;
7539 fl = STRLEN(ftp->ft_from);
7540 tl = STRLEN(ftp->ft_to);
7541 p = fword + sp->ts_fidx;
7542 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007543 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007544 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007545 repextra -= tl - fl;
7546 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007547 mch_memmove(p, ftp->ft_from, fl);
7548 sp->ts_state = STATE_REP;
7549 break;
7550
7551 default:
7552 /* Did all possible states at this level, go up one level. */
7553 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007554
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007555 if (depth >= 0 && stack[depth].ts_prefixdepth == PREFIXTREE)
7556 {
7557 /* Continue in or go back to the prefix tree. */
7558 byts = pbyts;
7559 idxs = pidxs;
7560 splitoff = 0;
7561 }
7562
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007563 /* Don't check for CTRL-C too often, it takes time. */
7564 line_breakcheck();
7565 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007566 }
7567 }
7568}
7569
7570/*
7571 * Try going one level deeper in the tree.
7572 */
7573 static int
7574try_deeper(su, stack, depth, score_add)
7575 suginfo_T *su;
7576 trystate_T *stack;
7577 int depth;
7578 int score_add;
7579{
7580 int newscore;
7581
7582 /* Refuse to go deeper if the scrore is getting too big. */
7583 newscore = stack[depth].ts_score + score_add;
7584 if (newscore >= su->su_maxscore)
7585 return FALSE;
7586
Bram Moolenaarea424162005-06-16 21:51:00 +00007587 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007588 stack[depth + 1].ts_state = STATE_START;
7589 stack[depth + 1].ts_score = newscore;
7590 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007591 return TRUE;
7592}
7593
7594/*
7595 * "fword" is a good word with case folded. Find the matching keep-case
7596 * words and put it in "kword".
7597 * Theoretically there could be several keep-case words that result in the
7598 * same case-folded word, but we only find one...
7599 */
7600 static void
7601find_keepcap_word(slang, fword, kword)
7602 slang_T *slang;
7603 char_u *fword;
7604 char_u *kword;
7605{
7606 char_u uword[MAXWLEN]; /* "fword" in upper-case */
7607 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007608 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007609
7610 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007611 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007612 int round[MAXWLEN];
7613 int fwordidx[MAXWLEN];
7614 int uwordidx[MAXWLEN];
7615 int kwordlen[MAXWLEN];
7616
7617 int flen, ulen;
7618 int l;
7619 int len;
7620 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007621 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007622 char_u *p;
7623 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007624 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007625
7626 if (byts == NULL)
7627 {
7628 /* array is empty: "cannot happen" */
7629 *kword = NUL;
7630 return;
7631 }
7632
7633 /* Make an all-cap version of "fword". */
7634 allcap_copy(fword, uword);
7635
7636 /*
7637 * Each character needs to be tried both case-folded and upper-case.
7638 * All this gets very complicated if we keep in mind that changing case
7639 * may change the byte length of a multi-byte character...
7640 */
7641 depth = 0;
7642 arridx[0] = 0;
7643 round[0] = 0;
7644 fwordidx[0] = 0;
7645 uwordidx[0] = 0;
7646 kwordlen[0] = 0;
7647 while (depth >= 0)
7648 {
7649 if (fword[fwordidx[depth]] == NUL)
7650 {
7651 /* We are at the end of "fword". If the tree allows a word to end
7652 * here we have found a match. */
7653 if (byts[arridx[depth] + 1] == 0)
7654 {
7655 kword[kwordlen[depth]] = NUL;
7656 return;
7657 }
7658
7659 /* kword is getting too long, continue one level up */
7660 --depth;
7661 }
7662 else if (++round[depth] > 2)
7663 {
7664 /* tried both fold-case and upper-case character, continue one
7665 * level up */
7666 --depth;
7667 }
7668 else
7669 {
7670 /*
7671 * round[depth] == 1: Try using the folded-case character.
7672 * round[depth] == 2: Try using the upper-case character.
7673 */
7674#ifdef FEAT_MBYTE
7675 if (has_mbyte)
7676 {
7677 flen = mb_ptr2len_check(fword + fwordidx[depth]);
7678 ulen = mb_ptr2len_check(uword + uwordidx[depth]);
7679 }
7680 else
7681#endif
7682 ulen = flen = 1;
7683 if (round[depth] == 1)
7684 {
7685 p = fword + fwordidx[depth];
7686 l = flen;
7687 }
7688 else
7689 {
7690 p = uword + uwordidx[depth];
7691 l = ulen;
7692 }
7693
7694 for (tryidx = arridx[depth]; l > 0; --l)
7695 {
7696 /* Perform a binary search in the list of accepted bytes. */
7697 len = byts[tryidx++];
7698 c = *p++;
7699 lo = tryidx;
7700 hi = tryidx + len - 1;
7701 while (lo < hi)
7702 {
7703 m = (lo + hi) / 2;
7704 if (byts[m] > c)
7705 hi = m - 1;
7706 else if (byts[m] < c)
7707 lo = m + 1;
7708 else
7709 {
7710 lo = hi = m;
7711 break;
7712 }
7713 }
7714
7715 /* Stop if there is no matching byte. */
7716 if (hi < lo || byts[lo] != c)
7717 break;
7718
7719 /* Continue at the child (if there is one). */
7720 tryidx = idxs[lo];
7721 }
7722
7723 if (l == 0)
7724 {
7725 /*
7726 * Found the matching char. Copy it to "kword" and go a
7727 * level deeper.
7728 */
7729 if (round[depth] == 1)
7730 {
7731 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
7732 flen);
7733 kwordlen[depth + 1] = kwordlen[depth] + flen;
7734 }
7735 else
7736 {
7737 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
7738 ulen);
7739 kwordlen[depth + 1] = kwordlen[depth] + ulen;
7740 }
7741 fwordidx[depth + 1] = fwordidx[depth] + flen;
7742 uwordidx[depth + 1] = uwordidx[depth] + ulen;
7743
7744 ++depth;
7745 arridx[depth] = tryidx;
7746 round[depth] = 0;
7747 }
7748 }
7749 }
7750
7751 /* Didn't find it: "cannot happen". */
7752 *kword = NUL;
7753}
7754
7755/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007756 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
7757 * su->su_sga.
7758 */
7759 static void
7760score_comp_sal(su)
7761 suginfo_T *su;
7762{
7763 langp_T *lp;
7764 char_u badsound[MAXWLEN];
7765 int i;
7766 suggest_T *stp;
7767 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007768 int score;
7769
7770 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
7771 return;
7772
7773 /* Use the sound-folding of the first language that supports it. */
7774 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7775 lp->lp_slang != NULL; ++lp)
7776 if (lp->lp_slang->sl_sal.ga_len > 0)
7777 {
7778 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007779 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007780
7781 for (i = 0; i < su->su_ga.ga_len; ++i)
7782 {
7783 stp = &SUG(su->su_ga, i);
7784
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007785 /* Case-fold the suggested word, sound-fold it and compute the
7786 * sound-a-like score. */
7787 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007788 if (score < SCORE_MAXMAX)
7789 {
7790 /* Add the suggestion. */
7791 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
7792 sstp->st_word = vim_strsave(stp->st_word);
7793 if (sstp->st_word != NULL)
7794 {
7795 sstp->st_score = score;
7796 sstp->st_altscore = 0;
7797 sstp->st_orglen = stp->st_orglen;
7798 ++su->su_sga.ga_len;
7799 }
7800 }
7801 }
7802 break;
7803 }
7804}
7805
7806/*
7807 * Combine the list of suggestions in su->su_ga and su->su_sga.
7808 * They are intwined.
7809 */
7810 static void
7811score_combine(su)
7812 suginfo_T *su;
7813{
7814 int i;
7815 int j;
7816 garray_T ga;
7817 garray_T *gap;
7818 langp_T *lp;
7819 suggest_T *stp;
7820 char_u *p;
7821 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007822 int round;
7823
7824 /* Add the alternate score to su_ga. */
7825 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7826 lp->lp_slang != NULL; ++lp)
7827 {
7828 if (lp->lp_slang->sl_sal.ga_len > 0)
7829 {
7830 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007831 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007832
7833 for (i = 0; i < su->su_ga.ga_len; ++i)
7834 {
7835 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007836 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
7837 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007838 if (stp->st_altscore == SCORE_MAXMAX)
7839 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
7840 else
7841 stp->st_score = (stp->st_score * 3
7842 + stp->st_altscore) / 4;
7843 stp->st_salscore = FALSE;
7844 }
7845 break;
7846 }
7847 }
7848
7849 /* Add the alternate score to su_sga. */
7850 for (i = 0; i < su->su_sga.ga_len; ++i)
7851 {
7852 stp = &SUG(su->su_sga, i);
7853 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
7854 if (stp->st_score == SCORE_MAXMAX)
7855 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
7856 else
7857 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
7858 stp->st_salscore = TRUE;
7859 }
7860
7861 /* Sort the suggestions and truncate at "maxcount" for both lists. */
7862 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
7863 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
7864
7865 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
7866 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
7867 return;
7868
7869 stp = &SUG(ga, 0);
7870 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
7871 {
7872 /* round 1: get a suggestion from su_ga
7873 * round 2: get a suggestion from su_sga */
7874 for (round = 1; round <= 2; ++round)
7875 {
7876 gap = round == 1 ? &su->su_ga : &su->su_sga;
7877 if (i < gap->ga_len)
7878 {
7879 /* Don't add a word if it's already there. */
7880 p = SUG(*gap, i).st_word;
7881 for (j = 0; j < ga.ga_len; ++j)
7882 if (STRCMP(stp[j].st_word, p) == 0)
7883 break;
7884 if (j == ga.ga_len)
7885 stp[ga.ga_len++] = SUG(*gap, i);
7886 else
7887 vim_free(p);
7888 }
7889 }
7890 }
7891
7892 ga_clear(&su->su_ga);
7893 ga_clear(&su->su_sga);
7894
7895 /* Truncate the list to the number of suggestions that will be displayed. */
7896 if (ga.ga_len > su->su_maxcount)
7897 {
7898 for (i = su->su_maxcount; i < ga.ga_len; ++i)
7899 vim_free(stp[i].st_word);
7900 ga.ga_len = su->su_maxcount;
7901 }
7902
7903 su->su_ga = ga;
7904}
7905
7906/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007907 * For the goodword in "stp" compute the soundalike score compared to the
7908 * badword.
7909 */
7910 static int
7911stp_sal_score(stp, su, slang, badsound)
7912 suggest_T *stp;
7913 suginfo_T *su;
7914 slang_T *slang;
7915 char_u *badsound; /* sound-folded badword */
7916{
7917 char_u *p;
7918 char_u badsound2[MAXWLEN];
7919 char_u fword[MAXWLEN];
7920 char_u goodsound[MAXWLEN];
7921
7922 if (stp->st_orglen <= su->su_badlen)
7923 p = badsound;
7924 else
7925 {
7926 /* soundfold the bad word with more characters following */
7927 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
7928
7929 /* When joining two words the sound often changes a lot. E.g., "t he"
7930 * sounds like "t h" while "the" sounds like "@". Avoid that by
7931 * removing the space. Don't do it when the good word also contains a
7932 * space. */
7933 if (vim_iswhite(su->su_badptr[su->su_badlen])
7934 && *skiptowhite(stp->st_word) == NUL)
7935 for (p = fword; *(p = skiptowhite(p)) != NUL; )
7936 mch_memmove(p, p + 1, STRLEN(p));
7937
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007938 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007939 p = badsound2;
7940 }
7941
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007942 /* Sound-fold the word and compute the score for the difference. */
7943 spell_soundfold(slang, stp->st_word, FALSE, goodsound);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007944
7945 return soundalike_score(goodsound, p);
7946}
7947
7948/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007949 * Find suggestions by comparing the word in a sound-a-like form.
7950 */
7951 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00007952suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007953 suginfo_T *su;
7954{
7955 char_u salword[MAXWLEN];
7956 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007957 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007958 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007959 int curi[MAXWLEN];
7960 langp_T *lp;
7961 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007962 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007963 int depth;
7964 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007965 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007966 int round;
7967 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007968 int sound_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007969
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007970 /* Do this for all languages that support sound folding. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007971 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7972 lp->lp_slang != NULL; ++lp)
7973 {
7974 if (lp->lp_slang->sl_sal.ga_len > 0)
7975 {
7976 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007977 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007978
7979 /*
7980 * Go through the whole tree, soundfold each word and compare.
7981 * round 1: use the case-folded tree.
7982 * round 2: use the keep-case tree.
7983 */
7984 for (round = 1; round <= 2; ++round)
7985 {
7986 if (round == 1)
7987 {
7988 byts = lp->lp_slang->sl_fbyts;
7989 idxs = lp->lp_slang->sl_fidxs;
7990 }
7991 else
7992 {
7993 byts = lp->lp_slang->sl_kbyts;
7994 idxs = lp->lp_slang->sl_kidxs;
7995 }
7996
7997 depth = 0;
7998 arridx[0] = 0;
7999 curi[0] = 1;
8000 while (depth >= 0 && !got_int)
8001 {
8002 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008003 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008004 /* Done all bytes at this node, go up one level. */
8005 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008006 line_breakcheck();
8007 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008008 else
8009 {
8010 /* Do one more byte at this node. */
8011 n = arridx[depth] + curi[depth];
8012 ++curi[depth];
8013 c = byts[n];
8014 if (c == 0)
8015 {
8016 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008017 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008018 if (round == 2 || (flags & WF_KEEPCAP) == 0)
8019 {
8020 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008021 /* Sound-fold. Only in keep-case tree need to
8022 * case-fold the word. */
8023 spell_soundfold(lp->lp_slang, tword,
8024 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008025
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008026 /* Compute the edit distance between the
8027 * sound-a-like words. */
8028 sound_score = soundalike_score(salword,
8029 tsalword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008030 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008031 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008032 char_u cword[MAXWLEN];
8033 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008034 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008035
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008036 flags |= su->su_badflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008037 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008038 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008039 /* Need to fix case according to
8040 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008041 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008042 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008043 }
8044 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008045 p = tword;
8046
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008047 if (sps_flags & SPS_DOUBLE)
8048 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008049 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008050 sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008051 else
8052 {
8053 /* Compute the score. */
8054 score = spell_edit_score(
8055 su->su_badword, p);
8056 if (sps_flags & SPS_BEST)
8057 /* give a bonus for the good word
8058 * sounding the same as the bad
8059 * word */
8060 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008061 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008062 RESCORE(score, sound_score),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008063 sound_score, TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008064 else
8065 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008066 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008067 score + sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008068 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008069 }
8070 }
8071
8072 /* Skip over other NUL bytes. */
8073 while (byts[n + 1] == 0)
8074 {
8075 ++n;
8076 ++curi[depth];
8077 }
8078 }
8079 else
8080 {
8081 /* Normal char, go one level deeper. */
8082 tword[depth++] = c;
8083 arridx[depth] = idxs[n];
8084 curi[depth] = 1;
8085 }
8086 }
8087 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008088 }
8089 }
8090 }
8091}
8092
8093/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008094 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008095 */
8096 static void
8097make_case_word(fword, cword, flags)
8098 char_u *fword;
8099 char_u *cword;
8100 int flags;
8101{
8102 if (flags & WF_ALLCAP)
8103 /* Make it all upper-case */
8104 allcap_copy(fword, cword);
8105 else if (flags & WF_ONECAP)
8106 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008107 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008108 else
8109 /* Use goodword as-is. */
8110 STRCPY(cword, fword);
8111}
8112
Bram Moolenaarea424162005-06-16 21:51:00 +00008113/*
8114 * Use map string "map" for languages "lp".
8115 */
8116 static void
8117set_map_str(lp, map)
8118 slang_T *lp;
8119 char_u *map;
8120{
8121 char_u *p;
8122 int headc = 0;
8123 int c;
8124 int i;
8125
8126 if (*map == NUL)
8127 {
8128 lp->sl_has_map = FALSE;
8129 return;
8130 }
8131 lp->sl_has_map = TRUE;
8132
8133 /* Init the array and hash table empty. */
8134 for (i = 0; i < 256; ++i)
8135 lp->sl_map_array[i] = 0;
8136#ifdef FEAT_MBYTE
8137 hash_init(&lp->sl_map_hash);
8138#endif
8139
8140 /*
8141 * The similar characters are stored separated with slashes:
8142 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
8143 * before the same slash. For characters above 255 sl_map_hash is used.
8144 */
8145 for (p = map; *p != NUL; )
8146 {
8147#ifdef FEAT_MBYTE
8148 c = mb_ptr2char_adv(&p);
8149#else
8150 c = *p++;
8151#endif
8152 if (c == '/')
8153 headc = 0;
8154 else
8155 {
8156 if (headc == 0)
8157 headc = c;
8158
8159#ifdef FEAT_MBYTE
8160 /* Characters above 255 don't fit in sl_map_array[], put them in
8161 * the hash table. Each entry is the char, a NUL the headchar and
8162 * a NUL. */
8163 if (c >= 256)
8164 {
8165 int cl = mb_char2len(c);
8166 int headcl = mb_char2len(headc);
8167 char_u *b;
8168 hash_T hash;
8169 hashitem_T *hi;
8170
8171 b = alloc((unsigned)(cl + headcl + 2));
8172 if (b == NULL)
8173 return;
8174 mb_char2bytes(c, b);
8175 b[cl] = NUL;
8176 mb_char2bytes(headc, b + cl + 1);
8177 b[cl + 1 + headcl] = NUL;
8178 hash = hash_hash(b);
8179 hi = hash_lookup(&lp->sl_map_hash, b, hash);
8180 if (HASHITEM_EMPTY(hi))
8181 hash_add_item(&lp->sl_map_hash, hi, b, hash);
8182 else
8183 {
8184 /* This should have been checked when generating the .spl
8185 * file. */
8186 EMSG(_("E999: duplicate char in MAP entry"));
8187 vim_free(b);
8188 }
8189 }
8190 else
8191#endif
8192 lp->sl_map_array[c] = headc;
8193 }
8194 }
8195}
8196
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008197/*
8198 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
8199 * lines in the .aff file.
8200 */
8201 static int
8202similar_chars(slang, c1, c2)
8203 slang_T *slang;
8204 int c1;
8205 int c2;
8206{
Bram Moolenaarea424162005-06-16 21:51:00 +00008207 int m1, m2;
8208#ifdef FEAT_MBYTE
8209 char_u buf[MB_MAXBYTES];
8210 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008211
Bram Moolenaarea424162005-06-16 21:51:00 +00008212 if (c1 >= 256)
8213 {
8214 buf[mb_char2bytes(c1, buf)] = 0;
8215 hi = hash_find(&slang->sl_map_hash, buf);
8216 if (HASHITEM_EMPTY(hi))
8217 m1 = 0;
8218 else
8219 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8220 }
8221 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008222#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00008223 m1 = slang->sl_map_array[c1];
8224 if (m1 == 0)
8225 return FALSE;
8226
8227
8228#ifdef FEAT_MBYTE
8229 if (c2 >= 256)
8230 {
8231 buf[mb_char2bytes(c2, buf)] = 0;
8232 hi = hash_find(&slang->sl_map_hash, buf);
8233 if (HASHITEM_EMPTY(hi))
8234 m2 = 0;
8235 else
8236 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8237 }
8238 else
8239#endif
8240 m2 = slang->sl_map_array[c2];
8241
8242 return m1 == m2;
8243}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008244
8245/*
8246 * Add a suggestion to the list of suggestions.
8247 * Do not add a duplicate suggestion or suggestions with a bad score.
8248 * When "use_score" is not zero it's used, otherwise the score is computed
8249 * with spell_edit_score().
8250 */
8251 static void
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008252add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008253 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008254 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008255 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008256 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008257 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008258 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008259 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008260{
8261 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008262 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008263 char_u *p = NULL;
8264 int c = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008265
8266 /* Check that the word wasn't banned. */
8267 if (was_banned(su, goodword))
8268 return;
8269
Bram Moolenaar0c405862005-06-22 22:26:26 +00008270 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
8271 * Remove the common part from "goodword". */
8272 i = badlen - su->su_badlen;
8273 if (i > 0)
8274 {
8275 /* This assumes there was no case folding or it didn't change the
8276 * length... */
8277 p = goodword + STRLEN(goodword) - i;
8278 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
8279 {
8280 badlen = su->su_badlen;
8281 c = *p;
8282 *p = NUL;
8283 }
8284 else
8285 p = NULL;
8286 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008287 else if (i < 0)
8288 {
8289 /* When replacing part of the word check that we actually change
8290 * something. For "the the" a suggestion can be replacing the first
8291 * "the" with itself, since "the" wasn't banned. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008292 if (badlen == (int)STRLEN(goodword)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008293 && STRNCMP(su->su_badword, goodword, badlen) == 0)
8294 return;
8295 }
8296
Bram Moolenaar0c405862005-06-22 22:26:26 +00008297
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008298 if (score <= su->su_maxscore)
8299 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008300 /* Check if the word is already there. Also check the length that is
8301 * being replaced "thes," -> "these" is a different suggestion from
8302 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008303 stp = &SUG(*gap, 0);
8304 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008305 if (STRCMP(stp[i].st_word, goodword) == 0
8306 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008307 {
8308 /* Found it. Remember the lowest score. */
8309 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008311 stp[i].st_score = score;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008312 stp[i].st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008313 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008314 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008315 break;
8316 }
8317
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008318 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008319 {
8320 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008321 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008322 stp->st_word = vim_strsave(goodword);
8323 if (stp->st_word != NULL)
8324 {
8325 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008326 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008327 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008328 stp->st_orglen = badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008329 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008330
8331 /* If we have too many suggestions now, sort the list and keep
8332 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008333 if (gap->ga_len > SUG_MAX_COUNT(su))
8334 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
8335 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008336 }
8337 }
8338 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008339
8340 if (p != NULL)
8341 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008342}
8343
8344/*
8345 * Add a word to be banned.
8346 */
8347 static void
8348add_banned(su, word)
8349 suginfo_T *su;
8350 char_u *word;
8351{
8352 char_u *s = vim_strsave(word);
8353 hash_T hash;
8354 hashitem_T *hi;
8355
8356 if (s != NULL)
8357 {
8358 hash = hash_hash(s);
8359 hi = hash_lookup(&su->su_banned, s, hash);
8360 if (HASHITEM_EMPTY(hi))
8361 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008362 else
8363 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008364 }
8365}
8366
8367/*
8368 * Return TRUE if a word appears in the list of banned words.
8369 */
8370 static int
8371was_banned(su, word)
8372 suginfo_T *su;
8373 char_u *word;
8374{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008375 hashitem_T *hi = hash_find(&su->su_banned, word);
8376
8377 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008378}
8379
8380/*
8381 * Free the banned words in "su".
8382 */
8383 static void
8384free_banned(su)
8385 suginfo_T *su;
8386{
8387 int todo;
8388 hashitem_T *hi;
8389
8390 todo = su->su_banned.ht_used;
8391 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
8392 {
8393 if (!HASHITEM_EMPTY(hi))
8394 {
8395 vim_free(hi->hi_key);
8396 --todo;
8397 }
8398 }
8399 hash_clear(&su->su_banned);
8400}
8401
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008402/*
8403 * Recompute the score if sound-folding is possible. This is slow,
8404 * thus only done for the final results.
8405 */
8406 static void
8407rescore_suggestions(su)
8408 suginfo_T *su;
8409{
8410 langp_T *lp;
8411 suggest_T *stp;
8412 char_u sal_badword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008413 int i;
8414
8415 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8416 lp->lp_slang != NULL; ++lp)
8417 {
8418 if (lp->lp_slang->sl_sal.ga_len > 0)
8419 {
8420 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008421 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008422
8423 for (i = 0; i < su->su_ga.ga_len; ++i)
8424 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008425 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008426 if (!stp->st_had_bonus)
8427 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008428 stp->st_altscore = stp_sal_score(stp, su,
8429 lp->lp_slang, sal_badword);
8430 if (stp->st_altscore == SCORE_MAXMAX)
8431 stp->st_altscore = SCORE_BIG;
8432 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008433 }
8434 }
8435 break;
8436 }
8437 }
8438}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008439
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008440static int
8441#ifdef __BORLANDC__
8442_RTLENTRYF
8443#endif
8444sug_compare __ARGS((const void *s1, const void *s2));
8445
8446/*
8447 * Function given to qsort() to sort the suggestions on st_score.
8448 */
8449 static int
8450#ifdef __BORLANDC__
8451_RTLENTRYF
8452#endif
8453sug_compare(s1, s2)
8454 const void *s1;
8455 const void *s2;
8456{
8457 suggest_T *p1 = (suggest_T *)s1;
8458 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008459 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008460
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008461 if (n == 0)
8462 return p1->st_altscore - p2->st_altscore;
8463 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008464}
8465
8466/*
8467 * Cleanup the suggestions:
8468 * - Sort on score.
8469 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008470 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008471 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008472 static int
8473cleanup_suggestions(gap, maxscore, keep)
8474 garray_T *gap;
8475 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008476 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008477{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008478 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008479 int i;
8480
8481 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008482 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008483
8484 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008485 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008486 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008487 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008488 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008489 gap->ga_len = keep;
8490 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008491 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008492 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008493}
8494
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008495#if defined(FEAT_EVAL) || defined(PROTO)
8496/*
8497 * Soundfold a string, for soundfold().
8498 * Result is in allocated memory, NULL for an error.
8499 */
8500 char_u *
8501eval_soundfold(word)
8502 char_u *word;
8503{
8504 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008505 char_u sound[MAXWLEN];
8506
8507 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
8508 /* Use the sound-folding of the first language that supports it. */
8509 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8510 lp->lp_slang != NULL; ++lp)
8511 if (lp->lp_slang->sl_sal.ga_len > 0)
8512 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008513 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008514 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008515 return vim_strsave(sound);
8516 }
8517
8518 /* No language with sound folding, return word as-is. */
8519 return vim_strsave(word);
8520}
8521#endif
8522
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008523/*
8524 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
8525 */
8526 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008527spell_soundfold(slang, inword, folded, res)
8528 slang_T *slang;
8529 char_u *inword;
8530 int folded; /* "inword" is already case-folded */
8531 char_u *res;
8532{
8533 char_u fword[MAXWLEN];
8534 char_u *word;
8535
8536 if (slang->sl_sofo)
8537 /* SOFOFROM and SOFOTO used */
8538 spell_soundfold_sofo(slang, inword, res);
8539 else
8540 {
8541 /* SAL items used. Requires the word to be case-folded. */
8542 if (folded)
8543 word = inword;
8544 else
8545 {
8546 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
8547 word = fword;
8548 }
8549
8550#ifdef FEAT_MBYTE
8551 if (has_mbyte)
8552 spell_soundfold_wsal(slang, word, res);
8553 else
8554#endif
8555 spell_soundfold_sal(slang, word, res);
8556 }
8557}
8558
8559/*
8560 * Perform sound folding of "inword" into "res" according to SOFOFROM and
8561 * SOFOTO lines.
8562 */
8563 static void
8564spell_soundfold_sofo(slang, inword, res)
8565 slang_T *slang;
8566 char_u *inword;
8567 char_u *res;
8568{
8569 char_u *s;
8570 int ri = 0;
8571 int c;
8572
8573#ifdef FEAT_MBYTE
8574 if (has_mbyte)
8575 {
8576 int prevc = 0;
8577 int *ip;
8578
8579 /* The sl_sal_first[] table contains the translation for chars up to
8580 * 255, sl_sal the rest. */
8581 for (s = inword; *s != NUL; )
8582 {
8583 c = mb_ptr2char_adv(&s);
8584 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
8585 c = ' ';
8586 else if (c < 256)
8587 c = slang->sl_sal_first[c];
8588 else
8589 {
8590 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
8591 if (ip == NULL) /* empty list, can't match */
8592 c = NUL;
8593 else
8594 for (;;) /* find "c" in the list */
8595 {
8596 if (*ip == 0) /* not found */
8597 {
8598 c = NUL;
8599 break;
8600 }
8601 if (*ip == c) /* match! */
8602 {
8603 c = ip[1];
8604 break;
8605 }
8606 ip += 2;
8607 }
8608 }
8609
8610 if (c != NUL && c != prevc)
8611 {
8612 ri += mb_char2bytes(c, res + ri);
8613 if (ri + MB_MAXBYTES > MAXWLEN)
8614 break;
8615 prevc = c;
8616 }
8617 }
8618 }
8619 else
8620#endif
8621 {
8622 /* The sl_sal_first[] table contains the translation. */
8623 for (s = inword; (c = *s) != NUL; ++s)
8624 {
8625 if (vim_iswhite(c))
8626 c = ' ';
8627 else
8628 c = slang->sl_sal_first[c];
8629 if (c != NUL && (ri == 0 || res[ri - 1] != c))
8630 res[ri++] = c;
8631 }
8632 }
8633
8634 res[ri] = NUL;
8635}
8636
8637 static void
8638spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008639 slang_T *slang;
8640 char_u *inword;
8641 char_u *res;
8642{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008643 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008644 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008645 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008646 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008647 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008648 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008649 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008650 int n, k = 0;
8651 int z0;
8652 int k0;
8653 int n0;
8654 int c;
8655 int pri;
8656 int p0 = -333;
8657 int c0;
8658
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008659 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008660 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008661 if (slang->sl_rem_accents)
8662 {
8663 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008664 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008665 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008666 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008667 {
8668 *t++ = ' ';
8669 s = skipwhite(s);
8670 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008671 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008672 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008673 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008674 *t++ = *s;
8675 ++s;
8676 }
8677 }
8678 *t = NUL;
8679 }
8680 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008681 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008682
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008683 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008684
8685 /*
8686 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008687 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008688 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008689 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008690 while ((c = word[i]) != NUL)
8691 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008692 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008693 n = slang->sl_sal_first[c];
8694 z0 = 0;
8695
8696 if (n >= 0)
8697 {
8698 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008699 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008700 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008701 /* Quickly skip entries that don't match the word. Most
8702 * entries are less then three chars, optimize for that. */
8703 k = smp[n].sm_leadlen;
8704 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008705 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008706 if (word[i + 1] != s[1])
8707 continue;
8708 if (k > 2)
8709 {
8710 for (j = 2; j < k; ++j)
8711 if (word[i + j] != s[j])
8712 break;
8713 if (j < k)
8714 continue;
8715 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008716 }
8717
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008718 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008719 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008720 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008721 while (*pf != NUL && *pf != word[i + k])
8722 ++pf;
8723 if (*pf == NUL)
8724 continue;
8725 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008726 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008727 s = smp[n].sm_rules;
8728 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008729
8730 p0 = *s;
8731 k0 = k;
8732 while (*s == '-' && k > 1)
8733 {
8734 k--;
8735 s++;
8736 }
8737 if (*s == '<')
8738 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008739 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008740 {
8741 /* determine priority */
8742 pri = *s - '0';
8743 s++;
8744 }
8745 if (*s == '^' && *(s + 1) == '^')
8746 s++;
8747
8748 if (*s == NUL
8749 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008750 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008751 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008752 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008753 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008754 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008755 && spell_iswordp(word + i - 1, curbuf)
8756 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008757 {
8758 /* search for followup rules, if: */
8759 /* followup and k > 1 and NO '-' in searchstring */
8760 c0 = word[i + k - 1];
8761 n0 = slang->sl_sal_first[c0];
8762
8763 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008764 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008765 {
8766 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008767 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008768 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008769 /* Quickly skip entries that don't match the word.
8770 * */
8771 k0 = smp[n0].sm_leadlen;
8772 if (k0 > 1)
8773 {
8774 if (word[i + k] != s[1])
8775 continue;
8776 if (k0 > 2)
8777 {
8778 pf = word + i + k + 1;
8779 for (j = 2; j < k0; ++j)
8780 if (*pf++ != s[j])
8781 break;
8782 if (j < k0)
8783 continue;
8784 }
8785 }
8786 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008787
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008788 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008789 {
8790 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008791 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008792 while (*pf != NUL && *pf != word[i + k0])
8793 ++pf;
8794 if (*pf == NUL)
8795 continue;
8796 ++k0;
8797 }
8798
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008799 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008800 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008801 while (*s == '-')
8802 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008803 /* "k0" gets NOT reduced because
8804 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008805 s++;
8806 }
8807 if (*s == '<')
8808 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008809 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008810 {
8811 p0 = *s - '0';
8812 s++;
8813 }
8814
8815 if (*s == NUL
8816 /* *s == '^' cuts */
8817 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008818 && !spell_iswordp(word + i + k0,
8819 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008820 {
8821 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008822 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008823 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008824
8825 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008826 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008827 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008828 /* rule fits; stop search */
8829 break;
8830 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008831 }
8832
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008833 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008834 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008835 }
8836
8837 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008838 s = smp[n].sm_to;
8839 pf = smp[n].sm_rules;
8840 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008841 if (p0 == 1 && z == 0)
8842 {
8843 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008844 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
8845 || res[reslen - 1] == *s))
8846 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008847 z0 = 1;
8848 z = 1;
8849 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008850 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008851 {
8852 word[i + k0] = *s;
8853 k0++;
8854 s++;
8855 }
8856 if (k > k0)
8857 mch_memmove(word + i + k0, word + i + k,
8858 STRLEN(word + i + k) + 1);
8859
8860 /* new "actual letter" */
8861 c = word[i];
8862 }
8863 else
8864 {
8865 /* no '<' rule used */
8866 i += k - 1;
8867 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008868 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008869 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008870 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008871 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008872 s++;
8873 }
8874 /* new "actual letter" */
8875 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008876 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008877 {
8878 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008879 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008880 mch_memmove(word, word + i + 1,
8881 STRLEN(word + i + 1) + 1);
8882 i = 0;
8883 z0 = 1;
8884 }
8885 }
8886 break;
8887 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008888 }
8889 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008890 else if (vim_iswhite(c))
8891 {
8892 c = ' ';
8893 k = 1;
8894 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008895
8896 if (z0 == 0)
8897 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008898 if (k && !p0 && reslen < MAXWLEN && c != NUL
8899 && (!slang->sl_collapse || reslen == 0
8900 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008901 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008902 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008903
8904 i++;
8905 z = 0;
8906 k = 0;
8907 }
8908 }
8909
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008910 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008911}
8912
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008913#ifdef FEAT_MBYTE
8914/*
8915 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
8916 * Multi-byte version of spell_soundfold().
8917 */
8918 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008919spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008920 slang_T *slang;
8921 char_u *inword;
8922 char_u *res;
8923{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008924 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008925 int word[MAXWLEN];
8926 int wres[MAXWLEN];
8927 int l;
8928 char_u *s;
8929 int *ws;
8930 char_u *t;
8931 int *pf;
8932 int i, j, z;
8933 int reslen;
8934 int n, k = 0;
8935 int z0;
8936 int k0;
8937 int n0;
8938 int c;
8939 int pri;
8940 int p0 = -333;
8941 int c0;
8942 int did_white = FALSE;
8943
8944 /*
8945 * Convert the multi-byte string to a wide-character string.
8946 * Remove accents, if wanted. We actually remove all non-word characters.
8947 * But keep white space.
8948 */
8949 n = 0;
8950 for (s = inword; *s != NUL; )
8951 {
8952 t = s;
8953 c = mb_ptr2char_adv(&s);
8954 if (slang->sl_rem_accents)
8955 {
8956 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
8957 {
8958 if (did_white)
8959 continue;
8960 c = ' ';
8961 did_white = TRUE;
8962 }
8963 else
8964 {
8965 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008966 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008967 continue;
8968 }
8969 }
8970 word[n++] = c;
8971 }
8972 word[n] = NUL;
8973
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008974 /*
8975 * This comes from Aspell phonet.cpp.
8976 * Converted from C++ to C. Added support for multi-byte chars.
8977 * Changed to keep spaces.
8978 */
8979 i = reslen = z = 0;
8980 while ((c = word[i]) != NUL)
8981 {
8982 /* Start with the first rule that has the character in the word. */
8983 n = slang->sl_sal_first[c & 0xff];
8984 z0 = 0;
8985
8986 if (n >= 0)
8987 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008988 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008989 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
8990 {
8991 /* Quickly skip entries that don't match the word. Most
8992 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008993 if (c != ws[0])
8994 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008995 k = smp[n].sm_leadlen;
8996 if (k > 1)
8997 {
8998 if (word[i + 1] != ws[1])
8999 continue;
9000 if (k > 2)
9001 {
9002 for (j = 2; j < k; ++j)
9003 if (word[i + j] != ws[j])
9004 break;
9005 if (j < k)
9006 continue;
9007 }
9008 }
9009
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009010 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009011 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009012 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009013 while (*pf != NUL && *pf != word[i + k])
9014 ++pf;
9015 if (*pf == NUL)
9016 continue;
9017 ++k;
9018 }
9019 s = smp[n].sm_rules;
9020 pri = 5; /* default priority */
9021
9022 p0 = *s;
9023 k0 = k;
9024 while (*s == '-' && k > 1)
9025 {
9026 k--;
9027 s++;
9028 }
9029 if (*s == '<')
9030 s++;
9031 if (VIM_ISDIGIT(*s))
9032 {
9033 /* determine priority */
9034 pri = *s - '0';
9035 s++;
9036 }
9037 if (*s == '^' && *(s + 1) == '^')
9038 s++;
9039
9040 if (*s == NUL
9041 || (*s == '^'
9042 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009043 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009044 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009045 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009046 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009047 && spell_iswordp_w(word + i - 1, curbuf)
9048 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009049 {
9050 /* search for followup rules, if: */
9051 /* followup and k > 1 and NO '-' in searchstring */
9052 c0 = word[i + k - 1];
9053 n0 = slang->sl_sal_first[c0 & 0xff];
9054
9055 if (slang->sl_followup && k > 1 && n0 >= 0
9056 && p0 != '-' && word[i + k] != NUL)
9057 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009058 /* Test follow-up rule for "word[i + k]"; loop over
9059 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009060 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
9061 == (c0 & 0xff); ++n0)
9062 {
9063 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009064 */
9065 if (c0 != ws[0])
9066 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009067 k0 = smp[n0].sm_leadlen;
9068 if (k0 > 1)
9069 {
9070 if (word[i + k] != ws[1])
9071 continue;
9072 if (k0 > 2)
9073 {
9074 pf = word + i + k + 1;
9075 for (j = 2; j < k0; ++j)
9076 if (*pf++ != ws[j])
9077 break;
9078 if (j < k0)
9079 continue;
9080 }
9081 }
9082 k0 += k - 1;
9083
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009084 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009085 {
9086 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009087 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009088 while (*pf != NUL && *pf != word[i + k0])
9089 ++pf;
9090 if (*pf == NUL)
9091 continue;
9092 ++k0;
9093 }
9094
9095 p0 = 5;
9096 s = smp[n0].sm_rules;
9097 while (*s == '-')
9098 {
9099 /* "k0" gets NOT reduced because
9100 * "if (k0 == k)" */
9101 s++;
9102 }
9103 if (*s == '<')
9104 s++;
9105 if (VIM_ISDIGIT(*s))
9106 {
9107 p0 = *s - '0';
9108 s++;
9109 }
9110
9111 if (*s == NUL
9112 /* *s == '^' cuts */
9113 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009114 && !spell_iswordp_w(word + i + k0,
9115 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009116 {
9117 if (k0 == k)
9118 /* this is just a piece of the string */
9119 continue;
9120
9121 if (p0 < pri)
9122 /* priority too low */
9123 continue;
9124 /* rule fits; stop search */
9125 break;
9126 }
9127 }
9128
9129 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
9130 == (c0 & 0xff))
9131 continue;
9132 }
9133
9134 /* replace string */
9135 ws = smp[n].sm_to_w;
9136 s = smp[n].sm_rules;
9137 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
9138 if (p0 == 1 && z == 0)
9139 {
9140 /* rule with '<' is used */
9141 if (reslen > 0 && *ws != NUL && (wres[reslen - 1] == c
9142 || wres[reslen - 1] == *ws))
9143 reslen--;
9144 z0 = 1;
9145 z = 1;
9146 k0 = 0;
9147 while (*ws != NUL && word[i + k0] != NUL)
9148 {
9149 word[i + k0] = *ws;
9150 k0++;
9151 ws++;
9152 }
9153 if (k > k0)
9154 mch_memmove(word + i + k0, word + i + k,
9155 sizeof(int) * (STRLEN(word + i + k) + 1));
9156
9157 /* new "actual letter" */
9158 c = word[i];
9159 }
9160 else
9161 {
9162 /* no '<' rule used */
9163 i += k - 1;
9164 z = 0;
9165 while (*ws != NUL && ws[1] != NUL && reslen < MAXWLEN)
9166 {
9167 if (reslen == 0 || wres[reslen - 1] != *ws)
9168 wres[reslen++] = *ws;
9169 ws++;
9170 }
9171 /* new "actual letter" */
9172 c = *ws;
9173 if (strstr((char *)s, "^^") != NULL)
9174 {
9175 if (c != NUL)
9176 wres[reslen++] = c;
9177 mch_memmove(word, word + i + 1,
9178 sizeof(int) * (STRLEN(word + i + 1) + 1));
9179 i = 0;
9180 z0 = 1;
9181 }
9182 }
9183 break;
9184 }
9185 }
9186 }
9187 else if (vim_iswhite(c))
9188 {
9189 c = ' ';
9190 k = 1;
9191 }
9192
9193 if (z0 == 0)
9194 {
9195 if (k && !p0 && reslen < MAXWLEN && c != NUL
9196 && (!slang->sl_collapse || reslen == 0
9197 || wres[reslen - 1] != c))
9198 /* condense only double letters */
9199 wres[reslen++] = c;
9200
9201 i++;
9202 z = 0;
9203 k = 0;
9204 }
9205 }
9206
9207 /* Convert wide characters in "wres" to a multi-byte string in "res". */
9208 l = 0;
9209 for (n = 0; n < reslen; ++n)
9210 {
9211 l += mb_char2bytes(wres[n], res + l);
9212 if (l + MB_MAXBYTES > MAXWLEN)
9213 break;
9214 }
9215 res[l] = NUL;
9216}
9217#endif
9218
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009219/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009220 * Compute a score for two sound-a-like words.
9221 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
9222 * Instead of a generic loop we write out the code. That keeps it fast by
9223 * avoiding checks that will not be possible.
9224 */
9225 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009226soundalike_score(goodstart, badstart)
9227 char_u *goodstart; /* sound-folded good word */
9228 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009229{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009230 char_u *goodsound = goodstart;
9231 char_u *badsound = badstart;
9232 int goodlen;
9233 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009234 int n;
9235 char_u *pl, *ps;
9236 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009237 int score = 0;
9238
9239 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
9240 * counted so much, vowels halfway the word aren't counted at all. */
9241 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
9242 {
9243 score = SCORE_DEL / 2;
9244 if (*badsound == '*')
9245 ++badsound;
9246 else
9247 ++goodsound;
9248 }
9249
9250 goodlen = STRLEN(goodsound);
9251 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009252
9253 /* Return quickly if the lenghts are too different to be fixed by two
9254 * changes. */
9255 n = goodlen - badlen;
9256 if (n < -2 || n > 2)
9257 return SCORE_MAXMAX;
9258
9259 if (n > 0)
9260 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009261 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009262 ps = badsound;
9263 }
9264 else
9265 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009266 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009267 ps = goodsound;
9268 }
9269
9270 /* Skip over the identical part. */
9271 while (*pl == *ps && *pl != NUL)
9272 {
9273 ++pl;
9274 ++ps;
9275 }
9276
9277 switch (n)
9278 {
9279 case -2:
9280 case 2:
9281 /*
9282 * Must delete two characters from "pl".
9283 */
9284 ++pl; /* first delete */
9285 while (*pl == *ps)
9286 {
9287 ++pl;
9288 ++ps;
9289 }
9290 /* strings must be equal after second delete */
9291 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009292 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009293
9294 /* Failed to compare. */
9295 break;
9296
9297 case -1:
9298 case 1:
9299 /*
9300 * Minimal one delete from "pl" required.
9301 */
9302
9303 /* 1: delete */
9304 pl2 = pl + 1;
9305 ps2 = ps;
9306 while (*pl2 == *ps2)
9307 {
9308 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009309 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009310 ++pl2;
9311 ++ps2;
9312 }
9313
9314 /* 2: delete then swap, then rest must be equal */
9315 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9316 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009317 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009318
9319 /* 3: delete then substitute, then the rest must be equal */
9320 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009321 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009322
9323 /* 4: first swap then delete */
9324 if (pl[0] == ps[1] && pl[1] == ps[0])
9325 {
9326 pl2 = pl + 2; /* swap, skip two chars */
9327 ps2 = ps + 2;
9328 while (*pl2 == *ps2)
9329 {
9330 ++pl2;
9331 ++ps2;
9332 }
9333 /* delete a char and then strings must be equal */
9334 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009335 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009336 }
9337
9338 /* 5: first substitute then delete */
9339 pl2 = pl + 1; /* substitute, skip one char */
9340 ps2 = ps + 1;
9341 while (*pl2 == *ps2)
9342 {
9343 ++pl2;
9344 ++ps2;
9345 }
9346 /* delete a char and then strings must be equal */
9347 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009348 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009349
9350 /* Failed to compare. */
9351 break;
9352
9353 case 0:
9354 /*
9355 * Lenghts are equal, thus changes must result in same length: An
9356 * insert is only possible in combination with a delete.
9357 * 1: check if for identical strings
9358 */
9359 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009360 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009361
9362 /* 2: swap */
9363 if (pl[0] == ps[1] && pl[1] == ps[0])
9364 {
9365 pl2 = pl + 2; /* swap, skip two chars */
9366 ps2 = ps + 2;
9367 while (*pl2 == *ps2)
9368 {
9369 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009370 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009371 ++pl2;
9372 ++ps2;
9373 }
9374 /* 3: swap and swap again */
9375 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9376 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009377 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009378
9379 /* 4: swap and substitute */
9380 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009381 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009382 }
9383
9384 /* 5: substitute */
9385 pl2 = pl + 1;
9386 ps2 = ps + 1;
9387 while (*pl2 == *ps2)
9388 {
9389 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009390 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009391 ++pl2;
9392 ++ps2;
9393 }
9394
9395 /* 6: substitute and swap */
9396 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9397 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009398 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009399
9400 /* 7: substitute and substitute */
9401 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009402 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009403
9404 /* 8: insert then delete */
9405 pl2 = pl;
9406 ps2 = ps + 1;
9407 while (*pl2 == *ps2)
9408 {
9409 ++pl2;
9410 ++ps2;
9411 }
9412 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009413 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009414
9415 /* 9: delete then insert */
9416 pl2 = pl + 1;
9417 ps2 = ps;
9418 while (*pl2 == *ps2)
9419 {
9420 ++pl2;
9421 ++ps2;
9422 }
9423 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009424 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009425
9426 /* Failed to compare. */
9427 break;
9428 }
9429
9430 return SCORE_MAXMAX;
9431}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009432
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009433/*
9434 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009435 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009436 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009437 * The algorithm comes from Aspell editdist.cpp, edit_distance().
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009438 * It has been converted from C++ to C and modified to support multi-byte
9439 * characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009440 */
9441 static int
9442spell_edit_score(badword, goodword)
9443 char_u *badword;
9444 char_u *goodword;
9445{
9446 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009447 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009448 int j, i;
9449 int t;
9450 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009451 int pbc, pgc;
9452#ifdef FEAT_MBYTE
9453 char_u *p;
9454 int wbadword[MAXWLEN];
9455 int wgoodword[MAXWLEN];
9456
9457 if (has_mbyte)
9458 {
9459 /* Get the characters from the multi-byte strings and put them in an
9460 * int array for easy access. */
9461 for (p = badword, badlen = 0; *p != NUL; )
9462 wbadword[badlen++] = mb_ptr2char_adv(&p);
9463 ++badlen;
9464 for (p = goodword, goodlen = 0; *p != NUL; )
9465 wgoodword[goodlen++] = mb_ptr2char_adv(&p);
9466 ++goodlen;
9467 }
9468 else
9469#endif
9470 {
9471 badlen = STRLEN(badword) + 1;
9472 goodlen = STRLEN(goodword) + 1;
9473 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009474
9475 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
9476#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009477 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
9478 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009479 if (cnt == NULL)
9480 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009481
9482 CNT(0, 0) = 0;
9483 for (j = 1; j <= goodlen; ++j)
9484 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
9485
9486 for (i = 1; i <= badlen; ++i)
9487 {
9488 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
9489 for (j = 1; j <= goodlen; ++j)
9490 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009491#ifdef FEAT_MBYTE
9492 if (has_mbyte)
9493 {
9494 bc = wbadword[i - 1];
9495 gc = wgoodword[j - 1];
9496 }
9497 else
9498#endif
9499 {
9500 bc = badword[i - 1];
9501 gc = goodword[j - 1];
9502 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009503 if (bc == gc)
9504 CNT(i, j) = CNT(i - 1, j - 1);
9505 else
9506 {
9507 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009508 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009509 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
9510 else
9511 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
9512
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009513 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009514 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009515#ifdef FEAT_MBYTE
9516 if (has_mbyte)
9517 {
9518 pbc = wbadword[i - 2];
9519 pgc = wgoodword[j - 2];
9520 }
9521 else
9522#endif
9523 {
9524 pbc = badword[i - 2];
9525 pgc = goodword[j - 2];
9526 }
9527 if (bc == pgc && pbc == gc)
9528 {
9529 t = SCORE_SWAP + CNT(i - 2, j - 2);
9530 if (t < CNT(i, j))
9531 CNT(i, j) = t;
9532 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009533 }
9534 t = SCORE_DEL + CNT(i - 1, j);
9535 if (t < CNT(i, j))
9536 CNT(i, j) = t;
9537 t = SCORE_INS + CNT(i, j - 1);
9538 if (t < CNT(i, j))
9539 CNT(i, j) = t;
9540 }
9541 }
9542 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009543
9544 i = CNT(badlen - 1, goodlen - 1);
9545 vim_free(cnt);
9546 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009547}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009548
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009549/*
9550 * ":spelldump"
9551 */
9552/*ARGSUSED*/
9553 void
9554ex_spelldump(eap)
9555 exarg_T *eap;
9556{
9557 buf_T *buf = curbuf;
9558 langp_T *lp;
9559 slang_T *slang;
9560 idx_T arridx[MAXWLEN];
9561 int curi[MAXWLEN];
9562 char_u word[MAXWLEN];
9563 int c;
9564 char_u *byts;
9565 idx_T *idxs;
9566 linenr_T lnum = 0;
9567 int round;
9568 int depth;
9569 int n;
9570 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009571 char_u *region_names = NULL; /* region names being used */
9572 int do_region = TRUE; /* dump region names and numbers */
9573 char_u *p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009574
9575 if (no_spell_checking())
9576 return;
9577
9578 /* Create a new empty buffer by splitting the window. */
9579 do_cmdline_cmd((char_u *)"new");
9580 if (!bufempty() || !buf_valid(buf))
9581 return;
9582
Bram Moolenaar7887d882005-07-01 22:33:52 +00009583 /* Find out if we can support regions: All languages must support the same
9584 * regions or none at all. */
9585 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
9586 {
9587 p = lp->lp_slang->sl_regions;
9588 if (p[0] != 0)
9589 {
9590 if (region_names == NULL) /* first language with regions */
9591 region_names = p;
9592 else if (STRCMP(region_names, p) != 0)
9593 {
9594 do_region = FALSE; /* region names are different */
9595 break;
9596 }
9597 }
9598 }
9599
9600 if (do_region && region_names != NULL)
9601 {
9602 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
9603 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
9604 }
9605 else
9606 do_region = FALSE;
9607
9608 /*
9609 * Loop over all files loaded for the entries in 'spelllang'.
9610 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009611 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
9612 {
9613 slang = lp->lp_slang;
9614
9615 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
9616 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
9617
9618 /* round 1: case-folded tree
9619 * round 2: keep-case tree */
9620 for (round = 1; round <= 2; ++round)
9621 {
9622 if (round == 1)
9623 {
9624 byts = slang->sl_fbyts;
9625 idxs = slang->sl_fidxs;
9626 }
9627 else
9628 {
9629 byts = slang->sl_kbyts;
9630 idxs = slang->sl_kidxs;
9631 }
9632 if (byts == NULL)
9633 continue; /* array is empty */
9634
9635 depth = 0;
9636 arridx[0] = 0;
9637 curi[0] = 1;
9638 while (depth >= 0 && !got_int)
9639 {
9640 if (curi[depth] > byts[arridx[depth]])
9641 {
9642 /* Done all bytes at this node, go up one level. */
9643 --depth;
9644 line_breakcheck();
9645 }
9646 else
9647 {
9648 /* Do one more byte at this node. */
9649 n = arridx[depth] + curi[depth];
9650 ++curi[depth];
9651 c = byts[n];
9652 if (c == 0)
9653 {
9654 /* End of word, deal with the word.
9655 * Don't use keep-case words in the fold-case tree,
9656 * they will appear in the keep-case tree.
9657 * Only use the word when the region matches. */
9658 flags = (int)idxs[n];
9659 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009660 && (do_region
9661 || (flags & WF_REGION) == 0
9662 || (((unsigned)flags >> 8)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009663 & lp->lp_region) != 0))
9664 {
9665 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009666 if (!do_region)
9667 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009668
9669 /* Dump the basic word if there is no prefix or
9670 * when it's the first one. */
9671 c = (unsigned)flags >> 16;
9672 if (c == 0 || curi[depth] == 2)
9673 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009674
9675 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009676 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009677 lnum = apply_prefixes(slang, word, round,
9678 flags, lnum);
9679 }
9680 }
9681 else
9682 {
9683 /* Normal char, go one level deeper. */
9684 word[depth++] = c;
9685 arridx[depth] = idxs[n];
9686 curi[depth] = 1;
9687 }
9688 }
9689 }
9690 }
9691 }
9692
9693 /* Delete the empty line that we started with. */
9694 if (curbuf->b_ml.ml_line_count > 1)
9695 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
9696
9697 redraw_later(NOT_VALID);
9698}
9699
9700/*
9701 * Dump one word: apply case modifications and append a line to the buffer.
9702 */
9703 static void
9704dump_word(word, round, flags, lnum)
9705 char_u *word;
9706 int round;
9707 int flags;
9708 linenr_T lnum;
9709{
9710 int keepcap = FALSE;
9711 char_u *p;
9712 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +00009713 char_u badword[MAXWLEN + 10];
9714 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009715
9716 if (round == 1 && (flags & WF_CAPMASK) != 0)
9717 {
9718 /* Need to fix case according to "flags". */
9719 make_case_word(word, cword, flags);
9720 p = cword;
9721 }
9722 else
9723 {
9724 p = word;
9725 if (round == 2 && (captype(word, NULL) & WF_KEEPCAP) == 0)
9726 keepcap = TRUE;
9727 }
9728
Bram Moolenaar7887d882005-07-01 22:33:52 +00009729 /* Add flags and regions after a slash. */
9730 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009731 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009732 STRCPY(badword, p);
9733 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009734 if (keepcap)
9735 STRCAT(badword, "=");
9736 if (flags & WF_BANNED)
9737 STRCAT(badword, "!");
9738 else if (flags & WF_RARE)
9739 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009740 if (flags & WF_REGION)
9741 for (i = 0; i < 7; ++i)
9742 if (flags & (0x100 << i))
9743 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009744 p = badword;
9745 }
9746
9747 ml_append(lnum, p, (colnr_T)0, FALSE);
9748}
9749
9750/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009751 * For ":spelldump": Find matching prefixes for "word". Prepend each to
9752 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009753 * Return the updated line number.
9754 */
9755 static linenr_T
9756apply_prefixes(slang, word, round, flags, startlnum)
9757 slang_T *slang;
9758 char_u *word; /* case-folded word */
9759 int round;
9760 int flags; /* flags with prefix ID */
9761 linenr_T startlnum;
9762{
9763 idx_T arridx[MAXWLEN];
9764 int curi[MAXWLEN];
9765 char_u prefix[MAXWLEN];
9766 int c;
9767 char_u *byts;
9768 idx_T *idxs;
9769 linenr_T lnum = startlnum;
9770 int depth;
9771 int n;
9772 int len;
9773 int prefid = (unsigned)flags >> 16;
9774 int i;
9775
9776 byts = slang->sl_pbyts;
9777 idxs = slang->sl_pidxs;
9778 if (byts != NULL) /* array not is empty */
9779 {
9780 /*
9781 * Loop over all prefixes, building them byte-by-byte in prefix[].
9782 * When at the end of a prefix check that it supports "prefid".
9783 */
9784 depth = 0;
9785 arridx[0] = 0;
9786 curi[0] = 1;
9787 while (depth >= 0 && !got_int)
9788 {
9789 len = arridx[depth];
9790 if (curi[depth] > byts[len])
9791 {
9792 /* Done all bytes at this node, go up one level. */
9793 --depth;
9794 line_breakcheck();
9795 }
9796 else
9797 {
9798 /* Do one more byte at this node. */
9799 n = len + curi[depth];
9800 ++curi[depth];
9801 c = byts[n];
9802 if (c == 0)
9803 {
9804 /* End of prefix, find out how many IDs there are. */
9805 for (i = 1; i < len; ++i)
9806 if (byts[n + i] != 0)
9807 break;
9808 curi[depth] += i - 1;
9809
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009810 i = valid_word_prefix(i, n, prefid, word, slang);
9811 if (i != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009812 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009813 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009814 dump_word(prefix, round,
9815 (i & WF_RAREPFX) ? (flags | WF_RARE)
9816 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009817 }
9818 }
9819 else
9820 {
9821 /* Normal char, go one level deeper. */
9822 prefix[depth++] = c;
9823 arridx[depth] = idxs[n];
9824 curi[depth] = 1;
9825 }
9826 }
9827 }
9828 }
9829
9830 return lnum;
9831}
9832
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009833#endif /* FEAT_SYN_HL */