blob: 853949b107217c9dc38e167b9e3e25cc42100721 [file] [log] [blame]
Bram Moolenaare19defe2005-03-21 08:23:33 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * spell.c: code for spell checking
Bram Moolenaarfc735152005-03-22 22:54:12 +000012 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000013 * The spell checking mechanism uses a tree (aka trie). Each node in the tree
14 * has a list of bytes that can appear (siblings). For each byte there is a
15 * pointer to the node with the byte that follows in the word (child).
Bram Moolenaar9f30f502005-06-14 22:01:04 +000016 *
17 * A NUL byte is used where the word may end. The bytes are sorted, so that
18 * binary searching can be used and the NUL bytes are at the start. The
19 * number of possible bytes is stored before the list of bytes.
20 *
21 * The tree uses two arrays: "byts" stores the characters, "idxs" stores
22 * either the next index or flags. The tree starts at index 0. For example,
23 * to lookup "vi" this sequence is followed:
24 * i = 0
25 * len = byts[i]
26 * n = where "v" appears in byts[i + 1] to byts[i + len]
27 * i = idxs[n]
28 * len = byts[i]
29 * n = where "i" appears in byts[i + 1] to byts[i + len]
30 * i = idxs[n]
31 * len = byts[i]
32 * find that byts[i + 1] is 0, idxs[i + 1] has flags for "vi".
Bram Moolenaar51485f02005-06-04 21:55:20 +000033 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000034 * There are two word trees: one with case-folded words and one with words in
Bram Moolenaar51485f02005-06-04 21:55:20 +000035 * original case. The second one is only used for keep-case words and is
36 * usually small.
37 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000038 * There is one additional tree for when prefixes are not applied when
39 * generating the .spl file. This tree stores all the possible prefixes, as
40 * if they were words. At each word (prefix) end the prefix nr is stored, the
41 * following word must support this prefix nr. And the condition nr is
42 * stored, used to lookup the condition that the word must match with.
43 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000044 * Thanks to Olaf Seibert for providing an example implementation of this tree
45 * and the compression mechanism.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000046 *
47 * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000048 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000049 * Why doesn't Vim use aspell/ispell/myspell/etc.?
50 * See ":help develop-spell".
51 */
52
Bram Moolenaar51485f02005-06-04 21:55:20 +000053/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000054 * Use this to adjust the score after finding suggestions, based on the
55 * suggested word sounding like the bad word. This is much faster than doing
56 * it for every possible suggestion.
57 * Disadvantage: When "the" is typed as "hte" it sounds different and goes
58 * down in the list.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000059 * Used when 'spellsuggest' is set to "best".
60 */
61#define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4)
62
63/*
64 * The double scoring mechanism is based on the principle that there are two
65 * kinds of spelling mistakes:
66 * 1. You know how to spell the word, but mistype something. This results in
67 * a small editing distance (character swapped/omitted/inserted) and
68 * possibly a word that sounds completely different.
69 * 2. You don't know how to spell the word and type something that sounds
70 * right. The edit distance can be big but the word is similar after
71 * sound-folding.
72 * Since scores for these two mistakes will be very different we use a list
73 * for each.
74 * The sound-folding is slow, only do double scoring when 'spellsuggest' is
75 * "double".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000076 */
77
78/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +000079 * Vim spell file format: <HEADER>
80 * <SUGGEST>
81 * <LWORDTREE>
82 * <KWORDTREE>
83 * <PREFIXTREE>
Bram Moolenaar51485f02005-06-04 21:55:20 +000084 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000085 * <HEADER>: <fileID>
86 * <regioncnt> <regionname> ...
87 * <charflagslen> <charflags>
88 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000089 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +000090 * <prefcondcnt> <prefcond> ...
Bram Moolenaar51485f02005-06-04 21:55:20 +000091 *
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000092 * <fileID> 10 bytes "VIMspell08"
Bram Moolenaar51485f02005-06-04 21:55:20 +000093 * <regioncnt> 1 byte number of regions following (8 supported)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000094 * <regionname> 2 bytes Region name: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +000095 * First <regionname> is region 1.
96 *
97 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
98 * <charflags> N bytes List of flags (first one is for character 128):
Bram Moolenaar9f30f502005-06-14 22:01:04 +000099 * 0x01 word character CF_WORD
100 * 0x02 upper-case character CF_UPPER
Bram Moolenaar51485f02005-06-04 21:55:20 +0000101 * <fcharslen> 2 bytes Number of bytes in <fchars>.
102 * <fchars> N bytes Folded characters, first one is for character 128.
103 *
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000104 * <midwordlen> 2 bytes Number of bytes in <midword>.
105 * <midword> N bytes Characters that are word characters only when used
106 * in the middle of a word.
107 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000108 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
109 *
110 * <prefcond> : <condlen> <condstr>
111 *
112 * <condlen> 1 byte Length of <condstr>.
113 *
114 * <condstr> N bytes Condition for the prefix.
115 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000116 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000117 * <SUGGEST> : <repcount> <rep> ...
118 * <salflags> <salcount> <sal> ...
119 * <maplen> <mapstr>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000120 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000121 * <repcount> 2 bytes number of <rep> items, MSB first.
122 *
123 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
124 *
125 * <repfromlen> 1 byte length of <repfrom>
126 *
127 * <repfrom> N bytes "from" part of replacement
128 *
129 * <reptolen> 1 byte length of <repto>
130 *
131 * <repto> N bytes "to" part of replacement
132 *
133 * <salflags> 1 byte flags for soundsalike conversion:
134 * SAL_F0LLOWUP
135 * SAL_COLLAPSE
136 * SAL_REM_ACCENTS
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000137 * SAL_SOFO: SOFOFROM and SOFOTO used instead of SAL
138 *
139 * <salcount> 2 bytes number of <sal> items following
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000140 *
141 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
142 *
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000143 * <salfromlen> 1-2 bytes length of <salfrom> (2 bytes for SAL_SOFO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000144 *
145 * <salfrom> N bytes "from" part of soundsalike
146 *
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000147 * <saltolen> 1-2 bytes length of <salto> (2 bytes for SAL_SOFO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000148 *
149 * <salto> N bytes "to" part of soundsalike
150 *
151 * <maplen> 2 bytes length of <mapstr>, MSB first
152 *
153 * <mapstr> N bytes String with sequences of similar characters,
154 * separated by slashes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000155 *
156 *
157 * <LWORDTREE>: <wordtree>
158 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000159 * <KWORDTREE>: <wordtree>
160 *
161 * <PREFIXTREE>: <wordtree>
162 *
163 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000164 * <wordtree>: <nodecount> <nodedata> ...
165 *
166 * <nodecount> 4 bytes Number of nodes following. MSB first.
167 *
168 * <nodedata>: <siblingcount> <sibling> ...
169 *
170 * <siblingcount> 1 byte Number of siblings in this node. The siblings
171 * follow in sorted order.
172 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000173 * <sibling>: <byte> [ <nodeidx> <xbyte>
174 * | <flags> [<region>] [<prefixID>]
175 * | <prefixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000176 *
177 * <byte> 1 byte Byte value of the sibling. Special cases:
178 * BY_NOFLAGS: End of word without flags and for all
179 * regions.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000180 * For PREFIXTREE <prefixID> and
181 * <prefcondnr> follow.
182 * BY_FLAGS: End of word, <flags> follow.
183 * For PREFIXTREE <prefixID> and
184 * <prefcondnr> follow for rare prefix.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000185 * BY_INDEX: Child of sibling is shared, <nodeidx>
186 * and <xbyte> follow.
187 *
188 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
189 *
190 * <xbyte> 1 byte byte value of the sibling.
191 *
192 * <flags> 1 byte bitmask of:
193 * WF_ALLCAP word must have only capitals
194 * WF_ONECAP first char of word must be capital
195 * WF_RARE rare word
196 * WF_REGION <region> follows
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000197 * WF_PFX <prefixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000198 *
199 * <region> 1 byte Bitmask for regions in which word is valid. When
200 * omitted it's valid in all regions.
201 * Lowest bit is for region 1.
202 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000203 * <prefixID> 1 byte ID of prefix that can be used with this word. For
204 * PREFIXTREE used for the required prefix ID.
205 *
206 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
207 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000208 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000209 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000210 */
211
Bram Moolenaare19defe2005-03-21 08:23:33 +0000212#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
213# include <io.h> /* for lseek(), must be before vim.h */
214#endif
215
216#include "vim.h"
217
218#if defined(FEAT_SYN_HL) || defined(PROTO)
219
220#ifdef HAVE_FCNTL_H
221# include <fcntl.h>
222#endif
223
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000224#define MAXWLEN 250 /* Assume max. word len is this many bytes.
225 Some places assume a word length fits in a
226 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000227
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000228/* Type used for indexes in the word tree need to be at least 3 bytes. If int
229 * is 8 bytes we could use something smaller, but what? */
230#if SIZEOF_INT > 2
231typedef int idx_T;
232#else
233typedef long idx_T;
234#endif
235
236/* Flags used for a word. Only the lowest byte can be used, the region byte
237 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000238#define WF_REGION 0x01 /* region byte follows */
239#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
240#define WF_ALLCAP 0x04 /* word must be all capitals */
241#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000242#define WF_BANNED 0x10 /* bad word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000243#define WF_PFX 0x20 /* prefix ID list follows */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000244#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000245
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000246#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000247
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000248#define WF_RAREPFX 0x1000000 /* in sl_pidxs: flag for rare postponed
249 prefix; must be above prefixID (one byte)
250 and prefcondnr (two bytes) */
251
Bram Moolenaar51485f02005-06-04 21:55:20 +0000252#define BY_NOFLAGS 0 /* end of word without flags or region */
253#define BY_FLAGS 1 /* end of word, flag byte follows */
254#define BY_INDEX 2 /* child is shared, index follows */
255#define BY_SPECIAL BY_INDEX /* hightest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000256
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000257/* Info from "REP" and "SAL" entries in ".aff" file used in si_rep, sl_rep,
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000258 * and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000259 * One replacement: from "ft_from" to "ft_to". */
260typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000261{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000262 char_u *ft_from;
263 char_u *ft_to;
264} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000265
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000266/* Info from "SAL" entries in ".aff" file used in sl_sal.
267 * The info is split for quick processing by spell_soundfold().
268 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
269typedef struct salitem_S
270{
271 char_u *sm_lead; /* leading letters */
272 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000273 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000274 char_u *sm_rules; /* rules like ^, $, priority */
275 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000276#ifdef FEAT_MBYTE
277 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000278 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000279 int *sm_to_w; /* wide character copy of "sm_to" */
280#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000281} salitem_T;
282
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000283#ifdef FEAT_MBYTE
284typedef int salfirst_T;
285#else
286typedef short salfirst_T;
287#endif
288
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000289/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000290 * Structure used to store words and other info for one language, loaded from
291 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000292 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
293 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
294 *
295 * The "byts" array stores the possible bytes in each tree node, preceded by
296 * the number of possible bytes, sorted on byte value:
297 * <len> <byte1> <byte2> ...
298 * The "idxs" array stores the index of the child node corresponding to the
299 * byte in "byts".
300 * Exception: when the byte is zero, the word may end here and "idxs" holds
301 * the flags and region for the word. There may be several zeros in sequence
302 * for alternative flag/region combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000303 */
304typedef struct slang_S slang_T;
305struct slang_S
306{
307 slang_T *sl_next; /* next language */
308 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000309 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000310 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000311
Bram Moolenaar51485f02005-06-04 21:55:20 +0000312 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000313 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000314 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000315 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000316 char_u *sl_pbyts; /* prefix tree word bytes */
317 idx_T *sl_pidxs; /* prefix tree word indexes */
318
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000319 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000320
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000321 char_u *sl_midword; /* MIDWORD string or NULL */
322
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000323 int sl_prefixcnt; /* number of items in "sl_prefprog" */
324 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
325
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000326 garray_T sl_rep; /* list of fromto_T entries from REP lines */
327 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
328 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000329 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000330 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000331 there is none */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000332 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
333 * "sl_sal_first" maps chars, when has_mbyte
334 * "sl_sal" is a list of wide char lists. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000335 int sl_followup; /* SAL followup */
336 int sl_collapse; /* SAL collapse_result */
337 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaarea424162005-06-16 21:51:00 +0000338 int sl_has_map; /* TRUE if there is a MAP line */
339#ifdef FEAT_MBYTE
340 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
341 int sl_map_array[256]; /* MAP for first 256 chars */
342#else
343 char_u sl_map_array[256]; /* MAP for first 256 chars */
344#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000345};
346
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000347/* First language that is loaded, start of the linked list of loaded
348 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000349static slang_T *first_lang = NULL;
350
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000351/* Flags used in .spl file for soundsalike flags. */
352#define SAL_F0LLOWUP 1
353#define SAL_COLLAPSE 2
354#define SAL_REM_ACCENTS 4
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000355#define SAL_SOFO 8 /* SOFOFROM and SOFOTO instead of SAL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000356
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000357/*
358 * Structure used in "b_langp", filled from 'spelllang'.
359 */
360typedef struct langp_S
361{
362 slang_T *lp_slang; /* info for this language (NULL for last one) */
363 int lp_region; /* bitmask for region or REGION_ALL */
364} langp_T;
365
366#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
367
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000368#define REGION_ALL 0xff /* word valid in all regions */
369
370/* Result values. Lower number is accepted over higher one. */
371#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000372#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000373#define SP_RARE 1
374#define SP_LOCAL 2
375#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000376
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000377#define VIMSPELLMAGIC "VIMspell08" /* string at start of Vim spell file */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000378#define VIMSPELLMAGICL 10
379
Bram Moolenaar7887d882005-07-01 22:33:52 +0000380/* file used for "zG" and "zW" */
381static char_u *temp_wordlist = NULL;
382
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000383/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000384 * Information used when looking for suggestions.
385 */
386typedef struct suginfo_S
387{
388 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000389 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000390 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000391 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000392 char_u *su_badptr; /* start of bad word in line */
393 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000394 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000395 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
396 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
397 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000398} suginfo_T;
399
400/* One word suggestion. Used in "si_ga". */
401typedef struct suggest_S
402{
403 char_u *st_word; /* suggested word, allocated string */
404 int st_orglen; /* length of replaced text */
405 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000406 int st_altscore; /* used when st_score compares equal */
407 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000408 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000409} suggest_T;
410
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000411#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000412
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000413/* Number of suggestions kept when cleaning up. When rescore_suggestions() is
414 * called the score may change, thus we need to keep more than what is
415 * displayed. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000416#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 50 ? 50 : (su)->su_maxcount)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000417
418/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
419 * of suggestions that are not going to be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000420#define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000421
422/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000423#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000424#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000425#define SCORE_REGION 70 /* word is for different region */
426#define SCORE_RARE 180 /* rare word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000427#define SCORE_SWAP 90 /* swap two characters */
428#define SCORE_SWAP3 110 /* swap two characters in three */
429#define SCORE_REP 87 /* REP replacement */
430#define SCORE_SUBST 93 /* substitute a character */
431#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000432#define SCORE_DEL 94 /* delete a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000433#define SCORE_DELDUP 64 /* delete a duplicated character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000434#define SCORE_INS 96 /* insert a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000435#define SCORE_INSDUP 66 /* insert a duplicate character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000436#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000437
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000438#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000439#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
440 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000441
442#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000443#define SCORE_MAXMAX 999999 /* accept any score */
444
445/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000446 * Structure to store info for word matching.
447 */
448typedef struct matchinf_S
449{
450 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000451
452 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000453 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000454 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000455 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000456 char_u *mi_cend; /* char after what was used for
457 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000458
459 /* case-folded text */
460 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000461 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000462
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000463 /* for when checking word after a prefix */
464 int mi_prefarridx; /* index in sl_pidxs with list of
465 prefixID/condition */
466 int mi_prefcnt; /* number of entries at mi_prefarridx */
467 int mi_prefixlen; /* byte length of prefix */
468
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000469 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000470 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000471 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000472 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000473} matchinf_T;
474
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000475/*
476 * The tables used for recognizing word characters according to spelling.
477 * These are only used for the first 256 characters of 'encoding'.
478 */
479typedef struct spelltab_S
480{
481 char_u st_isw[256]; /* flags: is word char */
482 char_u st_isu[256]; /* flags: is uppercase char */
483 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000484 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000485} spelltab_T;
486
487static spelltab_T spelltab;
488static int did_set_spelltab;
489
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000490#define CF_WORD 0x01
491#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000492
493static void clear_spell_chartab __ARGS((spelltab_T *sp));
494static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000495static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
496static int spell_iswordp_nmw __ARGS((char_u *p));
497#ifdef FEAT_MBYTE
498static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
499#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000500static void write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000501
502/*
Bram Moolenaarea408852005-06-25 22:49:46 +0000503 * Return TRUE if "p" points to a word character. Like spell_iswordp() but
504 * without the special handling of a single quote.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000505 * Checking for a word character is done very often, avoid the function call
506 * overhead.
507 */
508#ifdef FEAT_MBYTE
509# define SPELL_ISWORDP(p) ((has_mbyte && MB_BYTE2LEN(*(p)) > 1) \
510 ? (mb_get_class(p) >= 2) : spelltab.st_isw[*(p)])
511#else
512# define SPELL_ISWORDP(p) (spelltab.st_isw[*(p)])
513#endif
514
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000515/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000516 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000517 */
518typedef enum
519{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000520 STATE_START = 0, /* At start of node check for NUL bytes (goodword
521 * ends); if badword ends there is a match, otherwise
522 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000523 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000524 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000525 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
526 STATE_PLAIN, /* Use each byte of the node. */
527 STATE_DEL, /* Delete a byte from the bad word. */
528 STATE_INS, /* Insert a byte in the bad word. */
529 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000530 STATE_UNSWAP, /* Undo swap two characters. */
531 STATE_SWAP3, /* Swap two characters over three. */
532 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000533 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000534 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000535 STATE_REP_INI, /* Prepare for using REP items. */
536 STATE_REP, /* Use matching REP items from the .aff file. */
537 STATE_REP_UNDO, /* Undo a REP item replacement. */
538 STATE_FINAL /* End of this node. */
539} state_T;
540
541/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000542 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000543 */
544typedef struct trystate_S
545{
Bram Moolenaarea424162005-06-16 21:51:00 +0000546 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000547 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000548 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000549 short ts_curi; /* index in list of child nodes */
550 char_u ts_fidx; /* index in fword[], case-folded bad word */
551 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
552 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000553 char_u ts_prefixdepth; /* stack depth for end of prefix or PREFIXTREE
554 * or NOPREFIX */
Bram Moolenaarea424162005-06-16 21:51:00 +0000555#ifdef FEAT_MBYTE
556 char_u ts_tcharlen; /* number of bytes in tword character */
557 char_u ts_tcharidx; /* current byte index in tword character */
558 char_u ts_isdiff; /* DIFF_ values */
559 char_u ts_fcharstart; /* index in fword where badword char started */
560#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000561 char_u ts_save_prewordlen; /* saved "prewordlen" */
Bram Moolenaarea424162005-06-16 21:51:00 +0000562 char_u ts_save_splitoff; /* su_splitoff saved here */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000563 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000564} trystate_T;
565
Bram Moolenaarea424162005-06-16 21:51:00 +0000566/* values for ts_isdiff */
567#define DIFF_NONE 0 /* no different byte (yet) */
568#define DIFF_YES 1 /* different byte found */
569#define DIFF_INSERT 2 /* inserting character */
570
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000571/* special values ts_prefixdepth */
572#define PREFIXTREE 0xfe /* walking through the prefix tree */
573#define NOPREFIX 0xff /* not using prefixes */
574
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000575/* mode values for find_word */
576#define FIND_FOLDWORD 0 /* find word case-folded */
577#define FIND_KEEPWORD 1 /* find keep-case word */
578#define FIND_PREFIX 2 /* find word after prefix */
579
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000580static slang_T *slang_alloc __ARGS((char_u *lang));
581static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000582static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000583static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000584static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int prefid, char_u *word, slang_T *slang));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000585static void find_prefix __ARGS((matchinf_T *mip));
586static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000587static int spell_valid_case __ARGS((int origflags, int treeflags));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000588static int no_spell_checking __ARGS((void));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000589static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000590static char_u *spell_enc __ARGS((void));
591static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000592static 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 +0000593static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *errp));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000594static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
595static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000596#ifdef FEAT_MBYTE
597static int *mb_str2wide __ARGS((char_u *s));
598#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000599static 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 +0000600static void clear_midword __ARGS((buf_T *buf));
601static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000602static int find_region __ARGS((char_u *rp, char_u *region));
603static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000604static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000605static int set_spell_charflags __ARGS((char_u *flags, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000606static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
607static void write_spell_chartab __ARGS((FILE *fd));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000608static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaarea408852005-06-25 22:49:46 +0000609static void spell_find_suggest __ARGS((char_u *badptr, suginfo_T *su, int maxcount, int banbadword));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000610#ifdef FEAT_EVAL
611static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
612#endif
613static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
614static void spell_suggest_intern __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000615static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000616static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000617static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000618static void suggest_try_special __ARGS((suginfo_T *su));
619static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000620static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
621static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000622static void score_comp_sal __ARGS((suginfo_T *su));
623static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000624static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000625static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000626static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000627static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000628static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000629static 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 +0000630static void add_banned __ARGS((suginfo_T *su, char_u *word));
631static int was_banned __ARGS((suginfo_T *su, char_u *word));
632static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000633static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000634static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000635static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
636static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
637static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000638#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000639static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000640#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000641static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000642static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000643static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum));
644static 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 +0000645
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000646/*
647 * Use our own character-case definitions, because the current locale may
648 * differ from what the .spl file uses.
649 * These must not be called with negative number!
650 */
651#ifndef FEAT_MBYTE
652/* Non-multi-byte implementation. */
653# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
654# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
655# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
656#else
657/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
658 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
659 * the "w" library function for characters above 255 if available. */
660# ifdef HAVE_TOWLOWER
661# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
662 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
663# else
664# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
665 : (c) < 256 ? spelltab.st_fold[c] : (c))
666# endif
667
668# ifdef HAVE_TOWUPPER
669# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
670 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
671# else
672# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
673 : (c) < 256 ? spelltab.st_upper[c] : (c))
674# endif
675
676# ifdef HAVE_ISWUPPER
677# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
678 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
679# else
680# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
681 : (c) < 256 ? spelltab.st_isu[c] : (c))
682# endif
683#endif
684
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000685
686static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000687static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000688
689/*
690 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000691 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000692 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
693 * or when it's OK it remains unchanged.
694 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000695 *
696 * "sug" is normally NULL. When looking for suggestions it points to
697 * suginfo_T. It's passed as a void pointer to keep the struct local.
698 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000699 * Returns the length of the word in bytes, also when it's OK, so that the
700 * caller can skip over the word.
701 */
702 int
Bram Moolenaar51485f02005-06-04 21:55:20 +0000703spell_check(wp, ptr, attrp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000704 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000705 char_u *ptr;
706 int *attrp;
707{
708 matchinf_T mi; /* Most things are put in "mi" so that it can
709 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000710 int nrlen = 0; /* found a number first */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000711
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000712 /* A word never starts at a space or a control character. Return quickly
713 * then, skipping over the character. */
714 if (*ptr <= ' ')
715 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000716
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000717 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar0c405862005-06-22 22:26:26 +0000718 * 0X99FF. But when a word character follows do check spelling to find
719 * "3GPP". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000720 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000721 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000722 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
723 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000724 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000725 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000726 mi.mi_end = skipdigits(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000727 nrlen = mi.mi_end - ptr;
728 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000729 if (!spell_iswordp(mi.mi_end, wp->w_buffer))
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000730 return (int)(mi.mi_end - ptr);
Bram Moolenaar0c405862005-06-22 22:26:26 +0000731
732 /* Try including the digits in the word. */
733 mi.mi_fend = ptr + nrlen;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000734 }
Bram Moolenaar0c405862005-06-22 22:26:26 +0000735 else
736 mi.mi_fend = ptr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000737
Bram Moolenaar0c405862005-06-22 22:26:26 +0000738 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000739 mi.mi_word = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000740 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000741 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000742 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000743 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000744 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000745 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000746 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000747
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000748 /* We always use the characters up to the next non-word character,
749 * also for bad words. */
750 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000751
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000752 /* Check caps type later. */
753 mi.mi_capflags = 0;
754 mi.mi_cend = NULL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000755 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000756
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000757 /* Include one non-word character so that we can check for the
758 * word end. */
759 if (*mi.mi_fend != NUL)
760 mb_ptr_adv(mi.mi_fend);
761
762 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
763 MAXWLEN + 1);
764 mi.mi_fwordlen = STRLEN(mi.mi_fword);
765
766 /* The word is bad unless we recognize it. */
767 mi.mi_result = SP_BAD;
768
769 /*
770 * Loop over the languages specified in 'spelllang'.
771 * We check them all, because a matching word may be longer than an
772 * already found matching word.
773 */
774 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
775 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
776 {
777 /* Check for a matching word in case-folded words. */
778 find_word(&mi, FIND_FOLDWORD);
779
780 /* Check for a matching word in keep-case words. */
781 find_word(&mi, FIND_KEEPWORD);
782
783 /* Check for matching prefixes. */
784 find_prefix(&mi);
785 }
786
787 if (mi.mi_result != SP_OK)
788 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000789 /* If we found a number skip over it. Allows for "42nd". Do flag
790 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000791 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000792 {
793 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
794 return nrlen;
795 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000796
797 /* When we are at a non-word character there is no error, just
798 * skip over the character (try looking for a word after it). */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000799 else if (!SPELL_ISWORDP(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000800 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000801#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000802 if (has_mbyte)
803 return mb_ptr2len_check(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000804#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000805 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000806 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000807
808 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
809 *attrp = highlight_attr[HLF_SPB];
810 else if (mi.mi_result == SP_RARE)
811 *attrp = highlight_attr[HLF_SPR];
812 else
813 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000814 }
815
Bram Moolenaar51485f02005-06-04 21:55:20 +0000816 return (int)(mi.mi_end - ptr);
817}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000818
Bram Moolenaar51485f02005-06-04 21:55:20 +0000819/*
820 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000821 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
822 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
823 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
824 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000825 *
826 * For a match mip->mi_result is updated.
827 */
828 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000829find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000830 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000831 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000832{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000833 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000834 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000835 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000836 int endidxcnt = 0;
837 int len;
838 int wlen = 0;
839 int flen;
840 int c;
841 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000842 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000843#ifdef FEAT_MBYTE
844 char_u *s;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000845 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000846#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000847 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000848 slang_T *slang = mip->mi_lp->lp_slang;
849 unsigned flags;
850 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000851 idx_T *idxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000852 int prefid;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000853
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000854 if (mode == FIND_KEEPWORD)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000855 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000856 /* Check for word with matching case in keep-case tree. */
857 ptr = mip->mi_word;
858 flen = 9999; /* no case folding, always enough bytes */
859 byts = slang->sl_kbyts;
860 idxs = slang->sl_kidxs;
861 }
862 else
863 {
864 /* Check for case-folded in case-folded tree. */
865 ptr = mip->mi_fword;
866 flen = mip->mi_fwordlen; /* available case-folded bytes */
867 byts = slang->sl_fbyts;
868 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000869
870 if (mode == FIND_PREFIX)
871 {
872 /* Skip over the prefix. */
873 wlen = mip->mi_prefixlen;
874 flen -= mip->mi_prefixlen;
875 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000876 }
877
Bram Moolenaar51485f02005-06-04 21:55:20 +0000878 if (byts == NULL)
879 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000880
Bram Moolenaar51485f02005-06-04 21:55:20 +0000881 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000882 * Repeat advancing in the tree until:
883 * - there is a byte that doesn't match,
884 * - we reach the end of the tree,
885 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000886 */
887 for (;;)
888 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000889 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000890 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000891
892 len = byts[arridx++];
893
894 /* If the first possible byte is a zero the word could end here.
895 * Remember this index, we first check for the longest word. */
896 if (byts[arridx] == 0)
897 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000898 if (endidxcnt == MAXWLEN)
899 {
900 /* Must be a corrupted spell file. */
901 EMSG(_(e_format));
902 return;
903 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000904 endlen[endidxcnt] = wlen;
905 endidx[endidxcnt++] = arridx++;
906 --len;
907
908 /* Skip over the zeros, there can be several flag/region
909 * combinations. */
910 while (len > 0 && byts[arridx] == 0)
911 {
912 ++arridx;
913 --len;
914 }
915 if (len == 0)
916 break; /* no children, word must end here */
917 }
918
919 /* Stop looking at end of the line. */
920 if (ptr[wlen] == NUL)
921 break;
922
923 /* Perform a binary search in the list of accepted bytes. */
924 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +0000925 if (c == TAB) /* <Tab> is handled like <Space> */
926 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +0000927 lo = arridx;
928 hi = arridx + len - 1;
929 while (lo < hi)
930 {
931 m = (lo + hi) / 2;
932 if (byts[m] > c)
933 hi = m - 1;
934 else if (byts[m] < c)
935 lo = m + 1;
936 else
937 {
938 lo = hi = m;
939 break;
940 }
941 }
942
943 /* Stop if there is no matching byte. */
944 if (hi < lo || byts[lo] != c)
945 break;
946
947 /* Continue at the child (if there is one). */
948 arridx = idxs[lo];
949 ++wlen;
950 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +0000951
952 /* One space in the good word may stand for several spaces in the
953 * checked word. */
954 if (c == ' ')
955 {
956 for (;;)
957 {
958 if (flen <= 0 && *mip->mi_fend != NUL)
959 flen = fold_more(mip);
960 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
961 break;
962 ++wlen;
963 --flen;
964 }
965 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000966 }
967
968 /*
969 * Verify that one of the possible endings is valid. Try the longest
970 * first.
971 */
972 while (endidxcnt > 0)
973 {
974 --endidxcnt;
975 arridx = endidx[endidxcnt];
976 wlen = endlen[endidxcnt];
977
978#ifdef FEAT_MBYTE
979 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
980 continue; /* not at first byte of character */
981#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000982 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000983 continue; /* next char is a word character */
984
985#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000986 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000987 {
988 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000989 * when folding case. This can be slow, take a shortcut when the
990 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000991 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000992 if (STRNCMP(ptr, p, wlen) != 0)
993 {
994 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
995 mb_ptr_adv(p);
996 wlen = p - mip->mi_word;
997 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000998 }
999#endif
1000
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001001 /* Check flags and region. For FIND_PREFIX check the condition and
1002 * prefix ID.
1003 * Repeat this if there are more flags/region alternatives until there
1004 * is a match. */
1005 res = SP_BAD;
1006 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1007 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001008 {
1009 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001010
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001011 /* For the fold-case tree check that the case of the checked word
1012 * matches with what the word in the tree requires.
1013 * For keep-case tree the case is always right. For prefixes we
1014 * don't bother to check. */
1015 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001016 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001017 if (mip->mi_cend != mip->mi_word + wlen)
1018 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001019 /* mi_capflags was set for a different word length, need
1020 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001021 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001022 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001023 }
1024
Bram Moolenaar0c405862005-06-22 22:26:26 +00001025 if (mip->mi_capflags == WF_KEEPCAP
1026 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001027 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001028 }
1029
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001030 /* When mode is FIND_PREFIX the word must support the prefix:
1031 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001032 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001033 if (mode == FIND_PREFIX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001034 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001035 /* The prefix ID is stored two bytes above the flags. */
1036 prefid = (unsigned)flags >> 16;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001037 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001038 prefid, mip->mi_fword + mip->mi_prefixlen,
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001039 slang);
1040 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001041 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001042
1043 /* Use the WF_RARE flag for a rare prefix. */
1044 if (c & WF_RAREPFX)
1045 flags |= WF_RARE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001046 }
1047
1048 if (flags & WF_BANNED)
1049 res = SP_BANNED;
1050 else if (flags & WF_REGION)
1051 {
1052 /* Check region. */
1053 if ((mip->mi_lp->lp_region & (flags >> 8)) != 0)
1054 res = SP_OK;
1055 else
1056 res = SP_LOCAL;
1057 }
1058 else if (flags & WF_RARE)
1059 res = SP_RARE;
1060 else
1061 res = SP_OK;
1062
1063 /* Always use the longest match and the best result. */
1064 if (mip->mi_result > res)
1065 {
1066 mip->mi_result = res;
1067 mip->mi_end = mip->mi_word + wlen;
1068 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001069 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001070 mip->mi_end = mip->mi_word + wlen;
1071
1072 if (res == SP_OK)
1073 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001074 }
1075
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001076 if (res == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001077 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001078 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001079}
1080
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001081/*
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001082 * Return non-zero if the prefix indicated by "mip->mi_prefarridx" matches
1083 * with the prefix ID "prefid" for the word "word".
1084 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001085 */
1086 static int
1087valid_word_prefix(totprefcnt, arridx, prefid, word, slang)
1088 int totprefcnt; /* nr of prefix IDs */
1089 int arridx; /* idx in sl_pidxs[] */
1090 int prefid;
1091 char_u *word;
1092 slang_T *slang;
1093{
1094 int prefcnt;
1095 int pidx;
1096 regprog_T *rp;
1097 regmatch_T regmatch;
1098
1099 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1100 {
1101 pidx = slang->sl_pidxs[arridx + prefcnt];
1102
1103 /* Check the prefix ID. */
1104 if (prefid != (pidx & 0xff))
1105 continue;
1106
1107 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001108 * stored in the two bytes above the prefix ID byte. */
1109 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001110 if (rp != NULL)
1111 {
1112 regmatch.regprog = rp;
1113 regmatch.rm_ic = FALSE;
1114 if (!vim_regexec(&regmatch, word, 0))
1115 continue;
1116 }
1117
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001118 /* It's a match! Return the WF_RAREPFX flag. */
1119 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001120 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001121 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001122}
1123
1124/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001125 * Check if the word at "mip->mi_word" has a matching prefix.
1126 * If it does, then check the following word.
1127 *
1128 * For a match mip->mi_result is updated.
1129 */
1130 static void
1131find_prefix(mip)
1132 matchinf_T *mip;
1133{
1134 idx_T arridx = 0;
1135 int len;
1136 int wlen = 0;
1137 int flen;
1138 int c;
1139 char_u *ptr;
1140 idx_T lo, hi, m;
1141 slang_T *slang = mip->mi_lp->lp_slang;
1142 char_u *byts;
1143 idx_T *idxs;
1144
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001145 byts = slang->sl_pbyts;
1146 if (byts == NULL)
1147 return; /* array is empty */
1148
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001149 /* We use the case-folded word here, since prefixes are always
1150 * case-folded. */
1151 ptr = mip->mi_fword;
1152 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001153 idxs = slang->sl_pidxs;
1154
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001155 /*
1156 * Repeat advancing in the tree until:
1157 * - there is a byte that doesn't match,
1158 * - we reach the end of the tree,
1159 * - or we reach the end of the line.
1160 */
1161 for (;;)
1162 {
1163 if (flen == 0 && *mip->mi_fend != NUL)
1164 flen = fold_more(mip);
1165
1166 len = byts[arridx++];
1167
1168 /* If the first possible byte is a zero the prefix could end here.
1169 * Check if the following word matches and supports the prefix. */
1170 if (byts[arridx] == 0)
1171 {
1172 /* There can be several prefixes with different conditions. We
1173 * try them all, since we don't know which one will give the
1174 * longest match. The word is the same each time, pass the list
1175 * of possible prefixes to find_word(). */
1176 mip->mi_prefarridx = arridx;
1177 mip->mi_prefcnt = len;
1178 while (len > 0 && byts[arridx] == 0)
1179 {
1180 ++arridx;
1181 --len;
1182 }
1183 mip->mi_prefcnt -= len;
1184
1185 /* Find the word that comes after the prefix. */
1186 mip->mi_prefixlen = wlen;
1187 find_word(mip, FIND_PREFIX);
1188
1189
1190 if (len == 0)
1191 break; /* no children, word must end here */
1192 }
1193
1194 /* Stop looking at end of the line. */
1195 if (ptr[wlen] == NUL)
1196 break;
1197
1198 /* Perform a binary search in the list of accepted bytes. */
1199 c = ptr[wlen];
1200 lo = arridx;
1201 hi = arridx + len - 1;
1202 while (lo < hi)
1203 {
1204 m = (lo + hi) / 2;
1205 if (byts[m] > c)
1206 hi = m - 1;
1207 else if (byts[m] < c)
1208 lo = m + 1;
1209 else
1210 {
1211 lo = hi = m;
1212 break;
1213 }
1214 }
1215
1216 /* Stop if there is no matching byte. */
1217 if (hi < lo || byts[lo] != c)
1218 break;
1219
1220 /* Continue at the child (if there is one). */
1221 arridx = idxs[lo];
1222 ++wlen;
1223 --flen;
1224 }
1225}
1226
1227/*
1228 * Need to fold at least one more character. Do until next non-word character
1229 * for efficiency.
1230 * Return the length of the folded chars in bytes.
1231 */
1232 static int
1233fold_more(mip)
1234 matchinf_T *mip;
1235{
1236 int flen;
1237 char_u *p;
1238
1239 p = mip->mi_fend;
1240 do
1241 {
1242 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001243 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001244
1245 /* Include the non-word character so that we can check for the
1246 * word end. */
1247 if (*mip->mi_fend != NUL)
1248 mb_ptr_adv(mip->mi_fend);
1249
1250 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1251 mip->mi_fword + mip->mi_fwordlen,
1252 MAXWLEN - mip->mi_fwordlen);
1253 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1254 mip->mi_fwordlen += flen;
1255 return flen;
1256}
1257
1258/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001259 * Check case flags for a word. Return TRUE if the word has the requested
1260 * case.
1261 */
1262 static int
1263spell_valid_case(origflags, treeflags)
1264 int origflags; /* flags for the checked word. */
1265 int treeflags; /* flags for the word in the spell tree */
1266{
1267 return (origflags == WF_ALLCAP
1268 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
1269 && ((treeflags & WF_ONECAP) == 0 || origflags == WF_ONECAP)));
1270}
1271
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001272/*
1273 * Return TRUE if spell checking is not enabled.
1274 */
1275 static int
1276no_spell_checking()
1277{
1278 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
1279 {
1280 EMSG(_("E756: Spell checking is not enabled"));
1281 return TRUE;
1282 }
1283 return FALSE;
1284}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001285
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001286/*
1287 * Move to next spell error.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001288 * "curline" is TRUE for "z?": find word under/after cursor in the same line.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001289 * Return OK if found, FAIL otherwise.
1290 */
1291 int
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001292spell_move_to(dir, allwords, curline)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001293 int dir; /* FORWARD or BACKWARD */
1294 int allwords; /* TRUE for "[s" and "]s" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001295 int curline;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001296{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001297 linenr_T lnum;
1298 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001299 char_u *line;
1300 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001301 char_u *endp;
1302 int attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001303 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001304 int has_syntax = syntax_present(curbuf);
1305 int col;
1306 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001307 char_u *buf = NULL;
1308 int buflen = 0;
1309 int skip = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001310
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001311 if (no_spell_checking())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001312 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001313
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001314 /*
1315 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001316 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001317 *
1318 * When searching backwards, we continue in the line to find the last
1319 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001320 *
1321 * We concatenate the start of the next line, so that wrapped words work
1322 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1323 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001324 */
1325 lnum = curwin->w_cursor.lnum;
1326 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001327
1328 while (!got_int)
1329 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001330 line = ml_get(lnum);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001331
Bram Moolenaar0c405862005-06-22 22:26:26 +00001332 len = STRLEN(line);
1333 if (buflen < len + MAXWLEN + 2)
1334 {
1335 vim_free(buf);
1336 buflen = len + MAXWLEN + 2;
1337 buf = alloc(buflen);
1338 if (buf == NULL)
1339 break;
1340 }
1341
1342 /* Copy the line into "buf" and append the start of the next line if
1343 * possible. */
1344 STRCPY(buf, line);
1345 if (lnum < curbuf->b_ml.ml_line_count)
1346 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1347
1348 p = buf + skip;
1349 endp = buf + len;
1350 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001351 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001352 /* When searching backward don't search after the cursor. */
1353 if (dir == BACKWARD
1354 && lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001355 && (colnr_T)(p - buf) >= curwin->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001356 break;
1357
1358 /* start of word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00001359 attr = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001360 len = spell_check(curwin, p, &attr);
1361
1362 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001363 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001364 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001365 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001366 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001367 /* When searching forward only accept a bad word after
1368 * the cursor. */
1369 if (dir == BACKWARD
1370 || lnum > curwin->w_cursor.lnum
1371 || (lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001372 && (colnr_T)(curline ? p - buf + len
1373 : p - buf)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001374 > curwin->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001375 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001376 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001377 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001378 col = p - buf;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001379 (void)syn_get_id(lnum, (colnr_T)col,
1380 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001381 }
1382 else
1383 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001384
Bram Moolenaar51485f02005-06-04 21:55:20 +00001385 if (can_spell)
1386 {
1387 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001388 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001389#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001390 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001391#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001392 if (dir == FORWARD)
1393 {
1394 /* No need to search further. */
1395 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001396 vim_free(buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001397 return OK;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001398 }
1399 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001400 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001401 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001402 }
1403
Bram Moolenaar51485f02005-06-04 21:55:20 +00001404 /* advance to character after the word */
1405 p += len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001406 }
1407
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001408 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001409 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001410
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001411 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001412 if (dir == BACKWARD)
1413 {
1414 if (found_pos.lnum != 0)
1415 {
1416 /* Use the last match in the line. */
1417 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001418 vim_free(buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001419 return OK;
1420 }
1421 if (lnum == 1)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001422 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001423 --lnum;
1424 }
1425 else
1426 {
1427 if (lnum == curbuf->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001428 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001429 ++lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001430
1431 /* Skip the characters at the start of the next line that were
1432 * included in a match crossing line boundaries. */
1433 if (attr == 0)
1434 skip = p - endp;
1435 else
1436 skip = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001437 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001438
1439 line_breakcheck();
1440 }
1441
Bram Moolenaar0c405862005-06-22 22:26:26 +00001442 vim_free(buf);
1443 return FAIL;
1444}
1445
1446/*
1447 * For spell checking: concatenate the start of the following line "line" into
1448 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
1449 */
1450 void
1451spell_cat_line(buf, line, maxlen)
1452 char_u *buf;
1453 char_u *line;
1454 int maxlen;
1455{
1456 char_u *p;
1457 int n;
1458
1459 p = skipwhite(line);
1460 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1461 p = skipwhite(p + 1);
1462
1463 if (*p != NUL)
1464 {
1465 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001466 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001467 n = p - line;
1468 if (n >= maxlen)
1469 n = maxlen - 1;
1470 vim_memset(buf + 1, ' ', n);
1471 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001472}
1473
1474/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001475 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001476 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001477 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001478 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001479spell_load_lang(lang)
1480 char_u *lang;
1481{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001482 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001483 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001484 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001485
Bram Moolenaarb765d632005-06-07 21:00:02 +00001486 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001487 * It's truncated when an error is detected. */
1488 STRCPY(langcp, lang);
1489
Bram Moolenaarb765d632005-06-07 21:00:02 +00001490 /*
1491 * Find the first spell file for "lang" in 'runtimepath' and load it.
1492 */
1493 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
1494 "spell/%s.%s.spl", lang, spell_enc());
1495 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001496
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001497 if (r == FAIL && *langcp != NUL)
1498 {
1499 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001500 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001501 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001502 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001503 }
1504
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001505 if (r == FAIL)
1506 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
1507 fname_enc + 6);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001508 else if (*langcp != NUL)
1509 {
1510 /* Load all the additions. */
1511 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
1512 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
1513 }
1514}
1515
1516/*
1517 * Return the encoding used for spell checking: Use 'encoding', except that we
1518 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1519 */
1520 static char_u *
1521spell_enc()
1522{
1523
1524#ifdef FEAT_MBYTE
1525 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1526 return p_enc;
1527#endif
1528 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001529}
1530
1531/*
1532 * Allocate a new slang_T.
1533 * Caller must fill "sl_next".
1534 */
1535 static slang_T *
1536slang_alloc(lang)
1537 char_u *lang;
1538{
1539 slang_T *lp;
1540
Bram Moolenaar51485f02005-06-04 21:55:20 +00001541 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001542 if (lp != NULL)
1543 {
1544 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001545 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001546 ga_init2(&lp->sl_sal, sizeof(salitem_T), 10);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001547 }
1548 return lp;
1549}
1550
1551/*
1552 * Free the contents of an slang_T and the structure itself.
1553 */
1554 static void
1555slang_free(lp)
1556 slang_T *lp;
1557{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001558 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001559 vim_free(lp->sl_fname);
1560 slang_clear(lp);
1561 vim_free(lp);
1562}
1563
1564/*
1565 * Clear an slang_T so that the file can be reloaded.
1566 */
1567 static void
1568slang_clear(lp)
1569 slang_T *lp;
1570{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001571 garray_T *gap;
1572 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001573 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001574 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001575
Bram Moolenaar51485f02005-06-04 21:55:20 +00001576 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001577 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001578 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001579 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001580 vim_free(lp->sl_pbyts);
1581 lp->sl_pbyts = NULL;
1582
Bram Moolenaar51485f02005-06-04 21:55:20 +00001583 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001584 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001585 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001586 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001587 vim_free(lp->sl_pidxs);
1588 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001589
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001590 gap = &lp->sl_rep;
1591 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001592 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001593 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1594 vim_free(ftp->ft_from);
1595 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001596 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001597 ga_clear(gap);
1598
1599 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001600 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001601 {
1602 /* "ga_len" is set to 1 without adding an item for latin1 */
1603 if (gap->ga_data != NULL)
1604 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
1605 for (i = 0; i < gap->ga_len; ++i)
1606 vim_free(((int **)gap->ga_data)[i]);
1607 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001608 else
1609 /* SAL items: free salitem_T items */
1610 while (gap->ga_len > 0)
1611 {
1612 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1613 vim_free(smp->sm_lead);
1614 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
1615 vim_free(smp->sm_to);
1616#ifdef FEAT_MBYTE
1617 vim_free(smp->sm_lead_w);
1618 vim_free(smp->sm_oneof_w);
1619 vim_free(smp->sm_to_w);
1620#endif
1621 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001622 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001623
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001624 for (i = 0; i < lp->sl_prefixcnt; ++i)
1625 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001626 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001627 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001628 lp->sl_prefprog = NULL;
1629
1630 vim_free(lp->sl_midword);
1631 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001632
Bram Moolenaarea424162005-06-16 21:51:00 +00001633#ifdef FEAT_MBYTE
1634 {
1635 int todo = lp->sl_map_hash.ht_used;
1636 hashitem_T *hi;
1637
1638 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
1639 if (!HASHITEM_EMPTY(hi))
1640 {
1641 --todo;
1642 vim_free(hi->hi_key);
1643 }
1644 }
1645 hash_clear(&lp->sl_map_hash);
1646#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001647}
1648
1649/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001650 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001651 * Invoked through do_in_runtimepath().
1652 */
1653 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00001654spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001655 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001656 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001657{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001658 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001659}
1660
1661/*
1662 * Load one spell file and store the info into a slang_T.
1663 *
1664 * This is invoked in two ways:
1665 * - From spell_load_cb() to load a spell file for the first time. "lang" is
1666 * the language name, "old_lp" is NULL. Will allocate an slang_T.
1667 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
1668 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001669 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00001670 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001671 static slang_T *
1672spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001673 char_u *fname;
1674 char_u *lang;
1675 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001676 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001677{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001678 FILE *fd;
1679 char_u buf[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001680 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001681 char_u *bp;
1682 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001683 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001684 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001685 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001686 int round;
1687 char_u *save_sourcing_name = sourcing_name;
1688 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001689 int cnt, ccnt;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001690 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001691 slang_T *lp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001692 garray_T *gap;
1693 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001694 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001695 short *first;
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:
Bram Moolenaar7887d882005-07-01 22:33:52 +00001759 EMSG(_(e_spell_trunc));
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 Moolenaar7887d882005-07-01 22:33:52 +00001775 /* <charflagslen> <charflags> */
1776 p = read_cnt_string(fd, 1, &cnt);
1777 if (cnt == FAIL)
1778 goto endFAIL;
1779
1780 /* <fcharslen> <fchars> */
1781 fol = read_cnt_string(fd, 2, &cnt);
1782 if (cnt == FAIL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001783 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001784 vim_free(p);
Bram Moolenaar7887d882005-07-01 22:33:52 +00001785 goto endFAIL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001786 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00001787
1788 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
1789 if (p != NULL && fol != NULL)
1790 i = set_spell_charflags(p, fol);
1791
1792 vim_free(p);
1793 vim_free(fol);
1794
1795 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
1796 if ((p == NULL) != (fol == NULL))
1797 goto formerr;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001798
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001799 /* <midwordlen> <midword> */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001800 lp->sl_midword = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar7887d882005-07-01 22:33:52 +00001801 if (cnt == FAIL)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001802 goto endFAIL;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001803
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001804 /* <prefcondcnt> <prefcond> ... */
1805 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
1806 if (cnt > 0)
1807 {
1808 lp->sl_prefprog = (regprog_T **)alloc_clear(
1809 (unsigned)sizeof(regprog_T *) * cnt);
1810 if (lp->sl_prefprog == NULL)
1811 goto endFAIL;
1812 lp->sl_prefixcnt = cnt;
1813
1814 for (i = 0; i < cnt; ++i)
1815 {
1816 /* <prefcond> : <condlen> <condstr> */
1817 n = getc(fd); /* <condlen> */
1818 if (n < 0)
1819 goto formerr;
1820 /* When <condlen> is zero we have an empty condition. Otherwise
1821 * compile the regexp program used to check for the condition. */
1822 if (n > 0)
1823 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001824 buf[0] = '^'; /* always match at one position only */
1825 p = buf + 1;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001826 while (n-- > 0)
1827 *p++ = getc(fd); /* <condstr> */
1828 *p = NUL;
1829 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
1830 }
1831 }
1832 }
1833
1834
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001835 /* <SUGGEST> : <repcount> <rep> ...
1836 * <salflags> <salcount> <sal> ...
1837 * <maplen> <mapstr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001838
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001839 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
1840 if (cnt < 0)
1841 goto formerr;
1842
1843 gap = &lp->sl_rep;
1844 if (ga_grow(gap, cnt) == FAIL)
1845 goto endFAIL;
1846
1847 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
1848 for (; gap->ga_len < cnt; ++gap->ga_len)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001849 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001850 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
Bram Moolenaar7887d882005-07-01 22:33:52 +00001851 ftp->ft_from = read_cnt_string(fd, 1, &i);
1852 if (i == FAIL)
1853 goto endFAIL;
1854 ftp->ft_to = read_cnt_string(fd, 1, &i);
1855 if (i == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001856 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00001857 vim_free(ftp->ft_from);
1858 goto endFAIL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001859 }
1860 }
1861
1862 /* Fill the first-index table. */
1863 first = lp->sl_rep_first;
1864 for (i = 0; i < 256; ++i)
1865 first[i] = -1;
1866 for (i = 0; i < gap->ga_len; ++i)
1867 {
1868 ftp = &((fromto_T *)gap->ga_data)[i];
1869 if (first[*ftp->ft_from] == -1)
1870 first[*ftp->ft_from] = i;
1871 }
1872
1873 i = getc(fd); /* <salflags> */
1874 if (i & SAL_F0LLOWUP)
1875 lp->sl_followup = TRUE;
1876 if (i & SAL_COLLAPSE)
1877 lp->sl_collapse = TRUE;
1878 if (i & SAL_REM_ACCENTS)
1879 lp->sl_rem_accents = TRUE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001880 if (i & SAL_SOFO)
1881 lp->sl_sofo = TRUE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001882
1883 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
1884 if (cnt < 0)
1885 goto formerr;
1886
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001887 if (lp->sl_sofo)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001888 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001889 /*
1890 * SOFOFROM and SOFOTO items come in one <salfrom> and <salto>
1891 */
1892 if (cnt != 1)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001893 goto formerr;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001894
Bram Moolenaar7887d882005-07-01 22:33:52 +00001895 /* <salfromlen> <salfrom> */
1896 bp = read_cnt_string(fd, 2, &cnt);
1897 if (cnt == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001898 goto endFAIL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001899
Bram Moolenaar7887d882005-07-01 22:33:52 +00001900 /* <saltolen> <salto> */
1901 fol = read_cnt_string(fd, 2, &cnt);
1902 if (cnt == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001903 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001904 vim_free(bp);
1905 goto endFAIL;
1906 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001907
Bram Moolenaar7887d882005-07-01 22:33:52 +00001908 /* Store the info in lp->sl_sal and/or lp->sl_sal_first. */
1909 i = set_sofo(lp, bp, fol);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001910
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001911 vim_free(bp);
1912 vim_free(fol);
Bram Moolenaar7887d882005-07-01 22:33:52 +00001913 if (i == FAIL)
1914 goto formerr;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001915 }
1916 else
1917 {
1918 /*
1919 * SAL items
1920 */
1921 gap = &lp->sl_sal;
1922 if (ga_grow(gap, cnt) == FAIL)
1923 goto endFAIL;
1924
1925 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
1926 for (; gap->ga_len < cnt; ++gap->ga_len)
1927 {
1928 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
1929 ccnt = getc(fd); /* <salfromlen> */
1930 if (ccnt < 0)
1931 goto formerr;
1932 if ((p = alloc(ccnt + 2)) == NULL)
1933 goto endFAIL;
1934 smp->sm_lead = p;
1935
1936 /* Read up to the first special char into sm_lead. */
1937 for (i = 0; i < ccnt; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001938 {
1939 c = getc(fd); /* <salfrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001940 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001941 break;
1942 *p++ = c;
1943 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001944 smp->sm_leadlen = p - smp->sm_lead;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001945 *p++ = NUL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001946
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001947 /* Put (abc) chars in sm_oneof, if any. */
1948 if (c == '(')
1949 {
1950 smp->sm_oneof = p;
1951 for (++i; i < ccnt; ++i)
1952 {
1953 c = getc(fd); /* <salfrom> */
1954 if (c == ')')
1955 break;
1956 *p++ = c;
1957 }
1958 *p++ = NUL;
1959 if (++i < ccnt)
1960 c = getc(fd);
1961 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00001962 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001963 smp->sm_oneof = NULL;
1964
1965 /* Any following chars go in sm_rules. */
1966 smp->sm_rules = p;
1967 if (i < ccnt)
1968 /* store the char we got while checking for end of sm_lead */
1969 *p++ = c;
1970 for (++i; i < ccnt; ++i)
1971 *p++ = getc(fd); /* <salfrom> */
1972 *p++ = NUL;
1973
Bram Moolenaar7887d882005-07-01 22:33:52 +00001974 /* <saltolen> <salto> */
1975 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
1976 if (ccnt == FAIL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00001977 {
1978 vim_free(smp->sm_lead);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001979 goto formerr;
1980 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001981
Bram Moolenaara1ba8112005-06-28 23:23:32 +00001982#ifdef FEAT_MBYTE
1983 if (has_mbyte)
1984 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001985 /* convert the multi-byte strings to wide char strings */
1986 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
1987 smp->sm_leadlen = mb_charlen(smp->sm_lead);
1988 if (smp->sm_oneof == NULL)
1989 smp->sm_oneof_w = NULL;
1990 else
1991 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
1992 smp->sm_to_w = mb_str2wide(smp->sm_to);
1993 if (smp->sm_lead_w == NULL
1994 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
1995 || smp->sm_to_w == NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00001996 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001997 vim_free(smp->sm_lead);
1998 vim_free(smp->sm_to);
1999 vim_free(smp->sm_lead_w);
2000 vim_free(smp->sm_oneof_w);
2001 vim_free(smp->sm_to_w);
2002 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002003 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002004 }
2005#endif
2006 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002007
2008 /* Fill the first-index table. */
Bram Moolenaar7887d882005-07-01 22:33:52 +00002009 set_sal_first(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002010 }
2011
Bram Moolenaar7887d882005-07-01 22:33:52 +00002012 /* <maplen> <mapstr> */
2013 p = read_cnt_string(fd, 2, &cnt);
2014 if (cnt == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002015 goto endFAIL;
Bram Moolenaarea424162005-06-16 21:51:00 +00002016 set_map_str(lp, p);
2017 vim_free(p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002018
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002019
Bram Moolenaar51485f02005-06-04 21:55:20 +00002020 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002021 * round 2: <KWORDTREE>
2022 * round 3: <PREFIXTREE> */
2023 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002024 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002025 /* The tree size was computed when writing the file, so that we can
2026 * allocate it as one long block. <nodecount> */
2027 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2028 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002029 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002030 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002031 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002032 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002033 bp = lalloc((long_u)len, TRUE);
2034 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002035 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002036 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002037 lp->sl_fbyts = bp;
2038 else if (round == 2)
2039 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002040 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002041 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002042
2043 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002044 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
2045 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002046 goto endFAIL;
2047 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002048 lp->sl_fidxs = ip;
2049 else if (round == 2)
2050 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002051 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002052 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002053
2054 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002055 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002056 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002057 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002058 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002059 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002060 }
2061 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002062
Bram Moolenaarb765d632005-06-07 21:00:02 +00002063 /* For a new file link it in the list of spell files. */
2064 if (old_lp == NULL)
2065 {
2066 lp->sl_next = first_lang;
2067 first_lang = lp;
2068 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002069
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002070 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002071
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002072endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002073 if (lang != NULL)
2074 /* truncating the name signals the error to spell_load_lang() */
2075 *lang = NUL;
2076 if (lp != NULL && old_lp == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002077 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002078 slang_free(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002079 lp = NULL;
2080 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002081
2082endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002083 if (fd != NULL)
2084 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002085 sourcing_name = save_sourcing_name;
2086 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002087
2088 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002089}
2090
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002091/*
2092 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002093 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002094 * Returns NULL when the count is zero.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002095 * Sets "*errp" to FAIL when there is an error, OK otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002096 */
2097 static char_u *
2098read_cnt_string(fd, cnt_bytes, errp)
2099 FILE *fd;
2100 int cnt_bytes;
2101 int *errp;
2102{
2103 int cnt = 0;
2104 int i;
2105 char_u *str;
2106
2107 /* read the length bytes, MSB first */
2108 for (i = 0; i < cnt_bytes; ++i)
2109 cnt = (cnt << 8) + getc(fd);
2110 if (cnt < 0)
2111 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00002112 EMSG(_(e_spell_trunc));
2113 *errp = FAIL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002114 return NULL;
2115 }
2116
2117 /* allocate memory */
2118 str = alloc((unsigned)cnt + 1);
2119 if (str == NULL)
2120 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00002121 *errp = FAIL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002122 return NULL;
2123 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00002124 *errp = OK;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002125
2126 /* Read the string. Doesn't check for truncated file. */
2127 for (i = 0; i < cnt; ++i)
2128 str[i] = getc(fd);
2129 str[i] = NUL;
2130
2131 return str;
2132}
2133
Bram Moolenaar7887d882005-07-01 22:33:52 +00002134/*
2135 * Set the SOFOFROM and SOFOTO items in language "lp".
2136 * Returns FAIL when there is something wrong.
2137 */
2138 static int
2139set_sofo(lp, from, to)
2140 slang_T *lp;
2141 char_u *from;
2142 char_u *to;
2143{
2144 int i;
2145
2146#ifdef FEAT_MBYTE
2147 garray_T *gap;
2148 char_u *s;
2149 char_u *p;
2150 int c;
2151 int *inp;
2152
2153 if (has_mbyte)
2154 {
2155 /* Use "sl_sal" as an array with 256 pointers to a list of wide
2156 * characters. The index is the low byte of the character.
2157 * The list contains from-to pairs with a terminating NUL.
2158 * sl_sal_first[] is used for latin1 "from" characters. */
2159 gap = &lp->sl_sal;
2160 ga_init2(gap, sizeof(int *), 1);
2161 if (ga_grow(gap, 256) == FAIL)
2162 return FAIL;
2163 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
2164 gap->ga_len = 256;
2165
2166 /* First count the number of items for each list. Temporarily use
2167 * sl_sal_first[] for this. */
2168 for (p = from, s = to; *p != NUL && *s != NUL; )
2169 {
2170 c = mb_ptr2char_adv(&p);
2171 mb_ptr_adv(s);
2172 if (c >= 256)
2173 ++lp->sl_sal_first[c & 0xff];
2174 }
2175 if (*p != NUL || *s != NUL) /* lengths differ */
2176 return FAIL;
2177
2178 /* Allocate the lists. */
2179 for (i = 0; i < 256; ++i)
2180 if (lp->sl_sal_first[i] > 0)
2181 {
2182 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
2183 if (p == NULL)
2184 return FAIL;
2185 ((int **)gap->ga_data)[i] = (int *)p;
2186 *(int *)p = 0;
2187 }
2188
2189 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
2190 * list. */
2191 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
2192 for (p = from, s = to; *p != NUL && *s != NUL; )
2193 {
2194 c = mb_ptr2char_adv(&p);
2195 i = mb_ptr2char_adv(&s);
2196 if (c >= 256)
2197 {
2198 /* Append the from-to chars at the end of the list with
2199 * the low byte. */
2200 inp = ((int **)gap->ga_data)[c & 0xff];
2201 while (*inp != 0)
2202 ++inp;
2203 *inp++ = c; /* from char */
2204 *inp++ = i; /* to char */
2205 *inp++ = NUL; /* NUL at the end */
2206 }
2207 else
2208 /* mapping byte to char is done in sl_sal_first[] */
2209 lp->sl_sal_first[c] = i;
2210 }
2211 }
2212 else
2213#endif
2214 {
2215 /* mapping bytes to bytes is done in sl_sal_first[] */
2216 if (STRLEN(from) != STRLEN(to))
2217 return FAIL;
2218
2219 for (i = 0; to[i] != NUL; ++i)
2220 lp->sl_sal_first[from[i]] = to[i];
2221 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
2222 }
2223
2224 return OK;
2225}
2226
2227/*
2228 * Fill the first-index table for "lp".
2229 */
2230 static void
2231set_sal_first(lp)
2232 slang_T *lp;
2233{
2234 salfirst_T *sfirst;
2235 int i;
2236 salitem_T *smp;
2237 int c;
2238 garray_T *gap = &lp->sl_sal;
2239
2240 sfirst = lp->sl_sal_first;
2241 for (i = 0; i < 256; ++i)
2242 sfirst[i] = -1;
2243 smp = (salitem_T *)gap->ga_data;
2244 for (i = 0; i < gap->ga_len; ++i)
2245 {
2246#ifdef FEAT_MBYTE
2247 if (has_mbyte)
2248 /* Use the lowest byte of the first character. For latin1 it's
2249 * the character, for other encodings it should differ for most
2250 * characters. */
2251 c = *smp[i].sm_lead_w & 0xff;
2252 else
2253#endif
2254 c = *smp[i].sm_lead;
2255 if (sfirst[c] == -1)
2256 {
2257 sfirst[c] = i;
2258#ifdef FEAT_MBYTE
2259 if (has_mbyte)
2260 {
2261 int n;
2262
2263 /* Make sure all entries with this byte are following each
2264 * other. Move the ones that are in the wrong position. Do
2265 * keep the same ordering! */
2266 while (i + 1 < gap->ga_len
2267 && (*smp[i + 1].sm_lead_w & 0xff) == c)
2268 /* Skip over entry with same index byte. */
2269 ++i;
2270
2271 for (n = 1; i + n < gap->ga_len; ++n)
2272 if ((*smp[i + n].sm_lead_w & 0xff) == c)
2273 {
2274 salitem_T tsal;
2275
2276 /* Move entry with same index byte after the entries
2277 * we already found. */
2278 ++i;
2279 --n;
2280 tsal = smp[i + n];
2281 mch_memmove(smp + i + 1, smp + i,
2282 sizeof(salitem_T) * n);
2283 smp[i] = tsal;
2284 }
2285 }
2286#endif
2287 }
2288 }
2289}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002290
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002291#ifdef FEAT_MBYTE
2292/*
2293 * Turn a multi-byte string into a wide character string.
2294 * Return it in allocated memory (NULL for out-of-memory)
2295 */
2296 static int *
2297mb_str2wide(s)
2298 char_u *s;
2299{
2300 int *res;
2301 char_u *p;
2302 int i = 0;
2303
2304 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
2305 if (res != NULL)
2306 {
2307 for (p = s; *p != NUL; )
2308 res[i++] = mb_ptr2char_adv(&p);
2309 res[i] = NUL;
2310 }
2311 return res;
2312}
2313#endif
2314
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002315/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002316 * Read one row of siblings from the spell file and store it in the byte array
2317 * "byts" and index array "idxs". Recursively read the children.
2318 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00002319 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00002320 *
2321 * Returns the index follosing the siblings.
2322 * Returns -1 if the file is shorter than expected.
2323 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002324 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002325 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002326read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002327 FILE *fd;
2328 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002329 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002330 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002331 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002332 int prefixtree; /* TRUE for reading PREFIXTREE */
2333 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002334{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002335 int len;
2336 int i;
2337 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002338 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002339 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002340 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002341#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002342
Bram Moolenaar51485f02005-06-04 21:55:20 +00002343 len = getc(fd); /* <siblingcount> */
2344 if (len <= 0)
2345 return -1;
2346
2347 if (startidx + len >= maxidx)
2348 return -2;
2349 byts[idx++] = len;
2350
2351 /* Read the byte values, flag/region bytes and shared indexes. */
2352 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002353 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002354 c = getc(fd); /* <byte> */
2355 if (c < 0)
2356 return -1;
2357 if (c <= BY_SPECIAL)
2358 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002359 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002360 {
2361 /* No flags, all regions. */
2362 idxs[idx] = 0;
2363 c = 0;
2364 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002365 else if (c == BY_FLAGS || c == BY_NOFLAGS)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002366 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002367 if (prefixtree)
2368 {
2369 /* Read the prefix ID and the condition nr. In idxs[]
2370 * store the prefix ID in the low byte, the condition
2371 * index shifted up 8 bits. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002372 c2 = getc(fd); /* <prefixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002373 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
2374 if (n >= maxprefcondnr)
2375 return -2;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002376 c2 += (n << 8);
2377 if (c == BY_NOFLAGS)
2378 c = c2;
2379 else
2380 c = c2 | WF_RAREPFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002381 }
2382 else
2383 {
2384 /* Read flags and optional region and prefix ID. In
2385 * idxs[] the flags go in the low byte, region above that
2386 * and prefix ID above the region. */
2387 c = getc(fd); /* <flags> */
2388 if (c & WF_REGION)
2389 c = (getc(fd) << 8) + c; /* <region> */
2390 if (c & WF_PFX)
2391 c = (getc(fd) << 16) + c; /* <prefixID> */
2392 }
2393
Bram Moolenaar51485f02005-06-04 21:55:20 +00002394 idxs[idx] = c;
2395 c = 0;
2396 }
2397 else /* c == BY_INDEX */
2398 {
2399 /* <nodeidx> */
2400 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2401 if (n < 0 || n >= maxidx)
2402 return -2;
2403 idxs[idx] = n + SHARED_MASK;
2404 c = getc(fd); /* <xbyte> */
2405 }
2406 }
2407 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002408 }
2409
Bram Moolenaar51485f02005-06-04 21:55:20 +00002410 /* Recursively read the children for non-shared siblings.
2411 * Skip the end-of-word ones (zero byte value) and the shared ones (and
2412 * remove SHARED_MASK) */
2413 for (i = 1; i <= len; ++i)
2414 if (byts[startidx + i] != 0)
2415 {
2416 if (idxs[startidx + i] & SHARED_MASK)
2417 idxs[startidx + i] &= ~SHARED_MASK;
2418 else
2419 {
2420 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002421 idx = read_tree(fd, byts, idxs, maxidx, idx,
2422 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002423 if (idx < 0)
2424 break;
2425 }
2426 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002427
Bram Moolenaar51485f02005-06-04 21:55:20 +00002428 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002429}
2430
2431/*
2432 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002433 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002434 */
2435 char_u *
2436did_set_spelllang(buf)
2437 buf_T *buf;
2438{
2439 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002440 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002441 char_u *region;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002442 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002443 int region_mask;
2444 slang_T *lp;
2445 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002446 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002447 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002448 int load_spf;
2449 int len;
2450 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002451 int round;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002452
2453 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002454 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002455
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002456 /* Make the name of the .spl file associated with 'spellfile'. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002457 if (*buf->b_p_spf == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002458 load_spf = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002459 else
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002460 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002461 vim_snprintf((char *)spf_name, sizeof(spf_name), "%s.spl",
2462 buf->b_p_spf);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002463 load_spf = TRUE;
2464 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002465
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002466 /* loop over comma separated language names. */
2467 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002468 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002469 /* Get one language name. */
2470 copy_option_part(&splp, lang, MAXWLEN, ",");
2471
Bram Moolenaar5482f332005-04-17 20:18:43 +00002472 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002473 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002474
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002475 /* If the name ends in ".spl" use it as the name of the spell file.
2476 * If there is a region name let "region" point to it and remove it
2477 * from the name. */
2478 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2479 {
2480 filename = TRUE;
2481
2482 /* Check if we loaded this language before. */
2483 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2484 if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME)
2485 break;
2486 }
2487 else
2488 {
2489 filename = FALSE;
2490 if (len > 3 && lang[len - 3] == '_')
2491 {
2492 region = lang + len - 2;
2493 len -= 3;
2494 lang[len] = NUL;
2495 }
2496
2497 /* Check if we loaded this language before. */
2498 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2499 if (STRICMP(lang, lp->sl_name) == 0)
2500 break;
2501 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002502
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002503 /* If not found try loading the language now. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002504 if (lp == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002505 {
2506 if (filename)
2507 (void)spell_load_file(lang, lang, NULL, FALSE);
2508 else
2509 spell_load_lang(lang);
2510 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002511
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002512 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002513 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002514 */
2515 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002516 if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME
2517 : STRICMP(lang, lp->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002518 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002519 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002520 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002521 {
2522 /* find region in sl_regions */
2523 c = find_region(lp->sl_regions, region);
2524 if (c == REGION_ALL)
2525 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002526 if (!lp->sl_add)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002527 smsg((char_u *)
2528 _("Warning: region %s not supported"),
2529 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002530 }
2531 else
2532 region_mask = 1 << c;
2533 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002534
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002535 if (ga_grow(&ga, 1) == FAIL)
2536 {
2537 ga_clear(&ga);
2538 return e_outofmem;
2539 }
2540 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2541 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2542 ++ga.ga_len;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002543 use_midword(lp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002544
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002545 /* Check if this is the spell file related to 'spellfile'. */
2546 if (load_spf && fullpathcmp(spf_name, lp->sl_fname, FALSE)
2547 == FPC_SAME)
2548 load_spf = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002549 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002550 }
2551
Bram Moolenaar7887d882005-07-01 22:33:52 +00002552 /* round 1: load 'spellfile', if needed.
2553 * round 2: load temp_wordlist, if possible. */
2554 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002555 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00002556 if (round == 1)
2557 {
2558 /* Make sure the 'spellfile' file is loaded. It may be in
2559 * 'runtimepath', then it's probably loaded above already.
2560 * Otherwise load it here. */
2561 if (!load_spf)
2562 continue;
2563 }
2564 else
2565 {
2566 if (temp_wordlist == NULL)
2567 continue;
2568 vim_snprintf((char *)spf_name, sizeof(spf_name), "%s.%s.spl",
2569 temp_wordlist, spell_enc());
2570 }
2571
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002572 /* Check if it was loaded already. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002573 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2574 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
2575 break;
2576 if (lp == NULL)
2577 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002578 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaar7887d882005-07-01 22:33:52 +00002579 * region name, the region is ignored otherwise. for
2580 * temp_wordlist use an arbitrary name. */
2581 if (round == 1)
2582 {
2583 vim_strncpy(lang, gettail(buf->b_p_spf), MAXWLEN);
2584 p = vim_strchr(lang, '.');
2585 if (p != NULL)
2586 *p = NUL; /* truncate at ".encoding.add" */
2587 }
2588 else
2589 STRCPY(lang, "temp wordlist");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002590 lp = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002591 }
2592 if (lp != NULL && ga_grow(&ga, 1) == OK)
2593 {
2594 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2595 LANGP_ENTRY(ga, ga.ga_len)->lp_region = REGION_ALL;
2596 ++ga.ga_len;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002597 use_midword(lp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002598 }
2599 }
2600
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002601 /* Add a NULL entry to mark the end of the list. */
2602 if (ga_grow(&ga, 1) == FAIL)
2603 {
2604 ga_clear(&ga);
2605 return e_outofmem;
2606 }
2607 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
2608 ++ga.ga_len;
2609
2610 /* Everything is fine, store the new b_langp value. */
2611 ga_clear(&buf->b_langp);
2612 buf->b_langp = ga;
2613
2614 return NULL;
2615}
2616
2617/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002618 * Clear the midword characters for buffer "buf".
2619 */
2620 static void
2621clear_midword(buf)
2622 buf_T *buf;
2623{
2624 vim_memset(buf->b_spell_ismw, 0, 256);
2625#ifdef FEAT_MBYTE
2626 vim_free(buf->b_spell_ismw_mb);
2627 buf->b_spell_ismw_mb = NULL;
2628#endif
2629}
2630
2631/*
2632 * Use the "sl_midword" field of language "lp" for buffer "buf".
2633 * They add up to any currently used midword characters.
2634 */
2635 static void
2636use_midword(lp, buf)
2637 slang_T *lp;
2638 buf_T *buf;
2639{
2640 char_u *p;
2641
2642 for (p = lp->sl_midword; *p != NUL; )
2643#ifdef FEAT_MBYTE
2644 if (has_mbyte)
2645 {
2646 int c, l, n;
2647 char_u *bp;
2648
2649 c = mb_ptr2char(p);
2650 l = mb_ptr2len_check(p);
2651 if (c < 256)
2652 buf->b_spell_ismw[c] = TRUE;
2653 else if (buf->b_spell_ismw_mb == NULL)
2654 /* First multi-byte char in "b_spell_ismw_mb". */
2655 buf->b_spell_ismw_mb = vim_strnsave(p, l);
2656 else
2657 {
2658 /* Append multi-byte chars to "b_spell_ismw_mb". */
2659 n = STRLEN(buf->b_spell_ismw_mb);
2660 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
2661 if (bp != NULL)
2662 {
2663 vim_free(buf->b_spell_ismw_mb);
2664 buf->b_spell_ismw_mb = bp;
2665 vim_strncpy(bp + n, p, l);
2666 }
2667 }
2668 p += l;
2669 }
2670 else
2671#endif
2672 buf->b_spell_ismw[*p++] = TRUE;
2673}
2674
2675/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002676 * Find the region "region[2]" in "rp" (points to "sl_regions").
2677 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002678 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002679 */
2680 static int
2681find_region(rp, region)
2682 char_u *rp;
2683 char_u *region;
2684{
2685 int i;
2686
2687 for (i = 0; ; i += 2)
2688 {
2689 if (rp[i] == NUL)
2690 return REGION_ALL;
2691 if (rp[i] == region[0] && rp[i + 1] == region[1])
2692 break;
2693 }
2694 return i / 2;
2695}
2696
2697/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002698 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002699 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002700 * Word WF_ONECAP
2701 * W WORD WF_ALLCAP
2702 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002703 */
2704 static int
2705captype(word, end)
2706 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002707 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002708{
2709 char_u *p;
2710 int c;
2711 int firstcap;
2712 int allcap;
2713 int past_second = FALSE; /* past second word char */
2714
2715 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002716 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002717 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002718 return 0; /* only non-word characters, illegal word */
2719#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00002720 if (has_mbyte)
2721 c = mb_ptr2char_adv(&p);
2722 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002723#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00002724 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002725 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002726
2727 /*
2728 * Need to check all letters to find a word with mixed upper/lower.
2729 * But a word with an upper char only at start is a ONECAP.
2730 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002731 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002732 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002733 {
2734#ifdef FEAT_MBYTE
2735 c = mb_ptr2char(p);
2736#else
2737 c = *p;
2738#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002739 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002740 {
2741 /* UUl -> KEEPCAP */
2742 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002743 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002744 allcap = FALSE;
2745 }
2746 else if (!allcap)
2747 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002748 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002749 past_second = TRUE;
2750 }
2751
2752 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002753 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002754 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002755 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002756 return 0;
2757}
2758
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002759# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
2760/*
2761 * Free all languages.
2762 */
2763 void
2764spell_free_all()
2765{
2766 slang_T *lp;
2767 buf_T *buf;
2768
2769 /* Go through all buffers and handle 'spelllang'. */
2770 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2771 ga_clear(&buf->b_langp);
2772
2773 while (first_lang != NULL)
2774 {
2775 lp = first_lang;
2776 first_lang = lp->sl_next;
2777 slang_free(lp);
2778 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002779
Bram Moolenaar7887d882005-07-01 22:33:52 +00002780 if (temp_wordlist != NULL)
2781 {
2782 mch_remove(temp_wordlist);
2783 vim_free(temp_wordlist);
2784 temp_wordlist = NULL;
2785 }
2786
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002787 init_spell_chartab();
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002788}
2789# endif
2790
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002791# if defined(FEAT_MBYTE) || defined(PROTO)
2792/*
2793 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002794 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002795 */
2796 void
2797spell_reload()
2798{
2799 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002800 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002801
Bram Moolenaarea408852005-06-25 22:49:46 +00002802 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002803 init_spell_chartab();
2804
2805 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002806 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002807
2808 /* Go through all buffers and handle 'spelllang'. */
2809 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2810 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002811 /* Only load the wordlists when 'spelllang' is set and there is a
2812 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002813 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002814 {
2815 FOR_ALL_WINDOWS(wp)
2816 if (wp->w_buffer == buf && wp->w_p_spell)
2817 {
2818 (void)did_set_spelllang(buf);
2819# ifdef FEAT_WINDOWS
2820 break;
2821# endif
2822 }
2823 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002824 }
2825}
2826# endif
2827
Bram Moolenaarb765d632005-06-07 21:00:02 +00002828/*
2829 * Reload the spell file "fname" if it's loaded.
2830 */
2831 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002832spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002833 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002834 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002835{
2836 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002837 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002838
Bram Moolenaarb765d632005-06-07 21:00:02 +00002839 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2840 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
2841 {
2842 slang_clear(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002843 (void)spell_load_file(fname, NULL, lp, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002844 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002845 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002846 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002847
2848 /* When "zg" was used and the file wasn't loaded yet, should redo
2849 * 'spelllang' to get it loaded. */
2850 if (added_word && !didit)
2851 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002852}
2853
2854
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002855/*
2856 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002857 */
2858
Bram Moolenaar51485f02005-06-04 21:55:20 +00002859#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002860 and .dic file. */
2861/*
2862 * Main structure to store the contents of a ".aff" file.
2863 */
2864typedef struct afffile_S
2865{
2866 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002867 int af_rar; /* RAR ID for rare word */
2868 int af_kep; /* KEP ID for keep-case word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00002869 int af_bad; /* BAD ID for banned word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002870 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002871 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
2872 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002873} afffile_T;
2874
2875typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002876/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
2877struct affentry_S
2878{
2879 affentry_T *ae_next; /* next affix with same name/number */
2880 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
2881 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002882 char_u *ae_cond; /* condition (NULL for ".") */
2883 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002884 int ae_rare; /* rare affix */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002885};
2886
2887/* Affix header from ".aff" file. Used for af_pref and af_suff. */
2888typedef struct affheader_S
2889{
2890 char_u ah_key[2]; /* key for hashtable == name of affix entry */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002891 int ah_newID; /* prefix ID after renumbering */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002892 int ah_combine; /* suffix may combine with prefix */
2893 affentry_T *ah_first; /* first affix entry */
2894} affheader_T;
2895
2896#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
2897
2898/*
2899 * Structure that is used to store the items in the word tree. This avoids
2900 * the need to keep track of each allocated thing, it's freed all at once
2901 * after ":mkspell" is done.
2902 */
2903#define SBLOCKSIZE 16000 /* size of sb_data */
2904typedef struct sblock_S sblock_T;
2905struct sblock_S
2906{
2907 sblock_T *sb_next; /* next block in list */
2908 int sb_used; /* nr of bytes already in use */
2909 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002910};
2911
2912/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002913 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002914 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002915typedef struct wordnode_S wordnode_T;
2916struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002917{
Bram Moolenaar0c405862005-06-22 22:26:26 +00002918 union /* shared to save space */
2919 {
2920 char_u hashkey[6]; /* room for the hash key */
2921 int index; /* index in written nodes (valid after first
2922 round) */
2923 } wn_u1;
2924 union /* shared to save space */
2925 {
2926 wordnode_T *next; /* next node with same hash key */
2927 wordnode_T *wnode; /* parent node that will write this node */
2928 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002929 wordnode_T *wn_child; /* child (next byte in word) */
2930 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
2931 always sorted) */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002932 char_u wn_byte; /* Byte for this node. NUL for word end */
2933 char_u wn_flags; /* when wn_byte is NUL: WF_ flags */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002934 short wn_region; /* when wn_byte is NUL: region mask; for
2935 PREFIXTREE it's the prefcondnr */
2936 char_u wn_prefixID; /* supported/required prefix ID or 0 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002937};
2938
Bram Moolenaar51485f02005-06-04 21:55:20 +00002939#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002940
Bram Moolenaar51485f02005-06-04 21:55:20 +00002941/*
2942 * Info used while reading the spell files.
2943 */
2944typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002945{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002946 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00002947 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002948 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00002949 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002950 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002951 sblock_T *si_blocks; /* memory blocks used */
2952 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002953 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002954 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002955 int si_region; /* region mask */
2956 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00002957 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002958 int si_verbose; /* verbose messages */
Bram Moolenaar3982c542005-06-08 21:56:31 +00002959 int si_region_count; /* number of regions supported (1 when there
2960 are no regions) */
2961 char_u si_region_name[16]; /* region names (if count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002962
2963 garray_T si_rep; /* list of fromto_T entries from REP lines */
2964 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002965 char_u *si_sofofr; /* SOFOFROM text */
2966 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002967 int si_followup; /* soundsalike: ? */
2968 int si_collapse; /* soundsalike: ? */
2969 int si_rem_accents; /* soundsalike: remove accents */
2970 garray_T si_map; /* MAP info concatenated */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002971 char_u *si_midword; /* MIDWORD chars, alloc'ed string or NULL */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002972 garray_T si_prefcond; /* table with conditions for postponed
2973 * prefixes, each stored as a string */
2974 int si_newID; /* current value for ah_newID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002975} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002976
Bram Moolenaar51485f02005-06-04 21:55:20 +00002977static afffile_T *spell_read_aff __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002978static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002979static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
2980static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00002981static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00002982static void spell_free_aff __ARGS((afffile_T *aff));
2983static int spell_read_dic __ARGS((char_u *fname, spellinfo_T *spin, afffile_T *affile));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002984static char_u *get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, sblock_T **blp));
2985static 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 +00002986static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin));
2987static void *getroom __ARGS((sblock_T **blp, size_t len));
2988static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s));
2989static void free_blocks __ARGS((sblock_T *bl));
2990static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002991static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags, int region, char_u *pfxlist));
2992static 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 +00002993static void wordtree_compress __ARGS((wordnode_T *root, spellinfo_T *spin));
Bram Moolenaar51485f02005-06-04 21:55:20 +00002994static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot));
2995static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar3982c542005-06-08 21:56:31 +00002996static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar0c405862005-06-22 22:26:26 +00002997static void clear_node __ARGS((wordnode_T *node));
2998static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002999static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00003000static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003001
3002/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003003 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00003004 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003005 */
3006 static afffile_T *
Bram Moolenaar51485f02005-06-04 21:55:20 +00003007spell_read_aff(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003008 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003009 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003010{
3011 FILE *fd;
3012 afffile_T *aff;
3013 char_u rline[MAXLINELEN];
3014 char_u *line;
3015 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003016#define MAXITEMCNT 7
3017 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003018 int itemcnt;
3019 char_u *p;
3020 int lnum = 0;
3021 affheader_T *cur_aff = NULL;
3022 int aff_todo = 0;
3023 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003024 char_u *low = NULL;
3025 char_u *fol = NULL;
3026 char_u *upp = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003027 static char *e_affname = N_("Affix name too long in %s line %d: %s");
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003028 int do_rep;
3029 int do_sal;
3030 int do_map;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003031 int do_midword;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003032 int do_sofo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003033 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003034 hashitem_T *hi;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003035
Bram Moolenaar51485f02005-06-04 21:55:20 +00003036 /*
3037 * Open the file.
3038 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003039 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003040 if (fd == NULL)
3041 {
3042 EMSG2(_(e_notopen), fname);
3043 return NULL;
3044 }
3045
Bram Moolenaarb765d632005-06-07 21:00:02 +00003046 if (spin->si_verbose || p_verbose > 2)
3047 {
3048 if (!spin->si_verbose)
3049 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003050 smsg((char_u *)_("Reading affix file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003051 out_flush();
3052 if (!spin->si_verbose)
3053 verbose_leave();
3054 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003055
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003056 /* Only do REP lines when not done in another .aff file already. */
3057 do_rep = spin->si_rep.ga_len == 0;
3058
3059 /* Only do SAL lines when not done in another .aff file already. */
3060 do_sal = spin->si_sal.ga_len == 0;
3061
3062 /* Only do MAP lines when not done in another .aff file already. */
3063 do_map = spin->si_map.ga_len == 0;
3064
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003065 /* Only do MIDWORD line when not done in another .aff file already */
3066 do_midword = spin->si_midword == NULL;
3067
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003068 /* Only do SOFOFROM and SOFOTO when not done in another .aff file already */
3069 do_sofo = spin->si_sofofr == NULL;
3070
Bram Moolenaar51485f02005-06-04 21:55:20 +00003071 /*
3072 * Allocate and init the afffile_T structure.
3073 */
3074 aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003075 if (aff == NULL)
3076 return NULL;
3077 hash_init(&aff->af_pref);
3078 hash_init(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003079
3080 /*
3081 * Read all the lines in the file one by one.
3082 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003083 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003084 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003085 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003086 ++lnum;
3087
3088 /* Skip comment lines. */
3089 if (*rline == '#')
3090 continue;
3091
3092 /* Convert from "SET" to 'encoding' when needed. */
3093 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003094#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003095 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003096 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003097 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003098 if (pc == NULL)
3099 {
3100 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3101 fname, lnum, rline);
3102 continue;
3103 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003104 line = pc;
3105 }
3106 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003107#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003108 {
3109 pc = NULL;
3110 line = rline;
3111 }
3112
3113 /* Split the line up in white separated items. Put a NUL after each
3114 * item. */
3115 itemcnt = 0;
3116 for (p = line; ; )
3117 {
3118 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
3119 ++p;
3120 if (*p == NUL)
3121 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003122 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003123 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003124 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003125 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003126 ++p;
3127 if (*p == NUL)
3128 break;
3129 *p++ = NUL;
3130 }
3131
3132 /* Handle non-empty lines. */
3133 if (itemcnt > 0)
3134 {
3135 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
3136 && aff->af_enc == NULL)
3137 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003138#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003139 /* Setup for conversion from "ENC" to 'encoding'. */
3140 aff->af_enc = enc_canonize(items[1]);
3141 if (aff->af_enc != NULL && !spin->si_ascii
3142 && convert_setup(&spin->si_conv, aff->af_enc,
3143 p_enc) == FAIL)
3144 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
3145 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003146 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003147#else
3148 smsg((char_u *)_("Conversion in %s not supported"), fname);
3149#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003150 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003151 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2)
3152 {
3153 if (do_midword)
3154 spin->si_midword = vim_strsave(items[1]);
3155 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00003156 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
3157 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003158 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003159 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003160 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003161 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003162 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003163 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003164 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
3165 && aff->af_rar == 0)
3166 {
3167 aff->af_rar = items[1][0];
3168 if (items[1][1] != NUL)
3169 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3170 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00003171 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
3172 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003173 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003174 aff->af_kep = items[1][0];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003175 if (items[1][1] != NUL)
3176 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3177 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00003178 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
3179 && aff->af_bad == 0)
3180 {
3181 aff->af_bad = items[1][0];
3182 if (items[1][1] != NUL)
3183 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3184 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003185 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
3186 {
3187 aff->af_pfxpostpone = TRUE;
3188 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003189 else if ((STRCMP(items[0], "PFX") == 0
3190 || STRCMP(items[0], "SFX") == 0)
3191 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003192 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003193 {
Bram Moolenaar8db73182005-06-17 21:51:16 +00003194 /* Myspell allows extra text after the item, but that might
3195 * mean mistakes go unnoticed. Require a comment-starter. */
3196 if (itemcnt > 4 && *items[4] != '#')
3197 smsg((char_u *)_("Trailing text in %s line %d: %s"),
3198 fname, lnum, items[4]);
3199
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003200 /* New affix letter. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003201 cur_aff = (affheader_T *)getroom(&spin->si_blocks,
3202 sizeof(affheader_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003203 if (cur_aff == NULL)
3204 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003205 cur_aff->ah_key[0] = *items[1]; /* TODO: multi-byte? */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003206 cur_aff->ah_key[1] = NUL;
3207 if (items[1][1] != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003208 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003209 if (*items[2] == 'Y')
3210 cur_aff->ah_combine = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003211 else if (*items[2] != 'N')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003212 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
3213 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003214
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003215 if (*items[0] == 'P')
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003216 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003217 tp = &aff->af_pref;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003218 /* Use a new number in the .spl file later, to be able to
3219 * handle multiple .aff files. */
3220 if (aff->af_pfxpostpone)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003221 cur_aff->ah_newID = ++spin->si_newID;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003222 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003223 else
3224 tp = &aff->af_suff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003225 aff_todo = atoi((char *)items[3]);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003226 hi = hash_find(tp, cur_aff->ah_key);
3227 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar51485f02005-06-04 21:55:20 +00003228 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003229 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
3230 fname, lnum, items[1]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003231 aff_todo = 0;
3232 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003233 else
3234 hash_add(tp, cur_aff->ah_key);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003235 }
3236 else if ((STRCMP(items[0], "PFX") == 0
3237 || STRCMP(items[0], "SFX") == 0)
3238 && aff_todo > 0
3239 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003240 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003241 {
3242 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003243 int rare = FALSE;
3244 int lasti = 5;
3245
3246 /* Check for "rare" after the other info. */
3247 if (itemcnt > 5 && STRICMP(items[5], "rare") == 0)
3248 {
3249 rare = TRUE;
3250 lasti = 6;
3251 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003252
Bram Moolenaar8db73182005-06-17 21:51:16 +00003253 /* Myspell allows extra text after the item, but that might
3254 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003255 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar8db73182005-06-17 21:51:16 +00003256 smsg((char_u *)_("Trailing text in %s line %d: %s"),
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003257 fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00003258
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003259 /* New item for an affix letter. */
3260 --aff_todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003261 aff_entry = (affentry_T *)getroom(&spin->si_blocks,
3262 sizeof(affentry_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003263 if (aff_entry == NULL)
3264 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003265 aff_entry->ae_rare = rare;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003266
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003267 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003268 aff_entry->ae_chop = getroom_save(&spin->si_blocks,
3269 items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003270 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003271 aff_entry->ae_add = getroom_save(&spin->si_blocks,
3272 items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003273
Bram Moolenaar51485f02005-06-04 21:55:20 +00003274 /* Don't use an affix entry with non-ASCII characters when
3275 * "spin->si_ascii" is TRUE. */
3276 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00003277 || has_non_ascii(aff_entry->ae_add)))
3278 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003279 aff_entry->ae_next = cur_aff->ah_first;
3280 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003281
3282 if (STRCMP(items[4], ".") != 0)
3283 {
3284 char_u buf[MAXLINELEN];
3285
3286 aff_entry->ae_cond = getroom_save(&spin->si_blocks,
3287 items[4]);
3288 if (*items[0] == 'P')
3289 sprintf((char *)buf, "^%s", items[4]);
3290 else
3291 sprintf((char *)buf, "%s$", items[4]);
3292 aff_entry->ae_prog = vim_regcomp(buf,
3293 RE_MAGIC + RE_STRING);
3294 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003295
3296 /* For postponed prefixes we need an entry in si_prefcond
3297 * for the condition. Use an existing one if possible. */
3298 if (*items[0] == 'P' && aff->af_pfxpostpone
3299 && aff_entry->ae_chop == NULL)
3300 {
3301 int idx;
3302 char_u **pp;
3303
3304 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
3305 --idx)
3306 {
3307 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
3308 if (str_equal(p, aff_entry->ae_cond))
3309 break;
3310 }
3311 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
3312 {
3313 /* Not found, add a new condition. */
3314 idx = spin->si_prefcond.ga_len++;
3315 pp = ((char_u **)spin->si_prefcond.ga_data) + idx;
3316 if (aff_entry->ae_cond == NULL)
3317 *pp = NULL;
3318 else
3319 *pp = getroom_save(&spin->si_blocks,
3320 aff_entry->ae_cond);
3321 }
3322
3323 /* Add the prefix to the prefix tree. */
3324 if (aff_entry->ae_add == NULL)
3325 p = (char_u *)"";
3326 else
3327 p = aff_entry->ae_add;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003328 tree_add_word(p, spin->si_prefroot, rare ? -2 : -1,
3329 idx, cur_aff->ah_newID, &spin->si_blocks);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003330 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003331 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003332 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003333 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
3334 {
3335 if (fol != NULL)
3336 smsg((char_u *)_("Duplicate FOL in %s line %d"),
3337 fname, lnum);
3338 else
3339 fol = vim_strsave(items[1]);
3340 }
3341 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
3342 {
3343 if (low != NULL)
3344 smsg((char_u *)_("Duplicate LOW in %s line %d"),
3345 fname, lnum);
3346 else
3347 low = vim_strsave(items[1]);
3348 }
3349 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
3350 {
3351 if (upp != NULL)
3352 smsg((char_u *)_("Duplicate UPP in %s line %d"),
3353 fname, lnum);
3354 else
3355 upp = vim_strsave(items[1]);
3356 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003357 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003358 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003359 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003360 if (!isdigit(*items[1]))
3361 smsg((char_u *)_("Expected REP count in %s line %d"),
3362 fname, lnum);
3363 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003364 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
3365 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003366 /* REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003367 if (do_rep)
3368 add_fromto(spin, &spin->si_rep, items[1], items[2]);
3369 }
3370 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
3371 {
3372 /* MAP item or count */
3373 if (!found_map)
3374 {
3375 /* First line contains the count. */
3376 found_map = TRUE;
3377 if (!isdigit(*items[1]))
3378 smsg((char_u *)_("Expected MAP count in %s line %d"),
3379 fname, lnum);
3380 }
3381 else if (do_map)
3382 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003383 int c;
3384
3385 /* Check that every character appears only once. */
3386 for (p = items[1]; *p != NUL; )
3387 {
3388#ifdef FEAT_MBYTE
3389 c = mb_ptr2char_adv(&p);
3390#else
3391 c = *p++;
3392#endif
3393 if ((spin->si_map.ga_len > 0
3394 && vim_strchr(spin->si_map.ga_data, c)
3395 != NULL)
3396 || vim_strchr(p, c) != NULL)
3397 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
3398 fname, lnum);
3399 }
3400
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003401 /* We simply concatenate all the MAP strings, separated by
3402 * slashes. */
3403 ga_concat(&spin->si_map, items[1]);
3404 ga_append(&spin->si_map, '/');
3405 }
3406 }
3407 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
3408 {
3409 if (do_sal)
3410 {
3411 /* SAL item (sounds-a-like)
3412 * Either one of the known keys or a from-to pair. */
3413 if (STRCMP(items[1], "followup") == 0)
3414 spin->si_followup = sal_to_bool(items[2]);
3415 else if (STRCMP(items[1], "collapse_result") == 0)
3416 spin->si_collapse = sal_to_bool(items[2]);
3417 else if (STRCMP(items[1], "remove_accents") == 0)
3418 spin->si_rem_accents = sal_to_bool(items[2]);
3419 else
3420 /* when "to" is "_" it means empty */
3421 add_fromto(spin, &spin->si_sal, items[1],
3422 STRCMP(items[2], "_") == 0 ? (char_u *)""
3423 : items[2]);
3424 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003425 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003426 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
3427 && (!do_sofo || spin->si_sofofr == NULL))
3428 {
3429 if (do_sofo)
3430 spin->si_sofofr = vim_strsave(items[1]);
3431 }
3432 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
3433 && (!do_sofo || spin->si_sofoto == NULL))
3434 {
3435 if (do_sofo)
3436 spin->si_sofoto = vim_strsave(items[1]);
3437 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003438 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003439 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
3440 fname, lnum, items[0]);
3441 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003442 }
3443
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003444 if (do_sofo && (spin->si_sofofr == NULL) != (spin->si_sofoto == NULL))
3445 smsg((char_u *)_("Missing SOFO%s line in %s"),
3446 spin->si_sofofr == NULL ? "FROM" : "TO", fname);
3447 if (spin->si_sofofr != NULL && spin->si_sal.ga_len > 0)
3448 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
3449
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003450 if (fol != NULL || low != NULL || upp != NULL)
3451 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003452 if (spin->si_clear_chartab)
3453 {
3454 /* Clear the char type tables, don't want to use any of the
3455 * currently used spell properties. */
3456 init_spell_chartab();
3457 spin->si_clear_chartab = FALSE;
3458 }
3459
Bram Moolenaar3982c542005-06-08 21:56:31 +00003460 /*
3461 * Don't write a word table for an ASCII file, so that we don't check
3462 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003463 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00003464 * mb_get_class(), the list of chars in the file will be incomplete.
3465 */
3466 if (!spin->si_ascii
3467#ifdef FEAT_MBYTE
3468 && !enc_utf8
3469#endif
3470 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003471 {
3472 if (fol == NULL || low == NULL || upp == NULL)
3473 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
3474 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00003475 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003476 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003477
3478 vim_free(fol);
3479 vim_free(low);
3480 vim_free(upp);
3481 }
3482
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003483 vim_free(pc);
3484 fclose(fd);
3485 return aff;
3486}
3487
3488/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003489 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
3490 * NULL as equal.
3491 */
3492 static int
3493str_equal(s1, s2)
3494 char_u *s1;
3495 char_u *s2;
3496{
3497 if (s1 == NULL || s2 == NULL)
3498 return s1 == s2;
3499 return STRCMP(s1, s2) == 0;
3500}
3501
3502/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003503 * Add a from-to item to "gap". Used for REP and SAL items.
3504 * They are stored case-folded.
3505 */
3506 static void
3507add_fromto(spin, gap, from, to)
3508 spellinfo_T *spin;
3509 garray_T *gap;
3510 char_u *from;
3511 char_u *to;
3512{
3513 fromto_T *ftp;
3514 char_u word[MAXWLEN];
3515
3516 if (ga_grow(gap, 1) == OK)
3517 {
3518 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
3519 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
3520 ftp->ft_from = getroom_save(&spin->si_blocks, word);
3521 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
3522 ftp->ft_to = getroom_save(&spin->si_blocks, word);
3523 ++gap->ga_len;
3524 }
3525}
3526
3527/*
3528 * Convert a boolean argument in a SAL line to TRUE or FALSE;
3529 */
3530 static int
3531sal_to_bool(s)
3532 char_u *s;
3533{
3534 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
3535}
3536
3537/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00003538 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
3539 * When "s" is NULL FALSE is returned.
3540 */
3541 static int
3542has_non_ascii(s)
3543 char_u *s;
3544{
3545 char_u *p;
3546
3547 if (s != NULL)
3548 for (p = s; *p != NUL; ++p)
3549 if (*p >= 128)
3550 return TRUE;
3551 return FALSE;
3552}
3553
3554/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003555 * Free the structure filled by spell_read_aff().
3556 */
3557 static void
3558spell_free_aff(aff)
3559 afffile_T *aff;
3560{
3561 hashtab_T *ht;
3562 hashitem_T *hi;
3563 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003564 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003565 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003566
3567 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003568
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003569 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003570 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
3571 {
3572 todo = ht->ht_used;
3573 for (hi = ht->ht_array; todo > 0; ++hi)
3574 {
3575 if (!HASHITEM_EMPTY(hi))
3576 {
3577 --todo;
3578 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003579 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3580 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003581 }
3582 }
3583 if (ht == &aff->af_suff)
3584 break;
3585 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003586
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003587 hash_clear(&aff->af_pref);
3588 hash_clear(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003589}
3590
3591/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003592 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003593 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003594 */
3595 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00003596spell_read_dic(fname, spin, affile)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003597 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003598 spellinfo_T *spin;
3599 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003600{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003601 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003602 char_u line[MAXLINELEN];
Bram Moolenaar51485f02005-06-04 21:55:20 +00003603 char_u *afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003604 char_u *pfxlist;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003605 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003606 char_u *pc;
3607 char_u *w;
3608 int l;
3609 hash_T hash;
3610 hashitem_T *hi;
3611 FILE *fd;
3612 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003613 int non_ascii = 0;
3614 int retval = OK;
3615 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003616 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003617
Bram Moolenaar51485f02005-06-04 21:55:20 +00003618 /*
3619 * Open the file.
3620 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003621 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003622 if (fd == NULL)
3623 {
3624 EMSG2(_(e_notopen), fname);
3625 return FAIL;
3626 }
3627
Bram Moolenaar51485f02005-06-04 21:55:20 +00003628 /* The hashtable is only used to detect duplicated words. */
3629 hash_init(&ht);
3630
Bram Moolenaar8db73182005-06-17 21:51:16 +00003631 spin->si_foldwcount = 0;
3632 spin->si_keepwcount = 0;
3633
Bram Moolenaarb765d632005-06-07 21:00:02 +00003634 if (spin->si_verbose || p_verbose > 2)
3635 {
3636 if (!spin->si_verbose)
3637 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003638 smsg((char_u *)_("Reading dictionary file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003639 out_flush();
3640 if (!spin->si_verbose)
3641 verbose_leave();
3642 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003643
3644 /* Read and ignore the first line: word count. */
3645 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003646 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003647 EMSG2(_("E760: No word count in %s"), fname);
3648
3649 /*
3650 * Read all the lines in the file one by one.
3651 * The words are converted to 'encoding' here, before being added to
3652 * the hashtable.
3653 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003654 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003655 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003656 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003657 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003658 if (line[0] == '#')
3659 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003660
Bram Moolenaar51485f02005-06-04 21:55:20 +00003661 /* Remove CR, LF and white space from the end. White space halfway
3662 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003663 l = STRLEN(line);
3664 while (l > 0 && line[l - 1] <= ' ')
3665 --l;
3666 if (l == 0)
3667 continue; /* empty line */
3668 line[l] = NUL;
3669
Bram Moolenaar51485f02005-06-04 21:55:20 +00003670 /* Find the optional affix names. */
3671 afflist = vim_strchr(line, '/');
3672 if (afflist != NULL)
3673 *afflist++ = NUL;
3674
3675 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
3676 if (spin->si_ascii && has_non_ascii(line))
3677 {
3678 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003679 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003680 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003681
Bram Moolenaarb765d632005-06-07 21:00:02 +00003682#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003683 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003684 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003685 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003686 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003687 if (pc == NULL)
3688 {
3689 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3690 fname, lnum, line);
3691 continue;
3692 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003693 w = pc;
3694 }
3695 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003696#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003697 {
3698 pc = NULL;
3699 w = line;
3700 }
3701
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003702 /* This takes time, print a message now and then. */
3703 if (spin->si_verbose && (lnum & 0x3ff) == 0)
3704 {
3705 vim_snprintf((char *)message, sizeof(message),
3706 _("line %6d, word %6d - %s"),
3707 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
3708 msg_start();
3709 msg_puts_long_attr(message, 0);
3710 msg_clr_eos();
3711 msg_didout = FALSE;
3712 msg_col = 0;
3713 out_flush();
3714 }
3715
Bram Moolenaar51485f02005-06-04 21:55:20 +00003716 /* Store the word in the hashtable to be able to find duplicates. */
3717 dw = (char_u *)getroom_save(&spin->si_blocks, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003718 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003719 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003720 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003721 if (retval == FAIL)
3722 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003723
Bram Moolenaar51485f02005-06-04 21:55:20 +00003724 hash = hash_hash(dw);
3725 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003726 if (!HASHITEM_EMPTY(hi))
3727 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003728 fname, lnum, dw);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003729 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00003730 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003731
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003732 flags = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003733 pfxlist = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003734 if (afflist != NULL)
3735 {
3736 /* Check for affix name that stands for keep-case word and stands
3737 * for rare word (if defined). */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003738 if (affile->af_kep != NUL
3739 && vim_strchr(afflist, affile->af_kep) != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003740 flags |= WF_KEEPCAP;
3741 if (affile->af_rar != NUL
3742 && vim_strchr(afflist, affile->af_rar) != NULL)
3743 flags |= WF_RARE;
Bram Moolenaar0c405862005-06-22 22:26:26 +00003744 if (affile->af_bad != NUL
3745 && vim_strchr(afflist, affile->af_bad) != NULL)
3746 flags |= WF_BANNED;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003747
3748 if (affile->af_pfxpostpone)
3749 /* Need to store the list of prefix IDs with the word. */
3750 pfxlist = get_pfxlist(affile, afflist, &spin->si_blocks);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003751 }
3752
Bram Moolenaar51485f02005-06-04 21:55:20 +00003753 /* Add the word to the word tree(s). */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003754 if (store_word(dw, spin, flags, spin->si_region, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003755 retval = FAIL;
3756
3757 if (afflist != NULL)
3758 {
3759 /* Find all matching suffixes and add the resulting words.
3760 * Additionally do matching prefixes that combine. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003761 if (store_aff_word(dw, spin, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003762 &affile->af_suff, &affile->af_pref,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003763 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003764 retval = FAIL;
3765
3766 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003767 if (store_aff_word(dw, spin, afflist, affile,
3768 &affile->af_pref, NULL,
3769 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003770 retval = FAIL;
3771 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003772 }
3773
Bram Moolenaar51485f02005-06-04 21:55:20 +00003774 if (spin->si_ascii && non_ascii > 0)
3775 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
3776 non_ascii);
3777 hash_clear(&ht);
3778
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003779 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003780 return retval;
3781}
3782
3783/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003784 * Get the list of prefix IDs from the affix list "afflist".
3785 * Used for PFXPOSTPONE.
3786 * Returns a string allocated with getroom(). NULL when there are no prefixes
3787 * or when out of memory.
3788 */
3789 static char_u *
3790get_pfxlist(affile, afflist, blp)
3791 afffile_T *affile;
3792 char_u *afflist;
3793 sblock_T **blp;
3794{
3795 char_u *p;
3796 int cnt;
3797 int round;
3798 char_u *res = NULL;
3799 char_u key[2];
3800 hashitem_T *hi;
3801
3802 key[1] = NUL;
3803
3804 /* round 1: count the number of prefix IDs.
3805 * round 2: move prefix IDs to "res" */
3806 for (round = 1; round <= 2; ++round)
3807 {
3808 cnt = 0;
3809 for (p = afflist; *p != NUL; ++p)
3810 {
3811 key[0] = *p;
3812 hi = hash_find(&affile->af_pref, key);
3813 if (!HASHITEM_EMPTY(hi))
3814 {
3815 /* This is a prefix ID, use the new number. */
3816 if (round == 2)
3817 res[cnt] = HI2AH(hi)->ah_newID;
3818 ++cnt;
3819 }
3820 }
3821 if (round == 1 && cnt > 0)
3822 res = getroom(blp, cnt + 1);
3823 if (res == NULL)
3824 break;
3825 }
3826
3827 if (res != NULL)
3828 res[cnt] = NUL;
3829 return res;
3830}
3831
3832/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003833 * Apply affixes to a word and store the resulting words.
3834 * "ht" is the hashtable with affentry_T that need to be applied, either
3835 * prefixes or suffixes.
3836 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
3837 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003838 *
3839 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003840 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003841 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003842store_aff_word(word, spin, afflist, affile, ht, xht, comb, flags, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003843 char_u *word; /* basic word start */
3844 spellinfo_T *spin; /* spell info */
3845 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003846 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003847 hashtab_T *ht;
3848 hashtab_T *xht;
3849 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003850 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003851 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003852{
3853 int todo;
3854 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003855 affheader_T *ah;
3856 affentry_T *ae;
3857 regmatch_T regmatch;
3858 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003859 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003860 int i;
3861 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003862 int use_flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003863
Bram Moolenaar51485f02005-06-04 21:55:20 +00003864 todo = ht->ht_used;
3865 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003866 {
3867 if (!HASHITEM_EMPTY(hi))
3868 {
3869 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003870 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00003871
Bram Moolenaar51485f02005-06-04 21:55:20 +00003872 /* Check that the affix combines, if required, and that the word
3873 * supports this affix. */
3874 if ((!comb || ah->ah_combine)
3875 && vim_strchr(afflist, *ah->ah_key) != NULL)
Bram Moolenaar5482f332005-04-17 20:18:43 +00003876 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003877 /* Loop over all affix entries with this name. */
3878 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003879 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003880 /* Check the condition. It's not logical to match case
3881 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003882 * Myspell.
3883 * For prefixes, when "PFXPOSTPONE" was used, only do
3884 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003885 regmatch.regprog = ae->ae_prog;
3886 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003887 if ((xht != NULL || !affile->af_pfxpostpone
3888 || ae->ae_chop != NULL)
3889 && (ae->ae_prog == NULL
3890 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003891 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003892 /* Match. Remove the chop and add the affix. */
3893 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003894 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003895 /* prefix: chop/add at the start of the word */
3896 if (ae->ae_add == NULL)
3897 *newword = NUL;
3898 else
3899 STRCPY(newword, ae->ae_add);
3900 p = word;
3901 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003902 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003903 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003904#ifdef FEAT_MBYTE
3905 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003906 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003907 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003908 for ( ; i > 0; --i)
3909 mb_ptr_adv(p);
3910 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00003911 else
3912#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003913 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003914 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003915 STRCAT(newword, p);
3916 }
3917 else
3918 {
3919 /* suffix: chop/add at the end of the word */
3920 STRCPY(newword, word);
3921 if (ae->ae_chop != NULL)
3922 {
3923 /* Remove chop string. */
3924 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003925 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003926 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003927 mb_ptr_back(newword, p);
3928 *p = NUL;
3929 }
3930 if (ae->ae_add != NULL)
3931 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003932 }
3933
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003934 /* Obey the "rare" flag of the affix. */
3935 if (ae->ae_rare)
3936 use_flags = flags | WF_RARE;
3937 else
3938 use_flags = flags;
3939
Bram Moolenaar51485f02005-06-04 21:55:20 +00003940 /* Store the modified word. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003941 if (store_word(newword, spin, use_flags,
3942 spin->si_region, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003943 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003944
Bram Moolenaar51485f02005-06-04 21:55:20 +00003945 /* When added a suffix and combining is allowed also
3946 * try adding prefixes additionally. */
3947 if (xht != NULL && ah->ah_combine)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003948 if (store_aff_word(newword, spin, afflist, affile,
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003949 xht, NULL, TRUE, use_flags, pfxlist)
3950 == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003951 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003952 }
3953 }
3954 }
3955 }
3956 }
3957
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003958 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003959}
3960
3961/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003962 * Read a file with a list of words.
3963 */
3964 static int
3965spell_read_wordfile(fname, spin)
3966 char_u *fname;
3967 spellinfo_T *spin;
3968{
3969 FILE *fd;
3970 long lnum = 0;
3971 char_u rline[MAXLINELEN];
3972 char_u *line;
3973 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003974 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003975 int l;
3976 int retval = OK;
3977 int did_word = FALSE;
3978 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003979 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003980 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003981
3982 /*
3983 * Open the file.
3984 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003985 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00003986 if (fd == NULL)
3987 {
3988 EMSG2(_(e_notopen), fname);
3989 return FAIL;
3990 }
3991
Bram Moolenaarb765d632005-06-07 21:00:02 +00003992 if (spin->si_verbose || p_verbose > 2)
3993 {
3994 if (!spin->si_verbose)
3995 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003996 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003997 out_flush();
3998 if (!spin->si_verbose)
3999 verbose_leave();
4000 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004001
4002 /*
4003 * Read all the lines in the file one by one.
4004 */
4005 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
4006 {
4007 line_breakcheck();
4008 ++lnum;
4009
4010 /* Skip comment lines. */
4011 if (*rline == '#')
4012 continue;
4013
4014 /* Remove CR, LF and white space from the end. */
4015 l = STRLEN(rline);
4016 while (l > 0 && rline[l - 1] <= ' ')
4017 --l;
4018 if (l == 0)
4019 continue; /* empty or blank line */
4020 rline[l] = NUL;
4021
4022 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
4023 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004024#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004025 if (spin->si_conv.vc_type != CONV_NONE)
4026 {
4027 pc = string_convert(&spin->si_conv, rline, NULL);
4028 if (pc == NULL)
4029 {
4030 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4031 fname, lnum, rline);
4032 continue;
4033 }
4034 line = pc;
4035 }
4036 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004037#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004038 {
4039 pc = NULL;
4040 line = rline;
4041 }
4042
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004043 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00004044 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004045 ++line;
4046 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004047 {
4048 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004049 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
4050 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004051 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004052 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
4053 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004054 else
4055 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004056#ifdef FEAT_MBYTE
4057 char_u *enc;
4058
Bram Moolenaar51485f02005-06-04 21:55:20 +00004059 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004060 line += 10;
4061 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004062 if (enc != NULL && !spin->si_ascii
4063 && convert_setup(&spin->si_conv, enc,
4064 p_enc) == FAIL)
4065 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00004066 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004067 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004068 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004069#else
4070 smsg((char_u *)_("Conversion in %s not supported"), fname);
4071#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004072 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004073 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004074 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004075
Bram Moolenaar3982c542005-06-08 21:56:31 +00004076 if (STRNCMP(line, "regions=", 8) == 0)
4077 {
4078 if (spin->si_region_count > 1)
4079 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
4080 fname, lnum, line);
4081 else
4082 {
4083 line += 8;
4084 if (STRLEN(line) > 16)
4085 smsg((char_u *)_("Too many regions in %s line %d: %s"),
4086 fname, lnum, line);
4087 else
4088 {
4089 spin->si_region_count = STRLEN(line) / 2;
4090 STRCPY(spin->si_region_name, line);
4091 }
4092 }
4093 continue;
4094 }
4095
Bram Moolenaar7887d882005-07-01 22:33:52 +00004096 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
4097 fname, lnum, line - 1);
4098 continue;
4099 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004100
Bram Moolenaar7887d882005-07-01 22:33:52 +00004101 flags = 0;
4102 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004103
Bram Moolenaar7887d882005-07-01 22:33:52 +00004104 /* Check for flags and region after a slash. */
4105 p = vim_strchr(line, '/');
4106 if (p != NULL)
4107 {
4108 *p++ = NUL;
4109 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004110 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004111 if (*p == '=') /* keep-case word */
4112 flags |= WF_KEEPCAP;
4113 else if (*p == '!') /* Bad, bad, wicked word. */
4114 flags |= WF_BANNED;
4115 else if (*p == '?') /* Rare word. */
4116 flags |= WF_RARE;
4117 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004118 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004119 if ((flags & WF_REGION) == 0) /* first one */
4120 regionmask = 0;
4121 flags |= WF_REGION;
4122
4123 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00004124 if (l > spin->si_region_count)
4125 {
4126 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00004127 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004128 break;
4129 }
4130 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004131 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00004132 else
4133 {
4134 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
4135 fname, lnum, p);
4136 break;
4137 }
4138 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004139 }
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{
Bram Moolenaar7887d882005-07-01 22:33:52 +00005182 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
5183 eap->forceit);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005184}
5185
5186/*
5187 * Add "word[len]" to 'spellfile' as a good or bad word.
5188 */
5189 void
Bram Moolenaar7887d882005-07-01 22:33:52 +00005190spell_add_word(word, len, bad, temp)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005191 char_u *word;
5192 int len;
5193 int bad;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005194 int temp; /* "zG" and "zW": use temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005195{
5196 FILE *fd;
5197 buf_T *buf;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005198 int new_spf = FALSE;
5199 struct stat st;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005200 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005201
Bram Moolenaar7887d882005-07-01 22:33:52 +00005202 if (temp) /* use temp word list */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005203 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005204 if (temp_wordlist == NULL)
5205 {
5206 temp_wordlist = vim_tempname('s');
5207 if (temp_wordlist == NULL)
5208 return;
5209 }
5210 fname = temp_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005211 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005212 else
5213 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005214 /* If 'spellfile' isn't set figure out a good default value. */
5215 if (*curbuf->b_p_spf == NUL)
5216 {
5217 init_spellfile();
5218 new_spf = TRUE;
5219 }
5220
5221 if (*curbuf->b_p_spf == NUL)
5222 {
5223 EMSG(_("E764: 'spellfile' is not set"));
5224 return;
5225 }
5226
Bram Moolenaarb765d632005-06-07 21:00:02 +00005227 /* Check that the user isn't editing the .add file somewhere. */
5228 buf = buflist_findname_exp(curbuf->b_p_spf);
5229 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
5230 buf = NULL;
5231 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00005232 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005233 EMSG(_(e_bufloaded));
5234 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005235 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00005236
5237 fname = curbuf->b_p_spf;
5238 }
5239
5240 fd = mch_fopen((char *)fname, "a");
5241 if (fd == NULL && new_spf)
5242 {
5243 /* We just initialized the 'spellfile' option and can't open the file.
5244 * We may need to create the "spell" directory first. We already
5245 * checked the runtime directory is writable in init_spellfile(). */
5246 STRCPY(NameBuff, fname);
5247 *gettail_sep(NameBuff) = NUL;
5248 if (mch_stat((char *)NameBuff, &st) < 0)
5249 {
5250 /* The directory doesn't exist. Try creating it and opening the
5251 * file again. */
5252 vim_mkdir(NameBuff, 0755);
5253 fd = mch_fopen((char *)fname, "a");
5254 }
5255 }
5256
5257 if (fd == NULL)
5258 EMSG2(_(e_notopen), fname);
5259 else
5260 {
5261 if (bad)
5262 fprintf(fd, "%.*s/!\n", len, word);
5263 else
5264 fprintf(fd, "%.*s\n", len, word);
5265 fclose(fd);
5266
5267 /* Update the .add.spl file. */
5268 mkspell(1, &fname, FALSE, TRUE, TRUE);
5269
5270 /* If the .add file is edited somewhere, reload it. */
5271 if (buf != NULL)
5272 buf_reload(buf);
5273
5274 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005275 }
5276}
5277
5278/*
5279 * Initialize 'spellfile' for the current buffer.
5280 */
5281 static void
5282init_spellfile()
5283{
5284 char_u buf[MAXPATHL];
5285 int l;
5286 slang_T *sl;
5287 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005288 char_u *lend;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005289
5290 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
5291 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005292 /* Find the end of the language name. Exclude the region. */
5293 for (lend = curbuf->b_p_spl; *lend != NUL
5294 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
5295 ;
5296
5297 /* Loop over all entries in 'runtimepath'. Use the first one where we
5298 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005299 rtp = p_rtp;
5300 while (*rtp != NUL)
5301 {
5302 /* Copy the path from 'runtimepath' to buf[]. */
5303 copy_option_part(&rtp, buf, MAXPATHL, ",");
5304 if (filewritable(buf) == 2)
5305 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00005306 /* Use the first language name from 'spelllang' and the
5307 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005308 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
5309 l = STRLEN(buf);
5310 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00005311 "/spell/%.*s.%s.add",
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005312 (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00005313 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
5314 ? (char_u *)"ascii" : spell_enc());
5315 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
5316 break;
5317 }
5318 }
5319 }
5320}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005321
Bram Moolenaar51485f02005-06-04 21:55:20 +00005322
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005323/*
5324 * Init the chartab used for spelling for ASCII.
5325 * EBCDIC is not supported!
5326 */
5327 static void
5328clear_spell_chartab(sp)
5329 spelltab_T *sp;
5330{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005331 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005332
5333 /* Init everything to FALSE. */
5334 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
5335 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
5336 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005337 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005338 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005339 sp->st_upper[i] = i;
5340 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005341
5342 /* We include digits. A word shouldn't start with a digit, but handling
5343 * that is done separately. */
5344 for (i = '0'; i <= '9'; ++i)
5345 sp->st_isw[i] = TRUE;
5346 for (i = 'A'; i <= 'Z'; ++i)
5347 {
5348 sp->st_isw[i] = TRUE;
5349 sp->st_isu[i] = TRUE;
5350 sp->st_fold[i] = i + 0x20;
5351 }
5352 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005353 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005354 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005355 sp->st_upper[i] = i - 0x20;
5356 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005357}
5358
5359/*
5360 * Init the chartab used for spelling. Only depends on 'encoding'.
5361 * Called once while starting up and when 'encoding' changes.
5362 * The default is to use isalpha(), but the spell file should define the word
5363 * characters to make it possible that 'encoding' differs from the current
5364 * locale.
5365 */
5366 void
5367init_spell_chartab()
5368{
5369 int i;
5370
5371 did_set_spelltab = FALSE;
5372 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005373#ifdef FEAT_MBYTE
5374 if (enc_dbcs)
5375 {
5376 /* DBCS: assume double-wide characters are word characters. */
5377 for (i = 128; i <= 255; ++i)
5378 if (MB_BYTE2LEN(i) == 2)
5379 spelltab.st_isw[i] = TRUE;
5380 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005381 else if (enc_utf8)
5382 {
5383 for (i = 128; i < 256; ++i)
5384 {
5385 spelltab.st_isu[i] = utf_isupper(i);
5386 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
5387 spelltab.st_fold[i] = utf_fold(i);
5388 spelltab.st_upper[i] = utf_toupper(i);
5389 }
5390 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005391 else
5392#endif
5393 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005394 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005395 for (i = 128; i < 256; ++i)
5396 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005397 if (MB_ISUPPER(i))
5398 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005399 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005400 spelltab.st_isu[i] = TRUE;
5401 spelltab.st_fold[i] = MB_TOLOWER(i);
5402 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005403 else if (MB_ISLOWER(i))
5404 {
5405 spelltab.st_isw[i] = TRUE;
5406 spelltab.st_upper[i] = MB_TOUPPER(i);
5407 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005408 }
5409 }
5410}
5411
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005412static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
5413static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
5414
5415/*
5416 * Set the spell character tables from strings in the affix file.
5417 */
5418 static int
5419set_spell_chartab(fol, low, upp)
5420 char_u *fol;
5421 char_u *low;
5422 char_u *upp;
5423{
5424 /* We build the new tables here first, so that we can compare with the
5425 * previous one. */
5426 spelltab_T new_st;
5427 char_u *pf = fol, *pl = low, *pu = upp;
5428 int f, l, u;
5429
5430 clear_spell_chartab(&new_st);
5431
5432 while (*pf != NUL)
5433 {
5434 if (*pl == NUL || *pu == NUL)
5435 {
5436 EMSG(_(e_affform));
5437 return FAIL;
5438 }
5439#ifdef FEAT_MBYTE
5440 f = mb_ptr2char_adv(&pf);
5441 l = mb_ptr2char_adv(&pl);
5442 u = mb_ptr2char_adv(&pu);
5443#else
5444 f = *pf++;
5445 l = *pl++;
5446 u = *pu++;
5447#endif
5448 /* Every character that appears is a word character. */
5449 if (f < 256)
5450 new_st.st_isw[f] = TRUE;
5451 if (l < 256)
5452 new_st.st_isw[l] = TRUE;
5453 if (u < 256)
5454 new_st.st_isw[u] = TRUE;
5455
5456 /* if "LOW" and "FOL" are not the same the "LOW" char needs
5457 * case-folding */
5458 if (l < 256 && l != f)
5459 {
5460 if (f >= 256)
5461 {
5462 EMSG(_(e_affrange));
5463 return FAIL;
5464 }
5465 new_st.st_fold[l] = f;
5466 }
5467
5468 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005469 * case-folding, it's upper case and the "UPP" is the upper case of
5470 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005471 if (u < 256 && u != f)
5472 {
5473 if (f >= 256)
5474 {
5475 EMSG(_(e_affrange));
5476 return FAIL;
5477 }
5478 new_st.st_fold[u] = f;
5479 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005480 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005481 }
5482 }
5483
5484 if (*pl != NUL || *pu != NUL)
5485 {
5486 EMSG(_(e_affform));
5487 return FAIL;
5488 }
5489
5490 return set_spell_finish(&new_st);
5491}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005492
5493/*
5494 * Set the spell character tables from strings in the .spl file.
5495 */
5496 static int
Bram Moolenaar7887d882005-07-01 22:33:52 +00005497set_spell_charflags(flags, upp)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005498 char_u *flags;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005499 char_u *upp;
5500{
5501 /* We build the new tables here first, so that we can compare with the
5502 * previous one. */
5503 spelltab_T new_st;
5504 int i;
5505 char_u *p = upp;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005506 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005507
5508 clear_spell_chartab(&new_st);
5509
Bram Moolenaar7887d882005-07-01 22:33:52 +00005510 for (i = 0; flags[i] != NUL; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005511 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005512 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
5513 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005514
5515 if (*p == NUL)
5516 return FAIL;
5517#ifdef FEAT_MBYTE
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005518 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005519#else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005520 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005521#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005522 new_st.st_fold[i + 128] = c;
5523 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
5524 new_st.st_upper[c] = i + 128;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005525 }
5526
5527 return set_spell_finish(&new_st);
5528}
5529
5530 static int
5531set_spell_finish(new_st)
5532 spelltab_T *new_st;
5533{
5534 int i;
5535
5536 if (did_set_spelltab)
5537 {
5538 /* check that it's the same table */
5539 for (i = 0; i < 256; ++i)
5540 {
5541 if (spelltab.st_isw[i] != new_st->st_isw[i]
5542 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005543 || spelltab.st_fold[i] != new_st->st_fold[i]
5544 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005545 {
5546 EMSG(_("E763: Word characters differ between spell files"));
5547 return FAIL;
5548 }
5549 }
5550 }
5551 else
5552 {
5553 /* copy the new spelltab into the one being used */
5554 spelltab = *new_st;
5555 did_set_spelltab = TRUE;
5556 }
5557
5558 return OK;
5559}
5560
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005561/*
Bram Moolenaarea408852005-06-25 22:49:46 +00005562 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005563 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00005564 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005565 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00005566 */
5567 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005568spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00005569 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005570 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00005571{
Bram Moolenaarea408852005-06-25 22:49:46 +00005572#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005573 char_u *s;
5574 int l;
5575 int c;
5576
5577 if (has_mbyte)
5578 {
5579 l = MB_BYTE2LEN(*p);
5580 s = p;
5581 if (l == 1)
5582 {
5583 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005584 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005585 {
5586 s = p + 1; /* skip a mid-word character */
5587 l = MB_BYTE2LEN(*s);
5588 }
5589 }
5590 else
5591 {
5592 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005593 if (c < 256 ? buf->b_spell_ismw[c]
5594 : (buf->b_spell_ismw_mb != NULL
5595 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005596 {
5597 s = p + l;
5598 l = MB_BYTE2LEN(*s);
5599 }
5600 }
5601
5602 if (l > 1)
5603 return mb_get_class(s) >= 2;
5604 return spelltab.st_isw[*s];
5605 }
Bram Moolenaarea408852005-06-25 22:49:46 +00005606#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005607
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005608 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
5609}
5610
5611/*
5612 * Return TRUE if "p" points to a word character.
5613 * Unlike spell_iswordp() this doesn't check for "midword" characters.
5614 */
5615 static int
5616spell_iswordp_nmw(p)
5617 char_u *p;
5618{
5619#ifdef FEAT_MBYTE
5620 if (has_mbyte && MB_BYTE2LEN(*p) > 1)
5621 return mb_get_class(p) >= 2;
5622#endif
5623
5624 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00005625}
5626
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005627#ifdef FEAT_MBYTE
5628/*
5629 * Return TRUE if "p" points to a word character.
5630 * Wide version of spell_iswordp().
5631 */
5632 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005633spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005634 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005635 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005636{
5637 int *s;
5638
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005639 if (*p < 256 ? buf->b_spell_ismw[*p]
5640 : (buf->b_spell_ismw_mb != NULL
5641 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005642 s = p + 1;
5643 else
5644 s = p;
5645
5646 if (mb_char2len(*s) > 1)
5647 {
5648 if (enc_utf8)
5649 return utf_class(*s) >= 2;
5650 if (enc_dbcs)
5651 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
5652 return 0;
5653 }
5654 return spelltab.st_isw[*s];
5655}
5656#endif
5657
Bram Moolenaarea408852005-06-25 22:49:46 +00005658/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005659 * Write the table with prefix conditions to the .spl file.
5660 */
5661 static void
5662write_spell_prefcond(fd, gap)
5663 FILE *fd;
5664 garray_T *gap;
5665{
5666 int i;
5667 char_u *p;
5668 int len;
5669
5670 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
5671
5672 for (i = 0; i < gap->ga_len; ++i)
5673 {
5674 /* <prefcond> : <condlen> <condstr> */
5675 p = ((char_u **)gap->ga_data)[i];
5676 if (p == NULL)
5677 fputc(0, fd);
5678 else
5679 {
5680 len = STRLEN(p);
5681 fputc(len, fd);
5682 fwrite(p, (size_t)len, (size_t)1, fd);
5683 }
5684 }
5685}
5686
5687/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005688 * Write the current tables into the .spl file.
5689 * This makes sure the same characters are recognized as word characters when
5690 * generating an when using a spell file.
5691 */
5692 static void
5693write_spell_chartab(fd)
5694 FILE *fd;
5695{
5696 char_u charbuf[256 * 4];
5697 int len = 0;
5698 int flags;
5699 int i;
5700
5701 fputc(128, fd); /* <charflagslen> */
5702 for (i = 128; i < 256; ++i)
5703 {
5704 flags = 0;
5705 if (spelltab.st_isw[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005706 flags |= CF_WORD;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005707 if (spelltab.st_isu[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005708 flags |= CF_UPPER;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005709 fputc(flags, fd); /* <charflags> */
5710
Bram Moolenaarb765d632005-06-07 21:00:02 +00005711#ifdef FEAT_MBYTE
5712 if (has_mbyte)
5713 len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
5714 else
5715#endif
5716 charbuf[len++] = spelltab.st_fold[i];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005717 }
5718
5719 put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
5720 fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
5721}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005722
5723/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005724 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
5725 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005726 * When using a multi-byte 'encoding' the length may change!
5727 * Returns FAIL when something wrong.
5728 */
5729 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005730spell_casefold(str, len, buf, buflen)
5731 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005732 int len;
5733 char_u *buf;
5734 int buflen;
5735{
5736 int i;
5737
5738 if (len >= buflen)
5739 {
5740 buf[0] = NUL;
5741 return FAIL; /* result will not fit */
5742 }
5743
5744#ifdef FEAT_MBYTE
5745 if (has_mbyte)
5746 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005747 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005748 char_u *p;
5749 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005750
5751 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005752 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005753 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005754 if (outi + MB_MAXBYTES > buflen)
5755 {
5756 buf[outi] = NUL;
5757 return FAIL;
5758 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005759 c = mb_ptr2char_adv(&p);
5760 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005761 }
5762 buf[outi] = NUL;
5763 }
5764 else
5765#endif
5766 {
5767 /* Be quick for non-multibyte encodings. */
5768 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005769 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005770 buf[i] = NUL;
5771 }
5772
5773 return OK;
5774}
5775
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005776#define SPS_BEST 1
5777#define SPS_FAST 2
5778#define SPS_DOUBLE 4
5779
5780static int sps_flags = SPS_BEST;
5781
5782/*
5783 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
5784 * Sets "sps_flags".
5785 */
5786 int
5787spell_check_sps()
5788{
5789 char_u *p;
5790 char_u buf[MAXPATHL];
5791 int f;
5792
5793 sps_flags = 0;
5794
5795 for (p = p_sps; *p != NUL; )
5796 {
5797 copy_option_part(&p, buf, MAXPATHL, ",");
5798
5799 f = 0;
5800 if (STRCMP(buf, "best") == 0)
5801 f = SPS_BEST;
5802 else if (STRCMP(buf, "fast") == 0)
5803 f = SPS_FAST;
5804 else if (STRCMP(buf, "double") == 0)
5805 f = SPS_DOUBLE;
5806 else if (STRNCMP(buf, "expr:", 5) != 0
5807 && STRNCMP(buf, "file:", 5) != 0)
5808 f = -1;
5809
5810 if (f == -1 || (sps_flags != 0 && f != 0))
5811 {
5812 sps_flags = SPS_BEST;
5813 return FAIL;
5814 }
5815 if (f != 0)
5816 sps_flags = f;
5817 }
5818
5819 if (sps_flags == 0)
5820 sps_flags = SPS_BEST;
5821
5822 return OK;
5823}
5824
5825/* Remember what "z?" replaced. */
5826static char_u *repl_from = NULL;
5827static char_u *repl_to = NULL;
5828
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005829/*
5830 * "z?": Find badly spelled word under or after the cursor.
5831 * Give suggestions for the properly spelled word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005832 */
5833 void
5834spell_suggest()
5835{
5836 char_u *line;
5837 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005838 char_u wcopy[MAXWLEN + 2];
5839 char_u *p;
5840 int i;
5841 int c;
5842 suginfo_T sug;
5843 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005844 int mouse_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005845
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005846 /* Find the start of the badly spelled word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005847 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL
5848 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005849 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005850 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
5851 return;
5852
5853 /* No bad word or it starts after the cursor: use the word under the
5854 * cursor. */
5855 curwin->w_cursor = prev_cursor;
5856 line = ml_get_curline();
5857 p = line + curwin->w_cursor.col;
5858 /* Backup to before start of word. */
5859 while (p > line && SPELL_ISWORDP(p))
5860 mb_ptr_back(line, p);
5861 /* Forward to start of word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005862 while (*p != NUL && !SPELL_ISWORDP(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00005863 mb_ptr_adv(p);
5864
5865 if (!SPELL_ISWORDP(p)) /* No word found. */
5866 {
5867 beep_flush();
5868 return;
5869 }
5870 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005871 }
5872
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005873 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005874 line = ml_get_curline();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005875
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005876 /* Get the list of suggestions */
Bram Moolenaarea408852005-06-25 22:49:46 +00005877 spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005878
5879 if (sug.su_ga.ga_len == 0)
5880 MSG(_("Sorry, no suggestions"));
5881 else
5882 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005883 vim_free(repl_from);
5884 repl_from = NULL;
5885 vim_free(repl_to);
5886 repl_to = NULL;
5887
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005888 /* List the suggestions. */
5889 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005890 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005891 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
5892 sug.su_badlen, sug.su_badptr);
5893 msg_puts(IObuff);
5894 msg_clr_eos();
5895 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00005896
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005897 msg_scroll = TRUE;
5898 for (i = 0; i < sug.su_ga.ga_len; ++i)
5899 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005900 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005901
5902 /* The suggested word may replace only part of the bad word, add
5903 * the not replaced part. */
5904 STRCPY(wcopy, stp->st_word);
5905 if (sug.su_badlen > stp->st_orglen)
5906 vim_strncpy(wcopy + STRLEN(wcopy),
5907 sug.su_badptr + stp->st_orglen,
5908 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005909 vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\""), i + 1, wcopy);
5910 msg_puts(IObuff);
5911
5912 /* The word may replace more than "su_badlen". */
5913 if (sug.su_badlen < stp->st_orglen)
5914 {
5915 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
5916 stp->st_orglen, sug.su_badptr);
5917 msg_puts(IObuff);
5918 }
5919
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005920 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005921 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005922 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005923 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0c405862005-06-22 22:26:26 +00005924 vim_snprintf((char *)IObuff, IOSIZE, _(" (%s%d - %d)"),
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005925 stp->st_salscore ? "s " : "",
5926 stp->st_score, stp->st_altscore);
5927 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00005928 vim_snprintf((char *)IObuff, IOSIZE, _(" (%d)"),
5929 stp->st_score);
5930 msg_advance(30);
5931 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005932 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005933 msg_putchar('\n');
5934 }
5935
5936 /* Ask for choice. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005937 i = prompt_for_number(&mouse_used);
5938 if (mouse_used)
5939 i -= lines_left;
5940
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005941 if (i > 0 && i <= sug.su_ga.ga_len && u_save_cursor() == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005942 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005943 /* Save the from and to text for :spellrepall. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005944 stp = &SUG(sug.su_ga, i - 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005945 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
5946 repl_to = vim_strsave(stp->st_word);
5947
5948 /* Replace the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005949 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
5950 if (p != NULL)
5951 {
5952 c = sug.su_badptr - line;
5953 mch_memmove(p, line, c);
5954 STRCPY(p + c, stp->st_word);
5955 STRCAT(p, sug.su_badptr + stp->st_orglen);
5956 ml_replace(curwin->w_cursor.lnum, p, FALSE);
5957 curwin->w_cursor.col = c;
5958 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005959
5960 /* For redo we use a change-word command. */
5961 ResetRedobuff();
5962 AppendToRedobuff((char_u *)"ciw");
5963 AppendToRedobuff(stp->st_word);
5964 AppendCharToRedobuff(ESC);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005965 }
5966 }
5967 else
5968 curwin->w_cursor = prev_cursor;
5969 }
5970
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005971 spell_find_cleanup(&sug);
5972}
5973
5974/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005975 * ":spellrepall"
5976 */
5977/*ARGSUSED*/
5978 void
5979ex_spellrepall(eap)
5980 exarg_T *eap;
5981{
5982 pos_T pos = curwin->w_cursor;
5983 char_u *frompat;
5984 int addlen;
5985 char_u *line;
5986 char_u *p;
5987 int didone = FALSE;
5988 int save_ws = p_ws;
5989
5990 if (repl_from == NULL || repl_to == NULL)
5991 {
5992 EMSG(_("E752: No previous spell replacement"));
5993 return;
5994 }
5995 addlen = STRLEN(repl_to) - STRLEN(repl_from);
5996
5997 frompat = alloc(STRLEN(repl_from) + 7);
5998 if (frompat == NULL)
5999 return;
6000 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
6001 p_ws = FALSE;
6002
6003 curwin->w_cursor.lnum = 0;
6004 while (!got_int)
6005 {
6006 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
6007 || u_save_cursor() == FAIL)
6008 break;
6009
6010 /* Only replace when the right word isn't there yet. This happens
6011 * when changing "etc" to "etc.". */
6012 line = ml_get_curline();
6013 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
6014 repl_to, STRLEN(repl_to)) != 0)
6015 {
6016 p = alloc(STRLEN(line) + addlen + 1);
6017 if (p == NULL)
6018 break;
6019 mch_memmove(p, line, curwin->w_cursor.col);
6020 STRCPY(p + curwin->w_cursor.col, repl_to);
6021 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
6022 ml_replace(curwin->w_cursor.lnum, p, FALSE);
6023 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6024 didone = TRUE;
6025 }
6026 curwin->w_cursor.col += STRLEN(repl_to);
6027 }
6028
6029 p_ws = save_ws;
6030 curwin->w_cursor = pos;
6031 vim_free(frompat);
6032
6033 if (!didone)
6034 EMSG2(_("E753: Not found: %s"), repl_from);
6035}
6036
6037/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006038 * Find spell suggestions for "word". Return them in the growarray "*gap" as
6039 * a list of allocated strings.
6040 */
6041 void
6042spell_suggest_list(gap, word, maxcount)
6043 garray_T *gap;
6044 char_u *word;
6045 int maxcount; /* maximum nr of suggestions */
6046{
6047 suginfo_T sug;
6048 int i;
6049 suggest_T *stp;
6050 char_u *wcopy;
6051
Bram Moolenaarea408852005-06-25 22:49:46 +00006052 spell_find_suggest(word, &sug, maxcount, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006053
6054 /* Make room in "gap". */
6055 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
6056 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
6057 return;
6058
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006059 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006060 {
6061 stp = &SUG(sug.su_ga, i);
6062
6063 /* The suggested word may replace only part of "word", add the not
6064 * replaced part. */
6065 wcopy = alloc(STRLEN(stp->st_word)
6066 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
6067 if (wcopy == NULL)
6068 break;
6069 STRCPY(wcopy, stp->st_word);
6070 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
6071 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
6072 }
6073
6074 spell_find_cleanup(&sug);
6075}
6076
6077/*
6078 * Find spell suggestions for the word at the start of "badptr".
6079 * Return the suggestions in "su->su_ga".
6080 * The maximum number of suggestions is "maxcount".
6081 * Note: does use info for the current window.
6082 * This is based on the mechanisms of Aspell, but completely reimplemented.
6083 */
6084 static void
Bram Moolenaarea408852005-06-25 22:49:46 +00006085spell_find_suggest(badptr, su, maxcount, banbadword)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006086 char_u *badptr;
6087 suginfo_T *su;
6088 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00006089 int banbadword; /* don't include badword in suggestions */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006090{
6091 int attr;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006092 char_u buf[MAXPATHL];
6093 char_u *p;
6094 int do_combine = FALSE;
6095 char_u *sps_copy;
6096#ifdef FEAT_EVAL
6097 static int expr_busy = FALSE;
6098#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006099
6100 /*
6101 * Set the info in "*su".
6102 */
6103 vim_memset(su, 0, sizeof(suginfo_T));
6104 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
6105 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006106 if (*badptr == NUL)
6107 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006108 hash_init(&su->su_banned);
6109
6110 su->su_badptr = badptr;
6111 su->su_badlen = spell_check(curwin, su->su_badptr, &attr);
6112 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006113 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006114
6115 if (su->su_badlen >= MAXWLEN)
6116 su->su_badlen = MAXWLEN - 1; /* just in case */
6117 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
6118 (void)spell_casefold(su->su_badptr, su->su_badlen,
6119 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006120 /* get caps flags for bad word */
6121 su->su_badflags = captype(su->su_badptr, su->su_badptr + su->su_badlen);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006122
6123 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00006124 if (banbadword)
6125 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006126
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006127 /* Make a copy of 'spellsuggest', because the expression may change it. */
6128 sps_copy = vim_strsave(p_sps);
6129 if (sps_copy == NULL)
6130 return;
6131
6132 /* Loop over the items in 'spellsuggest'. */
6133 for (p = sps_copy; *p != NUL; )
6134 {
6135 copy_option_part(&p, buf, MAXPATHL, ",");
6136
6137 if (STRNCMP(buf, "expr:", 5) == 0)
6138 {
6139#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006140 /* Evaluate an expression. Skip this when called recursively,
6141 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006142 if (!expr_busy)
6143 {
6144 expr_busy = TRUE;
6145 spell_suggest_expr(su, buf + 5);
6146 expr_busy = FALSE;
6147 }
6148#endif
6149 }
6150 else if (STRNCMP(buf, "file:", 5) == 0)
6151 /* Use list of suggestions in a file. */
6152 spell_suggest_file(su, buf + 5);
6153 else
6154 {
6155 /* Use internal method. */
6156 spell_suggest_intern(su);
6157 if (sps_flags & SPS_DOUBLE)
6158 do_combine = TRUE;
6159 }
6160 }
6161
6162 vim_free(sps_copy);
6163
6164 if (do_combine)
6165 /* Combine the two list of suggestions. This must be done last,
6166 * because sorting changes the order again. */
6167 score_combine(su);
6168}
6169
6170#ifdef FEAT_EVAL
6171/*
6172 * Find suggestions by evaluating expression "expr".
6173 */
6174 static void
6175spell_suggest_expr(su, expr)
6176 suginfo_T *su;
6177 char_u *expr;
6178{
6179 list_T *list;
6180 listitem_T *li;
6181 int score;
6182 char_u *p;
6183
6184 /* The work is split up in a few parts to avoid having to export
6185 * suginfo_T.
6186 * First evaluate the expression and get the resulting list. */
6187 list = eval_spell_expr(su->su_badword, expr);
6188 if (list != NULL)
6189 {
6190 /* Loop over the items in the list. */
6191 for (li = list->lv_first; li != NULL; li = li->li_next)
6192 if (li->li_tv.v_type == VAR_LIST)
6193 {
6194 /* Get the word and the score from the items. */
6195 score = get_spellword(li->li_tv.vval.v_list, &p);
6196 if (score >= 0)
6197 add_suggestion(su, &su->su_ga, p,
6198 su->su_badlen, score, 0, TRUE);
6199 }
6200 list_unref(list);
6201 }
6202
6203 /* Sort the suggestions and truncate at "maxcount". */
6204 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
6205}
6206#endif
6207
6208/*
6209 * Find suggestions a file "fname".
6210 */
6211 static void
6212spell_suggest_file(su, fname)
6213 suginfo_T *su;
6214 char_u *fname;
6215{
6216 FILE *fd;
6217 char_u line[MAXWLEN * 2];
6218 char_u *p;
6219 int len;
6220 char_u cword[MAXWLEN];
6221
6222 /* Open the file. */
6223 fd = mch_fopen((char *)fname, "r");
6224 if (fd == NULL)
6225 {
6226 EMSG2(_(e_notopen), fname);
6227 return;
6228 }
6229
6230 /* Read it line by line. */
6231 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
6232 {
6233 line_breakcheck();
6234
6235 p = vim_strchr(line, '/');
6236 if (p == NULL)
6237 continue; /* No Tab found, just skip the line. */
6238 *p++ = NUL;
6239 if (STRICMP(su->su_badword, line) == 0)
6240 {
6241 /* Match! Isolate the good word, until CR or NL. */
6242 for (len = 0; p[len] >= ' '; ++len)
6243 ;
6244 p[len] = NUL;
6245
6246 /* If the suggestion doesn't have specific case duplicate the case
6247 * of the bad word. */
6248 if (captype(p, NULL) == 0)
6249 {
6250 make_case_word(p, cword, su->su_badflags);
6251 p = cword;
6252 }
6253
6254 add_suggestion(su, &su->su_ga, p, su->su_badlen,
6255 SCORE_FILE, 0, TRUE);
6256 }
6257 }
6258
6259 fclose(fd);
6260
6261 /* Sort the suggestions and truncate at "maxcount". */
6262 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
6263}
6264
6265/*
6266 * Find suggestions for the internal method indicated by "sps_flags".
6267 */
6268 static void
6269spell_suggest_intern(su)
6270 suginfo_T *su;
6271{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006272 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006273 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006274 *
6275 * Set a maximum score to limit the combination of operations that is
6276 * tried.
6277 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006278 suggest_try_special(su);
6279
6280 /*
6281 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
6282 * from the .aff file and inserting a space (split the word).
6283 */
6284 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006285
6286 /* For the resulting top-scorers compute the sound-a-like score. */
6287 if (sps_flags & SPS_DOUBLE)
6288 score_comp_sal(su);
6289
6290 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006291 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006292 *
6293 * Only do this when we don't have a lot of suggestions yet, because it's
6294 * very slow and often doesn't find new suggestions.
6295 */
6296 if ((sps_flags & SPS_DOUBLE)
6297 || (!(sps_flags & SPS_FAST)
6298 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
6299 {
6300 /* Allow a higher score now. */
6301 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006302 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006303 }
6304
6305 /* When CTRL-C was hit while searching do show the results. */
6306 ui_breakcheck();
6307 if (got_int)
6308 {
6309 (void)vgetc();
6310 got_int = FALSE;
6311 }
6312
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006313 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006314 {
6315 if (sps_flags & SPS_BEST)
6316 /* Adjust the word score for how it sounds like. */
6317 rescore_suggestions(su);
6318
6319 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006320 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006321 }
6322}
6323
6324/*
6325 * Free the info put in "*su" by spell_find_suggest().
6326 */
6327 static void
6328spell_find_cleanup(su)
6329 suginfo_T *su;
6330{
6331 int i;
6332
6333 /* Free the suggestions. */
6334 for (i = 0; i < su->su_ga.ga_len; ++i)
6335 vim_free(SUG(su->su_ga, i).st_word);
6336 ga_clear(&su->su_ga);
6337 for (i = 0; i < su->su_sga.ga_len; ++i)
6338 vim_free(SUG(su->su_sga, i).st_word);
6339 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006340
6341 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006342 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006343}
6344
6345/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006346 * Make a copy of "word", with the first letter upper or lower cased, to
6347 * "wcopy[MAXWLEN]". "word" must not be empty.
6348 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006349 */
6350 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006351onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006352 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006353 char_u *wcopy;
6354 int upper; /* TRUE: first letter made upper case */
6355{
6356 char_u *p;
6357 int c;
6358 int l;
6359
6360 p = word;
6361#ifdef FEAT_MBYTE
6362 if (has_mbyte)
6363 c = mb_ptr2char_adv(&p);
6364 else
6365#endif
6366 c = *p++;
6367 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006368 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006369 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006370 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006371#ifdef FEAT_MBYTE
6372 if (has_mbyte)
6373 l = mb_char2bytes(c, wcopy);
6374 else
6375#endif
6376 {
6377 l = 1;
6378 wcopy[0] = c;
6379 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006380 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006381}
6382
6383/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006384 * Make a copy of "word" with all the letters upper cased into
6385 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006386 */
6387 static void
6388allcap_copy(word, wcopy)
6389 char_u *word;
6390 char_u *wcopy;
6391{
6392 char_u *s;
6393 char_u *d;
6394 int c;
6395
6396 d = wcopy;
6397 for (s = word; *s != NUL; )
6398 {
6399#ifdef FEAT_MBYTE
6400 if (has_mbyte)
6401 c = mb_ptr2char_adv(&s);
6402 else
6403#endif
6404 c = *s++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006405 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006406
6407#ifdef FEAT_MBYTE
6408 if (has_mbyte)
6409 {
6410 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
6411 break;
6412 d += mb_char2bytes(c, d);
6413 }
6414 else
6415#endif
6416 {
6417 if (d - wcopy >= MAXWLEN - 1)
6418 break;
6419 *d++ = c;
6420 }
6421 }
6422 *d = NUL;
6423}
6424
6425/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006426 * Try finding suggestions by recognizing specific situations.
6427 */
6428 static void
6429suggest_try_special(su)
6430 suginfo_T *su;
6431{
6432 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006433 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006434 int c;
6435 char_u word[MAXWLEN];
6436
6437 /*
6438 * Recognize a word that is repeated: "the the".
6439 */
6440 p = skiptowhite(su->su_fbadword);
6441 len = p - su->su_fbadword;
6442 p = skipwhite(p);
6443 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
6444 {
6445 /* Include badflags: if the badword is onecap or allcap
6446 * use that for the goodword too: "The the" -> "The". */
6447 c = su->su_fbadword[len];
6448 su->su_fbadword[len] = NUL;
6449 make_case_word(su->su_fbadword, word, su->su_badflags);
6450 su->su_fbadword[len] = c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006451 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006452 }
6453}
6454
6455/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006456 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00006457 *
6458 * This uses a state machine. At each node in the tree we try various
6459 * operations. When trying if an operation work "depth" is increased and the
6460 * stack[] is used to store info. This allows combinations, thus insert one
6461 * character, replace one and delete another. The number of changes is
6462 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006463 */
6464 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00006465suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006466 suginfo_T *su;
6467{
6468 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
6469 char_u tword[MAXWLEN]; /* good word collected so far */
6470 trystate_T stack[MAXWLEN];
6471 char_u preword[MAXWLEN * 3]; /* word found with proper case (appended
6472 * to for word split) */
6473 char_u prewordlen = 0; /* length of word in "preword" */
6474 int splitoff = 0; /* index in tword after last split */
6475 trystate_T *sp;
6476 int newscore;
6477 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006478 char_u *byts, *fbyts, *pbyts;
6479 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006480 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00006481 int c, c2, c3;
6482 int n = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006483 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006484 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006485 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006486 int len;
6487 char_u *p;
6488 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00006489 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006490 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006491
6492 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00006493 * to find matches (esp. REP items). Append some more text, changing
6494 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006495 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006496 n = STRLEN(fword);
6497 p = su->su_badptr + su->su_badlen;
6498 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006499
6500 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
6501 lp->lp_slang != NULL; ++lp)
6502 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006503 /*
6504 * Go through the whole case-fold tree, try changes at each node.
6505 * "tword[]" contains the word collected from nodes in the tree.
6506 * "fword[]" the word we are trying to match with (initially the bad
6507 * word).
6508 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006509 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006510 sp = &stack[0];
6511 sp->ts_state = STATE_START;
6512 sp->ts_score = 0;
6513 sp->ts_curi = 1;
6514 sp->ts_fidx = 0;
6515 sp->ts_fidxtry = 0;
6516 sp->ts_twordlen = 0;
6517 sp->ts_arridx = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00006518#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006519 sp->ts_tcharlen = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00006520#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006521
Bram Moolenaarea424162005-06-16 21:51:00 +00006522 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006523 * When there are postponed prefixes we need to use these first. At
6524 * the end of the prefix we continue in the case-fold tree.
6525 */
6526 fbyts = lp->lp_slang->sl_fbyts;
6527 fidxs = lp->lp_slang->sl_fidxs;
6528 pbyts = lp->lp_slang->sl_pbyts;
6529 pidxs = lp->lp_slang->sl_pidxs;
6530 if (pbyts != NULL)
6531 {
6532 byts = pbyts;
6533 idxs = pidxs;
6534 sp->ts_prefixdepth = PREFIXTREE;
6535 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
6536 }
6537 else
6538 {
6539 byts = fbyts;
6540 idxs = fidxs;
6541 sp->ts_prefixdepth = NOPREFIX;
6542 }
6543
6544 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00006545 * Loop to find all suggestions. At each round we either:
6546 * - For the current state try one operation, advance "ts_curi",
6547 * increase "depth".
6548 * - When a state is done go to the next, set "ts_state".
6549 * - When all states are tried decrease "depth".
6550 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006551 while (depth >= 0 && !got_int)
6552 {
6553 sp = &stack[depth];
6554 switch (sp->ts_state)
6555 {
6556 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006557 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006558 /*
6559 * Start of node: Deal with NUL bytes, which means
6560 * tword[] may end here.
6561 */
6562 arridx = sp->ts_arridx; /* current node in the tree */
6563 len = byts[arridx]; /* bytes in this node */
6564 arridx += sp->ts_curi; /* index of current byte */
6565
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006566 if (sp->ts_prefixdepth == PREFIXTREE)
6567 {
6568 /* Skip over the NUL bytes, we use them later. */
6569 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
6570 ;
6571 sp->ts_curi += n;
6572
6573 /* At end of a prefix or at start of prefixtree: check for
6574 * following word. */
6575 if (byts[arridx] == 0 || sp->ts_state == STATE_NOPREFIX)
6576 {
6577 sp->ts_state = STATE_START;
6578 ++depth;
6579 stack[depth] = stack[depth - 1];
6580 sp = &stack[depth];
6581 sp->ts_prefixdepth = depth - 1;
6582 byts = fbyts;
6583 idxs = fidxs;
6584 sp->ts_state = STATE_START;
6585 sp->ts_curi = 1; /* start just after length byte */
6586 sp->ts_arridx = 0;
6587
6588 /* Move the prefix to preword[] so that
6589 * find_keepcap_word() works. */
6590 prewordlen = splitoff = sp->ts_twordlen;
6591 mch_memmove(preword, tword, splitoff);
6592 break;
6593 }
6594
6595 /* Always past NUL bytes now. */
6596 sp->ts_state = STATE_ENDNUL;
6597 break;
6598 }
6599
Bram Moolenaar0c405862005-06-22 22:26:26 +00006600 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006601 {
6602 /* Past bytes in node and/or past NUL bytes. */
6603 sp->ts_state = STATE_ENDNUL;
6604 break;
6605 }
6606
6607 /*
6608 * End of word in tree.
6609 */
6610 ++sp->ts_curi; /* eat one NUL byte */
6611
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006612 flags = (int)idxs[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006613
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006614 if (sp->ts_prefixdepth < MAXWLEN)
6615 {
6616 /* There was a prefix before the word. Check that the
6617 * prefix can be used with this word. */
6618 /* Count the length of the NULs in the prefix. If there
6619 * are none this must be the first try without a prefix.
6620 */
6621 n = stack[sp->ts_prefixdepth].ts_arridx;
6622 len = pbyts[n++];
6623 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
6624 ;
6625 if (c > 0)
6626 {
6627 /* The prefix ID is stored two bytes above the flags. */
6628 c = valid_word_prefix(c, n, (unsigned)flags >> 16,
6629 tword + splitoff, lp->lp_slang);
6630 if (c == 0)
6631 break;
6632
6633 /* Use the WF_RARE flag for a rare prefix. */
6634 if (c & WF_RAREPFX)
6635 flags |= WF_RARE;
6636 }
6637 }
6638
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006639 /*
6640 * Form the word with proper case in preword.
6641 * If there is a word from a previous split, append.
6642 */
6643 tword[sp->ts_twordlen] = NUL;
6644 if (flags & WF_KEEPCAP)
6645 /* Must find the word in the keep-case tree. */
6646 find_keepcap_word(lp->lp_slang, tword + splitoff,
6647 preword + prewordlen);
6648 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00006649 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006650 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00006651 * use that for the goodword too. But if the badword is
6652 * allcap and it's only one char long use onecap. */
6653 c = su->su_badflags;
6654 if ((c & WF_ALLCAP)
6655#ifdef FEAT_MBYTE
6656 && su->su_badlen == mb_ptr2len_check(su->su_badptr)
6657#else
6658 && su->su_badlen == 1
6659#endif
6660 )
6661 c = WF_ONECAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006662 make_case_word(tword + splitoff,
Bram Moolenaar0c405862005-06-22 22:26:26 +00006663 preword + prewordlen, flags | c);
6664 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006665
6666 /* Don't use a banned word. It may appear again as a good
6667 * word, thus remember it. */
6668 if (flags & WF_BANNED)
6669 {
6670 add_banned(su, preword + prewordlen);
6671 break;
6672 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006673 if (was_banned(su, preword + prewordlen)
6674 || was_banned(su, preword))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006675 break;
6676
6677 newscore = 0;
6678 if ((flags & WF_REGION)
6679 && (((unsigned)flags >> 8) & lp->lp_region) == 0)
6680 newscore += SCORE_REGION;
6681 if (flags & WF_RARE)
6682 newscore += SCORE_RARE;
6683
Bram Moolenaar0c405862005-06-22 22:26:26 +00006684 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006685 captype(preword + prewordlen, NULL)))
6686 newscore += SCORE_ICASE;
6687
Bram Moolenaar0c405862005-06-22 22:26:26 +00006688 if ((fword[sp->ts_fidx] == NUL
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006689 || !spell_iswordp(fword + sp->ts_fidx, curbuf))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006690 && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006691 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006692 /* The badword also ends: add suggestions. Give a penalty
6693 * when changing non-word char to word char, e.g., "thes,"
6694 * -> "these". */
6695 p = fword + sp->ts_fidx;
6696#ifdef FEAT_MBYTE
6697 if (has_mbyte)
6698 mb_ptr_back(fword, p);
6699 else
6700#endif
6701 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006702 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006703 {
6704 p = preword + STRLEN(preword);
6705#ifdef FEAT_MBYTE
6706 if (has_mbyte)
6707 mb_ptr_back(preword, p);
6708 else
6709#endif
6710 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006711 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006712 newscore += SCORE_NONWORD;
6713 }
6714
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006715 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00006716 sp->ts_fidx - repextra,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006717 sp->ts_score + newscore, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006718 }
Bram Moolenaarea424162005-06-16 21:51:00 +00006719 else if (sp->ts_fidx >= sp->ts_fidxtry
6720#ifdef FEAT_MBYTE
6721 /* Don't split halfway a character. */
6722 && (!has_mbyte || sp->ts_tcharlen == 0)
6723#endif
6724 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006725 {
6726 /* The word in the tree ends but the badword
6727 * continues: try inserting a space and check that a valid
6728 * words starts at fword[sp->ts_fidx]. */
6729 if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT))
6730 {
6731 /* Save things to be restored at STATE_SPLITUNDO. */
6732 sp->ts_save_prewordlen = prewordlen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006733 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006734 sp->ts_save_splitoff = splitoff;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006735 sp->ts_state = STATE_SPLITUNDO;
6736
6737 ++depth;
6738 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006739
6740 /* Append a space to preword. */
6741 STRCAT(preword, " ");
6742 prewordlen = STRLEN(preword);
6743 splitoff = sp->ts_twordlen;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006744
6745 /* If the badword has a non-word character at this
6746 * position skip it. That means replacing the
6747 * non-word character with a space. */
6748 if (!spell_iswordp_nmw(fword + sp->ts_fidx))
6749 {
6750 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
6751#ifdef FEAT_MBYTE
6752 if (has_mbyte)
6753 sp->ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
6754 else
6755#endif
6756 ++sp->ts_fidx;
6757 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006758#ifdef FEAT_MBYTE
6759 if (has_mbyte)
6760 {
6761 int i = 0;
6762
6763 /* Case-folding may change the number of bytes:
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006764 * Count nr of chars in fword[ts_fidx] and
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006765 * advance that many chars in su->su_badptr. */
6766 for (p = fword; p < fword + sp->ts_fidx;
6767 mb_ptr_adv(p))
6768 ++i;
6769 for (p = su->su_badptr; i > 0; mb_ptr_adv(p))
6770 --i;
6771 }
6772 else
6773#endif
6774 p = su->su_badptr + sp->ts_fidx;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006775 su->su_badflags = captype(p, su->su_badptr
6776 + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006777
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006778 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006779 sp->ts_arridx = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006780 }
6781 }
6782 break;
6783
6784 case STATE_SPLITUNDO:
Bram Moolenaar0c405862005-06-22 22:26:26 +00006785 /* Undo the changes done for word split. */
6786 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006787 splitoff = sp->ts_save_splitoff;
6788 prewordlen = sp->ts_save_prewordlen;
6789
6790 /* Continue looking for NUL bytes. */
6791 sp->ts_state = STATE_START;
6792 break;
6793
6794 case STATE_ENDNUL:
6795 /* Past the NUL bytes in the node. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006796 if (fword[sp->ts_fidx] == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006797 {
6798 /* The badword ends, can't use the bytes in this node. */
6799 sp->ts_state = STATE_DEL;
6800 break;
6801 }
6802 sp->ts_state = STATE_PLAIN;
6803 /*FALLTHROUGH*/
6804
6805 case STATE_PLAIN:
6806 /*
6807 * Go over all possible bytes at this node, add each to
6808 * tword[] and use child node. "ts_curi" is the index.
6809 */
6810 arridx = sp->ts_arridx;
6811 if (sp->ts_curi > byts[arridx])
6812 {
6813 /* Done all bytes at this node, do next state. When still
6814 * at already changed bytes skip the other tricks. */
6815 if (sp->ts_fidx >= sp->ts_fidxtry)
6816 sp->ts_state = STATE_DEL;
6817 else
6818 sp->ts_state = STATE_FINAL;
6819 }
6820 else
6821 {
6822 arridx += sp->ts_curi++;
6823 c = byts[arridx];
6824
6825 /* Normal byte, go one level deeper. If it's not equal to
6826 * the byte in the bad word adjust the score. But don't
6827 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00006828 if (c == fword[sp->ts_fidx]
6829#ifdef FEAT_MBYTE
6830 || (sp->ts_tcharlen > 0
6831 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006832#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00006833 )
6834 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006835 else
6836 newscore = SCORE_SUBST;
6837 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
6838 && try_deeper(su, stack, depth, newscore))
6839 {
6840 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00006841 sp = &stack[depth];
6842 ++sp->ts_fidx;
6843 tword[sp->ts_twordlen++] = c;
6844 sp->ts_arridx = idxs[arridx];
6845#ifdef FEAT_MBYTE
6846 if (newscore == SCORE_SUBST)
6847 sp->ts_isdiff = DIFF_YES;
6848 if (has_mbyte)
6849 {
6850 /* Multi-byte characters are a bit complicated to
6851 * handle: They differ when any of the bytes
6852 * differ and then their length may also differ. */
6853 if (sp->ts_tcharlen == 0)
6854 {
6855 /* First byte. */
6856 sp->ts_tcharidx = 0;
6857 sp->ts_tcharlen = MB_BYTE2LEN(c);
6858 sp->ts_fcharstart = sp->ts_fidx - 1;
6859 sp->ts_isdiff = (newscore != 0)
6860 ? DIFF_YES : DIFF_NONE;
6861 }
6862 else if (sp->ts_isdiff == DIFF_INSERT)
6863 /* When inserting trail bytes don't advance in
6864 * the bad word. */
6865 --sp->ts_fidx;
6866 if (++sp->ts_tcharidx == sp->ts_tcharlen)
6867 {
6868 /* Last byte of character. */
6869 if (sp->ts_isdiff == DIFF_YES)
6870 {
6871 /* Correct ts_fidx for the byte length of
6872 * the character (we didn't check that
6873 * before). */
6874 sp->ts_fidx = sp->ts_fcharstart
6875 + MB_BYTE2LEN(
6876 fword[sp->ts_fcharstart]);
6877
6878 /* For a similar character adjust score
6879 * from SCORE_SUBST to SCORE_SIMILAR. */
6880 if (lp->lp_slang->sl_has_map
6881 && similar_chars(lp->lp_slang,
6882 mb_ptr2char(tword
6883 + sp->ts_twordlen
6884 - sp->ts_tcharlen),
6885 mb_ptr2char(fword
6886 + sp->ts_fcharstart)))
6887 sp->ts_score -=
6888 SCORE_SUBST - SCORE_SIMILAR;
6889 }
Bram Moolenaarea408852005-06-25 22:49:46 +00006890 else if (sp->ts_isdiff == DIFF_INSERT
6891 && sp->ts_twordlen > sp->ts_tcharlen)
6892 {
6893 /* If the previous character was the same,
6894 * thus doubling a character, give a bonus
6895 * to the score. */
6896 p = tword + sp->ts_twordlen
6897 - sp->ts_tcharlen;
6898 c = mb_ptr2char(p);
6899 mb_ptr_back(tword, p);
6900 if (c == mb_ptr2char(p))
6901 sp->ts_score -= SCORE_INS
6902 - SCORE_INSDUP;
6903 }
Bram Moolenaarea424162005-06-16 21:51:00 +00006904
6905 /* Starting a new char, reset the length. */
6906 sp->ts_tcharlen = 0;
6907 }
6908 }
6909 else
6910#endif
6911 {
6912 /* If we found a similar char adjust the score.
6913 * We do this after calling try_deeper() because
6914 * it's slow. */
6915 if (newscore != 0
6916 && lp->lp_slang->sl_has_map
6917 && similar_chars(lp->lp_slang,
6918 c, fword[sp->ts_fidx - 1]))
6919 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
6920 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006921 }
6922 }
6923 break;
6924
6925 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00006926#ifdef FEAT_MBYTE
6927 /* When past the first byte of a multi-byte char don't try
6928 * delete/insert/swap a character. */
6929 if (has_mbyte && sp->ts_tcharlen > 0)
6930 {
6931 sp->ts_state = STATE_FINAL;
6932 break;
6933 }
6934#endif
6935 /*
6936 * Try skipping one character in the bad word (delete it).
6937 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006938 sp->ts_state = STATE_INS;
6939 sp->ts_curi = 1;
6940 if (fword[sp->ts_fidx] != NUL
6941 && try_deeper(su, stack, depth, SCORE_DEL))
6942 {
6943 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00006944
6945 /* Advance over the character in fword[]. Give a bonus to
6946 * the score if the same character is following "nn" ->
6947 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00006948#ifdef FEAT_MBYTE
6949 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00006950 {
6951 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00006952 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaarea408852005-06-25 22:49:46 +00006953 if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
6954 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
6955 }
Bram Moolenaarea424162005-06-16 21:51:00 +00006956 else
6957#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00006958 {
Bram Moolenaarea424162005-06-16 21:51:00 +00006959 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00006960 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
6961 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
6962 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006963 break;
6964 }
6965 /*FALLTHROUGH*/
6966
6967 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00006968 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006969 * node. */
6970 n = sp->ts_arridx;
6971 if (sp->ts_curi > byts[n])
6972 {
6973 /* Done all bytes at this node, do next state. */
6974 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006975 }
6976 else
6977 {
Bram Moolenaarea424162005-06-16 21:51:00 +00006978 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006979 n += sp->ts_curi++;
6980 c = byts[n];
6981 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
6982 {
6983 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00006984 sp = &stack[depth];
6985 tword[sp->ts_twordlen++] = c;
6986 sp->ts_arridx = idxs[n];
6987#ifdef FEAT_MBYTE
6988 if (has_mbyte)
6989 {
6990 fl = MB_BYTE2LEN(c);
6991 if (fl > 1)
6992 {
6993 /* There are following bytes for the same
6994 * character. We must find all bytes before
6995 * trying delete/insert/swap/etc. */
6996 sp->ts_tcharlen = fl;
6997 sp->ts_tcharidx = 1;
6998 sp->ts_isdiff = DIFF_INSERT;
6999 }
7000 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007001 else
7002 fl = 1;
7003 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00007004#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00007005 {
7006 /* If the previous character was the same, thus
7007 * doubling a character, give a bonus to the
7008 * score. */
7009 if (sp->ts_twordlen >= 2
7010 && tword[sp->ts_twordlen - 2] == c)
7011 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
7012 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007013 }
7014 }
7015 break;
7016
7017 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00007018 /*
7019 * Swap two bytes in the bad word: "12" -> "21".
7020 * We change "fword" here, it's changed back afterwards.
7021 */
7022 p = fword + sp->ts_fidx;
7023 c = *p;
7024 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007025 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007026 /* End of word, can't swap or replace. */
7027 sp->ts_state = STATE_FINAL;
7028 break;
7029 }
7030#ifdef FEAT_MBYTE
7031 if (has_mbyte)
7032 {
7033 n = mb_ptr2len_check(p);
7034 c = mb_ptr2char(p);
7035 c2 = mb_ptr2char(p + n);
7036 }
7037 else
7038#endif
7039 c2 = p[1];
7040 if (c == c2)
7041 {
7042 /* Characters are identical, swap won't do anything. */
7043 sp->ts_state = STATE_SWAP3;
7044 break;
7045 }
7046 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
7047 {
7048 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007049 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007050#ifdef FEAT_MBYTE
7051 if (has_mbyte)
7052 {
7053 fl = mb_char2len(c2);
7054 mch_memmove(p, p + n, fl);
7055 mb_char2bytes(c, p + fl);
7056 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
7057 }
7058 else
7059#endif
7060 {
7061 p[0] = c2;
7062 p[1] = c;
7063 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
7064 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007065 }
7066 else
7067 /* If this swap doesn't work then SWAP3 won't either. */
7068 sp->ts_state = STATE_REP_INI;
7069 break;
7070
Bram Moolenaarea424162005-06-16 21:51:00 +00007071 case STATE_UNSWAP:
7072 /* Undo the STATE_SWAP swap: "21" -> "12". */
7073 p = fword + sp->ts_fidx;
7074#ifdef FEAT_MBYTE
7075 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007076 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007077 n = MB_BYTE2LEN(*p);
7078 c = mb_ptr2char(p + n);
7079 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
7080 mb_char2bytes(c, p);
7081 }
7082 else
7083#endif
7084 {
7085 c = *p;
7086 *p = p[1];
7087 p[1] = c;
7088 }
7089 /*FALLTHROUGH*/
7090
7091 case STATE_SWAP3:
7092 /* Swap two bytes, skipping one: "123" -> "321". We change
7093 * "fword" here, it's changed back afterwards. */
7094 p = fword + sp->ts_fidx;
7095#ifdef FEAT_MBYTE
7096 if (has_mbyte)
7097 {
7098 n = mb_ptr2len_check(p);
7099 c = mb_ptr2char(p);
7100 fl = mb_ptr2len_check(p + n);
7101 c2 = mb_ptr2char(p + n);
7102 c3 = mb_ptr2char(p + n + fl);
7103 }
7104 else
7105#endif
7106 {
7107 c = *p;
7108 c2 = p[1];
7109 c3 = p[2];
7110 }
7111
7112 /* When characters are identical: "121" then SWAP3 result is
7113 * identical, ROT3L result is same as SWAP: "211", ROT3L
7114 * result is same as SWAP on next char: "112". Thus skip all
7115 * swapping. Also skip when c3 is NUL. */
7116 if (c == c3 || c3 == NUL)
7117 {
7118 sp->ts_state = STATE_REP_INI;
7119 break;
7120 }
7121 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7122 {
7123 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007124 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007125#ifdef FEAT_MBYTE
7126 if (has_mbyte)
7127 {
7128 tl = mb_char2len(c3);
7129 mch_memmove(p, p + n + fl, tl);
7130 mb_char2bytes(c2, p + tl);
7131 mb_char2bytes(c, p + fl + tl);
7132 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
7133 }
7134 else
7135#endif
7136 {
7137 p[0] = p[2];
7138 p[2] = c;
7139 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7140 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007141 }
7142 else
7143 sp->ts_state = STATE_REP_INI;
7144 break;
7145
Bram Moolenaarea424162005-06-16 21:51:00 +00007146 case STATE_UNSWAP3:
7147 /* Undo STATE_SWAP3: "321" -> "123" */
7148 p = fword + sp->ts_fidx;
7149#ifdef FEAT_MBYTE
7150 if (has_mbyte)
7151 {
7152 n = MB_BYTE2LEN(*p);
7153 c2 = mb_ptr2char(p + n);
7154 fl = MB_BYTE2LEN(p[n]);
7155 c = mb_ptr2char(p + n + fl);
7156 tl = MB_BYTE2LEN(p[n + fl]);
7157 mch_memmove(p + fl + tl, p, n);
7158 mb_char2bytes(c, p);
7159 mb_char2bytes(c2, p + tl);
7160 }
7161 else
7162#endif
7163 {
7164 c = *p;
7165 *p = p[2];
7166 p[2] = c;
7167 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007168
Bram Moolenaarea424162005-06-16 21:51:00 +00007169 /* Rotate three characters left: "123" -> "231". We change
7170 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007171 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7172 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007173 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007174 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007175 p = fword + sp->ts_fidx;
7176#ifdef FEAT_MBYTE
7177 if (has_mbyte)
7178 {
7179 n = mb_ptr2len_check(p);
7180 c = mb_ptr2char(p);
7181 fl = mb_ptr2len_check(p + n);
7182 fl += mb_ptr2len_check(p + n + fl);
7183 mch_memmove(p, p + n, fl);
7184 mb_char2bytes(c, p + fl);
7185 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
7186 }
7187 else
7188#endif
7189 {
7190 c = *p;
7191 *p = p[1];
7192 p[1] = p[2];
7193 p[2] = c;
7194 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7195 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007196 }
7197 else
7198 sp->ts_state = STATE_REP_INI;
7199 break;
7200
Bram Moolenaarea424162005-06-16 21:51:00 +00007201 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007202 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00007203 p = fword + sp->ts_fidx;
7204#ifdef FEAT_MBYTE
7205 if (has_mbyte)
7206 {
7207 n = MB_BYTE2LEN(*p);
7208 n += MB_BYTE2LEN(p[n]);
7209 c = mb_ptr2char(p + n);
7210 tl = MB_BYTE2LEN(p[n]);
7211 mch_memmove(p + tl, p, n);
7212 mb_char2bytes(c, p);
7213 }
7214 else
7215#endif
7216 {
7217 c = p[2];
7218 p[2] = p[1];
7219 p[1] = *p;
7220 *p = c;
7221 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007222
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007223 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00007224 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007225 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7226 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007227 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007228 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007229 p = fword + sp->ts_fidx;
7230#ifdef FEAT_MBYTE
7231 if (has_mbyte)
7232 {
7233 n = mb_ptr2len_check(p);
7234 n += mb_ptr2len_check(p + n);
7235 c = mb_ptr2char(p + n);
7236 tl = mb_ptr2len_check(p + n);
7237 mch_memmove(p + tl, p, n);
7238 mb_char2bytes(c, p);
7239 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
7240 }
7241 else
7242#endif
7243 {
7244 c = p[2];
7245 p[2] = p[1];
7246 p[1] = *p;
7247 *p = c;
7248 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7249 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007250 }
7251 else
7252 sp->ts_state = STATE_REP_INI;
7253 break;
7254
Bram Moolenaarea424162005-06-16 21:51:00 +00007255 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007256 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00007257 p = fword + sp->ts_fidx;
7258#ifdef FEAT_MBYTE
7259 if (has_mbyte)
7260 {
7261 c = mb_ptr2char(p);
7262 tl = MB_BYTE2LEN(*p);
7263 n = MB_BYTE2LEN(p[tl]);
7264 n += MB_BYTE2LEN(p[tl + n]);
7265 mch_memmove(p, p + tl, n);
7266 mb_char2bytes(c, p + n);
7267 }
7268 else
7269#endif
7270 {
7271 c = *p;
7272 *p = p[1];
7273 p[1] = p[2];
7274 p[2] = c;
7275 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007276 /*FALLTHROUGH*/
7277
7278 case STATE_REP_INI:
7279 /* Check if matching with REP items from the .aff file would
7280 * work. Quickly skip if there are no REP items or the score
7281 * is going to be too high anyway. */
7282 gap = &lp->lp_slang->sl_rep;
7283 if (gap->ga_len == 0
7284 || sp->ts_score + SCORE_REP >= su->su_maxscore)
7285 {
7286 sp->ts_state = STATE_FINAL;
7287 break;
7288 }
7289
7290 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00007291 * may match. If the index is -1 there is none. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007292 sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]];
7293 if (sp->ts_curi < 0)
7294 {
7295 sp->ts_state = STATE_FINAL;
7296 break;
7297 }
7298
7299 sp->ts_state = STATE_REP;
7300 /*FALLTHROUGH*/
7301
7302 case STATE_REP:
7303 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00007304 * match replace the characters and check if the resulting
7305 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007306 p = fword + sp->ts_fidx;
7307
7308 gap = &lp->lp_slang->sl_rep;
7309 while (sp->ts_curi < gap->ga_len)
7310 {
7311 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
7312 if (*ftp->ft_from != *p)
7313 {
7314 /* past possible matching entries */
7315 sp->ts_curi = gap->ga_len;
7316 break;
7317 }
7318 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
7319 && try_deeper(su, stack, depth, SCORE_REP))
7320 {
7321 /* Need to undo this afterwards. */
7322 sp->ts_state = STATE_REP_UNDO;
7323
7324 /* Change the "from" to the "to" string. */
7325 ++depth;
7326 fl = STRLEN(ftp->ft_from);
7327 tl = STRLEN(ftp->ft_to);
7328 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007329 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007330 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007331 repextra += tl - fl;
7332 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007333 mch_memmove(p, ftp->ft_to, tl);
7334 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +00007335#ifdef FEAT_MBYTE
7336 stack[depth].ts_tcharlen = 0;
7337#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007338 break;
7339 }
7340 }
7341
7342 if (sp->ts_curi >= gap->ga_len)
7343 /* No (more) matches. */
7344 sp->ts_state = STATE_FINAL;
7345
7346 break;
7347
7348 case STATE_REP_UNDO:
7349 /* Undo a REP replacement and continue with the next one. */
7350 ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data
7351 + sp->ts_curi - 1;
7352 fl = STRLEN(ftp->ft_from);
7353 tl = STRLEN(ftp->ft_to);
7354 p = fword + sp->ts_fidx;
7355 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007356 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007357 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007358 repextra -= tl - fl;
7359 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007360 mch_memmove(p, ftp->ft_from, fl);
7361 sp->ts_state = STATE_REP;
7362 break;
7363
7364 default:
7365 /* Did all possible states at this level, go up one level. */
7366 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007367
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007368 if (depth >= 0 && stack[depth].ts_prefixdepth == PREFIXTREE)
7369 {
7370 /* Continue in or go back to the prefix tree. */
7371 byts = pbyts;
7372 idxs = pidxs;
7373 splitoff = 0;
7374 }
7375
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007376 /* Don't check for CTRL-C too often, it takes time. */
7377 line_breakcheck();
7378 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007379 }
7380 }
7381}
7382
7383/*
7384 * Try going one level deeper in the tree.
7385 */
7386 static int
7387try_deeper(su, stack, depth, score_add)
7388 suginfo_T *su;
7389 trystate_T *stack;
7390 int depth;
7391 int score_add;
7392{
7393 int newscore;
7394
7395 /* Refuse to go deeper if the scrore is getting too big. */
7396 newscore = stack[depth].ts_score + score_add;
7397 if (newscore >= su->su_maxscore)
7398 return FALSE;
7399
Bram Moolenaarea424162005-06-16 21:51:00 +00007400 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007401 stack[depth + 1].ts_state = STATE_START;
7402 stack[depth + 1].ts_score = newscore;
7403 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007404 return TRUE;
7405}
7406
7407/*
7408 * "fword" is a good word with case folded. Find the matching keep-case
7409 * words and put it in "kword".
7410 * Theoretically there could be several keep-case words that result in the
7411 * same case-folded word, but we only find one...
7412 */
7413 static void
7414find_keepcap_word(slang, fword, kword)
7415 slang_T *slang;
7416 char_u *fword;
7417 char_u *kword;
7418{
7419 char_u uword[MAXWLEN]; /* "fword" in upper-case */
7420 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007421 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007422
7423 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007424 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007425 int round[MAXWLEN];
7426 int fwordidx[MAXWLEN];
7427 int uwordidx[MAXWLEN];
7428 int kwordlen[MAXWLEN];
7429
7430 int flen, ulen;
7431 int l;
7432 int len;
7433 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007434 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007435 char_u *p;
7436 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007437 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007438
7439 if (byts == NULL)
7440 {
7441 /* array is empty: "cannot happen" */
7442 *kword = NUL;
7443 return;
7444 }
7445
7446 /* Make an all-cap version of "fword". */
7447 allcap_copy(fword, uword);
7448
7449 /*
7450 * Each character needs to be tried both case-folded and upper-case.
7451 * All this gets very complicated if we keep in mind that changing case
7452 * may change the byte length of a multi-byte character...
7453 */
7454 depth = 0;
7455 arridx[0] = 0;
7456 round[0] = 0;
7457 fwordidx[0] = 0;
7458 uwordidx[0] = 0;
7459 kwordlen[0] = 0;
7460 while (depth >= 0)
7461 {
7462 if (fword[fwordidx[depth]] == NUL)
7463 {
7464 /* We are at the end of "fword". If the tree allows a word to end
7465 * here we have found a match. */
7466 if (byts[arridx[depth] + 1] == 0)
7467 {
7468 kword[kwordlen[depth]] = NUL;
7469 return;
7470 }
7471
7472 /* kword is getting too long, continue one level up */
7473 --depth;
7474 }
7475 else if (++round[depth] > 2)
7476 {
7477 /* tried both fold-case and upper-case character, continue one
7478 * level up */
7479 --depth;
7480 }
7481 else
7482 {
7483 /*
7484 * round[depth] == 1: Try using the folded-case character.
7485 * round[depth] == 2: Try using the upper-case character.
7486 */
7487#ifdef FEAT_MBYTE
7488 if (has_mbyte)
7489 {
7490 flen = mb_ptr2len_check(fword + fwordidx[depth]);
7491 ulen = mb_ptr2len_check(uword + uwordidx[depth]);
7492 }
7493 else
7494#endif
7495 ulen = flen = 1;
7496 if (round[depth] == 1)
7497 {
7498 p = fword + fwordidx[depth];
7499 l = flen;
7500 }
7501 else
7502 {
7503 p = uword + uwordidx[depth];
7504 l = ulen;
7505 }
7506
7507 for (tryidx = arridx[depth]; l > 0; --l)
7508 {
7509 /* Perform a binary search in the list of accepted bytes. */
7510 len = byts[tryidx++];
7511 c = *p++;
7512 lo = tryidx;
7513 hi = tryidx + len - 1;
7514 while (lo < hi)
7515 {
7516 m = (lo + hi) / 2;
7517 if (byts[m] > c)
7518 hi = m - 1;
7519 else if (byts[m] < c)
7520 lo = m + 1;
7521 else
7522 {
7523 lo = hi = m;
7524 break;
7525 }
7526 }
7527
7528 /* Stop if there is no matching byte. */
7529 if (hi < lo || byts[lo] != c)
7530 break;
7531
7532 /* Continue at the child (if there is one). */
7533 tryidx = idxs[lo];
7534 }
7535
7536 if (l == 0)
7537 {
7538 /*
7539 * Found the matching char. Copy it to "kword" and go a
7540 * level deeper.
7541 */
7542 if (round[depth] == 1)
7543 {
7544 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
7545 flen);
7546 kwordlen[depth + 1] = kwordlen[depth] + flen;
7547 }
7548 else
7549 {
7550 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
7551 ulen);
7552 kwordlen[depth + 1] = kwordlen[depth] + ulen;
7553 }
7554 fwordidx[depth + 1] = fwordidx[depth] + flen;
7555 uwordidx[depth + 1] = uwordidx[depth] + ulen;
7556
7557 ++depth;
7558 arridx[depth] = tryidx;
7559 round[depth] = 0;
7560 }
7561 }
7562 }
7563
7564 /* Didn't find it: "cannot happen". */
7565 *kword = NUL;
7566}
7567
7568/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007569 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
7570 * su->su_sga.
7571 */
7572 static void
7573score_comp_sal(su)
7574 suginfo_T *su;
7575{
7576 langp_T *lp;
7577 char_u badsound[MAXWLEN];
7578 int i;
7579 suggest_T *stp;
7580 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007581 int score;
7582
7583 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
7584 return;
7585
7586 /* Use the sound-folding of the first language that supports it. */
7587 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7588 lp->lp_slang != NULL; ++lp)
7589 if (lp->lp_slang->sl_sal.ga_len > 0)
7590 {
7591 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007592 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007593
7594 for (i = 0; i < su->su_ga.ga_len; ++i)
7595 {
7596 stp = &SUG(su->su_ga, i);
7597
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007598 /* Case-fold the suggested word, sound-fold it and compute the
7599 * sound-a-like score. */
7600 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007601 if (score < SCORE_MAXMAX)
7602 {
7603 /* Add the suggestion. */
7604 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
7605 sstp->st_word = vim_strsave(stp->st_word);
7606 if (sstp->st_word != NULL)
7607 {
7608 sstp->st_score = score;
7609 sstp->st_altscore = 0;
7610 sstp->st_orglen = stp->st_orglen;
7611 ++su->su_sga.ga_len;
7612 }
7613 }
7614 }
7615 break;
7616 }
7617}
7618
7619/*
7620 * Combine the list of suggestions in su->su_ga and su->su_sga.
7621 * They are intwined.
7622 */
7623 static void
7624score_combine(su)
7625 suginfo_T *su;
7626{
7627 int i;
7628 int j;
7629 garray_T ga;
7630 garray_T *gap;
7631 langp_T *lp;
7632 suggest_T *stp;
7633 char_u *p;
7634 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007635 int round;
7636
7637 /* Add the alternate score to su_ga. */
7638 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7639 lp->lp_slang != NULL; ++lp)
7640 {
7641 if (lp->lp_slang->sl_sal.ga_len > 0)
7642 {
7643 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007644 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007645
7646 for (i = 0; i < su->su_ga.ga_len; ++i)
7647 {
7648 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007649 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
7650 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007651 if (stp->st_altscore == SCORE_MAXMAX)
7652 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
7653 else
7654 stp->st_score = (stp->st_score * 3
7655 + stp->st_altscore) / 4;
7656 stp->st_salscore = FALSE;
7657 }
7658 break;
7659 }
7660 }
7661
7662 /* Add the alternate score to su_sga. */
7663 for (i = 0; i < su->su_sga.ga_len; ++i)
7664 {
7665 stp = &SUG(su->su_sga, i);
7666 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
7667 if (stp->st_score == SCORE_MAXMAX)
7668 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
7669 else
7670 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
7671 stp->st_salscore = TRUE;
7672 }
7673
7674 /* Sort the suggestions and truncate at "maxcount" for both lists. */
7675 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
7676 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
7677
7678 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
7679 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
7680 return;
7681
7682 stp = &SUG(ga, 0);
7683 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
7684 {
7685 /* round 1: get a suggestion from su_ga
7686 * round 2: get a suggestion from su_sga */
7687 for (round = 1; round <= 2; ++round)
7688 {
7689 gap = round == 1 ? &su->su_ga : &su->su_sga;
7690 if (i < gap->ga_len)
7691 {
7692 /* Don't add a word if it's already there. */
7693 p = SUG(*gap, i).st_word;
7694 for (j = 0; j < ga.ga_len; ++j)
7695 if (STRCMP(stp[j].st_word, p) == 0)
7696 break;
7697 if (j == ga.ga_len)
7698 stp[ga.ga_len++] = SUG(*gap, i);
7699 else
7700 vim_free(p);
7701 }
7702 }
7703 }
7704
7705 ga_clear(&su->su_ga);
7706 ga_clear(&su->su_sga);
7707
7708 /* Truncate the list to the number of suggestions that will be displayed. */
7709 if (ga.ga_len > su->su_maxcount)
7710 {
7711 for (i = su->su_maxcount; i < ga.ga_len; ++i)
7712 vim_free(stp[i].st_word);
7713 ga.ga_len = su->su_maxcount;
7714 }
7715
7716 su->su_ga = ga;
7717}
7718
7719/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007720 * For the goodword in "stp" compute the soundalike score compared to the
7721 * badword.
7722 */
7723 static int
7724stp_sal_score(stp, su, slang, badsound)
7725 suggest_T *stp;
7726 suginfo_T *su;
7727 slang_T *slang;
7728 char_u *badsound; /* sound-folded badword */
7729{
7730 char_u *p;
7731 char_u badsound2[MAXWLEN];
7732 char_u fword[MAXWLEN];
7733 char_u goodsound[MAXWLEN];
7734
7735 if (stp->st_orglen <= su->su_badlen)
7736 p = badsound;
7737 else
7738 {
7739 /* soundfold the bad word with more characters following */
7740 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
7741
7742 /* When joining two words the sound often changes a lot. E.g., "t he"
7743 * sounds like "t h" while "the" sounds like "@". Avoid that by
7744 * removing the space. Don't do it when the good word also contains a
7745 * space. */
7746 if (vim_iswhite(su->su_badptr[su->su_badlen])
7747 && *skiptowhite(stp->st_word) == NUL)
7748 for (p = fword; *(p = skiptowhite(p)) != NUL; )
7749 mch_memmove(p, p + 1, STRLEN(p));
7750
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007751 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007752 p = badsound2;
7753 }
7754
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007755 /* Sound-fold the word and compute the score for the difference. */
7756 spell_soundfold(slang, stp->st_word, FALSE, goodsound);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007757
7758 return soundalike_score(goodsound, p);
7759}
7760
7761/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007762 * Find suggestions by comparing the word in a sound-a-like form.
7763 */
7764 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00007765suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007766 suginfo_T *su;
7767{
7768 char_u salword[MAXWLEN];
7769 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007770 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007771 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007772 int curi[MAXWLEN];
7773 langp_T *lp;
7774 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007775 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007776 int depth;
7777 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007778 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007779 int round;
7780 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007781 int sound_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007782
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007783 /* Do this for all languages that support sound folding. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007784 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7785 lp->lp_slang != NULL; ++lp)
7786 {
7787 if (lp->lp_slang->sl_sal.ga_len > 0)
7788 {
7789 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007790 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007791
7792 /*
7793 * Go through the whole tree, soundfold each word and compare.
7794 * round 1: use the case-folded tree.
7795 * round 2: use the keep-case tree.
7796 */
7797 for (round = 1; round <= 2; ++round)
7798 {
7799 if (round == 1)
7800 {
7801 byts = lp->lp_slang->sl_fbyts;
7802 idxs = lp->lp_slang->sl_fidxs;
7803 }
7804 else
7805 {
7806 byts = lp->lp_slang->sl_kbyts;
7807 idxs = lp->lp_slang->sl_kidxs;
7808 }
7809
7810 depth = 0;
7811 arridx[0] = 0;
7812 curi[0] = 1;
7813 while (depth >= 0 && !got_int)
7814 {
7815 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007816 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007817 /* Done all bytes at this node, go up one level. */
7818 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007819 line_breakcheck();
7820 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007821 else
7822 {
7823 /* Do one more byte at this node. */
7824 n = arridx[depth] + curi[depth];
7825 ++curi[depth];
7826 c = byts[n];
7827 if (c == 0)
7828 {
7829 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007830 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007831 if (round == 2 || (flags & WF_KEEPCAP) == 0)
7832 {
7833 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007834 /* Sound-fold. Only in keep-case tree need to
7835 * case-fold the word. */
7836 spell_soundfold(lp->lp_slang, tword,
7837 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007838
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007839 /* Compute the edit distance between the
7840 * sound-a-like words. */
7841 sound_score = soundalike_score(salword,
7842 tsalword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007843 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007844 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007845 char_u cword[MAXWLEN];
7846 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007847 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007848
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007849 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007850 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007851 /* Need to fix case according to
7852 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007853 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007854 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007855 }
7856 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007857 p = tword;
7858
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007859 if (sps_flags & SPS_DOUBLE)
7860 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007861 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007862 sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007863 else
7864 {
7865 /* Compute the score. */
7866 score = spell_edit_score(
7867 su->su_badword, p);
7868 if (sps_flags & SPS_BEST)
7869 /* give a bonus for the good word
7870 * sounding the same as the bad
7871 * word */
7872 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007873 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007874 RESCORE(score, sound_score),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007875 sound_score, TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007876 else
7877 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007878 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007879 score + sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007880 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007881 }
7882 }
7883
7884 /* Skip over other NUL bytes. */
7885 while (byts[n + 1] == 0)
7886 {
7887 ++n;
7888 ++curi[depth];
7889 }
7890 }
7891 else
7892 {
7893 /* Normal char, go one level deeper. */
7894 tword[depth++] = c;
7895 arridx[depth] = idxs[n];
7896 curi[depth] = 1;
7897 }
7898 }
7899 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007900 }
7901 }
7902 }
7903}
7904
7905/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007906 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007907 */
7908 static void
7909make_case_word(fword, cword, flags)
7910 char_u *fword;
7911 char_u *cword;
7912 int flags;
7913{
7914 if (flags & WF_ALLCAP)
7915 /* Make it all upper-case */
7916 allcap_copy(fword, cword);
7917 else if (flags & WF_ONECAP)
7918 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007919 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007920 else
7921 /* Use goodword as-is. */
7922 STRCPY(cword, fword);
7923}
7924
Bram Moolenaarea424162005-06-16 21:51:00 +00007925/*
7926 * Use map string "map" for languages "lp".
7927 */
7928 static void
7929set_map_str(lp, map)
7930 slang_T *lp;
7931 char_u *map;
7932{
7933 char_u *p;
7934 int headc = 0;
7935 int c;
7936 int i;
7937
7938 if (*map == NUL)
7939 {
7940 lp->sl_has_map = FALSE;
7941 return;
7942 }
7943 lp->sl_has_map = TRUE;
7944
7945 /* Init the array and hash table empty. */
7946 for (i = 0; i < 256; ++i)
7947 lp->sl_map_array[i] = 0;
7948#ifdef FEAT_MBYTE
7949 hash_init(&lp->sl_map_hash);
7950#endif
7951
7952 /*
7953 * The similar characters are stored separated with slashes:
7954 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
7955 * before the same slash. For characters above 255 sl_map_hash is used.
7956 */
7957 for (p = map; *p != NUL; )
7958 {
7959#ifdef FEAT_MBYTE
7960 c = mb_ptr2char_adv(&p);
7961#else
7962 c = *p++;
7963#endif
7964 if (c == '/')
7965 headc = 0;
7966 else
7967 {
7968 if (headc == 0)
7969 headc = c;
7970
7971#ifdef FEAT_MBYTE
7972 /* Characters above 255 don't fit in sl_map_array[], put them in
7973 * the hash table. Each entry is the char, a NUL the headchar and
7974 * a NUL. */
7975 if (c >= 256)
7976 {
7977 int cl = mb_char2len(c);
7978 int headcl = mb_char2len(headc);
7979 char_u *b;
7980 hash_T hash;
7981 hashitem_T *hi;
7982
7983 b = alloc((unsigned)(cl + headcl + 2));
7984 if (b == NULL)
7985 return;
7986 mb_char2bytes(c, b);
7987 b[cl] = NUL;
7988 mb_char2bytes(headc, b + cl + 1);
7989 b[cl + 1 + headcl] = NUL;
7990 hash = hash_hash(b);
7991 hi = hash_lookup(&lp->sl_map_hash, b, hash);
7992 if (HASHITEM_EMPTY(hi))
7993 hash_add_item(&lp->sl_map_hash, hi, b, hash);
7994 else
7995 {
7996 /* This should have been checked when generating the .spl
7997 * file. */
7998 EMSG(_("E999: duplicate char in MAP entry"));
7999 vim_free(b);
8000 }
8001 }
8002 else
8003#endif
8004 lp->sl_map_array[c] = headc;
8005 }
8006 }
8007}
8008
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008009/*
8010 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
8011 * lines in the .aff file.
8012 */
8013 static int
8014similar_chars(slang, c1, c2)
8015 slang_T *slang;
8016 int c1;
8017 int c2;
8018{
Bram Moolenaarea424162005-06-16 21:51:00 +00008019 int m1, m2;
8020#ifdef FEAT_MBYTE
8021 char_u buf[MB_MAXBYTES];
8022 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008023
Bram Moolenaarea424162005-06-16 21:51:00 +00008024 if (c1 >= 256)
8025 {
8026 buf[mb_char2bytes(c1, buf)] = 0;
8027 hi = hash_find(&slang->sl_map_hash, buf);
8028 if (HASHITEM_EMPTY(hi))
8029 m1 = 0;
8030 else
8031 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8032 }
8033 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008034#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00008035 m1 = slang->sl_map_array[c1];
8036 if (m1 == 0)
8037 return FALSE;
8038
8039
8040#ifdef FEAT_MBYTE
8041 if (c2 >= 256)
8042 {
8043 buf[mb_char2bytes(c2, buf)] = 0;
8044 hi = hash_find(&slang->sl_map_hash, buf);
8045 if (HASHITEM_EMPTY(hi))
8046 m2 = 0;
8047 else
8048 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8049 }
8050 else
8051#endif
8052 m2 = slang->sl_map_array[c2];
8053
8054 return m1 == m2;
8055}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008056
8057/*
8058 * Add a suggestion to the list of suggestions.
8059 * Do not add a duplicate suggestion or suggestions with a bad score.
8060 * When "use_score" is not zero it's used, otherwise the score is computed
8061 * with spell_edit_score().
8062 */
8063 static void
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008064add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008065 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008066 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008067 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008068 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008069 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008070 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008071 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008072{
8073 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008074 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008075 char_u *p = NULL;
8076 int c = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008077
8078 /* Check that the word wasn't banned. */
8079 if (was_banned(su, goodword))
8080 return;
8081
Bram Moolenaar0c405862005-06-22 22:26:26 +00008082 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
8083 * Remove the common part from "goodword". */
8084 i = badlen - su->su_badlen;
8085 if (i > 0)
8086 {
8087 /* This assumes there was no case folding or it didn't change the
8088 * length... */
8089 p = goodword + STRLEN(goodword) - i;
8090 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
8091 {
8092 badlen = su->su_badlen;
8093 c = *p;
8094 *p = NUL;
8095 }
8096 else
8097 p = NULL;
8098 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008099 else if (i < 0)
8100 {
8101 /* When replacing part of the word check that we actually change
8102 * something. For "the the" a suggestion can be replacing the first
8103 * "the" with itself, since "the" wasn't banned. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008104 if (badlen == (int)STRLEN(goodword)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008105 && STRNCMP(su->su_badword, goodword, badlen) == 0)
8106 return;
8107 }
8108
Bram Moolenaar0c405862005-06-22 22:26:26 +00008109
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008110 if (score <= su->su_maxscore)
8111 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008112 /* Check if the word is already there. Also check the length that is
8113 * being replaced "thes," -> "these" is a different suggestion from
8114 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008115 stp = &SUG(*gap, 0);
8116 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008117 if (STRCMP(stp[i].st_word, goodword) == 0
8118 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008119 {
8120 /* Found it. Remember the lowest score. */
8121 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008122 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008123 stp[i].st_score = score;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008124 stp[i].st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008125 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008126 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008127 break;
8128 }
8129
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008130 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008131 {
8132 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008133 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008134 stp->st_word = vim_strsave(goodword);
8135 if (stp->st_word != NULL)
8136 {
8137 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008138 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008139 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008140 stp->st_orglen = badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008141 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008142
8143 /* If we have too many suggestions now, sort the list and keep
8144 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008145 if (gap->ga_len > SUG_MAX_COUNT(su))
8146 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
8147 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008148 }
8149 }
8150 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008151
8152 if (p != NULL)
8153 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008154}
8155
8156/*
8157 * Add a word to be banned.
8158 */
8159 static void
8160add_banned(su, word)
8161 suginfo_T *su;
8162 char_u *word;
8163{
8164 char_u *s = vim_strsave(word);
8165 hash_T hash;
8166 hashitem_T *hi;
8167
8168 if (s != NULL)
8169 {
8170 hash = hash_hash(s);
8171 hi = hash_lookup(&su->su_banned, s, hash);
8172 if (HASHITEM_EMPTY(hi))
8173 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008174 else
8175 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008176 }
8177}
8178
8179/*
8180 * Return TRUE if a word appears in the list of banned words.
8181 */
8182 static int
8183was_banned(su, word)
8184 suginfo_T *su;
8185 char_u *word;
8186{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008187 hashitem_T *hi = hash_find(&su->su_banned, word);
8188
8189 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008190}
8191
8192/*
8193 * Free the banned words in "su".
8194 */
8195 static void
8196free_banned(su)
8197 suginfo_T *su;
8198{
8199 int todo;
8200 hashitem_T *hi;
8201
8202 todo = su->su_banned.ht_used;
8203 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
8204 {
8205 if (!HASHITEM_EMPTY(hi))
8206 {
8207 vim_free(hi->hi_key);
8208 --todo;
8209 }
8210 }
8211 hash_clear(&su->su_banned);
8212}
8213
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008214/*
8215 * Recompute the score if sound-folding is possible. This is slow,
8216 * thus only done for the final results.
8217 */
8218 static void
8219rescore_suggestions(su)
8220 suginfo_T *su;
8221{
8222 langp_T *lp;
8223 suggest_T *stp;
8224 char_u sal_badword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008225 int i;
8226
8227 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8228 lp->lp_slang != NULL; ++lp)
8229 {
8230 if (lp->lp_slang->sl_sal.ga_len > 0)
8231 {
8232 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008233 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008234
8235 for (i = 0; i < su->su_ga.ga_len; ++i)
8236 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008237 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008238 if (!stp->st_had_bonus)
8239 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008240 stp->st_altscore = stp_sal_score(stp, su,
8241 lp->lp_slang, sal_badword);
8242 if (stp->st_altscore == SCORE_MAXMAX)
8243 stp->st_altscore = SCORE_BIG;
8244 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008245 }
8246 }
8247 break;
8248 }
8249 }
8250}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008251
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008252static int
8253#ifdef __BORLANDC__
8254_RTLENTRYF
8255#endif
8256sug_compare __ARGS((const void *s1, const void *s2));
8257
8258/*
8259 * Function given to qsort() to sort the suggestions on st_score.
8260 */
8261 static int
8262#ifdef __BORLANDC__
8263_RTLENTRYF
8264#endif
8265sug_compare(s1, s2)
8266 const void *s1;
8267 const void *s2;
8268{
8269 suggest_T *p1 = (suggest_T *)s1;
8270 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008271 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008272
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008273 if (n == 0)
8274 return p1->st_altscore - p2->st_altscore;
8275 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008276}
8277
8278/*
8279 * Cleanup the suggestions:
8280 * - Sort on score.
8281 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008282 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008283 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008284 static int
8285cleanup_suggestions(gap, maxscore, keep)
8286 garray_T *gap;
8287 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008288 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008289{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008290 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008291 int i;
8292
8293 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008294 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008295
8296 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008297 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008298 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008299 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008300 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008301 gap->ga_len = keep;
8302 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008303 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008304 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008305}
8306
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008307#if defined(FEAT_EVAL) || defined(PROTO)
8308/*
8309 * Soundfold a string, for soundfold().
8310 * Result is in allocated memory, NULL for an error.
8311 */
8312 char_u *
8313eval_soundfold(word)
8314 char_u *word;
8315{
8316 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008317 char_u sound[MAXWLEN];
8318
8319 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
8320 /* Use the sound-folding of the first language that supports it. */
8321 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8322 lp->lp_slang != NULL; ++lp)
8323 if (lp->lp_slang->sl_sal.ga_len > 0)
8324 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008325 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008326 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008327 return vim_strsave(sound);
8328 }
8329
8330 /* No language with sound folding, return word as-is. */
8331 return vim_strsave(word);
8332}
8333#endif
8334
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008335/*
8336 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
8337 */
8338 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008339spell_soundfold(slang, inword, folded, res)
8340 slang_T *slang;
8341 char_u *inword;
8342 int folded; /* "inword" is already case-folded */
8343 char_u *res;
8344{
8345 char_u fword[MAXWLEN];
8346 char_u *word;
8347
8348 if (slang->sl_sofo)
8349 /* SOFOFROM and SOFOTO used */
8350 spell_soundfold_sofo(slang, inword, res);
8351 else
8352 {
8353 /* SAL items used. Requires the word to be case-folded. */
8354 if (folded)
8355 word = inword;
8356 else
8357 {
8358 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
8359 word = fword;
8360 }
8361
8362#ifdef FEAT_MBYTE
8363 if (has_mbyte)
8364 spell_soundfold_wsal(slang, word, res);
8365 else
8366#endif
8367 spell_soundfold_sal(slang, word, res);
8368 }
8369}
8370
8371/*
8372 * Perform sound folding of "inword" into "res" according to SOFOFROM and
8373 * SOFOTO lines.
8374 */
8375 static void
8376spell_soundfold_sofo(slang, inword, res)
8377 slang_T *slang;
8378 char_u *inword;
8379 char_u *res;
8380{
8381 char_u *s;
8382 int ri = 0;
8383 int c;
8384
8385#ifdef FEAT_MBYTE
8386 if (has_mbyte)
8387 {
8388 int prevc = 0;
8389 int *ip;
8390
8391 /* The sl_sal_first[] table contains the translation for chars up to
8392 * 255, sl_sal the rest. */
8393 for (s = inword; *s != NUL; )
8394 {
8395 c = mb_ptr2char_adv(&s);
8396 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
8397 c = ' ';
8398 else if (c < 256)
8399 c = slang->sl_sal_first[c];
8400 else
8401 {
8402 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
8403 if (ip == NULL) /* empty list, can't match */
8404 c = NUL;
8405 else
8406 for (;;) /* find "c" in the list */
8407 {
8408 if (*ip == 0) /* not found */
8409 {
8410 c = NUL;
8411 break;
8412 }
8413 if (*ip == c) /* match! */
8414 {
8415 c = ip[1];
8416 break;
8417 }
8418 ip += 2;
8419 }
8420 }
8421
8422 if (c != NUL && c != prevc)
8423 {
8424 ri += mb_char2bytes(c, res + ri);
8425 if (ri + MB_MAXBYTES > MAXWLEN)
8426 break;
8427 prevc = c;
8428 }
8429 }
8430 }
8431 else
8432#endif
8433 {
8434 /* The sl_sal_first[] table contains the translation. */
8435 for (s = inword; (c = *s) != NUL; ++s)
8436 {
8437 if (vim_iswhite(c))
8438 c = ' ';
8439 else
8440 c = slang->sl_sal_first[c];
8441 if (c != NUL && (ri == 0 || res[ri - 1] != c))
8442 res[ri++] = c;
8443 }
8444 }
8445
8446 res[ri] = NUL;
8447}
8448
8449 static void
8450spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008451 slang_T *slang;
8452 char_u *inword;
8453 char_u *res;
8454{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008455 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008456 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008457 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008458 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008459 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008460 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008461 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008462 int n, k = 0;
8463 int z0;
8464 int k0;
8465 int n0;
8466 int c;
8467 int pri;
8468 int p0 = -333;
8469 int c0;
8470
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008471 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008472 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008473 if (slang->sl_rem_accents)
8474 {
8475 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008476 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008477 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008478 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008479 {
8480 *t++ = ' ';
8481 s = skipwhite(s);
8482 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008483 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008484 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008485 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008486 *t++ = *s;
8487 ++s;
8488 }
8489 }
8490 *t = NUL;
8491 }
8492 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008493 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008494
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008495 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008496
8497 /*
8498 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008499 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008500 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008501 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008502 while ((c = word[i]) != NUL)
8503 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008504 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008505 n = slang->sl_sal_first[c];
8506 z0 = 0;
8507
8508 if (n >= 0)
8509 {
8510 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008511 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008512 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008513 /* Quickly skip entries that don't match the word. Most
8514 * entries are less then three chars, optimize for that. */
8515 k = smp[n].sm_leadlen;
8516 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008517 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008518 if (word[i + 1] != s[1])
8519 continue;
8520 if (k > 2)
8521 {
8522 for (j = 2; j < k; ++j)
8523 if (word[i + j] != s[j])
8524 break;
8525 if (j < k)
8526 continue;
8527 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008528 }
8529
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008530 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008531 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008532 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008533 while (*pf != NUL && *pf != word[i + k])
8534 ++pf;
8535 if (*pf == NUL)
8536 continue;
8537 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008538 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008539 s = smp[n].sm_rules;
8540 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008541
8542 p0 = *s;
8543 k0 = k;
8544 while (*s == '-' && k > 1)
8545 {
8546 k--;
8547 s++;
8548 }
8549 if (*s == '<')
8550 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008551 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008552 {
8553 /* determine priority */
8554 pri = *s - '0';
8555 s++;
8556 }
8557 if (*s == '^' && *(s + 1) == '^')
8558 s++;
8559
8560 if (*s == NUL
8561 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008562 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008563 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008564 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008565 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008566 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008567 && spell_iswordp(word + i - 1, curbuf)
8568 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008569 {
8570 /* search for followup rules, if: */
8571 /* followup and k > 1 and NO '-' in searchstring */
8572 c0 = word[i + k - 1];
8573 n0 = slang->sl_sal_first[c0];
8574
8575 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008576 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008577 {
8578 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008579 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008580 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008581 /* Quickly skip entries that don't match the word.
8582 * */
8583 k0 = smp[n0].sm_leadlen;
8584 if (k0 > 1)
8585 {
8586 if (word[i + k] != s[1])
8587 continue;
8588 if (k0 > 2)
8589 {
8590 pf = word + i + k + 1;
8591 for (j = 2; j < k0; ++j)
8592 if (*pf++ != s[j])
8593 break;
8594 if (j < k0)
8595 continue;
8596 }
8597 }
8598 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008599
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008600 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008601 {
8602 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008603 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008604 while (*pf != NUL && *pf != word[i + k0])
8605 ++pf;
8606 if (*pf == NUL)
8607 continue;
8608 ++k0;
8609 }
8610
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008611 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008612 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008613 while (*s == '-')
8614 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008615 /* "k0" gets NOT reduced because
8616 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008617 s++;
8618 }
8619 if (*s == '<')
8620 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008621 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008622 {
8623 p0 = *s - '0';
8624 s++;
8625 }
8626
8627 if (*s == NUL
8628 /* *s == '^' cuts */
8629 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008630 && !spell_iswordp(word + i + k0,
8631 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008632 {
8633 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008634 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008635 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008636
8637 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008638 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008639 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008640 /* rule fits; stop search */
8641 break;
8642 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008643 }
8644
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008645 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008646 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008647 }
8648
8649 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008650 s = smp[n].sm_to;
8651 pf = smp[n].sm_rules;
8652 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008653 if (p0 == 1 && z == 0)
8654 {
8655 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008656 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
8657 || res[reslen - 1] == *s))
8658 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008659 z0 = 1;
8660 z = 1;
8661 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008662 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008663 {
8664 word[i + k0] = *s;
8665 k0++;
8666 s++;
8667 }
8668 if (k > k0)
8669 mch_memmove(word + i + k0, word + i + k,
8670 STRLEN(word + i + k) + 1);
8671
8672 /* new "actual letter" */
8673 c = word[i];
8674 }
8675 else
8676 {
8677 /* no '<' rule used */
8678 i += k - 1;
8679 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008680 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008681 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008682 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008683 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008684 s++;
8685 }
8686 /* new "actual letter" */
8687 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008688 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008689 {
8690 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008691 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008692 mch_memmove(word, word + i + 1,
8693 STRLEN(word + i + 1) + 1);
8694 i = 0;
8695 z0 = 1;
8696 }
8697 }
8698 break;
8699 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008700 }
8701 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008702 else if (vim_iswhite(c))
8703 {
8704 c = ' ';
8705 k = 1;
8706 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008707
8708 if (z0 == 0)
8709 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008710 if (k && !p0 && reslen < MAXWLEN && c != NUL
8711 && (!slang->sl_collapse || reslen == 0
8712 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008713 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008714 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008715
8716 i++;
8717 z = 0;
8718 k = 0;
8719 }
8720 }
8721
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008722 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008723}
8724
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008725#ifdef FEAT_MBYTE
8726/*
8727 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
8728 * Multi-byte version of spell_soundfold().
8729 */
8730 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008731spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008732 slang_T *slang;
8733 char_u *inword;
8734 char_u *res;
8735{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008736 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008737 int word[MAXWLEN];
8738 int wres[MAXWLEN];
8739 int l;
8740 char_u *s;
8741 int *ws;
8742 char_u *t;
8743 int *pf;
8744 int i, j, z;
8745 int reslen;
8746 int n, k = 0;
8747 int z0;
8748 int k0;
8749 int n0;
8750 int c;
8751 int pri;
8752 int p0 = -333;
8753 int c0;
8754 int did_white = FALSE;
8755
8756 /*
8757 * Convert the multi-byte string to a wide-character string.
8758 * Remove accents, if wanted. We actually remove all non-word characters.
8759 * But keep white space.
8760 */
8761 n = 0;
8762 for (s = inword; *s != NUL; )
8763 {
8764 t = s;
8765 c = mb_ptr2char_adv(&s);
8766 if (slang->sl_rem_accents)
8767 {
8768 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
8769 {
8770 if (did_white)
8771 continue;
8772 c = ' ';
8773 did_white = TRUE;
8774 }
8775 else
8776 {
8777 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008778 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008779 continue;
8780 }
8781 }
8782 word[n++] = c;
8783 }
8784 word[n] = NUL;
8785
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008786 /*
8787 * This comes from Aspell phonet.cpp.
8788 * Converted from C++ to C. Added support for multi-byte chars.
8789 * Changed to keep spaces.
8790 */
8791 i = reslen = z = 0;
8792 while ((c = word[i]) != NUL)
8793 {
8794 /* Start with the first rule that has the character in the word. */
8795 n = slang->sl_sal_first[c & 0xff];
8796 z0 = 0;
8797
8798 if (n >= 0)
8799 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008800 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008801 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
8802 {
8803 /* Quickly skip entries that don't match the word. Most
8804 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008805 if (c != ws[0])
8806 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008807 k = smp[n].sm_leadlen;
8808 if (k > 1)
8809 {
8810 if (word[i + 1] != ws[1])
8811 continue;
8812 if (k > 2)
8813 {
8814 for (j = 2; j < k; ++j)
8815 if (word[i + j] != ws[j])
8816 break;
8817 if (j < k)
8818 continue;
8819 }
8820 }
8821
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008822 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008823 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008824 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008825 while (*pf != NUL && *pf != word[i + k])
8826 ++pf;
8827 if (*pf == NUL)
8828 continue;
8829 ++k;
8830 }
8831 s = smp[n].sm_rules;
8832 pri = 5; /* default priority */
8833
8834 p0 = *s;
8835 k0 = k;
8836 while (*s == '-' && k > 1)
8837 {
8838 k--;
8839 s++;
8840 }
8841 if (*s == '<')
8842 s++;
8843 if (VIM_ISDIGIT(*s))
8844 {
8845 /* determine priority */
8846 pri = *s - '0';
8847 s++;
8848 }
8849 if (*s == '^' && *(s + 1) == '^')
8850 s++;
8851
8852 if (*s == NUL
8853 || (*s == '^'
8854 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008855 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008856 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008857 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008858 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008859 && spell_iswordp_w(word + i - 1, curbuf)
8860 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008861 {
8862 /* search for followup rules, if: */
8863 /* followup and k > 1 and NO '-' in searchstring */
8864 c0 = word[i + k - 1];
8865 n0 = slang->sl_sal_first[c0 & 0xff];
8866
8867 if (slang->sl_followup && k > 1 && n0 >= 0
8868 && p0 != '-' && word[i + k] != NUL)
8869 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008870 /* Test follow-up rule for "word[i + k]"; loop over
8871 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008872 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
8873 == (c0 & 0xff); ++n0)
8874 {
8875 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008876 */
8877 if (c0 != ws[0])
8878 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008879 k0 = smp[n0].sm_leadlen;
8880 if (k0 > 1)
8881 {
8882 if (word[i + k] != ws[1])
8883 continue;
8884 if (k0 > 2)
8885 {
8886 pf = word + i + k + 1;
8887 for (j = 2; j < k0; ++j)
8888 if (*pf++ != ws[j])
8889 break;
8890 if (j < k0)
8891 continue;
8892 }
8893 }
8894 k0 += k - 1;
8895
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008896 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008897 {
8898 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008899 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008900 while (*pf != NUL && *pf != word[i + k0])
8901 ++pf;
8902 if (*pf == NUL)
8903 continue;
8904 ++k0;
8905 }
8906
8907 p0 = 5;
8908 s = smp[n0].sm_rules;
8909 while (*s == '-')
8910 {
8911 /* "k0" gets NOT reduced because
8912 * "if (k0 == k)" */
8913 s++;
8914 }
8915 if (*s == '<')
8916 s++;
8917 if (VIM_ISDIGIT(*s))
8918 {
8919 p0 = *s - '0';
8920 s++;
8921 }
8922
8923 if (*s == NUL
8924 /* *s == '^' cuts */
8925 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008926 && !spell_iswordp_w(word + i + k0,
8927 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008928 {
8929 if (k0 == k)
8930 /* this is just a piece of the string */
8931 continue;
8932
8933 if (p0 < pri)
8934 /* priority too low */
8935 continue;
8936 /* rule fits; stop search */
8937 break;
8938 }
8939 }
8940
8941 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
8942 == (c0 & 0xff))
8943 continue;
8944 }
8945
8946 /* replace string */
8947 ws = smp[n].sm_to_w;
8948 s = smp[n].sm_rules;
8949 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
8950 if (p0 == 1 && z == 0)
8951 {
8952 /* rule with '<' is used */
8953 if (reslen > 0 && *ws != NUL && (wres[reslen - 1] == c
8954 || wres[reslen - 1] == *ws))
8955 reslen--;
8956 z0 = 1;
8957 z = 1;
8958 k0 = 0;
8959 while (*ws != NUL && word[i + k0] != NUL)
8960 {
8961 word[i + k0] = *ws;
8962 k0++;
8963 ws++;
8964 }
8965 if (k > k0)
8966 mch_memmove(word + i + k0, word + i + k,
8967 sizeof(int) * (STRLEN(word + i + k) + 1));
8968
8969 /* new "actual letter" */
8970 c = word[i];
8971 }
8972 else
8973 {
8974 /* no '<' rule used */
8975 i += k - 1;
8976 z = 0;
8977 while (*ws != NUL && ws[1] != NUL && reslen < MAXWLEN)
8978 {
8979 if (reslen == 0 || wres[reslen - 1] != *ws)
8980 wres[reslen++] = *ws;
8981 ws++;
8982 }
8983 /* new "actual letter" */
8984 c = *ws;
8985 if (strstr((char *)s, "^^") != NULL)
8986 {
8987 if (c != NUL)
8988 wres[reslen++] = c;
8989 mch_memmove(word, word + i + 1,
8990 sizeof(int) * (STRLEN(word + i + 1) + 1));
8991 i = 0;
8992 z0 = 1;
8993 }
8994 }
8995 break;
8996 }
8997 }
8998 }
8999 else if (vim_iswhite(c))
9000 {
9001 c = ' ';
9002 k = 1;
9003 }
9004
9005 if (z0 == 0)
9006 {
9007 if (k && !p0 && reslen < MAXWLEN && c != NUL
9008 && (!slang->sl_collapse || reslen == 0
9009 || wres[reslen - 1] != c))
9010 /* condense only double letters */
9011 wres[reslen++] = c;
9012
9013 i++;
9014 z = 0;
9015 k = 0;
9016 }
9017 }
9018
9019 /* Convert wide characters in "wres" to a multi-byte string in "res". */
9020 l = 0;
9021 for (n = 0; n < reslen; ++n)
9022 {
9023 l += mb_char2bytes(wres[n], res + l);
9024 if (l + MB_MAXBYTES > MAXWLEN)
9025 break;
9026 }
9027 res[l] = NUL;
9028}
9029#endif
9030
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009031/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009032 * Compute a score for two sound-a-like words.
9033 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
9034 * Instead of a generic loop we write out the code. That keeps it fast by
9035 * avoiding checks that will not be possible.
9036 */
9037 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009038soundalike_score(goodstart, badstart)
9039 char_u *goodstart; /* sound-folded good word */
9040 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009041{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009042 char_u *goodsound = goodstart;
9043 char_u *badsound = badstart;
9044 int goodlen;
9045 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009046 int n;
9047 char_u *pl, *ps;
9048 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009049 int score = 0;
9050
9051 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
9052 * counted so much, vowels halfway the word aren't counted at all. */
9053 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
9054 {
9055 score = SCORE_DEL / 2;
9056 if (*badsound == '*')
9057 ++badsound;
9058 else
9059 ++goodsound;
9060 }
9061
9062 goodlen = STRLEN(goodsound);
9063 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009064
9065 /* Return quickly if the lenghts are too different to be fixed by two
9066 * changes. */
9067 n = goodlen - badlen;
9068 if (n < -2 || n > 2)
9069 return SCORE_MAXMAX;
9070
9071 if (n > 0)
9072 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009073 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009074 ps = badsound;
9075 }
9076 else
9077 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009078 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009079 ps = goodsound;
9080 }
9081
9082 /* Skip over the identical part. */
9083 while (*pl == *ps && *pl != NUL)
9084 {
9085 ++pl;
9086 ++ps;
9087 }
9088
9089 switch (n)
9090 {
9091 case -2:
9092 case 2:
9093 /*
9094 * Must delete two characters from "pl".
9095 */
9096 ++pl; /* first delete */
9097 while (*pl == *ps)
9098 {
9099 ++pl;
9100 ++ps;
9101 }
9102 /* strings must be equal after second delete */
9103 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009104 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009105
9106 /* Failed to compare. */
9107 break;
9108
9109 case -1:
9110 case 1:
9111 /*
9112 * Minimal one delete from "pl" required.
9113 */
9114
9115 /* 1: delete */
9116 pl2 = pl + 1;
9117 ps2 = ps;
9118 while (*pl2 == *ps2)
9119 {
9120 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009121 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009122 ++pl2;
9123 ++ps2;
9124 }
9125
9126 /* 2: delete then swap, then rest must be equal */
9127 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9128 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009129 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009130
9131 /* 3: delete then substitute, then the rest must be equal */
9132 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009133 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009134
9135 /* 4: first swap then delete */
9136 if (pl[0] == ps[1] && pl[1] == ps[0])
9137 {
9138 pl2 = pl + 2; /* swap, skip two chars */
9139 ps2 = ps + 2;
9140 while (*pl2 == *ps2)
9141 {
9142 ++pl2;
9143 ++ps2;
9144 }
9145 /* delete a char and then strings must be equal */
9146 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009147 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009148 }
9149
9150 /* 5: first substitute then delete */
9151 pl2 = pl + 1; /* substitute, skip one char */
9152 ps2 = ps + 1;
9153 while (*pl2 == *ps2)
9154 {
9155 ++pl2;
9156 ++ps2;
9157 }
9158 /* delete a char and then strings must be equal */
9159 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009160 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009161
9162 /* Failed to compare. */
9163 break;
9164
9165 case 0:
9166 /*
9167 * Lenghts are equal, thus changes must result in same length: An
9168 * insert is only possible in combination with a delete.
9169 * 1: check if for identical strings
9170 */
9171 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009172 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009173
9174 /* 2: swap */
9175 if (pl[0] == ps[1] && pl[1] == ps[0])
9176 {
9177 pl2 = pl + 2; /* swap, skip two chars */
9178 ps2 = ps + 2;
9179 while (*pl2 == *ps2)
9180 {
9181 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009182 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009183 ++pl2;
9184 ++ps2;
9185 }
9186 /* 3: swap and swap again */
9187 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9188 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009189 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009190
9191 /* 4: swap and substitute */
9192 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009193 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009194 }
9195
9196 /* 5: substitute */
9197 pl2 = pl + 1;
9198 ps2 = ps + 1;
9199 while (*pl2 == *ps2)
9200 {
9201 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009202 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009203 ++pl2;
9204 ++ps2;
9205 }
9206
9207 /* 6: substitute and swap */
9208 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9209 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009210 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009211
9212 /* 7: substitute and substitute */
9213 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009214 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009215
9216 /* 8: insert then delete */
9217 pl2 = pl;
9218 ps2 = ps + 1;
9219 while (*pl2 == *ps2)
9220 {
9221 ++pl2;
9222 ++ps2;
9223 }
9224 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009225 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009226
9227 /* 9: delete then insert */
9228 pl2 = pl + 1;
9229 ps2 = ps;
9230 while (*pl2 == *ps2)
9231 {
9232 ++pl2;
9233 ++ps2;
9234 }
9235 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009236 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009237
9238 /* Failed to compare. */
9239 break;
9240 }
9241
9242 return SCORE_MAXMAX;
9243}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009244
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009245/*
9246 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009247 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009248 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009249 * The algorithm comes from Aspell editdist.cpp, edit_distance().
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009250 * It has been converted from C++ to C and modified to support multi-byte
9251 * characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009252 */
9253 static int
9254spell_edit_score(badword, goodword)
9255 char_u *badword;
9256 char_u *goodword;
9257{
9258 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009259 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009260 int j, i;
9261 int t;
9262 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009263 int pbc, pgc;
9264#ifdef FEAT_MBYTE
9265 char_u *p;
9266 int wbadword[MAXWLEN];
9267 int wgoodword[MAXWLEN];
9268
9269 if (has_mbyte)
9270 {
9271 /* Get the characters from the multi-byte strings and put them in an
9272 * int array for easy access. */
9273 for (p = badword, badlen = 0; *p != NUL; )
9274 wbadword[badlen++] = mb_ptr2char_adv(&p);
9275 ++badlen;
9276 for (p = goodword, goodlen = 0; *p != NUL; )
9277 wgoodword[goodlen++] = mb_ptr2char_adv(&p);
9278 ++goodlen;
9279 }
9280 else
9281#endif
9282 {
9283 badlen = STRLEN(badword) + 1;
9284 goodlen = STRLEN(goodword) + 1;
9285 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009286
9287 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
9288#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009289 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
9290 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009291 if (cnt == NULL)
9292 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009293
9294 CNT(0, 0) = 0;
9295 for (j = 1; j <= goodlen; ++j)
9296 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
9297
9298 for (i = 1; i <= badlen; ++i)
9299 {
9300 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
9301 for (j = 1; j <= goodlen; ++j)
9302 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009303#ifdef FEAT_MBYTE
9304 if (has_mbyte)
9305 {
9306 bc = wbadword[i - 1];
9307 gc = wgoodword[j - 1];
9308 }
9309 else
9310#endif
9311 {
9312 bc = badword[i - 1];
9313 gc = goodword[j - 1];
9314 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009315 if (bc == gc)
9316 CNT(i, j) = CNT(i - 1, j - 1);
9317 else
9318 {
9319 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009320 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009321 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
9322 else
9323 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
9324
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009325 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009326 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009327#ifdef FEAT_MBYTE
9328 if (has_mbyte)
9329 {
9330 pbc = wbadword[i - 2];
9331 pgc = wgoodword[j - 2];
9332 }
9333 else
9334#endif
9335 {
9336 pbc = badword[i - 2];
9337 pgc = goodword[j - 2];
9338 }
9339 if (bc == pgc && pbc == gc)
9340 {
9341 t = SCORE_SWAP + CNT(i - 2, j - 2);
9342 if (t < CNT(i, j))
9343 CNT(i, j) = t;
9344 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009345 }
9346 t = SCORE_DEL + CNT(i - 1, j);
9347 if (t < CNT(i, j))
9348 CNT(i, j) = t;
9349 t = SCORE_INS + CNT(i, j - 1);
9350 if (t < CNT(i, j))
9351 CNT(i, j) = t;
9352 }
9353 }
9354 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009355
9356 i = CNT(badlen - 1, goodlen - 1);
9357 vim_free(cnt);
9358 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009359}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009360
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009361/*
9362 * ":spelldump"
9363 */
9364/*ARGSUSED*/
9365 void
9366ex_spelldump(eap)
9367 exarg_T *eap;
9368{
9369 buf_T *buf = curbuf;
9370 langp_T *lp;
9371 slang_T *slang;
9372 idx_T arridx[MAXWLEN];
9373 int curi[MAXWLEN];
9374 char_u word[MAXWLEN];
9375 int c;
9376 char_u *byts;
9377 idx_T *idxs;
9378 linenr_T lnum = 0;
9379 int round;
9380 int depth;
9381 int n;
9382 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009383 char_u *region_names = NULL; /* region names being used */
9384 int do_region = TRUE; /* dump region names and numbers */
9385 char_u *p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009386
9387 if (no_spell_checking())
9388 return;
9389
9390 /* Create a new empty buffer by splitting the window. */
9391 do_cmdline_cmd((char_u *)"new");
9392 if (!bufempty() || !buf_valid(buf))
9393 return;
9394
Bram Moolenaar7887d882005-07-01 22:33:52 +00009395 /* Find out if we can support regions: All languages must support the same
9396 * regions or none at all. */
9397 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
9398 {
9399 p = lp->lp_slang->sl_regions;
9400 if (p[0] != 0)
9401 {
9402 if (region_names == NULL) /* first language with regions */
9403 region_names = p;
9404 else if (STRCMP(region_names, p) != 0)
9405 {
9406 do_region = FALSE; /* region names are different */
9407 break;
9408 }
9409 }
9410 }
9411
9412 if (do_region && region_names != NULL)
9413 {
9414 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
9415 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
9416 }
9417 else
9418 do_region = FALSE;
9419
9420 /*
9421 * Loop over all files loaded for the entries in 'spelllang'.
9422 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009423 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
9424 {
9425 slang = lp->lp_slang;
9426
9427 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
9428 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
9429
9430 /* round 1: case-folded tree
9431 * round 2: keep-case tree */
9432 for (round = 1; round <= 2; ++round)
9433 {
9434 if (round == 1)
9435 {
9436 byts = slang->sl_fbyts;
9437 idxs = slang->sl_fidxs;
9438 }
9439 else
9440 {
9441 byts = slang->sl_kbyts;
9442 idxs = slang->sl_kidxs;
9443 }
9444 if (byts == NULL)
9445 continue; /* array is empty */
9446
9447 depth = 0;
9448 arridx[0] = 0;
9449 curi[0] = 1;
9450 while (depth >= 0 && !got_int)
9451 {
9452 if (curi[depth] > byts[arridx[depth]])
9453 {
9454 /* Done all bytes at this node, go up one level. */
9455 --depth;
9456 line_breakcheck();
9457 }
9458 else
9459 {
9460 /* Do one more byte at this node. */
9461 n = arridx[depth] + curi[depth];
9462 ++curi[depth];
9463 c = byts[n];
9464 if (c == 0)
9465 {
9466 /* End of word, deal with the word.
9467 * Don't use keep-case words in the fold-case tree,
9468 * they will appear in the keep-case tree.
9469 * Only use the word when the region matches. */
9470 flags = (int)idxs[n];
9471 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009472 && (do_region
9473 || (flags & WF_REGION) == 0
9474 || (((unsigned)flags >> 8)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009475 & lp->lp_region) != 0))
9476 {
9477 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009478 if (!do_region)
9479 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009480
9481 /* Dump the basic word if there is no prefix or
9482 * when it's the first one. */
9483 c = (unsigned)flags >> 16;
9484 if (c == 0 || curi[depth] == 2)
9485 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009486
9487 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009488 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009489 lnum = apply_prefixes(slang, word, round,
9490 flags, lnum);
9491 }
9492 }
9493 else
9494 {
9495 /* Normal char, go one level deeper. */
9496 word[depth++] = c;
9497 arridx[depth] = idxs[n];
9498 curi[depth] = 1;
9499 }
9500 }
9501 }
9502 }
9503 }
9504
9505 /* Delete the empty line that we started with. */
9506 if (curbuf->b_ml.ml_line_count > 1)
9507 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
9508
9509 redraw_later(NOT_VALID);
9510}
9511
9512/*
9513 * Dump one word: apply case modifications and append a line to the buffer.
9514 */
9515 static void
9516dump_word(word, round, flags, lnum)
9517 char_u *word;
9518 int round;
9519 int flags;
9520 linenr_T lnum;
9521{
9522 int keepcap = FALSE;
9523 char_u *p;
9524 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +00009525 char_u badword[MAXWLEN + 10];
9526 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009527
9528 if (round == 1 && (flags & WF_CAPMASK) != 0)
9529 {
9530 /* Need to fix case according to "flags". */
9531 make_case_word(word, cword, flags);
9532 p = cword;
9533 }
9534 else
9535 {
9536 p = word;
9537 if (round == 2 && (captype(word, NULL) & WF_KEEPCAP) == 0)
9538 keepcap = TRUE;
9539 }
9540
Bram Moolenaar7887d882005-07-01 22:33:52 +00009541 /* Add flags and regions after a slash. */
9542 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009543 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009544 STRCPY(badword, p);
9545 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009546 if (keepcap)
9547 STRCAT(badword, "=");
9548 if (flags & WF_BANNED)
9549 STRCAT(badword, "!");
9550 else if (flags & WF_RARE)
9551 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009552 if (flags & WF_REGION)
9553 for (i = 0; i < 7; ++i)
9554 if (flags & (0x100 << i))
9555 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009556 p = badword;
9557 }
9558
9559 ml_append(lnum, p, (colnr_T)0, FALSE);
9560}
9561
9562/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009563 * For ":spelldump": Find matching prefixes for "word". Prepend each to
9564 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009565 * Return the updated line number.
9566 */
9567 static linenr_T
9568apply_prefixes(slang, word, round, flags, startlnum)
9569 slang_T *slang;
9570 char_u *word; /* case-folded word */
9571 int round;
9572 int flags; /* flags with prefix ID */
9573 linenr_T startlnum;
9574{
9575 idx_T arridx[MAXWLEN];
9576 int curi[MAXWLEN];
9577 char_u prefix[MAXWLEN];
9578 int c;
9579 char_u *byts;
9580 idx_T *idxs;
9581 linenr_T lnum = startlnum;
9582 int depth;
9583 int n;
9584 int len;
9585 int prefid = (unsigned)flags >> 16;
9586 int i;
9587
9588 byts = slang->sl_pbyts;
9589 idxs = slang->sl_pidxs;
9590 if (byts != NULL) /* array not is empty */
9591 {
9592 /*
9593 * Loop over all prefixes, building them byte-by-byte in prefix[].
9594 * When at the end of a prefix check that it supports "prefid".
9595 */
9596 depth = 0;
9597 arridx[0] = 0;
9598 curi[0] = 1;
9599 while (depth >= 0 && !got_int)
9600 {
9601 len = arridx[depth];
9602 if (curi[depth] > byts[len])
9603 {
9604 /* Done all bytes at this node, go up one level. */
9605 --depth;
9606 line_breakcheck();
9607 }
9608 else
9609 {
9610 /* Do one more byte at this node. */
9611 n = len + curi[depth];
9612 ++curi[depth];
9613 c = byts[n];
9614 if (c == 0)
9615 {
9616 /* End of prefix, find out how many IDs there are. */
9617 for (i = 1; i < len; ++i)
9618 if (byts[n + i] != 0)
9619 break;
9620 curi[depth] += i - 1;
9621
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009622 i = valid_word_prefix(i, n, prefid, word, slang);
9623 if (i != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009624 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009625 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009626 dump_word(prefix, round,
9627 (i & WF_RAREPFX) ? (flags | WF_RARE)
9628 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009629 }
9630 }
9631 else
9632 {
9633 /* Normal char, go one level deeper. */
9634 prefix[depth++] = c;
9635 arridx[depth] = idxs[n];
9636 curi[depth] = 1;
9637 }
9638 }
9639 }
9640 }
9641
9642 return lnum;
9643}
9644
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009645#endif /* FEAT_SYN_HL */