blob: e806476461e9287236fb8e7999a479620f512a37 [file] [log] [blame]
Bram Moolenaare19defe2005-03-21 08:23:33 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * spell.c: code for spell checking
Bram Moolenaarfc735152005-03-22 22:54:12 +000012 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000013 * The spell checking mechanism uses a tree (aka trie). Each node in the tree
14 * has a list of bytes that can appear (siblings). For each byte there is a
15 * pointer to the node with the byte that follows in the word (child).
Bram Moolenaar9f30f502005-06-14 22:01:04 +000016 *
17 * A NUL byte is used where the word may end. The bytes are sorted, so that
18 * binary searching can be used and the NUL bytes are at the start. The
19 * number of possible bytes is stored before the list of bytes.
20 *
21 * The tree uses two arrays: "byts" stores the characters, "idxs" stores
22 * either the next index or flags. The tree starts at index 0. For example,
23 * to lookup "vi" this sequence is followed:
24 * i = 0
25 * len = byts[i]
26 * n = where "v" appears in byts[i + 1] to byts[i + len]
27 * i = idxs[n]
28 * len = byts[i]
29 * n = where "i" appears in byts[i + 1] to byts[i + len]
30 * i = idxs[n]
31 * len = byts[i]
32 * find that byts[i + 1] is 0, idxs[i + 1] has flags for "vi".
Bram Moolenaar51485f02005-06-04 21:55:20 +000033 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000034 * There are two word trees: one with case-folded words and one with words in
Bram Moolenaar51485f02005-06-04 21:55:20 +000035 * original case. The second one is only used for keep-case words and is
36 * usually small.
37 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000038 * There is one additional tree for when prefixes are not applied when
39 * generating the .spl file. This tree stores all the possible prefixes, as
40 * if they were words. At each word (prefix) end the prefix nr is stored, the
41 * following word must support this prefix nr. And the condition nr is
42 * stored, used to lookup the condition that the word must match with.
43 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000044 * Thanks to Olaf Seibert for providing an example implementation of this tree
45 * and the compression mechanism.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000046 *
47 * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000048 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000049 * Why doesn't Vim use aspell/ispell/myspell/etc.?
50 * See ":help develop-spell".
51 */
52
Bram Moolenaar51485f02005-06-04 21:55:20 +000053/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000054 * Use this to adjust the score after finding suggestions, based on the
55 * suggested word sounding like the bad word. This is much faster than doing
56 * it for every possible suggestion.
57 * Disadvantage: When "the" is typed as "hte" it sounds different and goes
58 * down in the list.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000059 * Used when 'spellsuggest' is set to "best".
60 */
61#define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4)
62
63/*
64 * The double scoring mechanism is based on the principle that there are two
65 * kinds of spelling mistakes:
66 * 1. You know how to spell the word, but mistype something. This results in
67 * a small editing distance (character swapped/omitted/inserted) and
68 * possibly a word that sounds completely different.
69 * 2. You don't know how to spell the word and type something that sounds
70 * right. The edit distance can be big but the word is similar after
71 * sound-folding.
72 * Since scores for these two mistakes will be very different we use a list
73 * for each.
74 * The sound-folding is slow, only do double scoring when 'spellsuggest' is
75 * "double".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000076 */
77
78/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +000079 * Vim spell file format: <HEADER>
80 * <SUGGEST>
81 * <LWORDTREE>
82 * <KWORDTREE>
83 * <PREFIXTREE>
Bram Moolenaar51485f02005-06-04 21:55:20 +000084 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000085 * <HEADER>: <fileID>
86 * <regioncnt> <regionname> ...
87 * <charflagslen> <charflags>
88 * <fcharslen> <fchars>
89 * <prefcondcnt> <prefcond> ...
Bram Moolenaar51485f02005-06-04 21:55:20 +000090 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000091 * <fileID> 10 bytes "VIMspell07"
Bram Moolenaar51485f02005-06-04 21:55:20 +000092 * <regioncnt> 1 byte number of regions following (8 supported)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000093 * <regionname> 2 bytes Region name: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +000094 * First <regionname> is region 1.
95 *
96 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
97 * <charflags> N bytes List of flags (first one is for character 128):
Bram Moolenaar9f30f502005-06-14 22:01:04 +000098 * 0x01 word character CF_WORD
99 * 0x02 upper-case character CF_UPPER
Bram Moolenaar51485f02005-06-04 21:55:20 +0000100 * <fcharslen> 2 bytes Number of bytes in <fchars>.
101 * <fchars> N bytes Folded characters, first one is for character 128.
102 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000103 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
104 *
105 * <prefcond> : <condlen> <condstr>
106 *
107 * <condlen> 1 byte Length of <condstr>.
108 *
109 * <condstr> N bytes Condition for the prefix.
110 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000111 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000112 * <SUGGEST> : <repcount> <rep> ...
113 * <salflags> <salcount> <sal> ...
114 * <maplen> <mapstr>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000115 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000116 * <repcount> 2 bytes number of <rep> items, MSB first.
117 *
118 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
119 *
120 * <repfromlen> 1 byte length of <repfrom>
121 *
122 * <repfrom> N bytes "from" part of replacement
123 *
124 * <reptolen> 1 byte length of <repto>
125 *
126 * <repto> N bytes "to" part of replacement
127 *
128 * <salflags> 1 byte flags for soundsalike conversion:
129 * SAL_F0LLOWUP
130 * SAL_COLLAPSE
131 * SAL_REM_ACCENTS
132 *
133 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
134 *
135 * <salfromlen> 1 byte length of <salfrom>
136 *
137 * <salfrom> N bytes "from" part of soundsalike
138 *
139 * <saltolen> 1 byte length of <salto>
140 *
141 * <salto> N bytes "to" part of soundsalike
142 *
143 * <maplen> 2 bytes length of <mapstr>, MSB first
144 *
145 * <mapstr> N bytes String with sequences of similar characters,
146 * separated by slashes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000147 *
148 *
149 * <LWORDTREE>: <wordtree>
150 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000151 * <KWORDTREE>: <wordtree>
152 *
153 * <PREFIXTREE>: <wordtree>
154 *
155 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000156 * <wordtree>: <nodecount> <nodedata> ...
157 *
158 * <nodecount> 4 bytes Number of nodes following. MSB first.
159 *
160 * <nodedata>: <siblingcount> <sibling> ...
161 *
162 * <siblingcount> 1 byte Number of siblings in this node. The siblings
163 * follow in sorted order.
164 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000165 * <sibling>: <byte> [ <nodeidx> <xbyte>
166 * | <flags> [<region>] [<prefixID>]
167 * | <prefixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000168 *
169 * <byte> 1 byte Byte value of the sibling. Special cases:
170 * BY_NOFLAGS: End of word without flags and for all
171 * regions.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000172 * BY_FLAGS: End of word, <flags> follow. For
173 * PREFIXTREE <prefixID> and <prefcondnr>
174 * follow.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000175 * BY_INDEX: Child of sibling is shared, <nodeidx>
176 * and <xbyte> follow.
177 *
178 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
179 *
180 * <xbyte> 1 byte byte value of the sibling.
181 *
182 * <flags> 1 byte bitmask of:
183 * WF_ALLCAP word must have only capitals
184 * WF_ONECAP first char of word must be capital
185 * WF_RARE rare word
186 * WF_REGION <region> follows
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000187 * WF_PFX <prefixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000188 *
189 * <region> 1 byte Bitmask for regions in which word is valid. When
190 * omitted it's valid in all regions.
191 * Lowest bit is for region 1.
192 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000193 * <prefixID> 1 byte ID of prefix that can be used with this word. For
194 * PREFIXTREE used for the required prefix ID.
195 *
196 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
197 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000198 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000199 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000200 */
201
Bram Moolenaare19defe2005-03-21 08:23:33 +0000202#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
203# include <io.h> /* for lseek(), must be before vim.h */
204#endif
205
206#include "vim.h"
207
208#if defined(FEAT_SYN_HL) || defined(PROTO)
209
210#ifdef HAVE_FCNTL_H
211# include <fcntl.h>
212#endif
213
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000214#define MAXWLEN 250 /* Assume max. word len is this many bytes.
215 Some places assume a word length fits in a
216 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000217
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000218/* Type used for indexes in the word tree need to be at least 3 bytes. If int
219 * is 8 bytes we could use something smaller, but what? */
220#if SIZEOF_INT > 2
221typedef int idx_T;
222#else
223typedef long idx_T;
224#endif
225
226/* Flags used for a word. Only the lowest byte can be used, the region byte
227 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000228#define WF_REGION 0x01 /* region byte follows */
229#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
230#define WF_ALLCAP 0x04 /* word must be all capitals */
231#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000232#define WF_BANNED 0x10 /* bad word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000233#define WF_PFX 0x20 /* prefix ID list follows */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000234#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000235
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000236#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000237
238#define BY_NOFLAGS 0 /* end of word without flags or region */
239#define BY_FLAGS 1 /* end of word, flag byte follows */
240#define BY_INDEX 2 /* child is shared, index follows */
241#define BY_SPECIAL BY_INDEX /* hightest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000242
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000243/* Info from "REP" and "SAL" entries in ".aff" file used in si_rep, sl_rep,
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000244 * and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000245 * One replacement: from "ft_from" to "ft_to". */
246typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000247{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000248 char_u *ft_from;
249 char_u *ft_to;
250} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000251
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000252/* Info from "SAL" entries in ".aff" file used in sl_sal.
253 * The info is split for quick processing by spell_soundfold().
254 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
255typedef struct salitem_S
256{
257 char_u *sm_lead; /* leading letters */
258 int sm_leadlen; /* length of "sm_lead" */
259 char_u *sm_oneoff; /* letters from () or NULL */
260 char_u *sm_rules; /* rules like ^, $, priority */
261 char_u *sm_to; /* replacement. */
262} salitem_T;
263
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000264/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000265 * Structure used to store words and other info for one language, loaded from
266 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000267 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
268 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
269 *
270 * The "byts" array stores the possible bytes in each tree node, preceded by
271 * the number of possible bytes, sorted on byte value:
272 * <len> <byte1> <byte2> ...
273 * The "idxs" array stores the index of the child node corresponding to the
274 * byte in "byts".
275 * Exception: when the byte is zero, the word may end here and "idxs" holds
276 * the flags and region for the word. There may be several zeros in sequence
277 * for alternative flag/region combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000278 */
279typedef struct slang_S slang_T;
280struct slang_S
281{
282 slang_T *sl_next; /* next language */
283 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000284 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000285 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000286
Bram Moolenaar51485f02005-06-04 21:55:20 +0000287 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000288 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000289 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000290 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000291 char_u *sl_pbyts; /* prefix tree word bytes */
292 idx_T *sl_pidxs; /* prefix tree word indexes */
293
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000294 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000295
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000296 int sl_prefixcnt; /* number of items in "sl_prefprog" */
297 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
298
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000299 garray_T sl_rep; /* list of fromto_T entries from REP lines */
300 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
301 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000302 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000303 short sl_sal_first[256]; /* indexes where byte first appears, -1 if
304 there is none */
305 int sl_followup; /* SAL followup */
306 int sl_collapse; /* SAL collapse_result */
307 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaarea424162005-06-16 21:51:00 +0000308 int sl_has_map; /* TRUE if there is a MAP line */
309#ifdef FEAT_MBYTE
310 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
311 int sl_map_array[256]; /* MAP for first 256 chars */
312#else
313 char_u sl_map_array[256]; /* MAP for first 256 chars */
314#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000315};
316
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000317/* First language that is loaded, start of the linked list of loaded
318 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000319static slang_T *first_lang = NULL;
320
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000321/* Flags used in .spl file for soundsalike flags. */
322#define SAL_F0LLOWUP 1
323#define SAL_COLLAPSE 2
324#define SAL_REM_ACCENTS 4
325
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000326/*
327 * Structure used in "b_langp", filled from 'spelllang'.
328 */
329typedef struct langp_S
330{
331 slang_T *lp_slang; /* info for this language (NULL for last one) */
332 int lp_region; /* bitmask for region or REGION_ALL */
333} langp_T;
334
335#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
336
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000337#define REGION_ALL 0xff /* word valid in all regions */
338
339/* Result values. Lower number is accepted over higher one. */
340#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000341#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000342#define SP_RARE 1
343#define SP_LOCAL 2
344#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000345
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000346#define VIMSPELLMAGIC "VIMspell07" /* string at start of Vim spell file */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000347#define VIMSPELLMAGICL 10
348
349/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000350 * Information used when looking for suggestions.
351 */
352typedef struct suginfo_S
353{
354 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000355 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000356 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000357 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000358 char_u *su_badptr; /* start of bad word in line */
359 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000360 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000361 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
362 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
363 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000364} suginfo_T;
365
366/* One word suggestion. Used in "si_ga". */
367typedef struct suggest_S
368{
369 char_u *st_word; /* suggested word, allocated string */
370 int st_orglen; /* length of replaced text */
371 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000372 int st_altscore; /* used when st_score compares equal */
373 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000374 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000375} suggest_T;
376
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000377#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000378
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000379/* Number of suggestions kept when cleaning up. When rescore_suggestions() is
380 * called the score may change, thus we need to keep more than what is
381 * displayed. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000382#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 50 ? 50 : (su)->su_maxcount)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000383
384/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
385 * of suggestions that are not going to be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000386#define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000387
388/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000389#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000390#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000391#define SCORE_REGION 70 /* word is for different region */
392#define SCORE_RARE 180 /* rare word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000393#define SCORE_SWAP 90 /* swap two characters */
394#define SCORE_SWAP3 110 /* swap two characters in three */
395#define SCORE_REP 87 /* REP replacement */
396#define SCORE_SUBST 93 /* substitute a character */
397#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000398#define SCORE_DEL 94 /* delete a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000399#define SCORE_DELDUP 64 /* delete a duplicated character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000400#define SCORE_INS 96 /* insert a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000401#define SCORE_INSDUP 66 /* insert a duplicate character */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000402
403#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
404 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000405
406#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000407#define SCORE_MAXMAX 999999 /* accept any score */
408
409/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000410 * Structure to store info for word matching.
411 */
412typedef struct matchinf_S
413{
414 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000415
416 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000417 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000418 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000419 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000420 char_u *mi_cend; /* char after what was used for
421 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000422
423 /* case-folded text */
424 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000425 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000426
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000427 /* for when checking word after a prefix */
428 int mi_prefarridx; /* index in sl_pidxs with list of
429 prefixID/condition */
430 int mi_prefcnt; /* number of entries at mi_prefarridx */
431 int mi_prefixlen; /* byte length of prefix */
432
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000433 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000434 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000435 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000436} matchinf_T;
437
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000438/*
439 * The tables used for recognizing word characters according to spelling.
440 * These are only used for the first 256 characters of 'encoding'.
441 */
442typedef struct spelltab_S
443{
444 char_u st_isw[256]; /* flags: is word char */
445 char_u st_isu[256]; /* flags: is uppercase char */
446 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000447 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000448} spelltab_T;
449
450static spelltab_T spelltab;
451static int did_set_spelltab;
452
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000453#define CF_WORD 0x01
454#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000455
456static void clear_spell_chartab __ARGS((spelltab_T *sp));
457static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaarea408852005-06-25 22:49:46 +0000458static int spell_iswordp __ARGS((char_u *p));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000459static void write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000460
461/*
Bram Moolenaarea408852005-06-25 22:49:46 +0000462 * Return TRUE if "p" points to a word character. Like spell_iswordp() but
463 * without the special handling of a single quote.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000464 * Checking for a word character is done very often, avoid the function call
465 * overhead.
466 */
467#ifdef FEAT_MBYTE
468# define SPELL_ISWORDP(p) ((has_mbyte && MB_BYTE2LEN(*(p)) > 1) \
469 ? (mb_get_class(p) >= 2) : spelltab.st_isw[*(p)])
470#else
471# define SPELL_ISWORDP(p) (spelltab.st_isw[*(p)])
472#endif
473
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000474/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000475 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000476 */
477typedef enum
478{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000479 STATE_START = 0, /* At start of node check for NUL bytes (goodword
480 * ends); if badword ends there is a match, otherwise
481 * try splitting word. */
482 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000483 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
484 STATE_PLAIN, /* Use each byte of the node. */
485 STATE_DEL, /* Delete a byte from the bad word. */
486 STATE_INS, /* Insert a byte in the bad word. */
487 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000488 STATE_UNSWAP, /* Undo swap two characters. */
489 STATE_SWAP3, /* Swap two characters over three. */
490 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000491 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000492 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000493 STATE_REP_INI, /* Prepare for using REP items. */
494 STATE_REP, /* Use matching REP items from the .aff file. */
495 STATE_REP_UNDO, /* Undo a REP item replacement. */
496 STATE_FINAL /* End of this node. */
497} state_T;
498
499/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000500 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000501 */
502typedef struct trystate_S
503{
Bram Moolenaarea424162005-06-16 21:51:00 +0000504 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000505 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000506 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000507 short ts_curi; /* index in list of child nodes */
508 char_u ts_fidx; /* index in fword[], case-folded bad word */
509 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
510 char_u ts_twordlen; /* valid length of tword[] */
511#ifdef FEAT_MBYTE
512 char_u ts_tcharlen; /* number of bytes in tword character */
513 char_u ts_tcharidx; /* current byte index in tword character */
514 char_u ts_isdiff; /* DIFF_ values */
515 char_u ts_fcharstart; /* index in fword where badword char started */
516#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000517 char_u ts_save_prewordlen; /* saved "prewordlen" */
Bram Moolenaarea424162005-06-16 21:51:00 +0000518 char_u ts_save_splitoff; /* su_splitoff saved here */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000519 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000520} trystate_T;
521
Bram Moolenaarea424162005-06-16 21:51:00 +0000522/* values for ts_isdiff */
523#define DIFF_NONE 0 /* no different byte (yet) */
524#define DIFF_YES 1 /* different byte found */
525#define DIFF_INSERT 2 /* inserting character */
526
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000527/* mode values for find_word */
528#define FIND_FOLDWORD 0 /* find word case-folded */
529#define FIND_KEEPWORD 1 /* find keep-case word */
530#define FIND_PREFIX 2 /* find word after prefix */
531
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000532static slang_T *slang_alloc __ARGS((char_u *lang));
533static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000534static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000535static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000536static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int prefid, char_u *word, slang_T *slang));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000537static void find_prefix __ARGS((matchinf_T *mip));
538static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000539static int spell_valid_case __ARGS((int origflags, int treeflags));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000540static int no_spell_checking __ARGS((void));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000541static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000542static char_u *spell_enc __ARGS((void));
543static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000544static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000545static 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 +0000546static int find_region __ARGS((char_u *rp, char_u *region));
547static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000548static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000549static int set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000550static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
551static void write_spell_chartab __ARGS((FILE *fd));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000552static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaarea408852005-06-25 22:49:46 +0000553static void spell_find_suggest __ARGS((char_u *badptr, suginfo_T *su, int maxcount, int banbadword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000554static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000555static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000556static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000557static void suggest_try_special __ARGS((suginfo_T *su));
558static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000559static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
560static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000561static void score_comp_sal __ARGS((suginfo_T *su));
562static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000563static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000564static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000565static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000566static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000567static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000568static 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 +0000569static void add_banned __ARGS((suginfo_T *su, char_u *word));
570static int was_banned __ARGS((suginfo_T *su, char_u *word));
571static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000572static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000573static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000574static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000575static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000576static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000577static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum));
578static 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 +0000579
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000580/*
581 * Use our own character-case definitions, because the current locale may
582 * differ from what the .spl file uses.
583 * These must not be called with negative number!
584 */
585#ifndef FEAT_MBYTE
586/* Non-multi-byte implementation. */
587# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
588# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
589# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
590#else
591/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
592 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
593 * the "w" library function for characters above 255 if available. */
594# ifdef HAVE_TOWLOWER
595# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
596 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
597# else
598# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
599 : (c) < 256 ? spelltab.st_fold[c] : (c))
600# endif
601
602# ifdef HAVE_TOWUPPER
603# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
604 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
605# else
606# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
607 : (c) < 256 ? spelltab.st_upper[c] : (c))
608# endif
609
610# ifdef HAVE_ISWUPPER
611# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
612 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
613# else
614# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
615 : (c) < 256 ? spelltab.st_isu[c] : (c))
616# endif
617#endif
618
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000619
620static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000621
622/*
623 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000624 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000625 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
626 * or when it's OK it remains unchanged.
627 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000628 *
629 * "sug" is normally NULL. When looking for suggestions it points to
630 * suginfo_T. It's passed as a void pointer to keep the struct local.
631 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000632 * Returns the length of the word in bytes, also when it's OK, so that the
633 * caller can skip over the word.
634 */
635 int
Bram Moolenaar51485f02005-06-04 21:55:20 +0000636spell_check(wp, ptr, attrp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000637 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000638 char_u *ptr;
639 int *attrp;
640{
641 matchinf_T mi; /* Most things are put in "mi" so that it can
642 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000643 int nrlen = 0; /* found a number first */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000644
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000645 /* A word never starts at a space or a control character. Return quickly
646 * then, skipping over the character. */
647 if (*ptr <= ' ')
648 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000649
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000650 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar0c405862005-06-22 22:26:26 +0000651 * 0X99FF. But when a word character follows do check spelling to find
652 * "3GPP". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000653 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000654 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000655 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
656 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000657 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000658 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000659 mi.mi_end = skipdigits(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000660 nrlen = mi.mi_end - ptr;
661 }
Bram Moolenaarea408852005-06-25 22:49:46 +0000662 if (!spell_iswordp(mi.mi_end))
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000663 return (int)(mi.mi_end - ptr);
Bram Moolenaar0c405862005-06-22 22:26:26 +0000664
665 /* Try including the digits in the word. */
666 mi.mi_fend = ptr + nrlen;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000667 }
Bram Moolenaar0c405862005-06-22 22:26:26 +0000668 else
669 mi.mi_fend = ptr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000670
Bram Moolenaar0c405862005-06-22 22:26:26 +0000671 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000672 mi.mi_word = ptr;
Bram Moolenaarea408852005-06-25 22:49:46 +0000673 if (spell_iswordp(mi.mi_fend))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000674 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000675 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000676 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000677 mb_ptr_adv(mi.mi_fend);
Bram Moolenaarea408852005-06-25 22:49:46 +0000678 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000679 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000680
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000681 /* We always use the characters up to the next non-word character,
682 * also for bad words. */
683 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000684
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000685 /* Check caps type later. */
686 mi.mi_capflags = 0;
687 mi.mi_cend = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000688
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000689 /* Include one non-word character so that we can check for the
690 * word end. */
691 if (*mi.mi_fend != NUL)
692 mb_ptr_adv(mi.mi_fend);
693
694 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
695 MAXWLEN + 1);
696 mi.mi_fwordlen = STRLEN(mi.mi_fword);
697
698 /* The word is bad unless we recognize it. */
699 mi.mi_result = SP_BAD;
700
701 /*
702 * Loop over the languages specified in 'spelllang'.
703 * We check them all, because a matching word may be longer than an
704 * already found matching word.
705 */
706 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
707 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
708 {
709 /* Check for a matching word in case-folded words. */
710 find_word(&mi, FIND_FOLDWORD);
711
712 /* Check for a matching word in keep-case words. */
713 find_word(&mi, FIND_KEEPWORD);
714
715 /* Check for matching prefixes. */
716 find_prefix(&mi);
717 }
718
719 if (mi.mi_result != SP_OK)
720 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000721 /* If we found a number skip over it. Allows for "42nd". Do flag
722 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000723 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000724 {
725 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
726 return nrlen;
727 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000728
729 /* When we are at a non-word character there is no error, just
730 * skip over the character (try looking for a word after it). */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000731 else if (!SPELL_ISWORDP(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000732 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000733#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000734 if (has_mbyte)
735 return mb_ptr2len_check(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000736#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000737 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000738 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000739
740 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
741 *attrp = highlight_attr[HLF_SPB];
742 else if (mi.mi_result == SP_RARE)
743 *attrp = highlight_attr[HLF_SPR];
744 else
745 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000746 }
747
Bram Moolenaar51485f02005-06-04 21:55:20 +0000748 return (int)(mi.mi_end - ptr);
749}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000750
Bram Moolenaar51485f02005-06-04 21:55:20 +0000751/*
752 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000753 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
754 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
755 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
756 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000757 *
758 * For a match mip->mi_result is updated.
759 */
760 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000761find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000762 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000763 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000764{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000765 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000766 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000767 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000768 int endidxcnt = 0;
769 int len;
770 int wlen = 0;
771 int flen;
772 int c;
773 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000774 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000775#ifdef FEAT_MBYTE
776 char_u *s;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000777 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000778#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000779 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000780 slang_T *slang = mip->mi_lp->lp_slang;
781 unsigned flags;
782 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000783 idx_T *idxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000784 int prefid;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000785
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000786 if (mode == FIND_KEEPWORD)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000787 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000788 /* Check for word with matching case in keep-case tree. */
789 ptr = mip->mi_word;
790 flen = 9999; /* no case folding, always enough bytes */
791 byts = slang->sl_kbyts;
792 idxs = slang->sl_kidxs;
793 }
794 else
795 {
796 /* Check for case-folded in case-folded tree. */
797 ptr = mip->mi_fword;
798 flen = mip->mi_fwordlen; /* available case-folded bytes */
799 byts = slang->sl_fbyts;
800 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000801
802 if (mode == FIND_PREFIX)
803 {
804 /* Skip over the prefix. */
805 wlen = mip->mi_prefixlen;
806 flen -= mip->mi_prefixlen;
807 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000808 }
809
Bram Moolenaar51485f02005-06-04 21:55:20 +0000810 if (byts == NULL)
811 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000812
Bram Moolenaar51485f02005-06-04 21:55:20 +0000813 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000814 * Repeat advancing in the tree until:
815 * - there is a byte that doesn't match,
816 * - we reach the end of the tree,
817 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000818 */
819 for (;;)
820 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000821 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000822 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000823
824 len = byts[arridx++];
825
826 /* If the first possible byte is a zero the word could end here.
827 * Remember this index, we first check for the longest word. */
828 if (byts[arridx] == 0)
829 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000830 if (endidxcnt == MAXWLEN)
831 {
832 /* Must be a corrupted spell file. */
833 EMSG(_(e_format));
834 return;
835 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000836 endlen[endidxcnt] = wlen;
837 endidx[endidxcnt++] = arridx++;
838 --len;
839
840 /* Skip over the zeros, there can be several flag/region
841 * combinations. */
842 while (len > 0 && byts[arridx] == 0)
843 {
844 ++arridx;
845 --len;
846 }
847 if (len == 0)
848 break; /* no children, word must end here */
849 }
850
851 /* Stop looking at end of the line. */
852 if (ptr[wlen] == NUL)
853 break;
854
855 /* Perform a binary search in the list of accepted bytes. */
856 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +0000857 if (c == TAB) /* <Tab> is handled like <Space> */
858 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +0000859 lo = arridx;
860 hi = arridx + len - 1;
861 while (lo < hi)
862 {
863 m = (lo + hi) / 2;
864 if (byts[m] > c)
865 hi = m - 1;
866 else if (byts[m] < c)
867 lo = m + 1;
868 else
869 {
870 lo = hi = m;
871 break;
872 }
873 }
874
875 /* Stop if there is no matching byte. */
876 if (hi < lo || byts[lo] != c)
877 break;
878
879 /* Continue at the child (if there is one). */
880 arridx = idxs[lo];
881 ++wlen;
882 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +0000883
884 /* One space in the good word may stand for several spaces in the
885 * checked word. */
886 if (c == ' ')
887 {
888 for (;;)
889 {
890 if (flen <= 0 && *mip->mi_fend != NUL)
891 flen = fold_more(mip);
892 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
893 break;
894 ++wlen;
895 --flen;
896 }
897 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000898 }
899
900 /*
901 * Verify that one of the possible endings is valid. Try the longest
902 * first.
903 */
904 while (endidxcnt > 0)
905 {
906 --endidxcnt;
907 arridx = endidx[endidxcnt];
908 wlen = endlen[endidxcnt];
909
910#ifdef FEAT_MBYTE
911 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
912 continue; /* not at first byte of character */
913#endif
Bram Moolenaarea408852005-06-25 22:49:46 +0000914 if (spell_iswordp(ptr + wlen))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000915 continue; /* next char is a word character */
916
917#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000918 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000919 {
920 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000921 * when folding case. This can be slow, take a shortcut when the
922 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000923 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000924 if (STRNCMP(ptr, p, wlen) != 0)
925 {
926 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
927 mb_ptr_adv(p);
928 wlen = p - mip->mi_word;
929 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000930 }
931#endif
932
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000933 /* Check flags and region. For FIND_PREFIX check the condition and
934 * prefix ID.
935 * Repeat this if there are more flags/region alternatives until there
936 * is a match. */
937 res = SP_BAD;
938 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
939 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000940 {
941 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000942
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000943 /* For the fold-case tree check that the case of the checked word
944 * matches with what the word in the tree requires.
945 * For keep-case tree the case is always right. For prefixes we
946 * don't bother to check. */
947 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000948 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000949 if (mip->mi_cend != mip->mi_word + wlen)
950 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000951 /* mi_capflags was set for a different word length, need
952 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000953 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000954 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000955 }
956
Bram Moolenaar0c405862005-06-22 22:26:26 +0000957 if (mip->mi_capflags == WF_KEEPCAP
958 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000959 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000960 }
961
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000962 /* When mode is FIND_PREFIX the word must support the prefix:
963 * check the prefix ID and the condition. Do that for the list at
964 * mip->mi_prefarridx. */
965 if (mode == FIND_PREFIX)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000966 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000967 /* The prefix ID is stored two bytes above the flags. */
968 prefid = (unsigned)flags >> 16;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000969 if (!valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
970 prefid, mip->mi_fword + mip->mi_prefixlen,
971 slang))
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000972 continue;
973 }
974
975 if (flags & WF_BANNED)
976 res = SP_BANNED;
977 else if (flags & WF_REGION)
978 {
979 /* Check region. */
980 if ((mip->mi_lp->lp_region & (flags >> 8)) != 0)
981 res = SP_OK;
982 else
983 res = SP_LOCAL;
984 }
985 else if (flags & WF_RARE)
986 res = SP_RARE;
987 else
988 res = SP_OK;
989
990 /* Always use the longest match and the best result. */
991 if (mip->mi_result > res)
992 {
993 mip->mi_result = res;
994 mip->mi_end = mip->mi_word + wlen;
995 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000996 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000997 mip->mi_end = mip->mi_word + wlen;
998
999 if (res == SP_OK)
1000 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001001 }
1002
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001003 if (res == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001004 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001005 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001006}
1007
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001008/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001009 * Return TRUE if the prefix indicated by "mip->mi_prefarridx" matches with
1010 * the prefix ID "prefid" for the word "word".
1011 */
1012 static int
1013valid_word_prefix(totprefcnt, arridx, prefid, word, slang)
1014 int totprefcnt; /* nr of prefix IDs */
1015 int arridx; /* idx in sl_pidxs[] */
1016 int prefid;
1017 char_u *word;
1018 slang_T *slang;
1019{
1020 int prefcnt;
1021 int pidx;
1022 regprog_T *rp;
1023 regmatch_T regmatch;
1024
1025 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1026 {
1027 pidx = slang->sl_pidxs[arridx + prefcnt];
1028
1029 /* Check the prefix ID. */
1030 if (prefid != (pidx & 0xff))
1031 continue;
1032
1033 /* Check the condition, if there is one. The condition index is
1034 * stored above the prefix ID byte. */
1035 rp = slang->sl_prefprog[(unsigned)pidx >> 8];
1036 if (rp != NULL)
1037 {
1038 regmatch.regprog = rp;
1039 regmatch.rm_ic = FALSE;
1040 if (!vim_regexec(&regmatch, word, 0))
1041 continue;
1042 }
1043
1044 /* It's a match! */
1045 return TRUE;
1046 }
1047 return FALSE;
1048}
1049
1050/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001051 * Check if the word at "mip->mi_word" has a matching prefix.
1052 * If it does, then check the following word.
1053 *
1054 * For a match mip->mi_result is updated.
1055 */
1056 static void
1057find_prefix(mip)
1058 matchinf_T *mip;
1059{
1060 idx_T arridx = 0;
1061 int len;
1062 int wlen = 0;
1063 int flen;
1064 int c;
1065 char_u *ptr;
1066 idx_T lo, hi, m;
1067 slang_T *slang = mip->mi_lp->lp_slang;
1068 char_u *byts;
1069 idx_T *idxs;
1070
1071 /* We use the case-folded word here, since prefixes are always
1072 * case-folded. */
1073 ptr = mip->mi_fword;
1074 flen = mip->mi_fwordlen; /* available case-folded bytes */
1075 byts = slang->sl_pbyts;
1076 idxs = slang->sl_pidxs;
1077
1078 if (byts == NULL)
1079 return; /* array is empty */
1080
1081 /*
1082 * Repeat advancing in the tree until:
1083 * - there is a byte that doesn't match,
1084 * - we reach the end of the tree,
1085 * - or we reach the end of the line.
1086 */
1087 for (;;)
1088 {
1089 if (flen == 0 && *mip->mi_fend != NUL)
1090 flen = fold_more(mip);
1091
1092 len = byts[arridx++];
1093
1094 /* If the first possible byte is a zero the prefix could end here.
1095 * Check if the following word matches and supports the prefix. */
1096 if (byts[arridx] == 0)
1097 {
1098 /* There can be several prefixes with different conditions. We
1099 * try them all, since we don't know which one will give the
1100 * longest match. The word is the same each time, pass the list
1101 * of possible prefixes to find_word(). */
1102 mip->mi_prefarridx = arridx;
1103 mip->mi_prefcnt = len;
1104 while (len > 0 && byts[arridx] == 0)
1105 {
1106 ++arridx;
1107 --len;
1108 }
1109 mip->mi_prefcnt -= len;
1110
1111 /* Find the word that comes after the prefix. */
1112 mip->mi_prefixlen = wlen;
1113 find_word(mip, FIND_PREFIX);
1114
1115
1116 if (len == 0)
1117 break; /* no children, word must end here */
1118 }
1119
1120 /* Stop looking at end of the line. */
1121 if (ptr[wlen] == NUL)
1122 break;
1123
1124 /* Perform a binary search in the list of accepted bytes. */
1125 c = ptr[wlen];
1126 lo = arridx;
1127 hi = arridx + len - 1;
1128 while (lo < hi)
1129 {
1130 m = (lo + hi) / 2;
1131 if (byts[m] > c)
1132 hi = m - 1;
1133 else if (byts[m] < c)
1134 lo = m + 1;
1135 else
1136 {
1137 lo = hi = m;
1138 break;
1139 }
1140 }
1141
1142 /* Stop if there is no matching byte. */
1143 if (hi < lo || byts[lo] != c)
1144 break;
1145
1146 /* Continue at the child (if there is one). */
1147 arridx = idxs[lo];
1148 ++wlen;
1149 --flen;
1150 }
1151}
1152
1153/*
1154 * Need to fold at least one more character. Do until next non-word character
1155 * for efficiency.
1156 * Return the length of the folded chars in bytes.
1157 */
1158 static int
1159fold_more(mip)
1160 matchinf_T *mip;
1161{
1162 int flen;
1163 char_u *p;
1164
1165 p = mip->mi_fend;
1166 do
1167 {
1168 mb_ptr_adv(mip->mi_fend);
Bram Moolenaarea408852005-06-25 22:49:46 +00001169 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001170
1171 /* Include the non-word character so that we can check for the
1172 * word end. */
1173 if (*mip->mi_fend != NUL)
1174 mb_ptr_adv(mip->mi_fend);
1175
1176 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1177 mip->mi_fword + mip->mi_fwordlen,
1178 MAXWLEN - mip->mi_fwordlen);
1179 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1180 mip->mi_fwordlen += flen;
1181 return flen;
1182}
1183
1184/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001185 * Check case flags for a word. Return TRUE if the word has the requested
1186 * case.
1187 */
1188 static int
1189spell_valid_case(origflags, treeflags)
1190 int origflags; /* flags for the checked word. */
1191 int treeflags; /* flags for the word in the spell tree */
1192{
1193 return (origflags == WF_ALLCAP
1194 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
1195 && ((treeflags & WF_ONECAP) == 0 || origflags == WF_ONECAP)));
1196}
1197
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001198/*
1199 * Return TRUE if spell checking is not enabled.
1200 */
1201 static int
1202no_spell_checking()
1203{
1204 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
1205 {
1206 EMSG(_("E756: Spell checking is not enabled"));
1207 return TRUE;
1208 }
1209 return FALSE;
1210}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001211
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001212/*
1213 * Move to next spell error.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001214 * "curline" is TRUE for "z?": find word under/after cursor in the same line.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001215 * Return OK if found, FAIL otherwise.
1216 */
1217 int
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001218spell_move_to(dir, allwords, curline)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001219 int dir; /* FORWARD or BACKWARD */
1220 int allwords; /* TRUE for "[s" and "]s" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001221 int curline;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001222{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001223 linenr_T lnum;
1224 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001225 char_u *line;
1226 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001227 char_u *endp;
1228 int attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001229 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001230 int has_syntax = syntax_present(curbuf);
1231 int col;
1232 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001233 char_u *buf = NULL;
1234 int buflen = 0;
1235 int skip = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001236
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001237 if (no_spell_checking())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001238 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001239
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001240 /*
1241 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001242 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001243 *
1244 * When searching backwards, we continue in the line to find the last
1245 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001246 *
1247 * We concatenate the start of the next line, so that wrapped words work
1248 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1249 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001250 */
1251 lnum = curwin->w_cursor.lnum;
1252 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001253
1254 while (!got_int)
1255 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001256 line = ml_get(lnum);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001257
Bram Moolenaar0c405862005-06-22 22:26:26 +00001258 len = STRLEN(line);
1259 if (buflen < len + MAXWLEN + 2)
1260 {
1261 vim_free(buf);
1262 buflen = len + MAXWLEN + 2;
1263 buf = alloc(buflen);
1264 if (buf == NULL)
1265 break;
1266 }
1267
1268 /* Copy the line into "buf" and append the start of the next line if
1269 * possible. */
1270 STRCPY(buf, line);
1271 if (lnum < curbuf->b_ml.ml_line_count)
1272 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1273
1274 p = buf + skip;
1275 endp = buf + len;
1276 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001277 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001278 /* When searching backward don't search after the cursor. */
1279 if (dir == BACKWARD
1280 && lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001281 && (colnr_T)(p - buf) >= curwin->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001282 break;
1283
1284 /* start of word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00001285 attr = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001286 len = spell_check(curwin, p, &attr);
1287
1288 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001289 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001290 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001291 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001292 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001293 /* When searching forward only accept a bad word after
1294 * the cursor. */
1295 if (dir == BACKWARD
1296 || lnum > curwin->w_cursor.lnum
1297 || (lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001298 && (colnr_T)(curline ? p - buf + len
1299 : p - buf)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001300 > curwin->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001301 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001302 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001303 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001304 col = p - buf;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001305 (void)syn_get_id(lnum, (colnr_T)col,
1306 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001307 }
1308 else
1309 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001310
Bram Moolenaar51485f02005-06-04 21:55:20 +00001311 if (can_spell)
1312 {
1313 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001314 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001315#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001316 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001317#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001318 if (dir == FORWARD)
1319 {
1320 /* No need to search further. */
1321 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001322 vim_free(buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001323 return OK;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001324 }
1325 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001326 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001327 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001328 }
1329
Bram Moolenaar51485f02005-06-04 21:55:20 +00001330 /* advance to character after the word */
1331 p += len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001332 }
1333
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001334 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001335 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001336
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001337 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001338 if (dir == BACKWARD)
1339 {
1340 if (found_pos.lnum != 0)
1341 {
1342 /* Use the last match in the line. */
1343 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001344 vim_free(buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001345 return OK;
1346 }
1347 if (lnum == 1)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001348 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001349 --lnum;
1350 }
1351 else
1352 {
1353 if (lnum == curbuf->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001354 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001355 ++lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001356
1357 /* Skip the characters at the start of the next line that were
1358 * included in a match crossing line boundaries. */
1359 if (attr == 0)
1360 skip = p - endp;
1361 else
1362 skip = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001363 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001364
1365 line_breakcheck();
1366 }
1367
Bram Moolenaar0c405862005-06-22 22:26:26 +00001368 vim_free(buf);
1369 return FAIL;
1370}
1371
1372/*
1373 * For spell checking: concatenate the start of the following line "line" into
1374 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
1375 */
1376 void
1377spell_cat_line(buf, line, maxlen)
1378 char_u *buf;
1379 char_u *line;
1380 int maxlen;
1381{
1382 char_u *p;
1383 int n;
1384
1385 p = skipwhite(line);
1386 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1387 p = skipwhite(p + 1);
1388
1389 if (*p != NUL)
1390 {
1391 *buf = ' ';
1392 vim_strncpy(buf + 1, line, maxlen - 1);
1393 n = p - line;
1394 if (n >= maxlen)
1395 n = maxlen - 1;
1396 vim_memset(buf + 1, ' ', n);
1397 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001398}
1399
1400/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001401 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001402 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001403 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001404 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001405spell_load_lang(lang)
1406 char_u *lang;
1407{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001408 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001409 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001410 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001411
Bram Moolenaarb765d632005-06-07 21:00:02 +00001412 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001413 * It's truncated when an error is detected. */
1414 STRCPY(langcp, lang);
1415
Bram Moolenaarb765d632005-06-07 21:00:02 +00001416 /*
1417 * Find the first spell file for "lang" in 'runtimepath' and load it.
1418 */
1419 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
1420 "spell/%s.%s.spl", lang, spell_enc());
1421 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001422
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001423 if (r == FAIL && *langcp != NUL)
1424 {
1425 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001426 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001427 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001428 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001429 }
1430
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001431 if (r == FAIL)
1432 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
1433 fname_enc + 6);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001434 else if (*langcp != NUL)
1435 {
1436 /* Load all the additions. */
1437 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
1438 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
1439 }
1440}
1441
1442/*
1443 * Return the encoding used for spell checking: Use 'encoding', except that we
1444 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1445 */
1446 static char_u *
1447spell_enc()
1448{
1449
1450#ifdef FEAT_MBYTE
1451 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1452 return p_enc;
1453#endif
1454 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001455}
1456
1457/*
1458 * Allocate a new slang_T.
1459 * Caller must fill "sl_next".
1460 */
1461 static slang_T *
1462slang_alloc(lang)
1463 char_u *lang;
1464{
1465 slang_T *lp;
1466
Bram Moolenaar51485f02005-06-04 21:55:20 +00001467 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001468 if (lp != NULL)
1469 {
1470 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001471 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001472 ga_init2(&lp->sl_sal, sizeof(salitem_T), 10);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001473 }
1474 return lp;
1475}
1476
1477/*
1478 * Free the contents of an slang_T and the structure itself.
1479 */
1480 static void
1481slang_free(lp)
1482 slang_T *lp;
1483{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001484 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001485 vim_free(lp->sl_fname);
1486 slang_clear(lp);
1487 vim_free(lp);
1488}
1489
1490/*
1491 * Clear an slang_T so that the file can be reloaded.
1492 */
1493 static void
1494slang_clear(lp)
1495 slang_T *lp;
1496{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001497 garray_T *gap;
1498 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001499 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001500 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001501
Bram Moolenaar51485f02005-06-04 21:55:20 +00001502 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001503 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001504 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001505 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001506 vim_free(lp->sl_pbyts);
1507 lp->sl_pbyts = NULL;
1508
Bram Moolenaar51485f02005-06-04 21:55:20 +00001509 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001510 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001511 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001512 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001513 vim_free(lp->sl_pidxs);
1514 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001515
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001516 gap = &lp->sl_rep;
1517 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001518 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001519 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1520 vim_free(ftp->ft_from);
1521 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001522 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001523 ga_clear(gap);
1524
1525 gap = &lp->sl_sal;
1526 while (gap->ga_len > 0)
1527 {
1528 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1529 vim_free(smp->sm_lead);
1530 vim_free(smp->sm_to);
1531 }
1532 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001533
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001534 for (i = 0; i < lp->sl_prefixcnt; ++i)
1535 vim_free(lp->sl_prefprog[i]);
1536 vim_free(lp->sl_prefprog);
1537
Bram Moolenaarea424162005-06-16 21:51:00 +00001538#ifdef FEAT_MBYTE
1539 {
1540 int todo = lp->sl_map_hash.ht_used;
1541 hashitem_T *hi;
1542
1543 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
1544 if (!HASHITEM_EMPTY(hi))
1545 {
1546 --todo;
1547 vim_free(hi->hi_key);
1548 }
1549 }
1550 hash_clear(&lp->sl_map_hash);
1551#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001552}
1553
1554/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001555 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001556 * Invoked through do_in_runtimepath().
1557 */
1558 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00001559spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001560 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001561 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001562{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001563 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001564}
1565
1566/*
1567 * Load one spell file and store the info into a slang_T.
1568 *
1569 * This is invoked in two ways:
1570 * - From spell_load_cb() to load a spell file for the first time. "lang" is
1571 * the language name, "old_lp" is NULL. Will allocate an slang_T.
1572 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
1573 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001574 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00001575 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001576 static slang_T *
1577spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001578 char_u *fname;
1579 char_u *lang;
1580 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001581 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001582{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001583 FILE *fd;
1584 char_u buf[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001585 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001586 char_u *bp;
1587 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001588 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001589 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001590 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001591 int round;
1592 char_u *save_sourcing_name = sourcing_name;
1593 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001594 int cnt, ccnt;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001595 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001596 slang_T *lp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001597 garray_T *gap;
1598 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001599 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001600 int rr;
1601 short *first;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001602 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001603 int c = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001604
Bram Moolenaarb765d632005-06-07 21:00:02 +00001605 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001606 if (fd == NULL)
1607 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001608 if (!silent)
1609 EMSG2(_(e_notopen), fname);
1610 else if (p_verbose > 2)
1611 {
1612 verbose_enter();
1613 smsg((char_u *)e_notopen, fname);
1614 verbose_leave();
1615 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001616 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001617 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00001618 if (p_verbose > 2)
1619 {
1620 verbose_enter();
1621 smsg((char_u *)_("Reading spell file \"%s\""), fname);
1622 verbose_leave();
1623 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001624
Bram Moolenaarb765d632005-06-07 21:00:02 +00001625 if (old_lp == NULL)
1626 {
1627 lp = slang_alloc(lang);
1628 if (lp == NULL)
1629 goto endFAIL;
1630
1631 /* Remember the file name, used to reload the file when it's updated. */
1632 lp->sl_fname = vim_strsave(fname);
1633 if (lp->sl_fname == NULL)
1634 goto endFAIL;
1635
1636 /* Check for .add.spl. */
1637 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
1638 }
1639 else
1640 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001641
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001642 /* Set sourcing_name, so that error messages mention the file name. */
1643 sourcing_name = fname;
1644 sourcing_lnum = 0;
1645
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001646 /* <HEADER>: <fileID>
1647 * <regioncnt> <regionname> ...
1648 * <charflagslen> <charflags>
1649 * <fcharslen> <fchars>
1650 * <prefcondcnt> <prefcond> ...
1651 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001652 for (i = 0; i < VIMSPELLMAGICL; ++i)
1653 buf[i] = getc(fd); /* <fileID> */
1654 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
1655 {
1656 EMSG(_("E757: Wrong file ID in spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001657 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001658 }
1659
1660 cnt = getc(fd); /* <regioncnt> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001661 if (cnt < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001662 {
1663truncerr:
1664 EMSG(_("E758: Truncated spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001665 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001666 }
1667 if (cnt > 8)
1668 {
1669formerr:
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001670 EMSG(_(e_format));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001671 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001672 }
1673 for (i = 0; i < cnt; ++i)
1674 {
1675 lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
1676 lp->sl_regions[i * 2 + 1] = getc(fd);
1677 }
1678 lp->sl_regions[cnt * 2] = NUL;
1679
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001680 cnt = getc(fd); /* <charflagslen> */
1681 if (cnt > 0)
1682 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001683 p = alloc((unsigned)cnt);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001684 if (p == NULL)
1685 goto endFAIL;
1686 for (i = 0; i < cnt; ++i)
1687 p[i] = getc(fd); /* <charflags> */
1688
1689 ccnt = (getc(fd) << 8) + getc(fd); /* <fcharslen> */
1690 if (ccnt <= 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001691 {
1692 vim_free(p);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001693 goto formerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001694 }
1695 fol = alloc((unsigned)ccnt + 1);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001696 if (fol == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001697 {
1698 vim_free(p);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001699 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001700 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001701 for (i = 0; i < ccnt; ++i)
1702 fol[i] = getc(fd); /* <fchars> */
1703 fol[i] = NUL;
1704
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001705 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001706 i = set_spell_charflags(p, cnt, fol);
1707 vim_free(p);
1708 vim_free(fol);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001709#if 0 /* tolerate the differences */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001710 if (i == FAIL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001711 goto formerr;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001712#endif
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001713 }
1714 else
1715 {
1716 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
1717 cnt = (getc(fd) << 8) + getc(fd);
1718 if (cnt != 0)
1719 goto formerr;
1720 }
1721
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001722 /* <prefcondcnt> <prefcond> ... */
1723 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
1724 if (cnt > 0)
1725 {
1726 lp->sl_prefprog = (regprog_T **)alloc_clear(
1727 (unsigned)sizeof(regprog_T *) * cnt);
1728 if (lp->sl_prefprog == NULL)
1729 goto endFAIL;
1730 lp->sl_prefixcnt = cnt;
1731
1732 for (i = 0; i < cnt; ++i)
1733 {
1734 /* <prefcond> : <condlen> <condstr> */
1735 n = getc(fd); /* <condlen> */
1736 if (n < 0)
1737 goto formerr;
1738 /* When <condlen> is zero we have an empty condition. Otherwise
1739 * compile the regexp program used to check for the condition. */
1740 if (n > 0)
1741 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001742 buf[0] = '^'; /* always match at one position only */
1743 p = buf + 1;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001744 while (n-- > 0)
1745 *p++ = getc(fd); /* <condstr> */
1746 *p = NUL;
1747 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
1748 }
1749 }
1750 }
1751
1752
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001753 /* <SUGGEST> : <repcount> <rep> ...
1754 * <salflags> <salcount> <sal> ...
1755 * <maplen> <mapstr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001756
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001757 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
1758 if (cnt < 0)
1759 goto formerr;
1760
1761 gap = &lp->sl_rep;
1762 if (ga_grow(gap, cnt) == FAIL)
1763 goto endFAIL;
1764
1765 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
1766 for (; gap->ga_len < cnt; ++gap->ga_len)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001767 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001768 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
1769 for (rr = 1; rr <= 2; ++rr)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001770 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001771 ccnt = getc(fd);
1772 if (ccnt < 0)
1773 {
1774 if (rr == 2)
1775 vim_free(ftp->ft_from);
1776 goto formerr;
1777 }
1778 if ((p = alloc(ccnt + 1)) == NULL)
1779 {
1780 if (rr == 2)
1781 vim_free(ftp->ft_from);
1782 goto endFAIL;
1783 }
1784 for (i = 0; i < ccnt; ++i)
1785 p[i] = getc(fd); /* <repfrom> or <repto> */
1786 p[i] = NUL;
1787 if (rr == 1)
1788 ftp->ft_from = p;
1789 else
1790 ftp->ft_to = p;
1791 }
1792 }
1793
1794 /* Fill the first-index table. */
1795 first = lp->sl_rep_first;
1796 for (i = 0; i < 256; ++i)
1797 first[i] = -1;
1798 for (i = 0; i < gap->ga_len; ++i)
1799 {
1800 ftp = &((fromto_T *)gap->ga_data)[i];
1801 if (first[*ftp->ft_from] == -1)
1802 first[*ftp->ft_from] = i;
1803 }
1804
1805 i = getc(fd); /* <salflags> */
1806 if (i & SAL_F0LLOWUP)
1807 lp->sl_followup = TRUE;
1808 if (i & SAL_COLLAPSE)
1809 lp->sl_collapse = TRUE;
1810 if (i & SAL_REM_ACCENTS)
1811 lp->sl_rem_accents = TRUE;
1812
1813 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
1814 if (cnt < 0)
1815 goto formerr;
1816
1817 gap = &lp->sl_sal;
1818 if (ga_grow(gap, cnt) == FAIL)
1819 goto endFAIL;
1820
1821 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
1822 for (; gap->ga_len < cnt; ++gap->ga_len)
1823 {
1824 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
1825 ccnt = getc(fd); /* <salfromlen> */
1826 if (ccnt < 0)
1827 goto formerr;
1828 if ((p = alloc(ccnt + 2)) == NULL)
1829 goto endFAIL;
1830 smp->sm_lead = p;
1831
1832 /* Read up to the first special char into sm_lead. */
1833 for (i = 0; i < ccnt; ++i)
1834 {
1835 c = getc(fd); /* <salfrom> */
1836 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
1837 break;
1838 *p++ = c;
1839 }
1840 smp->sm_leadlen = p - smp->sm_lead;
1841 *p++ = NUL;
1842
1843 /* Put optional chars in sm_oneoff, if any. */
1844 if (c == '(')
1845 {
1846 smp->sm_oneoff = p;
1847 for (++i; i < ccnt; ++i)
1848 {
1849 c = getc(fd); /* <salfrom> */
1850 if (c == ')')
1851 break;
1852 *p++ = c;
1853 }
1854 *p++ = NUL;
1855 if (++i < ccnt)
1856 c = getc(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001857 }
1858 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001859 smp->sm_oneoff = NULL;
1860
1861 /* Any following chars go in sm_rules. */
1862 smp->sm_rules = p;
1863 if (i < ccnt)
1864 *p++ = c;
1865 for (++i; i < ccnt; ++i)
1866 *p++ = getc(fd); /* <salfrom> */
1867 *p++ = NUL;
1868
1869 ccnt = getc(fd); /* <saltolen> */
1870 if (ccnt < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001871 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001872 vim_free(smp->sm_lead);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001873 goto formerr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001874 }
1875 if ((p = alloc(ccnt + 1)) == NULL)
1876 {
1877 vim_free(smp->sm_lead);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001878 goto endFAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001879 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001880 smp->sm_to = p;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001881
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001882 for (i = 0; i < ccnt; ++i)
1883 *p++ = getc(fd); /* <salto> */
1884 *p++ = NUL;
1885 }
1886
1887 /* Fill the first-index table. */
1888 first = lp->sl_sal_first;
1889 for (i = 0; i < 256; ++i)
1890 first[i] = -1;
1891 for (i = 0; i < gap->ga_len; ++i)
1892 {
1893 smp = &((salitem_T *)gap->ga_data)[i];
1894 if (first[*smp->sm_lead] == -1)
1895 first[*smp->sm_lead] = i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001896 }
1897
1898 cnt = (getc(fd) << 8) + getc(fd); /* <maplen> */
1899 if (cnt < 0)
1900 goto formerr;
1901 p = alloc(cnt + 1);
1902 if (p == NULL)
1903 goto endFAIL;
1904 for (i = 0; i < cnt; ++i)
1905 p[i] = getc(fd); /* <mapstr> */
1906 p[i] = NUL;
Bram Moolenaarea424162005-06-16 21:51:00 +00001907 set_map_str(lp, p);
1908 vim_free(p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001909
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001910
Bram Moolenaar51485f02005-06-04 21:55:20 +00001911 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001912 * round 2: <KWORDTREE>
1913 * round 3: <PREFIXTREE> */
1914 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001915 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001916 /* The tree size was computed when writing the file, so that we can
1917 * allocate it as one long block. <nodecount> */
1918 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
1919 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001920 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001921 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001922 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001923 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001924 bp = lalloc((long_u)len, TRUE);
1925 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001926 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001927 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001928 lp->sl_fbyts = bp;
1929 else if (round == 2)
1930 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001931 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001932 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001933
1934 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001935 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
1936 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001937 goto endFAIL;
1938 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001939 lp->sl_fidxs = ip;
1940 else if (round == 2)
1941 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001942 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001943 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001944
1945 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001946 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001947 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001948 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001949 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001950 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001951 }
1952 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001953
Bram Moolenaarb765d632005-06-07 21:00:02 +00001954 /* For a new file link it in the list of spell files. */
1955 if (old_lp == NULL)
1956 {
1957 lp->sl_next = first_lang;
1958 first_lang = lp;
1959 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001960
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001961 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001962
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001963endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00001964 if (lang != NULL)
1965 /* truncating the name signals the error to spell_load_lang() */
1966 *lang = NUL;
1967 if (lp != NULL && old_lp == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001968 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001969 slang_free(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001970 lp = NULL;
1971 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001972
1973endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001974 if (fd != NULL)
1975 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001976 sourcing_name = save_sourcing_name;
1977 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001978
1979 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001980}
1981
1982/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00001983 * Read one row of siblings from the spell file and store it in the byte array
1984 * "byts" and index array "idxs". Recursively read the children.
1985 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00001986 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00001987 *
1988 * Returns the index follosing the siblings.
1989 * Returns -1 if the file is shorter than expected.
1990 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001991 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001992 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001993read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001994 FILE *fd;
1995 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001996 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001997 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001998 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001999 int prefixtree; /* TRUE for reading PREFIXTREE */
2000 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002001{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002002 int len;
2003 int i;
2004 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002005 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002006 int c;
2007#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002008
Bram Moolenaar51485f02005-06-04 21:55:20 +00002009 len = getc(fd); /* <siblingcount> */
2010 if (len <= 0)
2011 return -1;
2012
2013 if (startidx + len >= maxidx)
2014 return -2;
2015 byts[idx++] = len;
2016
2017 /* Read the byte values, flag/region bytes and shared indexes. */
2018 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002019 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002020 c = getc(fd); /* <byte> */
2021 if (c < 0)
2022 return -1;
2023 if (c <= BY_SPECIAL)
2024 {
2025 if (c == BY_NOFLAGS)
2026 {
2027 /* No flags, all regions. */
2028 idxs[idx] = 0;
2029 c = 0;
2030 }
2031 else if (c == BY_FLAGS)
2032 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002033 if (prefixtree)
2034 {
2035 /* Read the prefix ID and the condition nr. In idxs[]
2036 * store the prefix ID in the low byte, the condition
2037 * index shifted up 8 bits. */
2038 c = getc(fd); /* <prefixID> */
2039 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
2040 if (n >= maxprefcondnr)
2041 return -2;
2042 c = (n << 8) + c;
2043 }
2044 else
2045 {
2046 /* Read flags and optional region and prefix ID. In
2047 * idxs[] the flags go in the low byte, region above that
2048 * and prefix ID above the region. */
2049 c = getc(fd); /* <flags> */
2050 if (c & WF_REGION)
2051 c = (getc(fd) << 8) + c; /* <region> */
2052 if (c & WF_PFX)
2053 c = (getc(fd) << 16) + c; /* <prefixID> */
2054 }
2055
Bram Moolenaar51485f02005-06-04 21:55:20 +00002056 idxs[idx] = c;
2057 c = 0;
2058 }
2059 else /* c == BY_INDEX */
2060 {
2061 /* <nodeidx> */
2062 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2063 if (n < 0 || n >= maxidx)
2064 return -2;
2065 idxs[idx] = n + SHARED_MASK;
2066 c = getc(fd); /* <xbyte> */
2067 }
2068 }
2069 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002070 }
2071
Bram Moolenaar51485f02005-06-04 21:55:20 +00002072 /* Recursively read the children for non-shared siblings.
2073 * Skip the end-of-word ones (zero byte value) and the shared ones (and
2074 * remove SHARED_MASK) */
2075 for (i = 1; i <= len; ++i)
2076 if (byts[startidx + i] != 0)
2077 {
2078 if (idxs[startidx + i] & SHARED_MASK)
2079 idxs[startidx + i] &= ~SHARED_MASK;
2080 else
2081 {
2082 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002083 idx = read_tree(fd, byts, idxs, maxidx, idx,
2084 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002085 if (idx < 0)
2086 break;
2087 }
2088 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002089
Bram Moolenaar51485f02005-06-04 21:55:20 +00002090 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002091}
2092
2093/*
2094 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002095 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002096 */
2097 char_u *
2098did_set_spelllang(buf)
2099 buf_T *buf;
2100{
2101 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002102 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002103 char_u *region;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002104 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002105 int region_mask;
2106 slang_T *lp;
2107 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002108 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002109 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002110 int load_spf;
2111 int len;
2112 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002113
2114 ga_init2(&ga, sizeof(langp_T), 2);
2115
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002116 /* Make the name of the .spl file associated with 'spellfile'. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002117 if (*buf->b_p_spf == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002118 load_spf = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002119 else
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002120 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002121 vim_snprintf((char *)spf_name, sizeof(spf_name), "%s.spl",
2122 buf->b_p_spf);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002123 load_spf = TRUE;
2124 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002125
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002126 /* loop over comma separated language names. */
2127 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002128 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002129 /* Get one language name. */
2130 copy_option_part(&splp, lang, MAXWLEN, ",");
2131
Bram Moolenaar5482f332005-04-17 20:18:43 +00002132 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002133 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002134
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002135 /* If the name ends in ".spl" use it as the name of the spell file.
2136 * If there is a region name let "region" point to it and remove it
2137 * from the name. */
2138 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2139 {
2140 filename = TRUE;
2141
2142 /* Check if we loaded this language before. */
2143 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2144 if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME)
2145 break;
2146 }
2147 else
2148 {
2149 filename = FALSE;
2150 if (len > 3 && lang[len - 3] == '_')
2151 {
2152 region = lang + len - 2;
2153 len -= 3;
2154 lang[len] = NUL;
2155 }
2156
2157 /* Check if we loaded this language before. */
2158 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2159 if (STRICMP(lang, lp->sl_name) == 0)
2160 break;
2161 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002162
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002163 /* If not found try loading the language now. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002164 if (lp == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002165 {
2166 if (filename)
2167 (void)spell_load_file(lang, lang, NULL, FALSE);
2168 else
2169 spell_load_lang(lang);
2170 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002171
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002172 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002173 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002174 */
2175 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002176 if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME
2177 : STRICMP(lang, lp->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002178 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002179 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002180 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002181 {
2182 /* find region in sl_regions */
2183 c = find_region(lp->sl_regions, region);
2184 if (c == REGION_ALL)
2185 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002186 if (!lp->sl_add)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002187 smsg((char_u *)
2188 _("Warning: region %s not supported"),
2189 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002190 }
2191 else
2192 region_mask = 1 << c;
2193 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002194
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002195 if (ga_grow(&ga, 1) == FAIL)
2196 {
2197 ga_clear(&ga);
2198 return e_outofmem;
2199 }
2200 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2201 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2202 ++ga.ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002203
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002204 /* Check if this is the spell file related to 'spellfile'. */
2205 if (load_spf && fullpathcmp(spf_name, lp->sl_fname, FALSE)
2206 == FPC_SAME)
2207 load_spf = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002208 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002209 }
2210
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002211 /*
2212 * Make sure the 'spellfile' file is loaded. It may be in 'runtimepath',
2213 * then it's probably loaded above already. Otherwise load it here.
2214 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002215 if (load_spf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002216 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002217 /* Check if it was loaded already. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002218 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2219 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
2220 break;
2221 if (lp == NULL)
2222 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002223 /* Not loaded, try loading it now. The language name includes the
2224 * region name, the region is ignored otherwise. */
2225 vim_strncpy(lang, gettail(buf->b_p_spf), MAXWLEN);
2226 p = vim_strchr(lang, '.');
2227 if (p != NULL)
2228 *p = NUL; /* truncate at ".encoding.add" */
2229 lp = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002230 }
2231 if (lp != NULL && ga_grow(&ga, 1) == OK)
2232 {
2233 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2234 LANGP_ENTRY(ga, ga.ga_len)->lp_region = REGION_ALL;
2235 ++ga.ga_len;
2236 }
2237 }
2238
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002239 /* Add a NULL entry to mark the end of the list. */
2240 if (ga_grow(&ga, 1) == FAIL)
2241 {
2242 ga_clear(&ga);
2243 return e_outofmem;
2244 }
2245 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
2246 ++ga.ga_len;
2247
2248 /* Everything is fine, store the new b_langp value. */
2249 ga_clear(&buf->b_langp);
2250 buf->b_langp = ga;
2251
2252 return NULL;
2253}
2254
2255/*
2256 * Find the region "region[2]" in "rp" (points to "sl_regions").
2257 * Each region is simply stored as the two characters of it's name.
2258 * Returns the index if found, REGION_ALL if not found.
2259 */
2260 static int
2261find_region(rp, region)
2262 char_u *rp;
2263 char_u *region;
2264{
2265 int i;
2266
2267 for (i = 0; ; i += 2)
2268 {
2269 if (rp[i] == NUL)
2270 return REGION_ALL;
2271 if (rp[i] == region[0] && rp[i + 1] == region[1])
2272 break;
2273 }
2274 return i / 2;
2275}
2276
2277/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002278 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002279 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002280 * Word WF_ONECAP
2281 * W WORD WF_ALLCAP
2282 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002283 */
2284 static int
2285captype(word, end)
2286 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002287 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002288{
2289 char_u *p;
2290 int c;
2291 int firstcap;
2292 int allcap;
2293 int past_second = FALSE; /* past second word char */
2294
2295 /* find first letter */
Bram Moolenaarea408852005-06-25 22:49:46 +00002296 for (p = word; !spell_iswordp(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002297 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002298 return 0; /* only non-word characters, illegal word */
2299#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00002300 if (has_mbyte)
2301 c = mb_ptr2char_adv(&p);
2302 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002303#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00002304 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002305 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002306
2307 /*
2308 * Need to check all letters to find a word with mixed upper/lower.
2309 * But a word with an upper char only at start is a ONECAP.
2310 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002311 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaarea408852005-06-25 22:49:46 +00002312 if (spell_iswordp(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002313 {
2314#ifdef FEAT_MBYTE
2315 c = mb_ptr2char(p);
2316#else
2317 c = *p;
2318#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002319 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002320 {
2321 /* UUl -> KEEPCAP */
2322 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002323 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002324 allcap = FALSE;
2325 }
2326 else if (!allcap)
2327 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002328 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002329 past_second = TRUE;
2330 }
2331
2332 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002333 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002334 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002335 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002336 return 0;
2337}
2338
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002339# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
2340/*
2341 * Free all languages.
2342 */
2343 void
2344spell_free_all()
2345{
2346 slang_T *lp;
2347 buf_T *buf;
2348
2349 /* Go through all buffers and handle 'spelllang'. */
2350 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2351 ga_clear(&buf->b_langp);
2352
2353 while (first_lang != NULL)
2354 {
2355 lp = first_lang;
2356 first_lang = lp->sl_next;
2357 slang_free(lp);
2358 }
2359}
2360# endif
2361
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002362# if defined(FEAT_MBYTE) || defined(PROTO)
2363/*
2364 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002365 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002366 */
2367 void
2368spell_reload()
2369{
2370 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002371 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002372
Bram Moolenaarea408852005-06-25 22:49:46 +00002373 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002374 init_spell_chartab();
2375
2376 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002377 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002378
2379 /* Go through all buffers and handle 'spelllang'. */
2380 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2381 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002382 /* Only load the wordlists when 'spelllang' is set and there is a
2383 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002384 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002385 {
2386 FOR_ALL_WINDOWS(wp)
2387 if (wp->w_buffer == buf && wp->w_p_spell)
2388 {
2389 (void)did_set_spelllang(buf);
2390# ifdef FEAT_WINDOWS
2391 break;
2392# endif
2393 }
2394 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002395 }
2396}
2397# endif
2398
Bram Moolenaarb765d632005-06-07 21:00:02 +00002399/*
2400 * Reload the spell file "fname" if it's loaded.
2401 */
2402 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002403spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002404 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002405 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002406{
2407 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002408 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002409
Bram Moolenaarb765d632005-06-07 21:00:02 +00002410 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2411 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
2412 {
2413 slang_clear(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002414 (void)spell_load_file(fname, NULL, lp, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002415 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002416 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002417 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002418
2419 /* When "zg" was used and the file wasn't loaded yet, should redo
2420 * 'spelllang' to get it loaded. */
2421 if (added_word && !didit)
2422 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002423}
2424
2425
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002426/*
2427 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002428 */
2429
Bram Moolenaar51485f02005-06-04 21:55:20 +00002430#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002431 and .dic file. */
2432/*
2433 * Main structure to store the contents of a ".aff" file.
2434 */
2435typedef struct afffile_S
2436{
2437 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002438 int af_rar; /* RAR ID for rare word */
2439 int af_kep; /* KEP ID for keep-case word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00002440 int af_bad; /* BAD ID for banned word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002441 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002442 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
2443 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002444} afffile_T;
2445
2446typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002447/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
2448struct affentry_S
2449{
2450 affentry_T *ae_next; /* next affix with same name/number */
2451 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
2452 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002453 char_u *ae_cond; /* condition (NULL for ".") */
2454 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002455};
2456
2457/* Affix header from ".aff" file. Used for af_pref and af_suff. */
2458typedef struct affheader_S
2459{
2460 char_u ah_key[2]; /* key for hashtable == name of affix entry */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002461 int ah_newID; /* prefix ID after renumbering */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002462 int ah_combine; /* suffix may combine with prefix */
2463 affentry_T *ah_first; /* first affix entry */
2464} affheader_T;
2465
2466#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
2467
2468/*
2469 * Structure that is used to store the items in the word tree. This avoids
2470 * the need to keep track of each allocated thing, it's freed all at once
2471 * after ":mkspell" is done.
2472 */
2473#define SBLOCKSIZE 16000 /* size of sb_data */
2474typedef struct sblock_S sblock_T;
2475struct sblock_S
2476{
2477 sblock_T *sb_next; /* next block in list */
2478 int sb_used; /* nr of bytes already in use */
2479 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002480};
2481
2482/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002483 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002484 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002485typedef struct wordnode_S wordnode_T;
2486struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002487{
Bram Moolenaar0c405862005-06-22 22:26:26 +00002488 union /* shared to save space */
2489 {
2490 char_u hashkey[6]; /* room for the hash key */
2491 int index; /* index in written nodes (valid after first
2492 round) */
2493 } wn_u1;
2494 union /* shared to save space */
2495 {
2496 wordnode_T *next; /* next node with same hash key */
2497 wordnode_T *wnode; /* parent node that will write this node */
2498 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002499 wordnode_T *wn_child; /* child (next byte in word) */
2500 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
2501 always sorted) */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002502 char_u wn_byte; /* Byte for this node. NUL for word end */
2503 char_u wn_flags; /* when wn_byte is NUL: WF_ flags */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002504 short wn_region; /* when wn_byte is NUL: region mask; for
2505 PREFIXTREE it's the prefcondnr */
2506 char_u wn_prefixID; /* supported/required prefix ID or 0 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002507};
2508
Bram Moolenaar51485f02005-06-04 21:55:20 +00002509#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002510
Bram Moolenaar51485f02005-06-04 21:55:20 +00002511/*
2512 * Info used while reading the spell files.
2513 */
2514typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002515{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002516 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00002517 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002518 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00002519 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002520 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002521 sblock_T *si_blocks; /* memory blocks used */
2522 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002523 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002524 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002525 int si_region; /* region mask */
2526 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00002527 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002528 int si_verbose; /* verbose messages */
Bram Moolenaar3982c542005-06-08 21:56:31 +00002529 int si_region_count; /* number of regions supported (1 when there
2530 are no regions) */
2531 char_u si_region_name[16]; /* region names (if count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002532
2533 garray_T si_rep; /* list of fromto_T entries from REP lines */
2534 garray_T si_sal; /* list of fromto_T entries from SAL lines */
2535 int si_followup; /* soundsalike: ? */
2536 int si_collapse; /* soundsalike: ? */
2537 int si_rem_accents; /* soundsalike: remove accents */
2538 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002539 garray_T si_prefcond; /* table with conditions for postponed
2540 * prefixes, each stored as a string */
2541 int si_newID; /* current value for ah_newID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002542} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002543
Bram Moolenaar51485f02005-06-04 21:55:20 +00002544static afffile_T *spell_read_aff __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002545static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002546static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
2547static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00002548static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00002549static void spell_free_aff __ARGS((afffile_T *aff));
2550static int spell_read_dic __ARGS((char_u *fname, spellinfo_T *spin, afffile_T *affile));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002551static char_u *get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, sblock_T **blp));
2552static 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 +00002553static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin));
2554static void *getroom __ARGS((sblock_T **blp, size_t len));
2555static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s));
2556static void free_blocks __ARGS((sblock_T *bl));
2557static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002558static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags, int region, char_u *pfxlist));
2559static 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 +00002560static void wordtree_compress __ARGS((wordnode_T *root, spellinfo_T *spin));
Bram Moolenaar51485f02005-06-04 21:55:20 +00002561static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot));
2562static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar3982c542005-06-08 21:56:31 +00002563static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar0c405862005-06-22 22:26:26 +00002564static void clear_node __ARGS((wordnode_T *node));
2565static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002566static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00002567static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002568
2569/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002570 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00002571 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002572 */
2573 static afffile_T *
Bram Moolenaar51485f02005-06-04 21:55:20 +00002574spell_read_aff(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002575 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002576 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002577{
2578 FILE *fd;
2579 afffile_T *aff;
2580 char_u rline[MAXLINELEN];
2581 char_u *line;
2582 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00002583#define MAXITEMCNT 7
2584 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002585 int itemcnt;
2586 char_u *p;
2587 int lnum = 0;
2588 affheader_T *cur_aff = NULL;
2589 int aff_todo = 0;
2590 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002591 char_u *low = NULL;
2592 char_u *fol = NULL;
2593 char_u *upp = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002594 static char *e_affname = N_("Affix name too long in %s line %d: %s");
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002595 int do_rep;
2596 int do_sal;
2597 int do_map;
2598 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002599 hashitem_T *hi;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002600
Bram Moolenaar51485f02005-06-04 21:55:20 +00002601 /*
2602 * Open the file.
2603 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002604 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002605 if (fd == NULL)
2606 {
2607 EMSG2(_(e_notopen), fname);
2608 return NULL;
2609 }
2610
Bram Moolenaarb765d632005-06-07 21:00:02 +00002611 if (spin->si_verbose || p_verbose > 2)
2612 {
2613 if (!spin->si_verbose)
2614 verbose_enter();
2615 smsg((char_u *)_("Reading affix file %s..."), fname);
2616 out_flush();
2617 if (!spin->si_verbose)
2618 verbose_leave();
2619 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002620
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002621 /* Only do REP lines when not done in another .aff file already. */
2622 do_rep = spin->si_rep.ga_len == 0;
2623
2624 /* Only do SAL lines when not done in another .aff file already. */
2625 do_sal = spin->si_sal.ga_len == 0;
2626
2627 /* Only do MAP lines when not done in another .aff file already. */
2628 do_map = spin->si_map.ga_len == 0;
2629
Bram Moolenaar51485f02005-06-04 21:55:20 +00002630 /*
2631 * Allocate and init the afffile_T structure.
2632 */
2633 aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002634 if (aff == NULL)
2635 return NULL;
2636 hash_init(&aff->af_pref);
2637 hash_init(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002638
2639 /*
2640 * Read all the lines in the file one by one.
2641 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002642 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002643 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002644 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002645 ++lnum;
2646
2647 /* Skip comment lines. */
2648 if (*rline == '#')
2649 continue;
2650
2651 /* Convert from "SET" to 'encoding' when needed. */
2652 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002653#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00002654 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002655 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002656 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002657 if (pc == NULL)
2658 {
2659 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
2660 fname, lnum, rline);
2661 continue;
2662 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002663 line = pc;
2664 }
2665 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00002666#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002667 {
2668 pc = NULL;
2669 line = rline;
2670 }
2671
2672 /* Split the line up in white separated items. Put a NUL after each
2673 * item. */
2674 itemcnt = 0;
2675 for (p = line; ; )
2676 {
2677 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
2678 ++p;
2679 if (*p == NUL)
2680 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00002681 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002682 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002683 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002684 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002685 ++p;
2686 if (*p == NUL)
2687 break;
2688 *p++ = NUL;
2689 }
2690
2691 /* Handle non-empty lines. */
2692 if (itemcnt > 0)
2693 {
2694 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
2695 && aff->af_enc == NULL)
2696 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00002697#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00002698 /* Setup for conversion from "ENC" to 'encoding'. */
2699 aff->af_enc = enc_canonize(items[1]);
2700 if (aff->af_enc != NULL && !spin->si_ascii
2701 && convert_setup(&spin->si_conv, aff->af_enc,
2702 p_enc) == FAIL)
2703 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
2704 fname, aff->af_enc, p_enc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002705#else
2706 smsg((char_u *)_("Conversion in %s not supported"), fname);
2707#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002708 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00002709 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
2710 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002711 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00002712 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002713 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002714 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002715 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002716 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002717 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
2718 && aff->af_rar == 0)
2719 {
2720 aff->af_rar = items[1][0];
2721 if (items[1][1] != NUL)
2722 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
2723 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002724 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
2725 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002726 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00002727 aff->af_kep = items[1][0];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002728 if (items[1][1] != NUL)
2729 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
2730 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00002731 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
2732 && aff->af_bad == 0)
2733 {
2734 aff->af_bad = items[1][0];
2735 if (items[1][1] != NUL)
2736 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
2737 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002738 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
2739 {
2740 aff->af_pfxpostpone = TRUE;
2741 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002742 else if ((STRCMP(items[0], "PFX") == 0
2743 || STRCMP(items[0], "SFX") == 0)
2744 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00002745 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002746 {
Bram Moolenaar8db73182005-06-17 21:51:16 +00002747 /* Myspell allows extra text after the item, but that might
2748 * mean mistakes go unnoticed. Require a comment-starter. */
2749 if (itemcnt > 4 && *items[4] != '#')
2750 smsg((char_u *)_("Trailing text in %s line %d: %s"),
2751 fname, lnum, items[4]);
2752
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002753 /* New affix letter. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002754 cur_aff = (affheader_T *)getroom(&spin->si_blocks,
2755 sizeof(affheader_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002756 if (cur_aff == NULL)
2757 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002758 cur_aff->ah_key[0] = *items[1]; /* TODO: multi-byte? */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002759 cur_aff->ah_key[1] = NUL;
2760 if (items[1][1] != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002761 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002762 if (*items[2] == 'Y')
2763 cur_aff->ah_combine = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002764 else if (*items[2] != 'N')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002765 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
2766 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002767
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002768 if (*items[0] == 'P')
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002769 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002770 tp = &aff->af_pref;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002771 /* Use a new number in the .spl file later, to be able to
2772 * handle multiple .aff files. */
2773 if (aff->af_pfxpostpone)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002774 cur_aff->ah_newID = ++spin->si_newID;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002775 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002776 else
2777 tp = &aff->af_suff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002778 aff_todo = atoi((char *)items[3]);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002779 hi = hash_find(tp, cur_aff->ah_key);
2780 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar51485f02005-06-04 21:55:20 +00002781 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002782 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
2783 fname, lnum, items[1]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002784 aff_todo = 0;
2785 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002786 else
2787 hash_add(tp, cur_aff->ah_key);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002788 }
2789 else if ((STRCMP(items[0], "PFX") == 0
2790 || STRCMP(items[0], "SFX") == 0)
2791 && aff_todo > 0
2792 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00002793 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002794 {
2795 affentry_T *aff_entry;
2796
Bram Moolenaar8db73182005-06-17 21:51:16 +00002797 /* Myspell allows extra text after the item, but that might
2798 * mean mistakes go unnoticed. Require a comment-starter. */
2799 if (itemcnt > 5 && *items[5] != '#')
2800 smsg((char_u *)_("Trailing text in %s line %d: %s"),
2801 fname, lnum, items[5]);
2802
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002803 /* New item for an affix letter. */
2804 --aff_todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002805 aff_entry = (affentry_T *)getroom(&spin->si_blocks,
2806 sizeof(affentry_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002807 if (aff_entry == NULL)
2808 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00002809
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002810 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002811 aff_entry->ae_chop = getroom_save(&spin->si_blocks,
2812 items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002813 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002814 aff_entry->ae_add = getroom_save(&spin->si_blocks,
2815 items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002816
Bram Moolenaar51485f02005-06-04 21:55:20 +00002817 /* Don't use an affix entry with non-ASCII characters when
2818 * "spin->si_ascii" is TRUE. */
2819 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00002820 || has_non_ascii(aff_entry->ae_add)))
2821 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00002822 aff_entry->ae_next = cur_aff->ah_first;
2823 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002824
2825 if (STRCMP(items[4], ".") != 0)
2826 {
2827 char_u buf[MAXLINELEN];
2828
2829 aff_entry->ae_cond = getroom_save(&spin->si_blocks,
2830 items[4]);
2831 if (*items[0] == 'P')
2832 sprintf((char *)buf, "^%s", items[4]);
2833 else
2834 sprintf((char *)buf, "%s$", items[4]);
2835 aff_entry->ae_prog = vim_regcomp(buf,
2836 RE_MAGIC + RE_STRING);
2837 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002838
2839 /* For postponed prefixes we need an entry in si_prefcond
2840 * for the condition. Use an existing one if possible. */
2841 if (*items[0] == 'P' && aff->af_pfxpostpone
2842 && aff_entry->ae_chop == NULL)
2843 {
2844 int idx;
2845 char_u **pp;
2846
2847 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
2848 --idx)
2849 {
2850 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
2851 if (str_equal(p, aff_entry->ae_cond))
2852 break;
2853 }
2854 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
2855 {
2856 /* Not found, add a new condition. */
2857 idx = spin->si_prefcond.ga_len++;
2858 pp = ((char_u **)spin->si_prefcond.ga_data) + idx;
2859 if (aff_entry->ae_cond == NULL)
2860 *pp = NULL;
2861 else
2862 *pp = getroom_save(&spin->si_blocks,
2863 aff_entry->ae_cond);
2864 }
2865
2866 /* Add the prefix to the prefix tree. */
2867 if (aff_entry->ae_add == NULL)
2868 p = (char_u *)"";
2869 else
2870 p = aff_entry->ae_add;
2871 tree_add_word(p, spin->si_prefroot, -1, idx,
2872 cur_aff->ah_newID, &spin->si_blocks);
2873 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00002874 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002875 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002876 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
2877 {
2878 if (fol != NULL)
2879 smsg((char_u *)_("Duplicate FOL in %s line %d"),
2880 fname, lnum);
2881 else
2882 fol = vim_strsave(items[1]);
2883 }
2884 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
2885 {
2886 if (low != NULL)
2887 smsg((char_u *)_("Duplicate LOW in %s line %d"),
2888 fname, lnum);
2889 else
2890 low = vim_strsave(items[1]);
2891 }
2892 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
2893 {
2894 if (upp != NULL)
2895 smsg((char_u *)_("Duplicate UPP in %s line %d"),
2896 fname, lnum);
2897 else
2898 upp = vim_strsave(items[1]);
2899 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002900 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002901 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002902 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002903 if (!isdigit(*items[1]))
2904 smsg((char_u *)_("Expected REP count in %s line %d"),
2905 fname, lnum);
2906 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002907 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
2908 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002909 /* REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002910 if (do_rep)
2911 add_fromto(spin, &spin->si_rep, items[1], items[2]);
2912 }
2913 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
2914 {
2915 /* MAP item or count */
2916 if (!found_map)
2917 {
2918 /* First line contains the count. */
2919 found_map = TRUE;
2920 if (!isdigit(*items[1]))
2921 smsg((char_u *)_("Expected MAP count in %s line %d"),
2922 fname, lnum);
2923 }
2924 else if (do_map)
2925 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00002926 int c;
2927
2928 /* Check that every character appears only once. */
2929 for (p = items[1]; *p != NUL; )
2930 {
2931#ifdef FEAT_MBYTE
2932 c = mb_ptr2char_adv(&p);
2933#else
2934 c = *p++;
2935#endif
2936 if ((spin->si_map.ga_len > 0
2937 && vim_strchr(spin->si_map.ga_data, c)
2938 != NULL)
2939 || vim_strchr(p, c) != NULL)
2940 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
2941 fname, lnum);
2942 }
2943
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002944 /* We simply concatenate all the MAP strings, separated by
2945 * slashes. */
2946 ga_concat(&spin->si_map, items[1]);
2947 ga_append(&spin->si_map, '/');
2948 }
2949 }
2950 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
2951 {
2952 if (do_sal)
2953 {
2954 /* SAL item (sounds-a-like)
2955 * Either one of the known keys or a from-to pair. */
2956 if (STRCMP(items[1], "followup") == 0)
2957 spin->si_followup = sal_to_bool(items[2]);
2958 else if (STRCMP(items[1], "collapse_result") == 0)
2959 spin->si_collapse = sal_to_bool(items[2]);
2960 else if (STRCMP(items[1], "remove_accents") == 0)
2961 spin->si_rem_accents = sal_to_bool(items[2]);
2962 else
2963 /* when "to" is "_" it means empty */
2964 add_fromto(spin, &spin->si_sal, items[1],
2965 STRCMP(items[2], "_") == 0 ? (char_u *)""
2966 : items[2]);
2967 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002968 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002969 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002970 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
2971 fname, lnum, items[0]);
2972 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002973 }
2974
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002975 if (fol != NULL || low != NULL || upp != NULL)
2976 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002977 if (spin->si_clear_chartab)
2978 {
2979 /* Clear the char type tables, don't want to use any of the
2980 * currently used spell properties. */
2981 init_spell_chartab();
2982 spin->si_clear_chartab = FALSE;
2983 }
2984
Bram Moolenaar3982c542005-06-08 21:56:31 +00002985 /*
2986 * Don't write a word table for an ASCII file, so that we don't check
2987 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002988 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00002989 * mb_get_class(), the list of chars in the file will be incomplete.
2990 */
2991 if (!spin->si_ascii
2992#ifdef FEAT_MBYTE
2993 && !enc_utf8
2994#endif
2995 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00002996 {
2997 if (fol == NULL || low == NULL || upp == NULL)
2998 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
2999 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00003000 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003001 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003002
3003 vim_free(fol);
3004 vim_free(low);
3005 vim_free(upp);
3006 }
3007
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003008 vim_free(pc);
3009 fclose(fd);
3010 return aff;
3011}
3012
3013/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003014 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
3015 * NULL as equal.
3016 */
3017 static int
3018str_equal(s1, s2)
3019 char_u *s1;
3020 char_u *s2;
3021{
3022 if (s1 == NULL || s2 == NULL)
3023 return s1 == s2;
3024 return STRCMP(s1, s2) == 0;
3025}
3026
3027/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003028 * Add a from-to item to "gap". Used for REP and SAL items.
3029 * They are stored case-folded.
3030 */
3031 static void
3032add_fromto(spin, gap, from, to)
3033 spellinfo_T *spin;
3034 garray_T *gap;
3035 char_u *from;
3036 char_u *to;
3037{
3038 fromto_T *ftp;
3039 char_u word[MAXWLEN];
3040
3041 if (ga_grow(gap, 1) == OK)
3042 {
3043 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
3044 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
3045 ftp->ft_from = getroom_save(&spin->si_blocks, word);
3046 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
3047 ftp->ft_to = getroom_save(&spin->si_blocks, word);
3048 ++gap->ga_len;
3049 }
3050}
3051
3052/*
3053 * Convert a boolean argument in a SAL line to TRUE or FALSE;
3054 */
3055 static int
3056sal_to_bool(s)
3057 char_u *s;
3058{
3059 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
3060}
3061
3062/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00003063 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
3064 * When "s" is NULL FALSE is returned.
3065 */
3066 static int
3067has_non_ascii(s)
3068 char_u *s;
3069{
3070 char_u *p;
3071
3072 if (s != NULL)
3073 for (p = s; *p != NUL; ++p)
3074 if (*p >= 128)
3075 return TRUE;
3076 return FALSE;
3077}
3078
3079/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003080 * Free the structure filled by spell_read_aff().
3081 */
3082 static void
3083spell_free_aff(aff)
3084 afffile_T *aff;
3085{
3086 hashtab_T *ht;
3087 hashitem_T *hi;
3088 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003089 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003090 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003091
3092 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003093
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003094 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003095 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
3096 {
3097 todo = ht->ht_used;
3098 for (hi = ht->ht_array; todo > 0; ++hi)
3099 {
3100 if (!HASHITEM_EMPTY(hi))
3101 {
3102 --todo;
3103 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003104 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3105 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003106 }
3107 }
3108 if (ht == &aff->af_suff)
3109 break;
3110 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003111
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003112 hash_clear(&aff->af_pref);
3113 hash_clear(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003114}
3115
3116/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003117 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003118 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003119 */
3120 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00003121spell_read_dic(fname, spin, affile)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003122 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003123 spellinfo_T *spin;
3124 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003125{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003126 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003127 char_u line[MAXLINELEN];
Bram Moolenaar51485f02005-06-04 21:55:20 +00003128 char_u *afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003129 char_u *pfxlist;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003130 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003131 char_u *pc;
3132 char_u *w;
3133 int l;
3134 hash_T hash;
3135 hashitem_T *hi;
3136 FILE *fd;
3137 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003138 int non_ascii = 0;
3139 int retval = OK;
3140 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003141 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003142
Bram Moolenaar51485f02005-06-04 21:55:20 +00003143 /*
3144 * Open the file.
3145 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003146 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003147 if (fd == NULL)
3148 {
3149 EMSG2(_(e_notopen), fname);
3150 return FAIL;
3151 }
3152
Bram Moolenaar51485f02005-06-04 21:55:20 +00003153 /* The hashtable is only used to detect duplicated words. */
3154 hash_init(&ht);
3155
Bram Moolenaar8db73182005-06-17 21:51:16 +00003156 spin->si_foldwcount = 0;
3157 spin->si_keepwcount = 0;
3158
Bram Moolenaarb765d632005-06-07 21:00:02 +00003159 if (spin->si_verbose || p_verbose > 2)
3160 {
3161 if (!spin->si_verbose)
3162 verbose_enter();
3163 smsg((char_u *)_("Reading dictionary file %s..."), fname);
3164 out_flush();
3165 if (!spin->si_verbose)
3166 verbose_leave();
3167 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003168
3169 /* Read and ignore the first line: word count. */
3170 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003171 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003172 EMSG2(_("E760: No word count in %s"), fname);
3173
3174 /*
3175 * Read all the lines in the file one by one.
3176 * The words are converted to 'encoding' here, before being added to
3177 * the hashtable.
3178 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003179 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003180 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003181 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003182 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003183 if (line[0] == '#')
3184 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003185
Bram Moolenaar51485f02005-06-04 21:55:20 +00003186 /* Remove CR, LF and white space from the end. White space halfway
3187 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003188 l = STRLEN(line);
3189 while (l > 0 && line[l - 1] <= ' ')
3190 --l;
3191 if (l == 0)
3192 continue; /* empty line */
3193 line[l] = NUL;
3194
Bram Moolenaar51485f02005-06-04 21:55:20 +00003195 /* Find the optional affix names. */
3196 afflist = vim_strchr(line, '/');
3197 if (afflist != NULL)
3198 *afflist++ = NUL;
3199
3200 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
3201 if (spin->si_ascii && has_non_ascii(line))
3202 {
3203 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003204 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003205 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003206
Bram Moolenaarb765d632005-06-07 21:00:02 +00003207#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003208 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003209 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003210 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003211 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003212 if (pc == NULL)
3213 {
3214 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3215 fname, lnum, line);
3216 continue;
3217 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003218 w = pc;
3219 }
3220 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003221#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003222 {
3223 pc = NULL;
3224 w = line;
3225 }
3226
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003227 /* This takes time, print a message now and then. */
3228 if (spin->si_verbose && (lnum & 0x3ff) == 0)
3229 {
3230 vim_snprintf((char *)message, sizeof(message),
3231 _("line %6d, word %6d - %s"),
3232 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
3233 msg_start();
3234 msg_puts_long_attr(message, 0);
3235 msg_clr_eos();
3236 msg_didout = FALSE;
3237 msg_col = 0;
3238 out_flush();
3239 }
3240
Bram Moolenaar51485f02005-06-04 21:55:20 +00003241 /* Store the word in the hashtable to be able to find duplicates. */
3242 dw = (char_u *)getroom_save(&spin->si_blocks, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003243 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003244 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003245 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003246 if (retval == FAIL)
3247 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003248
Bram Moolenaar51485f02005-06-04 21:55:20 +00003249 hash = hash_hash(dw);
3250 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003251 if (!HASHITEM_EMPTY(hi))
3252 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003253 fname, lnum, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003254 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00003255 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003256
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003257 flags = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003258 pfxlist = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003259 if (afflist != NULL)
3260 {
3261 /* Check for affix name that stands for keep-case word and stands
3262 * for rare word (if defined). */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003263 if (affile->af_kep != NUL
3264 && vim_strchr(afflist, affile->af_kep) != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003265 flags |= WF_KEEPCAP;
3266 if (affile->af_rar != NUL
3267 && vim_strchr(afflist, affile->af_rar) != NULL)
3268 flags |= WF_RARE;
Bram Moolenaar0c405862005-06-22 22:26:26 +00003269 if (affile->af_bad != NUL
3270 && vim_strchr(afflist, affile->af_bad) != NULL)
3271 flags |= WF_BANNED;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003272
3273 if (affile->af_pfxpostpone)
3274 /* Need to store the list of prefix IDs with the word. */
3275 pfxlist = get_pfxlist(affile, afflist, &spin->si_blocks);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003276 }
3277
Bram Moolenaar51485f02005-06-04 21:55:20 +00003278 /* Add the word to the word tree(s). */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003279 if (store_word(dw, spin, flags, spin->si_region, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003280 retval = FAIL;
3281
3282 if (afflist != NULL)
3283 {
3284 /* Find all matching suffixes and add the resulting words.
3285 * Additionally do matching prefixes that combine. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003286 if (store_aff_word(dw, spin, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003287 &affile->af_suff, &affile->af_pref,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003288 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003289 retval = FAIL;
3290
3291 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003292 if (store_aff_word(dw, spin, afflist, affile,
3293 &affile->af_pref, NULL,
3294 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003295 retval = FAIL;
3296 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003297 }
3298
Bram Moolenaar51485f02005-06-04 21:55:20 +00003299 if (spin->si_ascii && non_ascii > 0)
3300 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
3301 non_ascii);
3302 hash_clear(&ht);
3303
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003304 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003305 return retval;
3306}
3307
3308/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003309 * Get the list of prefix IDs from the affix list "afflist".
3310 * Used for PFXPOSTPONE.
3311 * Returns a string allocated with getroom(). NULL when there are no prefixes
3312 * or when out of memory.
3313 */
3314 static char_u *
3315get_pfxlist(affile, afflist, blp)
3316 afffile_T *affile;
3317 char_u *afflist;
3318 sblock_T **blp;
3319{
3320 char_u *p;
3321 int cnt;
3322 int round;
3323 char_u *res = NULL;
3324 char_u key[2];
3325 hashitem_T *hi;
3326
3327 key[1] = NUL;
3328
3329 /* round 1: count the number of prefix IDs.
3330 * round 2: move prefix IDs to "res" */
3331 for (round = 1; round <= 2; ++round)
3332 {
3333 cnt = 0;
3334 for (p = afflist; *p != NUL; ++p)
3335 {
3336 key[0] = *p;
3337 hi = hash_find(&affile->af_pref, key);
3338 if (!HASHITEM_EMPTY(hi))
3339 {
3340 /* This is a prefix ID, use the new number. */
3341 if (round == 2)
3342 res[cnt] = HI2AH(hi)->ah_newID;
3343 ++cnt;
3344 }
3345 }
3346 if (round == 1 && cnt > 0)
3347 res = getroom(blp, cnt + 1);
3348 if (res == NULL)
3349 break;
3350 }
3351
3352 if (res != NULL)
3353 res[cnt] = NUL;
3354 return res;
3355}
3356
3357/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003358 * Apply affixes to a word and store the resulting words.
3359 * "ht" is the hashtable with affentry_T that need to be applied, either
3360 * prefixes or suffixes.
3361 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
3362 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003363 *
3364 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003365 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003366 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003367store_aff_word(word, spin, afflist, affile, ht, xht, comb, flags, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003368 char_u *word; /* basic word start */
3369 spellinfo_T *spin; /* spell info */
3370 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003371 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003372 hashtab_T *ht;
3373 hashtab_T *xht;
3374 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003375 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003376 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003377{
3378 int todo;
3379 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003380 affheader_T *ah;
3381 affentry_T *ae;
3382 regmatch_T regmatch;
3383 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003384 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003385 int i;
3386 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003387
Bram Moolenaar51485f02005-06-04 21:55:20 +00003388 todo = ht->ht_used;
3389 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003390 {
3391 if (!HASHITEM_EMPTY(hi))
3392 {
3393 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003394 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00003395
Bram Moolenaar51485f02005-06-04 21:55:20 +00003396 /* Check that the affix combines, if required, and that the word
3397 * supports this affix. */
3398 if ((!comb || ah->ah_combine)
3399 && vim_strchr(afflist, *ah->ah_key) != NULL)
Bram Moolenaar5482f332005-04-17 20:18:43 +00003400 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003401 /* Loop over all affix entries with this name. */
3402 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003403 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003404 /* Check the condition. It's not logical to match case
3405 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003406 * Myspell.
3407 * For prefixes, when "PFXPOSTPONE" was used, only do
3408 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003409 regmatch.regprog = ae->ae_prog;
3410 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003411 if ((xht != NULL || !affile->af_pfxpostpone
3412 || ae->ae_chop != NULL)
3413 && (ae->ae_prog == NULL
3414 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003415 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003416 /* Match. Remove the chop and add the affix. */
3417 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003418 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003419 /* prefix: chop/add at the start of the word */
3420 if (ae->ae_add == NULL)
3421 *newword = NUL;
3422 else
3423 STRCPY(newword, ae->ae_add);
3424 p = word;
3425 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003426 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003427 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003428#ifdef FEAT_MBYTE
3429 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003430 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003431 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003432 for ( ; i > 0; --i)
3433 mb_ptr_adv(p);
3434 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00003435 else
3436#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003437 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003438 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003439 STRCAT(newword, p);
3440 }
3441 else
3442 {
3443 /* suffix: chop/add at the end of the word */
3444 STRCPY(newword, word);
3445 if (ae->ae_chop != NULL)
3446 {
3447 /* Remove chop string. */
3448 p = newword + STRLEN(newword);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003449#ifdef FEAT_MBYTE
3450 if (has_mbyte)
3451 i = mb_charlen(ae->ae_chop);
3452 else
3453#endif
3454 i = STRLEN(ae->ae_chop);
3455 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003456 mb_ptr_back(newword, p);
3457 *p = NUL;
3458 }
3459 if (ae->ae_add != NULL)
3460 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003461 }
3462
Bram Moolenaar51485f02005-06-04 21:55:20 +00003463 /* Store the modified word. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003464 if (store_word(newword, spin,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003465 flags, spin->si_region, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003466 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003467
Bram Moolenaar51485f02005-06-04 21:55:20 +00003468 /* When added a suffix and combining is allowed also
3469 * try adding prefixes additionally. */
3470 if (xht != NULL && ah->ah_combine)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003471 if (store_aff_word(newword, spin, afflist, affile,
3472 xht, NULL, TRUE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003473 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003474 }
3475 }
3476 }
3477 }
3478 }
3479
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003480 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003481}
3482
3483/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003484 * Read a file with a list of words.
3485 */
3486 static int
3487spell_read_wordfile(fname, spin)
3488 char_u *fname;
3489 spellinfo_T *spin;
3490{
3491 FILE *fd;
3492 long lnum = 0;
3493 char_u rline[MAXLINELEN];
3494 char_u *line;
3495 char_u *pc = NULL;
3496 int l;
3497 int retval = OK;
3498 int did_word = FALSE;
3499 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003500 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003501 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003502
3503 /*
3504 * Open the file.
3505 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003506 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00003507 if (fd == NULL)
3508 {
3509 EMSG2(_(e_notopen), fname);
3510 return FAIL;
3511 }
3512
Bram Moolenaarb765d632005-06-07 21:00:02 +00003513 if (spin->si_verbose || p_verbose > 2)
3514 {
3515 if (!spin->si_verbose)
3516 verbose_enter();
3517 smsg((char_u *)_("Reading word file %s..."), fname);
3518 out_flush();
3519 if (!spin->si_verbose)
3520 verbose_leave();
3521 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003522
3523 /*
3524 * Read all the lines in the file one by one.
3525 */
3526 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
3527 {
3528 line_breakcheck();
3529 ++lnum;
3530
3531 /* Skip comment lines. */
3532 if (*rline == '#')
3533 continue;
3534
3535 /* Remove CR, LF and white space from the end. */
3536 l = STRLEN(rline);
3537 while (l > 0 && rline[l - 1] <= ' ')
3538 --l;
3539 if (l == 0)
3540 continue; /* empty or blank line */
3541 rline[l] = NUL;
3542
3543 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
3544 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003545#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003546 if (spin->si_conv.vc_type != CONV_NONE)
3547 {
3548 pc = string_convert(&spin->si_conv, rline, NULL);
3549 if (pc == NULL)
3550 {
3551 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3552 fname, lnum, rline);
3553 continue;
3554 }
3555 line = pc;
3556 }
3557 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003558#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00003559 {
3560 pc = NULL;
3561 line = rline;
3562 }
3563
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003564 flags = 0;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003565 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003566
3567 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00003568 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003569 ++line;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003570
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003571 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003572 {
3573 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00003574 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
3575 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003576 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00003577 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
3578 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003579 else
3580 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003581#ifdef FEAT_MBYTE
3582 char_u *enc;
3583
Bram Moolenaar51485f02005-06-04 21:55:20 +00003584 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003585 line += 10;
3586 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003587 if (enc != NULL && !spin->si_ascii
3588 && convert_setup(&spin->si_conv, enc,
3589 p_enc) == FAIL)
3590 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00003591 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003592 vim_free(enc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003593#else
3594 smsg((char_u *)_("Conversion in %s not supported"), fname);
3595#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00003596 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003597 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003598 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003599
Bram Moolenaar3982c542005-06-08 21:56:31 +00003600 if (STRNCMP(line, "regions=", 8) == 0)
3601 {
3602 if (spin->si_region_count > 1)
3603 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
3604 fname, lnum, line);
3605 else
3606 {
3607 line += 8;
3608 if (STRLEN(line) > 16)
3609 smsg((char_u *)_("Too many regions in %s line %d: %s"),
3610 fname, lnum, line);
3611 else
3612 {
3613 spin->si_region_count = STRLEN(line) / 2;
3614 STRCPY(spin->si_region_name, line);
3615 }
3616 }
3617 continue;
3618 }
3619
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003620 if (*line == '=')
3621 {
3622 /* keep-case word */
3623 flags |= WF_KEEPCAP;
3624 ++line;
3625 }
3626
3627 if (*line == '!')
3628 {
3629 /* Bad, bad, wicked word. */
3630 flags |= WF_BANNED;
3631 ++line;
3632 }
3633 else if (*line == '?')
3634 {
3635 /* Rare word. */
3636 flags |= WF_RARE;
3637 ++line;
3638 }
3639
Bram Moolenaar3982c542005-06-08 21:56:31 +00003640 if (VIM_ISDIGIT(*line))
3641 {
3642 /* region number(s) */
3643 regionmask = 0;
3644 while (VIM_ISDIGIT(*line))
3645 {
3646 l = *line - '0';
3647 if (l > spin->si_region_count)
3648 {
3649 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
3650 fname, lnum, line);
3651 break;
3652 }
3653 regionmask |= 1 << (l - 1);
3654 ++line;
3655 }
3656 flags |= WF_REGION;
3657 }
3658
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003659 if (flags == 0)
3660 {
3661 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
Bram Moolenaar51485f02005-06-04 21:55:20 +00003662 fname, lnum, line);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003663 continue;
3664 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003665 }
3666
3667 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
3668 if (spin->si_ascii && has_non_ascii(line))
3669 {
3670 ++non_ascii;
3671 continue;
3672 }
3673
3674 /* Normal word: store it. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003675 if (store_word(line, spin, flags, regionmask, NULL) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003676 {
3677 retval = FAIL;
3678 break;
3679 }
3680 did_word = TRUE;
3681 }
3682
3683 vim_free(pc);
3684 fclose(fd);
3685
Bram Moolenaarb765d632005-06-07 21:00:02 +00003686 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
3687 {
3688 if (p_verbose > 2)
3689 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00003690 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
3691 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003692 if (p_verbose > 2)
3693 verbose_leave();
3694 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003695 return retval;
3696}
3697
3698/*
3699 * Get part of an sblock_T, "len" bytes long.
3700 * This avoids calling free() for every little struct we use.
3701 * The memory is cleared to all zeros.
3702 * Returns NULL when out of memory.
3703 */
3704 static void *
3705getroom(blp, len)
3706 sblock_T **blp;
3707 size_t len; /* length needed */
3708{
3709 char_u *p;
3710 sblock_T *bl = *blp;
3711
3712 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
3713 {
3714 /* Allocate a block of memory. This is not freed until much later. */
3715 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
3716 if (bl == NULL)
3717 return NULL;
3718 bl->sb_next = *blp;
3719 *blp = bl;
3720 bl->sb_used = 0;
3721 }
3722
3723 p = bl->sb_data + bl->sb_used;
3724 bl->sb_used += len;
3725
3726 return p;
3727}
3728
3729/*
3730 * Make a copy of a string into memory allocated with getroom().
3731 */
3732 static char_u *
3733getroom_save(blp, s)
3734 sblock_T **blp;
3735 char_u *s;
3736{
3737 char_u *sc;
3738
3739 sc = (char_u *)getroom(blp, STRLEN(s) + 1);
3740 if (sc != NULL)
3741 STRCPY(sc, s);
3742 return sc;
3743}
3744
3745
3746/*
3747 * Free the list of allocated sblock_T.
3748 */
3749 static void
3750free_blocks(bl)
3751 sblock_T *bl;
3752{
3753 sblock_T *next;
3754
3755 while (bl != NULL)
3756 {
3757 next = bl->sb_next;
3758 vim_free(bl);
3759 bl = next;
3760 }
3761}
3762
3763/*
3764 * Allocate the root of a word tree.
3765 */
3766 static wordnode_T *
3767wordtree_alloc(blp)
3768 sblock_T **blp;
3769{
3770 return (wordnode_T *)getroom(blp, sizeof(wordnode_T));
3771}
3772
3773/*
3774 * Store a word in the tree(s).
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003775 * Always store it in the case-folded tree. A keep-case word can also be used
3776 * with all caps.
Bram Moolenaar51485f02005-06-04 21:55:20 +00003777 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003778 * When "pfxlist" is not NULL store the word for each prefix ID.
Bram Moolenaar51485f02005-06-04 21:55:20 +00003779 */
3780 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003781store_word(word, spin, flags, region, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003782 char_u *word;
3783 spellinfo_T *spin;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003784 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003785 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003786 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003787{
3788 int len = STRLEN(word);
3789 int ct = captype(word, word + len);
3790 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003791 int res = OK;
3792 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003793
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003794 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003795 for (p = pfxlist; res == OK; ++p)
3796 {
3797 res = tree_add_word(foldword, spin->si_foldroot, ct | flags,
3798 region, p == NULL ? 0 : *p, &spin->si_blocks);
3799 if (p == NULL || *p == NUL)
3800 break;
3801 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00003802 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003803
3804 if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP))
Bram Moolenaar8db73182005-06-17 21:51:16 +00003805 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003806 for (p = pfxlist; res == OK; ++p)
3807 {
3808 res = tree_add_word(word, spin->si_keeproot, flags,
3809 region, p == NULL ? 0 : *p, &spin->si_blocks);
3810 if (p == NULL || *p == NUL)
3811 break;
3812 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00003813 ++spin->si_keepwcount;
3814 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003815 return res;
3816}
3817
3818/*
3819 * Add word "word" to a word tree at "root".
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003820 * When "flags" is -1 we are adding to the prefix tree where flags don't
3821 * matter and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003822 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003823 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003824 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003825tree_add_word(word, root, flags, region, prefixID, blp)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003826 char_u *word;
3827 wordnode_T *root;
3828 int flags;
3829 int region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003830 int prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003831 sblock_T **blp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003832{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003833 wordnode_T *node = root;
3834 wordnode_T *np;
3835 wordnode_T **prev = NULL;
3836 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003837
Bram Moolenaar51485f02005-06-04 21:55:20 +00003838 /* Add each byte of the word to the tree, including the NUL at the end. */
3839 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003840 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003841 /* Look for the sibling that has the same character. They are sorted
3842 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003843 * higher byte value. For zero bytes (end of word) the sorting is
3844 * done on flags and then on prefixID
Bram Moolenaar51485f02005-06-04 21:55:20 +00003845 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003846 while (node != NULL
3847 && (node->wn_byte < word[i]
3848 || (node->wn_byte == NUL
3849 && (flags < 0
3850 ? node->wn_prefixID < prefixID
3851 : node->wn_flags < (flags & 0xff)
3852 || (node->wn_flags == (flags & 0xff)
3853 && node->wn_prefixID < prefixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003854 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003855 prev = &node->wn_sibling;
3856 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003857 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003858 if (node == NULL
3859 || node->wn_byte != word[i]
3860 || (word[i] == NUL
3861 && (flags < 0
3862 || node->wn_flags != (flags & 0xff)
3863 || node->wn_prefixID != prefixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003864 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003865 /* Allocate a new node. */
3866 np = (wordnode_T *)getroom(blp, sizeof(wordnode_T));
3867 if (np == NULL)
3868 return FAIL;
3869 np->wn_byte = word[i];
3870 *prev = np;
3871 np->wn_sibling = node;
3872 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003873 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003874
Bram Moolenaar51485f02005-06-04 21:55:20 +00003875 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003876 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003877 node->wn_flags = flags;
3878 node->wn_region |= region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003879 node->wn_prefixID = prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003880 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003881 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003882 prev = &node->wn_child;
3883 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003884 }
3885
3886 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003887}
3888
3889/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003890 * Compress a tree: find tails that are identical and can be shared.
3891 */
3892 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00003893wordtree_compress(root, spin)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003894 wordnode_T *root;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003895 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003896{
3897 hashtab_T ht;
3898 int n;
3899 int tot = 0;
3900
3901 if (root != NULL)
3902 {
3903 hash_init(&ht);
3904 n = node_compress(root, &ht, &tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003905 if (spin->si_verbose || p_verbose > 2)
3906 {
3907 if (!spin->si_verbose)
3908 verbose_enter();
3909 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar51485f02005-06-04 21:55:20 +00003910 n, tot, (tot - n) * 100 / tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003911 if (p_verbose > 2)
3912 verbose_leave();
3913 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003914 hash_clear(&ht);
3915 }
3916}
3917
3918/*
3919 * Compress a node, its siblings and its children, depth first.
3920 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003921 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003922 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00003923node_compress(node, ht, tot)
3924 wordnode_T *node;
3925 hashtab_T *ht;
3926 int *tot; /* total count of nodes before compressing,
3927 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003928{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003929 wordnode_T *np;
3930 wordnode_T *tp;
3931 wordnode_T *child;
3932 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003933 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003934 int len = 0;
3935 unsigned nr, n;
3936 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003937
Bram Moolenaar51485f02005-06-04 21:55:20 +00003938 /*
3939 * Go through the list of siblings. Compress each child and then try
3940 * finding an identical child to replace it.
3941 * Note that with "child" we mean not just the node that is pointed to,
3942 * but the whole list of siblings, of which the node is the first.
3943 */
3944 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003945 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003946 ++len;
3947 if ((child = np->wn_child) != NULL)
3948 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003949 /* Compress the child. This fills hashkey. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003950 compressed += node_compress(child, ht, tot);
3951
3952 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003953 hash = hash_hash(child->wn_u1.hashkey);
3954 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003955 tp = NULL;
3956 if (!HASHITEM_EMPTY(hi))
3957 {
3958 /* There are children with an identical hash value. Now check
3959 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003960 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003961 if (node_equal(child, tp))
3962 {
3963 /* Found one! Now use that child in place of the
3964 * current one. This means the current child is
3965 * dropped from the tree. */
3966 np->wn_child = tp;
3967 ++compressed;
3968 break;
3969 }
3970 if (tp == NULL)
3971 {
3972 /* No other child with this hash value equals the child of
3973 * the node, add it to the linked list after the first
3974 * item. */
3975 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003976 child->wn_u2.next = tp->wn_u2.next;
3977 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003978 }
3979 }
3980 else
3981 /* No other child has this hash value, add it to the
3982 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003983 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003984 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003985 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003986 *tot += len;
3987
3988 /*
3989 * Make a hash key for the node and its siblings, so that we can quickly
3990 * find a lookalike node. This must be done after compressing the sibling
3991 * list, otherwise the hash key would become invalid by the compression.
3992 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003993 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003994 nr = 0;
3995 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003996 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003997 if (np->wn_byte == NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003998 /* end node: use wn_flags, wn_region and wn_prefixID */
3999 n = np->wn_flags + (np->wn_region << 8) + (np->wn_prefixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004000 else
4001 /* byte node: use the byte value and the child pointer */
4002 n = np->wn_byte + ((long_u)np->wn_child << 8);
4003 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004004 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004005
4006 /* Avoid NUL bytes, it terminates the hash key. */
4007 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004008 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004009 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004010 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004011 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004012 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004013 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004014 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
4015 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004016
4017 return compressed;
4018}
4019
4020/*
4021 * Return TRUE when two nodes have identical siblings and children.
4022 */
4023 static int
4024node_equal(n1, n2)
4025 wordnode_T *n1;
4026 wordnode_T *n2;
4027{
4028 wordnode_T *p1;
4029 wordnode_T *p2;
4030
4031 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
4032 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
4033 if (p1->wn_byte != p2->wn_byte
4034 || (p1->wn_byte == NUL
4035 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004036 || p1->wn_region != p2->wn_region
4037 || p1->wn_prefixID != p2->wn_prefixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004038 : (p1->wn_child != p2->wn_child)))
4039 break;
4040
4041 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004042}
4043
4044/*
4045 * Write a number to file "fd", MSB first, in "len" bytes.
4046 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004047 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004048put_bytes(fd, nr, len)
4049 FILE *fd;
4050 long_u nr;
4051 int len;
4052{
4053 int i;
4054
4055 for (i = len - 1; i >= 0; --i)
4056 putc((int)(nr >> (i * 8)), fd);
4057}
4058
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004059static int
4060#ifdef __BORLANDC__
4061_RTLENTRYF
4062#endif
4063rep_compare __ARGS((const void *s1, const void *s2));
4064
4065/*
4066 * Function given to qsort() to sort the REP items on "from" string.
4067 */
4068 static int
4069#ifdef __BORLANDC__
4070_RTLENTRYF
4071#endif
4072rep_compare(s1, s2)
4073 const void *s1;
4074 const void *s2;
4075{
4076 fromto_T *p1 = (fromto_T *)s1;
4077 fromto_T *p2 = (fromto_T *)s2;
4078
4079 return STRCMP(p1->ft_from, p2->ft_from);
4080}
4081
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004082/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004083 * Write the Vim spell file "fname".
4084 */
4085 static void
Bram Moolenaar3982c542005-06-08 21:56:31 +00004086write_vim_spell(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004087 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004088 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004089{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004090 FILE *fd;
4091 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004092 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004093 wordnode_T *tree;
4094 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004095 int i;
4096 int l;
4097 garray_T *gap;
4098 fromto_T *ftp;
4099 char_u *p;
4100 int rr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004101
Bram Moolenaarb765d632005-06-07 21:00:02 +00004102 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00004103 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004104 {
4105 EMSG2(_(e_notopen), fname);
4106 return;
4107 }
4108
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004109 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004110 * <charflagslen> <charflags>
4111 * <fcharslen> <fchars>
4112 * <prefcondcnt> <prefcond> ... */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004113
4114 /* <fileID> */
4115 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
4116 EMSG(_(e_write));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004117
4118 /* write the region names if there is more than one */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004119 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004120 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004121 putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */
4122 fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2),
4123 (size_t)1, fd);
4124 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004125 }
4126 else
4127 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004128 putc(0, fd);
4129 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004130 }
4131
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004132 /*
4133 * Write the table with character flags and table for case folding.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004134 * <charflagslen> <charflags> <fcharlen> <fchars>
4135 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004136 * 'encoding'.
4137 * Also skip this for an .add.spl file, the main spell file must contain
4138 * the table (avoids that it conflicts). File is shorter too.
4139 */
4140 if (spin->si_ascii || spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004141 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004142 putc(0, fd);
4143 putc(0, fd);
4144 putc(0, fd);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004145 }
4146 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00004147 write_spell_chartab(fd);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004148
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004149 /* Write the prefix conditions. */
4150 write_spell_prefcond(fd, &spin->si_prefcond);
4151
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004152 /* Sort the REP items. */
4153 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
4154 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004155
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004156 /* <SUGGEST> : <repcount> <rep> ...
4157 * <salflags> <salcount> <sal> ...
4158 * <maplen> <mapstr> */
4159 for (round = 1; round <= 2; ++round)
4160 {
4161 if (round == 1)
4162 gap = &spin->si_rep;
4163 else
4164 {
4165 gap = &spin->si_sal;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004166
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004167 i = 0;
4168 if (spin->si_followup)
4169 i |= SAL_F0LLOWUP;
4170 if (spin->si_collapse)
4171 i |= SAL_COLLAPSE;
4172 if (spin->si_rem_accents)
4173 i |= SAL_REM_ACCENTS;
4174 putc(i, fd); /* <salflags> */
4175 }
4176
4177 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
4178 for (i = 0; i < gap->ga_len; ++i)
4179 {
4180 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
4181 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
4182 ftp = &((fromto_T *)gap->ga_data)[i];
4183 for (rr = 1; rr <= 2; ++rr)
4184 {
4185 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
4186 l = STRLEN(p);
4187 putc(l, fd);
4188 fwrite(p, l, (size_t)1, fd);
4189 }
4190 }
4191 }
4192
4193 put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */
4194 if (spin->si_map.ga_len > 0) /* <mapstr> */
4195 fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len,
4196 (size_t)1, fd);
Bram Moolenaar50cde822005-06-05 21:54:54 +00004197
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004198 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004199 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004200 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004201 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004202 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004203 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004204 if (round == 1)
4205 tree = spin->si_foldroot;
4206 else if (round == 2)
4207 tree = spin->si_keeproot;
4208 else
4209 tree = spin->si_prefroot;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004210
Bram Moolenaar0c405862005-06-22 22:26:26 +00004211 /* Clear the index and wnode fields in the tree. */
4212 clear_node(tree);
4213
Bram Moolenaar51485f02005-06-04 21:55:20 +00004214 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00004215 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00004216 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004217 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004218
Bram Moolenaar51485f02005-06-04 21:55:20 +00004219 /* number of nodes in 4 bytes */
4220 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004221 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004222
Bram Moolenaar51485f02005-06-04 21:55:20 +00004223 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004224 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004225 }
4226
Bram Moolenaar51485f02005-06-04 21:55:20 +00004227 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004228}
4229
4230/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00004231 * Clear the index and wnode fields of "node", it siblings and its
4232 * children. This is needed because they are a union with other items to save
4233 * space.
4234 */
4235 static void
4236clear_node(node)
4237 wordnode_T *node;
4238{
4239 wordnode_T *np;
4240
4241 if (node != NULL)
4242 for (np = node; np != NULL; np = np->wn_sibling)
4243 {
4244 np->wn_u1.index = 0;
4245 np->wn_u2.wnode = NULL;
4246
4247 if (np->wn_byte != NUL)
4248 clear_node(np->wn_child);
4249 }
4250}
4251
4252
4253/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004254 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004255 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00004256 * This first writes the list of possible bytes (siblings). Then for each
4257 * byte recursively write the children.
4258 *
4259 * NOTE: The code here must match the code in read_tree(), since assumptions
4260 * are made about the indexes (so that we don't have to write them in the
4261 * file).
4262 *
4263 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004264 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004265 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00004266put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004267 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004268 wordnode_T *node;
4269 int index;
4270 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004271 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004272{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004273 int newindex = index;
4274 int siblingcount = 0;
4275 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004276 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004277
Bram Moolenaar51485f02005-06-04 21:55:20 +00004278 /* If "node" is zero the tree is empty. */
4279 if (node == NULL)
4280 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004281
Bram Moolenaar51485f02005-06-04 21:55:20 +00004282 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004283 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004284
4285 /* Count the number of siblings. */
4286 for (np = node; np != NULL; np = np->wn_sibling)
4287 ++siblingcount;
4288
4289 /* Write the sibling count. */
4290 if (fd != NULL)
4291 putc(siblingcount, fd); /* <siblingcount> */
4292
4293 /* Write each sibling byte and optionally extra info. */
4294 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004295 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004296 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004297 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004298 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004299 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004300 /* For a NUL byte (end of word) write the flags etc. */
4301 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004302 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004303 /* In PREFIXTREE write the required prefixID and the
4304 * associated condition nr (stored in wn_region). */
4305 putc(BY_FLAGS, fd); /* <byte> */
4306 putc(np->wn_prefixID, fd); /* <prefixID> */
4307 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004308 }
4309 else
4310 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004311 /* For word trees we write the flag/region items. */
4312 flags = np->wn_flags;
4313 if (regionmask != 0 && np->wn_region != regionmask)
4314 flags |= WF_REGION;
4315 if (np->wn_prefixID != 0)
4316 flags |= WF_PFX;
4317 if (flags == 0)
4318 {
4319 /* word without flags or region */
4320 putc(BY_NOFLAGS, fd); /* <byte> */
4321 }
4322 else
4323 {
4324 putc(BY_FLAGS, fd); /* <byte> */
4325 putc(flags, fd); /* <flags> */
4326 if (flags & WF_REGION)
4327 putc(np->wn_region, fd); /* <region> */
4328 if (flags & WF_PFX)
4329 putc(np->wn_prefixID, fd); /* <prefixID> */
4330 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004331 }
4332 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004333 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004334 else
4335 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00004336 if (np->wn_child->wn_u1.index != 0
4337 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004338 {
4339 /* The child is written elsewhere, write the reference. */
4340 if (fd != NULL)
4341 {
4342 putc(BY_INDEX, fd); /* <byte> */
4343 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004344 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004345 }
4346 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00004347 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004348 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004349 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004350
Bram Moolenaar51485f02005-06-04 21:55:20 +00004351 if (fd != NULL)
4352 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
4353 {
4354 EMSG(_(e_write));
4355 return 0;
4356 }
4357 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004358 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004359
4360 /* Space used in the array when reading: one for each sibling and one for
4361 * the count. */
4362 newindex += siblingcount + 1;
4363
4364 /* Recursively dump the children of each sibling. */
4365 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00004366 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
4367 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004368 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004369
4370 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004371}
4372
4373
4374/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00004375 * ":mkspell [-ascii] outfile infile ..."
4376 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004377 */
4378 void
4379ex_mkspell(eap)
4380 exarg_T *eap;
4381{
4382 int fcount;
4383 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004384 char_u *arg = eap->arg;
4385 int ascii = FALSE;
4386
4387 if (STRNCMP(arg, "-ascii", 6) == 0)
4388 {
4389 ascii = TRUE;
4390 arg = skipwhite(arg + 6);
4391 }
4392
4393 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
4394 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
4395 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004396 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004397 FreeWild(fcount, fnames);
4398 }
4399}
4400
4401/*
4402 * Create a Vim spell file from one or more word lists.
4403 * "fnames[0]" is the output file name.
4404 * "fnames[fcount - 1]" is the last input file name.
4405 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
4406 * and ".spl" is appended to make the output file name.
4407 */
4408 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004409mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004410 int fcount;
4411 char_u **fnames;
4412 int ascii; /* -ascii argument given */
4413 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004414 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004415{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004416 char_u fname[MAXPATHL];
4417 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00004418 char_u **innames;
4419 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004420 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004421 int i;
4422 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004423 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004424 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004425 spellinfo_T spin;
4426
4427 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004428 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004429 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004430 spin.si_followup = TRUE;
4431 spin.si_rem_accents = TRUE;
4432 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
4433 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
4434 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004435 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004436
Bram Moolenaarb765d632005-06-07 21:00:02 +00004437 /* default: fnames[0] is output file, following are input files */
4438 innames = &fnames[1];
4439 incount = fcount - 1;
4440
4441 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004442 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004443 len = STRLEN(fnames[0]);
4444 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
4445 {
4446 /* For ":mkspell path/en.latin1.add" output file is
4447 * "path/en.latin1.add.spl". */
4448 innames = &fnames[0];
4449 incount = 1;
4450 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
4451 }
4452 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
4453 {
4454 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004455 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004456 }
4457 else
4458 /* Name should be language, make the file name from it. */
4459 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
4460 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
4461
4462 /* Check for .ascii.spl. */
4463 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
4464 spin.si_ascii = TRUE;
4465
4466 /* Check for .add.spl. */
4467 if (strstr((char *)gettail(wfname), ".add.") != NULL)
4468 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004469 }
4470
Bram Moolenaarb765d632005-06-07 21:00:02 +00004471 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004472 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004473 else if (vim_strchr(gettail(wfname), '_') != NULL)
4474 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004475 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004476 EMSG(_("E754: Only up to 8 regions supported"));
4477 else
4478 {
4479 /* Check for overwriting before doing things that may take a lot of
4480 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004481 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004482 {
4483 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004484 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004485 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004486 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004487 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004488 EMSG2(_(e_isadir2), wfname);
4489 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004490 }
4491
4492 /*
4493 * Init the aff and dic pointers.
4494 * Get the region names if there are more than 2 arguments.
4495 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004496 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004497 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004498 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004499
Bram Moolenaar3982c542005-06-08 21:56:31 +00004500 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004501 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004502 len = STRLEN(innames[i]);
4503 if (STRLEN(gettail(innames[i])) < 5
4504 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004505 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004506 EMSG2(_("E755: Invalid region in %s"), innames[i]);
4507 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004508 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00004509 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
4510 spin.si_region_name[i * 2 + 1] =
4511 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004512 }
4513 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00004514 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004515
Bram Moolenaar51485f02005-06-04 21:55:20 +00004516 spin.si_foldroot = wordtree_alloc(&spin.si_blocks);
4517 spin.si_keeproot = wordtree_alloc(&spin.si_blocks);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004518 spin.si_prefroot = wordtree_alloc(&spin.si_blocks);
4519 if (spin.si_foldroot == NULL
4520 || spin.si_keeproot == NULL
4521 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004522 {
4523 error = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004524 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004525 }
4526
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004527 /* When not producing a .add.spl file clear the character table when
4528 * we encounter one in the .aff file. This means we dump the current
4529 * one in the .spl file if the .aff file doesn't define one. That's
4530 * better than guessing the contents, the table will match a
4531 * previously loaded spell file. */
4532 if (!spin.si_add)
4533 spin.si_clear_chartab = TRUE;
4534
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004535 /*
4536 * Read all the .aff and .dic files.
4537 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004538 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004539 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004540 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004541 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004542 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004543 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004544
Bram Moolenaarb765d632005-06-07 21:00:02 +00004545 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004546 if (mch_stat((char *)fname, &st) >= 0)
4547 {
4548 /* Read the .aff file. Will init "spin->si_conv" based on the
4549 * "SET" line. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004550 afile[i] = spell_read_aff(fname, &spin);
4551 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004552 error = TRUE;
4553 else
4554 {
4555 /* Read the .dic file and store the words in the trees. */
4556 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00004557 innames[i]);
4558 if (spell_read_dic(fname, &spin, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004559 error = TRUE;
4560 }
4561 }
4562 else
4563 {
4564 /* No .aff file, try reading the file as a word list. Store
4565 * the words in the trees. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004566 if (spell_read_wordfile(innames[i], &spin) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004567 error = TRUE;
4568 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004569
Bram Moolenaarb765d632005-06-07 21:00:02 +00004570#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004571 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004572 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004573#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004574 }
4575
Bram Moolenaar51485f02005-06-04 21:55:20 +00004576 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004577 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004578 /*
4579 * Remove the dummy NUL from the start of the tree root.
4580 */
4581 spin.si_foldroot = spin.si_foldroot->wn_sibling;
4582 spin.si_keeproot = spin.si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004583 spin.si_prefroot = spin.si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004584
4585 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004586 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004587 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004588 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004589 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004590 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004591 verbose_enter();
4592 MSG(_("Compressing word tree..."));
4593 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004594 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004595 verbose_leave();
4596 }
4597 wordtree_compress(spin.si_foldroot, &spin);
4598 wordtree_compress(spin.si_keeproot, &spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004599 wordtree_compress(spin.si_prefroot, &spin);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004600 }
4601
Bram Moolenaar51485f02005-06-04 21:55:20 +00004602 if (!error)
4603 {
4604 /*
4605 * Write the info in the spell file.
4606 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004607 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004608 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004609 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004610 verbose_enter();
4611 smsg((char_u *)_("Writing spell file %s..."), wfname);
4612 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004613 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004614 verbose_leave();
4615 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00004616
Bram Moolenaar3982c542005-06-08 21:56:31 +00004617 write_vim_spell(wfname, &spin);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004618
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004619 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004620 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004621 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004622 verbose_enter();
4623 MSG(_("Done!"));
4624 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00004625 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004626 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004627 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004628 verbose_leave();
4629 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004630
Bram Moolenaarb765d632005-06-07 21:00:02 +00004631 /* If the file is loaded need to reload it. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004632 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004633 }
4634
4635 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004636 ga_clear(&spin.si_rep);
4637 ga_clear(&spin.si_sal);
4638 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004639 ga_clear(&spin.si_prefcond);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004640
4641 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004642 for (i = 0; i < incount; ++i)
4643 if (afile[i] != NULL)
4644 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004645
4646 /* Free all the bits and pieces at once. */
4647 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004648 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004649}
4650
Bram Moolenaarb765d632005-06-07 21:00:02 +00004651
4652/*
4653 * ":spellgood {word}"
4654 * ":spellwrong {word}"
4655 */
4656 void
4657ex_spell(eap)
4658 exarg_T *eap;
4659{
4660 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong);
4661}
4662
4663/*
4664 * Add "word[len]" to 'spellfile' as a good or bad word.
4665 */
4666 void
4667spell_add_word(word, len, bad)
4668 char_u *word;
4669 int len;
4670 int bad;
4671{
4672 FILE *fd;
4673 buf_T *buf;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004674 int new_spf = FALSE;
4675 struct stat st;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004676
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004677 /* If 'spellfile' isn't set figure out a good default value. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004678 if (*curbuf->b_p_spf == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004679 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004680 init_spellfile();
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004681 new_spf = TRUE;
4682 }
4683
Bram Moolenaarb765d632005-06-07 21:00:02 +00004684 if (*curbuf->b_p_spf == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004685 EMSG(_("E764: 'spellfile' is not set"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004686 else
4687 {
4688 /* Check that the user isn't editing the .add file somewhere. */
4689 buf = buflist_findname_exp(curbuf->b_p_spf);
4690 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
4691 buf = NULL;
4692 if (buf != NULL && bufIsChanged(buf))
4693 EMSG(_(e_bufloaded));
4694 else
4695 {
4696 fd = mch_fopen((char *)curbuf->b_p_spf, "a");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004697 if (fd == NULL && new_spf)
4698 {
4699 /* We just initialized the 'spellfile' option and can't open
4700 * the file. We may need to create the "spell" directory
4701 * first. We already checked the runtime directory is
4702 * writable in init_spellfile(). */
4703 STRCPY(NameBuff, curbuf->b_p_spf);
4704 *gettail_sep(NameBuff) = NUL;
4705 if (mch_stat((char *)NameBuff, &st) < 0)
4706 {
4707 /* The directory doesn't exist. Try creating it and
4708 * opening the file again. */
4709 vim_mkdir(NameBuff, 0755);
4710 fd = mch_fopen((char *)curbuf->b_p_spf, "a");
4711 }
4712 }
4713
Bram Moolenaarb765d632005-06-07 21:00:02 +00004714 if (fd == NULL)
4715 EMSG2(_(e_notopen), curbuf->b_p_spf);
4716 else
4717 {
4718 if (bad)
4719 fprintf(fd, "/!%.*s\n", len, word);
4720 else
4721 fprintf(fd, "%.*s\n", len, word);
4722 fclose(fd);
4723
4724 /* Update the .add.spl file. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004725 mkspell(1, &curbuf->b_p_spf, FALSE, TRUE, TRUE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004726
4727 /* If the .add file is edited somewhere, reload it. */
4728 if (buf != NULL)
4729 buf_reload(buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004730
4731 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004732 }
4733 }
4734 }
4735}
4736
4737/*
4738 * Initialize 'spellfile' for the current buffer.
4739 */
4740 static void
4741init_spellfile()
4742{
4743 char_u buf[MAXPATHL];
4744 int l;
4745 slang_T *sl;
4746 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004747 char_u *lend;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004748
4749 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
4750 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004751 /* Find the end of the language name. Exclude the region. */
4752 for (lend = curbuf->b_p_spl; *lend != NUL
4753 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
4754 ;
4755
4756 /* Loop over all entries in 'runtimepath'. Use the first one where we
4757 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004758 rtp = p_rtp;
4759 while (*rtp != NUL)
4760 {
4761 /* Copy the path from 'runtimepath' to buf[]. */
4762 copy_option_part(&rtp, buf, MAXPATHL, ",");
4763 if (filewritable(buf) == 2)
4764 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004765 /* Use the first language name from 'spelllang' and the
4766 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004767 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
4768 l = STRLEN(buf);
4769 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00004770 "/spell/%.*s.%s.add",
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004771 (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00004772 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
4773 ? (char_u *)"ascii" : spell_enc());
4774 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
4775 break;
4776 }
4777 }
4778 }
4779}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004780
Bram Moolenaar51485f02005-06-04 21:55:20 +00004781
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004782/*
4783 * Init the chartab used for spelling for ASCII.
4784 * EBCDIC is not supported!
4785 */
4786 static void
4787clear_spell_chartab(sp)
4788 spelltab_T *sp;
4789{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004790 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004791
4792 /* Init everything to FALSE. */
4793 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
4794 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
4795 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004796 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004797 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004798 sp->st_upper[i] = i;
4799 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004800
4801 /* We include digits. A word shouldn't start with a digit, but handling
4802 * that is done separately. */
4803 for (i = '0'; i <= '9'; ++i)
4804 sp->st_isw[i] = TRUE;
4805 for (i = 'A'; i <= 'Z'; ++i)
4806 {
4807 sp->st_isw[i] = TRUE;
4808 sp->st_isu[i] = TRUE;
4809 sp->st_fold[i] = i + 0x20;
4810 }
4811 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004812 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004813 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004814 sp->st_upper[i] = i - 0x20;
4815 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004816}
4817
4818/*
4819 * Init the chartab used for spelling. Only depends on 'encoding'.
4820 * Called once while starting up and when 'encoding' changes.
4821 * The default is to use isalpha(), but the spell file should define the word
4822 * characters to make it possible that 'encoding' differs from the current
4823 * locale.
4824 */
4825 void
4826init_spell_chartab()
4827{
4828 int i;
4829
4830 did_set_spelltab = FALSE;
4831 clear_spell_chartab(&spelltab);
4832
4833#ifdef FEAT_MBYTE
4834 if (enc_dbcs)
4835 {
4836 /* DBCS: assume double-wide characters are word characters. */
4837 for (i = 128; i <= 255; ++i)
4838 if (MB_BYTE2LEN(i) == 2)
4839 spelltab.st_isw[i] = TRUE;
4840 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004841 else if (enc_utf8)
4842 {
4843 for (i = 128; i < 256; ++i)
4844 {
4845 spelltab.st_isu[i] = utf_isupper(i);
4846 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
4847 spelltab.st_fold[i] = utf_fold(i);
4848 spelltab.st_upper[i] = utf_toupper(i);
4849 }
4850 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004851 else
4852#endif
4853 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004854 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004855 for (i = 128; i < 256; ++i)
4856 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004857 if (MB_ISUPPER(i))
4858 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004859 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004860 spelltab.st_isu[i] = TRUE;
4861 spelltab.st_fold[i] = MB_TOLOWER(i);
4862 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004863 else if (MB_ISLOWER(i))
4864 {
4865 spelltab.st_isw[i] = TRUE;
4866 spelltab.st_upper[i] = MB_TOUPPER(i);
4867 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004868 }
4869 }
4870}
4871
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004872static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
4873static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
4874
4875/*
4876 * Set the spell character tables from strings in the affix file.
4877 */
4878 static int
4879set_spell_chartab(fol, low, upp)
4880 char_u *fol;
4881 char_u *low;
4882 char_u *upp;
4883{
4884 /* We build the new tables here first, so that we can compare with the
4885 * previous one. */
4886 spelltab_T new_st;
4887 char_u *pf = fol, *pl = low, *pu = upp;
4888 int f, l, u;
4889
4890 clear_spell_chartab(&new_st);
4891
4892 while (*pf != NUL)
4893 {
4894 if (*pl == NUL || *pu == NUL)
4895 {
4896 EMSG(_(e_affform));
4897 return FAIL;
4898 }
4899#ifdef FEAT_MBYTE
4900 f = mb_ptr2char_adv(&pf);
4901 l = mb_ptr2char_adv(&pl);
4902 u = mb_ptr2char_adv(&pu);
4903#else
4904 f = *pf++;
4905 l = *pl++;
4906 u = *pu++;
4907#endif
4908 /* Every character that appears is a word character. */
4909 if (f < 256)
4910 new_st.st_isw[f] = TRUE;
4911 if (l < 256)
4912 new_st.st_isw[l] = TRUE;
4913 if (u < 256)
4914 new_st.st_isw[u] = TRUE;
4915
4916 /* if "LOW" and "FOL" are not the same the "LOW" char needs
4917 * case-folding */
4918 if (l < 256 && l != f)
4919 {
4920 if (f >= 256)
4921 {
4922 EMSG(_(e_affrange));
4923 return FAIL;
4924 }
4925 new_st.st_fold[l] = f;
4926 }
4927
4928 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004929 * case-folding, it's upper case and the "UPP" is the upper case of
4930 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004931 if (u < 256 && u != f)
4932 {
4933 if (f >= 256)
4934 {
4935 EMSG(_(e_affrange));
4936 return FAIL;
4937 }
4938 new_st.st_fold[u] = f;
4939 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004940 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004941 }
4942 }
4943
4944 if (*pl != NUL || *pu != NUL)
4945 {
4946 EMSG(_(e_affform));
4947 return FAIL;
4948 }
4949
4950 return set_spell_finish(&new_st);
4951}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004952
4953/*
4954 * Set the spell character tables from strings in the .spl file.
4955 */
4956 static int
4957set_spell_charflags(flags, cnt, upp)
4958 char_u *flags;
4959 int cnt;
4960 char_u *upp;
4961{
4962 /* We build the new tables here first, so that we can compare with the
4963 * previous one. */
4964 spelltab_T new_st;
4965 int i;
4966 char_u *p = upp;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004967 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004968
4969 clear_spell_chartab(&new_st);
4970
4971 for (i = 0; i < cnt; ++i)
4972 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004973 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
4974 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004975
4976 if (*p == NUL)
4977 return FAIL;
4978#ifdef FEAT_MBYTE
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004979 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004980#else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004981 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004982#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004983 new_st.st_fold[i + 128] = c;
4984 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
4985 new_st.st_upper[c] = i + 128;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004986 }
4987
4988 return set_spell_finish(&new_st);
4989}
4990
4991 static int
4992set_spell_finish(new_st)
4993 spelltab_T *new_st;
4994{
4995 int i;
4996
4997 if (did_set_spelltab)
4998 {
4999 /* check that it's the same table */
5000 for (i = 0; i < 256; ++i)
5001 {
5002 if (spelltab.st_isw[i] != new_st->st_isw[i]
5003 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005004 || spelltab.st_fold[i] != new_st->st_fold[i]
5005 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005006 {
5007 EMSG(_("E763: Word characters differ between spell files"));
5008 return FAIL;
5009 }
5010 }
5011 }
5012 else
5013 {
5014 /* copy the new spelltab into the one being used */
5015 spelltab = *new_st;
5016 did_set_spelltab = TRUE;
5017 }
5018
5019 return OK;
5020}
5021
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005022/*
Bram Moolenaarea408852005-06-25 22:49:46 +00005023 * Return TRUE if "p" points to a word character.
5024 * As a special case we see a single quote as a word character when it is
5025 * followed by a word character. This finds they'there but not 'they there'.
5026 */
5027 static int
5028spell_iswordp(p)
5029 char_u *p;
5030{
5031 char_u *s;
5032
5033 if (*p == '\'')
5034 s = p + 1;
5035 else
5036 s = p;
5037#ifdef FEAT_MBYTE
5038 if (has_mbyte && MB_BYTE2LEN(*s) > 1)
5039 return mb_get_class(s) >= 2;
5040#endif
5041 return spelltab.st_isw[*s];
5042}
5043
5044/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005045 * Write the table with prefix conditions to the .spl file.
5046 */
5047 static void
5048write_spell_prefcond(fd, gap)
5049 FILE *fd;
5050 garray_T *gap;
5051{
5052 int i;
5053 char_u *p;
5054 int len;
5055
5056 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
5057
5058 for (i = 0; i < gap->ga_len; ++i)
5059 {
5060 /* <prefcond> : <condlen> <condstr> */
5061 p = ((char_u **)gap->ga_data)[i];
5062 if (p == NULL)
5063 fputc(0, fd);
5064 else
5065 {
5066 len = STRLEN(p);
5067 fputc(len, fd);
5068 fwrite(p, (size_t)len, (size_t)1, fd);
5069 }
5070 }
5071}
5072
5073/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005074 * Write the current tables into the .spl file.
5075 * This makes sure the same characters are recognized as word characters when
5076 * generating an when using a spell file.
5077 */
5078 static void
5079write_spell_chartab(fd)
5080 FILE *fd;
5081{
5082 char_u charbuf[256 * 4];
5083 int len = 0;
5084 int flags;
5085 int i;
5086
5087 fputc(128, fd); /* <charflagslen> */
5088 for (i = 128; i < 256; ++i)
5089 {
5090 flags = 0;
5091 if (spelltab.st_isw[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005092 flags |= CF_WORD;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005093 if (spelltab.st_isu[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005094 flags |= CF_UPPER;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005095 fputc(flags, fd); /* <charflags> */
5096
Bram Moolenaarb765d632005-06-07 21:00:02 +00005097#ifdef FEAT_MBYTE
5098 if (has_mbyte)
5099 len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
5100 else
5101#endif
5102 charbuf[len++] = spelltab.st_fold[i];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005103 }
5104
5105 put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
5106 fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
5107}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005108
5109/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005110 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
5111 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005112 * When using a multi-byte 'encoding' the length may change!
5113 * Returns FAIL when something wrong.
5114 */
5115 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005116spell_casefold(str, len, buf, buflen)
5117 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005118 int len;
5119 char_u *buf;
5120 int buflen;
5121{
5122 int i;
5123
5124 if (len >= buflen)
5125 {
5126 buf[0] = NUL;
5127 return FAIL; /* result will not fit */
5128 }
5129
5130#ifdef FEAT_MBYTE
5131 if (has_mbyte)
5132 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005133 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005134 char_u *p;
5135 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005136
5137 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005138 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005139 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005140 if (outi + MB_MAXBYTES > buflen)
5141 {
5142 buf[outi] = NUL;
5143 return FAIL;
5144 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005145 c = mb_ptr2char_adv(&p);
5146 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005147 }
5148 buf[outi] = NUL;
5149 }
5150 else
5151#endif
5152 {
5153 /* Be quick for non-multibyte encodings. */
5154 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005155 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005156 buf[i] = NUL;
5157 }
5158
5159 return OK;
5160}
5161
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005162/*
5163 * "z?": Find badly spelled word under or after the cursor.
5164 * Give suggestions for the properly spelled word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005165 */
5166 void
5167spell_suggest()
5168{
5169 char_u *line;
5170 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005171 char_u wcopy[MAXWLEN + 2];
5172 char_u *p;
5173 int i;
5174 int c;
5175 suginfo_T sug;
5176 suggest_T *stp;
5177
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005178 /* Find the start of the badly spelled word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005179 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL
5180 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005181 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005182 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
5183 return;
5184
5185 /* No bad word or it starts after the cursor: use the word under the
5186 * cursor. */
5187 curwin->w_cursor = prev_cursor;
5188 line = ml_get_curline();
5189 p = line + curwin->w_cursor.col;
5190 /* Backup to before start of word. */
5191 while (p > line && SPELL_ISWORDP(p))
5192 mb_ptr_back(line, p);
5193 /* Forward to start of word. */
5194 while (!SPELL_ISWORDP(p))
5195 mb_ptr_adv(p);
5196
5197 if (!SPELL_ISWORDP(p)) /* No word found. */
5198 {
5199 beep_flush();
5200 return;
5201 }
5202 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005203 }
5204
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005205 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005206 line = ml_get_curline();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005207
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005208 /* Get the list of suggestions */
Bram Moolenaarea408852005-06-25 22:49:46 +00005209 spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005210
5211 if (sug.su_ga.ga_len == 0)
5212 MSG(_("Sorry, no suggestions"));
5213 else
5214 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005215 /* List the suggestions. */
5216 msg_start();
5217 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
5218 sug.su_badlen, sug.su_badptr);
5219 msg_puts(IObuff);
5220 msg_clr_eos();
5221 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00005222
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005223 msg_scroll = TRUE;
5224 for (i = 0; i < sug.su_ga.ga_len; ++i)
5225 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005226 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005227
5228 /* The suggested word may replace only part of the bad word, add
5229 * the not replaced part. */
5230 STRCPY(wcopy, stp->st_word);
5231 if (sug.su_badlen > stp->st_orglen)
5232 vim_strncpy(wcopy + STRLEN(wcopy),
5233 sug.su_badptr + stp->st_orglen,
5234 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005235 vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\""), i + 1, wcopy);
5236 msg_puts(IObuff);
5237
5238 /* The word may replace more than "su_badlen". */
5239 if (sug.su_badlen < stp->st_orglen)
5240 {
5241 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
5242 stp->st_orglen, sug.su_badptr);
5243 msg_puts(IObuff);
5244 }
5245
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005246 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005247 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005248 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005249 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0c405862005-06-22 22:26:26 +00005250 vim_snprintf((char *)IObuff, IOSIZE, _(" (%s%d - %d)"),
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005251 stp->st_salscore ? "s " : "",
5252 stp->st_score, stp->st_altscore);
5253 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00005254 vim_snprintf((char *)IObuff, IOSIZE, _(" (%d)"),
5255 stp->st_score);
5256 msg_advance(30);
5257 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005258 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005259 lines_left = 3; /* avoid more prompt */
5260 msg_putchar('\n');
5261 }
5262
5263 /* Ask for choice. */
5264 i = prompt_for_number();
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005265 if (i > 0 && i <= sug.su_ga.ga_len && u_save_cursor() == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005266 {
5267 /* Replace the word. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005268 stp = &SUG(sug.su_ga, i - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005269 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
5270 if (p != NULL)
5271 {
5272 c = sug.su_badptr - line;
5273 mch_memmove(p, line, c);
5274 STRCPY(p + c, stp->st_word);
5275 STRCAT(p, sug.su_badptr + stp->st_orglen);
5276 ml_replace(curwin->w_cursor.lnum, p, FALSE);
5277 curwin->w_cursor.col = c;
5278 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005279
5280 /* For redo we use a change-word command. */
5281 ResetRedobuff();
5282 AppendToRedobuff((char_u *)"ciw");
5283 AppendToRedobuff(stp->st_word);
5284 AppendCharToRedobuff(ESC);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005285 }
5286 }
5287 else
5288 curwin->w_cursor = prev_cursor;
5289 }
5290
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005291 spell_find_cleanup(&sug);
5292}
5293
5294/*
5295 * Find spell suggestions for "word". Return them in the growarray "*gap" as
5296 * a list of allocated strings.
5297 */
5298 void
5299spell_suggest_list(gap, word, maxcount)
5300 garray_T *gap;
5301 char_u *word;
5302 int maxcount; /* maximum nr of suggestions */
5303{
5304 suginfo_T sug;
5305 int i;
5306 suggest_T *stp;
5307 char_u *wcopy;
5308
Bram Moolenaarea408852005-06-25 22:49:46 +00005309 spell_find_suggest(word, &sug, maxcount, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005310
5311 /* Make room in "gap". */
5312 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
5313 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
5314 return;
5315
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005316 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005317 {
5318 stp = &SUG(sug.su_ga, i);
5319
5320 /* The suggested word may replace only part of "word", add the not
5321 * replaced part. */
5322 wcopy = alloc(STRLEN(stp->st_word)
5323 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
5324 if (wcopy == NULL)
5325 break;
5326 STRCPY(wcopy, stp->st_word);
5327 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
5328 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
5329 }
5330
5331 spell_find_cleanup(&sug);
5332}
5333
5334/*
5335 * Find spell suggestions for the word at the start of "badptr".
5336 * Return the suggestions in "su->su_ga".
5337 * The maximum number of suggestions is "maxcount".
5338 * Note: does use info for the current window.
5339 * This is based on the mechanisms of Aspell, but completely reimplemented.
5340 */
5341 static void
Bram Moolenaarea408852005-06-25 22:49:46 +00005342spell_find_suggest(badptr, su, maxcount, banbadword)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005343 char_u *badptr;
5344 suginfo_T *su;
5345 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00005346 int banbadword; /* don't include badword in suggestions */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005347{
5348 int attr;
5349
5350 /*
5351 * Set the info in "*su".
5352 */
5353 vim_memset(su, 0, sizeof(suginfo_T));
5354 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
5355 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005356 if (*badptr == NUL)
5357 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005358 hash_init(&su->su_banned);
5359
5360 su->su_badptr = badptr;
5361 su->su_badlen = spell_check(curwin, su->su_badptr, &attr);
5362 su->su_maxcount = maxcount;
5363
5364 if (su->su_badlen >= MAXWLEN)
5365 su->su_badlen = MAXWLEN - 1; /* just in case */
5366 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
5367 (void)spell_casefold(su->su_badptr, su->su_badlen,
5368 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005369 /* get caps flags for bad word */
5370 su->su_badflags = captype(su->su_badptr, su->su_badptr + su->su_badlen);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005371
5372 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00005373 if (banbadword)
5374 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005375
5376 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00005377 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005378 *
5379 * Set a maximum score to limit the combination of operations that is
5380 * tried.
5381 */
5382 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005383 suggest_try_special(su);
5384
5385 /*
5386 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
5387 * from the .aff file and inserting a space (split the word).
5388 */
5389 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005390
5391 /* For the resulting top-scorers compute the sound-a-like score. */
5392 if (sps_flags & SPS_DOUBLE)
5393 score_comp_sal(su);
5394
5395 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00005396 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005397 *
5398 * Only do this when we don't have a lot of suggestions yet, because it's
5399 * very slow and often doesn't find new suggestions.
5400 */
5401 if ((sps_flags & SPS_DOUBLE)
5402 || (!(sps_flags & SPS_FAST)
5403 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
5404 {
5405 /* Allow a higher score now. */
5406 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005407 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005408 }
5409
5410 /* When CTRL-C was hit while searching do show the results. */
5411 ui_breakcheck();
5412 if (got_int)
5413 {
5414 (void)vgetc();
5415 got_int = FALSE;
5416 }
5417
5418 if (sps_flags & SPS_DOUBLE)
5419 {
5420 /* Combine the two list of suggestions. */
5421 score_combine(su);
5422 }
5423 else if (su->su_ga.ga_len != 0)
5424 {
5425 if (sps_flags & SPS_BEST)
5426 /* Adjust the word score for how it sounds like. */
5427 rescore_suggestions(su);
5428
5429 /* Sort the suggestions and truncate at "maxcount". */
5430 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, maxcount);
5431 }
5432}
5433
5434/*
5435 * Free the info put in "*su" by spell_find_suggest().
5436 */
5437 static void
5438spell_find_cleanup(su)
5439 suginfo_T *su;
5440{
5441 int i;
5442
5443 /* Free the suggestions. */
5444 for (i = 0; i < su->su_ga.ga_len; ++i)
5445 vim_free(SUG(su->su_ga, i).st_word);
5446 ga_clear(&su->su_ga);
5447 for (i = 0; i < su->su_sga.ga_len; ++i)
5448 vim_free(SUG(su->su_sga, i).st_word);
5449 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005450
5451 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005452 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005453}
5454
5455/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005456 * Make a copy of "word", with the first letter upper or lower cased, to
5457 * "wcopy[MAXWLEN]". "word" must not be empty.
5458 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005459 */
5460 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005461onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005462 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005463 char_u *wcopy;
5464 int upper; /* TRUE: first letter made upper case */
5465{
5466 char_u *p;
5467 int c;
5468 int l;
5469
5470 p = word;
5471#ifdef FEAT_MBYTE
5472 if (has_mbyte)
5473 c = mb_ptr2char_adv(&p);
5474 else
5475#endif
5476 c = *p++;
5477 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005478 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005479 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005480 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005481#ifdef FEAT_MBYTE
5482 if (has_mbyte)
5483 l = mb_char2bytes(c, wcopy);
5484 else
5485#endif
5486 {
5487 l = 1;
5488 wcopy[0] = c;
5489 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005490 vim_strncpy(wcopy + l, p, MAXWLEN - l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005491}
5492
5493/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005494 * Make a copy of "word" with all the letters upper cased into
5495 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005496 */
5497 static void
5498allcap_copy(word, wcopy)
5499 char_u *word;
5500 char_u *wcopy;
5501{
5502 char_u *s;
5503 char_u *d;
5504 int c;
5505
5506 d = wcopy;
5507 for (s = word; *s != NUL; )
5508 {
5509#ifdef FEAT_MBYTE
5510 if (has_mbyte)
5511 c = mb_ptr2char_adv(&s);
5512 else
5513#endif
5514 c = *s++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005515 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005516
5517#ifdef FEAT_MBYTE
5518 if (has_mbyte)
5519 {
5520 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
5521 break;
5522 d += mb_char2bytes(c, d);
5523 }
5524 else
5525#endif
5526 {
5527 if (d - wcopy >= MAXWLEN - 1)
5528 break;
5529 *d++ = c;
5530 }
5531 }
5532 *d = NUL;
5533}
5534
5535/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00005536 * Try finding suggestions by recognizing specific situations.
5537 */
5538 static void
5539suggest_try_special(su)
5540 suginfo_T *su;
5541{
5542 char_u *p;
5543 int len;
5544 int c;
5545 char_u word[MAXWLEN];
5546
5547 /*
5548 * Recognize a word that is repeated: "the the".
5549 */
5550 p = skiptowhite(su->su_fbadword);
5551 len = p - su->su_fbadword;
5552 p = skipwhite(p);
5553 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
5554 {
5555 /* Include badflags: if the badword is onecap or allcap
5556 * use that for the goodword too: "The the" -> "The". */
5557 c = su->su_fbadword[len];
5558 su->su_fbadword[len] = NUL;
5559 make_case_word(su->su_fbadword, word, su->su_badflags);
5560 su->su_fbadword[len] = c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005561 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005562 }
5563}
5564
5565/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005566 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00005567 *
5568 * This uses a state machine. At each node in the tree we try various
5569 * operations. When trying if an operation work "depth" is increased and the
5570 * stack[] is used to store info. This allows combinations, thus insert one
5571 * character, replace one and delete another. The number of changes is
5572 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005573 */
5574 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00005575suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005576 suginfo_T *su;
5577{
5578 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
5579 char_u tword[MAXWLEN]; /* good word collected so far */
5580 trystate_T stack[MAXWLEN];
5581 char_u preword[MAXWLEN * 3]; /* word found with proper case (appended
5582 * to for word split) */
5583 char_u prewordlen = 0; /* length of word in "preword" */
5584 int splitoff = 0; /* index in tword after last split */
5585 trystate_T *sp;
5586 int newscore;
5587 langp_T *lp;
5588 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005589 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005590 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005591 int c, c2, c3;
5592 int n = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005593 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005594 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005595 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005596 int len;
5597 char_u *p;
5598 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00005599 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005600 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005601
5602 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00005603 * to find matches (esp. REP items). Append some more text, changing
5604 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005605 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005606 n = STRLEN(fword);
5607 p = su->su_badptr + su->su_badlen;
5608 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005609
5610 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
5611 lp->lp_slang != NULL; ++lp)
5612 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005613 /*
5614 * Go through the whole case-fold tree, try changes at each node.
5615 * "tword[]" contains the word collected from nodes in the tree.
5616 * "fword[]" the word we are trying to match with (initially the bad
5617 * word).
5618 */
5619 byts = lp->lp_slang->sl_fbyts;
5620 idxs = lp->lp_slang->sl_fidxs;
5621
5622 depth = 0;
5623 stack[0].ts_state = STATE_START;
5624 stack[0].ts_score = 0;
5625 stack[0].ts_curi = 1;
5626 stack[0].ts_fidx = 0;
5627 stack[0].ts_fidxtry = 0;
5628 stack[0].ts_twordlen = 0;
5629 stack[0].ts_arridx = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00005630#ifdef FEAT_MBYTE
5631 stack[0].ts_tcharlen = 0;
5632#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005633
Bram Moolenaarea424162005-06-16 21:51:00 +00005634 /*
5635 * Loop to find all suggestions. At each round we either:
5636 * - For the current state try one operation, advance "ts_curi",
5637 * increase "depth".
5638 * - When a state is done go to the next, set "ts_state".
5639 * - When all states are tried decrease "depth".
5640 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005641 while (depth >= 0 && !got_int)
5642 {
5643 sp = &stack[depth];
5644 switch (sp->ts_state)
5645 {
5646 case STATE_START:
5647 /*
5648 * Start of node: Deal with NUL bytes, which means
5649 * tword[] may end here.
5650 */
5651 arridx = sp->ts_arridx; /* current node in the tree */
5652 len = byts[arridx]; /* bytes in this node */
5653 arridx += sp->ts_curi; /* index of current byte */
5654
Bram Moolenaar0c405862005-06-22 22:26:26 +00005655 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005656 {
5657 /* Past bytes in node and/or past NUL bytes. */
5658 sp->ts_state = STATE_ENDNUL;
5659 break;
5660 }
5661
5662 /*
5663 * End of word in tree.
5664 */
5665 ++sp->ts_curi; /* eat one NUL byte */
5666
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005667 flags = (int)idxs[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005668
5669 /*
5670 * Form the word with proper case in preword.
5671 * If there is a word from a previous split, append.
5672 */
5673 tword[sp->ts_twordlen] = NUL;
5674 if (flags & WF_KEEPCAP)
5675 /* Must find the word in the keep-case tree. */
5676 find_keepcap_word(lp->lp_slang, tword + splitoff,
5677 preword + prewordlen);
5678 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00005679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005680 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00005681 * use that for the goodword too. But if the badword is
5682 * allcap and it's only one char long use onecap. */
5683 c = su->su_badflags;
5684 if ((c & WF_ALLCAP)
5685#ifdef FEAT_MBYTE
5686 && su->su_badlen == mb_ptr2len_check(su->su_badptr)
5687#else
5688 && su->su_badlen == 1
5689#endif
5690 )
5691 c = WF_ONECAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005692 make_case_word(tword + splitoff,
Bram Moolenaar0c405862005-06-22 22:26:26 +00005693 preword + prewordlen, flags | c);
5694 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005695
5696 /* Don't use a banned word. It may appear again as a good
5697 * word, thus remember it. */
5698 if (flags & WF_BANNED)
5699 {
5700 add_banned(su, preword + prewordlen);
5701 break;
5702 }
5703 if (was_banned(su, preword + prewordlen))
5704 break;
5705
5706 newscore = 0;
5707 if ((flags & WF_REGION)
5708 && (((unsigned)flags >> 8) & lp->lp_region) == 0)
5709 newscore += SCORE_REGION;
5710 if (flags & WF_RARE)
5711 newscore += SCORE_RARE;
5712
Bram Moolenaar0c405862005-06-22 22:26:26 +00005713 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005714 captype(preword + prewordlen, NULL)))
5715 newscore += SCORE_ICASE;
5716
Bram Moolenaar0c405862005-06-22 22:26:26 +00005717 if ((fword[sp->ts_fidx] == NUL
Bram Moolenaarea408852005-06-25 22:49:46 +00005718 || !spell_iswordp(fword + sp->ts_fidx))
Bram Moolenaar0c405862005-06-22 22:26:26 +00005719 && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005720 {
5721 /* The badword also ends: add suggestions, */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005722 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00005723 sp->ts_fidx - repextra,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005724 sp->ts_score + newscore, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005725 }
Bram Moolenaarea424162005-06-16 21:51:00 +00005726 else if (sp->ts_fidx >= sp->ts_fidxtry
5727#ifdef FEAT_MBYTE
5728 /* Don't split halfway a character. */
5729 && (!has_mbyte || sp->ts_tcharlen == 0)
5730#endif
5731 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005732 {
5733 /* The word in the tree ends but the badword
5734 * continues: try inserting a space and check that a valid
5735 * words starts at fword[sp->ts_fidx]. */
5736 if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT))
5737 {
5738 /* Save things to be restored at STATE_SPLITUNDO. */
5739 sp->ts_save_prewordlen = prewordlen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005740 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005741 sp->ts_save_splitoff = splitoff;
5742
5743 /* Append a space to preword. */
5744 STRCAT(preword, " ");
5745 prewordlen = STRLEN(preword);
5746 splitoff = sp->ts_twordlen;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005747#ifdef FEAT_MBYTE
5748 if (has_mbyte)
5749 {
5750 int i = 0;
5751
5752 /* Case-folding may change the number of bytes:
5753 * Count nr of chars in fword[sp->ts_fidx] and
5754 * advance that many chars in su->su_badptr. */
5755 for (p = fword; p < fword + sp->ts_fidx;
5756 mb_ptr_adv(p))
5757 ++i;
5758 for (p = su->su_badptr; i > 0; mb_ptr_adv(p))
5759 --i;
5760 }
5761 else
5762#endif
5763 p = su->su_badptr + sp->ts_fidx;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005764 su->su_badflags = captype(p, su->su_badptr
5765 + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005766
5767 sp->ts_state = STATE_SPLITUNDO;
5768 ++depth;
5769 /* Restart at top of the tree. */
5770 stack[depth].ts_arridx = 0;
5771 }
5772 }
5773 break;
5774
5775 case STATE_SPLITUNDO:
Bram Moolenaar0c405862005-06-22 22:26:26 +00005776 /* Undo the changes done for word split. */
5777 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005778 splitoff = sp->ts_save_splitoff;
5779 prewordlen = sp->ts_save_prewordlen;
5780
5781 /* Continue looking for NUL bytes. */
5782 sp->ts_state = STATE_START;
5783 break;
5784
5785 case STATE_ENDNUL:
5786 /* Past the NUL bytes in the node. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005787 if (fword[sp->ts_fidx] == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005788 {
5789 /* The badword ends, can't use the bytes in this node. */
5790 sp->ts_state = STATE_DEL;
5791 break;
5792 }
5793 sp->ts_state = STATE_PLAIN;
5794 /*FALLTHROUGH*/
5795
5796 case STATE_PLAIN:
5797 /*
5798 * Go over all possible bytes at this node, add each to
5799 * tword[] and use child node. "ts_curi" is the index.
5800 */
5801 arridx = sp->ts_arridx;
5802 if (sp->ts_curi > byts[arridx])
5803 {
5804 /* Done all bytes at this node, do next state. When still
5805 * at already changed bytes skip the other tricks. */
5806 if (sp->ts_fidx >= sp->ts_fidxtry)
5807 sp->ts_state = STATE_DEL;
5808 else
5809 sp->ts_state = STATE_FINAL;
5810 }
5811 else
5812 {
5813 arridx += sp->ts_curi++;
5814 c = byts[arridx];
5815
5816 /* Normal byte, go one level deeper. If it's not equal to
5817 * the byte in the bad word adjust the score. But don't
5818 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00005819 if (c == fword[sp->ts_fidx]
5820#ifdef FEAT_MBYTE
5821 || (sp->ts_tcharlen > 0
5822 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005823#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00005824 )
5825 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005826 else
5827 newscore = SCORE_SUBST;
5828 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
5829 && try_deeper(su, stack, depth, newscore))
5830 {
5831 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005832 sp = &stack[depth];
5833 ++sp->ts_fidx;
5834 tword[sp->ts_twordlen++] = c;
5835 sp->ts_arridx = idxs[arridx];
5836#ifdef FEAT_MBYTE
5837 if (newscore == SCORE_SUBST)
5838 sp->ts_isdiff = DIFF_YES;
5839 if (has_mbyte)
5840 {
5841 /* Multi-byte characters are a bit complicated to
5842 * handle: They differ when any of the bytes
5843 * differ and then their length may also differ. */
5844 if (sp->ts_tcharlen == 0)
5845 {
5846 /* First byte. */
5847 sp->ts_tcharidx = 0;
5848 sp->ts_tcharlen = MB_BYTE2LEN(c);
5849 sp->ts_fcharstart = sp->ts_fidx - 1;
5850 sp->ts_isdiff = (newscore != 0)
5851 ? DIFF_YES : DIFF_NONE;
5852 }
5853 else if (sp->ts_isdiff == DIFF_INSERT)
5854 /* When inserting trail bytes don't advance in
5855 * the bad word. */
5856 --sp->ts_fidx;
5857 if (++sp->ts_tcharidx == sp->ts_tcharlen)
5858 {
5859 /* Last byte of character. */
5860 if (sp->ts_isdiff == DIFF_YES)
5861 {
5862 /* Correct ts_fidx for the byte length of
5863 * the character (we didn't check that
5864 * before). */
5865 sp->ts_fidx = sp->ts_fcharstart
5866 + MB_BYTE2LEN(
5867 fword[sp->ts_fcharstart]);
5868
5869 /* For a similar character adjust score
5870 * from SCORE_SUBST to SCORE_SIMILAR. */
5871 if (lp->lp_slang->sl_has_map
5872 && similar_chars(lp->lp_slang,
5873 mb_ptr2char(tword
5874 + sp->ts_twordlen
5875 - sp->ts_tcharlen),
5876 mb_ptr2char(fword
5877 + sp->ts_fcharstart)))
5878 sp->ts_score -=
5879 SCORE_SUBST - SCORE_SIMILAR;
5880 }
Bram Moolenaarea408852005-06-25 22:49:46 +00005881 else if (sp->ts_isdiff == DIFF_INSERT
5882 && sp->ts_twordlen > sp->ts_tcharlen)
5883 {
5884 /* If the previous character was the same,
5885 * thus doubling a character, give a bonus
5886 * to the score. */
5887 p = tword + sp->ts_twordlen
5888 - sp->ts_tcharlen;
5889 c = mb_ptr2char(p);
5890 mb_ptr_back(tword, p);
5891 if (c == mb_ptr2char(p))
5892 sp->ts_score -= SCORE_INS
5893 - SCORE_INSDUP;
5894 }
Bram Moolenaarea424162005-06-16 21:51:00 +00005895
5896 /* Starting a new char, reset the length. */
5897 sp->ts_tcharlen = 0;
5898 }
5899 }
5900 else
5901#endif
5902 {
5903 /* If we found a similar char adjust the score.
5904 * We do this after calling try_deeper() because
5905 * it's slow. */
5906 if (newscore != 0
5907 && lp->lp_slang->sl_has_map
5908 && similar_chars(lp->lp_slang,
5909 c, fword[sp->ts_fidx - 1]))
5910 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
5911 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005912 }
5913 }
5914 break;
5915
5916 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00005917#ifdef FEAT_MBYTE
5918 /* When past the first byte of a multi-byte char don't try
5919 * delete/insert/swap a character. */
5920 if (has_mbyte && sp->ts_tcharlen > 0)
5921 {
5922 sp->ts_state = STATE_FINAL;
5923 break;
5924 }
5925#endif
5926 /*
5927 * Try skipping one character in the bad word (delete it).
5928 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005929 sp->ts_state = STATE_INS;
5930 sp->ts_curi = 1;
5931 if (fword[sp->ts_fidx] != NUL
5932 && try_deeper(su, stack, depth, SCORE_DEL))
5933 {
5934 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00005935
5936 /* Advance over the character in fword[]. Give a bonus to
5937 * the score if the same character is following "nn" ->
5938 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00005939#ifdef FEAT_MBYTE
5940 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00005941 {
5942 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00005943 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaarea408852005-06-25 22:49:46 +00005944 if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
5945 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
5946 }
Bram Moolenaarea424162005-06-16 21:51:00 +00005947 else
5948#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00005949 {
Bram Moolenaarea424162005-06-16 21:51:00 +00005950 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00005951 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
5952 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
5953 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005954 break;
5955 }
5956 /*FALLTHROUGH*/
5957
5958 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00005959 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005960 * node. */
5961 n = sp->ts_arridx;
5962 if (sp->ts_curi > byts[n])
5963 {
5964 /* Done all bytes at this node, do next state. */
5965 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005966 }
5967 else
5968 {
Bram Moolenaarea424162005-06-16 21:51:00 +00005969 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005970 n += sp->ts_curi++;
5971 c = byts[n];
5972 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
5973 {
5974 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005975 sp = &stack[depth];
5976 tword[sp->ts_twordlen++] = c;
5977 sp->ts_arridx = idxs[n];
5978#ifdef FEAT_MBYTE
5979 if (has_mbyte)
5980 {
5981 fl = MB_BYTE2LEN(c);
5982 if (fl > 1)
5983 {
5984 /* There are following bytes for the same
5985 * character. We must find all bytes before
5986 * trying delete/insert/swap/etc. */
5987 sp->ts_tcharlen = fl;
5988 sp->ts_tcharidx = 1;
5989 sp->ts_isdiff = DIFF_INSERT;
5990 }
5991 }
Bram Moolenaarea408852005-06-25 22:49:46 +00005992 else
5993 fl = 1;
5994 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00005995#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00005996 {
5997 /* If the previous character was the same, thus
5998 * doubling a character, give a bonus to the
5999 * score. */
6000 if (sp->ts_twordlen >= 2
6001 && tword[sp->ts_twordlen - 2] == c)
6002 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
6003 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006004 }
6005 }
6006 break;
6007
6008 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00006009 /*
6010 * Swap two bytes in the bad word: "12" -> "21".
6011 * We change "fword" here, it's changed back afterwards.
6012 */
6013 p = fword + sp->ts_fidx;
6014 c = *p;
6015 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006016 {
Bram Moolenaarea424162005-06-16 21:51:00 +00006017 /* End of word, can't swap or replace. */
6018 sp->ts_state = STATE_FINAL;
6019 break;
6020 }
6021#ifdef FEAT_MBYTE
6022 if (has_mbyte)
6023 {
6024 n = mb_ptr2len_check(p);
6025 c = mb_ptr2char(p);
6026 c2 = mb_ptr2char(p + n);
6027 }
6028 else
6029#endif
6030 c2 = p[1];
6031 if (c == c2)
6032 {
6033 /* Characters are identical, swap won't do anything. */
6034 sp->ts_state = STATE_SWAP3;
6035 break;
6036 }
6037 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
6038 {
6039 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006040 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00006041#ifdef FEAT_MBYTE
6042 if (has_mbyte)
6043 {
6044 fl = mb_char2len(c2);
6045 mch_memmove(p, p + n, fl);
6046 mb_char2bytes(c, p + fl);
6047 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
6048 }
6049 else
6050#endif
6051 {
6052 p[0] = c2;
6053 p[1] = c;
6054 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
6055 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006056 }
6057 else
6058 /* If this swap doesn't work then SWAP3 won't either. */
6059 sp->ts_state = STATE_REP_INI;
6060 break;
6061
Bram Moolenaarea424162005-06-16 21:51:00 +00006062 case STATE_UNSWAP:
6063 /* Undo the STATE_SWAP swap: "21" -> "12". */
6064 p = fword + sp->ts_fidx;
6065#ifdef FEAT_MBYTE
6066 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006067 {
Bram Moolenaarea424162005-06-16 21:51:00 +00006068 n = MB_BYTE2LEN(*p);
6069 c = mb_ptr2char(p + n);
6070 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
6071 mb_char2bytes(c, p);
6072 }
6073 else
6074#endif
6075 {
6076 c = *p;
6077 *p = p[1];
6078 p[1] = c;
6079 }
6080 /*FALLTHROUGH*/
6081
6082 case STATE_SWAP3:
6083 /* Swap two bytes, skipping one: "123" -> "321". We change
6084 * "fword" here, it's changed back afterwards. */
6085 p = fword + sp->ts_fidx;
6086#ifdef FEAT_MBYTE
6087 if (has_mbyte)
6088 {
6089 n = mb_ptr2len_check(p);
6090 c = mb_ptr2char(p);
6091 fl = mb_ptr2len_check(p + n);
6092 c2 = mb_ptr2char(p + n);
6093 c3 = mb_ptr2char(p + n + fl);
6094 }
6095 else
6096#endif
6097 {
6098 c = *p;
6099 c2 = p[1];
6100 c3 = p[2];
6101 }
6102
6103 /* When characters are identical: "121" then SWAP3 result is
6104 * identical, ROT3L result is same as SWAP: "211", ROT3L
6105 * result is same as SWAP on next char: "112". Thus skip all
6106 * swapping. Also skip when c3 is NUL. */
6107 if (c == c3 || c3 == NUL)
6108 {
6109 sp->ts_state = STATE_REP_INI;
6110 break;
6111 }
6112 if (try_deeper(su, stack, depth, SCORE_SWAP3))
6113 {
6114 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006115 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00006116#ifdef FEAT_MBYTE
6117 if (has_mbyte)
6118 {
6119 tl = mb_char2len(c3);
6120 mch_memmove(p, p + n + fl, tl);
6121 mb_char2bytes(c2, p + tl);
6122 mb_char2bytes(c, p + fl + tl);
6123 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
6124 }
6125 else
6126#endif
6127 {
6128 p[0] = p[2];
6129 p[2] = c;
6130 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
6131 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006132 }
6133 else
6134 sp->ts_state = STATE_REP_INI;
6135 break;
6136
Bram Moolenaarea424162005-06-16 21:51:00 +00006137 case STATE_UNSWAP3:
6138 /* Undo STATE_SWAP3: "321" -> "123" */
6139 p = fword + sp->ts_fidx;
6140#ifdef FEAT_MBYTE
6141 if (has_mbyte)
6142 {
6143 n = MB_BYTE2LEN(*p);
6144 c2 = mb_ptr2char(p + n);
6145 fl = MB_BYTE2LEN(p[n]);
6146 c = mb_ptr2char(p + n + fl);
6147 tl = MB_BYTE2LEN(p[n + fl]);
6148 mch_memmove(p + fl + tl, p, n);
6149 mb_char2bytes(c, p);
6150 mb_char2bytes(c2, p + tl);
6151 }
6152 else
6153#endif
6154 {
6155 c = *p;
6156 *p = p[2];
6157 p[2] = c;
6158 }
Bram Moolenaarea424162005-06-16 21:51:00 +00006159
Bram Moolenaarea424162005-06-16 21:51:00 +00006160 /* Rotate three characters left: "123" -> "231". We change
6161 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006162 if (try_deeper(su, stack, depth, SCORE_SWAP3))
6163 {
Bram Moolenaarea424162005-06-16 21:51:00 +00006164 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006165 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00006166 p = fword + sp->ts_fidx;
6167#ifdef FEAT_MBYTE
6168 if (has_mbyte)
6169 {
6170 n = mb_ptr2len_check(p);
6171 c = mb_ptr2char(p);
6172 fl = mb_ptr2len_check(p + n);
6173 fl += mb_ptr2len_check(p + n + fl);
6174 mch_memmove(p, p + n, fl);
6175 mb_char2bytes(c, p + fl);
6176 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
6177 }
6178 else
6179#endif
6180 {
6181 c = *p;
6182 *p = p[1];
6183 p[1] = p[2];
6184 p[2] = c;
6185 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
6186 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006187 }
6188 else
6189 sp->ts_state = STATE_REP_INI;
6190 break;
6191
Bram Moolenaarea424162005-06-16 21:51:00 +00006192 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +00006193 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00006194 p = fword + sp->ts_fidx;
6195#ifdef FEAT_MBYTE
6196 if (has_mbyte)
6197 {
6198 n = MB_BYTE2LEN(*p);
6199 n += MB_BYTE2LEN(p[n]);
6200 c = mb_ptr2char(p + n);
6201 tl = MB_BYTE2LEN(p[n]);
6202 mch_memmove(p + tl, p, n);
6203 mb_char2bytes(c, p);
6204 }
6205 else
6206#endif
6207 {
6208 c = p[2];
6209 p[2] = p[1];
6210 p[1] = *p;
6211 *p = c;
6212 }
Bram Moolenaarea424162005-06-16 21:51:00 +00006213
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006214 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00006215 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006216 if (try_deeper(su, stack, depth, SCORE_SWAP3))
6217 {
Bram Moolenaarea424162005-06-16 21:51:00 +00006218 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006219 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00006220 p = fword + sp->ts_fidx;
6221#ifdef FEAT_MBYTE
6222 if (has_mbyte)
6223 {
6224 n = mb_ptr2len_check(p);
6225 n += mb_ptr2len_check(p + n);
6226 c = mb_ptr2char(p + n);
6227 tl = mb_ptr2len_check(p + n);
6228 mch_memmove(p + tl, p, n);
6229 mb_char2bytes(c, p);
6230 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
6231 }
6232 else
6233#endif
6234 {
6235 c = p[2];
6236 p[2] = p[1];
6237 p[1] = *p;
6238 *p = c;
6239 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
6240 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006241 }
6242 else
6243 sp->ts_state = STATE_REP_INI;
6244 break;
6245
Bram Moolenaarea424162005-06-16 21:51:00 +00006246 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +00006247 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00006248 p = fword + sp->ts_fidx;
6249#ifdef FEAT_MBYTE
6250 if (has_mbyte)
6251 {
6252 c = mb_ptr2char(p);
6253 tl = MB_BYTE2LEN(*p);
6254 n = MB_BYTE2LEN(p[tl]);
6255 n += MB_BYTE2LEN(p[tl + n]);
6256 mch_memmove(p, p + tl, n);
6257 mb_char2bytes(c, p + n);
6258 }
6259 else
6260#endif
6261 {
6262 c = *p;
6263 *p = p[1];
6264 p[1] = p[2];
6265 p[2] = c;
6266 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006267 /*FALLTHROUGH*/
6268
6269 case STATE_REP_INI:
6270 /* Check if matching with REP items from the .aff file would
6271 * work. Quickly skip if there are no REP items or the score
6272 * is going to be too high anyway. */
6273 gap = &lp->lp_slang->sl_rep;
6274 if (gap->ga_len == 0
6275 || sp->ts_score + SCORE_REP >= su->su_maxscore)
6276 {
6277 sp->ts_state = STATE_FINAL;
6278 break;
6279 }
6280
6281 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00006282 * may match. If the index is -1 there is none. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006283 sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]];
6284 if (sp->ts_curi < 0)
6285 {
6286 sp->ts_state = STATE_FINAL;
6287 break;
6288 }
6289
6290 sp->ts_state = STATE_REP;
6291 /*FALLTHROUGH*/
6292
6293 case STATE_REP:
6294 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00006295 * match replace the characters and check if the resulting
6296 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006297 p = fword + sp->ts_fidx;
6298
6299 gap = &lp->lp_slang->sl_rep;
6300 while (sp->ts_curi < gap->ga_len)
6301 {
6302 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
6303 if (*ftp->ft_from != *p)
6304 {
6305 /* past possible matching entries */
6306 sp->ts_curi = gap->ga_len;
6307 break;
6308 }
6309 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
6310 && try_deeper(su, stack, depth, SCORE_REP))
6311 {
6312 /* Need to undo this afterwards. */
6313 sp->ts_state = STATE_REP_UNDO;
6314
6315 /* Change the "from" to the "to" string. */
6316 ++depth;
6317 fl = STRLEN(ftp->ft_from);
6318 tl = STRLEN(ftp->ft_to);
6319 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006321 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006322 repextra += tl - fl;
6323 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006324 mch_memmove(p, ftp->ft_to, tl);
6325 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +00006326#ifdef FEAT_MBYTE
6327 stack[depth].ts_tcharlen = 0;
6328#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006329 break;
6330 }
6331 }
6332
6333 if (sp->ts_curi >= gap->ga_len)
6334 /* No (more) matches. */
6335 sp->ts_state = STATE_FINAL;
6336
6337 break;
6338
6339 case STATE_REP_UNDO:
6340 /* Undo a REP replacement and continue with the next one. */
6341 ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data
6342 + sp->ts_curi - 1;
6343 fl = STRLEN(ftp->ft_from);
6344 tl = STRLEN(ftp->ft_to);
6345 p = fword + sp->ts_fidx;
6346 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006348 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006349 repextra -= tl - fl;
6350 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006351 mch_memmove(p, ftp->ft_from, fl);
6352 sp->ts_state = STATE_REP;
6353 break;
6354
6355 default:
6356 /* Did all possible states at this level, go up one level. */
6357 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006358
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006359 /* Don't check for CTRL-C too often, it takes time. */
6360 line_breakcheck();
6361 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006362 }
6363 }
6364}
6365
6366/*
6367 * Try going one level deeper in the tree.
6368 */
6369 static int
6370try_deeper(su, stack, depth, score_add)
6371 suginfo_T *su;
6372 trystate_T *stack;
6373 int depth;
6374 int score_add;
6375{
6376 int newscore;
6377
6378 /* Refuse to go deeper if the scrore is getting too big. */
6379 newscore = stack[depth].ts_score + score_add;
6380 if (newscore >= su->su_maxscore)
6381 return FALSE;
6382
Bram Moolenaarea424162005-06-16 21:51:00 +00006383 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006384 stack[depth + 1].ts_state = STATE_START;
6385 stack[depth + 1].ts_score = newscore;
6386 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006387 return TRUE;
6388}
6389
6390/*
6391 * "fword" is a good word with case folded. Find the matching keep-case
6392 * words and put it in "kword".
6393 * Theoretically there could be several keep-case words that result in the
6394 * same case-folded word, but we only find one...
6395 */
6396 static void
6397find_keepcap_word(slang, fword, kword)
6398 slang_T *slang;
6399 char_u *fword;
6400 char_u *kword;
6401{
6402 char_u uword[MAXWLEN]; /* "fword" in upper-case */
6403 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006404 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006405
6406 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006407 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006408 int round[MAXWLEN];
6409 int fwordidx[MAXWLEN];
6410 int uwordidx[MAXWLEN];
6411 int kwordlen[MAXWLEN];
6412
6413 int flen, ulen;
6414 int l;
6415 int len;
6416 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006417 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006418 char_u *p;
6419 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006420 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006421
6422 if (byts == NULL)
6423 {
6424 /* array is empty: "cannot happen" */
6425 *kword = NUL;
6426 return;
6427 }
6428
6429 /* Make an all-cap version of "fword". */
6430 allcap_copy(fword, uword);
6431
6432 /*
6433 * Each character needs to be tried both case-folded and upper-case.
6434 * All this gets very complicated if we keep in mind that changing case
6435 * may change the byte length of a multi-byte character...
6436 */
6437 depth = 0;
6438 arridx[0] = 0;
6439 round[0] = 0;
6440 fwordidx[0] = 0;
6441 uwordidx[0] = 0;
6442 kwordlen[0] = 0;
6443 while (depth >= 0)
6444 {
6445 if (fword[fwordidx[depth]] == NUL)
6446 {
6447 /* We are at the end of "fword". If the tree allows a word to end
6448 * here we have found a match. */
6449 if (byts[arridx[depth] + 1] == 0)
6450 {
6451 kword[kwordlen[depth]] = NUL;
6452 return;
6453 }
6454
6455 /* kword is getting too long, continue one level up */
6456 --depth;
6457 }
6458 else if (++round[depth] > 2)
6459 {
6460 /* tried both fold-case and upper-case character, continue one
6461 * level up */
6462 --depth;
6463 }
6464 else
6465 {
6466 /*
6467 * round[depth] == 1: Try using the folded-case character.
6468 * round[depth] == 2: Try using the upper-case character.
6469 */
6470#ifdef FEAT_MBYTE
6471 if (has_mbyte)
6472 {
6473 flen = mb_ptr2len_check(fword + fwordidx[depth]);
6474 ulen = mb_ptr2len_check(uword + uwordidx[depth]);
6475 }
6476 else
6477#endif
6478 ulen = flen = 1;
6479 if (round[depth] == 1)
6480 {
6481 p = fword + fwordidx[depth];
6482 l = flen;
6483 }
6484 else
6485 {
6486 p = uword + uwordidx[depth];
6487 l = ulen;
6488 }
6489
6490 for (tryidx = arridx[depth]; l > 0; --l)
6491 {
6492 /* Perform a binary search in the list of accepted bytes. */
6493 len = byts[tryidx++];
6494 c = *p++;
6495 lo = tryidx;
6496 hi = tryidx + len - 1;
6497 while (lo < hi)
6498 {
6499 m = (lo + hi) / 2;
6500 if (byts[m] > c)
6501 hi = m - 1;
6502 else if (byts[m] < c)
6503 lo = m + 1;
6504 else
6505 {
6506 lo = hi = m;
6507 break;
6508 }
6509 }
6510
6511 /* Stop if there is no matching byte. */
6512 if (hi < lo || byts[lo] != c)
6513 break;
6514
6515 /* Continue at the child (if there is one). */
6516 tryidx = idxs[lo];
6517 }
6518
6519 if (l == 0)
6520 {
6521 /*
6522 * Found the matching char. Copy it to "kword" and go a
6523 * level deeper.
6524 */
6525 if (round[depth] == 1)
6526 {
6527 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
6528 flen);
6529 kwordlen[depth + 1] = kwordlen[depth] + flen;
6530 }
6531 else
6532 {
6533 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
6534 ulen);
6535 kwordlen[depth + 1] = kwordlen[depth] + ulen;
6536 }
6537 fwordidx[depth + 1] = fwordidx[depth] + flen;
6538 uwordidx[depth + 1] = uwordidx[depth] + ulen;
6539
6540 ++depth;
6541 arridx[depth] = tryidx;
6542 round[depth] = 0;
6543 }
6544 }
6545 }
6546
6547 /* Didn't find it: "cannot happen". */
6548 *kword = NUL;
6549}
6550
6551/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006552 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
6553 * su->su_sga.
6554 */
6555 static void
6556score_comp_sal(su)
6557 suginfo_T *su;
6558{
6559 langp_T *lp;
6560 char_u badsound[MAXWLEN];
6561 int i;
6562 suggest_T *stp;
6563 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006564 int score;
6565
6566 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
6567 return;
6568
6569 /* Use the sound-folding of the first language that supports it. */
6570 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
6571 lp->lp_slang != NULL; ++lp)
6572 if (lp->lp_slang->sl_sal.ga_len > 0)
6573 {
6574 /* soundfold the bad word */
6575 spell_soundfold(lp->lp_slang, su->su_fbadword, badsound);
6576
6577 for (i = 0; i < su->su_ga.ga_len; ++i)
6578 {
6579 stp = &SUG(su->su_ga, i);
6580
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006581 /* Case-fold the suggested word, sound-fold it and compute the
6582 * sound-a-like score. */
6583 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006584 if (score < SCORE_MAXMAX)
6585 {
6586 /* Add the suggestion. */
6587 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
6588 sstp->st_word = vim_strsave(stp->st_word);
6589 if (sstp->st_word != NULL)
6590 {
6591 sstp->st_score = score;
6592 sstp->st_altscore = 0;
6593 sstp->st_orglen = stp->st_orglen;
6594 ++su->su_sga.ga_len;
6595 }
6596 }
6597 }
6598 break;
6599 }
6600}
6601
6602/*
6603 * Combine the list of suggestions in su->su_ga and su->su_sga.
6604 * They are intwined.
6605 */
6606 static void
6607score_combine(su)
6608 suginfo_T *su;
6609{
6610 int i;
6611 int j;
6612 garray_T ga;
6613 garray_T *gap;
6614 langp_T *lp;
6615 suggest_T *stp;
6616 char_u *p;
6617 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006618 int round;
6619
6620 /* Add the alternate score to su_ga. */
6621 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
6622 lp->lp_slang != NULL; ++lp)
6623 {
6624 if (lp->lp_slang->sl_sal.ga_len > 0)
6625 {
6626 /* soundfold the bad word */
6627 spell_soundfold(lp->lp_slang, su->su_fbadword, badsound);
6628
6629 for (i = 0; i < su->su_ga.ga_len; ++i)
6630 {
6631 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006632 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
6633 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006634 if (stp->st_altscore == SCORE_MAXMAX)
6635 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
6636 else
6637 stp->st_score = (stp->st_score * 3
6638 + stp->st_altscore) / 4;
6639 stp->st_salscore = FALSE;
6640 }
6641 break;
6642 }
6643 }
6644
6645 /* Add the alternate score to su_sga. */
6646 for (i = 0; i < su->su_sga.ga_len; ++i)
6647 {
6648 stp = &SUG(su->su_sga, i);
6649 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
6650 if (stp->st_score == SCORE_MAXMAX)
6651 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
6652 else
6653 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
6654 stp->st_salscore = TRUE;
6655 }
6656
6657 /* Sort the suggestions and truncate at "maxcount" for both lists. */
6658 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
6659 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
6660
6661 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
6662 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
6663 return;
6664
6665 stp = &SUG(ga, 0);
6666 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
6667 {
6668 /* round 1: get a suggestion from su_ga
6669 * round 2: get a suggestion from su_sga */
6670 for (round = 1; round <= 2; ++round)
6671 {
6672 gap = round == 1 ? &su->su_ga : &su->su_sga;
6673 if (i < gap->ga_len)
6674 {
6675 /* Don't add a word if it's already there. */
6676 p = SUG(*gap, i).st_word;
6677 for (j = 0; j < ga.ga_len; ++j)
6678 if (STRCMP(stp[j].st_word, p) == 0)
6679 break;
6680 if (j == ga.ga_len)
6681 stp[ga.ga_len++] = SUG(*gap, i);
6682 else
6683 vim_free(p);
6684 }
6685 }
6686 }
6687
6688 ga_clear(&su->su_ga);
6689 ga_clear(&su->su_sga);
6690
6691 /* Truncate the list to the number of suggestions that will be displayed. */
6692 if (ga.ga_len > su->su_maxcount)
6693 {
6694 for (i = su->su_maxcount; i < ga.ga_len; ++i)
6695 vim_free(stp[i].st_word);
6696 ga.ga_len = su->su_maxcount;
6697 }
6698
6699 su->su_ga = ga;
6700}
6701
6702/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006703 * For the goodword in "stp" compute the soundalike score compared to the
6704 * badword.
6705 */
6706 static int
6707stp_sal_score(stp, su, slang, badsound)
6708 suggest_T *stp;
6709 suginfo_T *su;
6710 slang_T *slang;
6711 char_u *badsound; /* sound-folded badword */
6712{
6713 char_u *p;
6714 char_u badsound2[MAXWLEN];
6715 char_u fword[MAXWLEN];
6716 char_u goodsound[MAXWLEN];
6717
6718 if (stp->st_orglen <= su->su_badlen)
6719 p = badsound;
6720 else
6721 {
6722 /* soundfold the bad word with more characters following */
6723 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
6724
6725 /* When joining two words the sound often changes a lot. E.g., "t he"
6726 * sounds like "t h" while "the" sounds like "@". Avoid that by
6727 * removing the space. Don't do it when the good word also contains a
6728 * space. */
6729 if (vim_iswhite(su->su_badptr[su->su_badlen])
6730 && *skiptowhite(stp->st_word) == NUL)
6731 for (p = fword; *(p = skiptowhite(p)) != NUL; )
6732 mch_memmove(p, p + 1, STRLEN(p));
6733
6734 spell_soundfold(slang, fword, badsound2);
6735 p = badsound2;
6736 }
6737
6738 /* Case-fold the word, sound-fold the word and compute the score for the
6739 * difference. */
6740 (void)spell_casefold(stp->st_word, STRLEN(stp->st_word), fword, MAXWLEN);
6741 spell_soundfold(slang, fword, goodsound);
6742
6743 return soundalike_score(goodsound, p);
6744}
6745
6746/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006747 * Find suggestions by comparing the word in a sound-a-like form.
6748 */
6749 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00006750suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006751 suginfo_T *su;
6752{
6753 char_u salword[MAXWLEN];
6754 char_u tword[MAXWLEN];
6755 char_u tfword[MAXWLEN];
6756 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006757 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006758 int curi[MAXWLEN];
6759 langp_T *lp;
6760 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006761 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006762 int depth;
6763 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006764 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006765 int round;
6766 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006767 int sound_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006768
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006769 /* Do this for all languages that support sound folding. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006770 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
6771 lp->lp_slang != NULL; ++lp)
6772 {
6773 if (lp->lp_slang->sl_sal.ga_len > 0)
6774 {
6775 /* soundfold the bad word */
6776 spell_soundfold(lp->lp_slang, su->su_fbadword, salword);
6777
6778 /*
6779 * Go through the whole tree, soundfold each word and compare.
6780 * round 1: use the case-folded tree.
6781 * round 2: use the keep-case tree.
6782 */
6783 for (round = 1; round <= 2; ++round)
6784 {
6785 if (round == 1)
6786 {
6787 byts = lp->lp_slang->sl_fbyts;
6788 idxs = lp->lp_slang->sl_fidxs;
6789 }
6790 else
6791 {
6792 byts = lp->lp_slang->sl_kbyts;
6793 idxs = lp->lp_slang->sl_kidxs;
6794 }
6795
6796 depth = 0;
6797 arridx[0] = 0;
6798 curi[0] = 1;
6799 while (depth >= 0 && !got_int)
6800 {
6801 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006802 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006803 /* Done all bytes at this node, go up one level. */
6804 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006805 line_breakcheck();
6806 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006807 else
6808 {
6809 /* Do one more byte at this node. */
6810 n = arridx[depth] + curi[depth];
6811 ++curi[depth];
6812 c = byts[n];
6813 if (c == 0)
6814 {
6815 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006816 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006817 if (round == 2 || (flags & WF_KEEPCAP) == 0)
6818 {
6819 tword[depth] = NUL;
6820 if (round == 1)
6821 spell_soundfold(lp->lp_slang,
6822 tword, tsalword);
6823 else
6824 {
6825 /* In keep-case tree need to case-fold the
6826 * word. */
6827 (void)spell_casefold(tword, depth,
6828 tfword, MAXWLEN);
6829 spell_soundfold(lp->lp_slang,
6830 tfword, tsalword);
6831 }
6832
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006833 /* Compute the edit distance between the
6834 * sound-a-like words. */
6835 sound_score = soundalike_score(salword,
6836 tsalword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006837 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006838 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006839 char_u cword[MAXWLEN];
6840 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006841 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006842
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006843 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006844 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006845 /* Need to fix case according to
6846 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006847 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006848 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006849 }
6850 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006851 p = tword;
6852
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006853 if (sps_flags & SPS_DOUBLE)
6854 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00006855 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006856 sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006857 else
6858 {
6859 /* Compute the score. */
6860 score = spell_edit_score(
6861 su->su_badword, p);
6862 if (sps_flags & SPS_BEST)
6863 /* give a bonus for the good word
6864 * sounding the same as the bad
6865 * word */
6866 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00006867 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006868 RESCORE(score, sound_score),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006869 sound_score, TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006870 else
6871 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00006872 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006873 score + sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006874 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006875 }
6876 }
6877
6878 /* Skip over other NUL bytes. */
6879 while (byts[n + 1] == 0)
6880 {
6881 ++n;
6882 ++curi[depth];
6883 }
6884 }
6885 else
6886 {
6887 /* Normal char, go one level deeper. */
6888 tword[depth++] = c;
6889 arridx[depth] = idxs[n];
6890 curi[depth] = 1;
6891 }
6892 }
6893 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006894 }
6895 }
6896 }
6897}
6898
6899/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006900 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006901 */
6902 static void
6903make_case_word(fword, cword, flags)
6904 char_u *fword;
6905 char_u *cword;
6906 int flags;
6907{
6908 if (flags & WF_ALLCAP)
6909 /* Make it all upper-case */
6910 allcap_copy(fword, cword);
6911 else if (flags & WF_ONECAP)
6912 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006913 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006914 else
6915 /* Use goodword as-is. */
6916 STRCPY(cword, fword);
6917}
6918
Bram Moolenaarea424162005-06-16 21:51:00 +00006919/*
6920 * Use map string "map" for languages "lp".
6921 */
6922 static void
6923set_map_str(lp, map)
6924 slang_T *lp;
6925 char_u *map;
6926{
6927 char_u *p;
6928 int headc = 0;
6929 int c;
6930 int i;
6931
6932 if (*map == NUL)
6933 {
6934 lp->sl_has_map = FALSE;
6935 return;
6936 }
6937 lp->sl_has_map = TRUE;
6938
6939 /* Init the array and hash table empty. */
6940 for (i = 0; i < 256; ++i)
6941 lp->sl_map_array[i] = 0;
6942#ifdef FEAT_MBYTE
6943 hash_init(&lp->sl_map_hash);
6944#endif
6945
6946 /*
6947 * The similar characters are stored separated with slashes:
6948 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
6949 * before the same slash. For characters above 255 sl_map_hash is used.
6950 */
6951 for (p = map; *p != NUL; )
6952 {
6953#ifdef FEAT_MBYTE
6954 c = mb_ptr2char_adv(&p);
6955#else
6956 c = *p++;
6957#endif
6958 if (c == '/')
6959 headc = 0;
6960 else
6961 {
6962 if (headc == 0)
6963 headc = c;
6964
6965#ifdef FEAT_MBYTE
6966 /* Characters above 255 don't fit in sl_map_array[], put them in
6967 * the hash table. Each entry is the char, a NUL the headchar and
6968 * a NUL. */
6969 if (c >= 256)
6970 {
6971 int cl = mb_char2len(c);
6972 int headcl = mb_char2len(headc);
6973 char_u *b;
6974 hash_T hash;
6975 hashitem_T *hi;
6976
6977 b = alloc((unsigned)(cl + headcl + 2));
6978 if (b == NULL)
6979 return;
6980 mb_char2bytes(c, b);
6981 b[cl] = NUL;
6982 mb_char2bytes(headc, b + cl + 1);
6983 b[cl + 1 + headcl] = NUL;
6984 hash = hash_hash(b);
6985 hi = hash_lookup(&lp->sl_map_hash, b, hash);
6986 if (HASHITEM_EMPTY(hi))
6987 hash_add_item(&lp->sl_map_hash, hi, b, hash);
6988 else
6989 {
6990 /* This should have been checked when generating the .spl
6991 * file. */
6992 EMSG(_("E999: duplicate char in MAP entry"));
6993 vim_free(b);
6994 }
6995 }
6996 else
6997#endif
6998 lp->sl_map_array[c] = headc;
6999 }
7000 }
7001}
7002
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007003/*
7004 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
7005 * lines in the .aff file.
7006 */
7007 static int
7008similar_chars(slang, c1, c2)
7009 slang_T *slang;
7010 int c1;
7011 int c2;
7012{
Bram Moolenaarea424162005-06-16 21:51:00 +00007013 int m1, m2;
7014#ifdef FEAT_MBYTE
7015 char_u buf[MB_MAXBYTES];
7016 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007017
Bram Moolenaarea424162005-06-16 21:51:00 +00007018 if (c1 >= 256)
7019 {
7020 buf[mb_char2bytes(c1, buf)] = 0;
7021 hi = hash_find(&slang->sl_map_hash, buf);
7022 if (HASHITEM_EMPTY(hi))
7023 m1 = 0;
7024 else
7025 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
7026 }
7027 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007028#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00007029 m1 = slang->sl_map_array[c1];
7030 if (m1 == 0)
7031 return FALSE;
7032
7033
7034#ifdef FEAT_MBYTE
7035 if (c2 >= 256)
7036 {
7037 buf[mb_char2bytes(c2, buf)] = 0;
7038 hi = hash_find(&slang->sl_map_hash, buf);
7039 if (HASHITEM_EMPTY(hi))
7040 m2 = 0;
7041 else
7042 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
7043 }
7044 else
7045#endif
7046 m2 = slang->sl_map_array[c2];
7047
7048 return m1 == m2;
7049}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007050
7051/*
7052 * Add a suggestion to the list of suggestions.
7053 * Do not add a duplicate suggestion or suggestions with a bad score.
7054 * When "use_score" is not zero it's used, otherwise the score is computed
7055 * with spell_edit_score().
7056 */
7057 static void
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007058add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007059 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007060 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007061 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007062 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007063 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007064 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007065 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007066{
7067 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007068 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007069 char_u *p = NULL;
7070 int c = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007071
7072 /* Check that the word wasn't banned. */
7073 if (was_banned(su, goodword))
7074 return;
7075
Bram Moolenaar0c405862005-06-22 22:26:26 +00007076 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
7077 * Remove the common part from "goodword". */
7078 i = badlen - su->su_badlen;
7079 if (i > 0)
7080 {
7081 /* This assumes there was no case folding or it didn't change the
7082 * length... */
7083 p = goodword + STRLEN(goodword) - i;
7084 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
7085 {
7086 badlen = su->su_badlen;
7087 c = *p;
7088 *p = NUL;
7089 }
7090 else
7091 p = NULL;
7092 }
7093
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007094 if (score <= su->su_maxscore)
7095 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007096 /* Check if the word is already there. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007097 stp = &SUG(*gap, 0);
7098 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007099 if (STRCMP(stp[i].st_word, goodword) == 0)
7100 {
7101 /* Found it. Remember the lowest score. */
7102 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007103 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007104 stp[i].st_score = score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007105 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007106 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007107 break;
7108 }
7109
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007110 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007111 {
7112 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007113 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007114 stp->st_word = vim_strsave(goodword);
7115 if (stp->st_word != NULL)
7116 {
7117 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007118 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007119 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007120 stp->st_orglen = badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007121 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007122
7123 /* If we have too many suggestions now, sort the list and keep
7124 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007125 if (gap->ga_len > SUG_MAX_COUNT(su))
7126 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
7127 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007128 }
7129 }
7130 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00007131
7132 if (p != NULL)
7133 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007134}
7135
7136/*
7137 * Add a word to be banned.
7138 */
7139 static void
7140add_banned(su, word)
7141 suginfo_T *su;
7142 char_u *word;
7143{
7144 char_u *s = vim_strsave(word);
7145 hash_T hash;
7146 hashitem_T *hi;
7147
7148 if (s != NULL)
7149 {
7150 hash = hash_hash(s);
7151 hi = hash_lookup(&su->su_banned, s, hash);
7152 if (HASHITEM_EMPTY(hi))
7153 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00007154 else
7155 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007156 }
7157}
7158
7159/*
7160 * Return TRUE if a word appears in the list of banned words.
7161 */
7162 static int
7163was_banned(su, word)
7164 suginfo_T *su;
7165 char_u *word;
7166{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007167 hashitem_T *hi = hash_find(&su->su_banned, word);
7168
7169 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007170}
7171
7172/*
7173 * Free the banned words in "su".
7174 */
7175 static void
7176free_banned(su)
7177 suginfo_T *su;
7178{
7179 int todo;
7180 hashitem_T *hi;
7181
7182 todo = su->su_banned.ht_used;
7183 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
7184 {
7185 if (!HASHITEM_EMPTY(hi))
7186 {
7187 vim_free(hi->hi_key);
7188 --todo;
7189 }
7190 }
7191 hash_clear(&su->su_banned);
7192}
7193
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007194/*
7195 * Recompute the score if sound-folding is possible. This is slow,
7196 * thus only done for the final results.
7197 */
7198 static void
7199rescore_suggestions(su)
7200 suginfo_T *su;
7201{
7202 langp_T *lp;
7203 suggest_T *stp;
7204 char_u sal_badword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007205 int i;
7206
7207 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7208 lp->lp_slang != NULL; ++lp)
7209 {
7210 if (lp->lp_slang->sl_sal.ga_len > 0)
7211 {
7212 /* soundfold the bad word */
7213 spell_soundfold(lp->lp_slang, su->su_fbadword, sal_badword);
7214
7215 for (i = 0; i < su->su_ga.ga_len; ++i)
7216 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007217 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007218 if (!stp->st_had_bonus)
7219 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007220 stp->st_altscore = stp_sal_score(stp, su,
7221 lp->lp_slang, sal_badword);
7222 if (stp->st_altscore == SCORE_MAXMAX)
7223 stp->st_altscore = SCORE_BIG;
7224 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007225 }
7226 }
7227 break;
7228 }
7229 }
7230}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007231
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007232static int
7233#ifdef __BORLANDC__
7234_RTLENTRYF
7235#endif
7236sug_compare __ARGS((const void *s1, const void *s2));
7237
7238/*
7239 * Function given to qsort() to sort the suggestions on st_score.
7240 */
7241 static int
7242#ifdef __BORLANDC__
7243_RTLENTRYF
7244#endif
7245sug_compare(s1, s2)
7246 const void *s1;
7247 const void *s2;
7248{
7249 suggest_T *p1 = (suggest_T *)s1;
7250 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007251 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007252
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007253 if (n == 0)
7254 return p1->st_altscore - p2->st_altscore;
7255 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007256}
7257
7258/*
7259 * Cleanup the suggestions:
7260 * - Sort on score.
7261 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007262 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007263 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007264 static int
7265cleanup_suggestions(gap, maxscore, keep)
7266 garray_T *gap;
7267 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007268 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007269{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007270 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007271 int i;
7272
7273 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007274 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007275
7276 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007277 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007278 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007279 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007280 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007281 gap->ga_len = keep;
7282 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007283 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007284 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007285}
7286
7287/*
7288 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
7289 */
7290 static void
7291spell_soundfold(slang, inword, res)
7292 slang_T *slang;
7293 char_u *inword;
7294 char_u *res;
7295{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007296 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007297 char_u word[MAXWLEN];
7298#ifdef FEAT_MBYTE
7299 int l;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007300 int found_mbyte = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007301#endif
7302 char_u *s;
7303 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007304 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007305 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007306 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007307 int n, k = 0;
7308 int z0;
7309 int k0;
7310 int n0;
7311 int c;
7312 int pri;
7313 int p0 = -333;
7314 int c0;
7315
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007316 /* Remove accents, if wanted. We actually remove all non-word characters.
7317 * But keep white space. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007318 if (slang->sl_rem_accents)
7319 {
7320 t = word;
7321 for (s = inword; *s != NUL; )
7322 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007323 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007324 {
7325 *t++ = ' ';
7326 s = skipwhite(s);
7327 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007328#ifdef FEAT_MBYTE
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007329 else if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007330 {
7331 l = mb_ptr2len_check(s);
Bram Moolenaarea408852005-06-25 22:49:46 +00007332 if (spell_iswordp(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007333 {
7334 mch_memmove(t, s, l);
7335 t += l;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007336 if (l > 1)
7337 found_mbyte = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007338 }
7339 s += l;
7340 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007341#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007342 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007343 {
Bram Moolenaarea408852005-06-25 22:49:46 +00007344 if (spell_iswordp(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007345 *t++ = *s;
7346 ++s;
7347 }
7348 }
7349 *t = NUL;
7350 }
7351 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007352 {
7353#ifdef FEAT_MBYTE
7354 if (has_mbyte)
7355 for (s = inword; *s != NUL; s += l)
7356 if ((l = mb_ptr2len_check(s)) > 1)
7357 {
7358 found_mbyte = TRUE;
7359 break;
7360 }
7361#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007362 STRCPY(word, inword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007363 }
7364
7365#ifdef FEAT_MBYTE
7366 /* If there are multi-byte characters in the word return it as-is, because
7367 * the following won't work. */
7368 if (found_mbyte)
7369 {
7370 STRCPY(res, word);
7371 return;
7372 }
7373#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007374
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007375 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007376
7377 /*
7378 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007379 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007380 * TODO: support for multi-byte chars.
7381 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007382 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007383 while ((c = word[i]) != NUL)
7384 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007385 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007386 n = slang->sl_sal_first[c];
7387 z0 = 0;
7388
7389 if (n >= 0)
7390 {
7391 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007392 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007393 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007394 /* Quickly skip entries that don't match the word. Most
7395 * entries are less then three chars, optimize for that. */
7396 k = smp[n].sm_leadlen;
7397 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007398 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007399 if (word[i + 1] != s[1])
7400 continue;
7401 if (k > 2)
7402 {
7403 for (j = 2; j < k; ++j)
7404 if (word[i + j] != s[j])
7405 break;
7406 if (j < k)
7407 continue;
7408 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007409 }
7410
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007411 if ((pf = smp[n].sm_oneoff) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007412 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007413 /* Check for match with one of the chars in "sm_oneoff". */
7414 while (*pf != NUL && *pf != word[i + k])
7415 ++pf;
7416 if (*pf == NUL)
7417 continue;
7418 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007419 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007420 s = smp[n].sm_rules;
7421 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007422
7423 p0 = *s;
7424 k0 = k;
7425 while (*s == '-' && k > 1)
7426 {
7427 k--;
7428 s++;
7429 }
7430 if (*s == '<')
7431 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007432 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007433 {
7434 /* determine priority */
7435 pri = *s - '0';
7436 s++;
7437 }
7438 if (*s == '^' && *(s + 1) == '^')
7439 s++;
7440
7441 if (*s == NUL
7442 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007443 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaarea408852005-06-25 22:49:46 +00007444 || spell_iswordp(word + i - 1)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007445 && (*(s + 1) != '$'
Bram Moolenaarea408852005-06-25 22:49:46 +00007446 || (!spell_iswordp(word + i + k0))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007447 || (*s == '$' && i > 0
Bram Moolenaarea408852005-06-25 22:49:46 +00007448 && spell_iswordp(word + i - 1)
7449 && (!spell_iswordp(word + i + k0))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007450 {
7451 /* search for followup rules, if: */
7452 /* followup and k > 1 and NO '-' in searchstring */
7453 c0 = word[i + k - 1];
7454 n0 = slang->sl_sal_first[c0];
7455
7456 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007457 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007458 {
7459 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007460 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007461 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007462 /* Quickly skip entries that don't match the word.
7463 * */
7464 k0 = smp[n0].sm_leadlen;
7465 if (k0 > 1)
7466 {
7467 if (word[i + k] != s[1])
7468 continue;
7469 if (k0 > 2)
7470 {
7471 pf = word + i + k + 1;
7472 for (j = 2; j < k0; ++j)
7473 if (*pf++ != s[j])
7474 break;
7475 if (j < k0)
7476 continue;
7477 }
7478 }
7479 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007480
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007481 if ((pf = smp[n0].sm_oneoff) != NULL)
7482 {
7483 /* Check for match with one of the chars in
7484 * "sm_oneoff". */
7485 while (*pf != NUL && *pf != word[i + k0])
7486 ++pf;
7487 if (*pf == NUL)
7488 continue;
7489 ++k0;
7490 }
7491
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007492 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007493 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007494 while (*s == '-')
7495 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007496 /* "k0" gets NOT reduced because
7497 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007498 s++;
7499 }
7500 if (*s == '<')
7501 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007502 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007503 {
7504 p0 = *s - '0';
7505 s++;
7506 }
7507
7508 if (*s == NUL
7509 /* *s == '^' cuts */
7510 || (*s == '$'
Bram Moolenaarea408852005-06-25 22:49:46 +00007511 && !spell_iswordp(word + i + k0)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007512 {
7513 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007514 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007515 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007516
7517 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007518 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007519 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007520 /* rule fits; stop search */
7521 break;
7522 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007523 }
7524
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007525 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007526 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007527 }
7528
7529 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007530 s = smp[n].sm_to;
7531 pf = smp[n].sm_rules;
7532 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007533 if (p0 == 1 && z == 0)
7534 {
7535 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007536 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
7537 || res[reslen - 1] == *s))
7538 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007539 z0 = 1;
7540 z = 1;
7541 k0 = 0;
7542 while (*s != NUL && word[i+k0] != NUL)
7543 {
7544 word[i + k0] = *s;
7545 k0++;
7546 s++;
7547 }
7548 if (k > k0)
7549 mch_memmove(word + i + k0, word + i + k,
7550 STRLEN(word + i + k) + 1);
7551
7552 /* new "actual letter" */
7553 c = word[i];
7554 }
7555 else
7556 {
7557 /* no '<' rule used */
7558 i += k - 1;
7559 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007560 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007561 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007562 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007563 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007564 res[reslen] = *s;
7565 reslen++;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007566 }
7567 s++;
7568 }
7569 /* new "actual letter" */
7570 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007571 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007572 {
7573 if (c != NUL)
7574 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007575 res[reslen] = c;
7576 reslen++;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007577 }
7578 mch_memmove(word, word + i + 1,
7579 STRLEN(word + i + 1) + 1);
7580 i = 0;
7581 z0 = 1;
7582 }
7583 }
7584 break;
7585 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007586 }
7587 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007588 else if (vim_iswhite(c))
7589 {
7590 c = ' ';
7591 k = 1;
7592 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007593
7594 if (z0 == 0)
7595 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007596 if (k && !p0 && reslen < MAXWLEN && c != NUL
7597 && (!slang->sl_collapse || reslen == 0
7598 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007599 {
7600 /* condense only double letters */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007601 res[reslen] = c;
7602 reslen++;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007603 }
7604
7605 i++;
7606 z = 0;
7607 k = 0;
7608 }
7609 }
7610
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007611 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007612}
7613
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007614/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007615 * Compute a score for two sound-a-like words.
7616 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
7617 * Instead of a generic loop we write out the code. That keeps it fast by
7618 * avoiding checks that will not be possible.
7619 */
7620 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007621soundalike_score(goodstart, badstart)
7622 char_u *goodstart; /* sound-folded good word */
7623 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007624{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007625 char_u *goodsound = goodstart;
7626 char_u *badsound = badstart;
7627 int goodlen;
7628 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007629 int n;
7630 char_u *pl, *ps;
7631 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007632 int score = 0;
7633
7634 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
7635 * counted so much, vowels halfway the word aren't counted at all. */
7636 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
7637 {
7638 score = SCORE_DEL / 2;
7639 if (*badsound == '*')
7640 ++badsound;
7641 else
7642 ++goodsound;
7643 }
7644
7645 goodlen = STRLEN(goodsound);
7646 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007647
7648 /* Return quickly if the lenghts are too different to be fixed by two
7649 * changes. */
7650 n = goodlen - badlen;
7651 if (n < -2 || n > 2)
7652 return SCORE_MAXMAX;
7653
7654 if (n > 0)
7655 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007656 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007657 ps = badsound;
7658 }
7659 else
7660 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007661 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007662 ps = goodsound;
7663 }
7664
7665 /* Skip over the identical part. */
7666 while (*pl == *ps && *pl != NUL)
7667 {
7668 ++pl;
7669 ++ps;
7670 }
7671
7672 switch (n)
7673 {
7674 case -2:
7675 case 2:
7676 /*
7677 * Must delete two characters from "pl".
7678 */
7679 ++pl; /* first delete */
7680 while (*pl == *ps)
7681 {
7682 ++pl;
7683 ++ps;
7684 }
7685 /* strings must be equal after second delete */
7686 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007687 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007688
7689 /* Failed to compare. */
7690 break;
7691
7692 case -1:
7693 case 1:
7694 /*
7695 * Minimal one delete from "pl" required.
7696 */
7697
7698 /* 1: delete */
7699 pl2 = pl + 1;
7700 ps2 = ps;
7701 while (*pl2 == *ps2)
7702 {
7703 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007704 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007705 ++pl2;
7706 ++ps2;
7707 }
7708
7709 /* 2: delete then swap, then rest must be equal */
7710 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7711 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007712 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007713
7714 /* 3: delete then substitute, then the rest must be equal */
7715 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007716 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007717
7718 /* 4: first swap then delete */
7719 if (pl[0] == ps[1] && pl[1] == ps[0])
7720 {
7721 pl2 = pl + 2; /* swap, skip two chars */
7722 ps2 = ps + 2;
7723 while (*pl2 == *ps2)
7724 {
7725 ++pl2;
7726 ++ps2;
7727 }
7728 /* delete a char and then strings must be equal */
7729 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007730 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007731 }
7732
7733 /* 5: first substitute then delete */
7734 pl2 = pl + 1; /* substitute, skip one char */
7735 ps2 = ps + 1;
7736 while (*pl2 == *ps2)
7737 {
7738 ++pl2;
7739 ++ps2;
7740 }
7741 /* delete a char and then strings must be equal */
7742 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007743 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007744
7745 /* Failed to compare. */
7746 break;
7747
7748 case 0:
7749 /*
7750 * Lenghts are equal, thus changes must result in same length: An
7751 * insert is only possible in combination with a delete.
7752 * 1: check if for identical strings
7753 */
7754 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007755 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007756
7757 /* 2: swap */
7758 if (pl[0] == ps[1] && pl[1] == ps[0])
7759 {
7760 pl2 = pl + 2; /* swap, skip two chars */
7761 ps2 = ps + 2;
7762 while (*pl2 == *ps2)
7763 {
7764 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007765 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007766 ++pl2;
7767 ++ps2;
7768 }
7769 /* 3: swap and swap again */
7770 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7771 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007772 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007773
7774 /* 4: swap and substitute */
7775 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007776 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007777 }
7778
7779 /* 5: substitute */
7780 pl2 = pl + 1;
7781 ps2 = ps + 1;
7782 while (*pl2 == *ps2)
7783 {
7784 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007785 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007786 ++pl2;
7787 ++ps2;
7788 }
7789
7790 /* 6: substitute and swap */
7791 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7792 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007793 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007794
7795 /* 7: substitute and substitute */
7796 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007797 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007798
7799 /* 8: insert then delete */
7800 pl2 = pl;
7801 ps2 = ps + 1;
7802 while (*pl2 == *ps2)
7803 {
7804 ++pl2;
7805 ++ps2;
7806 }
7807 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007808 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007809
7810 /* 9: delete then insert */
7811 pl2 = pl + 1;
7812 ps2 = ps;
7813 while (*pl2 == *ps2)
7814 {
7815 ++pl2;
7816 ++ps2;
7817 }
7818 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007819 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007820
7821 /* Failed to compare. */
7822 break;
7823 }
7824
7825 return SCORE_MAXMAX;
7826}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007827
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007828/*
7829 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007830 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007831 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007832 * The algorithm comes from Aspell editdist.cpp, edit_distance().
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007833 * It has been converted from C++ to C and modified to support multi-byte
7834 * characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007835 */
7836 static int
7837spell_edit_score(badword, goodword)
7838 char_u *badword;
7839 char_u *goodword;
7840{
7841 int *cnt;
7842 int badlen, goodlen;
7843 int j, i;
7844 int t;
7845 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007846 int pbc, pgc;
7847#ifdef FEAT_MBYTE
7848 char_u *p;
7849 int wbadword[MAXWLEN];
7850 int wgoodword[MAXWLEN];
7851
7852 if (has_mbyte)
7853 {
7854 /* Get the characters from the multi-byte strings and put them in an
7855 * int array for easy access. */
7856 for (p = badword, badlen = 0; *p != NUL; )
7857 wbadword[badlen++] = mb_ptr2char_adv(&p);
7858 ++badlen;
7859 for (p = goodword, goodlen = 0; *p != NUL; )
7860 wgoodword[goodlen++] = mb_ptr2char_adv(&p);
7861 ++goodlen;
7862 }
7863 else
7864#endif
7865 {
7866 badlen = STRLEN(badword) + 1;
7867 goodlen = STRLEN(goodword) + 1;
7868 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007869
7870 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
7871#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007872 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
7873 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007874 if (cnt == NULL)
7875 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007876
7877 CNT(0, 0) = 0;
7878 for (j = 1; j <= goodlen; ++j)
7879 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
7880
7881 for (i = 1; i <= badlen; ++i)
7882 {
7883 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
7884 for (j = 1; j <= goodlen; ++j)
7885 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007886#ifdef FEAT_MBYTE
7887 if (has_mbyte)
7888 {
7889 bc = wbadword[i - 1];
7890 gc = wgoodword[j - 1];
7891 }
7892 else
7893#endif
7894 {
7895 bc = badword[i - 1];
7896 gc = goodword[j - 1];
7897 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007898 if (bc == gc)
7899 CNT(i, j) = CNT(i - 1, j - 1);
7900 else
7901 {
7902 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007903 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007904 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
7905 else
7906 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
7907
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007908 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007909 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007910#ifdef FEAT_MBYTE
7911 if (has_mbyte)
7912 {
7913 pbc = wbadword[i - 2];
7914 pgc = wgoodword[j - 2];
7915 }
7916 else
7917#endif
7918 {
7919 pbc = badword[i - 2];
7920 pgc = goodword[j - 2];
7921 }
7922 if (bc == pgc && pbc == gc)
7923 {
7924 t = SCORE_SWAP + CNT(i - 2, j - 2);
7925 if (t < CNT(i, j))
7926 CNT(i, j) = t;
7927 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007928 }
7929 t = SCORE_DEL + CNT(i - 1, j);
7930 if (t < CNT(i, j))
7931 CNT(i, j) = t;
7932 t = SCORE_INS + CNT(i, j - 1);
7933 if (t < CNT(i, j))
7934 CNT(i, j) = t;
7935 }
7936 }
7937 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007938
7939 i = CNT(badlen - 1, goodlen - 1);
7940 vim_free(cnt);
7941 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007942}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007943
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007944/*
7945 * ":spelldump"
7946 */
7947/*ARGSUSED*/
7948 void
7949ex_spelldump(eap)
7950 exarg_T *eap;
7951{
7952 buf_T *buf = curbuf;
7953 langp_T *lp;
7954 slang_T *slang;
7955 idx_T arridx[MAXWLEN];
7956 int curi[MAXWLEN];
7957 char_u word[MAXWLEN];
7958 int c;
7959 char_u *byts;
7960 idx_T *idxs;
7961 linenr_T lnum = 0;
7962 int round;
7963 int depth;
7964 int n;
7965 int flags;
7966
7967 if (no_spell_checking())
7968 return;
7969
7970 /* Create a new empty buffer by splitting the window. */
7971 do_cmdline_cmd((char_u *)"new");
7972 if (!bufempty() || !buf_valid(buf))
7973 return;
7974
7975 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
7976 {
7977 slang = lp->lp_slang;
7978
7979 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
7980 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
7981
7982 /* round 1: case-folded tree
7983 * round 2: keep-case tree */
7984 for (round = 1; round <= 2; ++round)
7985 {
7986 if (round == 1)
7987 {
7988 byts = slang->sl_fbyts;
7989 idxs = slang->sl_fidxs;
7990 }
7991 else
7992 {
7993 byts = slang->sl_kbyts;
7994 idxs = slang->sl_kidxs;
7995 }
7996 if (byts == NULL)
7997 continue; /* array is empty */
7998
7999 depth = 0;
8000 arridx[0] = 0;
8001 curi[0] = 1;
8002 while (depth >= 0 && !got_int)
8003 {
8004 if (curi[depth] > byts[arridx[depth]])
8005 {
8006 /* Done all bytes at this node, go up one level. */
8007 --depth;
8008 line_breakcheck();
8009 }
8010 else
8011 {
8012 /* Do one more byte at this node. */
8013 n = arridx[depth] + curi[depth];
8014 ++curi[depth];
8015 c = byts[n];
8016 if (c == 0)
8017 {
8018 /* End of word, deal with the word.
8019 * Don't use keep-case words in the fold-case tree,
8020 * they will appear in the keep-case tree.
8021 * Only use the word when the region matches. */
8022 flags = (int)idxs[n];
8023 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
8024 && ((flags & WF_REGION) == 0
8025 || (((unsigned)flags >> 8)
8026 & lp->lp_region) != 0))
8027 {
8028 word[depth] = NUL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008029
8030 /* Dump the basic word if there is no prefix or
8031 * when it's the first one. */
8032 c = (unsigned)flags >> 16;
8033 if (c == 0 || curi[depth] == 2)
8034 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008035
8036 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008037 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008038 lnum = apply_prefixes(slang, word, round,
8039 flags, lnum);
8040 }
8041 }
8042 else
8043 {
8044 /* Normal char, go one level deeper. */
8045 word[depth++] = c;
8046 arridx[depth] = idxs[n];
8047 curi[depth] = 1;
8048 }
8049 }
8050 }
8051 }
8052 }
8053
8054 /* Delete the empty line that we started with. */
8055 if (curbuf->b_ml.ml_line_count > 1)
8056 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
8057
8058 redraw_later(NOT_VALID);
8059}
8060
8061/*
8062 * Dump one word: apply case modifications and append a line to the buffer.
8063 */
8064 static void
8065dump_word(word, round, flags, lnum)
8066 char_u *word;
8067 int round;
8068 int flags;
8069 linenr_T lnum;
8070{
8071 int keepcap = FALSE;
8072 char_u *p;
8073 char_u cword[MAXWLEN];
8074 char_u badword[MAXWLEN + 3];
8075
8076 if (round == 1 && (flags & WF_CAPMASK) != 0)
8077 {
8078 /* Need to fix case according to "flags". */
8079 make_case_word(word, cword, flags);
8080 p = cword;
8081 }
8082 else
8083 {
8084 p = word;
8085 if (round == 2 && (captype(word, NULL) & WF_KEEPCAP) == 0)
8086 keepcap = TRUE;
8087 }
8088
8089 /* Bad word is preceded by "/!" and some other
8090 * flags. */
8091 if ((flags & (WF_BANNED | WF_RARE)) || keepcap)
8092 {
8093 STRCPY(badword, "/");
8094 if (keepcap)
8095 STRCAT(badword, "=");
8096 if (flags & WF_BANNED)
8097 STRCAT(badword, "!");
8098 else if (flags & WF_RARE)
8099 STRCAT(badword, "?");
8100 STRCAT(badword, p);
8101 p = badword;
8102 }
8103
8104 ml_append(lnum, p, (colnr_T)0, FALSE);
8105}
8106
8107/*
8108 * Find matching prefixes for "word". Prepend each to "word" and append
8109 * a line to the buffer.
8110 * Return the updated line number.
8111 */
8112 static linenr_T
8113apply_prefixes(slang, word, round, flags, startlnum)
8114 slang_T *slang;
8115 char_u *word; /* case-folded word */
8116 int round;
8117 int flags; /* flags with prefix ID */
8118 linenr_T startlnum;
8119{
8120 idx_T arridx[MAXWLEN];
8121 int curi[MAXWLEN];
8122 char_u prefix[MAXWLEN];
8123 int c;
8124 char_u *byts;
8125 idx_T *idxs;
8126 linenr_T lnum = startlnum;
8127 int depth;
8128 int n;
8129 int len;
8130 int prefid = (unsigned)flags >> 16;
8131 int i;
8132
8133 byts = slang->sl_pbyts;
8134 idxs = slang->sl_pidxs;
8135 if (byts != NULL) /* array not is empty */
8136 {
8137 /*
8138 * Loop over all prefixes, building them byte-by-byte in prefix[].
8139 * When at the end of a prefix check that it supports "prefid".
8140 */
8141 depth = 0;
8142 arridx[0] = 0;
8143 curi[0] = 1;
8144 while (depth >= 0 && !got_int)
8145 {
8146 len = arridx[depth];
8147 if (curi[depth] > byts[len])
8148 {
8149 /* Done all bytes at this node, go up one level. */
8150 --depth;
8151 line_breakcheck();
8152 }
8153 else
8154 {
8155 /* Do one more byte at this node. */
8156 n = len + curi[depth];
8157 ++curi[depth];
8158 c = byts[n];
8159 if (c == 0)
8160 {
8161 /* End of prefix, find out how many IDs there are. */
8162 for (i = 1; i < len; ++i)
8163 if (byts[n + i] != 0)
8164 break;
8165 curi[depth] += i - 1;
8166
8167 if (valid_word_prefix(i, n, prefid, word, slang))
8168 {
8169 vim_strncpy(prefix + depth, word, MAXWLEN - depth);
8170 dump_word(prefix, round, flags, lnum++);
8171 }
8172 }
8173 else
8174 {
8175 /* Normal char, go one level deeper. */
8176 prefix[depth++] = c;
8177 arridx[depth] = idxs[n];
8178 curi[depth] = 1;
8179 }
8180 }
8181 }
8182 }
8183
8184 return lnum;
8185}
8186
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008187#endif /* FEAT_SYN_HL */