blob: 6251b93538051c6f61dc283239270f488bc86268 [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>
89 * <prefcondcnt> <prefcond> ...
Bram Moolenaar51485f02005-06-04 21:55:20 +000090 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000091 * <fileID> 10 bytes "VIMspell07"
Bram Moolenaar51485f02005-06-04 21:55:20 +000092 * <regioncnt> 1 byte number of regions following (8 supported)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000093 * <regionname> 2 bytes Region name: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +000094 * First <regionname> is region 1.
95 *
96 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
97 * <charflags> N bytes List of flags (first one is for character 128):
Bram Moolenaar9f30f502005-06-14 22:01:04 +000098 * 0x01 word character CF_WORD
99 * 0x02 upper-case character CF_UPPER
Bram Moolenaar51485f02005-06-04 21:55:20 +0000100 * <fcharslen> 2 bytes Number of bytes in <fchars>.
101 * <fchars> N bytes Folded characters, first one is for character 128.
102 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000103 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
104 *
105 * <prefcond> : <condlen> <condstr>
106 *
107 * <condlen> 1 byte Length of <condstr>.
108 *
109 * <condstr> N bytes Condition for the prefix.
110 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000111 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000112 * <SUGGEST> : <repcount> <rep> ...
113 * <salflags> <salcount> <sal> ...
114 * <maplen> <mapstr>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000115 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000116 * <repcount> 2 bytes number of <rep> items, MSB first.
117 *
118 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
119 *
120 * <repfromlen> 1 byte length of <repfrom>
121 *
122 * <repfrom> N bytes "from" part of replacement
123 *
124 * <reptolen> 1 byte length of <repto>
125 *
126 * <repto> N bytes "to" part of replacement
127 *
128 * <salflags> 1 byte flags for soundsalike conversion:
129 * SAL_F0LLOWUP
130 * SAL_COLLAPSE
131 * SAL_REM_ACCENTS
132 *
133 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
134 *
135 * <salfromlen> 1 byte length of <salfrom>
136 *
137 * <salfrom> N bytes "from" part of soundsalike
138 *
139 * <saltolen> 1 byte length of <salto>
140 *
141 * <salto> N bytes "to" part of soundsalike
142 *
143 * <maplen> 2 bytes length of <mapstr>, MSB first
144 *
145 * <mapstr> N bytes String with sequences of similar characters,
146 * separated by slashes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000147 *
148 *
149 * <LWORDTREE>: <wordtree>
150 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000151 * <KWORDTREE>: <wordtree>
152 *
153 * <PREFIXTREE>: <wordtree>
154 *
155 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000156 * <wordtree>: <nodecount> <nodedata> ...
157 *
158 * <nodecount> 4 bytes Number of nodes following. MSB first.
159 *
160 * <nodedata>: <siblingcount> <sibling> ...
161 *
162 * <siblingcount> 1 byte Number of siblings in this node. The siblings
163 * follow in sorted order.
164 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000165 * <sibling>: <byte> [ <nodeidx> <xbyte>
166 * | <flags> [<region>] [<prefixID>]
167 * | <prefixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000168 *
169 * <byte> 1 byte Byte value of the sibling. Special cases:
170 * BY_NOFLAGS: End of word without flags and for all
171 * regions.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000172 * BY_FLAGS: End of word, <flags> follow. For
173 * PREFIXTREE <prefixID> and <prefcondnr>
174 * follow.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000175 * BY_INDEX: Child of sibling is shared, <nodeidx>
176 * and <xbyte> follow.
177 *
178 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
179 *
180 * <xbyte> 1 byte byte value of the sibling.
181 *
182 * <flags> 1 byte bitmask of:
183 * WF_ALLCAP word must have only capitals
184 * WF_ONECAP first char of word must be capital
185 * WF_RARE rare word
186 * WF_REGION <region> follows
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000187 * WF_PFX <prefixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000188 *
189 * <region> 1 byte Bitmask for regions in which word is valid. When
190 * omitted it's valid in all regions.
191 * Lowest bit is for region 1.
192 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000193 * <prefixID> 1 byte ID of prefix that can be used with this word. For
194 * PREFIXTREE used for the required prefix ID.
195 *
196 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
197 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000198 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000199 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000200 */
201
Bram Moolenaare19defe2005-03-21 08:23:33 +0000202#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
203# include <io.h> /* for lseek(), must be before vim.h */
204#endif
205
206#include "vim.h"
207
208#if defined(FEAT_SYN_HL) || defined(PROTO)
209
210#ifdef HAVE_FCNTL_H
211# include <fcntl.h>
212#endif
213
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000214#define MAXWLEN 250 /* Assume max. word len is this many bytes.
215 Some places assume a word length fits in a
216 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000217
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000218/* Type used for indexes in the word tree need to be at least 3 bytes. If int
219 * is 8 bytes we could use something smaller, but what? */
220#if SIZEOF_INT > 2
221typedef int idx_T;
222#else
223typedef long idx_T;
224#endif
225
226/* Flags used for a word. Only the lowest byte can be used, the region byte
227 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000228#define WF_REGION 0x01 /* region byte follows */
229#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
230#define WF_ALLCAP 0x04 /* word must be all capitals */
231#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000232#define WF_BANNED 0x10 /* bad word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000233#define WF_PFX 0x20 /* prefix ID list follows */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000234#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000235
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000236#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000237
238#define BY_NOFLAGS 0 /* end of word without flags or region */
239#define BY_FLAGS 1 /* end of word, flag byte follows */
240#define BY_INDEX 2 /* child is shared, index follows */
241#define BY_SPECIAL BY_INDEX /* hightest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000242
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000243/* Info from "REP" and "SAL" entries in ".aff" file used in si_rep, sl_rep,
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000244 * and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000245 * One replacement: from "ft_from" to "ft_to". */
246typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000247{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000248 char_u *ft_from;
249 char_u *ft_to;
250} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000251
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000252/* Info from "SAL" entries in ".aff" file used in sl_sal.
253 * The info is split for quick processing by spell_soundfold().
254 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
255typedef struct salitem_S
256{
257 char_u *sm_lead; /* leading letters */
258 int sm_leadlen; /* length of "sm_lead" */
259 char_u *sm_oneoff; /* letters from () or NULL */
260 char_u *sm_rules; /* rules like ^, $, priority */
261 char_u *sm_to; /* replacement. */
262} salitem_T;
263
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000264/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000265 * Structure used to store words and other info for one language, loaded from
266 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000267 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
268 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
269 *
270 * The "byts" array stores the possible bytes in each tree node, preceded by
271 * the number of possible bytes, sorted on byte value:
272 * <len> <byte1> <byte2> ...
273 * The "idxs" array stores the index of the child node corresponding to the
274 * byte in "byts".
275 * Exception: when the byte is zero, the word may end here and "idxs" holds
276 * the flags and region for the word. There may be several zeros in sequence
277 * for alternative flag/region combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000278 */
279typedef struct slang_S slang_T;
280struct slang_S
281{
282 slang_T *sl_next; /* next language */
283 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000284 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000285 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000286
Bram Moolenaar51485f02005-06-04 21:55:20 +0000287 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000288 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000289 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000290 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000291 char_u *sl_pbyts; /* prefix tree word bytes */
292 idx_T *sl_pidxs; /* prefix tree word indexes */
293
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000294 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000295
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000296 int sl_prefixcnt; /* number of items in "sl_prefprog" */
297 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
298
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000299 garray_T sl_rep; /* list of fromto_T entries from REP lines */
300 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
301 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000302 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000303 short sl_sal_first[256]; /* indexes where byte first appears, -1 if
304 there is none */
305 int sl_followup; /* SAL followup */
306 int sl_collapse; /* SAL collapse_result */
307 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaarea424162005-06-16 21:51:00 +0000308 int sl_has_map; /* TRUE if there is a MAP line */
309#ifdef FEAT_MBYTE
310 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
311 int sl_map_array[256]; /* MAP for first 256 chars */
312#else
313 char_u sl_map_array[256]; /* MAP for first 256 chars */
314#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000315};
316
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000317/* First language that is loaded, start of the linked list of loaded
318 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000319static slang_T *first_lang = NULL;
320
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000321/* Flags used in .spl file for soundsalike flags. */
322#define SAL_F0LLOWUP 1
323#define SAL_COLLAPSE 2
324#define SAL_REM_ACCENTS 4
325
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000326/*
327 * Structure used in "b_langp", filled from 'spelllang'.
328 */
329typedef struct langp_S
330{
331 slang_T *lp_slang; /* info for this language (NULL for last one) */
332 int lp_region; /* bitmask for region or REGION_ALL */
333} langp_T;
334
335#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
336
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000337#define REGION_ALL 0xff /* word valid in all regions */
338
339/* Result values. Lower number is accepted over higher one. */
340#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000341#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000342#define SP_RARE 1
343#define SP_LOCAL 2
344#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000345
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000346#define VIMSPELLMAGIC "VIMspell07" /* string at start of Vim spell file */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000347#define VIMSPELLMAGICL 10
348
349/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000350 * Information used when looking for suggestions.
351 */
352typedef struct suginfo_S
353{
354 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000355 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000356 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000357 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000358 char_u *su_badptr; /* start of bad word in line */
359 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000360 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000361 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
362 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
363 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000364} suginfo_T;
365
366/* One word suggestion. Used in "si_ga". */
367typedef struct suggest_S
368{
369 char_u *st_word; /* suggested word, allocated string */
370 int st_orglen; /* length of replaced text */
371 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000372 int st_altscore; /* used when st_score compares equal */
373 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000374 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000375} suggest_T;
376
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000377#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000378
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000379/* Number of suggestions kept when cleaning up. When rescore_suggestions() is
380 * called the score may change, thus we need to keep more than what is
381 * displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000382#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 25 ? 25 : (su)->su_maxcount)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000383
384/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
385 * of suggestions that are not going to be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000386#define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000387
388/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000389#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000390#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000391#define SCORE_REGION 70 /* word is for different region */
392#define SCORE_RARE 180 /* rare word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000393#define SCORE_SWAP 90 /* swap two characters */
394#define SCORE_SWAP3 110 /* swap two characters in three */
395#define SCORE_REP 87 /* REP replacement */
396#define SCORE_SUBST 93 /* substitute a character */
397#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000398#define SCORE_DEL 94 /* delete a character */
399#define SCORE_INS 96 /* insert a character */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000400
401#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
402 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000403
404#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000405#define SCORE_MAXMAX 999999 /* accept any score */
406
407/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000408 * Structure to store info for word matching.
409 */
410typedef struct matchinf_S
411{
412 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000413
414 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000415 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000416 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000417 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000418 char_u *mi_cend; /* char after what was used for
419 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000420
421 /* case-folded text */
422 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000423 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000424
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000425 /* for when checking word after a prefix */
426 int mi_prefarridx; /* index in sl_pidxs with list of
427 prefixID/condition */
428 int mi_prefcnt; /* number of entries at mi_prefarridx */
429 int mi_prefixlen; /* byte length of prefix */
430
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000431 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000432 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000433 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000434} matchinf_T;
435
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000436/*
437 * The tables used for recognizing word characters according to spelling.
438 * These are only used for the first 256 characters of 'encoding'.
439 */
440typedef struct spelltab_S
441{
442 char_u st_isw[256]; /* flags: is word char */
443 char_u st_isu[256]; /* flags: is uppercase char */
444 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000445 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000446} spelltab_T;
447
448static spelltab_T spelltab;
449static int did_set_spelltab;
450
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000451#define CF_WORD 0x01
452#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000453
454static void clear_spell_chartab __ARGS((spelltab_T *sp));
455static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000456static void write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000457
458/*
459 * Return TRUE if "p" points to a word character or "c" is a word character
460 * for spelling.
461 * Checking for a word character is done very often, avoid the function call
462 * overhead.
463 */
464#ifdef FEAT_MBYTE
465# define SPELL_ISWORDP(p) ((has_mbyte && MB_BYTE2LEN(*(p)) > 1) \
466 ? (mb_get_class(p) >= 2) : spelltab.st_isw[*(p)])
467#else
468# define SPELL_ISWORDP(p) (spelltab.st_isw[*(p)])
469#endif
470
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000471/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000472 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000473 */
474typedef enum
475{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000476 STATE_START = 0, /* At start of node check for NUL bytes (goodword
477 * ends); if badword ends there is a match, otherwise
478 * try splitting word. */
479 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000480 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
481 STATE_PLAIN, /* Use each byte of the node. */
482 STATE_DEL, /* Delete a byte from the bad word. */
483 STATE_INS, /* Insert a byte in the bad word. */
484 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000485 STATE_UNSWAP, /* Undo swap two characters. */
486 STATE_SWAP3, /* Swap two characters over three. */
487 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000488 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000489 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000490 STATE_REP_INI, /* Prepare for using REP items. */
491 STATE_REP, /* Use matching REP items from the .aff file. */
492 STATE_REP_UNDO, /* Undo a REP item replacement. */
493 STATE_FINAL /* End of this node. */
494} state_T;
495
496/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000497 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000498 */
499typedef struct trystate_S
500{
Bram Moolenaarea424162005-06-16 21:51:00 +0000501 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000502 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000503 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000504 short ts_curi; /* index in list of child nodes */
505 char_u ts_fidx; /* index in fword[], case-folded bad word */
506 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
507 char_u ts_twordlen; /* valid length of tword[] */
508#ifdef FEAT_MBYTE
509 char_u ts_tcharlen; /* number of bytes in tword character */
510 char_u ts_tcharidx; /* current byte index in tword character */
511 char_u ts_isdiff; /* DIFF_ values */
512 char_u ts_fcharstart; /* index in fword where badword char started */
513#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000514 char_u ts_save_prewordlen; /* saved "prewordlen" */
Bram Moolenaarea424162005-06-16 21:51:00 +0000515 char_u ts_save_splitoff; /* su_splitoff saved here */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000516 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000517} trystate_T;
518
Bram Moolenaarea424162005-06-16 21:51:00 +0000519/* values for ts_isdiff */
520#define DIFF_NONE 0 /* no different byte (yet) */
521#define DIFF_YES 1 /* different byte found */
522#define DIFF_INSERT 2 /* inserting character */
523
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000524/* mode values for find_word */
525#define FIND_FOLDWORD 0 /* find word case-folded */
526#define FIND_KEEPWORD 1 /* find keep-case word */
527#define FIND_PREFIX 2 /* find word after prefix */
528
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000529static slang_T *slang_alloc __ARGS((char_u *lang));
530static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000531static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000532static void find_word __ARGS((matchinf_T *mip, int mode));
533static void find_prefix __ARGS((matchinf_T *mip));
534static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000535static int spell_valid_case __ARGS((int origflags, int treeflags));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000536static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000537static char_u *spell_enc __ARGS((void));
538static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000539static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000540static idx_T read_tree __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, int startidx, int prefixtree, int maxprefcondnr));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000541static int find_region __ARGS((char_u *rp, char_u *region));
542static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000543static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000544static int set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000545static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
546static void write_spell_chartab __ARGS((FILE *fd));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000547static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000548static void spell_find_suggest __ARGS((char_u *badptr, suginfo_T *su, int maxcount));
549static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000550static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000551static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000552static void suggest_try_special __ARGS((suginfo_T *su));
553static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000554static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
555static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000556static void score_comp_sal __ARGS((suginfo_T *su));
557static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000558static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000559static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000560static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000561static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000562static void add_suggestion __ARGS((suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int use_score, int had_bonus));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000563static void add_banned __ARGS((suginfo_T *su, char_u *word));
564static int was_banned __ARGS((suginfo_T *su, char_u *word));
565static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000566static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000567static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000568static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000569static int spell_sound_score __ARGS((slang_T *slang, char_u *goodword, char_u *badsound));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000570static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000571static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
572
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000573/*
574 * Use our own character-case definitions, because the current locale may
575 * differ from what the .spl file uses.
576 * These must not be called with negative number!
577 */
578#ifndef FEAT_MBYTE
579/* Non-multi-byte implementation. */
580# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
581# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
582# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
583#else
584/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
585 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
586 * the "w" library function for characters above 255 if available. */
587# ifdef HAVE_TOWLOWER
588# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
589 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
590# else
591# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
592 : (c) < 256 ? spelltab.st_fold[c] : (c))
593# endif
594
595# ifdef HAVE_TOWUPPER
596# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
597 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
598# else
599# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
600 : (c) < 256 ? spelltab.st_upper[c] : (c))
601# endif
602
603# ifdef HAVE_ISWUPPER
604# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
605 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
606# else
607# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
608 : (c) < 256 ? spelltab.st_isu[c] : (c))
609# endif
610#endif
611
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000612
613static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000614
615/*
616 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000617 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000618 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
619 * or when it's OK it remains unchanged.
620 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000621 *
622 * "sug" is normally NULL. When looking for suggestions it points to
623 * suginfo_T. It's passed as a void pointer to keep the struct local.
624 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000625 * Returns the length of the word in bytes, also when it's OK, so that the
626 * caller can skip over the word.
627 */
628 int
Bram Moolenaar51485f02005-06-04 21:55:20 +0000629spell_check(wp, ptr, attrp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000630 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000631 char_u *ptr;
632 int *attrp;
633{
634 matchinf_T mi; /* Most things are put in "mi" so that it can
635 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000636 int nrlen = 0; /* found a number first */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000637
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000638 /* A word never starts at a space or a control character. Return quickly
639 * then, skipping over the character. */
640 if (*ptr <= ' ')
641 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000642
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000643 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar0c405862005-06-22 22:26:26 +0000644 * 0X99FF. But when a word character follows do check spelling to find
645 * "3GPP". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000646 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000647 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000648 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
649 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000650 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000651 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000652 mi.mi_end = skipdigits(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000653 nrlen = mi.mi_end - ptr;
654 }
655 if (!SPELL_ISWORDP(mi.mi_end))
656 return (int)(mi.mi_end - ptr);
Bram Moolenaar0c405862005-06-22 22:26:26 +0000657
658 /* Try including the digits in the word. */
659 mi.mi_fend = ptr + nrlen;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000660 }
Bram Moolenaar0c405862005-06-22 22:26:26 +0000661 else
662 mi.mi_fend = ptr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000663
Bram Moolenaar0c405862005-06-22 22:26:26 +0000664 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000665 mi.mi_word = ptr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000666 if (SPELL_ISWORDP(mi.mi_fend))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000667 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000668 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000669 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000670 mb_ptr_adv(mi.mi_fend);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000671 } while (*mi.mi_fend != NUL && SPELL_ISWORDP(mi.mi_fend));
672 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000673
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000674 /* We always use the characters up to the next non-word character,
675 * also for bad words. */
676 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000677
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000678 /* Check caps type later. */
679 mi.mi_capflags = 0;
680 mi.mi_cend = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000681
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000682 /* Include one non-word character so that we can check for the
683 * word end. */
684 if (*mi.mi_fend != NUL)
685 mb_ptr_adv(mi.mi_fend);
686
687 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
688 MAXWLEN + 1);
689 mi.mi_fwordlen = STRLEN(mi.mi_fword);
690
691 /* The word is bad unless we recognize it. */
692 mi.mi_result = SP_BAD;
693
694 /*
695 * Loop over the languages specified in 'spelllang'.
696 * We check them all, because a matching word may be longer than an
697 * already found matching word.
698 */
699 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
700 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
701 {
702 /* Check for a matching word in case-folded words. */
703 find_word(&mi, FIND_FOLDWORD);
704
705 /* Check for a matching word in keep-case words. */
706 find_word(&mi, FIND_KEEPWORD);
707
708 /* Check for matching prefixes. */
709 find_prefix(&mi);
710 }
711
712 if (mi.mi_result != SP_OK)
713 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000714 /* If we found a number skip over it. Allows for "42nd". Do flag
715 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000716 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000717 {
718 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
719 return nrlen;
720 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000721
722 /* When we are at a non-word character there is no error, just
723 * skip over the character (try looking for a word after it). */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000724 else if (!SPELL_ISWORDP(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000725 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000726#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000727 if (has_mbyte)
728 return mb_ptr2len_check(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000729#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000730 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000731 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000732
733 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
734 *attrp = highlight_attr[HLF_SPB];
735 else if (mi.mi_result == SP_RARE)
736 *attrp = highlight_attr[HLF_SPR];
737 else
738 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000739 }
740
Bram Moolenaar51485f02005-06-04 21:55:20 +0000741 return (int)(mi.mi_end - ptr);
742}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000743
Bram Moolenaar51485f02005-06-04 21:55:20 +0000744/*
745 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000746 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
747 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
748 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
749 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000750 *
751 * For a match mip->mi_result is updated.
752 */
753 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000754find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000755 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000756 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000757{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000758 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000759 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000760 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000761 int endidxcnt = 0;
762 int len;
763 int wlen = 0;
764 int flen;
765 int c;
766 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000767 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000768#ifdef FEAT_MBYTE
769 char_u *s;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000770 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000771#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000772 int res = SP_BAD;
773 int valid;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000774 slang_T *slang = mip->mi_lp->lp_slang;
775 unsigned flags;
776 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000777 idx_T *idxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000778 int prefcnt;
779 int pidx;
780 regmatch_T regmatch;
781 regprog_T *rp;
782 int prefid;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000783
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000784 if (mode == FIND_KEEPWORD)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000785 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000786 /* Check for word with matching case in keep-case tree. */
787 ptr = mip->mi_word;
788 flen = 9999; /* no case folding, always enough bytes */
789 byts = slang->sl_kbyts;
790 idxs = slang->sl_kidxs;
791 }
792 else
793 {
794 /* Check for case-folded in case-folded tree. */
795 ptr = mip->mi_fword;
796 flen = mip->mi_fwordlen; /* available case-folded bytes */
797 byts = slang->sl_fbyts;
798 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000799
800 if (mode == FIND_PREFIX)
801 {
802 /* Skip over the prefix. */
803 wlen = mip->mi_prefixlen;
804 flen -= mip->mi_prefixlen;
805 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000806 }
807
Bram Moolenaar51485f02005-06-04 21:55:20 +0000808 if (byts == NULL)
809 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000810
Bram Moolenaar51485f02005-06-04 21:55:20 +0000811 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000812 * Repeat advancing in the tree until:
813 * - there is a byte that doesn't match,
814 * - we reach the end of the tree,
815 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000816 */
817 for (;;)
818 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000819 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000820 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000821
822 len = byts[arridx++];
823
824 /* If the first possible byte is a zero the word could end here.
825 * Remember this index, we first check for the longest word. */
826 if (byts[arridx] == 0)
827 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000828 if (endidxcnt == MAXWLEN)
829 {
830 /* Must be a corrupted spell file. */
831 EMSG(_(e_format));
832 return;
833 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000834 endlen[endidxcnt] = wlen;
835 endidx[endidxcnt++] = arridx++;
836 --len;
837
838 /* Skip over the zeros, there can be several flag/region
839 * combinations. */
840 while (len > 0 && byts[arridx] == 0)
841 {
842 ++arridx;
843 --len;
844 }
845 if (len == 0)
846 break; /* no children, word must end here */
847 }
848
849 /* Stop looking at end of the line. */
850 if (ptr[wlen] == NUL)
851 break;
852
853 /* Perform a binary search in the list of accepted bytes. */
854 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +0000855 if (c == TAB) /* <Tab> is handled like <Space> */
856 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +0000857 lo = arridx;
858 hi = arridx + len - 1;
859 while (lo < hi)
860 {
861 m = (lo + hi) / 2;
862 if (byts[m] > c)
863 hi = m - 1;
864 else if (byts[m] < c)
865 lo = m + 1;
866 else
867 {
868 lo = hi = m;
869 break;
870 }
871 }
872
873 /* Stop if there is no matching byte. */
874 if (hi < lo || byts[lo] != c)
875 break;
876
877 /* Continue at the child (if there is one). */
878 arridx = idxs[lo];
879 ++wlen;
880 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +0000881
882 /* One space in the good word may stand for several spaces in the
883 * checked word. */
884 if (c == ' ')
885 {
886 for (;;)
887 {
888 if (flen <= 0 && *mip->mi_fend != NUL)
889 flen = fold_more(mip);
890 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
891 break;
892 ++wlen;
893 --flen;
894 }
895 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000896 }
897
898 /*
899 * Verify that one of the possible endings is valid. Try the longest
900 * first.
901 */
902 while (endidxcnt > 0)
903 {
904 --endidxcnt;
905 arridx = endidx[endidxcnt];
906 wlen = endlen[endidxcnt];
907
908#ifdef FEAT_MBYTE
909 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
910 continue; /* not at first byte of character */
911#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000912 if (SPELL_ISWORDP(ptr + wlen))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000913 continue; /* next char is a word character */
914
915#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000916 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000917 {
918 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000919 * when folding case. This can be slow, take a shortcut when the
920 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000921 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000922 if (STRNCMP(ptr, p, wlen) != 0)
923 {
924 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
925 mb_ptr_adv(p);
926 wlen = p - mip->mi_word;
927 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000928 }
929#endif
930
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000931 /* Check flags and region. For FIND_PREFIX check the condition and
932 * prefix ID.
933 * Repeat this if there are more flags/region alternatives until there
934 * is a match. */
935 res = SP_BAD;
936 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
937 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000938 {
939 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000940
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000941 /* For the fold-case tree check that the case of the checked word
942 * matches with what the word in the tree requires.
943 * For keep-case tree the case is always right. For prefixes we
944 * don't bother to check. */
945 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000946 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000947 if (mip->mi_cend != mip->mi_word + wlen)
948 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000949 /* mi_capflags was set for a different word length, need
950 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000951 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000952 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000953 }
954
Bram Moolenaar0c405862005-06-22 22:26:26 +0000955 if (mip->mi_capflags == WF_KEEPCAP
956 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000957 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000958 }
959
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000960 /* When mode is FIND_PREFIX the word must support the prefix:
961 * check the prefix ID and the condition. Do that for the list at
962 * mip->mi_prefarridx. */
963 if (mode == FIND_PREFIX)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000964 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000965 /* The prefix ID is stored two bytes above the flags. */
966 prefid = (unsigned)flags >> 16;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000967
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000968 valid = FALSE;
969 for (prefcnt = mip->mi_prefcnt - 1; prefcnt >= 0; --prefcnt)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000970 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000971 pidx = slang->sl_pidxs[mip->mi_prefarridx + prefcnt];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000972
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000973 /* Check the prefix ID. */
974 if (prefid != (pidx & 0xff))
975 continue;
976
977 /* Check the condition, if there is one. The
978 * condition index is stored above the prefix ID byte.
979 */
980 rp = slang->sl_prefprog[(unsigned)pidx >> 8];
981 if (rp != NULL)
982 {
983 regmatch.regprog = rp;
984 regmatch.rm_ic = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000985 if (!vim_regexec(&regmatch,
986 mip->mi_fword + mip->mi_prefixlen, 0))
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000987 continue;
988 }
989
990 /* It's a match, use it. */
991 valid = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000992 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000993 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000994
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000995 if (!valid)
996 continue;
997 }
998
999 if (flags & WF_BANNED)
1000 res = SP_BANNED;
1001 else if (flags & WF_REGION)
1002 {
1003 /* Check region. */
1004 if ((mip->mi_lp->lp_region & (flags >> 8)) != 0)
1005 res = SP_OK;
1006 else
1007 res = SP_LOCAL;
1008 }
1009 else if (flags & WF_RARE)
1010 res = SP_RARE;
1011 else
1012 res = SP_OK;
1013
1014 /* Always use the longest match and the best result. */
1015 if (mip->mi_result > res)
1016 {
1017 mip->mi_result = res;
1018 mip->mi_end = mip->mi_word + wlen;
1019 }
1020 else if (mip->mi_result == res
1021 && mip->mi_end < mip->mi_word + wlen)
1022 mip->mi_end = mip->mi_word + wlen;
1023
1024 if (res == SP_OK)
1025 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001026 }
1027
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001028 if (res == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001029 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001030 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001031}
1032
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001033/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001034 * Check if the word at "mip->mi_word" has a matching prefix.
1035 * If it does, then check the following word.
1036 *
1037 * For a match mip->mi_result is updated.
1038 */
1039 static void
1040find_prefix(mip)
1041 matchinf_T *mip;
1042{
1043 idx_T arridx = 0;
1044 int len;
1045 int wlen = 0;
1046 int flen;
1047 int c;
1048 char_u *ptr;
1049 idx_T lo, hi, m;
1050 slang_T *slang = mip->mi_lp->lp_slang;
1051 char_u *byts;
1052 idx_T *idxs;
1053
1054 /* We use the case-folded word here, since prefixes are always
1055 * case-folded. */
1056 ptr = mip->mi_fword;
1057 flen = mip->mi_fwordlen; /* available case-folded bytes */
1058 byts = slang->sl_pbyts;
1059 idxs = slang->sl_pidxs;
1060
1061 if (byts == NULL)
1062 return; /* array is empty */
1063
1064 /*
1065 * Repeat advancing in the tree until:
1066 * - there is a byte that doesn't match,
1067 * - we reach the end of the tree,
1068 * - or we reach the end of the line.
1069 */
1070 for (;;)
1071 {
1072 if (flen == 0 && *mip->mi_fend != NUL)
1073 flen = fold_more(mip);
1074
1075 len = byts[arridx++];
1076
1077 /* If the first possible byte is a zero the prefix could end here.
1078 * Check if the following word matches and supports the prefix. */
1079 if (byts[arridx] == 0)
1080 {
1081 /* There can be several prefixes with different conditions. We
1082 * try them all, since we don't know which one will give the
1083 * longest match. The word is the same each time, pass the list
1084 * of possible prefixes to find_word(). */
1085 mip->mi_prefarridx = arridx;
1086 mip->mi_prefcnt = len;
1087 while (len > 0 && byts[arridx] == 0)
1088 {
1089 ++arridx;
1090 --len;
1091 }
1092 mip->mi_prefcnt -= len;
1093
1094 /* Find the word that comes after the prefix. */
1095 mip->mi_prefixlen = wlen;
1096 find_word(mip, FIND_PREFIX);
1097
1098
1099 if (len == 0)
1100 break; /* no children, word must end here */
1101 }
1102
1103 /* Stop looking at end of the line. */
1104 if (ptr[wlen] == NUL)
1105 break;
1106
1107 /* Perform a binary search in the list of accepted bytes. */
1108 c = ptr[wlen];
1109 lo = arridx;
1110 hi = arridx + len - 1;
1111 while (lo < hi)
1112 {
1113 m = (lo + hi) / 2;
1114 if (byts[m] > c)
1115 hi = m - 1;
1116 else if (byts[m] < c)
1117 lo = m + 1;
1118 else
1119 {
1120 lo = hi = m;
1121 break;
1122 }
1123 }
1124
1125 /* Stop if there is no matching byte. */
1126 if (hi < lo || byts[lo] != c)
1127 break;
1128
1129 /* Continue at the child (if there is one). */
1130 arridx = idxs[lo];
1131 ++wlen;
1132 --flen;
1133 }
1134}
1135
1136/*
1137 * Need to fold at least one more character. Do until next non-word character
1138 * for efficiency.
1139 * Return the length of the folded chars in bytes.
1140 */
1141 static int
1142fold_more(mip)
1143 matchinf_T *mip;
1144{
1145 int flen;
1146 char_u *p;
1147
1148 p = mip->mi_fend;
1149 do
1150 {
1151 mb_ptr_adv(mip->mi_fend);
1152 } while (*mip->mi_fend != NUL && SPELL_ISWORDP(mip->mi_fend));
1153
1154 /* Include the non-word character so that we can check for the
1155 * word end. */
1156 if (*mip->mi_fend != NUL)
1157 mb_ptr_adv(mip->mi_fend);
1158
1159 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1160 mip->mi_fword + mip->mi_fwordlen,
1161 MAXWLEN - mip->mi_fwordlen);
1162 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1163 mip->mi_fwordlen += flen;
1164 return flen;
1165}
1166
1167/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001168 * Check case flags for a word. Return TRUE if the word has the requested
1169 * case.
1170 */
1171 static int
1172spell_valid_case(origflags, treeflags)
1173 int origflags; /* flags for the checked word. */
1174 int treeflags; /* flags for the word in the spell tree */
1175{
1176 return (origflags == WF_ALLCAP
1177 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
1178 && ((treeflags & WF_ONECAP) == 0 || origflags == WF_ONECAP)));
1179}
1180
Bram Moolenaar51485f02005-06-04 21:55:20 +00001181
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001182/*
1183 * Move to next spell error.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001184 * "curline" is TRUE for "z?": find word under/after cursor in the same line.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001185 * Return OK if found, FAIL otherwise.
1186 */
1187 int
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001188spell_move_to(dir, allwords, curline)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001189 int dir; /* FORWARD or BACKWARD */
1190 int allwords; /* TRUE for "[s" and "]s" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001191 int curline;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001192{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001193 linenr_T lnum;
1194 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001195 char_u *line;
1196 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001197 char_u *endp;
1198 int attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001199 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001200 int has_syntax = syntax_present(curbuf);
1201 int col;
1202 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001203 char_u *buf = NULL;
1204 int buflen = 0;
1205 int skip = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001206
Bram Moolenaarb765d632005-06-07 21:00:02 +00001207 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001208 {
1209 EMSG(_("E756: Spell checking not enabled"));
1210 return FAIL;
1211 }
1212
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001213 /*
1214 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001215 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001216 *
1217 * When searching backwards, we continue in the line to find the last
1218 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001219 *
1220 * We concatenate the start of the next line, so that wrapped words work
1221 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1222 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001223 */
1224 lnum = curwin->w_cursor.lnum;
1225 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001226
1227 while (!got_int)
1228 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001229 line = ml_get(lnum);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001230
Bram Moolenaar0c405862005-06-22 22:26:26 +00001231 len = STRLEN(line);
1232 if (buflen < len + MAXWLEN + 2)
1233 {
1234 vim_free(buf);
1235 buflen = len + MAXWLEN + 2;
1236 buf = alloc(buflen);
1237 if (buf == NULL)
1238 break;
1239 }
1240
1241 /* Copy the line into "buf" and append the start of the next line if
1242 * possible. */
1243 STRCPY(buf, line);
1244 if (lnum < curbuf->b_ml.ml_line_count)
1245 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1246
1247 p = buf + skip;
1248 endp = buf + len;
1249 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001250 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001251 /* When searching backward don't search after the cursor. */
1252 if (dir == BACKWARD
1253 && lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001254 && (colnr_T)(p - buf) >= curwin->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001255 break;
1256
1257 /* start of word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00001258 attr = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001259 len = spell_check(curwin, p, &attr);
1260
1261 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001262 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001263 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001264 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001265 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001266 /* When searching forward only accept a bad word after
1267 * the cursor. */
1268 if (dir == BACKWARD
1269 || lnum > curwin->w_cursor.lnum
1270 || (lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001271 && (colnr_T)(curline ? p - buf + len
1272 : p - buf)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001273 > curwin->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001274 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001275 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001276 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001277 col = p - buf;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001278 (void)syn_get_id(lnum, (colnr_T)col,
1279 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001280 }
1281 else
1282 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001283
Bram Moolenaar51485f02005-06-04 21:55:20 +00001284 if (can_spell)
1285 {
1286 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001287 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001288#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001289 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001290#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001291 if (dir == FORWARD)
1292 {
1293 /* No need to search further. */
1294 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001295 vim_free(buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001296 return OK;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001297 }
1298 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001299 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001300 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001301 }
1302
Bram Moolenaar51485f02005-06-04 21:55:20 +00001303 /* advance to character after the word */
1304 p += len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001305 }
1306
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001307 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001308 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001309
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001310 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001311 if (dir == BACKWARD)
1312 {
1313 if (found_pos.lnum != 0)
1314 {
1315 /* Use the last match in the line. */
1316 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001317 vim_free(buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001318 return OK;
1319 }
1320 if (lnum == 1)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001321 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001322 --lnum;
1323 }
1324 else
1325 {
1326 if (lnum == curbuf->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001327 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001328 ++lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001329
1330 /* Skip the characters at the start of the next line that were
1331 * included in a match crossing line boundaries. */
1332 if (attr == 0)
1333 skip = p - endp;
1334 else
1335 skip = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001336 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001337
1338 line_breakcheck();
1339 }
1340
Bram Moolenaar0c405862005-06-22 22:26:26 +00001341 vim_free(buf);
1342 return FAIL;
1343}
1344
1345/*
1346 * For spell checking: concatenate the start of the following line "line" into
1347 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
1348 */
1349 void
1350spell_cat_line(buf, line, maxlen)
1351 char_u *buf;
1352 char_u *line;
1353 int maxlen;
1354{
1355 char_u *p;
1356 int n;
1357
1358 p = skipwhite(line);
1359 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1360 p = skipwhite(p + 1);
1361
1362 if (*p != NUL)
1363 {
1364 *buf = ' ';
1365 vim_strncpy(buf + 1, line, maxlen - 1);
1366 n = p - line;
1367 if (n >= maxlen)
1368 n = maxlen - 1;
1369 vim_memset(buf + 1, ' ', n);
1370 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001371}
1372
1373/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001374 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001375 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001376 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001377 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001378spell_load_lang(lang)
1379 char_u *lang;
1380{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001381 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001382 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001383 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001384
Bram Moolenaarb765d632005-06-07 21:00:02 +00001385 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001386 * It's truncated when an error is detected. */
1387 STRCPY(langcp, lang);
1388
Bram Moolenaarb765d632005-06-07 21:00:02 +00001389 /*
1390 * Find the first spell file for "lang" in 'runtimepath' and load it.
1391 */
1392 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
1393 "spell/%s.%s.spl", lang, spell_enc());
1394 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001395
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001396 if (r == FAIL && *langcp != NUL)
1397 {
1398 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001399 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001400 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001401 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001402 }
1403
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001404 if (r == FAIL)
1405 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
1406 fname_enc + 6);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001407 else if (*langcp != NUL)
1408 {
1409 /* Load all the additions. */
1410 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
1411 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
1412 }
1413}
1414
1415/*
1416 * Return the encoding used for spell checking: Use 'encoding', except that we
1417 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1418 */
1419 static char_u *
1420spell_enc()
1421{
1422
1423#ifdef FEAT_MBYTE
1424 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1425 return p_enc;
1426#endif
1427 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001428}
1429
1430/*
1431 * Allocate a new slang_T.
1432 * Caller must fill "sl_next".
1433 */
1434 static slang_T *
1435slang_alloc(lang)
1436 char_u *lang;
1437{
1438 slang_T *lp;
1439
Bram Moolenaar51485f02005-06-04 21:55:20 +00001440 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001441 if (lp != NULL)
1442 {
1443 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001444 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001445 ga_init2(&lp->sl_sal, sizeof(salitem_T), 10);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001446 }
1447 return lp;
1448}
1449
1450/*
1451 * Free the contents of an slang_T and the structure itself.
1452 */
1453 static void
1454slang_free(lp)
1455 slang_T *lp;
1456{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001457 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001458 vim_free(lp->sl_fname);
1459 slang_clear(lp);
1460 vim_free(lp);
1461}
1462
1463/*
1464 * Clear an slang_T so that the file can be reloaded.
1465 */
1466 static void
1467slang_clear(lp)
1468 slang_T *lp;
1469{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001470 garray_T *gap;
1471 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001472 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001473 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001474
Bram Moolenaar51485f02005-06-04 21:55:20 +00001475 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001476 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001477 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001478 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001479 vim_free(lp->sl_pbyts);
1480 lp->sl_pbyts = NULL;
1481
Bram Moolenaar51485f02005-06-04 21:55:20 +00001482 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001483 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001484 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001485 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001486 vim_free(lp->sl_pidxs);
1487 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001488
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001489 gap = &lp->sl_rep;
1490 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001491 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001492 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1493 vim_free(ftp->ft_from);
1494 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001495 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001496 ga_clear(gap);
1497
1498 gap = &lp->sl_sal;
1499 while (gap->ga_len > 0)
1500 {
1501 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1502 vim_free(smp->sm_lead);
1503 vim_free(smp->sm_to);
1504 }
1505 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001506
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001507 for (i = 0; i < lp->sl_prefixcnt; ++i)
1508 vim_free(lp->sl_prefprog[i]);
1509 vim_free(lp->sl_prefprog);
1510
Bram Moolenaarea424162005-06-16 21:51:00 +00001511#ifdef FEAT_MBYTE
1512 {
1513 int todo = lp->sl_map_hash.ht_used;
1514 hashitem_T *hi;
1515
1516 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
1517 if (!HASHITEM_EMPTY(hi))
1518 {
1519 --todo;
1520 vim_free(hi->hi_key);
1521 }
1522 }
1523 hash_clear(&lp->sl_map_hash);
1524#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001525}
1526
1527/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001528 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001529 * Invoked through do_in_runtimepath().
1530 */
1531 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00001532spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001533 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001534 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001535{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001536 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001537}
1538
1539/*
1540 * Load one spell file and store the info into a slang_T.
1541 *
1542 * This is invoked in two ways:
1543 * - From spell_load_cb() to load a spell file for the first time. "lang" is
1544 * the language name, "old_lp" is NULL. Will allocate an slang_T.
1545 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
1546 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001547 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00001548 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001549 static slang_T *
1550spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001551 char_u *fname;
1552 char_u *lang;
1553 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001554 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001555{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001556 FILE *fd;
1557 char_u buf[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001558 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001559 char_u *bp;
1560 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001561 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001562 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001563 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001564 int round;
1565 char_u *save_sourcing_name = sourcing_name;
1566 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001567 int cnt, ccnt;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001568 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001569 slang_T *lp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001570 garray_T *gap;
1571 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001572 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001573 int rr;
1574 short *first;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001575 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001576 int c = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001577
Bram Moolenaarb765d632005-06-07 21:00:02 +00001578 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001579 if (fd == NULL)
1580 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001581 if (!silent)
1582 EMSG2(_(e_notopen), fname);
1583 else if (p_verbose > 2)
1584 {
1585 verbose_enter();
1586 smsg((char_u *)e_notopen, fname);
1587 verbose_leave();
1588 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001589 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001590 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00001591 if (p_verbose > 2)
1592 {
1593 verbose_enter();
1594 smsg((char_u *)_("Reading spell file \"%s\""), fname);
1595 verbose_leave();
1596 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001597
Bram Moolenaarb765d632005-06-07 21:00:02 +00001598 if (old_lp == NULL)
1599 {
1600 lp = slang_alloc(lang);
1601 if (lp == NULL)
1602 goto endFAIL;
1603
1604 /* Remember the file name, used to reload the file when it's updated. */
1605 lp->sl_fname = vim_strsave(fname);
1606 if (lp->sl_fname == NULL)
1607 goto endFAIL;
1608
1609 /* Check for .add.spl. */
1610 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
1611 }
1612 else
1613 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001614
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001615 /* Set sourcing_name, so that error messages mention the file name. */
1616 sourcing_name = fname;
1617 sourcing_lnum = 0;
1618
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001619 /* <HEADER>: <fileID>
1620 * <regioncnt> <regionname> ...
1621 * <charflagslen> <charflags>
1622 * <fcharslen> <fchars>
1623 * <prefcondcnt> <prefcond> ...
1624 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001625 for (i = 0; i < VIMSPELLMAGICL; ++i)
1626 buf[i] = getc(fd); /* <fileID> */
1627 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
1628 {
1629 EMSG(_("E757: Wrong file ID in spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001630 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001631 }
1632
1633 cnt = getc(fd); /* <regioncnt> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001634 if (cnt < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001635 {
1636truncerr:
1637 EMSG(_("E758: Truncated spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001638 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001639 }
1640 if (cnt > 8)
1641 {
1642formerr:
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001643 EMSG(_(e_format));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001644 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001645 }
1646 for (i = 0; i < cnt; ++i)
1647 {
1648 lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
1649 lp->sl_regions[i * 2 + 1] = getc(fd);
1650 }
1651 lp->sl_regions[cnt * 2] = NUL;
1652
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001653 cnt = getc(fd); /* <charflagslen> */
1654 if (cnt > 0)
1655 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001656 p = alloc((unsigned)cnt);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001657 if (p == NULL)
1658 goto endFAIL;
1659 for (i = 0; i < cnt; ++i)
1660 p[i] = getc(fd); /* <charflags> */
1661
1662 ccnt = (getc(fd) << 8) + getc(fd); /* <fcharslen> */
1663 if (ccnt <= 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001664 {
1665 vim_free(p);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001666 goto formerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001667 }
1668 fol = alloc((unsigned)ccnt + 1);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001669 if (fol == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001670 {
1671 vim_free(p);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001672 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001673 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001674 for (i = 0; i < ccnt; ++i)
1675 fol[i] = getc(fd); /* <fchars> */
1676 fol[i] = NUL;
1677
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001678 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001679 i = set_spell_charflags(p, cnt, fol);
1680 vim_free(p);
1681 vim_free(fol);
1682 if (i == FAIL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001683 goto formerr;
1684 }
1685 else
1686 {
1687 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
1688 cnt = (getc(fd) << 8) + getc(fd);
1689 if (cnt != 0)
1690 goto formerr;
1691 }
1692
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001693 /* <prefcondcnt> <prefcond> ... */
1694 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
1695 if (cnt > 0)
1696 {
1697 lp->sl_prefprog = (regprog_T **)alloc_clear(
1698 (unsigned)sizeof(regprog_T *) * cnt);
1699 if (lp->sl_prefprog == NULL)
1700 goto endFAIL;
1701 lp->sl_prefixcnt = cnt;
1702
1703 for (i = 0; i < cnt; ++i)
1704 {
1705 /* <prefcond> : <condlen> <condstr> */
1706 n = getc(fd); /* <condlen> */
1707 if (n < 0)
1708 goto formerr;
1709 /* When <condlen> is zero we have an empty condition. Otherwise
1710 * compile the regexp program used to check for the condition. */
1711 if (n > 0)
1712 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001713 buf[0] = '^'; /* always match at one position only */
1714 p = buf + 1;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001715 while (n-- > 0)
1716 *p++ = getc(fd); /* <condstr> */
1717 *p = NUL;
1718 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
1719 }
1720 }
1721 }
1722
1723
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001724 /* <SUGGEST> : <repcount> <rep> ...
1725 * <salflags> <salcount> <sal> ...
1726 * <maplen> <mapstr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001727
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001728 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
1729 if (cnt < 0)
1730 goto formerr;
1731
1732 gap = &lp->sl_rep;
1733 if (ga_grow(gap, cnt) == FAIL)
1734 goto endFAIL;
1735
1736 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
1737 for (; gap->ga_len < cnt; ++gap->ga_len)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001738 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001739 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
1740 for (rr = 1; rr <= 2; ++rr)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001741 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001742 ccnt = getc(fd);
1743 if (ccnt < 0)
1744 {
1745 if (rr == 2)
1746 vim_free(ftp->ft_from);
1747 goto formerr;
1748 }
1749 if ((p = alloc(ccnt + 1)) == NULL)
1750 {
1751 if (rr == 2)
1752 vim_free(ftp->ft_from);
1753 goto endFAIL;
1754 }
1755 for (i = 0; i < ccnt; ++i)
1756 p[i] = getc(fd); /* <repfrom> or <repto> */
1757 p[i] = NUL;
1758 if (rr == 1)
1759 ftp->ft_from = p;
1760 else
1761 ftp->ft_to = p;
1762 }
1763 }
1764
1765 /* Fill the first-index table. */
1766 first = lp->sl_rep_first;
1767 for (i = 0; i < 256; ++i)
1768 first[i] = -1;
1769 for (i = 0; i < gap->ga_len; ++i)
1770 {
1771 ftp = &((fromto_T *)gap->ga_data)[i];
1772 if (first[*ftp->ft_from] == -1)
1773 first[*ftp->ft_from] = i;
1774 }
1775
1776 i = getc(fd); /* <salflags> */
1777 if (i & SAL_F0LLOWUP)
1778 lp->sl_followup = TRUE;
1779 if (i & SAL_COLLAPSE)
1780 lp->sl_collapse = TRUE;
1781 if (i & SAL_REM_ACCENTS)
1782 lp->sl_rem_accents = TRUE;
1783
1784 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
1785 if (cnt < 0)
1786 goto formerr;
1787
1788 gap = &lp->sl_sal;
1789 if (ga_grow(gap, cnt) == FAIL)
1790 goto endFAIL;
1791
1792 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
1793 for (; gap->ga_len < cnt; ++gap->ga_len)
1794 {
1795 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
1796 ccnt = getc(fd); /* <salfromlen> */
1797 if (ccnt < 0)
1798 goto formerr;
1799 if ((p = alloc(ccnt + 2)) == NULL)
1800 goto endFAIL;
1801 smp->sm_lead = p;
1802
1803 /* Read up to the first special char into sm_lead. */
1804 for (i = 0; i < ccnt; ++i)
1805 {
1806 c = getc(fd); /* <salfrom> */
1807 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
1808 break;
1809 *p++ = c;
1810 }
1811 smp->sm_leadlen = p - smp->sm_lead;
1812 *p++ = NUL;
1813
1814 /* Put optional chars in sm_oneoff, if any. */
1815 if (c == '(')
1816 {
1817 smp->sm_oneoff = p;
1818 for (++i; i < ccnt; ++i)
1819 {
1820 c = getc(fd); /* <salfrom> */
1821 if (c == ')')
1822 break;
1823 *p++ = c;
1824 }
1825 *p++ = NUL;
1826 if (++i < ccnt)
1827 c = getc(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001828 }
1829 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001830 smp->sm_oneoff = NULL;
1831
1832 /* Any following chars go in sm_rules. */
1833 smp->sm_rules = p;
1834 if (i < ccnt)
1835 *p++ = c;
1836 for (++i; i < ccnt; ++i)
1837 *p++ = getc(fd); /* <salfrom> */
1838 *p++ = NUL;
1839
1840 ccnt = getc(fd); /* <saltolen> */
1841 if (ccnt < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001842 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001843 vim_free(smp->sm_lead);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001844 goto formerr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001845 }
1846 if ((p = alloc(ccnt + 1)) == NULL)
1847 {
1848 vim_free(smp->sm_lead);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001849 goto endFAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001850 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001851 smp->sm_to = p;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001852
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001853 for (i = 0; i < ccnt; ++i)
1854 *p++ = getc(fd); /* <salto> */
1855 *p++ = NUL;
1856 }
1857
1858 /* Fill the first-index table. */
1859 first = lp->sl_sal_first;
1860 for (i = 0; i < 256; ++i)
1861 first[i] = -1;
1862 for (i = 0; i < gap->ga_len; ++i)
1863 {
1864 smp = &((salitem_T *)gap->ga_data)[i];
1865 if (first[*smp->sm_lead] == -1)
1866 first[*smp->sm_lead] = i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001867 }
1868
1869 cnt = (getc(fd) << 8) + getc(fd); /* <maplen> */
1870 if (cnt < 0)
1871 goto formerr;
1872 p = alloc(cnt + 1);
1873 if (p == NULL)
1874 goto endFAIL;
1875 for (i = 0; i < cnt; ++i)
1876 p[i] = getc(fd); /* <mapstr> */
1877 p[i] = NUL;
Bram Moolenaarea424162005-06-16 21:51:00 +00001878 set_map_str(lp, p);
1879 vim_free(p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001880
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001881
Bram Moolenaar51485f02005-06-04 21:55:20 +00001882 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001883 * round 2: <KWORDTREE>
1884 * round 3: <PREFIXTREE> */
1885 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001886 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001887 /* The tree size was computed when writing the file, so that we can
1888 * allocate it as one long block. <nodecount> */
1889 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
1890 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001891 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001892 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001893 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001894 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001895 bp = lalloc((long_u)len, TRUE);
1896 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001897 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001898 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001899 lp->sl_fbyts = bp;
1900 else if (round == 2)
1901 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001902 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001903 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001904
1905 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001906 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
1907 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001908 goto endFAIL;
1909 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001910 lp->sl_fidxs = ip;
1911 else if (round == 2)
1912 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001913 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001914 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001915
1916 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001917 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001918 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001919 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001920 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001921 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001922 }
1923 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001924
Bram Moolenaarb765d632005-06-07 21:00:02 +00001925 /* For a new file link it in the list of spell files. */
1926 if (old_lp == NULL)
1927 {
1928 lp->sl_next = first_lang;
1929 first_lang = lp;
1930 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001931
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001932 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001933
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001934endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00001935 if (lang != NULL)
1936 /* truncating the name signals the error to spell_load_lang() */
1937 *lang = NUL;
1938 if (lp != NULL && old_lp == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001939 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001940 slang_free(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001941 lp = NULL;
1942 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001943
1944endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001945 if (fd != NULL)
1946 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001947 sourcing_name = save_sourcing_name;
1948 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001949
1950 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001951}
1952
1953/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00001954 * Read one row of siblings from the spell file and store it in the byte array
1955 * "byts" and index array "idxs". Recursively read the children.
1956 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00001957 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00001958 *
1959 * Returns the index follosing the siblings.
1960 * Returns -1 if the file is shorter than expected.
1961 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001962 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001963 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001964read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001965 FILE *fd;
1966 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001967 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001968 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001969 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001970 int prefixtree; /* TRUE for reading PREFIXTREE */
1971 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001972{
Bram Moolenaar51485f02005-06-04 21:55:20 +00001973 int len;
1974 int i;
1975 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001976 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001977 int c;
1978#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001979
Bram Moolenaar51485f02005-06-04 21:55:20 +00001980 len = getc(fd); /* <siblingcount> */
1981 if (len <= 0)
1982 return -1;
1983
1984 if (startidx + len >= maxidx)
1985 return -2;
1986 byts[idx++] = len;
1987
1988 /* Read the byte values, flag/region bytes and shared indexes. */
1989 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001990 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001991 c = getc(fd); /* <byte> */
1992 if (c < 0)
1993 return -1;
1994 if (c <= BY_SPECIAL)
1995 {
1996 if (c == BY_NOFLAGS)
1997 {
1998 /* No flags, all regions. */
1999 idxs[idx] = 0;
2000 c = 0;
2001 }
2002 else if (c == BY_FLAGS)
2003 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002004 if (prefixtree)
2005 {
2006 /* Read the prefix ID and the condition nr. In idxs[]
2007 * store the prefix ID in the low byte, the condition
2008 * index shifted up 8 bits. */
2009 c = getc(fd); /* <prefixID> */
2010 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
2011 if (n >= maxprefcondnr)
2012 return -2;
2013 c = (n << 8) + c;
2014 }
2015 else
2016 {
2017 /* Read flags and optional region and prefix ID. In
2018 * idxs[] the flags go in the low byte, region above that
2019 * and prefix ID above the region. */
2020 c = getc(fd); /* <flags> */
2021 if (c & WF_REGION)
2022 c = (getc(fd) << 8) + c; /* <region> */
2023 if (c & WF_PFX)
2024 c = (getc(fd) << 16) + c; /* <prefixID> */
2025 }
2026
Bram Moolenaar51485f02005-06-04 21:55:20 +00002027 idxs[idx] = c;
2028 c = 0;
2029 }
2030 else /* c == BY_INDEX */
2031 {
2032 /* <nodeidx> */
2033 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2034 if (n < 0 || n >= maxidx)
2035 return -2;
2036 idxs[idx] = n + SHARED_MASK;
2037 c = getc(fd); /* <xbyte> */
2038 }
2039 }
2040 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002041 }
2042
Bram Moolenaar51485f02005-06-04 21:55:20 +00002043 /* Recursively read the children for non-shared siblings.
2044 * Skip the end-of-word ones (zero byte value) and the shared ones (and
2045 * remove SHARED_MASK) */
2046 for (i = 1; i <= len; ++i)
2047 if (byts[startidx + i] != 0)
2048 {
2049 if (idxs[startidx + i] & SHARED_MASK)
2050 idxs[startidx + i] &= ~SHARED_MASK;
2051 else
2052 {
2053 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002054 idx = read_tree(fd, byts, idxs, maxidx, idx,
2055 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002056 if (idx < 0)
2057 break;
2058 }
2059 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002060
Bram Moolenaar51485f02005-06-04 21:55:20 +00002061 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002062}
2063
2064/*
2065 * Parse 'spelllang' and set buf->b_langp accordingly.
2066 * Returns an error message or NULL.
2067 */
2068 char_u *
2069did_set_spelllang(buf)
2070 buf_T *buf;
2071{
2072 garray_T ga;
2073 char_u *lang;
2074 char_u *e;
2075 char_u *region;
2076 int region_mask;
2077 slang_T *lp;
2078 int c;
2079 char_u lbuf[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002080 char_u spf_name[MAXPATHL];
2081 int did_spf = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002082
2083 ga_init2(&ga, sizeof(langp_T), 2);
2084
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002085 /* Get the name of the .spl file associated with 'spellfile'. */
2086 if (*buf->b_p_spf == NUL)
2087 did_spf = TRUE;
2088 else
2089 vim_snprintf((char *)spf_name, sizeof(spf_name), "%s.spl",
2090 buf->b_p_spf);
2091
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002092 /* loop over comma separated languages. */
2093 for (lang = buf->b_p_spl; *lang != NUL; lang = e)
2094 {
2095 e = vim_strchr(lang, ',');
2096 if (e == NULL)
2097 e = lang + STRLEN(lang);
Bram Moolenaar5482f332005-04-17 20:18:43 +00002098 region = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002099 if (e > lang + 2)
2100 {
2101 if (e - lang >= MAXWLEN)
2102 {
2103 ga_clear(&ga);
2104 return e_invarg;
2105 }
2106 if (lang[2] == '_')
2107 region = lang + 3;
2108 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002109
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002110 /* Check if we loaded this language before. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002111 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2112 if (STRNICMP(lp->sl_name, lang, 2) == 0)
2113 break;
2114
2115 if (lp == NULL)
2116 {
2117 /* Not found, load the language. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002118 vim_strncpy(lbuf, lang, e - lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002119 if (region != NULL)
2120 mch_memmove(lbuf + 2, lbuf + 5, e - lang - 4);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002121 spell_load_lang(lbuf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002122 }
2123
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002124 /*
2125 * Loop over the languages, there can be several files for each.
2126 */
2127 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2128 if (STRNICMP(lp->sl_name, lang, 2) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002129 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002130 region_mask = REGION_ALL;
2131 if (region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002132 {
2133 /* find region in sl_regions */
2134 c = find_region(lp->sl_regions, region);
2135 if (c == REGION_ALL)
2136 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002137 if (!lp->sl_add)
2138 {
2139 c = *e;
2140 *e = NUL;
2141 smsg((char_u *)_("Warning: region %s not supported"),
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002142 lang);
Bram Moolenaar3982c542005-06-08 21:56:31 +00002143 *e = c;
2144 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002145 }
2146 else
2147 region_mask = 1 << c;
2148 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002149
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002150 if (ga_grow(&ga, 1) == FAIL)
2151 {
2152 ga_clear(&ga);
2153 return e_outofmem;
2154 }
2155 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2156 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2157 ++ga.ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002158
2159 /* Check if this is the 'spellfile' spell file. */
2160 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
2161 did_spf = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002162 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002163
2164 if (*e == ',')
2165 ++e;
2166 }
2167
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002168 /*
2169 * Make sure the 'spellfile' file is loaded. It may be in 'runtimepath',
2170 * then it's probably loaded above already. Otherwise load it here.
2171 */
2172 if (!did_spf)
2173 {
2174 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2175 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
2176 break;
2177 if (lp == NULL)
2178 {
2179 vim_strncpy(lbuf, gettail(spf_name), 2);
2180 lp = spell_load_file(spf_name, lbuf, NULL, TRUE);
2181 }
2182 if (lp != NULL && ga_grow(&ga, 1) == OK)
2183 {
2184 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2185 LANGP_ENTRY(ga, ga.ga_len)->lp_region = REGION_ALL;
2186 ++ga.ga_len;
2187 }
2188 }
2189
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002190 /* Add a NULL entry to mark the end of the list. */
2191 if (ga_grow(&ga, 1) == FAIL)
2192 {
2193 ga_clear(&ga);
2194 return e_outofmem;
2195 }
2196 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
2197 ++ga.ga_len;
2198
2199 /* Everything is fine, store the new b_langp value. */
2200 ga_clear(&buf->b_langp);
2201 buf->b_langp = ga;
2202
2203 return NULL;
2204}
2205
2206/*
2207 * Find the region "region[2]" in "rp" (points to "sl_regions").
2208 * Each region is simply stored as the two characters of it's name.
2209 * Returns the index if found, REGION_ALL if not found.
2210 */
2211 static int
2212find_region(rp, region)
2213 char_u *rp;
2214 char_u *region;
2215{
2216 int i;
2217
2218 for (i = 0; ; i += 2)
2219 {
2220 if (rp[i] == NUL)
2221 return REGION_ALL;
2222 if (rp[i] == region[0] && rp[i + 1] == region[1])
2223 break;
2224 }
2225 return i / 2;
2226}
2227
2228/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002229 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002230 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002231 * Word WF_ONECAP
2232 * W WORD WF_ALLCAP
2233 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002234 */
2235 static int
2236captype(word, end)
2237 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002238 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002239{
2240 char_u *p;
2241 int c;
2242 int firstcap;
2243 int allcap;
2244 int past_second = FALSE; /* past second word char */
2245
2246 /* find first letter */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002247 for (p = word; !SPELL_ISWORDP(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002248 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002249 return 0; /* only non-word characters, illegal word */
2250#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00002251 if (has_mbyte)
2252 c = mb_ptr2char_adv(&p);
2253 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002254#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00002255 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002256 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002257
2258 /*
2259 * Need to check all letters to find a word with mixed upper/lower.
2260 * But a word with an upper char only at start is a ONECAP.
2261 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002262 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002263 if (SPELL_ISWORDP(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002264 {
2265#ifdef FEAT_MBYTE
2266 c = mb_ptr2char(p);
2267#else
2268 c = *p;
2269#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002270 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002271 {
2272 /* UUl -> KEEPCAP */
2273 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002274 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002275 allcap = FALSE;
2276 }
2277 else if (!allcap)
2278 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002279 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002280 past_second = TRUE;
2281 }
2282
2283 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002284 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002285 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002286 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002287 return 0;
2288}
2289
2290# if defined(FEAT_MBYTE) || defined(PROTO)
2291/*
2292 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002293 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002294 */
2295 void
2296spell_reload()
2297{
2298 buf_T *buf;
2299 slang_T *lp;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002300 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002301
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002302 /* Initialize the table for SPELL_ISWORDP(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002303 init_spell_chartab();
2304
2305 /* Unload all allocated memory. */
2306 while (first_lang != NULL)
2307 {
2308 lp = first_lang;
2309 first_lang = lp->sl_next;
2310 slang_free(lp);
2311 }
2312
2313 /* Go through all buffers and handle 'spelllang'. */
2314 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2315 {
2316 ga_clear(&buf->b_langp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00002317
2318 /* Only load the wordlists when 'spelllang' is set and there is a
2319 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002320 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002321 {
2322 FOR_ALL_WINDOWS(wp)
2323 if (wp->w_buffer == buf && wp->w_p_spell)
2324 {
2325 (void)did_set_spelllang(buf);
2326# ifdef FEAT_WINDOWS
2327 break;
2328# endif
2329 }
2330 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002331 }
2332}
2333# endif
2334
Bram Moolenaarb765d632005-06-07 21:00:02 +00002335/*
2336 * Reload the spell file "fname" if it's loaded.
2337 */
2338 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002339spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002340 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002341 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002342{
2343 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002344 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002345
Bram Moolenaarb765d632005-06-07 21:00:02 +00002346 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2347 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
2348 {
2349 slang_clear(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002350 (void)spell_load_file(fname, NULL, lp, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002351 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002352 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002353 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002354
2355 /* When "zg" was used and the file wasn't loaded yet, should redo
2356 * 'spelllang' to get it loaded. */
2357 if (added_word && !didit)
2358 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002359}
2360
2361
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002362/*
2363 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002364 */
2365
Bram Moolenaar51485f02005-06-04 21:55:20 +00002366#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002367 and .dic file. */
2368/*
2369 * Main structure to store the contents of a ".aff" file.
2370 */
2371typedef struct afffile_S
2372{
2373 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002374 int af_rar; /* RAR ID for rare word */
2375 int af_kep; /* KEP ID for keep-case word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00002376 int af_bad; /* BAD ID for banned word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002377 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002378 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
2379 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002380} afffile_T;
2381
2382typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002383/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
2384struct affentry_S
2385{
2386 affentry_T *ae_next; /* next affix with same name/number */
2387 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
2388 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002389 char_u *ae_cond; /* condition (NULL for ".") */
2390 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002391};
2392
2393/* Affix header from ".aff" file. Used for af_pref and af_suff. */
2394typedef struct affheader_S
2395{
2396 char_u ah_key[2]; /* key for hashtable == name of affix entry */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002397 int ah_newID; /* prefix ID after renumbering */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002398 int ah_combine; /* suffix may combine with prefix */
2399 affentry_T *ah_first; /* first affix entry */
2400} affheader_T;
2401
2402#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
2403
2404/*
2405 * Structure that is used to store the items in the word tree. This avoids
2406 * the need to keep track of each allocated thing, it's freed all at once
2407 * after ":mkspell" is done.
2408 */
2409#define SBLOCKSIZE 16000 /* size of sb_data */
2410typedef struct sblock_S sblock_T;
2411struct sblock_S
2412{
2413 sblock_T *sb_next; /* next block in list */
2414 int sb_used; /* nr of bytes already in use */
2415 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002416};
2417
2418/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002419 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002420 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002421typedef struct wordnode_S wordnode_T;
2422struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002423{
Bram Moolenaar0c405862005-06-22 22:26:26 +00002424 union /* shared to save space */
2425 {
2426 char_u hashkey[6]; /* room for the hash key */
2427 int index; /* index in written nodes (valid after first
2428 round) */
2429 } wn_u1;
2430 union /* shared to save space */
2431 {
2432 wordnode_T *next; /* next node with same hash key */
2433 wordnode_T *wnode; /* parent node that will write this node */
2434 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002435 wordnode_T *wn_child; /* child (next byte in word) */
2436 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
2437 always sorted) */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002438 char_u wn_byte; /* Byte for this node. NUL for word end */
2439 char_u wn_flags; /* when wn_byte is NUL: WF_ flags */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002440 short wn_region; /* when wn_byte is NUL: region mask; for
2441 PREFIXTREE it's the prefcondnr */
2442 char_u wn_prefixID; /* supported/required prefix ID or 0 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002443};
2444
Bram Moolenaar51485f02005-06-04 21:55:20 +00002445#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002446
Bram Moolenaar51485f02005-06-04 21:55:20 +00002447/*
2448 * Info used while reading the spell files.
2449 */
2450typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002451{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002452 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00002453 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002454 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00002455 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002456 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002457 sblock_T *si_blocks; /* memory blocks used */
2458 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002459 int si_add; /* addition file */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002460 int si_region; /* region mask */
2461 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00002462 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002463 int si_verbose; /* verbose messages */
Bram Moolenaar3982c542005-06-08 21:56:31 +00002464 int si_region_count; /* number of regions supported (1 when there
2465 are no regions) */
2466 char_u si_region_name[16]; /* region names (if count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002467
2468 garray_T si_rep; /* list of fromto_T entries from REP lines */
2469 garray_T si_sal; /* list of fromto_T entries from SAL lines */
2470 int si_followup; /* soundsalike: ? */
2471 int si_collapse; /* soundsalike: ? */
2472 int si_rem_accents; /* soundsalike: remove accents */
2473 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002474 garray_T si_prefcond; /* table with conditions for postponed
2475 * prefixes, each stored as a string */
2476 int si_newID; /* current value for ah_newID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002477} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002478
Bram Moolenaar51485f02005-06-04 21:55:20 +00002479static afffile_T *spell_read_aff __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002480static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002481static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
2482static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00002483static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00002484static void spell_free_aff __ARGS((afffile_T *aff));
2485static int spell_read_dic __ARGS((char_u *fname, spellinfo_T *spin, afffile_T *affile));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002486static char_u *get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, sblock_T **blp));
2487static 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 +00002488static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin));
2489static void *getroom __ARGS((sblock_T **blp, size_t len));
2490static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s));
2491static void free_blocks __ARGS((sblock_T *bl));
2492static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002493static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags, int region, char_u *pfxlist));
2494static 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 +00002495static void wordtree_compress __ARGS((wordnode_T *root, spellinfo_T *spin));
Bram Moolenaar51485f02005-06-04 21:55:20 +00002496static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot));
2497static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar3982c542005-06-08 21:56:31 +00002498static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar0c405862005-06-22 22:26:26 +00002499static void clear_node __ARGS((wordnode_T *node));
2500static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002501static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00002502static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002503
2504/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002505 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00002506 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002507 */
2508 static afffile_T *
Bram Moolenaar51485f02005-06-04 21:55:20 +00002509spell_read_aff(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002510 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002511 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002512{
2513 FILE *fd;
2514 afffile_T *aff;
2515 char_u rline[MAXLINELEN];
2516 char_u *line;
2517 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00002518#define MAXITEMCNT 7
2519 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002520 int itemcnt;
2521 char_u *p;
2522 int lnum = 0;
2523 affheader_T *cur_aff = NULL;
2524 int aff_todo = 0;
2525 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002526 char_u *low = NULL;
2527 char_u *fol = NULL;
2528 char_u *upp = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002529 static char *e_affname = N_("Affix name too long in %s line %d: %s");
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002530 int do_rep;
2531 int do_sal;
2532 int do_map;
2533 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002534 hashitem_T *hi;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002535
Bram Moolenaar51485f02005-06-04 21:55:20 +00002536 /*
2537 * Open the file.
2538 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002539 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002540 if (fd == NULL)
2541 {
2542 EMSG2(_(e_notopen), fname);
2543 return NULL;
2544 }
2545
Bram Moolenaarb765d632005-06-07 21:00:02 +00002546 if (spin->si_verbose || p_verbose > 2)
2547 {
2548 if (!spin->si_verbose)
2549 verbose_enter();
2550 smsg((char_u *)_("Reading affix file %s..."), fname);
2551 out_flush();
2552 if (!spin->si_verbose)
2553 verbose_leave();
2554 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002555
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002556 /* Only do REP lines when not done in another .aff file already. */
2557 do_rep = spin->si_rep.ga_len == 0;
2558
2559 /* Only do SAL lines when not done in another .aff file already. */
2560 do_sal = spin->si_sal.ga_len == 0;
2561
2562 /* Only do MAP lines when not done in another .aff file already. */
2563 do_map = spin->si_map.ga_len == 0;
2564
Bram Moolenaar51485f02005-06-04 21:55:20 +00002565 /*
2566 * Allocate and init the afffile_T structure.
2567 */
2568 aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002569 if (aff == NULL)
2570 return NULL;
2571 hash_init(&aff->af_pref);
2572 hash_init(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002573
2574 /*
2575 * Read all the lines in the file one by one.
2576 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002577 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002578 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002579 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002580 ++lnum;
2581
2582 /* Skip comment lines. */
2583 if (*rline == '#')
2584 continue;
2585
2586 /* Convert from "SET" to 'encoding' when needed. */
2587 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002588#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00002589 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002590 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002591 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002592 if (pc == NULL)
2593 {
2594 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
2595 fname, lnum, rline);
2596 continue;
2597 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002598 line = pc;
2599 }
2600 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00002601#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002602 {
2603 pc = NULL;
2604 line = rline;
2605 }
2606
2607 /* Split the line up in white separated items. Put a NUL after each
2608 * item. */
2609 itemcnt = 0;
2610 for (p = line; ; )
2611 {
2612 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
2613 ++p;
2614 if (*p == NUL)
2615 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00002616 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002617 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002618 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002619 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002620 ++p;
2621 if (*p == NUL)
2622 break;
2623 *p++ = NUL;
2624 }
2625
2626 /* Handle non-empty lines. */
2627 if (itemcnt > 0)
2628 {
2629 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
2630 && aff->af_enc == NULL)
2631 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00002632#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00002633 /* Setup for conversion from "ENC" to 'encoding'. */
2634 aff->af_enc = enc_canonize(items[1]);
2635 if (aff->af_enc != NULL && !spin->si_ascii
2636 && convert_setup(&spin->si_conv, aff->af_enc,
2637 p_enc) == FAIL)
2638 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
2639 fname, aff->af_enc, p_enc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002640#else
2641 smsg((char_u *)_("Conversion in %s not supported"), fname);
2642#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002643 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00002644 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
2645 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002646 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00002647 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002648 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002649 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002650 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002651 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002652 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
2653 && aff->af_rar == 0)
2654 {
2655 aff->af_rar = items[1][0];
2656 if (items[1][1] != NUL)
2657 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
2658 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002659 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
2660 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002661 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00002662 aff->af_kep = items[1][0];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002663 if (items[1][1] != NUL)
2664 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
2665 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00002666 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
2667 && aff->af_bad == 0)
2668 {
2669 aff->af_bad = items[1][0];
2670 if (items[1][1] != NUL)
2671 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
2672 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002673 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
2674 {
2675 aff->af_pfxpostpone = TRUE;
2676 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002677 else if ((STRCMP(items[0], "PFX") == 0
2678 || STRCMP(items[0], "SFX") == 0)
2679 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00002680 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002681 {
Bram Moolenaar8db73182005-06-17 21:51:16 +00002682 /* Myspell allows extra text after the item, but that might
2683 * mean mistakes go unnoticed. Require a comment-starter. */
2684 if (itemcnt > 4 && *items[4] != '#')
2685 smsg((char_u *)_("Trailing text in %s line %d: %s"),
2686 fname, lnum, items[4]);
2687
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002688 /* New affix letter. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002689 cur_aff = (affheader_T *)getroom(&spin->si_blocks,
2690 sizeof(affheader_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002691 if (cur_aff == NULL)
2692 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002693 cur_aff->ah_key[0] = *items[1]; /* TODO: multi-byte? */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002694 cur_aff->ah_key[1] = NUL;
2695 if (items[1][1] != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002696 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002697 if (*items[2] == 'Y')
2698 cur_aff->ah_combine = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002699 else if (*items[2] != 'N')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002700 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
2701 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002702
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002703 if (*items[0] == 'P')
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002704 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002705 tp = &aff->af_pref;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002706 /* Use a new number in the .spl file later, to be able to
2707 * handle multiple .aff files. */
2708 if (aff->af_pfxpostpone)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002709 cur_aff->ah_newID = ++spin->si_newID;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002710 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002711 else
2712 tp = &aff->af_suff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002713 aff_todo = atoi((char *)items[3]);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002714 hi = hash_find(tp, cur_aff->ah_key);
2715 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar51485f02005-06-04 21:55:20 +00002716 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002717 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
2718 fname, lnum, items[1]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002719 aff_todo = 0;
2720 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002721 else
2722 hash_add(tp, cur_aff->ah_key);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002723 }
2724 else if ((STRCMP(items[0], "PFX") == 0
2725 || STRCMP(items[0], "SFX") == 0)
2726 && aff_todo > 0
2727 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00002728 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002729 {
2730 affentry_T *aff_entry;
2731
Bram Moolenaar8db73182005-06-17 21:51:16 +00002732 /* Myspell allows extra text after the item, but that might
2733 * mean mistakes go unnoticed. Require a comment-starter. */
2734 if (itemcnt > 5 && *items[5] != '#')
2735 smsg((char_u *)_("Trailing text in %s line %d: %s"),
2736 fname, lnum, items[5]);
2737
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002738 /* New item for an affix letter. */
2739 --aff_todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002740 aff_entry = (affentry_T *)getroom(&spin->si_blocks,
2741 sizeof(affentry_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002742 if (aff_entry == NULL)
2743 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00002744
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002745 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002746 aff_entry->ae_chop = getroom_save(&spin->si_blocks,
2747 items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002748 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002749 aff_entry->ae_add = getroom_save(&spin->si_blocks,
2750 items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002751
Bram Moolenaar51485f02005-06-04 21:55:20 +00002752 /* Don't use an affix entry with non-ASCII characters when
2753 * "spin->si_ascii" is TRUE. */
2754 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00002755 || has_non_ascii(aff_entry->ae_add)))
2756 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00002757 aff_entry->ae_next = cur_aff->ah_first;
2758 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002759
2760 if (STRCMP(items[4], ".") != 0)
2761 {
2762 char_u buf[MAXLINELEN];
2763
2764 aff_entry->ae_cond = getroom_save(&spin->si_blocks,
2765 items[4]);
2766 if (*items[0] == 'P')
2767 sprintf((char *)buf, "^%s", items[4]);
2768 else
2769 sprintf((char *)buf, "%s$", items[4]);
2770 aff_entry->ae_prog = vim_regcomp(buf,
2771 RE_MAGIC + RE_STRING);
2772 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002773
2774 /* For postponed prefixes we need an entry in si_prefcond
2775 * for the condition. Use an existing one if possible. */
2776 if (*items[0] == 'P' && aff->af_pfxpostpone
2777 && aff_entry->ae_chop == NULL)
2778 {
2779 int idx;
2780 char_u **pp;
2781
2782 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
2783 --idx)
2784 {
2785 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
2786 if (str_equal(p, aff_entry->ae_cond))
2787 break;
2788 }
2789 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
2790 {
2791 /* Not found, add a new condition. */
2792 idx = spin->si_prefcond.ga_len++;
2793 pp = ((char_u **)spin->si_prefcond.ga_data) + idx;
2794 if (aff_entry->ae_cond == NULL)
2795 *pp = NULL;
2796 else
2797 *pp = getroom_save(&spin->si_blocks,
2798 aff_entry->ae_cond);
2799 }
2800
2801 /* Add the prefix to the prefix tree. */
2802 if (aff_entry->ae_add == NULL)
2803 p = (char_u *)"";
2804 else
2805 p = aff_entry->ae_add;
2806 tree_add_word(p, spin->si_prefroot, -1, idx,
2807 cur_aff->ah_newID, &spin->si_blocks);
2808 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00002809 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002810 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002811 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
2812 {
2813 if (fol != NULL)
2814 smsg((char_u *)_("Duplicate FOL in %s line %d"),
2815 fname, lnum);
2816 else
2817 fol = vim_strsave(items[1]);
2818 }
2819 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
2820 {
2821 if (low != NULL)
2822 smsg((char_u *)_("Duplicate LOW in %s line %d"),
2823 fname, lnum);
2824 else
2825 low = vim_strsave(items[1]);
2826 }
2827 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
2828 {
2829 if (upp != NULL)
2830 smsg((char_u *)_("Duplicate UPP in %s line %d"),
2831 fname, lnum);
2832 else
2833 upp = vim_strsave(items[1]);
2834 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002835 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002836 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002837 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002838 if (!isdigit(*items[1]))
2839 smsg((char_u *)_("Expected REP count in %s line %d"),
2840 fname, lnum);
2841 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002842 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
2843 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002844 /* REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002845 if (do_rep)
2846 add_fromto(spin, &spin->si_rep, items[1], items[2]);
2847 }
2848 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
2849 {
2850 /* MAP item or count */
2851 if (!found_map)
2852 {
2853 /* First line contains the count. */
2854 found_map = TRUE;
2855 if (!isdigit(*items[1]))
2856 smsg((char_u *)_("Expected MAP count in %s line %d"),
2857 fname, lnum);
2858 }
2859 else if (do_map)
2860 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00002861 int c;
2862
2863 /* Check that every character appears only once. */
2864 for (p = items[1]; *p != NUL; )
2865 {
2866#ifdef FEAT_MBYTE
2867 c = mb_ptr2char_adv(&p);
2868#else
2869 c = *p++;
2870#endif
2871 if ((spin->si_map.ga_len > 0
2872 && vim_strchr(spin->si_map.ga_data, c)
2873 != NULL)
2874 || vim_strchr(p, c) != NULL)
2875 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
2876 fname, lnum);
2877 }
2878
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002879 /* We simply concatenate all the MAP strings, separated by
2880 * slashes. */
2881 ga_concat(&spin->si_map, items[1]);
2882 ga_append(&spin->si_map, '/');
2883 }
2884 }
2885 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
2886 {
2887 if (do_sal)
2888 {
2889 /* SAL item (sounds-a-like)
2890 * Either one of the known keys or a from-to pair. */
2891 if (STRCMP(items[1], "followup") == 0)
2892 spin->si_followup = sal_to_bool(items[2]);
2893 else if (STRCMP(items[1], "collapse_result") == 0)
2894 spin->si_collapse = sal_to_bool(items[2]);
2895 else if (STRCMP(items[1], "remove_accents") == 0)
2896 spin->si_rem_accents = sal_to_bool(items[2]);
2897 else
2898 /* when "to" is "_" it means empty */
2899 add_fromto(spin, &spin->si_sal, items[1],
2900 STRCMP(items[2], "_") == 0 ? (char_u *)""
2901 : items[2]);
2902 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002903 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002904 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002905 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
2906 fname, lnum, items[0]);
2907 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002908 }
2909
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002910 if (fol != NULL || low != NULL || upp != NULL)
2911 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002912 /*
2913 * Don't write a word table for an ASCII file, so that we don't check
2914 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002915 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00002916 * mb_get_class(), the list of chars in the file will be incomplete.
2917 */
2918 if (!spin->si_ascii
2919#ifdef FEAT_MBYTE
2920 && !enc_utf8
2921#endif
2922 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00002923 {
2924 if (fol == NULL || low == NULL || upp == NULL)
2925 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
2926 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00002927 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00002928 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002929
2930 vim_free(fol);
2931 vim_free(low);
2932 vim_free(upp);
2933 }
2934
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002935 vim_free(pc);
2936 fclose(fd);
2937 return aff;
2938}
2939
2940/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002941 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
2942 * NULL as equal.
2943 */
2944 static int
2945str_equal(s1, s2)
2946 char_u *s1;
2947 char_u *s2;
2948{
2949 if (s1 == NULL || s2 == NULL)
2950 return s1 == s2;
2951 return STRCMP(s1, s2) == 0;
2952}
2953
2954/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002955 * Add a from-to item to "gap". Used for REP and SAL items.
2956 * They are stored case-folded.
2957 */
2958 static void
2959add_fromto(spin, gap, from, to)
2960 spellinfo_T *spin;
2961 garray_T *gap;
2962 char_u *from;
2963 char_u *to;
2964{
2965 fromto_T *ftp;
2966 char_u word[MAXWLEN];
2967
2968 if (ga_grow(gap, 1) == OK)
2969 {
2970 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
2971 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
2972 ftp->ft_from = getroom_save(&spin->si_blocks, word);
2973 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
2974 ftp->ft_to = getroom_save(&spin->si_blocks, word);
2975 ++gap->ga_len;
2976 }
2977}
2978
2979/*
2980 * Convert a boolean argument in a SAL line to TRUE or FALSE;
2981 */
2982 static int
2983sal_to_bool(s)
2984 char_u *s;
2985{
2986 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
2987}
2988
2989/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00002990 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
2991 * When "s" is NULL FALSE is returned.
2992 */
2993 static int
2994has_non_ascii(s)
2995 char_u *s;
2996{
2997 char_u *p;
2998
2999 if (s != NULL)
3000 for (p = s; *p != NUL; ++p)
3001 if (*p >= 128)
3002 return TRUE;
3003 return FALSE;
3004}
3005
3006/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003007 * Free the structure filled by spell_read_aff().
3008 */
3009 static void
3010spell_free_aff(aff)
3011 afffile_T *aff;
3012{
3013 hashtab_T *ht;
3014 hashitem_T *hi;
3015 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003016 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003017 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003018
3019 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003020
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003021 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003022 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
3023 {
3024 todo = ht->ht_used;
3025 for (hi = ht->ht_array; todo > 0; ++hi)
3026 {
3027 if (!HASHITEM_EMPTY(hi))
3028 {
3029 --todo;
3030 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003031 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3032 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003033 }
3034 }
3035 if (ht == &aff->af_suff)
3036 break;
3037 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003038
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003039 hash_clear(&aff->af_pref);
3040 hash_clear(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003041}
3042
3043/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003044 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003045 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003046 */
3047 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00003048spell_read_dic(fname, spin, affile)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003049 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003050 spellinfo_T *spin;
3051 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003052{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003053 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003054 char_u line[MAXLINELEN];
Bram Moolenaar51485f02005-06-04 21:55:20 +00003055 char_u *afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003056 char_u *pfxlist;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003057 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003058 char_u *pc;
3059 char_u *w;
3060 int l;
3061 hash_T hash;
3062 hashitem_T *hi;
3063 FILE *fd;
3064 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003065 int non_ascii = 0;
3066 int retval = OK;
3067 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003068 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003069
Bram Moolenaar51485f02005-06-04 21:55:20 +00003070 /*
3071 * Open the file.
3072 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003073 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003074 if (fd == NULL)
3075 {
3076 EMSG2(_(e_notopen), fname);
3077 return FAIL;
3078 }
3079
Bram Moolenaar51485f02005-06-04 21:55:20 +00003080 /* The hashtable is only used to detect duplicated words. */
3081 hash_init(&ht);
3082
Bram Moolenaar8db73182005-06-17 21:51:16 +00003083 spin->si_foldwcount = 0;
3084 spin->si_keepwcount = 0;
3085
Bram Moolenaarb765d632005-06-07 21:00:02 +00003086 if (spin->si_verbose || p_verbose > 2)
3087 {
3088 if (!spin->si_verbose)
3089 verbose_enter();
3090 smsg((char_u *)_("Reading dictionary file %s..."), fname);
3091 out_flush();
3092 if (!spin->si_verbose)
3093 verbose_leave();
3094 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003095
3096 /* Read and ignore the first line: word count. */
3097 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003098 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003099 EMSG2(_("E760: No word count in %s"), fname);
3100
3101 /*
3102 * Read all the lines in the file one by one.
3103 * The words are converted to 'encoding' here, before being added to
3104 * the hashtable.
3105 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003106 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003107 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003108 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003109 ++lnum;
3110
Bram Moolenaar51485f02005-06-04 21:55:20 +00003111 /* Remove CR, LF and white space from the end. White space halfway
3112 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003113 l = STRLEN(line);
3114 while (l > 0 && line[l - 1] <= ' ')
3115 --l;
3116 if (l == 0)
3117 continue; /* empty line */
3118 line[l] = NUL;
3119
Bram Moolenaar51485f02005-06-04 21:55:20 +00003120 /* Find the optional affix names. */
3121 afflist = vim_strchr(line, '/');
3122 if (afflist != NULL)
3123 *afflist++ = NUL;
3124
3125 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
3126 if (spin->si_ascii && has_non_ascii(line))
3127 {
3128 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003129 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003130 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003131
Bram Moolenaarb765d632005-06-07 21:00:02 +00003132#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003133 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003134 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003135 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003136 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003137 if (pc == NULL)
3138 {
3139 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3140 fname, lnum, line);
3141 continue;
3142 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003143 w = pc;
3144 }
3145 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003146#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003147 {
3148 pc = NULL;
3149 w = line;
3150 }
3151
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003152 /* This takes time, print a message now and then. */
3153 if (spin->si_verbose && (lnum & 0x3ff) == 0)
3154 {
3155 vim_snprintf((char *)message, sizeof(message),
3156 _("line %6d, word %6d - %s"),
3157 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
3158 msg_start();
3159 msg_puts_long_attr(message, 0);
3160 msg_clr_eos();
3161 msg_didout = FALSE;
3162 msg_col = 0;
3163 out_flush();
3164 }
3165
Bram Moolenaar51485f02005-06-04 21:55:20 +00003166 /* Store the word in the hashtable to be able to find duplicates. */
3167 dw = (char_u *)getroom_save(&spin->si_blocks, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003168 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003169 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003170 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003171 if (retval == FAIL)
3172 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003173
Bram Moolenaar51485f02005-06-04 21:55:20 +00003174 hash = hash_hash(dw);
3175 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003176 if (!HASHITEM_EMPTY(hi))
3177 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003178 fname, lnum, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003179 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00003180 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003181
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003182 flags = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003183 pfxlist = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003184 if (afflist != NULL)
3185 {
3186 /* Check for affix name that stands for keep-case word and stands
3187 * for rare word (if defined). */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003188 if (affile->af_kep != NUL
3189 && vim_strchr(afflist, affile->af_kep) != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003190 flags |= WF_KEEPCAP;
3191 if (affile->af_rar != NUL
3192 && vim_strchr(afflist, affile->af_rar) != NULL)
3193 flags |= WF_RARE;
Bram Moolenaar0c405862005-06-22 22:26:26 +00003194 if (affile->af_bad != NUL
3195 && vim_strchr(afflist, affile->af_bad) != NULL)
3196 flags |= WF_BANNED;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003197
3198 if (affile->af_pfxpostpone)
3199 /* Need to store the list of prefix IDs with the word. */
3200 pfxlist = get_pfxlist(affile, afflist, &spin->si_blocks);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003201 }
3202
Bram Moolenaar51485f02005-06-04 21:55:20 +00003203 /* Add the word to the word tree(s). */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003204 if (store_word(dw, spin, flags, spin->si_region, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003205 retval = FAIL;
3206
3207 if (afflist != NULL)
3208 {
3209 /* Find all matching suffixes and add the resulting words.
3210 * Additionally do matching prefixes that combine. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003211 if (store_aff_word(dw, spin, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003212 &affile->af_suff, &affile->af_pref,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003213 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003214 retval = FAIL;
3215
3216 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003217 if (store_aff_word(dw, spin, afflist, affile,
3218 &affile->af_pref, NULL,
3219 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003220 retval = FAIL;
3221 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003222 }
3223
Bram Moolenaar51485f02005-06-04 21:55:20 +00003224 if (spin->si_ascii && non_ascii > 0)
3225 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
3226 non_ascii);
3227 hash_clear(&ht);
3228
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003229 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003230 return retval;
3231}
3232
3233/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003234 * Get the list of prefix IDs from the affix list "afflist".
3235 * Used for PFXPOSTPONE.
3236 * Returns a string allocated with getroom(). NULL when there are no prefixes
3237 * or when out of memory.
3238 */
3239 static char_u *
3240get_pfxlist(affile, afflist, blp)
3241 afffile_T *affile;
3242 char_u *afflist;
3243 sblock_T **blp;
3244{
3245 char_u *p;
3246 int cnt;
3247 int round;
3248 char_u *res = NULL;
3249 char_u key[2];
3250 hashitem_T *hi;
3251
3252 key[1] = NUL;
3253
3254 /* round 1: count the number of prefix IDs.
3255 * round 2: move prefix IDs to "res" */
3256 for (round = 1; round <= 2; ++round)
3257 {
3258 cnt = 0;
3259 for (p = afflist; *p != NUL; ++p)
3260 {
3261 key[0] = *p;
3262 hi = hash_find(&affile->af_pref, key);
3263 if (!HASHITEM_EMPTY(hi))
3264 {
3265 /* This is a prefix ID, use the new number. */
3266 if (round == 2)
3267 res[cnt] = HI2AH(hi)->ah_newID;
3268 ++cnt;
3269 }
3270 }
3271 if (round == 1 && cnt > 0)
3272 res = getroom(blp, cnt + 1);
3273 if (res == NULL)
3274 break;
3275 }
3276
3277 if (res != NULL)
3278 res[cnt] = NUL;
3279 return res;
3280}
3281
3282/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003283 * Apply affixes to a word and store the resulting words.
3284 * "ht" is the hashtable with affentry_T that need to be applied, either
3285 * prefixes or suffixes.
3286 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
3287 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003288 *
3289 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003290 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003291 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003292store_aff_word(word, spin, afflist, affile, ht, xht, comb, flags, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003293 char_u *word; /* basic word start */
3294 spellinfo_T *spin; /* spell info */
3295 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003296 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003297 hashtab_T *ht;
3298 hashtab_T *xht;
3299 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003300 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003301 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003302{
3303 int todo;
3304 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003305 affheader_T *ah;
3306 affentry_T *ae;
3307 regmatch_T regmatch;
3308 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003309 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003310 int i;
3311 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003312
Bram Moolenaar51485f02005-06-04 21:55:20 +00003313 todo = ht->ht_used;
3314 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003315 {
3316 if (!HASHITEM_EMPTY(hi))
3317 {
3318 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003319 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00003320
Bram Moolenaar51485f02005-06-04 21:55:20 +00003321 /* Check that the affix combines, if required, and that the word
3322 * supports this affix. */
3323 if ((!comb || ah->ah_combine)
3324 && vim_strchr(afflist, *ah->ah_key) != NULL)
Bram Moolenaar5482f332005-04-17 20:18:43 +00003325 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003326 /* Loop over all affix entries with this name. */
3327 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003328 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003329 /* Check the condition. It's not logical to match case
3330 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003331 * Myspell.
3332 * For prefixes, when "PFXPOSTPONE" was used, only do
3333 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003334 regmatch.regprog = ae->ae_prog;
3335 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003336 if ((xht != NULL || !affile->af_pfxpostpone
3337 || ae->ae_chop != NULL)
3338 && (ae->ae_prog == NULL
3339 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003340 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003341 /* Match. Remove the chop and add the affix. */
3342 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003343 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003344 /* prefix: chop/add at the start of the word */
3345 if (ae->ae_add == NULL)
3346 *newword = NUL;
3347 else
3348 STRCPY(newword, ae->ae_add);
3349 p = word;
3350 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003351 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003352 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003353#ifdef FEAT_MBYTE
3354 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003355 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003356 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003357 for ( ; i > 0; --i)
3358 mb_ptr_adv(p);
3359 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00003360 else
3361#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003362 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003363 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003364 STRCAT(newword, p);
3365 }
3366 else
3367 {
3368 /* suffix: chop/add at the end of the word */
3369 STRCPY(newword, word);
3370 if (ae->ae_chop != NULL)
3371 {
3372 /* Remove chop string. */
3373 p = newword + STRLEN(newword);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003374#ifdef FEAT_MBYTE
3375 if (has_mbyte)
3376 i = mb_charlen(ae->ae_chop);
3377 else
3378#endif
3379 i = STRLEN(ae->ae_chop);
3380 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003381 mb_ptr_back(newword, p);
3382 *p = NUL;
3383 }
3384 if (ae->ae_add != NULL)
3385 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003386 }
3387
Bram Moolenaar51485f02005-06-04 21:55:20 +00003388 /* Store the modified word. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003389 if (store_word(newword, spin,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003390 flags, spin->si_region, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003391 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003392
Bram Moolenaar51485f02005-06-04 21:55:20 +00003393 /* When added a suffix and combining is allowed also
3394 * try adding prefixes additionally. */
3395 if (xht != NULL && ah->ah_combine)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003396 if (store_aff_word(newword, spin, afflist, affile,
3397 xht, NULL, TRUE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003398 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003399 }
3400 }
3401 }
3402 }
3403 }
3404
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003405 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003406}
3407
3408/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003409 * Read a file with a list of words.
3410 */
3411 static int
3412spell_read_wordfile(fname, spin)
3413 char_u *fname;
3414 spellinfo_T *spin;
3415{
3416 FILE *fd;
3417 long lnum = 0;
3418 char_u rline[MAXLINELEN];
3419 char_u *line;
3420 char_u *pc = NULL;
3421 int l;
3422 int retval = OK;
3423 int did_word = FALSE;
3424 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003425 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003426 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003427
3428 /*
3429 * Open the file.
3430 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003431 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00003432 if (fd == NULL)
3433 {
3434 EMSG2(_(e_notopen), fname);
3435 return FAIL;
3436 }
3437
Bram Moolenaarb765d632005-06-07 21:00:02 +00003438 if (spin->si_verbose || p_verbose > 2)
3439 {
3440 if (!spin->si_verbose)
3441 verbose_enter();
3442 smsg((char_u *)_("Reading word file %s..."), fname);
3443 out_flush();
3444 if (!spin->si_verbose)
3445 verbose_leave();
3446 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003447
3448 /*
3449 * Read all the lines in the file one by one.
3450 */
3451 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
3452 {
3453 line_breakcheck();
3454 ++lnum;
3455
3456 /* Skip comment lines. */
3457 if (*rline == '#')
3458 continue;
3459
3460 /* Remove CR, LF and white space from the end. */
3461 l = STRLEN(rline);
3462 while (l > 0 && rline[l - 1] <= ' ')
3463 --l;
3464 if (l == 0)
3465 continue; /* empty or blank line */
3466 rline[l] = NUL;
3467
3468 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
3469 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003470#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003471 if (spin->si_conv.vc_type != CONV_NONE)
3472 {
3473 pc = string_convert(&spin->si_conv, rline, NULL);
3474 if (pc == NULL)
3475 {
3476 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3477 fname, lnum, rline);
3478 continue;
3479 }
3480 line = pc;
3481 }
3482 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003483#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00003484 {
3485 pc = NULL;
3486 line = rline;
3487 }
3488
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003489 flags = 0;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003490 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003491
3492 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00003493 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003494 ++line;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003495
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003496 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003497 {
3498 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00003499 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
3500 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003501 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00003502 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
3503 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003504 else
3505 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003506#ifdef FEAT_MBYTE
3507 char_u *enc;
3508
Bram Moolenaar51485f02005-06-04 21:55:20 +00003509 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003510 line += 10;
3511 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003512 if (enc != NULL && !spin->si_ascii
3513 && convert_setup(&spin->si_conv, enc,
3514 p_enc) == FAIL)
3515 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00003516 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003517 vim_free(enc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003518#else
3519 smsg((char_u *)_("Conversion in %s not supported"), fname);
3520#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00003521 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003522 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003523 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003524
Bram Moolenaar3982c542005-06-08 21:56:31 +00003525 if (STRNCMP(line, "regions=", 8) == 0)
3526 {
3527 if (spin->si_region_count > 1)
3528 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
3529 fname, lnum, line);
3530 else
3531 {
3532 line += 8;
3533 if (STRLEN(line) > 16)
3534 smsg((char_u *)_("Too many regions in %s line %d: %s"),
3535 fname, lnum, line);
3536 else
3537 {
3538 spin->si_region_count = STRLEN(line) / 2;
3539 STRCPY(spin->si_region_name, line);
3540 }
3541 }
3542 continue;
3543 }
3544
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003545 if (*line == '=')
3546 {
3547 /* keep-case word */
3548 flags |= WF_KEEPCAP;
3549 ++line;
3550 }
3551
3552 if (*line == '!')
3553 {
3554 /* Bad, bad, wicked word. */
3555 flags |= WF_BANNED;
3556 ++line;
3557 }
3558 else if (*line == '?')
3559 {
3560 /* Rare word. */
3561 flags |= WF_RARE;
3562 ++line;
3563 }
3564
Bram Moolenaar3982c542005-06-08 21:56:31 +00003565 if (VIM_ISDIGIT(*line))
3566 {
3567 /* region number(s) */
3568 regionmask = 0;
3569 while (VIM_ISDIGIT(*line))
3570 {
3571 l = *line - '0';
3572 if (l > spin->si_region_count)
3573 {
3574 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
3575 fname, lnum, line);
3576 break;
3577 }
3578 regionmask |= 1 << (l - 1);
3579 ++line;
3580 }
3581 flags |= WF_REGION;
3582 }
3583
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003584 if (flags == 0)
3585 {
3586 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
Bram Moolenaar51485f02005-06-04 21:55:20 +00003587 fname, lnum, line);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003588 continue;
3589 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003590 }
3591
3592 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
3593 if (spin->si_ascii && has_non_ascii(line))
3594 {
3595 ++non_ascii;
3596 continue;
3597 }
3598
3599 /* Normal word: store it. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003600 if (store_word(line, spin, flags, regionmask, NULL) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003601 {
3602 retval = FAIL;
3603 break;
3604 }
3605 did_word = TRUE;
3606 }
3607
3608 vim_free(pc);
3609 fclose(fd);
3610
Bram Moolenaarb765d632005-06-07 21:00:02 +00003611 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
3612 {
3613 if (p_verbose > 2)
3614 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00003615 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
3616 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003617 if (p_verbose > 2)
3618 verbose_leave();
3619 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003620 return retval;
3621}
3622
3623/*
3624 * Get part of an sblock_T, "len" bytes long.
3625 * This avoids calling free() for every little struct we use.
3626 * The memory is cleared to all zeros.
3627 * Returns NULL when out of memory.
3628 */
3629 static void *
3630getroom(blp, len)
3631 sblock_T **blp;
3632 size_t len; /* length needed */
3633{
3634 char_u *p;
3635 sblock_T *bl = *blp;
3636
3637 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
3638 {
3639 /* Allocate a block of memory. This is not freed until much later. */
3640 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
3641 if (bl == NULL)
3642 return NULL;
3643 bl->sb_next = *blp;
3644 *blp = bl;
3645 bl->sb_used = 0;
3646 }
3647
3648 p = bl->sb_data + bl->sb_used;
3649 bl->sb_used += len;
3650
3651 return p;
3652}
3653
3654/*
3655 * Make a copy of a string into memory allocated with getroom().
3656 */
3657 static char_u *
3658getroom_save(blp, s)
3659 sblock_T **blp;
3660 char_u *s;
3661{
3662 char_u *sc;
3663
3664 sc = (char_u *)getroom(blp, STRLEN(s) + 1);
3665 if (sc != NULL)
3666 STRCPY(sc, s);
3667 return sc;
3668}
3669
3670
3671/*
3672 * Free the list of allocated sblock_T.
3673 */
3674 static void
3675free_blocks(bl)
3676 sblock_T *bl;
3677{
3678 sblock_T *next;
3679
3680 while (bl != NULL)
3681 {
3682 next = bl->sb_next;
3683 vim_free(bl);
3684 bl = next;
3685 }
3686}
3687
3688/*
3689 * Allocate the root of a word tree.
3690 */
3691 static wordnode_T *
3692wordtree_alloc(blp)
3693 sblock_T **blp;
3694{
3695 return (wordnode_T *)getroom(blp, sizeof(wordnode_T));
3696}
3697
3698/*
3699 * Store a word in the tree(s).
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003700 * Always store it in the case-folded tree. A keep-case word can also be used
3701 * with all caps.
Bram Moolenaar51485f02005-06-04 21:55:20 +00003702 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003703 * When "pfxlist" is not NULL store the word for each prefix ID.
Bram Moolenaar51485f02005-06-04 21:55:20 +00003704 */
3705 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003706store_word(word, spin, flags, region, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003707 char_u *word;
3708 spellinfo_T *spin;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003709 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003710 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003711 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003712{
3713 int len = STRLEN(word);
3714 int ct = captype(word, word + len);
3715 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003716 int res = OK;
3717 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003718
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003719 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003720 for (p = pfxlist; res == OK; ++p)
3721 {
3722 res = tree_add_word(foldword, spin->si_foldroot, ct | flags,
3723 region, p == NULL ? 0 : *p, &spin->si_blocks);
3724 if (p == NULL || *p == NUL)
3725 break;
3726 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00003727 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003728
3729 if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP))
Bram Moolenaar8db73182005-06-17 21:51:16 +00003730 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003731 for (p = pfxlist; res == OK; ++p)
3732 {
3733 res = tree_add_word(word, spin->si_keeproot, flags,
3734 region, p == NULL ? 0 : *p, &spin->si_blocks);
3735 if (p == NULL || *p == NUL)
3736 break;
3737 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00003738 ++spin->si_keepwcount;
3739 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003740 return res;
3741}
3742
3743/*
3744 * Add word "word" to a word tree at "root".
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003745 * When "flags" is -1 we are adding to the prefix tree where flags don't
3746 * matter and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003747 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003748 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003749 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003750tree_add_word(word, root, flags, region, prefixID, blp)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003751 char_u *word;
3752 wordnode_T *root;
3753 int flags;
3754 int region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003755 int prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003756 sblock_T **blp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003757{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003758 wordnode_T *node = root;
3759 wordnode_T *np;
3760 wordnode_T **prev = NULL;
3761 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003762
Bram Moolenaar51485f02005-06-04 21:55:20 +00003763 /* Add each byte of the word to the tree, including the NUL at the end. */
3764 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003765 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003766 /* Look for the sibling that has the same character. They are sorted
3767 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003768 * higher byte value. For zero bytes (end of word) the sorting is
3769 * done on flags and then on prefixID
Bram Moolenaar51485f02005-06-04 21:55:20 +00003770 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003771 while (node != NULL
3772 && (node->wn_byte < word[i]
3773 || (node->wn_byte == NUL
3774 && (flags < 0
3775 ? node->wn_prefixID < prefixID
3776 : node->wn_flags < (flags & 0xff)
3777 || (node->wn_flags == (flags & 0xff)
3778 && node->wn_prefixID < prefixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003779 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003780 prev = &node->wn_sibling;
3781 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003782 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003783 if (node == NULL
3784 || node->wn_byte != word[i]
3785 || (word[i] == NUL
3786 && (flags < 0
3787 || node->wn_flags != (flags & 0xff)
3788 || node->wn_prefixID != prefixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003789 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003790 /* Allocate a new node. */
3791 np = (wordnode_T *)getroom(blp, sizeof(wordnode_T));
3792 if (np == NULL)
3793 return FAIL;
3794 np->wn_byte = word[i];
3795 *prev = np;
3796 np->wn_sibling = node;
3797 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003798 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003799
Bram Moolenaar51485f02005-06-04 21:55:20 +00003800 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003801 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003802 node->wn_flags = flags;
3803 node->wn_region |= region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003804 node->wn_prefixID = prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003805 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003806 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003807 prev = &node->wn_child;
3808 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003809 }
3810
3811 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003812}
3813
3814/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003815 * Compress a tree: find tails that are identical and can be shared.
3816 */
3817 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00003818wordtree_compress(root, spin)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003819 wordnode_T *root;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003820 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003821{
3822 hashtab_T ht;
3823 int n;
3824 int tot = 0;
3825
3826 if (root != NULL)
3827 {
3828 hash_init(&ht);
3829 n = node_compress(root, &ht, &tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003830 if (spin->si_verbose || p_verbose > 2)
3831 {
3832 if (!spin->si_verbose)
3833 verbose_enter();
3834 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar51485f02005-06-04 21:55:20 +00003835 n, tot, (tot - n) * 100 / tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003836 if (p_verbose > 2)
3837 verbose_leave();
3838 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003839 hash_clear(&ht);
3840 }
3841}
3842
3843/*
3844 * Compress a node, its siblings and its children, depth first.
3845 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003846 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003847 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00003848node_compress(node, ht, tot)
3849 wordnode_T *node;
3850 hashtab_T *ht;
3851 int *tot; /* total count of nodes before compressing,
3852 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003853{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003854 wordnode_T *np;
3855 wordnode_T *tp;
3856 wordnode_T *child;
3857 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003858 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003859 int len = 0;
3860 unsigned nr, n;
3861 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003862
Bram Moolenaar51485f02005-06-04 21:55:20 +00003863 /*
3864 * Go through the list of siblings. Compress each child and then try
3865 * finding an identical child to replace it.
3866 * Note that with "child" we mean not just the node that is pointed to,
3867 * but the whole list of siblings, of which the node is the first.
3868 */
3869 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003870 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003871 ++len;
3872 if ((child = np->wn_child) != NULL)
3873 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003874 /* Compress the child. This fills hashkey. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003875 compressed += node_compress(child, ht, tot);
3876
3877 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003878 hash = hash_hash(child->wn_u1.hashkey);
3879 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003880 tp = NULL;
3881 if (!HASHITEM_EMPTY(hi))
3882 {
3883 /* There are children with an identical hash value. Now check
3884 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003885 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003886 if (node_equal(child, tp))
3887 {
3888 /* Found one! Now use that child in place of the
3889 * current one. This means the current child is
3890 * dropped from the tree. */
3891 np->wn_child = tp;
3892 ++compressed;
3893 break;
3894 }
3895 if (tp == NULL)
3896 {
3897 /* No other child with this hash value equals the child of
3898 * the node, add it to the linked list after the first
3899 * item. */
3900 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003901 child->wn_u2.next = tp->wn_u2.next;
3902 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003903 }
3904 }
3905 else
3906 /* No other child has this hash value, add it to the
3907 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003908 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003909 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003910 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003911 *tot += len;
3912
3913 /*
3914 * Make a hash key for the node and its siblings, so that we can quickly
3915 * find a lookalike node. This must be done after compressing the sibling
3916 * list, otherwise the hash key would become invalid by the compression.
3917 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003918 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003919 nr = 0;
3920 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003921 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003922 if (np->wn_byte == NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003923 /* end node: use wn_flags, wn_region and wn_prefixID */
3924 n = np->wn_flags + (np->wn_region << 8) + (np->wn_prefixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003925 else
3926 /* byte node: use the byte value and the child pointer */
3927 n = np->wn_byte + ((long_u)np->wn_child << 8);
3928 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003929 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003930
3931 /* Avoid NUL bytes, it terminates the hash key. */
3932 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00003933 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003934 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00003935 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003936 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00003937 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003938 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00003939 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
3940 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003941
3942 return compressed;
3943}
3944
3945/*
3946 * Return TRUE when two nodes have identical siblings and children.
3947 */
3948 static int
3949node_equal(n1, n2)
3950 wordnode_T *n1;
3951 wordnode_T *n2;
3952{
3953 wordnode_T *p1;
3954 wordnode_T *p2;
3955
3956 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
3957 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
3958 if (p1->wn_byte != p2->wn_byte
3959 || (p1->wn_byte == NUL
3960 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003961 || p1->wn_region != p2->wn_region
3962 || p1->wn_prefixID != p2->wn_prefixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003963 : (p1->wn_child != p2->wn_child)))
3964 break;
3965
3966 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003967}
3968
3969/*
3970 * Write a number to file "fd", MSB first, in "len" bytes.
3971 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003972 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003973put_bytes(fd, nr, len)
3974 FILE *fd;
3975 long_u nr;
3976 int len;
3977{
3978 int i;
3979
3980 for (i = len - 1; i >= 0; --i)
3981 putc((int)(nr >> (i * 8)), fd);
3982}
3983
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003984static int
3985#ifdef __BORLANDC__
3986_RTLENTRYF
3987#endif
3988rep_compare __ARGS((const void *s1, const void *s2));
3989
3990/*
3991 * Function given to qsort() to sort the REP items on "from" string.
3992 */
3993 static int
3994#ifdef __BORLANDC__
3995_RTLENTRYF
3996#endif
3997rep_compare(s1, s2)
3998 const void *s1;
3999 const void *s2;
4000{
4001 fromto_T *p1 = (fromto_T *)s1;
4002 fromto_T *p2 = (fromto_T *)s2;
4003
4004 return STRCMP(p1->ft_from, p2->ft_from);
4005}
4006
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004007/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004008 * Write the Vim spell file "fname".
4009 */
4010 static void
Bram Moolenaar3982c542005-06-08 21:56:31 +00004011write_vim_spell(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004012 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004013 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004014{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004015 FILE *fd;
4016 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004017 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004018 wordnode_T *tree;
4019 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004020 int i;
4021 int l;
4022 garray_T *gap;
4023 fromto_T *ftp;
4024 char_u *p;
4025 int rr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004026
Bram Moolenaarb765d632005-06-07 21:00:02 +00004027 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00004028 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004029 {
4030 EMSG2(_(e_notopen), fname);
4031 return;
4032 }
4033
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004034 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004035 * <charflagslen> <charflags>
4036 * <fcharslen> <fchars>
4037 * <prefcondcnt> <prefcond> ... */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004038
4039 /* <fileID> */
4040 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
4041 EMSG(_(e_write));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004042
4043 /* write the region names if there is more than one */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004044 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004045 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004046 putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */
4047 fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2),
4048 (size_t)1, fd);
4049 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004050 }
4051 else
4052 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004053 putc(0, fd);
4054 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004055 }
4056
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004057 /*
4058 * Write the table with character flags and table for case folding.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004059 * <charflagslen> <charflags> <fcharlen> <fchars>
4060 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004061 * 'encoding'.
4062 * Also skip this for an .add.spl file, the main spell file must contain
4063 * the table (avoids that it conflicts). File is shorter too.
4064 */
4065 if (spin->si_ascii || spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004066 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004067 putc(0, fd);
4068 putc(0, fd);
4069 putc(0, fd);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004070 }
4071 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00004072 write_spell_chartab(fd);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004073
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004074 /* Write the prefix conditions. */
4075 write_spell_prefcond(fd, &spin->si_prefcond);
4076
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004077 /* Sort the REP items. */
4078 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
4079 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004080
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004081 /* <SUGGEST> : <repcount> <rep> ...
4082 * <salflags> <salcount> <sal> ...
4083 * <maplen> <mapstr> */
4084 for (round = 1; round <= 2; ++round)
4085 {
4086 if (round == 1)
4087 gap = &spin->si_rep;
4088 else
4089 {
4090 gap = &spin->si_sal;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004091
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004092 i = 0;
4093 if (spin->si_followup)
4094 i |= SAL_F0LLOWUP;
4095 if (spin->si_collapse)
4096 i |= SAL_COLLAPSE;
4097 if (spin->si_rem_accents)
4098 i |= SAL_REM_ACCENTS;
4099 putc(i, fd); /* <salflags> */
4100 }
4101
4102 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
4103 for (i = 0; i < gap->ga_len; ++i)
4104 {
4105 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
4106 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
4107 ftp = &((fromto_T *)gap->ga_data)[i];
4108 for (rr = 1; rr <= 2; ++rr)
4109 {
4110 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
4111 l = STRLEN(p);
4112 putc(l, fd);
4113 fwrite(p, l, (size_t)1, fd);
4114 }
4115 }
4116 }
4117
4118 put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */
4119 if (spin->si_map.ga_len > 0) /* <mapstr> */
4120 fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len,
4121 (size_t)1, fd);
Bram Moolenaar50cde822005-06-05 21:54:54 +00004122
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004123 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004124 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004125 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004126 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004127 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004128 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004129 if (round == 1)
4130 tree = spin->si_foldroot;
4131 else if (round == 2)
4132 tree = spin->si_keeproot;
4133 else
4134 tree = spin->si_prefroot;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004135
Bram Moolenaar0c405862005-06-22 22:26:26 +00004136 /* Clear the index and wnode fields in the tree. */
4137 clear_node(tree);
4138
Bram Moolenaar51485f02005-06-04 21:55:20 +00004139 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00004140 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00004141 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004142 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004143
Bram Moolenaar51485f02005-06-04 21:55:20 +00004144 /* number of nodes in 4 bytes */
4145 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004146 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004147
Bram Moolenaar51485f02005-06-04 21:55:20 +00004148 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004149 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004150 }
4151
Bram Moolenaar51485f02005-06-04 21:55:20 +00004152 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004153}
4154
4155/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00004156 * Clear the index and wnode fields of "node", it siblings and its
4157 * children. This is needed because they are a union with other items to save
4158 * space.
4159 */
4160 static void
4161clear_node(node)
4162 wordnode_T *node;
4163{
4164 wordnode_T *np;
4165
4166 if (node != NULL)
4167 for (np = node; np != NULL; np = np->wn_sibling)
4168 {
4169 np->wn_u1.index = 0;
4170 np->wn_u2.wnode = NULL;
4171
4172 if (np->wn_byte != NUL)
4173 clear_node(np->wn_child);
4174 }
4175}
4176
4177
4178/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004179 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004180 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00004181 * This first writes the list of possible bytes (siblings). Then for each
4182 * byte recursively write the children.
4183 *
4184 * NOTE: The code here must match the code in read_tree(), since assumptions
4185 * are made about the indexes (so that we don't have to write them in the
4186 * file).
4187 *
4188 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004189 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004190 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00004191put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004192 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004193 wordnode_T *node;
4194 int index;
4195 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004196 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004197{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004198 int newindex = index;
4199 int siblingcount = 0;
4200 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004201 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004202
Bram Moolenaar51485f02005-06-04 21:55:20 +00004203 /* If "node" is zero the tree is empty. */
4204 if (node == NULL)
4205 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004206
Bram Moolenaar51485f02005-06-04 21:55:20 +00004207 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004208 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004209
4210 /* Count the number of siblings. */
4211 for (np = node; np != NULL; np = np->wn_sibling)
4212 ++siblingcount;
4213
4214 /* Write the sibling count. */
4215 if (fd != NULL)
4216 putc(siblingcount, fd); /* <siblingcount> */
4217
4218 /* Write each sibling byte and optionally extra info. */
4219 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004220 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004221 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004222 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004223 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004224 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004225 /* For a NUL byte (end of word) write the flags etc. */
4226 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004227 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004228 /* In PREFIXTREE write the required prefixID and the
4229 * associated condition nr (stored in wn_region). */
4230 putc(BY_FLAGS, fd); /* <byte> */
4231 putc(np->wn_prefixID, fd); /* <prefixID> */
4232 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004233 }
4234 else
4235 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004236 /* For word trees we write the flag/region items. */
4237 flags = np->wn_flags;
4238 if (regionmask != 0 && np->wn_region != regionmask)
4239 flags |= WF_REGION;
4240 if (np->wn_prefixID != 0)
4241 flags |= WF_PFX;
4242 if (flags == 0)
4243 {
4244 /* word without flags or region */
4245 putc(BY_NOFLAGS, fd); /* <byte> */
4246 }
4247 else
4248 {
4249 putc(BY_FLAGS, fd); /* <byte> */
4250 putc(flags, fd); /* <flags> */
4251 if (flags & WF_REGION)
4252 putc(np->wn_region, fd); /* <region> */
4253 if (flags & WF_PFX)
4254 putc(np->wn_prefixID, fd); /* <prefixID> */
4255 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004256 }
4257 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004258 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004259 else
4260 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00004261 if (np->wn_child->wn_u1.index != 0
4262 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004263 {
4264 /* The child is written elsewhere, write the reference. */
4265 if (fd != NULL)
4266 {
4267 putc(BY_INDEX, fd); /* <byte> */
4268 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004269 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004270 }
4271 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00004272 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004273 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004274 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004275
Bram Moolenaar51485f02005-06-04 21:55:20 +00004276 if (fd != NULL)
4277 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
4278 {
4279 EMSG(_(e_write));
4280 return 0;
4281 }
4282 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004283 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004284
4285 /* Space used in the array when reading: one for each sibling and one for
4286 * the count. */
4287 newindex += siblingcount + 1;
4288
4289 /* Recursively dump the children of each sibling. */
4290 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00004291 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
4292 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004293 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004294
4295 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004296}
4297
4298
4299/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00004300 * ":mkspell [-ascii] outfile infile ..."
4301 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004302 */
4303 void
4304ex_mkspell(eap)
4305 exarg_T *eap;
4306{
4307 int fcount;
4308 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004309 char_u *arg = eap->arg;
4310 int ascii = FALSE;
4311
4312 if (STRNCMP(arg, "-ascii", 6) == 0)
4313 {
4314 ascii = TRUE;
4315 arg = skipwhite(arg + 6);
4316 }
4317
4318 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
4319 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
4320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004321 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004322 FreeWild(fcount, fnames);
4323 }
4324}
4325
4326/*
4327 * Create a Vim spell file from one or more word lists.
4328 * "fnames[0]" is the output file name.
4329 * "fnames[fcount - 1]" is the last input file name.
4330 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
4331 * and ".spl" is appended to make the output file name.
4332 */
4333 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004334mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004335 int fcount;
4336 char_u **fnames;
4337 int ascii; /* -ascii argument given */
4338 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004339 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004340{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004341 char_u fname[MAXPATHL];
4342 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00004343 char_u **innames;
4344 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004345 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004346 int i;
4347 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004348 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004349 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004350 spellinfo_T spin;
4351
4352 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004353 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004354 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004355 spin.si_followup = TRUE;
4356 spin.si_rem_accents = TRUE;
4357 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
4358 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
4359 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004360 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004361
Bram Moolenaarb765d632005-06-07 21:00:02 +00004362 /* default: fnames[0] is output file, following are input files */
4363 innames = &fnames[1];
4364 incount = fcount - 1;
4365
4366 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004367 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004368 len = STRLEN(fnames[0]);
4369 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
4370 {
4371 /* For ":mkspell path/en.latin1.add" output file is
4372 * "path/en.latin1.add.spl". */
4373 innames = &fnames[0];
4374 incount = 1;
4375 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
4376 }
4377 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
4378 {
4379 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004380 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004381 }
4382 else
4383 /* Name should be language, make the file name from it. */
4384 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
4385 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
4386
4387 /* Check for .ascii.spl. */
4388 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
4389 spin.si_ascii = TRUE;
4390
4391 /* Check for .add.spl. */
4392 if (strstr((char *)gettail(wfname), ".add.") != NULL)
4393 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004394 }
4395
Bram Moolenaarb765d632005-06-07 21:00:02 +00004396 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004397 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004398 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004399 EMSG(_("E754: Only up to 8 regions supported"));
4400 else
4401 {
4402 /* Check for overwriting before doing things that may take a lot of
4403 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004404 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004405 {
4406 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004407 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004408 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004409 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004410 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004411 EMSG2(_(e_isadir2), wfname);
4412 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004413 }
4414
4415 /*
4416 * Init the aff and dic pointers.
4417 * Get the region names if there are more than 2 arguments.
4418 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004419 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004420 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004421 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004422
Bram Moolenaar3982c542005-06-08 21:56:31 +00004423 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004424 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004425 len = STRLEN(innames[i]);
4426 if (STRLEN(gettail(innames[i])) < 5
4427 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004428 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004429 EMSG2(_("E755: Invalid region in %s"), innames[i]);
4430 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004431 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00004432 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
4433 spin.si_region_name[i * 2 + 1] =
4434 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004435 }
4436 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00004437 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004438
Bram Moolenaarb765d632005-06-07 21:00:02 +00004439 if (!spin.si_add)
4440 /* Clear the char type tables, don't want to use any of the
4441 * currently used spell properties. */
4442 init_spell_chartab();
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004443
Bram Moolenaar51485f02005-06-04 21:55:20 +00004444 spin.si_foldroot = wordtree_alloc(&spin.si_blocks);
4445 spin.si_keeproot = wordtree_alloc(&spin.si_blocks);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004446 spin.si_prefroot = wordtree_alloc(&spin.si_blocks);
4447 if (spin.si_foldroot == NULL
4448 || spin.si_keeproot == NULL
4449 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004450 {
4451 error = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004452 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004453 }
4454
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004455 /*
4456 * Read all the .aff and .dic files.
4457 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004458 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004459 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004460 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004461 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004462 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004463 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004464
Bram Moolenaarb765d632005-06-07 21:00:02 +00004465 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004466 if (mch_stat((char *)fname, &st) >= 0)
4467 {
4468 /* Read the .aff file. Will init "spin->si_conv" based on the
4469 * "SET" line. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004470 afile[i] = spell_read_aff(fname, &spin);
4471 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004472 error = TRUE;
4473 else
4474 {
4475 /* Read the .dic file and store the words in the trees. */
4476 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00004477 innames[i]);
4478 if (spell_read_dic(fname, &spin, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004479 error = TRUE;
4480 }
4481 }
4482 else
4483 {
4484 /* No .aff file, try reading the file as a word list. Store
4485 * the words in the trees. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004486 if (spell_read_wordfile(innames[i], &spin) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004487 error = TRUE;
4488 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004489
Bram Moolenaarb765d632005-06-07 21:00:02 +00004490#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004491 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004492 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004493#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004494 }
4495
Bram Moolenaar51485f02005-06-04 21:55:20 +00004496 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004497 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004498 /*
4499 * Remove the dummy NUL from the start of the tree root.
4500 */
4501 spin.si_foldroot = spin.si_foldroot->wn_sibling;
4502 spin.si_keeproot = spin.si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004503 spin.si_prefroot = spin.si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004504
4505 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004506 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004507 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004508 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004510 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004511 verbose_enter();
4512 MSG(_("Compressing word tree..."));
4513 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004514 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004515 verbose_leave();
4516 }
4517 wordtree_compress(spin.si_foldroot, &spin);
4518 wordtree_compress(spin.si_keeproot, &spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004519 wordtree_compress(spin.si_prefroot, &spin);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004520 }
4521
Bram Moolenaar51485f02005-06-04 21:55:20 +00004522 if (!error)
4523 {
4524 /*
4525 * Write the info in the spell file.
4526 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004527 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004528 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004529 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004530 verbose_enter();
4531 smsg((char_u *)_("Writing spell file %s..."), wfname);
4532 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004533 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004534 verbose_leave();
4535 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00004536
Bram Moolenaar3982c542005-06-08 21:56:31 +00004537 write_vim_spell(wfname, &spin);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004538
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004539 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004540 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004541 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004542 verbose_enter();
4543 MSG(_("Done!"));
4544 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00004545 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004546 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004547 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004548 verbose_leave();
4549 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004550
Bram Moolenaarb765d632005-06-07 21:00:02 +00004551 /* If the file is loaded need to reload it. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004552 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004553 }
4554
4555 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004556 ga_clear(&spin.si_rep);
4557 ga_clear(&spin.si_sal);
4558 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004559 ga_clear(&spin.si_prefcond);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004560
4561 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004562 for (i = 0; i < incount; ++i)
4563 if (afile[i] != NULL)
4564 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004565
4566 /* Free all the bits and pieces at once. */
4567 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004568 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004569}
4570
Bram Moolenaarb765d632005-06-07 21:00:02 +00004571
4572/*
4573 * ":spellgood {word}"
4574 * ":spellwrong {word}"
4575 */
4576 void
4577ex_spell(eap)
4578 exarg_T *eap;
4579{
4580 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong);
4581}
4582
4583/*
4584 * Add "word[len]" to 'spellfile' as a good or bad word.
4585 */
4586 void
4587spell_add_word(word, len, bad)
4588 char_u *word;
4589 int len;
4590 int bad;
4591{
4592 FILE *fd;
4593 buf_T *buf;
4594
4595 if (*curbuf->b_p_spf == NUL)
4596 init_spellfile();
4597 if (*curbuf->b_p_spf == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004598 EMSG(_("E764: 'spellfile' is not set"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004599 else
4600 {
4601 /* Check that the user isn't editing the .add file somewhere. */
4602 buf = buflist_findname_exp(curbuf->b_p_spf);
4603 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
4604 buf = NULL;
4605 if (buf != NULL && bufIsChanged(buf))
4606 EMSG(_(e_bufloaded));
4607 else
4608 {
4609 fd = mch_fopen((char *)curbuf->b_p_spf, "a");
4610 if (fd == NULL)
4611 EMSG2(_(e_notopen), curbuf->b_p_spf);
4612 else
4613 {
4614 if (bad)
4615 fprintf(fd, "/!%.*s\n", len, word);
4616 else
4617 fprintf(fd, "%.*s\n", len, word);
4618 fclose(fd);
4619
4620 /* Update the .add.spl file. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004621 mkspell(1, &curbuf->b_p_spf, FALSE, TRUE, TRUE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004622
4623 /* If the .add file is edited somewhere, reload it. */
4624 if (buf != NULL)
4625 buf_reload(buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004626
4627 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004628 }
4629 }
4630 }
4631}
4632
4633/*
4634 * Initialize 'spellfile' for the current buffer.
4635 */
4636 static void
4637init_spellfile()
4638{
4639 char_u buf[MAXPATHL];
4640 int l;
4641 slang_T *sl;
4642 char_u *rtp;
4643
4644 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
4645 {
4646 /* Loop over all entries in 'runtimepath'. */
4647 rtp = p_rtp;
4648 while (*rtp != NUL)
4649 {
4650 /* Copy the path from 'runtimepath' to buf[]. */
4651 copy_option_part(&rtp, buf, MAXPATHL, ",");
4652 if (filewritable(buf) == 2)
4653 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004654 /* Use the first language name from 'spelllang' and the
4655 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004656 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
4657 l = STRLEN(buf);
4658 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00004659 "/spell/%.*s.%s.add",
4660 2, curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00004661 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
4662 ? (char_u *)"ascii" : spell_enc());
4663 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
4664 break;
4665 }
4666 }
4667 }
4668}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004669
Bram Moolenaar51485f02005-06-04 21:55:20 +00004670
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004671/*
4672 * Init the chartab used for spelling for ASCII.
4673 * EBCDIC is not supported!
4674 */
4675 static void
4676clear_spell_chartab(sp)
4677 spelltab_T *sp;
4678{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004679 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004680
4681 /* Init everything to FALSE. */
4682 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
4683 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
4684 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004685 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004686 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004687 sp->st_upper[i] = i;
4688 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004689
4690 /* We include digits. A word shouldn't start with a digit, but handling
4691 * that is done separately. */
4692 for (i = '0'; i <= '9'; ++i)
4693 sp->st_isw[i] = TRUE;
4694 for (i = 'A'; i <= 'Z'; ++i)
4695 {
4696 sp->st_isw[i] = TRUE;
4697 sp->st_isu[i] = TRUE;
4698 sp->st_fold[i] = i + 0x20;
4699 }
4700 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004701 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004702 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004703 sp->st_upper[i] = i - 0x20;
4704 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004705}
4706
4707/*
4708 * Init the chartab used for spelling. Only depends on 'encoding'.
4709 * Called once while starting up and when 'encoding' changes.
4710 * The default is to use isalpha(), but the spell file should define the word
4711 * characters to make it possible that 'encoding' differs from the current
4712 * locale.
4713 */
4714 void
4715init_spell_chartab()
4716{
4717 int i;
4718
4719 did_set_spelltab = FALSE;
4720 clear_spell_chartab(&spelltab);
4721
4722#ifdef FEAT_MBYTE
4723 if (enc_dbcs)
4724 {
4725 /* DBCS: assume double-wide characters are word characters. */
4726 for (i = 128; i <= 255; ++i)
4727 if (MB_BYTE2LEN(i) == 2)
4728 spelltab.st_isw[i] = TRUE;
4729 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004730 else if (enc_utf8)
4731 {
4732 for (i = 128; i < 256; ++i)
4733 {
4734 spelltab.st_isu[i] = utf_isupper(i);
4735 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
4736 spelltab.st_fold[i] = utf_fold(i);
4737 spelltab.st_upper[i] = utf_toupper(i);
4738 }
4739 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004740 else
4741#endif
4742 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004743 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004744 for (i = 128; i < 256; ++i)
4745 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004746 if (MB_ISUPPER(i))
4747 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004748 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004749 spelltab.st_isu[i] = TRUE;
4750 spelltab.st_fold[i] = MB_TOLOWER(i);
4751 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004752 else if (MB_ISLOWER(i))
4753 {
4754 spelltab.st_isw[i] = TRUE;
4755 spelltab.st_upper[i] = MB_TOUPPER(i);
4756 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004757 }
4758 }
4759}
4760
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004761static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
4762static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
4763
4764/*
4765 * Set the spell character tables from strings in the affix file.
4766 */
4767 static int
4768set_spell_chartab(fol, low, upp)
4769 char_u *fol;
4770 char_u *low;
4771 char_u *upp;
4772{
4773 /* We build the new tables here first, so that we can compare with the
4774 * previous one. */
4775 spelltab_T new_st;
4776 char_u *pf = fol, *pl = low, *pu = upp;
4777 int f, l, u;
4778
4779 clear_spell_chartab(&new_st);
4780
4781 while (*pf != NUL)
4782 {
4783 if (*pl == NUL || *pu == NUL)
4784 {
4785 EMSG(_(e_affform));
4786 return FAIL;
4787 }
4788#ifdef FEAT_MBYTE
4789 f = mb_ptr2char_adv(&pf);
4790 l = mb_ptr2char_adv(&pl);
4791 u = mb_ptr2char_adv(&pu);
4792#else
4793 f = *pf++;
4794 l = *pl++;
4795 u = *pu++;
4796#endif
4797 /* Every character that appears is a word character. */
4798 if (f < 256)
4799 new_st.st_isw[f] = TRUE;
4800 if (l < 256)
4801 new_st.st_isw[l] = TRUE;
4802 if (u < 256)
4803 new_st.st_isw[u] = TRUE;
4804
4805 /* if "LOW" and "FOL" are not the same the "LOW" char needs
4806 * case-folding */
4807 if (l < 256 && l != f)
4808 {
4809 if (f >= 256)
4810 {
4811 EMSG(_(e_affrange));
4812 return FAIL;
4813 }
4814 new_st.st_fold[l] = f;
4815 }
4816
4817 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004818 * case-folding, it's upper case and the "UPP" is the upper case of
4819 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004820 if (u < 256 && u != f)
4821 {
4822 if (f >= 256)
4823 {
4824 EMSG(_(e_affrange));
4825 return FAIL;
4826 }
4827 new_st.st_fold[u] = f;
4828 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004829 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004830 }
4831 }
4832
4833 if (*pl != NUL || *pu != NUL)
4834 {
4835 EMSG(_(e_affform));
4836 return FAIL;
4837 }
4838
4839 return set_spell_finish(&new_st);
4840}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004841
4842/*
4843 * Set the spell character tables from strings in the .spl file.
4844 */
4845 static int
4846set_spell_charflags(flags, cnt, upp)
4847 char_u *flags;
4848 int cnt;
4849 char_u *upp;
4850{
4851 /* We build the new tables here first, so that we can compare with the
4852 * previous one. */
4853 spelltab_T new_st;
4854 int i;
4855 char_u *p = upp;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004856 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004857
4858 clear_spell_chartab(&new_st);
4859
4860 for (i = 0; i < cnt; ++i)
4861 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004862 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
4863 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004864
4865 if (*p == NUL)
4866 return FAIL;
4867#ifdef FEAT_MBYTE
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004868 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004869#else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004870 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004871#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004872 new_st.st_fold[i + 128] = c;
4873 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
4874 new_st.st_upper[c] = i + 128;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004875 }
4876
4877 return set_spell_finish(&new_st);
4878}
4879
4880 static int
4881set_spell_finish(new_st)
4882 spelltab_T *new_st;
4883{
4884 int i;
4885
4886 if (did_set_spelltab)
4887 {
4888 /* check that it's the same table */
4889 for (i = 0; i < 256; ++i)
4890 {
4891 if (spelltab.st_isw[i] != new_st->st_isw[i]
4892 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004893 || spelltab.st_fold[i] != new_st->st_fold[i]
4894 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004895 {
4896 EMSG(_("E763: Word characters differ between spell files"));
4897 return FAIL;
4898 }
4899 }
4900 }
4901 else
4902 {
4903 /* copy the new spelltab into the one being used */
4904 spelltab = *new_st;
4905 did_set_spelltab = TRUE;
4906 }
4907
4908 return OK;
4909}
4910
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004911/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004912 * Write the table with prefix conditions to the .spl file.
4913 */
4914 static void
4915write_spell_prefcond(fd, gap)
4916 FILE *fd;
4917 garray_T *gap;
4918{
4919 int i;
4920 char_u *p;
4921 int len;
4922
4923 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
4924
4925 for (i = 0; i < gap->ga_len; ++i)
4926 {
4927 /* <prefcond> : <condlen> <condstr> */
4928 p = ((char_u **)gap->ga_data)[i];
4929 if (p == NULL)
4930 fputc(0, fd);
4931 else
4932 {
4933 len = STRLEN(p);
4934 fputc(len, fd);
4935 fwrite(p, (size_t)len, (size_t)1, fd);
4936 }
4937 }
4938}
4939
4940/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004941 * Write the current tables into the .spl file.
4942 * This makes sure the same characters are recognized as word characters when
4943 * generating an when using a spell file.
4944 */
4945 static void
4946write_spell_chartab(fd)
4947 FILE *fd;
4948{
4949 char_u charbuf[256 * 4];
4950 int len = 0;
4951 int flags;
4952 int i;
4953
4954 fputc(128, fd); /* <charflagslen> */
4955 for (i = 128; i < 256; ++i)
4956 {
4957 flags = 0;
4958 if (spelltab.st_isw[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004959 flags |= CF_WORD;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004960 if (spelltab.st_isu[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004961 flags |= CF_UPPER;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004962 fputc(flags, fd); /* <charflags> */
4963
Bram Moolenaarb765d632005-06-07 21:00:02 +00004964#ifdef FEAT_MBYTE
4965 if (has_mbyte)
4966 len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
4967 else
4968#endif
4969 charbuf[len++] = spelltab.st_fold[i];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004970 }
4971
4972 put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
4973 fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
4974}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004975
4976/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004977 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
4978 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004979 * When using a multi-byte 'encoding' the length may change!
4980 * Returns FAIL when something wrong.
4981 */
4982 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004983spell_casefold(str, len, buf, buflen)
4984 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004985 int len;
4986 char_u *buf;
4987 int buflen;
4988{
4989 int i;
4990
4991 if (len >= buflen)
4992 {
4993 buf[0] = NUL;
4994 return FAIL; /* result will not fit */
4995 }
4996
4997#ifdef FEAT_MBYTE
4998 if (has_mbyte)
4999 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005000 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005001 char_u *p;
5002 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005003
5004 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005005 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005006 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005007 if (outi + MB_MAXBYTES > buflen)
5008 {
5009 buf[outi] = NUL;
5010 return FAIL;
5011 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005012 c = mb_ptr2char_adv(&p);
5013 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005014 }
5015 buf[outi] = NUL;
5016 }
5017 else
5018#endif
5019 {
5020 /* Be quick for non-multibyte encodings. */
5021 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005022 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005023 buf[i] = NUL;
5024 }
5025
5026 return OK;
5027}
5028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005029/*
5030 * "z?": Find badly spelled word under or after the cursor.
5031 * Give suggestions for the properly spelled word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005032 */
5033 void
5034spell_suggest()
5035{
5036 char_u *line;
5037 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005038 char_u wcopy[MAXWLEN + 2];
5039 char_u *p;
5040 int i;
5041 int c;
5042 suginfo_T sug;
5043 suggest_T *stp;
5044
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005045 /* Find the start of the badly spelled word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005046 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL
5047 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005048 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005049 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
5050 return;
5051
5052 /* No bad word or it starts after the cursor: use the word under the
5053 * cursor. */
5054 curwin->w_cursor = prev_cursor;
5055 line = ml_get_curline();
5056 p = line + curwin->w_cursor.col;
5057 /* Backup to before start of word. */
5058 while (p > line && SPELL_ISWORDP(p))
5059 mb_ptr_back(line, p);
5060 /* Forward to start of word. */
5061 while (!SPELL_ISWORDP(p))
5062 mb_ptr_adv(p);
5063
5064 if (!SPELL_ISWORDP(p)) /* No word found. */
5065 {
5066 beep_flush();
5067 return;
5068 }
5069 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005070 }
5071
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005072 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005073 line = ml_get_curline();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005074
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005075 /* Get the list of suggestions */
5076 spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005077
5078 if (sug.su_ga.ga_len == 0)
5079 MSG(_("Sorry, no suggestions"));
5080 else
5081 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005082 /* List the suggestions. */
5083 msg_start();
5084 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
5085 sug.su_badlen, sug.su_badptr);
5086 msg_puts(IObuff);
5087 msg_clr_eos();
5088 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00005089
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005090 msg_scroll = TRUE;
5091 for (i = 0; i < sug.su_ga.ga_len; ++i)
5092 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005093 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005094
5095 /* The suggested word may replace only part of the bad word, add
5096 * the not replaced part. */
5097 STRCPY(wcopy, stp->st_word);
5098 if (sug.su_badlen > stp->st_orglen)
5099 vim_strncpy(wcopy + STRLEN(wcopy),
5100 sug.su_badptr + stp->st_orglen,
5101 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005102 vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\""), i + 1, wcopy);
5103 msg_puts(IObuff);
5104
5105 /* The word may replace more than "su_badlen". */
5106 if (sug.su_badlen < stp->st_orglen)
5107 {
5108 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
5109 stp->st_orglen, sug.su_badptr);
5110 msg_puts(IObuff);
5111 }
5112
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005113 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005114 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005115 /* Add the score. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005116 if (sps_flags & SPS_DOUBLE)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005117 vim_snprintf((char *)IObuff, IOSIZE, _(" (%s%d - %d)"),
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005118 stp->st_salscore ? "s " : "",
5119 stp->st_score, stp->st_altscore);
5120 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00005121 vim_snprintf((char *)IObuff, IOSIZE, _(" (%d)"),
5122 stp->st_score);
5123 msg_advance(30);
5124 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005125 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005126 lines_left = 3; /* avoid more prompt */
5127 msg_putchar('\n');
5128 }
5129
5130 /* Ask for choice. */
5131 i = prompt_for_number();
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005132 if (i > 0 && i <= sug.su_ga.ga_len && u_save_cursor() == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005133 {
5134 /* Replace the word. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005135 stp = &SUG(sug.su_ga, i - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005136 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
5137 if (p != NULL)
5138 {
5139 c = sug.su_badptr - line;
5140 mch_memmove(p, line, c);
5141 STRCPY(p + c, stp->st_word);
5142 STRCAT(p, sug.su_badptr + stp->st_orglen);
5143 ml_replace(curwin->w_cursor.lnum, p, FALSE);
5144 curwin->w_cursor.col = c;
5145 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005146
5147 /* For redo we use a change-word command. */
5148 ResetRedobuff();
5149 AppendToRedobuff((char_u *)"ciw");
5150 AppendToRedobuff(stp->st_word);
5151 AppendCharToRedobuff(ESC);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005152 }
5153 }
5154 else
5155 curwin->w_cursor = prev_cursor;
5156 }
5157
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005158 spell_find_cleanup(&sug);
5159}
5160
5161/*
5162 * Find spell suggestions for "word". Return them in the growarray "*gap" as
5163 * a list of allocated strings.
5164 */
5165 void
5166spell_suggest_list(gap, word, maxcount)
5167 garray_T *gap;
5168 char_u *word;
5169 int maxcount; /* maximum nr of suggestions */
5170{
5171 suginfo_T sug;
5172 int i;
5173 suggest_T *stp;
5174 char_u *wcopy;
5175
5176 spell_find_suggest(word, &sug, maxcount);
5177
5178 /* Make room in "gap". */
5179 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
5180 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
5181 return;
5182
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005183 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005184 {
5185 stp = &SUG(sug.su_ga, i);
5186
5187 /* The suggested word may replace only part of "word", add the not
5188 * replaced part. */
5189 wcopy = alloc(STRLEN(stp->st_word)
5190 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
5191 if (wcopy == NULL)
5192 break;
5193 STRCPY(wcopy, stp->st_word);
5194 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
5195 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
5196 }
5197
5198 spell_find_cleanup(&sug);
5199}
5200
5201/*
5202 * Find spell suggestions for the word at the start of "badptr".
5203 * Return the suggestions in "su->su_ga".
5204 * The maximum number of suggestions is "maxcount".
5205 * Note: does use info for the current window.
5206 * This is based on the mechanisms of Aspell, but completely reimplemented.
5207 */
5208 static void
5209spell_find_suggest(badptr, su, maxcount)
5210 char_u *badptr;
5211 suginfo_T *su;
5212 int maxcount;
5213{
5214 int attr;
5215
5216 /*
5217 * Set the info in "*su".
5218 */
5219 vim_memset(su, 0, sizeof(suginfo_T));
5220 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
5221 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
5222 hash_init(&su->su_banned);
5223
5224 su->su_badptr = badptr;
5225 su->su_badlen = spell_check(curwin, su->su_badptr, &attr);
5226 su->su_maxcount = maxcount;
5227
5228 if (su->su_badlen >= MAXWLEN)
5229 su->su_badlen = MAXWLEN - 1; /* just in case */
5230 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
5231 (void)spell_casefold(su->su_badptr, su->su_badlen,
5232 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005233 /* get caps flags for bad word */
5234 su->su_badflags = captype(su->su_badptr, su->su_badptr + su->su_badlen);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005235
5236 /* Ban the bad word itself. It may appear in another region. */
5237 add_banned(su, su->su_badword);
5238
5239 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00005240 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005241 *
5242 * Set a maximum score to limit the combination of operations that is
5243 * tried.
5244 */
5245 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005246 suggest_try_special(su);
5247
5248 /*
5249 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
5250 * from the .aff file and inserting a space (split the word).
5251 */
5252 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005253
5254 /* For the resulting top-scorers compute the sound-a-like score. */
5255 if (sps_flags & SPS_DOUBLE)
5256 score_comp_sal(su);
5257
5258 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00005259 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005260 *
5261 * Only do this when we don't have a lot of suggestions yet, because it's
5262 * very slow and often doesn't find new suggestions.
5263 */
5264 if ((sps_flags & SPS_DOUBLE)
5265 || (!(sps_flags & SPS_FAST)
5266 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
5267 {
5268 /* Allow a higher score now. */
5269 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005270 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005271 }
5272
5273 /* When CTRL-C was hit while searching do show the results. */
5274 ui_breakcheck();
5275 if (got_int)
5276 {
5277 (void)vgetc();
5278 got_int = FALSE;
5279 }
5280
5281 if (sps_flags & SPS_DOUBLE)
5282 {
5283 /* Combine the two list of suggestions. */
5284 score_combine(su);
5285 }
5286 else if (su->su_ga.ga_len != 0)
5287 {
5288 if (sps_flags & SPS_BEST)
5289 /* Adjust the word score for how it sounds like. */
5290 rescore_suggestions(su);
5291
5292 /* Sort the suggestions and truncate at "maxcount". */
5293 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, maxcount);
5294 }
5295}
5296
5297/*
5298 * Free the info put in "*su" by spell_find_suggest().
5299 */
5300 static void
5301spell_find_cleanup(su)
5302 suginfo_T *su;
5303{
5304 int i;
5305
5306 /* Free the suggestions. */
5307 for (i = 0; i < su->su_ga.ga_len; ++i)
5308 vim_free(SUG(su->su_ga, i).st_word);
5309 ga_clear(&su->su_ga);
5310 for (i = 0; i < su->su_sga.ga_len; ++i)
5311 vim_free(SUG(su->su_sga, i).st_word);
5312 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005313
5314 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005315 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005316}
5317
5318/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005319 * Make a copy of "word", with the first letter upper or lower cased, to
5320 * "wcopy[MAXWLEN]". "word" must not be empty.
5321 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005322 */
5323 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005324onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005325 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005326 char_u *wcopy;
5327 int upper; /* TRUE: first letter made upper case */
5328{
5329 char_u *p;
5330 int c;
5331 int l;
5332
5333 p = word;
5334#ifdef FEAT_MBYTE
5335 if (has_mbyte)
5336 c = mb_ptr2char_adv(&p);
5337 else
5338#endif
5339 c = *p++;
5340 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005341 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005342 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005343 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005344#ifdef FEAT_MBYTE
5345 if (has_mbyte)
5346 l = mb_char2bytes(c, wcopy);
5347 else
5348#endif
5349 {
5350 l = 1;
5351 wcopy[0] = c;
5352 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005353 vim_strncpy(wcopy + l, p, MAXWLEN - l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005354}
5355
5356/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005357 * Make a copy of "word" with all the letters upper cased into
5358 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005359 */
5360 static void
5361allcap_copy(word, wcopy)
5362 char_u *word;
5363 char_u *wcopy;
5364{
5365 char_u *s;
5366 char_u *d;
5367 int c;
5368
5369 d = wcopy;
5370 for (s = word; *s != NUL; )
5371 {
5372#ifdef FEAT_MBYTE
5373 if (has_mbyte)
5374 c = mb_ptr2char_adv(&s);
5375 else
5376#endif
5377 c = *s++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005378 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005379
5380#ifdef FEAT_MBYTE
5381 if (has_mbyte)
5382 {
5383 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
5384 break;
5385 d += mb_char2bytes(c, d);
5386 }
5387 else
5388#endif
5389 {
5390 if (d - wcopy >= MAXWLEN - 1)
5391 break;
5392 *d++ = c;
5393 }
5394 }
5395 *d = NUL;
5396}
5397
5398/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00005399 * Try finding suggestions by recognizing specific situations.
5400 */
5401 static void
5402suggest_try_special(su)
5403 suginfo_T *su;
5404{
5405 char_u *p;
5406 int len;
5407 int c;
5408 char_u word[MAXWLEN];
5409
5410 /*
5411 * Recognize a word that is repeated: "the the".
5412 */
5413 p = skiptowhite(su->su_fbadword);
5414 len = p - su->su_fbadword;
5415 p = skipwhite(p);
5416 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
5417 {
5418 /* Include badflags: if the badword is onecap or allcap
5419 * use that for the goodword too: "The the" -> "The". */
5420 c = su->su_fbadword[len];
5421 su->su_fbadword[len] = NUL;
5422 make_case_word(su->su_fbadword, word, su->su_badflags);
5423 su->su_fbadword[len] = c;
5424 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, TRUE);
5425 }
5426}
5427
5428/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005429 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00005430 *
5431 * This uses a state machine. At each node in the tree we try various
5432 * operations. When trying if an operation work "depth" is increased and the
5433 * stack[] is used to store info. This allows combinations, thus insert one
5434 * character, replace one and delete another. The number of changes is
5435 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005436 */
5437 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00005438suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005439 suginfo_T *su;
5440{
5441 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
5442 char_u tword[MAXWLEN]; /* good word collected so far */
5443 trystate_T stack[MAXWLEN];
5444 char_u preword[MAXWLEN * 3]; /* word found with proper case (appended
5445 * to for word split) */
5446 char_u prewordlen = 0; /* length of word in "preword" */
5447 int splitoff = 0; /* index in tword after last split */
5448 trystate_T *sp;
5449 int newscore;
5450 langp_T *lp;
5451 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005452 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005453 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005454 int c, c2, c3;
5455 int n = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005456 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005457 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005458 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005459 int len;
5460 char_u *p;
5461 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00005462 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005463 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005464
5465 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00005466 * to find matches (esp. REP items). Append some more text, changing
5467 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005468 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005469 n = STRLEN(fword);
5470 p = su->su_badptr + su->su_badlen;
5471 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005472
5473 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
5474 lp->lp_slang != NULL; ++lp)
5475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005476 /*
5477 * Go through the whole case-fold tree, try changes at each node.
5478 * "tword[]" contains the word collected from nodes in the tree.
5479 * "fword[]" the word we are trying to match with (initially the bad
5480 * word).
5481 */
5482 byts = lp->lp_slang->sl_fbyts;
5483 idxs = lp->lp_slang->sl_fidxs;
5484
5485 depth = 0;
5486 stack[0].ts_state = STATE_START;
5487 stack[0].ts_score = 0;
5488 stack[0].ts_curi = 1;
5489 stack[0].ts_fidx = 0;
5490 stack[0].ts_fidxtry = 0;
5491 stack[0].ts_twordlen = 0;
5492 stack[0].ts_arridx = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00005493#ifdef FEAT_MBYTE
5494 stack[0].ts_tcharlen = 0;
5495#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005496
Bram Moolenaarea424162005-06-16 21:51:00 +00005497 /*
5498 * Loop to find all suggestions. At each round we either:
5499 * - For the current state try one operation, advance "ts_curi",
5500 * increase "depth".
5501 * - When a state is done go to the next, set "ts_state".
5502 * - When all states are tried decrease "depth".
5503 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005504 while (depth >= 0 && !got_int)
5505 {
5506 sp = &stack[depth];
5507 switch (sp->ts_state)
5508 {
5509 case STATE_START:
5510 /*
5511 * Start of node: Deal with NUL bytes, which means
5512 * tword[] may end here.
5513 */
5514 arridx = sp->ts_arridx; /* current node in the tree */
5515 len = byts[arridx]; /* bytes in this node */
5516 arridx += sp->ts_curi; /* index of current byte */
5517
Bram Moolenaar0c405862005-06-22 22:26:26 +00005518 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005519 {
5520 /* Past bytes in node and/or past NUL bytes. */
5521 sp->ts_state = STATE_ENDNUL;
5522 break;
5523 }
5524
5525 /*
5526 * End of word in tree.
5527 */
5528 ++sp->ts_curi; /* eat one NUL byte */
5529
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005530 flags = (int)idxs[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005531
5532 /*
5533 * Form the word with proper case in preword.
5534 * If there is a word from a previous split, append.
5535 */
5536 tword[sp->ts_twordlen] = NUL;
5537 if (flags & WF_KEEPCAP)
5538 /* Must find the word in the keep-case tree. */
5539 find_keepcap_word(lp->lp_slang, tword + splitoff,
5540 preword + prewordlen);
5541 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00005542 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005543 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00005544 * use that for the goodword too. But if the badword is
5545 * allcap and it's only one char long use onecap. */
5546 c = su->su_badflags;
5547 if ((c & WF_ALLCAP)
5548#ifdef FEAT_MBYTE
5549 && su->su_badlen == mb_ptr2len_check(su->su_badptr)
5550#else
5551 && su->su_badlen == 1
5552#endif
5553 )
5554 c = WF_ONECAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005555 make_case_word(tword + splitoff,
Bram Moolenaar0c405862005-06-22 22:26:26 +00005556 preword + prewordlen, flags | c);
5557 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005558
5559 /* Don't use a banned word. It may appear again as a good
5560 * word, thus remember it. */
5561 if (flags & WF_BANNED)
5562 {
5563 add_banned(su, preword + prewordlen);
5564 break;
5565 }
5566 if (was_banned(su, preword + prewordlen))
5567 break;
5568
5569 newscore = 0;
5570 if ((flags & WF_REGION)
5571 && (((unsigned)flags >> 8) & lp->lp_region) == 0)
5572 newscore += SCORE_REGION;
5573 if (flags & WF_RARE)
5574 newscore += SCORE_RARE;
5575
Bram Moolenaar0c405862005-06-22 22:26:26 +00005576 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005577 captype(preword + prewordlen, NULL)))
5578 newscore += SCORE_ICASE;
5579
Bram Moolenaar0c405862005-06-22 22:26:26 +00005580 if ((fword[sp->ts_fidx] == NUL
5581 || !SPELL_ISWORDP(fword + sp->ts_fidx))
5582 && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005583 {
5584 /* The badword also ends: add suggestions, */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005585 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00005586 sp->ts_fidx - repextra,
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005587 sp->ts_score + newscore, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005588 }
Bram Moolenaarea424162005-06-16 21:51:00 +00005589 else if (sp->ts_fidx >= sp->ts_fidxtry
5590#ifdef FEAT_MBYTE
5591 /* Don't split halfway a character. */
5592 && (!has_mbyte || sp->ts_tcharlen == 0)
5593#endif
5594 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005595 {
5596 /* The word in the tree ends but the badword
5597 * continues: try inserting a space and check that a valid
5598 * words starts at fword[sp->ts_fidx]. */
5599 if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT))
5600 {
5601 /* Save things to be restored at STATE_SPLITUNDO. */
5602 sp->ts_save_prewordlen = prewordlen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005603 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005604 sp->ts_save_splitoff = splitoff;
5605
5606 /* Append a space to preword. */
5607 STRCAT(preword, " ");
5608 prewordlen = STRLEN(preword);
5609 splitoff = sp->ts_twordlen;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005610#ifdef FEAT_MBYTE
5611 if (has_mbyte)
5612 {
5613 int i = 0;
5614
5615 /* Case-folding may change the number of bytes:
5616 * Count nr of chars in fword[sp->ts_fidx] and
5617 * advance that many chars in su->su_badptr. */
5618 for (p = fword; p < fword + sp->ts_fidx;
5619 mb_ptr_adv(p))
5620 ++i;
5621 for (p = su->su_badptr; i > 0; mb_ptr_adv(p))
5622 --i;
5623 }
5624 else
5625#endif
5626 p = su->su_badptr + sp->ts_fidx;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005627 su->su_badflags = captype(p, su->su_badptr
5628 + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005629
5630 sp->ts_state = STATE_SPLITUNDO;
5631 ++depth;
5632 /* Restart at top of the tree. */
5633 stack[depth].ts_arridx = 0;
5634 }
5635 }
5636 break;
5637
5638 case STATE_SPLITUNDO:
Bram Moolenaar0c405862005-06-22 22:26:26 +00005639 /* Undo the changes done for word split. */
5640 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005641 splitoff = sp->ts_save_splitoff;
5642 prewordlen = sp->ts_save_prewordlen;
5643
5644 /* Continue looking for NUL bytes. */
5645 sp->ts_state = STATE_START;
5646 break;
5647
5648 case STATE_ENDNUL:
5649 /* Past the NUL bytes in the node. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005650 if (fword[sp->ts_fidx] == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005651 {
5652 /* The badword ends, can't use the bytes in this node. */
5653 sp->ts_state = STATE_DEL;
5654 break;
5655 }
5656 sp->ts_state = STATE_PLAIN;
5657 /*FALLTHROUGH*/
5658
5659 case STATE_PLAIN:
5660 /*
5661 * Go over all possible bytes at this node, add each to
5662 * tword[] and use child node. "ts_curi" is the index.
5663 */
5664 arridx = sp->ts_arridx;
5665 if (sp->ts_curi > byts[arridx])
5666 {
5667 /* Done all bytes at this node, do next state. When still
5668 * at already changed bytes skip the other tricks. */
5669 if (sp->ts_fidx >= sp->ts_fidxtry)
5670 sp->ts_state = STATE_DEL;
5671 else
5672 sp->ts_state = STATE_FINAL;
5673 }
5674 else
5675 {
5676 arridx += sp->ts_curi++;
5677 c = byts[arridx];
5678
5679 /* Normal byte, go one level deeper. If it's not equal to
5680 * the byte in the bad word adjust the score. But don't
5681 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00005682 if (c == fword[sp->ts_fidx]
5683#ifdef FEAT_MBYTE
5684 || (sp->ts_tcharlen > 0
5685 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005686#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00005687 )
5688 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005689 else
5690 newscore = SCORE_SUBST;
5691 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
5692 && try_deeper(su, stack, depth, newscore))
5693 {
5694 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005695 sp = &stack[depth];
5696 ++sp->ts_fidx;
5697 tword[sp->ts_twordlen++] = c;
5698 sp->ts_arridx = idxs[arridx];
5699#ifdef FEAT_MBYTE
5700 if (newscore == SCORE_SUBST)
5701 sp->ts_isdiff = DIFF_YES;
5702 if (has_mbyte)
5703 {
5704 /* Multi-byte characters are a bit complicated to
5705 * handle: They differ when any of the bytes
5706 * differ and then their length may also differ. */
5707 if (sp->ts_tcharlen == 0)
5708 {
5709 /* First byte. */
5710 sp->ts_tcharidx = 0;
5711 sp->ts_tcharlen = MB_BYTE2LEN(c);
5712 sp->ts_fcharstart = sp->ts_fidx - 1;
5713 sp->ts_isdiff = (newscore != 0)
5714 ? DIFF_YES : DIFF_NONE;
5715 }
5716 else if (sp->ts_isdiff == DIFF_INSERT)
5717 /* When inserting trail bytes don't advance in
5718 * the bad word. */
5719 --sp->ts_fidx;
5720 if (++sp->ts_tcharidx == sp->ts_tcharlen)
5721 {
5722 /* Last byte of character. */
5723 if (sp->ts_isdiff == DIFF_YES)
5724 {
5725 /* Correct ts_fidx for the byte length of
5726 * the character (we didn't check that
5727 * before). */
5728 sp->ts_fidx = sp->ts_fcharstart
5729 + MB_BYTE2LEN(
5730 fword[sp->ts_fcharstart]);
5731
5732 /* For a similar character adjust score
5733 * from SCORE_SUBST to SCORE_SIMILAR. */
5734 if (lp->lp_slang->sl_has_map
5735 && similar_chars(lp->lp_slang,
5736 mb_ptr2char(tword
5737 + sp->ts_twordlen
5738 - sp->ts_tcharlen),
5739 mb_ptr2char(fword
5740 + sp->ts_fcharstart)))
5741 sp->ts_score -=
5742 SCORE_SUBST - SCORE_SIMILAR;
5743 }
5744
5745 /* Starting a new char, reset the length. */
5746 sp->ts_tcharlen = 0;
5747 }
5748 }
5749 else
5750#endif
5751 {
5752 /* If we found a similar char adjust the score.
5753 * We do this after calling try_deeper() because
5754 * it's slow. */
5755 if (newscore != 0
5756 && lp->lp_slang->sl_has_map
5757 && similar_chars(lp->lp_slang,
5758 c, fword[sp->ts_fidx - 1]))
5759 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
5760 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005761 }
5762 }
5763 break;
5764
5765 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00005766#ifdef FEAT_MBYTE
5767 /* When past the first byte of a multi-byte char don't try
5768 * delete/insert/swap a character. */
5769 if (has_mbyte && sp->ts_tcharlen > 0)
5770 {
5771 sp->ts_state = STATE_FINAL;
5772 break;
5773 }
5774#endif
5775 /*
5776 * Try skipping one character in the bad word (delete it).
5777 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005778 sp->ts_state = STATE_INS;
5779 sp->ts_curi = 1;
5780 if (fword[sp->ts_fidx] != NUL
5781 && try_deeper(su, stack, depth, SCORE_DEL))
5782 {
5783 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005784#ifdef FEAT_MBYTE
5785 if (has_mbyte)
5786 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
5787 else
5788#endif
5789 ++stack[depth].ts_fidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005790 break;
5791 }
5792 /*FALLTHROUGH*/
5793
5794 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00005795 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005796 * node. */
5797 n = sp->ts_arridx;
5798 if (sp->ts_curi > byts[n])
5799 {
5800 /* Done all bytes at this node, do next state. */
5801 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005802 }
5803 else
5804 {
Bram Moolenaarea424162005-06-16 21:51:00 +00005805 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005806 n += sp->ts_curi++;
5807 c = byts[n];
5808 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
5809 {
5810 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005811 sp = &stack[depth];
5812 tword[sp->ts_twordlen++] = c;
5813 sp->ts_arridx = idxs[n];
5814#ifdef FEAT_MBYTE
5815 if (has_mbyte)
5816 {
5817 fl = MB_BYTE2LEN(c);
5818 if (fl > 1)
5819 {
5820 /* There are following bytes for the same
5821 * character. We must find all bytes before
5822 * trying delete/insert/swap/etc. */
5823 sp->ts_tcharlen = fl;
5824 sp->ts_tcharidx = 1;
5825 sp->ts_isdiff = DIFF_INSERT;
5826 }
5827 }
5828#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005829 }
5830 }
5831 break;
5832
5833 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00005834 /*
5835 * Swap two bytes in the bad word: "12" -> "21".
5836 * We change "fword" here, it's changed back afterwards.
5837 */
5838 p = fword + sp->ts_fidx;
5839 c = *p;
5840 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005841 {
Bram Moolenaarea424162005-06-16 21:51:00 +00005842 /* End of word, can't swap or replace. */
5843 sp->ts_state = STATE_FINAL;
5844 break;
5845 }
5846#ifdef FEAT_MBYTE
5847 if (has_mbyte)
5848 {
5849 n = mb_ptr2len_check(p);
5850 c = mb_ptr2char(p);
5851 c2 = mb_ptr2char(p + n);
5852 }
5853 else
5854#endif
5855 c2 = p[1];
5856 if (c == c2)
5857 {
5858 /* Characters are identical, swap won't do anything. */
5859 sp->ts_state = STATE_SWAP3;
5860 break;
5861 }
5862 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
5863 {
5864 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005865 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005866#ifdef FEAT_MBYTE
5867 if (has_mbyte)
5868 {
5869 fl = mb_char2len(c2);
5870 mch_memmove(p, p + n, fl);
5871 mb_char2bytes(c, p + fl);
5872 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
5873 }
5874 else
5875#endif
5876 {
5877 p[0] = c2;
5878 p[1] = c;
5879 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
5880 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005881 }
5882 else
5883 /* If this swap doesn't work then SWAP3 won't either. */
5884 sp->ts_state = STATE_REP_INI;
5885 break;
5886
Bram Moolenaarea424162005-06-16 21:51:00 +00005887 case STATE_UNSWAP:
5888 /* Undo the STATE_SWAP swap: "21" -> "12". */
5889 p = fword + sp->ts_fidx;
5890#ifdef FEAT_MBYTE
5891 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005892 {
Bram Moolenaarea424162005-06-16 21:51:00 +00005893 n = MB_BYTE2LEN(*p);
5894 c = mb_ptr2char(p + n);
5895 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
5896 mb_char2bytes(c, p);
5897 }
5898 else
5899#endif
5900 {
5901 c = *p;
5902 *p = p[1];
5903 p[1] = c;
5904 }
5905 /*FALLTHROUGH*/
5906
5907 case STATE_SWAP3:
5908 /* Swap two bytes, skipping one: "123" -> "321". We change
5909 * "fword" here, it's changed back afterwards. */
5910 p = fword + sp->ts_fidx;
5911#ifdef FEAT_MBYTE
5912 if (has_mbyte)
5913 {
5914 n = mb_ptr2len_check(p);
5915 c = mb_ptr2char(p);
5916 fl = mb_ptr2len_check(p + n);
5917 c2 = mb_ptr2char(p + n);
5918 c3 = mb_ptr2char(p + n + fl);
5919 }
5920 else
5921#endif
5922 {
5923 c = *p;
5924 c2 = p[1];
5925 c3 = p[2];
5926 }
5927
5928 /* When characters are identical: "121" then SWAP3 result is
5929 * identical, ROT3L result is same as SWAP: "211", ROT3L
5930 * result is same as SWAP on next char: "112". Thus skip all
5931 * swapping. Also skip when c3 is NUL. */
5932 if (c == c3 || c3 == NUL)
5933 {
5934 sp->ts_state = STATE_REP_INI;
5935 break;
5936 }
5937 if (try_deeper(su, stack, depth, SCORE_SWAP3))
5938 {
5939 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005940 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005941#ifdef FEAT_MBYTE
5942 if (has_mbyte)
5943 {
5944 tl = mb_char2len(c3);
5945 mch_memmove(p, p + n + fl, tl);
5946 mb_char2bytes(c2, p + tl);
5947 mb_char2bytes(c, p + fl + tl);
5948 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
5949 }
5950 else
5951#endif
5952 {
5953 p[0] = p[2];
5954 p[2] = c;
5955 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5956 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005957 }
5958 else
5959 sp->ts_state = STATE_REP_INI;
5960 break;
5961
Bram Moolenaarea424162005-06-16 21:51:00 +00005962 case STATE_UNSWAP3:
5963 /* Undo STATE_SWAP3: "321" -> "123" */
5964 p = fword + sp->ts_fidx;
5965#ifdef FEAT_MBYTE
5966 if (has_mbyte)
5967 {
5968 n = MB_BYTE2LEN(*p);
5969 c2 = mb_ptr2char(p + n);
5970 fl = MB_BYTE2LEN(p[n]);
5971 c = mb_ptr2char(p + n + fl);
5972 tl = MB_BYTE2LEN(p[n + fl]);
5973 mch_memmove(p + fl + tl, p, n);
5974 mb_char2bytes(c, p);
5975 mb_char2bytes(c2, p + tl);
5976 }
5977 else
5978#endif
5979 {
5980 c = *p;
5981 *p = p[2];
5982 p[2] = c;
5983 }
Bram Moolenaarea424162005-06-16 21:51:00 +00005984
Bram Moolenaarea424162005-06-16 21:51:00 +00005985 /* Rotate three characters left: "123" -> "231". We change
5986 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005987 if (try_deeper(su, stack, depth, SCORE_SWAP3))
5988 {
Bram Moolenaarea424162005-06-16 21:51:00 +00005989 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005990 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005991 p = fword + sp->ts_fidx;
5992#ifdef FEAT_MBYTE
5993 if (has_mbyte)
5994 {
5995 n = mb_ptr2len_check(p);
5996 c = mb_ptr2char(p);
5997 fl = mb_ptr2len_check(p + n);
5998 fl += mb_ptr2len_check(p + n + fl);
5999 mch_memmove(p, p + n, fl);
6000 mb_char2bytes(c, p + fl);
6001 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
6002 }
6003 else
6004#endif
6005 {
6006 c = *p;
6007 *p = p[1];
6008 p[1] = p[2];
6009 p[2] = c;
6010 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
6011 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006012 }
6013 else
6014 sp->ts_state = STATE_REP_INI;
6015 break;
6016
Bram Moolenaarea424162005-06-16 21:51:00 +00006017 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +00006018 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00006019 p = fword + sp->ts_fidx;
6020#ifdef FEAT_MBYTE
6021 if (has_mbyte)
6022 {
6023 n = MB_BYTE2LEN(*p);
6024 n += MB_BYTE2LEN(p[n]);
6025 c = mb_ptr2char(p + n);
6026 tl = MB_BYTE2LEN(p[n]);
6027 mch_memmove(p + tl, p, n);
6028 mb_char2bytes(c, p);
6029 }
6030 else
6031#endif
6032 {
6033 c = p[2];
6034 p[2] = p[1];
6035 p[1] = *p;
6036 *p = c;
6037 }
Bram Moolenaarea424162005-06-16 21:51:00 +00006038
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006039 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00006040 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006041 if (try_deeper(su, stack, depth, SCORE_SWAP3))
6042 {
Bram Moolenaarea424162005-06-16 21:51:00 +00006043 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006044 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00006045 p = fword + sp->ts_fidx;
6046#ifdef FEAT_MBYTE
6047 if (has_mbyte)
6048 {
6049 n = mb_ptr2len_check(p);
6050 n += mb_ptr2len_check(p + n);
6051 c = mb_ptr2char(p + n);
6052 tl = mb_ptr2len_check(p + n);
6053 mch_memmove(p + tl, p, n);
6054 mb_char2bytes(c, p);
6055 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
6056 }
6057 else
6058#endif
6059 {
6060 c = p[2];
6061 p[2] = p[1];
6062 p[1] = *p;
6063 *p = c;
6064 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
6065 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006066 }
6067 else
6068 sp->ts_state = STATE_REP_INI;
6069 break;
6070
Bram Moolenaarea424162005-06-16 21:51:00 +00006071 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +00006072 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00006073 p = fword + sp->ts_fidx;
6074#ifdef FEAT_MBYTE
6075 if (has_mbyte)
6076 {
6077 c = mb_ptr2char(p);
6078 tl = MB_BYTE2LEN(*p);
6079 n = MB_BYTE2LEN(p[tl]);
6080 n += MB_BYTE2LEN(p[tl + n]);
6081 mch_memmove(p, p + tl, n);
6082 mb_char2bytes(c, p + n);
6083 }
6084 else
6085#endif
6086 {
6087 c = *p;
6088 *p = p[1];
6089 p[1] = p[2];
6090 p[2] = c;
6091 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006092 /*FALLTHROUGH*/
6093
6094 case STATE_REP_INI:
6095 /* Check if matching with REP items from the .aff file would
6096 * work. Quickly skip if there are no REP items or the score
6097 * is going to be too high anyway. */
6098 gap = &lp->lp_slang->sl_rep;
6099 if (gap->ga_len == 0
6100 || sp->ts_score + SCORE_REP >= su->su_maxscore)
6101 {
6102 sp->ts_state = STATE_FINAL;
6103 break;
6104 }
6105
6106 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00006107 * may match. If the index is -1 there is none. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006108 sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]];
6109 if (sp->ts_curi < 0)
6110 {
6111 sp->ts_state = STATE_FINAL;
6112 break;
6113 }
6114
6115 sp->ts_state = STATE_REP;
6116 /*FALLTHROUGH*/
6117
6118 case STATE_REP:
6119 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00006120 * match replace the characters and check if the resulting
6121 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006122 p = fword + sp->ts_fidx;
6123
6124 gap = &lp->lp_slang->sl_rep;
6125 while (sp->ts_curi < gap->ga_len)
6126 {
6127 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
6128 if (*ftp->ft_from != *p)
6129 {
6130 /* past possible matching entries */
6131 sp->ts_curi = gap->ga_len;
6132 break;
6133 }
6134 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
6135 && try_deeper(su, stack, depth, SCORE_REP))
6136 {
6137 /* Need to undo this afterwards. */
6138 sp->ts_state = STATE_REP_UNDO;
6139
6140 /* Change the "from" to the "to" string. */
6141 ++depth;
6142 fl = STRLEN(ftp->ft_from);
6143 tl = STRLEN(ftp->ft_to);
6144 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006145 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006146 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006147 repextra += tl - fl;
6148 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006149 mch_memmove(p, ftp->ft_to, tl);
6150 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +00006151#ifdef FEAT_MBYTE
6152 stack[depth].ts_tcharlen = 0;
6153#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006154 break;
6155 }
6156 }
6157
6158 if (sp->ts_curi >= gap->ga_len)
6159 /* No (more) matches. */
6160 sp->ts_state = STATE_FINAL;
6161
6162 break;
6163
6164 case STATE_REP_UNDO:
6165 /* Undo a REP replacement and continue with the next one. */
6166 ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data
6167 + sp->ts_curi - 1;
6168 fl = STRLEN(ftp->ft_from);
6169 tl = STRLEN(ftp->ft_to);
6170 p = fword + sp->ts_fidx;
6171 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006172 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006173 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006174 repextra -= tl - fl;
6175 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006176 mch_memmove(p, ftp->ft_from, fl);
6177 sp->ts_state = STATE_REP;
6178 break;
6179
6180 default:
6181 /* Did all possible states at this level, go up one level. */
6182 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006183
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006184 /* Don't check for CTRL-C too often, it takes time. */
6185 line_breakcheck();
6186 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006187 }
6188 }
6189}
6190
6191/*
6192 * Try going one level deeper in the tree.
6193 */
6194 static int
6195try_deeper(su, stack, depth, score_add)
6196 suginfo_T *su;
6197 trystate_T *stack;
6198 int depth;
6199 int score_add;
6200{
6201 int newscore;
6202
6203 /* Refuse to go deeper if the scrore is getting too big. */
6204 newscore = stack[depth].ts_score + score_add;
6205 if (newscore >= su->su_maxscore)
6206 return FALSE;
6207
Bram Moolenaarea424162005-06-16 21:51:00 +00006208 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006209 stack[depth + 1].ts_state = STATE_START;
6210 stack[depth + 1].ts_score = newscore;
6211 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006212 return TRUE;
6213}
6214
6215/*
6216 * "fword" is a good word with case folded. Find the matching keep-case
6217 * words and put it in "kword".
6218 * Theoretically there could be several keep-case words that result in the
6219 * same case-folded word, but we only find one...
6220 */
6221 static void
6222find_keepcap_word(slang, fword, kword)
6223 slang_T *slang;
6224 char_u *fword;
6225 char_u *kword;
6226{
6227 char_u uword[MAXWLEN]; /* "fword" in upper-case */
6228 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006229 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006230
6231 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006232 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006233 int round[MAXWLEN];
6234 int fwordidx[MAXWLEN];
6235 int uwordidx[MAXWLEN];
6236 int kwordlen[MAXWLEN];
6237
6238 int flen, ulen;
6239 int l;
6240 int len;
6241 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006242 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006243 char_u *p;
6244 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006245 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006246
6247 if (byts == NULL)
6248 {
6249 /* array is empty: "cannot happen" */
6250 *kword = NUL;
6251 return;
6252 }
6253
6254 /* Make an all-cap version of "fword". */
6255 allcap_copy(fword, uword);
6256
6257 /*
6258 * Each character needs to be tried both case-folded and upper-case.
6259 * All this gets very complicated if we keep in mind that changing case
6260 * may change the byte length of a multi-byte character...
6261 */
6262 depth = 0;
6263 arridx[0] = 0;
6264 round[0] = 0;
6265 fwordidx[0] = 0;
6266 uwordidx[0] = 0;
6267 kwordlen[0] = 0;
6268 while (depth >= 0)
6269 {
6270 if (fword[fwordidx[depth]] == NUL)
6271 {
6272 /* We are at the end of "fword". If the tree allows a word to end
6273 * here we have found a match. */
6274 if (byts[arridx[depth] + 1] == 0)
6275 {
6276 kword[kwordlen[depth]] = NUL;
6277 return;
6278 }
6279
6280 /* kword is getting too long, continue one level up */
6281 --depth;
6282 }
6283 else if (++round[depth] > 2)
6284 {
6285 /* tried both fold-case and upper-case character, continue one
6286 * level up */
6287 --depth;
6288 }
6289 else
6290 {
6291 /*
6292 * round[depth] == 1: Try using the folded-case character.
6293 * round[depth] == 2: Try using the upper-case character.
6294 */
6295#ifdef FEAT_MBYTE
6296 if (has_mbyte)
6297 {
6298 flen = mb_ptr2len_check(fword + fwordidx[depth]);
6299 ulen = mb_ptr2len_check(uword + uwordidx[depth]);
6300 }
6301 else
6302#endif
6303 ulen = flen = 1;
6304 if (round[depth] == 1)
6305 {
6306 p = fword + fwordidx[depth];
6307 l = flen;
6308 }
6309 else
6310 {
6311 p = uword + uwordidx[depth];
6312 l = ulen;
6313 }
6314
6315 for (tryidx = arridx[depth]; l > 0; --l)
6316 {
6317 /* Perform a binary search in the list of accepted bytes. */
6318 len = byts[tryidx++];
6319 c = *p++;
6320 lo = tryidx;
6321 hi = tryidx + len - 1;
6322 while (lo < hi)
6323 {
6324 m = (lo + hi) / 2;
6325 if (byts[m] > c)
6326 hi = m - 1;
6327 else if (byts[m] < c)
6328 lo = m + 1;
6329 else
6330 {
6331 lo = hi = m;
6332 break;
6333 }
6334 }
6335
6336 /* Stop if there is no matching byte. */
6337 if (hi < lo || byts[lo] != c)
6338 break;
6339
6340 /* Continue at the child (if there is one). */
6341 tryidx = idxs[lo];
6342 }
6343
6344 if (l == 0)
6345 {
6346 /*
6347 * Found the matching char. Copy it to "kword" and go a
6348 * level deeper.
6349 */
6350 if (round[depth] == 1)
6351 {
6352 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
6353 flen);
6354 kwordlen[depth + 1] = kwordlen[depth] + flen;
6355 }
6356 else
6357 {
6358 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
6359 ulen);
6360 kwordlen[depth + 1] = kwordlen[depth] + ulen;
6361 }
6362 fwordidx[depth + 1] = fwordidx[depth] + flen;
6363 uwordidx[depth + 1] = uwordidx[depth] + ulen;
6364
6365 ++depth;
6366 arridx[depth] = tryidx;
6367 round[depth] = 0;
6368 }
6369 }
6370 }
6371
6372 /* Didn't find it: "cannot happen". */
6373 *kword = NUL;
6374}
6375
6376/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006377 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
6378 * su->su_sga.
6379 */
6380 static void
6381score_comp_sal(su)
6382 suginfo_T *su;
6383{
6384 langp_T *lp;
6385 char_u badsound[MAXWLEN];
6386 int i;
6387 suggest_T *stp;
6388 suggest_T *sstp;
6389 char_u fword[MAXWLEN];
6390 char_u goodsound[MAXWLEN];
6391 int score;
6392
6393 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
6394 return;
6395
6396 /* Use the sound-folding of the first language that supports it. */
6397 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
6398 lp->lp_slang != NULL; ++lp)
6399 if (lp->lp_slang->sl_sal.ga_len > 0)
6400 {
6401 /* soundfold the bad word */
6402 spell_soundfold(lp->lp_slang, su->su_fbadword, badsound);
6403
6404 for (i = 0; i < su->su_ga.ga_len; ++i)
6405 {
6406 stp = &SUG(su->su_ga, i);
6407
6408 /* Case-fold the suggested word and sound-fold it. */
6409 (void)spell_casefold(stp->st_word, STRLEN(stp->st_word),
6410 fword, MAXWLEN);
6411 spell_soundfold(lp->lp_slang, fword, goodsound);
6412 score = soundalike_score(goodsound, badsound);
6413 if (score < SCORE_MAXMAX)
6414 {
6415 /* Add the suggestion. */
6416 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
6417 sstp->st_word = vim_strsave(stp->st_word);
6418 if (sstp->st_word != NULL)
6419 {
6420 sstp->st_score = score;
6421 sstp->st_altscore = 0;
6422 sstp->st_orglen = stp->st_orglen;
6423 ++su->su_sga.ga_len;
6424 }
6425 }
6426 }
6427 break;
6428 }
6429}
6430
6431/*
6432 * Combine the list of suggestions in su->su_ga and su->su_sga.
6433 * They are intwined.
6434 */
6435 static void
6436score_combine(su)
6437 suginfo_T *su;
6438{
6439 int i;
6440 int j;
6441 garray_T ga;
6442 garray_T *gap;
6443 langp_T *lp;
6444 suggest_T *stp;
6445 char_u *p;
6446 char_u badsound[MAXWLEN];
Bram Moolenaar0c405862005-06-22 22:26:26 +00006447 char_u badsound2[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006448 char_u goodsound[MAXWLEN];
6449 char_u fword[MAXWLEN];
6450 int round;
6451
6452 /* Add the alternate score to su_ga. */
6453 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
6454 lp->lp_slang != NULL; ++lp)
6455 {
6456 if (lp->lp_slang->sl_sal.ga_len > 0)
6457 {
6458 /* soundfold the bad word */
6459 spell_soundfold(lp->lp_slang, su->su_fbadword, badsound);
6460
6461 for (i = 0; i < su->su_ga.ga_len; ++i)
6462 {
6463 stp = &SUG(su->su_ga, i);
6464
Bram Moolenaar0c405862005-06-22 22:26:26 +00006465 if (stp->st_orglen <= su->su_badlen)
6466 p = badsound;
6467 else
6468 {
6469 /* soundfold the bad word with a different length */
6470 (void)spell_casefold(su->su_badptr, stp->st_orglen,
6471 fword, MAXWLEN);
6472 spell_soundfold(lp->lp_slang, fword, badsound2);
6473 p = badsound2;
6474 }
6475
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006476 /* Case-fold the word, sound-fold the word and compute the
6477 * score for the difference. */
6478 (void)spell_casefold(stp->st_word, STRLEN(stp->st_word),
Bram Moolenaar0c405862005-06-22 22:26:26 +00006479 fword, MAXWLEN);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006480 spell_soundfold(lp->lp_slang, fword, goodsound);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006481
6482 stp->st_altscore = soundalike_score(goodsound, p);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006483 if (stp->st_altscore == SCORE_MAXMAX)
6484 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
6485 else
6486 stp->st_score = (stp->st_score * 3
6487 + stp->st_altscore) / 4;
6488 stp->st_salscore = FALSE;
6489 }
6490 break;
6491 }
6492 }
6493
6494 /* Add the alternate score to su_sga. */
6495 for (i = 0; i < su->su_sga.ga_len; ++i)
6496 {
6497 stp = &SUG(su->su_sga, i);
6498 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
6499 if (stp->st_score == SCORE_MAXMAX)
6500 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
6501 else
6502 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
6503 stp->st_salscore = TRUE;
6504 }
6505
6506 /* Sort the suggestions and truncate at "maxcount" for both lists. */
6507 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
6508 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
6509
6510 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
6511 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
6512 return;
6513
6514 stp = &SUG(ga, 0);
6515 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
6516 {
6517 /* round 1: get a suggestion from su_ga
6518 * round 2: get a suggestion from su_sga */
6519 for (round = 1; round <= 2; ++round)
6520 {
6521 gap = round == 1 ? &su->su_ga : &su->su_sga;
6522 if (i < gap->ga_len)
6523 {
6524 /* Don't add a word if it's already there. */
6525 p = SUG(*gap, i).st_word;
6526 for (j = 0; j < ga.ga_len; ++j)
6527 if (STRCMP(stp[j].st_word, p) == 0)
6528 break;
6529 if (j == ga.ga_len)
6530 stp[ga.ga_len++] = SUG(*gap, i);
6531 else
6532 vim_free(p);
6533 }
6534 }
6535 }
6536
6537 ga_clear(&su->su_ga);
6538 ga_clear(&su->su_sga);
6539
6540 /* Truncate the list to the number of suggestions that will be displayed. */
6541 if (ga.ga_len > su->su_maxcount)
6542 {
6543 for (i = su->su_maxcount; i < ga.ga_len; ++i)
6544 vim_free(stp[i].st_word);
6545 ga.ga_len = su->su_maxcount;
6546 }
6547
6548 su->su_ga = ga;
6549}
6550
6551/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006552 * Find suggestions by comparing the word in a sound-a-like form.
6553 */
6554 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00006555suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006556 suginfo_T *su;
6557{
6558 char_u salword[MAXWLEN];
6559 char_u tword[MAXWLEN];
6560 char_u tfword[MAXWLEN];
6561 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006562 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006563 int curi[MAXWLEN];
6564 langp_T *lp;
6565 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006566 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006567 int depth;
6568 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006569 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006570 int round;
6571 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006572 int sound_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006573
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006574 /* Do this for all languages that support sound folding. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006575 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
6576 lp->lp_slang != NULL; ++lp)
6577 {
6578 if (lp->lp_slang->sl_sal.ga_len > 0)
6579 {
6580 /* soundfold the bad word */
6581 spell_soundfold(lp->lp_slang, su->su_fbadword, salword);
6582
6583 /*
6584 * Go through the whole tree, soundfold each word and compare.
6585 * round 1: use the case-folded tree.
6586 * round 2: use the keep-case tree.
6587 */
6588 for (round = 1; round <= 2; ++round)
6589 {
6590 if (round == 1)
6591 {
6592 byts = lp->lp_slang->sl_fbyts;
6593 idxs = lp->lp_slang->sl_fidxs;
6594 }
6595 else
6596 {
6597 byts = lp->lp_slang->sl_kbyts;
6598 idxs = lp->lp_slang->sl_kidxs;
6599 }
6600
6601 depth = 0;
6602 arridx[0] = 0;
6603 curi[0] = 1;
6604 while (depth >= 0 && !got_int)
6605 {
6606 if (curi[depth] > byts[arridx[depth]])
6607 /* Done all bytes at this node, go up one level. */
6608 --depth;
6609 else
6610 {
6611 /* Do one more byte at this node. */
6612 n = arridx[depth] + curi[depth];
6613 ++curi[depth];
6614 c = byts[n];
6615 if (c == 0)
6616 {
6617 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006618 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006619 if (round == 2 || (flags & WF_KEEPCAP) == 0)
6620 {
6621 tword[depth] = NUL;
6622 if (round == 1)
6623 spell_soundfold(lp->lp_slang,
6624 tword, tsalword);
6625 else
6626 {
6627 /* In keep-case tree need to case-fold the
6628 * word. */
6629 (void)spell_casefold(tword, depth,
6630 tfword, MAXWLEN);
6631 spell_soundfold(lp->lp_slang,
6632 tfword, tsalword);
6633 }
6634
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006635 /* Compute the edit distance between the
6636 * sound-a-like words. */
6637 sound_score = soundalike_score(salword,
6638 tsalword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006639 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006640 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006641 char_u cword[MAXWLEN];
6642 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006643 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006644
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006645 if (round == 1 && flags != 0)
6646 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006647 /* Need to fix case according to
6648 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006649 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006650 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006651 }
6652 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006653 p = tword;
6654
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006655 if (sps_flags & SPS_DOUBLE)
6656 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00006657 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006658 sound_score, FALSE);
6659 else
6660 {
6661 /* Compute the score. */
6662 score = spell_edit_score(
6663 su->su_badword, p);
6664 if (sps_flags & SPS_BEST)
6665 /* give a bonus for the good word
6666 * sounding the same as the bad
6667 * word */
6668 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00006669 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006670 RESCORE(score, sound_score),
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006671 TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006672 else
6673 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00006674 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006675 score + sound_score, FALSE);
6676 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006677 }
6678 }
6679
6680 /* Skip over other NUL bytes. */
6681 while (byts[n + 1] == 0)
6682 {
6683 ++n;
6684 ++curi[depth];
6685 }
6686 }
6687 else
6688 {
6689 /* Normal char, go one level deeper. */
6690 tword[depth++] = c;
6691 arridx[depth] = idxs[n];
6692 curi[depth] = 1;
6693 }
6694 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006695
6696 line_breakcheck();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006697 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006698 }
6699 }
6700 }
6701}
6702
6703/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006704 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006705 */
6706 static void
6707make_case_word(fword, cword, flags)
6708 char_u *fword;
6709 char_u *cword;
6710 int flags;
6711{
6712 if (flags & WF_ALLCAP)
6713 /* Make it all upper-case */
6714 allcap_copy(fword, cword);
6715 else if (flags & WF_ONECAP)
6716 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006717 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006718 else
6719 /* Use goodword as-is. */
6720 STRCPY(cword, fword);
6721}
6722
Bram Moolenaarea424162005-06-16 21:51:00 +00006723/*
6724 * Use map string "map" for languages "lp".
6725 */
6726 static void
6727set_map_str(lp, map)
6728 slang_T *lp;
6729 char_u *map;
6730{
6731 char_u *p;
6732 int headc = 0;
6733 int c;
6734 int i;
6735
6736 if (*map == NUL)
6737 {
6738 lp->sl_has_map = FALSE;
6739 return;
6740 }
6741 lp->sl_has_map = TRUE;
6742
6743 /* Init the array and hash table empty. */
6744 for (i = 0; i < 256; ++i)
6745 lp->sl_map_array[i] = 0;
6746#ifdef FEAT_MBYTE
6747 hash_init(&lp->sl_map_hash);
6748#endif
6749
6750 /*
6751 * The similar characters are stored separated with slashes:
6752 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
6753 * before the same slash. For characters above 255 sl_map_hash is used.
6754 */
6755 for (p = map; *p != NUL; )
6756 {
6757#ifdef FEAT_MBYTE
6758 c = mb_ptr2char_adv(&p);
6759#else
6760 c = *p++;
6761#endif
6762 if (c == '/')
6763 headc = 0;
6764 else
6765 {
6766 if (headc == 0)
6767 headc = c;
6768
6769#ifdef FEAT_MBYTE
6770 /* Characters above 255 don't fit in sl_map_array[], put them in
6771 * the hash table. Each entry is the char, a NUL the headchar and
6772 * a NUL. */
6773 if (c >= 256)
6774 {
6775 int cl = mb_char2len(c);
6776 int headcl = mb_char2len(headc);
6777 char_u *b;
6778 hash_T hash;
6779 hashitem_T *hi;
6780
6781 b = alloc((unsigned)(cl + headcl + 2));
6782 if (b == NULL)
6783 return;
6784 mb_char2bytes(c, b);
6785 b[cl] = NUL;
6786 mb_char2bytes(headc, b + cl + 1);
6787 b[cl + 1 + headcl] = NUL;
6788 hash = hash_hash(b);
6789 hi = hash_lookup(&lp->sl_map_hash, b, hash);
6790 if (HASHITEM_EMPTY(hi))
6791 hash_add_item(&lp->sl_map_hash, hi, b, hash);
6792 else
6793 {
6794 /* This should have been checked when generating the .spl
6795 * file. */
6796 EMSG(_("E999: duplicate char in MAP entry"));
6797 vim_free(b);
6798 }
6799 }
6800 else
6801#endif
6802 lp->sl_map_array[c] = headc;
6803 }
6804 }
6805}
6806
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006807/*
6808 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
6809 * lines in the .aff file.
6810 */
6811 static int
6812similar_chars(slang, c1, c2)
6813 slang_T *slang;
6814 int c1;
6815 int c2;
6816{
Bram Moolenaarea424162005-06-16 21:51:00 +00006817 int m1, m2;
6818#ifdef FEAT_MBYTE
6819 char_u buf[MB_MAXBYTES];
6820 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006821
Bram Moolenaarea424162005-06-16 21:51:00 +00006822 if (c1 >= 256)
6823 {
6824 buf[mb_char2bytes(c1, buf)] = 0;
6825 hi = hash_find(&slang->sl_map_hash, buf);
6826 if (HASHITEM_EMPTY(hi))
6827 m1 = 0;
6828 else
6829 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
6830 }
6831 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006832#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00006833 m1 = slang->sl_map_array[c1];
6834 if (m1 == 0)
6835 return FALSE;
6836
6837
6838#ifdef FEAT_MBYTE
6839 if (c2 >= 256)
6840 {
6841 buf[mb_char2bytes(c2, buf)] = 0;
6842 hi = hash_find(&slang->sl_map_hash, buf);
6843 if (HASHITEM_EMPTY(hi))
6844 m2 = 0;
6845 else
6846 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
6847 }
6848 else
6849#endif
6850 m2 = slang->sl_map_array[c2];
6851
6852 return m1 == m2;
6853}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006854
6855/*
6856 * Add a suggestion to the list of suggestions.
6857 * Do not add a duplicate suggestion or suggestions with a bad score.
6858 * When "use_score" is not zero it's used, otherwise the score is computed
6859 * with spell_edit_score().
6860 */
6861 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00006862add_suggestion(su, gap, goodword, badlen, score, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006863 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006864 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006865 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006866 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006867 int score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006868 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006869{
6870 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006871 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006872 char_u *p = NULL;
6873 int c = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006874
6875 /* Check that the word wasn't banned. */
6876 if (was_banned(su, goodword))
6877 return;
6878
Bram Moolenaar0c405862005-06-22 22:26:26 +00006879 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
6880 * Remove the common part from "goodword". */
6881 i = badlen - su->su_badlen;
6882 if (i > 0)
6883 {
6884 /* This assumes there was no case folding or it didn't change the
6885 * length... */
6886 p = goodword + STRLEN(goodword) - i;
6887 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
6888 {
6889 badlen = su->su_badlen;
6890 c = *p;
6891 *p = NUL;
6892 }
6893 else
6894 p = NULL;
6895 }
6896
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006897 if (score <= su->su_maxscore)
6898 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006899 /* Check if the word is already there. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006900 stp = &SUG(*gap, 0);
6901 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006902 if (STRCMP(stp[i].st_word, goodword) == 0)
6903 {
6904 /* Found it. Remember the lowest score. */
6905 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006906 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006907 stp[i].st_score = score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006908 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006909 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006910 break;
6911 }
6912
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006913 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006914 {
6915 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006916 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006917 stp->st_word = vim_strsave(goodword);
6918 if (stp->st_word != NULL)
6919 {
6920 stp->st_score = score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006921 stp->st_altscore = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006922 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006923 stp->st_orglen = badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006924 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006925
6926 /* If we have too many suggestions now, sort the list and keep
6927 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006928 if (gap->ga_len > SUG_MAX_COUNT(su))
6929 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
6930 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006931 }
6932 }
6933 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00006934
6935 if (p != NULL)
6936 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006937}
6938
6939/*
6940 * Add a word to be banned.
6941 */
6942 static void
6943add_banned(su, word)
6944 suginfo_T *su;
6945 char_u *word;
6946{
6947 char_u *s = vim_strsave(word);
6948 hash_T hash;
6949 hashitem_T *hi;
6950
6951 if (s != NULL)
6952 {
6953 hash = hash_hash(s);
6954 hi = hash_lookup(&su->su_banned, s, hash);
6955 if (HASHITEM_EMPTY(hi))
6956 hash_add_item(&su->su_banned, hi, s, hash);
6957 }
6958}
6959
6960/*
6961 * Return TRUE if a word appears in the list of banned words.
6962 */
6963 static int
6964was_banned(su, word)
6965 suginfo_T *su;
6966 char_u *word;
6967{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006968 hashitem_T *hi = hash_find(&su->su_banned, word);
6969
6970 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006971}
6972
6973/*
6974 * Free the banned words in "su".
6975 */
6976 static void
6977free_banned(su)
6978 suginfo_T *su;
6979{
6980 int todo;
6981 hashitem_T *hi;
6982
6983 todo = su->su_banned.ht_used;
6984 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
6985 {
6986 if (!HASHITEM_EMPTY(hi))
6987 {
6988 vim_free(hi->hi_key);
6989 --todo;
6990 }
6991 }
6992 hash_clear(&su->su_banned);
6993}
6994
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006995/*
6996 * Recompute the score if sound-folding is possible. This is slow,
6997 * thus only done for the final results.
6998 */
6999 static void
7000rescore_suggestions(su)
7001 suginfo_T *su;
7002{
7003 langp_T *lp;
7004 suggest_T *stp;
7005 char_u sal_badword[MAXWLEN];
Bram Moolenaar0c405862005-06-22 22:26:26 +00007006 char_u tword[MAXWLEN];
7007 char_u salword[MAXWLEN];
7008 char_u *p;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007009 int score;
7010 int i;
7011
7012 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7013 lp->lp_slang != NULL; ++lp)
7014 {
7015 if (lp->lp_slang->sl_sal.ga_len > 0)
7016 {
7017 /* soundfold the bad word */
7018 spell_soundfold(lp->lp_slang, su->su_fbadword, sal_badword);
7019
7020 for (i = 0; i < su->su_ga.ga_len; ++i)
7021 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007022 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007023 if (!stp->st_had_bonus)
7024 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00007025 if (stp->st_orglen <= su->su_badlen)
7026 p = sal_badword;
7027 else
7028 {
7029 /* soundfold the bad word with a different length */
7030 (void)spell_casefold(su->su_badptr, stp->st_orglen,
7031 tword, MAXWLEN);
7032 spell_soundfold(lp->lp_slang, tword, salword);
7033 p = salword;
7034 }
7035 score = spell_sound_score(lp->lp_slang, stp->st_word, p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007036 stp->st_score = RESCORE(stp->st_score, score);
7037 }
7038 }
7039 break;
7040 }
7041 }
7042}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007043
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007044static int
7045#ifdef __BORLANDC__
7046_RTLENTRYF
7047#endif
7048sug_compare __ARGS((const void *s1, const void *s2));
7049
7050/*
7051 * Function given to qsort() to sort the suggestions on st_score.
7052 */
7053 static int
7054#ifdef __BORLANDC__
7055_RTLENTRYF
7056#endif
7057sug_compare(s1, s2)
7058 const void *s1;
7059 const void *s2;
7060{
7061 suggest_T *p1 = (suggest_T *)s1;
7062 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007063 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007064
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007065 if (n == 0)
7066 return p1->st_altscore - p2->st_altscore;
7067 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007068}
7069
7070/*
7071 * Cleanup the suggestions:
7072 * - Sort on score.
7073 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007074 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007075 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007076 static int
7077cleanup_suggestions(gap, maxscore, keep)
7078 garray_T *gap;
7079 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007080 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007081{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007082 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007083 int i;
7084
7085 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007086 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007087
7088 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007089 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007090 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007091 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007092 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007093 gap->ga_len = keep;
7094 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007095 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007096 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007097}
7098
7099/*
7100 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
7101 */
7102 static void
7103spell_soundfold(slang, inword, res)
7104 slang_T *slang;
7105 char_u *inword;
7106 char_u *res;
7107{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007108 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007109 char_u word[MAXWLEN];
7110#ifdef FEAT_MBYTE
7111 int l;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007112 int found_mbyte = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007113#endif
7114 char_u *s;
7115 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007116 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007117 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007118 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007119 int n, k = 0;
7120 int z0;
7121 int k0;
7122 int n0;
7123 int c;
7124 int pri;
7125 int p0 = -333;
7126 int c0;
7127
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007128 /* Remove accents, if wanted. We actually remove all non-word characters.
7129 * But keep white space. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007130 if (slang->sl_rem_accents)
7131 {
7132 t = word;
7133 for (s = inword; *s != NUL; )
7134 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007135 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007136 {
7137 *t++ = ' ';
7138 s = skipwhite(s);
7139 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007140#ifdef FEAT_MBYTE
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007141 else if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007142 {
7143 l = mb_ptr2len_check(s);
7144 if (SPELL_ISWORDP(s))
7145 {
7146 mch_memmove(t, s, l);
7147 t += l;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007148 if (l > 1)
7149 found_mbyte = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007150 }
7151 s += l;
7152 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007153#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007154 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007155 {
7156 if (SPELL_ISWORDP(s))
7157 *t++ = *s;
7158 ++s;
7159 }
7160 }
7161 *t = NUL;
7162 }
7163 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007164 {
7165#ifdef FEAT_MBYTE
7166 if (has_mbyte)
7167 for (s = inword; *s != NUL; s += l)
7168 if ((l = mb_ptr2len_check(s)) > 1)
7169 {
7170 found_mbyte = TRUE;
7171 break;
7172 }
7173#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007174 STRCPY(word, inword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007175 }
7176
7177#ifdef FEAT_MBYTE
7178 /* If there are multi-byte characters in the word return it as-is, because
7179 * the following won't work. */
7180 if (found_mbyte)
7181 {
7182 STRCPY(res, word);
7183 return;
7184 }
7185#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007186
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007187 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007188
7189 /*
7190 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007191 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007192 * TODO: support for multi-byte chars.
7193 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007194 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007195 while ((c = word[i]) != NUL)
7196 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007197 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007198 n = slang->sl_sal_first[c];
7199 z0 = 0;
7200
7201 if (n >= 0)
7202 {
7203 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007204 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007205 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007206 /* Quickly skip entries that don't match the word. Most
7207 * entries are less then three chars, optimize for that. */
7208 k = smp[n].sm_leadlen;
7209 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007210 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007211 if (word[i + 1] != s[1])
7212 continue;
7213 if (k > 2)
7214 {
7215 for (j = 2; j < k; ++j)
7216 if (word[i + j] != s[j])
7217 break;
7218 if (j < k)
7219 continue;
7220 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007221 }
7222
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007223 if ((pf = smp[n].sm_oneoff) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007224 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007225 /* Check for match with one of the chars in "sm_oneoff". */
7226 while (*pf != NUL && *pf != word[i + k])
7227 ++pf;
7228 if (*pf == NUL)
7229 continue;
7230 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007231 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007232 s = smp[n].sm_rules;
7233 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007234
7235 p0 = *s;
7236 k0 = k;
7237 while (*s == '-' && k > 1)
7238 {
7239 k--;
7240 s++;
7241 }
7242 if (*s == '<')
7243 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007244 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007245 {
7246 /* determine priority */
7247 pri = *s - '0';
7248 s++;
7249 }
7250 if (*s == '^' && *(s + 1) == '^')
7251 s++;
7252
7253 if (*s == NUL
7254 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007255 && (i == 0 || !(word[i - 1] == ' '
7256 || SPELL_ISWORDP(word + i - 1)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007257 && (*(s + 1) != '$'
7258 || (!SPELL_ISWORDP(word + i + k0))))
7259 || (*s == '$' && i > 0
7260 && SPELL_ISWORDP(word + i - 1)
7261 && (!SPELL_ISWORDP(word + i + k0))))
7262 {
7263 /* search for followup rules, if: */
7264 /* followup and k > 1 and NO '-' in searchstring */
7265 c0 = word[i + k - 1];
7266 n0 = slang->sl_sal_first[c0];
7267
7268 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007269 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007270 {
7271 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007272 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007273 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007274 /* Quickly skip entries that don't match the word.
7275 * */
7276 k0 = smp[n0].sm_leadlen;
7277 if (k0 > 1)
7278 {
7279 if (word[i + k] != s[1])
7280 continue;
7281 if (k0 > 2)
7282 {
7283 pf = word + i + k + 1;
7284 for (j = 2; j < k0; ++j)
7285 if (*pf++ != s[j])
7286 break;
7287 if (j < k0)
7288 continue;
7289 }
7290 }
7291 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007292
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007293 if ((pf = smp[n0].sm_oneoff) != NULL)
7294 {
7295 /* Check for match with one of the chars in
7296 * "sm_oneoff". */
7297 while (*pf != NUL && *pf != word[i + k0])
7298 ++pf;
7299 if (*pf == NUL)
7300 continue;
7301 ++k0;
7302 }
7303
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007304 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007305 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007306 while (*s == '-')
7307 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007308 /* "k0" gets NOT reduced because
7309 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007310 s++;
7311 }
7312 if (*s == '<')
7313 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007314 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007315 {
7316 p0 = *s - '0';
7317 s++;
7318 }
7319
7320 if (*s == NUL
7321 /* *s == '^' cuts */
7322 || (*s == '$'
7323 && !SPELL_ISWORDP(word + i + k0)))
7324 {
7325 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007326 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007327 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007328
7329 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007330 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007331 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007332 /* rule fits; stop search */
7333 break;
7334 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007335 }
7336
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007337 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007338 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007339 }
7340
7341 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007342 s = smp[n].sm_to;
7343 pf = smp[n].sm_rules;
7344 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007345 if (p0 == 1 && z == 0)
7346 {
7347 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007348 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
7349 || res[reslen - 1] == *s))
7350 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007351 z0 = 1;
7352 z = 1;
7353 k0 = 0;
7354 while (*s != NUL && word[i+k0] != NUL)
7355 {
7356 word[i + k0] = *s;
7357 k0++;
7358 s++;
7359 }
7360 if (k > k0)
7361 mch_memmove(word + i + k0, word + i + k,
7362 STRLEN(word + i + k) + 1);
7363
7364 /* new "actual letter" */
7365 c = word[i];
7366 }
7367 else
7368 {
7369 /* no '<' rule used */
7370 i += k - 1;
7371 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007372 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007373 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007374 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007375 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007376 res[reslen] = *s;
7377 reslen++;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007378 }
7379 s++;
7380 }
7381 /* new "actual letter" */
7382 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007383 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007384 {
7385 if (c != NUL)
7386 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007387 res[reslen] = c;
7388 reslen++;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007389 }
7390 mch_memmove(word, word + i + 1,
7391 STRLEN(word + i + 1) + 1);
7392 i = 0;
7393 z0 = 1;
7394 }
7395 }
7396 break;
7397 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007398 }
7399 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007400 else if (vim_iswhite(c))
7401 {
7402 c = ' ';
7403 k = 1;
7404 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007405
7406 if (z0 == 0)
7407 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007408 if (k && !p0 && reslen < MAXWLEN && c != NUL
7409 && (!slang->sl_collapse || reslen == 0
7410 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007411 {
7412 /* condense only double letters */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007413 res[reslen] = c;
7414 reslen++;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007415 }
7416
7417 i++;
7418 z = 0;
7419 k = 0;
7420 }
7421 }
7422
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007423 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007424}
7425
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007426/*
7427 * Return the score for how much words sound different.
7428 */
7429 static int
7430spell_sound_score(slang, goodword, badsound)
7431 slang_T *slang;
7432 char_u *goodword; /* good word */
7433 char_u *badsound; /* sound-folded bad word */
7434{
7435 char_u fword[MAXWLEN];
7436 char_u goodsound[MAXWLEN];
7437 int score;
7438
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007439 /* Case-fold the goodword, needed for sound folding. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007440 (void)spell_casefold(goodword, STRLEN(goodword), fword, MAXWLEN);
7441
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007442 /* sound-fold the goodword */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007443 spell_soundfold(slang, fword, goodsound);
7444
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007445 /* Compute the edit distance-score of the sounds. This is slow but we
7446 * only do it for a small number of words. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007447 score = spell_edit_score(badsound, goodsound);
7448
7449 /* Correction: adding/inserting "*" at the start (word starts with vowel)
7450 * shouldn't be counted so much, vowels halfway the word aren't counted at
7451 * all. */
7452 if (*badsound != *goodsound && (*badsound == '*' || *goodsound == '*'))
7453 score -= SCORE_DEL / 2;
7454
7455 return score;
7456}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007457
7458/*
7459 * Compute a score for two sound-a-like words.
7460 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
7461 * Instead of a generic loop we write out the code. That keeps it fast by
7462 * avoiding checks that will not be possible.
7463 */
7464 static int
7465soundalike_score(goodsound, badsound)
7466 char_u *goodsound; /* sound-folded good word */
7467 char_u *badsound; /* sound-folded bad word */
7468{
7469 int goodlen = STRLEN(goodsound);
7470 int badlen = STRLEN(badsound);
7471 int n;
7472 char_u *pl, *ps;
7473 char_u *pl2, *ps2;
7474
7475 /* Return quickly if the lenghts are too different to be fixed by two
7476 * changes. */
7477 n = goodlen - badlen;
7478 if (n < -2 || n > 2)
7479 return SCORE_MAXMAX;
7480
7481 if (n > 0)
7482 {
7483 pl = goodsound; /* longest */
7484 ps = badsound;
7485 }
7486 else
7487 {
7488 pl = badsound; /* longest */
7489 ps = goodsound;
7490 }
7491
7492 /* Skip over the identical part. */
7493 while (*pl == *ps && *pl != NUL)
7494 {
7495 ++pl;
7496 ++ps;
7497 }
7498
7499 switch (n)
7500 {
7501 case -2:
7502 case 2:
7503 /*
7504 * Must delete two characters from "pl".
7505 */
7506 ++pl; /* first delete */
7507 while (*pl == *ps)
7508 {
7509 ++pl;
7510 ++ps;
7511 }
7512 /* strings must be equal after second delete */
7513 if (STRCMP(pl + 1, ps) == 0)
7514 return SCORE_DEL * 2;
7515
7516 /* Failed to compare. */
7517 break;
7518
7519 case -1:
7520 case 1:
7521 /*
7522 * Minimal one delete from "pl" required.
7523 */
7524
7525 /* 1: delete */
7526 pl2 = pl + 1;
7527 ps2 = ps;
7528 while (*pl2 == *ps2)
7529 {
7530 if (*pl2 == NUL) /* reached the end */
7531 return SCORE_DEL;
7532 ++pl2;
7533 ++ps2;
7534 }
7535
7536 /* 2: delete then swap, then rest must be equal */
7537 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7538 && STRCMP(pl2 + 2, ps2 + 2) == 0)
7539 return SCORE_DEL + SCORE_SWAP;
7540
7541 /* 3: delete then substitute, then the rest must be equal */
7542 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
7543 return SCORE_DEL + SCORE_SUBST;
7544
7545 /* 4: first swap then delete */
7546 if (pl[0] == ps[1] && pl[1] == ps[0])
7547 {
7548 pl2 = pl + 2; /* swap, skip two chars */
7549 ps2 = ps + 2;
7550 while (*pl2 == *ps2)
7551 {
7552 ++pl2;
7553 ++ps2;
7554 }
7555 /* delete a char and then strings must be equal */
7556 if (STRCMP(pl2 + 1, ps2) == 0)
7557 return SCORE_SWAP + SCORE_DEL;
7558 }
7559
7560 /* 5: first substitute then delete */
7561 pl2 = pl + 1; /* substitute, skip one char */
7562 ps2 = ps + 1;
7563 while (*pl2 == *ps2)
7564 {
7565 ++pl2;
7566 ++ps2;
7567 }
7568 /* delete a char and then strings must be equal */
7569 if (STRCMP(pl2 + 1, ps2) == 0)
7570 return SCORE_SUBST + SCORE_DEL;
7571
7572 /* Failed to compare. */
7573 break;
7574
7575 case 0:
7576 /*
7577 * Lenghts are equal, thus changes must result in same length: An
7578 * insert is only possible in combination with a delete.
7579 * 1: check if for identical strings
7580 */
7581 if (*pl == NUL)
7582 return 0;
7583
7584 /* 2: swap */
7585 if (pl[0] == ps[1] && pl[1] == ps[0])
7586 {
7587 pl2 = pl + 2; /* swap, skip two chars */
7588 ps2 = ps + 2;
7589 while (*pl2 == *ps2)
7590 {
7591 if (*pl2 == NUL) /* reached the end */
7592 return SCORE_SWAP;
7593 ++pl2;
7594 ++ps2;
7595 }
7596 /* 3: swap and swap again */
7597 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7598 && STRCMP(pl2 + 2, ps2 + 2) == 0)
7599 return SCORE_SWAP + SCORE_SWAP;
7600
7601 /* 4: swap and substitute */
7602 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
7603 return SCORE_SWAP + SCORE_SUBST;
7604 }
7605
7606 /* 5: substitute */
7607 pl2 = pl + 1;
7608 ps2 = ps + 1;
7609 while (*pl2 == *ps2)
7610 {
7611 if (*pl2 == NUL) /* reached the end */
7612 return SCORE_SUBST;
7613 ++pl2;
7614 ++ps2;
7615 }
7616
7617 /* 6: substitute and swap */
7618 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7619 && STRCMP(pl2 + 2, ps2 + 2) == 0)
7620 return SCORE_SUBST + SCORE_SWAP;
7621
7622 /* 7: substitute and substitute */
7623 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
7624 return SCORE_SUBST + SCORE_SUBST;
7625
7626 /* 8: insert then delete */
7627 pl2 = pl;
7628 ps2 = ps + 1;
7629 while (*pl2 == *ps2)
7630 {
7631 ++pl2;
7632 ++ps2;
7633 }
7634 if (STRCMP(pl2 + 1, ps2) == 0)
7635 return SCORE_INS + SCORE_DEL;
7636
7637 /* 9: delete then insert */
7638 pl2 = pl + 1;
7639 ps2 = ps;
7640 while (*pl2 == *ps2)
7641 {
7642 ++pl2;
7643 ++ps2;
7644 }
7645 if (STRCMP(pl2, ps2 + 1) == 0)
7646 return SCORE_INS + SCORE_DEL;
7647
7648 /* Failed to compare. */
7649 break;
7650 }
7651
7652 return SCORE_MAXMAX;
7653}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007654
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007655/*
7656 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007657 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007658 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007659 * The algorithm comes from Aspell editdist.cpp, edit_distance().
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007660 * It has been converted from C++ to C and modified to support multi-byte
7661 * characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007662 */
7663 static int
7664spell_edit_score(badword, goodword)
7665 char_u *badword;
7666 char_u *goodword;
7667{
7668 int *cnt;
7669 int badlen, goodlen;
7670 int j, i;
7671 int t;
7672 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007673 int pbc, pgc;
7674#ifdef FEAT_MBYTE
7675 char_u *p;
7676 int wbadword[MAXWLEN];
7677 int wgoodword[MAXWLEN];
7678
7679 if (has_mbyte)
7680 {
7681 /* Get the characters from the multi-byte strings and put them in an
7682 * int array for easy access. */
7683 for (p = badword, badlen = 0; *p != NUL; )
7684 wbadword[badlen++] = mb_ptr2char_adv(&p);
7685 ++badlen;
7686 for (p = goodword, goodlen = 0; *p != NUL; )
7687 wgoodword[goodlen++] = mb_ptr2char_adv(&p);
7688 ++goodlen;
7689 }
7690 else
7691#endif
7692 {
7693 badlen = STRLEN(badword) + 1;
7694 goodlen = STRLEN(goodword) + 1;
7695 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007696
7697 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
7698#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007699 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
7700 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007701 if (cnt == NULL)
7702 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007703
7704 CNT(0, 0) = 0;
7705 for (j = 1; j <= goodlen; ++j)
7706 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
7707
7708 for (i = 1; i <= badlen; ++i)
7709 {
7710 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
7711 for (j = 1; j <= goodlen; ++j)
7712 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007713#ifdef FEAT_MBYTE
7714 if (has_mbyte)
7715 {
7716 bc = wbadword[i - 1];
7717 gc = wgoodword[j - 1];
7718 }
7719 else
7720#endif
7721 {
7722 bc = badword[i - 1];
7723 gc = goodword[j - 1];
7724 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007725 if (bc == gc)
7726 CNT(i, j) = CNT(i - 1, j - 1);
7727 else
7728 {
7729 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007730 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007731 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
7732 else
7733 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
7734
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007735 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007736 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007737#ifdef FEAT_MBYTE
7738 if (has_mbyte)
7739 {
7740 pbc = wbadword[i - 2];
7741 pgc = wgoodword[j - 2];
7742 }
7743 else
7744#endif
7745 {
7746 pbc = badword[i - 2];
7747 pgc = goodword[j - 2];
7748 }
7749 if (bc == pgc && pbc == gc)
7750 {
7751 t = SCORE_SWAP + CNT(i - 2, j - 2);
7752 if (t < CNT(i, j))
7753 CNT(i, j) = t;
7754 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007755 }
7756 t = SCORE_DEL + CNT(i - 1, j);
7757 if (t < CNT(i, j))
7758 CNT(i, j) = t;
7759 t = SCORE_INS + CNT(i, j - 1);
7760 if (t < CNT(i, j))
7761 CNT(i, j) = t;
7762 }
7763 }
7764 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007765
7766 i = CNT(badlen - 1, goodlen - 1);
7767 vim_free(cnt);
7768 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007769}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007770
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007771#endif /* FEAT_SYN_HL */