blob: eab725a111d0001ecdf6f590e4282eb9a4f35c3d [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 */
360 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
361 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
362 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000363} suginfo_T;
364
365/* One word suggestion. Used in "si_ga". */
366typedef struct suggest_S
367{
368 char_u *st_word; /* suggested word, allocated string */
369 int st_orglen; /* length of replaced text */
370 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000371 int st_altscore; /* used when st_score compares equal */
372 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000373 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000374} suggest_T;
375
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000376#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000377
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000378/* Number of suggestions kept when cleaning up. When rescore_suggestions() is
379 * called the score may change, thus we need to keep more than what is
380 * displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000381#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 25 ? 25 : (su)->su_maxcount)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000382
383/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
384 * of suggestions that are not going to be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000385#define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000386
387/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000388#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000389#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000390#define SCORE_REGION 70 /* word is for different region */
391#define SCORE_RARE 180 /* rare word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000392#define SCORE_SWAP 90 /* swap two characters */
393#define SCORE_SWAP3 110 /* swap two characters in three */
394#define SCORE_REP 87 /* REP replacement */
395#define SCORE_SUBST 93 /* substitute a character */
396#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000397#define SCORE_DEL 94 /* delete a character */
398#define SCORE_INS 96 /* insert a character */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000399
400#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
401 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000402
403#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000404#define SCORE_MAXMAX 999999 /* accept any score */
405
406/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000407 * Structure to store info for word matching.
408 */
409typedef struct matchinf_S
410{
411 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000412
413 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000414 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000415 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000416 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000417 char_u *mi_cend; /* char after what was used for
418 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000419
420 /* case-folded text */
421 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000422 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000423
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000424 /* for when checking word after a prefix */
425 int mi_prefarridx; /* index in sl_pidxs with list of
426 prefixID/condition */
427 int mi_prefcnt; /* number of entries at mi_prefarridx */
428 int mi_prefixlen; /* byte length of prefix */
429
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000430 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000431 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000432 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000433} matchinf_T;
434
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000435/*
436 * The tables used for recognizing word characters according to spelling.
437 * These are only used for the first 256 characters of 'encoding'.
438 */
439typedef struct spelltab_S
440{
441 char_u st_isw[256]; /* flags: is word char */
442 char_u st_isu[256]; /* flags: is uppercase char */
443 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000444 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000445} spelltab_T;
446
447static spelltab_T spelltab;
448static int did_set_spelltab;
449
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000450#define CF_WORD 0x01
451#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000452
453static void clear_spell_chartab __ARGS((spelltab_T *sp));
454static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000455static void write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000456
457/*
458 * Return TRUE if "p" points to a word character or "c" is a word character
459 * for spelling.
460 * Checking for a word character is done very often, avoid the function call
461 * overhead.
462 */
463#ifdef FEAT_MBYTE
464# define SPELL_ISWORDP(p) ((has_mbyte && MB_BYTE2LEN(*(p)) > 1) \
465 ? (mb_get_class(p) >= 2) : spelltab.st_isw[*(p)])
466#else
467# define SPELL_ISWORDP(p) (spelltab.st_isw[*(p)])
468#endif
469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000470/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000471 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000472 */
473typedef enum
474{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000475 STATE_START = 0, /* At start of node check for NUL bytes (goodword
476 * ends); if badword ends there is a match, otherwise
477 * try splitting word. */
478 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000479 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
480 STATE_PLAIN, /* Use each byte of the node. */
481 STATE_DEL, /* Delete a byte from the bad word. */
482 STATE_INS, /* Insert a byte in the bad word. */
483 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000484 STATE_UNSWAP, /* Undo swap two characters. */
485 STATE_SWAP3, /* Swap two characters over three. */
486 STATE_UNSWAP3, /* Undo Swap two characters over three. */
487 STATE_ROT3L, /* Rotate three characters left */
488 STATE_UNROT3L, /* Undo rotate three characters left */
489 STATE_ROT3R, /* Rotate three characters right */
490 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000491 STATE_REP_INI, /* Prepare for using REP items. */
492 STATE_REP, /* Use matching REP items from the .aff file. */
493 STATE_REP_UNDO, /* Undo a REP item replacement. */
494 STATE_FINAL /* End of this node. */
495} state_T;
496
497/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000498 * Struct to keep the state at each level in spell_try_change().
499 */
500typedef struct trystate_S
501{
Bram Moolenaarea424162005-06-16 21:51:00 +0000502 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000503 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000504 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000505 short ts_curi; /* index in list of child nodes */
506 char_u ts_fidx; /* index in fword[], case-folded bad word */
507 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
508 char_u ts_twordlen; /* valid length of tword[] */
509#ifdef FEAT_MBYTE
510 char_u ts_tcharlen; /* number of bytes in tword character */
511 char_u ts_tcharidx; /* current byte index in tword character */
512 char_u ts_isdiff; /* DIFF_ values */
513 char_u ts_fcharstart; /* index in fword where badword char started */
514#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000515 char_u ts_save_prewordlen; /* saved "prewordlen" */
Bram Moolenaarea424162005-06-16 21:51:00 +0000516 char_u ts_save_splitoff; /* su_splitoff saved here */
517 char_u ts_save_badflags; /* badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000518} trystate_T;
519
Bram Moolenaarea424162005-06-16 21:51:00 +0000520/* values for ts_isdiff */
521#define DIFF_NONE 0 /* no different byte (yet) */
522#define DIFF_YES 1 /* different byte found */
523#define DIFF_INSERT 2 /* inserting character */
524
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000525/* mode values for find_word */
526#define FIND_FOLDWORD 0 /* find word case-folded */
527#define FIND_KEEPWORD 1 /* find keep-case word */
528#define FIND_PREFIX 2 /* find word after prefix */
529
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000530static slang_T *slang_alloc __ARGS((char_u *lang));
531static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000532static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000533static void find_word __ARGS((matchinf_T *mip, int mode));
534static void find_prefix __ARGS((matchinf_T *mip));
535static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000536static int spell_valid_case __ARGS((int origflags, int treeflags));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000537static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000538static char_u *spell_enc __ARGS((void));
539static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000540static 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 +0000541static 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 +0000542static int find_region __ARGS((char_u *rp, char_u *region));
543static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000544static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000545static int set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000546static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
547static void write_spell_chartab __ARGS((FILE *fd));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000548static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000549static void spell_find_suggest __ARGS((char_u *badptr, suginfo_T *su, int maxcount));
550static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000551static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000552static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000553static void spell_try_change __ARGS((suginfo_T *su));
554static 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 Moolenaar9ba0eb82005-06-13 22:28:56 +0000558static void spell_try_soundalike __ARGS((suginfo_T *su));
559static 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 Moolenaard857f0e2005-06-21 22:37:39 +0000562static void add_suggestion __ARGS((suginfo_T *su, garray_T *gap, char_u *goodword, 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
644 * 0X99FF. But when a word character follows do check spelling. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000645 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000646 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000647 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
648 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000649 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000650 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000651 mi.mi_end = skipdigits(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000652 nrlen = mi.mi_end - ptr;
653 }
654 if (!SPELL_ISWORDP(mi.mi_end))
655 return (int)(mi.mi_end - ptr);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000656 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000657
658 /* Find the end of the word. */
659 mi.mi_word = ptr;
660 mi.mi_fend = ptr;
661
662 if (SPELL_ISWORDP(mi.mi_fend))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000663 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000664 /* Make case-folded copy of the characters until the next non-word
665 * character. */
666 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000667 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000668 mb_ptr_adv(mi.mi_fend);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000669 } while (*mi.mi_fend != NUL && SPELL_ISWORDP(mi.mi_fend));
670 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000671
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000672 /* We always use the characters up to the next non-word character,
673 * also for bad words. */
674 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000675
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000676 /* Check caps type later. */
677 mi.mi_capflags = 0;
678 mi.mi_cend = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000679
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000680 /* Include one non-word character so that we can check for the
681 * word end. */
682 if (*mi.mi_fend != NUL)
683 mb_ptr_adv(mi.mi_fend);
684
685 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
686 MAXWLEN + 1);
687 mi.mi_fwordlen = STRLEN(mi.mi_fword);
688
689 /* The word is bad unless we recognize it. */
690 mi.mi_result = SP_BAD;
691
692 /*
693 * Loop over the languages specified in 'spelllang'.
694 * We check them all, because a matching word may be longer than an
695 * already found matching word.
696 */
697 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
698 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
699 {
700 /* Check for a matching word in case-folded words. */
701 find_word(&mi, FIND_FOLDWORD);
702
703 /* Check for a matching word in keep-case words. */
704 find_word(&mi, FIND_KEEPWORD);
705
706 /* Check for matching prefixes. */
707 find_prefix(&mi);
708 }
709
710 if (mi.mi_result != SP_OK)
711 {
712 /* If we found a number skip over it. Allows for "42nd". */
713 if (nrlen > 0)
714 return nrlen;
715
716 /* When we are at a non-word character there is no error, just
717 * skip over the character (try looking for a word after it). */
718 if (!SPELL_ISWORDP(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000719 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000720#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000721 if (has_mbyte)
722 return mb_ptr2len_check(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000723#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000724 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000725 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000726
727 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
728 *attrp = highlight_attr[HLF_SPB];
729 else if (mi.mi_result == SP_RARE)
730 *attrp = highlight_attr[HLF_SPR];
731 else
732 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000733 }
734
Bram Moolenaar51485f02005-06-04 21:55:20 +0000735 return (int)(mi.mi_end - ptr);
736}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000737
Bram Moolenaar51485f02005-06-04 21:55:20 +0000738/*
739 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000740 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
741 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
742 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
743 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000744 *
745 * For a match mip->mi_result is updated.
746 */
747 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000748find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000749 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000750 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000751{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000752 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000753 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000754 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000755 int endidxcnt = 0;
756 int len;
757 int wlen = 0;
758 int flen;
759 int c;
760 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000761 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000762#ifdef FEAT_MBYTE
763 char_u *s;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000764 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000765#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000766 int res = SP_BAD;
767 int valid;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000768 slang_T *slang = mip->mi_lp->lp_slang;
769 unsigned flags;
770 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000771 idx_T *idxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000772 int prefcnt;
773 int pidx;
774 regmatch_T regmatch;
775 regprog_T *rp;
776 int prefid;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000777
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000778 if (mode == FIND_KEEPWORD)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000779 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000780 /* Check for word with matching case in keep-case tree. */
781 ptr = mip->mi_word;
782 flen = 9999; /* no case folding, always enough bytes */
783 byts = slang->sl_kbyts;
784 idxs = slang->sl_kidxs;
785 }
786 else
787 {
788 /* Check for case-folded in case-folded tree. */
789 ptr = mip->mi_fword;
790 flen = mip->mi_fwordlen; /* available case-folded bytes */
791 byts = slang->sl_fbyts;
792 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000793
794 if (mode == FIND_PREFIX)
795 {
796 /* Skip over the prefix. */
797 wlen = mip->mi_prefixlen;
798 flen -= mip->mi_prefixlen;
799 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000800 }
801
Bram Moolenaar51485f02005-06-04 21:55:20 +0000802 if (byts == NULL)
803 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000804
Bram Moolenaar51485f02005-06-04 21:55:20 +0000805 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000806 * Repeat advancing in the tree until:
807 * - there is a byte that doesn't match,
808 * - we reach the end of the tree,
809 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000810 */
811 for (;;)
812 {
813 if (flen == 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000814 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000815
816 len = byts[arridx++];
817
818 /* If the first possible byte is a zero the word could end here.
819 * Remember this index, we first check for the longest word. */
820 if (byts[arridx] == 0)
821 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000822 if (endidxcnt == MAXWLEN)
823 {
824 /* Must be a corrupted spell file. */
825 EMSG(_(e_format));
826 return;
827 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000828 endlen[endidxcnt] = wlen;
829 endidx[endidxcnt++] = arridx++;
830 --len;
831
832 /* Skip over the zeros, there can be several flag/region
833 * combinations. */
834 while (len > 0 && byts[arridx] == 0)
835 {
836 ++arridx;
837 --len;
838 }
839 if (len == 0)
840 break; /* no children, word must end here */
841 }
842
843 /* Stop looking at end of the line. */
844 if (ptr[wlen] == NUL)
845 break;
846
847 /* Perform a binary search in the list of accepted bytes. */
848 c = ptr[wlen];
849 lo = arridx;
850 hi = arridx + len - 1;
851 while (lo < hi)
852 {
853 m = (lo + hi) / 2;
854 if (byts[m] > c)
855 hi = m - 1;
856 else if (byts[m] < c)
857 lo = m + 1;
858 else
859 {
860 lo = hi = m;
861 break;
862 }
863 }
864
865 /* Stop if there is no matching byte. */
866 if (hi < lo || byts[lo] != c)
867 break;
868
869 /* Continue at the child (if there is one). */
870 arridx = idxs[lo];
871 ++wlen;
872 --flen;
873 }
874
875 /*
876 * Verify that one of the possible endings is valid. Try the longest
877 * first.
878 */
879 while (endidxcnt > 0)
880 {
881 --endidxcnt;
882 arridx = endidx[endidxcnt];
883 wlen = endlen[endidxcnt];
884
885#ifdef FEAT_MBYTE
886 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
887 continue; /* not at first byte of character */
888#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000889 if (SPELL_ISWORDP(ptr + wlen))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000890 continue; /* next char is a word character */
891
892#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000893 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000894 {
895 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000896 * when folding case. This can be slow, take a shortcut when the
897 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000898 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000899 if (STRNCMP(ptr, p, wlen) != 0)
900 {
901 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
902 mb_ptr_adv(p);
903 wlen = p - mip->mi_word;
904 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000905 }
906#endif
907
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000908 /* Check flags and region. For FIND_PREFIX check the condition and
909 * prefix ID.
910 * Repeat this if there are more flags/region alternatives until there
911 * is a match. */
912 res = SP_BAD;
913 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
914 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000915 {
916 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000917
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000918 /* For the fold-case tree check that the case of the checked word
919 * matches with what the word in the tree requires.
920 * For keep-case tree the case is always right. For prefixes we
921 * don't bother to check. */
922 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000923 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000924 if (mip->mi_cend != mip->mi_word + wlen)
925 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000926 /* mi_capflags was set for a different word length, need
927 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000928 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000929 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000930 }
931
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000932 if (!spell_valid_case(mip->mi_capflags, flags))
933 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000934 }
935
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000936 /* When mode is FIND_PREFIX the word must support the prefix:
937 * check the prefix ID and the condition. Do that for the list at
938 * mip->mi_prefarridx. */
939 if (mode == FIND_PREFIX)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000940 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000941 /* The prefix ID is stored two bytes above the flags. */
942 prefid = (unsigned)flags >> 16;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000943
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000944 valid = FALSE;
945 for (prefcnt = mip->mi_prefcnt - 1; prefcnt >= 0; --prefcnt)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000946 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000947 pidx = slang->sl_pidxs[mip->mi_prefarridx + prefcnt];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000948
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000949 /* Check the prefix ID. */
950 if (prefid != (pidx & 0xff))
951 continue;
952
953 /* Check the condition, if there is one. The
954 * condition index is stored above the prefix ID byte.
955 */
956 rp = slang->sl_prefprog[(unsigned)pidx >> 8];
957 if (rp != NULL)
958 {
959 regmatch.regprog = rp;
960 regmatch.rm_ic = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000961 if (!vim_regexec(&regmatch,
962 mip->mi_fword + mip->mi_prefixlen, 0))
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000963 continue;
964 }
965
966 /* It's a match, use it. */
967 valid = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000968 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000969 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000970
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000971 if (!valid)
972 continue;
973 }
974
975 if (flags & WF_BANNED)
976 res = SP_BANNED;
977 else if (flags & WF_REGION)
978 {
979 /* Check region. */
980 if ((mip->mi_lp->lp_region & (flags >> 8)) != 0)
981 res = SP_OK;
982 else
983 res = SP_LOCAL;
984 }
985 else if (flags & WF_RARE)
986 res = SP_RARE;
987 else
988 res = SP_OK;
989
990 /* Always use the longest match and the best result. */
991 if (mip->mi_result > res)
992 {
993 mip->mi_result = res;
994 mip->mi_end = mip->mi_word + wlen;
995 }
996 else if (mip->mi_result == res
997 && mip->mi_end < mip->mi_word + wlen)
998 mip->mi_end = mip->mi_word + wlen;
999
1000 if (res == SP_OK)
1001 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001002 }
1003
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001004 if (res == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001005 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001006 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001007}
1008
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001009/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001010 * Check if the word at "mip->mi_word" has a matching prefix.
1011 * If it does, then check the following word.
1012 *
1013 * For a match mip->mi_result is updated.
1014 */
1015 static void
1016find_prefix(mip)
1017 matchinf_T *mip;
1018{
1019 idx_T arridx = 0;
1020 int len;
1021 int wlen = 0;
1022 int flen;
1023 int c;
1024 char_u *ptr;
1025 idx_T lo, hi, m;
1026 slang_T *slang = mip->mi_lp->lp_slang;
1027 char_u *byts;
1028 idx_T *idxs;
1029
1030 /* We use the case-folded word here, since prefixes are always
1031 * case-folded. */
1032 ptr = mip->mi_fword;
1033 flen = mip->mi_fwordlen; /* available case-folded bytes */
1034 byts = slang->sl_pbyts;
1035 idxs = slang->sl_pidxs;
1036
1037 if (byts == NULL)
1038 return; /* array is empty */
1039
1040 /*
1041 * Repeat advancing in the tree until:
1042 * - there is a byte that doesn't match,
1043 * - we reach the end of the tree,
1044 * - or we reach the end of the line.
1045 */
1046 for (;;)
1047 {
1048 if (flen == 0 && *mip->mi_fend != NUL)
1049 flen = fold_more(mip);
1050
1051 len = byts[arridx++];
1052
1053 /* If the first possible byte is a zero the prefix could end here.
1054 * Check if the following word matches and supports the prefix. */
1055 if (byts[arridx] == 0)
1056 {
1057 /* There can be several prefixes with different conditions. We
1058 * try them all, since we don't know which one will give the
1059 * longest match. The word is the same each time, pass the list
1060 * of possible prefixes to find_word(). */
1061 mip->mi_prefarridx = arridx;
1062 mip->mi_prefcnt = len;
1063 while (len > 0 && byts[arridx] == 0)
1064 {
1065 ++arridx;
1066 --len;
1067 }
1068 mip->mi_prefcnt -= len;
1069
1070 /* Find the word that comes after the prefix. */
1071 mip->mi_prefixlen = wlen;
1072 find_word(mip, FIND_PREFIX);
1073
1074
1075 if (len == 0)
1076 break; /* no children, word must end here */
1077 }
1078
1079 /* Stop looking at end of the line. */
1080 if (ptr[wlen] == NUL)
1081 break;
1082
1083 /* Perform a binary search in the list of accepted bytes. */
1084 c = ptr[wlen];
1085 lo = arridx;
1086 hi = arridx + len - 1;
1087 while (lo < hi)
1088 {
1089 m = (lo + hi) / 2;
1090 if (byts[m] > c)
1091 hi = m - 1;
1092 else if (byts[m] < c)
1093 lo = m + 1;
1094 else
1095 {
1096 lo = hi = m;
1097 break;
1098 }
1099 }
1100
1101 /* Stop if there is no matching byte. */
1102 if (hi < lo || byts[lo] != c)
1103 break;
1104
1105 /* Continue at the child (if there is one). */
1106 arridx = idxs[lo];
1107 ++wlen;
1108 --flen;
1109 }
1110}
1111
1112/*
1113 * Need to fold at least one more character. Do until next non-word character
1114 * for efficiency.
1115 * Return the length of the folded chars in bytes.
1116 */
1117 static int
1118fold_more(mip)
1119 matchinf_T *mip;
1120{
1121 int flen;
1122 char_u *p;
1123
1124 p = mip->mi_fend;
1125 do
1126 {
1127 mb_ptr_adv(mip->mi_fend);
1128 } while (*mip->mi_fend != NUL && SPELL_ISWORDP(mip->mi_fend));
1129
1130 /* Include the non-word character so that we can check for the
1131 * word end. */
1132 if (*mip->mi_fend != NUL)
1133 mb_ptr_adv(mip->mi_fend);
1134
1135 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1136 mip->mi_fword + mip->mi_fwordlen,
1137 MAXWLEN - mip->mi_fwordlen);
1138 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1139 mip->mi_fwordlen += flen;
1140 return flen;
1141}
1142
1143/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001144 * Check case flags for a word. Return TRUE if the word has the requested
1145 * case.
1146 */
1147 static int
1148spell_valid_case(origflags, treeflags)
1149 int origflags; /* flags for the checked word. */
1150 int treeflags; /* flags for the word in the spell tree */
1151{
1152 return (origflags == WF_ALLCAP
1153 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
1154 && ((treeflags & WF_ONECAP) == 0 || origflags == WF_ONECAP)));
1155}
1156
Bram Moolenaar51485f02005-06-04 21:55:20 +00001157
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001158/*
1159 * Move to next spell error.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001160 * "curline" is TRUE for "z?": find word under/after cursor in the same line.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001161 * Return OK if found, FAIL otherwise.
1162 */
1163 int
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001164spell_move_to(dir, allwords, curline)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001165 int dir; /* FORWARD or BACKWARD */
1166 int allwords; /* TRUE for "[s" and "]s" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001167 int curline;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001168{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001169 linenr_T lnum;
1170 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001171 char_u *line;
1172 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001173 int attr = 0;
1174 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001175 int has_syntax = syntax_present(curbuf);
1176 int col;
1177 int can_spell;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001178
Bram Moolenaarb765d632005-06-07 21:00:02 +00001179 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001180 {
1181 EMSG(_("E756: Spell checking not enabled"));
1182 return FAIL;
1183 }
1184
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001185 /*
1186 * Start looking for bad word at the start of the line, because we can't
1187 * start halfway a word, we don't know where it starts or ends.
1188 *
1189 * When searching backwards, we continue in the line to find the last
1190 * bad word (in the cursor line: before the cursor).
1191 */
1192 lnum = curwin->w_cursor.lnum;
1193 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001194
1195 while (!got_int)
1196 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001197 line = ml_get(lnum);
1198 p = line;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001199
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001200 while (*p != NUL)
1201 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001202 /* When searching backward don't search after the cursor. */
1203 if (dir == BACKWARD
1204 && lnum == curwin->w_cursor.lnum
1205 && (colnr_T)(p - line) >= curwin->w_cursor.col)
1206 break;
1207
1208 /* start of word */
1209 len = spell_check(curwin, p, &attr);
1210
1211 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001212 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001213 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001214 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001215 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001216 /* When searching forward only accept a bad word after
1217 * the cursor. */
1218 if (dir == BACKWARD
1219 || lnum > curwin->w_cursor.lnum
1220 || (lnum == curwin->w_cursor.lnum
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001221 && (colnr_T)(curline ? p - line + len
1222 : p - line)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001223 > curwin->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001224 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001225 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001226 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001227 col = p - line;
1228 (void)syn_get_id(lnum, (colnr_T)col,
1229 FALSE, &can_spell);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001230
Bram Moolenaar51485f02005-06-04 21:55:20 +00001231 /* have to get the line again, a multi-line
1232 * regexp may make it invalid */
1233 line = ml_get(lnum);
1234 p = line + col;
1235 }
1236 else
1237 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001238
Bram Moolenaar51485f02005-06-04 21:55:20 +00001239 if (can_spell)
1240 {
1241 found_pos.lnum = lnum;
1242 found_pos.col = p - line;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001243#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001244 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001245#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001246 if (dir == FORWARD)
1247 {
1248 /* No need to search further. */
1249 curwin->w_cursor = found_pos;
1250 return OK;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001251 }
1252 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001253 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001254 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001255 attr = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001256 }
1257
Bram Moolenaar51485f02005-06-04 21:55:20 +00001258 /* advance to character after the word */
1259 p += len;
1260 if (*p == NUL)
1261 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001262 }
1263
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001264 if (curline)
1265 return FAIL; /* only check cursor line */
1266
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001267 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001268 if (dir == BACKWARD)
1269 {
1270 if (found_pos.lnum != 0)
1271 {
1272 /* Use the last match in the line. */
1273 curwin->w_cursor = found_pos;
1274 return OK;
1275 }
1276 if (lnum == 1)
1277 return FAIL;
1278 --lnum;
1279 }
1280 else
1281 {
1282 if (lnum == curbuf->b_ml.ml_line_count)
1283 return FAIL;
1284 ++lnum;
1285 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001286
1287 line_breakcheck();
1288 }
1289
1290 return FAIL; /* interrupted */
1291}
1292
1293/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001294 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001295 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001296 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001297 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001298spell_load_lang(lang)
1299 char_u *lang;
1300{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001301 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001302 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001303 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001304
Bram Moolenaarb765d632005-06-07 21:00:02 +00001305 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001306 * It's truncated when an error is detected. */
1307 STRCPY(langcp, lang);
1308
Bram Moolenaarb765d632005-06-07 21:00:02 +00001309 /*
1310 * Find the first spell file for "lang" in 'runtimepath' and load it.
1311 */
1312 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
1313 "spell/%s.%s.spl", lang, spell_enc());
1314 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001315
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001316 if (r == FAIL && *langcp != NUL)
1317 {
1318 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001319 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001320 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001321 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001322 }
1323
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001324 if (r == FAIL)
1325 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
1326 fname_enc + 6);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001327 else if (*langcp != NUL)
1328 {
1329 /* Load all the additions. */
1330 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
1331 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
1332 }
1333}
1334
1335/*
1336 * Return the encoding used for spell checking: Use 'encoding', except that we
1337 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1338 */
1339 static char_u *
1340spell_enc()
1341{
1342
1343#ifdef FEAT_MBYTE
1344 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1345 return p_enc;
1346#endif
1347 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001348}
1349
1350/*
1351 * Allocate a new slang_T.
1352 * Caller must fill "sl_next".
1353 */
1354 static slang_T *
1355slang_alloc(lang)
1356 char_u *lang;
1357{
1358 slang_T *lp;
1359
Bram Moolenaar51485f02005-06-04 21:55:20 +00001360 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001361 if (lp != NULL)
1362 {
1363 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001364 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001365 ga_init2(&lp->sl_sal, sizeof(salitem_T), 10);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001366 }
1367 return lp;
1368}
1369
1370/*
1371 * Free the contents of an slang_T and the structure itself.
1372 */
1373 static void
1374slang_free(lp)
1375 slang_T *lp;
1376{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001377 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001378 vim_free(lp->sl_fname);
1379 slang_clear(lp);
1380 vim_free(lp);
1381}
1382
1383/*
1384 * Clear an slang_T so that the file can be reloaded.
1385 */
1386 static void
1387slang_clear(lp)
1388 slang_T *lp;
1389{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001390 garray_T *gap;
1391 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001392 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001393 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001394
Bram Moolenaar51485f02005-06-04 21:55:20 +00001395 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001396 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001397 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001398 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001399 vim_free(lp->sl_pbyts);
1400 lp->sl_pbyts = NULL;
1401
Bram Moolenaar51485f02005-06-04 21:55:20 +00001402 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001403 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001404 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001405 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001406 vim_free(lp->sl_pidxs);
1407 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001408
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001409 gap = &lp->sl_rep;
1410 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001411 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001412 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1413 vim_free(ftp->ft_from);
1414 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001415 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001416 ga_clear(gap);
1417
1418 gap = &lp->sl_sal;
1419 while (gap->ga_len > 0)
1420 {
1421 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1422 vim_free(smp->sm_lead);
1423 vim_free(smp->sm_to);
1424 }
1425 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001426
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001427 for (i = 0; i < lp->sl_prefixcnt; ++i)
1428 vim_free(lp->sl_prefprog[i]);
1429 vim_free(lp->sl_prefprog);
1430
Bram Moolenaarea424162005-06-16 21:51:00 +00001431#ifdef FEAT_MBYTE
1432 {
1433 int todo = lp->sl_map_hash.ht_used;
1434 hashitem_T *hi;
1435
1436 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
1437 if (!HASHITEM_EMPTY(hi))
1438 {
1439 --todo;
1440 vim_free(hi->hi_key);
1441 }
1442 }
1443 hash_clear(&lp->sl_map_hash);
1444#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001445}
1446
1447/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001448 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001449 * Invoked through do_in_runtimepath().
1450 */
1451 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00001452spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001453 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001454 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001455{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001456 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001457}
1458
1459/*
1460 * Load one spell file and store the info into a slang_T.
1461 *
1462 * This is invoked in two ways:
1463 * - From spell_load_cb() to load a spell file for the first time. "lang" is
1464 * the language name, "old_lp" is NULL. Will allocate an slang_T.
1465 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
1466 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001467 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00001468 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001469 static slang_T *
1470spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001471 char_u *fname;
1472 char_u *lang;
1473 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001474 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001475{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001476 FILE *fd;
1477 char_u buf[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001478 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001479 char_u *bp;
1480 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001481 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001482 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001483 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001484 int round;
1485 char_u *save_sourcing_name = sourcing_name;
1486 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001487 int cnt, ccnt;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001488 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001489 slang_T *lp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001490 garray_T *gap;
1491 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001492 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001493 int rr;
1494 short *first;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001495 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001496 int c = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001497
Bram Moolenaarb765d632005-06-07 21:00:02 +00001498 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001499 if (fd == NULL)
1500 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001501 if (!silent)
1502 EMSG2(_(e_notopen), fname);
1503 else if (p_verbose > 2)
1504 {
1505 verbose_enter();
1506 smsg((char_u *)e_notopen, fname);
1507 verbose_leave();
1508 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001509 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001510 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00001511 if (p_verbose > 2)
1512 {
1513 verbose_enter();
1514 smsg((char_u *)_("Reading spell file \"%s\""), fname);
1515 verbose_leave();
1516 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001517
Bram Moolenaarb765d632005-06-07 21:00:02 +00001518 if (old_lp == NULL)
1519 {
1520 lp = slang_alloc(lang);
1521 if (lp == NULL)
1522 goto endFAIL;
1523
1524 /* Remember the file name, used to reload the file when it's updated. */
1525 lp->sl_fname = vim_strsave(fname);
1526 if (lp->sl_fname == NULL)
1527 goto endFAIL;
1528
1529 /* Check for .add.spl. */
1530 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
1531 }
1532 else
1533 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001534
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001535 /* Set sourcing_name, so that error messages mention the file name. */
1536 sourcing_name = fname;
1537 sourcing_lnum = 0;
1538
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001539 /* <HEADER>: <fileID>
1540 * <regioncnt> <regionname> ...
1541 * <charflagslen> <charflags>
1542 * <fcharslen> <fchars>
1543 * <prefcondcnt> <prefcond> ...
1544 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001545 for (i = 0; i < VIMSPELLMAGICL; ++i)
1546 buf[i] = getc(fd); /* <fileID> */
1547 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
1548 {
1549 EMSG(_("E757: Wrong file ID in spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001550 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001551 }
1552
1553 cnt = getc(fd); /* <regioncnt> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001554 if (cnt < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001555 {
1556truncerr:
1557 EMSG(_("E758: Truncated spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001558 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001559 }
1560 if (cnt > 8)
1561 {
1562formerr:
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001563 EMSG(_(e_format));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001564 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001565 }
1566 for (i = 0; i < cnt; ++i)
1567 {
1568 lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
1569 lp->sl_regions[i * 2 + 1] = getc(fd);
1570 }
1571 lp->sl_regions[cnt * 2] = NUL;
1572
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001573 cnt = getc(fd); /* <charflagslen> */
1574 if (cnt > 0)
1575 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001576 p = alloc((unsigned)cnt);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001577 if (p == NULL)
1578 goto endFAIL;
1579 for (i = 0; i < cnt; ++i)
1580 p[i] = getc(fd); /* <charflags> */
1581
1582 ccnt = (getc(fd) << 8) + getc(fd); /* <fcharslen> */
1583 if (ccnt <= 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001584 {
1585 vim_free(p);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001586 goto formerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001587 }
1588 fol = alloc((unsigned)ccnt + 1);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001589 if (fol == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001590 {
1591 vim_free(p);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001592 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001593 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001594 for (i = 0; i < ccnt; ++i)
1595 fol[i] = getc(fd); /* <fchars> */
1596 fol[i] = NUL;
1597
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001598 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001599 i = set_spell_charflags(p, cnt, fol);
1600 vim_free(p);
1601 vim_free(fol);
1602 if (i == FAIL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001603 goto formerr;
1604 }
1605 else
1606 {
1607 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
1608 cnt = (getc(fd) << 8) + getc(fd);
1609 if (cnt != 0)
1610 goto formerr;
1611 }
1612
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001613 /* <prefcondcnt> <prefcond> ... */
1614 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
1615 if (cnt > 0)
1616 {
1617 lp->sl_prefprog = (regprog_T **)alloc_clear(
1618 (unsigned)sizeof(regprog_T *) * cnt);
1619 if (lp->sl_prefprog == NULL)
1620 goto endFAIL;
1621 lp->sl_prefixcnt = cnt;
1622
1623 for (i = 0; i < cnt; ++i)
1624 {
1625 /* <prefcond> : <condlen> <condstr> */
1626 n = getc(fd); /* <condlen> */
1627 if (n < 0)
1628 goto formerr;
1629 /* When <condlen> is zero we have an empty condition. Otherwise
1630 * compile the regexp program used to check for the condition. */
1631 if (n > 0)
1632 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001633 buf[0] = '^'; /* always match at one position only */
1634 p = buf + 1;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001635 while (n-- > 0)
1636 *p++ = getc(fd); /* <condstr> */
1637 *p = NUL;
1638 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
1639 }
1640 }
1641 }
1642
1643
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001644 /* <SUGGEST> : <repcount> <rep> ...
1645 * <salflags> <salcount> <sal> ...
1646 * <maplen> <mapstr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001647
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001648 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
1649 if (cnt < 0)
1650 goto formerr;
1651
1652 gap = &lp->sl_rep;
1653 if (ga_grow(gap, cnt) == FAIL)
1654 goto endFAIL;
1655
1656 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
1657 for (; gap->ga_len < cnt; ++gap->ga_len)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001658 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001659 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
1660 for (rr = 1; rr <= 2; ++rr)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001661 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001662 ccnt = getc(fd);
1663 if (ccnt < 0)
1664 {
1665 if (rr == 2)
1666 vim_free(ftp->ft_from);
1667 goto formerr;
1668 }
1669 if ((p = alloc(ccnt + 1)) == NULL)
1670 {
1671 if (rr == 2)
1672 vim_free(ftp->ft_from);
1673 goto endFAIL;
1674 }
1675 for (i = 0; i < ccnt; ++i)
1676 p[i] = getc(fd); /* <repfrom> or <repto> */
1677 p[i] = NUL;
1678 if (rr == 1)
1679 ftp->ft_from = p;
1680 else
1681 ftp->ft_to = p;
1682 }
1683 }
1684
1685 /* Fill the first-index table. */
1686 first = lp->sl_rep_first;
1687 for (i = 0; i < 256; ++i)
1688 first[i] = -1;
1689 for (i = 0; i < gap->ga_len; ++i)
1690 {
1691 ftp = &((fromto_T *)gap->ga_data)[i];
1692 if (first[*ftp->ft_from] == -1)
1693 first[*ftp->ft_from] = i;
1694 }
1695
1696 i = getc(fd); /* <salflags> */
1697 if (i & SAL_F0LLOWUP)
1698 lp->sl_followup = TRUE;
1699 if (i & SAL_COLLAPSE)
1700 lp->sl_collapse = TRUE;
1701 if (i & SAL_REM_ACCENTS)
1702 lp->sl_rem_accents = TRUE;
1703
1704 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
1705 if (cnt < 0)
1706 goto formerr;
1707
1708 gap = &lp->sl_sal;
1709 if (ga_grow(gap, cnt) == FAIL)
1710 goto endFAIL;
1711
1712 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
1713 for (; gap->ga_len < cnt; ++gap->ga_len)
1714 {
1715 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
1716 ccnt = getc(fd); /* <salfromlen> */
1717 if (ccnt < 0)
1718 goto formerr;
1719 if ((p = alloc(ccnt + 2)) == NULL)
1720 goto endFAIL;
1721 smp->sm_lead = p;
1722
1723 /* Read up to the first special char into sm_lead. */
1724 for (i = 0; i < ccnt; ++i)
1725 {
1726 c = getc(fd); /* <salfrom> */
1727 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
1728 break;
1729 *p++ = c;
1730 }
1731 smp->sm_leadlen = p - smp->sm_lead;
1732 *p++ = NUL;
1733
1734 /* Put optional chars in sm_oneoff, if any. */
1735 if (c == '(')
1736 {
1737 smp->sm_oneoff = p;
1738 for (++i; i < ccnt; ++i)
1739 {
1740 c = getc(fd); /* <salfrom> */
1741 if (c == ')')
1742 break;
1743 *p++ = c;
1744 }
1745 *p++ = NUL;
1746 if (++i < ccnt)
1747 c = getc(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001748 }
1749 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001750 smp->sm_oneoff = NULL;
1751
1752 /* Any following chars go in sm_rules. */
1753 smp->sm_rules = p;
1754 if (i < ccnt)
1755 *p++ = c;
1756 for (++i; i < ccnt; ++i)
1757 *p++ = getc(fd); /* <salfrom> */
1758 *p++ = NUL;
1759
1760 ccnt = getc(fd); /* <saltolen> */
1761 if (ccnt < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001762 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001763 vim_free(smp->sm_lead);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001764 goto formerr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001765 }
1766 if ((p = alloc(ccnt + 1)) == NULL)
1767 {
1768 vim_free(smp->sm_lead);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001769 goto endFAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001770 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001771 smp->sm_to = p;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001772
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001773 for (i = 0; i < ccnt; ++i)
1774 *p++ = getc(fd); /* <salto> */
1775 *p++ = NUL;
1776 }
1777
1778 /* Fill the first-index table. */
1779 first = lp->sl_sal_first;
1780 for (i = 0; i < 256; ++i)
1781 first[i] = -1;
1782 for (i = 0; i < gap->ga_len; ++i)
1783 {
1784 smp = &((salitem_T *)gap->ga_data)[i];
1785 if (first[*smp->sm_lead] == -1)
1786 first[*smp->sm_lead] = i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001787 }
1788
1789 cnt = (getc(fd) << 8) + getc(fd); /* <maplen> */
1790 if (cnt < 0)
1791 goto formerr;
1792 p = alloc(cnt + 1);
1793 if (p == NULL)
1794 goto endFAIL;
1795 for (i = 0; i < cnt; ++i)
1796 p[i] = getc(fd); /* <mapstr> */
1797 p[i] = NUL;
Bram Moolenaarea424162005-06-16 21:51:00 +00001798 set_map_str(lp, p);
1799 vim_free(p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001800
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001801
Bram Moolenaar51485f02005-06-04 21:55:20 +00001802 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001803 * round 2: <KWORDTREE>
1804 * round 3: <PREFIXTREE> */
1805 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001806 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001807 /* The tree size was computed when writing the file, so that we can
1808 * allocate it as one long block. <nodecount> */
1809 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
1810 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001811 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001812 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001813 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001814 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001815 bp = lalloc((long_u)len, TRUE);
1816 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001817 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001818 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001819 lp->sl_fbyts = bp;
1820 else if (round == 2)
1821 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001822 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001823 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001824
1825 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001826 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
1827 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001828 goto endFAIL;
1829 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001830 lp->sl_fidxs = ip;
1831 else if (round == 2)
1832 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001833 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001834 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001835
1836 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001837 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001838 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001839 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001840 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001841 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001842 }
1843 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001844
Bram Moolenaarb765d632005-06-07 21:00:02 +00001845 /* For a new file link it in the list of spell files. */
1846 if (old_lp == NULL)
1847 {
1848 lp->sl_next = first_lang;
1849 first_lang = lp;
1850 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001851
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001852 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001853
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001854endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00001855 if (lang != NULL)
1856 /* truncating the name signals the error to spell_load_lang() */
1857 *lang = NUL;
1858 if (lp != NULL && old_lp == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001859 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001860 slang_free(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001861 lp = NULL;
1862 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001863
1864endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001865 if (fd != NULL)
1866 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001867 sourcing_name = save_sourcing_name;
1868 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001869
1870 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001871}
1872
1873/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00001874 * Read one row of siblings from the spell file and store it in the byte array
1875 * "byts" and index array "idxs". Recursively read the children.
1876 *
1877 * NOTE: The code here must match put_tree().
1878 *
1879 * Returns the index follosing the siblings.
1880 * Returns -1 if the file is shorter than expected.
1881 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001882 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001883 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001884read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001885 FILE *fd;
1886 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001887 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001888 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001889 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001890 int prefixtree; /* TRUE for reading PREFIXTREE */
1891 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001892{
Bram Moolenaar51485f02005-06-04 21:55:20 +00001893 int len;
1894 int i;
1895 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001896 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001897 int c;
1898#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001899
Bram Moolenaar51485f02005-06-04 21:55:20 +00001900 len = getc(fd); /* <siblingcount> */
1901 if (len <= 0)
1902 return -1;
1903
1904 if (startidx + len >= maxidx)
1905 return -2;
1906 byts[idx++] = len;
1907
1908 /* Read the byte values, flag/region bytes and shared indexes. */
1909 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001910 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001911 c = getc(fd); /* <byte> */
1912 if (c < 0)
1913 return -1;
1914 if (c <= BY_SPECIAL)
1915 {
1916 if (c == BY_NOFLAGS)
1917 {
1918 /* No flags, all regions. */
1919 idxs[idx] = 0;
1920 c = 0;
1921 }
1922 else if (c == BY_FLAGS)
1923 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001924 if (prefixtree)
1925 {
1926 /* Read the prefix ID and the condition nr. In idxs[]
1927 * store the prefix ID in the low byte, the condition
1928 * index shifted up 8 bits. */
1929 c = getc(fd); /* <prefixID> */
1930 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
1931 if (n >= maxprefcondnr)
1932 return -2;
1933 c = (n << 8) + c;
1934 }
1935 else
1936 {
1937 /* Read flags and optional region and prefix ID. In
1938 * idxs[] the flags go in the low byte, region above that
1939 * and prefix ID above the region. */
1940 c = getc(fd); /* <flags> */
1941 if (c & WF_REGION)
1942 c = (getc(fd) << 8) + c; /* <region> */
1943 if (c & WF_PFX)
1944 c = (getc(fd) << 16) + c; /* <prefixID> */
1945 }
1946
Bram Moolenaar51485f02005-06-04 21:55:20 +00001947 idxs[idx] = c;
1948 c = 0;
1949 }
1950 else /* c == BY_INDEX */
1951 {
1952 /* <nodeidx> */
1953 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
1954 if (n < 0 || n >= maxidx)
1955 return -2;
1956 idxs[idx] = n + SHARED_MASK;
1957 c = getc(fd); /* <xbyte> */
1958 }
1959 }
1960 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001961 }
1962
Bram Moolenaar51485f02005-06-04 21:55:20 +00001963 /* Recursively read the children for non-shared siblings.
1964 * Skip the end-of-word ones (zero byte value) and the shared ones (and
1965 * remove SHARED_MASK) */
1966 for (i = 1; i <= len; ++i)
1967 if (byts[startidx + i] != 0)
1968 {
1969 if (idxs[startidx + i] & SHARED_MASK)
1970 idxs[startidx + i] &= ~SHARED_MASK;
1971 else
1972 {
1973 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001974 idx = read_tree(fd, byts, idxs, maxidx, idx,
1975 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001976 if (idx < 0)
1977 break;
1978 }
1979 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001980
Bram Moolenaar51485f02005-06-04 21:55:20 +00001981 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001982}
1983
1984/*
1985 * Parse 'spelllang' and set buf->b_langp accordingly.
1986 * Returns an error message or NULL.
1987 */
1988 char_u *
1989did_set_spelllang(buf)
1990 buf_T *buf;
1991{
1992 garray_T ga;
1993 char_u *lang;
1994 char_u *e;
1995 char_u *region;
1996 int region_mask;
1997 slang_T *lp;
1998 int c;
1999 char_u lbuf[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002000 char_u spf_name[MAXPATHL];
2001 int did_spf = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002002
2003 ga_init2(&ga, sizeof(langp_T), 2);
2004
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002005 /* Get the name of the .spl file associated with 'spellfile'. */
2006 if (*buf->b_p_spf == NUL)
2007 did_spf = TRUE;
2008 else
2009 vim_snprintf((char *)spf_name, sizeof(spf_name), "%s.spl",
2010 buf->b_p_spf);
2011
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002012 /* loop over comma separated languages. */
2013 for (lang = buf->b_p_spl; *lang != NUL; lang = e)
2014 {
2015 e = vim_strchr(lang, ',');
2016 if (e == NULL)
2017 e = lang + STRLEN(lang);
Bram Moolenaar5482f332005-04-17 20:18:43 +00002018 region = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002019 if (e > lang + 2)
2020 {
2021 if (e - lang >= MAXWLEN)
2022 {
2023 ga_clear(&ga);
2024 return e_invarg;
2025 }
2026 if (lang[2] == '_')
2027 region = lang + 3;
2028 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002029
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002030 /* Check if we loaded this language before. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002031 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2032 if (STRNICMP(lp->sl_name, lang, 2) == 0)
2033 break;
2034
2035 if (lp == NULL)
2036 {
2037 /* Not found, load the language. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002038 vim_strncpy(lbuf, lang, e - lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002039 if (region != NULL)
2040 mch_memmove(lbuf + 2, lbuf + 5, e - lang - 4);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002041 spell_load_lang(lbuf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002042 }
2043
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002044 /*
2045 * Loop over the languages, there can be several files for each.
2046 */
2047 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2048 if (STRNICMP(lp->sl_name, lang, 2) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002049 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002050 region_mask = REGION_ALL;
2051 if (region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002052 {
2053 /* find region in sl_regions */
2054 c = find_region(lp->sl_regions, region);
2055 if (c == REGION_ALL)
2056 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002057 if (!lp->sl_add)
2058 {
2059 c = *e;
2060 *e = NUL;
2061 smsg((char_u *)_("Warning: region %s not supported"),
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002062 lang);
Bram Moolenaar3982c542005-06-08 21:56:31 +00002063 *e = c;
2064 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002065 }
2066 else
2067 region_mask = 1 << c;
2068 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002069
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002070 if (ga_grow(&ga, 1) == FAIL)
2071 {
2072 ga_clear(&ga);
2073 return e_outofmem;
2074 }
2075 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2076 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2077 ++ga.ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002078
2079 /* Check if this is the 'spellfile' spell file. */
2080 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
2081 did_spf = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002082 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002083
2084 if (*e == ',')
2085 ++e;
2086 }
2087
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002088 /*
2089 * Make sure the 'spellfile' file is loaded. It may be in 'runtimepath',
2090 * then it's probably loaded above already. Otherwise load it here.
2091 */
2092 if (!did_spf)
2093 {
2094 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2095 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
2096 break;
2097 if (lp == NULL)
2098 {
2099 vim_strncpy(lbuf, gettail(spf_name), 2);
2100 lp = spell_load_file(spf_name, lbuf, NULL, TRUE);
2101 }
2102 if (lp != NULL && ga_grow(&ga, 1) == OK)
2103 {
2104 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2105 LANGP_ENTRY(ga, ga.ga_len)->lp_region = REGION_ALL;
2106 ++ga.ga_len;
2107 }
2108 }
2109
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002110 /* Add a NULL entry to mark the end of the list. */
2111 if (ga_grow(&ga, 1) == FAIL)
2112 {
2113 ga_clear(&ga);
2114 return e_outofmem;
2115 }
2116 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
2117 ++ga.ga_len;
2118
2119 /* Everything is fine, store the new b_langp value. */
2120 ga_clear(&buf->b_langp);
2121 buf->b_langp = ga;
2122
2123 return NULL;
2124}
2125
2126/*
2127 * Find the region "region[2]" in "rp" (points to "sl_regions").
2128 * Each region is simply stored as the two characters of it's name.
2129 * Returns the index if found, REGION_ALL if not found.
2130 */
2131 static int
2132find_region(rp, region)
2133 char_u *rp;
2134 char_u *region;
2135{
2136 int i;
2137
2138 for (i = 0; ; i += 2)
2139 {
2140 if (rp[i] == NUL)
2141 return REGION_ALL;
2142 if (rp[i] == region[0] && rp[i + 1] == region[1])
2143 break;
2144 }
2145 return i / 2;
2146}
2147
2148/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002149 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002150 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002151 * Word WF_ONECAP
2152 * W WORD WF_ALLCAP
2153 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002154 */
2155 static int
2156captype(word, end)
2157 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002158 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002159{
2160 char_u *p;
2161 int c;
2162 int firstcap;
2163 int allcap;
2164 int past_second = FALSE; /* past second word char */
2165
2166 /* find first letter */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002167 for (p = word; !SPELL_ISWORDP(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002168 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002169 return 0; /* only non-word characters, illegal word */
2170#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00002171 if (has_mbyte)
2172 c = mb_ptr2char_adv(&p);
2173 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002174#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00002175 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002176 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002177
2178 /*
2179 * Need to check all letters to find a word with mixed upper/lower.
2180 * But a word with an upper char only at start is a ONECAP.
2181 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002182 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002183 if (SPELL_ISWORDP(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002184 {
2185#ifdef FEAT_MBYTE
2186 c = mb_ptr2char(p);
2187#else
2188 c = *p;
2189#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002190 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002191 {
2192 /* UUl -> KEEPCAP */
2193 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002194 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002195 allcap = FALSE;
2196 }
2197 else if (!allcap)
2198 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002199 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002200 past_second = TRUE;
2201 }
2202
2203 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002204 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002205 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002206 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002207 return 0;
2208}
2209
2210# if defined(FEAT_MBYTE) || defined(PROTO)
2211/*
2212 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002213 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002214 */
2215 void
2216spell_reload()
2217{
2218 buf_T *buf;
2219 slang_T *lp;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002220 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002221
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002222 /* Initialize the table for SPELL_ISWORDP(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002223 init_spell_chartab();
2224
2225 /* Unload all allocated memory. */
2226 while (first_lang != NULL)
2227 {
2228 lp = first_lang;
2229 first_lang = lp->sl_next;
2230 slang_free(lp);
2231 }
2232
2233 /* Go through all buffers and handle 'spelllang'. */
2234 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2235 {
2236 ga_clear(&buf->b_langp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00002237
2238 /* Only load the wordlists when 'spelllang' is set and there is a
2239 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002240 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002241 {
2242 FOR_ALL_WINDOWS(wp)
2243 if (wp->w_buffer == buf && wp->w_p_spell)
2244 {
2245 (void)did_set_spelllang(buf);
2246# ifdef FEAT_WINDOWS
2247 break;
2248# endif
2249 }
2250 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002251 }
2252}
2253# endif
2254
Bram Moolenaarb765d632005-06-07 21:00:02 +00002255/*
2256 * Reload the spell file "fname" if it's loaded.
2257 */
2258 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002259spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002260 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002261 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002262{
2263 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002264 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002265
Bram Moolenaarb765d632005-06-07 21:00:02 +00002266 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2267 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
2268 {
2269 slang_clear(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002270 (void)spell_load_file(fname, NULL, lp, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002271 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002272 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002273 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002274
2275 /* When "zg" was used and the file wasn't loaded yet, should redo
2276 * 'spelllang' to get it loaded. */
2277 if (added_word && !didit)
2278 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002279}
2280
2281
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002282/*
2283 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002284 */
2285
Bram Moolenaar51485f02005-06-04 21:55:20 +00002286#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002287 and .dic file. */
2288/*
2289 * Main structure to store the contents of a ".aff" file.
2290 */
2291typedef struct afffile_S
2292{
2293 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002294 int af_rar; /* RAR ID for rare word */
2295 int af_kep; /* KEP ID for keep-case word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002296 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002297 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
2298 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002299} afffile_T;
2300
2301typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002302/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
2303struct affentry_S
2304{
2305 affentry_T *ae_next; /* next affix with same name/number */
2306 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
2307 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002308 char_u *ae_cond; /* condition (NULL for ".") */
2309 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002310};
2311
2312/* Affix header from ".aff" file. Used for af_pref and af_suff. */
2313typedef struct affheader_S
2314{
2315 char_u ah_key[2]; /* key for hashtable == name of affix entry */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002316 int ah_newID; /* prefix ID after renumbering */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002317 int ah_combine; /* suffix may combine with prefix */
2318 affentry_T *ah_first; /* first affix entry */
2319} affheader_T;
2320
2321#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
2322
2323/*
2324 * Structure that is used to store the items in the word tree. This avoids
2325 * the need to keep track of each allocated thing, it's freed all at once
2326 * after ":mkspell" is done.
2327 */
2328#define SBLOCKSIZE 16000 /* size of sb_data */
2329typedef struct sblock_S sblock_T;
2330struct sblock_S
2331{
2332 sblock_T *sb_next; /* next block in list */
2333 int sb_used; /* nr of bytes already in use */
2334 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002335};
2336
2337/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002338 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002339 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002340typedef struct wordnode_S wordnode_T;
2341struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002342{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002343 char_u wn_hashkey[6]; /* room for the hash key */
2344 wordnode_T *wn_next; /* next node with same hash key */
2345 wordnode_T *wn_child; /* child (next byte in word) */
2346 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
2347 always sorted) */
2348 wordnode_T *wn_wnode; /* parent node that will write this node */
2349 int wn_index; /* index in written nodes (valid after first
2350 round) */
2351 char_u wn_byte; /* Byte for this node. NUL for word end */
2352 char_u wn_flags; /* when wn_byte is NUL: WF_ flags */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002353 short wn_region; /* when wn_byte is NUL: region mask; for
2354 PREFIXTREE it's the prefcondnr */
2355 char_u wn_prefixID; /* supported/required prefix ID or 0 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002356};
2357
Bram Moolenaar51485f02005-06-04 21:55:20 +00002358#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002359
Bram Moolenaar51485f02005-06-04 21:55:20 +00002360/*
2361 * Info used while reading the spell files.
2362 */
2363typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002364{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002365 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00002366 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002367 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00002368 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002369 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002370 sblock_T *si_blocks; /* memory blocks used */
2371 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002372 int si_add; /* addition file */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002373 int si_region; /* region mask */
2374 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00002375 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002376 int si_verbose; /* verbose messages */
Bram Moolenaar3982c542005-06-08 21:56:31 +00002377 int si_region_count; /* number of regions supported (1 when there
2378 are no regions) */
2379 char_u si_region_name[16]; /* region names (if count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002380
2381 garray_T si_rep; /* list of fromto_T entries from REP lines */
2382 garray_T si_sal; /* list of fromto_T entries from SAL lines */
2383 int si_followup; /* soundsalike: ? */
2384 int si_collapse; /* soundsalike: ? */
2385 int si_rem_accents; /* soundsalike: remove accents */
2386 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002387 garray_T si_prefcond; /* table with conditions for postponed
2388 * prefixes, each stored as a string */
2389 int si_newID; /* current value for ah_newID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002390} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002391
Bram Moolenaar51485f02005-06-04 21:55:20 +00002392static afffile_T *spell_read_aff __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002393static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002394static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
2395static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00002396static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00002397static void spell_free_aff __ARGS((afffile_T *aff));
2398static int spell_read_dic __ARGS((char_u *fname, spellinfo_T *spin, afffile_T *affile));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002399static char_u *get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, sblock_T **blp));
2400static 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 +00002401static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin));
2402static void *getroom __ARGS((sblock_T **blp, size_t len));
2403static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s));
2404static void free_blocks __ARGS((sblock_T *bl));
2405static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002406static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags, int region, char_u *pfxlist));
2407static 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 +00002408static void wordtree_compress __ARGS((wordnode_T *root, spellinfo_T *spin));
Bram Moolenaar51485f02005-06-04 21:55:20 +00002409static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot));
2410static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar3982c542005-06-08 21:56:31 +00002411static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002412static int put_tree __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002413static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00002414static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002415
2416/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002417 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00002418 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002419 */
2420 static afffile_T *
Bram Moolenaar51485f02005-06-04 21:55:20 +00002421spell_read_aff(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002422 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002423 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002424{
2425 FILE *fd;
2426 afffile_T *aff;
2427 char_u rline[MAXLINELEN];
2428 char_u *line;
2429 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00002430#define MAXITEMCNT 7
2431 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002432 int itemcnt;
2433 char_u *p;
2434 int lnum = 0;
2435 affheader_T *cur_aff = NULL;
2436 int aff_todo = 0;
2437 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002438 char_u *low = NULL;
2439 char_u *fol = NULL;
2440 char_u *upp = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002441 static char *e_affname = N_("Affix name too long in %s line %d: %s");
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002442 int do_rep;
2443 int do_sal;
2444 int do_map;
2445 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002446 hashitem_T *hi;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002447
Bram Moolenaar51485f02005-06-04 21:55:20 +00002448 /*
2449 * Open the file.
2450 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002451 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002452 if (fd == NULL)
2453 {
2454 EMSG2(_(e_notopen), fname);
2455 return NULL;
2456 }
2457
Bram Moolenaarb765d632005-06-07 21:00:02 +00002458 if (spin->si_verbose || p_verbose > 2)
2459 {
2460 if (!spin->si_verbose)
2461 verbose_enter();
2462 smsg((char_u *)_("Reading affix file %s..."), fname);
2463 out_flush();
2464 if (!spin->si_verbose)
2465 verbose_leave();
2466 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002468 /* Only do REP lines when not done in another .aff file already. */
2469 do_rep = spin->si_rep.ga_len == 0;
2470
2471 /* Only do SAL lines when not done in another .aff file already. */
2472 do_sal = spin->si_sal.ga_len == 0;
2473
2474 /* Only do MAP lines when not done in another .aff file already. */
2475 do_map = spin->si_map.ga_len == 0;
2476
Bram Moolenaar51485f02005-06-04 21:55:20 +00002477 /*
2478 * Allocate and init the afffile_T structure.
2479 */
2480 aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002481 if (aff == NULL)
2482 return NULL;
2483 hash_init(&aff->af_pref);
2484 hash_init(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002485
2486 /*
2487 * Read all the lines in the file one by one.
2488 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002489 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002490 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002491 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002492 ++lnum;
2493
2494 /* Skip comment lines. */
2495 if (*rline == '#')
2496 continue;
2497
2498 /* Convert from "SET" to 'encoding' when needed. */
2499 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002500#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00002501 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002502 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002503 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002504 if (pc == NULL)
2505 {
2506 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
2507 fname, lnum, rline);
2508 continue;
2509 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002510 line = pc;
2511 }
2512 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00002513#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002514 {
2515 pc = NULL;
2516 line = rline;
2517 }
2518
2519 /* Split the line up in white separated items. Put a NUL after each
2520 * item. */
2521 itemcnt = 0;
2522 for (p = line; ; )
2523 {
2524 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
2525 ++p;
2526 if (*p == NUL)
2527 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00002528 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002529 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002530 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002531 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002532 ++p;
2533 if (*p == NUL)
2534 break;
2535 *p++ = NUL;
2536 }
2537
2538 /* Handle non-empty lines. */
2539 if (itemcnt > 0)
2540 {
2541 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
2542 && aff->af_enc == NULL)
2543 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00002544#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00002545 /* Setup for conversion from "ENC" to 'encoding'. */
2546 aff->af_enc = enc_canonize(items[1]);
2547 if (aff->af_enc != NULL && !spin->si_ascii
2548 && convert_setup(&spin->si_conv, aff->af_enc,
2549 p_enc) == FAIL)
2550 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
2551 fname, aff->af_enc, p_enc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002552#else
2553 smsg((char_u *)_("Conversion in %s not supported"), fname);
2554#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002555 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00002556 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
2557 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002558 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00002559 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002560 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002561 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002562 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002563 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002564 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
2565 && aff->af_rar == 0)
2566 {
2567 aff->af_rar = items[1][0];
2568 if (items[1][1] != NUL)
2569 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
2570 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002571 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
2572 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002573 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00002574 aff->af_kep = items[1][0];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002575 if (items[1][1] != NUL)
2576 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
2577 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002578 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
2579 {
2580 aff->af_pfxpostpone = TRUE;
2581 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002582 else if ((STRCMP(items[0], "PFX") == 0
2583 || STRCMP(items[0], "SFX") == 0)
2584 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00002585 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002586 {
Bram Moolenaar8db73182005-06-17 21:51:16 +00002587 /* Myspell allows extra text after the item, but that might
2588 * mean mistakes go unnoticed. Require a comment-starter. */
2589 if (itemcnt > 4 && *items[4] != '#')
2590 smsg((char_u *)_("Trailing text in %s line %d: %s"),
2591 fname, lnum, items[4]);
2592
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002593 /* New affix letter. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002594 cur_aff = (affheader_T *)getroom(&spin->si_blocks,
2595 sizeof(affheader_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002596 if (cur_aff == NULL)
2597 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002598 cur_aff->ah_key[0] = *items[1]; /* TODO: multi-byte? */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002599 cur_aff->ah_key[1] = NUL;
2600 if (items[1][1] != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002601 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002602 if (*items[2] == 'Y')
2603 cur_aff->ah_combine = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002604 else if (*items[2] != 'N')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002605 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
2606 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002607
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002608 if (*items[0] == 'P')
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002609 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002610 tp = &aff->af_pref;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002611 /* Use a new number in the .spl file later, to be able to
2612 * handle multiple .aff files. */
2613 if (aff->af_pfxpostpone)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002614 cur_aff->ah_newID = ++spin->si_newID;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002615 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002616 else
2617 tp = &aff->af_suff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002618 aff_todo = atoi((char *)items[3]);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002619 hi = hash_find(tp, cur_aff->ah_key);
2620 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar51485f02005-06-04 21:55:20 +00002621 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002622 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
2623 fname, lnum, items[1]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002624 aff_todo = 0;
2625 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002626 else
2627 hash_add(tp, cur_aff->ah_key);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002628 }
2629 else if ((STRCMP(items[0], "PFX") == 0
2630 || STRCMP(items[0], "SFX") == 0)
2631 && aff_todo > 0
2632 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00002633 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002634 {
2635 affentry_T *aff_entry;
2636
Bram Moolenaar8db73182005-06-17 21:51:16 +00002637 /* Myspell allows extra text after the item, but that might
2638 * mean mistakes go unnoticed. Require a comment-starter. */
2639 if (itemcnt > 5 && *items[5] != '#')
2640 smsg((char_u *)_("Trailing text in %s line %d: %s"),
2641 fname, lnum, items[5]);
2642
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002643 /* New item for an affix letter. */
2644 --aff_todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002645 aff_entry = (affentry_T *)getroom(&spin->si_blocks,
2646 sizeof(affentry_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002647 if (aff_entry == NULL)
2648 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00002649
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002650 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002651 aff_entry->ae_chop = getroom_save(&spin->si_blocks,
2652 items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002653 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002654 aff_entry->ae_add = getroom_save(&spin->si_blocks,
2655 items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002656
Bram Moolenaar51485f02005-06-04 21:55:20 +00002657 /* Don't use an affix entry with non-ASCII characters when
2658 * "spin->si_ascii" is TRUE. */
2659 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00002660 || has_non_ascii(aff_entry->ae_add)))
2661 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00002662 aff_entry->ae_next = cur_aff->ah_first;
2663 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002664
2665 if (STRCMP(items[4], ".") != 0)
2666 {
2667 char_u buf[MAXLINELEN];
2668
2669 aff_entry->ae_cond = getroom_save(&spin->si_blocks,
2670 items[4]);
2671 if (*items[0] == 'P')
2672 sprintf((char *)buf, "^%s", items[4]);
2673 else
2674 sprintf((char *)buf, "%s$", items[4]);
2675 aff_entry->ae_prog = vim_regcomp(buf,
2676 RE_MAGIC + RE_STRING);
2677 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002678
2679 /* For postponed prefixes we need an entry in si_prefcond
2680 * for the condition. Use an existing one if possible. */
2681 if (*items[0] == 'P' && aff->af_pfxpostpone
2682 && aff_entry->ae_chop == NULL)
2683 {
2684 int idx;
2685 char_u **pp;
2686
2687 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
2688 --idx)
2689 {
2690 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
2691 if (str_equal(p, aff_entry->ae_cond))
2692 break;
2693 }
2694 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
2695 {
2696 /* Not found, add a new condition. */
2697 idx = spin->si_prefcond.ga_len++;
2698 pp = ((char_u **)spin->si_prefcond.ga_data) + idx;
2699 if (aff_entry->ae_cond == NULL)
2700 *pp = NULL;
2701 else
2702 *pp = getroom_save(&spin->si_blocks,
2703 aff_entry->ae_cond);
2704 }
2705
2706 /* Add the prefix to the prefix tree. */
2707 if (aff_entry->ae_add == NULL)
2708 p = (char_u *)"";
2709 else
2710 p = aff_entry->ae_add;
2711 tree_add_word(p, spin->si_prefroot, -1, idx,
2712 cur_aff->ah_newID, &spin->si_blocks);
2713 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00002714 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002715 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002716 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
2717 {
2718 if (fol != NULL)
2719 smsg((char_u *)_("Duplicate FOL in %s line %d"),
2720 fname, lnum);
2721 else
2722 fol = vim_strsave(items[1]);
2723 }
2724 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
2725 {
2726 if (low != NULL)
2727 smsg((char_u *)_("Duplicate LOW in %s line %d"),
2728 fname, lnum);
2729 else
2730 low = vim_strsave(items[1]);
2731 }
2732 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
2733 {
2734 if (upp != NULL)
2735 smsg((char_u *)_("Duplicate UPP in %s line %d"),
2736 fname, lnum);
2737 else
2738 upp = vim_strsave(items[1]);
2739 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002740 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002741 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002742 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002743 if (!isdigit(*items[1]))
2744 smsg((char_u *)_("Expected REP count in %s line %d"),
2745 fname, lnum);
2746 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002747 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
2748 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002749 /* REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002750 if (do_rep)
2751 add_fromto(spin, &spin->si_rep, items[1], items[2]);
2752 }
2753 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
2754 {
2755 /* MAP item or count */
2756 if (!found_map)
2757 {
2758 /* First line contains the count. */
2759 found_map = TRUE;
2760 if (!isdigit(*items[1]))
2761 smsg((char_u *)_("Expected MAP count in %s line %d"),
2762 fname, lnum);
2763 }
2764 else if (do_map)
2765 {
2766 /* We simply concatenate all the MAP strings, separated by
2767 * slashes. */
2768 ga_concat(&spin->si_map, items[1]);
2769 ga_append(&spin->si_map, '/');
2770 }
2771 }
2772 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
2773 {
2774 if (do_sal)
2775 {
2776 /* SAL item (sounds-a-like)
2777 * Either one of the known keys or a from-to pair. */
2778 if (STRCMP(items[1], "followup") == 0)
2779 spin->si_followup = sal_to_bool(items[2]);
2780 else if (STRCMP(items[1], "collapse_result") == 0)
2781 spin->si_collapse = sal_to_bool(items[2]);
2782 else if (STRCMP(items[1], "remove_accents") == 0)
2783 spin->si_rem_accents = sal_to_bool(items[2]);
2784 else
2785 /* when "to" is "_" it means empty */
2786 add_fromto(spin, &spin->si_sal, items[1],
2787 STRCMP(items[2], "_") == 0 ? (char_u *)""
2788 : items[2]);
2789 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002790 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002791 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002792 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
2793 fname, lnum, items[0]);
2794 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002795 }
2796
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002797 if (fol != NULL || low != NULL || upp != NULL)
2798 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002799 /*
2800 * Don't write a word table for an ASCII file, so that we don't check
2801 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002802 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00002803 * mb_get_class(), the list of chars in the file will be incomplete.
2804 */
2805 if (!spin->si_ascii
2806#ifdef FEAT_MBYTE
2807 && !enc_utf8
2808#endif
2809 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00002810 {
2811 if (fol == NULL || low == NULL || upp == NULL)
2812 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
2813 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00002814 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00002815 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002816
2817 vim_free(fol);
2818 vim_free(low);
2819 vim_free(upp);
2820 }
2821
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002822 vim_free(pc);
2823 fclose(fd);
2824 return aff;
2825}
2826
2827/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002828 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
2829 * NULL as equal.
2830 */
2831 static int
2832str_equal(s1, s2)
2833 char_u *s1;
2834 char_u *s2;
2835{
2836 if (s1 == NULL || s2 == NULL)
2837 return s1 == s2;
2838 return STRCMP(s1, s2) == 0;
2839}
2840
2841/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002842 * Add a from-to item to "gap". Used for REP and SAL items.
2843 * They are stored case-folded.
2844 */
2845 static void
2846add_fromto(spin, gap, from, to)
2847 spellinfo_T *spin;
2848 garray_T *gap;
2849 char_u *from;
2850 char_u *to;
2851{
2852 fromto_T *ftp;
2853 char_u word[MAXWLEN];
2854
2855 if (ga_grow(gap, 1) == OK)
2856 {
2857 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
2858 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
2859 ftp->ft_from = getroom_save(&spin->si_blocks, word);
2860 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
2861 ftp->ft_to = getroom_save(&spin->si_blocks, word);
2862 ++gap->ga_len;
2863 }
2864}
2865
2866/*
2867 * Convert a boolean argument in a SAL line to TRUE or FALSE;
2868 */
2869 static int
2870sal_to_bool(s)
2871 char_u *s;
2872{
2873 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
2874}
2875
2876/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00002877 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
2878 * When "s" is NULL FALSE is returned.
2879 */
2880 static int
2881has_non_ascii(s)
2882 char_u *s;
2883{
2884 char_u *p;
2885
2886 if (s != NULL)
2887 for (p = s; *p != NUL; ++p)
2888 if (*p >= 128)
2889 return TRUE;
2890 return FALSE;
2891}
2892
2893/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002894 * Free the structure filled by spell_read_aff().
2895 */
2896 static void
2897spell_free_aff(aff)
2898 afffile_T *aff;
2899{
2900 hashtab_T *ht;
2901 hashitem_T *hi;
2902 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002903 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002904 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002905
2906 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002907
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002908 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002909 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
2910 {
2911 todo = ht->ht_used;
2912 for (hi = ht->ht_array; todo > 0; ++hi)
2913 {
2914 if (!HASHITEM_EMPTY(hi))
2915 {
2916 --todo;
2917 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002918 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
2919 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002920 }
2921 }
2922 if (ht == &aff->af_suff)
2923 break;
2924 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002925
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002926 hash_clear(&aff->af_pref);
2927 hash_clear(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002928}
2929
2930/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002931 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002932 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002933 */
2934 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00002935spell_read_dic(fname, spin, affile)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002936 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002937 spellinfo_T *spin;
2938 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002939{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002940 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002941 char_u line[MAXLINELEN];
Bram Moolenaar51485f02005-06-04 21:55:20 +00002942 char_u *afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002943 char_u *pfxlist;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002944 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002945 char_u *pc;
2946 char_u *w;
2947 int l;
2948 hash_T hash;
2949 hashitem_T *hi;
2950 FILE *fd;
2951 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002952 int non_ascii = 0;
2953 int retval = OK;
2954 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002955 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002956
Bram Moolenaar51485f02005-06-04 21:55:20 +00002957 /*
2958 * Open the file.
2959 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002960 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002961 if (fd == NULL)
2962 {
2963 EMSG2(_(e_notopen), fname);
2964 return FAIL;
2965 }
2966
Bram Moolenaar51485f02005-06-04 21:55:20 +00002967 /* The hashtable is only used to detect duplicated words. */
2968 hash_init(&ht);
2969
Bram Moolenaar8db73182005-06-17 21:51:16 +00002970 spin->si_foldwcount = 0;
2971 spin->si_keepwcount = 0;
2972
Bram Moolenaarb765d632005-06-07 21:00:02 +00002973 if (spin->si_verbose || p_verbose > 2)
2974 {
2975 if (!spin->si_verbose)
2976 verbose_enter();
2977 smsg((char_u *)_("Reading dictionary file %s..."), fname);
2978 out_flush();
2979 if (!spin->si_verbose)
2980 verbose_leave();
2981 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002982
2983 /* Read and ignore the first line: word count. */
2984 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002985 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002986 EMSG2(_("E760: No word count in %s"), fname);
2987
2988 /*
2989 * Read all the lines in the file one by one.
2990 * The words are converted to 'encoding' here, before being added to
2991 * the hashtable.
2992 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002993 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002994 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002995 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002996 ++lnum;
2997
Bram Moolenaar51485f02005-06-04 21:55:20 +00002998 /* Remove CR, LF and white space from the end. White space halfway
2999 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003000 l = STRLEN(line);
3001 while (l > 0 && line[l - 1] <= ' ')
3002 --l;
3003 if (l == 0)
3004 continue; /* empty line */
3005 line[l] = NUL;
3006
Bram Moolenaar51485f02005-06-04 21:55:20 +00003007 /* Find the optional affix names. */
3008 afflist = vim_strchr(line, '/');
3009 if (afflist != NULL)
3010 *afflist++ = NUL;
3011
3012 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
3013 if (spin->si_ascii && has_non_ascii(line))
3014 {
3015 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003016 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003017 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003018
Bram Moolenaarb765d632005-06-07 21:00:02 +00003019#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003020 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003021 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003022 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003023 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003024 if (pc == NULL)
3025 {
3026 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3027 fname, lnum, line);
3028 continue;
3029 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003030 w = pc;
3031 }
3032 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003033#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003034 {
3035 pc = NULL;
3036 w = line;
3037 }
3038
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003039 /* This takes time, print a message now and then. */
3040 if (spin->si_verbose && (lnum & 0x3ff) == 0)
3041 {
3042 vim_snprintf((char *)message, sizeof(message),
3043 _("line %6d, word %6d - %s"),
3044 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
3045 msg_start();
3046 msg_puts_long_attr(message, 0);
3047 msg_clr_eos();
3048 msg_didout = FALSE;
3049 msg_col = 0;
3050 out_flush();
3051 }
3052
Bram Moolenaar51485f02005-06-04 21:55:20 +00003053 /* Store the word in the hashtable to be able to find duplicates. */
3054 dw = (char_u *)getroom_save(&spin->si_blocks, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003055 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003056 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003057 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003058 if (retval == FAIL)
3059 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003060
Bram Moolenaar51485f02005-06-04 21:55:20 +00003061 hash = hash_hash(dw);
3062 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003063 if (!HASHITEM_EMPTY(hi))
3064 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003065 fname, lnum, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003066 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00003067 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003068
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003069 flags = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003070 pfxlist = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003071 if (afflist != NULL)
3072 {
3073 /* Check for affix name that stands for keep-case word and stands
3074 * for rare word (if defined). */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003075 if (affile->af_kep != NUL
3076 && vim_strchr(afflist, affile->af_kep) != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003077 flags |= WF_KEEPCAP;
3078 if (affile->af_rar != NUL
3079 && vim_strchr(afflist, affile->af_rar) != NULL)
3080 flags |= WF_RARE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003081
3082 if (affile->af_pfxpostpone)
3083 /* Need to store the list of prefix IDs with the word. */
3084 pfxlist = get_pfxlist(affile, afflist, &spin->si_blocks);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003085 }
3086
Bram Moolenaar51485f02005-06-04 21:55:20 +00003087 /* Add the word to the word tree(s). */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003088 if (store_word(dw, spin, flags, spin->si_region, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003089 retval = FAIL;
3090
3091 if (afflist != NULL)
3092 {
3093 /* Find all matching suffixes and add the resulting words.
3094 * Additionally do matching prefixes that combine. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003095 if (store_aff_word(dw, spin, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003096 &affile->af_suff, &affile->af_pref,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003097 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003098 retval = FAIL;
3099
3100 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003101 if (store_aff_word(dw, spin, afflist, affile,
3102 &affile->af_pref, NULL,
3103 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003104 retval = FAIL;
3105 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003106 }
3107
Bram Moolenaar51485f02005-06-04 21:55:20 +00003108 if (spin->si_ascii && non_ascii > 0)
3109 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
3110 non_ascii);
3111 hash_clear(&ht);
3112
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003113 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003114 return retval;
3115}
3116
3117/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003118 * Get the list of prefix IDs from the affix list "afflist".
3119 * Used for PFXPOSTPONE.
3120 * Returns a string allocated with getroom(). NULL when there are no prefixes
3121 * or when out of memory.
3122 */
3123 static char_u *
3124get_pfxlist(affile, afflist, blp)
3125 afffile_T *affile;
3126 char_u *afflist;
3127 sblock_T **blp;
3128{
3129 char_u *p;
3130 int cnt;
3131 int round;
3132 char_u *res = NULL;
3133 char_u key[2];
3134 hashitem_T *hi;
3135
3136 key[1] = NUL;
3137
3138 /* round 1: count the number of prefix IDs.
3139 * round 2: move prefix IDs to "res" */
3140 for (round = 1; round <= 2; ++round)
3141 {
3142 cnt = 0;
3143 for (p = afflist; *p != NUL; ++p)
3144 {
3145 key[0] = *p;
3146 hi = hash_find(&affile->af_pref, key);
3147 if (!HASHITEM_EMPTY(hi))
3148 {
3149 /* This is a prefix ID, use the new number. */
3150 if (round == 2)
3151 res[cnt] = HI2AH(hi)->ah_newID;
3152 ++cnt;
3153 }
3154 }
3155 if (round == 1 && cnt > 0)
3156 res = getroom(blp, cnt + 1);
3157 if (res == NULL)
3158 break;
3159 }
3160
3161 if (res != NULL)
3162 res[cnt] = NUL;
3163 return res;
3164}
3165
3166/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003167 * Apply affixes to a word and store the resulting words.
3168 * "ht" is the hashtable with affentry_T that need to be applied, either
3169 * prefixes or suffixes.
3170 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
3171 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003172 *
3173 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003174 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003175 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003176store_aff_word(word, spin, afflist, affile, ht, xht, comb, flags, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003177 char_u *word; /* basic word start */
3178 spellinfo_T *spin; /* spell info */
3179 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003180 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003181 hashtab_T *ht;
3182 hashtab_T *xht;
3183 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003184 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003185 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003186{
3187 int todo;
3188 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003189 affheader_T *ah;
3190 affentry_T *ae;
3191 regmatch_T regmatch;
3192 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003193 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003194 int i;
3195 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003196
Bram Moolenaar51485f02005-06-04 21:55:20 +00003197 todo = ht->ht_used;
3198 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003199 {
3200 if (!HASHITEM_EMPTY(hi))
3201 {
3202 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003203 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00003204
Bram Moolenaar51485f02005-06-04 21:55:20 +00003205 /* Check that the affix combines, if required, and that the word
3206 * supports this affix. */
3207 if ((!comb || ah->ah_combine)
3208 && vim_strchr(afflist, *ah->ah_key) != NULL)
Bram Moolenaar5482f332005-04-17 20:18:43 +00003209 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003210 /* Loop over all affix entries with this name. */
3211 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003212 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003213 /* Check the condition. It's not logical to match case
3214 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003215 * Myspell.
3216 * For prefixes, when "PFXPOSTPONE" was used, only do
3217 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003218 regmatch.regprog = ae->ae_prog;
3219 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003220 if ((xht != NULL || !affile->af_pfxpostpone
3221 || ae->ae_chop != NULL)
3222 && (ae->ae_prog == NULL
3223 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003224 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003225 /* Match. Remove the chop and add the affix. */
3226 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003227 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003228 /* prefix: chop/add at the start of the word */
3229 if (ae->ae_add == NULL)
3230 *newword = NUL;
3231 else
3232 STRCPY(newword, ae->ae_add);
3233 p = word;
3234 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003235 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003236 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003237#ifdef FEAT_MBYTE
3238 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003239 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003240 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003241 for ( ; i > 0; --i)
3242 mb_ptr_adv(p);
3243 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00003244 else
3245#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003246 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003247 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003248 STRCAT(newword, p);
3249 }
3250 else
3251 {
3252 /* suffix: chop/add at the end of the word */
3253 STRCPY(newword, word);
3254 if (ae->ae_chop != NULL)
3255 {
3256 /* Remove chop string. */
3257 p = newword + STRLEN(newword);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003258#ifdef FEAT_MBYTE
3259 if (has_mbyte)
3260 i = mb_charlen(ae->ae_chop);
3261 else
3262#endif
3263 i = STRLEN(ae->ae_chop);
3264 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003265 mb_ptr_back(newword, p);
3266 *p = NUL;
3267 }
3268 if (ae->ae_add != NULL)
3269 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003270 }
3271
Bram Moolenaar51485f02005-06-04 21:55:20 +00003272 /* Store the modified word. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003273 if (store_word(newword, spin,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003274 flags, spin->si_region, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003275 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003276
Bram Moolenaar51485f02005-06-04 21:55:20 +00003277 /* When added a suffix and combining is allowed also
3278 * try adding prefixes additionally. */
3279 if (xht != NULL && ah->ah_combine)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003280 if (store_aff_word(newword, spin, afflist, affile,
3281 xht, NULL, TRUE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003282 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003283 }
3284 }
3285 }
3286 }
3287 }
3288
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003289 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003290}
3291
3292/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003293 * Read a file with a list of words.
3294 */
3295 static int
3296spell_read_wordfile(fname, spin)
3297 char_u *fname;
3298 spellinfo_T *spin;
3299{
3300 FILE *fd;
3301 long lnum = 0;
3302 char_u rline[MAXLINELEN];
3303 char_u *line;
3304 char_u *pc = NULL;
3305 int l;
3306 int retval = OK;
3307 int did_word = FALSE;
3308 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003309 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003310 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003311
3312 /*
3313 * Open the file.
3314 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003315 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00003316 if (fd == NULL)
3317 {
3318 EMSG2(_(e_notopen), fname);
3319 return FAIL;
3320 }
3321
Bram Moolenaarb765d632005-06-07 21:00:02 +00003322 if (spin->si_verbose || p_verbose > 2)
3323 {
3324 if (!spin->si_verbose)
3325 verbose_enter();
3326 smsg((char_u *)_("Reading word file %s..."), fname);
3327 out_flush();
3328 if (!spin->si_verbose)
3329 verbose_leave();
3330 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003331
3332 /*
3333 * Read all the lines in the file one by one.
3334 */
3335 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
3336 {
3337 line_breakcheck();
3338 ++lnum;
3339
3340 /* Skip comment lines. */
3341 if (*rline == '#')
3342 continue;
3343
3344 /* Remove CR, LF and white space from the end. */
3345 l = STRLEN(rline);
3346 while (l > 0 && rline[l - 1] <= ' ')
3347 --l;
3348 if (l == 0)
3349 continue; /* empty or blank line */
3350 rline[l] = NUL;
3351
3352 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
3353 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003354#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003355 if (spin->si_conv.vc_type != CONV_NONE)
3356 {
3357 pc = string_convert(&spin->si_conv, rline, NULL);
3358 if (pc == NULL)
3359 {
3360 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3361 fname, lnum, rline);
3362 continue;
3363 }
3364 line = pc;
3365 }
3366 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003367#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00003368 {
3369 pc = NULL;
3370 line = rline;
3371 }
3372
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003373 flags = 0;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003374 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003375
3376 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00003377 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003378 ++line;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003379
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003380 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003381 {
3382 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00003383 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
3384 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003385 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00003386 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
3387 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003388 else
3389 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003390#ifdef FEAT_MBYTE
3391 char_u *enc;
3392
Bram Moolenaar51485f02005-06-04 21:55:20 +00003393 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003394 line += 10;
3395 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003396 if (enc != NULL && !spin->si_ascii
3397 && convert_setup(&spin->si_conv, enc,
3398 p_enc) == FAIL)
3399 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00003400 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003401 vim_free(enc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003402#else
3403 smsg((char_u *)_("Conversion in %s not supported"), fname);
3404#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00003405 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003406 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003407 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003408
Bram Moolenaar3982c542005-06-08 21:56:31 +00003409 if (STRNCMP(line, "regions=", 8) == 0)
3410 {
3411 if (spin->si_region_count > 1)
3412 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
3413 fname, lnum, line);
3414 else
3415 {
3416 line += 8;
3417 if (STRLEN(line) > 16)
3418 smsg((char_u *)_("Too many regions in %s line %d: %s"),
3419 fname, lnum, line);
3420 else
3421 {
3422 spin->si_region_count = STRLEN(line) / 2;
3423 STRCPY(spin->si_region_name, line);
3424 }
3425 }
3426 continue;
3427 }
3428
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003429 if (*line == '=')
3430 {
3431 /* keep-case word */
3432 flags |= WF_KEEPCAP;
3433 ++line;
3434 }
3435
3436 if (*line == '!')
3437 {
3438 /* Bad, bad, wicked word. */
3439 flags |= WF_BANNED;
3440 ++line;
3441 }
3442 else if (*line == '?')
3443 {
3444 /* Rare word. */
3445 flags |= WF_RARE;
3446 ++line;
3447 }
3448
Bram Moolenaar3982c542005-06-08 21:56:31 +00003449 if (VIM_ISDIGIT(*line))
3450 {
3451 /* region number(s) */
3452 regionmask = 0;
3453 while (VIM_ISDIGIT(*line))
3454 {
3455 l = *line - '0';
3456 if (l > spin->si_region_count)
3457 {
3458 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
3459 fname, lnum, line);
3460 break;
3461 }
3462 regionmask |= 1 << (l - 1);
3463 ++line;
3464 }
3465 flags |= WF_REGION;
3466 }
3467
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003468 if (flags == 0)
3469 {
3470 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
Bram Moolenaar51485f02005-06-04 21:55:20 +00003471 fname, lnum, line);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003472 continue;
3473 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003474 }
3475
3476 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
3477 if (spin->si_ascii && has_non_ascii(line))
3478 {
3479 ++non_ascii;
3480 continue;
3481 }
3482
3483 /* Normal word: store it. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003484 if (store_word(line, spin, flags, regionmask, NULL) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003485 {
3486 retval = FAIL;
3487 break;
3488 }
3489 did_word = TRUE;
3490 }
3491
3492 vim_free(pc);
3493 fclose(fd);
3494
Bram Moolenaarb765d632005-06-07 21:00:02 +00003495 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
3496 {
3497 if (p_verbose > 2)
3498 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00003499 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
3500 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003501 if (p_verbose > 2)
3502 verbose_leave();
3503 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003504 return retval;
3505}
3506
3507/*
3508 * Get part of an sblock_T, "len" bytes long.
3509 * This avoids calling free() for every little struct we use.
3510 * The memory is cleared to all zeros.
3511 * Returns NULL when out of memory.
3512 */
3513 static void *
3514getroom(blp, len)
3515 sblock_T **blp;
3516 size_t len; /* length needed */
3517{
3518 char_u *p;
3519 sblock_T *bl = *blp;
3520
3521 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
3522 {
3523 /* Allocate a block of memory. This is not freed until much later. */
3524 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
3525 if (bl == NULL)
3526 return NULL;
3527 bl->sb_next = *blp;
3528 *blp = bl;
3529 bl->sb_used = 0;
3530 }
3531
3532 p = bl->sb_data + bl->sb_used;
3533 bl->sb_used += len;
3534
3535 return p;
3536}
3537
3538/*
3539 * Make a copy of a string into memory allocated with getroom().
3540 */
3541 static char_u *
3542getroom_save(blp, s)
3543 sblock_T **blp;
3544 char_u *s;
3545{
3546 char_u *sc;
3547
3548 sc = (char_u *)getroom(blp, STRLEN(s) + 1);
3549 if (sc != NULL)
3550 STRCPY(sc, s);
3551 return sc;
3552}
3553
3554
3555/*
3556 * Free the list of allocated sblock_T.
3557 */
3558 static void
3559free_blocks(bl)
3560 sblock_T *bl;
3561{
3562 sblock_T *next;
3563
3564 while (bl != NULL)
3565 {
3566 next = bl->sb_next;
3567 vim_free(bl);
3568 bl = next;
3569 }
3570}
3571
3572/*
3573 * Allocate the root of a word tree.
3574 */
3575 static wordnode_T *
3576wordtree_alloc(blp)
3577 sblock_T **blp;
3578{
3579 return (wordnode_T *)getroom(blp, sizeof(wordnode_T));
3580}
3581
3582/*
3583 * Store a word in the tree(s).
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003584 * Always store it in the case-folded tree. A keep-case word can also be used
3585 * with all caps.
Bram Moolenaar51485f02005-06-04 21:55:20 +00003586 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003587 * When "pfxlist" is not NULL store the word for each prefix ID.
Bram Moolenaar51485f02005-06-04 21:55:20 +00003588 */
3589 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003590store_word(word, spin, flags, region, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003591 char_u *word;
3592 spellinfo_T *spin;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003593 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003594 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003595 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003596{
3597 int len = STRLEN(word);
3598 int ct = captype(word, word + len);
3599 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003600 int res = OK;
3601 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003602
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003603 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003604 for (p = pfxlist; res == OK; ++p)
3605 {
3606 res = tree_add_word(foldword, spin->si_foldroot, ct | flags,
3607 region, p == NULL ? 0 : *p, &spin->si_blocks);
3608 if (p == NULL || *p == NUL)
3609 break;
3610 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00003611 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003612
3613 if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP))
Bram Moolenaar8db73182005-06-17 21:51:16 +00003614 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003615 for (p = pfxlist; res == OK; ++p)
3616 {
3617 res = tree_add_word(word, spin->si_keeproot, flags,
3618 region, p == NULL ? 0 : *p, &spin->si_blocks);
3619 if (p == NULL || *p == NUL)
3620 break;
3621 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00003622 ++spin->si_keepwcount;
3623 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003624 return res;
3625}
3626
3627/*
3628 * Add word "word" to a word tree at "root".
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003629 * When "flags" is -1 we are adding to the prefix tree where flags don't
3630 * matter and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003631 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003632 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003633 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003634tree_add_word(word, root, flags, region, prefixID, blp)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003635 char_u *word;
3636 wordnode_T *root;
3637 int flags;
3638 int region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003639 int prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003640 sblock_T **blp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003641{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003642 wordnode_T *node = root;
3643 wordnode_T *np;
3644 wordnode_T **prev = NULL;
3645 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003646
Bram Moolenaar51485f02005-06-04 21:55:20 +00003647 /* Add each byte of the word to the tree, including the NUL at the end. */
3648 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003649 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003650 /* Look for the sibling that has the same character. They are sorted
3651 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003652 * higher byte value. For zero bytes (end of word) the sorting is
3653 * done on flags and then on prefixID
Bram Moolenaar51485f02005-06-04 21:55:20 +00003654 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003655 while (node != NULL
3656 && (node->wn_byte < word[i]
3657 || (node->wn_byte == NUL
3658 && (flags < 0
3659 ? node->wn_prefixID < prefixID
3660 : node->wn_flags < (flags & 0xff)
3661 || (node->wn_flags == (flags & 0xff)
3662 && node->wn_prefixID < prefixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003663 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003664 prev = &node->wn_sibling;
3665 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003666 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003667 if (node == NULL
3668 || node->wn_byte != word[i]
3669 || (word[i] == NUL
3670 && (flags < 0
3671 || node->wn_flags != (flags & 0xff)
3672 || node->wn_prefixID != prefixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003673 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003674 /* Allocate a new node. */
3675 np = (wordnode_T *)getroom(blp, sizeof(wordnode_T));
3676 if (np == NULL)
3677 return FAIL;
3678 np->wn_byte = word[i];
3679 *prev = np;
3680 np->wn_sibling = node;
3681 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003682 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003683
Bram Moolenaar51485f02005-06-04 21:55:20 +00003684 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003685 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003686 node->wn_flags = flags;
3687 node->wn_region |= region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003688 node->wn_prefixID = prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003689 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003690 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003691 prev = &node->wn_child;
3692 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003693 }
3694
3695 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003696}
3697
3698/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003699 * Compress a tree: find tails that are identical and can be shared.
3700 */
3701 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00003702wordtree_compress(root, spin)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003703 wordnode_T *root;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003704 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003705{
3706 hashtab_T ht;
3707 int n;
3708 int tot = 0;
3709
3710 if (root != NULL)
3711 {
3712 hash_init(&ht);
3713 n = node_compress(root, &ht, &tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003714 if (spin->si_verbose || p_verbose > 2)
3715 {
3716 if (!spin->si_verbose)
3717 verbose_enter();
3718 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar51485f02005-06-04 21:55:20 +00003719 n, tot, (tot - n) * 100 / tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003720 if (p_verbose > 2)
3721 verbose_leave();
3722 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003723 hash_clear(&ht);
3724 }
3725}
3726
3727/*
3728 * Compress a node, its siblings and its children, depth first.
3729 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003730 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003731 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00003732node_compress(node, ht, tot)
3733 wordnode_T *node;
3734 hashtab_T *ht;
3735 int *tot; /* total count of nodes before compressing,
3736 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003737{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003738 wordnode_T *np;
3739 wordnode_T *tp;
3740 wordnode_T *child;
3741 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003742 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003743 int len = 0;
3744 unsigned nr, n;
3745 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003746
Bram Moolenaar51485f02005-06-04 21:55:20 +00003747 /*
3748 * Go through the list of siblings. Compress each child and then try
3749 * finding an identical child to replace it.
3750 * Note that with "child" we mean not just the node that is pointed to,
3751 * but the whole list of siblings, of which the node is the first.
3752 */
3753 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003754 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003755 ++len;
3756 if ((child = np->wn_child) != NULL)
3757 {
3758 /* Compress the child. This fills wn_hashkey. */
3759 compressed += node_compress(child, ht, tot);
3760
3761 /* Try to find an identical child. */
3762 hash = hash_hash(child->wn_hashkey);
3763 hi = hash_lookup(ht, child->wn_hashkey, hash);
3764 tp = NULL;
3765 if (!HASHITEM_EMPTY(hi))
3766 {
3767 /* There are children with an identical hash value. Now check
3768 * if there is one that is really identical. */
3769 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_next)
3770 if (node_equal(child, tp))
3771 {
3772 /* Found one! Now use that child in place of the
3773 * current one. This means the current child is
3774 * dropped from the tree. */
3775 np->wn_child = tp;
3776 ++compressed;
3777 break;
3778 }
3779 if (tp == NULL)
3780 {
3781 /* No other child with this hash value equals the child of
3782 * the node, add it to the linked list after the first
3783 * item. */
3784 tp = HI2WN(hi);
3785 child->wn_next = tp->wn_next;
3786 tp->wn_next = child;
3787 }
3788 }
3789 else
3790 /* No other child has this hash value, add it to the
3791 * hashtable. */
3792 hash_add_item(ht, hi, child->wn_hashkey, hash);
3793 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003794 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003795 *tot += len;
3796
3797 /*
3798 * Make a hash key for the node and its siblings, so that we can quickly
3799 * find a lookalike node. This must be done after compressing the sibling
3800 * list, otherwise the hash key would become invalid by the compression.
3801 */
3802 node->wn_hashkey[0] = len;
3803 nr = 0;
3804 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003805 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003806 if (np->wn_byte == NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003807 /* end node: use wn_flags, wn_region and wn_prefixID */
3808 n = np->wn_flags + (np->wn_region << 8) + (np->wn_prefixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003809 else
3810 /* byte node: use the byte value and the child pointer */
3811 n = np->wn_byte + ((long_u)np->wn_child << 8);
3812 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003813 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003814
3815 /* Avoid NUL bytes, it terminates the hash key. */
3816 n = nr & 0xff;
3817 node->wn_hashkey[1] = n == 0 ? 1 : n;
3818 n = (nr >> 8) & 0xff;
3819 node->wn_hashkey[2] = n == 0 ? 1 : n;
3820 n = (nr >> 16) & 0xff;
3821 node->wn_hashkey[3] = n == 0 ? 1 : n;
3822 n = (nr >> 24) & 0xff;
3823 node->wn_hashkey[4] = n == 0 ? 1 : n;
3824 node->wn_hashkey[5] = NUL;
3825
3826 return compressed;
3827}
3828
3829/*
3830 * Return TRUE when two nodes have identical siblings and children.
3831 */
3832 static int
3833node_equal(n1, n2)
3834 wordnode_T *n1;
3835 wordnode_T *n2;
3836{
3837 wordnode_T *p1;
3838 wordnode_T *p2;
3839
3840 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
3841 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
3842 if (p1->wn_byte != p2->wn_byte
3843 || (p1->wn_byte == NUL
3844 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003845 || p1->wn_region != p2->wn_region
3846 || p1->wn_prefixID != p2->wn_prefixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003847 : (p1->wn_child != p2->wn_child)))
3848 break;
3849
3850 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003851}
3852
3853/*
3854 * Write a number to file "fd", MSB first, in "len" bytes.
3855 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003856 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003857put_bytes(fd, nr, len)
3858 FILE *fd;
3859 long_u nr;
3860 int len;
3861{
3862 int i;
3863
3864 for (i = len - 1; i >= 0; --i)
3865 putc((int)(nr >> (i * 8)), fd);
3866}
3867
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003868static int
3869#ifdef __BORLANDC__
3870_RTLENTRYF
3871#endif
3872rep_compare __ARGS((const void *s1, const void *s2));
3873
3874/*
3875 * Function given to qsort() to sort the REP items on "from" string.
3876 */
3877 static int
3878#ifdef __BORLANDC__
3879_RTLENTRYF
3880#endif
3881rep_compare(s1, s2)
3882 const void *s1;
3883 const void *s2;
3884{
3885 fromto_T *p1 = (fromto_T *)s1;
3886 fromto_T *p2 = (fromto_T *)s2;
3887
3888 return STRCMP(p1->ft_from, p2->ft_from);
3889}
3890
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003891/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003892 * Write the Vim spell file "fname".
3893 */
3894 static void
Bram Moolenaar3982c542005-06-08 21:56:31 +00003895write_vim_spell(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003896 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003897 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003898{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003899 FILE *fd;
3900 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003901 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003902 wordnode_T *tree;
3903 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003904 int i;
3905 int l;
3906 garray_T *gap;
3907 fromto_T *ftp;
3908 char_u *p;
3909 int rr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003910
Bram Moolenaarb765d632005-06-07 21:00:02 +00003911 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00003912 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003913 {
3914 EMSG2(_(e_notopen), fname);
3915 return;
3916 }
3917
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003918 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003919 * <charflagslen> <charflags>
3920 * <fcharslen> <fchars>
3921 * <prefcondcnt> <prefcond> ... */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003922
3923 /* <fileID> */
3924 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
3925 EMSG(_(e_write));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003926
3927 /* write the region names if there is more than one */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003928 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003929 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003930 putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */
3931 fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2),
3932 (size_t)1, fd);
3933 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003934 }
3935 else
3936 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003937 putc(0, fd);
3938 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003939 }
3940
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003941 /*
3942 * Write the table with character flags and table for case folding.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003943 * <charflagslen> <charflags> <fcharlen> <fchars>
3944 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003945 * 'encoding'.
3946 * Also skip this for an .add.spl file, the main spell file must contain
3947 * the table (avoids that it conflicts). File is shorter too.
3948 */
3949 if (spin->si_ascii || spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003950 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003951 putc(0, fd);
3952 putc(0, fd);
3953 putc(0, fd);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003954 }
3955 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00003956 write_spell_chartab(fd);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003957
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003958 /* Write the prefix conditions. */
3959 write_spell_prefcond(fd, &spin->si_prefcond);
3960
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003961 /* Sort the REP items. */
3962 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
3963 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003964
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003965 /* <SUGGEST> : <repcount> <rep> ...
3966 * <salflags> <salcount> <sal> ...
3967 * <maplen> <mapstr> */
3968 for (round = 1; round <= 2; ++round)
3969 {
3970 if (round == 1)
3971 gap = &spin->si_rep;
3972 else
3973 {
3974 gap = &spin->si_sal;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003975
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003976 i = 0;
3977 if (spin->si_followup)
3978 i |= SAL_F0LLOWUP;
3979 if (spin->si_collapse)
3980 i |= SAL_COLLAPSE;
3981 if (spin->si_rem_accents)
3982 i |= SAL_REM_ACCENTS;
3983 putc(i, fd); /* <salflags> */
3984 }
3985
3986 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
3987 for (i = 0; i < gap->ga_len; ++i)
3988 {
3989 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
3990 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
3991 ftp = &((fromto_T *)gap->ga_data)[i];
3992 for (rr = 1; rr <= 2; ++rr)
3993 {
3994 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
3995 l = STRLEN(p);
3996 putc(l, fd);
3997 fwrite(p, l, (size_t)1, fd);
3998 }
3999 }
4000 }
4001
4002 put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */
4003 if (spin->si_map.ga_len > 0) /* <mapstr> */
4004 fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len,
4005 (size_t)1, fd);
Bram Moolenaar50cde822005-06-05 21:54:54 +00004006
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004007 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004008 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004009 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004010 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004011 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004012 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004013 if (round == 1)
4014 tree = spin->si_foldroot;
4015 else if (round == 2)
4016 tree = spin->si_keeproot;
4017 else
4018 tree = spin->si_prefroot;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004019
Bram Moolenaar51485f02005-06-04 21:55:20 +00004020 /* Count the number of nodes. Needed to be able to allocate the
4021 * memory when reading the nodes. Also fills in the index for shared
4022 * nodes. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004023 nodecount = put_tree(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004024
Bram Moolenaar51485f02005-06-04 21:55:20 +00004025 /* number of nodes in 4 bytes */
4026 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004027 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004028
Bram Moolenaar51485f02005-06-04 21:55:20 +00004029 /* Write the nodes. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004030 (void)put_tree(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004031 }
4032
Bram Moolenaar51485f02005-06-04 21:55:20 +00004033 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004034}
4035
4036/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004037 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004038 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00004039 * This first writes the list of possible bytes (siblings). Then for each
4040 * byte recursively write the children.
4041 *
4042 * NOTE: The code here must match the code in read_tree(), since assumptions
4043 * are made about the indexes (so that we don't have to write them in the
4044 * file).
4045 *
4046 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004047 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004048 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004049put_tree(fd, node, index, regionmask, prefixtree)
4050 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004051 wordnode_T *node;
4052 int index;
4053 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004054 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004055{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004056 int newindex = index;
4057 int siblingcount = 0;
4058 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004059 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004060
Bram Moolenaar51485f02005-06-04 21:55:20 +00004061 /* If "node" is zero the tree is empty. */
4062 if (node == NULL)
4063 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004064
Bram Moolenaar51485f02005-06-04 21:55:20 +00004065 /* Store the index where this node is written. */
4066 node->wn_index = index;
4067
4068 /* Count the number of siblings. */
4069 for (np = node; np != NULL; np = np->wn_sibling)
4070 ++siblingcount;
4071
4072 /* Write the sibling count. */
4073 if (fd != NULL)
4074 putc(siblingcount, fd); /* <siblingcount> */
4075
4076 /* Write each sibling byte and optionally extra info. */
4077 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004078 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004079 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004080 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004081 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004082 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004083 /* For a NUL byte (end of word) write the flags etc. */
4084 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004085 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004086 /* In PREFIXTREE write the required prefixID and the
4087 * associated condition nr (stored in wn_region). */
4088 putc(BY_FLAGS, fd); /* <byte> */
4089 putc(np->wn_prefixID, fd); /* <prefixID> */
4090 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004091 }
4092 else
4093 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004094 /* For word trees we write the flag/region items. */
4095 flags = np->wn_flags;
4096 if (regionmask != 0 && np->wn_region != regionmask)
4097 flags |= WF_REGION;
4098 if (np->wn_prefixID != 0)
4099 flags |= WF_PFX;
4100 if (flags == 0)
4101 {
4102 /* word without flags or region */
4103 putc(BY_NOFLAGS, fd); /* <byte> */
4104 }
4105 else
4106 {
4107 putc(BY_FLAGS, fd); /* <byte> */
4108 putc(flags, fd); /* <flags> */
4109 if (flags & WF_REGION)
4110 putc(np->wn_region, fd); /* <region> */
4111 if (flags & WF_PFX)
4112 putc(np->wn_prefixID, fd); /* <prefixID> */
4113 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004114 }
4115 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004116 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004117 else
4118 {
4119 if (np->wn_child->wn_index != 0 && np->wn_child->wn_wnode != node)
4120 {
4121 /* The child is written elsewhere, write the reference. */
4122 if (fd != NULL)
4123 {
4124 putc(BY_INDEX, fd); /* <byte> */
4125 /* <nodeidx> */
4126 put_bytes(fd, (long_u)np->wn_child->wn_index, 3);
4127 }
4128 }
4129 else if (np->wn_child->wn_wnode == NULL)
4130 /* We will write the child below and give it an index. */
4131 np->wn_child->wn_wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004132
Bram Moolenaar51485f02005-06-04 21:55:20 +00004133 if (fd != NULL)
4134 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
4135 {
4136 EMSG(_(e_write));
4137 return 0;
4138 }
4139 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004140 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004141
4142 /* Space used in the array when reading: one for each sibling and one for
4143 * the count. */
4144 newindex += siblingcount + 1;
4145
4146 /* Recursively dump the children of each sibling. */
4147 for (np = node; np != NULL; np = np->wn_sibling)
4148 if (np->wn_byte != 0 && np->wn_child->wn_wnode == node)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004149 newindex = put_tree(fd, np->wn_child, newindex, regionmask,
4150 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004151
4152 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004153}
4154
4155
4156/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00004157 * ":mkspell [-ascii] outfile infile ..."
4158 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004159 */
4160 void
4161ex_mkspell(eap)
4162 exarg_T *eap;
4163{
4164 int fcount;
4165 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004166 char_u *arg = eap->arg;
4167 int ascii = FALSE;
4168
4169 if (STRNCMP(arg, "-ascii", 6) == 0)
4170 {
4171 ascii = TRUE;
4172 arg = skipwhite(arg + 6);
4173 }
4174
4175 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
4176 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
4177 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004178 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004179 FreeWild(fcount, fnames);
4180 }
4181}
4182
4183/*
4184 * Create a Vim spell file from one or more word lists.
4185 * "fnames[0]" is the output file name.
4186 * "fnames[fcount - 1]" is the last input file name.
4187 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
4188 * and ".spl" is appended to make the output file name.
4189 */
4190 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004191mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004192 int fcount;
4193 char_u **fnames;
4194 int ascii; /* -ascii argument given */
4195 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004196 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004197{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004198 char_u fname[MAXPATHL];
4199 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00004200 char_u **innames;
4201 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004202 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004203 int i;
4204 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004205 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004206 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004207 spellinfo_T spin;
4208
4209 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004210 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004211 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004212 spin.si_followup = TRUE;
4213 spin.si_rem_accents = TRUE;
4214 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
4215 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
4216 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004217 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004218
Bram Moolenaarb765d632005-06-07 21:00:02 +00004219 /* default: fnames[0] is output file, following are input files */
4220 innames = &fnames[1];
4221 incount = fcount - 1;
4222
4223 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004224 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004225 len = STRLEN(fnames[0]);
4226 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
4227 {
4228 /* For ":mkspell path/en.latin1.add" output file is
4229 * "path/en.latin1.add.spl". */
4230 innames = &fnames[0];
4231 incount = 1;
4232 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
4233 }
4234 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
4235 {
4236 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004237 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004238 }
4239 else
4240 /* Name should be language, make the file name from it. */
4241 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
4242 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
4243
4244 /* Check for .ascii.spl. */
4245 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
4246 spin.si_ascii = TRUE;
4247
4248 /* Check for .add.spl. */
4249 if (strstr((char *)gettail(wfname), ".add.") != NULL)
4250 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004251 }
4252
Bram Moolenaarb765d632005-06-07 21:00:02 +00004253 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004254 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004255 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004256 EMSG(_("E754: Only up to 8 regions supported"));
4257 else
4258 {
4259 /* Check for overwriting before doing things that may take a lot of
4260 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004261 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004262 {
4263 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004264 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004265 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004266 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004267 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004268 EMSG2(_(e_isadir2), wfname);
4269 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004270 }
4271
4272 /*
4273 * Init the aff and dic pointers.
4274 * Get the region names if there are more than 2 arguments.
4275 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004276 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004277 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004278 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004279
Bram Moolenaar3982c542005-06-08 21:56:31 +00004280 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004281 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004282 len = STRLEN(innames[i]);
4283 if (STRLEN(gettail(innames[i])) < 5
4284 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004285 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004286 EMSG2(_("E755: Invalid region in %s"), innames[i]);
4287 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004288 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00004289 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
4290 spin.si_region_name[i * 2 + 1] =
4291 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004292 }
4293 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00004294 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004295
Bram Moolenaarb765d632005-06-07 21:00:02 +00004296 if (!spin.si_add)
4297 /* Clear the char type tables, don't want to use any of the
4298 * currently used spell properties. */
4299 init_spell_chartab();
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004300
Bram Moolenaar51485f02005-06-04 21:55:20 +00004301 spin.si_foldroot = wordtree_alloc(&spin.si_blocks);
4302 spin.si_keeproot = wordtree_alloc(&spin.si_blocks);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004303 spin.si_prefroot = wordtree_alloc(&spin.si_blocks);
4304 if (spin.si_foldroot == NULL
4305 || spin.si_keeproot == NULL
4306 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004307 {
4308 error = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004309 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004310 }
4311
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004312 /*
4313 * Read all the .aff and .dic files.
4314 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004315 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004316 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004317 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004318 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004319 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004320 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004321
Bram Moolenaarb765d632005-06-07 21:00:02 +00004322 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004323 if (mch_stat((char *)fname, &st) >= 0)
4324 {
4325 /* Read the .aff file. Will init "spin->si_conv" based on the
4326 * "SET" line. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004327 afile[i] = spell_read_aff(fname, &spin);
4328 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004329 error = TRUE;
4330 else
4331 {
4332 /* Read the .dic file and store the words in the trees. */
4333 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00004334 innames[i]);
4335 if (spell_read_dic(fname, &spin, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004336 error = TRUE;
4337 }
4338 }
4339 else
4340 {
4341 /* No .aff file, try reading the file as a word list. Store
4342 * the words in the trees. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004343 if (spell_read_wordfile(innames[i], &spin) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004344 error = TRUE;
4345 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004346
Bram Moolenaarb765d632005-06-07 21:00:02 +00004347#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004348 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004349 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004350#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004351 }
4352
Bram Moolenaar51485f02005-06-04 21:55:20 +00004353 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004354 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004355 /*
4356 * Remove the dummy NUL from the start of the tree root.
4357 */
4358 spin.si_foldroot = spin.si_foldroot->wn_sibling;
4359 spin.si_keeproot = spin.si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004360 spin.si_prefroot = spin.si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004361
4362 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004363 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004364 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004365 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004366 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004367 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004368 verbose_enter();
4369 MSG(_("Compressing word tree..."));
4370 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004371 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004372 verbose_leave();
4373 }
4374 wordtree_compress(spin.si_foldroot, &spin);
4375 wordtree_compress(spin.si_keeproot, &spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004376 wordtree_compress(spin.si_prefroot, &spin);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004377 }
4378
Bram Moolenaar51485f02005-06-04 21:55:20 +00004379 if (!error)
4380 {
4381 /*
4382 * Write the info in the spell file.
4383 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004384 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004386 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004387 verbose_enter();
4388 smsg((char_u *)_("Writing spell file %s..."), wfname);
4389 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004390 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004391 verbose_leave();
4392 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00004393
Bram Moolenaar3982c542005-06-08 21:56:31 +00004394 write_vim_spell(wfname, &spin);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004395
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004396 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004397 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004398 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004399 verbose_enter();
4400 MSG(_("Done!"));
4401 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00004402 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004403 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004404 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004405 verbose_leave();
4406 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004407
Bram Moolenaarb765d632005-06-07 21:00:02 +00004408 /* If the file is loaded need to reload it. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004409 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004410 }
4411
4412 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004413 ga_clear(&spin.si_rep);
4414 ga_clear(&spin.si_sal);
4415 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004416 ga_clear(&spin.si_prefcond);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004417
4418 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004419 for (i = 0; i < incount; ++i)
4420 if (afile[i] != NULL)
4421 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004422
4423 /* Free all the bits and pieces at once. */
4424 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004425 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004426}
4427
Bram Moolenaarb765d632005-06-07 21:00:02 +00004428
4429/*
4430 * ":spellgood {word}"
4431 * ":spellwrong {word}"
4432 */
4433 void
4434ex_spell(eap)
4435 exarg_T *eap;
4436{
4437 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong);
4438}
4439
4440/*
4441 * Add "word[len]" to 'spellfile' as a good or bad word.
4442 */
4443 void
4444spell_add_word(word, len, bad)
4445 char_u *word;
4446 int len;
4447 int bad;
4448{
4449 FILE *fd;
4450 buf_T *buf;
4451
4452 if (*curbuf->b_p_spf == NUL)
4453 init_spellfile();
4454 if (*curbuf->b_p_spf == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004455 EMSG(_("E764: 'spellfile' is not set"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004456 else
4457 {
4458 /* Check that the user isn't editing the .add file somewhere. */
4459 buf = buflist_findname_exp(curbuf->b_p_spf);
4460 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
4461 buf = NULL;
4462 if (buf != NULL && bufIsChanged(buf))
4463 EMSG(_(e_bufloaded));
4464 else
4465 {
4466 fd = mch_fopen((char *)curbuf->b_p_spf, "a");
4467 if (fd == NULL)
4468 EMSG2(_(e_notopen), curbuf->b_p_spf);
4469 else
4470 {
4471 if (bad)
4472 fprintf(fd, "/!%.*s\n", len, word);
4473 else
4474 fprintf(fd, "%.*s\n", len, word);
4475 fclose(fd);
4476
4477 /* Update the .add.spl file. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004478 mkspell(1, &curbuf->b_p_spf, FALSE, TRUE, TRUE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004479
4480 /* If the .add file is edited somewhere, reload it. */
4481 if (buf != NULL)
4482 buf_reload(buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004483
4484 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004485 }
4486 }
4487 }
4488}
4489
4490/*
4491 * Initialize 'spellfile' for the current buffer.
4492 */
4493 static void
4494init_spellfile()
4495{
4496 char_u buf[MAXPATHL];
4497 int l;
4498 slang_T *sl;
4499 char_u *rtp;
4500
4501 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
4502 {
4503 /* Loop over all entries in 'runtimepath'. */
4504 rtp = p_rtp;
4505 while (*rtp != NUL)
4506 {
4507 /* Copy the path from 'runtimepath' to buf[]. */
4508 copy_option_part(&rtp, buf, MAXPATHL, ",");
4509 if (filewritable(buf) == 2)
4510 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004511 /* Use the first language name from 'spelllang' and the
4512 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004513 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
4514 l = STRLEN(buf);
4515 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00004516 "/spell/%.*s.%s.add",
4517 2, curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00004518 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
4519 ? (char_u *)"ascii" : spell_enc());
4520 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
4521 break;
4522 }
4523 }
4524 }
4525}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004526
Bram Moolenaar51485f02005-06-04 21:55:20 +00004527
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004528/*
4529 * Init the chartab used for spelling for ASCII.
4530 * EBCDIC is not supported!
4531 */
4532 static void
4533clear_spell_chartab(sp)
4534 spelltab_T *sp;
4535{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004536 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004537
4538 /* Init everything to FALSE. */
4539 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
4540 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
4541 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004542 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004543 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004544 sp->st_upper[i] = i;
4545 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004546
4547 /* We include digits. A word shouldn't start with a digit, but handling
4548 * that is done separately. */
4549 for (i = '0'; i <= '9'; ++i)
4550 sp->st_isw[i] = TRUE;
4551 for (i = 'A'; i <= 'Z'; ++i)
4552 {
4553 sp->st_isw[i] = TRUE;
4554 sp->st_isu[i] = TRUE;
4555 sp->st_fold[i] = i + 0x20;
4556 }
4557 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004558 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004559 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004560 sp->st_upper[i] = i - 0x20;
4561 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004562}
4563
4564/*
4565 * Init the chartab used for spelling. Only depends on 'encoding'.
4566 * Called once while starting up and when 'encoding' changes.
4567 * The default is to use isalpha(), but the spell file should define the word
4568 * characters to make it possible that 'encoding' differs from the current
4569 * locale.
4570 */
4571 void
4572init_spell_chartab()
4573{
4574 int i;
4575
4576 did_set_spelltab = FALSE;
4577 clear_spell_chartab(&spelltab);
4578
4579#ifdef FEAT_MBYTE
4580 if (enc_dbcs)
4581 {
4582 /* DBCS: assume double-wide characters are word characters. */
4583 for (i = 128; i <= 255; ++i)
4584 if (MB_BYTE2LEN(i) == 2)
4585 spelltab.st_isw[i] = TRUE;
4586 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004587 else if (enc_utf8)
4588 {
4589 for (i = 128; i < 256; ++i)
4590 {
4591 spelltab.st_isu[i] = utf_isupper(i);
4592 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
4593 spelltab.st_fold[i] = utf_fold(i);
4594 spelltab.st_upper[i] = utf_toupper(i);
4595 }
4596 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004597 else
4598#endif
4599 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004600 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004601 for (i = 128; i < 256; ++i)
4602 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004603 if (MB_ISUPPER(i))
4604 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004605 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004606 spelltab.st_isu[i] = TRUE;
4607 spelltab.st_fold[i] = MB_TOLOWER(i);
4608 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004609 else if (MB_ISLOWER(i))
4610 {
4611 spelltab.st_isw[i] = TRUE;
4612 spelltab.st_upper[i] = MB_TOUPPER(i);
4613 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004614 }
4615 }
4616}
4617
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004618static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
4619static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
4620
4621/*
4622 * Set the spell character tables from strings in the affix file.
4623 */
4624 static int
4625set_spell_chartab(fol, low, upp)
4626 char_u *fol;
4627 char_u *low;
4628 char_u *upp;
4629{
4630 /* We build the new tables here first, so that we can compare with the
4631 * previous one. */
4632 spelltab_T new_st;
4633 char_u *pf = fol, *pl = low, *pu = upp;
4634 int f, l, u;
4635
4636 clear_spell_chartab(&new_st);
4637
4638 while (*pf != NUL)
4639 {
4640 if (*pl == NUL || *pu == NUL)
4641 {
4642 EMSG(_(e_affform));
4643 return FAIL;
4644 }
4645#ifdef FEAT_MBYTE
4646 f = mb_ptr2char_adv(&pf);
4647 l = mb_ptr2char_adv(&pl);
4648 u = mb_ptr2char_adv(&pu);
4649#else
4650 f = *pf++;
4651 l = *pl++;
4652 u = *pu++;
4653#endif
4654 /* Every character that appears is a word character. */
4655 if (f < 256)
4656 new_st.st_isw[f] = TRUE;
4657 if (l < 256)
4658 new_st.st_isw[l] = TRUE;
4659 if (u < 256)
4660 new_st.st_isw[u] = TRUE;
4661
4662 /* if "LOW" and "FOL" are not the same the "LOW" char needs
4663 * case-folding */
4664 if (l < 256 && l != f)
4665 {
4666 if (f >= 256)
4667 {
4668 EMSG(_(e_affrange));
4669 return FAIL;
4670 }
4671 new_st.st_fold[l] = f;
4672 }
4673
4674 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004675 * case-folding, it's upper case and the "UPP" is the upper case of
4676 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004677 if (u < 256 && u != f)
4678 {
4679 if (f >= 256)
4680 {
4681 EMSG(_(e_affrange));
4682 return FAIL;
4683 }
4684 new_st.st_fold[u] = f;
4685 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004686 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004687 }
4688 }
4689
4690 if (*pl != NUL || *pu != NUL)
4691 {
4692 EMSG(_(e_affform));
4693 return FAIL;
4694 }
4695
4696 return set_spell_finish(&new_st);
4697}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004698
4699/*
4700 * Set the spell character tables from strings in the .spl file.
4701 */
4702 static int
4703set_spell_charflags(flags, cnt, upp)
4704 char_u *flags;
4705 int cnt;
4706 char_u *upp;
4707{
4708 /* We build the new tables here first, so that we can compare with the
4709 * previous one. */
4710 spelltab_T new_st;
4711 int i;
4712 char_u *p = upp;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004713 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004714
4715 clear_spell_chartab(&new_st);
4716
4717 for (i = 0; i < cnt; ++i)
4718 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004719 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
4720 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004721
4722 if (*p == NUL)
4723 return FAIL;
4724#ifdef FEAT_MBYTE
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004725 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004726#else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004727 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004728#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004729 new_st.st_fold[i + 128] = c;
4730 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
4731 new_st.st_upper[c] = i + 128;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004732 }
4733
4734 return set_spell_finish(&new_st);
4735}
4736
4737 static int
4738set_spell_finish(new_st)
4739 spelltab_T *new_st;
4740{
4741 int i;
4742
4743 if (did_set_spelltab)
4744 {
4745 /* check that it's the same table */
4746 for (i = 0; i < 256; ++i)
4747 {
4748 if (spelltab.st_isw[i] != new_st->st_isw[i]
4749 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004750 || spelltab.st_fold[i] != new_st->st_fold[i]
4751 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004752 {
4753 EMSG(_("E763: Word characters differ between spell files"));
4754 return FAIL;
4755 }
4756 }
4757 }
4758 else
4759 {
4760 /* copy the new spelltab into the one being used */
4761 spelltab = *new_st;
4762 did_set_spelltab = TRUE;
4763 }
4764
4765 return OK;
4766}
4767
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004768/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004769 * Write the table with prefix conditions to the .spl file.
4770 */
4771 static void
4772write_spell_prefcond(fd, gap)
4773 FILE *fd;
4774 garray_T *gap;
4775{
4776 int i;
4777 char_u *p;
4778 int len;
4779
4780 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
4781
4782 for (i = 0; i < gap->ga_len; ++i)
4783 {
4784 /* <prefcond> : <condlen> <condstr> */
4785 p = ((char_u **)gap->ga_data)[i];
4786 if (p == NULL)
4787 fputc(0, fd);
4788 else
4789 {
4790 len = STRLEN(p);
4791 fputc(len, fd);
4792 fwrite(p, (size_t)len, (size_t)1, fd);
4793 }
4794 }
4795}
4796
4797/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004798 * Write the current tables into the .spl file.
4799 * This makes sure the same characters are recognized as word characters when
4800 * generating an when using a spell file.
4801 */
4802 static void
4803write_spell_chartab(fd)
4804 FILE *fd;
4805{
4806 char_u charbuf[256 * 4];
4807 int len = 0;
4808 int flags;
4809 int i;
4810
4811 fputc(128, fd); /* <charflagslen> */
4812 for (i = 128; i < 256; ++i)
4813 {
4814 flags = 0;
4815 if (spelltab.st_isw[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004816 flags |= CF_WORD;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004817 if (spelltab.st_isu[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004818 flags |= CF_UPPER;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004819 fputc(flags, fd); /* <charflags> */
4820
Bram Moolenaarb765d632005-06-07 21:00:02 +00004821#ifdef FEAT_MBYTE
4822 if (has_mbyte)
4823 len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
4824 else
4825#endif
4826 charbuf[len++] = spelltab.st_fold[i];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004827 }
4828
4829 put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
4830 fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
4831}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004832
4833/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004834 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
4835 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004836 * When using a multi-byte 'encoding' the length may change!
4837 * Returns FAIL when something wrong.
4838 */
4839 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004840spell_casefold(str, len, buf, buflen)
4841 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004842 int len;
4843 char_u *buf;
4844 int buflen;
4845{
4846 int i;
4847
4848 if (len >= buflen)
4849 {
4850 buf[0] = NUL;
4851 return FAIL; /* result will not fit */
4852 }
4853
4854#ifdef FEAT_MBYTE
4855 if (has_mbyte)
4856 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004857 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004858 char_u *p;
4859 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004860
4861 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004862 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004863 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004864 if (outi + MB_MAXBYTES > buflen)
4865 {
4866 buf[outi] = NUL;
4867 return FAIL;
4868 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004869 c = mb_ptr2char_adv(&p);
4870 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004871 }
4872 buf[outi] = NUL;
4873 }
4874 else
4875#endif
4876 {
4877 /* Be quick for non-multibyte encodings. */
4878 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004879 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004880 buf[i] = NUL;
4881 }
4882
4883 return OK;
4884}
4885
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004886/*
4887 * "z?": Find badly spelled word under or after the cursor.
4888 * Give suggestions for the properly spelled word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004889 */
4890 void
4891spell_suggest()
4892{
4893 char_u *line;
4894 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004895 char_u wcopy[MAXWLEN + 2];
4896 char_u *p;
4897 int i;
4898 int c;
4899 suginfo_T sug;
4900 suggest_T *stp;
4901
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004902 /* Find the start of the badly spelled word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004903 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL)
4904 {
4905 beep_flush();
4906 return;
4907 }
4908
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004909 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004910 line = ml_get_curline();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004911
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004912 /* Get the list of suggestions */
4913 spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004914
4915 if (sug.su_ga.ga_len == 0)
4916 MSG(_("Sorry, no suggestions"));
4917 else
4918 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004919 /* List the suggestions. */
4920 msg_start();
4921 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
4922 sug.su_badlen, sug.su_badptr);
4923 msg_puts(IObuff);
4924 msg_clr_eos();
4925 msg_putchar('\n');
4926 msg_scroll = TRUE;
4927 for (i = 0; i < sug.su_ga.ga_len; ++i)
4928 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004929 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004930
4931 /* The suggested word may replace only part of the bad word, add
4932 * the not replaced part. */
4933 STRCPY(wcopy, stp->st_word);
4934 if (sug.su_badlen > stp->st_orglen)
4935 vim_strncpy(wcopy + STRLEN(wcopy),
4936 sug.su_badptr + stp->st_orglen,
4937 sug.su_badlen - stp->st_orglen);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004938 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004939 {
4940 if (sps_flags & SPS_DOUBLE)
4941 vim_snprintf((char *)IObuff, IOSIZE,
4942 _("%2d \"%s\" (%s%d - %d)"),
4943 i + 1, wcopy,
4944 stp->st_salscore ? "s " : "",
4945 stp->st_score, stp->st_altscore);
4946 else
4947 vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\" (%d)"),
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004948 i + 1, wcopy, stp->st_score);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004949 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004950 else
4951 vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\""),
4952 i + 1, wcopy);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004953 msg_puts(IObuff);
4954 lines_left = 3; /* avoid more prompt */
4955 msg_putchar('\n');
4956 }
4957
4958 /* Ask for choice. */
4959 i = prompt_for_number();
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004960 if (i > 0 && i <= sug.su_ga.ga_len && u_save_cursor() == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004961 {
4962 /* Replace the word. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004963 stp = &SUG(sug.su_ga, i - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004964 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
4965 if (p != NULL)
4966 {
4967 c = sug.su_badptr - line;
4968 mch_memmove(p, line, c);
4969 STRCPY(p + c, stp->st_word);
4970 STRCAT(p, sug.su_badptr + stp->st_orglen);
4971 ml_replace(curwin->w_cursor.lnum, p, FALSE);
4972 curwin->w_cursor.col = c;
4973 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004974
4975 /* For redo we use a change-word command. */
4976 ResetRedobuff();
4977 AppendToRedobuff((char_u *)"ciw");
4978 AppendToRedobuff(stp->st_word);
4979 AppendCharToRedobuff(ESC);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004980 }
4981 }
4982 else
4983 curwin->w_cursor = prev_cursor;
4984 }
4985
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004986 spell_find_cleanup(&sug);
4987}
4988
4989/*
4990 * Find spell suggestions for "word". Return them in the growarray "*gap" as
4991 * a list of allocated strings.
4992 */
4993 void
4994spell_suggest_list(gap, word, maxcount)
4995 garray_T *gap;
4996 char_u *word;
4997 int maxcount; /* maximum nr of suggestions */
4998{
4999 suginfo_T sug;
5000 int i;
5001 suggest_T *stp;
5002 char_u *wcopy;
5003
5004 spell_find_suggest(word, &sug, maxcount);
5005
5006 /* Make room in "gap". */
5007 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
5008 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
5009 return;
5010
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005011 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005012 {
5013 stp = &SUG(sug.su_ga, i);
5014
5015 /* The suggested word may replace only part of "word", add the not
5016 * replaced part. */
5017 wcopy = alloc(STRLEN(stp->st_word)
5018 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
5019 if (wcopy == NULL)
5020 break;
5021 STRCPY(wcopy, stp->st_word);
5022 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
5023 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
5024 }
5025
5026 spell_find_cleanup(&sug);
5027}
5028
5029/*
5030 * Find spell suggestions for the word at the start of "badptr".
5031 * Return the suggestions in "su->su_ga".
5032 * The maximum number of suggestions is "maxcount".
5033 * Note: does use info for the current window.
5034 * This is based on the mechanisms of Aspell, but completely reimplemented.
5035 */
5036 static void
5037spell_find_suggest(badptr, su, maxcount)
5038 char_u *badptr;
5039 suginfo_T *su;
5040 int maxcount;
5041{
5042 int attr;
5043
5044 /*
5045 * Set the info in "*su".
5046 */
5047 vim_memset(su, 0, sizeof(suginfo_T));
5048 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
5049 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
5050 hash_init(&su->su_banned);
5051
5052 su->su_badptr = badptr;
5053 su->su_badlen = spell_check(curwin, su->su_badptr, &attr);
5054 su->su_maxcount = maxcount;
5055
5056 if (su->su_badlen >= MAXWLEN)
5057 su->su_badlen = MAXWLEN - 1; /* just in case */
5058 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
5059 (void)spell_casefold(su->su_badptr, su->su_badlen,
5060 su->su_fbadword, MAXWLEN);
5061
5062 /* Ban the bad word itself. It may appear in another region. */
5063 add_banned(su, su->su_badword);
5064
5065 /*
5066 * 1. Try inserting/deleting/swapping/changing a letter, use REP entries
5067 * from the .aff file and inserting a space (split the word).
5068 *
5069 * Set a maximum score to limit the combination of operations that is
5070 * tried.
5071 */
5072 su->su_maxscore = SCORE_MAXINIT;
5073 spell_try_change(su);
5074
5075 /* For the resulting top-scorers compute the sound-a-like score. */
5076 if (sps_flags & SPS_DOUBLE)
5077 score_comp_sal(su);
5078
5079 /*
5080 * 2. Try finding sound-a-like words.
5081 *
5082 * Only do this when we don't have a lot of suggestions yet, because it's
5083 * very slow and often doesn't find new suggestions.
5084 */
5085 if ((sps_flags & SPS_DOUBLE)
5086 || (!(sps_flags & SPS_FAST)
5087 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
5088 {
5089 /* Allow a higher score now. */
5090 su->su_maxscore = SCORE_MAXMAX;
5091 spell_try_soundalike(su);
5092 }
5093
5094 /* When CTRL-C was hit while searching do show the results. */
5095 ui_breakcheck();
5096 if (got_int)
5097 {
5098 (void)vgetc();
5099 got_int = FALSE;
5100 }
5101
5102 if (sps_flags & SPS_DOUBLE)
5103 {
5104 /* Combine the two list of suggestions. */
5105 score_combine(su);
5106 }
5107 else if (su->su_ga.ga_len != 0)
5108 {
5109 if (sps_flags & SPS_BEST)
5110 /* Adjust the word score for how it sounds like. */
5111 rescore_suggestions(su);
5112
5113 /* Sort the suggestions and truncate at "maxcount". */
5114 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, maxcount);
5115 }
5116}
5117
5118/*
5119 * Free the info put in "*su" by spell_find_suggest().
5120 */
5121 static void
5122spell_find_cleanup(su)
5123 suginfo_T *su;
5124{
5125 int i;
5126
5127 /* Free the suggestions. */
5128 for (i = 0; i < su->su_ga.ga_len; ++i)
5129 vim_free(SUG(su->su_ga, i).st_word);
5130 ga_clear(&su->su_ga);
5131 for (i = 0; i < su->su_sga.ga_len; ++i)
5132 vim_free(SUG(su->su_sga, i).st_word);
5133 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005134
5135 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005136 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005137}
5138
5139/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005140 * Make a copy of "word", with the first letter upper or lower cased, to
5141 * "wcopy[MAXWLEN]". "word" must not be empty.
5142 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005143 */
5144 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005145onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005146 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005147 char_u *wcopy;
5148 int upper; /* TRUE: first letter made upper case */
5149{
5150 char_u *p;
5151 int c;
5152 int l;
5153
5154 p = word;
5155#ifdef FEAT_MBYTE
5156 if (has_mbyte)
5157 c = mb_ptr2char_adv(&p);
5158 else
5159#endif
5160 c = *p++;
5161 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005162 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005163 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005164 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005165#ifdef FEAT_MBYTE
5166 if (has_mbyte)
5167 l = mb_char2bytes(c, wcopy);
5168 else
5169#endif
5170 {
5171 l = 1;
5172 wcopy[0] = c;
5173 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005174 vim_strncpy(wcopy + l, p, MAXWLEN - l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005175}
5176
5177/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005178 * Make a copy of "word" with all the letters upper cased into
5179 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005180 */
5181 static void
5182allcap_copy(word, wcopy)
5183 char_u *word;
5184 char_u *wcopy;
5185{
5186 char_u *s;
5187 char_u *d;
5188 int c;
5189
5190 d = wcopy;
5191 for (s = word; *s != NUL; )
5192 {
5193#ifdef FEAT_MBYTE
5194 if (has_mbyte)
5195 c = mb_ptr2char_adv(&s);
5196 else
5197#endif
5198 c = *s++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005199 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005200
5201#ifdef FEAT_MBYTE
5202 if (has_mbyte)
5203 {
5204 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
5205 break;
5206 d += mb_char2bytes(c, d);
5207 }
5208 else
5209#endif
5210 {
5211 if (d - wcopy >= MAXWLEN - 1)
5212 break;
5213 *d++ = c;
5214 }
5215 }
5216 *d = NUL;
5217}
5218
5219/*
5220 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00005221 *
5222 * This uses a state machine. At each node in the tree we try various
5223 * operations. When trying if an operation work "depth" is increased and the
5224 * stack[] is used to store info. This allows combinations, thus insert one
5225 * character, replace one and delete another. The number of changes is
5226 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005227 */
5228 static void
5229spell_try_change(su)
5230 suginfo_T *su;
5231{
5232 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
5233 char_u tword[MAXWLEN]; /* good word collected so far */
5234 trystate_T stack[MAXWLEN];
5235 char_u preword[MAXWLEN * 3]; /* word found with proper case (appended
5236 * to for word split) */
5237 char_u prewordlen = 0; /* length of word in "preword" */
5238 int splitoff = 0; /* index in tword after last split */
5239 trystate_T *sp;
5240 int newscore;
5241 langp_T *lp;
5242 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005243 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005244 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005245 int c, c2, c3;
5246 int n = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005247 int flags;
5248 int badflags;
5249 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005250 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005251 int len;
5252 char_u *p;
5253 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00005254 int fl = 0, tl;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005255
5256 /* get caps flags for bad word */
5257 badflags = captype(su->su_badptr, su->su_badptr + su->su_badlen);
5258
5259 /* We make a copy of the case-folded bad word, so that we can modify it
5260 * to find matches (esp. REP items). */
5261 STRCPY(fword, su->su_fbadword);
5262
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005263
5264 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
5265 lp->lp_slang != NULL; ++lp)
5266 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005267 /*
5268 * Go through the whole case-fold tree, try changes at each node.
5269 * "tword[]" contains the word collected from nodes in the tree.
5270 * "fword[]" the word we are trying to match with (initially the bad
5271 * word).
5272 */
5273 byts = lp->lp_slang->sl_fbyts;
5274 idxs = lp->lp_slang->sl_fidxs;
5275
5276 depth = 0;
5277 stack[0].ts_state = STATE_START;
5278 stack[0].ts_score = 0;
5279 stack[0].ts_curi = 1;
5280 stack[0].ts_fidx = 0;
5281 stack[0].ts_fidxtry = 0;
5282 stack[0].ts_twordlen = 0;
5283 stack[0].ts_arridx = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00005284#ifdef FEAT_MBYTE
5285 stack[0].ts_tcharlen = 0;
5286#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005287
Bram Moolenaarea424162005-06-16 21:51:00 +00005288 /*
5289 * Loop to find all suggestions. At each round we either:
5290 * - For the current state try one operation, advance "ts_curi",
5291 * increase "depth".
5292 * - When a state is done go to the next, set "ts_state".
5293 * - When all states are tried decrease "depth".
5294 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005295 while (depth >= 0 && !got_int)
5296 {
5297 sp = &stack[depth];
5298 switch (sp->ts_state)
5299 {
5300 case STATE_START:
5301 /*
5302 * Start of node: Deal with NUL bytes, which means
5303 * tword[] may end here.
5304 */
5305 arridx = sp->ts_arridx; /* current node in the tree */
5306 len = byts[arridx]; /* bytes in this node */
5307 arridx += sp->ts_curi; /* index of current byte */
5308
5309 if (sp->ts_curi > len || (c = byts[arridx]) != 0)
5310 {
5311 /* Past bytes in node and/or past NUL bytes. */
5312 sp->ts_state = STATE_ENDNUL;
5313 break;
5314 }
5315
5316 /*
5317 * End of word in tree.
5318 */
5319 ++sp->ts_curi; /* eat one NUL byte */
5320
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005321 flags = (int)idxs[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005322
5323 /*
5324 * Form the word with proper case in preword.
5325 * If there is a word from a previous split, append.
5326 */
5327 tword[sp->ts_twordlen] = NUL;
5328 if (flags & WF_KEEPCAP)
5329 /* Must find the word in the keep-case tree. */
5330 find_keepcap_word(lp->lp_slang, tword + splitoff,
5331 preword + prewordlen);
5332 else
5333 /* Include badflags: if the badword is onecap or allcap
5334 * use that for the goodword too. */
5335 make_case_word(tword + splitoff,
5336 preword + prewordlen, flags | badflags);
5337
5338 /* Don't use a banned word. It may appear again as a good
5339 * word, thus remember it. */
5340 if (flags & WF_BANNED)
5341 {
5342 add_banned(su, preword + prewordlen);
5343 break;
5344 }
5345 if (was_banned(su, preword + prewordlen))
5346 break;
5347
5348 newscore = 0;
5349 if ((flags & WF_REGION)
5350 && (((unsigned)flags >> 8) & lp->lp_region) == 0)
5351 newscore += SCORE_REGION;
5352 if (flags & WF_RARE)
5353 newscore += SCORE_RARE;
5354
5355 if (!spell_valid_case(badflags,
5356 captype(preword + prewordlen, NULL)))
5357 newscore += SCORE_ICASE;
5358
5359 if (fword[sp->ts_fidx] == 0)
5360 {
5361 /* The badword also ends: add suggestions, */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005362 add_suggestion(su, &su->su_ga, preword,
5363 sp->ts_score + newscore, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005364 }
Bram Moolenaarea424162005-06-16 21:51:00 +00005365 else if (sp->ts_fidx >= sp->ts_fidxtry
5366#ifdef FEAT_MBYTE
5367 /* Don't split halfway a character. */
5368 && (!has_mbyte || sp->ts_tcharlen == 0)
5369#endif
5370 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005371 {
5372 /* The word in the tree ends but the badword
5373 * continues: try inserting a space and check that a valid
5374 * words starts at fword[sp->ts_fidx]. */
5375 if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT))
5376 {
5377 /* Save things to be restored at STATE_SPLITUNDO. */
5378 sp->ts_save_prewordlen = prewordlen;
5379 sp->ts_save_badflags = badflags;
5380 sp->ts_save_splitoff = splitoff;
5381
5382 /* Append a space to preword. */
5383 STRCAT(preword, " ");
5384 prewordlen = STRLEN(preword);
5385 splitoff = sp->ts_twordlen;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005386#ifdef FEAT_MBYTE
5387 if (has_mbyte)
5388 {
5389 int i = 0;
5390
5391 /* Case-folding may change the number of bytes:
5392 * Count nr of chars in fword[sp->ts_fidx] and
5393 * advance that many chars in su->su_badptr. */
5394 for (p = fword; p < fword + sp->ts_fidx;
5395 mb_ptr_adv(p))
5396 ++i;
5397 for (p = su->su_badptr; i > 0; mb_ptr_adv(p))
5398 --i;
5399 }
5400 else
5401#endif
5402 p = su->su_badptr + sp->ts_fidx;
5403 badflags = captype(p, su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005404
5405 sp->ts_state = STATE_SPLITUNDO;
5406 ++depth;
5407 /* Restart at top of the tree. */
5408 stack[depth].ts_arridx = 0;
5409 }
5410 }
5411 break;
5412
5413 case STATE_SPLITUNDO:
5414 /* Fixup the changes done for word split. */
5415 badflags = sp->ts_save_badflags;
5416 splitoff = sp->ts_save_splitoff;
5417 prewordlen = sp->ts_save_prewordlen;
5418
5419 /* Continue looking for NUL bytes. */
5420 sp->ts_state = STATE_START;
5421 break;
5422
5423 case STATE_ENDNUL:
5424 /* Past the NUL bytes in the node. */
5425 if (fword[sp->ts_fidx] == 0)
5426 {
5427 /* The badword ends, can't use the bytes in this node. */
5428 sp->ts_state = STATE_DEL;
5429 break;
5430 }
5431 sp->ts_state = STATE_PLAIN;
5432 /*FALLTHROUGH*/
5433
5434 case STATE_PLAIN:
5435 /*
5436 * Go over all possible bytes at this node, add each to
5437 * tword[] and use child node. "ts_curi" is the index.
5438 */
5439 arridx = sp->ts_arridx;
5440 if (sp->ts_curi > byts[arridx])
5441 {
5442 /* Done all bytes at this node, do next state. When still
5443 * at already changed bytes skip the other tricks. */
5444 if (sp->ts_fidx >= sp->ts_fidxtry)
5445 sp->ts_state = STATE_DEL;
5446 else
5447 sp->ts_state = STATE_FINAL;
5448 }
5449 else
5450 {
5451 arridx += sp->ts_curi++;
5452 c = byts[arridx];
5453
5454 /* Normal byte, go one level deeper. If it's not equal to
5455 * the byte in the bad word adjust the score. But don't
5456 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00005457 if (c == fword[sp->ts_fidx]
5458#ifdef FEAT_MBYTE
5459 || (sp->ts_tcharlen > 0
5460 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005461#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00005462 )
5463 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005464 else
5465 newscore = SCORE_SUBST;
5466 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
5467 && try_deeper(su, stack, depth, newscore))
5468 {
5469 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005470 sp = &stack[depth];
5471 ++sp->ts_fidx;
5472 tword[sp->ts_twordlen++] = c;
5473 sp->ts_arridx = idxs[arridx];
5474#ifdef FEAT_MBYTE
5475 if (newscore == SCORE_SUBST)
5476 sp->ts_isdiff = DIFF_YES;
5477 if (has_mbyte)
5478 {
5479 /* Multi-byte characters are a bit complicated to
5480 * handle: They differ when any of the bytes
5481 * differ and then their length may also differ. */
5482 if (sp->ts_tcharlen == 0)
5483 {
5484 /* First byte. */
5485 sp->ts_tcharidx = 0;
5486 sp->ts_tcharlen = MB_BYTE2LEN(c);
5487 sp->ts_fcharstart = sp->ts_fidx - 1;
5488 sp->ts_isdiff = (newscore != 0)
5489 ? DIFF_YES : DIFF_NONE;
5490 }
5491 else if (sp->ts_isdiff == DIFF_INSERT)
5492 /* When inserting trail bytes don't advance in
5493 * the bad word. */
5494 --sp->ts_fidx;
5495 if (++sp->ts_tcharidx == sp->ts_tcharlen)
5496 {
5497 /* Last byte of character. */
5498 if (sp->ts_isdiff == DIFF_YES)
5499 {
5500 /* Correct ts_fidx for the byte length of
5501 * the character (we didn't check that
5502 * before). */
5503 sp->ts_fidx = sp->ts_fcharstart
5504 + MB_BYTE2LEN(
5505 fword[sp->ts_fcharstart]);
5506
5507 /* For a similar character adjust score
5508 * from SCORE_SUBST to SCORE_SIMILAR. */
5509 if (lp->lp_slang->sl_has_map
5510 && similar_chars(lp->lp_slang,
5511 mb_ptr2char(tword
5512 + sp->ts_twordlen
5513 - sp->ts_tcharlen),
5514 mb_ptr2char(fword
5515 + sp->ts_fcharstart)))
5516 sp->ts_score -=
5517 SCORE_SUBST - SCORE_SIMILAR;
5518 }
5519
5520 /* Starting a new char, reset the length. */
5521 sp->ts_tcharlen = 0;
5522 }
5523 }
5524 else
5525#endif
5526 {
5527 /* If we found a similar char adjust the score.
5528 * We do this after calling try_deeper() because
5529 * it's slow. */
5530 if (newscore != 0
5531 && lp->lp_slang->sl_has_map
5532 && similar_chars(lp->lp_slang,
5533 c, fword[sp->ts_fidx - 1]))
5534 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
5535 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005536 }
5537 }
5538 break;
5539
5540 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00005541#ifdef FEAT_MBYTE
5542 /* When past the first byte of a multi-byte char don't try
5543 * delete/insert/swap a character. */
5544 if (has_mbyte && sp->ts_tcharlen > 0)
5545 {
5546 sp->ts_state = STATE_FINAL;
5547 break;
5548 }
5549#endif
5550 /*
5551 * Try skipping one character in the bad word (delete it).
5552 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005553 sp->ts_state = STATE_INS;
5554 sp->ts_curi = 1;
5555 if (fword[sp->ts_fidx] != NUL
5556 && try_deeper(su, stack, depth, SCORE_DEL))
5557 {
5558 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005559#ifdef FEAT_MBYTE
5560 if (has_mbyte)
5561 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
5562 else
5563#endif
5564 ++stack[depth].ts_fidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005565 break;
5566 }
5567 /*FALLTHROUGH*/
5568
5569 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00005570 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005571 * node. */
5572 n = sp->ts_arridx;
5573 if (sp->ts_curi > byts[n])
5574 {
5575 /* Done all bytes at this node, do next state. */
5576 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005577 }
5578 else
5579 {
Bram Moolenaarea424162005-06-16 21:51:00 +00005580 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005581 n += sp->ts_curi++;
5582 c = byts[n];
5583 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
5584 {
5585 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005586 sp = &stack[depth];
5587 tword[sp->ts_twordlen++] = c;
5588 sp->ts_arridx = idxs[n];
5589#ifdef FEAT_MBYTE
5590 if (has_mbyte)
5591 {
5592 fl = MB_BYTE2LEN(c);
5593 if (fl > 1)
5594 {
5595 /* There are following bytes for the same
5596 * character. We must find all bytes before
5597 * trying delete/insert/swap/etc. */
5598 sp->ts_tcharlen = fl;
5599 sp->ts_tcharidx = 1;
5600 sp->ts_isdiff = DIFF_INSERT;
5601 }
5602 }
5603#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005604 }
5605 }
5606 break;
5607
5608 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00005609 /*
5610 * Swap two bytes in the bad word: "12" -> "21".
5611 * We change "fword" here, it's changed back afterwards.
5612 */
5613 p = fword + sp->ts_fidx;
5614 c = *p;
5615 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005616 {
Bram Moolenaarea424162005-06-16 21:51:00 +00005617 /* End of word, can't swap or replace. */
5618 sp->ts_state = STATE_FINAL;
5619 break;
5620 }
5621#ifdef FEAT_MBYTE
5622 if (has_mbyte)
5623 {
5624 n = mb_ptr2len_check(p);
5625 c = mb_ptr2char(p);
5626 c2 = mb_ptr2char(p + n);
5627 }
5628 else
5629#endif
5630 c2 = p[1];
5631 if (c == c2)
5632 {
5633 /* Characters are identical, swap won't do anything. */
5634 sp->ts_state = STATE_SWAP3;
5635 break;
5636 }
5637 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
5638 {
5639 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005640 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005641#ifdef FEAT_MBYTE
5642 if (has_mbyte)
5643 {
5644 fl = mb_char2len(c2);
5645 mch_memmove(p, p + n, fl);
5646 mb_char2bytes(c, p + fl);
5647 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
5648 }
5649 else
5650#endif
5651 {
5652 p[0] = c2;
5653 p[1] = c;
5654 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
5655 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005656 }
5657 else
5658 /* If this swap doesn't work then SWAP3 won't either. */
5659 sp->ts_state = STATE_REP_INI;
5660 break;
5661
Bram Moolenaarea424162005-06-16 21:51:00 +00005662 case STATE_UNSWAP:
5663 /* Undo the STATE_SWAP swap: "21" -> "12". */
5664 p = fword + sp->ts_fidx;
5665#ifdef FEAT_MBYTE
5666 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005667 {
Bram Moolenaarea424162005-06-16 21:51:00 +00005668 n = MB_BYTE2LEN(*p);
5669 c = mb_ptr2char(p + n);
5670 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
5671 mb_char2bytes(c, p);
5672 }
5673 else
5674#endif
5675 {
5676 c = *p;
5677 *p = p[1];
5678 p[1] = c;
5679 }
5680 /*FALLTHROUGH*/
5681
5682 case STATE_SWAP3:
5683 /* Swap two bytes, skipping one: "123" -> "321". We change
5684 * "fword" here, it's changed back afterwards. */
5685 p = fword + sp->ts_fidx;
5686#ifdef FEAT_MBYTE
5687 if (has_mbyte)
5688 {
5689 n = mb_ptr2len_check(p);
5690 c = mb_ptr2char(p);
5691 fl = mb_ptr2len_check(p + n);
5692 c2 = mb_ptr2char(p + n);
5693 c3 = mb_ptr2char(p + n + fl);
5694 }
5695 else
5696#endif
5697 {
5698 c = *p;
5699 c2 = p[1];
5700 c3 = p[2];
5701 }
5702
5703 /* When characters are identical: "121" then SWAP3 result is
5704 * identical, ROT3L result is same as SWAP: "211", ROT3L
5705 * result is same as SWAP on next char: "112". Thus skip all
5706 * swapping. Also skip when c3 is NUL. */
5707 if (c == c3 || c3 == NUL)
5708 {
5709 sp->ts_state = STATE_REP_INI;
5710 break;
5711 }
5712 if (try_deeper(su, stack, depth, SCORE_SWAP3))
5713 {
5714 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005715 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005716#ifdef FEAT_MBYTE
5717 if (has_mbyte)
5718 {
5719 tl = mb_char2len(c3);
5720 mch_memmove(p, p + n + fl, tl);
5721 mb_char2bytes(c2, p + tl);
5722 mb_char2bytes(c, p + fl + tl);
5723 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
5724 }
5725 else
5726#endif
5727 {
5728 p[0] = p[2];
5729 p[2] = c;
5730 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5731 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005732 }
5733 else
5734 sp->ts_state = STATE_REP_INI;
5735 break;
5736
Bram Moolenaarea424162005-06-16 21:51:00 +00005737 case STATE_UNSWAP3:
5738 /* Undo STATE_SWAP3: "321" -> "123" */
5739 p = fword + sp->ts_fidx;
5740#ifdef FEAT_MBYTE
5741 if (has_mbyte)
5742 {
5743 n = MB_BYTE2LEN(*p);
5744 c2 = mb_ptr2char(p + n);
5745 fl = MB_BYTE2LEN(p[n]);
5746 c = mb_ptr2char(p + n + fl);
5747 tl = MB_BYTE2LEN(p[n + fl]);
5748 mch_memmove(p + fl + tl, p, n);
5749 mb_char2bytes(c, p);
5750 mb_char2bytes(c2, p + tl);
5751 }
5752 else
5753#endif
5754 {
5755 c = *p;
5756 *p = p[2];
5757 p[2] = c;
5758 }
5759 /*FALLTHROUGH*/
5760
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005761 case STATE_ROT3L:
Bram Moolenaarea424162005-06-16 21:51:00 +00005762 /* Rotate three characters left: "123" -> "231". We change
5763 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005764 if (try_deeper(su, stack, depth, SCORE_SWAP3))
5765 {
Bram Moolenaarea424162005-06-16 21:51:00 +00005766 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005767 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005768 p = fword + sp->ts_fidx;
5769#ifdef FEAT_MBYTE
5770 if (has_mbyte)
5771 {
5772 n = mb_ptr2len_check(p);
5773 c = mb_ptr2char(p);
5774 fl = mb_ptr2len_check(p + n);
5775 fl += mb_ptr2len_check(p + n + fl);
5776 mch_memmove(p, p + n, fl);
5777 mb_char2bytes(c, p + fl);
5778 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
5779 }
5780 else
5781#endif
5782 {
5783 c = *p;
5784 *p = p[1];
5785 p[1] = p[2];
5786 p[2] = c;
5787 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5788 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005789 }
5790 else
5791 sp->ts_state = STATE_REP_INI;
5792 break;
5793
Bram Moolenaarea424162005-06-16 21:51:00 +00005794 case STATE_UNROT3L:
5795 /* Undo STATE_ROT3L: "231" -> "123" */
5796 p = fword + sp->ts_fidx;
5797#ifdef FEAT_MBYTE
5798 if (has_mbyte)
5799 {
5800 n = MB_BYTE2LEN(*p);
5801 n += MB_BYTE2LEN(p[n]);
5802 c = mb_ptr2char(p + n);
5803 tl = MB_BYTE2LEN(p[n]);
5804 mch_memmove(p + tl, p, n);
5805 mb_char2bytes(c, p);
5806 }
5807 else
5808#endif
5809 {
5810 c = p[2];
5811 p[2] = p[1];
5812 p[1] = *p;
5813 *p = c;
5814 }
5815 /*FALLTHROUGH*/
5816
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005817 case STATE_ROT3R:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005818 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00005819 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005820 if (try_deeper(su, stack, depth, SCORE_SWAP3))
5821 {
Bram Moolenaarea424162005-06-16 21:51:00 +00005822 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005823 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005824 p = fword + sp->ts_fidx;
5825#ifdef FEAT_MBYTE
5826 if (has_mbyte)
5827 {
5828 n = mb_ptr2len_check(p);
5829 n += mb_ptr2len_check(p + n);
5830 c = mb_ptr2char(p + n);
5831 tl = mb_ptr2len_check(p + n);
5832 mch_memmove(p + tl, p, n);
5833 mb_char2bytes(c, p);
5834 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
5835 }
5836 else
5837#endif
5838 {
5839 c = p[2];
5840 p[2] = p[1];
5841 p[1] = *p;
5842 *p = c;
5843 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5844 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005845 }
5846 else
5847 sp->ts_state = STATE_REP_INI;
5848 break;
5849
Bram Moolenaarea424162005-06-16 21:51:00 +00005850 case STATE_UNROT3R:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005851 /* Undo STATE_ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00005852 p = fword + sp->ts_fidx;
5853#ifdef FEAT_MBYTE
5854 if (has_mbyte)
5855 {
5856 c = mb_ptr2char(p);
5857 tl = MB_BYTE2LEN(*p);
5858 n = MB_BYTE2LEN(p[tl]);
5859 n += MB_BYTE2LEN(p[tl + n]);
5860 mch_memmove(p, p + tl, n);
5861 mb_char2bytes(c, p + n);
5862 }
5863 else
5864#endif
5865 {
5866 c = *p;
5867 *p = p[1];
5868 p[1] = p[2];
5869 p[2] = c;
5870 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005871 /*FALLTHROUGH*/
5872
5873 case STATE_REP_INI:
5874 /* Check if matching with REP items from the .aff file would
5875 * work. Quickly skip if there are no REP items or the score
5876 * is going to be too high anyway. */
5877 gap = &lp->lp_slang->sl_rep;
5878 if (gap->ga_len == 0
5879 || sp->ts_score + SCORE_REP >= su->su_maxscore)
5880 {
5881 sp->ts_state = STATE_FINAL;
5882 break;
5883 }
5884
5885 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00005886 * may match. If the index is -1 there is none. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005887 sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]];
5888 if (sp->ts_curi < 0)
5889 {
5890 sp->ts_state = STATE_FINAL;
5891 break;
5892 }
5893
5894 sp->ts_state = STATE_REP;
5895 /*FALLTHROUGH*/
5896
5897 case STATE_REP:
5898 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00005899 * match replace the characters and check if the resulting
5900 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005901 p = fword + sp->ts_fidx;
5902
5903 gap = &lp->lp_slang->sl_rep;
5904 while (sp->ts_curi < gap->ga_len)
5905 {
5906 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
5907 if (*ftp->ft_from != *p)
5908 {
5909 /* past possible matching entries */
5910 sp->ts_curi = gap->ga_len;
5911 break;
5912 }
5913 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
5914 && try_deeper(su, stack, depth, SCORE_REP))
5915 {
5916 /* Need to undo this afterwards. */
5917 sp->ts_state = STATE_REP_UNDO;
5918
5919 /* Change the "from" to the "to" string. */
5920 ++depth;
5921 fl = STRLEN(ftp->ft_from);
5922 tl = STRLEN(ftp->ft_to);
5923 if (fl != tl)
5924 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
5925 mch_memmove(p, ftp->ft_to, tl);
5926 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +00005927#ifdef FEAT_MBYTE
5928 stack[depth].ts_tcharlen = 0;
5929#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005930 break;
5931 }
5932 }
5933
5934 if (sp->ts_curi >= gap->ga_len)
5935 /* No (more) matches. */
5936 sp->ts_state = STATE_FINAL;
5937
5938 break;
5939
5940 case STATE_REP_UNDO:
5941 /* Undo a REP replacement and continue with the next one. */
5942 ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data
5943 + sp->ts_curi - 1;
5944 fl = STRLEN(ftp->ft_from);
5945 tl = STRLEN(ftp->ft_to);
5946 p = fword + sp->ts_fidx;
5947 if (fl != tl)
5948 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
5949 mch_memmove(p, ftp->ft_from, fl);
5950 sp->ts_state = STATE_REP;
5951 break;
5952
5953 default:
5954 /* Did all possible states at this level, go up one level. */
5955 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005956
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005957 /* Don't check for CTRL-C too often, it takes time. */
5958 line_breakcheck();
5959 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005960 }
5961 }
5962}
5963
5964/*
5965 * Try going one level deeper in the tree.
5966 */
5967 static int
5968try_deeper(su, stack, depth, score_add)
5969 suginfo_T *su;
5970 trystate_T *stack;
5971 int depth;
5972 int score_add;
5973{
5974 int newscore;
5975
5976 /* Refuse to go deeper if the scrore is getting too big. */
5977 newscore = stack[depth].ts_score + score_add;
5978 if (newscore >= su->su_maxscore)
5979 return FALSE;
5980
Bram Moolenaarea424162005-06-16 21:51:00 +00005981 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005982 stack[depth + 1].ts_state = STATE_START;
5983 stack[depth + 1].ts_score = newscore;
5984 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005985 return TRUE;
5986}
5987
5988/*
5989 * "fword" is a good word with case folded. Find the matching keep-case
5990 * words and put it in "kword".
5991 * Theoretically there could be several keep-case words that result in the
5992 * same case-folded word, but we only find one...
5993 */
5994 static void
5995find_keepcap_word(slang, fword, kword)
5996 slang_T *slang;
5997 char_u *fword;
5998 char_u *kword;
5999{
6000 char_u uword[MAXWLEN]; /* "fword" in upper-case */
6001 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006002 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006003
6004 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006005 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006006 int round[MAXWLEN];
6007 int fwordidx[MAXWLEN];
6008 int uwordidx[MAXWLEN];
6009 int kwordlen[MAXWLEN];
6010
6011 int flen, ulen;
6012 int l;
6013 int len;
6014 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006015 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006016 char_u *p;
6017 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006018 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006019
6020 if (byts == NULL)
6021 {
6022 /* array is empty: "cannot happen" */
6023 *kword = NUL;
6024 return;
6025 }
6026
6027 /* Make an all-cap version of "fword". */
6028 allcap_copy(fword, uword);
6029
6030 /*
6031 * Each character needs to be tried both case-folded and upper-case.
6032 * All this gets very complicated if we keep in mind that changing case
6033 * may change the byte length of a multi-byte character...
6034 */
6035 depth = 0;
6036 arridx[0] = 0;
6037 round[0] = 0;
6038 fwordidx[0] = 0;
6039 uwordidx[0] = 0;
6040 kwordlen[0] = 0;
6041 while (depth >= 0)
6042 {
6043 if (fword[fwordidx[depth]] == NUL)
6044 {
6045 /* We are at the end of "fword". If the tree allows a word to end
6046 * here we have found a match. */
6047 if (byts[arridx[depth] + 1] == 0)
6048 {
6049 kword[kwordlen[depth]] = NUL;
6050 return;
6051 }
6052
6053 /* kword is getting too long, continue one level up */
6054 --depth;
6055 }
6056 else if (++round[depth] > 2)
6057 {
6058 /* tried both fold-case and upper-case character, continue one
6059 * level up */
6060 --depth;
6061 }
6062 else
6063 {
6064 /*
6065 * round[depth] == 1: Try using the folded-case character.
6066 * round[depth] == 2: Try using the upper-case character.
6067 */
6068#ifdef FEAT_MBYTE
6069 if (has_mbyte)
6070 {
6071 flen = mb_ptr2len_check(fword + fwordidx[depth]);
6072 ulen = mb_ptr2len_check(uword + uwordidx[depth]);
6073 }
6074 else
6075#endif
6076 ulen = flen = 1;
6077 if (round[depth] == 1)
6078 {
6079 p = fword + fwordidx[depth];
6080 l = flen;
6081 }
6082 else
6083 {
6084 p = uword + uwordidx[depth];
6085 l = ulen;
6086 }
6087
6088 for (tryidx = arridx[depth]; l > 0; --l)
6089 {
6090 /* Perform a binary search in the list of accepted bytes. */
6091 len = byts[tryidx++];
6092 c = *p++;
6093 lo = tryidx;
6094 hi = tryidx + len - 1;
6095 while (lo < hi)
6096 {
6097 m = (lo + hi) / 2;
6098 if (byts[m] > c)
6099 hi = m - 1;
6100 else if (byts[m] < c)
6101 lo = m + 1;
6102 else
6103 {
6104 lo = hi = m;
6105 break;
6106 }
6107 }
6108
6109 /* Stop if there is no matching byte. */
6110 if (hi < lo || byts[lo] != c)
6111 break;
6112
6113 /* Continue at the child (if there is one). */
6114 tryidx = idxs[lo];
6115 }
6116
6117 if (l == 0)
6118 {
6119 /*
6120 * Found the matching char. Copy it to "kword" and go a
6121 * level deeper.
6122 */
6123 if (round[depth] == 1)
6124 {
6125 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
6126 flen);
6127 kwordlen[depth + 1] = kwordlen[depth] + flen;
6128 }
6129 else
6130 {
6131 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
6132 ulen);
6133 kwordlen[depth + 1] = kwordlen[depth] + ulen;
6134 }
6135 fwordidx[depth + 1] = fwordidx[depth] + flen;
6136 uwordidx[depth + 1] = uwordidx[depth] + ulen;
6137
6138 ++depth;
6139 arridx[depth] = tryidx;
6140 round[depth] = 0;
6141 }
6142 }
6143 }
6144
6145 /* Didn't find it: "cannot happen". */
6146 *kword = NUL;
6147}
6148
6149/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006150 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
6151 * su->su_sga.
6152 */
6153 static void
6154score_comp_sal(su)
6155 suginfo_T *su;
6156{
6157 langp_T *lp;
6158 char_u badsound[MAXWLEN];
6159 int i;
6160 suggest_T *stp;
6161 suggest_T *sstp;
6162 char_u fword[MAXWLEN];
6163 char_u goodsound[MAXWLEN];
6164 int score;
6165
6166 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
6167 return;
6168
6169 /* Use the sound-folding of the first language that supports it. */
6170 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
6171 lp->lp_slang != NULL; ++lp)
6172 if (lp->lp_slang->sl_sal.ga_len > 0)
6173 {
6174 /* soundfold the bad word */
6175 spell_soundfold(lp->lp_slang, su->su_fbadword, badsound);
6176
6177 for (i = 0; i < su->su_ga.ga_len; ++i)
6178 {
6179 stp = &SUG(su->su_ga, i);
6180
6181 /* Case-fold the suggested word and sound-fold it. */
6182 (void)spell_casefold(stp->st_word, STRLEN(stp->st_word),
6183 fword, MAXWLEN);
6184 spell_soundfold(lp->lp_slang, fword, goodsound);
6185 score = soundalike_score(goodsound, badsound);
6186 if (score < SCORE_MAXMAX)
6187 {
6188 /* Add the suggestion. */
6189 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
6190 sstp->st_word = vim_strsave(stp->st_word);
6191 if (sstp->st_word != NULL)
6192 {
6193 sstp->st_score = score;
6194 sstp->st_altscore = 0;
6195 sstp->st_orglen = stp->st_orglen;
6196 ++su->su_sga.ga_len;
6197 }
6198 }
6199 }
6200 break;
6201 }
6202}
6203
6204/*
6205 * Combine the list of suggestions in su->su_ga and su->su_sga.
6206 * They are intwined.
6207 */
6208 static void
6209score_combine(su)
6210 suginfo_T *su;
6211{
6212 int i;
6213 int j;
6214 garray_T ga;
6215 garray_T *gap;
6216 langp_T *lp;
6217 suggest_T *stp;
6218 char_u *p;
6219 char_u badsound[MAXWLEN];
6220 char_u goodsound[MAXWLEN];
6221 char_u fword[MAXWLEN];
6222 int round;
6223
6224 /* Add the alternate score to su_ga. */
6225 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
6226 lp->lp_slang != NULL; ++lp)
6227 {
6228 if (lp->lp_slang->sl_sal.ga_len > 0)
6229 {
6230 /* soundfold the bad word */
6231 spell_soundfold(lp->lp_slang, su->su_fbadword, badsound);
6232
6233 for (i = 0; i < su->su_ga.ga_len; ++i)
6234 {
6235 stp = &SUG(su->su_ga, i);
6236
6237 /* Case-fold the word, sound-fold the word and compute the
6238 * score for the difference. */
6239 (void)spell_casefold(stp->st_word, STRLEN(stp->st_word),
6240 fword, MAXWLEN);
6241 spell_soundfold(lp->lp_slang, fword, goodsound);
6242 stp->st_altscore = soundalike_score(goodsound, badsound);
6243 if (stp->st_altscore == SCORE_MAXMAX)
6244 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
6245 else
6246 stp->st_score = (stp->st_score * 3
6247 + stp->st_altscore) / 4;
6248 stp->st_salscore = FALSE;
6249 }
6250 break;
6251 }
6252 }
6253
6254 /* Add the alternate score to su_sga. */
6255 for (i = 0; i < su->su_sga.ga_len; ++i)
6256 {
6257 stp = &SUG(su->su_sga, i);
6258 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
6259 if (stp->st_score == SCORE_MAXMAX)
6260 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
6261 else
6262 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
6263 stp->st_salscore = TRUE;
6264 }
6265
6266 /* Sort the suggestions and truncate at "maxcount" for both lists. */
6267 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
6268 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
6269
6270 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
6271 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
6272 return;
6273
6274 stp = &SUG(ga, 0);
6275 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
6276 {
6277 /* round 1: get a suggestion from su_ga
6278 * round 2: get a suggestion from su_sga */
6279 for (round = 1; round <= 2; ++round)
6280 {
6281 gap = round == 1 ? &su->su_ga : &su->su_sga;
6282 if (i < gap->ga_len)
6283 {
6284 /* Don't add a word if it's already there. */
6285 p = SUG(*gap, i).st_word;
6286 for (j = 0; j < ga.ga_len; ++j)
6287 if (STRCMP(stp[j].st_word, p) == 0)
6288 break;
6289 if (j == ga.ga_len)
6290 stp[ga.ga_len++] = SUG(*gap, i);
6291 else
6292 vim_free(p);
6293 }
6294 }
6295 }
6296
6297 ga_clear(&su->su_ga);
6298 ga_clear(&su->su_sga);
6299
6300 /* Truncate the list to the number of suggestions that will be displayed. */
6301 if (ga.ga_len > su->su_maxcount)
6302 {
6303 for (i = su->su_maxcount; i < ga.ga_len; ++i)
6304 vim_free(stp[i].st_word);
6305 ga.ga_len = su->su_maxcount;
6306 }
6307
6308 su->su_ga = ga;
6309}
6310
6311/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006312 * Find suggestions by comparing the word in a sound-a-like form.
6313 */
6314 static void
6315spell_try_soundalike(su)
6316 suginfo_T *su;
6317{
6318 char_u salword[MAXWLEN];
6319 char_u tword[MAXWLEN];
6320 char_u tfword[MAXWLEN];
6321 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006322 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006323 int curi[MAXWLEN];
6324 langp_T *lp;
6325 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006326 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006327 int depth;
6328 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006329 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006330 int round;
6331 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006332 int sound_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006333
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006334 /* Do this for all languages that support sound folding. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006335 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
6336 lp->lp_slang != NULL; ++lp)
6337 {
6338 if (lp->lp_slang->sl_sal.ga_len > 0)
6339 {
6340 /* soundfold the bad word */
6341 spell_soundfold(lp->lp_slang, su->su_fbadword, salword);
6342
6343 /*
6344 * Go through the whole tree, soundfold each word and compare.
6345 * round 1: use the case-folded tree.
6346 * round 2: use the keep-case tree.
6347 */
6348 for (round = 1; round <= 2; ++round)
6349 {
6350 if (round == 1)
6351 {
6352 byts = lp->lp_slang->sl_fbyts;
6353 idxs = lp->lp_slang->sl_fidxs;
6354 }
6355 else
6356 {
6357 byts = lp->lp_slang->sl_kbyts;
6358 idxs = lp->lp_slang->sl_kidxs;
6359 }
6360
6361 depth = 0;
6362 arridx[0] = 0;
6363 curi[0] = 1;
6364 while (depth >= 0 && !got_int)
6365 {
6366 if (curi[depth] > byts[arridx[depth]])
6367 /* Done all bytes at this node, go up one level. */
6368 --depth;
6369 else
6370 {
6371 /* Do one more byte at this node. */
6372 n = arridx[depth] + curi[depth];
6373 ++curi[depth];
6374 c = byts[n];
6375 if (c == 0)
6376 {
6377 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006378 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006379 if (round == 2 || (flags & WF_KEEPCAP) == 0)
6380 {
6381 tword[depth] = NUL;
6382 if (round == 1)
6383 spell_soundfold(lp->lp_slang,
6384 tword, tsalword);
6385 else
6386 {
6387 /* In keep-case tree need to case-fold the
6388 * word. */
6389 (void)spell_casefold(tword, depth,
6390 tfword, MAXWLEN);
6391 spell_soundfold(lp->lp_slang,
6392 tfword, tsalword);
6393 }
6394
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006395 /* Compute the edit distance between the
6396 * sound-a-like words. */
6397 sound_score = soundalike_score(salword,
6398 tsalword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006399 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006400 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006401 char_u cword[MAXWLEN];
6402 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006403 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006404
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006405 if (round == 1 && flags != 0)
6406 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006407 /* Need to fix case according to
6408 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006409 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006410 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006411 }
6412 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006413 p = tword;
6414
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006415 if (sps_flags & SPS_DOUBLE)
6416 add_suggestion(su, &su->su_sga, p,
6417 sound_score, FALSE);
6418 else
6419 {
6420 /* Compute the score. */
6421 score = spell_edit_score(
6422 su->su_badword, p);
6423 if (sps_flags & SPS_BEST)
6424 /* give a bonus for the good word
6425 * sounding the same as the bad
6426 * word */
6427 add_suggestion(su, &su->su_ga, p,
6428 RESCORE(score, sound_score),
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006429 TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006430 else
6431 add_suggestion(su, &su->su_ga, p,
6432 score + sound_score, FALSE);
6433 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006434 }
6435 }
6436
6437 /* Skip over other NUL bytes. */
6438 while (byts[n + 1] == 0)
6439 {
6440 ++n;
6441 ++curi[depth];
6442 }
6443 }
6444 else
6445 {
6446 /* Normal char, go one level deeper. */
6447 tword[depth++] = c;
6448 arridx[depth] = idxs[n];
6449 curi[depth] = 1;
6450 }
6451 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006452
6453 line_breakcheck();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006454 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006455 }
6456 }
6457 }
6458}
6459
6460/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006461 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006462 */
6463 static void
6464make_case_word(fword, cword, flags)
6465 char_u *fword;
6466 char_u *cword;
6467 int flags;
6468{
6469 if (flags & WF_ALLCAP)
6470 /* Make it all upper-case */
6471 allcap_copy(fword, cword);
6472 else if (flags & WF_ONECAP)
6473 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006474 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006475 else
6476 /* Use goodword as-is. */
6477 STRCPY(cword, fword);
6478}
6479
Bram Moolenaarea424162005-06-16 21:51:00 +00006480/*
6481 * Use map string "map" for languages "lp".
6482 */
6483 static void
6484set_map_str(lp, map)
6485 slang_T *lp;
6486 char_u *map;
6487{
6488 char_u *p;
6489 int headc = 0;
6490 int c;
6491 int i;
6492
6493 if (*map == NUL)
6494 {
6495 lp->sl_has_map = FALSE;
6496 return;
6497 }
6498 lp->sl_has_map = TRUE;
6499
6500 /* Init the array and hash table empty. */
6501 for (i = 0; i < 256; ++i)
6502 lp->sl_map_array[i] = 0;
6503#ifdef FEAT_MBYTE
6504 hash_init(&lp->sl_map_hash);
6505#endif
6506
6507 /*
6508 * The similar characters are stored separated with slashes:
6509 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
6510 * before the same slash. For characters above 255 sl_map_hash is used.
6511 */
6512 for (p = map; *p != NUL; )
6513 {
6514#ifdef FEAT_MBYTE
6515 c = mb_ptr2char_adv(&p);
6516#else
6517 c = *p++;
6518#endif
6519 if (c == '/')
6520 headc = 0;
6521 else
6522 {
6523 if (headc == 0)
6524 headc = c;
6525
6526#ifdef FEAT_MBYTE
6527 /* Characters above 255 don't fit in sl_map_array[], put them in
6528 * the hash table. Each entry is the char, a NUL the headchar and
6529 * a NUL. */
6530 if (c >= 256)
6531 {
6532 int cl = mb_char2len(c);
6533 int headcl = mb_char2len(headc);
6534 char_u *b;
6535 hash_T hash;
6536 hashitem_T *hi;
6537
6538 b = alloc((unsigned)(cl + headcl + 2));
6539 if (b == NULL)
6540 return;
6541 mb_char2bytes(c, b);
6542 b[cl] = NUL;
6543 mb_char2bytes(headc, b + cl + 1);
6544 b[cl + 1 + headcl] = NUL;
6545 hash = hash_hash(b);
6546 hi = hash_lookup(&lp->sl_map_hash, b, hash);
6547 if (HASHITEM_EMPTY(hi))
6548 hash_add_item(&lp->sl_map_hash, hi, b, hash);
6549 else
6550 {
6551 /* This should have been checked when generating the .spl
6552 * file. */
6553 EMSG(_("E999: duplicate char in MAP entry"));
6554 vim_free(b);
6555 }
6556 }
6557 else
6558#endif
6559 lp->sl_map_array[c] = headc;
6560 }
6561 }
6562}
6563
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006564/*
6565 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
6566 * lines in the .aff file.
6567 */
6568 static int
6569similar_chars(slang, c1, c2)
6570 slang_T *slang;
6571 int c1;
6572 int c2;
6573{
Bram Moolenaarea424162005-06-16 21:51:00 +00006574 int m1, m2;
6575#ifdef FEAT_MBYTE
6576 char_u buf[MB_MAXBYTES];
6577 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006578
Bram Moolenaarea424162005-06-16 21:51:00 +00006579 if (c1 >= 256)
6580 {
6581 buf[mb_char2bytes(c1, buf)] = 0;
6582 hi = hash_find(&slang->sl_map_hash, buf);
6583 if (HASHITEM_EMPTY(hi))
6584 m1 = 0;
6585 else
6586 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
6587 }
6588 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006589#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00006590 m1 = slang->sl_map_array[c1];
6591 if (m1 == 0)
6592 return FALSE;
6593
6594
6595#ifdef FEAT_MBYTE
6596 if (c2 >= 256)
6597 {
6598 buf[mb_char2bytes(c2, buf)] = 0;
6599 hi = hash_find(&slang->sl_map_hash, buf);
6600 if (HASHITEM_EMPTY(hi))
6601 m2 = 0;
6602 else
6603 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
6604 }
6605 else
6606#endif
6607 m2 = slang->sl_map_array[c2];
6608
6609 return m1 == m2;
6610}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006611
6612/*
6613 * Add a suggestion to the list of suggestions.
6614 * Do not add a duplicate suggestion or suggestions with a bad score.
6615 * When "use_score" is not zero it's used, otherwise the score is computed
6616 * with spell_edit_score().
6617 */
6618 static void
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006619add_suggestion(su, gap, goodword, score, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006620 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006621 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006622 char_u *goodword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006623 int score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006624 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006625{
6626 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006627 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006628
6629 /* Check that the word wasn't banned. */
6630 if (was_banned(su, goodword))
6631 return;
6632
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006633 if (score <= su->su_maxscore)
6634 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006635 /* Check if the word is already there. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006636 stp = &SUG(*gap, 0);
6637 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006638 if (STRCMP(stp[i].st_word, goodword) == 0)
6639 {
6640 /* Found it. Remember the lowest score. */
6641 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006642 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006643 stp[i].st_score = score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006644 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006645 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006646 break;
6647 }
6648
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006649 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006650 {
6651 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006652 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006653 stp->st_word = vim_strsave(goodword);
6654 if (stp->st_word != NULL)
6655 {
6656 stp->st_score = score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006657 stp->st_altscore = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006658 stp->st_had_bonus = had_bonus;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006659 stp->st_orglen = su->su_badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006660 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006661
6662 /* If we have too many suggestions now, sort the list and keep
6663 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006664 if (gap->ga_len > SUG_MAX_COUNT(su))
6665 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
6666 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006667 }
6668 }
6669 }
6670}
6671
6672/*
6673 * Add a word to be banned.
6674 */
6675 static void
6676add_banned(su, word)
6677 suginfo_T *su;
6678 char_u *word;
6679{
6680 char_u *s = vim_strsave(word);
6681 hash_T hash;
6682 hashitem_T *hi;
6683
6684 if (s != NULL)
6685 {
6686 hash = hash_hash(s);
6687 hi = hash_lookup(&su->su_banned, s, hash);
6688 if (HASHITEM_EMPTY(hi))
6689 hash_add_item(&su->su_banned, hi, s, hash);
6690 }
6691}
6692
6693/*
6694 * Return TRUE if a word appears in the list of banned words.
6695 */
6696 static int
6697was_banned(su, word)
6698 suginfo_T *su;
6699 char_u *word;
6700{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006701 hashitem_T *hi = hash_find(&su->su_banned, word);
6702
6703 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006704}
6705
6706/*
6707 * Free the banned words in "su".
6708 */
6709 static void
6710free_banned(su)
6711 suginfo_T *su;
6712{
6713 int todo;
6714 hashitem_T *hi;
6715
6716 todo = su->su_banned.ht_used;
6717 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
6718 {
6719 if (!HASHITEM_EMPTY(hi))
6720 {
6721 vim_free(hi->hi_key);
6722 --todo;
6723 }
6724 }
6725 hash_clear(&su->su_banned);
6726}
6727
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006728/*
6729 * Recompute the score if sound-folding is possible. This is slow,
6730 * thus only done for the final results.
6731 */
6732 static void
6733rescore_suggestions(su)
6734 suginfo_T *su;
6735{
6736 langp_T *lp;
6737 suggest_T *stp;
6738 char_u sal_badword[MAXWLEN];
6739 int score;
6740 int i;
6741
6742 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
6743 lp->lp_slang != NULL; ++lp)
6744 {
6745 if (lp->lp_slang->sl_sal.ga_len > 0)
6746 {
6747 /* soundfold the bad word */
6748 spell_soundfold(lp->lp_slang, su->su_fbadword, sal_badword);
6749
6750 for (i = 0; i < su->su_ga.ga_len; ++i)
6751 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006752 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006753 if (!stp->st_had_bonus)
6754 {
6755 score = spell_sound_score(lp->lp_slang, stp->st_word,
6756 sal_badword);
6757 stp->st_score = RESCORE(stp->st_score, score);
6758 }
6759 }
6760 break;
6761 }
6762 }
6763}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006764
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006765static int
6766#ifdef __BORLANDC__
6767_RTLENTRYF
6768#endif
6769sug_compare __ARGS((const void *s1, const void *s2));
6770
6771/*
6772 * Function given to qsort() to sort the suggestions on st_score.
6773 */
6774 static int
6775#ifdef __BORLANDC__
6776_RTLENTRYF
6777#endif
6778sug_compare(s1, s2)
6779 const void *s1;
6780 const void *s2;
6781{
6782 suggest_T *p1 = (suggest_T *)s1;
6783 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006784 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006785
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006786 if (n == 0)
6787 return p1->st_altscore - p2->st_altscore;
6788 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006789}
6790
6791/*
6792 * Cleanup the suggestions:
6793 * - Sort on score.
6794 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006795 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006796 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006797 static int
6798cleanup_suggestions(gap, maxscore, keep)
6799 garray_T *gap;
6800 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006801 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006802{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006803 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006804 int i;
6805
6806 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006807 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006808
6809 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006810 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006811 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006812 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006813 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006814 gap->ga_len = keep;
6815 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006816 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006817 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006818}
6819
6820/*
6821 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
6822 */
6823 static void
6824spell_soundfold(slang, inword, res)
6825 slang_T *slang;
6826 char_u *inword;
6827 char_u *res;
6828{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006829 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006830 char_u word[MAXWLEN];
6831#ifdef FEAT_MBYTE
6832 int l;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006833 int found_mbyte = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006834#endif
6835 char_u *s;
6836 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006837 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006838 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006839 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006840 int n, k = 0;
6841 int z0;
6842 int k0;
6843 int n0;
6844 int c;
6845 int pri;
6846 int p0 = -333;
6847 int c0;
6848
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006849 /* Remove accents, if wanted. We actually remove all non-word characters.
6850 * But keep white space. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006851 if (slang->sl_rem_accents)
6852 {
6853 t = word;
6854 for (s = inword; *s != NUL; )
6855 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006856 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006857 {
6858 *t++ = ' ';
6859 s = skipwhite(s);
6860 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006861#ifdef FEAT_MBYTE
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006862 else if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006863 {
6864 l = mb_ptr2len_check(s);
6865 if (SPELL_ISWORDP(s))
6866 {
6867 mch_memmove(t, s, l);
6868 t += l;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006869 if (l > 1)
6870 found_mbyte = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006871 }
6872 s += l;
6873 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006874#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006875 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006876 {
6877 if (SPELL_ISWORDP(s))
6878 *t++ = *s;
6879 ++s;
6880 }
6881 }
6882 *t = NUL;
6883 }
6884 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006885 {
6886#ifdef FEAT_MBYTE
6887 if (has_mbyte)
6888 for (s = inword; *s != NUL; s += l)
6889 if ((l = mb_ptr2len_check(s)) > 1)
6890 {
6891 found_mbyte = TRUE;
6892 break;
6893 }
6894#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006895 STRCPY(word, inword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006896 }
6897
6898#ifdef FEAT_MBYTE
6899 /* If there are multi-byte characters in the word return it as-is, because
6900 * the following won't work. */
6901 if (found_mbyte)
6902 {
6903 STRCPY(res, word);
6904 return;
6905 }
6906#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006907
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006908 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006909
6910 /*
6911 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006912 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006913 * TODO: support for multi-byte chars.
6914 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006915 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006916 while ((c = word[i]) != NUL)
6917 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006918 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006919 n = slang->sl_sal_first[c];
6920 z0 = 0;
6921
6922 if (n >= 0)
6923 {
6924 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006925 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006926 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006927 /* Quickly skip entries that don't match the word. Most
6928 * entries are less then three chars, optimize for that. */
6929 k = smp[n].sm_leadlen;
6930 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006931 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006932 if (word[i + 1] != s[1])
6933 continue;
6934 if (k > 2)
6935 {
6936 for (j = 2; j < k; ++j)
6937 if (word[i + j] != s[j])
6938 break;
6939 if (j < k)
6940 continue;
6941 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006942 }
6943
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006944 if ((pf = smp[n].sm_oneoff) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006945 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006946 /* Check for match with one of the chars in "sm_oneoff". */
6947 while (*pf != NUL && *pf != word[i + k])
6948 ++pf;
6949 if (*pf == NUL)
6950 continue;
6951 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006952 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006953 s = smp[n].sm_rules;
6954 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006955
6956 p0 = *s;
6957 k0 = k;
6958 while (*s == '-' && k > 1)
6959 {
6960 k--;
6961 s++;
6962 }
6963 if (*s == '<')
6964 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006965 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006966 {
6967 /* determine priority */
6968 pri = *s - '0';
6969 s++;
6970 }
6971 if (*s == '^' && *(s + 1) == '^')
6972 s++;
6973
6974 if (*s == NUL
6975 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006976 && (i == 0 || !(word[i - 1] == ' '
6977 || SPELL_ISWORDP(word + i - 1)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006978 && (*(s + 1) != '$'
6979 || (!SPELL_ISWORDP(word + i + k0))))
6980 || (*s == '$' && i > 0
6981 && SPELL_ISWORDP(word + i - 1)
6982 && (!SPELL_ISWORDP(word + i + k0))))
6983 {
6984 /* search for followup rules, if: */
6985 /* followup and k > 1 and NO '-' in searchstring */
6986 c0 = word[i + k - 1];
6987 n0 = slang->sl_sal_first[c0];
6988
6989 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006990 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006991 {
6992 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006993 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006994 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006995 /* Quickly skip entries that don't match the word.
6996 * */
6997 k0 = smp[n0].sm_leadlen;
6998 if (k0 > 1)
6999 {
7000 if (word[i + k] != s[1])
7001 continue;
7002 if (k0 > 2)
7003 {
7004 pf = word + i + k + 1;
7005 for (j = 2; j < k0; ++j)
7006 if (*pf++ != s[j])
7007 break;
7008 if (j < k0)
7009 continue;
7010 }
7011 }
7012 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007013
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007014 if ((pf = smp[n0].sm_oneoff) != NULL)
7015 {
7016 /* Check for match with one of the chars in
7017 * "sm_oneoff". */
7018 while (*pf != NUL && *pf != word[i + k0])
7019 ++pf;
7020 if (*pf == NUL)
7021 continue;
7022 ++k0;
7023 }
7024
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007025 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007026 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007027 while (*s == '-')
7028 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007029 /* "k0" gets NOT reduced because
7030 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007031 s++;
7032 }
7033 if (*s == '<')
7034 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007035 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007036 {
7037 p0 = *s - '0';
7038 s++;
7039 }
7040
7041 if (*s == NUL
7042 /* *s == '^' cuts */
7043 || (*s == '$'
7044 && !SPELL_ISWORDP(word + i + k0)))
7045 {
7046 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007047 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007048 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007049
7050 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007051 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007052 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007053 /* rule fits; stop search */
7054 break;
7055 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007056 }
7057
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007058 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007059 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007060 }
7061
7062 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007063 s = smp[n].sm_to;
7064 pf = smp[n].sm_rules;
7065 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007066 if (p0 == 1 && z == 0)
7067 {
7068 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007069 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
7070 || res[reslen - 1] == *s))
7071 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007072 z0 = 1;
7073 z = 1;
7074 k0 = 0;
7075 while (*s != NUL && word[i+k0] != NUL)
7076 {
7077 word[i + k0] = *s;
7078 k0++;
7079 s++;
7080 }
7081 if (k > k0)
7082 mch_memmove(word + i + k0, word + i + k,
7083 STRLEN(word + i + k) + 1);
7084
7085 /* new "actual letter" */
7086 c = word[i];
7087 }
7088 else
7089 {
7090 /* no '<' rule used */
7091 i += k - 1;
7092 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007093 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007094 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007095 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007096 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007097 res[reslen] = *s;
7098 reslen++;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007099 }
7100 s++;
7101 }
7102 /* new "actual letter" */
7103 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007104 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007105 {
7106 if (c != NUL)
7107 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007108 res[reslen] = c;
7109 reslen++;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007110 }
7111 mch_memmove(word, word + i + 1,
7112 STRLEN(word + i + 1) + 1);
7113 i = 0;
7114 z0 = 1;
7115 }
7116 }
7117 break;
7118 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007119 }
7120 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007121 else if (vim_iswhite(c))
7122 {
7123 c = ' ';
7124 k = 1;
7125 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007126
7127 if (z0 == 0)
7128 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007129 if (k && !p0 && reslen < MAXWLEN && c != NUL
7130 && (!slang->sl_collapse || reslen == 0
7131 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007132 {
7133 /* condense only double letters */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007134 res[reslen] = c;
7135 reslen++;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007136 }
7137
7138 i++;
7139 z = 0;
7140 k = 0;
7141 }
7142 }
7143
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007144 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007145}
7146
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007147/*
7148 * Return the score for how much words sound different.
7149 */
7150 static int
7151spell_sound_score(slang, goodword, badsound)
7152 slang_T *slang;
7153 char_u *goodword; /* good word */
7154 char_u *badsound; /* sound-folded bad word */
7155{
7156 char_u fword[MAXWLEN];
7157 char_u goodsound[MAXWLEN];
7158 int score;
7159
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007160 /* Case-fold the goodword, needed for sound folding. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007161 (void)spell_casefold(goodword, STRLEN(goodword), fword, MAXWLEN);
7162
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007163 /* sound-fold the goodword */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007164 spell_soundfold(slang, fword, goodsound);
7165
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007166 /* Compute the edit distance-score of the sounds. This is slow but we
7167 * only do it for a small number of words. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007168 score = spell_edit_score(badsound, goodsound);
7169
7170 /* Correction: adding/inserting "*" at the start (word starts with vowel)
7171 * shouldn't be counted so much, vowels halfway the word aren't counted at
7172 * all. */
7173 if (*badsound != *goodsound && (*badsound == '*' || *goodsound == '*'))
7174 score -= SCORE_DEL / 2;
7175
7176 return score;
7177}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007178
7179/*
7180 * Compute a score for two sound-a-like words.
7181 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
7182 * Instead of a generic loop we write out the code. That keeps it fast by
7183 * avoiding checks that will not be possible.
7184 */
7185 static int
7186soundalike_score(goodsound, badsound)
7187 char_u *goodsound; /* sound-folded good word */
7188 char_u *badsound; /* sound-folded bad word */
7189{
7190 int goodlen = STRLEN(goodsound);
7191 int badlen = STRLEN(badsound);
7192 int n;
7193 char_u *pl, *ps;
7194 char_u *pl2, *ps2;
7195
7196 /* Return quickly if the lenghts are too different to be fixed by two
7197 * changes. */
7198 n = goodlen - badlen;
7199 if (n < -2 || n > 2)
7200 return SCORE_MAXMAX;
7201
7202 if (n > 0)
7203 {
7204 pl = goodsound; /* longest */
7205 ps = badsound;
7206 }
7207 else
7208 {
7209 pl = badsound; /* longest */
7210 ps = goodsound;
7211 }
7212
7213 /* Skip over the identical part. */
7214 while (*pl == *ps && *pl != NUL)
7215 {
7216 ++pl;
7217 ++ps;
7218 }
7219
7220 switch (n)
7221 {
7222 case -2:
7223 case 2:
7224 /*
7225 * Must delete two characters from "pl".
7226 */
7227 ++pl; /* first delete */
7228 while (*pl == *ps)
7229 {
7230 ++pl;
7231 ++ps;
7232 }
7233 /* strings must be equal after second delete */
7234 if (STRCMP(pl + 1, ps) == 0)
7235 return SCORE_DEL * 2;
7236
7237 /* Failed to compare. */
7238 break;
7239
7240 case -1:
7241 case 1:
7242 /*
7243 * Minimal one delete from "pl" required.
7244 */
7245
7246 /* 1: delete */
7247 pl2 = pl + 1;
7248 ps2 = ps;
7249 while (*pl2 == *ps2)
7250 {
7251 if (*pl2 == NUL) /* reached the end */
7252 return SCORE_DEL;
7253 ++pl2;
7254 ++ps2;
7255 }
7256
7257 /* 2: delete then swap, then rest must be equal */
7258 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7259 && STRCMP(pl2 + 2, ps2 + 2) == 0)
7260 return SCORE_DEL + SCORE_SWAP;
7261
7262 /* 3: delete then substitute, then the rest must be equal */
7263 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
7264 return SCORE_DEL + SCORE_SUBST;
7265
7266 /* 4: first swap then delete */
7267 if (pl[0] == ps[1] && pl[1] == ps[0])
7268 {
7269 pl2 = pl + 2; /* swap, skip two chars */
7270 ps2 = ps + 2;
7271 while (*pl2 == *ps2)
7272 {
7273 ++pl2;
7274 ++ps2;
7275 }
7276 /* delete a char and then strings must be equal */
7277 if (STRCMP(pl2 + 1, ps2) == 0)
7278 return SCORE_SWAP + SCORE_DEL;
7279 }
7280
7281 /* 5: first substitute then delete */
7282 pl2 = pl + 1; /* substitute, skip one char */
7283 ps2 = ps + 1;
7284 while (*pl2 == *ps2)
7285 {
7286 ++pl2;
7287 ++ps2;
7288 }
7289 /* delete a char and then strings must be equal */
7290 if (STRCMP(pl2 + 1, ps2) == 0)
7291 return SCORE_SUBST + SCORE_DEL;
7292
7293 /* Failed to compare. */
7294 break;
7295
7296 case 0:
7297 /*
7298 * Lenghts are equal, thus changes must result in same length: An
7299 * insert is only possible in combination with a delete.
7300 * 1: check if for identical strings
7301 */
7302 if (*pl == NUL)
7303 return 0;
7304
7305 /* 2: swap */
7306 if (pl[0] == ps[1] && pl[1] == ps[0])
7307 {
7308 pl2 = pl + 2; /* swap, skip two chars */
7309 ps2 = ps + 2;
7310 while (*pl2 == *ps2)
7311 {
7312 if (*pl2 == NUL) /* reached the end */
7313 return SCORE_SWAP;
7314 ++pl2;
7315 ++ps2;
7316 }
7317 /* 3: swap and swap again */
7318 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7319 && STRCMP(pl2 + 2, ps2 + 2) == 0)
7320 return SCORE_SWAP + SCORE_SWAP;
7321
7322 /* 4: swap and substitute */
7323 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
7324 return SCORE_SWAP + SCORE_SUBST;
7325 }
7326
7327 /* 5: substitute */
7328 pl2 = pl + 1;
7329 ps2 = ps + 1;
7330 while (*pl2 == *ps2)
7331 {
7332 if (*pl2 == NUL) /* reached the end */
7333 return SCORE_SUBST;
7334 ++pl2;
7335 ++ps2;
7336 }
7337
7338 /* 6: substitute and swap */
7339 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7340 && STRCMP(pl2 + 2, ps2 + 2) == 0)
7341 return SCORE_SUBST + SCORE_SWAP;
7342
7343 /* 7: substitute and substitute */
7344 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
7345 return SCORE_SUBST + SCORE_SUBST;
7346
7347 /* 8: insert then delete */
7348 pl2 = pl;
7349 ps2 = ps + 1;
7350 while (*pl2 == *ps2)
7351 {
7352 ++pl2;
7353 ++ps2;
7354 }
7355 if (STRCMP(pl2 + 1, ps2) == 0)
7356 return SCORE_INS + SCORE_DEL;
7357
7358 /* 9: delete then insert */
7359 pl2 = pl + 1;
7360 ps2 = ps;
7361 while (*pl2 == *ps2)
7362 {
7363 ++pl2;
7364 ++ps2;
7365 }
7366 if (STRCMP(pl2, ps2 + 1) == 0)
7367 return SCORE_INS + SCORE_DEL;
7368
7369 /* Failed to compare. */
7370 break;
7371 }
7372
7373 return SCORE_MAXMAX;
7374}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007375
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007376/*
7377 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007378 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007379 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007380 * The algorithm comes from Aspell editdist.cpp, edit_distance().
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007381 * It has been converted from C++ to C and modified to support multi-byte
7382 * characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007383 */
7384 static int
7385spell_edit_score(badword, goodword)
7386 char_u *badword;
7387 char_u *goodword;
7388{
7389 int *cnt;
7390 int badlen, goodlen;
7391 int j, i;
7392 int t;
7393 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007394 int pbc, pgc;
7395#ifdef FEAT_MBYTE
7396 char_u *p;
7397 int wbadword[MAXWLEN];
7398 int wgoodword[MAXWLEN];
7399
7400 if (has_mbyte)
7401 {
7402 /* Get the characters from the multi-byte strings and put them in an
7403 * int array for easy access. */
7404 for (p = badword, badlen = 0; *p != NUL; )
7405 wbadword[badlen++] = mb_ptr2char_adv(&p);
7406 ++badlen;
7407 for (p = goodword, goodlen = 0; *p != NUL; )
7408 wgoodword[goodlen++] = mb_ptr2char_adv(&p);
7409 ++goodlen;
7410 }
7411 else
7412#endif
7413 {
7414 badlen = STRLEN(badword) + 1;
7415 goodlen = STRLEN(goodword) + 1;
7416 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007417
7418 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
7419#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007420 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
7421 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007422 if (cnt == NULL)
7423 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007424
7425 CNT(0, 0) = 0;
7426 for (j = 1; j <= goodlen; ++j)
7427 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
7428
7429 for (i = 1; i <= badlen; ++i)
7430 {
7431 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
7432 for (j = 1; j <= goodlen; ++j)
7433 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007434#ifdef FEAT_MBYTE
7435 if (has_mbyte)
7436 {
7437 bc = wbadword[i - 1];
7438 gc = wgoodword[j - 1];
7439 }
7440 else
7441#endif
7442 {
7443 bc = badword[i - 1];
7444 gc = goodword[j - 1];
7445 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007446 if (bc == gc)
7447 CNT(i, j) = CNT(i - 1, j - 1);
7448 else
7449 {
7450 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007451 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007452 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
7453 else
7454 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
7455
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007456 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007457 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007458#ifdef FEAT_MBYTE
7459 if (has_mbyte)
7460 {
7461 pbc = wbadword[i - 2];
7462 pgc = wgoodword[j - 2];
7463 }
7464 else
7465#endif
7466 {
7467 pbc = badword[i - 2];
7468 pgc = goodword[j - 2];
7469 }
7470 if (bc == pgc && pbc == gc)
7471 {
7472 t = SCORE_SWAP + CNT(i - 2, j - 2);
7473 if (t < CNT(i, j))
7474 CNT(i, j) = t;
7475 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007476 }
7477 t = SCORE_DEL + CNT(i - 1, j);
7478 if (t < CNT(i, j))
7479 CNT(i, j) = t;
7480 t = SCORE_INS + CNT(i, j - 1);
7481 if (t < CNT(i, j))
7482 CNT(i, j) = t;
7483 }
7484 }
7485 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007486
7487 i = CNT(badlen - 1, goodlen - 1);
7488 vim_free(cnt);
7489 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007490}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007491
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007492#endif /* FEAT_SYN_HL */