blob: c4efed27592e4029e93874a80cc426b493bd5219 [file] [log] [blame]
Bram Moolenaare19defe2005-03-21 08:23:33 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * spell.c: code for spell checking
Bram Moolenaarfc735152005-03-22 22:54:12 +000012 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000013 * The spell checking mechanism uses a tree (aka trie). Each node in the tree
14 * has a list of bytes that can appear (siblings). For each byte there is a
15 * pointer to the node with the byte that follows in the word (child).
Bram Moolenaar9f30f502005-06-14 22:01:04 +000016 *
17 * A NUL byte is used where the word may end. The bytes are sorted, so that
18 * binary searching can be used and the NUL bytes are at the start. The
19 * number of possible bytes is stored before the list of bytes.
20 *
21 * The tree uses two arrays: "byts" stores the characters, "idxs" stores
22 * either the next index or flags. The tree starts at index 0. For example,
23 * to lookup "vi" this sequence is followed:
24 * i = 0
25 * len = byts[i]
26 * n = where "v" appears in byts[i + 1] to byts[i + len]
27 * i = idxs[n]
28 * len = byts[i]
29 * n = where "i" appears in byts[i + 1] to byts[i + len]
30 * i = idxs[n]
31 * len = byts[i]
32 * find that byts[i + 1] is 0, idxs[i + 1] has flags for "vi".
Bram Moolenaar51485f02005-06-04 21:55:20 +000033 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000034 * There are two word trees: one with case-folded words and one with words in
Bram Moolenaar51485f02005-06-04 21:55:20 +000035 * original case. The second one is only used for keep-case words and is
36 * usually small.
37 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000038 * There is one additional tree for when prefixes are not applied when
39 * generating the .spl file. This tree stores all the possible prefixes, as
40 * if they were words. At each word (prefix) end the prefix nr is stored, the
41 * following word must support this prefix nr. And the condition nr is
42 * stored, used to lookup the condition that the word must match with.
43 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000044 * Thanks to Olaf Seibert for providing an example implementation of this tree
45 * and the compression mechanism.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000046 *
47 * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000048 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000049 * Why doesn't Vim use aspell/ispell/myspell/etc.?
50 * See ":help develop-spell".
51 */
52
Bram Moolenaar51485f02005-06-04 21:55:20 +000053/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000054 * Use this to adjust the score after finding suggestions, based on the
55 * suggested word sounding like the bad word. This is much faster than doing
56 * it for every possible suggestion.
57 * Disadvantage: When "the" is typed as "hte" it sounds different and goes
58 * down in the list.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000059 * Used when 'spellsuggest' is set to "best".
60 */
61#define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4)
62
63/*
64 * The double scoring mechanism is based on the principle that there are two
65 * kinds of spelling mistakes:
66 * 1. You know how to spell the word, but mistype something. This results in
67 * a small editing distance (character swapped/omitted/inserted) and
68 * possibly a word that sounds completely different.
69 * 2. You don't know how to spell the word and type something that sounds
70 * right. The edit distance can be big but the word is similar after
71 * sound-folding.
72 * Since scores for these two mistakes will be very different we use a list
73 * for each.
74 * The sound-folding is slow, only do double scoring when 'spellsuggest' is
75 * "double".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000076 */
77
78/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +000079 * Vim spell file format: <HEADER>
80 * <SUGGEST>
81 * <LWORDTREE>
82 * <KWORDTREE>
83 * <PREFIXTREE>
Bram Moolenaar51485f02005-06-04 21:55:20 +000084 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000085 * <HEADER>: <fileID>
86 * <regioncnt> <regionname> ...
87 * <charflagslen> <charflags>
88 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000089 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +000090 * <prefcondcnt> <prefcond> ...
Bram Moolenaar51485f02005-06-04 21:55:20 +000091 *
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000092 * <fileID> 10 bytes "VIMspell08"
Bram Moolenaar51485f02005-06-04 21:55:20 +000093 * <regioncnt> 1 byte number of regions following (8 supported)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000094 * <regionname> 2 bytes Region name: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +000095 * First <regionname> is region 1.
96 *
97 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
98 * <charflags> N bytes List of flags (first one is for character 128):
Bram Moolenaar9f30f502005-06-14 22:01:04 +000099 * 0x01 word character CF_WORD
100 * 0x02 upper-case character CF_UPPER
Bram Moolenaar51485f02005-06-04 21:55:20 +0000101 * <fcharslen> 2 bytes Number of bytes in <fchars>.
102 * <fchars> N bytes Folded characters, first one is for character 128.
103 *
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000104 * <midwordlen> 2 bytes Number of bytes in <midword>.
105 * <midword> N bytes Characters that are word characters only when used
106 * in the middle of a word.
107 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000108 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
109 *
110 * <prefcond> : <condlen> <condstr>
111 *
112 * <condlen> 1 byte Length of <condstr>.
113 *
114 * <condstr> N bytes Condition for the prefix.
115 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000116 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000117 * <SUGGEST> : <repcount> <rep> ...
118 * <salflags> <salcount> <sal> ...
119 * <maplen> <mapstr>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000120 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000121 * <repcount> 2 bytes number of <rep> items, MSB first.
122 *
123 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
124 *
125 * <repfromlen> 1 byte length of <repfrom>
126 *
127 * <repfrom> N bytes "from" part of replacement
128 *
129 * <reptolen> 1 byte length of <repto>
130 *
131 * <repto> N bytes "to" part of replacement
132 *
133 * <salflags> 1 byte flags for soundsalike conversion:
134 * SAL_F0LLOWUP
135 * SAL_COLLAPSE
136 * SAL_REM_ACCENTS
137 *
138 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
139 *
140 * <salfromlen> 1 byte length of <salfrom>
141 *
142 * <salfrom> N bytes "from" part of soundsalike
143 *
144 * <saltolen> 1 byte length of <salto>
145 *
146 * <salto> N bytes "to" part of soundsalike
147 *
148 * <maplen> 2 bytes length of <mapstr>, MSB first
149 *
150 * <mapstr> N bytes String with sequences of similar characters,
151 * separated by slashes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000152 *
153 *
154 * <LWORDTREE>: <wordtree>
155 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000156 * <KWORDTREE>: <wordtree>
157 *
158 * <PREFIXTREE>: <wordtree>
159 *
160 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000161 * <wordtree>: <nodecount> <nodedata> ...
162 *
163 * <nodecount> 4 bytes Number of nodes following. MSB first.
164 *
165 * <nodedata>: <siblingcount> <sibling> ...
166 *
167 * <siblingcount> 1 byte Number of siblings in this node. The siblings
168 * follow in sorted order.
169 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000170 * <sibling>: <byte> [ <nodeidx> <xbyte>
171 * | <flags> [<region>] [<prefixID>]
172 * | <prefixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000173 *
174 * <byte> 1 byte Byte value of the sibling. Special cases:
175 * BY_NOFLAGS: End of word without flags and for all
176 * regions.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000177 * For PREFIXTREE <prefixID> and
178 * <prefcondnr> follow.
179 * BY_FLAGS: End of word, <flags> follow.
180 * For PREFIXTREE <prefixID> and
181 * <prefcondnr> follow for rare prefix.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000182 * BY_INDEX: Child of sibling is shared, <nodeidx>
183 * and <xbyte> follow.
184 *
185 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
186 *
187 * <xbyte> 1 byte byte value of the sibling.
188 *
189 * <flags> 1 byte bitmask of:
190 * WF_ALLCAP word must have only capitals
191 * WF_ONECAP first char of word must be capital
192 * WF_RARE rare word
193 * WF_REGION <region> follows
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000194 * WF_PFX <prefixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000195 *
196 * <region> 1 byte Bitmask for regions in which word is valid. When
197 * omitted it's valid in all regions.
198 * Lowest bit is for region 1.
199 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000200 * <prefixID> 1 byte ID of prefix that can be used with this word. For
201 * PREFIXTREE used for the required prefix ID.
202 *
203 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
204 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000205 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000206 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000207 */
208
Bram Moolenaare19defe2005-03-21 08:23:33 +0000209#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
210# include <io.h> /* for lseek(), must be before vim.h */
211#endif
212
213#include "vim.h"
214
215#if defined(FEAT_SYN_HL) || defined(PROTO)
216
217#ifdef HAVE_FCNTL_H
218# include <fcntl.h>
219#endif
220
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000221#define MAXWLEN 250 /* Assume max. word len is this many bytes.
222 Some places assume a word length fits in a
223 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000224
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000225/* Type used for indexes in the word tree need to be at least 3 bytes. If int
226 * is 8 bytes we could use something smaller, but what? */
227#if SIZEOF_INT > 2
228typedef int idx_T;
229#else
230typedef long idx_T;
231#endif
232
233/* Flags used for a word. Only the lowest byte can be used, the region byte
234 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000235#define WF_REGION 0x01 /* region byte follows */
236#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
237#define WF_ALLCAP 0x04 /* word must be all capitals */
238#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000239#define WF_BANNED 0x10 /* bad word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000240#define WF_PFX 0x20 /* prefix ID list follows */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000241#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000242
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000243#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000244
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000245#define WF_RAREPFX 0x1000000 /* in sl_pidxs: flag for rare postponed
246 prefix; must be above prefixID (one byte)
247 and prefcondnr (two bytes) */
248
Bram Moolenaar51485f02005-06-04 21:55:20 +0000249#define BY_NOFLAGS 0 /* end of word without flags or region */
250#define BY_FLAGS 1 /* end of word, flag byte follows */
251#define BY_INDEX 2 /* child is shared, index follows */
252#define BY_SPECIAL BY_INDEX /* hightest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000253
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000254/* Info from "REP" and "SAL" entries in ".aff" file used in si_rep, sl_rep,
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000255 * and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000256 * One replacement: from "ft_from" to "ft_to". */
257typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000258{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000259 char_u *ft_from;
260 char_u *ft_to;
261} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000262
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000263/* Info from "SAL" entries in ".aff" file used in sl_sal.
264 * The info is split for quick processing by spell_soundfold().
265 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
266typedef struct salitem_S
267{
268 char_u *sm_lead; /* leading letters */
269 int sm_leadlen; /* length of "sm_lead" */
270 char_u *sm_oneoff; /* letters from () or NULL */
271 char_u *sm_rules; /* rules like ^, $, priority */
272 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000273#ifdef FEAT_MBYTE
274 int *sm_lead_w; /* wide character copy of "sm_lead" */
275 int *sm_oneoff_w; /* wide character copy of "sm_oneoff" */
276 int *sm_to_w; /* wide character copy of "sm_to" */
277#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000278} salitem_T;
279
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000280/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000281 * Structure used to store words and other info for one language, loaded from
282 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000283 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
284 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
285 *
286 * The "byts" array stores the possible bytes in each tree node, preceded by
287 * the number of possible bytes, sorted on byte value:
288 * <len> <byte1> <byte2> ...
289 * The "idxs" array stores the index of the child node corresponding to the
290 * byte in "byts".
291 * Exception: when the byte is zero, the word may end here and "idxs" holds
292 * the flags and region for the word. There may be several zeros in sequence
293 * for alternative flag/region combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000294 */
295typedef struct slang_S slang_T;
296struct slang_S
297{
298 slang_T *sl_next; /* next language */
299 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000300 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000301 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000302
Bram Moolenaar51485f02005-06-04 21:55:20 +0000303 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000304 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000305 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000306 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000307 char_u *sl_pbyts; /* prefix tree word bytes */
308 idx_T *sl_pidxs; /* prefix tree word indexes */
309
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000310 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000311
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000312 int sl_prefixcnt; /* number of items in "sl_prefprog" */
313 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
314
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000315 garray_T sl_rep; /* list of fromto_T entries from REP lines */
316 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
317 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000318 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000319 short sl_sal_first[256]; /* indexes where byte first appears, -1 if
320 there is none */
321 int sl_followup; /* SAL followup */
322 int sl_collapse; /* SAL collapse_result */
323 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaarea424162005-06-16 21:51:00 +0000324 int sl_has_map; /* TRUE if there is a MAP line */
325#ifdef FEAT_MBYTE
326 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
327 int sl_map_array[256]; /* MAP for first 256 chars */
328#else
329 char_u sl_map_array[256]; /* MAP for first 256 chars */
330#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000331};
332
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000333/* First language that is loaded, start of the linked list of loaded
334 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000335static slang_T *first_lang = NULL;
336
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000337/* Flags used in .spl file for soundsalike flags. */
338#define SAL_F0LLOWUP 1
339#define SAL_COLLAPSE 2
340#define SAL_REM_ACCENTS 4
341
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000342/*
343 * Structure used in "b_langp", filled from 'spelllang'.
344 */
345typedef struct langp_S
346{
347 slang_T *lp_slang; /* info for this language (NULL for last one) */
348 int lp_region; /* bitmask for region or REGION_ALL */
349} langp_T;
350
351#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
352
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000353#define REGION_ALL 0xff /* word valid in all regions */
354
355/* Result values. Lower number is accepted over higher one. */
356#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000357#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000358#define SP_RARE 1
359#define SP_LOCAL 2
360#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000361
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000362#define VIMSPELLMAGIC "VIMspell08" /* string at start of Vim spell file */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000363#define VIMSPELLMAGICL 10
364
365/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000366 * Information used when looking for suggestions.
367 */
368typedef struct suginfo_S
369{
370 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000371 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000372 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000373 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000374 char_u *su_badptr; /* start of bad word in line */
375 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000376 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000377 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
378 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
379 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000380} suginfo_T;
381
382/* One word suggestion. Used in "si_ga". */
383typedef struct suggest_S
384{
385 char_u *st_word; /* suggested word, allocated string */
386 int st_orglen; /* length of replaced text */
387 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000388 int st_altscore; /* used when st_score compares equal */
389 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000390 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000391} suggest_T;
392
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000393#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000394
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000395/* Number of suggestions kept when cleaning up. When rescore_suggestions() is
396 * called the score may change, thus we need to keep more than what is
397 * displayed. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000398#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 50 ? 50 : (su)->su_maxcount)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000399
400/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
401 * of suggestions that are not going to be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000402#define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000403
404/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000405#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000406#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000407#define SCORE_REGION 70 /* word is for different region */
408#define SCORE_RARE 180 /* rare word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000409#define SCORE_SWAP 90 /* swap two characters */
410#define SCORE_SWAP3 110 /* swap two characters in three */
411#define SCORE_REP 87 /* REP replacement */
412#define SCORE_SUBST 93 /* substitute a character */
413#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000414#define SCORE_DEL 94 /* delete a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000415#define SCORE_DELDUP 64 /* delete a duplicated character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000416#define SCORE_INS 96 /* insert a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000417#define SCORE_INSDUP 66 /* insert a duplicate character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000418#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000419
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000420#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000421#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
422 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000423
424#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000425#define SCORE_MAXMAX 999999 /* accept any score */
426
427/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000428 * Structure to store info for word matching.
429 */
430typedef struct matchinf_S
431{
432 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000433
434 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000435 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000436 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000437 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000438 char_u *mi_cend; /* char after what was used for
439 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000440
441 /* case-folded text */
442 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000443 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000444
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000445 /* for when checking word after a prefix */
446 int mi_prefarridx; /* index in sl_pidxs with list of
447 prefixID/condition */
448 int mi_prefcnt; /* number of entries at mi_prefarridx */
449 int mi_prefixlen; /* byte length of prefix */
450
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000451 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000452 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000453 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000454} matchinf_T;
455
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000456/*
457 * The tables used for recognizing word characters according to spelling.
458 * These are only used for the first 256 characters of 'encoding'.
459 */
460typedef struct spelltab_S
461{
462 char_u st_isw[256]; /* flags: is word char */
463 char_u st_isu[256]; /* flags: is uppercase char */
464 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000465 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000466} spelltab_T;
467
468static spelltab_T spelltab;
469static int did_set_spelltab;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000470static char_u spell_ismw[256]; /* flags: is midword char */
471#ifdef FEAT_MBYTE
472static char_u *spell_ismw_mb = NULL; /* multi-byte midword chars */
473#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000474
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000475#define CF_WORD 0x01
476#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000477
478static void clear_spell_chartab __ARGS((spelltab_T *sp));
479static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaarea408852005-06-25 22:49:46 +0000480static int spell_iswordp __ARGS((char_u *p));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000481static void write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000482
483/*
Bram Moolenaarea408852005-06-25 22:49:46 +0000484 * Return TRUE if "p" points to a word character. Like spell_iswordp() but
485 * without the special handling of a single quote.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000486 * Checking for a word character is done very often, avoid the function call
487 * overhead.
488 */
489#ifdef FEAT_MBYTE
490# define SPELL_ISWORDP(p) ((has_mbyte && MB_BYTE2LEN(*(p)) > 1) \
491 ? (mb_get_class(p) >= 2) : spelltab.st_isw[*(p)])
492#else
493# define SPELL_ISWORDP(p) (spelltab.st_isw[*(p)])
494#endif
495
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000496/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000497 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000498 */
499typedef enum
500{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000501 STATE_START = 0, /* At start of node check for NUL bytes (goodword
502 * ends); if badword ends there is a match, otherwise
503 * try splitting word. */
504 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000505 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
506 STATE_PLAIN, /* Use each byte of the node. */
507 STATE_DEL, /* Delete a byte from the bad word. */
508 STATE_INS, /* Insert a byte in the bad word. */
509 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000510 STATE_UNSWAP, /* Undo swap two characters. */
511 STATE_SWAP3, /* Swap two characters over three. */
512 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000513 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000514 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000515 STATE_REP_INI, /* Prepare for using REP items. */
516 STATE_REP, /* Use matching REP items from the .aff file. */
517 STATE_REP_UNDO, /* Undo a REP item replacement. */
518 STATE_FINAL /* End of this node. */
519} state_T;
520
521/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000522 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000523 */
524typedef struct trystate_S
525{
Bram Moolenaarea424162005-06-16 21:51:00 +0000526 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000527 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000528 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000529 short ts_curi; /* index in list of child nodes */
530 char_u ts_fidx; /* index in fword[], case-folded bad word */
531 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
532 char_u ts_twordlen; /* valid length of tword[] */
533#ifdef FEAT_MBYTE
534 char_u ts_tcharlen; /* number of bytes in tword character */
535 char_u ts_tcharidx; /* current byte index in tword character */
536 char_u ts_isdiff; /* DIFF_ values */
537 char_u ts_fcharstart; /* index in fword where badword char started */
538#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000539 char_u ts_save_prewordlen; /* saved "prewordlen" */
Bram Moolenaarea424162005-06-16 21:51:00 +0000540 char_u ts_save_splitoff; /* su_splitoff saved here */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000541 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000542} trystate_T;
543
Bram Moolenaarea424162005-06-16 21:51:00 +0000544/* values for ts_isdiff */
545#define DIFF_NONE 0 /* no different byte (yet) */
546#define DIFF_YES 1 /* different byte found */
547#define DIFF_INSERT 2 /* inserting character */
548
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000549/* mode values for find_word */
550#define FIND_FOLDWORD 0 /* find word case-folded */
551#define FIND_KEEPWORD 1 /* find keep-case word */
552#define FIND_PREFIX 2 /* find word after prefix */
553
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000554static slang_T *slang_alloc __ARGS((char_u *lang));
555static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000556static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000557static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000558static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int prefid, char_u *word, slang_T *slang));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000559static void find_prefix __ARGS((matchinf_T *mip));
560static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000561static int spell_valid_case __ARGS((int origflags, int treeflags));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000562static int no_spell_checking __ARGS((void));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000563static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000564static char_u *spell_enc __ARGS((void));
565static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000566static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000567#ifdef FEAT_MBYTE
568static int *mb_str2wide __ARGS((char_u *s));
569#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000570static 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 +0000571static int find_region __ARGS((char_u *rp, char_u *region));
572static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000573static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000574static int set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000575static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
576static void write_spell_chartab __ARGS((FILE *fd));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000577static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaarea408852005-06-25 22:49:46 +0000578static void spell_find_suggest __ARGS((char_u *badptr, suginfo_T *su, int maxcount, int banbadword));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000579#ifdef FEAT_EVAL
580static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
581#endif
582static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
583static void spell_suggest_intern __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000584static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000585static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000586static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000587static void suggest_try_special __ARGS((suginfo_T *su));
588static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000589static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
590static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000591static void score_comp_sal __ARGS((suginfo_T *su));
592static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000593static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000594static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000595static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000596static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000597static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000598static void add_suggestion __ARGS((suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int score, int altscore, int had_bonus));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000599static void add_banned __ARGS((suginfo_T *su, char_u *word));
600static int was_banned __ARGS((suginfo_T *su, char_u *word));
601static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000602static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000603static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000604static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000605#ifdef FEAT_MBYTE
606static void spell_soundfold_w __ARGS((slang_T *slang, char_u *inword, char_u *res));
607#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000608static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000609static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000610static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum));
611static linenr_T apply_prefixes __ARGS((slang_T *slang, char_u *word, int round, int flags, linenr_T startlnum));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000612
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000613/*
614 * Use our own character-case definitions, because the current locale may
615 * differ from what the .spl file uses.
616 * These must not be called with negative number!
617 */
618#ifndef FEAT_MBYTE
619/* Non-multi-byte implementation. */
620# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
621# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
622# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
623#else
624/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
625 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
626 * the "w" library function for characters above 255 if available. */
627# ifdef HAVE_TOWLOWER
628# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
629 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
630# else
631# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
632 : (c) < 256 ? spelltab.st_fold[c] : (c))
633# endif
634
635# ifdef HAVE_TOWUPPER
636# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
637 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
638# else
639# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
640 : (c) < 256 ? spelltab.st_upper[c] : (c))
641# endif
642
643# ifdef HAVE_ISWUPPER
644# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
645 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
646# else
647# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
648 : (c) < 256 ? spelltab.st_isu[c] : (c))
649# endif
650#endif
651
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000652
653static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000654
655/*
656 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000657 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000658 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
659 * or when it's OK it remains unchanged.
660 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000661 *
662 * "sug" is normally NULL. When looking for suggestions it points to
663 * suginfo_T. It's passed as a void pointer to keep the struct local.
664 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000665 * Returns the length of the word in bytes, also when it's OK, so that the
666 * caller can skip over the word.
667 */
668 int
Bram Moolenaar51485f02005-06-04 21:55:20 +0000669spell_check(wp, ptr, attrp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000670 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000671 char_u *ptr;
672 int *attrp;
673{
674 matchinf_T mi; /* Most things are put in "mi" so that it can
675 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000676 int nrlen = 0; /* found a number first */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000677
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000678 /* A word never starts at a space or a control character. Return quickly
679 * then, skipping over the character. */
680 if (*ptr <= ' ')
681 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000682
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000683 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar0c405862005-06-22 22:26:26 +0000684 * 0X99FF. But when a word character follows do check spelling to find
685 * "3GPP". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000686 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000687 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000688 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
689 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000690 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000691 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000692 mi.mi_end = skipdigits(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000693 nrlen = mi.mi_end - ptr;
694 }
Bram Moolenaarea408852005-06-25 22:49:46 +0000695 if (!spell_iswordp(mi.mi_end))
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000696 return (int)(mi.mi_end - ptr);
Bram Moolenaar0c405862005-06-22 22:26:26 +0000697
698 /* Try including the digits in the word. */
699 mi.mi_fend = ptr + nrlen;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000700 }
Bram Moolenaar0c405862005-06-22 22:26:26 +0000701 else
702 mi.mi_fend = ptr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000703
Bram Moolenaar0c405862005-06-22 22:26:26 +0000704 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000705 mi.mi_word = ptr;
Bram Moolenaarea408852005-06-25 22:49:46 +0000706 if (spell_iswordp(mi.mi_fend))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000707 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000708 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000709 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000710 mb_ptr_adv(mi.mi_fend);
Bram Moolenaarea408852005-06-25 22:49:46 +0000711 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000712 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000713
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000714 /* We always use the characters up to the next non-word character,
715 * also for bad words. */
716 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000717
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000718 /* Check caps type later. */
719 mi.mi_capflags = 0;
720 mi.mi_cend = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000721
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000722 /* Include one non-word character so that we can check for the
723 * word end. */
724 if (*mi.mi_fend != NUL)
725 mb_ptr_adv(mi.mi_fend);
726
727 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
728 MAXWLEN + 1);
729 mi.mi_fwordlen = STRLEN(mi.mi_fword);
730
731 /* The word is bad unless we recognize it. */
732 mi.mi_result = SP_BAD;
733
734 /*
735 * Loop over the languages specified in 'spelllang'.
736 * We check them all, because a matching word may be longer than an
737 * already found matching word.
738 */
739 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
740 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
741 {
742 /* Check for a matching word in case-folded words. */
743 find_word(&mi, FIND_FOLDWORD);
744
745 /* Check for a matching word in keep-case words. */
746 find_word(&mi, FIND_KEEPWORD);
747
748 /* Check for matching prefixes. */
749 find_prefix(&mi);
750 }
751
752 if (mi.mi_result != SP_OK)
753 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000754 /* If we found a number skip over it. Allows for "42nd". Do flag
755 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000756 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000757 {
758 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
759 return nrlen;
760 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000761
762 /* When we are at a non-word character there is no error, just
763 * skip over the character (try looking for a word after it). */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000764 else if (!SPELL_ISWORDP(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000765 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000766#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000767 if (has_mbyte)
768 return mb_ptr2len_check(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000769#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000770 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000771 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000772
773 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
774 *attrp = highlight_attr[HLF_SPB];
775 else if (mi.mi_result == SP_RARE)
776 *attrp = highlight_attr[HLF_SPR];
777 else
778 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000779 }
780
Bram Moolenaar51485f02005-06-04 21:55:20 +0000781 return (int)(mi.mi_end - ptr);
782}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000783
Bram Moolenaar51485f02005-06-04 21:55:20 +0000784/*
785 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000786 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
787 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
788 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
789 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000790 *
791 * For a match mip->mi_result is updated.
792 */
793 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000794find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000795 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000796 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000797{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000798 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000799 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000800 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000801 int endidxcnt = 0;
802 int len;
803 int wlen = 0;
804 int flen;
805 int c;
806 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000807 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000808#ifdef FEAT_MBYTE
809 char_u *s;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000810 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000811#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000812 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000813 slang_T *slang = mip->mi_lp->lp_slang;
814 unsigned flags;
815 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000816 idx_T *idxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000817 int prefid;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000818
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000819 if (mode == FIND_KEEPWORD)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000820 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000821 /* Check for word with matching case in keep-case tree. */
822 ptr = mip->mi_word;
823 flen = 9999; /* no case folding, always enough bytes */
824 byts = slang->sl_kbyts;
825 idxs = slang->sl_kidxs;
826 }
827 else
828 {
829 /* Check for case-folded in case-folded tree. */
830 ptr = mip->mi_fword;
831 flen = mip->mi_fwordlen; /* available case-folded bytes */
832 byts = slang->sl_fbyts;
833 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000834
835 if (mode == FIND_PREFIX)
836 {
837 /* Skip over the prefix. */
838 wlen = mip->mi_prefixlen;
839 flen -= mip->mi_prefixlen;
840 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000841 }
842
Bram Moolenaar51485f02005-06-04 21:55:20 +0000843 if (byts == NULL)
844 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000845
Bram Moolenaar51485f02005-06-04 21:55:20 +0000846 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000847 * Repeat advancing in the tree until:
848 * - there is a byte that doesn't match,
849 * - we reach the end of the tree,
850 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000851 */
852 for (;;)
853 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000854 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000855 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000856
857 len = byts[arridx++];
858
859 /* If the first possible byte is a zero the word could end here.
860 * Remember this index, we first check for the longest word. */
861 if (byts[arridx] == 0)
862 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000863 if (endidxcnt == MAXWLEN)
864 {
865 /* Must be a corrupted spell file. */
866 EMSG(_(e_format));
867 return;
868 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000869 endlen[endidxcnt] = wlen;
870 endidx[endidxcnt++] = arridx++;
871 --len;
872
873 /* Skip over the zeros, there can be several flag/region
874 * combinations. */
875 while (len > 0 && byts[arridx] == 0)
876 {
877 ++arridx;
878 --len;
879 }
880 if (len == 0)
881 break; /* no children, word must end here */
882 }
883
884 /* Stop looking at end of the line. */
885 if (ptr[wlen] == NUL)
886 break;
887
888 /* Perform a binary search in the list of accepted bytes. */
889 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +0000890 if (c == TAB) /* <Tab> is handled like <Space> */
891 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +0000892 lo = arridx;
893 hi = arridx + len - 1;
894 while (lo < hi)
895 {
896 m = (lo + hi) / 2;
897 if (byts[m] > c)
898 hi = m - 1;
899 else if (byts[m] < c)
900 lo = m + 1;
901 else
902 {
903 lo = hi = m;
904 break;
905 }
906 }
907
908 /* Stop if there is no matching byte. */
909 if (hi < lo || byts[lo] != c)
910 break;
911
912 /* Continue at the child (if there is one). */
913 arridx = idxs[lo];
914 ++wlen;
915 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +0000916
917 /* One space in the good word may stand for several spaces in the
918 * checked word. */
919 if (c == ' ')
920 {
921 for (;;)
922 {
923 if (flen <= 0 && *mip->mi_fend != NUL)
924 flen = fold_more(mip);
925 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
926 break;
927 ++wlen;
928 --flen;
929 }
930 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000931 }
932
933 /*
934 * Verify that one of the possible endings is valid. Try the longest
935 * first.
936 */
937 while (endidxcnt > 0)
938 {
939 --endidxcnt;
940 arridx = endidx[endidxcnt];
941 wlen = endlen[endidxcnt];
942
943#ifdef FEAT_MBYTE
944 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
945 continue; /* not at first byte of character */
946#endif
Bram Moolenaarea408852005-06-25 22:49:46 +0000947 if (spell_iswordp(ptr + wlen))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000948 continue; /* next char is a word character */
949
950#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000951 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000952 {
953 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000954 * when folding case. This can be slow, take a shortcut when the
955 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000956 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000957 if (STRNCMP(ptr, p, wlen) != 0)
958 {
959 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
960 mb_ptr_adv(p);
961 wlen = p - mip->mi_word;
962 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000963 }
964#endif
965
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000966 /* Check flags and region. For FIND_PREFIX check the condition and
967 * prefix ID.
968 * Repeat this if there are more flags/region alternatives until there
969 * is a match. */
970 res = SP_BAD;
971 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
972 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000973 {
974 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000975
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000976 /* For the fold-case tree check that the case of the checked word
977 * matches with what the word in the tree requires.
978 * For keep-case tree the case is always right. For prefixes we
979 * don't bother to check. */
980 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000981 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000982 if (mip->mi_cend != mip->mi_word + wlen)
983 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000984 /* mi_capflags was set for a different word length, need
985 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000986 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000987 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000988 }
989
Bram Moolenaar0c405862005-06-22 22:26:26 +0000990 if (mip->mi_capflags == WF_KEEPCAP
991 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000992 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000993 }
994
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000995 /* When mode is FIND_PREFIX the word must support the prefix:
996 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000997 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000998 if (mode == FIND_PREFIX)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000999 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001000 /* The prefix ID is stored two bytes above the flags. */
1001 prefid = (unsigned)flags >> 16;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001002 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001003 prefid, mip->mi_fword + mip->mi_prefixlen,
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001004 slang);
1005 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001006 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001007
1008 /* Use the WF_RARE flag for a rare prefix. */
1009 if (c & WF_RAREPFX)
1010 flags |= WF_RARE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001011 }
1012
1013 if (flags & WF_BANNED)
1014 res = SP_BANNED;
1015 else if (flags & WF_REGION)
1016 {
1017 /* Check region. */
1018 if ((mip->mi_lp->lp_region & (flags >> 8)) != 0)
1019 res = SP_OK;
1020 else
1021 res = SP_LOCAL;
1022 }
1023 else if (flags & WF_RARE)
1024 res = SP_RARE;
1025 else
1026 res = SP_OK;
1027
1028 /* Always use the longest match and the best result. */
1029 if (mip->mi_result > res)
1030 {
1031 mip->mi_result = res;
1032 mip->mi_end = mip->mi_word + wlen;
1033 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001034 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001035 mip->mi_end = mip->mi_word + wlen;
1036
1037 if (res == SP_OK)
1038 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001039 }
1040
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001041 if (res == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001042 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001043 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001044}
1045
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001046/*
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001047 * Return non-zero if the prefix indicated by "mip->mi_prefarridx" matches
1048 * with the prefix ID "prefid" for the word "word".
1049 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001050 */
1051 static int
1052valid_word_prefix(totprefcnt, arridx, prefid, word, slang)
1053 int totprefcnt; /* nr of prefix IDs */
1054 int arridx; /* idx in sl_pidxs[] */
1055 int prefid;
1056 char_u *word;
1057 slang_T *slang;
1058{
1059 int prefcnt;
1060 int pidx;
1061 regprog_T *rp;
1062 regmatch_T regmatch;
1063
1064 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1065 {
1066 pidx = slang->sl_pidxs[arridx + prefcnt];
1067
1068 /* Check the prefix ID. */
1069 if (prefid != (pidx & 0xff))
1070 continue;
1071
1072 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001073 * stored in the two bytes above the prefix ID byte. */
1074 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001075 if (rp != NULL)
1076 {
1077 regmatch.regprog = rp;
1078 regmatch.rm_ic = FALSE;
1079 if (!vim_regexec(&regmatch, word, 0))
1080 continue;
1081 }
1082
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001083 /* It's a match! Return the WF_RAREPFX flag. */
1084 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001085 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001086 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001087}
1088
1089/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001090 * Check if the word at "mip->mi_word" has a matching prefix.
1091 * If it does, then check the following word.
1092 *
1093 * For a match mip->mi_result is updated.
1094 */
1095 static void
1096find_prefix(mip)
1097 matchinf_T *mip;
1098{
1099 idx_T arridx = 0;
1100 int len;
1101 int wlen = 0;
1102 int flen;
1103 int c;
1104 char_u *ptr;
1105 idx_T lo, hi, m;
1106 slang_T *slang = mip->mi_lp->lp_slang;
1107 char_u *byts;
1108 idx_T *idxs;
1109
1110 /* We use the case-folded word here, since prefixes are always
1111 * case-folded. */
1112 ptr = mip->mi_fword;
1113 flen = mip->mi_fwordlen; /* available case-folded bytes */
1114 byts = slang->sl_pbyts;
1115 idxs = slang->sl_pidxs;
1116
1117 if (byts == NULL)
1118 return; /* array is empty */
1119
1120 /*
1121 * Repeat advancing in the tree until:
1122 * - there is a byte that doesn't match,
1123 * - we reach the end of the tree,
1124 * - or we reach the end of the line.
1125 */
1126 for (;;)
1127 {
1128 if (flen == 0 && *mip->mi_fend != NUL)
1129 flen = fold_more(mip);
1130
1131 len = byts[arridx++];
1132
1133 /* If the first possible byte is a zero the prefix could end here.
1134 * Check if the following word matches and supports the prefix. */
1135 if (byts[arridx] == 0)
1136 {
1137 /* There can be several prefixes with different conditions. We
1138 * try them all, since we don't know which one will give the
1139 * longest match. The word is the same each time, pass the list
1140 * of possible prefixes to find_word(). */
1141 mip->mi_prefarridx = arridx;
1142 mip->mi_prefcnt = len;
1143 while (len > 0 && byts[arridx] == 0)
1144 {
1145 ++arridx;
1146 --len;
1147 }
1148 mip->mi_prefcnt -= len;
1149
1150 /* Find the word that comes after the prefix. */
1151 mip->mi_prefixlen = wlen;
1152 find_word(mip, FIND_PREFIX);
1153
1154
1155 if (len == 0)
1156 break; /* no children, word must end here */
1157 }
1158
1159 /* Stop looking at end of the line. */
1160 if (ptr[wlen] == NUL)
1161 break;
1162
1163 /* Perform a binary search in the list of accepted bytes. */
1164 c = ptr[wlen];
1165 lo = arridx;
1166 hi = arridx + len - 1;
1167 while (lo < hi)
1168 {
1169 m = (lo + hi) / 2;
1170 if (byts[m] > c)
1171 hi = m - 1;
1172 else if (byts[m] < c)
1173 lo = m + 1;
1174 else
1175 {
1176 lo = hi = m;
1177 break;
1178 }
1179 }
1180
1181 /* Stop if there is no matching byte. */
1182 if (hi < lo || byts[lo] != c)
1183 break;
1184
1185 /* Continue at the child (if there is one). */
1186 arridx = idxs[lo];
1187 ++wlen;
1188 --flen;
1189 }
1190}
1191
1192/*
1193 * Need to fold at least one more character. Do until next non-word character
1194 * for efficiency.
1195 * Return the length of the folded chars in bytes.
1196 */
1197 static int
1198fold_more(mip)
1199 matchinf_T *mip;
1200{
1201 int flen;
1202 char_u *p;
1203
1204 p = mip->mi_fend;
1205 do
1206 {
1207 mb_ptr_adv(mip->mi_fend);
Bram Moolenaarea408852005-06-25 22:49:46 +00001208 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001209
1210 /* Include the non-word character so that we can check for the
1211 * word end. */
1212 if (*mip->mi_fend != NUL)
1213 mb_ptr_adv(mip->mi_fend);
1214
1215 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1216 mip->mi_fword + mip->mi_fwordlen,
1217 MAXWLEN - mip->mi_fwordlen);
1218 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1219 mip->mi_fwordlen += flen;
1220 return flen;
1221}
1222
1223/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001224 * Check case flags for a word. Return TRUE if the word has the requested
1225 * case.
1226 */
1227 static int
1228spell_valid_case(origflags, treeflags)
1229 int origflags; /* flags for the checked word. */
1230 int treeflags; /* flags for the word in the spell tree */
1231{
1232 return (origflags == WF_ALLCAP
1233 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
1234 && ((treeflags & WF_ONECAP) == 0 || origflags == WF_ONECAP)));
1235}
1236
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001237/*
1238 * Return TRUE if spell checking is not enabled.
1239 */
1240 static int
1241no_spell_checking()
1242{
1243 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
1244 {
1245 EMSG(_("E756: Spell checking is not enabled"));
1246 return TRUE;
1247 }
1248 return FALSE;
1249}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001250
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001251/*
1252 * Move to next spell error.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001253 * "curline" is TRUE for "z?": find word under/after cursor in the same line.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001254 * Return OK if found, FAIL otherwise.
1255 */
1256 int
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001257spell_move_to(dir, allwords, curline)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001258 int dir; /* FORWARD or BACKWARD */
1259 int allwords; /* TRUE for "[s" and "]s" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001260 int curline;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001261{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001262 linenr_T lnum;
1263 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001264 char_u *line;
1265 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001266 char_u *endp;
1267 int attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001268 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001269 int has_syntax = syntax_present(curbuf);
1270 int col;
1271 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001272 char_u *buf = NULL;
1273 int buflen = 0;
1274 int skip = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001275
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001276 if (no_spell_checking())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001277 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001278
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001279 /*
1280 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001281 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001282 *
1283 * When searching backwards, we continue in the line to find the last
1284 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001285 *
1286 * We concatenate the start of the next line, so that wrapped words work
1287 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1288 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001289 */
1290 lnum = curwin->w_cursor.lnum;
1291 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001292
1293 while (!got_int)
1294 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001295 line = ml_get(lnum);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001296
Bram Moolenaar0c405862005-06-22 22:26:26 +00001297 len = STRLEN(line);
1298 if (buflen < len + MAXWLEN + 2)
1299 {
1300 vim_free(buf);
1301 buflen = len + MAXWLEN + 2;
1302 buf = alloc(buflen);
1303 if (buf == NULL)
1304 break;
1305 }
1306
1307 /* Copy the line into "buf" and append the start of the next line if
1308 * possible. */
1309 STRCPY(buf, line);
1310 if (lnum < curbuf->b_ml.ml_line_count)
1311 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1312
1313 p = buf + skip;
1314 endp = buf + len;
1315 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001316 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001317 /* When searching backward don't search after the cursor. */
1318 if (dir == BACKWARD
1319 && lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001320 && (colnr_T)(p - buf) >= curwin->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001321 break;
1322
1323 /* start of word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00001324 attr = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001325 len = spell_check(curwin, p, &attr);
1326
1327 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001328 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001329 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001330 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001331 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001332 /* When searching forward only accept a bad word after
1333 * the cursor. */
1334 if (dir == BACKWARD
1335 || lnum > curwin->w_cursor.lnum
1336 || (lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001337 && (colnr_T)(curline ? p - buf + len
1338 : p - buf)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001339 > curwin->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001340 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001341 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001342 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001343 col = p - buf;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001344 (void)syn_get_id(lnum, (colnr_T)col,
1345 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001346 }
1347 else
1348 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001349
Bram Moolenaar51485f02005-06-04 21:55:20 +00001350 if (can_spell)
1351 {
1352 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001353 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001354#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001355 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001356#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001357 if (dir == FORWARD)
1358 {
1359 /* No need to search further. */
1360 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001361 vim_free(buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001362 return OK;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001363 }
1364 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001365 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001366 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001367 }
1368
Bram Moolenaar51485f02005-06-04 21:55:20 +00001369 /* advance to character after the word */
1370 p += len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001371 }
1372
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001373 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001374 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001375
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001376 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001377 if (dir == BACKWARD)
1378 {
1379 if (found_pos.lnum != 0)
1380 {
1381 /* Use the last match in the line. */
1382 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001383 vim_free(buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001384 return OK;
1385 }
1386 if (lnum == 1)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001387 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001388 --lnum;
1389 }
1390 else
1391 {
1392 if (lnum == curbuf->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001393 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001394 ++lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001395
1396 /* Skip the characters at the start of the next line that were
1397 * included in a match crossing line boundaries. */
1398 if (attr == 0)
1399 skip = p - endp;
1400 else
1401 skip = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001402 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001403
1404 line_breakcheck();
1405 }
1406
Bram Moolenaar0c405862005-06-22 22:26:26 +00001407 vim_free(buf);
1408 return FAIL;
1409}
1410
1411/*
1412 * For spell checking: concatenate the start of the following line "line" into
1413 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
1414 */
1415 void
1416spell_cat_line(buf, line, maxlen)
1417 char_u *buf;
1418 char_u *line;
1419 int maxlen;
1420{
1421 char_u *p;
1422 int n;
1423
1424 p = skipwhite(line);
1425 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1426 p = skipwhite(p + 1);
1427
1428 if (*p != NUL)
1429 {
1430 *buf = ' ';
1431 vim_strncpy(buf + 1, line, maxlen - 1);
1432 n = p - line;
1433 if (n >= maxlen)
1434 n = maxlen - 1;
1435 vim_memset(buf + 1, ' ', n);
1436 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001437}
1438
1439/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001440 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001441 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001442 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001443 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001444spell_load_lang(lang)
1445 char_u *lang;
1446{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001447 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001448 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001449 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001450
Bram Moolenaarb765d632005-06-07 21:00:02 +00001451 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001452 * It's truncated when an error is detected. */
1453 STRCPY(langcp, lang);
1454
Bram Moolenaarb765d632005-06-07 21:00:02 +00001455 /*
1456 * Find the first spell file for "lang" in 'runtimepath' and load it.
1457 */
1458 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
1459 "spell/%s.%s.spl", lang, spell_enc());
1460 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001461
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001462 if (r == FAIL && *langcp != NUL)
1463 {
1464 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001465 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001466 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001467 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001468 }
1469
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001470 if (r == FAIL)
1471 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
1472 fname_enc + 6);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001473 else if (*langcp != NUL)
1474 {
1475 /* Load all the additions. */
1476 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
1477 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
1478 }
1479}
1480
1481/*
1482 * Return the encoding used for spell checking: Use 'encoding', except that we
1483 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1484 */
1485 static char_u *
1486spell_enc()
1487{
1488
1489#ifdef FEAT_MBYTE
1490 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1491 return p_enc;
1492#endif
1493 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001494}
1495
1496/*
1497 * Allocate a new slang_T.
1498 * Caller must fill "sl_next".
1499 */
1500 static slang_T *
1501slang_alloc(lang)
1502 char_u *lang;
1503{
1504 slang_T *lp;
1505
Bram Moolenaar51485f02005-06-04 21:55:20 +00001506 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001507 if (lp != NULL)
1508 {
1509 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001510 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001511 ga_init2(&lp->sl_sal, sizeof(salitem_T), 10);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001512 }
1513 return lp;
1514}
1515
1516/*
1517 * Free the contents of an slang_T and the structure itself.
1518 */
1519 static void
1520slang_free(lp)
1521 slang_T *lp;
1522{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001523 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001524 vim_free(lp->sl_fname);
1525 slang_clear(lp);
1526 vim_free(lp);
1527}
1528
1529/*
1530 * Clear an slang_T so that the file can be reloaded.
1531 */
1532 static void
1533slang_clear(lp)
1534 slang_T *lp;
1535{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001536 garray_T *gap;
1537 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001538 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001539 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001540
Bram Moolenaar51485f02005-06-04 21:55:20 +00001541 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001542 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001543 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001544 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001545 vim_free(lp->sl_pbyts);
1546 lp->sl_pbyts = NULL;
1547
Bram Moolenaar51485f02005-06-04 21:55:20 +00001548 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001549 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001550 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001551 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001552 vim_free(lp->sl_pidxs);
1553 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001554
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001555 gap = &lp->sl_rep;
1556 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001557 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001558 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1559 vim_free(ftp->ft_from);
1560 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001561 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001562 ga_clear(gap);
1563
1564 gap = &lp->sl_sal;
1565 while (gap->ga_len > 0)
1566 {
1567 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1568 vim_free(smp->sm_lead);
1569 vim_free(smp->sm_to);
1570 }
1571 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001572
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001573 for (i = 0; i < lp->sl_prefixcnt; ++i)
1574 vim_free(lp->sl_prefprog[i]);
1575 vim_free(lp->sl_prefprog);
1576
Bram Moolenaarea424162005-06-16 21:51:00 +00001577#ifdef FEAT_MBYTE
1578 {
1579 int todo = lp->sl_map_hash.ht_used;
1580 hashitem_T *hi;
1581
1582 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
1583 if (!HASHITEM_EMPTY(hi))
1584 {
1585 --todo;
1586 vim_free(hi->hi_key);
1587 }
1588 }
1589 hash_clear(&lp->sl_map_hash);
1590#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001591}
1592
1593/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001594 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001595 * Invoked through do_in_runtimepath().
1596 */
1597 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00001598spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001599 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001600 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001601{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001602 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001603}
1604
1605/*
1606 * Load one spell file and store the info into a slang_T.
1607 *
1608 * This is invoked in two ways:
1609 * - From spell_load_cb() to load a spell file for the first time. "lang" is
1610 * the language name, "old_lp" is NULL. Will allocate an slang_T.
1611 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
1612 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001613 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00001614 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001615 static slang_T *
1616spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001617 char_u *fname;
1618 char_u *lang;
1619 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001620 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001621{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001622 FILE *fd;
1623 char_u buf[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001624 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001625 char_u *bp;
1626 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001627 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001628 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001629 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001630 int round;
1631 char_u *save_sourcing_name = sourcing_name;
1632 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001633 int cnt, ccnt;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001634 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001635 slang_T *lp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001636 garray_T *gap;
1637 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001638 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001639 int rr;
1640 short *first;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001641 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001642 int c = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001643
Bram Moolenaarb765d632005-06-07 21:00:02 +00001644 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001645 if (fd == NULL)
1646 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001647 if (!silent)
1648 EMSG2(_(e_notopen), fname);
1649 else if (p_verbose > 2)
1650 {
1651 verbose_enter();
1652 smsg((char_u *)e_notopen, fname);
1653 verbose_leave();
1654 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001655 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001656 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00001657 if (p_verbose > 2)
1658 {
1659 verbose_enter();
1660 smsg((char_u *)_("Reading spell file \"%s\""), fname);
1661 verbose_leave();
1662 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001663
Bram Moolenaarb765d632005-06-07 21:00:02 +00001664 if (old_lp == NULL)
1665 {
1666 lp = slang_alloc(lang);
1667 if (lp == NULL)
1668 goto endFAIL;
1669
1670 /* Remember the file name, used to reload the file when it's updated. */
1671 lp->sl_fname = vim_strsave(fname);
1672 if (lp->sl_fname == NULL)
1673 goto endFAIL;
1674
1675 /* Check for .add.spl. */
1676 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
1677 }
1678 else
1679 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001680
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001681 /* Set sourcing_name, so that error messages mention the file name. */
1682 sourcing_name = fname;
1683 sourcing_lnum = 0;
1684
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001685 /* <HEADER>: <fileID>
1686 * <regioncnt> <regionname> ...
1687 * <charflagslen> <charflags>
1688 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001689 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001690 * <prefcondcnt> <prefcond> ...
1691 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001692 for (i = 0; i < VIMSPELLMAGICL; ++i)
1693 buf[i] = getc(fd); /* <fileID> */
1694 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
1695 {
1696 EMSG(_("E757: Wrong file ID in spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001697 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001698 }
1699
1700 cnt = getc(fd); /* <regioncnt> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001701 if (cnt < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001702 {
1703truncerr:
1704 EMSG(_("E758: Truncated spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001705 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001706 }
1707 if (cnt > 8)
1708 {
1709formerr:
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001710 EMSG(_(e_format));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001711 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001712 }
1713 for (i = 0; i < cnt; ++i)
1714 {
1715 lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
1716 lp->sl_regions[i * 2 + 1] = getc(fd);
1717 }
1718 lp->sl_regions[cnt * 2] = NUL;
1719
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001720 cnt = getc(fd); /* <charflagslen> */
1721 if (cnt > 0)
1722 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001723 p = alloc((unsigned)cnt);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001724 if (p == NULL)
1725 goto endFAIL;
1726 for (i = 0; i < cnt; ++i)
1727 p[i] = getc(fd); /* <charflags> */
1728
1729 ccnt = (getc(fd) << 8) + getc(fd); /* <fcharslen> */
1730 if (ccnt <= 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001731 {
1732 vim_free(p);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001733 goto formerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001734 }
1735 fol = alloc((unsigned)ccnt + 1);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001736 if (fol == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001737 {
1738 vim_free(p);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001739 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001740 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001741 for (i = 0; i < ccnt; ++i)
1742 fol[i] = getc(fd); /* <fchars> */
1743 fol[i] = NUL;
1744
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001745 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001746 i = set_spell_charflags(p, cnt, fol);
1747 vim_free(p);
1748 vim_free(fol);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001749#if 0 /* tolerate the differences */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001750 if (i == FAIL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001751 goto formerr;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001752#endif
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001753 }
1754 else
1755 {
1756 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
1757 cnt = (getc(fd) << 8) + getc(fd);
1758 if (cnt != 0)
1759 goto formerr;
1760 }
1761
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001762 /* <midwordlen> <midword> */
1763 cnt = (getc(fd) << 8) + getc(fd);
1764 if (cnt < 0)
1765 goto truncerr;
1766 if (cnt > 0)
1767 {
1768 for (i = 0; i < cnt; ++i)
1769 if (i < MAXWLEN) /* truncate at reasonable length */
1770 buf[i] = getc(fd);
1771 if (i < MAXWLEN)
1772 buf[i] = NUL;
1773 else
1774 buf[MAXWLEN] = NUL;
1775
1776 /* The midword characters add up to any midword characters from other
1777 * .spel files. */
1778 for (p = buf; *p != NUL; )
1779#ifdef FEAT_MBYTE
1780 if (has_mbyte)
1781 {
1782 c = mb_ptr2char(p);
1783 i = mb_ptr2len_check(p);
1784 if (c < 256)
1785 spell_ismw[c] = TRUE;
1786 else if (spell_ismw_mb == NULL)
1787 /* First multi-byte char in "spell_ismw_mb". */
1788 spell_ismw_mb = vim_strnsave(p, i);
1789 else
1790 {
1791 /* Append multi-byte chars to "spell_ismw_mb". */
1792 n = STRLEN(spell_ismw_mb);
1793 bp = vim_strnsave(spell_ismw_mb, n + i);
1794 if (bp != NULL)
1795 {
1796 vim_free(spell_ismw_mb);
1797 spell_ismw_mb = bp;
1798 vim_strncpy(bp + n, p, i);
1799 }
1800 }
1801 p += i;
1802 }
1803 else
1804#endif
1805 spell_ismw[*p++] = TRUE;
1806 }
1807
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001808 /* <prefcondcnt> <prefcond> ... */
1809 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
1810 if (cnt > 0)
1811 {
1812 lp->sl_prefprog = (regprog_T **)alloc_clear(
1813 (unsigned)sizeof(regprog_T *) * cnt);
1814 if (lp->sl_prefprog == NULL)
1815 goto endFAIL;
1816 lp->sl_prefixcnt = cnt;
1817
1818 for (i = 0; i < cnt; ++i)
1819 {
1820 /* <prefcond> : <condlen> <condstr> */
1821 n = getc(fd); /* <condlen> */
1822 if (n < 0)
1823 goto formerr;
1824 /* When <condlen> is zero we have an empty condition. Otherwise
1825 * compile the regexp program used to check for the condition. */
1826 if (n > 0)
1827 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001828 buf[0] = '^'; /* always match at one position only */
1829 p = buf + 1;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001830 while (n-- > 0)
1831 *p++ = getc(fd); /* <condstr> */
1832 *p = NUL;
1833 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
1834 }
1835 }
1836 }
1837
1838
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001839 /* <SUGGEST> : <repcount> <rep> ...
1840 * <salflags> <salcount> <sal> ...
1841 * <maplen> <mapstr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001842
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001843 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
1844 if (cnt < 0)
1845 goto formerr;
1846
1847 gap = &lp->sl_rep;
1848 if (ga_grow(gap, cnt) == FAIL)
1849 goto endFAIL;
1850
1851 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
1852 for (; gap->ga_len < cnt; ++gap->ga_len)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001853 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001854 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
1855 for (rr = 1; rr <= 2; ++rr)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001856 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001857 ccnt = getc(fd);
1858 if (ccnt < 0)
1859 {
1860 if (rr == 2)
1861 vim_free(ftp->ft_from);
1862 goto formerr;
1863 }
1864 if ((p = alloc(ccnt + 1)) == NULL)
1865 {
1866 if (rr == 2)
1867 vim_free(ftp->ft_from);
1868 goto endFAIL;
1869 }
1870 for (i = 0; i < ccnt; ++i)
1871 p[i] = getc(fd); /* <repfrom> or <repto> */
1872 p[i] = NUL;
1873 if (rr == 1)
1874 ftp->ft_from = p;
1875 else
1876 ftp->ft_to = p;
1877 }
1878 }
1879
1880 /* Fill the first-index table. */
1881 first = lp->sl_rep_first;
1882 for (i = 0; i < 256; ++i)
1883 first[i] = -1;
1884 for (i = 0; i < gap->ga_len; ++i)
1885 {
1886 ftp = &((fromto_T *)gap->ga_data)[i];
1887 if (first[*ftp->ft_from] == -1)
1888 first[*ftp->ft_from] = i;
1889 }
1890
1891 i = getc(fd); /* <salflags> */
1892 if (i & SAL_F0LLOWUP)
1893 lp->sl_followup = TRUE;
1894 if (i & SAL_COLLAPSE)
1895 lp->sl_collapse = TRUE;
1896 if (i & SAL_REM_ACCENTS)
1897 lp->sl_rem_accents = TRUE;
1898
1899 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
1900 if (cnt < 0)
1901 goto formerr;
1902
1903 gap = &lp->sl_sal;
1904 if (ga_grow(gap, cnt) == FAIL)
1905 goto endFAIL;
1906
1907 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
1908 for (; gap->ga_len < cnt; ++gap->ga_len)
1909 {
1910 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
1911 ccnt = getc(fd); /* <salfromlen> */
1912 if (ccnt < 0)
1913 goto formerr;
1914 if ((p = alloc(ccnt + 2)) == NULL)
1915 goto endFAIL;
1916 smp->sm_lead = p;
1917
1918 /* Read up to the first special char into sm_lead. */
1919 for (i = 0; i < ccnt; ++i)
1920 {
1921 c = getc(fd); /* <salfrom> */
1922 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
1923 break;
1924 *p++ = c;
1925 }
1926 smp->sm_leadlen = p - smp->sm_lead;
1927 *p++ = NUL;
1928
1929 /* Put optional chars in sm_oneoff, if any. */
1930 if (c == '(')
1931 {
1932 smp->sm_oneoff = p;
1933 for (++i; i < ccnt; ++i)
1934 {
1935 c = getc(fd); /* <salfrom> */
1936 if (c == ')')
1937 break;
1938 *p++ = c;
1939 }
1940 *p++ = NUL;
1941 if (++i < ccnt)
1942 c = getc(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001943 }
1944 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001945 smp->sm_oneoff = NULL;
1946
1947 /* Any following chars go in sm_rules. */
1948 smp->sm_rules = p;
1949 if (i < ccnt)
1950 *p++ = c;
1951 for (++i; i < ccnt; ++i)
1952 *p++ = getc(fd); /* <salfrom> */
1953 *p++ = NUL;
1954
1955 ccnt = getc(fd); /* <saltolen> */
1956 if (ccnt < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001957 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001958 vim_free(smp->sm_lead);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001959 goto formerr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001960 }
1961 if ((p = alloc(ccnt + 1)) == NULL)
1962 {
1963 vim_free(smp->sm_lead);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001964 goto endFAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001965 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001966 smp->sm_to = p;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001967
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001968 for (i = 0; i < ccnt; ++i)
1969 *p++ = getc(fd); /* <salto> */
1970 *p++ = NUL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00001971
1972#ifdef FEAT_MBYTE
1973 if (has_mbyte)
1974 {
1975 /* convert the multi-byte strings to wide char strings */
1976 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
1977 smp->sm_leadlen = mb_charlen(smp->sm_lead);
1978 if (smp->sm_oneoff == NULL)
1979 smp->sm_oneoff_w = NULL;
1980 else
1981 smp->sm_oneoff_w = mb_str2wide(smp->sm_oneoff);
1982 smp->sm_to_w = mb_str2wide(smp->sm_to);
1983 if (smp->sm_lead_w == NULL
1984 || (smp->sm_oneoff_w == NULL && smp->sm_oneoff != NULL)
1985 || smp->sm_to_w == NULL)
1986 {
1987 vim_free(smp->sm_lead);
1988 vim_free(smp->sm_to);
1989 vim_free(smp->sm_lead_w);
1990 vim_free(smp->sm_oneoff_w);
1991 vim_free(smp->sm_to_w);
1992 goto endFAIL;
1993 }
1994 }
1995#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001996 }
1997
1998 /* Fill the first-index table. */
1999 first = lp->sl_sal_first;
2000 for (i = 0; i < 256; ++i)
2001 first[i] = -1;
2002 for (i = 0; i < gap->ga_len; ++i)
2003 {
2004 smp = &((salitem_T *)gap->ga_data)[i];
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002005#ifdef FEAT_MBYTE
2006 if (has_mbyte)
2007 /* Use the lowest byte of the first character. */
2008 c = *smp->sm_lead_w & 0xff;
2009 else
2010#endif
2011 c = *smp->sm_lead;
2012 if (first[c] == -1)
2013 {
2014 first[c] = i;
2015#ifdef FEAT_MBYTE
2016 if (has_mbyte)
2017 {
2018 int j;
2019
2020 /* Make sure all entries with this byte are following each
2021 * other. Move the ones down that are in the wrong position.
2022 * Do keep the right sequence. */
2023 while (i + 1 < gap->ga_len && (*smp[1].sm_lead_w & 0xff) == c)
2024 {
2025 ++i;
2026 ++smp;
2027 }
2028 for (j = 1; i + j < gap->ga_len; ++j)
2029 if ((*smp[j].sm_lead_w & 0xff) == c)
2030 {
2031 salitem_T tsal;
2032
2033 ++i;
2034 ++smp;
2035 --j;
2036 tsal = smp[j];
2037 mch_memmove(smp + 1, smp, sizeof(salitem_T) * j);
2038 *smp = tsal;
2039 }
2040 }
2041#endif
2042 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002043 }
2044
2045 cnt = (getc(fd) << 8) + getc(fd); /* <maplen> */
2046 if (cnt < 0)
2047 goto formerr;
2048 p = alloc(cnt + 1);
2049 if (p == NULL)
2050 goto endFAIL;
2051 for (i = 0; i < cnt; ++i)
2052 p[i] = getc(fd); /* <mapstr> */
2053 p[i] = NUL;
Bram Moolenaarea424162005-06-16 21:51:00 +00002054 set_map_str(lp, p);
2055 vim_free(p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002056
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002057
Bram Moolenaar51485f02005-06-04 21:55:20 +00002058 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002059 * round 2: <KWORDTREE>
2060 * round 3: <PREFIXTREE> */
2061 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002062 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002063 /* The tree size was computed when writing the file, so that we can
2064 * allocate it as one long block. <nodecount> */
2065 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2066 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002067 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002068 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002069 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002070 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002071 bp = lalloc((long_u)len, TRUE);
2072 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002073 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002074 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002075 lp->sl_fbyts = bp;
2076 else if (round == 2)
2077 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002078 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002079 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002080
2081 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002082 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
2083 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002084 goto endFAIL;
2085 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002086 lp->sl_fidxs = ip;
2087 else if (round == 2)
2088 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002089 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002090 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002091
2092 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002093 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002094 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002095 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002096 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002097 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002098 }
2099 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002100
Bram Moolenaarb765d632005-06-07 21:00:02 +00002101 /* For a new file link it in the list of spell files. */
2102 if (old_lp == NULL)
2103 {
2104 lp->sl_next = first_lang;
2105 first_lang = lp;
2106 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002107
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002108 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002109
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002110endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002111 if (lang != NULL)
2112 /* truncating the name signals the error to spell_load_lang() */
2113 *lang = NUL;
2114 if (lp != NULL && old_lp == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002115 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002116 slang_free(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002117 lp = NULL;
2118 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002119
2120endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002121 if (fd != NULL)
2122 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002123 sourcing_name = save_sourcing_name;
2124 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002125
2126 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002127}
2128
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002129#ifdef FEAT_MBYTE
2130/*
2131 * Turn a multi-byte string into a wide character string.
2132 * Return it in allocated memory (NULL for out-of-memory)
2133 */
2134 static int *
2135mb_str2wide(s)
2136 char_u *s;
2137{
2138 int *res;
2139 char_u *p;
2140 int i = 0;
2141
2142 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
2143 if (res != NULL)
2144 {
2145 for (p = s; *p != NUL; )
2146 res[i++] = mb_ptr2char_adv(&p);
2147 res[i] = NUL;
2148 }
2149 return res;
2150}
2151#endif
2152
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002153/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002154 * Read one row of siblings from the spell file and store it in the byte array
2155 * "byts" and index array "idxs". Recursively read the children.
2156 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00002157 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00002158 *
2159 * Returns the index follosing the siblings.
2160 * Returns -1 if the file is shorter than expected.
2161 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002162 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002163 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002164read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002165 FILE *fd;
2166 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002167 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002168 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002169 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002170 int prefixtree; /* TRUE for reading PREFIXTREE */
2171 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002172{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002173 int len;
2174 int i;
2175 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002176 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002177 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002178 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002179#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002180
Bram Moolenaar51485f02005-06-04 21:55:20 +00002181 len = getc(fd); /* <siblingcount> */
2182 if (len <= 0)
2183 return -1;
2184
2185 if (startidx + len >= maxidx)
2186 return -2;
2187 byts[idx++] = len;
2188
2189 /* Read the byte values, flag/region bytes and shared indexes. */
2190 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002191 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002192 c = getc(fd); /* <byte> */
2193 if (c < 0)
2194 return -1;
2195 if (c <= BY_SPECIAL)
2196 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002197 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002198 {
2199 /* No flags, all regions. */
2200 idxs[idx] = 0;
2201 c = 0;
2202 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002203 else if (c == BY_FLAGS || c == BY_NOFLAGS)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002204 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002205 if (prefixtree)
2206 {
2207 /* Read the prefix ID and the condition nr. In idxs[]
2208 * store the prefix ID in the low byte, the condition
2209 * index shifted up 8 bits. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002210 c2 = getc(fd); /* <prefixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002211 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
2212 if (n >= maxprefcondnr)
2213 return -2;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002214 c2 += (n << 8);
2215 if (c == BY_NOFLAGS)
2216 c = c2;
2217 else
2218 c = c2 | WF_RAREPFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002219 }
2220 else
2221 {
2222 /* Read flags and optional region and prefix ID. In
2223 * idxs[] the flags go in the low byte, region above that
2224 * and prefix ID above the region. */
2225 c = getc(fd); /* <flags> */
2226 if (c & WF_REGION)
2227 c = (getc(fd) << 8) + c; /* <region> */
2228 if (c & WF_PFX)
2229 c = (getc(fd) << 16) + c; /* <prefixID> */
2230 }
2231
Bram Moolenaar51485f02005-06-04 21:55:20 +00002232 idxs[idx] = c;
2233 c = 0;
2234 }
2235 else /* c == BY_INDEX */
2236 {
2237 /* <nodeidx> */
2238 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2239 if (n < 0 || n >= maxidx)
2240 return -2;
2241 idxs[idx] = n + SHARED_MASK;
2242 c = getc(fd); /* <xbyte> */
2243 }
2244 }
2245 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002246 }
2247
Bram Moolenaar51485f02005-06-04 21:55:20 +00002248 /* Recursively read the children for non-shared siblings.
2249 * Skip the end-of-word ones (zero byte value) and the shared ones (and
2250 * remove SHARED_MASK) */
2251 for (i = 1; i <= len; ++i)
2252 if (byts[startidx + i] != 0)
2253 {
2254 if (idxs[startidx + i] & SHARED_MASK)
2255 idxs[startidx + i] &= ~SHARED_MASK;
2256 else
2257 {
2258 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002259 idx = read_tree(fd, byts, idxs, maxidx, idx,
2260 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002261 if (idx < 0)
2262 break;
2263 }
2264 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002265
Bram Moolenaar51485f02005-06-04 21:55:20 +00002266 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002267}
2268
2269/*
2270 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002271 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002272 */
2273 char_u *
2274did_set_spelllang(buf)
2275 buf_T *buf;
2276{
2277 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002278 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002279 char_u *region;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002280 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002281 int region_mask;
2282 slang_T *lp;
2283 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002284 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002285 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002286 int load_spf;
2287 int len;
2288 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002289
2290 ga_init2(&ga, sizeof(langp_T), 2);
2291
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002292 /* Make the name of the .spl file associated with 'spellfile'. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002293 if (*buf->b_p_spf == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002294 load_spf = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002295 else
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002296 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002297 vim_snprintf((char *)spf_name, sizeof(spf_name), "%s.spl",
2298 buf->b_p_spf);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002299 load_spf = TRUE;
2300 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002301
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002302 /* loop over comma separated language names. */
2303 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002304 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002305 /* Get one language name. */
2306 copy_option_part(&splp, lang, MAXWLEN, ",");
2307
Bram Moolenaar5482f332005-04-17 20:18:43 +00002308 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002309 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002310
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002311 /* If the name ends in ".spl" use it as the name of the spell file.
2312 * If there is a region name let "region" point to it and remove it
2313 * from the name. */
2314 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2315 {
2316 filename = TRUE;
2317
2318 /* Check if we loaded this language before. */
2319 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2320 if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME)
2321 break;
2322 }
2323 else
2324 {
2325 filename = FALSE;
2326 if (len > 3 && lang[len - 3] == '_')
2327 {
2328 region = lang + len - 2;
2329 len -= 3;
2330 lang[len] = NUL;
2331 }
2332
2333 /* Check if we loaded this language before. */
2334 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2335 if (STRICMP(lang, lp->sl_name) == 0)
2336 break;
2337 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002338
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002339 /* If not found try loading the language now. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002340 if (lp == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002341 {
2342 if (filename)
2343 (void)spell_load_file(lang, lang, NULL, FALSE);
2344 else
2345 spell_load_lang(lang);
2346 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002347
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002348 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002349 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002350 */
2351 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002352 if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME
2353 : STRICMP(lang, lp->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002354 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002355 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002356 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002357 {
2358 /* find region in sl_regions */
2359 c = find_region(lp->sl_regions, region);
2360 if (c == REGION_ALL)
2361 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002362 if (!lp->sl_add)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002363 smsg((char_u *)
2364 _("Warning: region %s not supported"),
2365 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002366 }
2367 else
2368 region_mask = 1 << c;
2369 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002370
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002371 if (ga_grow(&ga, 1) == FAIL)
2372 {
2373 ga_clear(&ga);
2374 return e_outofmem;
2375 }
2376 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2377 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2378 ++ga.ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002379
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002380 /* Check if this is the spell file related to 'spellfile'. */
2381 if (load_spf && fullpathcmp(spf_name, lp->sl_fname, FALSE)
2382 == FPC_SAME)
2383 load_spf = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002384 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002385 }
2386
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002387 /*
2388 * Make sure the 'spellfile' file is loaded. It may be in 'runtimepath',
2389 * then it's probably loaded above already. Otherwise load it here.
2390 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002391 if (load_spf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002392 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002393 /* Check if it was loaded already. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002394 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2395 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
2396 break;
2397 if (lp == NULL)
2398 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002399 /* Not loaded, try loading it now. The language name includes the
2400 * region name, the region is ignored otherwise. */
2401 vim_strncpy(lang, gettail(buf->b_p_spf), MAXWLEN);
2402 p = vim_strchr(lang, '.');
2403 if (p != NULL)
2404 *p = NUL; /* truncate at ".encoding.add" */
2405 lp = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002406 }
2407 if (lp != NULL && ga_grow(&ga, 1) == OK)
2408 {
2409 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2410 LANGP_ENTRY(ga, ga.ga_len)->lp_region = REGION_ALL;
2411 ++ga.ga_len;
2412 }
2413 }
2414
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002415 /* Add a NULL entry to mark the end of the list. */
2416 if (ga_grow(&ga, 1) == FAIL)
2417 {
2418 ga_clear(&ga);
2419 return e_outofmem;
2420 }
2421 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
2422 ++ga.ga_len;
2423
2424 /* Everything is fine, store the new b_langp value. */
2425 ga_clear(&buf->b_langp);
2426 buf->b_langp = ga;
2427
2428 return NULL;
2429}
2430
2431/*
2432 * Find the region "region[2]" in "rp" (points to "sl_regions").
2433 * Each region is simply stored as the two characters of it's name.
2434 * Returns the index if found, REGION_ALL if not found.
2435 */
2436 static int
2437find_region(rp, region)
2438 char_u *rp;
2439 char_u *region;
2440{
2441 int i;
2442
2443 for (i = 0; ; i += 2)
2444 {
2445 if (rp[i] == NUL)
2446 return REGION_ALL;
2447 if (rp[i] == region[0] && rp[i + 1] == region[1])
2448 break;
2449 }
2450 return i / 2;
2451}
2452
2453/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002454 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002455 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002456 * Word WF_ONECAP
2457 * W WORD WF_ALLCAP
2458 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002459 */
2460 static int
2461captype(word, end)
2462 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002463 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002464{
2465 char_u *p;
2466 int c;
2467 int firstcap;
2468 int allcap;
2469 int past_second = FALSE; /* past second word char */
2470
2471 /* find first letter */
Bram Moolenaarea408852005-06-25 22:49:46 +00002472 for (p = word; !spell_iswordp(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002473 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002474 return 0; /* only non-word characters, illegal word */
2475#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00002476 if (has_mbyte)
2477 c = mb_ptr2char_adv(&p);
2478 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002479#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00002480 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002481 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002482
2483 /*
2484 * Need to check all letters to find a word with mixed upper/lower.
2485 * But a word with an upper char only at start is a ONECAP.
2486 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002487 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaarea408852005-06-25 22:49:46 +00002488 if (spell_iswordp(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002489 {
2490#ifdef FEAT_MBYTE
2491 c = mb_ptr2char(p);
2492#else
2493 c = *p;
2494#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002495 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002496 {
2497 /* UUl -> KEEPCAP */
2498 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002499 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002500 allcap = FALSE;
2501 }
2502 else if (!allcap)
2503 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002504 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002505 past_second = TRUE;
2506 }
2507
2508 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002509 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002510 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002511 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002512 return 0;
2513}
2514
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002515# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
2516/*
2517 * Free all languages.
2518 */
2519 void
2520spell_free_all()
2521{
2522 slang_T *lp;
2523 buf_T *buf;
2524
2525 /* Go through all buffers and handle 'spelllang'. */
2526 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2527 ga_clear(&buf->b_langp);
2528
2529 while (first_lang != NULL)
2530 {
2531 lp = first_lang;
2532 first_lang = lp->sl_next;
2533 slang_free(lp);
2534 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002535
2536 init_spell_chartab();
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002537}
2538# endif
2539
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002540# if defined(FEAT_MBYTE) || defined(PROTO)
2541/*
2542 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002543 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002544 */
2545 void
2546spell_reload()
2547{
2548 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002549 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002550
Bram Moolenaarea408852005-06-25 22:49:46 +00002551 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002552 init_spell_chartab();
2553
2554 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002555 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002556
2557 /* Go through all buffers and handle 'spelllang'. */
2558 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2559 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002560 /* Only load the wordlists when 'spelllang' is set and there is a
2561 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002562 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002563 {
2564 FOR_ALL_WINDOWS(wp)
2565 if (wp->w_buffer == buf && wp->w_p_spell)
2566 {
2567 (void)did_set_spelllang(buf);
2568# ifdef FEAT_WINDOWS
2569 break;
2570# endif
2571 }
2572 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002573 }
2574}
2575# endif
2576
Bram Moolenaarb765d632005-06-07 21:00:02 +00002577/*
2578 * Reload the spell file "fname" if it's loaded.
2579 */
2580 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002581spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002582 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002583 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002584{
2585 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002586 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002587
Bram Moolenaarb765d632005-06-07 21:00:02 +00002588 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2589 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
2590 {
2591 slang_clear(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002592 (void)spell_load_file(fname, NULL, lp, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002593 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002594 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002595 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002596
2597 /* When "zg" was used and the file wasn't loaded yet, should redo
2598 * 'spelllang' to get it loaded. */
2599 if (added_word && !didit)
2600 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002601}
2602
2603
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002604/*
2605 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002606 */
2607
Bram Moolenaar51485f02005-06-04 21:55:20 +00002608#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002609 and .dic file. */
2610/*
2611 * Main structure to store the contents of a ".aff" file.
2612 */
2613typedef struct afffile_S
2614{
2615 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002616 int af_rar; /* RAR ID for rare word */
2617 int af_kep; /* KEP ID for keep-case word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00002618 int af_bad; /* BAD ID for banned word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002619 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002620 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
2621 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002622} afffile_T;
2623
2624typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002625/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
2626struct affentry_S
2627{
2628 affentry_T *ae_next; /* next affix with same name/number */
2629 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
2630 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002631 char_u *ae_cond; /* condition (NULL for ".") */
2632 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002633 int ae_rare; /* rare affix */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002634};
2635
2636/* Affix header from ".aff" file. Used for af_pref and af_suff. */
2637typedef struct affheader_S
2638{
2639 char_u ah_key[2]; /* key for hashtable == name of affix entry */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002640 int ah_newID; /* prefix ID after renumbering */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002641 int ah_combine; /* suffix may combine with prefix */
2642 affentry_T *ah_first; /* first affix entry */
2643} affheader_T;
2644
2645#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
2646
2647/*
2648 * Structure that is used to store the items in the word tree. This avoids
2649 * the need to keep track of each allocated thing, it's freed all at once
2650 * after ":mkspell" is done.
2651 */
2652#define SBLOCKSIZE 16000 /* size of sb_data */
2653typedef struct sblock_S sblock_T;
2654struct sblock_S
2655{
2656 sblock_T *sb_next; /* next block in list */
2657 int sb_used; /* nr of bytes already in use */
2658 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002659};
2660
2661/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002662 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002663 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002664typedef struct wordnode_S wordnode_T;
2665struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002666{
Bram Moolenaar0c405862005-06-22 22:26:26 +00002667 union /* shared to save space */
2668 {
2669 char_u hashkey[6]; /* room for the hash key */
2670 int index; /* index in written nodes (valid after first
2671 round) */
2672 } wn_u1;
2673 union /* shared to save space */
2674 {
2675 wordnode_T *next; /* next node with same hash key */
2676 wordnode_T *wnode; /* parent node that will write this node */
2677 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002678 wordnode_T *wn_child; /* child (next byte in word) */
2679 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
2680 always sorted) */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002681 char_u wn_byte; /* Byte for this node. NUL for word end */
2682 char_u wn_flags; /* when wn_byte is NUL: WF_ flags */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002683 short wn_region; /* when wn_byte is NUL: region mask; for
2684 PREFIXTREE it's the prefcondnr */
2685 char_u wn_prefixID; /* supported/required prefix ID or 0 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002686};
2687
Bram Moolenaar51485f02005-06-04 21:55:20 +00002688#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002689
Bram Moolenaar51485f02005-06-04 21:55:20 +00002690/*
2691 * Info used while reading the spell files.
2692 */
2693typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002694{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002695 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00002696 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002697 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00002698 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002699 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002700 sblock_T *si_blocks; /* memory blocks used */
2701 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002702 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002703 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002704 int si_region; /* region mask */
2705 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00002706 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002707 int si_verbose; /* verbose messages */
Bram Moolenaar3982c542005-06-08 21:56:31 +00002708 int si_region_count; /* number of regions supported (1 when there
2709 are no regions) */
2710 char_u si_region_name[16]; /* region names (if count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002711
2712 garray_T si_rep; /* list of fromto_T entries from REP lines */
2713 garray_T si_sal; /* list of fromto_T entries from SAL lines */
2714 int si_followup; /* soundsalike: ? */
2715 int si_collapse; /* soundsalike: ? */
2716 int si_rem_accents; /* soundsalike: remove accents */
2717 garray_T si_map; /* MAP info concatenated */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002718 char_u *si_midword; /* MIDWORD chars, alloc'ed string or NULL */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002719 garray_T si_prefcond; /* table with conditions for postponed
2720 * prefixes, each stored as a string */
2721 int si_newID; /* current value for ah_newID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002722} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002723
Bram Moolenaar51485f02005-06-04 21:55:20 +00002724static afffile_T *spell_read_aff __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002725static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002726static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
2727static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00002728static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00002729static void spell_free_aff __ARGS((afffile_T *aff));
2730static int spell_read_dic __ARGS((char_u *fname, spellinfo_T *spin, afffile_T *affile));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002731static char_u *get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, sblock_T **blp));
2732static 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 +00002733static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin));
2734static void *getroom __ARGS((sblock_T **blp, size_t len));
2735static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s));
2736static void free_blocks __ARGS((sblock_T *bl));
2737static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002738static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags, int region, char_u *pfxlist));
2739static 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 +00002740static void wordtree_compress __ARGS((wordnode_T *root, spellinfo_T *spin));
Bram Moolenaar51485f02005-06-04 21:55:20 +00002741static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot));
2742static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar3982c542005-06-08 21:56:31 +00002743static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar0c405862005-06-22 22:26:26 +00002744static void clear_node __ARGS((wordnode_T *node));
2745static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002746static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00002747static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002748
2749/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002750 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00002751 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002752 */
2753 static afffile_T *
Bram Moolenaar51485f02005-06-04 21:55:20 +00002754spell_read_aff(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002755 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002756 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002757{
2758 FILE *fd;
2759 afffile_T *aff;
2760 char_u rline[MAXLINELEN];
2761 char_u *line;
2762 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00002763#define MAXITEMCNT 7
2764 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002765 int itemcnt;
2766 char_u *p;
2767 int lnum = 0;
2768 affheader_T *cur_aff = NULL;
2769 int aff_todo = 0;
2770 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002771 char_u *low = NULL;
2772 char_u *fol = NULL;
2773 char_u *upp = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002774 static char *e_affname = N_("Affix name too long in %s line %d: %s");
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002775 int do_rep;
2776 int do_sal;
2777 int do_map;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002778 int do_midword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002779 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002780 hashitem_T *hi;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002781
Bram Moolenaar51485f02005-06-04 21:55:20 +00002782 /*
2783 * Open the file.
2784 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002785 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002786 if (fd == NULL)
2787 {
2788 EMSG2(_(e_notopen), fname);
2789 return NULL;
2790 }
2791
Bram Moolenaarb765d632005-06-07 21:00:02 +00002792 if (spin->si_verbose || p_verbose > 2)
2793 {
2794 if (!spin->si_verbose)
2795 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002796 smsg((char_u *)_("Reading affix file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002797 out_flush();
2798 if (!spin->si_verbose)
2799 verbose_leave();
2800 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002801
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002802 /* Only do REP lines when not done in another .aff file already. */
2803 do_rep = spin->si_rep.ga_len == 0;
2804
2805 /* Only do SAL lines when not done in another .aff file already. */
2806 do_sal = spin->si_sal.ga_len == 0;
2807
2808 /* Only do MAP lines when not done in another .aff file already. */
2809 do_map = spin->si_map.ga_len == 0;
2810
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002811 /* Only do MIDWORD line when not done in another .aff file already */
2812 do_midword = spin->si_midword == NULL;
2813
Bram Moolenaar51485f02005-06-04 21:55:20 +00002814 /*
2815 * Allocate and init the afffile_T structure.
2816 */
2817 aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002818 if (aff == NULL)
2819 return NULL;
2820 hash_init(&aff->af_pref);
2821 hash_init(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002822
2823 /*
2824 * Read all the lines in the file one by one.
2825 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002826 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002827 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002828 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002829 ++lnum;
2830
2831 /* Skip comment lines. */
2832 if (*rline == '#')
2833 continue;
2834
2835 /* Convert from "SET" to 'encoding' when needed. */
2836 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002837#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00002838 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002839 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002840 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002841 if (pc == NULL)
2842 {
2843 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
2844 fname, lnum, rline);
2845 continue;
2846 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002847 line = pc;
2848 }
2849 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00002850#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002851 {
2852 pc = NULL;
2853 line = rline;
2854 }
2855
2856 /* Split the line up in white separated items. Put a NUL after each
2857 * item. */
2858 itemcnt = 0;
2859 for (p = line; ; )
2860 {
2861 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
2862 ++p;
2863 if (*p == NUL)
2864 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00002865 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002866 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002867 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002868 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002869 ++p;
2870 if (*p == NUL)
2871 break;
2872 *p++ = NUL;
2873 }
2874
2875 /* Handle non-empty lines. */
2876 if (itemcnt > 0)
2877 {
2878 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
2879 && aff->af_enc == NULL)
2880 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00002881#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00002882 /* Setup for conversion from "ENC" to 'encoding'. */
2883 aff->af_enc = enc_canonize(items[1]);
2884 if (aff->af_enc != NULL && !spin->si_ascii
2885 && convert_setup(&spin->si_conv, aff->af_enc,
2886 p_enc) == FAIL)
2887 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
2888 fname, aff->af_enc, p_enc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002889#else
2890 smsg((char_u *)_("Conversion in %s not supported"), fname);
2891#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002892 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002893 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2)
2894 {
2895 if (do_midword)
2896 spin->si_midword = vim_strsave(items[1]);
2897 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00002898 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
2899 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002900 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00002901 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002902 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002903 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002904 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002905 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002906 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
2907 && aff->af_rar == 0)
2908 {
2909 aff->af_rar = items[1][0];
2910 if (items[1][1] != NUL)
2911 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
2912 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002913 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
2914 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002915 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00002916 aff->af_kep = items[1][0];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002917 if (items[1][1] != NUL)
2918 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
2919 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00002920 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
2921 && aff->af_bad == 0)
2922 {
2923 aff->af_bad = items[1][0];
2924 if (items[1][1] != NUL)
2925 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
2926 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002927 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
2928 {
2929 aff->af_pfxpostpone = TRUE;
2930 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002931 else if ((STRCMP(items[0], "PFX") == 0
2932 || STRCMP(items[0], "SFX") == 0)
2933 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00002934 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002935 {
Bram Moolenaar8db73182005-06-17 21:51:16 +00002936 /* Myspell allows extra text after the item, but that might
2937 * mean mistakes go unnoticed. Require a comment-starter. */
2938 if (itemcnt > 4 && *items[4] != '#')
2939 smsg((char_u *)_("Trailing text in %s line %d: %s"),
2940 fname, lnum, items[4]);
2941
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002942 /* New affix letter. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002943 cur_aff = (affheader_T *)getroom(&spin->si_blocks,
2944 sizeof(affheader_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002945 if (cur_aff == NULL)
2946 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002947 cur_aff->ah_key[0] = *items[1]; /* TODO: multi-byte? */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002948 cur_aff->ah_key[1] = NUL;
2949 if (items[1][1] != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002950 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002951 if (*items[2] == 'Y')
2952 cur_aff->ah_combine = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002953 else if (*items[2] != 'N')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002954 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
2955 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002956
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002957 if (*items[0] == 'P')
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002958 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002959 tp = &aff->af_pref;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002960 /* Use a new number in the .spl file later, to be able to
2961 * handle multiple .aff files. */
2962 if (aff->af_pfxpostpone)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002963 cur_aff->ah_newID = ++spin->si_newID;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002964 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002965 else
2966 tp = &aff->af_suff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002967 aff_todo = atoi((char *)items[3]);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002968 hi = hash_find(tp, cur_aff->ah_key);
2969 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar51485f02005-06-04 21:55:20 +00002970 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002971 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
2972 fname, lnum, items[1]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002973 aff_todo = 0;
2974 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002975 else
2976 hash_add(tp, cur_aff->ah_key);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002977 }
2978 else if ((STRCMP(items[0], "PFX") == 0
2979 || STRCMP(items[0], "SFX") == 0)
2980 && aff_todo > 0
2981 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00002982 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002983 {
2984 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002985 int rare = FALSE;
2986 int lasti = 5;
2987
2988 /* Check for "rare" after the other info. */
2989 if (itemcnt > 5 && STRICMP(items[5], "rare") == 0)
2990 {
2991 rare = TRUE;
2992 lasti = 6;
2993 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002994
Bram Moolenaar8db73182005-06-17 21:51:16 +00002995 /* Myspell allows extra text after the item, but that might
2996 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002997 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar8db73182005-06-17 21:51:16 +00002998 smsg((char_u *)_("Trailing text in %s line %d: %s"),
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002999 fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00003000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003001 /* New item for an affix letter. */
3002 --aff_todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003003 aff_entry = (affentry_T *)getroom(&spin->si_blocks,
3004 sizeof(affentry_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003005 if (aff_entry == NULL)
3006 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003007 aff_entry->ae_rare = rare;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003008
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003009 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003010 aff_entry->ae_chop = getroom_save(&spin->si_blocks,
3011 items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003012 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003013 aff_entry->ae_add = getroom_save(&spin->si_blocks,
3014 items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003015
Bram Moolenaar51485f02005-06-04 21:55:20 +00003016 /* Don't use an affix entry with non-ASCII characters when
3017 * "spin->si_ascii" is TRUE. */
3018 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00003019 || has_non_ascii(aff_entry->ae_add)))
3020 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003021 aff_entry->ae_next = cur_aff->ah_first;
3022 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003023
3024 if (STRCMP(items[4], ".") != 0)
3025 {
3026 char_u buf[MAXLINELEN];
3027
3028 aff_entry->ae_cond = getroom_save(&spin->si_blocks,
3029 items[4]);
3030 if (*items[0] == 'P')
3031 sprintf((char *)buf, "^%s", items[4]);
3032 else
3033 sprintf((char *)buf, "%s$", items[4]);
3034 aff_entry->ae_prog = vim_regcomp(buf,
3035 RE_MAGIC + RE_STRING);
3036 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003037
3038 /* For postponed prefixes we need an entry in si_prefcond
3039 * for the condition. Use an existing one if possible. */
3040 if (*items[0] == 'P' && aff->af_pfxpostpone
3041 && aff_entry->ae_chop == NULL)
3042 {
3043 int idx;
3044 char_u **pp;
3045
3046 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
3047 --idx)
3048 {
3049 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
3050 if (str_equal(p, aff_entry->ae_cond))
3051 break;
3052 }
3053 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
3054 {
3055 /* Not found, add a new condition. */
3056 idx = spin->si_prefcond.ga_len++;
3057 pp = ((char_u **)spin->si_prefcond.ga_data) + idx;
3058 if (aff_entry->ae_cond == NULL)
3059 *pp = NULL;
3060 else
3061 *pp = getroom_save(&spin->si_blocks,
3062 aff_entry->ae_cond);
3063 }
3064
3065 /* Add the prefix to the prefix tree. */
3066 if (aff_entry->ae_add == NULL)
3067 p = (char_u *)"";
3068 else
3069 p = aff_entry->ae_add;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003070 tree_add_word(p, spin->si_prefroot, rare ? -2 : -1,
3071 idx, cur_aff->ah_newID, &spin->si_blocks);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003072 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003073 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003074 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003075 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
3076 {
3077 if (fol != NULL)
3078 smsg((char_u *)_("Duplicate FOL in %s line %d"),
3079 fname, lnum);
3080 else
3081 fol = vim_strsave(items[1]);
3082 }
3083 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
3084 {
3085 if (low != NULL)
3086 smsg((char_u *)_("Duplicate LOW in %s line %d"),
3087 fname, lnum);
3088 else
3089 low = vim_strsave(items[1]);
3090 }
3091 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
3092 {
3093 if (upp != NULL)
3094 smsg((char_u *)_("Duplicate UPP in %s line %d"),
3095 fname, lnum);
3096 else
3097 upp = vim_strsave(items[1]);
3098 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003099 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003100 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003101 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003102 if (!isdigit(*items[1]))
3103 smsg((char_u *)_("Expected REP count in %s line %d"),
3104 fname, lnum);
3105 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003106 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
3107 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003108 /* REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003109 if (do_rep)
3110 add_fromto(spin, &spin->si_rep, items[1], items[2]);
3111 }
3112 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
3113 {
3114 /* MAP item or count */
3115 if (!found_map)
3116 {
3117 /* First line contains the count. */
3118 found_map = TRUE;
3119 if (!isdigit(*items[1]))
3120 smsg((char_u *)_("Expected MAP count in %s line %d"),
3121 fname, lnum);
3122 }
3123 else if (do_map)
3124 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003125 int c;
3126
3127 /* Check that every character appears only once. */
3128 for (p = items[1]; *p != NUL; )
3129 {
3130#ifdef FEAT_MBYTE
3131 c = mb_ptr2char_adv(&p);
3132#else
3133 c = *p++;
3134#endif
3135 if ((spin->si_map.ga_len > 0
3136 && vim_strchr(spin->si_map.ga_data, c)
3137 != NULL)
3138 || vim_strchr(p, c) != NULL)
3139 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
3140 fname, lnum);
3141 }
3142
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003143 /* We simply concatenate all the MAP strings, separated by
3144 * slashes. */
3145 ga_concat(&spin->si_map, items[1]);
3146 ga_append(&spin->si_map, '/');
3147 }
3148 }
3149 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
3150 {
3151 if (do_sal)
3152 {
3153 /* SAL item (sounds-a-like)
3154 * Either one of the known keys or a from-to pair. */
3155 if (STRCMP(items[1], "followup") == 0)
3156 spin->si_followup = sal_to_bool(items[2]);
3157 else if (STRCMP(items[1], "collapse_result") == 0)
3158 spin->si_collapse = sal_to_bool(items[2]);
3159 else if (STRCMP(items[1], "remove_accents") == 0)
3160 spin->si_rem_accents = sal_to_bool(items[2]);
3161 else
3162 /* when "to" is "_" it means empty */
3163 add_fromto(spin, &spin->si_sal, items[1],
3164 STRCMP(items[2], "_") == 0 ? (char_u *)""
3165 : items[2]);
3166 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003167 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003168 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003169 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
3170 fname, lnum, items[0]);
3171 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003172 }
3173
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003174 if (fol != NULL || low != NULL || upp != NULL)
3175 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003176 if (spin->si_clear_chartab)
3177 {
3178 /* Clear the char type tables, don't want to use any of the
3179 * currently used spell properties. */
3180 init_spell_chartab();
3181 spin->si_clear_chartab = FALSE;
3182 }
3183
Bram Moolenaar3982c542005-06-08 21:56:31 +00003184 /*
3185 * Don't write a word table for an ASCII file, so that we don't check
3186 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003187 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00003188 * mb_get_class(), the list of chars in the file will be incomplete.
3189 */
3190 if (!spin->si_ascii
3191#ifdef FEAT_MBYTE
3192 && !enc_utf8
3193#endif
3194 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003195 {
3196 if (fol == NULL || low == NULL || upp == NULL)
3197 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
3198 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00003199 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003200 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003201
3202 vim_free(fol);
3203 vim_free(low);
3204 vim_free(upp);
3205 }
3206
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003207 vim_free(pc);
3208 fclose(fd);
3209 return aff;
3210}
3211
3212/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003213 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
3214 * NULL as equal.
3215 */
3216 static int
3217str_equal(s1, s2)
3218 char_u *s1;
3219 char_u *s2;
3220{
3221 if (s1 == NULL || s2 == NULL)
3222 return s1 == s2;
3223 return STRCMP(s1, s2) == 0;
3224}
3225
3226/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003227 * Add a from-to item to "gap". Used for REP and SAL items.
3228 * They are stored case-folded.
3229 */
3230 static void
3231add_fromto(spin, gap, from, to)
3232 spellinfo_T *spin;
3233 garray_T *gap;
3234 char_u *from;
3235 char_u *to;
3236{
3237 fromto_T *ftp;
3238 char_u word[MAXWLEN];
3239
3240 if (ga_grow(gap, 1) == OK)
3241 {
3242 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
3243 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
3244 ftp->ft_from = getroom_save(&spin->si_blocks, word);
3245 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
3246 ftp->ft_to = getroom_save(&spin->si_blocks, word);
3247 ++gap->ga_len;
3248 }
3249}
3250
3251/*
3252 * Convert a boolean argument in a SAL line to TRUE or FALSE;
3253 */
3254 static int
3255sal_to_bool(s)
3256 char_u *s;
3257{
3258 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
3259}
3260
3261/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00003262 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
3263 * When "s" is NULL FALSE is returned.
3264 */
3265 static int
3266has_non_ascii(s)
3267 char_u *s;
3268{
3269 char_u *p;
3270
3271 if (s != NULL)
3272 for (p = s; *p != NUL; ++p)
3273 if (*p >= 128)
3274 return TRUE;
3275 return FALSE;
3276}
3277
3278/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003279 * Free the structure filled by spell_read_aff().
3280 */
3281 static void
3282spell_free_aff(aff)
3283 afffile_T *aff;
3284{
3285 hashtab_T *ht;
3286 hashitem_T *hi;
3287 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003288 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003289 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003290
3291 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003292
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003293 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003294 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
3295 {
3296 todo = ht->ht_used;
3297 for (hi = ht->ht_array; todo > 0; ++hi)
3298 {
3299 if (!HASHITEM_EMPTY(hi))
3300 {
3301 --todo;
3302 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003303 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3304 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003305 }
3306 }
3307 if (ht == &aff->af_suff)
3308 break;
3309 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003310
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003311 hash_clear(&aff->af_pref);
3312 hash_clear(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003313}
3314
3315/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003316 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003317 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003318 */
3319 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00003320spell_read_dic(fname, spin, affile)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003321 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003322 spellinfo_T *spin;
3323 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003324{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003325 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003326 char_u line[MAXLINELEN];
Bram Moolenaar51485f02005-06-04 21:55:20 +00003327 char_u *afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003328 char_u *pfxlist;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003329 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003330 char_u *pc;
3331 char_u *w;
3332 int l;
3333 hash_T hash;
3334 hashitem_T *hi;
3335 FILE *fd;
3336 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003337 int non_ascii = 0;
3338 int retval = OK;
3339 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003340 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003341
Bram Moolenaar51485f02005-06-04 21:55:20 +00003342 /*
3343 * Open the file.
3344 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003345 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003346 if (fd == NULL)
3347 {
3348 EMSG2(_(e_notopen), fname);
3349 return FAIL;
3350 }
3351
Bram Moolenaar51485f02005-06-04 21:55:20 +00003352 /* The hashtable is only used to detect duplicated words. */
3353 hash_init(&ht);
3354
Bram Moolenaar8db73182005-06-17 21:51:16 +00003355 spin->si_foldwcount = 0;
3356 spin->si_keepwcount = 0;
3357
Bram Moolenaarb765d632005-06-07 21:00:02 +00003358 if (spin->si_verbose || p_verbose > 2)
3359 {
3360 if (!spin->si_verbose)
3361 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003362 smsg((char_u *)_("Reading dictionary file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003363 out_flush();
3364 if (!spin->si_verbose)
3365 verbose_leave();
3366 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003367
3368 /* Read and ignore the first line: word count. */
3369 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003370 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003371 EMSG2(_("E760: No word count in %s"), fname);
3372
3373 /*
3374 * Read all the lines in the file one by one.
3375 * The words are converted to 'encoding' here, before being added to
3376 * the hashtable.
3377 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003378 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003379 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003380 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003381 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003382 if (line[0] == '#')
3383 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003384
Bram Moolenaar51485f02005-06-04 21:55:20 +00003385 /* Remove CR, LF and white space from the end. White space halfway
3386 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003387 l = STRLEN(line);
3388 while (l > 0 && line[l - 1] <= ' ')
3389 --l;
3390 if (l == 0)
3391 continue; /* empty line */
3392 line[l] = NUL;
3393
Bram Moolenaar51485f02005-06-04 21:55:20 +00003394 /* Find the optional affix names. */
3395 afflist = vim_strchr(line, '/');
3396 if (afflist != NULL)
3397 *afflist++ = NUL;
3398
3399 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
3400 if (spin->si_ascii && has_non_ascii(line))
3401 {
3402 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003403 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003404 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003405
Bram Moolenaarb765d632005-06-07 21:00:02 +00003406#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003407 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003408 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003409 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003410 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003411 if (pc == NULL)
3412 {
3413 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3414 fname, lnum, line);
3415 continue;
3416 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003417 w = pc;
3418 }
3419 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003420#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003421 {
3422 pc = NULL;
3423 w = line;
3424 }
3425
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003426 /* This takes time, print a message now and then. */
3427 if (spin->si_verbose && (lnum & 0x3ff) == 0)
3428 {
3429 vim_snprintf((char *)message, sizeof(message),
3430 _("line %6d, word %6d - %s"),
3431 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
3432 msg_start();
3433 msg_puts_long_attr(message, 0);
3434 msg_clr_eos();
3435 msg_didout = FALSE;
3436 msg_col = 0;
3437 out_flush();
3438 }
3439
Bram Moolenaar51485f02005-06-04 21:55:20 +00003440 /* Store the word in the hashtable to be able to find duplicates. */
3441 dw = (char_u *)getroom_save(&spin->si_blocks, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003442 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003443 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003444 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003445 if (retval == FAIL)
3446 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003447
Bram Moolenaar51485f02005-06-04 21:55:20 +00003448 hash = hash_hash(dw);
3449 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003450 if (!HASHITEM_EMPTY(hi))
3451 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003452 fname, lnum, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003453 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00003454 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003455
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003456 flags = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003457 pfxlist = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003458 if (afflist != NULL)
3459 {
3460 /* Check for affix name that stands for keep-case word and stands
3461 * for rare word (if defined). */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003462 if (affile->af_kep != NUL
3463 && vim_strchr(afflist, affile->af_kep) != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003464 flags |= WF_KEEPCAP;
3465 if (affile->af_rar != NUL
3466 && vim_strchr(afflist, affile->af_rar) != NULL)
3467 flags |= WF_RARE;
Bram Moolenaar0c405862005-06-22 22:26:26 +00003468 if (affile->af_bad != NUL
3469 && vim_strchr(afflist, affile->af_bad) != NULL)
3470 flags |= WF_BANNED;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003471
3472 if (affile->af_pfxpostpone)
3473 /* Need to store the list of prefix IDs with the word. */
3474 pfxlist = get_pfxlist(affile, afflist, &spin->si_blocks);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003475 }
3476
Bram Moolenaar51485f02005-06-04 21:55:20 +00003477 /* Add the word to the word tree(s). */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003478 if (store_word(dw, spin, flags, spin->si_region, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003479 retval = FAIL;
3480
3481 if (afflist != NULL)
3482 {
3483 /* Find all matching suffixes and add the resulting words.
3484 * Additionally do matching prefixes that combine. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003485 if (store_aff_word(dw, spin, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003486 &affile->af_suff, &affile->af_pref,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003487 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003488 retval = FAIL;
3489
3490 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003491 if (store_aff_word(dw, spin, afflist, affile,
3492 &affile->af_pref, NULL,
3493 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003494 retval = FAIL;
3495 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003496 }
3497
Bram Moolenaar51485f02005-06-04 21:55:20 +00003498 if (spin->si_ascii && non_ascii > 0)
3499 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
3500 non_ascii);
3501 hash_clear(&ht);
3502
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003503 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003504 return retval;
3505}
3506
3507/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003508 * Get the list of prefix IDs from the affix list "afflist".
3509 * Used for PFXPOSTPONE.
3510 * Returns a string allocated with getroom(). NULL when there are no prefixes
3511 * or when out of memory.
3512 */
3513 static char_u *
3514get_pfxlist(affile, afflist, blp)
3515 afffile_T *affile;
3516 char_u *afflist;
3517 sblock_T **blp;
3518{
3519 char_u *p;
3520 int cnt;
3521 int round;
3522 char_u *res = NULL;
3523 char_u key[2];
3524 hashitem_T *hi;
3525
3526 key[1] = NUL;
3527
3528 /* round 1: count the number of prefix IDs.
3529 * round 2: move prefix IDs to "res" */
3530 for (round = 1; round <= 2; ++round)
3531 {
3532 cnt = 0;
3533 for (p = afflist; *p != NUL; ++p)
3534 {
3535 key[0] = *p;
3536 hi = hash_find(&affile->af_pref, key);
3537 if (!HASHITEM_EMPTY(hi))
3538 {
3539 /* This is a prefix ID, use the new number. */
3540 if (round == 2)
3541 res[cnt] = HI2AH(hi)->ah_newID;
3542 ++cnt;
3543 }
3544 }
3545 if (round == 1 && cnt > 0)
3546 res = getroom(blp, cnt + 1);
3547 if (res == NULL)
3548 break;
3549 }
3550
3551 if (res != NULL)
3552 res[cnt] = NUL;
3553 return res;
3554}
3555
3556/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003557 * Apply affixes to a word and store the resulting words.
3558 * "ht" is the hashtable with affentry_T that need to be applied, either
3559 * prefixes or suffixes.
3560 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
3561 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003562 *
3563 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003564 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003565 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003566store_aff_word(word, spin, afflist, affile, ht, xht, comb, flags, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003567 char_u *word; /* basic word start */
3568 spellinfo_T *spin; /* spell info */
3569 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003570 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003571 hashtab_T *ht;
3572 hashtab_T *xht;
3573 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003574 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003575 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003576{
3577 int todo;
3578 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003579 affheader_T *ah;
3580 affentry_T *ae;
3581 regmatch_T regmatch;
3582 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003583 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003584 int i;
3585 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003586 int use_flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003587
Bram Moolenaar51485f02005-06-04 21:55:20 +00003588 todo = ht->ht_used;
3589 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003590 {
3591 if (!HASHITEM_EMPTY(hi))
3592 {
3593 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003594 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00003595
Bram Moolenaar51485f02005-06-04 21:55:20 +00003596 /* Check that the affix combines, if required, and that the word
3597 * supports this affix. */
3598 if ((!comb || ah->ah_combine)
3599 && vim_strchr(afflist, *ah->ah_key) != NULL)
Bram Moolenaar5482f332005-04-17 20:18:43 +00003600 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003601 /* Loop over all affix entries with this name. */
3602 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003603 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003604 /* Check the condition. It's not logical to match case
3605 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003606 * Myspell.
3607 * For prefixes, when "PFXPOSTPONE" was used, only do
3608 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003609 regmatch.regprog = ae->ae_prog;
3610 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003611 if ((xht != NULL || !affile->af_pfxpostpone
3612 || ae->ae_chop != NULL)
3613 && (ae->ae_prog == NULL
3614 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003615 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003616 /* Match. Remove the chop and add the affix. */
3617 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003618 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003619 /* prefix: chop/add at the start of the word */
3620 if (ae->ae_add == NULL)
3621 *newword = NUL;
3622 else
3623 STRCPY(newword, ae->ae_add);
3624 p = word;
3625 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003626 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003627 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003628#ifdef FEAT_MBYTE
3629 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003630 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003631 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003632 for ( ; i > 0; --i)
3633 mb_ptr_adv(p);
3634 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00003635 else
3636#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003637 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003638 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003639 STRCAT(newword, p);
3640 }
3641 else
3642 {
3643 /* suffix: chop/add at the end of the word */
3644 STRCPY(newword, word);
3645 if (ae->ae_chop != NULL)
3646 {
3647 /* Remove chop string. */
3648 p = newword + STRLEN(newword);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003649#ifdef FEAT_MBYTE
3650 if (has_mbyte)
3651 i = mb_charlen(ae->ae_chop);
3652 else
3653#endif
3654 i = STRLEN(ae->ae_chop);
3655 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003656 mb_ptr_back(newword, p);
3657 *p = NUL;
3658 }
3659 if (ae->ae_add != NULL)
3660 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003661 }
3662
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003663 /* Obey the "rare" flag of the affix. */
3664 if (ae->ae_rare)
3665 use_flags = flags | WF_RARE;
3666 else
3667 use_flags = flags;
3668
Bram Moolenaar51485f02005-06-04 21:55:20 +00003669 /* Store the modified word. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003670 if (store_word(newword, spin, use_flags,
3671 spin->si_region, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003672 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003673
Bram Moolenaar51485f02005-06-04 21:55:20 +00003674 /* When added a suffix and combining is allowed also
3675 * try adding prefixes additionally. */
3676 if (xht != NULL && ah->ah_combine)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003677 if (store_aff_word(newword, spin, afflist, affile,
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003678 xht, NULL, TRUE, use_flags, pfxlist)
3679 == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003680 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003681 }
3682 }
3683 }
3684 }
3685 }
3686
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003687 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003688}
3689
3690/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003691 * Read a file with a list of words.
3692 */
3693 static int
3694spell_read_wordfile(fname, spin)
3695 char_u *fname;
3696 spellinfo_T *spin;
3697{
3698 FILE *fd;
3699 long lnum = 0;
3700 char_u rline[MAXLINELEN];
3701 char_u *line;
3702 char_u *pc = NULL;
3703 int l;
3704 int retval = OK;
3705 int did_word = FALSE;
3706 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003707 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003708 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003709
3710 /*
3711 * Open the file.
3712 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003713 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00003714 if (fd == NULL)
3715 {
3716 EMSG2(_(e_notopen), fname);
3717 return FAIL;
3718 }
3719
Bram Moolenaarb765d632005-06-07 21:00:02 +00003720 if (spin->si_verbose || p_verbose > 2)
3721 {
3722 if (!spin->si_verbose)
3723 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003724 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003725 out_flush();
3726 if (!spin->si_verbose)
3727 verbose_leave();
3728 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003729
3730 /*
3731 * Read all the lines in the file one by one.
3732 */
3733 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
3734 {
3735 line_breakcheck();
3736 ++lnum;
3737
3738 /* Skip comment lines. */
3739 if (*rline == '#')
3740 continue;
3741
3742 /* Remove CR, LF and white space from the end. */
3743 l = STRLEN(rline);
3744 while (l > 0 && rline[l - 1] <= ' ')
3745 --l;
3746 if (l == 0)
3747 continue; /* empty or blank line */
3748 rline[l] = NUL;
3749
3750 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
3751 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003752#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003753 if (spin->si_conv.vc_type != CONV_NONE)
3754 {
3755 pc = string_convert(&spin->si_conv, rline, NULL);
3756 if (pc == NULL)
3757 {
3758 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3759 fname, lnum, rline);
3760 continue;
3761 }
3762 line = pc;
3763 }
3764 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003765#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00003766 {
3767 pc = NULL;
3768 line = rline;
3769 }
3770
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003771 flags = 0;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003772 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003773
3774 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00003775 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003776 ++line;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003777
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003778 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003779 {
3780 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00003781 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
3782 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003783 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00003784 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
3785 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003786 else
3787 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003788#ifdef FEAT_MBYTE
3789 char_u *enc;
3790
Bram Moolenaar51485f02005-06-04 21:55:20 +00003791 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003792 line += 10;
3793 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003794 if (enc != NULL && !spin->si_ascii
3795 && convert_setup(&spin->si_conv, enc,
3796 p_enc) == FAIL)
3797 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00003798 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003799 vim_free(enc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003800#else
3801 smsg((char_u *)_("Conversion in %s not supported"), fname);
3802#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00003803 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003804 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003805 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003806
Bram Moolenaar3982c542005-06-08 21:56:31 +00003807 if (STRNCMP(line, "regions=", 8) == 0)
3808 {
3809 if (spin->si_region_count > 1)
3810 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
3811 fname, lnum, line);
3812 else
3813 {
3814 line += 8;
3815 if (STRLEN(line) > 16)
3816 smsg((char_u *)_("Too many regions in %s line %d: %s"),
3817 fname, lnum, line);
3818 else
3819 {
3820 spin->si_region_count = STRLEN(line) / 2;
3821 STRCPY(spin->si_region_name, line);
3822 }
3823 }
3824 continue;
3825 }
3826
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003827 if (*line == '=')
3828 {
3829 /* keep-case word */
3830 flags |= WF_KEEPCAP;
3831 ++line;
3832 }
3833
3834 if (*line == '!')
3835 {
3836 /* Bad, bad, wicked word. */
3837 flags |= WF_BANNED;
3838 ++line;
3839 }
3840 else if (*line == '?')
3841 {
3842 /* Rare word. */
3843 flags |= WF_RARE;
3844 ++line;
3845 }
3846
Bram Moolenaar3982c542005-06-08 21:56:31 +00003847 if (VIM_ISDIGIT(*line))
3848 {
3849 /* region number(s) */
3850 regionmask = 0;
3851 while (VIM_ISDIGIT(*line))
3852 {
3853 l = *line - '0';
3854 if (l > spin->si_region_count)
3855 {
3856 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
3857 fname, lnum, line);
3858 break;
3859 }
3860 regionmask |= 1 << (l - 1);
3861 ++line;
3862 }
3863 flags |= WF_REGION;
3864 }
3865
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003866 if (flags == 0)
3867 {
3868 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
Bram Moolenaar51485f02005-06-04 21:55:20 +00003869 fname, lnum, line);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003870 continue;
3871 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003872 }
3873
3874 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
3875 if (spin->si_ascii && has_non_ascii(line))
3876 {
3877 ++non_ascii;
3878 continue;
3879 }
3880
3881 /* Normal word: store it. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003882 if (store_word(line, spin, flags, regionmask, NULL) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003883 {
3884 retval = FAIL;
3885 break;
3886 }
3887 did_word = TRUE;
3888 }
3889
3890 vim_free(pc);
3891 fclose(fd);
3892
Bram Moolenaarb765d632005-06-07 21:00:02 +00003893 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
3894 {
3895 if (p_verbose > 2)
3896 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00003897 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
3898 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003899 if (p_verbose > 2)
3900 verbose_leave();
3901 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003902 return retval;
3903}
3904
3905/*
3906 * Get part of an sblock_T, "len" bytes long.
3907 * This avoids calling free() for every little struct we use.
3908 * The memory is cleared to all zeros.
3909 * Returns NULL when out of memory.
3910 */
3911 static void *
3912getroom(blp, len)
3913 sblock_T **blp;
3914 size_t len; /* length needed */
3915{
3916 char_u *p;
3917 sblock_T *bl = *blp;
3918
3919 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
3920 {
3921 /* Allocate a block of memory. This is not freed until much later. */
3922 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
3923 if (bl == NULL)
3924 return NULL;
3925 bl->sb_next = *blp;
3926 *blp = bl;
3927 bl->sb_used = 0;
3928 }
3929
3930 p = bl->sb_data + bl->sb_used;
3931 bl->sb_used += len;
3932
3933 return p;
3934}
3935
3936/*
3937 * Make a copy of a string into memory allocated with getroom().
3938 */
3939 static char_u *
3940getroom_save(blp, s)
3941 sblock_T **blp;
3942 char_u *s;
3943{
3944 char_u *sc;
3945
3946 sc = (char_u *)getroom(blp, STRLEN(s) + 1);
3947 if (sc != NULL)
3948 STRCPY(sc, s);
3949 return sc;
3950}
3951
3952
3953/*
3954 * Free the list of allocated sblock_T.
3955 */
3956 static void
3957free_blocks(bl)
3958 sblock_T *bl;
3959{
3960 sblock_T *next;
3961
3962 while (bl != NULL)
3963 {
3964 next = bl->sb_next;
3965 vim_free(bl);
3966 bl = next;
3967 }
3968}
3969
3970/*
3971 * Allocate the root of a word tree.
3972 */
3973 static wordnode_T *
3974wordtree_alloc(blp)
3975 sblock_T **blp;
3976{
3977 return (wordnode_T *)getroom(blp, sizeof(wordnode_T));
3978}
3979
3980/*
3981 * Store a word in the tree(s).
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003982 * Always store it in the case-folded tree. A keep-case word can also be used
3983 * with all caps.
Bram Moolenaar51485f02005-06-04 21:55:20 +00003984 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003985 * When "pfxlist" is not NULL store the word for each prefix ID.
Bram Moolenaar51485f02005-06-04 21:55:20 +00003986 */
3987 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003988store_word(word, spin, flags, region, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003989 char_u *word;
3990 spellinfo_T *spin;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003991 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003992 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003993 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003994{
3995 int len = STRLEN(word);
3996 int ct = captype(word, word + len);
3997 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003998 int res = OK;
3999 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004000
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004001 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004002 for (p = pfxlist; res == OK; ++p)
4003 {
4004 res = tree_add_word(foldword, spin->si_foldroot, ct | flags,
4005 region, p == NULL ? 0 : *p, &spin->si_blocks);
4006 if (p == NULL || *p == NUL)
4007 break;
4008 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00004009 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004010
4011 if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP))
Bram Moolenaar8db73182005-06-17 21:51:16 +00004012 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004013 for (p = pfxlist; res == OK; ++p)
4014 {
4015 res = tree_add_word(word, spin->si_keeproot, flags,
4016 region, p == NULL ? 0 : *p, &spin->si_blocks);
4017 if (p == NULL || *p == NUL)
4018 break;
4019 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00004020 ++spin->si_keepwcount;
4021 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004022 return res;
4023}
4024
4025/*
4026 * Add word "word" to a word tree at "root".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004027 * When "flags" < 0 we are adding to the prefix tree where flags is used for
4028 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004029 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004030 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004031 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004032tree_add_word(word, root, flags, region, prefixID, blp)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004033 char_u *word;
4034 wordnode_T *root;
4035 int flags;
4036 int region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004037 int prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004038 sblock_T **blp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004039{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004040 wordnode_T *node = root;
4041 wordnode_T *np;
4042 wordnode_T **prev = NULL;
4043 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004044
Bram Moolenaar51485f02005-06-04 21:55:20 +00004045 /* Add each byte of the word to the tree, including the NUL at the end. */
4046 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004047 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004048 /* Look for the sibling that has the same character. They are sorted
4049 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004050 * higher byte value. For zero bytes (end of word) the sorting is
4051 * done on flags and then on prefixID
Bram Moolenaar51485f02005-06-04 21:55:20 +00004052 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004053 while (node != NULL
4054 && (node->wn_byte < word[i]
4055 || (node->wn_byte == NUL
4056 && (flags < 0
4057 ? node->wn_prefixID < prefixID
4058 : node->wn_flags < (flags & 0xff)
4059 || (node->wn_flags == (flags & 0xff)
4060 && node->wn_prefixID < prefixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004061 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004062 prev = &node->wn_sibling;
4063 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004064 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004065 if (node == NULL
4066 || node->wn_byte != word[i]
4067 || (word[i] == NUL
4068 && (flags < 0
4069 || node->wn_flags != (flags & 0xff)
4070 || node->wn_prefixID != prefixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004071 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004072 /* Allocate a new node. */
4073 np = (wordnode_T *)getroom(blp, sizeof(wordnode_T));
4074 if (np == NULL)
4075 return FAIL;
4076 np->wn_byte = word[i];
4077 *prev = np;
4078 np->wn_sibling = node;
4079 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004080 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004081
Bram Moolenaar51485f02005-06-04 21:55:20 +00004082 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004083 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004084 node->wn_flags = flags;
4085 node->wn_region |= region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004086 node->wn_prefixID = prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004087 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004088 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004089 prev = &node->wn_child;
4090 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004091 }
4092
4093 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004094}
4095
4096/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004097 * Compress a tree: find tails that are identical and can be shared.
4098 */
4099 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00004100wordtree_compress(root, spin)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004101 wordnode_T *root;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004102 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004103{
4104 hashtab_T ht;
4105 int n;
4106 int tot = 0;
4107
4108 if (root != NULL)
4109 {
4110 hash_init(&ht);
4111 n = node_compress(root, &ht, &tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004112 if (spin->si_verbose || p_verbose > 2)
4113 {
4114 if (!spin->si_verbose)
4115 verbose_enter();
4116 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar51485f02005-06-04 21:55:20 +00004117 n, tot, (tot - n) * 100 / tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004118 if (p_verbose > 2)
4119 verbose_leave();
4120 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004121 hash_clear(&ht);
4122 }
4123}
4124
4125/*
4126 * Compress a node, its siblings and its children, depth first.
4127 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004128 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004129 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00004130node_compress(node, ht, tot)
4131 wordnode_T *node;
4132 hashtab_T *ht;
4133 int *tot; /* total count of nodes before compressing,
4134 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004135{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004136 wordnode_T *np;
4137 wordnode_T *tp;
4138 wordnode_T *child;
4139 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004140 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004141 int len = 0;
4142 unsigned nr, n;
4143 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004144
Bram Moolenaar51485f02005-06-04 21:55:20 +00004145 /*
4146 * Go through the list of siblings. Compress each child and then try
4147 * finding an identical child to replace it.
4148 * Note that with "child" we mean not just the node that is pointed to,
4149 * but the whole list of siblings, of which the node is the first.
4150 */
4151 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004152 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004153 ++len;
4154 if ((child = np->wn_child) != NULL)
4155 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00004156 /* Compress the child. This fills hashkey. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004157 compressed += node_compress(child, ht, tot);
4158
4159 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004160 hash = hash_hash(child->wn_u1.hashkey);
4161 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004162 tp = NULL;
4163 if (!HASHITEM_EMPTY(hi))
4164 {
4165 /* There are children with an identical hash value. Now check
4166 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004167 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004168 if (node_equal(child, tp))
4169 {
4170 /* Found one! Now use that child in place of the
4171 * current one. This means the current child is
4172 * dropped from the tree. */
4173 np->wn_child = tp;
4174 ++compressed;
4175 break;
4176 }
4177 if (tp == NULL)
4178 {
4179 /* No other child with this hash value equals the child of
4180 * the node, add it to the linked list after the first
4181 * item. */
4182 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004183 child->wn_u2.next = tp->wn_u2.next;
4184 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004185 }
4186 }
4187 else
4188 /* No other child has this hash value, add it to the
4189 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004190 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004191 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004192 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004193 *tot += len;
4194
4195 /*
4196 * Make a hash key for the node and its siblings, so that we can quickly
4197 * find a lookalike node. This must be done after compressing the sibling
4198 * list, otherwise the hash key would become invalid by the compression.
4199 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004200 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004201 nr = 0;
4202 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004203 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004204 if (np->wn_byte == NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004205 /* end node: use wn_flags, wn_region and wn_prefixID */
4206 n = np->wn_flags + (np->wn_region << 8) + (np->wn_prefixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004207 else
4208 /* byte node: use the byte value and the child pointer */
4209 n = np->wn_byte + ((long_u)np->wn_child << 8);
4210 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004211 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004212
4213 /* Avoid NUL bytes, it terminates the hash key. */
4214 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004215 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004216 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004217 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004218 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004219 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004220 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004221 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
4222 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004223
4224 return compressed;
4225}
4226
4227/*
4228 * Return TRUE when two nodes have identical siblings and children.
4229 */
4230 static int
4231node_equal(n1, n2)
4232 wordnode_T *n1;
4233 wordnode_T *n2;
4234{
4235 wordnode_T *p1;
4236 wordnode_T *p2;
4237
4238 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
4239 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
4240 if (p1->wn_byte != p2->wn_byte
4241 || (p1->wn_byte == NUL
4242 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004243 || p1->wn_region != p2->wn_region
4244 || p1->wn_prefixID != p2->wn_prefixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004245 : (p1->wn_child != p2->wn_child)))
4246 break;
4247
4248 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004249}
4250
4251/*
4252 * Write a number to file "fd", MSB first, in "len" bytes.
4253 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004254 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004255put_bytes(fd, nr, len)
4256 FILE *fd;
4257 long_u nr;
4258 int len;
4259{
4260 int i;
4261
4262 for (i = len - 1; i >= 0; --i)
4263 putc((int)(nr >> (i * 8)), fd);
4264}
4265
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004266static int
4267#ifdef __BORLANDC__
4268_RTLENTRYF
4269#endif
4270rep_compare __ARGS((const void *s1, const void *s2));
4271
4272/*
4273 * Function given to qsort() to sort the REP items on "from" string.
4274 */
4275 static int
4276#ifdef __BORLANDC__
4277_RTLENTRYF
4278#endif
4279rep_compare(s1, s2)
4280 const void *s1;
4281 const void *s2;
4282{
4283 fromto_T *p1 = (fromto_T *)s1;
4284 fromto_T *p2 = (fromto_T *)s2;
4285
4286 return STRCMP(p1->ft_from, p2->ft_from);
4287}
4288
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004289/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004290 * Write the Vim spell file "fname".
4291 */
4292 static void
Bram Moolenaar3982c542005-06-08 21:56:31 +00004293write_vim_spell(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004294 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004295 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004296{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004297 FILE *fd;
4298 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004299 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004300 wordnode_T *tree;
4301 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004302 int i;
4303 int l;
4304 garray_T *gap;
4305 fromto_T *ftp;
4306 char_u *p;
4307 int rr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004308
Bram Moolenaarb765d632005-06-07 21:00:02 +00004309 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00004310 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004311 {
4312 EMSG2(_(e_notopen), fname);
4313 return;
4314 }
4315
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004316 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004317 * <charflagslen> <charflags>
4318 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004319 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004320 * <prefcondcnt> <prefcond> ... */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004321
4322 /* <fileID> */
4323 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
4324 EMSG(_(e_write));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004325
4326 /* write the region names if there is more than one */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004327 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004328 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004329 putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */
4330 fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2),
4331 (size_t)1, fd);
4332 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004333 }
4334 else
4335 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004336 putc(0, fd);
4337 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004338 }
4339
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004340 /*
4341 * Write the table with character flags and table for case folding.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004342 * <charflagslen> <charflags> <fcharlen> <fchars>
4343 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004344 * 'encoding'.
4345 * Also skip this for an .add.spl file, the main spell file must contain
4346 * the table (avoids that it conflicts). File is shorter too.
4347 */
4348 if (spin->si_ascii || spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004349 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004350 putc(0, fd);
4351 putc(0, fd);
4352 putc(0, fd);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004353 }
4354 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00004355 write_spell_chartab(fd);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004356
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004357
4358 if (spin->si_midword == NULL)
4359 put_bytes(fd, 0L, 2); /* <midwordlen> */
4360 else
4361 {
4362 i = STRLEN(spin->si_midword);
4363 put_bytes(fd, (long_u)i, 2); /* <midwordlen> */
4364 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
4365 }
4366
4367
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004368 /* Write the prefix conditions. */
4369 write_spell_prefcond(fd, &spin->si_prefcond);
4370
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004371 /* <SUGGEST> : <repcount> <rep> ...
4372 * <salflags> <salcount> <sal> ...
4373 * <maplen> <mapstr> */
4374
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004375 /* Sort the REP items. */
4376 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
4377 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004378
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004379 for (round = 1; round <= 2; ++round)
4380 {
4381 if (round == 1)
4382 gap = &spin->si_rep;
4383 else
4384 {
4385 gap = &spin->si_sal;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004386
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004387 i = 0;
4388 if (spin->si_followup)
4389 i |= SAL_F0LLOWUP;
4390 if (spin->si_collapse)
4391 i |= SAL_COLLAPSE;
4392 if (spin->si_rem_accents)
4393 i |= SAL_REM_ACCENTS;
4394 putc(i, fd); /* <salflags> */
4395 }
4396
4397 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
4398 for (i = 0; i < gap->ga_len; ++i)
4399 {
4400 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
4401 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
4402 ftp = &((fromto_T *)gap->ga_data)[i];
4403 for (rr = 1; rr <= 2; ++rr)
4404 {
4405 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
4406 l = STRLEN(p);
4407 putc(l, fd);
4408 fwrite(p, l, (size_t)1, fd);
4409 }
4410 }
4411 }
4412
4413 put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */
4414 if (spin->si_map.ga_len > 0) /* <mapstr> */
4415 fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len,
4416 (size_t)1, fd);
Bram Moolenaar50cde822005-06-05 21:54:54 +00004417
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004418 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004419 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004420 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004421 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004422 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004423 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004424 if (round == 1)
4425 tree = spin->si_foldroot;
4426 else if (round == 2)
4427 tree = spin->si_keeproot;
4428 else
4429 tree = spin->si_prefroot;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004430
Bram Moolenaar0c405862005-06-22 22:26:26 +00004431 /* Clear the index and wnode fields in the tree. */
4432 clear_node(tree);
4433
Bram Moolenaar51485f02005-06-04 21:55:20 +00004434 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00004435 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00004436 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004437 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004438
Bram Moolenaar51485f02005-06-04 21:55:20 +00004439 /* number of nodes in 4 bytes */
4440 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004441 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004442
Bram Moolenaar51485f02005-06-04 21:55:20 +00004443 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004444 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004445 }
4446
Bram Moolenaar51485f02005-06-04 21:55:20 +00004447 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004448}
4449
4450/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00004451 * Clear the index and wnode fields of "node", it siblings and its
4452 * children. This is needed because they are a union with other items to save
4453 * space.
4454 */
4455 static void
4456clear_node(node)
4457 wordnode_T *node;
4458{
4459 wordnode_T *np;
4460
4461 if (node != NULL)
4462 for (np = node; np != NULL; np = np->wn_sibling)
4463 {
4464 np->wn_u1.index = 0;
4465 np->wn_u2.wnode = NULL;
4466
4467 if (np->wn_byte != NUL)
4468 clear_node(np->wn_child);
4469 }
4470}
4471
4472
4473/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004474 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004475 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00004476 * This first writes the list of possible bytes (siblings). Then for each
4477 * byte recursively write the children.
4478 *
4479 * NOTE: The code here must match the code in read_tree(), since assumptions
4480 * are made about the indexes (so that we don't have to write them in the
4481 * file).
4482 *
4483 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004484 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004485 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00004486put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004487 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004488 wordnode_T *node;
4489 int index;
4490 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004491 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004492{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004493 int newindex = index;
4494 int siblingcount = 0;
4495 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004496 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004497
Bram Moolenaar51485f02005-06-04 21:55:20 +00004498 /* If "node" is zero the tree is empty. */
4499 if (node == NULL)
4500 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004501
Bram Moolenaar51485f02005-06-04 21:55:20 +00004502 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004503 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004504
4505 /* Count the number of siblings. */
4506 for (np = node; np != NULL; np = np->wn_sibling)
4507 ++siblingcount;
4508
4509 /* Write the sibling count. */
4510 if (fd != NULL)
4511 putc(siblingcount, fd); /* <siblingcount> */
4512
4513 /* Write each sibling byte and optionally extra info. */
4514 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004515 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004516 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004517 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004518 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004519 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004520 /* For a NUL byte (end of word) write the flags etc. */
4521 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004522 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004523 /* In PREFIXTREE write the required prefixID and the
4524 * associated condition nr (stored in wn_region). */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004525 if (np->wn_flags == (char_u)-2)
4526 putc(BY_FLAGS, fd); /* <byte> rare */
4527 else
4528 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004529 putc(np->wn_prefixID, fd); /* <prefixID> */
4530 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004531 }
4532 else
4533 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004534 /* For word trees we write the flag/region items. */
4535 flags = np->wn_flags;
4536 if (regionmask != 0 && np->wn_region != regionmask)
4537 flags |= WF_REGION;
4538 if (np->wn_prefixID != 0)
4539 flags |= WF_PFX;
4540 if (flags == 0)
4541 {
4542 /* word without flags or region */
4543 putc(BY_NOFLAGS, fd); /* <byte> */
4544 }
4545 else
4546 {
4547 putc(BY_FLAGS, fd); /* <byte> */
4548 putc(flags, fd); /* <flags> */
4549 if (flags & WF_REGION)
4550 putc(np->wn_region, fd); /* <region> */
4551 if (flags & WF_PFX)
4552 putc(np->wn_prefixID, fd); /* <prefixID> */
4553 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004554 }
4555 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004556 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004557 else
4558 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00004559 if (np->wn_child->wn_u1.index != 0
4560 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004561 {
4562 /* The child is written elsewhere, write the reference. */
4563 if (fd != NULL)
4564 {
4565 putc(BY_INDEX, fd); /* <byte> */
4566 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004567 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004568 }
4569 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00004570 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004571 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004572 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004573
Bram Moolenaar51485f02005-06-04 21:55:20 +00004574 if (fd != NULL)
4575 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
4576 {
4577 EMSG(_(e_write));
4578 return 0;
4579 }
4580 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004581 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004582
4583 /* Space used in the array when reading: one for each sibling and one for
4584 * the count. */
4585 newindex += siblingcount + 1;
4586
4587 /* Recursively dump the children of each sibling. */
4588 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00004589 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
4590 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004591 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004592
4593 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004594}
4595
4596
4597/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00004598 * ":mkspell [-ascii] outfile infile ..."
4599 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004600 */
4601 void
4602ex_mkspell(eap)
4603 exarg_T *eap;
4604{
4605 int fcount;
4606 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004607 char_u *arg = eap->arg;
4608 int ascii = FALSE;
4609
4610 if (STRNCMP(arg, "-ascii", 6) == 0)
4611 {
4612 ascii = TRUE;
4613 arg = skipwhite(arg + 6);
4614 }
4615
4616 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
4617 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
4618 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004619 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004620 FreeWild(fcount, fnames);
4621 }
4622}
4623
4624/*
4625 * Create a Vim spell file from one or more word lists.
4626 * "fnames[0]" is the output file name.
4627 * "fnames[fcount - 1]" is the last input file name.
4628 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
4629 * and ".spl" is appended to make the output file name.
4630 */
4631 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004632mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004633 int fcount;
4634 char_u **fnames;
4635 int ascii; /* -ascii argument given */
4636 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004637 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004638{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004639 char_u fname[MAXPATHL];
4640 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00004641 char_u **innames;
4642 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004643 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004644 int i;
4645 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004646 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004647 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004648 spellinfo_T spin;
4649
4650 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004651 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004652 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004653 spin.si_followup = TRUE;
4654 spin.si_rem_accents = TRUE;
4655 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
4656 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
4657 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004658 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004659
Bram Moolenaarb765d632005-06-07 21:00:02 +00004660 /* default: fnames[0] is output file, following are input files */
4661 innames = &fnames[1];
4662 incount = fcount - 1;
4663
4664 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004665 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004666 len = STRLEN(fnames[0]);
4667 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
4668 {
4669 /* For ":mkspell path/en.latin1.add" output file is
4670 * "path/en.latin1.add.spl". */
4671 innames = &fnames[0];
4672 incount = 1;
4673 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
4674 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004675 else if (fcount == 1)
4676 {
4677 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
4678 innames = &fnames[0];
4679 incount = 1;
4680 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
4681 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
4682 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004683 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
4684 {
4685 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004686 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004687 }
4688 else
4689 /* Name should be language, make the file name from it. */
4690 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
4691 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
4692
4693 /* Check for .ascii.spl. */
4694 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
4695 spin.si_ascii = TRUE;
4696
4697 /* Check for .add.spl. */
4698 if (strstr((char *)gettail(wfname), ".add.") != NULL)
4699 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004700 }
4701
Bram Moolenaarb765d632005-06-07 21:00:02 +00004702 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004703 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004704 else if (vim_strchr(gettail(wfname), '_') != NULL)
4705 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004706 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004707 EMSG(_("E754: Only up to 8 regions supported"));
4708 else
4709 {
4710 /* Check for overwriting before doing things that may take a lot of
4711 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004712 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004713 {
4714 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004715 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004716 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004717 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004718 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004719 EMSG2(_(e_isadir2), wfname);
4720 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004721 }
4722
4723 /*
4724 * Init the aff and dic pointers.
4725 * Get the region names if there are more than 2 arguments.
4726 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004727 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004728 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004729 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004730
Bram Moolenaar3982c542005-06-08 21:56:31 +00004731 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004732 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004733 len = STRLEN(innames[i]);
4734 if (STRLEN(gettail(innames[i])) < 5
4735 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004736 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004737 EMSG2(_("E755: Invalid region in %s"), innames[i]);
4738 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004739 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00004740 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
4741 spin.si_region_name[i * 2 + 1] =
4742 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004743 }
4744 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00004745 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004746
Bram Moolenaar51485f02005-06-04 21:55:20 +00004747 spin.si_foldroot = wordtree_alloc(&spin.si_blocks);
4748 spin.si_keeproot = wordtree_alloc(&spin.si_blocks);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004749 spin.si_prefroot = wordtree_alloc(&spin.si_blocks);
4750 if (spin.si_foldroot == NULL
4751 || spin.si_keeproot == NULL
4752 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004753 {
4754 error = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004755 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004756 }
4757
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004758 /* When not producing a .add.spl file clear the character table when
4759 * we encounter one in the .aff file. This means we dump the current
4760 * one in the .spl file if the .aff file doesn't define one. That's
4761 * better than guessing the contents, the table will match a
4762 * previously loaded spell file. */
4763 if (!spin.si_add)
4764 spin.si_clear_chartab = TRUE;
4765
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004766 /*
4767 * Read all the .aff and .dic files.
4768 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004769 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004770 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004771 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004772 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004773 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004774 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004775
Bram Moolenaarb765d632005-06-07 21:00:02 +00004776 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004777 if (mch_stat((char *)fname, &st) >= 0)
4778 {
4779 /* Read the .aff file. Will init "spin->si_conv" based on the
4780 * "SET" line. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004781 afile[i] = spell_read_aff(fname, &spin);
4782 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004783 error = TRUE;
4784 else
4785 {
4786 /* Read the .dic file and store the words in the trees. */
4787 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00004788 innames[i]);
4789 if (spell_read_dic(fname, &spin, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004790 error = TRUE;
4791 }
4792 }
4793 else
4794 {
4795 /* No .aff file, try reading the file as a word list. Store
4796 * the words in the trees. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004797 if (spell_read_wordfile(innames[i], &spin) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004798 error = TRUE;
4799 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004800
Bram Moolenaarb765d632005-06-07 21:00:02 +00004801#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004802 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004803 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004804#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004805 }
4806
Bram Moolenaar51485f02005-06-04 21:55:20 +00004807 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004808 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004809 /*
4810 * Remove the dummy NUL from the start of the tree root.
4811 */
4812 spin.si_foldroot = spin.si_foldroot->wn_sibling;
4813 spin.si_keeproot = spin.si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004814 spin.si_prefroot = spin.si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004815
4816 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004817 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004818 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004819 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004820 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004821 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004822 verbose_enter();
4823 MSG(_("Compressing word tree..."));
4824 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004825 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004826 verbose_leave();
4827 }
4828 wordtree_compress(spin.si_foldroot, &spin);
4829 wordtree_compress(spin.si_keeproot, &spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004830 wordtree_compress(spin.si_prefroot, &spin);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004831 }
4832
Bram Moolenaar51485f02005-06-04 21:55:20 +00004833 if (!error)
4834 {
4835 /*
4836 * Write the info in the spell file.
4837 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004838 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004839 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004840 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004841 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004842 smsg((char_u *)_("Writing spell file %s ..."), wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004843 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004844 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004845 verbose_leave();
4846 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00004847
Bram Moolenaar3982c542005-06-08 21:56:31 +00004848 write_vim_spell(wfname, &spin);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004849
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004850 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004851 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004852 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004853 verbose_enter();
4854 MSG(_("Done!"));
4855 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00004856 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004857 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004858 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004859 verbose_leave();
4860 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004861
Bram Moolenaarb765d632005-06-07 21:00:02 +00004862 /* If the file is loaded need to reload it. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004863 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004864 }
4865
4866 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004867 ga_clear(&spin.si_rep);
4868 ga_clear(&spin.si_sal);
4869 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004870 ga_clear(&spin.si_prefcond);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004871 vim_free(spin.si_midword);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004872
4873 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004874 for (i = 0; i < incount; ++i)
4875 if (afile[i] != NULL)
4876 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004877
4878 /* Free all the bits and pieces at once. */
4879 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004880 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004881}
4882
Bram Moolenaarb765d632005-06-07 21:00:02 +00004883
4884/*
4885 * ":spellgood {word}"
4886 * ":spellwrong {word}"
4887 */
4888 void
4889ex_spell(eap)
4890 exarg_T *eap;
4891{
4892 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong);
4893}
4894
4895/*
4896 * Add "word[len]" to 'spellfile' as a good or bad word.
4897 */
4898 void
4899spell_add_word(word, len, bad)
4900 char_u *word;
4901 int len;
4902 int bad;
4903{
4904 FILE *fd;
4905 buf_T *buf;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004906 int new_spf = FALSE;
4907 struct stat st;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004908
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004909 /* If 'spellfile' isn't set figure out a good default value. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004910 if (*curbuf->b_p_spf == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004911 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004912 init_spellfile();
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004913 new_spf = TRUE;
4914 }
4915
Bram Moolenaarb765d632005-06-07 21:00:02 +00004916 if (*curbuf->b_p_spf == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004917 EMSG(_("E764: 'spellfile' is not set"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004918 else
4919 {
4920 /* Check that the user isn't editing the .add file somewhere. */
4921 buf = buflist_findname_exp(curbuf->b_p_spf);
4922 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
4923 buf = NULL;
4924 if (buf != NULL && bufIsChanged(buf))
4925 EMSG(_(e_bufloaded));
4926 else
4927 {
4928 fd = mch_fopen((char *)curbuf->b_p_spf, "a");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004929 if (fd == NULL && new_spf)
4930 {
4931 /* We just initialized the 'spellfile' option and can't open
4932 * the file. We may need to create the "spell" directory
4933 * first. We already checked the runtime directory is
4934 * writable in init_spellfile(). */
4935 STRCPY(NameBuff, curbuf->b_p_spf);
4936 *gettail_sep(NameBuff) = NUL;
4937 if (mch_stat((char *)NameBuff, &st) < 0)
4938 {
4939 /* The directory doesn't exist. Try creating it and
4940 * opening the file again. */
4941 vim_mkdir(NameBuff, 0755);
4942 fd = mch_fopen((char *)curbuf->b_p_spf, "a");
4943 }
4944 }
4945
Bram Moolenaarb765d632005-06-07 21:00:02 +00004946 if (fd == NULL)
4947 EMSG2(_(e_notopen), curbuf->b_p_spf);
4948 else
4949 {
4950 if (bad)
4951 fprintf(fd, "/!%.*s\n", len, word);
4952 else
4953 fprintf(fd, "%.*s\n", len, word);
4954 fclose(fd);
4955
4956 /* Update the .add.spl file. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004957 mkspell(1, &curbuf->b_p_spf, FALSE, TRUE, TRUE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004958
4959 /* If the .add file is edited somewhere, reload it. */
4960 if (buf != NULL)
4961 buf_reload(buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004962
4963 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004964 }
4965 }
4966 }
4967}
4968
4969/*
4970 * Initialize 'spellfile' for the current buffer.
4971 */
4972 static void
4973init_spellfile()
4974{
4975 char_u buf[MAXPATHL];
4976 int l;
4977 slang_T *sl;
4978 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004979 char_u *lend;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004980
4981 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
4982 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004983 /* Find the end of the language name. Exclude the region. */
4984 for (lend = curbuf->b_p_spl; *lend != NUL
4985 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
4986 ;
4987
4988 /* Loop over all entries in 'runtimepath'. Use the first one where we
4989 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004990 rtp = p_rtp;
4991 while (*rtp != NUL)
4992 {
4993 /* Copy the path from 'runtimepath' to buf[]. */
4994 copy_option_part(&rtp, buf, MAXPATHL, ",");
4995 if (filewritable(buf) == 2)
4996 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004997 /* Use the first language name from 'spelllang' and the
4998 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004999 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
5000 l = STRLEN(buf);
5001 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00005002 "/spell/%.*s.%s.add",
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005003 (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00005004 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
5005 ? (char_u *)"ascii" : spell_enc());
5006 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
5007 break;
5008 }
5009 }
5010 }
5011}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005012
Bram Moolenaar51485f02005-06-04 21:55:20 +00005013
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005014/*
5015 * Init the chartab used for spelling for ASCII.
5016 * EBCDIC is not supported!
5017 */
5018 static void
5019clear_spell_chartab(sp)
5020 spelltab_T *sp;
5021{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005022 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005023
5024 /* Init everything to FALSE. */
5025 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
5026 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
5027 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005028 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005029 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005030 sp->st_upper[i] = i;
5031 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005032
5033 /* We include digits. A word shouldn't start with a digit, but handling
5034 * that is done separately. */
5035 for (i = '0'; i <= '9'; ++i)
5036 sp->st_isw[i] = TRUE;
5037 for (i = 'A'; i <= 'Z'; ++i)
5038 {
5039 sp->st_isw[i] = TRUE;
5040 sp->st_isu[i] = TRUE;
5041 sp->st_fold[i] = i + 0x20;
5042 }
5043 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005044 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005045 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005046 sp->st_upper[i] = i - 0x20;
5047 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005048}
5049
5050/*
5051 * Init the chartab used for spelling. Only depends on 'encoding'.
5052 * Called once while starting up and when 'encoding' changes.
5053 * The default is to use isalpha(), but the spell file should define the word
5054 * characters to make it possible that 'encoding' differs from the current
5055 * locale.
5056 */
5057 void
5058init_spell_chartab()
5059{
5060 int i;
5061
5062 did_set_spelltab = FALSE;
5063 clear_spell_chartab(&spelltab);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005064 vim_memset(spell_ismw, FALSE, sizeof(spell_ismw));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005065#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005066 vim_free(spell_ismw_mb);
5067 spell_ismw_mb = NULL;
5068
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005069 if (enc_dbcs)
5070 {
5071 /* DBCS: assume double-wide characters are word characters. */
5072 for (i = 128; i <= 255; ++i)
5073 if (MB_BYTE2LEN(i) == 2)
5074 spelltab.st_isw[i] = TRUE;
5075 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005076 else if (enc_utf8)
5077 {
5078 for (i = 128; i < 256; ++i)
5079 {
5080 spelltab.st_isu[i] = utf_isupper(i);
5081 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
5082 spelltab.st_fold[i] = utf_fold(i);
5083 spelltab.st_upper[i] = utf_toupper(i);
5084 }
5085 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005086 else
5087#endif
5088 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005089 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005090 for (i = 128; i < 256; ++i)
5091 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005092 if (MB_ISUPPER(i))
5093 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005094 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005095 spelltab.st_isu[i] = TRUE;
5096 spelltab.st_fold[i] = MB_TOLOWER(i);
5097 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005098 else if (MB_ISLOWER(i))
5099 {
5100 spelltab.st_isw[i] = TRUE;
5101 spelltab.st_upper[i] = MB_TOUPPER(i);
5102 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005103 }
5104 }
5105}
5106
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005107static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
5108static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
5109
5110/*
5111 * Set the spell character tables from strings in the affix file.
5112 */
5113 static int
5114set_spell_chartab(fol, low, upp)
5115 char_u *fol;
5116 char_u *low;
5117 char_u *upp;
5118{
5119 /* We build the new tables here first, so that we can compare with the
5120 * previous one. */
5121 spelltab_T new_st;
5122 char_u *pf = fol, *pl = low, *pu = upp;
5123 int f, l, u;
5124
5125 clear_spell_chartab(&new_st);
5126
5127 while (*pf != NUL)
5128 {
5129 if (*pl == NUL || *pu == NUL)
5130 {
5131 EMSG(_(e_affform));
5132 return FAIL;
5133 }
5134#ifdef FEAT_MBYTE
5135 f = mb_ptr2char_adv(&pf);
5136 l = mb_ptr2char_adv(&pl);
5137 u = mb_ptr2char_adv(&pu);
5138#else
5139 f = *pf++;
5140 l = *pl++;
5141 u = *pu++;
5142#endif
5143 /* Every character that appears is a word character. */
5144 if (f < 256)
5145 new_st.st_isw[f] = TRUE;
5146 if (l < 256)
5147 new_st.st_isw[l] = TRUE;
5148 if (u < 256)
5149 new_st.st_isw[u] = TRUE;
5150
5151 /* if "LOW" and "FOL" are not the same the "LOW" char needs
5152 * case-folding */
5153 if (l < 256 && l != f)
5154 {
5155 if (f >= 256)
5156 {
5157 EMSG(_(e_affrange));
5158 return FAIL;
5159 }
5160 new_st.st_fold[l] = f;
5161 }
5162
5163 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005164 * case-folding, it's upper case and the "UPP" is the upper case of
5165 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005166 if (u < 256 && u != f)
5167 {
5168 if (f >= 256)
5169 {
5170 EMSG(_(e_affrange));
5171 return FAIL;
5172 }
5173 new_st.st_fold[u] = f;
5174 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005175 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005176 }
5177 }
5178
5179 if (*pl != NUL || *pu != NUL)
5180 {
5181 EMSG(_(e_affform));
5182 return FAIL;
5183 }
5184
5185 return set_spell_finish(&new_st);
5186}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005187
5188/*
5189 * Set the spell character tables from strings in the .spl file.
5190 */
5191 static int
5192set_spell_charflags(flags, cnt, upp)
5193 char_u *flags;
5194 int cnt;
5195 char_u *upp;
5196{
5197 /* We build the new tables here first, so that we can compare with the
5198 * previous one. */
5199 spelltab_T new_st;
5200 int i;
5201 char_u *p = upp;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005202 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005203
5204 clear_spell_chartab(&new_st);
5205
5206 for (i = 0; i < cnt; ++i)
5207 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005208 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
5209 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005210
5211 if (*p == NUL)
5212 return FAIL;
5213#ifdef FEAT_MBYTE
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005214 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005215#else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005216 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005217#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005218 new_st.st_fold[i + 128] = c;
5219 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
5220 new_st.st_upper[c] = i + 128;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005221 }
5222
5223 return set_spell_finish(&new_st);
5224}
5225
5226 static int
5227set_spell_finish(new_st)
5228 spelltab_T *new_st;
5229{
5230 int i;
5231
5232 if (did_set_spelltab)
5233 {
5234 /* check that it's the same table */
5235 for (i = 0; i < 256; ++i)
5236 {
5237 if (spelltab.st_isw[i] != new_st->st_isw[i]
5238 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005239 || spelltab.st_fold[i] != new_st->st_fold[i]
5240 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005241 {
5242 EMSG(_("E763: Word characters differ between spell files"));
5243 return FAIL;
5244 }
5245 }
5246 }
5247 else
5248 {
5249 /* copy the new spelltab into the one being used */
5250 spelltab = *new_st;
5251 did_set_spelltab = TRUE;
5252 }
5253
5254 return OK;
5255}
5256
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005257/*
Bram Moolenaarea408852005-06-25 22:49:46 +00005258 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005259 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00005260 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005261 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00005262 */
5263 static int
5264spell_iswordp(p)
5265 char_u *p;
5266{
Bram Moolenaarea408852005-06-25 22:49:46 +00005267#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005268 char_u *s;
5269 int l;
5270 int c;
5271
5272 if (has_mbyte)
5273 {
5274 l = MB_BYTE2LEN(*p);
5275 s = p;
5276 if (l == 1)
5277 {
5278 /* be quick for ASCII */
5279 if (spell_ismw[*p])
5280 {
5281 s = p + 1; /* skip a mid-word character */
5282 l = MB_BYTE2LEN(*s);
5283 }
5284 }
5285 else
5286 {
5287 c = mb_ptr2char(p);
5288 if (c < 256 ? spell_ismw[c] : (spell_ismw_mb != NULL
5289 && vim_strchr(spell_ismw_mb, c) != NULL))
5290 {
5291 s = p + l;
5292 l = MB_BYTE2LEN(*s);
5293 }
5294 }
5295
5296 if (l > 1)
5297 return mb_get_class(s) >= 2;
5298 return spelltab.st_isw[*s];
5299 }
Bram Moolenaarea408852005-06-25 22:49:46 +00005300#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005301
5302 return spelltab.st_isw[spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaarea408852005-06-25 22:49:46 +00005303}
5304
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005305#ifdef FEAT_MBYTE
5306/*
5307 * Return TRUE if "p" points to a word character.
5308 * Wide version of spell_iswordp().
5309 */
5310 static int
5311spell_iswordp_w(p)
5312 int *p;
5313{
5314 int *s;
5315
5316 if (*p < 256 ? spell_ismw[*p] : (spell_ismw_mb != NULL
5317 && vim_strchr(spell_ismw_mb, *p) != NULL))
5318 s = p + 1;
5319 else
5320 s = p;
5321
5322 if (mb_char2len(*s) > 1)
5323 {
5324 if (enc_utf8)
5325 return utf_class(*s) >= 2;
5326 if (enc_dbcs)
5327 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
5328 return 0;
5329 }
5330 return spelltab.st_isw[*s];
5331}
5332#endif
5333
Bram Moolenaarea408852005-06-25 22:49:46 +00005334/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005335 * Write the table with prefix conditions to the .spl file.
5336 */
5337 static void
5338write_spell_prefcond(fd, gap)
5339 FILE *fd;
5340 garray_T *gap;
5341{
5342 int i;
5343 char_u *p;
5344 int len;
5345
5346 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
5347
5348 for (i = 0; i < gap->ga_len; ++i)
5349 {
5350 /* <prefcond> : <condlen> <condstr> */
5351 p = ((char_u **)gap->ga_data)[i];
5352 if (p == NULL)
5353 fputc(0, fd);
5354 else
5355 {
5356 len = STRLEN(p);
5357 fputc(len, fd);
5358 fwrite(p, (size_t)len, (size_t)1, fd);
5359 }
5360 }
5361}
5362
5363/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005364 * Write the current tables into the .spl file.
5365 * This makes sure the same characters are recognized as word characters when
5366 * generating an when using a spell file.
5367 */
5368 static void
5369write_spell_chartab(fd)
5370 FILE *fd;
5371{
5372 char_u charbuf[256 * 4];
5373 int len = 0;
5374 int flags;
5375 int i;
5376
5377 fputc(128, fd); /* <charflagslen> */
5378 for (i = 128; i < 256; ++i)
5379 {
5380 flags = 0;
5381 if (spelltab.st_isw[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005382 flags |= CF_WORD;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005383 if (spelltab.st_isu[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005384 flags |= CF_UPPER;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005385 fputc(flags, fd); /* <charflags> */
5386
Bram Moolenaarb765d632005-06-07 21:00:02 +00005387#ifdef FEAT_MBYTE
5388 if (has_mbyte)
5389 len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
5390 else
5391#endif
5392 charbuf[len++] = spelltab.st_fold[i];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005393 }
5394
5395 put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
5396 fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
5397}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005398
5399/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005400 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
5401 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005402 * When using a multi-byte 'encoding' the length may change!
5403 * Returns FAIL when something wrong.
5404 */
5405 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005406spell_casefold(str, len, buf, buflen)
5407 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005408 int len;
5409 char_u *buf;
5410 int buflen;
5411{
5412 int i;
5413
5414 if (len >= buflen)
5415 {
5416 buf[0] = NUL;
5417 return FAIL; /* result will not fit */
5418 }
5419
5420#ifdef FEAT_MBYTE
5421 if (has_mbyte)
5422 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005423 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005424 char_u *p;
5425 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005426
5427 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005428 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005429 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005430 if (outi + MB_MAXBYTES > buflen)
5431 {
5432 buf[outi] = NUL;
5433 return FAIL;
5434 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005435 c = mb_ptr2char_adv(&p);
5436 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005437 }
5438 buf[outi] = NUL;
5439 }
5440 else
5441#endif
5442 {
5443 /* Be quick for non-multibyte encodings. */
5444 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005445 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005446 buf[i] = NUL;
5447 }
5448
5449 return OK;
5450}
5451
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005452#define SPS_BEST 1
5453#define SPS_FAST 2
5454#define SPS_DOUBLE 4
5455
5456static int sps_flags = SPS_BEST;
5457
5458/*
5459 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
5460 * Sets "sps_flags".
5461 */
5462 int
5463spell_check_sps()
5464{
5465 char_u *p;
5466 char_u buf[MAXPATHL];
5467 int f;
5468
5469 sps_flags = 0;
5470
5471 for (p = p_sps; *p != NUL; )
5472 {
5473 copy_option_part(&p, buf, MAXPATHL, ",");
5474
5475 f = 0;
5476 if (STRCMP(buf, "best") == 0)
5477 f = SPS_BEST;
5478 else if (STRCMP(buf, "fast") == 0)
5479 f = SPS_FAST;
5480 else if (STRCMP(buf, "double") == 0)
5481 f = SPS_DOUBLE;
5482 else if (STRNCMP(buf, "expr:", 5) != 0
5483 && STRNCMP(buf, "file:", 5) != 0)
5484 f = -1;
5485
5486 if (f == -1 || (sps_flags != 0 && f != 0))
5487 {
5488 sps_flags = SPS_BEST;
5489 return FAIL;
5490 }
5491 if (f != 0)
5492 sps_flags = f;
5493 }
5494
5495 if (sps_flags == 0)
5496 sps_flags = SPS_BEST;
5497
5498 return OK;
5499}
5500
5501/* Remember what "z?" replaced. */
5502static char_u *repl_from = NULL;
5503static char_u *repl_to = NULL;
5504
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005505/*
5506 * "z?": Find badly spelled word under or after the cursor.
5507 * Give suggestions for the properly spelled word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005508 */
5509 void
5510spell_suggest()
5511{
5512 char_u *line;
5513 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005514 char_u wcopy[MAXWLEN + 2];
5515 char_u *p;
5516 int i;
5517 int c;
5518 suginfo_T sug;
5519 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005520 int mouse_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005521
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005522 /* Find the start of the badly spelled word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005523 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL
5524 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005525 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005526 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
5527 return;
5528
5529 /* No bad word or it starts after the cursor: use the word under the
5530 * cursor. */
5531 curwin->w_cursor = prev_cursor;
5532 line = ml_get_curline();
5533 p = line + curwin->w_cursor.col;
5534 /* Backup to before start of word. */
5535 while (p > line && SPELL_ISWORDP(p))
5536 mb_ptr_back(line, p);
5537 /* Forward to start of word. */
5538 while (!SPELL_ISWORDP(p))
5539 mb_ptr_adv(p);
5540
5541 if (!SPELL_ISWORDP(p)) /* No word found. */
5542 {
5543 beep_flush();
5544 return;
5545 }
5546 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005547 }
5548
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005549 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005550 line = ml_get_curline();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005551
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005552 /* Get the list of suggestions */
Bram Moolenaarea408852005-06-25 22:49:46 +00005553 spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005554
5555 if (sug.su_ga.ga_len == 0)
5556 MSG(_("Sorry, no suggestions"));
5557 else
5558 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005559 vim_free(repl_from);
5560 repl_from = NULL;
5561 vim_free(repl_to);
5562 repl_to = NULL;
5563
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005564 /* List the suggestions. */
5565 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005566 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005567 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
5568 sug.su_badlen, sug.su_badptr);
5569 msg_puts(IObuff);
5570 msg_clr_eos();
5571 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00005572
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005573 msg_scroll = TRUE;
5574 for (i = 0; i < sug.su_ga.ga_len; ++i)
5575 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005576 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005577
5578 /* The suggested word may replace only part of the bad word, add
5579 * the not replaced part. */
5580 STRCPY(wcopy, stp->st_word);
5581 if (sug.su_badlen > stp->st_orglen)
5582 vim_strncpy(wcopy + STRLEN(wcopy),
5583 sug.su_badptr + stp->st_orglen,
5584 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005585 vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\""), i + 1, wcopy);
5586 msg_puts(IObuff);
5587
5588 /* The word may replace more than "su_badlen". */
5589 if (sug.su_badlen < stp->st_orglen)
5590 {
5591 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
5592 stp->st_orglen, sug.su_badptr);
5593 msg_puts(IObuff);
5594 }
5595
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005596 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005597 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005598 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005599 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0c405862005-06-22 22:26:26 +00005600 vim_snprintf((char *)IObuff, IOSIZE, _(" (%s%d - %d)"),
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005601 stp->st_salscore ? "s " : "",
5602 stp->st_score, stp->st_altscore);
5603 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00005604 vim_snprintf((char *)IObuff, IOSIZE, _(" (%d)"),
5605 stp->st_score);
5606 msg_advance(30);
5607 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005608 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005609 msg_putchar('\n');
5610 }
5611
5612 /* Ask for choice. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005613 i = prompt_for_number(&mouse_used);
5614 if (mouse_used)
5615 i -= lines_left;
5616
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005617 if (i > 0 && i <= sug.su_ga.ga_len && u_save_cursor() == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005618 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005619 /* Save the from and to text for :spellrepall. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005620 stp = &SUG(sug.su_ga, i - 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005621 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
5622 repl_to = vim_strsave(stp->st_word);
5623
5624 /* Replace the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005625 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
5626 if (p != NULL)
5627 {
5628 c = sug.su_badptr - line;
5629 mch_memmove(p, line, c);
5630 STRCPY(p + c, stp->st_word);
5631 STRCAT(p, sug.su_badptr + stp->st_orglen);
5632 ml_replace(curwin->w_cursor.lnum, p, FALSE);
5633 curwin->w_cursor.col = c;
5634 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005635
5636 /* For redo we use a change-word command. */
5637 ResetRedobuff();
5638 AppendToRedobuff((char_u *)"ciw");
5639 AppendToRedobuff(stp->st_word);
5640 AppendCharToRedobuff(ESC);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005641 }
5642 }
5643 else
5644 curwin->w_cursor = prev_cursor;
5645 }
5646
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005647 spell_find_cleanup(&sug);
5648}
5649
5650/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005651 * ":spellrepall"
5652 */
5653/*ARGSUSED*/
5654 void
5655ex_spellrepall(eap)
5656 exarg_T *eap;
5657{
5658 pos_T pos = curwin->w_cursor;
5659 char_u *frompat;
5660 int addlen;
5661 char_u *line;
5662 char_u *p;
5663 int didone = FALSE;
5664 int save_ws = p_ws;
5665
5666 if (repl_from == NULL || repl_to == NULL)
5667 {
5668 EMSG(_("E752: No previous spell replacement"));
5669 return;
5670 }
5671 addlen = STRLEN(repl_to) - STRLEN(repl_from);
5672
5673 frompat = alloc(STRLEN(repl_from) + 7);
5674 if (frompat == NULL)
5675 return;
5676 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
5677 p_ws = FALSE;
5678
5679 curwin->w_cursor.lnum = 0;
5680 while (!got_int)
5681 {
5682 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
5683 || u_save_cursor() == FAIL)
5684 break;
5685
5686 /* Only replace when the right word isn't there yet. This happens
5687 * when changing "etc" to "etc.". */
5688 line = ml_get_curline();
5689 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
5690 repl_to, STRLEN(repl_to)) != 0)
5691 {
5692 p = alloc(STRLEN(line) + addlen + 1);
5693 if (p == NULL)
5694 break;
5695 mch_memmove(p, line, curwin->w_cursor.col);
5696 STRCPY(p + curwin->w_cursor.col, repl_to);
5697 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
5698 ml_replace(curwin->w_cursor.lnum, p, FALSE);
5699 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
5700 didone = TRUE;
5701 }
5702 curwin->w_cursor.col += STRLEN(repl_to);
5703 }
5704
5705 p_ws = save_ws;
5706 curwin->w_cursor = pos;
5707 vim_free(frompat);
5708
5709 if (!didone)
5710 EMSG2(_("E753: Not found: %s"), repl_from);
5711}
5712
5713/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005714 * Find spell suggestions for "word". Return them in the growarray "*gap" as
5715 * a list of allocated strings.
5716 */
5717 void
5718spell_suggest_list(gap, word, maxcount)
5719 garray_T *gap;
5720 char_u *word;
5721 int maxcount; /* maximum nr of suggestions */
5722{
5723 suginfo_T sug;
5724 int i;
5725 suggest_T *stp;
5726 char_u *wcopy;
5727
Bram Moolenaarea408852005-06-25 22:49:46 +00005728 spell_find_suggest(word, &sug, maxcount, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005729
5730 /* Make room in "gap". */
5731 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
5732 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
5733 return;
5734
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005735 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005736 {
5737 stp = &SUG(sug.su_ga, i);
5738
5739 /* The suggested word may replace only part of "word", add the not
5740 * replaced part. */
5741 wcopy = alloc(STRLEN(stp->st_word)
5742 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
5743 if (wcopy == NULL)
5744 break;
5745 STRCPY(wcopy, stp->st_word);
5746 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
5747 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
5748 }
5749
5750 spell_find_cleanup(&sug);
5751}
5752
5753/*
5754 * Find spell suggestions for the word at the start of "badptr".
5755 * Return the suggestions in "su->su_ga".
5756 * The maximum number of suggestions is "maxcount".
5757 * Note: does use info for the current window.
5758 * This is based on the mechanisms of Aspell, but completely reimplemented.
5759 */
5760 static void
Bram Moolenaarea408852005-06-25 22:49:46 +00005761spell_find_suggest(badptr, su, maxcount, banbadword)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005762 char_u *badptr;
5763 suginfo_T *su;
5764 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00005765 int banbadword; /* don't include badword in suggestions */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005766{
5767 int attr;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005768 char_u buf[MAXPATHL];
5769 char_u *p;
5770 int do_combine = FALSE;
5771 char_u *sps_copy;
5772#ifdef FEAT_EVAL
5773 static int expr_busy = FALSE;
5774#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005775
5776 /*
5777 * Set the info in "*su".
5778 */
5779 vim_memset(su, 0, sizeof(suginfo_T));
5780 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
5781 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005782 if (*badptr == NUL)
5783 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005784 hash_init(&su->su_banned);
5785
5786 su->su_badptr = badptr;
5787 su->su_badlen = spell_check(curwin, su->su_badptr, &attr);
5788 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005789 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005790
5791 if (su->su_badlen >= MAXWLEN)
5792 su->su_badlen = MAXWLEN - 1; /* just in case */
5793 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
5794 (void)spell_casefold(su->su_badptr, su->su_badlen,
5795 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005796 /* get caps flags for bad word */
5797 su->su_badflags = captype(su->su_badptr, su->su_badptr + su->su_badlen);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005798
5799 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00005800 if (banbadword)
5801 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005802
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005803 /* Make a copy of 'spellsuggest', because the expression may change it. */
5804 sps_copy = vim_strsave(p_sps);
5805 if (sps_copy == NULL)
5806 return;
5807
5808 /* Loop over the items in 'spellsuggest'. */
5809 for (p = sps_copy; *p != NUL; )
5810 {
5811 copy_option_part(&p, buf, MAXPATHL, ",");
5812
5813 if (STRNCMP(buf, "expr:", 5) == 0)
5814 {
5815#ifdef FEAT_EVAL
5816 /* Evaluate an expression. Skip this when called recursively
5817 * (using spellsuggest() in the expression). */
5818 if (!expr_busy)
5819 {
5820 expr_busy = TRUE;
5821 spell_suggest_expr(su, buf + 5);
5822 expr_busy = FALSE;
5823 }
5824#endif
5825 }
5826 else if (STRNCMP(buf, "file:", 5) == 0)
5827 /* Use list of suggestions in a file. */
5828 spell_suggest_file(su, buf + 5);
5829 else
5830 {
5831 /* Use internal method. */
5832 spell_suggest_intern(su);
5833 if (sps_flags & SPS_DOUBLE)
5834 do_combine = TRUE;
5835 }
5836 }
5837
5838 vim_free(sps_copy);
5839
5840 if (do_combine)
5841 /* Combine the two list of suggestions. This must be done last,
5842 * because sorting changes the order again. */
5843 score_combine(su);
5844}
5845
5846#ifdef FEAT_EVAL
5847/*
5848 * Find suggestions by evaluating expression "expr".
5849 */
5850 static void
5851spell_suggest_expr(su, expr)
5852 suginfo_T *su;
5853 char_u *expr;
5854{
5855 list_T *list;
5856 listitem_T *li;
5857 int score;
5858 char_u *p;
5859
5860 /* The work is split up in a few parts to avoid having to export
5861 * suginfo_T.
5862 * First evaluate the expression and get the resulting list. */
5863 list = eval_spell_expr(su->su_badword, expr);
5864 if (list != NULL)
5865 {
5866 /* Loop over the items in the list. */
5867 for (li = list->lv_first; li != NULL; li = li->li_next)
5868 if (li->li_tv.v_type == VAR_LIST)
5869 {
5870 /* Get the word and the score from the items. */
5871 score = get_spellword(li->li_tv.vval.v_list, &p);
5872 if (score >= 0)
5873 add_suggestion(su, &su->su_ga, p,
5874 su->su_badlen, score, 0, TRUE);
5875 }
5876 list_unref(list);
5877 }
5878
5879 /* Sort the suggestions and truncate at "maxcount". */
5880 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
5881}
5882#endif
5883
5884/*
5885 * Find suggestions a file "fname".
5886 */
5887 static void
5888spell_suggest_file(su, fname)
5889 suginfo_T *su;
5890 char_u *fname;
5891{
5892 FILE *fd;
5893 char_u line[MAXWLEN * 2];
5894 char_u *p;
5895 int len;
5896 char_u cword[MAXWLEN];
5897
5898 /* Open the file. */
5899 fd = mch_fopen((char *)fname, "r");
5900 if (fd == NULL)
5901 {
5902 EMSG2(_(e_notopen), fname);
5903 return;
5904 }
5905
5906 /* Read it line by line. */
5907 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
5908 {
5909 line_breakcheck();
5910
5911 p = vim_strchr(line, '/');
5912 if (p == NULL)
5913 continue; /* No Tab found, just skip the line. */
5914 *p++ = NUL;
5915 if (STRICMP(su->su_badword, line) == 0)
5916 {
5917 /* Match! Isolate the good word, until CR or NL. */
5918 for (len = 0; p[len] >= ' '; ++len)
5919 ;
5920 p[len] = NUL;
5921
5922 /* If the suggestion doesn't have specific case duplicate the case
5923 * of the bad word. */
5924 if (captype(p, NULL) == 0)
5925 {
5926 make_case_word(p, cword, su->su_badflags);
5927 p = cword;
5928 }
5929
5930 add_suggestion(su, &su->su_ga, p, su->su_badlen,
5931 SCORE_FILE, 0, TRUE);
5932 }
5933 }
5934
5935 fclose(fd);
5936
5937 /* Sort the suggestions and truncate at "maxcount". */
5938 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
5939}
5940
5941/*
5942 * Find suggestions for the internal method indicated by "sps_flags".
5943 */
5944 static void
5945spell_suggest_intern(su)
5946 suginfo_T *su;
5947{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005948 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00005949 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005950 *
5951 * Set a maximum score to limit the combination of operations that is
5952 * tried.
5953 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005954 suggest_try_special(su);
5955
5956 /*
5957 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
5958 * from the .aff file and inserting a space (split the word).
5959 */
5960 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005961
5962 /* For the resulting top-scorers compute the sound-a-like score. */
5963 if (sps_flags & SPS_DOUBLE)
5964 score_comp_sal(su);
5965
5966 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00005967 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005968 *
5969 * Only do this when we don't have a lot of suggestions yet, because it's
5970 * very slow and often doesn't find new suggestions.
5971 */
5972 if ((sps_flags & SPS_DOUBLE)
5973 || (!(sps_flags & SPS_FAST)
5974 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
5975 {
5976 /* Allow a higher score now. */
5977 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005978 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005979 }
5980
5981 /* When CTRL-C was hit while searching do show the results. */
5982 ui_breakcheck();
5983 if (got_int)
5984 {
5985 (void)vgetc();
5986 got_int = FALSE;
5987 }
5988
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005989 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005990 {
5991 if (sps_flags & SPS_BEST)
5992 /* Adjust the word score for how it sounds like. */
5993 rescore_suggestions(su);
5994
5995 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005996 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005997 }
5998}
5999
6000/*
6001 * Free the info put in "*su" by spell_find_suggest().
6002 */
6003 static void
6004spell_find_cleanup(su)
6005 suginfo_T *su;
6006{
6007 int i;
6008
6009 /* Free the suggestions. */
6010 for (i = 0; i < su->su_ga.ga_len; ++i)
6011 vim_free(SUG(su->su_ga, i).st_word);
6012 ga_clear(&su->su_ga);
6013 for (i = 0; i < su->su_sga.ga_len; ++i)
6014 vim_free(SUG(su->su_sga, i).st_word);
6015 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006016
6017 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006018 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006019}
6020
6021/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006022 * Make a copy of "word", with the first letter upper or lower cased, to
6023 * "wcopy[MAXWLEN]". "word" must not be empty.
6024 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006025 */
6026 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006027onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006028 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006029 char_u *wcopy;
6030 int upper; /* TRUE: first letter made upper case */
6031{
6032 char_u *p;
6033 int c;
6034 int l;
6035
6036 p = word;
6037#ifdef FEAT_MBYTE
6038 if (has_mbyte)
6039 c = mb_ptr2char_adv(&p);
6040 else
6041#endif
6042 c = *p++;
6043 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006044 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006045 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006046 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006047#ifdef FEAT_MBYTE
6048 if (has_mbyte)
6049 l = mb_char2bytes(c, wcopy);
6050 else
6051#endif
6052 {
6053 l = 1;
6054 wcopy[0] = c;
6055 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006056 vim_strncpy(wcopy + l, p, MAXWLEN - l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006057}
6058
6059/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006060 * Make a copy of "word" with all the letters upper cased into
6061 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006062 */
6063 static void
6064allcap_copy(word, wcopy)
6065 char_u *word;
6066 char_u *wcopy;
6067{
6068 char_u *s;
6069 char_u *d;
6070 int c;
6071
6072 d = wcopy;
6073 for (s = word; *s != NUL; )
6074 {
6075#ifdef FEAT_MBYTE
6076 if (has_mbyte)
6077 c = mb_ptr2char_adv(&s);
6078 else
6079#endif
6080 c = *s++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006081 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006082
6083#ifdef FEAT_MBYTE
6084 if (has_mbyte)
6085 {
6086 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
6087 break;
6088 d += mb_char2bytes(c, d);
6089 }
6090 else
6091#endif
6092 {
6093 if (d - wcopy >= MAXWLEN - 1)
6094 break;
6095 *d++ = c;
6096 }
6097 }
6098 *d = NUL;
6099}
6100
6101/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006102 * Try finding suggestions by recognizing specific situations.
6103 */
6104 static void
6105suggest_try_special(su)
6106 suginfo_T *su;
6107{
6108 char_u *p;
6109 int len;
6110 int c;
6111 char_u word[MAXWLEN];
6112
6113 /*
6114 * Recognize a word that is repeated: "the the".
6115 */
6116 p = skiptowhite(su->su_fbadword);
6117 len = p - su->su_fbadword;
6118 p = skipwhite(p);
6119 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
6120 {
6121 /* Include badflags: if the badword is onecap or allcap
6122 * use that for the goodword too: "The the" -> "The". */
6123 c = su->su_fbadword[len];
6124 su->su_fbadword[len] = NUL;
6125 make_case_word(su->su_fbadword, word, su->su_badflags);
6126 su->su_fbadword[len] = c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006127 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006128 }
6129}
6130
6131/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006132 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00006133 *
6134 * This uses a state machine. At each node in the tree we try various
6135 * operations. When trying if an operation work "depth" is increased and the
6136 * stack[] is used to store info. This allows combinations, thus insert one
6137 * character, replace one and delete another. The number of changes is
6138 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006139 */
6140 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00006141suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006142 suginfo_T *su;
6143{
6144 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
6145 char_u tword[MAXWLEN]; /* good word collected so far */
6146 trystate_T stack[MAXWLEN];
6147 char_u preword[MAXWLEN * 3]; /* word found with proper case (appended
6148 * to for word split) */
6149 char_u prewordlen = 0; /* length of word in "preword" */
6150 int splitoff = 0; /* index in tword after last split */
6151 trystate_T *sp;
6152 int newscore;
6153 langp_T *lp;
6154 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006155 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006156 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00006157 int c, c2, c3;
6158 int n = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006159 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006160 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006161 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006162 int len;
6163 char_u *p;
6164 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00006165 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006166 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006167
6168 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00006169 * to find matches (esp. REP items). Append some more text, changing
6170 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006171 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006172 n = STRLEN(fword);
6173 p = su->su_badptr + su->su_badlen;
6174 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006175
6176 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
6177 lp->lp_slang != NULL; ++lp)
6178 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006179 /*
6180 * Go through the whole case-fold tree, try changes at each node.
6181 * "tword[]" contains the word collected from nodes in the tree.
6182 * "fword[]" the word we are trying to match with (initially the bad
6183 * word).
6184 */
6185 byts = lp->lp_slang->sl_fbyts;
6186 idxs = lp->lp_slang->sl_fidxs;
6187
6188 depth = 0;
6189 stack[0].ts_state = STATE_START;
6190 stack[0].ts_score = 0;
6191 stack[0].ts_curi = 1;
6192 stack[0].ts_fidx = 0;
6193 stack[0].ts_fidxtry = 0;
6194 stack[0].ts_twordlen = 0;
6195 stack[0].ts_arridx = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00006196#ifdef FEAT_MBYTE
6197 stack[0].ts_tcharlen = 0;
6198#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006199
Bram Moolenaarea424162005-06-16 21:51:00 +00006200 /*
6201 * Loop to find all suggestions. At each round we either:
6202 * - For the current state try one operation, advance "ts_curi",
6203 * increase "depth".
6204 * - When a state is done go to the next, set "ts_state".
6205 * - When all states are tried decrease "depth".
6206 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006207 while (depth >= 0 && !got_int)
6208 {
6209 sp = &stack[depth];
6210 switch (sp->ts_state)
6211 {
6212 case STATE_START:
6213 /*
6214 * Start of node: Deal with NUL bytes, which means
6215 * tword[] may end here.
6216 */
6217 arridx = sp->ts_arridx; /* current node in the tree */
6218 len = byts[arridx]; /* bytes in this node */
6219 arridx += sp->ts_curi; /* index of current byte */
6220
Bram Moolenaar0c405862005-06-22 22:26:26 +00006221 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006222 {
6223 /* Past bytes in node and/or past NUL bytes. */
6224 sp->ts_state = STATE_ENDNUL;
6225 break;
6226 }
6227
6228 /*
6229 * End of word in tree.
6230 */
6231 ++sp->ts_curi; /* eat one NUL byte */
6232
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006233 flags = (int)idxs[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006234
6235 /*
6236 * Form the word with proper case in preword.
6237 * If there is a word from a previous split, append.
6238 */
6239 tword[sp->ts_twordlen] = NUL;
6240 if (flags & WF_KEEPCAP)
6241 /* Must find the word in the keep-case tree. */
6242 find_keepcap_word(lp->lp_slang, tword + splitoff,
6243 preword + prewordlen);
6244 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00006245 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006246 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00006247 * use that for the goodword too. But if the badword is
6248 * allcap and it's only one char long use onecap. */
6249 c = su->su_badflags;
6250 if ((c & WF_ALLCAP)
6251#ifdef FEAT_MBYTE
6252 && su->su_badlen == mb_ptr2len_check(su->su_badptr)
6253#else
6254 && su->su_badlen == 1
6255#endif
6256 )
6257 c = WF_ONECAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006258 make_case_word(tword + splitoff,
Bram Moolenaar0c405862005-06-22 22:26:26 +00006259 preword + prewordlen, flags | c);
6260 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006261
6262 /* Don't use a banned word. It may appear again as a good
6263 * word, thus remember it. */
6264 if (flags & WF_BANNED)
6265 {
6266 add_banned(su, preword + prewordlen);
6267 break;
6268 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006269 if (was_banned(su, preword + prewordlen)
6270 || was_banned(su, preword))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006271 break;
6272
6273 newscore = 0;
6274 if ((flags & WF_REGION)
6275 && (((unsigned)flags >> 8) & lp->lp_region) == 0)
6276 newscore += SCORE_REGION;
6277 if (flags & WF_RARE)
6278 newscore += SCORE_RARE;
6279
Bram Moolenaar0c405862005-06-22 22:26:26 +00006280 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006281 captype(preword + prewordlen, NULL)))
6282 newscore += SCORE_ICASE;
6283
Bram Moolenaar0c405862005-06-22 22:26:26 +00006284 if ((fword[sp->ts_fidx] == NUL
Bram Moolenaarea408852005-06-25 22:49:46 +00006285 || !spell_iswordp(fword + sp->ts_fidx))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006286 && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006287 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006288 /* The badword also ends: add suggestions. Give a penalty
6289 * when changing non-word char to word char, e.g., "thes,"
6290 * -> "these". */
6291 p = fword + sp->ts_fidx;
6292#ifdef FEAT_MBYTE
6293 if (has_mbyte)
6294 mb_ptr_back(fword, p);
6295 else
6296#endif
6297 --p;
6298 if (!spell_iswordp(p))
6299 {
6300 p = preword + STRLEN(preword);
6301#ifdef FEAT_MBYTE
6302 if (has_mbyte)
6303 mb_ptr_back(preword, p);
6304 else
6305#endif
6306 --p;
6307 if (spell_iswordp(p))
6308 newscore += SCORE_NONWORD;
6309 }
6310
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006311 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00006312 sp->ts_fidx - repextra,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006313 sp->ts_score + newscore, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006314 }
Bram Moolenaarea424162005-06-16 21:51:00 +00006315 else if (sp->ts_fidx >= sp->ts_fidxtry
6316#ifdef FEAT_MBYTE
6317 /* Don't split halfway a character. */
6318 && (!has_mbyte || sp->ts_tcharlen == 0)
6319#endif
6320 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006321 {
6322 /* The word in the tree ends but the badword
6323 * continues: try inserting a space and check that a valid
6324 * words starts at fword[sp->ts_fidx]. */
6325 if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT))
6326 {
6327 /* Save things to be restored at STATE_SPLITUNDO. */
6328 sp->ts_save_prewordlen = prewordlen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006329 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006330 sp->ts_save_splitoff = splitoff;
6331
6332 /* Append a space to preword. */
6333 STRCAT(preword, " ");
6334 prewordlen = STRLEN(preword);
6335 splitoff = sp->ts_twordlen;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006336#ifdef FEAT_MBYTE
6337 if (has_mbyte)
6338 {
6339 int i = 0;
6340
6341 /* Case-folding may change the number of bytes:
6342 * Count nr of chars in fword[sp->ts_fidx] and
6343 * advance that many chars in su->su_badptr. */
6344 for (p = fword; p < fword + sp->ts_fidx;
6345 mb_ptr_adv(p))
6346 ++i;
6347 for (p = su->su_badptr; i > 0; mb_ptr_adv(p))
6348 --i;
6349 }
6350 else
6351#endif
6352 p = su->su_badptr + sp->ts_fidx;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006353 su->su_badflags = captype(p, su->su_badptr
6354 + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006355
6356 sp->ts_state = STATE_SPLITUNDO;
6357 ++depth;
6358 /* Restart at top of the tree. */
6359 stack[depth].ts_arridx = 0;
6360 }
6361 }
6362 break;
6363
6364 case STATE_SPLITUNDO:
Bram Moolenaar0c405862005-06-22 22:26:26 +00006365 /* Undo the changes done for word split. */
6366 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006367 splitoff = sp->ts_save_splitoff;
6368 prewordlen = sp->ts_save_prewordlen;
6369
6370 /* Continue looking for NUL bytes. */
6371 sp->ts_state = STATE_START;
6372 break;
6373
6374 case STATE_ENDNUL:
6375 /* Past the NUL bytes in the node. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006376 if (fword[sp->ts_fidx] == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006377 {
6378 /* The badword ends, can't use the bytes in this node. */
6379 sp->ts_state = STATE_DEL;
6380 break;
6381 }
6382 sp->ts_state = STATE_PLAIN;
6383 /*FALLTHROUGH*/
6384
6385 case STATE_PLAIN:
6386 /*
6387 * Go over all possible bytes at this node, add each to
6388 * tword[] and use child node. "ts_curi" is the index.
6389 */
6390 arridx = sp->ts_arridx;
6391 if (sp->ts_curi > byts[arridx])
6392 {
6393 /* Done all bytes at this node, do next state. When still
6394 * at already changed bytes skip the other tricks. */
6395 if (sp->ts_fidx >= sp->ts_fidxtry)
6396 sp->ts_state = STATE_DEL;
6397 else
6398 sp->ts_state = STATE_FINAL;
6399 }
6400 else
6401 {
6402 arridx += sp->ts_curi++;
6403 c = byts[arridx];
6404
6405 /* Normal byte, go one level deeper. If it's not equal to
6406 * the byte in the bad word adjust the score. But don't
6407 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00006408 if (c == fword[sp->ts_fidx]
6409#ifdef FEAT_MBYTE
6410 || (sp->ts_tcharlen > 0
6411 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006412#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00006413 )
6414 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006415 else
6416 newscore = SCORE_SUBST;
6417 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
6418 && try_deeper(su, stack, depth, newscore))
6419 {
6420 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00006421 sp = &stack[depth];
6422 ++sp->ts_fidx;
6423 tword[sp->ts_twordlen++] = c;
6424 sp->ts_arridx = idxs[arridx];
6425#ifdef FEAT_MBYTE
6426 if (newscore == SCORE_SUBST)
6427 sp->ts_isdiff = DIFF_YES;
6428 if (has_mbyte)
6429 {
6430 /* Multi-byte characters are a bit complicated to
6431 * handle: They differ when any of the bytes
6432 * differ and then their length may also differ. */
6433 if (sp->ts_tcharlen == 0)
6434 {
6435 /* First byte. */
6436 sp->ts_tcharidx = 0;
6437 sp->ts_tcharlen = MB_BYTE2LEN(c);
6438 sp->ts_fcharstart = sp->ts_fidx - 1;
6439 sp->ts_isdiff = (newscore != 0)
6440 ? DIFF_YES : DIFF_NONE;
6441 }
6442 else if (sp->ts_isdiff == DIFF_INSERT)
6443 /* When inserting trail bytes don't advance in
6444 * the bad word. */
6445 --sp->ts_fidx;
6446 if (++sp->ts_tcharidx == sp->ts_tcharlen)
6447 {
6448 /* Last byte of character. */
6449 if (sp->ts_isdiff == DIFF_YES)
6450 {
6451 /* Correct ts_fidx for the byte length of
6452 * the character (we didn't check that
6453 * before). */
6454 sp->ts_fidx = sp->ts_fcharstart
6455 + MB_BYTE2LEN(
6456 fword[sp->ts_fcharstart]);
6457
6458 /* For a similar character adjust score
6459 * from SCORE_SUBST to SCORE_SIMILAR. */
6460 if (lp->lp_slang->sl_has_map
6461 && similar_chars(lp->lp_slang,
6462 mb_ptr2char(tword
6463 + sp->ts_twordlen
6464 - sp->ts_tcharlen),
6465 mb_ptr2char(fword
6466 + sp->ts_fcharstart)))
6467 sp->ts_score -=
6468 SCORE_SUBST - SCORE_SIMILAR;
6469 }
Bram Moolenaarea408852005-06-25 22:49:46 +00006470 else if (sp->ts_isdiff == DIFF_INSERT
6471 && sp->ts_twordlen > sp->ts_tcharlen)
6472 {
6473 /* If the previous character was the same,
6474 * thus doubling a character, give a bonus
6475 * to the score. */
6476 p = tword + sp->ts_twordlen
6477 - sp->ts_tcharlen;
6478 c = mb_ptr2char(p);
6479 mb_ptr_back(tword, p);
6480 if (c == mb_ptr2char(p))
6481 sp->ts_score -= SCORE_INS
6482 - SCORE_INSDUP;
6483 }
Bram Moolenaarea424162005-06-16 21:51:00 +00006484
6485 /* Starting a new char, reset the length. */
6486 sp->ts_tcharlen = 0;
6487 }
6488 }
6489 else
6490#endif
6491 {
6492 /* If we found a similar char adjust the score.
6493 * We do this after calling try_deeper() because
6494 * it's slow. */
6495 if (newscore != 0
6496 && lp->lp_slang->sl_has_map
6497 && similar_chars(lp->lp_slang,
6498 c, fword[sp->ts_fidx - 1]))
6499 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
6500 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006501 }
6502 }
6503 break;
6504
6505 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00006506#ifdef FEAT_MBYTE
6507 /* When past the first byte of a multi-byte char don't try
6508 * delete/insert/swap a character. */
6509 if (has_mbyte && sp->ts_tcharlen > 0)
6510 {
6511 sp->ts_state = STATE_FINAL;
6512 break;
6513 }
6514#endif
6515 /*
6516 * Try skipping one character in the bad word (delete it).
6517 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006518 sp->ts_state = STATE_INS;
6519 sp->ts_curi = 1;
6520 if (fword[sp->ts_fidx] != NUL
6521 && try_deeper(su, stack, depth, SCORE_DEL))
6522 {
6523 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00006524
6525 /* Advance over the character in fword[]. Give a bonus to
6526 * the score if the same character is following "nn" ->
6527 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00006528#ifdef FEAT_MBYTE
6529 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00006530 {
6531 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00006532 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaarea408852005-06-25 22:49:46 +00006533 if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
6534 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
6535 }
Bram Moolenaarea424162005-06-16 21:51:00 +00006536 else
6537#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00006538 {
Bram Moolenaarea424162005-06-16 21:51:00 +00006539 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00006540 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
6541 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
6542 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006543 break;
6544 }
6545 /*FALLTHROUGH*/
6546
6547 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00006548 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006549 * node. */
6550 n = sp->ts_arridx;
6551 if (sp->ts_curi > byts[n])
6552 {
6553 /* Done all bytes at this node, do next state. */
6554 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006555 }
6556 else
6557 {
Bram Moolenaarea424162005-06-16 21:51:00 +00006558 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006559 n += sp->ts_curi++;
6560 c = byts[n];
6561 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
6562 {
6563 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00006564 sp = &stack[depth];
6565 tword[sp->ts_twordlen++] = c;
6566 sp->ts_arridx = idxs[n];
6567#ifdef FEAT_MBYTE
6568 if (has_mbyte)
6569 {
6570 fl = MB_BYTE2LEN(c);
6571 if (fl > 1)
6572 {
6573 /* There are following bytes for the same
6574 * character. We must find all bytes before
6575 * trying delete/insert/swap/etc. */
6576 sp->ts_tcharlen = fl;
6577 sp->ts_tcharidx = 1;
6578 sp->ts_isdiff = DIFF_INSERT;
6579 }
6580 }
Bram Moolenaarea408852005-06-25 22:49:46 +00006581 else
6582 fl = 1;
6583 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00006584#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00006585 {
6586 /* If the previous character was the same, thus
6587 * doubling a character, give a bonus to the
6588 * score. */
6589 if (sp->ts_twordlen >= 2
6590 && tword[sp->ts_twordlen - 2] == c)
6591 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
6592 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006593 }
6594 }
6595 break;
6596
6597 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00006598 /*
6599 * Swap two bytes in the bad word: "12" -> "21".
6600 * We change "fword" here, it's changed back afterwards.
6601 */
6602 p = fword + sp->ts_fidx;
6603 c = *p;
6604 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006605 {
Bram Moolenaarea424162005-06-16 21:51:00 +00006606 /* End of word, can't swap or replace. */
6607 sp->ts_state = STATE_FINAL;
6608 break;
6609 }
6610#ifdef FEAT_MBYTE
6611 if (has_mbyte)
6612 {
6613 n = mb_ptr2len_check(p);
6614 c = mb_ptr2char(p);
6615 c2 = mb_ptr2char(p + n);
6616 }
6617 else
6618#endif
6619 c2 = p[1];
6620 if (c == c2)
6621 {
6622 /* Characters are identical, swap won't do anything. */
6623 sp->ts_state = STATE_SWAP3;
6624 break;
6625 }
6626 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
6627 {
6628 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006629 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00006630#ifdef FEAT_MBYTE
6631 if (has_mbyte)
6632 {
6633 fl = mb_char2len(c2);
6634 mch_memmove(p, p + n, fl);
6635 mb_char2bytes(c, p + fl);
6636 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
6637 }
6638 else
6639#endif
6640 {
6641 p[0] = c2;
6642 p[1] = c;
6643 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
6644 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006645 }
6646 else
6647 /* If this swap doesn't work then SWAP3 won't either. */
6648 sp->ts_state = STATE_REP_INI;
6649 break;
6650
Bram Moolenaarea424162005-06-16 21:51:00 +00006651 case STATE_UNSWAP:
6652 /* Undo the STATE_SWAP swap: "21" -> "12". */
6653 p = fword + sp->ts_fidx;
6654#ifdef FEAT_MBYTE
6655 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006656 {
Bram Moolenaarea424162005-06-16 21:51:00 +00006657 n = MB_BYTE2LEN(*p);
6658 c = mb_ptr2char(p + n);
6659 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
6660 mb_char2bytes(c, p);
6661 }
6662 else
6663#endif
6664 {
6665 c = *p;
6666 *p = p[1];
6667 p[1] = c;
6668 }
6669 /*FALLTHROUGH*/
6670
6671 case STATE_SWAP3:
6672 /* Swap two bytes, skipping one: "123" -> "321". We change
6673 * "fword" here, it's changed back afterwards. */
6674 p = fword + sp->ts_fidx;
6675#ifdef FEAT_MBYTE
6676 if (has_mbyte)
6677 {
6678 n = mb_ptr2len_check(p);
6679 c = mb_ptr2char(p);
6680 fl = mb_ptr2len_check(p + n);
6681 c2 = mb_ptr2char(p + n);
6682 c3 = mb_ptr2char(p + n + fl);
6683 }
6684 else
6685#endif
6686 {
6687 c = *p;
6688 c2 = p[1];
6689 c3 = p[2];
6690 }
6691
6692 /* When characters are identical: "121" then SWAP3 result is
6693 * identical, ROT3L result is same as SWAP: "211", ROT3L
6694 * result is same as SWAP on next char: "112". Thus skip all
6695 * swapping. Also skip when c3 is NUL. */
6696 if (c == c3 || c3 == NUL)
6697 {
6698 sp->ts_state = STATE_REP_INI;
6699 break;
6700 }
6701 if (try_deeper(su, stack, depth, SCORE_SWAP3))
6702 {
6703 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006704 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00006705#ifdef FEAT_MBYTE
6706 if (has_mbyte)
6707 {
6708 tl = mb_char2len(c3);
6709 mch_memmove(p, p + n + fl, tl);
6710 mb_char2bytes(c2, p + tl);
6711 mb_char2bytes(c, p + fl + tl);
6712 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
6713 }
6714 else
6715#endif
6716 {
6717 p[0] = p[2];
6718 p[2] = c;
6719 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
6720 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006721 }
6722 else
6723 sp->ts_state = STATE_REP_INI;
6724 break;
6725
Bram Moolenaarea424162005-06-16 21:51:00 +00006726 case STATE_UNSWAP3:
6727 /* Undo STATE_SWAP3: "321" -> "123" */
6728 p = fword + sp->ts_fidx;
6729#ifdef FEAT_MBYTE
6730 if (has_mbyte)
6731 {
6732 n = MB_BYTE2LEN(*p);
6733 c2 = mb_ptr2char(p + n);
6734 fl = MB_BYTE2LEN(p[n]);
6735 c = mb_ptr2char(p + n + fl);
6736 tl = MB_BYTE2LEN(p[n + fl]);
6737 mch_memmove(p + fl + tl, p, n);
6738 mb_char2bytes(c, p);
6739 mb_char2bytes(c2, p + tl);
6740 }
6741 else
6742#endif
6743 {
6744 c = *p;
6745 *p = p[2];
6746 p[2] = c;
6747 }
Bram Moolenaarea424162005-06-16 21:51:00 +00006748
Bram Moolenaarea424162005-06-16 21:51:00 +00006749 /* Rotate three characters left: "123" -> "231". We change
6750 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006751 if (try_deeper(su, stack, depth, SCORE_SWAP3))
6752 {
Bram Moolenaarea424162005-06-16 21:51:00 +00006753 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006754 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00006755 p = fword + sp->ts_fidx;
6756#ifdef FEAT_MBYTE
6757 if (has_mbyte)
6758 {
6759 n = mb_ptr2len_check(p);
6760 c = mb_ptr2char(p);
6761 fl = mb_ptr2len_check(p + n);
6762 fl += mb_ptr2len_check(p + n + fl);
6763 mch_memmove(p, p + n, fl);
6764 mb_char2bytes(c, p + fl);
6765 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
6766 }
6767 else
6768#endif
6769 {
6770 c = *p;
6771 *p = p[1];
6772 p[1] = p[2];
6773 p[2] = c;
6774 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
6775 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006776 }
6777 else
6778 sp->ts_state = STATE_REP_INI;
6779 break;
6780
Bram Moolenaarea424162005-06-16 21:51:00 +00006781 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +00006782 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00006783 p = fword + sp->ts_fidx;
6784#ifdef FEAT_MBYTE
6785 if (has_mbyte)
6786 {
6787 n = MB_BYTE2LEN(*p);
6788 n += MB_BYTE2LEN(p[n]);
6789 c = mb_ptr2char(p + n);
6790 tl = MB_BYTE2LEN(p[n]);
6791 mch_memmove(p + tl, p, n);
6792 mb_char2bytes(c, p);
6793 }
6794 else
6795#endif
6796 {
6797 c = p[2];
6798 p[2] = p[1];
6799 p[1] = *p;
6800 *p = c;
6801 }
Bram Moolenaarea424162005-06-16 21:51:00 +00006802
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006803 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00006804 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006805 if (try_deeper(su, stack, depth, SCORE_SWAP3))
6806 {
Bram Moolenaarea424162005-06-16 21:51:00 +00006807 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006808 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00006809 p = fword + sp->ts_fidx;
6810#ifdef FEAT_MBYTE
6811 if (has_mbyte)
6812 {
6813 n = mb_ptr2len_check(p);
6814 n += mb_ptr2len_check(p + n);
6815 c = mb_ptr2char(p + n);
6816 tl = mb_ptr2len_check(p + n);
6817 mch_memmove(p + tl, p, n);
6818 mb_char2bytes(c, p);
6819 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
6820 }
6821 else
6822#endif
6823 {
6824 c = p[2];
6825 p[2] = p[1];
6826 p[1] = *p;
6827 *p = c;
6828 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
6829 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006830 }
6831 else
6832 sp->ts_state = STATE_REP_INI;
6833 break;
6834
Bram Moolenaarea424162005-06-16 21:51:00 +00006835 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +00006836 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00006837 p = fword + sp->ts_fidx;
6838#ifdef FEAT_MBYTE
6839 if (has_mbyte)
6840 {
6841 c = mb_ptr2char(p);
6842 tl = MB_BYTE2LEN(*p);
6843 n = MB_BYTE2LEN(p[tl]);
6844 n += MB_BYTE2LEN(p[tl + n]);
6845 mch_memmove(p, p + tl, n);
6846 mb_char2bytes(c, p + n);
6847 }
6848 else
6849#endif
6850 {
6851 c = *p;
6852 *p = p[1];
6853 p[1] = p[2];
6854 p[2] = c;
6855 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006856 /*FALLTHROUGH*/
6857
6858 case STATE_REP_INI:
6859 /* Check if matching with REP items from the .aff file would
6860 * work. Quickly skip if there are no REP items or the score
6861 * is going to be too high anyway. */
6862 gap = &lp->lp_slang->sl_rep;
6863 if (gap->ga_len == 0
6864 || sp->ts_score + SCORE_REP >= su->su_maxscore)
6865 {
6866 sp->ts_state = STATE_FINAL;
6867 break;
6868 }
6869
6870 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00006871 * may match. If the index is -1 there is none. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006872 sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]];
6873 if (sp->ts_curi < 0)
6874 {
6875 sp->ts_state = STATE_FINAL;
6876 break;
6877 }
6878
6879 sp->ts_state = STATE_REP;
6880 /*FALLTHROUGH*/
6881
6882 case STATE_REP:
6883 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00006884 * match replace the characters and check if the resulting
6885 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006886 p = fword + sp->ts_fidx;
6887
6888 gap = &lp->lp_slang->sl_rep;
6889 while (sp->ts_curi < gap->ga_len)
6890 {
6891 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
6892 if (*ftp->ft_from != *p)
6893 {
6894 /* past possible matching entries */
6895 sp->ts_curi = gap->ga_len;
6896 break;
6897 }
6898 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
6899 && try_deeper(su, stack, depth, SCORE_REP))
6900 {
6901 /* Need to undo this afterwards. */
6902 sp->ts_state = STATE_REP_UNDO;
6903
6904 /* Change the "from" to the "to" string. */
6905 ++depth;
6906 fl = STRLEN(ftp->ft_from);
6907 tl = STRLEN(ftp->ft_to);
6908 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006909 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006910 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006911 repextra += tl - fl;
6912 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006913 mch_memmove(p, ftp->ft_to, tl);
6914 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +00006915#ifdef FEAT_MBYTE
6916 stack[depth].ts_tcharlen = 0;
6917#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006918 break;
6919 }
6920 }
6921
6922 if (sp->ts_curi >= gap->ga_len)
6923 /* No (more) matches. */
6924 sp->ts_state = STATE_FINAL;
6925
6926 break;
6927
6928 case STATE_REP_UNDO:
6929 /* Undo a REP replacement and continue with the next one. */
6930 ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data
6931 + sp->ts_curi - 1;
6932 fl = STRLEN(ftp->ft_from);
6933 tl = STRLEN(ftp->ft_to);
6934 p = fword + sp->ts_fidx;
6935 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006936 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006937 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006938 repextra -= tl - fl;
6939 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006940 mch_memmove(p, ftp->ft_from, fl);
6941 sp->ts_state = STATE_REP;
6942 break;
6943
6944 default:
6945 /* Did all possible states at this level, go up one level. */
6946 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006947
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006948 /* Don't check for CTRL-C too often, it takes time. */
6949 line_breakcheck();
6950 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006951 }
6952 }
6953}
6954
6955/*
6956 * Try going one level deeper in the tree.
6957 */
6958 static int
6959try_deeper(su, stack, depth, score_add)
6960 suginfo_T *su;
6961 trystate_T *stack;
6962 int depth;
6963 int score_add;
6964{
6965 int newscore;
6966
6967 /* Refuse to go deeper if the scrore is getting too big. */
6968 newscore = stack[depth].ts_score + score_add;
6969 if (newscore >= su->su_maxscore)
6970 return FALSE;
6971
Bram Moolenaarea424162005-06-16 21:51:00 +00006972 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006973 stack[depth + 1].ts_state = STATE_START;
6974 stack[depth + 1].ts_score = newscore;
6975 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006976 return TRUE;
6977}
6978
6979/*
6980 * "fword" is a good word with case folded. Find the matching keep-case
6981 * words and put it in "kword".
6982 * Theoretically there could be several keep-case words that result in the
6983 * same case-folded word, but we only find one...
6984 */
6985 static void
6986find_keepcap_word(slang, fword, kword)
6987 slang_T *slang;
6988 char_u *fword;
6989 char_u *kword;
6990{
6991 char_u uword[MAXWLEN]; /* "fword" in upper-case */
6992 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006993 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006994
6995 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006996 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006997 int round[MAXWLEN];
6998 int fwordidx[MAXWLEN];
6999 int uwordidx[MAXWLEN];
7000 int kwordlen[MAXWLEN];
7001
7002 int flen, ulen;
7003 int l;
7004 int len;
7005 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007006 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007007 char_u *p;
7008 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007009 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007010
7011 if (byts == NULL)
7012 {
7013 /* array is empty: "cannot happen" */
7014 *kword = NUL;
7015 return;
7016 }
7017
7018 /* Make an all-cap version of "fword". */
7019 allcap_copy(fword, uword);
7020
7021 /*
7022 * Each character needs to be tried both case-folded and upper-case.
7023 * All this gets very complicated if we keep in mind that changing case
7024 * may change the byte length of a multi-byte character...
7025 */
7026 depth = 0;
7027 arridx[0] = 0;
7028 round[0] = 0;
7029 fwordidx[0] = 0;
7030 uwordidx[0] = 0;
7031 kwordlen[0] = 0;
7032 while (depth >= 0)
7033 {
7034 if (fword[fwordidx[depth]] == NUL)
7035 {
7036 /* We are at the end of "fword". If the tree allows a word to end
7037 * here we have found a match. */
7038 if (byts[arridx[depth] + 1] == 0)
7039 {
7040 kword[kwordlen[depth]] = NUL;
7041 return;
7042 }
7043
7044 /* kword is getting too long, continue one level up */
7045 --depth;
7046 }
7047 else if (++round[depth] > 2)
7048 {
7049 /* tried both fold-case and upper-case character, continue one
7050 * level up */
7051 --depth;
7052 }
7053 else
7054 {
7055 /*
7056 * round[depth] == 1: Try using the folded-case character.
7057 * round[depth] == 2: Try using the upper-case character.
7058 */
7059#ifdef FEAT_MBYTE
7060 if (has_mbyte)
7061 {
7062 flen = mb_ptr2len_check(fword + fwordidx[depth]);
7063 ulen = mb_ptr2len_check(uword + uwordidx[depth]);
7064 }
7065 else
7066#endif
7067 ulen = flen = 1;
7068 if (round[depth] == 1)
7069 {
7070 p = fword + fwordidx[depth];
7071 l = flen;
7072 }
7073 else
7074 {
7075 p = uword + uwordidx[depth];
7076 l = ulen;
7077 }
7078
7079 for (tryidx = arridx[depth]; l > 0; --l)
7080 {
7081 /* Perform a binary search in the list of accepted bytes. */
7082 len = byts[tryidx++];
7083 c = *p++;
7084 lo = tryidx;
7085 hi = tryidx + len - 1;
7086 while (lo < hi)
7087 {
7088 m = (lo + hi) / 2;
7089 if (byts[m] > c)
7090 hi = m - 1;
7091 else if (byts[m] < c)
7092 lo = m + 1;
7093 else
7094 {
7095 lo = hi = m;
7096 break;
7097 }
7098 }
7099
7100 /* Stop if there is no matching byte. */
7101 if (hi < lo || byts[lo] != c)
7102 break;
7103
7104 /* Continue at the child (if there is one). */
7105 tryidx = idxs[lo];
7106 }
7107
7108 if (l == 0)
7109 {
7110 /*
7111 * Found the matching char. Copy it to "kword" and go a
7112 * level deeper.
7113 */
7114 if (round[depth] == 1)
7115 {
7116 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
7117 flen);
7118 kwordlen[depth + 1] = kwordlen[depth] + flen;
7119 }
7120 else
7121 {
7122 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
7123 ulen);
7124 kwordlen[depth + 1] = kwordlen[depth] + ulen;
7125 }
7126 fwordidx[depth + 1] = fwordidx[depth] + flen;
7127 uwordidx[depth + 1] = uwordidx[depth] + ulen;
7128
7129 ++depth;
7130 arridx[depth] = tryidx;
7131 round[depth] = 0;
7132 }
7133 }
7134 }
7135
7136 /* Didn't find it: "cannot happen". */
7137 *kword = NUL;
7138}
7139
7140/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007141 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
7142 * su->su_sga.
7143 */
7144 static void
7145score_comp_sal(su)
7146 suginfo_T *su;
7147{
7148 langp_T *lp;
7149 char_u badsound[MAXWLEN];
7150 int i;
7151 suggest_T *stp;
7152 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007153 int score;
7154
7155 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
7156 return;
7157
7158 /* Use the sound-folding of the first language that supports it. */
7159 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7160 lp->lp_slang != NULL; ++lp)
7161 if (lp->lp_slang->sl_sal.ga_len > 0)
7162 {
7163 /* soundfold the bad word */
7164 spell_soundfold(lp->lp_slang, su->su_fbadword, badsound);
7165
7166 for (i = 0; i < su->su_ga.ga_len; ++i)
7167 {
7168 stp = &SUG(su->su_ga, i);
7169
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007170 /* Case-fold the suggested word, sound-fold it and compute the
7171 * sound-a-like score. */
7172 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007173 if (score < SCORE_MAXMAX)
7174 {
7175 /* Add the suggestion. */
7176 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
7177 sstp->st_word = vim_strsave(stp->st_word);
7178 if (sstp->st_word != NULL)
7179 {
7180 sstp->st_score = score;
7181 sstp->st_altscore = 0;
7182 sstp->st_orglen = stp->st_orglen;
7183 ++su->su_sga.ga_len;
7184 }
7185 }
7186 }
7187 break;
7188 }
7189}
7190
7191/*
7192 * Combine the list of suggestions in su->su_ga and su->su_sga.
7193 * They are intwined.
7194 */
7195 static void
7196score_combine(su)
7197 suginfo_T *su;
7198{
7199 int i;
7200 int j;
7201 garray_T ga;
7202 garray_T *gap;
7203 langp_T *lp;
7204 suggest_T *stp;
7205 char_u *p;
7206 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007207 int round;
7208
7209 /* Add the alternate score to su_ga. */
7210 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7211 lp->lp_slang != NULL; ++lp)
7212 {
7213 if (lp->lp_slang->sl_sal.ga_len > 0)
7214 {
7215 /* soundfold the bad word */
7216 spell_soundfold(lp->lp_slang, su->su_fbadword, badsound);
7217
7218 for (i = 0; i < su->su_ga.ga_len; ++i)
7219 {
7220 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007221 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
7222 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007223 if (stp->st_altscore == SCORE_MAXMAX)
7224 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
7225 else
7226 stp->st_score = (stp->st_score * 3
7227 + stp->st_altscore) / 4;
7228 stp->st_salscore = FALSE;
7229 }
7230 break;
7231 }
7232 }
7233
7234 /* Add the alternate score to su_sga. */
7235 for (i = 0; i < su->su_sga.ga_len; ++i)
7236 {
7237 stp = &SUG(su->su_sga, i);
7238 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
7239 if (stp->st_score == SCORE_MAXMAX)
7240 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
7241 else
7242 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
7243 stp->st_salscore = TRUE;
7244 }
7245
7246 /* Sort the suggestions and truncate at "maxcount" for both lists. */
7247 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
7248 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
7249
7250 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
7251 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
7252 return;
7253
7254 stp = &SUG(ga, 0);
7255 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
7256 {
7257 /* round 1: get a suggestion from su_ga
7258 * round 2: get a suggestion from su_sga */
7259 for (round = 1; round <= 2; ++round)
7260 {
7261 gap = round == 1 ? &su->su_ga : &su->su_sga;
7262 if (i < gap->ga_len)
7263 {
7264 /* Don't add a word if it's already there. */
7265 p = SUG(*gap, i).st_word;
7266 for (j = 0; j < ga.ga_len; ++j)
7267 if (STRCMP(stp[j].st_word, p) == 0)
7268 break;
7269 if (j == ga.ga_len)
7270 stp[ga.ga_len++] = SUG(*gap, i);
7271 else
7272 vim_free(p);
7273 }
7274 }
7275 }
7276
7277 ga_clear(&su->su_ga);
7278 ga_clear(&su->su_sga);
7279
7280 /* Truncate the list to the number of suggestions that will be displayed. */
7281 if (ga.ga_len > su->su_maxcount)
7282 {
7283 for (i = su->su_maxcount; i < ga.ga_len; ++i)
7284 vim_free(stp[i].st_word);
7285 ga.ga_len = su->su_maxcount;
7286 }
7287
7288 su->su_ga = ga;
7289}
7290
7291/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007292 * For the goodword in "stp" compute the soundalike score compared to the
7293 * badword.
7294 */
7295 static int
7296stp_sal_score(stp, su, slang, badsound)
7297 suggest_T *stp;
7298 suginfo_T *su;
7299 slang_T *slang;
7300 char_u *badsound; /* sound-folded badword */
7301{
7302 char_u *p;
7303 char_u badsound2[MAXWLEN];
7304 char_u fword[MAXWLEN];
7305 char_u goodsound[MAXWLEN];
7306
7307 if (stp->st_orglen <= su->su_badlen)
7308 p = badsound;
7309 else
7310 {
7311 /* soundfold the bad word with more characters following */
7312 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
7313
7314 /* When joining two words the sound often changes a lot. E.g., "t he"
7315 * sounds like "t h" while "the" sounds like "@". Avoid that by
7316 * removing the space. Don't do it when the good word also contains a
7317 * space. */
7318 if (vim_iswhite(su->su_badptr[su->su_badlen])
7319 && *skiptowhite(stp->st_word) == NUL)
7320 for (p = fword; *(p = skiptowhite(p)) != NUL; )
7321 mch_memmove(p, p + 1, STRLEN(p));
7322
7323 spell_soundfold(slang, fword, badsound2);
7324 p = badsound2;
7325 }
7326
7327 /* Case-fold the word, sound-fold the word and compute the score for the
7328 * difference. */
7329 (void)spell_casefold(stp->st_word, STRLEN(stp->st_word), fword, MAXWLEN);
7330 spell_soundfold(slang, fword, goodsound);
7331
7332 return soundalike_score(goodsound, p);
7333}
7334
7335/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007336 * Find suggestions by comparing the word in a sound-a-like form.
7337 */
7338 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00007339suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007340 suginfo_T *su;
7341{
7342 char_u salword[MAXWLEN];
7343 char_u tword[MAXWLEN];
7344 char_u tfword[MAXWLEN];
7345 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007346 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007347 int curi[MAXWLEN];
7348 langp_T *lp;
7349 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007350 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007351 int depth;
7352 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007353 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007354 int round;
7355 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007356 int sound_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007357
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007358 /* Do this for all languages that support sound folding. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007359 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7360 lp->lp_slang != NULL; ++lp)
7361 {
7362 if (lp->lp_slang->sl_sal.ga_len > 0)
7363 {
7364 /* soundfold the bad word */
7365 spell_soundfold(lp->lp_slang, su->su_fbadword, salword);
7366
7367 /*
7368 * Go through the whole tree, soundfold each word and compare.
7369 * round 1: use the case-folded tree.
7370 * round 2: use the keep-case tree.
7371 */
7372 for (round = 1; round <= 2; ++round)
7373 {
7374 if (round == 1)
7375 {
7376 byts = lp->lp_slang->sl_fbyts;
7377 idxs = lp->lp_slang->sl_fidxs;
7378 }
7379 else
7380 {
7381 byts = lp->lp_slang->sl_kbyts;
7382 idxs = lp->lp_slang->sl_kidxs;
7383 }
7384
7385 depth = 0;
7386 arridx[0] = 0;
7387 curi[0] = 1;
7388 while (depth >= 0 && !got_int)
7389 {
7390 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007391 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007392 /* Done all bytes at this node, go up one level. */
7393 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007394 line_breakcheck();
7395 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007396 else
7397 {
7398 /* Do one more byte at this node. */
7399 n = arridx[depth] + curi[depth];
7400 ++curi[depth];
7401 c = byts[n];
7402 if (c == 0)
7403 {
7404 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007405 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007406 if (round == 2 || (flags & WF_KEEPCAP) == 0)
7407 {
7408 tword[depth] = NUL;
7409 if (round == 1)
7410 spell_soundfold(lp->lp_slang,
7411 tword, tsalword);
7412 else
7413 {
7414 /* In keep-case tree need to case-fold the
7415 * word. */
7416 (void)spell_casefold(tword, depth,
7417 tfword, MAXWLEN);
7418 spell_soundfold(lp->lp_slang,
7419 tfword, tsalword);
7420 }
7421
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007422 /* Compute the edit distance between the
7423 * sound-a-like words. */
7424 sound_score = soundalike_score(salword,
7425 tsalword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007426 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007427 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007428 char_u cword[MAXWLEN];
7429 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007430 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007431
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007432 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007433 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007434 /* Need to fix case according to
7435 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007436 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007437 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007438 }
7439 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007440 p = tword;
7441
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007442 if (sps_flags & SPS_DOUBLE)
7443 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007444 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007445 sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007446 else
7447 {
7448 /* Compute the score. */
7449 score = spell_edit_score(
7450 su->su_badword, p);
7451 if (sps_flags & SPS_BEST)
7452 /* give a bonus for the good word
7453 * sounding the same as the bad
7454 * word */
7455 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007456 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007457 RESCORE(score, sound_score),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007458 sound_score, TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007459 else
7460 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007461 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007462 score + sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007463 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007464 }
7465 }
7466
7467 /* Skip over other NUL bytes. */
7468 while (byts[n + 1] == 0)
7469 {
7470 ++n;
7471 ++curi[depth];
7472 }
7473 }
7474 else
7475 {
7476 /* Normal char, go one level deeper. */
7477 tword[depth++] = c;
7478 arridx[depth] = idxs[n];
7479 curi[depth] = 1;
7480 }
7481 }
7482 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007483 }
7484 }
7485 }
7486}
7487
7488/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007489 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007490 */
7491 static void
7492make_case_word(fword, cword, flags)
7493 char_u *fword;
7494 char_u *cword;
7495 int flags;
7496{
7497 if (flags & WF_ALLCAP)
7498 /* Make it all upper-case */
7499 allcap_copy(fword, cword);
7500 else if (flags & WF_ONECAP)
7501 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007502 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007503 else
7504 /* Use goodword as-is. */
7505 STRCPY(cword, fword);
7506}
7507
Bram Moolenaarea424162005-06-16 21:51:00 +00007508/*
7509 * Use map string "map" for languages "lp".
7510 */
7511 static void
7512set_map_str(lp, map)
7513 slang_T *lp;
7514 char_u *map;
7515{
7516 char_u *p;
7517 int headc = 0;
7518 int c;
7519 int i;
7520
7521 if (*map == NUL)
7522 {
7523 lp->sl_has_map = FALSE;
7524 return;
7525 }
7526 lp->sl_has_map = TRUE;
7527
7528 /* Init the array and hash table empty. */
7529 for (i = 0; i < 256; ++i)
7530 lp->sl_map_array[i] = 0;
7531#ifdef FEAT_MBYTE
7532 hash_init(&lp->sl_map_hash);
7533#endif
7534
7535 /*
7536 * The similar characters are stored separated with slashes:
7537 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
7538 * before the same slash. For characters above 255 sl_map_hash is used.
7539 */
7540 for (p = map; *p != NUL; )
7541 {
7542#ifdef FEAT_MBYTE
7543 c = mb_ptr2char_adv(&p);
7544#else
7545 c = *p++;
7546#endif
7547 if (c == '/')
7548 headc = 0;
7549 else
7550 {
7551 if (headc == 0)
7552 headc = c;
7553
7554#ifdef FEAT_MBYTE
7555 /* Characters above 255 don't fit in sl_map_array[], put them in
7556 * the hash table. Each entry is the char, a NUL the headchar and
7557 * a NUL. */
7558 if (c >= 256)
7559 {
7560 int cl = mb_char2len(c);
7561 int headcl = mb_char2len(headc);
7562 char_u *b;
7563 hash_T hash;
7564 hashitem_T *hi;
7565
7566 b = alloc((unsigned)(cl + headcl + 2));
7567 if (b == NULL)
7568 return;
7569 mb_char2bytes(c, b);
7570 b[cl] = NUL;
7571 mb_char2bytes(headc, b + cl + 1);
7572 b[cl + 1 + headcl] = NUL;
7573 hash = hash_hash(b);
7574 hi = hash_lookup(&lp->sl_map_hash, b, hash);
7575 if (HASHITEM_EMPTY(hi))
7576 hash_add_item(&lp->sl_map_hash, hi, b, hash);
7577 else
7578 {
7579 /* This should have been checked when generating the .spl
7580 * file. */
7581 EMSG(_("E999: duplicate char in MAP entry"));
7582 vim_free(b);
7583 }
7584 }
7585 else
7586#endif
7587 lp->sl_map_array[c] = headc;
7588 }
7589 }
7590}
7591
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007592/*
7593 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
7594 * lines in the .aff file.
7595 */
7596 static int
7597similar_chars(slang, c1, c2)
7598 slang_T *slang;
7599 int c1;
7600 int c2;
7601{
Bram Moolenaarea424162005-06-16 21:51:00 +00007602 int m1, m2;
7603#ifdef FEAT_MBYTE
7604 char_u buf[MB_MAXBYTES];
7605 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007606
Bram Moolenaarea424162005-06-16 21:51:00 +00007607 if (c1 >= 256)
7608 {
7609 buf[mb_char2bytes(c1, buf)] = 0;
7610 hi = hash_find(&slang->sl_map_hash, buf);
7611 if (HASHITEM_EMPTY(hi))
7612 m1 = 0;
7613 else
7614 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
7615 }
7616 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007617#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00007618 m1 = slang->sl_map_array[c1];
7619 if (m1 == 0)
7620 return FALSE;
7621
7622
7623#ifdef FEAT_MBYTE
7624 if (c2 >= 256)
7625 {
7626 buf[mb_char2bytes(c2, buf)] = 0;
7627 hi = hash_find(&slang->sl_map_hash, buf);
7628 if (HASHITEM_EMPTY(hi))
7629 m2 = 0;
7630 else
7631 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
7632 }
7633 else
7634#endif
7635 m2 = slang->sl_map_array[c2];
7636
7637 return m1 == m2;
7638}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007639
7640/*
7641 * Add a suggestion to the list of suggestions.
7642 * Do not add a duplicate suggestion or suggestions with a bad score.
7643 * When "use_score" is not zero it's used, otherwise the score is computed
7644 * with spell_edit_score().
7645 */
7646 static void
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007647add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007648 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007649 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007650 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007651 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007652 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007653 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007654 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007655{
7656 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007657 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007658 char_u *p = NULL;
7659 int c = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007660
7661 /* Check that the word wasn't banned. */
7662 if (was_banned(su, goodword))
7663 return;
7664
Bram Moolenaar0c405862005-06-22 22:26:26 +00007665 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
7666 * Remove the common part from "goodword". */
7667 i = badlen - su->su_badlen;
7668 if (i > 0)
7669 {
7670 /* This assumes there was no case folding or it didn't change the
7671 * length... */
7672 p = goodword + STRLEN(goodword) - i;
7673 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
7674 {
7675 badlen = su->su_badlen;
7676 c = *p;
7677 *p = NUL;
7678 }
7679 else
7680 p = NULL;
7681 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007682 else if (i < 0)
7683 {
7684 /* When replacing part of the word check that we actually change
7685 * something. For "the the" a suggestion can be replacing the first
7686 * "the" with itself, since "the" wasn't banned. */
7687 if (badlen == STRLEN(goodword)
7688 && STRNCMP(su->su_badword, goodword, badlen) == 0)
7689 return;
7690 }
7691
Bram Moolenaar0c405862005-06-22 22:26:26 +00007692
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007693 if (score <= su->su_maxscore)
7694 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007695 /* Check if the word is already there. Also check the length that is
7696 * being replaced "thes," -> "these" is a different suggestion from
7697 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007698 stp = &SUG(*gap, 0);
7699 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007700 if (STRCMP(stp[i].st_word, goodword) == 0
7701 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007702 {
7703 /* Found it. Remember the lowest score. */
7704 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007705 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007706 stp[i].st_score = score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007707 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007708 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007709 break;
7710 }
7711
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007712 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007713 {
7714 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007715 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007716 stp->st_word = vim_strsave(goodword);
7717 if (stp->st_word != NULL)
7718 {
7719 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007720 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007721 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007722 stp->st_orglen = badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007723 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007724
7725 /* If we have too many suggestions now, sort the list and keep
7726 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007727 if (gap->ga_len > SUG_MAX_COUNT(su))
7728 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
7729 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007730 }
7731 }
7732 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00007733
7734 if (p != NULL)
7735 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007736}
7737
7738/*
7739 * Add a word to be banned.
7740 */
7741 static void
7742add_banned(su, word)
7743 suginfo_T *su;
7744 char_u *word;
7745{
7746 char_u *s = vim_strsave(word);
7747 hash_T hash;
7748 hashitem_T *hi;
7749
7750 if (s != NULL)
7751 {
7752 hash = hash_hash(s);
7753 hi = hash_lookup(&su->su_banned, s, hash);
7754 if (HASHITEM_EMPTY(hi))
7755 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00007756 else
7757 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007758 }
7759}
7760
7761/*
7762 * Return TRUE if a word appears in the list of banned words.
7763 */
7764 static int
7765was_banned(su, word)
7766 suginfo_T *su;
7767 char_u *word;
7768{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007769 hashitem_T *hi = hash_find(&su->su_banned, word);
7770
7771 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007772}
7773
7774/*
7775 * Free the banned words in "su".
7776 */
7777 static void
7778free_banned(su)
7779 suginfo_T *su;
7780{
7781 int todo;
7782 hashitem_T *hi;
7783
7784 todo = su->su_banned.ht_used;
7785 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
7786 {
7787 if (!HASHITEM_EMPTY(hi))
7788 {
7789 vim_free(hi->hi_key);
7790 --todo;
7791 }
7792 }
7793 hash_clear(&su->su_banned);
7794}
7795
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007796/*
7797 * Recompute the score if sound-folding is possible. This is slow,
7798 * thus only done for the final results.
7799 */
7800 static void
7801rescore_suggestions(su)
7802 suginfo_T *su;
7803{
7804 langp_T *lp;
7805 suggest_T *stp;
7806 char_u sal_badword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007807 int i;
7808
7809 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7810 lp->lp_slang != NULL; ++lp)
7811 {
7812 if (lp->lp_slang->sl_sal.ga_len > 0)
7813 {
7814 /* soundfold the bad word */
7815 spell_soundfold(lp->lp_slang, su->su_fbadword, sal_badword);
7816
7817 for (i = 0; i < su->su_ga.ga_len; ++i)
7818 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007819 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007820 if (!stp->st_had_bonus)
7821 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007822 stp->st_altscore = stp_sal_score(stp, su,
7823 lp->lp_slang, sal_badword);
7824 if (stp->st_altscore == SCORE_MAXMAX)
7825 stp->st_altscore = SCORE_BIG;
7826 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007827 }
7828 }
7829 break;
7830 }
7831 }
7832}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007833
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007834static int
7835#ifdef __BORLANDC__
7836_RTLENTRYF
7837#endif
7838sug_compare __ARGS((const void *s1, const void *s2));
7839
7840/*
7841 * Function given to qsort() to sort the suggestions on st_score.
7842 */
7843 static int
7844#ifdef __BORLANDC__
7845_RTLENTRYF
7846#endif
7847sug_compare(s1, s2)
7848 const void *s1;
7849 const void *s2;
7850{
7851 suggest_T *p1 = (suggest_T *)s1;
7852 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007853 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007854
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007855 if (n == 0)
7856 return p1->st_altscore - p2->st_altscore;
7857 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007858}
7859
7860/*
7861 * Cleanup the suggestions:
7862 * - Sort on score.
7863 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007864 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007865 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007866 static int
7867cleanup_suggestions(gap, maxscore, keep)
7868 garray_T *gap;
7869 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007870 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007871{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007872 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007873 int i;
7874
7875 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007876 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007877
7878 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007879 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007880 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007881 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007882 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007883 gap->ga_len = keep;
7884 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007885 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007886 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007887}
7888
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007889#if defined(FEAT_EVAL) || defined(PROTO)
7890/*
7891 * Soundfold a string, for soundfold().
7892 * Result is in allocated memory, NULL for an error.
7893 */
7894 char_u *
7895eval_soundfold(word)
7896 char_u *word;
7897{
7898 langp_T *lp;
7899 char_u fword[MAXWLEN];
7900 char_u sound[MAXWLEN];
7901
7902 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
7903 /* Use the sound-folding of the first language that supports it. */
7904 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7905 lp->lp_slang != NULL; ++lp)
7906 if (lp->lp_slang->sl_sal.ga_len > 0)
7907 {
7908 /* word most be case-folded first. */
7909 (void)spell_casefold(word, STRLEN(word), fword, MAXWLEN);
7910
7911 /* soundfold the word */
7912 spell_soundfold(lp->lp_slang, fword, sound);
7913 return vim_strsave(sound);
7914 }
7915
7916 /* No language with sound folding, return word as-is. */
7917 return vim_strsave(word);
7918}
7919#endif
7920
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007921/*
7922 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
7923 */
7924 static void
7925spell_soundfold(slang, inword, res)
7926 slang_T *slang;
7927 char_u *inword;
7928 char_u *res;
7929{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007930 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007931 char_u word[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007932 char_u *s;
7933 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007934 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007935 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007936 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007937 int n, k = 0;
7938 int z0;
7939 int k0;
7940 int n0;
7941 int c;
7942 int pri;
7943 int p0 = -333;
7944 int c0;
7945
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007946#ifdef FEAT_MBYTE
7947 if (has_mbyte)
7948 {
7949 /* Call the multi-byte version of this. */
7950 spell_soundfold_w(slang, inword, res);
7951 return;
7952 }
7953#endif
7954
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007955 /* Remove accents, if wanted. We actually remove all non-word characters.
7956 * But keep white space. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007957 if (slang->sl_rem_accents)
7958 {
7959 t = word;
7960 for (s = inword; *s != NUL; )
7961 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007962 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007963 {
7964 *t++ = ' ';
7965 s = skipwhite(s);
7966 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007967 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007968 {
Bram Moolenaarea408852005-06-25 22:49:46 +00007969 if (spell_iswordp(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007970 *t++ = *s;
7971 ++s;
7972 }
7973 }
7974 *t = NUL;
7975 }
7976 else
7977 STRCPY(word, inword);
7978
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007979 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007980
7981 /*
7982 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007983 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007984 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007985 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007986 while ((c = word[i]) != NUL)
7987 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007988 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007989 n = slang->sl_sal_first[c];
7990 z0 = 0;
7991
7992 if (n >= 0)
7993 {
7994 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007995 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007996 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007997 /* Quickly skip entries that don't match the word. Most
7998 * entries are less then three chars, optimize for that. */
7999 k = smp[n].sm_leadlen;
8000 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008001 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008002 if (word[i + 1] != s[1])
8003 continue;
8004 if (k > 2)
8005 {
8006 for (j = 2; j < k; ++j)
8007 if (word[i + j] != s[j])
8008 break;
8009 if (j < k)
8010 continue;
8011 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008012 }
8013
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008014 if ((pf = smp[n].sm_oneoff) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008015 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008016 /* Check for match with one of the chars in "sm_oneoff". */
8017 while (*pf != NUL && *pf != word[i + k])
8018 ++pf;
8019 if (*pf == NUL)
8020 continue;
8021 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008022 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008023 s = smp[n].sm_rules;
8024 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008025
8026 p0 = *s;
8027 k0 = k;
8028 while (*s == '-' && k > 1)
8029 {
8030 k--;
8031 s++;
8032 }
8033 if (*s == '<')
8034 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008035 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008036 {
8037 /* determine priority */
8038 pri = *s - '0';
8039 s++;
8040 }
8041 if (*s == '^' && *(s + 1) == '^')
8042 s++;
8043
8044 if (*s == NUL
8045 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008046 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaarea408852005-06-25 22:49:46 +00008047 || spell_iswordp(word + i - 1)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008048 && (*(s + 1) != '$'
Bram Moolenaarea408852005-06-25 22:49:46 +00008049 || (!spell_iswordp(word + i + k0))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008050 || (*s == '$' && i > 0
Bram Moolenaarea408852005-06-25 22:49:46 +00008051 && spell_iswordp(word + i - 1)
8052 && (!spell_iswordp(word + i + k0))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008053 {
8054 /* search for followup rules, if: */
8055 /* followup and k > 1 and NO '-' in searchstring */
8056 c0 = word[i + k - 1];
8057 n0 = slang->sl_sal_first[c0];
8058
8059 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008060 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008061 {
8062 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008063 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008064 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008065 /* Quickly skip entries that don't match the word.
8066 * */
8067 k0 = smp[n0].sm_leadlen;
8068 if (k0 > 1)
8069 {
8070 if (word[i + k] != s[1])
8071 continue;
8072 if (k0 > 2)
8073 {
8074 pf = word + i + k + 1;
8075 for (j = 2; j < k0; ++j)
8076 if (*pf++ != s[j])
8077 break;
8078 if (j < k0)
8079 continue;
8080 }
8081 }
8082 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008083
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008084 if ((pf = smp[n0].sm_oneoff) != NULL)
8085 {
8086 /* Check for match with one of the chars in
8087 * "sm_oneoff". */
8088 while (*pf != NUL && *pf != word[i + k0])
8089 ++pf;
8090 if (*pf == NUL)
8091 continue;
8092 ++k0;
8093 }
8094
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008095 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008096 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008097 while (*s == '-')
8098 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008099 /* "k0" gets NOT reduced because
8100 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008101 s++;
8102 }
8103 if (*s == '<')
8104 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008105 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008106 {
8107 p0 = *s - '0';
8108 s++;
8109 }
8110
8111 if (*s == NUL
8112 /* *s == '^' cuts */
8113 || (*s == '$'
Bram Moolenaarea408852005-06-25 22:49:46 +00008114 && !spell_iswordp(word + i + k0)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008115 {
8116 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008117 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008118 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008119
8120 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008121 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008122 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008123 /* rule fits; stop search */
8124 break;
8125 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008126 }
8127
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008128 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008129 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008130 }
8131
8132 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008133 s = smp[n].sm_to;
8134 pf = smp[n].sm_rules;
8135 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008136 if (p0 == 1 && z == 0)
8137 {
8138 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008139 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
8140 || res[reslen - 1] == *s))
8141 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008142 z0 = 1;
8143 z = 1;
8144 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008145 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008146 {
8147 word[i + k0] = *s;
8148 k0++;
8149 s++;
8150 }
8151 if (k > k0)
8152 mch_memmove(word + i + k0, word + i + k,
8153 STRLEN(word + i + k) + 1);
8154
8155 /* new "actual letter" */
8156 c = word[i];
8157 }
8158 else
8159 {
8160 /* no '<' rule used */
8161 i += k - 1;
8162 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008163 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008164 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008165 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008166 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008167 s++;
8168 }
8169 /* new "actual letter" */
8170 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008171 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008172 {
8173 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008174 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008175 mch_memmove(word, word + i + 1,
8176 STRLEN(word + i + 1) + 1);
8177 i = 0;
8178 z0 = 1;
8179 }
8180 }
8181 break;
8182 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008183 }
8184 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008185 else if (vim_iswhite(c))
8186 {
8187 c = ' ';
8188 k = 1;
8189 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008190
8191 if (z0 == 0)
8192 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008193 if (k && !p0 && reslen < MAXWLEN && c != NUL
8194 && (!slang->sl_collapse || reslen == 0
8195 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008196 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008197 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008198
8199 i++;
8200 z = 0;
8201 k = 0;
8202 }
8203 }
8204
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008205 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008206}
8207
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008208#ifdef FEAT_MBYTE
8209/*
8210 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
8211 * Multi-byte version of spell_soundfold().
8212 */
8213 static void
8214spell_soundfold_w(slang, inword, res)
8215 slang_T *slang;
8216 char_u *inword;
8217 char_u *res;
8218{
8219 salitem_T *smp;
8220 int word[MAXWLEN];
8221 int wres[MAXWLEN];
8222 int l;
8223 char_u *s;
8224 int *ws;
8225 char_u *t;
8226 int *pf;
8227 int i, j, z;
8228 int reslen;
8229 int n, k = 0;
8230 int z0;
8231 int k0;
8232 int n0;
8233 int c;
8234 int pri;
8235 int p0 = -333;
8236 int c0;
8237 int did_white = FALSE;
8238
8239 /*
8240 * Convert the multi-byte string to a wide-character string.
8241 * Remove accents, if wanted. We actually remove all non-word characters.
8242 * But keep white space.
8243 */
8244 n = 0;
8245 for (s = inword; *s != NUL; )
8246 {
8247 t = s;
8248 c = mb_ptr2char_adv(&s);
8249 if (slang->sl_rem_accents)
8250 {
8251 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
8252 {
8253 if (did_white)
8254 continue;
8255 c = ' ';
8256 did_white = TRUE;
8257 }
8258 else
8259 {
8260 did_white = FALSE;
8261 if (!spell_iswordp(t))
8262 continue;
8263 }
8264 }
8265 word[n++] = c;
8266 }
8267 word[n] = NUL;
8268
8269 smp = (salitem_T *)slang->sl_sal.ga_data;
8270
8271 /*
8272 * This comes from Aspell phonet.cpp.
8273 * Converted from C++ to C. Added support for multi-byte chars.
8274 * Changed to keep spaces.
8275 */
8276 i = reslen = z = 0;
8277 while ((c = word[i]) != NUL)
8278 {
8279 /* Start with the first rule that has the character in the word. */
8280 n = slang->sl_sal_first[c & 0xff];
8281 z0 = 0;
8282
8283 if (n >= 0)
8284 {
8285 /* check all rules for the same letter */
8286 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
8287 {
8288 /* Quickly skip entries that don't match the word. Most
8289 * entries are less then three chars, optimize for that. */
8290 k = smp[n].sm_leadlen;
8291 if (k > 1)
8292 {
8293 if (word[i + 1] != ws[1])
8294 continue;
8295 if (k > 2)
8296 {
8297 for (j = 2; j < k; ++j)
8298 if (word[i + j] != ws[j])
8299 break;
8300 if (j < k)
8301 continue;
8302 }
8303 }
8304
8305 if ((pf = smp[n].sm_oneoff_w) != NULL)
8306 {
8307 /* Check for match with one of the chars in "sm_oneoff". */
8308 while (*pf != NUL && *pf != word[i + k])
8309 ++pf;
8310 if (*pf == NUL)
8311 continue;
8312 ++k;
8313 }
8314 s = smp[n].sm_rules;
8315 pri = 5; /* default priority */
8316
8317 p0 = *s;
8318 k0 = k;
8319 while (*s == '-' && k > 1)
8320 {
8321 k--;
8322 s++;
8323 }
8324 if (*s == '<')
8325 s++;
8326 if (VIM_ISDIGIT(*s))
8327 {
8328 /* determine priority */
8329 pri = *s - '0';
8330 s++;
8331 }
8332 if (*s == '^' && *(s + 1) == '^')
8333 s++;
8334
8335 if (*s == NUL
8336 || (*s == '^'
8337 && (i == 0 || !(word[i - 1] == ' '
8338 || spell_iswordp_w(word + i - 1)))
8339 && (*(s + 1) != '$'
8340 || (!spell_iswordp_w(word + i + k0))))
8341 || (*s == '$' && i > 0
8342 && spell_iswordp_w(word + i - 1)
8343 && (!spell_iswordp_w(word + i + k0))))
8344 {
8345 /* search for followup rules, if: */
8346 /* followup and k > 1 and NO '-' in searchstring */
8347 c0 = word[i + k - 1];
8348 n0 = slang->sl_sal_first[c0 & 0xff];
8349
8350 if (slang->sl_followup && k > 1 && n0 >= 0
8351 && p0 != '-' && word[i + k] != NUL)
8352 {
8353 /* test follow-up rule for "word[i + k]" */
8354 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
8355 == (c0 & 0xff); ++n0)
8356 {
8357 /* Quickly skip entries that don't match the word.
8358 * */
8359 k0 = smp[n0].sm_leadlen;
8360 if (k0 > 1)
8361 {
8362 if (word[i + k] != ws[1])
8363 continue;
8364 if (k0 > 2)
8365 {
8366 pf = word + i + k + 1;
8367 for (j = 2; j < k0; ++j)
8368 if (*pf++ != ws[j])
8369 break;
8370 if (j < k0)
8371 continue;
8372 }
8373 }
8374 k0 += k - 1;
8375
8376 if ((pf = smp[n0].sm_oneoff_w) != NULL)
8377 {
8378 /* Check for match with one of the chars in
8379 * "sm_oneoff". */
8380 while (*pf != NUL && *pf != word[i + k0])
8381 ++pf;
8382 if (*pf == NUL)
8383 continue;
8384 ++k0;
8385 }
8386
8387 p0 = 5;
8388 s = smp[n0].sm_rules;
8389 while (*s == '-')
8390 {
8391 /* "k0" gets NOT reduced because
8392 * "if (k0 == k)" */
8393 s++;
8394 }
8395 if (*s == '<')
8396 s++;
8397 if (VIM_ISDIGIT(*s))
8398 {
8399 p0 = *s - '0';
8400 s++;
8401 }
8402
8403 if (*s == NUL
8404 /* *s == '^' cuts */
8405 || (*s == '$'
8406 && !spell_iswordp_w(word + i + k0)))
8407 {
8408 if (k0 == k)
8409 /* this is just a piece of the string */
8410 continue;
8411
8412 if (p0 < pri)
8413 /* priority too low */
8414 continue;
8415 /* rule fits; stop search */
8416 break;
8417 }
8418 }
8419
8420 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
8421 == (c0 & 0xff))
8422 continue;
8423 }
8424
8425 /* replace string */
8426 ws = smp[n].sm_to_w;
8427 s = smp[n].sm_rules;
8428 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
8429 if (p0 == 1 && z == 0)
8430 {
8431 /* rule with '<' is used */
8432 if (reslen > 0 && *ws != NUL && (wres[reslen - 1] == c
8433 || wres[reslen - 1] == *ws))
8434 reslen--;
8435 z0 = 1;
8436 z = 1;
8437 k0 = 0;
8438 while (*ws != NUL && word[i + k0] != NUL)
8439 {
8440 word[i + k0] = *ws;
8441 k0++;
8442 ws++;
8443 }
8444 if (k > k0)
8445 mch_memmove(word + i + k0, word + i + k,
8446 sizeof(int) * (STRLEN(word + i + k) + 1));
8447
8448 /* new "actual letter" */
8449 c = word[i];
8450 }
8451 else
8452 {
8453 /* no '<' rule used */
8454 i += k - 1;
8455 z = 0;
8456 while (*ws != NUL && ws[1] != NUL && reslen < MAXWLEN)
8457 {
8458 if (reslen == 0 || wres[reslen - 1] != *ws)
8459 wres[reslen++] = *ws;
8460 ws++;
8461 }
8462 /* new "actual letter" */
8463 c = *ws;
8464 if (strstr((char *)s, "^^") != NULL)
8465 {
8466 if (c != NUL)
8467 wres[reslen++] = c;
8468 mch_memmove(word, word + i + 1,
8469 sizeof(int) * (STRLEN(word + i + 1) + 1));
8470 i = 0;
8471 z0 = 1;
8472 }
8473 }
8474 break;
8475 }
8476 }
8477 }
8478 else if (vim_iswhite(c))
8479 {
8480 c = ' ';
8481 k = 1;
8482 }
8483
8484 if (z0 == 0)
8485 {
8486 if (k && !p0 && reslen < MAXWLEN && c != NUL
8487 && (!slang->sl_collapse || reslen == 0
8488 || wres[reslen - 1] != c))
8489 /* condense only double letters */
8490 wres[reslen++] = c;
8491
8492 i++;
8493 z = 0;
8494 k = 0;
8495 }
8496 }
8497
8498 /* Convert wide characters in "wres" to a multi-byte string in "res". */
8499 l = 0;
8500 for (n = 0; n < reslen; ++n)
8501 {
8502 l += mb_char2bytes(wres[n], res + l);
8503 if (l + MB_MAXBYTES > MAXWLEN)
8504 break;
8505 }
8506 res[l] = NUL;
8507}
8508#endif
8509
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008510/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008511 * Compute a score for two sound-a-like words.
8512 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
8513 * Instead of a generic loop we write out the code. That keeps it fast by
8514 * avoiding checks that will not be possible.
8515 */
8516 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008517soundalike_score(goodstart, badstart)
8518 char_u *goodstart; /* sound-folded good word */
8519 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008520{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008521 char_u *goodsound = goodstart;
8522 char_u *badsound = badstart;
8523 int goodlen;
8524 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008525 int n;
8526 char_u *pl, *ps;
8527 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008528 int score = 0;
8529
8530 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
8531 * counted so much, vowels halfway the word aren't counted at all. */
8532 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
8533 {
8534 score = SCORE_DEL / 2;
8535 if (*badsound == '*')
8536 ++badsound;
8537 else
8538 ++goodsound;
8539 }
8540
8541 goodlen = STRLEN(goodsound);
8542 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008543
8544 /* Return quickly if the lenghts are too different to be fixed by two
8545 * changes. */
8546 n = goodlen - badlen;
8547 if (n < -2 || n > 2)
8548 return SCORE_MAXMAX;
8549
8550 if (n > 0)
8551 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008552 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008553 ps = badsound;
8554 }
8555 else
8556 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008557 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008558 ps = goodsound;
8559 }
8560
8561 /* Skip over the identical part. */
8562 while (*pl == *ps && *pl != NUL)
8563 {
8564 ++pl;
8565 ++ps;
8566 }
8567
8568 switch (n)
8569 {
8570 case -2:
8571 case 2:
8572 /*
8573 * Must delete two characters from "pl".
8574 */
8575 ++pl; /* first delete */
8576 while (*pl == *ps)
8577 {
8578 ++pl;
8579 ++ps;
8580 }
8581 /* strings must be equal after second delete */
8582 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008583 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008584
8585 /* Failed to compare. */
8586 break;
8587
8588 case -1:
8589 case 1:
8590 /*
8591 * Minimal one delete from "pl" required.
8592 */
8593
8594 /* 1: delete */
8595 pl2 = pl + 1;
8596 ps2 = ps;
8597 while (*pl2 == *ps2)
8598 {
8599 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008600 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008601 ++pl2;
8602 ++ps2;
8603 }
8604
8605 /* 2: delete then swap, then rest must be equal */
8606 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
8607 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008608 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008609
8610 /* 3: delete then substitute, then the rest must be equal */
8611 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008612 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008613
8614 /* 4: first swap then delete */
8615 if (pl[0] == ps[1] && pl[1] == ps[0])
8616 {
8617 pl2 = pl + 2; /* swap, skip two chars */
8618 ps2 = ps + 2;
8619 while (*pl2 == *ps2)
8620 {
8621 ++pl2;
8622 ++ps2;
8623 }
8624 /* delete a char and then strings must be equal */
8625 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008626 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008627 }
8628
8629 /* 5: first substitute then delete */
8630 pl2 = pl + 1; /* substitute, skip one char */
8631 ps2 = ps + 1;
8632 while (*pl2 == *ps2)
8633 {
8634 ++pl2;
8635 ++ps2;
8636 }
8637 /* delete a char and then strings must be equal */
8638 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008639 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008640
8641 /* Failed to compare. */
8642 break;
8643
8644 case 0:
8645 /*
8646 * Lenghts are equal, thus changes must result in same length: An
8647 * insert is only possible in combination with a delete.
8648 * 1: check if for identical strings
8649 */
8650 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008651 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008652
8653 /* 2: swap */
8654 if (pl[0] == ps[1] && pl[1] == ps[0])
8655 {
8656 pl2 = pl + 2; /* swap, skip two chars */
8657 ps2 = ps + 2;
8658 while (*pl2 == *ps2)
8659 {
8660 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008661 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008662 ++pl2;
8663 ++ps2;
8664 }
8665 /* 3: swap and swap again */
8666 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
8667 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008668 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008669
8670 /* 4: swap and substitute */
8671 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008672 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008673 }
8674
8675 /* 5: substitute */
8676 pl2 = pl + 1;
8677 ps2 = ps + 1;
8678 while (*pl2 == *ps2)
8679 {
8680 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008681 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008682 ++pl2;
8683 ++ps2;
8684 }
8685
8686 /* 6: substitute and swap */
8687 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
8688 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008689 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008690
8691 /* 7: substitute and substitute */
8692 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008693 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008694
8695 /* 8: insert then delete */
8696 pl2 = pl;
8697 ps2 = ps + 1;
8698 while (*pl2 == *ps2)
8699 {
8700 ++pl2;
8701 ++ps2;
8702 }
8703 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008704 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008705
8706 /* 9: delete then insert */
8707 pl2 = pl + 1;
8708 ps2 = ps;
8709 while (*pl2 == *ps2)
8710 {
8711 ++pl2;
8712 ++ps2;
8713 }
8714 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008715 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008716
8717 /* Failed to compare. */
8718 break;
8719 }
8720
8721 return SCORE_MAXMAX;
8722}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008723
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008724/*
8725 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008726 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008727 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008728 * The algorithm comes from Aspell editdist.cpp, edit_distance().
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008729 * It has been converted from C++ to C and modified to support multi-byte
8730 * characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008731 */
8732 static int
8733spell_edit_score(badword, goodword)
8734 char_u *badword;
8735 char_u *goodword;
8736{
8737 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008738 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008739 int j, i;
8740 int t;
8741 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008742 int pbc, pgc;
8743#ifdef FEAT_MBYTE
8744 char_u *p;
8745 int wbadword[MAXWLEN];
8746 int wgoodword[MAXWLEN];
8747
8748 if (has_mbyte)
8749 {
8750 /* Get the characters from the multi-byte strings and put them in an
8751 * int array for easy access. */
8752 for (p = badword, badlen = 0; *p != NUL; )
8753 wbadword[badlen++] = mb_ptr2char_adv(&p);
8754 ++badlen;
8755 for (p = goodword, goodlen = 0; *p != NUL; )
8756 wgoodword[goodlen++] = mb_ptr2char_adv(&p);
8757 ++goodlen;
8758 }
8759 else
8760#endif
8761 {
8762 badlen = STRLEN(badword) + 1;
8763 goodlen = STRLEN(goodword) + 1;
8764 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008765
8766 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
8767#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008768 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
8769 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008770 if (cnt == NULL)
8771 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008772
8773 CNT(0, 0) = 0;
8774 for (j = 1; j <= goodlen; ++j)
8775 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
8776
8777 for (i = 1; i <= badlen; ++i)
8778 {
8779 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
8780 for (j = 1; j <= goodlen; ++j)
8781 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008782#ifdef FEAT_MBYTE
8783 if (has_mbyte)
8784 {
8785 bc = wbadword[i - 1];
8786 gc = wgoodword[j - 1];
8787 }
8788 else
8789#endif
8790 {
8791 bc = badword[i - 1];
8792 gc = goodword[j - 1];
8793 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008794 if (bc == gc)
8795 CNT(i, j) = CNT(i - 1, j - 1);
8796 else
8797 {
8798 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008799 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008800 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
8801 else
8802 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
8803
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008804 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008805 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008806#ifdef FEAT_MBYTE
8807 if (has_mbyte)
8808 {
8809 pbc = wbadword[i - 2];
8810 pgc = wgoodword[j - 2];
8811 }
8812 else
8813#endif
8814 {
8815 pbc = badword[i - 2];
8816 pgc = goodword[j - 2];
8817 }
8818 if (bc == pgc && pbc == gc)
8819 {
8820 t = SCORE_SWAP + CNT(i - 2, j - 2);
8821 if (t < CNT(i, j))
8822 CNT(i, j) = t;
8823 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008824 }
8825 t = SCORE_DEL + CNT(i - 1, j);
8826 if (t < CNT(i, j))
8827 CNT(i, j) = t;
8828 t = SCORE_INS + CNT(i, j - 1);
8829 if (t < CNT(i, j))
8830 CNT(i, j) = t;
8831 }
8832 }
8833 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008834
8835 i = CNT(badlen - 1, goodlen - 1);
8836 vim_free(cnt);
8837 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008838}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008839
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008840/*
8841 * ":spelldump"
8842 */
8843/*ARGSUSED*/
8844 void
8845ex_spelldump(eap)
8846 exarg_T *eap;
8847{
8848 buf_T *buf = curbuf;
8849 langp_T *lp;
8850 slang_T *slang;
8851 idx_T arridx[MAXWLEN];
8852 int curi[MAXWLEN];
8853 char_u word[MAXWLEN];
8854 int c;
8855 char_u *byts;
8856 idx_T *idxs;
8857 linenr_T lnum = 0;
8858 int round;
8859 int depth;
8860 int n;
8861 int flags;
8862
8863 if (no_spell_checking())
8864 return;
8865
8866 /* Create a new empty buffer by splitting the window. */
8867 do_cmdline_cmd((char_u *)"new");
8868 if (!bufempty() || !buf_valid(buf))
8869 return;
8870
8871 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
8872 {
8873 slang = lp->lp_slang;
8874
8875 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
8876 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
8877
8878 /* round 1: case-folded tree
8879 * round 2: keep-case tree */
8880 for (round = 1; round <= 2; ++round)
8881 {
8882 if (round == 1)
8883 {
8884 byts = slang->sl_fbyts;
8885 idxs = slang->sl_fidxs;
8886 }
8887 else
8888 {
8889 byts = slang->sl_kbyts;
8890 idxs = slang->sl_kidxs;
8891 }
8892 if (byts == NULL)
8893 continue; /* array is empty */
8894
8895 depth = 0;
8896 arridx[0] = 0;
8897 curi[0] = 1;
8898 while (depth >= 0 && !got_int)
8899 {
8900 if (curi[depth] > byts[arridx[depth]])
8901 {
8902 /* Done all bytes at this node, go up one level. */
8903 --depth;
8904 line_breakcheck();
8905 }
8906 else
8907 {
8908 /* Do one more byte at this node. */
8909 n = arridx[depth] + curi[depth];
8910 ++curi[depth];
8911 c = byts[n];
8912 if (c == 0)
8913 {
8914 /* End of word, deal with the word.
8915 * Don't use keep-case words in the fold-case tree,
8916 * they will appear in the keep-case tree.
8917 * Only use the word when the region matches. */
8918 flags = (int)idxs[n];
8919 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
8920 && ((flags & WF_REGION) == 0
8921 || (((unsigned)flags >> 8)
8922 & lp->lp_region) != 0))
8923 {
8924 word[depth] = NUL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008925
8926 /* Dump the basic word if there is no prefix or
8927 * when it's the first one. */
8928 c = (unsigned)flags >> 16;
8929 if (c == 0 || curi[depth] == 2)
8930 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008931
8932 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008933 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008934 lnum = apply_prefixes(slang, word, round,
8935 flags, lnum);
8936 }
8937 }
8938 else
8939 {
8940 /* Normal char, go one level deeper. */
8941 word[depth++] = c;
8942 arridx[depth] = idxs[n];
8943 curi[depth] = 1;
8944 }
8945 }
8946 }
8947 }
8948 }
8949
8950 /* Delete the empty line that we started with. */
8951 if (curbuf->b_ml.ml_line_count > 1)
8952 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
8953
8954 redraw_later(NOT_VALID);
8955}
8956
8957/*
8958 * Dump one word: apply case modifications and append a line to the buffer.
8959 */
8960 static void
8961dump_word(word, round, flags, lnum)
8962 char_u *word;
8963 int round;
8964 int flags;
8965 linenr_T lnum;
8966{
8967 int keepcap = FALSE;
8968 char_u *p;
8969 char_u cword[MAXWLEN];
8970 char_u badword[MAXWLEN + 3];
8971
8972 if (round == 1 && (flags & WF_CAPMASK) != 0)
8973 {
8974 /* Need to fix case according to "flags". */
8975 make_case_word(word, cword, flags);
8976 p = cword;
8977 }
8978 else
8979 {
8980 p = word;
8981 if (round == 2 && (captype(word, NULL) & WF_KEEPCAP) == 0)
8982 keepcap = TRUE;
8983 }
8984
8985 /* Bad word is preceded by "/!" and some other
8986 * flags. */
8987 if ((flags & (WF_BANNED | WF_RARE)) || keepcap)
8988 {
8989 STRCPY(badword, "/");
8990 if (keepcap)
8991 STRCAT(badword, "=");
8992 if (flags & WF_BANNED)
8993 STRCAT(badword, "!");
8994 else if (flags & WF_RARE)
8995 STRCAT(badword, "?");
8996 STRCAT(badword, p);
8997 p = badword;
8998 }
8999
9000 ml_append(lnum, p, (colnr_T)0, FALSE);
9001}
9002
9003/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009004 * For ":spelldump": Find matching prefixes for "word". Prepend each to
9005 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009006 * Return the updated line number.
9007 */
9008 static linenr_T
9009apply_prefixes(slang, word, round, flags, startlnum)
9010 slang_T *slang;
9011 char_u *word; /* case-folded word */
9012 int round;
9013 int flags; /* flags with prefix ID */
9014 linenr_T startlnum;
9015{
9016 idx_T arridx[MAXWLEN];
9017 int curi[MAXWLEN];
9018 char_u prefix[MAXWLEN];
9019 int c;
9020 char_u *byts;
9021 idx_T *idxs;
9022 linenr_T lnum = startlnum;
9023 int depth;
9024 int n;
9025 int len;
9026 int prefid = (unsigned)flags >> 16;
9027 int i;
9028
9029 byts = slang->sl_pbyts;
9030 idxs = slang->sl_pidxs;
9031 if (byts != NULL) /* array not is empty */
9032 {
9033 /*
9034 * Loop over all prefixes, building them byte-by-byte in prefix[].
9035 * When at the end of a prefix check that it supports "prefid".
9036 */
9037 depth = 0;
9038 arridx[0] = 0;
9039 curi[0] = 1;
9040 while (depth >= 0 && !got_int)
9041 {
9042 len = arridx[depth];
9043 if (curi[depth] > byts[len])
9044 {
9045 /* Done all bytes at this node, go up one level. */
9046 --depth;
9047 line_breakcheck();
9048 }
9049 else
9050 {
9051 /* Do one more byte at this node. */
9052 n = len + curi[depth];
9053 ++curi[depth];
9054 c = byts[n];
9055 if (c == 0)
9056 {
9057 /* End of prefix, find out how many IDs there are. */
9058 for (i = 1; i < len; ++i)
9059 if (byts[n + i] != 0)
9060 break;
9061 curi[depth] += i - 1;
9062
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009063 i = valid_word_prefix(i, n, prefid, word, slang);
9064 if (i != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009065 {
9066 vim_strncpy(prefix + depth, word, MAXWLEN - depth);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009067 dump_word(prefix, round,
9068 (i & WF_RAREPFX) ? (flags | WF_RARE)
9069 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009070 }
9071 }
9072 else
9073 {
9074 /* Normal char, go one level deeper. */
9075 prefix[depth++] = c;
9076 arridx[depth] = idxs[n];
9077 curi[depth] = 1;
9078 }
9079 }
9080 }
9081 }
9082
9083 return lnum;
9084}
9085
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009086#endif /* FEAT_SYN_HL */