blob: 8eaa46e3a5528f63e19b2d9d59fb4da170acb121 [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
380/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000381 * Information used when looking for suggestions.
382 */
383typedef struct suginfo_S
384{
385 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000386 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000387 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000388 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000389 char_u *su_badptr; /* start of bad word in line */
390 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000391 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000392 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
393 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
394 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000395} suginfo_T;
396
397/* One word suggestion. Used in "si_ga". */
398typedef struct suggest_S
399{
400 char_u *st_word; /* suggested word, allocated string */
401 int st_orglen; /* length of replaced text */
402 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000403 int st_altscore; /* used when st_score compares equal */
404 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000405 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000406} suggest_T;
407
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000408#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000409
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000410/* Number of suggestions kept when cleaning up. When rescore_suggestions() is
411 * called the score may change, thus we need to keep more than what is
412 * displayed. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000413#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 50 ? 50 : (su)->su_maxcount)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000414
415/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
416 * of suggestions that are not going to be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000417#define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000418
419/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000420#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000421#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000422#define SCORE_REGION 70 /* word is for different region */
423#define SCORE_RARE 180 /* rare word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000424#define SCORE_SWAP 90 /* swap two characters */
425#define SCORE_SWAP3 110 /* swap two characters in three */
426#define SCORE_REP 87 /* REP replacement */
427#define SCORE_SUBST 93 /* substitute a character */
428#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000429#define SCORE_DEL 94 /* delete a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000430#define SCORE_DELDUP 64 /* delete a duplicated character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000431#define SCORE_INS 96 /* insert a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000432#define SCORE_INSDUP 66 /* insert a duplicate character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000433#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000434
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000435#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000436#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
437 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000438
439#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000440#define SCORE_MAXMAX 999999 /* accept any score */
441
442/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000443 * Structure to store info for word matching.
444 */
445typedef struct matchinf_S
446{
447 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000448
449 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000450 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000451 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000452 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000453 char_u *mi_cend; /* char after what was used for
454 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000455
456 /* case-folded text */
457 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000458 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000459
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000460 /* for when checking word after a prefix */
461 int mi_prefarridx; /* index in sl_pidxs with list of
462 prefixID/condition */
463 int mi_prefcnt; /* number of entries at mi_prefarridx */
464 int mi_prefixlen; /* byte length of prefix */
465
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000466 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000467 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000468 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000469 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000470} matchinf_T;
471
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000472/*
473 * The tables used for recognizing word characters according to spelling.
474 * These are only used for the first 256 characters of 'encoding'.
475 */
476typedef struct spelltab_S
477{
478 char_u st_isw[256]; /* flags: is word char */
479 char_u st_isu[256]; /* flags: is uppercase char */
480 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000481 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000482} spelltab_T;
483
484static spelltab_T spelltab;
485static int did_set_spelltab;
486
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000487#define CF_WORD 0x01
488#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000489
490static void clear_spell_chartab __ARGS((spelltab_T *sp));
491static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000492static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
493static int spell_iswordp_nmw __ARGS((char_u *p));
494#ifdef FEAT_MBYTE
495static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
496#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000497static void write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000498
499/*
Bram Moolenaarea408852005-06-25 22:49:46 +0000500 * Return TRUE if "p" points to a word character. Like spell_iswordp() but
501 * without the special handling of a single quote.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000502 * Checking for a word character is done very often, avoid the function call
503 * overhead.
504 */
505#ifdef FEAT_MBYTE
506# define SPELL_ISWORDP(p) ((has_mbyte && MB_BYTE2LEN(*(p)) > 1) \
507 ? (mb_get_class(p) >= 2) : spelltab.st_isw[*(p)])
508#else
509# define SPELL_ISWORDP(p) (spelltab.st_isw[*(p)])
510#endif
511
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000512/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000513 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000514 */
515typedef enum
516{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000517 STATE_START = 0, /* At start of node check for NUL bytes (goodword
518 * ends); if badword ends there is a match, otherwise
519 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000520 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000521 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000522 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
523 STATE_PLAIN, /* Use each byte of the node. */
524 STATE_DEL, /* Delete a byte from the bad word. */
525 STATE_INS, /* Insert a byte in the bad word. */
526 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000527 STATE_UNSWAP, /* Undo swap two characters. */
528 STATE_SWAP3, /* Swap two characters over three. */
529 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000530 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000531 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000532 STATE_REP_INI, /* Prepare for using REP items. */
533 STATE_REP, /* Use matching REP items from the .aff file. */
534 STATE_REP_UNDO, /* Undo a REP item replacement. */
535 STATE_FINAL /* End of this node. */
536} state_T;
537
538/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000539 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000540 */
541typedef struct trystate_S
542{
Bram Moolenaarea424162005-06-16 21:51:00 +0000543 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000544 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000545 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000546 short ts_curi; /* index in list of child nodes */
547 char_u ts_fidx; /* index in fword[], case-folded bad word */
548 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
549 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000550 char_u ts_prefixdepth; /* stack depth for end of prefix or PREFIXTREE
551 * or NOPREFIX */
Bram Moolenaarea424162005-06-16 21:51:00 +0000552#ifdef FEAT_MBYTE
553 char_u ts_tcharlen; /* number of bytes in tword character */
554 char_u ts_tcharidx; /* current byte index in tword character */
555 char_u ts_isdiff; /* DIFF_ values */
556 char_u ts_fcharstart; /* index in fword where badword char started */
557#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000558 char_u ts_save_prewordlen; /* saved "prewordlen" */
Bram Moolenaarea424162005-06-16 21:51:00 +0000559 char_u ts_save_splitoff; /* su_splitoff saved here */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000560 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000561} trystate_T;
562
Bram Moolenaarea424162005-06-16 21:51:00 +0000563/* values for ts_isdiff */
564#define DIFF_NONE 0 /* no different byte (yet) */
565#define DIFF_YES 1 /* different byte found */
566#define DIFF_INSERT 2 /* inserting character */
567
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000568/* special values ts_prefixdepth */
569#define PREFIXTREE 0xfe /* walking through the prefix tree */
570#define NOPREFIX 0xff /* not using prefixes */
571
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000572/* mode values for find_word */
573#define FIND_FOLDWORD 0 /* find word case-folded */
574#define FIND_KEEPWORD 1 /* find keep-case word */
575#define FIND_PREFIX 2 /* find word after prefix */
576
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000577/* values for read_cnt_string() */
578#define ERR_NOMEM -1
579#define ERR_TRUNC -2
580
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000581static slang_T *slang_alloc __ARGS((char_u *lang));
582static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000583static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000584static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000585static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int prefid, char_u *word, slang_T *slang));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000586static void find_prefix __ARGS((matchinf_T *mip));
587static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000588static int spell_valid_case __ARGS((int origflags, int treeflags));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000589static int no_spell_checking __ARGS((void));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000590static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000591static char_u *spell_enc __ARGS((void));
592static 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 Moolenaara1ba8112005-06-28 23:23:32 +0000595#ifdef FEAT_MBYTE
596static int *mb_str2wide __ARGS((char_u *s));
597#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000598static 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 +0000599static void clear_midword __ARGS((buf_T *buf));
600static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000601static int find_region __ARGS((char_u *rp, char_u *region));
602static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000603static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000604static int set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000605static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
606static void write_spell_chartab __ARGS((FILE *fd));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000607static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaarea408852005-06-25 22:49:46 +0000608static void spell_find_suggest __ARGS((char_u *badptr, suginfo_T *su, int maxcount, int banbadword));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000609#ifdef FEAT_EVAL
610static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
611#endif
612static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
613static void spell_suggest_intern __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000614static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000615static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000616static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000617static void suggest_try_special __ARGS((suginfo_T *su));
618static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000619static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
620static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000621static void score_comp_sal __ARGS((suginfo_T *su));
622static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000623static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000624static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000625static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000626static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000627static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000628static 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 +0000629static void add_banned __ARGS((suginfo_T *su, char_u *word));
630static int was_banned __ARGS((suginfo_T *su, char_u *word));
631static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000632static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000633static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000634static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
635static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
636static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000637#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000638static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000639#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000640static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000641static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000642static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum));
643static 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 +0000644
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000645/*
646 * Use our own character-case definitions, because the current locale may
647 * differ from what the .spl file uses.
648 * These must not be called with negative number!
649 */
650#ifndef FEAT_MBYTE
651/* Non-multi-byte implementation. */
652# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
653# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
654# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
655#else
656/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
657 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
658 * the "w" library function for characters above 255 if available. */
659# ifdef HAVE_TOWLOWER
660# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
661 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
662# else
663# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
664 : (c) < 256 ? spelltab.st_fold[c] : (c))
665# endif
666
667# ifdef HAVE_TOWUPPER
668# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
669 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
670# else
671# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
672 : (c) < 256 ? spelltab.st_upper[c] : (c))
673# endif
674
675# ifdef HAVE_ISWUPPER
676# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
677 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
678# else
679# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
680 : (c) < 256 ? spelltab.st_isu[c] : (c))
681# endif
682#endif
683
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000684
685static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000686
687/*
688 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000689 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000690 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
691 * or when it's OK it remains unchanged.
692 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000693 *
694 * "sug" is normally NULL. When looking for suggestions it points to
695 * suginfo_T. It's passed as a void pointer to keep the struct local.
696 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000697 * Returns the length of the word in bytes, also when it's OK, so that the
698 * caller can skip over the word.
699 */
700 int
Bram Moolenaar51485f02005-06-04 21:55:20 +0000701spell_check(wp, ptr, attrp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000702 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000703 char_u *ptr;
704 int *attrp;
705{
706 matchinf_T mi; /* Most things are put in "mi" so that it can
707 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000708 int nrlen = 0; /* found a number first */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000709
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000710 /* A word never starts at a space or a control character. Return quickly
711 * then, skipping over the character. */
712 if (*ptr <= ' ')
713 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000714
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000715 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar0c405862005-06-22 22:26:26 +0000716 * 0X99FF. But when a word character follows do check spelling to find
717 * "3GPP". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000718 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000719 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000720 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
721 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000722 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000723 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000724 mi.mi_end = skipdigits(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000725 nrlen = mi.mi_end - ptr;
726 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000727 if (!spell_iswordp(mi.mi_end, wp->w_buffer))
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000728 return (int)(mi.mi_end - ptr);
Bram Moolenaar0c405862005-06-22 22:26:26 +0000729
730 /* Try including the digits in the word. */
731 mi.mi_fend = ptr + nrlen;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000732 }
Bram Moolenaar0c405862005-06-22 22:26:26 +0000733 else
734 mi.mi_fend = ptr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000735
Bram Moolenaar0c405862005-06-22 22:26:26 +0000736 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000737 mi.mi_word = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000738 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000739 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000740 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000741 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000742 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000743 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000744 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000745
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000746 /* We always use the characters up to the next non-word character,
747 * also for bad words. */
748 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000749
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000750 /* Check caps type later. */
751 mi.mi_capflags = 0;
752 mi.mi_cend = NULL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000753 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000754
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000755 /* Include one non-word character so that we can check for the
756 * word end. */
757 if (*mi.mi_fend != NUL)
758 mb_ptr_adv(mi.mi_fend);
759
760 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
761 MAXWLEN + 1);
762 mi.mi_fwordlen = STRLEN(mi.mi_fword);
763
764 /* The word is bad unless we recognize it. */
765 mi.mi_result = SP_BAD;
766
767 /*
768 * Loop over the languages specified in 'spelllang'.
769 * We check them all, because a matching word may be longer than an
770 * already found matching word.
771 */
772 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
773 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
774 {
775 /* Check for a matching word in case-folded words. */
776 find_word(&mi, FIND_FOLDWORD);
777
778 /* Check for a matching word in keep-case words. */
779 find_word(&mi, FIND_KEEPWORD);
780
781 /* Check for matching prefixes. */
782 find_prefix(&mi);
783 }
784
785 if (mi.mi_result != SP_OK)
786 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000787 /* If we found a number skip over it. Allows for "42nd". Do flag
788 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000789 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000790 {
791 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
792 return nrlen;
793 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000794
795 /* When we are at a non-word character there is no error, just
796 * skip over the character (try looking for a word after it). */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000797 else if (!SPELL_ISWORDP(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000798 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000799#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000800 if (has_mbyte)
801 return mb_ptr2len_check(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000802#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000803 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000804 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000805
806 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
807 *attrp = highlight_attr[HLF_SPB];
808 else if (mi.mi_result == SP_RARE)
809 *attrp = highlight_attr[HLF_SPR];
810 else
811 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000812 }
813
Bram Moolenaar51485f02005-06-04 21:55:20 +0000814 return (int)(mi.mi_end - ptr);
815}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000816
Bram Moolenaar51485f02005-06-04 21:55:20 +0000817/*
818 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000819 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
820 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
821 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
822 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000823 *
824 * For a match mip->mi_result is updated.
825 */
826 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000827find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000828 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000829 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000830{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000831 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000832 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000833 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000834 int endidxcnt = 0;
835 int len;
836 int wlen = 0;
837 int flen;
838 int c;
839 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000840 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000841#ifdef FEAT_MBYTE
842 char_u *s;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000843 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000844#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000845 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000846 slang_T *slang = mip->mi_lp->lp_slang;
847 unsigned flags;
848 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000849 idx_T *idxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000850 int prefid;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000851
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000852 if (mode == FIND_KEEPWORD)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000853 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000854 /* Check for word with matching case in keep-case tree. */
855 ptr = mip->mi_word;
856 flen = 9999; /* no case folding, always enough bytes */
857 byts = slang->sl_kbyts;
858 idxs = slang->sl_kidxs;
859 }
860 else
861 {
862 /* Check for case-folded in case-folded tree. */
863 ptr = mip->mi_fword;
864 flen = mip->mi_fwordlen; /* available case-folded bytes */
865 byts = slang->sl_fbyts;
866 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000867
868 if (mode == FIND_PREFIX)
869 {
870 /* Skip over the prefix. */
871 wlen = mip->mi_prefixlen;
872 flen -= mip->mi_prefixlen;
873 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000874 }
875
Bram Moolenaar51485f02005-06-04 21:55:20 +0000876 if (byts == NULL)
877 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000878
Bram Moolenaar51485f02005-06-04 21:55:20 +0000879 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000880 * Repeat advancing in the tree until:
881 * - there is a byte that doesn't match,
882 * - we reach the end of the tree,
883 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000884 */
885 for (;;)
886 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000887 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000888 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000889
890 len = byts[arridx++];
891
892 /* If the first possible byte is a zero the word could end here.
893 * Remember this index, we first check for the longest word. */
894 if (byts[arridx] == 0)
895 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000896 if (endidxcnt == MAXWLEN)
897 {
898 /* Must be a corrupted spell file. */
899 EMSG(_(e_format));
900 return;
901 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000902 endlen[endidxcnt] = wlen;
903 endidx[endidxcnt++] = arridx++;
904 --len;
905
906 /* Skip over the zeros, there can be several flag/region
907 * combinations. */
908 while (len > 0 && byts[arridx] == 0)
909 {
910 ++arridx;
911 --len;
912 }
913 if (len == 0)
914 break; /* no children, word must end here */
915 }
916
917 /* Stop looking at end of the line. */
918 if (ptr[wlen] == NUL)
919 break;
920
921 /* Perform a binary search in the list of accepted bytes. */
922 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +0000923 if (c == TAB) /* <Tab> is handled like <Space> */
924 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +0000925 lo = arridx;
926 hi = arridx + len - 1;
927 while (lo < hi)
928 {
929 m = (lo + hi) / 2;
930 if (byts[m] > c)
931 hi = m - 1;
932 else if (byts[m] < c)
933 lo = m + 1;
934 else
935 {
936 lo = hi = m;
937 break;
938 }
939 }
940
941 /* Stop if there is no matching byte. */
942 if (hi < lo || byts[lo] != c)
943 break;
944
945 /* Continue at the child (if there is one). */
946 arridx = idxs[lo];
947 ++wlen;
948 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +0000949
950 /* One space in the good word may stand for several spaces in the
951 * checked word. */
952 if (c == ' ')
953 {
954 for (;;)
955 {
956 if (flen <= 0 && *mip->mi_fend != NUL)
957 flen = fold_more(mip);
958 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
959 break;
960 ++wlen;
961 --flen;
962 }
963 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000964 }
965
966 /*
967 * Verify that one of the possible endings is valid. Try the longest
968 * first.
969 */
970 while (endidxcnt > 0)
971 {
972 --endidxcnt;
973 arridx = endidx[endidxcnt];
974 wlen = endlen[endidxcnt];
975
976#ifdef FEAT_MBYTE
977 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
978 continue; /* not at first byte of character */
979#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000980 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000981 continue; /* next char is a word character */
982
983#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000984 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000985 {
986 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000987 * when folding case. This can be slow, take a shortcut when the
988 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000989 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000990 if (STRNCMP(ptr, p, wlen) != 0)
991 {
992 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
993 mb_ptr_adv(p);
994 wlen = p - mip->mi_word;
995 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000996 }
997#endif
998
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000999 /* Check flags and region. For FIND_PREFIX check the condition and
1000 * prefix ID.
1001 * Repeat this if there are more flags/region alternatives until there
1002 * is a match. */
1003 res = SP_BAD;
1004 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1005 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001006 {
1007 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001008
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001009 /* For the fold-case tree check that the case of the checked word
1010 * matches with what the word in the tree requires.
1011 * For keep-case tree the case is always right. For prefixes we
1012 * don't bother to check. */
1013 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001014 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001015 if (mip->mi_cend != mip->mi_word + wlen)
1016 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001017 /* mi_capflags was set for a different word length, need
1018 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001019 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001020 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001021 }
1022
Bram Moolenaar0c405862005-06-22 22:26:26 +00001023 if (mip->mi_capflags == WF_KEEPCAP
1024 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001025 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001026 }
1027
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001028 /* When mode is FIND_PREFIX the word must support the prefix:
1029 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001030 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001031 if (mode == FIND_PREFIX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001032 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001033 /* The prefix ID is stored two bytes above the flags. */
1034 prefid = (unsigned)flags >> 16;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001035 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001036 prefid, mip->mi_fword + mip->mi_prefixlen,
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001037 slang);
1038 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001039 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001040
1041 /* Use the WF_RARE flag for a rare prefix. */
1042 if (c & WF_RAREPFX)
1043 flags |= WF_RARE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001044 }
1045
1046 if (flags & WF_BANNED)
1047 res = SP_BANNED;
1048 else if (flags & WF_REGION)
1049 {
1050 /* Check region. */
1051 if ((mip->mi_lp->lp_region & (flags >> 8)) != 0)
1052 res = SP_OK;
1053 else
1054 res = SP_LOCAL;
1055 }
1056 else if (flags & WF_RARE)
1057 res = SP_RARE;
1058 else
1059 res = SP_OK;
1060
1061 /* Always use the longest match and the best result. */
1062 if (mip->mi_result > res)
1063 {
1064 mip->mi_result = res;
1065 mip->mi_end = mip->mi_word + wlen;
1066 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001067 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001068 mip->mi_end = mip->mi_word + wlen;
1069
1070 if (res == SP_OK)
1071 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001072 }
1073
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001074 if (res == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001075 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001076 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001077}
1078
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001079/*
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001080 * Return non-zero if the prefix indicated by "mip->mi_prefarridx" matches
1081 * with the prefix ID "prefid" for the word "word".
1082 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001083 */
1084 static int
1085valid_word_prefix(totprefcnt, arridx, prefid, word, slang)
1086 int totprefcnt; /* nr of prefix IDs */
1087 int arridx; /* idx in sl_pidxs[] */
1088 int prefid;
1089 char_u *word;
1090 slang_T *slang;
1091{
1092 int prefcnt;
1093 int pidx;
1094 regprog_T *rp;
1095 regmatch_T regmatch;
1096
1097 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1098 {
1099 pidx = slang->sl_pidxs[arridx + prefcnt];
1100
1101 /* Check the prefix ID. */
1102 if (prefid != (pidx & 0xff))
1103 continue;
1104
1105 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001106 * stored in the two bytes above the prefix ID byte. */
1107 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001108 if (rp != NULL)
1109 {
1110 regmatch.regprog = rp;
1111 regmatch.rm_ic = FALSE;
1112 if (!vim_regexec(&regmatch, word, 0))
1113 continue;
1114 }
1115
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001116 /* It's a match! Return the WF_RAREPFX flag. */
1117 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001118 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001119 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001120}
1121
1122/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001123 * Check if the word at "mip->mi_word" has a matching prefix.
1124 * If it does, then check the following word.
1125 *
1126 * For a match mip->mi_result is updated.
1127 */
1128 static void
1129find_prefix(mip)
1130 matchinf_T *mip;
1131{
1132 idx_T arridx = 0;
1133 int len;
1134 int wlen = 0;
1135 int flen;
1136 int c;
1137 char_u *ptr;
1138 idx_T lo, hi, m;
1139 slang_T *slang = mip->mi_lp->lp_slang;
1140 char_u *byts;
1141 idx_T *idxs;
1142
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001143 byts = slang->sl_pbyts;
1144 if (byts == NULL)
1145 return; /* array is empty */
1146
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001147 /* We use the case-folded word here, since prefixes are always
1148 * case-folded. */
1149 ptr = mip->mi_fword;
1150 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001151 idxs = slang->sl_pidxs;
1152
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001153 /*
1154 * Repeat advancing in the tree until:
1155 * - there is a byte that doesn't match,
1156 * - we reach the end of the tree,
1157 * - or we reach the end of the line.
1158 */
1159 for (;;)
1160 {
1161 if (flen == 0 && *mip->mi_fend != NUL)
1162 flen = fold_more(mip);
1163
1164 len = byts[arridx++];
1165
1166 /* If the first possible byte is a zero the prefix could end here.
1167 * Check if the following word matches and supports the prefix. */
1168 if (byts[arridx] == 0)
1169 {
1170 /* There can be several prefixes with different conditions. We
1171 * try them all, since we don't know which one will give the
1172 * longest match. The word is the same each time, pass the list
1173 * of possible prefixes to find_word(). */
1174 mip->mi_prefarridx = arridx;
1175 mip->mi_prefcnt = len;
1176 while (len > 0 && byts[arridx] == 0)
1177 {
1178 ++arridx;
1179 --len;
1180 }
1181 mip->mi_prefcnt -= len;
1182
1183 /* Find the word that comes after the prefix. */
1184 mip->mi_prefixlen = wlen;
1185 find_word(mip, FIND_PREFIX);
1186
1187
1188 if (len == 0)
1189 break; /* no children, word must end here */
1190 }
1191
1192 /* Stop looking at end of the line. */
1193 if (ptr[wlen] == NUL)
1194 break;
1195
1196 /* Perform a binary search in the list of accepted bytes. */
1197 c = ptr[wlen];
1198 lo = arridx;
1199 hi = arridx + len - 1;
1200 while (lo < hi)
1201 {
1202 m = (lo + hi) / 2;
1203 if (byts[m] > c)
1204 hi = m - 1;
1205 else if (byts[m] < c)
1206 lo = m + 1;
1207 else
1208 {
1209 lo = hi = m;
1210 break;
1211 }
1212 }
1213
1214 /* Stop if there is no matching byte. */
1215 if (hi < lo || byts[lo] != c)
1216 break;
1217
1218 /* Continue at the child (if there is one). */
1219 arridx = idxs[lo];
1220 ++wlen;
1221 --flen;
1222 }
1223}
1224
1225/*
1226 * Need to fold at least one more character. Do until next non-word character
1227 * for efficiency.
1228 * Return the length of the folded chars in bytes.
1229 */
1230 static int
1231fold_more(mip)
1232 matchinf_T *mip;
1233{
1234 int flen;
1235 char_u *p;
1236
1237 p = mip->mi_fend;
1238 do
1239 {
1240 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001241 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001242
1243 /* Include the non-word character so that we can check for the
1244 * word end. */
1245 if (*mip->mi_fend != NUL)
1246 mb_ptr_adv(mip->mi_fend);
1247
1248 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1249 mip->mi_fword + mip->mi_fwordlen,
1250 MAXWLEN - mip->mi_fwordlen);
1251 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1252 mip->mi_fwordlen += flen;
1253 return flen;
1254}
1255
1256/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001257 * Check case flags for a word. Return TRUE if the word has the requested
1258 * case.
1259 */
1260 static int
1261spell_valid_case(origflags, treeflags)
1262 int origflags; /* flags for the checked word. */
1263 int treeflags; /* flags for the word in the spell tree */
1264{
1265 return (origflags == WF_ALLCAP
1266 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
1267 && ((treeflags & WF_ONECAP) == 0 || origflags == WF_ONECAP)));
1268}
1269
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001270/*
1271 * Return TRUE if spell checking is not enabled.
1272 */
1273 static int
1274no_spell_checking()
1275{
1276 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
1277 {
1278 EMSG(_("E756: Spell checking is not enabled"));
1279 return TRUE;
1280 }
1281 return FALSE;
1282}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001283
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001284/*
1285 * Move to next spell error.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001286 * "curline" is TRUE for "z?": find word under/after cursor in the same line.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001287 * Return OK if found, FAIL otherwise.
1288 */
1289 int
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001290spell_move_to(dir, allwords, curline)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001291 int dir; /* FORWARD or BACKWARD */
1292 int allwords; /* TRUE for "[s" and "]s" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001293 int curline;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001294{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001295 linenr_T lnum;
1296 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001297 char_u *line;
1298 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001299 char_u *endp;
1300 int attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001301 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001302 int has_syntax = syntax_present(curbuf);
1303 int col;
1304 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001305 char_u *buf = NULL;
1306 int buflen = 0;
1307 int skip = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001308
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001309 if (no_spell_checking())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001310 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001311
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001312 /*
1313 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001314 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001315 *
1316 * When searching backwards, we continue in the line to find the last
1317 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001318 *
1319 * We concatenate the start of the next line, so that wrapped words work
1320 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1321 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001322 */
1323 lnum = curwin->w_cursor.lnum;
1324 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001325
1326 while (!got_int)
1327 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001328 line = ml_get(lnum);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001329
Bram Moolenaar0c405862005-06-22 22:26:26 +00001330 len = STRLEN(line);
1331 if (buflen < len + MAXWLEN + 2)
1332 {
1333 vim_free(buf);
1334 buflen = len + MAXWLEN + 2;
1335 buf = alloc(buflen);
1336 if (buf == NULL)
1337 break;
1338 }
1339
1340 /* Copy the line into "buf" and append the start of the next line if
1341 * possible. */
1342 STRCPY(buf, line);
1343 if (lnum < curbuf->b_ml.ml_line_count)
1344 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1345
1346 p = buf + skip;
1347 endp = buf + len;
1348 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001349 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001350 /* When searching backward don't search after the cursor. */
1351 if (dir == BACKWARD
1352 && lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001353 && (colnr_T)(p - buf) >= curwin->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001354 break;
1355
1356 /* start of word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00001357 attr = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001358 len = spell_check(curwin, p, &attr);
1359
1360 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001361 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001362 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001363 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001364 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001365 /* When searching forward only accept a bad word after
1366 * the cursor. */
1367 if (dir == BACKWARD
1368 || lnum > curwin->w_cursor.lnum
1369 || (lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001370 && (colnr_T)(curline ? p - buf + len
1371 : p - buf)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001372 > curwin->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001373 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001374 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001375 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001376 col = p - buf;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001377 (void)syn_get_id(lnum, (colnr_T)col,
1378 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001379 }
1380 else
1381 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001382
Bram Moolenaar51485f02005-06-04 21:55:20 +00001383 if (can_spell)
1384 {
1385 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001386 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001387#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001388 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001389#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001390 if (dir == FORWARD)
1391 {
1392 /* No need to search further. */
1393 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001394 vim_free(buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001395 return OK;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001396 }
1397 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001398 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001399 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001400 }
1401
Bram Moolenaar51485f02005-06-04 21:55:20 +00001402 /* advance to character after the word */
1403 p += len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001404 }
1405
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001406 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001407 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001408
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001409 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001410 if (dir == BACKWARD)
1411 {
1412 if (found_pos.lnum != 0)
1413 {
1414 /* Use the last match in the line. */
1415 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001416 vim_free(buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001417 return OK;
1418 }
1419 if (lnum == 1)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001420 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001421 --lnum;
1422 }
1423 else
1424 {
1425 if (lnum == curbuf->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001426 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001427 ++lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001428
1429 /* Skip the characters at the start of the next line that were
1430 * included in a match crossing line boundaries. */
1431 if (attr == 0)
1432 skip = p - endp;
1433 else
1434 skip = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001435 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001436
1437 line_breakcheck();
1438 }
1439
Bram Moolenaar0c405862005-06-22 22:26:26 +00001440 vim_free(buf);
1441 return FAIL;
1442}
1443
1444/*
1445 * For spell checking: concatenate the start of the following line "line" into
1446 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
1447 */
1448 void
1449spell_cat_line(buf, line, maxlen)
1450 char_u *buf;
1451 char_u *line;
1452 int maxlen;
1453{
1454 char_u *p;
1455 int n;
1456
1457 p = skipwhite(line);
1458 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1459 p = skipwhite(p + 1);
1460
1461 if (*p != NUL)
1462 {
1463 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001464 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001465 n = p - line;
1466 if (n >= maxlen)
1467 n = maxlen - 1;
1468 vim_memset(buf + 1, ' ', n);
1469 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001470}
1471
1472/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001473 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001474 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001475 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001476 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001477spell_load_lang(lang)
1478 char_u *lang;
1479{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001480 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001481 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001482 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001483
Bram Moolenaarb765d632005-06-07 21:00:02 +00001484 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001485 * It's truncated when an error is detected. */
1486 STRCPY(langcp, lang);
1487
Bram Moolenaarb765d632005-06-07 21:00:02 +00001488 /*
1489 * Find the first spell file for "lang" in 'runtimepath' and load it.
1490 */
1491 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
1492 "spell/%s.%s.spl", lang, spell_enc());
1493 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001494
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001495 if (r == FAIL && *langcp != NUL)
1496 {
1497 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001498 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001499 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001500 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001501 }
1502
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001503 if (r == FAIL)
1504 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
1505 fname_enc + 6);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001506 else if (*langcp != NUL)
1507 {
1508 /* Load all the additions. */
1509 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
1510 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
1511 }
1512}
1513
1514/*
1515 * Return the encoding used for spell checking: Use 'encoding', except that we
1516 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1517 */
1518 static char_u *
1519spell_enc()
1520{
1521
1522#ifdef FEAT_MBYTE
1523 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1524 return p_enc;
1525#endif
1526 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001527}
1528
1529/*
1530 * Allocate a new slang_T.
1531 * Caller must fill "sl_next".
1532 */
1533 static slang_T *
1534slang_alloc(lang)
1535 char_u *lang;
1536{
1537 slang_T *lp;
1538
Bram Moolenaar51485f02005-06-04 21:55:20 +00001539 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001540 if (lp != NULL)
1541 {
1542 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001543 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001544 ga_init2(&lp->sl_sal, sizeof(salitem_T), 10);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001545 }
1546 return lp;
1547}
1548
1549/*
1550 * Free the contents of an slang_T and the structure itself.
1551 */
1552 static void
1553slang_free(lp)
1554 slang_T *lp;
1555{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001556 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001557 vim_free(lp->sl_fname);
1558 slang_clear(lp);
1559 vim_free(lp);
1560}
1561
1562/*
1563 * Clear an slang_T so that the file can be reloaded.
1564 */
1565 static void
1566slang_clear(lp)
1567 slang_T *lp;
1568{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001569 garray_T *gap;
1570 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001571 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001572 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001573
Bram Moolenaar51485f02005-06-04 21:55:20 +00001574 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001575 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001576 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001577 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001578 vim_free(lp->sl_pbyts);
1579 lp->sl_pbyts = NULL;
1580
Bram Moolenaar51485f02005-06-04 21:55:20 +00001581 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001582 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001583 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001584 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001585 vim_free(lp->sl_pidxs);
1586 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001587
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001588 gap = &lp->sl_rep;
1589 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001590 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001591 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1592 vim_free(ftp->ft_from);
1593 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001594 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001595 ga_clear(gap);
1596
1597 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001598 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001599 {
1600 /* "ga_len" is set to 1 without adding an item for latin1 */
1601 if (gap->ga_data != NULL)
1602 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
1603 for (i = 0; i < gap->ga_len; ++i)
1604 vim_free(((int **)gap->ga_data)[i]);
1605 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001606 else
1607 /* SAL items: free salitem_T items */
1608 while (gap->ga_len > 0)
1609 {
1610 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1611 vim_free(smp->sm_lead);
1612 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
1613 vim_free(smp->sm_to);
1614#ifdef FEAT_MBYTE
1615 vim_free(smp->sm_lead_w);
1616 vim_free(smp->sm_oneof_w);
1617 vim_free(smp->sm_to_w);
1618#endif
1619 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001620 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001621
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001622 for (i = 0; i < lp->sl_prefixcnt; ++i)
1623 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001624 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001625 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001626 lp->sl_prefprog = NULL;
1627
1628 vim_free(lp->sl_midword);
1629 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001630
Bram Moolenaarea424162005-06-16 21:51:00 +00001631#ifdef FEAT_MBYTE
1632 {
1633 int todo = lp->sl_map_hash.ht_used;
1634 hashitem_T *hi;
1635
1636 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
1637 if (!HASHITEM_EMPTY(hi))
1638 {
1639 --todo;
1640 vim_free(hi->hi_key);
1641 }
1642 }
1643 hash_clear(&lp->sl_map_hash);
1644#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001645}
1646
1647/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001648 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001649 * Invoked through do_in_runtimepath().
1650 */
1651 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00001652spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001653 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001654 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001655{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001656 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001657}
1658
1659/*
1660 * Load one spell file and store the info into a slang_T.
1661 *
1662 * This is invoked in two ways:
1663 * - From spell_load_cb() to load a spell file for the first time. "lang" is
1664 * the language name, "old_lp" is NULL. Will allocate an slang_T.
1665 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
1666 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001667 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00001668 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001669 static slang_T *
1670spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001671 char_u *fname;
1672 char_u *lang;
1673 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001674 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001675{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001676 FILE *fd;
1677 char_u buf[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001678 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001679 char_u *bp;
1680 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001681 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001682 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001683 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001684 int round;
1685 char_u *save_sourcing_name = sourcing_name;
1686 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001687 int cnt, ccnt;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001688 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001689 slang_T *lp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001690 garray_T *gap;
1691 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001692 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001693 int rr;
1694 short *first;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001695 salfirst_T *sfirst;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001696 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001697 int c = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001698
Bram Moolenaarb765d632005-06-07 21:00:02 +00001699 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001700 if (fd == NULL)
1701 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001702 if (!silent)
1703 EMSG2(_(e_notopen), fname);
1704 else if (p_verbose > 2)
1705 {
1706 verbose_enter();
1707 smsg((char_u *)e_notopen, fname);
1708 verbose_leave();
1709 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001710 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001711 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00001712 if (p_verbose > 2)
1713 {
1714 verbose_enter();
1715 smsg((char_u *)_("Reading spell file \"%s\""), fname);
1716 verbose_leave();
1717 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001718
Bram Moolenaarb765d632005-06-07 21:00:02 +00001719 if (old_lp == NULL)
1720 {
1721 lp = slang_alloc(lang);
1722 if (lp == NULL)
1723 goto endFAIL;
1724
1725 /* Remember the file name, used to reload the file when it's updated. */
1726 lp->sl_fname = vim_strsave(fname);
1727 if (lp->sl_fname == NULL)
1728 goto endFAIL;
1729
1730 /* Check for .add.spl. */
1731 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
1732 }
1733 else
1734 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001735
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001736 /* Set sourcing_name, so that error messages mention the file name. */
1737 sourcing_name = fname;
1738 sourcing_lnum = 0;
1739
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001740 /* <HEADER>: <fileID>
1741 * <regioncnt> <regionname> ...
1742 * <charflagslen> <charflags>
1743 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001744 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001745 * <prefcondcnt> <prefcond> ...
1746 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001747 for (i = 0; i < VIMSPELLMAGICL; ++i)
1748 buf[i] = getc(fd); /* <fileID> */
1749 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
1750 {
1751 EMSG(_("E757: Wrong file ID in spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001752 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001753 }
1754
1755 cnt = getc(fd); /* <regioncnt> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001756 if (cnt < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001757 {
1758truncerr:
1759 EMSG(_("E758: Truncated spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001760 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001761 }
1762 if (cnt > 8)
1763 {
1764formerr:
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001765 EMSG(_(e_format));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001766 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001767 }
1768 for (i = 0; i < cnt; ++i)
1769 {
1770 lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
1771 lp->sl_regions[i * 2 + 1] = getc(fd);
1772 }
1773 lp->sl_regions[cnt * 2] = NUL;
1774
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001775 cnt = getc(fd); /* <charflagslen> */
1776 if (cnt > 0)
1777 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001778 p = alloc((unsigned)cnt);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001779 if (p == NULL)
1780 goto endFAIL;
1781 for (i = 0; i < cnt; ++i)
1782 p[i] = getc(fd); /* <charflags> */
1783
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001784 /* <fcharslen> <fchars> */
1785 fol = read_cnt_string(fd, 2, &ccnt);
1786 if (ccnt != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001787 {
1788 vim_free(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001789 if (ccnt == ERR_NOMEM)
1790 goto endFAIL;
1791 if (ccnt == ERR_TRUNC)
1792 goto formerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001793 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001794
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001795 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001796 i = set_spell_charflags(p, cnt, fol);
1797 vim_free(p);
1798 vim_free(fol);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001799#if 0 /* tolerate the differences */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001800 if (i == FAIL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001801 goto formerr;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001802#endif
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001803 }
1804 else
1805 {
1806 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
1807 cnt = (getc(fd) << 8) + getc(fd);
1808 if (cnt != 0)
1809 goto formerr;
1810 }
1811
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001812 /* <midwordlen> <midword> */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001813 lp->sl_midword = read_cnt_string(fd, 2, &cnt);
1814 if (cnt == ERR_TRUNC)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001815 goto truncerr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001816 if (cnt == ERR_NOMEM)
1817 goto endFAIL;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001818
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001819 /* <prefcondcnt> <prefcond> ... */
1820 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
1821 if (cnt > 0)
1822 {
1823 lp->sl_prefprog = (regprog_T **)alloc_clear(
1824 (unsigned)sizeof(regprog_T *) * cnt);
1825 if (lp->sl_prefprog == NULL)
1826 goto endFAIL;
1827 lp->sl_prefixcnt = cnt;
1828
1829 for (i = 0; i < cnt; ++i)
1830 {
1831 /* <prefcond> : <condlen> <condstr> */
1832 n = getc(fd); /* <condlen> */
1833 if (n < 0)
1834 goto formerr;
1835 /* When <condlen> is zero we have an empty condition. Otherwise
1836 * compile the regexp program used to check for the condition. */
1837 if (n > 0)
1838 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001839 buf[0] = '^'; /* always match at one position only */
1840 p = buf + 1;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001841 while (n-- > 0)
1842 *p++ = getc(fd); /* <condstr> */
1843 *p = NUL;
1844 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
1845 }
1846 }
1847 }
1848
1849
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001850 /* <SUGGEST> : <repcount> <rep> ...
1851 * <salflags> <salcount> <sal> ...
1852 * <maplen> <mapstr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001853
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001854 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
1855 if (cnt < 0)
1856 goto formerr;
1857
1858 gap = &lp->sl_rep;
1859 if (ga_grow(gap, cnt) == FAIL)
1860 goto endFAIL;
1861
1862 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
1863 for (; gap->ga_len < cnt; ++gap->ga_len)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001864 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001865 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
1866 for (rr = 1; rr <= 2; ++rr)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001867 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001868 ccnt = getc(fd);
1869 if (ccnt < 0)
1870 {
1871 if (rr == 2)
1872 vim_free(ftp->ft_from);
1873 goto formerr;
1874 }
1875 if ((p = alloc(ccnt + 1)) == NULL)
1876 {
1877 if (rr == 2)
1878 vim_free(ftp->ft_from);
1879 goto endFAIL;
1880 }
1881 for (i = 0; i < ccnt; ++i)
1882 p[i] = getc(fd); /* <repfrom> or <repto> */
1883 p[i] = NUL;
1884 if (rr == 1)
1885 ftp->ft_from = p;
1886 else
1887 ftp->ft_to = p;
1888 }
1889 }
1890
1891 /* Fill the first-index table. */
1892 first = lp->sl_rep_first;
1893 for (i = 0; i < 256; ++i)
1894 first[i] = -1;
1895 for (i = 0; i < gap->ga_len; ++i)
1896 {
1897 ftp = &((fromto_T *)gap->ga_data)[i];
1898 if (first[*ftp->ft_from] == -1)
1899 first[*ftp->ft_from] = i;
1900 }
1901
1902 i = getc(fd); /* <salflags> */
1903 if (i & SAL_F0LLOWUP)
1904 lp->sl_followup = TRUE;
1905 if (i & SAL_COLLAPSE)
1906 lp->sl_collapse = TRUE;
1907 if (i & SAL_REM_ACCENTS)
1908 lp->sl_rem_accents = TRUE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001909 if (i & SAL_SOFO)
1910 lp->sl_sofo = TRUE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001911
1912 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
1913 if (cnt < 0)
1914 goto formerr;
1915
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001916 if (lp->sl_sofo)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001917 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001918 /*
1919 * SOFOFROM and SOFOTO items come in one <salfrom> and <salto>
1920 */
1921 if (cnt != 1)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001922 goto formerr;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001923
1924 cnt = (getc(fd) << 8) + getc(fd); /* <salfromlen> */
1925 if (cnt < 0)
1926 goto formerr;
1927 if ((bp = alloc(cnt + 1)) == NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001928 goto endFAIL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001929 for (i = 0; i < cnt; ++i)
1930 bp[i] = getc(fd); /* <salfrom> */
1931 bp[i] = NUL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001932
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001933 ccnt = (getc(fd) << 8) + getc(fd); /* <saltolen> */
1934 if (ccnt < 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001935 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001936 vim_free(bp);
1937 goto formerr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001938 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001939 if ((fol = alloc(ccnt + 1)) == NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001940 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001941 vim_free(bp);
1942 goto endFAIL;
1943 }
1944 for (i = 0; i < ccnt; ++i)
1945 fol[i] = getc(fd); /* <salto> */
1946 fol[i] = NUL;
1947
1948#ifdef FEAT_MBYTE
1949 if (has_mbyte)
1950 {
1951 char_u *s;
1952
1953 /* Use "sl_sal" as an array with 256 pointers to a list of wide
1954 * characters. The index is the low byte of the character.
1955 * The list contains from-to pairs with a terminating NUL.
1956 * sl_sal_first[] is used for latin1 "from" characters. */
1957 gap = &lp->sl_sal;
1958 ga_init2(gap, sizeof(int *), 1);
1959 if (ga_grow(gap, 256) == FAIL)
1960 {
1961sofoFAIL:
1962 vim_free(bp);
1963 vim_free(fol);
1964 goto endFAIL;
1965 }
1966 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
1967 gap->ga_len = 256;
1968
1969 /* First count the number of items for each list. Temporarily use
1970 * sl_sal_first[] for this. */
1971 for (p = bp, s = fol; *p != NUL && *s != NUL; )
1972 {
1973 c = mb_ptr2char_adv(&p);
1974 mb_ptr_adv(s);
1975 if (c >= 256)
1976 ++lp->sl_sal_first[c & 0xff];
1977 }
1978 if (*p != NUL || *s != NUL) /* lengths differ */
1979 goto sofoerr;
1980
1981 /* Allocate the lists. */
1982 for (i = 0; i < 256; ++i)
1983 if (lp->sl_sal_first[i] > 0)
1984 {
1985 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
1986 if (p == NULL)
1987 goto sofoFAIL;
1988 ((int **)gap->ga_data)[i] = (int *)p;
1989 *(int *)p = 0;
1990 }
1991
1992 /* Put the characters in sl_sal_first[] or a sl_sal list. */
1993 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
1994 for (p = bp, s = fol; *p != NUL && *s != NUL; )
1995 {
1996 c = mb_ptr2char_adv(&p);
1997 i = mb_ptr2char_adv(&s);
1998 if (c >= 256)
1999 {
2000 int *inp;
2001
2002 /* Append the from-to chars at the end of the list with
2003 * the low byte. */
2004 inp = ((int **)gap->ga_data)[c & 0xff];
2005 while (*inp != 0)
2006 ++inp;
2007 *inp++ = c; /* from char */
2008 *inp++ = i; /* to char */
2009 *inp++ = NUL; /* NUL at the end */
2010 }
2011 else
2012 /* mapping byte to char is done in sl_sal_first[] */
2013 lp->sl_sal_first[c] = i;
2014 }
2015 }
2016 else
2017#endif
2018 {
2019 /* mapping bytes to bytes is done in sl_sal_first[] */
2020 if (cnt != ccnt)
2021 {
2022#ifdef FEAT_MBYTE
2023sofoerr:
2024#endif
2025 vim_free(bp);
2026 vim_free(fol);
2027 goto formerr;
2028 }
2029 for (i = 0; i < cnt; ++i)
2030 lp->sl_sal_first[bp[i]] = fol[i];
2031 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
2032 }
2033 vim_free(bp);
2034 vim_free(fol);
2035 }
2036 else
2037 {
2038 /*
2039 * SAL items
2040 */
2041 gap = &lp->sl_sal;
2042 if (ga_grow(gap, cnt) == FAIL)
2043 goto endFAIL;
2044
2045 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
2046 for (; gap->ga_len < cnt; ++gap->ga_len)
2047 {
2048 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
2049 ccnt = getc(fd); /* <salfromlen> */
2050 if (ccnt < 0)
2051 goto formerr;
2052 if ((p = alloc(ccnt + 2)) == NULL)
2053 goto endFAIL;
2054 smp->sm_lead = p;
2055
2056 /* Read up to the first special char into sm_lead. */
2057 for (i = 0; i < ccnt; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002058 {
2059 c = getc(fd); /* <salfrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002060 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002061 break;
2062 *p++ = c;
2063 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002064 smp->sm_leadlen = p - smp->sm_lead;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002065 *p++ = NUL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002066
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002067 /* Put (abc) chars in sm_oneof, if any. */
2068 if (c == '(')
2069 {
2070 smp->sm_oneof = p;
2071 for (++i; i < ccnt; ++i)
2072 {
2073 c = getc(fd); /* <salfrom> */
2074 if (c == ')')
2075 break;
2076 *p++ = c;
2077 }
2078 *p++ = NUL;
2079 if (++i < ccnt)
2080 c = getc(fd);
2081 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002082 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002083 smp->sm_oneof = NULL;
2084
2085 /* Any following chars go in sm_rules. */
2086 smp->sm_rules = p;
2087 if (i < ccnt)
2088 /* store the char we got while checking for end of sm_lead */
2089 *p++ = c;
2090 for (++i; i < ccnt; ++i)
2091 *p++ = getc(fd); /* <salfrom> */
2092 *p++ = NUL;
2093
2094 ccnt = getc(fd); /* <saltolen> */
2095 if (ccnt < 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002096 {
2097 vim_free(smp->sm_lead);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002098 goto formerr;
2099 }
2100 if ((p = alloc(ccnt + 1)) == NULL)
2101 {
2102 vim_free(smp->sm_lead);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002103 goto endFAIL;
2104 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002105 smp->sm_to = p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002106
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002107 for (i = 0; i < ccnt; ++i)
2108 *p++ = getc(fd); /* <salto> */
2109 *p++ = NUL;
2110
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002111#ifdef FEAT_MBYTE
2112 if (has_mbyte)
2113 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002114 /* convert the multi-byte strings to wide char strings */
2115 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2116 smp->sm_leadlen = mb_charlen(smp->sm_lead);
2117 if (smp->sm_oneof == NULL)
2118 smp->sm_oneof_w = NULL;
2119 else
2120 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
2121 smp->sm_to_w = mb_str2wide(smp->sm_to);
2122 if (smp->sm_lead_w == NULL
2123 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
2124 || smp->sm_to_w == NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002125 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002126 vim_free(smp->sm_lead);
2127 vim_free(smp->sm_to);
2128 vim_free(smp->sm_lead_w);
2129 vim_free(smp->sm_oneof_w);
2130 vim_free(smp->sm_to_w);
2131 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002132 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002133 }
2134#endif
2135 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002136
2137 /* Fill the first-index table. */
2138 sfirst = lp->sl_sal_first;
2139 for (i = 0; i < 256; ++i)
2140 sfirst[i] = -1;
2141 smp = (salitem_T *)gap->ga_data;
2142 for (i = 0; i < gap->ga_len; ++i)
2143 {
2144#ifdef FEAT_MBYTE
2145 if (has_mbyte)
2146 /* Use the lowest byte of the first character. For latin1 it's
2147 * the character, for other encodings it should differ for most
2148 * characters. */
2149 c = *smp[i].sm_lead_w & 0xff;
2150 else
2151#endif
2152 c = *smp[i].sm_lead;
2153 if (sfirst[c] == -1)
2154 {
2155 sfirst[c] = i;
2156#ifdef FEAT_MBYTE
2157 if (has_mbyte)
2158 {
2159 /* Make sure all entries with this byte are following each
2160 * other. Move the ones that are in the wrong position. Do
2161 * keep the same ordering! */
2162 while (i + 1 < gap->ga_len
2163 && (*smp[i + 1].sm_lead_w & 0xff) == c)
2164 /* Skip over entry with same index byte. */
2165 ++i;
2166
2167 for (n = 1; i + n < gap->ga_len; ++n)
2168 if ((*smp[i + n].sm_lead_w & 0xff) == c)
2169 {
2170 salitem_T tsal;
2171
2172 /* Move entry with same index byte after the entries
2173 * we already found. */
2174 ++i;
2175 --n;
2176 tsal = smp[i + n];
2177 mch_memmove(smp + i + 1, smp + i,
2178 sizeof(salitem_T) * n);
2179 smp[i] = tsal;
2180 }
2181 }
2182#endif
2183 }
2184 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002185 }
2186
2187 cnt = (getc(fd) << 8) + getc(fd); /* <maplen> */
2188 if (cnt < 0)
2189 goto formerr;
2190 p = alloc(cnt + 1);
2191 if (p == NULL)
2192 goto endFAIL;
2193 for (i = 0; i < cnt; ++i)
2194 p[i] = getc(fd); /* <mapstr> */
2195 p[i] = NUL;
Bram Moolenaarea424162005-06-16 21:51:00 +00002196 set_map_str(lp, p);
2197 vim_free(p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002198
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002199
Bram Moolenaar51485f02005-06-04 21:55:20 +00002200 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002201 * round 2: <KWORDTREE>
2202 * round 3: <PREFIXTREE> */
2203 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002204 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002205 /* The tree size was computed when writing the file, so that we can
2206 * allocate it as one long block. <nodecount> */
2207 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2208 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002209 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002210 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002211 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002212 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002213 bp = lalloc((long_u)len, TRUE);
2214 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002215 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002216 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002217 lp->sl_fbyts = bp;
2218 else if (round == 2)
2219 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002220 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002221 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002222
2223 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002224 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
2225 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002226 goto endFAIL;
2227 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002228 lp->sl_fidxs = ip;
2229 else if (round == 2)
2230 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002231 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002232 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002233
2234 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002235 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002236 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002237 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002238 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002239 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002240 }
2241 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002242
Bram Moolenaarb765d632005-06-07 21:00:02 +00002243 /* For a new file link it in the list of spell files. */
2244 if (old_lp == NULL)
2245 {
2246 lp->sl_next = first_lang;
2247 first_lang = lp;
2248 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002249
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002250 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002251
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002252endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002253 if (lang != NULL)
2254 /* truncating the name signals the error to spell_load_lang() */
2255 *lang = NUL;
2256 if (lp != NULL && old_lp == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002257 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002258 slang_free(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002259 lp = NULL;
2260 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002261
2262endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002263 if (fd != NULL)
2264 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002265 sourcing_name = save_sourcing_name;
2266 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002267
2268 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002269}
2270
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002271/*
2272 * Read a length field from "fd" in "cnt_bytes" bytes.
2273 * Allocate memory and read the string into it.
2274 * Returns NULL when the count is zero.
2275 * Sets "errp" to ERR_TRUNC when reading failed, ERR_NOMEM when out of
2276 * memory, zero when OK.
2277 */
2278 static char_u *
2279read_cnt_string(fd, cnt_bytes, errp)
2280 FILE *fd;
2281 int cnt_bytes;
2282 int *errp;
2283{
2284 int cnt = 0;
2285 int i;
2286 char_u *str;
2287
2288 /* read the length bytes, MSB first */
2289 for (i = 0; i < cnt_bytes; ++i)
2290 cnt = (cnt << 8) + getc(fd);
2291 if (cnt < 0)
2292 {
2293 *errp = ERR_TRUNC;
2294 return NULL;
2295 }
2296
2297 /* allocate memory */
2298 str = alloc((unsigned)cnt + 1);
2299 if (str == NULL)
2300 {
2301 *errp = ERR_NOMEM;
2302 return NULL;
2303 }
2304 *errp = 0;
2305
2306 /* Read the string. Doesn't check for truncated file. */
2307 for (i = 0; i < cnt; ++i)
2308 str[i] = getc(fd);
2309 str[i] = NUL;
2310
2311 return str;
2312}
2313
2314
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002315#ifdef FEAT_MBYTE
2316/*
2317 * Turn a multi-byte string into a wide character string.
2318 * Return it in allocated memory (NULL for out-of-memory)
2319 */
2320 static int *
2321mb_str2wide(s)
2322 char_u *s;
2323{
2324 int *res;
2325 char_u *p;
2326 int i = 0;
2327
2328 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
2329 if (res != NULL)
2330 {
2331 for (p = s; *p != NUL; )
2332 res[i++] = mb_ptr2char_adv(&p);
2333 res[i] = NUL;
2334 }
2335 return res;
2336}
2337#endif
2338
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002339/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002340 * Read one row of siblings from the spell file and store it in the byte array
2341 * "byts" and index array "idxs". Recursively read the children.
2342 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00002343 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00002344 *
2345 * Returns the index follosing the siblings.
2346 * Returns -1 if the file is shorter than expected.
2347 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002348 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002349 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002350read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002351 FILE *fd;
2352 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002353 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002354 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002355 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002356 int prefixtree; /* TRUE for reading PREFIXTREE */
2357 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002358{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002359 int len;
2360 int i;
2361 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002362 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002363 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002364 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002365#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002366
Bram Moolenaar51485f02005-06-04 21:55:20 +00002367 len = getc(fd); /* <siblingcount> */
2368 if (len <= 0)
2369 return -1;
2370
2371 if (startidx + len >= maxidx)
2372 return -2;
2373 byts[idx++] = len;
2374
2375 /* Read the byte values, flag/region bytes and shared indexes. */
2376 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002377 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002378 c = getc(fd); /* <byte> */
2379 if (c < 0)
2380 return -1;
2381 if (c <= BY_SPECIAL)
2382 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002383 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002384 {
2385 /* No flags, all regions. */
2386 idxs[idx] = 0;
2387 c = 0;
2388 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002389 else if (c == BY_FLAGS || c == BY_NOFLAGS)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002390 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002391 if (prefixtree)
2392 {
2393 /* Read the prefix ID and the condition nr. In idxs[]
2394 * store the prefix ID in the low byte, the condition
2395 * index shifted up 8 bits. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002396 c2 = getc(fd); /* <prefixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002397 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
2398 if (n >= maxprefcondnr)
2399 return -2;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002400 c2 += (n << 8);
2401 if (c == BY_NOFLAGS)
2402 c = c2;
2403 else
2404 c = c2 | WF_RAREPFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002405 }
2406 else
2407 {
2408 /* Read flags and optional region and prefix ID. In
2409 * idxs[] the flags go in the low byte, region above that
2410 * and prefix ID above the region. */
2411 c = getc(fd); /* <flags> */
2412 if (c & WF_REGION)
2413 c = (getc(fd) << 8) + c; /* <region> */
2414 if (c & WF_PFX)
2415 c = (getc(fd) << 16) + c; /* <prefixID> */
2416 }
2417
Bram Moolenaar51485f02005-06-04 21:55:20 +00002418 idxs[idx] = c;
2419 c = 0;
2420 }
2421 else /* c == BY_INDEX */
2422 {
2423 /* <nodeidx> */
2424 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2425 if (n < 0 || n >= maxidx)
2426 return -2;
2427 idxs[idx] = n + SHARED_MASK;
2428 c = getc(fd); /* <xbyte> */
2429 }
2430 }
2431 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002432 }
2433
Bram Moolenaar51485f02005-06-04 21:55:20 +00002434 /* Recursively read the children for non-shared siblings.
2435 * Skip the end-of-word ones (zero byte value) and the shared ones (and
2436 * remove SHARED_MASK) */
2437 for (i = 1; i <= len; ++i)
2438 if (byts[startidx + i] != 0)
2439 {
2440 if (idxs[startidx + i] & SHARED_MASK)
2441 idxs[startidx + i] &= ~SHARED_MASK;
2442 else
2443 {
2444 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002445 idx = read_tree(fd, byts, idxs, maxidx, idx,
2446 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002447 if (idx < 0)
2448 break;
2449 }
2450 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002451
Bram Moolenaar51485f02005-06-04 21:55:20 +00002452 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002453}
2454
2455/*
2456 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002457 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002458 */
2459 char_u *
2460did_set_spelllang(buf)
2461 buf_T *buf;
2462{
2463 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002464 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002465 char_u *region;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002466 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002467 int region_mask;
2468 slang_T *lp;
2469 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002470 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002471 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002472 int load_spf;
2473 int len;
2474 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002475
2476 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002477 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002478
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002479 /* Make the name of the .spl file associated with 'spellfile'. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002480 if (*buf->b_p_spf == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002481 load_spf = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002482 else
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002483 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002484 vim_snprintf((char *)spf_name, sizeof(spf_name), "%s.spl",
2485 buf->b_p_spf);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002486 load_spf = TRUE;
2487 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002488
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002489 /* loop over comma separated language names. */
2490 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002491 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002492 /* Get one language name. */
2493 copy_option_part(&splp, lang, MAXWLEN, ",");
2494
Bram Moolenaar5482f332005-04-17 20:18:43 +00002495 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002496 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002497
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002498 /* If the name ends in ".spl" use it as the name of the spell file.
2499 * If there is a region name let "region" point to it and remove it
2500 * from the name. */
2501 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2502 {
2503 filename = TRUE;
2504
2505 /* Check if we loaded this language before. */
2506 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2507 if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME)
2508 break;
2509 }
2510 else
2511 {
2512 filename = FALSE;
2513 if (len > 3 && lang[len - 3] == '_')
2514 {
2515 region = lang + len - 2;
2516 len -= 3;
2517 lang[len] = NUL;
2518 }
2519
2520 /* Check if we loaded this language before. */
2521 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2522 if (STRICMP(lang, lp->sl_name) == 0)
2523 break;
2524 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002525
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002526 /* If not found try loading the language now. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002527 if (lp == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002528 {
2529 if (filename)
2530 (void)spell_load_file(lang, lang, NULL, FALSE);
2531 else
2532 spell_load_lang(lang);
2533 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002534
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002535 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002536 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002537 */
2538 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002539 if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME
2540 : STRICMP(lang, lp->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002541 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002542 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002543 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002544 {
2545 /* find region in sl_regions */
2546 c = find_region(lp->sl_regions, region);
2547 if (c == REGION_ALL)
2548 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002549 if (!lp->sl_add)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002550 smsg((char_u *)
2551 _("Warning: region %s not supported"),
2552 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002553 }
2554 else
2555 region_mask = 1 << c;
2556 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002557
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002558 if (ga_grow(&ga, 1) == FAIL)
2559 {
2560 ga_clear(&ga);
2561 return e_outofmem;
2562 }
2563 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2564 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2565 ++ga.ga_len;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002566 use_midword(lp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002567
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002568 /* Check if this is the spell file related to 'spellfile'. */
2569 if (load_spf && fullpathcmp(spf_name, lp->sl_fname, FALSE)
2570 == FPC_SAME)
2571 load_spf = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002572 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002573 }
2574
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002575 /*
2576 * Make sure the 'spellfile' file is loaded. It may be in 'runtimepath',
2577 * then it's probably loaded above already. Otherwise load it here.
2578 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002579 if (load_spf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002580 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002581 /* Check if it was loaded already. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002582 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2583 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
2584 break;
2585 if (lp == NULL)
2586 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002587 /* Not loaded, try loading it now. The language name includes the
2588 * region name, the region is ignored otherwise. */
2589 vim_strncpy(lang, gettail(buf->b_p_spf), MAXWLEN);
2590 p = vim_strchr(lang, '.');
2591 if (p != NULL)
2592 *p = NUL; /* truncate at ".encoding.add" */
2593 lp = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002594 }
2595 if (lp != NULL && ga_grow(&ga, 1) == OK)
2596 {
2597 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2598 LANGP_ENTRY(ga, ga.ga_len)->lp_region = REGION_ALL;
2599 ++ga.ga_len;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002600 use_midword(lp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002601 }
2602 }
2603
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002604 /* Add a NULL entry to mark the end of the list. */
2605 if (ga_grow(&ga, 1) == FAIL)
2606 {
2607 ga_clear(&ga);
2608 return e_outofmem;
2609 }
2610 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
2611 ++ga.ga_len;
2612
2613 /* Everything is fine, store the new b_langp value. */
2614 ga_clear(&buf->b_langp);
2615 buf->b_langp = ga;
2616
2617 return NULL;
2618}
2619
2620/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002621 * Clear the midword characters for buffer "buf".
2622 */
2623 static void
2624clear_midword(buf)
2625 buf_T *buf;
2626{
2627 vim_memset(buf->b_spell_ismw, 0, 256);
2628#ifdef FEAT_MBYTE
2629 vim_free(buf->b_spell_ismw_mb);
2630 buf->b_spell_ismw_mb = NULL;
2631#endif
2632}
2633
2634/*
2635 * Use the "sl_midword" field of language "lp" for buffer "buf".
2636 * They add up to any currently used midword characters.
2637 */
2638 static void
2639use_midword(lp, buf)
2640 slang_T *lp;
2641 buf_T *buf;
2642{
2643 char_u *p;
2644
2645 for (p = lp->sl_midword; *p != NUL; )
2646#ifdef FEAT_MBYTE
2647 if (has_mbyte)
2648 {
2649 int c, l, n;
2650 char_u *bp;
2651
2652 c = mb_ptr2char(p);
2653 l = mb_ptr2len_check(p);
2654 if (c < 256)
2655 buf->b_spell_ismw[c] = TRUE;
2656 else if (buf->b_spell_ismw_mb == NULL)
2657 /* First multi-byte char in "b_spell_ismw_mb". */
2658 buf->b_spell_ismw_mb = vim_strnsave(p, l);
2659 else
2660 {
2661 /* Append multi-byte chars to "b_spell_ismw_mb". */
2662 n = STRLEN(buf->b_spell_ismw_mb);
2663 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
2664 if (bp != NULL)
2665 {
2666 vim_free(buf->b_spell_ismw_mb);
2667 buf->b_spell_ismw_mb = bp;
2668 vim_strncpy(bp + n, p, l);
2669 }
2670 }
2671 p += l;
2672 }
2673 else
2674#endif
2675 buf->b_spell_ismw[*p++] = TRUE;
2676}
2677
2678/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002679 * Find the region "region[2]" in "rp" (points to "sl_regions").
2680 * Each region is simply stored as the two characters of it's name.
2681 * Returns the index if found, REGION_ALL if not found.
2682 */
2683 static int
2684find_region(rp, region)
2685 char_u *rp;
2686 char_u *region;
2687{
2688 int i;
2689
2690 for (i = 0; ; i += 2)
2691 {
2692 if (rp[i] == NUL)
2693 return REGION_ALL;
2694 if (rp[i] == region[0] && rp[i + 1] == region[1])
2695 break;
2696 }
2697 return i / 2;
2698}
2699
2700/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002701 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002702 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002703 * Word WF_ONECAP
2704 * W WORD WF_ALLCAP
2705 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002706 */
2707 static int
2708captype(word, end)
2709 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002710 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002711{
2712 char_u *p;
2713 int c;
2714 int firstcap;
2715 int allcap;
2716 int past_second = FALSE; /* past second word char */
2717
2718 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002719 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002720 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002721 return 0; /* only non-word characters, illegal word */
2722#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00002723 if (has_mbyte)
2724 c = mb_ptr2char_adv(&p);
2725 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002726#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00002727 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002728 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002729
2730 /*
2731 * Need to check all letters to find a word with mixed upper/lower.
2732 * But a word with an upper char only at start is a ONECAP.
2733 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002734 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002735 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002736 {
2737#ifdef FEAT_MBYTE
2738 c = mb_ptr2char(p);
2739#else
2740 c = *p;
2741#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002742 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002743 {
2744 /* UUl -> KEEPCAP */
2745 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002746 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002747 allcap = FALSE;
2748 }
2749 else if (!allcap)
2750 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002751 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002752 past_second = TRUE;
2753 }
2754
2755 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002756 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002757 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002758 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002759 return 0;
2760}
2761
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002762# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
2763/*
2764 * Free all languages.
2765 */
2766 void
2767spell_free_all()
2768{
2769 slang_T *lp;
2770 buf_T *buf;
2771
2772 /* Go through all buffers and handle 'spelllang'. */
2773 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2774 ga_clear(&buf->b_langp);
2775
2776 while (first_lang != NULL)
2777 {
2778 lp = first_lang;
2779 first_lang = lp->sl_next;
2780 slang_free(lp);
2781 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002782
2783 init_spell_chartab();
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002784}
2785# endif
2786
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002787# if defined(FEAT_MBYTE) || defined(PROTO)
2788/*
2789 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002790 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002791 */
2792 void
2793spell_reload()
2794{
2795 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002796 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002797
Bram Moolenaarea408852005-06-25 22:49:46 +00002798 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002799 init_spell_chartab();
2800
2801 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002802 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002803
2804 /* Go through all buffers and handle 'spelllang'. */
2805 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2806 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002807 /* Only load the wordlists when 'spelllang' is set and there is a
2808 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002809 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002810 {
2811 FOR_ALL_WINDOWS(wp)
2812 if (wp->w_buffer == buf && wp->w_p_spell)
2813 {
2814 (void)did_set_spelllang(buf);
2815# ifdef FEAT_WINDOWS
2816 break;
2817# endif
2818 }
2819 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002820 }
2821}
2822# endif
2823
Bram Moolenaarb765d632005-06-07 21:00:02 +00002824/*
2825 * Reload the spell file "fname" if it's loaded.
2826 */
2827 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002828spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002829 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002830 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002831{
2832 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002833 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002834
Bram Moolenaarb765d632005-06-07 21:00:02 +00002835 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2836 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
2837 {
2838 slang_clear(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002839 (void)spell_load_file(fname, NULL, lp, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002840 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002841 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002842 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002843
2844 /* When "zg" was used and the file wasn't loaded yet, should redo
2845 * 'spelllang' to get it loaded. */
2846 if (added_word && !didit)
2847 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002848}
2849
2850
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002851/*
2852 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002853 */
2854
Bram Moolenaar51485f02005-06-04 21:55:20 +00002855#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002856 and .dic file. */
2857/*
2858 * Main structure to store the contents of a ".aff" file.
2859 */
2860typedef struct afffile_S
2861{
2862 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002863 int af_rar; /* RAR ID for rare word */
2864 int af_kep; /* KEP ID for keep-case word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00002865 int af_bad; /* BAD ID for banned word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002866 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002867 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
2868 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002869} afffile_T;
2870
2871typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002872/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
2873struct affentry_S
2874{
2875 affentry_T *ae_next; /* next affix with same name/number */
2876 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
2877 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002878 char_u *ae_cond; /* condition (NULL for ".") */
2879 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002880 int ae_rare; /* rare affix */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002881};
2882
2883/* Affix header from ".aff" file. Used for af_pref and af_suff. */
2884typedef struct affheader_S
2885{
2886 char_u ah_key[2]; /* key for hashtable == name of affix entry */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002887 int ah_newID; /* prefix ID after renumbering */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002888 int ah_combine; /* suffix may combine with prefix */
2889 affentry_T *ah_first; /* first affix entry */
2890} affheader_T;
2891
2892#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
2893
2894/*
2895 * Structure that is used to store the items in the word tree. This avoids
2896 * the need to keep track of each allocated thing, it's freed all at once
2897 * after ":mkspell" is done.
2898 */
2899#define SBLOCKSIZE 16000 /* size of sb_data */
2900typedef struct sblock_S sblock_T;
2901struct sblock_S
2902{
2903 sblock_T *sb_next; /* next block in list */
2904 int sb_used; /* nr of bytes already in use */
2905 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002906};
2907
2908/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002909 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002910 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002911typedef struct wordnode_S wordnode_T;
2912struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002913{
Bram Moolenaar0c405862005-06-22 22:26:26 +00002914 union /* shared to save space */
2915 {
2916 char_u hashkey[6]; /* room for the hash key */
2917 int index; /* index in written nodes (valid after first
2918 round) */
2919 } wn_u1;
2920 union /* shared to save space */
2921 {
2922 wordnode_T *next; /* next node with same hash key */
2923 wordnode_T *wnode; /* parent node that will write this node */
2924 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002925 wordnode_T *wn_child; /* child (next byte in word) */
2926 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
2927 always sorted) */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002928 char_u wn_byte; /* Byte for this node. NUL for word end */
2929 char_u wn_flags; /* when wn_byte is NUL: WF_ flags */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002930 short wn_region; /* when wn_byte is NUL: region mask; for
2931 PREFIXTREE it's the prefcondnr */
2932 char_u wn_prefixID; /* supported/required prefix ID or 0 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002933};
2934
Bram Moolenaar51485f02005-06-04 21:55:20 +00002935#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002936
Bram Moolenaar51485f02005-06-04 21:55:20 +00002937/*
2938 * Info used while reading the spell files.
2939 */
2940typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002941{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002942 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00002943 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002944 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00002945 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002946 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002947 sblock_T *si_blocks; /* memory blocks used */
2948 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002949 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002950 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002951 int si_region; /* region mask */
2952 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00002953 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002954 int si_verbose; /* verbose messages */
Bram Moolenaar3982c542005-06-08 21:56:31 +00002955 int si_region_count; /* number of regions supported (1 when there
2956 are no regions) */
2957 char_u si_region_name[16]; /* region names (if count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002958
2959 garray_T si_rep; /* list of fromto_T entries from REP lines */
2960 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002961 char_u *si_sofofr; /* SOFOFROM text */
2962 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002963 int si_followup; /* soundsalike: ? */
2964 int si_collapse; /* soundsalike: ? */
2965 int si_rem_accents; /* soundsalike: remove accents */
2966 garray_T si_map; /* MAP info concatenated */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002967 char_u *si_midword; /* MIDWORD chars, alloc'ed string or NULL */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002968 garray_T si_prefcond; /* table with conditions for postponed
2969 * prefixes, each stored as a string */
2970 int si_newID; /* current value for ah_newID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002971} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002972
Bram Moolenaar51485f02005-06-04 21:55:20 +00002973static afffile_T *spell_read_aff __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002974static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002975static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
2976static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00002977static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00002978static void spell_free_aff __ARGS((afffile_T *aff));
2979static int spell_read_dic __ARGS((char_u *fname, spellinfo_T *spin, afffile_T *affile));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002980static char_u *get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, sblock_T **blp));
2981static 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 +00002982static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin));
2983static void *getroom __ARGS((sblock_T **blp, size_t len));
2984static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s));
2985static void free_blocks __ARGS((sblock_T *bl));
2986static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002987static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags, int region, char_u *pfxlist));
2988static 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 +00002989static void wordtree_compress __ARGS((wordnode_T *root, spellinfo_T *spin));
Bram Moolenaar51485f02005-06-04 21:55:20 +00002990static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot));
2991static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar3982c542005-06-08 21:56:31 +00002992static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar0c405862005-06-22 22:26:26 +00002993static void clear_node __ARGS((wordnode_T *node));
2994static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002995static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00002996static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002997
2998/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002999 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00003000 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003001 */
3002 static afffile_T *
Bram Moolenaar51485f02005-06-04 21:55:20 +00003003spell_read_aff(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003004 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003005 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003006{
3007 FILE *fd;
3008 afffile_T *aff;
3009 char_u rline[MAXLINELEN];
3010 char_u *line;
3011 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003012#define MAXITEMCNT 7
3013 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003014 int itemcnt;
3015 char_u *p;
3016 int lnum = 0;
3017 affheader_T *cur_aff = NULL;
3018 int aff_todo = 0;
3019 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003020 char_u *low = NULL;
3021 char_u *fol = NULL;
3022 char_u *upp = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003023 static char *e_affname = N_("Affix name too long in %s line %d: %s");
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003024 int do_rep;
3025 int do_sal;
3026 int do_map;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003027 int do_midword;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003028 int do_sofo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003029 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003030 hashitem_T *hi;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003031
Bram Moolenaar51485f02005-06-04 21:55:20 +00003032 /*
3033 * Open the file.
3034 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003035 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003036 if (fd == NULL)
3037 {
3038 EMSG2(_(e_notopen), fname);
3039 return NULL;
3040 }
3041
Bram Moolenaarb765d632005-06-07 21:00:02 +00003042 if (spin->si_verbose || p_verbose > 2)
3043 {
3044 if (!spin->si_verbose)
3045 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003046 smsg((char_u *)_("Reading affix file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003047 out_flush();
3048 if (!spin->si_verbose)
3049 verbose_leave();
3050 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003051
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003052 /* Only do REP lines when not done in another .aff file already. */
3053 do_rep = spin->si_rep.ga_len == 0;
3054
3055 /* Only do SAL lines when not done in another .aff file already. */
3056 do_sal = spin->si_sal.ga_len == 0;
3057
3058 /* Only do MAP lines when not done in another .aff file already. */
3059 do_map = spin->si_map.ga_len == 0;
3060
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003061 /* Only do MIDWORD line when not done in another .aff file already */
3062 do_midword = spin->si_midword == NULL;
3063
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003064 /* Only do SOFOFROM and SOFOTO when not done in another .aff file already */
3065 do_sofo = spin->si_sofofr == NULL;
3066
Bram Moolenaar51485f02005-06-04 21:55:20 +00003067 /*
3068 * Allocate and init the afffile_T structure.
3069 */
3070 aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003071 if (aff == NULL)
3072 return NULL;
3073 hash_init(&aff->af_pref);
3074 hash_init(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003075
3076 /*
3077 * Read all the lines in the file one by one.
3078 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003079 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003080 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003081 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003082 ++lnum;
3083
3084 /* Skip comment lines. */
3085 if (*rline == '#')
3086 continue;
3087
3088 /* Convert from "SET" to 'encoding' when needed. */
3089 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003090#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003091 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003092 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003093 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003094 if (pc == NULL)
3095 {
3096 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3097 fname, lnum, rline);
3098 continue;
3099 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003100 line = pc;
3101 }
3102 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003103#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003104 {
3105 pc = NULL;
3106 line = rline;
3107 }
3108
3109 /* Split the line up in white separated items. Put a NUL after each
3110 * item. */
3111 itemcnt = 0;
3112 for (p = line; ; )
3113 {
3114 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
3115 ++p;
3116 if (*p == NUL)
3117 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003118 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003119 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003120 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003121 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003122 ++p;
3123 if (*p == NUL)
3124 break;
3125 *p++ = NUL;
3126 }
3127
3128 /* Handle non-empty lines. */
3129 if (itemcnt > 0)
3130 {
3131 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
3132 && aff->af_enc == NULL)
3133 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003134#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003135 /* Setup for conversion from "ENC" to 'encoding'. */
3136 aff->af_enc = enc_canonize(items[1]);
3137 if (aff->af_enc != NULL && !spin->si_ascii
3138 && convert_setup(&spin->si_conv, aff->af_enc,
3139 p_enc) == FAIL)
3140 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
3141 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003142 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003143#else
3144 smsg((char_u *)_("Conversion in %s not supported"), fname);
3145#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003146 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003147 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2)
3148 {
3149 if (do_midword)
3150 spin->si_midword = vim_strsave(items[1]);
3151 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00003152 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
3153 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003154 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003155 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003156 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003157 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003158 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003159 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003160 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
3161 && aff->af_rar == 0)
3162 {
3163 aff->af_rar = items[1][0];
3164 if (items[1][1] != NUL)
3165 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3166 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00003167 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
3168 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003169 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003170 aff->af_kep = items[1][0];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003171 if (items[1][1] != NUL)
3172 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3173 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00003174 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
3175 && aff->af_bad == 0)
3176 {
3177 aff->af_bad = items[1][0];
3178 if (items[1][1] != NUL)
3179 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3180 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003181 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
3182 {
3183 aff->af_pfxpostpone = TRUE;
3184 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003185 else if ((STRCMP(items[0], "PFX") == 0
3186 || STRCMP(items[0], "SFX") == 0)
3187 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003188 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003189 {
Bram Moolenaar8db73182005-06-17 21:51:16 +00003190 /* Myspell allows extra text after the item, but that might
3191 * mean mistakes go unnoticed. Require a comment-starter. */
3192 if (itemcnt > 4 && *items[4] != '#')
3193 smsg((char_u *)_("Trailing text in %s line %d: %s"),
3194 fname, lnum, items[4]);
3195
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003196 /* New affix letter. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003197 cur_aff = (affheader_T *)getroom(&spin->si_blocks,
3198 sizeof(affheader_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003199 if (cur_aff == NULL)
3200 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003201 cur_aff->ah_key[0] = *items[1]; /* TODO: multi-byte? */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003202 cur_aff->ah_key[1] = NUL;
3203 if (items[1][1] != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003204 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003205 if (*items[2] == 'Y')
3206 cur_aff->ah_combine = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003207 else if (*items[2] != 'N')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003208 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
3209 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003210
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003211 if (*items[0] == 'P')
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003212 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003213 tp = &aff->af_pref;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003214 /* Use a new number in the .spl file later, to be able to
3215 * handle multiple .aff files. */
3216 if (aff->af_pfxpostpone)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003217 cur_aff->ah_newID = ++spin->si_newID;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003218 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003219 else
3220 tp = &aff->af_suff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003221 aff_todo = atoi((char *)items[3]);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003222 hi = hash_find(tp, cur_aff->ah_key);
3223 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar51485f02005-06-04 21:55:20 +00003224 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003225 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
3226 fname, lnum, items[1]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003227 aff_todo = 0;
3228 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003229 else
3230 hash_add(tp, cur_aff->ah_key);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003231 }
3232 else if ((STRCMP(items[0], "PFX") == 0
3233 || STRCMP(items[0], "SFX") == 0)
3234 && aff_todo > 0
3235 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003236 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003237 {
3238 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003239 int rare = FALSE;
3240 int lasti = 5;
3241
3242 /* Check for "rare" after the other info. */
3243 if (itemcnt > 5 && STRICMP(items[5], "rare") == 0)
3244 {
3245 rare = TRUE;
3246 lasti = 6;
3247 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003248
Bram Moolenaar8db73182005-06-17 21:51:16 +00003249 /* Myspell allows extra text after the item, but that might
3250 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003251 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar8db73182005-06-17 21:51:16 +00003252 smsg((char_u *)_("Trailing text in %s line %d: %s"),
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003253 fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00003254
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003255 /* New item for an affix letter. */
3256 --aff_todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003257 aff_entry = (affentry_T *)getroom(&spin->si_blocks,
3258 sizeof(affentry_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003259 if (aff_entry == NULL)
3260 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003261 aff_entry->ae_rare = rare;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003262
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003263 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003264 aff_entry->ae_chop = getroom_save(&spin->si_blocks,
3265 items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003266 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003267 aff_entry->ae_add = getroom_save(&spin->si_blocks,
3268 items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003269
Bram Moolenaar51485f02005-06-04 21:55:20 +00003270 /* Don't use an affix entry with non-ASCII characters when
3271 * "spin->si_ascii" is TRUE. */
3272 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00003273 || has_non_ascii(aff_entry->ae_add)))
3274 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003275 aff_entry->ae_next = cur_aff->ah_first;
3276 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003277
3278 if (STRCMP(items[4], ".") != 0)
3279 {
3280 char_u buf[MAXLINELEN];
3281
3282 aff_entry->ae_cond = getroom_save(&spin->si_blocks,
3283 items[4]);
3284 if (*items[0] == 'P')
3285 sprintf((char *)buf, "^%s", items[4]);
3286 else
3287 sprintf((char *)buf, "%s$", items[4]);
3288 aff_entry->ae_prog = vim_regcomp(buf,
3289 RE_MAGIC + RE_STRING);
3290 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003291
3292 /* For postponed prefixes we need an entry in si_prefcond
3293 * for the condition. Use an existing one if possible. */
3294 if (*items[0] == 'P' && aff->af_pfxpostpone
3295 && aff_entry->ae_chop == NULL)
3296 {
3297 int idx;
3298 char_u **pp;
3299
3300 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
3301 --idx)
3302 {
3303 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
3304 if (str_equal(p, aff_entry->ae_cond))
3305 break;
3306 }
3307 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
3308 {
3309 /* Not found, add a new condition. */
3310 idx = spin->si_prefcond.ga_len++;
3311 pp = ((char_u **)spin->si_prefcond.ga_data) + idx;
3312 if (aff_entry->ae_cond == NULL)
3313 *pp = NULL;
3314 else
3315 *pp = getroom_save(&spin->si_blocks,
3316 aff_entry->ae_cond);
3317 }
3318
3319 /* Add the prefix to the prefix tree. */
3320 if (aff_entry->ae_add == NULL)
3321 p = (char_u *)"";
3322 else
3323 p = aff_entry->ae_add;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003324 tree_add_word(p, spin->si_prefroot, rare ? -2 : -1,
3325 idx, cur_aff->ah_newID, &spin->si_blocks);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003326 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003327 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003328 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003329 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
3330 {
3331 if (fol != NULL)
3332 smsg((char_u *)_("Duplicate FOL in %s line %d"),
3333 fname, lnum);
3334 else
3335 fol = vim_strsave(items[1]);
3336 }
3337 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
3338 {
3339 if (low != NULL)
3340 smsg((char_u *)_("Duplicate LOW in %s line %d"),
3341 fname, lnum);
3342 else
3343 low = vim_strsave(items[1]);
3344 }
3345 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
3346 {
3347 if (upp != NULL)
3348 smsg((char_u *)_("Duplicate UPP in %s line %d"),
3349 fname, lnum);
3350 else
3351 upp = vim_strsave(items[1]);
3352 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003353 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003354 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003355 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003356 if (!isdigit(*items[1]))
3357 smsg((char_u *)_("Expected REP count in %s line %d"),
3358 fname, lnum);
3359 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003360 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
3361 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003362 /* REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003363 if (do_rep)
3364 add_fromto(spin, &spin->si_rep, items[1], items[2]);
3365 }
3366 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
3367 {
3368 /* MAP item or count */
3369 if (!found_map)
3370 {
3371 /* First line contains the count. */
3372 found_map = TRUE;
3373 if (!isdigit(*items[1]))
3374 smsg((char_u *)_("Expected MAP count in %s line %d"),
3375 fname, lnum);
3376 }
3377 else if (do_map)
3378 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003379 int c;
3380
3381 /* Check that every character appears only once. */
3382 for (p = items[1]; *p != NUL; )
3383 {
3384#ifdef FEAT_MBYTE
3385 c = mb_ptr2char_adv(&p);
3386#else
3387 c = *p++;
3388#endif
3389 if ((spin->si_map.ga_len > 0
3390 && vim_strchr(spin->si_map.ga_data, c)
3391 != NULL)
3392 || vim_strchr(p, c) != NULL)
3393 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
3394 fname, lnum);
3395 }
3396
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003397 /* We simply concatenate all the MAP strings, separated by
3398 * slashes. */
3399 ga_concat(&spin->si_map, items[1]);
3400 ga_append(&spin->si_map, '/');
3401 }
3402 }
3403 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
3404 {
3405 if (do_sal)
3406 {
3407 /* SAL item (sounds-a-like)
3408 * Either one of the known keys or a from-to pair. */
3409 if (STRCMP(items[1], "followup") == 0)
3410 spin->si_followup = sal_to_bool(items[2]);
3411 else if (STRCMP(items[1], "collapse_result") == 0)
3412 spin->si_collapse = sal_to_bool(items[2]);
3413 else if (STRCMP(items[1], "remove_accents") == 0)
3414 spin->si_rem_accents = sal_to_bool(items[2]);
3415 else
3416 /* when "to" is "_" it means empty */
3417 add_fromto(spin, &spin->si_sal, items[1],
3418 STRCMP(items[2], "_") == 0 ? (char_u *)""
3419 : items[2]);
3420 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003421 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003422 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
3423 && (!do_sofo || spin->si_sofofr == NULL))
3424 {
3425 if (do_sofo)
3426 spin->si_sofofr = vim_strsave(items[1]);
3427 }
3428 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
3429 && (!do_sofo || spin->si_sofoto == NULL))
3430 {
3431 if (do_sofo)
3432 spin->si_sofoto = vim_strsave(items[1]);
3433 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003434 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003435 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
3436 fname, lnum, items[0]);
3437 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003438 }
3439
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003440 if (do_sofo && (spin->si_sofofr == NULL) != (spin->si_sofoto == NULL))
3441 smsg((char_u *)_("Missing SOFO%s line in %s"),
3442 spin->si_sofofr == NULL ? "FROM" : "TO", fname);
3443 if (spin->si_sofofr != NULL && spin->si_sal.ga_len > 0)
3444 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
3445
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003446 if (fol != NULL || low != NULL || upp != NULL)
3447 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003448 if (spin->si_clear_chartab)
3449 {
3450 /* Clear the char type tables, don't want to use any of the
3451 * currently used spell properties. */
3452 init_spell_chartab();
3453 spin->si_clear_chartab = FALSE;
3454 }
3455
Bram Moolenaar3982c542005-06-08 21:56:31 +00003456 /*
3457 * Don't write a word table for an ASCII file, so that we don't check
3458 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003459 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00003460 * mb_get_class(), the list of chars in the file will be incomplete.
3461 */
3462 if (!spin->si_ascii
3463#ifdef FEAT_MBYTE
3464 && !enc_utf8
3465#endif
3466 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003467 {
3468 if (fol == NULL || low == NULL || upp == NULL)
3469 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
3470 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00003471 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003472 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003473
3474 vim_free(fol);
3475 vim_free(low);
3476 vim_free(upp);
3477 }
3478
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003479 vim_free(pc);
3480 fclose(fd);
3481 return aff;
3482}
3483
3484/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003485 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
3486 * NULL as equal.
3487 */
3488 static int
3489str_equal(s1, s2)
3490 char_u *s1;
3491 char_u *s2;
3492{
3493 if (s1 == NULL || s2 == NULL)
3494 return s1 == s2;
3495 return STRCMP(s1, s2) == 0;
3496}
3497
3498/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003499 * Add a from-to item to "gap". Used for REP and SAL items.
3500 * They are stored case-folded.
3501 */
3502 static void
3503add_fromto(spin, gap, from, to)
3504 spellinfo_T *spin;
3505 garray_T *gap;
3506 char_u *from;
3507 char_u *to;
3508{
3509 fromto_T *ftp;
3510 char_u word[MAXWLEN];
3511
3512 if (ga_grow(gap, 1) == OK)
3513 {
3514 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
3515 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
3516 ftp->ft_from = getroom_save(&spin->si_blocks, word);
3517 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
3518 ftp->ft_to = getroom_save(&spin->si_blocks, word);
3519 ++gap->ga_len;
3520 }
3521}
3522
3523/*
3524 * Convert a boolean argument in a SAL line to TRUE or FALSE;
3525 */
3526 static int
3527sal_to_bool(s)
3528 char_u *s;
3529{
3530 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
3531}
3532
3533/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00003534 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
3535 * When "s" is NULL FALSE is returned.
3536 */
3537 static int
3538has_non_ascii(s)
3539 char_u *s;
3540{
3541 char_u *p;
3542
3543 if (s != NULL)
3544 for (p = s; *p != NUL; ++p)
3545 if (*p >= 128)
3546 return TRUE;
3547 return FALSE;
3548}
3549
3550/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003551 * Free the structure filled by spell_read_aff().
3552 */
3553 static void
3554spell_free_aff(aff)
3555 afffile_T *aff;
3556{
3557 hashtab_T *ht;
3558 hashitem_T *hi;
3559 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003560 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003561 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003562
3563 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003564
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003565 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003566 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
3567 {
3568 todo = ht->ht_used;
3569 for (hi = ht->ht_array; todo > 0; ++hi)
3570 {
3571 if (!HASHITEM_EMPTY(hi))
3572 {
3573 --todo;
3574 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003575 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3576 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003577 }
3578 }
3579 if (ht == &aff->af_suff)
3580 break;
3581 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003582
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003583 hash_clear(&aff->af_pref);
3584 hash_clear(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003585}
3586
3587/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003588 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003589 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003590 */
3591 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00003592spell_read_dic(fname, spin, affile)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003593 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003594 spellinfo_T *spin;
3595 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003596{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003597 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003598 char_u line[MAXLINELEN];
Bram Moolenaar51485f02005-06-04 21:55:20 +00003599 char_u *afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003600 char_u *pfxlist;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003601 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003602 char_u *pc;
3603 char_u *w;
3604 int l;
3605 hash_T hash;
3606 hashitem_T *hi;
3607 FILE *fd;
3608 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003609 int non_ascii = 0;
3610 int retval = OK;
3611 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003612 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003613
Bram Moolenaar51485f02005-06-04 21:55:20 +00003614 /*
3615 * Open the file.
3616 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003617 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003618 if (fd == NULL)
3619 {
3620 EMSG2(_(e_notopen), fname);
3621 return FAIL;
3622 }
3623
Bram Moolenaar51485f02005-06-04 21:55:20 +00003624 /* The hashtable is only used to detect duplicated words. */
3625 hash_init(&ht);
3626
Bram Moolenaar8db73182005-06-17 21:51:16 +00003627 spin->si_foldwcount = 0;
3628 spin->si_keepwcount = 0;
3629
Bram Moolenaarb765d632005-06-07 21:00:02 +00003630 if (spin->si_verbose || p_verbose > 2)
3631 {
3632 if (!spin->si_verbose)
3633 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003634 smsg((char_u *)_("Reading dictionary file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003635 out_flush();
3636 if (!spin->si_verbose)
3637 verbose_leave();
3638 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003639
3640 /* Read and ignore the first line: word count. */
3641 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003642 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003643 EMSG2(_("E760: No word count in %s"), fname);
3644
3645 /*
3646 * Read all the lines in the file one by one.
3647 * The words are converted to 'encoding' here, before being added to
3648 * the hashtable.
3649 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003650 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003651 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003652 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003653 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003654 if (line[0] == '#')
3655 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003656
Bram Moolenaar51485f02005-06-04 21:55:20 +00003657 /* Remove CR, LF and white space from the end. White space halfway
3658 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003659 l = STRLEN(line);
3660 while (l > 0 && line[l - 1] <= ' ')
3661 --l;
3662 if (l == 0)
3663 continue; /* empty line */
3664 line[l] = NUL;
3665
Bram Moolenaar51485f02005-06-04 21:55:20 +00003666 /* Find the optional affix names. */
3667 afflist = vim_strchr(line, '/');
3668 if (afflist != NULL)
3669 *afflist++ = NUL;
3670
3671 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
3672 if (spin->si_ascii && has_non_ascii(line))
3673 {
3674 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003675 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003676 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003677
Bram Moolenaarb765d632005-06-07 21:00:02 +00003678#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003679 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003680 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003681 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003682 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003683 if (pc == NULL)
3684 {
3685 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3686 fname, lnum, line);
3687 continue;
3688 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003689 w = pc;
3690 }
3691 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003692#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003693 {
3694 pc = NULL;
3695 w = line;
3696 }
3697
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003698 /* This takes time, print a message now and then. */
3699 if (spin->si_verbose && (lnum & 0x3ff) == 0)
3700 {
3701 vim_snprintf((char *)message, sizeof(message),
3702 _("line %6d, word %6d - %s"),
3703 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
3704 msg_start();
3705 msg_puts_long_attr(message, 0);
3706 msg_clr_eos();
3707 msg_didout = FALSE;
3708 msg_col = 0;
3709 out_flush();
3710 }
3711
Bram Moolenaar51485f02005-06-04 21:55:20 +00003712 /* Store the word in the hashtable to be able to find duplicates. */
3713 dw = (char_u *)getroom_save(&spin->si_blocks, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003714 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003715 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003716 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003717 if (retval == FAIL)
3718 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003719
Bram Moolenaar51485f02005-06-04 21:55:20 +00003720 hash = hash_hash(dw);
3721 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003722 if (!HASHITEM_EMPTY(hi))
3723 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003724 fname, lnum, dw);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003725 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00003726 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003727
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003728 flags = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003729 pfxlist = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003730 if (afflist != NULL)
3731 {
3732 /* Check for affix name that stands for keep-case word and stands
3733 * for rare word (if defined). */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003734 if (affile->af_kep != NUL
3735 && vim_strchr(afflist, affile->af_kep) != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003736 flags |= WF_KEEPCAP;
3737 if (affile->af_rar != NUL
3738 && vim_strchr(afflist, affile->af_rar) != NULL)
3739 flags |= WF_RARE;
Bram Moolenaar0c405862005-06-22 22:26:26 +00003740 if (affile->af_bad != NUL
3741 && vim_strchr(afflist, affile->af_bad) != NULL)
3742 flags |= WF_BANNED;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003743
3744 if (affile->af_pfxpostpone)
3745 /* Need to store the list of prefix IDs with the word. */
3746 pfxlist = get_pfxlist(affile, afflist, &spin->si_blocks);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003747 }
3748
Bram Moolenaar51485f02005-06-04 21:55:20 +00003749 /* Add the word to the word tree(s). */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003750 if (store_word(dw, spin, flags, spin->si_region, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003751 retval = FAIL;
3752
3753 if (afflist != NULL)
3754 {
3755 /* Find all matching suffixes and add the resulting words.
3756 * Additionally do matching prefixes that combine. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003757 if (store_aff_word(dw, spin, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003758 &affile->af_suff, &affile->af_pref,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003759 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003760 retval = FAIL;
3761
3762 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003763 if (store_aff_word(dw, spin, afflist, affile,
3764 &affile->af_pref, NULL,
3765 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003766 retval = FAIL;
3767 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003768 }
3769
Bram Moolenaar51485f02005-06-04 21:55:20 +00003770 if (spin->si_ascii && non_ascii > 0)
3771 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
3772 non_ascii);
3773 hash_clear(&ht);
3774
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003775 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003776 return retval;
3777}
3778
3779/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003780 * Get the list of prefix IDs from the affix list "afflist".
3781 * Used for PFXPOSTPONE.
3782 * Returns a string allocated with getroom(). NULL when there are no prefixes
3783 * or when out of memory.
3784 */
3785 static char_u *
3786get_pfxlist(affile, afflist, blp)
3787 afffile_T *affile;
3788 char_u *afflist;
3789 sblock_T **blp;
3790{
3791 char_u *p;
3792 int cnt;
3793 int round;
3794 char_u *res = NULL;
3795 char_u key[2];
3796 hashitem_T *hi;
3797
3798 key[1] = NUL;
3799
3800 /* round 1: count the number of prefix IDs.
3801 * round 2: move prefix IDs to "res" */
3802 for (round = 1; round <= 2; ++round)
3803 {
3804 cnt = 0;
3805 for (p = afflist; *p != NUL; ++p)
3806 {
3807 key[0] = *p;
3808 hi = hash_find(&affile->af_pref, key);
3809 if (!HASHITEM_EMPTY(hi))
3810 {
3811 /* This is a prefix ID, use the new number. */
3812 if (round == 2)
3813 res[cnt] = HI2AH(hi)->ah_newID;
3814 ++cnt;
3815 }
3816 }
3817 if (round == 1 && cnt > 0)
3818 res = getroom(blp, cnt + 1);
3819 if (res == NULL)
3820 break;
3821 }
3822
3823 if (res != NULL)
3824 res[cnt] = NUL;
3825 return res;
3826}
3827
3828/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003829 * Apply affixes to a word and store the resulting words.
3830 * "ht" is the hashtable with affentry_T that need to be applied, either
3831 * prefixes or suffixes.
3832 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
3833 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003834 *
3835 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003836 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003837 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003838store_aff_word(word, spin, afflist, affile, ht, xht, comb, flags, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003839 char_u *word; /* basic word start */
3840 spellinfo_T *spin; /* spell info */
3841 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003842 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003843 hashtab_T *ht;
3844 hashtab_T *xht;
3845 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003846 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003847 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003848{
3849 int todo;
3850 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003851 affheader_T *ah;
3852 affentry_T *ae;
3853 regmatch_T regmatch;
3854 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003855 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003856 int i;
3857 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003858 int use_flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003859
Bram Moolenaar51485f02005-06-04 21:55:20 +00003860 todo = ht->ht_used;
3861 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003862 {
3863 if (!HASHITEM_EMPTY(hi))
3864 {
3865 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003866 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00003867
Bram Moolenaar51485f02005-06-04 21:55:20 +00003868 /* Check that the affix combines, if required, and that the word
3869 * supports this affix. */
3870 if ((!comb || ah->ah_combine)
3871 && vim_strchr(afflist, *ah->ah_key) != NULL)
Bram Moolenaar5482f332005-04-17 20:18:43 +00003872 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003873 /* Loop over all affix entries with this name. */
3874 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003875 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003876 /* Check the condition. It's not logical to match case
3877 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003878 * Myspell.
3879 * For prefixes, when "PFXPOSTPONE" was used, only do
3880 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003881 regmatch.regprog = ae->ae_prog;
3882 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003883 if ((xht != NULL || !affile->af_pfxpostpone
3884 || ae->ae_chop != NULL)
3885 && (ae->ae_prog == NULL
3886 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003887 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003888 /* Match. Remove the chop and add the affix. */
3889 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003890 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003891 /* prefix: chop/add at the start of the word */
3892 if (ae->ae_add == NULL)
3893 *newword = NUL;
3894 else
3895 STRCPY(newword, ae->ae_add);
3896 p = word;
3897 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003898 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003899 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003900#ifdef FEAT_MBYTE
3901 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003902 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003903 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003904 for ( ; i > 0; --i)
3905 mb_ptr_adv(p);
3906 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00003907 else
3908#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003909 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003910 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003911 STRCAT(newword, p);
3912 }
3913 else
3914 {
3915 /* suffix: chop/add at the end of the word */
3916 STRCPY(newword, word);
3917 if (ae->ae_chop != NULL)
3918 {
3919 /* Remove chop string. */
3920 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003921 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003922 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003923 mb_ptr_back(newword, p);
3924 *p = NUL;
3925 }
3926 if (ae->ae_add != NULL)
3927 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003928 }
3929
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003930 /* Obey the "rare" flag of the affix. */
3931 if (ae->ae_rare)
3932 use_flags = flags | WF_RARE;
3933 else
3934 use_flags = flags;
3935
Bram Moolenaar51485f02005-06-04 21:55:20 +00003936 /* Store the modified word. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003937 if (store_word(newword, spin, use_flags,
3938 spin->si_region, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003939 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003940
Bram Moolenaar51485f02005-06-04 21:55:20 +00003941 /* When added a suffix and combining is allowed also
3942 * try adding prefixes additionally. */
3943 if (xht != NULL && ah->ah_combine)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003944 if (store_aff_word(newword, spin, afflist, affile,
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003945 xht, NULL, TRUE, use_flags, pfxlist)
3946 == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003947 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003948 }
3949 }
3950 }
3951 }
3952 }
3953
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003954 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003955}
3956
3957/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003958 * Read a file with a list of words.
3959 */
3960 static int
3961spell_read_wordfile(fname, spin)
3962 char_u *fname;
3963 spellinfo_T *spin;
3964{
3965 FILE *fd;
3966 long lnum = 0;
3967 char_u rline[MAXLINELEN];
3968 char_u *line;
3969 char_u *pc = NULL;
3970 int l;
3971 int retval = OK;
3972 int did_word = FALSE;
3973 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003974 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003975 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003976
3977 /*
3978 * Open the file.
3979 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003980 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00003981 if (fd == NULL)
3982 {
3983 EMSG2(_(e_notopen), fname);
3984 return FAIL;
3985 }
3986
Bram Moolenaarb765d632005-06-07 21:00:02 +00003987 if (spin->si_verbose || p_verbose > 2)
3988 {
3989 if (!spin->si_verbose)
3990 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003991 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003992 out_flush();
3993 if (!spin->si_verbose)
3994 verbose_leave();
3995 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003996
3997 /*
3998 * Read all the lines in the file one by one.
3999 */
4000 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
4001 {
4002 line_breakcheck();
4003 ++lnum;
4004
4005 /* Skip comment lines. */
4006 if (*rline == '#')
4007 continue;
4008
4009 /* Remove CR, LF and white space from the end. */
4010 l = STRLEN(rline);
4011 while (l > 0 && rline[l - 1] <= ' ')
4012 --l;
4013 if (l == 0)
4014 continue; /* empty or blank line */
4015 rline[l] = NUL;
4016
4017 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
4018 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004019#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004020 if (spin->si_conv.vc_type != CONV_NONE)
4021 {
4022 pc = string_convert(&spin->si_conv, rline, NULL);
4023 if (pc == NULL)
4024 {
4025 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4026 fname, lnum, rline);
4027 continue;
4028 }
4029 line = pc;
4030 }
4031 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004032#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004033 {
4034 pc = NULL;
4035 line = rline;
4036 }
4037
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004038 flags = 0;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004039 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004040
4041 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00004042 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004043 ++line;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004044
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004045 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004046 {
4047 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004048 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
4049 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004050 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004051 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
4052 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004053 else
4054 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004055#ifdef FEAT_MBYTE
4056 char_u *enc;
4057
Bram Moolenaar51485f02005-06-04 21:55:20 +00004058 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004059 line += 10;
4060 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004061 if (enc != NULL && !spin->si_ascii
4062 && convert_setup(&spin->si_conv, enc,
4063 p_enc) == FAIL)
4064 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00004065 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004066 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004067 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004068#else
4069 smsg((char_u *)_("Conversion in %s not supported"), fname);
4070#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004071 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004072 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004073 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004074
Bram Moolenaar3982c542005-06-08 21:56:31 +00004075 if (STRNCMP(line, "regions=", 8) == 0)
4076 {
4077 if (spin->si_region_count > 1)
4078 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
4079 fname, lnum, line);
4080 else
4081 {
4082 line += 8;
4083 if (STRLEN(line) > 16)
4084 smsg((char_u *)_("Too many regions in %s line %d: %s"),
4085 fname, lnum, line);
4086 else
4087 {
4088 spin->si_region_count = STRLEN(line) / 2;
4089 STRCPY(spin->si_region_name, line);
4090 }
4091 }
4092 continue;
4093 }
4094
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004095 if (*line == '=')
4096 {
4097 /* keep-case word */
4098 flags |= WF_KEEPCAP;
4099 ++line;
4100 }
4101
4102 if (*line == '!')
4103 {
4104 /* Bad, bad, wicked word. */
4105 flags |= WF_BANNED;
4106 ++line;
4107 }
4108 else if (*line == '?')
4109 {
4110 /* Rare word. */
4111 flags |= WF_RARE;
4112 ++line;
4113 }
4114
Bram Moolenaar3982c542005-06-08 21:56:31 +00004115 if (VIM_ISDIGIT(*line))
4116 {
4117 /* region number(s) */
4118 regionmask = 0;
4119 while (VIM_ISDIGIT(*line))
4120 {
4121 l = *line - '0';
4122 if (l > spin->si_region_count)
4123 {
4124 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
4125 fname, lnum, line);
4126 break;
4127 }
4128 regionmask |= 1 << (l - 1);
4129 ++line;
4130 }
4131 flags |= WF_REGION;
4132 }
4133
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004134 if (flags == 0)
4135 {
4136 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
Bram Moolenaar51485f02005-06-04 21:55:20 +00004137 fname, lnum, line);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004138 continue;
4139 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004140 }
4141
4142 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
4143 if (spin->si_ascii && has_non_ascii(line))
4144 {
4145 ++non_ascii;
4146 continue;
4147 }
4148
4149 /* Normal word: store it. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004150 if (store_word(line, spin, flags, regionmask, NULL) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004151 {
4152 retval = FAIL;
4153 break;
4154 }
4155 did_word = TRUE;
4156 }
4157
4158 vim_free(pc);
4159 fclose(fd);
4160
Bram Moolenaarb765d632005-06-07 21:00:02 +00004161 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
4162 {
4163 if (p_verbose > 2)
4164 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00004165 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
4166 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004167 if (p_verbose > 2)
4168 verbose_leave();
4169 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004170 return retval;
4171}
4172
4173/*
4174 * Get part of an sblock_T, "len" bytes long.
4175 * This avoids calling free() for every little struct we use.
4176 * The memory is cleared to all zeros.
4177 * Returns NULL when out of memory.
4178 */
4179 static void *
4180getroom(blp, len)
4181 sblock_T **blp;
4182 size_t len; /* length needed */
4183{
4184 char_u *p;
4185 sblock_T *bl = *blp;
4186
4187 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
4188 {
4189 /* Allocate a block of memory. This is not freed until much later. */
4190 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
4191 if (bl == NULL)
4192 return NULL;
4193 bl->sb_next = *blp;
4194 *blp = bl;
4195 bl->sb_used = 0;
4196 }
4197
4198 p = bl->sb_data + bl->sb_used;
4199 bl->sb_used += len;
4200
4201 return p;
4202}
4203
4204/*
4205 * Make a copy of a string into memory allocated with getroom().
4206 */
4207 static char_u *
4208getroom_save(blp, s)
4209 sblock_T **blp;
4210 char_u *s;
4211{
4212 char_u *sc;
4213
4214 sc = (char_u *)getroom(blp, STRLEN(s) + 1);
4215 if (sc != NULL)
4216 STRCPY(sc, s);
4217 return sc;
4218}
4219
4220
4221/*
4222 * Free the list of allocated sblock_T.
4223 */
4224 static void
4225free_blocks(bl)
4226 sblock_T *bl;
4227{
4228 sblock_T *next;
4229
4230 while (bl != NULL)
4231 {
4232 next = bl->sb_next;
4233 vim_free(bl);
4234 bl = next;
4235 }
4236}
4237
4238/*
4239 * Allocate the root of a word tree.
4240 */
4241 static wordnode_T *
4242wordtree_alloc(blp)
4243 sblock_T **blp;
4244{
4245 return (wordnode_T *)getroom(blp, sizeof(wordnode_T));
4246}
4247
4248/*
4249 * Store a word in the tree(s).
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004250 * Always store it in the case-folded tree. A keep-case word can also be used
4251 * with all caps.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004252 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004253 * When "pfxlist" is not NULL store the word for each prefix ID.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004254 */
4255 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004256store_word(word, spin, flags, region, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004257 char_u *word;
4258 spellinfo_T *spin;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004259 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004260 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004261 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004262{
4263 int len = STRLEN(word);
4264 int ct = captype(word, word + len);
4265 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004266 int res = OK;
4267 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004268
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004269 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004270 for (p = pfxlist; res == OK; ++p)
4271 {
4272 res = tree_add_word(foldword, spin->si_foldroot, ct | flags,
4273 region, p == NULL ? 0 : *p, &spin->si_blocks);
4274 if (p == NULL || *p == NUL)
4275 break;
4276 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00004277 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004278
4279 if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP))
Bram Moolenaar8db73182005-06-17 21:51:16 +00004280 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004281 for (p = pfxlist; res == OK; ++p)
4282 {
4283 res = tree_add_word(word, spin->si_keeproot, flags,
4284 region, p == NULL ? 0 : *p, &spin->si_blocks);
4285 if (p == NULL || *p == NUL)
4286 break;
4287 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00004288 ++spin->si_keepwcount;
4289 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004290 return res;
4291}
4292
4293/*
4294 * Add word "word" to a word tree at "root".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004295 * When "flags" < 0 we are adding to the prefix tree where flags is used for
4296 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004297 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004298 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004299 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004300tree_add_word(word, root, flags, region, prefixID, blp)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004301 char_u *word;
4302 wordnode_T *root;
4303 int flags;
4304 int region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004305 int prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004306 sblock_T **blp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004307{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004308 wordnode_T *node = root;
4309 wordnode_T *np;
4310 wordnode_T **prev = NULL;
4311 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004312
Bram Moolenaar51485f02005-06-04 21:55:20 +00004313 /* Add each byte of the word to the tree, including the NUL at the end. */
4314 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004315 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004316 /* Look for the sibling that has the same character. They are sorted
4317 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004318 * higher byte value. For zero bytes (end of word) the sorting is
4319 * done on flags and then on prefixID
Bram Moolenaar51485f02005-06-04 21:55:20 +00004320 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004321 while (node != NULL
4322 && (node->wn_byte < word[i]
4323 || (node->wn_byte == NUL
4324 && (flags < 0
4325 ? node->wn_prefixID < prefixID
4326 : node->wn_flags < (flags & 0xff)
4327 || (node->wn_flags == (flags & 0xff)
4328 && node->wn_prefixID < prefixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004329 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004330 prev = &node->wn_sibling;
4331 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004332 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004333 if (node == NULL
4334 || node->wn_byte != word[i]
4335 || (word[i] == NUL
4336 && (flags < 0
4337 || node->wn_flags != (flags & 0xff)
4338 || node->wn_prefixID != prefixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004339 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004340 /* Allocate a new node. */
4341 np = (wordnode_T *)getroom(blp, sizeof(wordnode_T));
4342 if (np == NULL)
4343 return FAIL;
4344 np->wn_byte = word[i];
4345 *prev = np;
4346 np->wn_sibling = node;
4347 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004348 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004349
Bram Moolenaar51485f02005-06-04 21:55:20 +00004350 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004351 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004352 node->wn_flags = flags;
4353 node->wn_region |= region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004354 node->wn_prefixID = prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004355 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004356 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004357 prev = &node->wn_child;
4358 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004359 }
4360
4361 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004362}
4363
4364/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004365 * Compress a tree: find tails that are identical and can be shared.
4366 */
4367 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00004368wordtree_compress(root, spin)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004369 wordnode_T *root;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004370 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004371{
4372 hashtab_T ht;
4373 int n;
4374 int tot = 0;
4375
4376 if (root != NULL)
4377 {
4378 hash_init(&ht);
4379 n = node_compress(root, &ht, &tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004380 if (spin->si_verbose || p_verbose > 2)
4381 {
4382 if (!spin->si_verbose)
4383 verbose_enter();
4384 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar51485f02005-06-04 21:55:20 +00004385 n, tot, (tot - n) * 100 / tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004386 if (p_verbose > 2)
4387 verbose_leave();
4388 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004389 hash_clear(&ht);
4390 }
4391}
4392
4393/*
4394 * Compress a node, its siblings and its children, depth first.
4395 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004396 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004397 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00004398node_compress(node, ht, tot)
4399 wordnode_T *node;
4400 hashtab_T *ht;
4401 int *tot; /* total count of nodes before compressing,
4402 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004403{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004404 wordnode_T *np;
4405 wordnode_T *tp;
4406 wordnode_T *child;
4407 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004408 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004409 int len = 0;
4410 unsigned nr, n;
4411 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004412
Bram Moolenaar51485f02005-06-04 21:55:20 +00004413 /*
4414 * Go through the list of siblings. Compress each child and then try
4415 * finding an identical child to replace it.
4416 * Note that with "child" we mean not just the node that is pointed to,
4417 * but the whole list of siblings, of which the node is the first.
4418 */
4419 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004420 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004421 ++len;
4422 if ((child = np->wn_child) != NULL)
4423 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00004424 /* Compress the child. This fills hashkey. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004425 compressed += node_compress(child, ht, tot);
4426
4427 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004428 hash = hash_hash(child->wn_u1.hashkey);
4429 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004430 tp = NULL;
4431 if (!HASHITEM_EMPTY(hi))
4432 {
4433 /* There are children with an identical hash value. Now check
4434 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004435 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004436 if (node_equal(child, tp))
4437 {
4438 /* Found one! Now use that child in place of the
4439 * current one. This means the current child is
4440 * dropped from the tree. */
4441 np->wn_child = tp;
4442 ++compressed;
4443 break;
4444 }
4445 if (tp == NULL)
4446 {
4447 /* No other child with this hash value equals the child of
4448 * the node, add it to the linked list after the first
4449 * item. */
4450 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004451 child->wn_u2.next = tp->wn_u2.next;
4452 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004453 }
4454 }
4455 else
4456 /* No other child has this hash value, add it to the
4457 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004458 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004459 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004460 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004461 *tot += len;
4462
4463 /*
4464 * Make a hash key for the node and its siblings, so that we can quickly
4465 * find a lookalike node. This must be done after compressing the sibling
4466 * list, otherwise the hash key would become invalid by the compression.
4467 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004468 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004469 nr = 0;
4470 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004471 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004472 if (np->wn_byte == NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004473 /* end node: use wn_flags, wn_region and wn_prefixID */
4474 n = np->wn_flags + (np->wn_region << 8) + (np->wn_prefixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004475 else
4476 /* byte node: use the byte value and the child pointer */
4477 n = np->wn_byte + ((long_u)np->wn_child << 8);
4478 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004479 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004480
4481 /* Avoid NUL bytes, it terminates the hash key. */
4482 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004483 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004484 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004485 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004486 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004487 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004488 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004489 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
4490 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004491
4492 return compressed;
4493}
4494
4495/*
4496 * Return TRUE when two nodes have identical siblings and children.
4497 */
4498 static int
4499node_equal(n1, n2)
4500 wordnode_T *n1;
4501 wordnode_T *n2;
4502{
4503 wordnode_T *p1;
4504 wordnode_T *p2;
4505
4506 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
4507 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
4508 if (p1->wn_byte != p2->wn_byte
4509 || (p1->wn_byte == NUL
4510 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004511 || p1->wn_region != p2->wn_region
4512 || p1->wn_prefixID != p2->wn_prefixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004513 : (p1->wn_child != p2->wn_child)))
4514 break;
4515
4516 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004517}
4518
4519/*
4520 * Write a number to file "fd", MSB first, in "len" bytes.
4521 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004522 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004523put_bytes(fd, nr, len)
4524 FILE *fd;
4525 long_u nr;
4526 int len;
4527{
4528 int i;
4529
4530 for (i = len - 1; i >= 0; --i)
4531 putc((int)(nr >> (i * 8)), fd);
4532}
4533
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004534static int
4535#ifdef __BORLANDC__
4536_RTLENTRYF
4537#endif
4538rep_compare __ARGS((const void *s1, const void *s2));
4539
4540/*
4541 * Function given to qsort() to sort the REP items on "from" string.
4542 */
4543 static int
4544#ifdef __BORLANDC__
4545_RTLENTRYF
4546#endif
4547rep_compare(s1, s2)
4548 const void *s1;
4549 const void *s2;
4550{
4551 fromto_T *p1 = (fromto_T *)s1;
4552 fromto_T *p2 = (fromto_T *)s2;
4553
4554 return STRCMP(p1->ft_from, p2->ft_from);
4555}
4556
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004557/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004558 * Write the Vim spell file "fname".
4559 */
4560 static void
Bram Moolenaar3982c542005-06-08 21:56:31 +00004561write_vim_spell(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004562 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004563 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004564{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004565 FILE *fd;
4566 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004567 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004568 wordnode_T *tree;
4569 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004570 int i;
4571 int l;
4572 garray_T *gap;
4573 fromto_T *ftp;
4574 char_u *p;
4575 int rr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004576
Bram Moolenaarb765d632005-06-07 21:00:02 +00004577 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00004578 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004579 {
4580 EMSG2(_(e_notopen), fname);
4581 return;
4582 }
4583
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004584 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004585 * <charflagslen> <charflags>
4586 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004587 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004588 * <prefcondcnt> <prefcond> ... */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004589
4590 /* <fileID> */
4591 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
4592 EMSG(_(e_write));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004593
4594 /* write the region names if there is more than one */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004595 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004596 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004597 putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */
4598 fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2),
4599 (size_t)1, fd);
4600 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004601 }
4602 else
4603 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004604 putc(0, fd);
4605 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004606 }
4607
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004608 /*
4609 * Write the table with character flags and table for case folding.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004610 * <charflagslen> <charflags> <fcharlen> <fchars>
4611 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004612 * 'encoding'.
4613 * Also skip this for an .add.spl file, the main spell file must contain
4614 * the table (avoids that it conflicts). File is shorter too.
4615 */
4616 if (spin->si_ascii || spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004617 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004618 putc(0, fd);
4619 putc(0, fd);
4620 putc(0, fd);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004621 }
4622 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00004623 write_spell_chartab(fd);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004624
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004625
4626 if (spin->si_midword == NULL)
4627 put_bytes(fd, 0L, 2); /* <midwordlen> */
4628 else
4629 {
4630 i = STRLEN(spin->si_midword);
4631 put_bytes(fd, (long_u)i, 2); /* <midwordlen> */
4632 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
4633 }
4634
4635
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004636 /* Write the prefix conditions. */
4637 write_spell_prefcond(fd, &spin->si_prefcond);
4638
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004639 /* <SUGGEST> : <repcount> <rep> ...
4640 * <salflags> <salcount> <sal> ...
4641 * <maplen> <mapstr> */
4642
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004643 /* Sort the REP items. */
4644 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
4645 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004646
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004647 /* round 1: REP items
4648 * round 2: SAL items (unless SOFO is used) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004649 for (round = 1; round <= 2; ++round)
4650 {
4651 if (round == 1)
4652 gap = &spin->si_rep;
4653 else
4654 {
4655 gap = &spin->si_sal;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004656
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004657 i = 0;
4658 if (spin->si_followup)
4659 i |= SAL_F0LLOWUP;
4660 if (spin->si_collapse)
4661 i |= SAL_COLLAPSE;
4662 if (spin->si_rem_accents)
4663 i |= SAL_REM_ACCENTS;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004664 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
4665 i |= SAL_SOFO;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004666 putc(i, fd); /* <salflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004667 if (i & SAL_SOFO)
4668 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004669 }
4670
4671 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
4672 for (i = 0; i < gap->ga_len; ++i)
4673 {
4674 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
4675 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
4676 ftp = &((fromto_T *)gap->ga_data)[i];
4677 for (rr = 1; rr <= 2; ++rr)
4678 {
4679 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
4680 l = STRLEN(p);
4681 putc(l, fd);
4682 fwrite(p, l, (size_t)1, fd);
4683 }
4684 }
4685 }
4686
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004687 /* SOFOFROM and SOFOTO */
4688 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
4689 {
4690 put_bytes(fd, 1L, 2); /* <salcount> */
4691
4692 l = STRLEN(spin->si_sofofr);
4693 put_bytes(fd, (long_u)l, 2); /* <salfromlen> */
4694 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <salfrom> */
4695
4696 l = STRLEN(spin->si_sofoto);
4697 put_bytes(fd, (long_u)l, 2); /* <saltolen> */
4698 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <salto> */
4699 }
4700
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004701 put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */
4702 if (spin->si_map.ga_len > 0) /* <mapstr> */
4703 fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len,
4704 (size_t)1, fd);
Bram Moolenaar50cde822005-06-05 21:54:54 +00004705
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004706 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004707 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004708 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004709 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004710 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004711 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004712 if (round == 1)
4713 tree = spin->si_foldroot;
4714 else if (round == 2)
4715 tree = spin->si_keeproot;
4716 else
4717 tree = spin->si_prefroot;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004718
Bram Moolenaar0c405862005-06-22 22:26:26 +00004719 /* Clear the index and wnode fields in the tree. */
4720 clear_node(tree);
4721
Bram Moolenaar51485f02005-06-04 21:55:20 +00004722 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00004723 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00004724 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004725 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004726
Bram Moolenaar51485f02005-06-04 21:55:20 +00004727 /* number of nodes in 4 bytes */
4728 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004729 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004730
Bram Moolenaar51485f02005-06-04 21:55:20 +00004731 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004732 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004733 }
4734
Bram Moolenaar51485f02005-06-04 21:55:20 +00004735 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004736}
4737
4738/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00004739 * Clear the index and wnode fields of "node", it siblings and its
4740 * children. This is needed because they are a union with other items to save
4741 * space.
4742 */
4743 static void
4744clear_node(node)
4745 wordnode_T *node;
4746{
4747 wordnode_T *np;
4748
4749 if (node != NULL)
4750 for (np = node; np != NULL; np = np->wn_sibling)
4751 {
4752 np->wn_u1.index = 0;
4753 np->wn_u2.wnode = NULL;
4754
4755 if (np->wn_byte != NUL)
4756 clear_node(np->wn_child);
4757 }
4758}
4759
4760
4761/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004762 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004763 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00004764 * This first writes the list of possible bytes (siblings). Then for each
4765 * byte recursively write the children.
4766 *
4767 * NOTE: The code here must match the code in read_tree(), since assumptions
4768 * are made about the indexes (so that we don't have to write them in the
4769 * file).
4770 *
4771 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004772 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004773 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00004774put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004775 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004776 wordnode_T *node;
4777 int index;
4778 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004779 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004780{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004781 int newindex = index;
4782 int siblingcount = 0;
4783 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004784 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004785
Bram Moolenaar51485f02005-06-04 21:55:20 +00004786 /* If "node" is zero the tree is empty. */
4787 if (node == NULL)
4788 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004789
Bram Moolenaar51485f02005-06-04 21:55:20 +00004790 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004791 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004792
4793 /* Count the number of siblings. */
4794 for (np = node; np != NULL; np = np->wn_sibling)
4795 ++siblingcount;
4796
4797 /* Write the sibling count. */
4798 if (fd != NULL)
4799 putc(siblingcount, fd); /* <siblingcount> */
4800
4801 /* Write each sibling byte and optionally extra info. */
4802 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004803 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004804 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004805 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004806 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004807 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004808 /* For a NUL byte (end of word) write the flags etc. */
4809 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004810 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004811 /* In PREFIXTREE write the required prefixID and the
4812 * associated condition nr (stored in wn_region). */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004813 if (np->wn_flags == (char_u)-2)
4814 putc(BY_FLAGS, fd); /* <byte> rare */
4815 else
4816 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004817 putc(np->wn_prefixID, fd); /* <prefixID> */
4818 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004819 }
4820 else
4821 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004822 /* For word trees we write the flag/region items. */
4823 flags = np->wn_flags;
4824 if (regionmask != 0 && np->wn_region != regionmask)
4825 flags |= WF_REGION;
4826 if (np->wn_prefixID != 0)
4827 flags |= WF_PFX;
4828 if (flags == 0)
4829 {
4830 /* word without flags or region */
4831 putc(BY_NOFLAGS, fd); /* <byte> */
4832 }
4833 else
4834 {
4835 putc(BY_FLAGS, fd); /* <byte> */
4836 putc(flags, fd); /* <flags> */
4837 if (flags & WF_REGION)
4838 putc(np->wn_region, fd); /* <region> */
4839 if (flags & WF_PFX)
4840 putc(np->wn_prefixID, fd); /* <prefixID> */
4841 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004842 }
4843 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004844 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004845 else
4846 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00004847 if (np->wn_child->wn_u1.index != 0
4848 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004849 {
4850 /* The child is written elsewhere, write the reference. */
4851 if (fd != NULL)
4852 {
4853 putc(BY_INDEX, fd); /* <byte> */
4854 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004855 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004856 }
4857 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00004858 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004859 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004860 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004861
Bram Moolenaar51485f02005-06-04 21:55:20 +00004862 if (fd != NULL)
4863 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
4864 {
4865 EMSG(_(e_write));
4866 return 0;
4867 }
4868 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004869 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004870
4871 /* Space used in the array when reading: one for each sibling and one for
4872 * the count. */
4873 newindex += siblingcount + 1;
4874
4875 /* Recursively dump the children of each sibling. */
4876 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00004877 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
4878 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004879 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004880
4881 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004882}
4883
4884
4885/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00004886 * ":mkspell [-ascii] outfile infile ..."
4887 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004888 */
4889 void
4890ex_mkspell(eap)
4891 exarg_T *eap;
4892{
4893 int fcount;
4894 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004895 char_u *arg = eap->arg;
4896 int ascii = FALSE;
4897
4898 if (STRNCMP(arg, "-ascii", 6) == 0)
4899 {
4900 ascii = TRUE;
4901 arg = skipwhite(arg + 6);
4902 }
4903
4904 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
4905 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
4906 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004907 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004908 FreeWild(fcount, fnames);
4909 }
4910}
4911
4912/*
4913 * Create a Vim spell file from one or more word lists.
4914 * "fnames[0]" is the output file name.
4915 * "fnames[fcount - 1]" is the last input file name.
4916 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
4917 * and ".spl" is appended to make the output file name.
4918 */
4919 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004920mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004921 int fcount;
4922 char_u **fnames;
4923 int ascii; /* -ascii argument given */
4924 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004925 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004926{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004927 char_u fname[MAXPATHL];
4928 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00004929 char_u **innames;
4930 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004931 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004932 int i;
4933 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004934 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004935 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004936 spellinfo_T spin;
4937
4938 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004939 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004940 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004941 spin.si_followup = TRUE;
4942 spin.si_rem_accents = TRUE;
4943 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
4944 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
4945 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004946 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004947
Bram Moolenaarb765d632005-06-07 21:00:02 +00004948 /* default: fnames[0] is output file, following are input files */
4949 innames = &fnames[1];
4950 incount = fcount - 1;
4951
4952 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004953 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004954 len = STRLEN(fnames[0]);
4955 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
4956 {
4957 /* For ":mkspell path/en.latin1.add" output file is
4958 * "path/en.latin1.add.spl". */
4959 innames = &fnames[0];
4960 incount = 1;
4961 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
4962 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004963 else if (fcount == 1)
4964 {
4965 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
4966 innames = &fnames[0];
4967 incount = 1;
4968 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
4969 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
4970 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004971 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
4972 {
4973 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004974 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004975 }
4976 else
4977 /* Name should be language, make the file name from it. */
4978 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
4979 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
4980
4981 /* Check for .ascii.spl. */
4982 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
4983 spin.si_ascii = TRUE;
4984
4985 /* Check for .add.spl. */
4986 if (strstr((char *)gettail(wfname), ".add.") != NULL)
4987 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004988 }
4989
Bram Moolenaarb765d632005-06-07 21:00:02 +00004990 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004991 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004992 else if (vim_strchr(gettail(wfname), '_') != NULL)
4993 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004994 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004995 EMSG(_("E754: Only up to 8 regions supported"));
4996 else
4997 {
4998 /* Check for overwriting before doing things that may take a lot of
4999 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005000 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005001 {
5002 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005003 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005004 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005005 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005006 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005007 EMSG2(_(e_isadir2), wfname);
5008 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005009 }
5010
5011 /*
5012 * Init the aff and dic pointers.
5013 * Get the region names if there are more than 2 arguments.
5014 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005015 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005016 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005017 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005018
Bram Moolenaar3982c542005-06-08 21:56:31 +00005019 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005020 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005021 len = STRLEN(innames[i]);
5022 if (STRLEN(gettail(innames[i])) < 5
5023 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005024 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005025 EMSG2(_("E755: Invalid region in %s"), innames[i]);
5026 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005027 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005028 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
5029 spin.si_region_name[i * 2 + 1] =
5030 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005031 }
5032 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005033 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005034
Bram Moolenaar51485f02005-06-04 21:55:20 +00005035 spin.si_foldroot = wordtree_alloc(&spin.si_blocks);
5036 spin.si_keeproot = wordtree_alloc(&spin.si_blocks);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005037 spin.si_prefroot = wordtree_alloc(&spin.si_blocks);
5038 if (spin.si_foldroot == NULL
5039 || spin.si_keeproot == NULL
5040 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005041 {
5042 error = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005043 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005044 }
5045
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005046 /* When not producing a .add.spl file clear the character table when
5047 * we encounter one in the .aff file. This means we dump the current
5048 * one in the .spl file if the .aff file doesn't define one. That's
5049 * better than guessing the contents, the table will match a
5050 * previously loaded spell file. */
5051 if (!spin.si_add)
5052 spin.si_clear_chartab = TRUE;
5053
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005054 /*
5055 * Read all the .aff and .dic files.
5056 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00005057 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005058 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005059 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005060 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005061 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005062 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005063
Bram Moolenaarb765d632005-06-07 21:00:02 +00005064 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005065 if (mch_stat((char *)fname, &st) >= 0)
5066 {
5067 /* Read the .aff file. Will init "spin->si_conv" based on the
5068 * "SET" line. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005069 afile[i] = spell_read_aff(fname, &spin);
5070 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005071 error = TRUE;
5072 else
5073 {
5074 /* Read the .dic file and store the words in the trees. */
5075 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00005076 innames[i]);
5077 if (spell_read_dic(fname, &spin, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005078 error = TRUE;
5079 }
5080 }
5081 else
5082 {
5083 /* No .aff file, try reading the file as a word list. Store
5084 * the words in the trees. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005085 if (spell_read_wordfile(innames[i], &spin) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005086 error = TRUE;
5087 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005088
Bram Moolenaarb765d632005-06-07 21:00:02 +00005089#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005090 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005091 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005092#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005093 }
5094
Bram Moolenaar51485f02005-06-04 21:55:20 +00005095 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005096 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005097 /*
5098 * Remove the dummy NUL from the start of the tree root.
5099 */
5100 spin.si_foldroot = spin.si_foldroot->wn_sibling;
5101 spin.si_keeproot = spin.si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005102 spin.si_prefroot = spin.si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005103
5104 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005105 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005106 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005107 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005108 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005109 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005110 verbose_enter();
5111 MSG(_("Compressing word tree..."));
5112 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005113 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005114 verbose_leave();
5115 }
5116 wordtree_compress(spin.si_foldroot, &spin);
5117 wordtree_compress(spin.si_keeproot, &spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005118 wordtree_compress(spin.si_prefroot, &spin);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005119 }
5120
Bram Moolenaar51485f02005-06-04 21:55:20 +00005121 if (!error)
5122 {
5123 /*
5124 * Write the info in the spell file.
5125 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005126 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005127 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005128 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005129 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005130 smsg((char_u *)_("Writing spell file %s ..."), wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005131 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005132 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005133 verbose_leave();
5134 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00005135
Bram Moolenaar3982c542005-06-08 21:56:31 +00005136 write_vim_spell(wfname, &spin);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005137
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005138 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005139 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005140 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005141 verbose_enter();
5142 MSG(_("Done!"));
5143 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00005144 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005145 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005146 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005147 verbose_leave();
5148 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005149
Bram Moolenaarb765d632005-06-07 21:00:02 +00005150 /* If the file is loaded need to reload it. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005151 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005152 }
5153
5154 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005155 ga_clear(&spin.si_rep);
5156 ga_clear(&spin.si_sal);
5157 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005158 ga_clear(&spin.si_prefcond);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005159 vim_free(spin.si_midword);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005160 vim_free(spin.si_sofofr);
5161 vim_free(spin.si_sofoto);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005162
5163 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005164 for (i = 0; i < incount; ++i)
5165 if (afile[i] != NULL)
5166 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005167
5168 /* Free all the bits and pieces at once. */
5169 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005170 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005171}
5172
Bram Moolenaarb765d632005-06-07 21:00:02 +00005173
5174/*
5175 * ":spellgood {word}"
5176 * ":spellwrong {word}"
5177 */
5178 void
5179ex_spell(eap)
5180 exarg_T *eap;
5181{
5182 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong);
5183}
5184
5185/*
5186 * Add "word[len]" to 'spellfile' as a good or bad word.
5187 */
5188 void
5189spell_add_word(word, len, bad)
5190 char_u *word;
5191 int len;
5192 int bad;
5193{
5194 FILE *fd;
5195 buf_T *buf;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005196 int new_spf = FALSE;
5197 struct stat st;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005198
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005199 /* If 'spellfile' isn't set figure out a good default value. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005200 if (*curbuf->b_p_spf == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005201 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005202 init_spellfile();
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005203 new_spf = TRUE;
5204 }
5205
Bram Moolenaarb765d632005-06-07 21:00:02 +00005206 if (*curbuf->b_p_spf == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005207 EMSG(_("E764: 'spellfile' is not set"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005208 else
5209 {
5210 /* Check that the user isn't editing the .add file somewhere. */
5211 buf = buflist_findname_exp(curbuf->b_p_spf);
5212 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
5213 buf = NULL;
5214 if (buf != NULL && bufIsChanged(buf))
5215 EMSG(_(e_bufloaded));
5216 else
5217 {
5218 fd = mch_fopen((char *)curbuf->b_p_spf, "a");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005219 if (fd == NULL && new_spf)
5220 {
5221 /* We just initialized the 'spellfile' option and can't open
5222 * the file. We may need to create the "spell" directory
5223 * first. We already checked the runtime directory is
5224 * writable in init_spellfile(). */
5225 STRCPY(NameBuff, curbuf->b_p_spf);
5226 *gettail_sep(NameBuff) = NUL;
5227 if (mch_stat((char *)NameBuff, &st) < 0)
5228 {
5229 /* The directory doesn't exist. Try creating it and
5230 * opening the file again. */
5231 vim_mkdir(NameBuff, 0755);
5232 fd = mch_fopen((char *)curbuf->b_p_spf, "a");
5233 }
5234 }
5235
Bram Moolenaarb765d632005-06-07 21:00:02 +00005236 if (fd == NULL)
5237 EMSG2(_(e_notopen), curbuf->b_p_spf);
5238 else
5239 {
5240 if (bad)
5241 fprintf(fd, "/!%.*s\n", len, word);
5242 else
5243 fprintf(fd, "%.*s\n", len, word);
5244 fclose(fd);
5245
5246 /* Update the .add.spl file. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005247 mkspell(1, &curbuf->b_p_spf, FALSE, TRUE, TRUE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005248
5249 /* If the .add file is edited somewhere, reload it. */
5250 if (buf != NULL)
5251 buf_reload(buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005252
5253 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005254 }
5255 }
5256 }
5257}
5258
5259/*
5260 * Initialize 'spellfile' for the current buffer.
5261 */
5262 static void
5263init_spellfile()
5264{
5265 char_u buf[MAXPATHL];
5266 int l;
5267 slang_T *sl;
5268 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005269 char_u *lend;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005270
5271 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
5272 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005273 /* Find the end of the language name. Exclude the region. */
5274 for (lend = curbuf->b_p_spl; *lend != NUL
5275 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
5276 ;
5277
5278 /* Loop over all entries in 'runtimepath'. Use the first one where we
5279 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005280 rtp = p_rtp;
5281 while (*rtp != NUL)
5282 {
5283 /* Copy the path from 'runtimepath' to buf[]. */
5284 copy_option_part(&rtp, buf, MAXPATHL, ",");
5285 if (filewritable(buf) == 2)
5286 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00005287 /* Use the first language name from 'spelllang' and the
5288 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005289 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
5290 l = STRLEN(buf);
5291 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00005292 "/spell/%.*s.%s.add",
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005293 (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00005294 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
5295 ? (char_u *)"ascii" : spell_enc());
5296 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
5297 break;
5298 }
5299 }
5300 }
5301}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005302
Bram Moolenaar51485f02005-06-04 21:55:20 +00005303
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005304/*
5305 * Init the chartab used for spelling for ASCII.
5306 * EBCDIC is not supported!
5307 */
5308 static void
5309clear_spell_chartab(sp)
5310 spelltab_T *sp;
5311{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005312 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005313
5314 /* Init everything to FALSE. */
5315 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
5316 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
5317 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005318 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005319 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005320 sp->st_upper[i] = i;
5321 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005322
5323 /* We include digits. A word shouldn't start with a digit, but handling
5324 * that is done separately. */
5325 for (i = '0'; i <= '9'; ++i)
5326 sp->st_isw[i] = TRUE;
5327 for (i = 'A'; i <= 'Z'; ++i)
5328 {
5329 sp->st_isw[i] = TRUE;
5330 sp->st_isu[i] = TRUE;
5331 sp->st_fold[i] = i + 0x20;
5332 }
5333 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005334 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005335 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005336 sp->st_upper[i] = i - 0x20;
5337 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005338}
5339
5340/*
5341 * Init the chartab used for spelling. Only depends on 'encoding'.
5342 * Called once while starting up and when 'encoding' changes.
5343 * The default is to use isalpha(), but the spell file should define the word
5344 * characters to make it possible that 'encoding' differs from the current
5345 * locale.
5346 */
5347 void
5348init_spell_chartab()
5349{
5350 int i;
5351
5352 did_set_spelltab = FALSE;
5353 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005354#ifdef FEAT_MBYTE
5355 if (enc_dbcs)
5356 {
5357 /* DBCS: assume double-wide characters are word characters. */
5358 for (i = 128; i <= 255; ++i)
5359 if (MB_BYTE2LEN(i) == 2)
5360 spelltab.st_isw[i] = TRUE;
5361 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005362 else if (enc_utf8)
5363 {
5364 for (i = 128; i < 256; ++i)
5365 {
5366 spelltab.st_isu[i] = utf_isupper(i);
5367 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
5368 spelltab.st_fold[i] = utf_fold(i);
5369 spelltab.st_upper[i] = utf_toupper(i);
5370 }
5371 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005372 else
5373#endif
5374 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005375 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005376 for (i = 128; i < 256; ++i)
5377 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005378 if (MB_ISUPPER(i))
5379 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005380 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005381 spelltab.st_isu[i] = TRUE;
5382 spelltab.st_fold[i] = MB_TOLOWER(i);
5383 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005384 else if (MB_ISLOWER(i))
5385 {
5386 spelltab.st_isw[i] = TRUE;
5387 spelltab.st_upper[i] = MB_TOUPPER(i);
5388 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005389 }
5390 }
5391}
5392
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005393static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
5394static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
5395
5396/*
5397 * Set the spell character tables from strings in the affix file.
5398 */
5399 static int
5400set_spell_chartab(fol, low, upp)
5401 char_u *fol;
5402 char_u *low;
5403 char_u *upp;
5404{
5405 /* We build the new tables here first, so that we can compare with the
5406 * previous one. */
5407 spelltab_T new_st;
5408 char_u *pf = fol, *pl = low, *pu = upp;
5409 int f, l, u;
5410
5411 clear_spell_chartab(&new_st);
5412
5413 while (*pf != NUL)
5414 {
5415 if (*pl == NUL || *pu == NUL)
5416 {
5417 EMSG(_(e_affform));
5418 return FAIL;
5419 }
5420#ifdef FEAT_MBYTE
5421 f = mb_ptr2char_adv(&pf);
5422 l = mb_ptr2char_adv(&pl);
5423 u = mb_ptr2char_adv(&pu);
5424#else
5425 f = *pf++;
5426 l = *pl++;
5427 u = *pu++;
5428#endif
5429 /* Every character that appears is a word character. */
5430 if (f < 256)
5431 new_st.st_isw[f] = TRUE;
5432 if (l < 256)
5433 new_st.st_isw[l] = TRUE;
5434 if (u < 256)
5435 new_st.st_isw[u] = TRUE;
5436
5437 /* if "LOW" and "FOL" are not the same the "LOW" char needs
5438 * case-folding */
5439 if (l < 256 && l != f)
5440 {
5441 if (f >= 256)
5442 {
5443 EMSG(_(e_affrange));
5444 return FAIL;
5445 }
5446 new_st.st_fold[l] = f;
5447 }
5448
5449 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005450 * case-folding, it's upper case and the "UPP" is the upper case of
5451 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005452 if (u < 256 && u != f)
5453 {
5454 if (f >= 256)
5455 {
5456 EMSG(_(e_affrange));
5457 return FAIL;
5458 }
5459 new_st.st_fold[u] = f;
5460 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005461 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005462 }
5463 }
5464
5465 if (*pl != NUL || *pu != NUL)
5466 {
5467 EMSG(_(e_affform));
5468 return FAIL;
5469 }
5470
5471 return set_spell_finish(&new_st);
5472}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005473
5474/*
5475 * Set the spell character tables from strings in the .spl file.
5476 */
5477 static int
5478set_spell_charflags(flags, cnt, upp)
5479 char_u *flags;
5480 int cnt;
5481 char_u *upp;
5482{
5483 /* We build the new tables here first, so that we can compare with the
5484 * previous one. */
5485 spelltab_T new_st;
5486 int i;
5487 char_u *p = upp;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005488 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005489
5490 clear_spell_chartab(&new_st);
5491
5492 for (i = 0; i < cnt; ++i)
5493 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005494 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
5495 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005496
5497 if (*p == NUL)
5498 return FAIL;
5499#ifdef FEAT_MBYTE
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005500 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005501#else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005502 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005503#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005504 new_st.st_fold[i + 128] = c;
5505 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
5506 new_st.st_upper[c] = i + 128;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005507 }
5508
5509 return set_spell_finish(&new_st);
5510}
5511
5512 static int
5513set_spell_finish(new_st)
5514 spelltab_T *new_st;
5515{
5516 int i;
5517
5518 if (did_set_spelltab)
5519 {
5520 /* check that it's the same table */
5521 for (i = 0; i < 256; ++i)
5522 {
5523 if (spelltab.st_isw[i] != new_st->st_isw[i]
5524 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005525 || spelltab.st_fold[i] != new_st->st_fold[i]
5526 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005527 {
5528 EMSG(_("E763: Word characters differ between spell files"));
5529 return FAIL;
5530 }
5531 }
5532 }
5533 else
5534 {
5535 /* copy the new spelltab into the one being used */
5536 spelltab = *new_st;
5537 did_set_spelltab = TRUE;
5538 }
5539
5540 return OK;
5541}
5542
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005543/*
Bram Moolenaarea408852005-06-25 22:49:46 +00005544 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005545 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00005546 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005547 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00005548 */
5549 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005550spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00005551 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005552 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00005553{
Bram Moolenaarea408852005-06-25 22:49:46 +00005554#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005555 char_u *s;
5556 int l;
5557 int c;
5558
5559 if (has_mbyte)
5560 {
5561 l = MB_BYTE2LEN(*p);
5562 s = p;
5563 if (l == 1)
5564 {
5565 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005566 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005567 {
5568 s = p + 1; /* skip a mid-word character */
5569 l = MB_BYTE2LEN(*s);
5570 }
5571 }
5572 else
5573 {
5574 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005575 if (c < 256 ? buf->b_spell_ismw[c]
5576 : (buf->b_spell_ismw_mb != NULL
5577 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005578 {
5579 s = p + l;
5580 l = MB_BYTE2LEN(*s);
5581 }
5582 }
5583
5584 if (l > 1)
5585 return mb_get_class(s) >= 2;
5586 return spelltab.st_isw[*s];
5587 }
Bram Moolenaarea408852005-06-25 22:49:46 +00005588#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005589
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005590 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
5591}
5592
5593/*
5594 * Return TRUE if "p" points to a word character.
5595 * Unlike spell_iswordp() this doesn't check for "midword" characters.
5596 */
5597 static int
5598spell_iswordp_nmw(p)
5599 char_u *p;
5600{
5601#ifdef FEAT_MBYTE
5602 if (has_mbyte && MB_BYTE2LEN(*p) > 1)
5603 return mb_get_class(p) >= 2;
5604#endif
5605
5606 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00005607}
5608
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005609#ifdef FEAT_MBYTE
5610/*
5611 * Return TRUE if "p" points to a word character.
5612 * Wide version of spell_iswordp().
5613 */
5614 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005615spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005616 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005617 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005618{
5619 int *s;
5620
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005621 if (*p < 256 ? buf->b_spell_ismw[*p]
5622 : (buf->b_spell_ismw_mb != NULL
5623 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005624 s = p + 1;
5625 else
5626 s = p;
5627
5628 if (mb_char2len(*s) > 1)
5629 {
5630 if (enc_utf8)
5631 return utf_class(*s) >= 2;
5632 if (enc_dbcs)
5633 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
5634 return 0;
5635 }
5636 return spelltab.st_isw[*s];
5637}
5638#endif
5639
Bram Moolenaarea408852005-06-25 22:49:46 +00005640/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005641 * Write the table with prefix conditions to the .spl file.
5642 */
5643 static void
5644write_spell_prefcond(fd, gap)
5645 FILE *fd;
5646 garray_T *gap;
5647{
5648 int i;
5649 char_u *p;
5650 int len;
5651
5652 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
5653
5654 for (i = 0; i < gap->ga_len; ++i)
5655 {
5656 /* <prefcond> : <condlen> <condstr> */
5657 p = ((char_u **)gap->ga_data)[i];
5658 if (p == NULL)
5659 fputc(0, fd);
5660 else
5661 {
5662 len = STRLEN(p);
5663 fputc(len, fd);
5664 fwrite(p, (size_t)len, (size_t)1, fd);
5665 }
5666 }
5667}
5668
5669/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005670 * Write the current tables into the .spl file.
5671 * This makes sure the same characters are recognized as word characters when
5672 * generating an when using a spell file.
5673 */
5674 static void
5675write_spell_chartab(fd)
5676 FILE *fd;
5677{
5678 char_u charbuf[256 * 4];
5679 int len = 0;
5680 int flags;
5681 int i;
5682
5683 fputc(128, fd); /* <charflagslen> */
5684 for (i = 128; i < 256; ++i)
5685 {
5686 flags = 0;
5687 if (spelltab.st_isw[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005688 flags |= CF_WORD;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005689 if (spelltab.st_isu[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005690 flags |= CF_UPPER;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005691 fputc(flags, fd); /* <charflags> */
5692
Bram Moolenaarb765d632005-06-07 21:00:02 +00005693#ifdef FEAT_MBYTE
5694 if (has_mbyte)
5695 len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
5696 else
5697#endif
5698 charbuf[len++] = spelltab.st_fold[i];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005699 }
5700
5701 put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
5702 fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
5703}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005704
5705/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005706 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
5707 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005708 * When using a multi-byte 'encoding' the length may change!
5709 * Returns FAIL when something wrong.
5710 */
5711 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005712spell_casefold(str, len, buf, buflen)
5713 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005714 int len;
5715 char_u *buf;
5716 int buflen;
5717{
5718 int i;
5719
5720 if (len >= buflen)
5721 {
5722 buf[0] = NUL;
5723 return FAIL; /* result will not fit */
5724 }
5725
5726#ifdef FEAT_MBYTE
5727 if (has_mbyte)
5728 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005729 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005730 char_u *p;
5731 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005732
5733 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005734 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005735 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005736 if (outi + MB_MAXBYTES > buflen)
5737 {
5738 buf[outi] = NUL;
5739 return FAIL;
5740 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005741 c = mb_ptr2char_adv(&p);
5742 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005743 }
5744 buf[outi] = NUL;
5745 }
5746 else
5747#endif
5748 {
5749 /* Be quick for non-multibyte encodings. */
5750 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005751 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005752 buf[i] = NUL;
5753 }
5754
5755 return OK;
5756}
5757
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005758#define SPS_BEST 1
5759#define SPS_FAST 2
5760#define SPS_DOUBLE 4
5761
5762static int sps_flags = SPS_BEST;
5763
5764/*
5765 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
5766 * Sets "sps_flags".
5767 */
5768 int
5769spell_check_sps()
5770{
5771 char_u *p;
5772 char_u buf[MAXPATHL];
5773 int f;
5774
5775 sps_flags = 0;
5776
5777 for (p = p_sps; *p != NUL; )
5778 {
5779 copy_option_part(&p, buf, MAXPATHL, ",");
5780
5781 f = 0;
5782 if (STRCMP(buf, "best") == 0)
5783 f = SPS_BEST;
5784 else if (STRCMP(buf, "fast") == 0)
5785 f = SPS_FAST;
5786 else if (STRCMP(buf, "double") == 0)
5787 f = SPS_DOUBLE;
5788 else if (STRNCMP(buf, "expr:", 5) != 0
5789 && STRNCMP(buf, "file:", 5) != 0)
5790 f = -1;
5791
5792 if (f == -1 || (sps_flags != 0 && f != 0))
5793 {
5794 sps_flags = SPS_BEST;
5795 return FAIL;
5796 }
5797 if (f != 0)
5798 sps_flags = f;
5799 }
5800
5801 if (sps_flags == 0)
5802 sps_flags = SPS_BEST;
5803
5804 return OK;
5805}
5806
5807/* Remember what "z?" replaced. */
5808static char_u *repl_from = NULL;
5809static char_u *repl_to = NULL;
5810
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005811/*
5812 * "z?": Find badly spelled word under or after the cursor.
5813 * Give suggestions for the properly spelled word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005814 */
5815 void
5816spell_suggest()
5817{
5818 char_u *line;
5819 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005820 char_u wcopy[MAXWLEN + 2];
5821 char_u *p;
5822 int i;
5823 int c;
5824 suginfo_T sug;
5825 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005826 int mouse_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005827
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005828 /* Find the start of the badly spelled word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005829 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL
5830 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005831 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005832 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
5833 return;
5834
5835 /* No bad word or it starts after the cursor: use the word under the
5836 * cursor. */
5837 curwin->w_cursor = prev_cursor;
5838 line = ml_get_curline();
5839 p = line + curwin->w_cursor.col;
5840 /* Backup to before start of word. */
5841 while (p > line && SPELL_ISWORDP(p))
5842 mb_ptr_back(line, p);
5843 /* Forward to start of word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005844 while (*p != NUL && !SPELL_ISWORDP(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00005845 mb_ptr_adv(p);
5846
5847 if (!SPELL_ISWORDP(p)) /* No word found. */
5848 {
5849 beep_flush();
5850 return;
5851 }
5852 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005853 }
5854
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005855 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005856 line = ml_get_curline();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005857
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005858 /* Get the list of suggestions */
Bram Moolenaarea408852005-06-25 22:49:46 +00005859 spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005860
5861 if (sug.su_ga.ga_len == 0)
5862 MSG(_("Sorry, no suggestions"));
5863 else
5864 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005865 vim_free(repl_from);
5866 repl_from = NULL;
5867 vim_free(repl_to);
5868 repl_to = NULL;
5869
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005870 /* List the suggestions. */
5871 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005872 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005873 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
5874 sug.su_badlen, sug.su_badptr);
5875 msg_puts(IObuff);
5876 msg_clr_eos();
5877 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00005878
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005879 msg_scroll = TRUE;
5880 for (i = 0; i < sug.su_ga.ga_len; ++i)
5881 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005882 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005883
5884 /* The suggested word may replace only part of the bad word, add
5885 * the not replaced part. */
5886 STRCPY(wcopy, stp->st_word);
5887 if (sug.su_badlen > stp->st_orglen)
5888 vim_strncpy(wcopy + STRLEN(wcopy),
5889 sug.su_badptr + stp->st_orglen,
5890 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005891 vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\""), i + 1, wcopy);
5892 msg_puts(IObuff);
5893
5894 /* The word may replace more than "su_badlen". */
5895 if (sug.su_badlen < stp->st_orglen)
5896 {
5897 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
5898 stp->st_orglen, sug.su_badptr);
5899 msg_puts(IObuff);
5900 }
5901
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005902 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005903 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005904 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005905 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0c405862005-06-22 22:26:26 +00005906 vim_snprintf((char *)IObuff, IOSIZE, _(" (%s%d - %d)"),
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005907 stp->st_salscore ? "s " : "",
5908 stp->st_score, stp->st_altscore);
5909 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00005910 vim_snprintf((char *)IObuff, IOSIZE, _(" (%d)"),
5911 stp->st_score);
5912 msg_advance(30);
5913 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005914 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005915 msg_putchar('\n');
5916 }
5917
5918 /* Ask for choice. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005919 i = prompt_for_number(&mouse_used);
5920 if (mouse_used)
5921 i -= lines_left;
5922
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005923 if (i > 0 && i <= sug.su_ga.ga_len && u_save_cursor() == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005924 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005925 /* Save the from and to text for :spellrepall. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005926 stp = &SUG(sug.su_ga, i - 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005927 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
5928 repl_to = vim_strsave(stp->st_word);
5929
5930 /* Replace the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005931 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
5932 if (p != NULL)
5933 {
5934 c = sug.su_badptr - line;
5935 mch_memmove(p, line, c);
5936 STRCPY(p + c, stp->st_word);
5937 STRCAT(p, sug.su_badptr + stp->st_orglen);
5938 ml_replace(curwin->w_cursor.lnum, p, FALSE);
5939 curwin->w_cursor.col = c;
5940 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005941
5942 /* For redo we use a change-word command. */
5943 ResetRedobuff();
5944 AppendToRedobuff((char_u *)"ciw");
5945 AppendToRedobuff(stp->st_word);
5946 AppendCharToRedobuff(ESC);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005947 }
5948 }
5949 else
5950 curwin->w_cursor = prev_cursor;
5951 }
5952
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005953 spell_find_cleanup(&sug);
5954}
5955
5956/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005957 * ":spellrepall"
5958 */
5959/*ARGSUSED*/
5960 void
5961ex_spellrepall(eap)
5962 exarg_T *eap;
5963{
5964 pos_T pos = curwin->w_cursor;
5965 char_u *frompat;
5966 int addlen;
5967 char_u *line;
5968 char_u *p;
5969 int didone = FALSE;
5970 int save_ws = p_ws;
5971
5972 if (repl_from == NULL || repl_to == NULL)
5973 {
5974 EMSG(_("E752: No previous spell replacement"));
5975 return;
5976 }
5977 addlen = STRLEN(repl_to) - STRLEN(repl_from);
5978
5979 frompat = alloc(STRLEN(repl_from) + 7);
5980 if (frompat == NULL)
5981 return;
5982 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
5983 p_ws = FALSE;
5984
5985 curwin->w_cursor.lnum = 0;
5986 while (!got_int)
5987 {
5988 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
5989 || u_save_cursor() == FAIL)
5990 break;
5991
5992 /* Only replace when the right word isn't there yet. This happens
5993 * when changing "etc" to "etc.". */
5994 line = ml_get_curline();
5995 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
5996 repl_to, STRLEN(repl_to)) != 0)
5997 {
5998 p = alloc(STRLEN(line) + addlen + 1);
5999 if (p == NULL)
6000 break;
6001 mch_memmove(p, line, curwin->w_cursor.col);
6002 STRCPY(p + curwin->w_cursor.col, repl_to);
6003 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
6004 ml_replace(curwin->w_cursor.lnum, p, FALSE);
6005 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6006 didone = TRUE;
6007 }
6008 curwin->w_cursor.col += STRLEN(repl_to);
6009 }
6010
6011 p_ws = save_ws;
6012 curwin->w_cursor = pos;
6013 vim_free(frompat);
6014
6015 if (!didone)
6016 EMSG2(_("E753: Not found: %s"), repl_from);
6017}
6018
6019/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006020 * Find spell suggestions for "word". Return them in the growarray "*gap" as
6021 * a list of allocated strings.
6022 */
6023 void
6024spell_suggest_list(gap, word, maxcount)
6025 garray_T *gap;
6026 char_u *word;
6027 int maxcount; /* maximum nr of suggestions */
6028{
6029 suginfo_T sug;
6030 int i;
6031 suggest_T *stp;
6032 char_u *wcopy;
6033
Bram Moolenaarea408852005-06-25 22:49:46 +00006034 spell_find_suggest(word, &sug, maxcount, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006035
6036 /* Make room in "gap". */
6037 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
6038 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
6039 return;
6040
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006041 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006042 {
6043 stp = &SUG(sug.su_ga, i);
6044
6045 /* The suggested word may replace only part of "word", add the not
6046 * replaced part. */
6047 wcopy = alloc(STRLEN(stp->st_word)
6048 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
6049 if (wcopy == NULL)
6050 break;
6051 STRCPY(wcopy, stp->st_word);
6052 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
6053 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
6054 }
6055
6056 spell_find_cleanup(&sug);
6057}
6058
6059/*
6060 * Find spell suggestions for the word at the start of "badptr".
6061 * Return the suggestions in "su->su_ga".
6062 * The maximum number of suggestions is "maxcount".
6063 * Note: does use info for the current window.
6064 * This is based on the mechanisms of Aspell, but completely reimplemented.
6065 */
6066 static void
Bram Moolenaarea408852005-06-25 22:49:46 +00006067spell_find_suggest(badptr, su, maxcount, banbadword)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006068 char_u *badptr;
6069 suginfo_T *su;
6070 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00006071 int banbadword; /* don't include badword in suggestions */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006072{
6073 int attr;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006074 char_u buf[MAXPATHL];
6075 char_u *p;
6076 int do_combine = FALSE;
6077 char_u *sps_copy;
6078#ifdef FEAT_EVAL
6079 static int expr_busy = FALSE;
6080#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006081
6082 /*
6083 * Set the info in "*su".
6084 */
6085 vim_memset(su, 0, sizeof(suginfo_T));
6086 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
6087 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006088 if (*badptr == NUL)
6089 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006090 hash_init(&su->su_banned);
6091
6092 su->su_badptr = badptr;
6093 su->su_badlen = spell_check(curwin, su->su_badptr, &attr);
6094 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006095 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006096
6097 if (su->su_badlen >= MAXWLEN)
6098 su->su_badlen = MAXWLEN - 1; /* just in case */
6099 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
6100 (void)spell_casefold(su->su_badptr, su->su_badlen,
6101 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006102 /* get caps flags for bad word */
6103 su->su_badflags = captype(su->su_badptr, su->su_badptr + su->su_badlen);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006104
6105 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00006106 if (banbadword)
6107 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006108
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006109 /* Make a copy of 'spellsuggest', because the expression may change it. */
6110 sps_copy = vim_strsave(p_sps);
6111 if (sps_copy == NULL)
6112 return;
6113
6114 /* Loop over the items in 'spellsuggest'. */
6115 for (p = sps_copy; *p != NUL; )
6116 {
6117 copy_option_part(&p, buf, MAXPATHL, ",");
6118
6119 if (STRNCMP(buf, "expr:", 5) == 0)
6120 {
6121#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006122 /* Evaluate an expression. Skip this when called recursively,
6123 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006124 if (!expr_busy)
6125 {
6126 expr_busy = TRUE;
6127 spell_suggest_expr(su, buf + 5);
6128 expr_busy = FALSE;
6129 }
6130#endif
6131 }
6132 else if (STRNCMP(buf, "file:", 5) == 0)
6133 /* Use list of suggestions in a file. */
6134 spell_suggest_file(su, buf + 5);
6135 else
6136 {
6137 /* Use internal method. */
6138 spell_suggest_intern(su);
6139 if (sps_flags & SPS_DOUBLE)
6140 do_combine = TRUE;
6141 }
6142 }
6143
6144 vim_free(sps_copy);
6145
6146 if (do_combine)
6147 /* Combine the two list of suggestions. This must be done last,
6148 * because sorting changes the order again. */
6149 score_combine(su);
6150}
6151
6152#ifdef FEAT_EVAL
6153/*
6154 * Find suggestions by evaluating expression "expr".
6155 */
6156 static void
6157spell_suggest_expr(su, expr)
6158 suginfo_T *su;
6159 char_u *expr;
6160{
6161 list_T *list;
6162 listitem_T *li;
6163 int score;
6164 char_u *p;
6165
6166 /* The work is split up in a few parts to avoid having to export
6167 * suginfo_T.
6168 * First evaluate the expression and get the resulting list. */
6169 list = eval_spell_expr(su->su_badword, expr);
6170 if (list != NULL)
6171 {
6172 /* Loop over the items in the list. */
6173 for (li = list->lv_first; li != NULL; li = li->li_next)
6174 if (li->li_tv.v_type == VAR_LIST)
6175 {
6176 /* Get the word and the score from the items. */
6177 score = get_spellword(li->li_tv.vval.v_list, &p);
6178 if (score >= 0)
6179 add_suggestion(su, &su->su_ga, p,
6180 su->su_badlen, score, 0, TRUE);
6181 }
6182 list_unref(list);
6183 }
6184
6185 /* Sort the suggestions and truncate at "maxcount". */
6186 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
6187}
6188#endif
6189
6190/*
6191 * Find suggestions a file "fname".
6192 */
6193 static void
6194spell_suggest_file(su, fname)
6195 suginfo_T *su;
6196 char_u *fname;
6197{
6198 FILE *fd;
6199 char_u line[MAXWLEN * 2];
6200 char_u *p;
6201 int len;
6202 char_u cword[MAXWLEN];
6203
6204 /* Open the file. */
6205 fd = mch_fopen((char *)fname, "r");
6206 if (fd == NULL)
6207 {
6208 EMSG2(_(e_notopen), fname);
6209 return;
6210 }
6211
6212 /* Read it line by line. */
6213 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
6214 {
6215 line_breakcheck();
6216
6217 p = vim_strchr(line, '/');
6218 if (p == NULL)
6219 continue; /* No Tab found, just skip the line. */
6220 *p++ = NUL;
6221 if (STRICMP(su->su_badword, line) == 0)
6222 {
6223 /* Match! Isolate the good word, until CR or NL. */
6224 for (len = 0; p[len] >= ' '; ++len)
6225 ;
6226 p[len] = NUL;
6227
6228 /* If the suggestion doesn't have specific case duplicate the case
6229 * of the bad word. */
6230 if (captype(p, NULL) == 0)
6231 {
6232 make_case_word(p, cword, su->su_badflags);
6233 p = cword;
6234 }
6235
6236 add_suggestion(su, &su->su_ga, p, su->su_badlen,
6237 SCORE_FILE, 0, TRUE);
6238 }
6239 }
6240
6241 fclose(fd);
6242
6243 /* Sort the suggestions and truncate at "maxcount". */
6244 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
6245}
6246
6247/*
6248 * Find suggestions for the internal method indicated by "sps_flags".
6249 */
6250 static void
6251spell_suggest_intern(su)
6252 suginfo_T *su;
6253{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006254 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006255 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006256 *
6257 * Set a maximum score to limit the combination of operations that is
6258 * tried.
6259 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006260 suggest_try_special(su);
6261
6262 /*
6263 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
6264 * from the .aff file and inserting a space (split the word).
6265 */
6266 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006267
6268 /* For the resulting top-scorers compute the sound-a-like score. */
6269 if (sps_flags & SPS_DOUBLE)
6270 score_comp_sal(su);
6271
6272 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006273 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006274 *
6275 * Only do this when we don't have a lot of suggestions yet, because it's
6276 * very slow and often doesn't find new suggestions.
6277 */
6278 if ((sps_flags & SPS_DOUBLE)
6279 || (!(sps_flags & SPS_FAST)
6280 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
6281 {
6282 /* Allow a higher score now. */
6283 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006284 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006285 }
6286
6287 /* When CTRL-C was hit while searching do show the results. */
6288 ui_breakcheck();
6289 if (got_int)
6290 {
6291 (void)vgetc();
6292 got_int = FALSE;
6293 }
6294
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006295 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006296 {
6297 if (sps_flags & SPS_BEST)
6298 /* Adjust the word score for how it sounds like. */
6299 rescore_suggestions(su);
6300
6301 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006302 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006303 }
6304}
6305
6306/*
6307 * Free the info put in "*su" by spell_find_suggest().
6308 */
6309 static void
6310spell_find_cleanup(su)
6311 suginfo_T *su;
6312{
6313 int i;
6314
6315 /* Free the suggestions. */
6316 for (i = 0; i < su->su_ga.ga_len; ++i)
6317 vim_free(SUG(su->su_ga, i).st_word);
6318 ga_clear(&su->su_ga);
6319 for (i = 0; i < su->su_sga.ga_len; ++i)
6320 vim_free(SUG(su->su_sga, i).st_word);
6321 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006322
6323 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006324 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006325}
6326
6327/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006328 * Make a copy of "word", with the first letter upper or lower cased, to
6329 * "wcopy[MAXWLEN]". "word" must not be empty.
6330 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006331 */
6332 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006333onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006334 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006335 char_u *wcopy;
6336 int upper; /* TRUE: first letter made upper case */
6337{
6338 char_u *p;
6339 int c;
6340 int l;
6341
6342 p = word;
6343#ifdef FEAT_MBYTE
6344 if (has_mbyte)
6345 c = mb_ptr2char_adv(&p);
6346 else
6347#endif
6348 c = *p++;
6349 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006350 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006351 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006352 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006353#ifdef FEAT_MBYTE
6354 if (has_mbyte)
6355 l = mb_char2bytes(c, wcopy);
6356 else
6357#endif
6358 {
6359 l = 1;
6360 wcopy[0] = c;
6361 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006362 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006363}
6364
6365/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006366 * Make a copy of "word" with all the letters upper cased into
6367 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006368 */
6369 static void
6370allcap_copy(word, wcopy)
6371 char_u *word;
6372 char_u *wcopy;
6373{
6374 char_u *s;
6375 char_u *d;
6376 int c;
6377
6378 d = wcopy;
6379 for (s = word; *s != NUL; )
6380 {
6381#ifdef FEAT_MBYTE
6382 if (has_mbyte)
6383 c = mb_ptr2char_adv(&s);
6384 else
6385#endif
6386 c = *s++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006387 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006388
6389#ifdef FEAT_MBYTE
6390 if (has_mbyte)
6391 {
6392 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
6393 break;
6394 d += mb_char2bytes(c, d);
6395 }
6396 else
6397#endif
6398 {
6399 if (d - wcopy >= MAXWLEN - 1)
6400 break;
6401 *d++ = c;
6402 }
6403 }
6404 *d = NUL;
6405}
6406
6407/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006408 * Try finding suggestions by recognizing specific situations.
6409 */
6410 static void
6411suggest_try_special(su)
6412 suginfo_T *su;
6413{
6414 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006415 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006416 int c;
6417 char_u word[MAXWLEN];
6418
6419 /*
6420 * Recognize a word that is repeated: "the the".
6421 */
6422 p = skiptowhite(su->su_fbadword);
6423 len = p - su->su_fbadword;
6424 p = skipwhite(p);
6425 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
6426 {
6427 /* Include badflags: if the badword is onecap or allcap
6428 * use that for the goodword too: "The the" -> "The". */
6429 c = su->su_fbadword[len];
6430 su->su_fbadword[len] = NUL;
6431 make_case_word(su->su_fbadword, word, su->su_badflags);
6432 su->su_fbadword[len] = c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006433 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006434 }
6435}
6436
6437/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006438 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00006439 *
6440 * This uses a state machine. At each node in the tree we try various
6441 * operations. When trying if an operation work "depth" is increased and the
6442 * stack[] is used to store info. This allows combinations, thus insert one
6443 * character, replace one and delete another. The number of changes is
6444 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006445 */
6446 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00006447suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006448 suginfo_T *su;
6449{
6450 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
6451 char_u tword[MAXWLEN]; /* good word collected so far */
6452 trystate_T stack[MAXWLEN];
6453 char_u preword[MAXWLEN * 3]; /* word found with proper case (appended
6454 * to for word split) */
6455 char_u prewordlen = 0; /* length of word in "preword" */
6456 int splitoff = 0; /* index in tword after last split */
6457 trystate_T *sp;
6458 int newscore;
6459 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006460 char_u *byts, *fbyts, *pbyts;
6461 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006462 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00006463 int c, c2, c3;
6464 int n = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006465 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006466 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006467 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006468 int len;
6469 char_u *p;
6470 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00006471 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006472 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006473
6474 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00006475 * to find matches (esp. REP items). Append some more text, changing
6476 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006477 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006478 n = STRLEN(fword);
6479 p = su->su_badptr + su->su_badlen;
6480 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006481
6482 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
6483 lp->lp_slang != NULL; ++lp)
6484 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006485 /*
6486 * Go through the whole case-fold tree, try changes at each node.
6487 * "tword[]" contains the word collected from nodes in the tree.
6488 * "fword[]" the word we are trying to match with (initially the bad
6489 * word).
6490 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006491 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006492 sp = &stack[0];
6493 sp->ts_state = STATE_START;
6494 sp->ts_score = 0;
6495 sp->ts_curi = 1;
6496 sp->ts_fidx = 0;
6497 sp->ts_fidxtry = 0;
6498 sp->ts_twordlen = 0;
6499 sp->ts_arridx = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00006500#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006501 sp->ts_tcharlen = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00006502#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006503
Bram Moolenaarea424162005-06-16 21:51:00 +00006504 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006505 * When there are postponed prefixes we need to use these first. At
6506 * the end of the prefix we continue in the case-fold tree.
6507 */
6508 fbyts = lp->lp_slang->sl_fbyts;
6509 fidxs = lp->lp_slang->sl_fidxs;
6510 pbyts = lp->lp_slang->sl_pbyts;
6511 pidxs = lp->lp_slang->sl_pidxs;
6512 if (pbyts != NULL)
6513 {
6514 byts = pbyts;
6515 idxs = pidxs;
6516 sp->ts_prefixdepth = PREFIXTREE;
6517 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
6518 }
6519 else
6520 {
6521 byts = fbyts;
6522 idxs = fidxs;
6523 sp->ts_prefixdepth = NOPREFIX;
6524 }
6525
6526 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00006527 * Loop to find all suggestions. At each round we either:
6528 * - For the current state try one operation, advance "ts_curi",
6529 * increase "depth".
6530 * - When a state is done go to the next, set "ts_state".
6531 * - When all states are tried decrease "depth".
6532 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006533 while (depth >= 0 && !got_int)
6534 {
6535 sp = &stack[depth];
6536 switch (sp->ts_state)
6537 {
6538 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006539 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006540 /*
6541 * Start of node: Deal with NUL bytes, which means
6542 * tword[] may end here.
6543 */
6544 arridx = sp->ts_arridx; /* current node in the tree */
6545 len = byts[arridx]; /* bytes in this node */
6546 arridx += sp->ts_curi; /* index of current byte */
6547
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006548 if (sp->ts_prefixdepth == PREFIXTREE)
6549 {
6550 /* Skip over the NUL bytes, we use them later. */
6551 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
6552 ;
6553 sp->ts_curi += n;
6554
6555 /* At end of a prefix or at start of prefixtree: check for
6556 * following word. */
6557 if (byts[arridx] == 0 || sp->ts_state == STATE_NOPREFIX)
6558 {
6559 sp->ts_state = STATE_START;
6560 ++depth;
6561 stack[depth] = stack[depth - 1];
6562 sp = &stack[depth];
6563 sp->ts_prefixdepth = depth - 1;
6564 byts = fbyts;
6565 idxs = fidxs;
6566 sp->ts_state = STATE_START;
6567 sp->ts_curi = 1; /* start just after length byte */
6568 sp->ts_arridx = 0;
6569
6570 /* Move the prefix to preword[] so that
6571 * find_keepcap_word() works. */
6572 prewordlen = splitoff = sp->ts_twordlen;
6573 mch_memmove(preword, tword, splitoff);
6574 break;
6575 }
6576
6577 /* Always past NUL bytes now. */
6578 sp->ts_state = STATE_ENDNUL;
6579 break;
6580 }
6581
Bram Moolenaar0c405862005-06-22 22:26:26 +00006582 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006583 {
6584 /* Past bytes in node and/or past NUL bytes. */
6585 sp->ts_state = STATE_ENDNUL;
6586 break;
6587 }
6588
6589 /*
6590 * End of word in tree.
6591 */
6592 ++sp->ts_curi; /* eat one NUL byte */
6593
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006594 flags = (int)idxs[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006595
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006596 if (sp->ts_prefixdepth < MAXWLEN)
6597 {
6598 /* There was a prefix before the word. Check that the
6599 * prefix can be used with this word. */
6600 /* Count the length of the NULs in the prefix. If there
6601 * are none this must be the first try without a prefix.
6602 */
6603 n = stack[sp->ts_prefixdepth].ts_arridx;
6604 len = pbyts[n++];
6605 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
6606 ;
6607 if (c > 0)
6608 {
6609 /* The prefix ID is stored two bytes above the flags. */
6610 c = valid_word_prefix(c, n, (unsigned)flags >> 16,
6611 tword + splitoff, lp->lp_slang);
6612 if (c == 0)
6613 break;
6614
6615 /* Use the WF_RARE flag for a rare prefix. */
6616 if (c & WF_RAREPFX)
6617 flags |= WF_RARE;
6618 }
6619 }
6620
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006621 /*
6622 * Form the word with proper case in preword.
6623 * If there is a word from a previous split, append.
6624 */
6625 tword[sp->ts_twordlen] = NUL;
6626 if (flags & WF_KEEPCAP)
6627 /* Must find the word in the keep-case tree. */
6628 find_keepcap_word(lp->lp_slang, tword + splitoff,
6629 preword + prewordlen);
6630 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00006631 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006632 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00006633 * use that for the goodword too. But if the badword is
6634 * allcap and it's only one char long use onecap. */
6635 c = su->su_badflags;
6636 if ((c & WF_ALLCAP)
6637#ifdef FEAT_MBYTE
6638 && su->su_badlen == mb_ptr2len_check(su->su_badptr)
6639#else
6640 && su->su_badlen == 1
6641#endif
6642 )
6643 c = WF_ONECAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006644 make_case_word(tword + splitoff,
Bram Moolenaar0c405862005-06-22 22:26:26 +00006645 preword + prewordlen, flags | c);
6646 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006647
6648 /* Don't use a banned word. It may appear again as a good
6649 * word, thus remember it. */
6650 if (flags & WF_BANNED)
6651 {
6652 add_banned(su, preword + prewordlen);
6653 break;
6654 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006655 if (was_banned(su, preword + prewordlen)
6656 || was_banned(su, preword))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006657 break;
6658
6659 newscore = 0;
6660 if ((flags & WF_REGION)
6661 && (((unsigned)flags >> 8) & lp->lp_region) == 0)
6662 newscore += SCORE_REGION;
6663 if (flags & WF_RARE)
6664 newscore += SCORE_RARE;
6665
Bram Moolenaar0c405862005-06-22 22:26:26 +00006666 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006667 captype(preword + prewordlen, NULL)))
6668 newscore += SCORE_ICASE;
6669
Bram Moolenaar0c405862005-06-22 22:26:26 +00006670 if ((fword[sp->ts_fidx] == NUL
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006671 || !spell_iswordp(fword + sp->ts_fidx, curbuf))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006672 && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006673 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006674 /* The badword also ends: add suggestions. Give a penalty
6675 * when changing non-word char to word char, e.g., "thes,"
6676 * -> "these". */
6677 p = fword + sp->ts_fidx;
6678#ifdef FEAT_MBYTE
6679 if (has_mbyte)
6680 mb_ptr_back(fword, p);
6681 else
6682#endif
6683 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006684 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006685 {
6686 p = preword + STRLEN(preword);
6687#ifdef FEAT_MBYTE
6688 if (has_mbyte)
6689 mb_ptr_back(preword, p);
6690 else
6691#endif
6692 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006693 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006694 newscore += SCORE_NONWORD;
6695 }
6696
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006697 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00006698 sp->ts_fidx - repextra,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006699 sp->ts_score + newscore, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006700 }
Bram Moolenaarea424162005-06-16 21:51:00 +00006701 else if (sp->ts_fidx >= sp->ts_fidxtry
6702#ifdef FEAT_MBYTE
6703 /* Don't split halfway a character. */
6704 && (!has_mbyte || sp->ts_tcharlen == 0)
6705#endif
6706 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006707 {
6708 /* The word in the tree ends but the badword
6709 * continues: try inserting a space and check that a valid
6710 * words starts at fword[sp->ts_fidx]. */
6711 if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT))
6712 {
6713 /* Save things to be restored at STATE_SPLITUNDO. */
6714 sp->ts_save_prewordlen = prewordlen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006715 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006716 sp->ts_save_splitoff = splitoff;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006717 sp->ts_state = STATE_SPLITUNDO;
6718
6719 ++depth;
6720 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006721
6722 /* Append a space to preword. */
6723 STRCAT(preword, " ");
6724 prewordlen = STRLEN(preword);
6725 splitoff = sp->ts_twordlen;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006726
6727 /* If the badword has a non-word character at this
6728 * position skip it. That means replacing the
6729 * non-word character with a space. */
6730 if (!spell_iswordp_nmw(fword + sp->ts_fidx))
6731 {
6732 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
6733#ifdef FEAT_MBYTE
6734 if (has_mbyte)
6735 sp->ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
6736 else
6737#endif
6738 ++sp->ts_fidx;
6739 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006740#ifdef FEAT_MBYTE
6741 if (has_mbyte)
6742 {
6743 int i = 0;
6744
6745 /* Case-folding may change the number of bytes:
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006746 * Count nr of chars in fword[ts_fidx] and
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006747 * advance that many chars in su->su_badptr. */
6748 for (p = fword; p < fword + sp->ts_fidx;
6749 mb_ptr_adv(p))
6750 ++i;
6751 for (p = su->su_badptr; i > 0; mb_ptr_adv(p))
6752 --i;
6753 }
6754 else
6755#endif
6756 p = su->su_badptr + sp->ts_fidx;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006757 su->su_badflags = captype(p, su->su_badptr
6758 + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006759
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006760 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006761 sp->ts_arridx = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006762 }
6763 }
6764 break;
6765
6766 case STATE_SPLITUNDO:
Bram Moolenaar0c405862005-06-22 22:26:26 +00006767 /* Undo the changes done for word split. */
6768 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006769 splitoff = sp->ts_save_splitoff;
6770 prewordlen = sp->ts_save_prewordlen;
6771
6772 /* Continue looking for NUL bytes. */
6773 sp->ts_state = STATE_START;
6774 break;
6775
6776 case STATE_ENDNUL:
6777 /* Past the NUL bytes in the node. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006778 if (fword[sp->ts_fidx] == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006779 {
6780 /* The badword ends, can't use the bytes in this node. */
6781 sp->ts_state = STATE_DEL;
6782 break;
6783 }
6784 sp->ts_state = STATE_PLAIN;
6785 /*FALLTHROUGH*/
6786
6787 case STATE_PLAIN:
6788 /*
6789 * Go over all possible bytes at this node, add each to
6790 * tword[] and use child node. "ts_curi" is the index.
6791 */
6792 arridx = sp->ts_arridx;
6793 if (sp->ts_curi > byts[arridx])
6794 {
6795 /* Done all bytes at this node, do next state. When still
6796 * at already changed bytes skip the other tricks. */
6797 if (sp->ts_fidx >= sp->ts_fidxtry)
6798 sp->ts_state = STATE_DEL;
6799 else
6800 sp->ts_state = STATE_FINAL;
6801 }
6802 else
6803 {
6804 arridx += sp->ts_curi++;
6805 c = byts[arridx];
6806
6807 /* Normal byte, go one level deeper. If it's not equal to
6808 * the byte in the bad word adjust the score. But don't
6809 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00006810 if (c == fword[sp->ts_fidx]
6811#ifdef FEAT_MBYTE
6812 || (sp->ts_tcharlen > 0
6813 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006814#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00006815 )
6816 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006817 else
6818 newscore = SCORE_SUBST;
6819 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
6820 && try_deeper(su, stack, depth, newscore))
6821 {
6822 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00006823 sp = &stack[depth];
6824 ++sp->ts_fidx;
6825 tword[sp->ts_twordlen++] = c;
6826 sp->ts_arridx = idxs[arridx];
6827#ifdef FEAT_MBYTE
6828 if (newscore == SCORE_SUBST)
6829 sp->ts_isdiff = DIFF_YES;
6830 if (has_mbyte)
6831 {
6832 /* Multi-byte characters are a bit complicated to
6833 * handle: They differ when any of the bytes
6834 * differ and then their length may also differ. */
6835 if (sp->ts_tcharlen == 0)
6836 {
6837 /* First byte. */
6838 sp->ts_tcharidx = 0;
6839 sp->ts_tcharlen = MB_BYTE2LEN(c);
6840 sp->ts_fcharstart = sp->ts_fidx - 1;
6841 sp->ts_isdiff = (newscore != 0)
6842 ? DIFF_YES : DIFF_NONE;
6843 }
6844 else if (sp->ts_isdiff == DIFF_INSERT)
6845 /* When inserting trail bytes don't advance in
6846 * the bad word. */
6847 --sp->ts_fidx;
6848 if (++sp->ts_tcharidx == sp->ts_tcharlen)
6849 {
6850 /* Last byte of character. */
6851 if (sp->ts_isdiff == DIFF_YES)
6852 {
6853 /* Correct ts_fidx for the byte length of
6854 * the character (we didn't check that
6855 * before). */
6856 sp->ts_fidx = sp->ts_fcharstart
6857 + MB_BYTE2LEN(
6858 fword[sp->ts_fcharstart]);
6859
6860 /* For a similar character adjust score
6861 * from SCORE_SUBST to SCORE_SIMILAR. */
6862 if (lp->lp_slang->sl_has_map
6863 && similar_chars(lp->lp_slang,
6864 mb_ptr2char(tword
6865 + sp->ts_twordlen
6866 - sp->ts_tcharlen),
6867 mb_ptr2char(fword
6868 + sp->ts_fcharstart)))
6869 sp->ts_score -=
6870 SCORE_SUBST - SCORE_SIMILAR;
6871 }
Bram Moolenaarea408852005-06-25 22:49:46 +00006872 else if (sp->ts_isdiff == DIFF_INSERT
6873 && sp->ts_twordlen > sp->ts_tcharlen)
6874 {
6875 /* If the previous character was the same,
6876 * thus doubling a character, give a bonus
6877 * to the score. */
6878 p = tword + sp->ts_twordlen
6879 - sp->ts_tcharlen;
6880 c = mb_ptr2char(p);
6881 mb_ptr_back(tword, p);
6882 if (c == mb_ptr2char(p))
6883 sp->ts_score -= SCORE_INS
6884 - SCORE_INSDUP;
6885 }
Bram Moolenaarea424162005-06-16 21:51:00 +00006886
6887 /* Starting a new char, reset the length. */
6888 sp->ts_tcharlen = 0;
6889 }
6890 }
6891 else
6892#endif
6893 {
6894 /* If we found a similar char adjust the score.
6895 * We do this after calling try_deeper() because
6896 * it's slow. */
6897 if (newscore != 0
6898 && lp->lp_slang->sl_has_map
6899 && similar_chars(lp->lp_slang,
6900 c, fword[sp->ts_fidx - 1]))
6901 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
6902 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006903 }
6904 }
6905 break;
6906
6907 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00006908#ifdef FEAT_MBYTE
6909 /* When past the first byte of a multi-byte char don't try
6910 * delete/insert/swap a character. */
6911 if (has_mbyte && sp->ts_tcharlen > 0)
6912 {
6913 sp->ts_state = STATE_FINAL;
6914 break;
6915 }
6916#endif
6917 /*
6918 * Try skipping one character in the bad word (delete it).
6919 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006920 sp->ts_state = STATE_INS;
6921 sp->ts_curi = 1;
6922 if (fword[sp->ts_fidx] != NUL
6923 && try_deeper(su, stack, depth, SCORE_DEL))
6924 {
6925 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00006926
6927 /* Advance over the character in fword[]. Give a bonus to
6928 * the score if the same character is following "nn" ->
6929 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00006930#ifdef FEAT_MBYTE
6931 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00006932 {
6933 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00006934 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaarea408852005-06-25 22:49:46 +00006935 if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
6936 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
6937 }
Bram Moolenaarea424162005-06-16 21:51:00 +00006938 else
6939#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00006940 {
Bram Moolenaarea424162005-06-16 21:51:00 +00006941 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00006942 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
6943 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
6944 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006945 break;
6946 }
6947 /*FALLTHROUGH*/
6948
6949 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00006950 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006951 * node. */
6952 n = sp->ts_arridx;
6953 if (sp->ts_curi > byts[n])
6954 {
6955 /* Done all bytes at this node, do next state. */
6956 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006957 }
6958 else
6959 {
Bram Moolenaarea424162005-06-16 21:51:00 +00006960 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006961 n += sp->ts_curi++;
6962 c = byts[n];
6963 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
6964 {
6965 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00006966 sp = &stack[depth];
6967 tword[sp->ts_twordlen++] = c;
6968 sp->ts_arridx = idxs[n];
6969#ifdef FEAT_MBYTE
6970 if (has_mbyte)
6971 {
6972 fl = MB_BYTE2LEN(c);
6973 if (fl > 1)
6974 {
6975 /* There are following bytes for the same
6976 * character. We must find all bytes before
6977 * trying delete/insert/swap/etc. */
6978 sp->ts_tcharlen = fl;
6979 sp->ts_tcharidx = 1;
6980 sp->ts_isdiff = DIFF_INSERT;
6981 }
6982 }
Bram Moolenaarea408852005-06-25 22:49:46 +00006983 else
6984 fl = 1;
6985 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00006986#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00006987 {
6988 /* If the previous character was the same, thus
6989 * doubling a character, give a bonus to the
6990 * score. */
6991 if (sp->ts_twordlen >= 2
6992 && tword[sp->ts_twordlen - 2] == c)
6993 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
6994 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006995 }
6996 }
6997 break;
6998
6999 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00007000 /*
7001 * Swap two bytes in the bad word: "12" -> "21".
7002 * We change "fword" here, it's changed back afterwards.
7003 */
7004 p = fword + sp->ts_fidx;
7005 c = *p;
7006 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007007 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007008 /* End of word, can't swap or replace. */
7009 sp->ts_state = STATE_FINAL;
7010 break;
7011 }
7012#ifdef FEAT_MBYTE
7013 if (has_mbyte)
7014 {
7015 n = mb_ptr2len_check(p);
7016 c = mb_ptr2char(p);
7017 c2 = mb_ptr2char(p + n);
7018 }
7019 else
7020#endif
7021 c2 = p[1];
7022 if (c == c2)
7023 {
7024 /* Characters are identical, swap won't do anything. */
7025 sp->ts_state = STATE_SWAP3;
7026 break;
7027 }
7028 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
7029 {
7030 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007031 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007032#ifdef FEAT_MBYTE
7033 if (has_mbyte)
7034 {
7035 fl = mb_char2len(c2);
7036 mch_memmove(p, p + n, fl);
7037 mb_char2bytes(c, p + fl);
7038 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
7039 }
7040 else
7041#endif
7042 {
7043 p[0] = c2;
7044 p[1] = c;
7045 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
7046 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007047 }
7048 else
7049 /* If this swap doesn't work then SWAP3 won't either. */
7050 sp->ts_state = STATE_REP_INI;
7051 break;
7052
Bram Moolenaarea424162005-06-16 21:51:00 +00007053 case STATE_UNSWAP:
7054 /* Undo the STATE_SWAP swap: "21" -> "12". */
7055 p = fword + sp->ts_fidx;
7056#ifdef FEAT_MBYTE
7057 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007058 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007059 n = MB_BYTE2LEN(*p);
7060 c = mb_ptr2char(p + n);
7061 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
7062 mb_char2bytes(c, p);
7063 }
7064 else
7065#endif
7066 {
7067 c = *p;
7068 *p = p[1];
7069 p[1] = c;
7070 }
7071 /*FALLTHROUGH*/
7072
7073 case STATE_SWAP3:
7074 /* Swap two bytes, skipping one: "123" -> "321". We change
7075 * "fword" here, it's changed back afterwards. */
7076 p = fword + sp->ts_fidx;
7077#ifdef FEAT_MBYTE
7078 if (has_mbyte)
7079 {
7080 n = mb_ptr2len_check(p);
7081 c = mb_ptr2char(p);
7082 fl = mb_ptr2len_check(p + n);
7083 c2 = mb_ptr2char(p + n);
7084 c3 = mb_ptr2char(p + n + fl);
7085 }
7086 else
7087#endif
7088 {
7089 c = *p;
7090 c2 = p[1];
7091 c3 = p[2];
7092 }
7093
7094 /* When characters are identical: "121" then SWAP3 result is
7095 * identical, ROT3L result is same as SWAP: "211", ROT3L
7096 * result is same as SWAP on next char: "112". Thus skip all
7097 * swapping. Also skip when c3 is NUL. */
7098 if (c == c3 || c3 == NUL)
7099 {
7100 sp->ts_state = STATE_REP_INI;
7101 break;
7102 }
7103 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7104 {
7105 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007106 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007107#ifdef FEAT_MBYTE
7108 if (has_mbyte)
7109 {
7110 tl = mb_char2len(c3);
7111 mch_memmove(p, p + n + fl, tl);
7112 mb_char2bytes(c2, p + tl);
7113 mb_char2bytes(c, p + fl + tl);
7114 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
7115 }
7116 else
7117#endif
7118 {
7119 p[0] = p[2];
7120 p[2] = c;
7121 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7122 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007123 }
7124 else
7125 sp->ts_state = STATE_REP_INI;
7126 break;
7127
Bram Moolenaarea424162005-06-16 21:51:00 +00007128 case STATE_UNSWAP3:
7129 /* Undo STATE_SWAP3: "321" -> "123" */
7130 p = fword + sp->ts_fidx;
7131#ifdef FEAT_MBYTE
7132 if (has_mbyte)
7133 {
7134 n = MB_BYTE2LEN(*p);
7135 c2 = mb_ptr2char(p + n);
7136 fl = MB_BYTE2LEN(p[n]);
7137 c = mb_ptr2char(p + n + fl);
7138 tl = MB_BYTE2LEN(p[n + fl]);
7139 mch_memmove(p + fl + tl, p, n);
7140 mb_char2bytes(c, p);
7141 mb_char2bytes(c2, p + tl);
7142 }
7143 else
7144#endif
7145 {
7146 c = *p;
7147 *p = p[2];
7148 p[2] = c;
7149 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007150
Bram Moolenaarea424162005-06-16 21:51:00 +00007151 /* Rotate three characters left: "123" -> "231". We change
7152 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007153 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7154 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007155 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007156 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007157 p = fword + sp->ts_fidx;
7158#ifdef FEAT_MBYTE
7159 if (has_mbyte)
7160 {
7161 n = mb_ptr2len_check(p);
7162 c = mb_ptr2char(p);
7163 fl = mb_ptr2len_check(p + n);
7164 fl += mb_ptr2len_check(p + n + fl);
7165 mch_memmove(p, p + n, fl);
7166 mb_char2bytes(c, p + fl);
7167 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
7168 }
7169 else
7170#endif
7171 {
7172 c = *p;
7173 *p = p[1];
7174 p[1] = p[2];
7175 p[2] = c;
7176 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7177 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007178 }
7179 else
7180 sp->ts_state = STATE_REP_INI;
7181 break;
7182
Bram Moolenaarea424162005-06-16 21:51:00 +00007183 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007184 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00007185 p = fword + sp->ts_fidx;
7186#ifdef FEAT_MBYTE
7187 if (has_mbyte)
7188 {
7189 n = MB_BYTE2LEN(*p);
7190 n += MB_BYTE2LEN(p[n]);
7191 c = mb_ptr2char(p + n);
7192 tl = MB_BYTE2LEN(p[n]);
7193 mch_memmove(p + tl, p, n);
7194 mb_char2bytes(c, p);
7195 }
7196 else
7197#endif
7198 {
7199 c = p[2];
7200 p[2] = p[1];
7201 p[1] = *p;
7202 *p = c;
7203 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007204
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007205 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00007206 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007207 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7208 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007209 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007210 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007211 p = fword + sp->ts_fidx;
7212#ifdef FEAT_MBYTE
7213 if (has_mbyte)
7214 {
7215 n = mb_ptr2len_check(p);
7216 n += mb_ptr2len_check(p + n);
7217 c = mb_ptr2char(p + n);
7218 tl = mb_ptr2len_check(p + n);
7219 mch_memmove(p + tl, p, n);
7220 mb_char2bytes(c, p);
7221 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
7222 }
7223 else
7224#endif
7225 {
7226 c = p[2];
7227 p[2] = p[1];
7228 p[1] = *p;
7229 *p = c;
7230 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7231 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007232 }
7233 else
7234 sp->ts_state = STATE_REP_INI;
7235 break;
7236
Bram Moolenaarea424162005-06-16 21:51:00 +00007237 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007238 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00007239 p = fword + sp->ts_fidx;
7240#ifdef FEAT_MBYTE
7241 if (has_mbyte)
7242 {
7243 c = mb_ptr2char(p);
7244 tl = MB_BYTE2LEN(*p);
7245 n = MB_BYTE2LEN(p[tl]);
7246 n += MB_BYTE2LEN(p[tl + n]);
7247 mch_memmove(p, p + tl, n);
7248 mb_char2bytes(c, p + n);
7249 }
7250 else
7251#endif
7252 {
7253 c = *p;
7254 *p = p[1];
7255 p[1] = p[2];
7256 p[2] = c;
7257 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007258 /*FALLTHROUGH*/
7259
7260 case STATE_REP_INI:
7261 /* Check if matching with REP items from the .aff file would
7262 * work. Quickly skip if there are no REP items or the score
7263 * is going to be too high anyway. */
7264 gap = &lp->lp_slang->sl_rep;
7265 if (gap->ga_len == 0
7266 || sp->ts_score + SCORE_REP >= su->su_maxscore)
7267 {
7268 sp->ts_state = STATE_FINAL;
7269 break;
7270 }
7271
7272 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00007273 * may match. If the index is -1 there is none. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007274 sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]];
7275 if (sp->ts_curi < 0)
7276 {
7277 sp->ts_state = STATE_FINAL;
7278 break;
7279 }
7280
7281 sp->ts_state = STATE_REP;
7282 /*FALLTHROUGH*/
7283
7284 case STATE_REP:
7285 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00007286 * match replace the characters and check if the resulting
7287 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007288 p = fword + sp->ts_fidx;
7289
7290 gap = &lp->lp_slang->sl_rep;
7291 while (sp->ts_curi < gap->ga_len)
7292 {
7293 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
7294 if (*ftp->ft_from != *p)
7295 {
7296 /* past possible matching entries */
7297 sp->ts_curi = gap->ga_len;
7298 break;
7299 }
7300 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
7301 && try_deeper(su, stack, depth, SCORE_REP))
7302 {
7303 /* Need to undo this afterwards. */
7304 sp->ts_state = STATE_REP_UNDO;
7305
7306 /* Change the "from" to the "to" string. */
7307 ++depth;
7308 fl = STRLEN(ftp->ft_from);
7309 tl = STRLEN(ftp->ft_to);
7310 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007311 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007312 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007313 repextra += tl - fl;
7314 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007315 mch_memmove(p, ftp->ft_to, tl);
7316 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +00007317#ifdef FEAT_MBYTE
7318 stack[depth].ts_tcharlen = 0;
7319#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007320 break;
7321 }
7322 }
7323
7324 if (sp->ts_curi >= gap->ga_len)
7325 /* No (more) matches. */
7326 sp->ts_state = STATE_FINAL;
7327
7328 break;
7329
7330 case STATE_REP_UNDO:
7331 /* Undo a REP replacement and continue with the next one. */
7332 ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data
7333 + sp->ts_curi - 1;
7334 fl = STRLEN(ftp->ft_from);
7335 tl = STRLEN(ftp->ft_to);
7336 p = fword + sp->ts_fidx;
7337 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007339 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007340 repextra -= tl - fl;
7341 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007342 mch_memmove(p, ftp->ft_from, fl);
7343 sp->ts_state = STATE_REP;
7344 break;
7345
7346 default:
7347 /* Did all possible states at this level, go up one level. */
7348 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007349
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007350 if (depth >= 0 && stack[depth].ts_prefixdepth == PREFIXTREE)
7351 {
7352 /* Continue in or go back to the prefix tree. */
7353 byts = pbyts;
7354 idxs = pidxs;
7355 splitoff = 0;
7356 }
7357
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007358 /* Don't check for CTRL-C too often, it takes time. */
7359 line_breakcheck();
7360 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007361 }
7362 }
7363}
7364
7365/*
7366 * Try going one level deeper in the tree.
7367 */
7368 static int
7369try_deeper(su, stack, depth, score_add)
7370 suginfo_T *su;
7371 trystate_T *stack;
7372 int depth;
7373 int score_add;
7374{
7375 int newscore;
7376
7377 /* Refuse to go deeper if the scrore is getting too big. */
7378 newscore = stack[depth].ts_score + score_add;
7379 if (newscore >= su->su_maxscore)
7380 return FALSE;
7381
Bram Moolenaarea424162005-06-16 21:51:00 +00007382 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007383 stack[depth + 1].ts_state = STATE_START;
7384 stack[depth + 1].ts_score = newscore;
7385 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007386 return TRUE;
7387}
7388
7389/*
7390 * "fword" is a good word with case folded. Find the matching keep-case
7391 * words and put it in "kword".
7392 * Theoretically there could be several keep-case words that result in the
7393 * same case-folded word, but we only find one...
7394 */
7395 static void
7396find_keepcap_word(slang, fword, kword)
7397 slang_T *slang;
7398 char_u *fword;
7399 char_u *kword;
7400{
7401 char_u uword[MAXWLEN]; /* "fword" in upper-case */
7402 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007403 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007404
7405 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007406 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007407 int round[MAXWLEN];
7408 int fwordidx[MAXWLEN];
7409 int uwordidx[MAXWLEN];
7410 int kwordlen[MAXWLEN];
7411
7412 int flen, ulen;
7413 int l;
7414 int len;
7415 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007416 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007417 char_u *p;
7418 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007419 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007420
7421 if (byts == NULL)
7422 {
7423 /* array is empty: "cannot happen" */
7424 *kword = NUL;
7425 return;
7426 }
7427
7428 /* Make an all-cap version of "fword". */
7429 allcap_copy(fword, uword);
7430
7431 /*
7432 * Each character needs to be tried both case-folded and upper-case.
7433 * All this gets very complicated if we keep in mind that changing case
7434 * may change the byte length of a multi-byte character...
7435 */
7436 depth = 0;
7437 arridx[0] = 0;
7438 round[0] = 0;
7439 fwordidx[0] = 0;
7440 uwordidx[0] = 0;
7441 kwordlen[0] = 0;
7442 while (depth >= 0)
7443 {
7444 if (fword[fwordidx[depth]] == NUL)
7445 {
7446 /* We are at the end of "fword". If the tree allows a word to end
7447 * here we have found a match. */
7448 if (byts[arridx[depth] + 1] == 0)
7449 {
7450 kword[kwordlen[depth]] = NUL;
7451 return;
7452 }
7453
7454 /* kword is getting too long, continue one level up */
7455 --depth;
7456 }
7457 else if (++round[depth] > 2)
7458 {
7459 /* tried both fold-case and upper-case character, continue one
7460 * level up */
7461 --depth;
7462 }
7463 else
7464 {
7465 /*
7466 * round[depth] == 1: Try using the folded-case character.
7467 * round[depth] == 2: Try using the upper-case character.
7468 */
7469#ifdef FEAT_MBYTE
7470 if (has_mbyte)
7471 {
7472 flen = mb_ptr2len_check(fword + fwordidx[depth]);
7473 ulen = mb_ptr2len_check(uword + uwordidx[depth]);
7474 }
7475 else
7476#endif
7477 ulen = flen = 1;
7478 if (round[depth] == 1)
7479 {
7480 p = fword + fwordidx[depth];
7481 l = flen;
7482 }
7483 else
7484 {
7485 p = uword + uwordidx[depth];
7486 l = ulen;
7487 }
7488
7489 for (tryidx = arridx[depth]; l > 0; --l)
7490 {
7491 /* Perform a binary search in the list of accepted bytes. */
7492 len = byts[tryidx++];
7493 c = *p++;
7494 lo = tryidx;
7495 hi = tryidx + len - 1;
7496 while (lo < hi)
7497 {
7498 m = (lo + hi) / 2;
7499 if (byts[m] > c)
7500 hi = m - 1;
7501 else if (byts[m] < c)
7502 lo = m + 1;
7503 else
7504 {
7505 lo = hi = m;
7506 break;
7507 }
7508 }
7509
7510 /* Stop if there is no matching byte. */
7511 if (hi < lo || byts[lo] != c)
7512 break;
7513
7514 /* Continue at the child (if there is one). */
7515 tryidx = idxs[lo];
7516 }
7517
7518 if (l == 0)
7519 {
7520 /*
7521 * Found the matching char. Copy it to "kword" and go a
7522 * level deeper.
7523 */
7524 if (round[depth] == 1)
7525 {
7526 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
7527 flen);
7528 kwordlen[depth + 1] = kwordlen[depth] + flen;
7529 }
7530 else
7531 {
7532 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
7533 ulen);
7534 kwordlen[depth + 1] = kwordlen[depth] + ulen;
7535 }
7536 fwordidx[depth + 1] = fwordidx[depth] + flen;
7537 uwordidx[depth + 1] = uwordidx[depth] + ulen;
7538
7539 ++depth;
7540 arridx[depth] = tryidx;
7541 round[depth] = 0;
7542 }
7543 }
7544 }
7545
7546 /* Didn't find it: "cannot happen". */
7547 *kword = NUL;
7548}
7549
7550/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007551 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
7552 * su->su_sga.
7553 */
7554 static void
7555score_comp_sal(su)
7556 suginfo_T *su;
7557{
7558 langp_T *lp;
7559 char_u badsound[MAXWLEN];
7560 int i;
7561 suggest_T *stp;
7562 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007563 int score;
7564
7565 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
7566 return;
7567
7568 /* Use the sound-folding of the first language that supports it. */
7569 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7570 lp->lp_slang != NULL; ++lp)
7571 if (lp->lp_slang->sl_sal.ga_len > 0)
7572 {
7573 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007574 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007575
7576 for (i = 0; i < su->su_ga.ga_len; ++i)
7577 {
7578 stp = &SUG(su->su_ga, i);
7579
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007580 /* Case-fold the suggested word, sound-fold it and compute the
7581 * sound-a-like score. */
7582 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007583 if (score < SCORE_MAXMAX)
7584 {
7585 /* Add the suggestion. */
7586 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
7587 sstp->st_word = vim_strsave(stp->st_word);
7588 if (sstp->st_word != NULL)
7589 {
7590 sstp->st_score = score;
7591 sstp->st_altscore = 0;
7592 sstp->st_orglen = stp->st_orglen;
7593 ++su->su_sga.ga_len;
7594 }
7595 }
7596 }
7597 break;
7598 }
7599}
7600
7601/*
7602 * Combine the list of suggestions in su->su_ga and su->su_sga.
7603 * They are intwined.
7604 */
7605 static void
7606score_combine(su)
7607 suginfo_T *su;
7608{
7609 int i;
7610 int j;
7611 garray_T ga;
7612 garray_T *gap;
7613 langp_T *lp;
7614 suggest_T *stp;
7615 char_u *p;
7616 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007617 int round;
7618
7619 /* Add the alternate score to su_ga. */
7620 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7621 lp->lp_slang != NULL; ++lp)
7622 {
7623 if (lp->lp_slang->sl_sal.ga_len > 0)
7624 {
7625 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007626 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007627
7628 for (i = 0; i < su->su_ga.ga_len; ++i)
7629 {
7630 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007631 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
7632 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007633 if (stp->st_altscore == SCORE_MAXMAX)
7634 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
7635 else
7636 stp->st_score = (stp->st_score * 3
7637 + stp->st_altscore) / 4;
7638 stp->st_salscore = FALSE;
7639 }
7640 break;
7641 }
7642 }
7643
7644 /* Add the alternate score to su_sga. */
7645 for (i = 0; i < su->su_sga.ga_len; ++i)
7646 {
7647 stp = &SUG(su->su_sga, i);
7648 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
7649 if (stp->st_score == SCORE_MAXMAX)
7650 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
7651 else
7652 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
7653 stp->st_salscore = TRUE;
7654 }
7655
7656 /* Sort the suggestions and truncate at "maxcount" for both lists. */
7657 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
7658 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
7659
7660 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
7661 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
7662 return;
7663
7664 stp = &SUG(ga, 0);
7665 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
7666 {
7667 /* round 1: get a suggestion from su_ga
7668 * round 2: get a suggestion from su_sga */
7669 for (round = 1; round <= 2; ++round)
7670 {
7671 gap = round == 1 ? &su->su_ga : &su->su_sga;
7672 if (i < gap->ga_len)
7673 {
7674 /* Don't add a word if it's already there. */
7675 p = SUG(*gap, i).st_word;
7676 for (j = 0; j < ga.ga_len; ++j)
7677 if (STRCMP(stp[j].st_word, p) == 0)
7678 break;
7679 if (j == ga.ga_len)
7680 stp[ga.ga_len++] = SUG(*gap, i);
7681 else
7682 vim_free(p);
7683 }
7684 }
7685 }
7686
7687 ga_clear(&su->su_ga);
7688 ga_clear(&su->su_sga);
7689
7690 /* Truncate the list to the number of suggestions that will be displayed. */
7691 if (ga.ga_len > su->su_maxcount)
7692 {
7693 for (i = su->su_maxcount; i < ga.ga_len; ++i)
7694 vim_free(stp[i].st_word);
7695 ga.ga_len = su->su_maxcount;
7696 }
7697
7698 su->su_ga = ga;
7699}
7700
7701/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007702 * For the goodword in "stp" compute the soundalike score compared to the
7703 * badword.
7704 */
7705 static int
7706stp_sal_score(stp, su, slang, badsound)
7707 suggest_T *stp;
7708 suginfo_T *su;
7709 slang_T *slang;
7710 char_u *badsound; /* sound-folded badword */
7711{
7712 char_u *p;
7713 char_u badsound2[MAXWLEN];
7714 char_u fword[MAXWLEN];
7715 char_u goodsound[MAXWLEN];
7716
7717 if (stp->st_orglen <= su->su_badlen)
7718 p = badsound;
7719 else
7720 {
7721 /* soundfold the bad word with more characters following */
7722 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
7723
7724 /* When joining two words the sound often changes a lot. E.g., "t he"
7725 * sounds like "t h" while "the" sounds like "@". Avoid that by
7726 * removing the space. Don't do it when the good word also contains a
7727 * space. */
7728 if (vim_iswhite(su->su_badptr[su->su_badlen])
7729 && *skiptowhite(stp->st_word) == NUL)
7730 for (p = fword; *(p = skiptowhite(p)) != NUL; )
7731 mch_memmove(p, p + 1, STRLEN(p));
7732
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007733 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007734 p = badsound2;
7735 }
7736
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007737 /* Sound-fold the word and compute the score for the difference. */
7738 spell_soundfold(slang, stp->st_word, FALSE, goodsound);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007739
7740 return soundalike_score(goodsound, p);
7741}
7742
7743/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007744 * Find suggestions by comparing the word in a sound-a-like form.
7745 */
7746 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00007747suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007748 suginfo_T *su;
7749{
7750 char_u salword[MAXWLEN];
7751 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007752 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007753 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007754 int curi[MAXWLEN];
7755 langp_T *lp;
7756 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007757 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007758 int depth;
7759 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007760 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007761 int round;
7762 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007763 int sound_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007764
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007765 /* Do this for all languages that support sound folding. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007766 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7767 lp->lp_slang != NULL; ++lp)
7768 {
7769 if (lp->lp_slang->sl_sal.ga_len > 0)
7770 {
7771 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007772 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007773
7774 /*
7775 * Go through the whole tree, soundfold each word and compare.
7776 * round 1: use the case-folded tree.
7777 * round 2: use the keep-case tree.
7778 */
7779 for (round = 1; round <= 2; ++round)
7780 {
7781 if (round == 1)
7782 {
7783 byts = lp->lp_slang->sl_fbyts;
7784 idxs = lp->lp_slang->sl_fidxs;
7785 }
7786 else
7787 {
7788 byts = lp->lp_slang->sl_kbyts;
7789 idxs = lp->lp_slang->sl_kidxs;
7790 }
7791
7792 depth = 0;
7793 arridx[0] = 0;
7794 curi[0] = 1;
7795 while (depth >= 0 && !got_int)
7796 {
7797 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007798 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007799 /* Done all bytes at this node, go up one level. */
7800 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007801 line_breakcheck();
7802 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007803 else
7804 {
7805 /* Do one more byte at this node. */
7806 n = arridx[depth] + curi[depth];
7807 ++curi[depth];
7808 c = byts[n];
7809 if (c == 0)
7810 {
7811 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007812 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007813 if (round == 2 || (flags & WF_KEEPCAP) == 0)
7814 {
7815 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007816 /* Sound-fold. Only in keep-case tree need to
7817 * case-fold the word. */
7818 spell_soundfold(lp->lp_slang, tword,
7819 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007820
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007821 /* Compute the edit distance between the
7822 * sound-a-like words. */
7823 sound_score = soundalike_score(salword,
7824 tsalword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007825 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007826 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007827 char_u cword[MAXWLEN];
7828 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007829 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007830
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007831 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007832 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007833 /* Need to fix case according to
7834 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007835 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007836 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007837 }
7838 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007839 p = tword;
7840
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007841 if (sps_flags & SPS_DOUBLE)
7842 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007843 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007844 sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007845 else
7846 {
7847 /* Compute the score. */
7848 score = spell_edit_score(
7849 su->su_badword, p);
7850 if (sps_flags & SPS_BEST)
7851 /* give a bonus for the good word
7852 * sounding the same as the bad
7853 * word */
7854 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007855 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007856 RESCORE(score, sound_score),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007857 sound_score, TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007858 else
7859 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007860 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007861 score + sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007862 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007863 }
7864 }
7865
7866 /* Skip over other NUL bytes. */
7867 while (byts[n + 1] == 0)
7868 {
7869 ++n;
7870 ++curi[depth];
7871 }
7872 }
7873 else
7874 {
7875 /* Normal char, go one level deeper. */
7876 tword[depth++] = c;
7877 arridx[depth] = idxs[n];
7878 curi[depth] = 1;
7879 }
7880 }
7881 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007882 }
7883 }
7884 }
7885}
7886
7887/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007888 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007889 */
7890 static void
7891make_case_word(fword, cword, flags)
7892 char_u *fword;
7893 char_u *cword;
7894 int flags;
7895{
7896 if (flags & WF_ALLCAP)
7897 /* Make it all upper-case */
7898 allcap_copy(fword, cword);
7899 else if (flags & WF_ONECAP)
7900 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007901 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007902 else
7903 /* Use goodword as-is. */
7904 STRCPY(cword, fword);
7905}
7906
Bram Moolenaarea424162005-06-16 21:51:00 +00007907/*
7908 * Use map string "map" for languages "lp".
7909 */
7910 static void
7911set_map_str(lp, map)
7912 slang_T *lp;
7913 char_u *map;
7914{
7915 char_u *p;
7916 int headc = 0;
7917 int c;
7918 int i;
7919
7920 if (*map == NUL)
7921 {
7922 lp->sl_has_map = FALSE;
7923 return;
7924 }
7925 lp->sl_has_map = TRUE;
7926
7927 /* Init the array and hash table empty. */
7928 for (i = 0; i < 256; ++i)
7929 lp->sl_map_array[i] = 0;
7930#ifdef FEAT_MBYTE
7931 hash_init(&lp->sl_map_hash);
7932#endif
7933
7934 /*
7935 * The similar characters are stored separated with slashes:
7936 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
7937 * before the same slash. For characters above 255 sl_map_hash is used.
7938 */
7939 for (p = map; *p != NUL; )
7940 {
7941#ifdef FEAT_MBYTE
7942 c = mb_ptr2char_adv(&p);
7943#else
7944 c = *p++;
7945#endif
7946 if (c == '/')
7947 headc = 0;
7948 else
7949 {
7950 if (headc == 0)
7951 headc = c;
7952
7953#ifdef FEAT_MBYTE
7954 /* Characters above 255 don't fit in sl_map_array[], put them in
7955 * the hash table. Each entry is the char, a NUL the headchar and
7956 * a NUL. */
7957 if (c >= 256)
7958 {
7959 int cl = mb_char2len(c);
7960 int headcl = mb_char2len(headc);
7961 char_u *b;
7962 hash_T hash;
7963 hashitem_T *hi;
7964
7965 b = alloc((unsigned)(cl + headcl + 2));
7966 if (b == NULL)
7967 return;
7968 mb_char2bytes(c, b);
7969 b[cl] = NUL;
7970 mb_char2bytes(headc, b + cl + 1);
7971 b[cl + 1 + headcl] = NUL;
7972 hash = hash_hash(b);
7973 hi = hash_lookup(&lp->sl_map_hash, b, hash);
7974 if (HASHITEM_EMPTY(hi))
7975 hash_add_item(&lp->sl_map_hash, hi, b, hash);
7976 else
7977 {
7978 /* This should have been checked when generating the .spl
7979 * file. */
7980 EMSG(_("E999: duplicate char in MAP entry"));
7981 vim_free(b);
7982 }
7983 }
7984 else
7985#endif
7986 lp->sl_map_array[c] = headc;
7987 }
7988 }
7989}
7990
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007991/*
7992 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
7993 * lines in the .aff file.
7994 */
7995 static int
7996similar_chars(slang, c1, c2)
7997 slang_T *slang;
7998 int c1;
7999 int c2;
8000{
Bram Moolenaarea424162005-06-16 21:51:00 +00008001 int m1, m2;
8002#ifdef FEAT_MBYTE
8003 char_u buf[MB_MAXBYTES];
8004 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008005
Bram Moolenaarea424162005-06-16 21:51:00 +00008006 if (c1 >= 256)
8007 {
8008 buf[mb_char2bytes(c1, buf)] = 0;
8009 hi = hash_find(&slang->sl_map_hash, buf);
8010 if (HASHITEM_EMPTY(hi))
8011 m1 = 0;
8012 else
8013 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8014 }
8015 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008016#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00008017 m1 = slang->sl_map_array[c1];
8018 if (m1 == 0)
8019 return FALSE;
8020
8021
8022#ifdef FEAT_MBYTE
8023 if (c2 >= 256)
8024 {
8025 buf[mb_char2bytes(c2, buf)] = 0;
8026 hi = hash_find(&slang->sl_map_hash, buf);
8027 if (HASHITEM_EMPTY(hi))
8028 m2 = 0;
8029 else
8030 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8031 }
8032 else
8033#endif
8034 m2 = slang->sl_map_array[c2];
8035
8036 return m1 == m2;
8037}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008038
8039/*
8040 * Add a suggestion to the list of suggestions.
8041 * Do not add a duplicate suggestion or suggestions with a bad score.
8042 * When "use_score" is not zero it's used, otherwise the score is computed
8043 * with spell_edit_score().
8044 */
8045 static void
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008046add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008047 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008048 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008049 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008050 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008051 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008052 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008053 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008054{
8055 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008056 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008057 char_u *p = NULL;
8058 int c = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008059
8060 /* Check that the word wasn't banned. */
8061 if (was_banned(su, goodword))
8062 return;
8063
Bram Moolenaar0c405862005-06-22 22:26:26 +00008064 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
8065 * Remove the common part from "goodword". */
8066 i = badlen - su->su_badlen;
8067 if (i > 0)
8068 {
8069 /* This assumes there was no case folding or it didn't change the
8070 * length... */
8071 p = goodword + STRLEN(goodword) - i;
8072 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
8073 {
8074 badlen = su->su_badlen;
8075 c = *p;
8076 *p = NUL;
8077 }
8078 else
8079 p = NULL;
8080 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008081 else if (i < 0)
8082 {
8083 /* When replacing part of the word check that we actually change
8084 * something. For "the the" a suggestion can be replacing the first
8085 * "the" with itself, since "the" wasn't banned. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008086 if (badlen == (int)STRLEN(goodword)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008087 && STRNCMP(su->su_badword, goodword, badlen) == 0)
8088 return;
8089 }
8090
Bram Moolenaar0c405862005-06-22 22:26:26 +00008091
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008092 if (score <= su->su_maxscore)
8093 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008094 /* Check if the word is already there. Also check the length that is
8095 * being replaced "thes," -> "these" is a different suggestion from
8096 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008097 stp = &SUG(*gap, 0);
8098 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008099 if (STRCMP(stp[i].st_word, goodword) == 0
8100 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008101 {
8102 /* Found it. Remember the lowest score. */
8103 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008104 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008105 stp[i].st_score = score;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008106 stp[i].st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008107 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008108 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008109 break;
8110 }
8111
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008112 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008113 {
8114 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008115 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008116 stp->st_word = vim_strsave(goodword);
8117 if (stp->st_word != NULL)
8118 {
8119 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008120 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008121 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008122 stp->st_orglen = badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008123 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008124
8125 /* If we have too many suggestions now, sort the list and keep
8126 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008127 if (gap->ga_len > SUG_MAX_COUNT(su))
8128 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
8129 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008130 }
8131 }
8132 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008133
8134 if (p != NULL)
8135 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008136}
8137
8138/*
8139 * Add a word to be banned.
8140 */
8141 static void
8142add_banned(su, word)
8143 suginfo_T *su;
8144 char_u *word;
8145{
8146 char_u *s = vim_strsave(word);
8147 hash_T hash;
8148 hashitem_T *hi;
8149
8150 if (s != NULL)
8151 {
8152 hash = hash_hash(s);
8153 hi = hash_lookup(&su->su_banned, s, hash);
8154 if (HASHITEM_EMPTY(hi))
8155 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008156 else
8157 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008158 }
8159}
8160
8161/*
8162 * Return TRUE if a word appears in the list of banned words.
8163 */
8164 static int
8165was_banned(su, word)
8166 suginfo_T *su;
8167 char_u *word;
8168{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008169 hashitem_T *hi = hash_find(&su->su_banned, word);
8170
8171 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008172}
8173
8174/*
8175 * Free the banned words in "su".
8176 */
8177 static void
8178free_banned(su)
8179 suginfo_T *su;
8180{
8181 int todo;
8182 hashitem_T *hi;
8183
8184 todo = su->su_banned.ht_used;
8185 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
8186 {
8187 if (!HASHITEM_EMPTY(hi))
8188 {
8189 vim_free(hi->hi_key);
8190 --todo;
8191 }
8192 }
8193 hash_clear(&su->su_banned);
8194}
8195
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008196/*
8197 * Recompute the score if sound-folding is possible. This is slow,
8198 * thus only done for the final results.
8199 */
8200 static void
8201rescore_suggestions(su)
8202 suginfo_T *su;
8203{
8204 langp_T *lp;
8205 suggest_T *stp;
8206 char_u sal_badword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008207 int i;
8208
8209 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8210 lp->lp_slang != NULL; ++lp)
8211 {
8212 if (lp->lp_slang->sl_sal.ga_len > 0)
8213 {
8214 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008215 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008216
8217 for (i = 0; i < su->su_ga.ga_len; ++i)
8218 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008219 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008220 if (!stp->st_had_bonus)
8221 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008222 stp->st_altscore = stp_sal_score(stp, su,
8223 lp->lp_slang, sal_badword);
8224 if (stp->st_altscore == SCORE_MAXMAX)
8225 stp->st_altscore = SCORE_BIG;
8226 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008227 }
8228 }
8229 break;
8230 }
8231 }
8232}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008233
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008234static int
8235#ifdef __BORLANDC__
8236_RTLENTRYF
8237#endif
8238sug_compare __ARGS((const void *s1, const void *s2));
8239
8240/*
8241 * Function given to qsort() to sort the suggestions on st_score.
8242 */
8243 static int
8244#ifdef __BORLANDC__
8245_RTLENTRYF
8246#endif
8247sug_compare(s1, s2)
8248 const void *s1;
8249 const void *s2;
8250{
8251 suggest_T *p1 = (suggest_T *)s1;
8252 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008253 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008254
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008255 if (n == 0)
8256 return p1->st_altscore - p2->st_altscore;
8257 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008258}
8259
8260/*
8261 * Cleanup the suggestions:
8262 * - Sort on score.
8263 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008264 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008265 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008266 static int
8267cleanup_suggestions(gap, maxscore, keep)
8268 garray_T *gap;
8269 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008270 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008271{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008272 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008273 int i;
8274
8275 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008276 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008277
8278 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008279 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008280 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008281 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008282 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008283 gap->ga_len = keep;
8284 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008285 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008286 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008287}
8288
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008289#if defined(FEAT_EVAL) || defined(PROTO)
8290/*
8291 * Soundfold a string, for soundfold().
8292 * Result is in allocated memory, NULL for an error.
8293 */
8294 char_u *
8295eval_soundfold(word)
8296 char_u *word;
8297{
8298 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008299 char_u sound[MAXWLEN];
8300
8301 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
8302 /* Use the sound-folding of the first language that supports it. */
8303 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8304 lp->lp_slang != NULL; ++lp)
8305 if (lp->lp_slang->sl_sal.ga_len > 0)
8306 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008307 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008308 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008309 return vim_strsave(sound);
8310 }
8311
8312 /* No language with sound folding, return word as-is. */
8313 return vim_strsave(word);
8314}
8315#endif
8316
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008317/*
8318 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
8319 */
8320 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008321spell_soundfold(slang, inword, folded, res)
8322 slang_T *slang;
8323 char_u *inword;
8324 int folded; /* "inword" is already case-folded */
8325 char_u *res;
8326{
8327 char_u fword[MAXWLEN];
8328 char_u *word;
8329
8330 if (slang->sl_sofo)
8331 /* SOFOFROM and SOFOTO used */
8332 spell_soundfold_sofo(slang, inword, res);
8333 else
8334 {
8335 /* SAL items used. Requires the word to be case-folded. */
8336 if (folded)
8337 word = inword;
8338 else
8339 {
8340 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
8341 word = fword;
8342 }
8343
8344#ifdef FEAT_MBYTE
8345 if (has_mbyte)
8346 spell_soundfold_wsal(slang, word, res);
8347 else
8348#endif
8349 spell_soundfold_sal(slang, word, res);
8350 }
8351}
8352
8353/*
8354 * Perform sound folding of "inword" into "res" according to SOFOFROM and
8355 * SOFOTO lines.
8356 */
8357 static void
8358spell_soundfold_sofo(slang, inword, res)
8359 slang_T *slang;
8360 char_u *inword;
8361 char_u *res;
8362{
8363 char_u *s;
8364 int ri = 0;
8365 int c;
8366
8367#ifdef FEAT_MBYTE
8368 if (has_mbyte)
8369 {
8370 int prevc = 0;
8371 int *ip;
8372
8373 /* The sl_sal_first[] table contains the translation for chars up to
8374 * 255, sl_sal the rest. */
8375 for (s = inword; *s != NUL; )
8376 {
8377 c = mb_ptr2char_adv(&s);
8378 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
8379 c = ' ';
8380 else if (c < 256)
8381 c = slang->sl_sal_first[c];
8382 else
8383 {
8384 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
8385 if (ip == NULL) /* empty list, can't match */
8386 c = NUL;
8387 else
8388 for (;;) /* find "c" in the list */
8389 {
8390 if (*ip == 0) /* not found */
8391 {
8392 c = NUL;
8393 break;
8394 }
8395 if (*ip == c) /* match! */
8396 {
8397 c = ip[1];
8398 break;
8399 }
8400 ip += 2;
8401 }
8402 }
8403
8404 if (c != NUL && c != prevc)
8405 {
8406 ri += mb_char2bytes(c, res + ri);
8407 if (ri + MB_MAXBYTES > MAXWLEN)
8408 break;
8409 prevc = c;
8410 }
8411 }
8412 }
8413 else
8414#endif
8415 {
8416 /* The sl_sal_first[] table contains the translation. */
8417 for (s = inword; (c = *s) != NUL; ++s)
8418 {
8419 if (vim_iswhite(c))
8420 c = ' ';
8421 else
8422 c = slang->sl_sal_first[c];
8423 if (c != NUL && (ri == 0 || res[ri - 1] != c))
8424 res[ri++] = c;
8425 }
8426 }
8427
8428 res[ri] = NUL;
8429}
8430
8431 static void
8432spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008433 slang_T *slang;
8434 char_u *inword;
8435 char_u *res;
8436{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008437 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008438 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008439 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008440 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008441 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008442 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008443 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008444 int n, k = 0;
8445 int z0;
8446 int k0;
8447 int n0;
8448 int c;
8449 int pri;
8450 int p0 = -333;
8451 int c0;
8452
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008453 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008454 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008455 if (slang->sl_rem_accents)
8456 {
8457 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008458 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008459 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008460 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008461 {
8462 *t++ = ' ';
8463 s = skipwhite(s);
8464 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008465 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008466 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008467 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008468 *t++ = *s;
8469 ++s;
8470 }
8471 }
8472 *t = NUL;
8473 }
8474 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008475 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008476
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008477 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008478
8479 /*
8480 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008481 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008482 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008483 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008484 while ((c = word[i]) != NUL)
8485 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008486 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008487 n = slang->sl_sal_first[c];
8488 z0 = 0;
8489
8490 if (n >= 0)
8491 {
8492 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008493 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008494 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008495 /* Quickly skip entries that don't match the word. Most
8496 * entries are less then three chars, optimize for that. */
8497 k = smp[n].sm_leadlen;
8498 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008499 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008500 if (word[i + 1] != s[1])
8501 continue;
8502 if (k > 2)
8503 {
8504 for (j = 2; j < k; ++j)
8505 if (word[i + j] != s[j])
8506 break;
8507 if (j < k)
8508 continue;
8509 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008510 }
8511
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008512 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008513 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008514 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008515 while (*pf != NUL && *pf != word[i + k])
8516 ++pf;
8517 if (*pf == NUL)
8518 continue;
8519 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008520 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008521 s = smp[n].sm_rules;
8522 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008523
8524 p0 = *s;
8525 k0 = k;
8526 while (*s == '-' && k > 1)
8527 {
8528 k--;
8529 s++;
8530 }
8531 if (*s == '<')
8532 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008533 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008534 {
8535 /* determine priority */
8536 pri = *s - '0';
8537 s++;
8538 }
8539 if (*s == '^' && *(s + 1) == '^')
8540 s++;
8541
8542 if (*s == NUL
8543 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008544 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008545 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008546 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008547 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008548 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008549 && spell_iswordp(word + i - 1, curbuf)
8550 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008551 {
8552 /* search for followup rules, if: */
8553 /* followup and k > 1 and NO '-' in searchstring */
8554 c0 = word[i + k - 1];
8555 n0 = slang->sl_sal_first[c0];
8556
8557 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008558 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008559 {
8560 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008561 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008562 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008563 /* Quickly skip entries that don't match the word.
8564 * */
8565 k0 = smp[n0].sm_leadlen;
8566 if (k0 > 1)
8567 {
8568 if (word[i + k] != s[1])
8569 continue;
8570 if (k0 > 2)
8571 {
8572 pf = word + i + k + 1;
8573 for (j = 2; j < k0; ++j)
8574 if (*pf++ != s[j])
8575 break;
8576 if (j < k0)
8577 continue;
8578 }
8579 }
8580 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008581
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008582 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008583 {
8584 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008585 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008586 while (*pf != NUL && *pf != word[i + k0])
8587 ++pf;
8588 if (*pf == NUL)
8589 continue;
8590 ++k0;
8591 }
8592
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008593 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008594 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008595 while (*s == '-')
8596 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008597 /* "k0" gets NOT reduced because
8598 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008599 s++;
8600 }
8601 if (*s == '<')
8602 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008603 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008604 {
8605 p0 = *s - '0';
8606 s++;
8607 }
8608
8609 if (*s == NUL
8610 /* *s == '^' cuts */
8611 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008612 && !spell_iswordp(word + i + k0,
8613 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008614 {
8615 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008616 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008617 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008618
8619 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008620 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008621 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008622 /* rule fits; stop search */
8623 break;
8624 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008625 }
8626
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008627 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008628 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008629 }
8630
8631 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008632 s = smp[n].sm_to;
8633 pf = smp[n].sm_rules;
8634 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008635 if (p0 == 1 && z == 0)
8636 {
8637 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008638 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
8639 || res[reslen - 1] == *s))
8640 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008641 z0 = 1;
8642 z = 1;
8643 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008644 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008645 {
8646 word[i + k0] = *s;
8647 k0++;
8648 s++;
8649 }
8650 if (k > k0)
8651 mch_memmove(word + i + k0, word + i + k,
8652 STRLEN(word + i + k) + 1);
8653
8654 /* new "actual letter" */
8655 c = word[i];
8656 }
8657 else
8658 {
8659 /* no '<' rule used */
8660 i += k - 1;
8661 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008662 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008663 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008664 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008665 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008666 s++;
8667 }
8668 /* new "actual letter" */
8669 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008670 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008671 {
8672 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008673 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008674 mch_memmove(word, word + i + 1,
8675 STRLEN(word + i + 1) + 1);
8676 i = 0;
8677 z0 = 1;
8678 }
8679 }
8680 break;
8681 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008682 }
8683 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008684 else if (vim_iswhite(c))
8685 {
8686 c = ' ';
8687 k = 1;
8688 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008689
8690 if (z0 == 0)
8691 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008692 if (k && !p0 && reslen < MAXWLEN && c != NUL
8693 && (!slang->sl_collapse || reslen == 0
8694 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008695 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008696 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008697
8698 i++;
8699 z = 0;
8700 k = 0;
8701 }
8702 }
8703
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008704 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008705}
8706
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008707#ifdef FEAT_MBYTE
8708/*
8709 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
8710 * Multi-byte version of spell_soundfold().
8711 */
8712 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008713spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008714 slang_T *slang;
8715 char_u *inword;
8716 char_u *res;
8717{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008718 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008719 int word[MAXWLEN];
8720 int wres[MAXWLEN];
8721 int l;
8722 char_u *s;
8723 int *ws;
8724 char_u *t;
8725 int *pf;
8726 int i, j, z;
8727 int reslen;
8728 int n, k = 0;
8729 int z0;
8730 int k0;
8731 int n0;
8732 int c;
8733 int pri;
8734 int p0 = -333;
8735 int c0;
8736 int did_white = FALSE;
8737
8738 /*
8739 * Convert the multi-byte string to a wide-character string.
8740 * Remove accents, if wanted. We actually remove all non-word characters.
8741 * But keep white space.
8742 */
8743 n = 0;
8744 for (s = inword; *s != NUL; )
8745 {
8746 t = s;
8747 c = mb_ptr2char_adv(&s);
8748 if (slang->sl_rem_accents)
8749 {
8750 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
8751 {
8752 if (did_white)
8753 continue;
8754 c = ' ';
8755 did_white = TRUE;
8756 }
8757 else
8758 {
8759 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008760 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008761 continue;
8762 }
8763 }
8764 word[n++] = c;
8765 }
8766 word[n] = NUL;
8767
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008768 /*
8769 * This comes from Aspell phonet.cpp.
8770 * Converted from C++ to C. Added support for multi-byte chars.
8771 * Changed to keep spaces.
8772 */
8773 i = reslen = z = 0;
8774 while ((c = word[i]) != NUL)
8775 {
8776 /* Start with the first rule that has the character in the word. */
8777 n = slang->sl_sal_first[c & 0xff];
8778 z0 = 0;
8779
8780 if (n >= 0)
8781 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008782 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008783 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
8784 {
8785 /* Quickly skip entries that don't match the word. Most
8786 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008787 if (c != ws[0])
8788 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008789 k = smp[n].sm_leadlen;
8790 if (k > 1)
8791 {
8792 if (word[i + 1] != ws[1])
8793 continue;
8794 if (k > 2)
8795 {
8796 for (j = 2; j < k; ++j)
8797 if (word[i + j] != ws[j])
8798 break;
8799 if (j < k)
8800 continue;
8801 }
8802 }
8803
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008804 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008805 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008806 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008807 while (*pf != NUL && *pf != word[i + k])
8808 ++pf;
8809 if (*pf == NUL)
8810 continue;
8811 ++k;
8812 }
8813 s = smp[n].sm_rules;
8814 pri = 5; /* default priority */
8815
8816 p0 = *s;
8817 k0 = k;
8818 while (*s == '-' && k > 1)
8819 {
8820 k--;
8821 s++;
8822 }
8823 if (*s == '<')
8824 s++;
8825 if (VIM_ISDIGIT(*s))
8826 {
8827 /* determine priority */
8828 pri = *s - '0';
8829 s++;
8830 }
8831 if (*s == '^' && *(s + 1) == '^')
8832 s++;
8833
8834 if (*s == NUL
8835 || (*s == '^'
8836 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008837 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008838 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008839 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008840 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008841 && spell_iswordp_w(word + i - 1, curbuf)
8842 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008843 {
8844 /* search for followup rules, if: */
8845 /* followup and k > 1 and NO '-' in searchstring */
8846 c0 = word[i + k - 1];
8847 n0 = slang->sl_sal_first[c0 & 0xff];
8848
8849 if (slang->sl_followup && k > 1 && n0 >= 0
8850 && p0 != '-' && word[i + k] != NUL)
8851 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008852 /* Test follow-up rule for "word[i + k]"; loop over
8853 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008854 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
8855 == (c0 & 0xff); ++n0)
8856 {
8857 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008858 */
8859 if (c0 != ws[0])
8860 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008861 k0 = smp[n0].sm_leadlen;
8862 if (k0 > 1)
8863 {
8864 if (word[i + k] != ws[1])
8865 continue;
8866 if (k0 > 2)
8867 {
8868 pf = word + i + k + 1;
8869 for (j = 2; j < k0; ++j)
8870 if (*pf++ != ws[j])
8871 break;
8872 if (j < k0)
8873 continue;
8874 }
8875 }
8876 k0 += k - 1;
8877
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008878 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008879 {
8880 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008881 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008882 while (*pf != NUL && *pf != word[i + k0])
8883 ++pf;
8884 if (*pf == NUL)
8885 continue;
8886 ++k0;
8887 }
8888
8889 p0 = 5;
8890 s = smp[n0].sm_rules;
8891 while (*s == '-')
8892 {
8893 /* "k0" gets NOT reduced because
8894 * "if (k0 == k)" */
8895 s++;
8896 }
8897 if (*s == '<')
8898 s++;
8899 if (VIM_ISDIGIT(*s))
8900 {
8901 p0 = *s - '0';
8902 s++;
8903 }
8904
8905 if (*s == NUL
8906 /* *s == '^' cuts */
8907 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008908 && !spell_iswordp_w(word + i + k0,
8909 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008910 {
8911 if (k0 == k)
8912 /* this is just a piece of the string */
8913 continue;
8914
8915 if (p0 < pri)
8916 /* priority too low */
8917 continue;
8918 /* rule fits; stop search */
8919 break;
8920 }
8921 }
8922
8923 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
8924 == (c0 & 0xff))
8925 continue;
8926 }
8927
8928 /* replace string */
8929 ws = smp[n].sm_to_w;
8930 s = smp[n].sm_rules;
8931 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
8932 if (p0 == 1 && z == 0)
8933 {
8934 /* rule with '<' is used */
8935 if (reslen > 0 && *ws != NUL && (wres[reslen - 1] == c
8936 || wres[reslen - 1] == *ws))
8937 reslen--;
8938 z0 = 1;
8939 z = 1;
8940 k0 = 0;
8941 while (*ws != NUL && word[i + k0] != NUL)
8942 {
8943 word[i + k0] = *ws;
8944 k0++;
8945 ws++;
8946 }
8947 if (k > k0)
8948 mch_memmove(word + i + k0, word + i + k,
8949 sizeof(int) * (STRLEN(word + i + k) + 1));
8950
8951 /* new "actual letter" */
8952 c = word[i];
8953 }
8954 else
8955 {
8956 /* no '<' rule used */
8957 i += k - 1;
8958 z = 0;
8959 while (*ws != NUL && ws[1] != NUL && reslen < MAXWLEN)
8960 {
8961 if (reslen == 0 || wres[reslen - 1] != *ws)
8962 wres[reslen++] = *ws;
8963 ws++;
8964 }
8965 /* new "actual letter" */
8966 c = *ws;
8967 if (strstr((char *)s, "^^") != NULL)
8968 {
8969 if (c != NUL)
8970 wres[reslen++] = c;
8971 mch_memmove(word, word + i + 1,
8972 sizeof(int) * (STRLEN(word + i + 1) + 1));
8973 i = 0;
8974 z0 = 1;
8975 }
8976 }
8977 break;
8978 }
8979 }
8980 }
8981 else if (vim_iswhite(c))
8982 {
8983 c = ' ';
8984 k = 1;
8985 }
8986
8987 if (z0 == 0)
8988 {
8989 if (k && !p0 && reslen < MAXWLEN && c != NUL
8990 && (!slang->sl_collapse || reslen == 0
8991 || wres[reslen - 1] != c))
8992 /* condense only double letters */
8993 wres[reslen++] = c;
8994
8995 i++;
8996 z = 0;
8997 k = 0;
8998 }
8999 }
9000
9001 /* Convert wide characters in "wres" to a multi-byte string in "res". */
9002 l = 0;
9003 for (n = 0; n < reslen; ++n)
9004 {
9005 l += mb_char2bytes(wres[n], res + l);
9006 if (l + MB_MAXBYTES > MAXWLEN)
9007 break;
9008 }
9009 res[l] = NUL;
9010}
9011#endif
9012
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009013/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009014 * Compute a score for two sound-a-like words.
9015 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
9016 * Instead of a generic loop we write out the code. That keeps it fast by
9017 * avoiding checks that will not be possible.
9018 */
9019 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009020soundalike_score(goodstart, badstart)
9021 char_u *goodstart; /* sound-folded good word */
9022 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009023{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009024 char_u *goodsound = goodstart;
9025 char_u *badsound = badstart;
9026 int goodlen;
9027 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009028 int n;
9029 char_u *pl, *ps;
9030 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009031 int score = 0;
9032
9033 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
9034 * counted so much, vowels halfway the word aren't counted at all. */
9035 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
9036 {
9037 score = SCORE_DEL / 2;
9038 if (*badsound == '*')
9039 ++badsound;
9040 else
9041 ++goodsound;
9042 }
9043
9044 goodlen = STRLEN(goodsound);
9045 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009046
9047 /* Return quickly if the lenghts are too different to be fixed by two
9048 * changes. */
9049 n = goodlen - badlen;
9050 if (n < -2 || n > 2)
9051 return SCORE_MAXMAX;
9052
9053 if (n > 0)
9054 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009055 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009056 ps = badsound;
9057 }
9058 else
9059 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009060 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009061 ps = goodsound;
9062 }
9063
9064 /* Skip over the identical part. */
9065 while (*pl == *ps && *pl != NUL)
9066 {
9067 ++pl;
9068 ++ps;
9069 }
9070
9071 switch (n)
9072 {
9073 case -2:
9074 case 2:
9075 /*
9076 * Must delete two characters from "pl".
9077 */
9078 ++pl; /* first delete */
9079 while (*pl == *ps)
9080 {
9081 ++pl;
9082 ++ps;
9083 }
9084 /* strings must be equal after second delete */
9085 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009086 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009087
9088 /* Failed to compare. */
9089 break;
9090
9091 case -1:
9092 case 1:
9093 /*
9094 * Minimal one delete from "pl" required.
9095 */
9096
9097 /* 1: delete */
9098 pl2 = pl + 1;
9099 ps2 = ps;
9100 while (*pl2 == *ps2)
9101 {
9102 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009103 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009104 ++pl2;
9105 ++ps2;
9106 }
9107
9108 /* 2: delete then swap, then rest must be equal */
9109 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9110 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009111 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009112
9113 /* 3: delete then substitute, then the rest must be equal */
9114 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009115 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009116
9117 /* 4: first swap then delete */
9118 if (pl[0] == ps[1] && pl[1] == ps[0])
9119 {
9120 pl2 = pl + 2; /* swap, skip two chars */
9121 ps2 = ps + 2;
9122 while (*pl2 == *ps2)
9123 {
9124 ++pl2;
9125 ++ps2;
9126 }
9127 /* delete a char and then strings must be equal */
9128 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009129 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009130 }
9131
9132 /* 5: first substitute then delete */
9133 pl2 = pl + 1; /* substitute, skip one char */
9134 ps2 = ps + 1;
9135 while (*pl2 == *ps2)
9136 {
9137 ++pl2;
9138 ++ps2;
9139 }
9140 /* delete a char and then strings must be equal */
9141 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009142 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009143
9144 /* Failed to compare. */
9145 break;
9146
9147 case 0:
9148 /*
9149 * Lenghts are equal, thus changes must result in same length: An
9150 * insert is only possible in combination with a delete.
9151 * 1: check if for identical strings
9152 */
9153 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009154 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009155
9156 /* 2: swap */
9157 if (pl[0] == ps[1] && pl[1] == ps[0])
9158 {
9159 pl2 = pl + 2; /* swap, skip two chars */
9160 ps2 = ps + 2;
9161 while (*pl2 == *ps2)
9162 {
9163 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009164 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009165 ++pl2;
9166 ++ps2;
9167 }
9168 /* 3: swap and swap again */
9169 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9170 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009171 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009172
9173 /* 4: swap and substitute */
9174 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009175 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009176 }
9177
9178 /* 5: substitute */
9179 pl2 = pl + 1;
9180 ps2 = ps + 1;
9181 while (*pl2 == *ps2)
9182 {
9183 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009184 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009185 ++pl2;
9186 ++ps2;
9187 }
9188
9189 /* 6: substitute and swap */
9190 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9191 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009192 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009193
9194 /* 7: substitute and substitute */
9195 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009196 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009197
9198 /* 8: insert then delete */
9199 pl2 = pl;
9200 ps2 = ps + 1;
9201 while (*pl2 == *ps2)
9202 {
9203 ++pl2;
9204 ++ps2;
9205 }
9206 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009207 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009208
9209 /* 9: delete then insert */
9210 pl2 = pl + 1;
9211 ps2 = ps;
9212 while (*pl2 == *ps2)
9213 {
9214 ++pl2;
9215 ++ps2;
9216 }
9217 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009218 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009219
9220 /* Failed to compare. */
9221 break;
9222 }
9223
9224 return SCORE_MAXMAX;
9225}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009226
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009227/*
9228 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009229 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009230 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009231 * The algorithm comes from Aspell editdist.cpp, edit_distance().
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009232 * It has been converted from C++ to C and modified to support multi-byte
9233 * characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009234 */
9235 static int
9236spell_edit_score(badword, goodword)
9237 char_u *badword;
9238 char_u *goodword;
9239{
9240 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009241 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009242 int j, i;
9243 int t;
9244 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009245 int pbc, pgc;
9246#ifdef FEAT_MBYTE
9247 char_u *p;
9248 int wbadword[MAXWLEN];
9249 int wgoodword[MAXWLEN];
9250
9251 if (has_mbyte)
9252 {
9253 /* Get the characters from the multi-byte strings and put them in an
9254 * int array for easy access. */
9255 for (p = badword, badlen = 0; *p != NUL; )
9256 wbadword[badlen++] = mb_ptr2char_adv(&p);
9257 ++badlen;
9258 for (p = goodword, goodlen = 0; *p != NUL; )
9259 wgoodword[goodlen++] = mb_ptr2char_adv(&p);
9260 ++goodlen;
9261 }
9262 else
9263#endif
9264 {
9265 badlen = STRLEN(badword) + 1;
9266 goodlen = STRLEN(goodword) + 1;
9267 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009268
9269 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
9270#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009271 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
9272 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009273 if (cnt == NULL)
9274 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009275
9276 CNT(0, 0) = 0;
9277 for (j = 1; j <= goodlen; ++j)
9278 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
9279
9280 for (i = 1; i <= badlen; ++i)
9281 {
9282 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
9283 for (j = 1; j <= goodlen; ++j)
9284 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009285#ifdef FEAT_MBYTE
9286 if (has_mbyte)
9287 {
9288 bc = wbadword[i - 1];
9289 gc = wgoodword[j - 1];
9290 }
9291 else
9292#endif
9293 {
9294 bc = badword[i - 1];
9295 gc = goodword[j - 1];
9296 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009297 if (bc == gc)
9298 CNT(i, j) = CNT(i - 1, j - 1);
9299 else
9300 {
9301 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009302 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009303 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
9304 else
9305 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
9306
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009307 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009308 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009309#ifdef FEAT_MBYTE
9310 if (has_mbyte)
9311 {
9312 pbc = wbadword[i - 2];
9313 pgc = wgoodword[j - 2];
9314 }
9315 else
9316#endif
9317 {
9318 pbc = badword[i - 2];
9319 pgc = goodword[j - 2];
9320 }
9321 if (bc == pgc && pbc == gc)
9322 {
9323 t = SCORE_SWAP + CNT(i - 2, j - 2);
9324 if (t < CNT(i, j))
9325 CNT(i, j) = t;
9326 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009327 }
9328 t = SCORE_DEL + CNT(i - 1, j);
9329 if (t < CNT(i, j))
9330 CNT(i, j) = t;
9331 t = SCORE_INS + CNT(i, j - 1);
9332 if (t < CNT(i, j))
9333 CNT(i, j) = t;
9334 }
9335 }
9336 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009337
9338 i = CNT(badlen - 1, goodlen - 1);
9339 vim_free(cnt);
9340 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009341}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009342
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009343/*
9344 * ":spelldump"
9345 */
9346/*ARGSUSED*/
9347 void
9348ex_spelldump(eap)
9349 exarg_T *eap;
9350{
9351 buf_T *buf = curbuf;
9352 langp_T *lp;
9353 slang_T *slang;
9354 idx_T arridx[MAXWLEN];
9355 int curi[MAXWLEN];
9356 char_u word[MAXWLEN];
9357 int c;
9358 char_u *byts;
9359 idx_T *idxs;
9360 linenr_T lnum = 0;
9361 int round;
9362 int depth;
9363 int n;
9364 int flags;
9365
9366 if (no_spell_checking())
9367 return;
9368
9369 /* Create a new empty buffer by splitting the window. */
9370 do_cmdline_cmd((char_u *)"new");
9371 if (!bufempty() || !buf_valid(buf))
9372 return;
9373
9374 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
9375 {
9376 slang = lp->lp_slang;
9377
9378 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
9379 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
9380
9381 /* round 1: case-folded tree
9382 * round 2: keep-case tree */
9383 for (round = 1; round <= 2; ++round)
9384 {
9385 if (round == 1)
9386 {
9387 byts = slang->sl_fbyts;
9388 idxs = slang->sl_fidxs;
9389 }
9390 else
9391 {
9392 byts = slang->sl_kbyts;
9393 idxs = slang->sl_kidxs;
9394 }
9395 if (byts == NULL)
9396 continue; /* array is empty */
9397
9398 depth = 0;
9399 arridx[0] = 0;
9400 curi[0] = 1;
9401 while (depth >= 0 && !got_int)
9402 {
9403 if (curi[depth] > byts[arridx[depth]])
9404 {
9405 /* Done all bytes at this node, go up one level. */
9406 --depth;
9407 line_breakcheck();
9408 }
9409 else
9410 {
9411 /* Do one more byte at this node. */
9412 n = arridx[depth] + curi[depth];
9413 ++curi[depth];
9414 c = byts[n];
9415 if (c == 0)
9416 {
9417 /* End of word, deal with the word.
9418 * Don't use keep-case words in the fold-case tree,
9419 * they will appear in the keep-case tree.
9420 * Only use the word when the region matches. */
9421 flags = (int)idxs[n];
9422 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
9423 && ((flags & WF_REGION) == 0
9424 || (((unsigned)flags >> 8)
9425 & lp->lp_region) != 0))
9426 {
9427 word[depth] = NUL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009428
9429 /* Dump the basic word if there is no prefix or
9430 * when it's the first one. */
9431 c = (unsigned)flags >> 16;
9432 if (c == 0 || curi[depth] == 2)
9433 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009434
9435 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009436 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009437 lnum = apply_prefixes(slang, word, round,
9438 flags, lnum);
9439 }
9440 }
9441 else
9442 {
9443 /* Normal char, go one level deeper. */
9444 word[depth++] = c;
9445 arridx[depth] = idxs[n];
9446 curi[depth] = 1;
9447 }
9448 }
9449 }
9450 }
9451 }
9452
9453 /* Delete the empty line that we started with. */
9454 if (curbuf->b_ml.ml_line_count > 1)
9455 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
9456
9457 redraw_later(NOT_VALID);
9458}
9459
9460/*
9461 * Dump one word: apply case modifications and append a line to the buffer.
9462 */
9463 static void
9464dump_word(word, round, flags, lnum)
9465 char_u *word;
9466 int round;
9467 int flags;
9468 linenr_T lnum;
9469{
9470 int keepcap = FALSE;
9471 char_u *p;
9472 char_u cword[MAXWLEN];
9473 char_u badword[MAXWLEN + 3];
9474
9475 if (round == 1 && (flags & WF_CAPMASK) != 0)
9476 {
9477 /* Need to fix case according to "flags". */
9478 make_case_word(word, cword, flags);
9479 p = cword;
9480 }
9481 else
9482 {
9483 p = word;
9484 if (round == 2 && (captype(word, NULL) & WF_KEEPCAP) == 0)
9485 keepcap = TRUE;
9486 }
9487
9488 /* Bad word is preceded by "/!" and some other
9489 * flags. */
9490 if ((flags & (WF_BANNED | WF_RARE)) || keepcap)
9491 {
9492 STRCPY(badword, "/");
9493 if (keepcap)
9494 STRCAT(badword, "=");
9495 if (flags & WF_BANNED)
9496 STRCAT(badword, "!");
9497 else if (flags & WF_RARE)
9498 STRCAT(badword, "?");
9499 STRCAT(badword, p);
9500 p = badword;
9501 }
9502
9503 ml_append(lnum, p, (colnr_T)0, FALSE);
9504}
9505
9506/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009507 * For ":spelldump": Find matching prefixes for "word". Prepend each to
9508 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009509 * Return the updated line number.
9510 */
9511 static linenr_T
9512apply_prefixes(slang, word, round, flags, startlnum)
9513 slang_T *slang;
9514 char_u *word; /* case-folded word */
9515 int round;
9516 int flags; /* flags with prefix ID */
9517 linenr_T startlnum;
9518{
9519 idx_T arridx[MAXWLEN];
9520 int curi[MAXWLEN];
9521 char_u prefix[MAXWLEN];
9522 int c;
9523 char_u *byts;
9524 idx_T *idxs;
9525 linenr_T lnum = startlnum;
9526 int depth;
9527 int n;
9528 int len;
9529 int prefid = (unsigned)flags >> 16;
9530 int i;
9531
9532 byts = slang->sl_pbyts;
9533 idxs = slang->sl_pidxs;
9534 if (byts != NULL) /* array not is empty */
9535 {
9536 /*
9537 * Loop over all prefixes, building them byte-by-byte in prefix[].
9538 * When at the end of a prefix check that it supports "prefid".
9539 */
9540 depth = 0;
9541 arridx[0] = 0;
9542 curi[0] = 1;
9543 while (depth >= 0 && !got_int)
9544 {
9545 len = arridx[depth];
9546 if (curi[depth] > byts[len])
9547 {
9548 /* Done all bytes at this node, go up one level. */
9549 --depth;
9550 line_breakcheck();
9551 }
9552 else
9553 {
9554 /* Do one more byte at this node. */
9555 n = len + curi[depth];
9556 ++curi[depth];
9557 c = byts[n];
9558 if (c == 0)
9559 {
9560 /* End of prefix, find out how many IDs there are. */
9561 for (i = 1; i < len; ++i)
9562 if (byts[n + i] != 0)
9563 break;
9564 curi[depth] += i - 1;
9565
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009566 i = valid_word_prefix(i, n, prefid, word, slang);
9567 if (i != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009568 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009569 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009570 dump_word(prefix, round,
9571 (i & WF_RAREPFX) ? (flags | WF_RARE)
9572 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009573 }
9574 }
9575 else
9576 {
9577 /* Normal char, go one level deeper. */
9578 prefix[depth++] = c;
9579 arridx[depth] = idxs[n];
9580 curi[depth] = 1;
9581 }
9582 }
9583 }
9584 }
9585
9586 return lnum;
9587}
9588
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009589#endif /* FEAT_SYN_HL */