blob: cc2d4fbfd880274065aaf75a58a1acbd72dc4ef2 [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 Moolenaar63d5a1e2005-04-19 21:30:25 +000013 * The basic spell checking mechanism is:
14 * 1. Isolate a word, up to the next non-word character.
15 * 2. Find the word in the hashtable of basic words.
16 * 3. If not found, look in the hashtable with "prewords". These are prefixes
17 * with a non-word character following a word character, e.g., "de-".
18 * 4. If still not found, for each matching a prefix try if the word matches
19 * without the prefix (and with the "chop" string added back).
20 * 5. If still still not found, for each matching suffix try if the word
21 * matches without the suffix (and with the "chop" string added back).
22 *
23 * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
24 * After finding a matching word check for a leadstring (non-word characters
25 * before the word) and addstring (more text following, starting with a
26 * non-word character).
27 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000028 * Why doesn't Vim use aspell/ispell/myspell/etc.?
29 * See ":help develop-spell".
30 */
31
Bram Moolenaare19defe2005-03-21 08:23:33 +000032#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
33# include <io.h> /* for lseek(), must be before vim.h */
34#endif
35
36#include "vim.h"
37
38#if defined(FEAT_SYN_HL) || defined(PROTO)
39
40#ifdef HAVE_FCNTL_H
41# include <fcntl.h>
42#endif
43
Bram Moolenaarfc735152005-03-22 22:54:12 +000044#define MAXWLEN 100 /* assume max. word len is this many bytes */
45
Bram Moolenaare19defe2005-03-21 08:23:33 +000046/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000047 * Structure that is used to store the structures and strings from the
48 * language file. This avoids the need to allocate space for each individual
49 * word. It's allocated in big chunks for speed. It's freed all at once when
50 * 'encoding' changes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000051 */
52#define SBLOCKSIZE 4096 /* default size of sb_data */
53typedef struct sblock_S sblock_T;
54struct sblock_S
55{
56 sblock_T *sb_next; /* next block in list */
57 char_u sb_data[1]; /* data, actually longer */
58};
59
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000060/* Info from "REP" entries in ".aff" file used in af_rep.
61 * TODO: This is not used yet. Either use it or remove it. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000062typedef struct repentry_S
63{
64 char_u *re_from;
65 char_u *re_to;
66} repentry_T;
67
68/*
69 * Structure to store affix info.
70 */
71typedef struct affitem_S affitem_T;
72struct affitem_S
73{
74 affitem_T *ai_next; /* next affix with same ai_add[] or NULL */
75 short_u ai_nr; /* affix number */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000076 char_u ai_flags; /* AFF_ flags */
77 char_u ai_choplen; /* length of chop string in bytes */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000078 char_u ai_addlen; /* length of ai_add in bytes */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000079 char_u ai_leadlen; /* for AFF_PREWORD: length of lead string */
80 char_u ai_taillen; /* for AFF_PREWORD: length of tail string */
81 char_u ai_add[1]; /* Text added to basic word. This stores:
82 * 0: word for AFF_PREWORD or whole addition
83 * ai_addlen + 1: chop string
84 * + ai_choplen + 1: lead string for AFF_PREWORD
85 * + ai_leadlen + 1: trail string f. AFF_PREWORD
86 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000087};
88
89/* Get affitem_T pointer from hashitem that uses ai_add */
90static affitem_T dumai;
91#define HI2AI(hi) ((affitem_T *)((hi)->hi_key - (dumai.ai_add - (char_u *)&dumai)))
92
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000093/* ai_flags: Affix item flags */
94#define AFF_COMBINE 0x01 /* prefix combines with suffix */
95#define AFF_PREWORD 0x02 /* prefix includes word */
96
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000097/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000098 * Structure used to store words and other info for one language, loaded from
99 * a .spl file.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000100 * The main access is through hashtable "sl_word", using the case-folded
101 * word as the key. This finds a linked list of fword_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000102 */
103typedef struct slang_S slang_T;
104struct slang_S
105{
106 slang_T *sl_next; /* next language */
107 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
108 hashtab_T sl_words; /* main word table, fword_T */
109 int sl_prefcnt; /* number of prefix NRs */
110 garray_T sl_preftab; /* list of hashtables to lookup prefixes */
111 affitem_T *sl_prefzero; /* list of prefixes with zero add length */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000112 hashtab_T sl_prewords; /* prefixes that include a word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000113 int sl_suffcnt; /* number of suffix NRs */
114 garray_T sl_sufftab; /* list of hashtables to lookup suffixes */
115 affitem_T *sl_suffzero; /* list of suffixes with zero add length */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000116 char_u *sl_try; /* "TRY" from .aff file TODO: not used */
117 garray_T sl_rep; /* list of repentry_T entries from REP lines
118 * TODO not used */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000119 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
120 sblock_T *sl_block; /* list with allocated memory blocks */
121 int sl_error; /* error while loading */
122};
123
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000124/* First language that is loaded, start of the linked list of loaded
125 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000126static slang_T *first_lang = NULL;
127
128/*
129 * Structure to store an addition to a basic word.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000130 * There are many of these, keep it small!
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000131 */
132typedef struct addword_S addword_T;
133struct addword_S
134{
135 addword_T *aw_next; /* next addition */
136 char_u aw_flags; /* ADD_ flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000137 char_u aw_region; /* region for word with this addition */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000138 char_u aw_leadlen; /* byte length of lead in aw_word */
139 char_u aw_wordlen; /* byte length of first word in aw_word */
140 char_u aw_saveb; /* saved byte where aw_word[] is truncated at
141 end of hashtable key; NUL when not using
142 hashtable */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000143 char_u aw_word[1]; /* text, actually longer: case-folded addition
144 plus, with ADD_KEEPCAP: keep-case addition */
145};
146
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000147/* Get addword_T pointer from hashitem that uses aw_word */
148static addword_T dumaw;
149#define HI2ADDWORD(hi) ((addword_T *)((hi)->hi_key - (dumaw.aw_word - (char_u *)&dumaw)))
150
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000151/*
152 * Structure to store a basic word.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000153 * There are many of these, keep it small!
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000154 * The list of prefix and suffix NRs is stored after "fw_word" to avoid the
155 * need for two extra pointers.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000156 */
157typedef struct fword_S fword_T;
158struct fword_S
159{
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000160 fword_T *fw_next; /* same basic word with different caps and/or
161 * affixes */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000162 addword_T *fw_adds; /* first addword_T entry */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000163 short_u fw_flags; /* BWF_ flags */
164 char_u fw_region; /* region bits */
165 char_u fw_prefixcnt; /* number of prefix NRs */
166 char_u fw_suffixcnt; /* number of suffix NRs */
167 char_u fw_word[1]; /* actually longer:
168 * 0: case folded word or keep-case word when
169 * (flags & BWF_KEEPCAP)
170 * + word length + 1: list of prefix NRs
171 * + fw_prefixcnt [* 2]: list of suffix NRs
172 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000173};
174
175/* Get fword_T pointer from hashitem that uses fw_word */
176static fword_T dumfw;
177#define HI2FWORD(hi) ((fword_T *)((hi)->hi_key - (dumfw.fw_word - (char_u *)&dumfw)))
178
179#define REGION_ALL 0xff
180
181
182/*
183 * Structure used in "b_langp", filled from 'spelllang'.
184 */
185typedef struct langp_S
186{
187 slang_T *lp_slang; /* info for this language (NULL for last one) */
188 int lp_region; /* bitmask for region or REGION_ALL */
189} langp_T;
190
191#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
192
193#define SP_OK 0
194#define SP_BAD 1
195#define SP_RARE 2
196#define SP_LOCAL 3
197
198/* flags used for basic words in the spell file */
199#define BWF_VALID 0x01 /* word is valid without additions */
200#define BWF_REGION 0x02 /* region byte follows */
201#define BWF_ONECAP 0x04 /* first letter must be capital */
202#define BWF_SUFFIX 0x08 /* has suffix NR list */
203#define BWF_SECOND 0x10 /* second flags byte follows */
204
205#define BWF_ADDS 0x0100 /* there are additions */
206#define BWF_PREFIX 0x0200 /* has prefix NR list */
207#define BWF_ALLCAP 0x0400 /* all letters must be capital (not used
208 for single-letter words) */
209#define BWF_KEEPCAP 0x0800 /* Keep case as-is */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000210#define BWF_ADDS_M 0x1000 /* there are more than 255 additions */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000211
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000212#define BWF_ADDHASH 0x8000 /* Internal: use hashtab for additions */
213
214#define NOWC_KEY (char_u *)"x" /* hashtab key used for additions without
215 any word character */
216
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000217/* flags used for addition in the spell file */
218#define ADD_REGION 0x02 /* region byte follows */
219#define ADD_ONECAP 0x04 /* first letter must be capital */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000220#define ADD_LEADLEN 0x10 /* there is a leadlen byte */
221#define ADD_COPYLEN 0x20 /* there is a copylen byte */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000222#define ADD_ALLCAP 0x40 /* all letters must be capital (not used
223 for single-letter words) */
224#define ADD_KEEPCAP 0x80 /* fixed case */
225
226/* Translate ADD_ flags to BWF_ flags.
227 * (Needed to keep ADD_ flags in one byte.) */
228#define ADD2BWF(x) (((x) & 0x0f) | (((x) & 0xf0) << 4))
229
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000230#define VIMSPELLMAGIC "VIMspell04" /* string at start of Vim spell file */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000231#define VIMSPELLMAGICL 10
232
233/*
234 * Structure to store info for word matching.
235 */
236typedef struct matchinf_S
237{
238 langp_T *mi_lp; /* info for language and region */
239 slang_T *mi_slang; /* info for the language */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000240
241 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000242 char_u *mi_line; /* start of line containing word */
243 char_u *mi_word; /* start of word being checked */
244 char_u *mi_end; /* first non-word char after mi_word */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000245 char_u *mi_wend; /* end of matching word (is mi_end
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000246 * or further) */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000247 char_u *mi_fend; /* next char to be added to mi_fword */
248
249 /* case-folded text */
250 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
251 int mi_fendlen; /* byte length of first word in
252 mi_fword */
253 int mi_faddlen; /* byte length of text in mi_fword
254 after first word */
255 char_u *mi_cword; /* word to check, points in mi_fword */
256 char_u *mi_awend; /* after next word, to check for
257 addition (NULL when not done yet) */
258 int mi_did_awend; /* did compute mi_awend */
259
260 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000261 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
262 int mi_capflags; /* BWF_ONECAP BWF_ALLCAP BWF_KEEPCAP */
263} matchinf_T;
264
265static int word_match __ARGS((matchinf_T *mip));
266static int check_adds __ARGS((matchinf_T *mip, fword_T *fw, int req_pref, int req_suf));
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000267static void fill_awend __ARGS((matchinf_T *mip));
268static void fold_addchars __ARGS((matchinf_T *mip, int addlen));
269static int supports_affix __ARGS((int cnt, char_u *afflist, int afflistlen, int nr));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000270static int prefix_match __ARGS((matchinf_T *mip));
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000271static int noprefix_match __ARGS((matchinf_T *mip, char_u *pword, char_u *cstart, affitem_T *ai));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000272static int suffix_match __ARGS((matchinf_T *mip));
273static int match_caps __ARGS((int flags, char_u *caseword, matchinf_T *mip, char_u *cword, char_u *end));
274static slang_T *slang_alloc __ARGS((char_u *lang));
275static void slang_free __ARGS((slang_T *lp));
276static slang_T *spell_load_lang __ARGS((char_u *lang));
277static void spell_load_file __ARGS((char_u *fname, void *cookie));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000278static void *getroom __ARGS((slang_T *lp, int *bl_used, int len));
279static int find_region __ARGS((char_u *rp, char_u *region));
280static int captype __ARGS((char_u *word, char_u *end));
281
282/*
283 * Main spell-checking function.
284 * "ptr" points to the start of a word.
285 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
286 * or when it's OK it remains unchanged.
287 * This must only be called when 'spelllang' is not empty.
288 * Returns the length of the word in bytes, also when it's OK, so that the
289 * caller can skip over the word.
290 */
291 int
292spell_check(wp, line, ptr, attrp)
293 win_T *wp; /* current window */
294 char_u *line; /* start of line where "ptr" points into */
295 char_u *ptr;
296 int *attrp;
297{
298 matchinf_T mi; /* Most things are put in "mi" so that it can
299 be passed to functions quickly. */
300
301 /* Find the end of the word. We already know that *ptr is a word char. */
302 mi.mi_word = ptr;
303 mi.mi_end = ptr;
304 do
305 {
306 mb_ptr_adv(mi.mi_end);
307 } while (*mi.mi_end != NUL && spell_iswordc(mi.mi_end));
308
309 /* A word starting with a number is always OK. */
310 if (*ptr >= '0' && *ptr <= '9')
311 return (int)(mi.mi_end - ptr);
312
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000313 /* Make case-folded copy of the word. */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000314 (void)spell_casefold(ptr, mi.mi_end - ptr, mi.mi_fword, MAXWLEN + 1);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000315 mi.mi_cword = mi.mi_fword;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000316 mi.mi_fendlen = STRLEN(mi.mi_fword);
317 mi.mi_faddlen = 0;
318 mi.mi_fend = mi.mi_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000319
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000320 /* Check the caps type of the word. */
321 mi.mi_capflags = captype(ptr, mi.mi_end);
322
323 /* The word is bad unless we recognize it. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000324 mi.mi_result = SP_BAD;
325 mi.mi_wend = mi.mi_end;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000326
327 mi.mi_awend = NULL;
328 mi.mi_did_awend = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000329 mi.mi_line = line;
330
331 /*
332 * Loop over the languages specified in 'spelllang'.
333 * We check them all, because a matching word may have additions that are
334 * longer than an already found matching word.
335 */
336 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
337 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
338 {
339 /*
340 * Check for a matching word.
341 * If not found or wrong region try removing prefixes (and then
342 * suffixes).
343 * If still not found or wrong region try removing suffixes.
344 */
345 mi.mi_slang = mi.mi_lp->lp_slang;
346 if (!word_match(&mi) || mi.mi_result != SP_OK)
347 if (!prefix_match(&mi) || mi.mi_result != SP_OK)
348 suffix_match(&mi);
349 }
350
351 if (mi.mi_result != SP_OK)
352 {
353 if (mi.mi_result == SP_BAD)
354 *attrp = highlight_attr[HLF_SPB];
355 else if (mi.mi_result == SP_RARE)
356 *attrp = highlight_attr[HLF_SPR];
357 else
358 *attrp = highlight_attr[HLF_SPL];
359 }
360
361 return (int)(mi.mi_wend - ptr);
362}
363
364/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000365 * Check if the word "mip->mi_word" matches.
366 * "mip->mi_fword" is the same word case-folded;
367 *
368 * This checks the word as a whole and for prefixes that include a word.
369 *
370 * Note that when called mi_fword only contains the word up to mip->mi_end,
371 * but when checking additions it gets longer.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000372 */
373 static int
374word_match(mip)
375 matchinf_T *mip;
376{
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000377 hash_T fhash = hash_hash(mip->mi_fword);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000378 hashitem_T *hi;
379 fword_T *fw;
380 int valid = FALSE;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000381 char_u *p;
382 char_u pword[MAXWLEN + 1];
383 int charlen;
384 int capflags_save;
385 affitem_T *ai;
386 char_u *cstart;
387 int addlen;
388 int n;
389 char_u *save_end;
390 int cc;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000391
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000392 hi = hash_lookup(&mip->mi_slang->sl_words, mip->mi_fword, fhash);
393 if (!HASHITEM_EMPTY(hi))
394 {
395 /*
396 * Find a basic word for which the case of "mi_word" is correct.
397 * If it is, check additions and use the longest one.
398 */
399 for (fw = HI2FWORD(hi); fw != NULL; fw = fw->fw_next)
400 if (match_caps(fw->fw_flags, fw->fw_word, mip,
401 mip->mi_word, mip->mi_end))
402 valid |= check_adds(mip, fw, -1, -1);
403 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000404
405 /*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000406 * Try finding a matching preword for "mip->mi_word". These are
407 * prefixes that have a non-word character after a word character:
408 * "d'", "de-", "'s-", "l'de-". But not "'s".
409 * Also need to do this when a matching word was already found, because we
410 * might find a longer match this way (French: "qu" and "qu'a-t-elle").
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000411 * The check above may have added characters to mi_fword, thus we need to
412 * truncate it after the basic word for the hash lookup.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000413 */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000414 cc = mip->mi_fword[mip->mi_fendlen];
415 mip->mi_fword[mip->mi_fendlen] = NUL;
416 hi = hash_lookup(&mip->mi_slang->sl_prewords, mip->mi_fword, fhash);
417 mip->mi_fword[mip->mi_fendlen] = cc;
418 if (!HASHITEM_EMPTY(hi))
419 {
420 capflags_save = mip->mi_capflags;
421
422 /* Go through the list of matching prewords. */
423 for (ai = HI2AI(hi); ai != NULL; ai = ai->ai_next)
424 {
425 /* Check that the lead string matches before the word. */
426 p = ai->ai_add + ai->ai_addlen + ai->ai_choplen + 2;
427 if (ai->ai_leadlen > 0)
428 {
429 if (mip->mi_word - mip->mi_line < ai->ai_leadlen
430 || STRNCMP(mip->mi_word - ai->ai_leadlen, p,
431 ai->ai_leadlen) != 0)
432 continue;
433 p += ai->ai_leadlen + 1; /* advance "p" to tail */
434 }
435 else
436 ++p; /* advance "p" to tail */
437
438 /* Check that the tail string matches after the word. Need
439 * to fold case first. */
440 if (ai->ai_taillen > 0)
441 {
442 if (ai->ai_taillen >= mip->mi_faddlen)
443 {
444 fold_addchars(mip, ai->ai_taillen);
445 if (ai->ai_taillen > mip->mi_faddlen)
446 continue; /* not enough chars, can't match */
447 }
448 if (STRNCMP(mip->mi_fword + mip->mi_fendlen,
449 p, ai->ai_taillen) != 0)
450 continue;
451 }
452
453 /*
454 * This preword matches. Remove the preword and check that
455 * the resulting word exits.
456 */
457
458 /* Find the place in the original word where the tail ends,
459 * needed for case checks. */
460#ifdef FEAT_MBYTE
461 charlen = mb_charlen(p);
462#else
463 charlen = ai->ai_taillen;
464#endif
465 cstart = mip->mi_end;
466 for (n = 0; n < charlen; ++n)
467 mb_ptr_adv(cstart);
468
469 /* The new word starts with the chop. Then add up to the next
470 * non-word char. */
471 mch_memmove(pword, ai->ai_add + ai->ai_addlen + 1,
472 ai->ai_choplen);
473 p = mip->mi_fword + mip->mi_fendlen + ai->ai_taillen;
474 addlen = ai->ai_taillen;
475 while (spell_iswordc(p))
476 {
477 ++charlen;
478#ifdef FEAT_MBYTE
479 addlen += (*mb_ptr2len_check)(p);
480#else
481 ++addlen;
482#endif
483 mb_ptr_adv(p);
484 if (addlen >= mip->mi_faddlen)
485 {
486 /* Get more folded characters in mip->mi_fword. */
487 fold_addchars(mip, addlen);
488 if (addlen >= mip->mi_faddlen)
489 break; /* not enough chars, can't match */
490 }
491 }
492 mch_memmove(pword + ai->ai_choplen,
493 mip->mi_fword + mip->mi_fendlen + ai->ai_taillen,
494 addlen - ai->ai_taillen);
495 pword[ai->ai_choplen + addlen - ai->ai_taillen] = NUL;
496
497 /* Need to set mi_end to find additions. Also set mi_fendlen
498 * and mi_faddlen. */
499 save_end = mip->mi_end;
500 while (--charlen >= 0)
501 mb_ptr_adv(mip->mi_end);
502 mip->mi_fendlen += addlen;
503 mip->mi_faddlen -= addlen;
504
505 /* Find the word "pword", caseword "cstart". */
506 n = noprefix_match(mip, pword, cstart, ai);
507 mip->mi_end = save_end;
508 mip->mi_fendlen -= addlen;
509 mip->mi_faddlen += addlen;
510 if (n)
511 valid = TRUE;
512
513 /* If we found a valid word, we still need to try other
514 * suffixes, because it may have an addition that's longer. */
515 }
516
517 mip->mi_capflags = capflags_save;
518 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000519
520 return valid;
521}
522
523/*
524 * Check a matching basic word for additions.
525 * Return TRUE if we have a valid match.
526 */
527 static int
528check_adds(mip, fw, req_pref, req_suf)
529 matchinf_T *mip;
530 fword_T *fw;
531 int req_pref; /* required prefix nr, -1 if none */
532 int req_suf; /* required suffix nr, -1 if none */
533{
534 int valid = FALSE;
535 addword_T *aw;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000536 addword_T *naw = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000537 char_u *p;
538 int addlen;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000539 int cc;
540 hashitem_T *hi;
541 char_u *cp = NULL;
542 int n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000543
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000544 /* Check if required prefixes and suffixes are supported. These are on
545 * the basic word, not on each addition. */
546 if (req_pref >= 0 || req_suf >= 0)
547 {
548 /* Prefix NRs are stored just after the word in fw_word. */
549 cp = fw->fw_word + STRLEN(fw->fw_word) + 1;
550 if (req_pref >= 0 && !supports_affix(mip->mi_slang->sl_prefcnt,
551 cp, fw->fw_prefixcnt, req_pref))
552 return FALSE;
553 if (req_suf >= 0)
554 {
555 /* Suffix NRs are stored just after the Prefix NRs. */
556 if (fw->fw_prefixcnt > 0)
557 {
558 if (mip->mi_slang->sl_prefcnt > 256)
559 cp += fw->fw_prefixcnt * 2;
560 else
561 cp += fw->fw_prefixcnt;
562 }
563 if (!supports_affix(mip->mi_slang->sl_suffcnt,
564 cp, fw->fw_suffixcnt, req_suf))
565 return FALSE;
566 }
567 }
568
569 /* A word may be valid without an addition. */
570 if (fw->fw_flags & BWF_VALID)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000571 {
572 valid = TRUE;
573 if (mip->mi_result != SP_OK)
574 {
575 if ((fw->fw_region & mip->mi_lp->lp_region) == 0)
576 mip->mi_result = SP_LOCAL;
577 else
578 mip->mi_result = SP_OK;
579 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000580 /* Set word end, required when matching a word after a preword. */
581 if (mip->mi_wend < mip->mi_end)
582 mip->mi_wend = mip->mi_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000583 }
584
585 /*
586 * Check additions, both before and after the word.
587 * This may make the word longer, thus we also need to check
588 * when we already found a matching word.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000589 * When the BWF_ADDHASH flag is present then fw_adds points to a hashtable
590 * for quick lookup. Otherwise it points to the list of all possible
591 * additions.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000592 */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000593 if (fw->fw_flags & BWF_ADDHASH)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000594 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000595 /* Locate the text up to the next end-of-word. */
596 if (!mip->mi_did_awend)
597 fill_awend(mip);
598 if (mip->mi_awend == NULL)
599 return valid; /* there is no next word */
600
601 cc = *mip->mi_awend;
602 *mip->mi_awend = NUL;
603 hi = hash_find((hashtab_T *)fw->fw_adds,
604 mip->mi_fword + mip->mi_fendlen);
605 *mip->mi_awend = cc;
606 if (HASHITEM_EMPTY(hi))
607 return valid; /* no matching addition */
608 aw = HI2ADDWORD(hi);
609
610 /* Also check additions without word characters. If they are there,
611 * skip the first dummy entry. */
612 hi = hash_find((hashtab_T *)fw->fw_adds, NOWC_KEY);
613 if (!HASHITEM_EMPTY(hi))
614 naw = HI2ADDWORD(hi)->aw_next;
615 }
616 else
617 aw = fw->fw_adds;
618
619 for ( ; ; aw = aw->aw_next)
620 {
621 if (aw == NULL)
622 {
623 /* At end of list: may also try additions without word chars. */
624 if (naw == NULL)
625 break;
626 aw = naw;
627 naw = NULL;
628 }
629
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000630 if (aw->aw_leadlen > 0)
631 {
632 /* There is a leader, verify that it matches. */
633 if (aw->aw_leadlen > mip->mi_word - mip->mi_line
634 || STRNCMP(mip->mi_word - aw->aw_leadlen,
635 aw->aw_word, aw->aw_leadlen) != 0)
636 continue;
637 if (mip->mi_word - aw->aw_leadlen > mip->mi_line)
638 {
639 /* There must not be a word character just before the
640 * leader. */
641 p = mip->mi_word - aw->aw_leadlen;
642 mb_ptr_back(mip->mi_line, p);
643 if (spell_iswordc(p))
644 continue;
645 }
646 /* Leader matches. Addition is rest of "aw_word". */
647 p = aw->aw_word + aw->aw_leadlen;
648 }
649 else
650 /* No leader, use whole of "aw_word" for addition. */
651 p = aw->aw_word;
652
653 addlen = aw->aw_wordlen - aw->aw_leadlen;
654 if (addlen > 0)
655 {
656 /* Check for matching addition and no word character after it.
657 * First make sure we have enough case-folded chars to compare
658 * with. */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000659 if (addlen >= mip->mi_faddlen)
660 fold_addchars(mip, addlen);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000661
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000662 /* Put back the saved char, if needed. */
663 if (aw->aw_saveb != NUL)
664 {
665 cp = p + STRLEN(p);
666 *cp = aw->aw_saveb;
667 }
668 n = STRNCMP(mip->mi_fword + mip->mi_fendlen, p, addlen);
669 if (aw->aw_saveb != NUL)
670 *cp = NUL;
671
672 if (n != 0 || (mip->mi_fword[mip->mi_fendlen + addlen] != NUL
673 && spell_iswordc(mip->mi_fword + mip->mi_fendlen + addlen)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000674 continue;
675
676 /* Compute the length in the original word, before case folding. */
677#ifdef FEAT_MBYTE
678 if (has_mbyte)
679 {
680 int l;
681
682 p = mip->mi_end;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000683 for (l = 0; l < addlen; l += (*mb_ptr2len_check)(mip->mi_fword
684 + mip->mi_fendlen + l))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000685 mb_ptr_adv(p);
686 addlen = p - mip->mi_end;
687 }
688#endif
689
690 /* Check case of the addition. */
691 if (!match_caps(ADD2BWF(aw->aw_flags),
692 aw->aw_word + aw->aw_wordlen + 1, mip,
693 mip->mi_end, mip->mi_end + addlen))
694 continue;
695 }
696
697 /* Match! Use the new length if it's longer. */
698 if (mip->mi_wend < mip->mi_end + addlen)
699 mip->mi_wend = mip->mi_end + addlen;
700
701 valid = TRUE;
702 if (mip->mi_result != SP_OK)
703 {
704 if ((aw->aw_region & mip->mi_lp->lp_region) == 0)
705 mip->mi_result = SP_LOCAL;
706 else
707 mip->mi_result = SP_OK;
708 }
709 }
710
711 return valid;
712}
713
714/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000715 * Locate the text up to the next end-of-word after mip->mi_end.
716 */
717 static void
718fill_awend(mip)
719 matchinf_T *mip;
720{
721 char_u *p = mip->mi_end;
722 int addlen = 0;
723 int find_word = TRUE;
724
725 mip->mi_did_awend = TRUE;
726 if (mip->mi_faddlen == 0)
727 fold_addchars(mip, 0); /* need to fold first char */
728
729 /* 1: find_word == TRUE: skip over non-word characters after mi_end.
730 * 2: find_word == FALSE: skip over following word characters. */
731 for (p = mip->mi_fword + mip->mi_fendlen; *p != NUL; mb_ptr_adv(p))
732 {
733 if (spell_iswordc(p) == find_word)
734 {
735 if (!find_word)
736 break; /* done */
737 find_word = !find_word;
738 }
739#ifdef FEAT_MBYTE
740 addlen += (*mb_ptr2len_check)(p);
741#else
742 ++addlen;
743#endif
744 if (addlen >= mip->mi_faddlen)
745 fold_addchars(mip, addlen); /* need to fold more chars */
746 }
747
748 /* If there are extra chars store the result. */
749 if (addlen != 0)
750 mip->mi_awend = p;
751}
752
753/*
754 * Fold enough characters of the checked text to be able to compare with an
755 * addition of length "addlen" plus one character (to be able to check the
756 * next character to be a non-word char).
757 * When there are not enough characters (end of line) mip->mi_faddlen will be
758 * smaller than "addlen".
759 */
760 static void
761fold_addchars(mip, addlen)
762 matchinf_T *mip;
763 int addlen;
764{
765 int l;
766 char_u *p = mip->mi_fword + mip->mi_fendlen;
767
768 while (mip->mi_faddlen <= addlen)
769 {
770 if (*mip->mi_fend == NUL) /* end of the line */
771 {
772 p[mip->mi_faddlen] = NUL;
773 break;
774 }
775#ifdef FEAT_MBYTE
776 if (has_mbyte)
777 l = (*mb_ptr2len_check)(mip->mi_fend);
778 else
779#endif
780 l = 1;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000781 (void)spell_casefold(mip->mi_fend, l, p + mip->mi_faddlen,
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000782 MAXWLEN - mip->mi_fendlen - mip->mi_faddlen);
783 mip->mi_fend += l;
784 mip->mi_faddlen += STRLEN(p + mip->mi_faddlen);
785 }
786}
787
788/*
789 * Return TRUE if affix "nr" appears in affix list "afflist[afflistlen]".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000790 */
791 static int
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000792supports_affix(cnt, afflist, afflistlen, nr)
793 int cnt; /* total affix NR count */
794 char_u *afflist;
795 int afflistlen; /* affix count in "afflist" */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000796 int nr;
797{
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000798 char_u *pc = afflist;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000799 int i;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000800 int nr_msb, nr_lsb;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000801
802 if (cnt <= 256)
803 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000804 /* one byte affix numbers */
805 for (i = afflistlen; --i >= 0; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000806 if (*pc++ == nr)
807 return TRUE;
808 }
809 else
810 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000811 /* two byte affix numbers, MSB first */
812 nr_msb = (unsigned)nr >> 8;
813 nr_lsb = nr & 0xff;
814 for (i = afflistlen; --i >= 0; )
815 {
816 if (*pc++ == nr_msb && *pc == nr_lsb)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000817 return TRUE;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000818 ++pc;
819 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000820 }
821 return FALSE;
822}
823
824/*
825 * Try finding a match for "mip->mi_cword" by removing prefixes.
826 */
827 static int
828prefix_match(mip)
829 matchinf_T *mip;
830{
831 int len = 0;
832 int charlen = 0;
833 int cc;
834 affitem_T *ai;
835 char_u pword[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000836 hashtab_T *ht;
837 hashitem_T *hi;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000838 int found_valid = FALSE;
839 int cstart_charlen = 0;
840 char_u *cstart = mip->mi_word;
841 int capflags_save = mip->mi_capflags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000842
843 /*
844 * Check for prefixes with different character lengths.
845 * Start with zero length (only chop off).
846 */
847 for (charlen = 0; charlen <= mip->mi_slang->sl_preftab.ga_len; ++charlen)
848 {
849 if (charlen > 0)
850 {
851#ifdef FEAT_MBYTE
852 if (has_mbyte)
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000853 len += (*mb_ptr2len_check)(mip->mi_cword + len);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000854 else
855#endif
856 len += 1;
857 }
858 if (mip->mi_cword[len] == NUL) /* end of word, no prefix possible */
859 break;
860
861 if (charlen == 0)
862 ai = mip->mi_slang->sl_prefzero;
863 else
864 {
865 /* Get pointer to hashtab for prefix of this many chars. */
866 ht = ((hashtab_T *)mip->mi_slang->sl_preftab.ga_data) + charlen - 1;
867 if (ht->ht_used == 0)
868 continue;
869
870 cc = mip->mi_cword[len];
871 mip->mi_cword[len] = NUL;
872 hi = hash_find(ht, mip->mi_cword);
873 mip->mi_cword[len] = cc;
874
875 if (HASHITEM_EMPTY(hi))
876 ai = NULL;
877 else
878 ai = HI2AI(hi);
879 }
880
881 /* Loop over all matching prefixes. */
882 for ( ; ai != NULL; ai = ai->ai_next)
883 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000884 /* Create the basic word from the chop string and the word after
885 * the matching add string. */
886 mch_memmove(pword, ai->ai_add + ai->ai_addlen + 1, ai->ai_choplen);
887 mch_memmove(pword + ai->ai_choplen, mip->mi_cword + ai->ai_addlen,
888 mip->mi_fendlen - ai->ai_addlen);
889 pword[mip->mi_fendlen - ai->ai_addlen] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000890
891 /* Adjust the word start for case checks, we only check the
892 * part after the prefix. */
893 while (cstart_charlen < charlen)
894 {
895 mb_ptr_adv(cstart);
896 ++cstart_charlen;
897 }
898
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000899 /* Find the word "pword", caseword "cstart". */
900 found_valid |= noprefix_match(mip, pword, cstart, ai);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000901
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000902 if (found_valid && mip->mi_result == SP_OK)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000903 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000904 /* Found a valid word, no need to try other suffixes. */
905 mip->mi_capflags = capflags_save;
906 return TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000907 }
908 }
909 }
910
911 mip->mi_capflags = capflags_save;
912 return FALSE;
913}
914
915/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000916 * Check for matching word after removing a prefix.
917 * Return TRUE if found.
918 */
919 static int
920noprefix_match(mip, pword, cstart, ai)
921 matchinf_T *mip;
922 char_u *pword; /* case-folded word */
923 char_u *cstart; /* original word after removed prefix */
924 affitem_T *ai; /* the prefix item */
925{
926 hashitem_T *hi;
927 fword_T *fw;
928 int found_valid = FALSE;
929 char_u *word;
930 int i;
931 int fendlen;
932
933 /* Removing the prefix may change the caps, e.g. for
934 * "deAlf" removing "de" makes it ONECAP. */
935 mip->mi_capflags = captype(cstart, mip->mi_end);
936
937 /* Find the basic word. */
938 hi = hash_find(&mip->mi_slang->sl_words, pword);
939 if (!HASHITEM_EMPTY(hi))
940 {
941 /* Check if the word supports this prefix. */
942 for (fw = HI2FWORD(hi); fw != NULL; fw = fw->fw_next)
943 if (match_caps(fw->fw_flags, fw->fw_word, mip,
944 cstart, mip->mi_end))
945 found_valid |= check_adds(mip, fw, ai->ai_nr, -1);
946
947 if (found_valid && mip->mi_result == SP_OK)
948 /* Found a valid word, no need to try other suffixes. */
949 return TRUE;
950 }
951
952 /* No matching basic word without prefix. When combining is
953 * allowed try with suffixes. */
954 if (ai->ai_flags & AFF_COMBINE)
955 {
956 /* Pass the word with prefix removed to suffix_match(). */
957 mip->mi_cword = pword;
958 word = mip->mi_word;
959 mip->mi_word = cstart;
960 fendlen = mip->mi_fendlen;
961 mip->mi_fendlen = STRLEN(pword);
962 i = suffix_match(mip);
963 mip->mi_cword = mip->mi_fword;
964 mip->mi_word = word;
965 mip->mi_fendlen = fendlen;
966 if (i)
967 return TRUE;
968 }
969
970 return FALSE;
971}
972
973/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000974 * Try finding a match for "mip->mi_cword" by removing suffixes.
975 */
976 static int
977suffix_match(mip)
978 matchinf_T *mip;
979{
980 char_u *sufp;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000981 char_u *endw = mip->mi_cword + mip->mi_fendlen;
982 int endw_c = *endw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000983 int charlen;
984 affitem_T *ai;
985 char_u pword[MAXWLEN + 1];
986 fword_T *fw;
987 hashtab_T *ht;
988 hashitem_T *hi;
989 int tlen;
990 int cend_charlen = 0;
991 char_u *cend = mip->mi_end;
992 int found_valid = FALSE;
993 int capflags_save = mip->mi_capflags;
994
995 /*
996 * Try suffixes of different length, starting with an empty suffix (chop
997 * only, thus adds something).
998 * Stop checking if there are no suffixes with so many characters.
999 */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001000 sufp = endw;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001001 *endw = NUL; /* truncate after possible suffix */
1002
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001003 for (charlen = 0; charlen <= mip->mi_slang->sl_sufftab.ga_len; ++charlen)
1004 {
1005 /* Move the pointer to the possible suffix back one character, unless
1006 * doing the first round (empty suffix). */
1007 if (charlen > 0)
1008 {
1009 mb_ptr_back(mip->mi_cword, sufp);
1010 if (sufp <= mip->mi_cword) /* start of word, no suffix possible */
1011 break;
1012 }
1013
1014 if (charlen == 0)
1015 ai = mip->mi_slang->sl_suffzero;
1016 else
1017 {
1018 /* Get pointer to hashtab for suffix of this many chars. */
1019 ht = ((hashtab_T *)mip->mi_slang->sl_sufftab.ga_data) + charlen - 1;
1020 if (ht->ht_used == 0)
1021 continue;
1022
1023 hi = hash_find(ht, sufp);
1024 if (HASHITEM_EMPTY(hi))
1025 ai = NULL;
1026 else
1027 ai = HI2AI(hi);
1028 }
1029
1030 if (ai != NULL)
1031 {
1032 /* Found a list of matching suffixes. Now check that there is one
1033 * we can use. */
1034 tlen = sufp - mip->mi_cword; /* length of word without suffix */
1035 mch_memmove(pword, mip->mi_cword, tlen);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001036 *endw = endw_c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001037
1038 for ( ; ai != NULL; ai = ai->ai_next)
1039 {
1040 /* Found a matching suffix. Create the basic word by removing
1041 * the suffix and adding the chop string. */
1042 if (ai->ai_choplen == 0)
1043 pword[tlen] = NUL;
1044 else
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001045 mch_memmove(pword + tlen, ai->ai_add + ai->ai_addlen + 1,
1046 ai->ai_choplen + 1);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001047
1048 /* Find the basic word. */
1049 hi = hash_find(&mip->mi_slang->sl_words, pword);
1050 if (!HASHITEM_EMPTY(hi))
1051 {
1052 /* Adjust the end for case checks, we only check the part
1053 * before the suffix. */
1054 while (cend_charlen < charlen)
1055 {
1056 mb_ptr_back(mip->mi_word, cend);
1057 ++cend_charlen;
1058 }
1059
1060 /* Removing the suffix may change the caps, e.g. for
1061 * "UFOs" removing 's' makes it ALLCAP. */
1062 mip->mi_capflags = captype(mip->mi_word, cend);
1063
1064 /* Check if the word supports this suffix. */
1065 for (fw = HI2FWORD(hi); fw != NULL; fw = fw->fw_next)
1066 if (match_caps(fw->fw_flags, fw->fw_word, mip,
1067 mip->mi_word, cend))
1068 found_valid |= check_adds(mip, fw, -1, ai->ai_nr);
1069
1070 if (found_valid && mip->mi_result == SP_OK)
1071 {
1072 /* Found a valid word, no need to try other suffixes. */
1073 mip->mi_capflags = capflags_save;
1074 return TRUE;
1075 }
1076 }
1077 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001078
1079 *endw = NUL; /* truncate after possible suffix */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001080 }
1081 }
1082
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001083 *endw = endw_c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001084 mip->mi_capflags = capflags_save;
1085 return FALSE;
1086}
1087
1088/*
1089 * Return TRUE if case of "cword" meets the requirements of case flags
1090 * "flags".
1091 */
1092 static int
1093match_caps(flags, caseword, mip, cword, end)
1094 int flags; /* flags required by basic word or addition */
1095 char_u *caseword; /* word with case as required */
1096 matchinf_T *mip;
1097 char_u *cword; /* word to compare against "caseword" */
1098 char_u *end; /* end of "cword" */
1099{
1100 char_u *p;
1101 int c;
1102 int len;
1103 int capflags = mip->mi_capflags; /* flags of checked word */
1104 int past_second;
1105
1106 if ((capflags & BWF_KEEPCAP) == 0 && end > mip->mi_end)
1107 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001108 /* If "end" is past "mip->mi_end" we need to adjust the caps type for
1109 * characters after the basic word. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001110#ifdef FEAT_MBYTE
1111 past_second = (mip->mi_word + (*mb_ptr2len_check)(mip->mi_word)
1112 < mip->mi_end);
1113#else
1114 past_second = mip->mi_word + 1 < mip->mi_end;
1115#endif
1116 for (p = mip->mi_end; p < end; )
1117 {
1118 if (!spell_iswordc(p))
1119 mb_ptr_adv(p);
1120 else
1121 {
1122#ifdef FEAT_MBYTE
1123 if (has_mbyte)
1124 c = mb_ptr2char_adv(&p);
1125 else
1126#endif
1127 c = *p++;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001128 if (spell_isupper(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001129 {
1130 if (capflags == 0 || (capflags & BWF_ONECAP))
1131 {
1132 capflags = BWF_KEEPCAP; /* lU or UlU */
1133 break;
1134 }
1135 }
1136 else
1137 {
1138 if (capflags & BWF_ALLCAP)
1139 {
1140 if (past_second)
1141 {
1142 capflags = BWF_KEEPCAP; /* UUl */
1143 break;
1144 }
1145 capflags = BWF_ONECAP; /* Uu */
1146 }
1147 }
1148 past_second = TRUE;
1149 }
1150 }
1151 }
1152
1153 if (capflags == BWF_ALLCAP)
1154 return TRUE; /* All caps is always OK. */
1155
1156 if (flags & BWF_KEEPCAP)
1157 {
1158 len = STRLEN(caseword);
1159 return (len == end - cword && STRNCMP(caseword, cword, len) == 0);
1160 }
1161
1162 if (flags & BWF_ALLCAP)
1163 return FALSE; /* need ALLCAP, already checked above */
1164
1165 if (flags & BWF_ONECAP)
1166 return capflags == BWF_ONECAP;
1167
1168 return capflags != BWF_KEEPCAP; /* no case check, only KEEPCAP is bad */
1169}
1170
1171/*
1172 * Move to next spell error.
1173 * Return OK if found, FAIL otherwise.
1174 */
1175 int
1176spell_move_to(dir, allwords)
1177 int dir; /* FORWARD or BACKWARD */
1178 int allwords; /* TRUE for "[s" and "]s" */
1179{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001180 linenr_T lnum;
1181 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001182 char_u *line;
1183 char_u *p;
1184 int wc;
1185 int nwc;
1186 int attr = 0;
1187 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001188 int has_syntax = syntax_present(curbuf);
1189 int col;
1190 int can_spell;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001191
1192 if (!curwin->w_p_spell || *curwin->w_buffer->b_p_spl == NUL)
1193 {
1194 EMSG(_("E756: Spell checking not enabled"));
1195 return FAIL;
1196 }
1197
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001198 /*
1199 * Start looking for bad word at the start of the line, because we can't
1200 * start halfway a word, we don't know where it starts or ends.
1201 *
1202 * When searching backwards, we continue in the line to find the last
1203 * bad word (in the cursor line: before the cursor).
1204 */
1205 lnum = curwin->w_cursor.lnum;
1206 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001207
1208 while (!got_int)
1209 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001210 line = ml_get(lnum);
1211 p = line;
1212 wc = FALSE;
1213
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001214 while (*p != NUL)
1215 {
1216 nwc = spell_iswordc(p);
1217 if (!wc && nwc)
1218 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001219 /* When searching backward don't search after the cursor. */
1220 if (dir == BACKWARD
1221 && lnum == curwin->w_cursor.lnum
1222 && (colnr_T)(p - line) >= curwin->w_cursor.col)
1223 break;
1224
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001225 /* start of word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001226 len = spell_check(curwin, line, p, &attr);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001227
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001228 if (attr != 0)
1229 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001230 /* We found a bad word. Check the attribute. */
1231 /* TODO: check for syntax @Spell cluster. */
1232 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001233 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001234 /* When searching forward only accept a bad word after
1235 * the cursor. */
1236 if (dir == BACKWARD
1237 || lnum > curwin->w_cursor.lnum
1238 || (lnum == curwin->w_cursor.lnum
1239 && (colnr_T)(p - line)
1240 > curwin->w_cursor.col))
1241 {
1242 if (has_syntax)
1243 {
1244 col = p - line;
1245 (void)syn_get_id(lnum, (colnr_T)col,
1246 FALSE, &can_spell);
1247
1248 /* have to get the line again, a multi-line
1249 * regexp may make it invalid */
1250 line = ml_get(lnum);
1251 p = line + col;
1252 }
1253 else
1254 can_spell = TRUE;
1255
1256 if (can_spell)
1257 {
1258 found_pos.lnum = lnum;
1259 found_pos.col = p - line;
1260#ifdef FEAT_VIRTUALEDIT
1261 found_pos.coladd = 0;
1262#endif
1263 if (dir == FORWARD)
1264 {
1265 /* No need to search further. */
1266 curwin->w_cursor = found_pos;
1267 return OK;
1268 }
1269 }
1270 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001271 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001272 attr = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001273 }
1274 p += len;
1275 if (*p == NUL)
1276 break;
1277 nwc = FALSE;
1278 }
1279
1280 /* advance to next character */
1281 mb_ptr_adv(p);
1282 wc = nwc;
1283 }
1284
1285 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001286 if (dir == BACKWARD)
1287 {
1288 if (found_pos.lnum != 0)
1289 {
1290 /* Use the last match in the line. */
1291 curwin->w_cursor = found_pos;
1292 return OK;
1293 }
1294 if (lnum == 1)
1295 return FAIL;
1296 --lnum;
1297 }
1298 else
1299 {
1300 if (lnum == curbuf->b_ml.ml_line_count)
1301 return FAIL;
1302 ++lnum;
1303 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001304
1305 line_breakcheck();
1306 }
1307
1308 return FAIL; /* interrupted */
1309}
1310
1311/*
1312 * Load word list for "lang" from a Vim spell file.
1313 * "lang" must be the language without the region: "en" or "en-rare".
1314 */
1315 static slang_T *
1316spell_load_lang(lang)
1317 char_u *lang;
1318{
1319 slang_T *lp;
1320 char_u fname_enc[80];
1321 char_u *p;
1322 int r;
1323
1324 lp = slang_alloc(lang);
1325 if (lp != NULL)
1326 {
1327 /* Find all spell files for "lang" in 'runtimepath' and load them.
1328 * Use 'encoding', except that we use "latin1" for "latin9". */
1329#ifdef FEAT_MBYTE
1330 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1331 p = p_enc;
1332 else
1333#endif
1334 p = (char_u *)"latin1";
1335 sprintf((char *)fname_enc, "spell/%s.%s.spl", lang, p);
1336
1337 r = do_in_runtimepath(fname_enc, TRUE, spell_load_file, lp);
Bram Moolenaar5482f332005-04-17 20:18:43 +00001338 if (r == FAIL && !lp->sl_error)
1339 {
1340 /* Try loading the ASCII version. */
1341 sprintf((char *)fname_enc, "spell/%s.ascii.spl", lang);
1342
1343 r = do_in_runtimepath(fname_enc, TRUE, spell_load_file, lp);
1344 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001345 if (r == FAIL || lp->sl_error)
1346 {
1347 slang_free(lp);
1348 lp = NULL;
1349 if (r == FAIL)
1350 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
1351 fname_enc + 6);
1352 }
1353 else
1354 {
1355 lp->sl_next = first_lang;
1356 first_lang = lp;
1357 }
1358 }
1359
1360 return lp;
1361}
1362
1363/*
1364 * Allocate a new slang_T.
1365 * Caller must fill "sl_next".
1366 */
1367 static slang_T *
1368slang_alloc(lang)
1369 char_u *lang;
1370{
1371 slang_T *lp;
1372
1373 lp = (slang_T *)alloc(sizeof(slang_T));
1374 if (lp != NULL)
1375 {
1376 lp->sl_name = vim_strsave(lang);
1377 hash_init(&lp->sl_words);
1378 ga_init2(&lp->sl_preftab, sizeof(hashtab_T), 4);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001379 hash_init(&lp->sl_prewords);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001380 ga_init2(&lp->sl_sufftab, sizeof(hashtab_T), 4);
1381 lp->sl_prefzero = NULL;
1382 lp->sl_suffzero = NULL;
1383 lp->sl_try = NULL;
1384 ga_init2(&lp->sl_rep, sizeof(repentry_T), 4);
1385 lp->sl_regions[0] = NUL;
1386 lp->sl_block = NULL;
1387 lp->sl_error = FALSE;
1388 }
1389 return lp;
1390}
1391
1392/*
1393 * Free the contents of an slang_T and the structure itself.
1394 */
1395 static void
1396slang_free(lp)
1397 slang_T *lp;
1398{
1399 sblock_T *sp;
1400 int i;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001401 fword_T *fw;
1402 int todo;
1403 hashitem_T *hi;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001404
1405 vim_free(lp->sl_name);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001406
1407 /* The words themselves are in memory blocks referenced by "sl_block".
1408 * Only the hashtables for additions need to be cleared. */
1409 todo = lp->sl_words.ht_used;
1410 for (hi = lp->sl_words.ht_array; todo > 0; ++hi)
1411 {
1412 if (!HASHITEM_EMPTY(hi))
1413 {
1414 --todo;
1415 fw = HI2FWORD(hi);
1416 if (fw->fw_flags & BWF_ADDHASH)
1417 hash_clear((hashtab_T *)fw->fw_adds);
1418 }
1419 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001420 hash_clear(&lp->sl_words);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001421
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001422 for (i = 0; i < lp->sl_preftab.ga_len; ++i)
1423 hash_clear(((hashtab_T *)lp->sl_preftab.ga_data) + i);
1424 ga_clear(&lp->sl_preftab);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001425 hash_clear(&lp->sl_prewords);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001426 for (i = 0; i < lp->sl_sufftab.ga_len; ++i)
1427 hash_clear(((hashtab_T *)lp->sl_sufftab.ga_data) + i);
1428 ga_clear(&lp->sl_sufftab);
1429 ga_clear(&lp->sl_rep);
1430 vim_free(lp->sl_try);
1431 while (lp->sl_block != NULL)
1432 {
1433 sp = lp->sl_block;
1434 lp->sl_block = sp->sb_next;
1435 vim_free(sp);
1436 }
1437 vim_free(lp);
1438}
1439
1440/*
1441 * Load one spell file into an slang_T.
1442 * Invoked through do_in_runtimepath().
1443 */
1444 static void
1445spell_load_file(fname, cookie)
1446 char_u *fname;
1447 void *cookie; /* points to the slang_T to be filled */
1448{
1449 slang_T *lp = cookie;
1450 FILE *fd;
1451 char_u buf[MAXWLEN + 1];
1452 char_u cbuf[MAXWLEN + 1];
1453 char_u fbuf[MAXWLEN + 1];
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001454 char_u affixbuf[256 * 2 * 2]; /* max 2 * 256 affix nrs of 2 bytes */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001455 char_u *p;
1456 int itm;
1457 int i;
1458 int affcount;
1459 int affnr;
1460 int affflags;
1461 int affitemcnt;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001462 int prefixcnt, suffixcnt;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001463 int bl_used = SBLOCKSIZE;
1464 int widx;
Bram Moolenaar5482f332005-04-17 20:18:43 +00001465 int prefm = 0; /* 1 if <= 256 prefixes, sizeof(short_u) otherw. */
1466 int suffm = 0; /* 1 if <= 256 suffixes, sizeof(short_u) otherw. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001467 int wlen;
1468 int flags;
1469 affitem_T *ai, *ai2, **aip;
1470 int round;
1471 char_u *save_sourcing_name = sourcing_name;
1472 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001473 int cnt, ccnt;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001474 int choplen;
1475 int addlen;
1476 int leadlen;
1477 int wordcount;
1478 fword_T *fw, *fw2;
1479 garray_T *gap;
1480 hashtab_T *ht;
1481 hashitem_T *hi;
1482 hash_T hash;
1483 int adds;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001484 addword_T *aw, *naw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001485 int flen;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001486 int xlen;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001487 char_u *fol;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001488
1489 fd = fopen((char *)fname, "r");
1490 if (fd == NULL)
1491 {
1492 EMSG2(_(e_notopen), fname);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001493 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001494 }
1495
1496 /* Set sourcing_name, so that error messages mention the file name. */
1497 sourcing_name = fname;
1498 sourcing_lnum = 0;
1499
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001500 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
1501 * <charflagslen> <charflags> <fcharslen> <fchars> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001502 for (i = 0; i < VIMSPELLMAGICL; ++i)
1503 buf[i] = getc(fd); /* <fileID> */
1504 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
1505 {
1506 EMSG(_("E757: Wrong file ID in spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001507 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001508 }
1509
1510 cnt = getc(fd); /* <regioncnt> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001511 if (cnt < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001512 {
1513truncerr:
1514 EMSG(_("E758: Truncated spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001515 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001516 }
1517 if (cnt > 8)
1518 {
1519formerr:
1520 EMSG(_("E759: Format error in spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001521 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001522 }
1523 for (i = 0; i < cnt; ++i)
1524 {
1525 lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
1526 lp->sl_regions[i * 2 + 1] = getc(fd);
1527 }
1528 lp->sl_regions[cnt * 2] = NUL;
1529
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001530 cnt = getc(fd); /* <charflagslen> */
1531 if (cnt > 0)
1532 {
1533 p = (char_u *)getroom(lp, &bl_used, cnt);
1534 if (p == NULL)
1535 goto endFAIL;
1536 for (i = 0; i < cnt; ++i)
1537 p[i] = getc(fd); /* <charflags> */
1538
1539 ccnt = (getc(fd) << 8) + getc(fd); /* <fcharslen> */
1540 if (ccnt <= 0)
1541 goto formerr;
1542 fol = (char_u *)getroom(lp, &bl_used, ccnt + 1);
1543 if (fol == NULL)
1544 goto endFAIL;
1545 for (i = 0; i < ccnt; ++i)
1546 fol[i] = getc(fd); /* <fchars> */
1547 fol[i] = NUL;
1548
1549 /* Set the word-char flags and fill spell_isupper() table. */
1550 if (set_spell_charflags(p, cnt, fol) == FAIL)
1551 goto formerr;
1552 }
1553 else
1554 {
1555 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
1556 cnt = (getc(fd) << 8) + getc(fd);
1557 if (cnt != 0)
1558 goto formerr;
1559 }
1560
1561 /* round 1: <PREFIXLIST>: <affcount> <affix> ...
1562 * round 2: <SUFFIXLIST>: <affcount> <affix> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001563 for (round = 1; round <= 2; ++round)
1564 {
1565 affcount = (getc(fd) << 8) + getc(fd); /* <affcount> */
1566 if (affcount < 0)
1567 goto truncerr;
1568 if (round == 1)
1569 {
1570 gap = &lp->sl_preftab;
1571 aip = &lp->sl_prefzero;
1572 lp->sl_prefcnt = affcount;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001573 prefm = affcount > 256 ? 2 : 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001574 }
1575 else
1576 {
1577 gap = &lp->sl_sufftab;
1578 aip = &lp->sl_suffzero;
1579 lp->sl_suffcnt = affcount;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001580 suffm = affcount > 256 ? 2 : 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001581 }
1582
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001583 /*
1584 * For each affix NR there can be several affixes.
1585 */
1586 for (affnr = 0; affnr < affcount; ++affnr)
1587 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001588 /* <affix>: <affitemcnt> <affitem> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001589 affitemcnt = (getc(fd) << 8) + getc(fd); /* <affitemcnt> */
1590 if (affitemcnt < 0)
1591 goto truncerr;
1592 for (itm = 0; itm < affitemcnt; ++itm)
1593 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001594 /* <affitem>: <affflags> <affchoplen> <affchop>
1595 * <affaddlen> <affadd> */
1596 affflags = getc(fd); /* <affflags> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001597 choplen = getc(fd); /* <affchoplen> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001598 if (choplen < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001599 goto truncerr;
1600 if (choplen >= MAXWLEN)
1601 goto formerr;
1602 for (i = 0; i < choplen; ++i) /* <affchop> */
1603 buf[i] = getc(fd);
1604 buf[i] = NUL;
1605 addlen = getc(fd); /* <affaddlen> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001606 if (addlen < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001607 goto truncerr;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001608 if (affflags & AFF_PREWORD)
1609 xlen = addlen + 2; /* space for lead and trail string */
1610 else
1611 xlen = 0;
1612
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001613 /* Get room to store the affitem_T, chop and add strings. */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001614 ai = (affitem_T *)getroom(lp, &bl_used,
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001615 sizeof(affitem_T) + addlen + choplen + 1 + xlen);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001616 if (ai == NULL)
1617 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001618
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001619 ai->ai_nr = affnr;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001620 ai->ai_flags = affflags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001621 ai->ai_choplen = choplen;
1622 ai->ai_addlen = addlen;
1623
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001624 /* Chop string is at ai_add[ai_addlen + 1]. */
1625 p = ai->ai_add + addlen + 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001626 STRCPY(p, buf);
1627
1628 p = ai->ai_add;
1629 for (i = 0; i < addlen; ++i) /* <affadd> */
1630 p[i] = getc(fd);
1631 p[i] = NUL;
1632
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001633 if (affflags & AFF_PREWORD)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001634 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001635 int l, leadoff, trailoff;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001636
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001637 /*
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001638 * A preword is a prefix that's recognized as a word: it
1639 * contains a word characters folled by a non-word
1640 * character.
1641 * <affadd> is the whole prefix. Separate lead and trail
1642 * string, put the word itself at ai_add, so that it can
1643 * be used as hashtable key.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001644 */
1645 /* lead string: up to first word char */
1646 while (*p != NUL && !spell_iswordc(p))
1647 mb_ptr_adv(p);
1648 ai->ai_leadlen = p - ai->ai_add;
1649 leadoff = addlen + choplen + 2;
1650 mch_memmove(ai->ai_add + leadoff, ai->ai_add,
1651 ai->ai_leadlen);
1652 ai->ai_add[leadoff + ai->ai_leadlen] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001653
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001654 /* trail string: after last word char */
1655 while (*p != NUL && spell_iswordc(p))
1656 mb_ptr_adv(p);
1657 trailoff = leadoff + ai->ai_leadlen + 1;
1658 STRCPY(ai->ai_add + trailoff, p);
1659 ai->ai_taillen = STRLEN(p);
1660
1661 /* word itself */
1662 l = (p - ai->ai_add) - ai->ai_leadlen;
1663 mch_memmove(ai->ai_add, ai->ai_add + ai->ai_leadlen, l);
1664 ai->ai_add[l] = NUL;
1665 hash = hash_hash(ai->ai_add);
1666 hi = hash_lookup(&lp->sl_prewords, ai->ai_add, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001667 if (HASHITEM_EMPTY(hi))
1668 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001669 /* First preword with this word, add to hashtable. */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001670 hash_add_item(&lp->sl_prewords, hi, ai->ai_add, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001671 ai->ai_next = NULL;
1672 }
1673 else
1674 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001675 /* There already is a preword with this word, link in
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001676 * the list. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001677 ai2 = HI2AI(hi);
1678 ai->ai_next = ai2->ai_next;
1679 ai2->ai_next = ai;
1680 }
1681 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001682 else
1683 {
1684 /*
1685 * Add the affix to a hashtable. Which one depends on the
1686 * length of the added string in characters.
1687 */
1688#ifdef FEAT_MBYTE
1689 /* Change "addlen" from length in bytes to length in
1690 * chars. */
1691 if (has_mbyte)
1692 addlen = mb_charlen(p);
1693#endif
1694 if (addlen == 0)
1695 {
1696 /* Link in list of zero length affixes. */
1697 ai->ai_next = *aip;
1698 *aip = ai;
1699 }
1700 else
1701 {
1702 if (gap->ga_len < addlen)
1703 {
1704 /* Longer affix, need more hashtables. */
1705 if (ga_grow(gap, addlen - gap->ga_len) == FAIL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001706 goto endFAIL;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001707
1708 /* Re-allocating ga_data means that an ht_array
1709 * pointing to ht_smallarray becomes invalid. We
1710 * can recognize this: ht_mask is at its init
1711 * value. */
1712 for (i = 0; i < gap->ga_len; ++i)
1713 {
1714 ht = ((hashtab_T *)gap->ga_data) + i;
1715 if (ht->ht_mask == HT_INIT_SIZE - 1)
1716 ht->ht_array = ht->ht_smallarray;
1717 }
1718
1719 /* Init the newly used hashtable(s). */
1720 while (gap->ga_len < addlen)
1721 {
1722 hash_init(((hashtab_T *)gap->ga_data)
1723 + gap->ga_len);
1724 ++gap->ga_len;
1725 }
1726 }
1727 ht = ((hashtab_T *)gap->ga_data) + addlen - 1;
1728 hash = hash_hash(p);
1729 hi = hash_lookup(ht, p, hash);
1730 if (HASHITEM_EMPTY(hi))
1731 {
1732 /* First affix with this "ai_add", add to
1733 * hashtable. */
1734 hash_add_item(ht, hi, p, hash);
1735 ai->ai_next = NULL;
1736 }
1737 else
1738 {
1739 /* There already is an affix with this "ai_add",
1740 * link in the list. */
1741 ai2 = HI2AI(hi);
1742 ai->ai_next = ai2->ai_next;
1743 ai2->ai_next = ai;
1744 }
1745 }
1746 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001747 }
1748 }
1749 }
1750
1751 /* <SUGGEST> : <suggestlen> <more> ... */
1752 /* TODO, just skip this for now */
1753 i = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
1754 while (i-- > 0)
1755 if (getc(fd) == EOF) /* <suggestlen> */
1756 goto truncerr;
1757
1758 /* <WORDLIST>: <wordcount> <worditem> ... */ /* <wordcount> */
1759 wordcount = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8)
1760 + getc(fd);
1761 if (wordcount < 0)
1762 goto truncerr;
1763
1764 /* Init hashtable for this number of words, so that it doesn't need to
1765 * reallocate the table halfway. */
1766 hash_lock_size(&lp->sl_words, wordcount);
1767
1768 for (widx = 0; ; ++widx)
1769 {
1770 /* <worditem>: <nr> <string> <flags> [<flags2>]
1771 * [<caselen> <caseword>]
1772 * [<affixcnt> <affixNR> ...] (prefixes)
1773 * [<affixcnt> <affixNR> ...] (suffixes)
1774 * [<region>]
1775 * [<addcnt> <add> ...]
1776 */
1777 /* Use <nr> bytes from the previous word. */
1778 wlen = getc(fd); /* <nr> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001779 if (wlen < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001780 {
1781 if (widx >= wordcount) /* normal way to end the file */
1782 break;
1783 goto truncerr;
1784 }
1785
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001786 /* Read further word bytes until one below 0x20, that one must be the
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001787 * flags. Keep this fast! */
1788 for (;;)
1789 {
1790 if ((buf[wlen] = getc(fd)) < 0x20) /* <string> */
1791 break;
1792 if (++wlen == MAXWLEN)
1793 goto formerr;
1794 }
1795 flags = buf[wlen]; /* <flags> */
1796 buf[wlen] = NUL;
1797
1798 /* Get more flags if they're there. */
1799 if (flags & BWF_SECOND)
1800 flags += getc(fd) << 8; /* <flags2> */
1801
1802 if (flags & BWF_KEEPCAP)
1803 {
1804 /* Read <caselen> and <caseword> first, its length may differ from
1805 * the case-folded word. Note: this should only happen after the
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001806 * basic word without KEEPCAP! */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001807 wlen = getc(fd);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001808 if (wlen < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001809 goto truncerr;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001810 if (wlen >= MAXWLEN)
1811 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001812 for (i = 0; i < wlen; ++i)
1813 cbuf[i] = getc(fd);
1814 cbuf[i] = NUL;
1815 }
1816
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001817 /* Optional prefixes */
1818 p = affixbuf;
1819 if (flags & BWF_PREFIX)
1820 {
1821 cnt = getc(fd); /* <affixcnt> */
1822 if (cnt < 0)
1823 goto truncerr;
1824 prefixcnt = cnt;
1825 for (i = cnt * prefm; --i >= 0; ) /* <affixNR> */
1826 *p++ = getc(fd);
1827 }
1828 else
1829 prefixcnt = 0;
1830
1831 /* Optional suffixes */
1832 if (flags & BWF_SUFFIX)
1833 {
1834 cnt = getc(fd); /* <affixcnt> */
1835 if (cnt < 0)
1836 goto truncerr;
1837 suffixcnt = cnt;
1838 for (i = cnt * suffm; --i >= 0; ) /* <affixNR> */
1839 *p++ = getc(fd);
1840 }
1841 else
1842 suffixcnt = 0;
1843
1844 /* Find room to store the word in an fword_T. */
1845 fw = (fword_T *)getroom(lp, &bl_used, (int)sizeof(fword_T) + wlen
1846 + (p - affixbuf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001847 if (fw == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001848 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001849 mch_memmove(fw->fw_word, (flags & BWF_KEEPCAP) ? cbuf : buf, wlen + 1);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001850
1851 /* Put the affix NRs just after the word, if any. */
1852 if (p > affixbuf)
1853 mch_memmove(fw->fw_word + wlen + 1, affixbuf, p - affixbuf);
1854
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001855 fw->fw_flags = flags;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001856 fw->fw_prefixcnt = prefixcnt;
1857 fw->fw_suffixcnt = suffixcnt;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001858
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001859 /* We store the word in the hashtable case-folded. For a KEEPCAP word
1860 * the entry must already exist, because fw_word can't be used as the
1861 * key, it differs from "buf"! */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001862 hash = hash_hash(buf);
1863 hi = hash_lookup(&lp->sl_words, buf, hash);
1864 if (HASHITEM_EMPTY(hi))
1865 {
1866 if (hash_add_item(&lp->sl_words, hi, fw->fw_word, hash) == FAIL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001867 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001868 fw->fw_next = NULL;
1869 }
1870 else
1871 {
1872 /* Already have this basic word in the hashtable, this one will
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001873 * have different case flags and/or affixes. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001874 fw2 = HI2FWORD(hi);
1875 fw->fw_next = fw2->fw_next;
1876 fw2->fw_next = fw;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001877 --widx; /* don't count this one as a basic word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001878 }
1879
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001880 if (flags & BWF_REGION)
1881 fw->fw_region = getc(fd); /* <region> */
1882 else
1883 fw->fw_region = REGION_ALL;
1884
1885 fw->fw_adds = NULL;
1886 if (flags & BWF_ADDS)
1887 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001888 if (flags & BWF_ADDS_M)
1889 adds = (getc(fd) << 8) + getc(fd); /* <addcnt> */
1890 else
1891 adds = getc(fd); /* <addcnt> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001892 if (adds < 0)
1893 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001894
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001895 if (adds > 30)
1896 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001897 /* Use a hashtable to lookup the part until the next word end.
1898 * Thus for "de-bur-die" "de" is the basic word, "-bur" is key
1899 * in the addition hashtable, "-bur<NUL>die" the whole
1900 * addition and "aw_saveb" is '-'.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001901 * This uses more memory and involves some overhead, thus only
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001902 * do it when there are many additions (e.g., for French). */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001903 ht = (hashtab_T *)getroom(lp, &bl_used, sizeof(hashtab_T));
1904 if (ht == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001905 goto endFAIL;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001906 hash_init(ht);
1907 fw->fw_adds = (addword_T *)ht;
1908 fw->fw_flags |= BWF_ADDHASH;
1909
1910 /* Preset the size of the hashtable. It's never unlocked. */
1911 hash_lock_size(ht, adds + 1);
1912 }
1913 else
1914 ht = NULL;
1915
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001916 /*
1917 * Note: uses cbuf[] to copy bytes from previous addition.
1918 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001919 while (--adds >= 0)
1920 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001921 /* <add>: <addflags> <addlen> [<leadlen>] [<copylen>]
1922 * [<addstring>] [<region>] */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001923 flags = getc(fd); /* <addflags> */
1924 addlen = getc(fd); /* <addlen> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001925 if (addlen < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001926 goto truncerr;
1927 if (addlen >= MAXWLEN)
1928 goto formerr;
1929
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001930 if (flags & ADD_LEADLEN)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001931 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001932 leadlen = getc(fd); /* <leadlen> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001933 if (leadlen > addlen)
1934 goto formerr;
1935 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001936 else
1937 leadlen = 0;
1938
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001939 if (addlen > 0)
1940 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001941 if (flags & ADD_COPYLEN)
1942 i = getc(fd); /* <copylen> */
1943 else
1944 i = 0;
1945 for ( ; i < addlen; ++i) /* <addstring> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001946 cbuf[i] = getc(fd);
1947 cbuf[i] = NUL;
1948 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001949
1950 if (flags & ADD_KEEPCAP)
1951 {
1952 /* <addstring> is in original case, need to get
1953 * case-folded word too. */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001954 (void)spell_casefold(cbuf, addlen, fbuf, MAXWLEN);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001955 flen = addlen - leadlen + 1;
1956 addlen = STRLEN(fbuf);
1957 }
1958 else
1959 flen = 0;
1960
1961 aw = (addword_T *)getroom(lp, &bl_used,
1962 sizeof(addword_T) + addlen + flen);
1963 if (aw == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001964 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001965
1966 if (flags & ADD_KEEPCAP)
1967 {
1968 /* Put the addition in original case after the case-folded
1969 * string. */
1970 STRCPY(aw->aw_word, fbuf);
1971 STRCPY(aw->aw_word + addlen + 1, cbuf + leadlen);
1972 }
1973 else
1974 STRCPY(aw->aw_word, cbuf);
1975
1976 aw->aw_flags = flags;
1977 aw->aw_wordlen = addlen;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001978 aw->aw_leadlen = leadlen;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001979
1980 if (flags & ADD_REGION)
1981 aw->aw_region = getc(fd); /* <region> */
1982 else
1983 aw->aw_region = REGION_ALL;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001984
1985 if (ht == NULL)
1986 {
1987 /* Using simple linked list, put it in front. */
1988 aw->aw_next = fw->fw_adds;
1989 fw->fw_adds = aw;
1990 aw->aw_saveb = NUL;
1991 }
1992 else
1993 {
1994 /* Put addition in hashtable. For key we use the part up
1995 * to the next end-of-word. */
1996 if (leadlen == 0)
1997 {
1998 p = aw->aw_word;
1999 while (*p != NUL && !spell_iswordc(p))
2000 mb_ptr_adv(p);
2001 }
2002
2003 if (leadlen != 0 || *p == NUL)
2004 {
2005 /* Only non-word characters in addition, add it to the
2006 * list with the special key NOWC_KEY. Also do this
2007 * when there is a leadstring, it would get too
2008 * complicated. */
2009 hash = hash_hash(NOWC_KEY);
2010 hi = hash_lookup(ht, NOWC_KEY, hash);
2011 if (HASHITEM_EMPTY(hi))
2012 {
2013 /* we use a dummy item as the list header */
2014 naw = (addword_T *)getroom(lp, &bl_used,
2015 sizeof(addword_T) + STRLEN(NOWC_KEY));
2016 if (naw == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002017 goto endFAIL;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00002018 STRCPY(naw->aw_word, NOWC_KEY);
2019 hash_add_item(ht, hi, naw->aw_word, hash);
2020 naw->aw_next = aw;
2021 aw->aw_next = NULL;
2022 }
2023 else
2024 {
2025 naw = HI2ADDWORD(hi);
2026 aw->aw_next = naw->aw_next;
2027 naw->aw_next = aw;
2028 }
2029 aw->aw_saveb = NUL;
2030 }
2031 else
2032 {
2033 /* Truncate at next non-word character, store that
2034 * byte in "aw_saveb". */
2035 while (*p != NUL && spell_iswordc(p))
2036 mb_ptr_adv(p);
2037 aw->aw_saveb = *p;
2038 *p = NUL;
2039 hash = hash_hash(aw->aw_word);
2040 hi = hash_lookup(ht, aw->aw_word, hash);
2041 if (HASHITEM_EMPTY(hi))
2042 {
2043 hash_add_item(ht, hi, aw->aw_word, hash);
2044 aw->aw_next = NULL;
2045 }
2046 else
2047 {
2048 naw = HI2ADDWORD(hi);
2049 aw->aw_next = naw->aw_next;
2050 naw->aw_next = aw;
2051 }
2052 }
2053 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002054 }
2055 }
2056 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002057 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002058
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002059endFAIL:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002060 lp->sl_error = TRUE;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002061
2062endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002063 if (fd != NULL)
2064 fclose(fd);
2065 hash_unlock(&lp->sl_words);
2066 sourcing_name = save_sourcing_name;
2067 sourcing_lnum = save_sourcing_lnum;
2068}
2069
2070/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002071 * Get part of an sblock_T, at least "len" bytes long.
2072 * Returns NULL when out of memory.
2073 */
2074 static void *
2075getroom(lp, bl_used, len)
2076 slang_T *lp; /* lp->sl_block is current block or NULL */
2077 int *bl_used; /* used up from current block */
2078 int len; /* length needed */
2079{
2080 char_u *p;
2081 sblock_T *bl = lp->sl_block;
2082
2083 if (bl == NULL || *bl_used + len > SBLOCKSIZE)
2084 {
2085 /* Allocate a block of memory. This is not freed until spell_reload()
2086 * is called. */
2087 bl = (sblock_T *)alloc((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
2088 if (bl == NULL)
2089 return NULL;
2090 bl->sb_next = lp->sl_block;
2091 lp->sl_block = bl;
2092 *bl_used = 0;
2093 }
2094
2095 p = bl->sb_data + *bl_used;
2096 *bl_used += len;
2097
2098 return p;
2099}
2100
2101/*
2102 * Parse 'spelllang' and set buf->b_langp accordingly.
2103 * Returns an error message or NULL.
2104 */
2105 char_u *
2106did_set_spelllang(buf)
2107 buf_T *buf;
2108{
2109 garray_T ga;
2110 char_u *lang;
2111 char_u *e;
2112 char_u *region;
2113 int region_mask;
2114 slang_T *lp;
2115 int c;
2116 char_u lbuf[MAXWLEN + 1];
2117
2118 ga_init2(&ga, sizeof(langp_T), 2);
2119
2120 /* loop over comma separated languages. */
2121 for (lang = buf->b_p_spl; *lang != NUL; lang = e)
2122 {
2123 e = vim_strchr(lang, ',');
2124 if (e == NULL)
2125 e = lang + STRLEN(lang);
Bram Moolenaar5482f332005-04-17 20:18:43 +00002126 region = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002127 if (e > lang + 2)
2128 {
2129 if (e - lang >= MAXWLEN)
2130 {
2131 ga_clear(&ga);
2132 return e_invarg;
2133 }
2134 if (lang[2] == '_')
2135 region = lang + 3;
2136 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002137
2138 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2139 if (STRNICMP(lp->sl_name, lang, 2) == 0)
2140 break;
2141
2142 if (lp == NULL)
2143 {
2144 /* Not found, load the language. */
2145 STRNCPY(lbuf, lang, e - lang);
2146 lbuf[e - lang] = NUL;
2147 if (region != NULL)
2148 mch_memmove(lbuf + 2, lbuf + 5, e - lang - 4);
2149 lp = spell_load_lang(lbuf);
2150 }
2151
2152 if (lp != NULL)
2153 {
2154 if (region == NULL)
2155 region_mask = REGION_ALL;
2156 else
2157 {
2158 /* find region in sl_regions */
2159 c = find_region(lp->sl_regions, region);
2160 if (c == REGION_ALL)
2161 {
2162 c = *e;
2163 *e = NUL;
2164 smsg((char_u *)_("Warning: region %s not supported"), lang);
2165 *e = c;
2166 region_mask = REGION_ALL;
2167 }
2168 else
2169 region_mask = 1 << c;
2170 }
2171
2172 if (ga_grow(&ga, 1) == FAIL)
2173 {
2174 ga_clear(&ga);
2175 return e_outofmem;
2176 }
2177 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2178 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2179 ++ga.ga_len;
2180 }
2181
2182 if (*e == ',')
2183 ++e;
2184 }
2185
2186 /* Add a NULL entry to mark the end of the list. */
2187 if (ga_grow(&ga, 1) == FAIL)
2188 {
2189 ga_clear(&ga);
2190 return e_outofmem;
2191 }
2192 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
2193 ++ga.ga_len;
2194
2195 /* Everything is fine, store the new b_langp value. */
2196 ga_clear(&buf->b_langp);
2197 buf->b_langp = ga;
2198
2199 return NULL;
2200}
2201
2202/*
2203 * Find the region "region[2]" in "rp" (points to "sl_regions").
2204 * Each region is simply stored as the two characters of it's name.
2205 * Returns the index if found, REGION_ALL if not found.
2206 */
2207 static int
2208find_region(rp, region)
2209 char_u *rp;
2210 char_u *region;
2211{
2212 int i;
2213
2214 for (i = 0; ; i += 2)
2215 {
2216 if (rp[i] == NUL)
2217 return REGION_ALL;
2218 if (rp[i] == region[0] && rp[i + 1] == region[1])
2219 break;
2220 }
2221 return i / 2;
2222}
2223
2224/*
2225 * Return type of word:
2226 * w word 0
2227 * Word BWF_ONECAP
2228 * W WORD BWF_ALLCAP
2229 * WoRd wOrd BWF_KEEPCAP
2230 */
2231 static int
2232captype(word, end)
2233 char_u *word;
2234 char_u *end;
2235{
2236 char_u *p;
2237 int c;
2238 int firstcap;
2239 int allcap;
2240 int past_second = FALSE; /* past second word char */
2241
2242 /* find first letter */
2243 for (p = word; !spell_iswordc(p); mb_ptr_adv(p))
2244 if (p >= end)
2245 return 0; /* only non-word characters, illegal word */
2246#ifdef FEAT_MBYTE
2247 c = mb_ptr2char_adv(&p);
2248#else
2249 c = *p++;
2250#endif
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002251 firstcap = allcap = spell_isupper(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002252
2253 /*
2254 * Need to check all letters to find a word with mixed upper/lower.
2255 * But a word with an upper char only at start is a ONECAP.
2256 */
2257 for ( ; p < end; mb_ptr_adv(p))
2258 if (spell_iswordc(p))
2259 {
2260#ifdef FEAT_MBYTE
2261 c = mb_ptr2char(p);
2262#else
2263 c = *p;
2264#endif
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002265 if (!spell_isupper(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002266 {
2267 /* UUl -> KEEPCAP */
2268 if (past_second && allcap)
2269 return BWF_KEEPCAP;
2270 allcap = FALSE;
2271 }
2272 else if (!allcap)
2273 /* UlU -> KEEPCAP */
2274 return BWF_KEEPCAP;
2275 past_second = TRUE;
2276 }
2277
2278 if (allcap)
2279 return BWF_ALLCAP;
2280 if (firstcap)
2281 return BWF_ONECAP;
2282 return 0;
2283}
2284
2285# if defined(FEAT_MBYTE) || defined(PROTO)
2286/*
2287 * Clear all spelling tables and reload them.
2288 * Used after 'encoding' is set.
2289 */
2290 void
2291spell_reload()
2292{
2293 buf_T *buf;
2294 slang_T *lp;
2295
2296 /* Initialize the table for spell_iswordc(). */
2297 init_spell_chartab();
2298
2299 /* Unload all allocated memory. */
2300 while (first_lang != NULL)
2301 {
2302 lp = first_lang;
2303 first_lang = lp->sl_next;
2304 slang_free(lp);
2305 }
2306
2307 /* Go through all buffers and handle 'spelllang'. */
2308 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2309 {
2310 ga_clear(&buf->b_langp);
2311 if (*buf->b_p_spl != NUL)
2312 did_set_spelllang(buf);
2313 }
2314}
2315# endif
2316
2317/*
2318 * Recognizing words uses a two-step mechanism:
2319 * 1. Locate a basic word, made out of word characters only and separated by
2320 * non-word characters.
2321 * 2. When a basic word is found, check if (possibly required) additions
2322 * before and after the word are present.
2323 *
2324 * Both mechanisms use affixes (prefixes and suffixes) to reduce the number of
2325 * words. When no matching word was found in the hashtable the start of the
2326 * word is checked for matching prefixes and the end of the word for matching
2327 * suffixes. All matching affixes are removed and then the resulting word is
2328 * searched for. If found it is checked if it supports the used affix.
2329 */
2330
2331
2332#if defined(FEAT_MBYTE) || defined(PROTO)
2333/*
2334 * Functions for ":mkspell".
2335 * Only possible with the multi-byte feature.
2336 */
2337
2338#define MAXLINELEN 300 /* Maximum length in bytes of a line in a .aff
2339 and .dic file. */
2340/*
2341 * Main structure to store the contents of a ".aff" file.
2342 */
2343typedef struct afffile_S
2344{
2345 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
2346 char_u *af_try; /* "TRY" line in "af_enc" encoding */
2347 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
2348 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
2349 garray_T af_rep; /* list of repentry_T entries from REP lines */
2350} afffile_T;
2351
2352typedef struct affentry_S affentry_T;
2353
2354/* Affix header from ".aff" file. Used for af_pref and af_suff. */
2355typedef struct affheader_S
2356{
2357 char_u ah_key[2]; /* key for hashtable == name of affix entry */
2358 int ah_combine;
2359 affentry_T *ah_first; /* first affix entry */
2360 short_u ah_affnr; /* used in get_new_aff() */
2361} affheader_T;
2362
2363#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
2364
2365/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
2366struct affentry_S
2367{
2368 affentry_T *ae_next; /* next affix with same name/number */
2369 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
2370 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar5482f332005-04-17 20:18:43 +00002371 char_u *ae_add_nw; /* For a suffix: first non-word char in
2372 * "ae_add"; for a prefix with only non-word
2373 * chars: equal to "ae_add", for a prefix with
2374 * word and non-word chars: first non-word
2375 * char after word char. NULL otherwise. */
2376 char_u *ae_add_pw; /* For a prefix with both word and non-word
2377 * chars: first word char. NULL otherwise. */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00002378 char_u ae_preword; /* TRUE for a prefix with one word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002379 char_u *ae_cond; /* condition (NULL for ".") */
2380 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
2381 short_u ae_affnr; /* for old affix: new affix number */
2382};
2383
2384/*
2385 * Structure to store a word from a ".dic" file.
2386 */
2387typedef struct dicword_S
2388{
2389 char_u *dw_affnm; /* original affix names */
2390 char_u dw_word[1]; /* actually longer: the word in 'encoding' */
2391} dicword_T;
2392
2393static dicword_T dumdw;
2394#define HI2DW(hi) ((dicword_T *)((hi)->hi_key - (dumdw.dw_word - (char_u *)&dumdw)))
2395
2396/*
2397 * Structure to store a basic word for the spell file.
2398 * This is used for ":mkspell", not for spell checking.
2399 */
2400typedef struct basicword_S basicword_T;
2401struct basicword_S
2402{
2403 basicword_T *bw_next; /* next word with same basic word */
2404 basicword_T *bw_cnext; /* next word with same caps */
2405 int bw_flags; /* BWF_ flags */
2406 garray_T bw_prefix; /* table with prefix numbers */
2407 garray_T bw_suffix; /* table with suffix numbers */
2408 int bw_region; /* region bits */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002409 char_u *bw_caseword; /* keep-case word or NULL */
2410 char_u *bw_leadstring; /* must come before bw_word or NULL */
2411 char_u *bw_addstring; /* must come after bw_word or NULL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002412 char_u bw_word[1]; /* actually longer: word case folded */
2413};
2414
2415static basicword_T dumbw;
2416#define KEY2BW(p) ((basicword_T *)((p) - (dumbw.bw_word - (char_u *)&dumbw)))
2417#define HI2BW(hi) KEY2BW((hi)->hi_key)
2418
2419/* Store the affix number related with a certain string. */
2420typedef struct affhash_S
2421{
2422 short_u as_nr; /* the affix nr */
2423 char_u as_word[1]; /* actually longer */
2424} affhash_T;
2425
2426static affhash_T dumas;
2427#define HI2AS(hi) ((affhash_T *)((hi)->hi_key - (dumas.as_word - (char_u *)&dumas)))
2428
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002429/* info for writing the spell file */
2430typedef struct winfo_S
2431{
2432 FILE *wif_fd;
2433 basicword_T *wif_prevbw; /* last written basic word */
2434 int wif_regionmask; /* regions supported */
2435 int wif_prefm; /* 1 or 2 bytes used for prefix NR */
2436 int wif_suffm; /* 1 or 2 bytes used for suffix NR */
2437 long wif_wcount; /* written word count */
2438 long wif_acount; /* written addition count */
2439 long wif_addmax; /* max number of additions on one word */
2440 char_u *wif_addmaxw; /* word with max additions */
2441} winfo_T;
2442
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002443
Bram Moolenaar5482f332005-04-17 20:18:43 +00002444static afffile_T *spell_read_aff __ARGS((char_u *fname, vimconv_T *conv, int ascii));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002445static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar5482f332005-04-17 20:18:43 +00002446static int has_non_ascii __ARGS((char_u *s));
2447static int spell_read_dic __ARGS((hashtab_T *ht, char_u *fname, vimconv_T *conv, int ascii));
2448static int get_new_aff __ARGS((hashtab_T *oldaff, garray_T *gap, int prefix));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002449static void spell_free_dic __ARGS((hashtab_T *dic));
2450static int same_affentries __ARGS((affheader_T *ah1, affheader_T *ah2));
2451static void add_affhash __ARGS((hashtab_T *ht, char_u *key, int newnr));
2452static void clear_affhash __ARGS((hashtab_T *ht));
2453static void trans_affixes __ARGS((dicword_T *dw, basicword_T *bw, afffile_T *oldaff, hashtab_T *newwords));
2454static int build_wordlist __ARGS((hashtab_T *newwords, hashtab_T *oldwords, afffile_T *oldaff, int regionmask));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002455static basicword_T *get_basicword __ARGS((char_u *word, int asize));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002456static void combine_regions __ARGS((hashtab_T *newwords));
2457static int same_affixes __ARGS((basicword_T *bw, basicword_T *nbw));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002458static int expand_affixes __ARGS((hashtab_T *newwords, garray_T *prefgap, garray_T *suffgap));
2459static int expand_one_aff __ARGS((basicword_T *bw, garray_T *add_words, affentry_T *pae, affentry_T *sae));
2460static int add_to_wordlist __ARGS((hashtab_T *newwords, basicword_T *bw));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002461static void write_affix __ARGS((FILE *fd, affheader_T *ah));
2462static void write_affixlist __ARGS((FILE *fd, garray_T *aff, int bytes));
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00002463static void write_vim_spell __ARGS((char_u *fname, garray_T *prefga, garray_T *suffga, hashtab_T *newwords, int regcount, char_u *regchars, int ascii));
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002464static void write_bword __ARGS((winfo_T *wif, basicword_T *bw, int lowcap));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002465static void free_wordtable __ARGS((hashtab_T *ht));
2466static void free_basicword __ARGS((basicword_T *bw));
2467static void free_affixentries __ARGS((affentry_T *first));
Bram Moolenaar5482f332005-04-17 20:18:43 +00002468static void free_affix_entry __ARGS((affentry_T *ap));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002469
2470/*
2471 * Read an affix ".aff" file.
2472 * Returns an afffile_T, NULL for failure.
2473 */
2474 static afffile_T *
Bram Moolenaar5482f332005-04-17 20:18:43 +00002475spell_read_aff(fname, conv, ascii)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002476 char_u *fname;
2477 vimconv_T *conv; /* info for encoding conversion */
Bram Moolenaar5482f332005-04-17 20:18:43 +00002478 int ascii; /* Only accept ASCII characters */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002479{
2480 FILE *fd;
2481 afffile_T *aff;
2482 char_u rline[MAXLINELEN];
2483 char_u *line;
2484 char_u *pc = NULL;
2485 char_u *(items[6]);
2486 int itemcnt;
2487 char_u *p;
2488 int lnum = 0;
2489 affheader_T *cur_aff = NULL;
2490 int aff_todo = 0;
2491 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002492 char_u *low = NULL;
2493 char_u *fol = NULL;
2494 char_u *upp = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002495
2496 fd = fopen((char *)fname, "r");
2497 if (fd == NULL)
2498 {
2499 EMSG2(_(e_notopen), fname);
2500 return NULL;
2501 }
2502
2503 smsg((char_u *)_("Reading affix file %s..."), fname);
2504 out_flush();
2505
2506 aff = (afffile_T *)alloc_clear((unsigned)sizeof(afffile_T));
2507 if (aff == NULL)
2508 return NULL;
2509 hash_init(&aff->af_pref);
2510 hash_init(&aff->af_suff);
2511 ga_init2(&aff->af_rep, (int)sizeof(repentry_T), 20);
2512
2513 /*
2514 * Read all the lines in the file one by one.
2515 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002516 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002517 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002518 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002519 ++lnum;
2520
2521 /* Skip comment lines. */
2522 if (*rline == '#')
2523 continue;
2524
2525 /* Convert from "SET" to 'encoding' when needed. */
2526 vim_free(pc);
2527 if (conv->vc_type != CONV_NONE)
2528 {
2529 pc = string_convert(conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002530 if (pc == NULL)
2531 {
2532 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
2533 fname, lnum, rline);
2534 continue;
2535 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002536 line = pc;
2537 }
2538 else
2539 {
2540 pc = NULL;
2541 line = rline;
2542 }
2543
2544 /* Split the line up in white separated items. Put a NUL after each
2545 * item. */
2546 itemcnt = 0;
2547 for (p = line; ; )
2548 {
2549 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
2550 ++p;
2551 if (*p == NUL)
2552 break;
2553 items[itemcnt++] = p;
2554 while (*p > ' ') /* skip until white space or CR/NL */
2555 ++p;
2556 if (*p == NUL)
2557 break;
2558 *p++ = NUL;
2559 }
2560
2561 /* Handle non-empty lines. */
2562 if (itemcnt > 0)
2563 {
2564 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
2565 && aff->af_enc == NULL)
2566 {
2567 if (aff->af_enc != NULL)
2568 smsg((char_u *)_("Duplicate SET line ignored in %s line %d: %s"),
2569 fname, lnum, line);
2570 else
2571 {
2572 /* Setup for conversion from "ENC" to 'encoding'. */
2573 aff->af_enc = enc_canonize(items[1]);
Bram Moolenaar5482f332005-04-17 20:18:43 +00002574 if (aff->af_enc != NULL && !ascii
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002575 && convert_setup(conv, aff->af_enc, p_enc) == FAIL)
2576 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
2577 fname, aff->af_enc, p_enc);
2578 }
2579 }
2580 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2
2581 && aff->af_try == NULL)
2582 aff->af_try = vim_strsave(items[1]);
2583 else if ((STRCMP(items[0], "PFX") == 0
2584 || STRCMP(items[0], "SFX") == 0)
2585 && aff_todo == 0
2586 && itemcnt == 4)
2587 {
2588 /* New affix letter. */
2589 cur_aff = (affheader_T *)alloc((unsigned)sizeof(affheader_T));
2590 if (cur_aff == NULL)
2591 break;
2592 cur_aff->ah_key[0] = *items[1];
2593 cur_aff->ah_key[1] = NUL;
2594 if (items[1][1] != NUL)
2595 smsg((char_u *)_("Affix name too long in %s line %d: %s"),
2596 fname, lnum, items[1]);
2597 if (*items[2] == 'Y')
2598 cur_aff->ah_combine = TRUE;
2599 else if (*items[2] == 'N')
2600 cur_aff->ah_combine = FALSE;
2601 else if (p_verbose > 0)
2602 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
2603 fname, lnum, items[2]);
2604 cur_aff->ah_first = NULL;
2605 if (*items[0] == 'P')
2606 tp = &aff->af_pref;
2607 else
2608 tp = &aff->af_suff;
2609 if (!HASHITEM_EMPTY(hash_find(tp, cur_aff->ah_key)))
2610 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
2611 fname, lnum, items[1]);
2612 else
2613 hash_add(tp, cur_aff->ah_key);
2614
2615 aff_todo = atoi((char *)items[3]);
2616 }
2617 else if ((STRCMP(items[0], "PFX") == 0
2618 || STRCMP(items[0], "SFX") == 0)
2619 && aff_todo > 0
2620 && STRCMP(cur_aff->ah_key, items[1]) == 0
2621 && itemcnt == 5)
2622 {
2623 affentry_T *aff_entry;
2624
2625 /* New item for an affix letter. */
2626 --aff_todo;
2627 aff_entry = (affentry_T *)alloc_clear(
2628 (unsigned)sizeof(affentry_T));
2629 if (aff_entry == NULL)
2630 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00002631
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002632 if (STRCMP(items[2], "0") != 0)
2633 aff_entry->ae_chop = vim_strsave(items[2]);
2634 if (STRCMP(items[3], "0") != 0)
2635 aff_entry->ae_add = vim_strsave(items[3]);
2636 if (STRCMP(items[4], ".") != 0)
2637 {
2638 char_u buf[MAXLINELEN];
2639
2640 aff_entry->ae_cond = vim_strsave(items[4]);
2641 if (*items[0] == 'P')
2642 sprintf((char *)buf, "^%s", items[4]);
2643 else
2644 sprintf((char *)buf, "%s$", items[4]);
2645 aff_entry->ae_prog = vim_regcomp(buf, RE_MAGIC + RE_STRING);
2646 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00002647
2648 if (ascii && (has_non_ascii(aff_entry->ae_chop)
2649 || has_non_ascii(aff_entry->ae_add)))
2650 {
2651 /* Don't use an affix entry with non-ASCII characters when
2652 * "ascii" is TRUE. */
2653 free_affix_entry(aff_entry);
2654 }
2655 else
2656 {
2657 aff_entry->ae_next = cur_aff->ah_first;
2658 cur_aff->ah_first = aff_entry;
2659 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002660 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002661 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
2662 {
2663 if (fol != NULL)
2664 smsg((char_u *)_("Duplicate FOL in %s line %d"),
2665 fname, lnum);
2666 else
2667 fol = vim_strsave(items[1]);
2668 }
2669 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
2670 {
2671 if (low != NULL)
2672 smsg((char_u *)_("Duplicate LOW in %s line %d"),
2673 fname, lnum);
2674 else
2675 low = vim_strsave(items[1]);
2676 }
2677 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
2678 {
2679 if (upp != NULL)
2680 smsg((char_u *)_("Duplicate UPP in %s line %d"),
2681 fname, lnum);
2682 else
2683 upp = vim_strsave(items[1]);
2684 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002685 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
2686 /* Ignore REP count */;
2687 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
2688 {
2689 repentry_T *rp;
2690
2691 /* REP item */
2692 if (ga_grow(&aff->af_rep, 1) == FAIL)
2693 break;
2694 rp = ((repentry_T *)aff->af_rep.ga_data) + aff->af_rep.ga_len;
2695 rp->re_from = vim_strsave(items[1]);
2696 rp->re_to = vim_strsave(items[2]);
2697 ++aff->af_rep.ga_len;
2698 }
2699 else if (p_verbose > 0)
2700 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
2701 fname, lnum, items[0]);
2702 }
2703
2704 }
2705
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002706 if (fol != NULL || low != NULL || upp != NULL)
2707 {
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00002708 /* Don't write a word table for an ASCII file, so that we don't check
2709 * for conflicts with a word table that matches 'encoding'. */
2710 if (!ascii)
2711 {
2712 if (fol == NULL || low == NULL || upp == NULL)
2713 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
2714 else
2715 set_spell_chartab(fol, low, upp);
2716 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002717
2718 vim_free(fol);
2719 vim_free(low);
2720 vim_free(upp);
2721 }
2722
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002723 vim_free(pc);
2724 fclose(fd);
2725 return aff;
2726}
2727
2728/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00002729 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
2730 * When "s" is NULL FALSE is returned.
2731 */
2732 static int
2733has_non_ascii(s)
2734 char_u *s;
2735{
2736 char_u *p;
2737
2738 if (s != NULL)
2739 for (p = s; *p != NUL; ++p)
2740 if (*p >= 128)
2741 return TRUE;
2742 return FALSE;
2743}
2744
2745/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002746 * Free the structure filled by spell_read_aff().
2747 */
2748 static void
2749spell_free_aff(aff)
2750 afffile_T *aff;
2751{
2752 hashtab_T *ht;
2753 hashitem_T *hi;
2754 int todo;
2755 int i;
2756 repentry_T *rp;
2757 affheader_T *ah;
2758
2759 vim_free(aff->af_enc);
2760 vim_free(aff->af_try);
2761
2762 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
2763 {
2764 todo = ht->ht_used;
2765 for (hi = ht->ht_array; todo > 0; ++hi)
2766 {
2767 if (!HASHITEM_EMPTY(hi))
2768 {
2769 --todo;
2770 ah = HI2AH(hi);
2771 free_affixentries(ah->ah_first);
2772 vim_free(ah);
2773 }
2774 }
2775 if (ht == &aff->af_suff)
2776 break;
2777 }
2778 hash_clear(&aff->af_pref);
2779 hash_clear(&aff->af_suff);
2780
2781 for (i = 0; i < aff->af_rep.ga_len; ++i)
2782 {
2783 rp = ((repentry_T *)aff->af_rep.ga_data) + i;
2784 vim_free(rp->re_from);
2785 vim_free(rp->re_to);
2786 }
2787 ga_clear(&aff->af_rep);
2788
2789 vim_free(aff);
2790}
2791
2792/*
2793 * Read a dictionary ".dic" file.
2794 * Returns OK or FAIL;
2795 * Each entry in the hashtab_T is a dicword_T.
2796 */
2797 static int
Bram Moolenaar5482f332005-04-17 20:18:43 +00002798spell_read_dic(ht, fname, conv, ascii)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002799 hashtab_T *ht;
2800 char_u *fname;
2801 vimconv_T *conv; /* info for encoding conversion */
Bram Moolenaar5482f332005-04-17 20:18:43 +00002802 int ascii; /* only accept ASCII words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002803{
2804 char_u line[MAXLINELEN];
2805 char_u *p;
2806 dicword_T *dw;
2807 char_u *pc;
2808 char_u *w;
2809 int l;
2810 hash_T hash;
2811 hashitem_T *hi;
2812 FILE *fd;
2813 int lnum = 1;
2814
2815 fd = fopen((char *)fname, "r");
2816 if (fd == NULL)
2817 {
2818 EMSG2(_(e_notopen), fname);
2819 return FAIL;
2820 }
2821
2822 smsg((char_u *)_("Reading dictionary file %s..."), fname);
2823 out_flush();
2824
2825 /* Read and ignore the first line: word count. */
2826 (void)vim_fgets(line, MAXLINELEN, fd);
2827 if (!isdigit(*skipwhite(line)))
2828 EMSG2(_("E760: No word count in %s"), fname);
2829
2830 /*
2831 * Read all the lines in the file one by one.
2832 * The words are converted to 'encoding' here, before being added to
2833 * the hashtable.
2834 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002835 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002836 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002837 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002838 ++lnum;
2839
2840 /* Remove CR, LF and white space from end. */
2841 l = STRLEN(line);
2842 while (l > 0 && line[l - 1] <= ' ')
2843 --l;
2844 if (l == 0)
2845 continue; /* empty line */
2846 line[l] = NUL;
2847
2848 /* Find the optional affix names. */
2849 p = vim_strchr(line, '/');
2850 if (p != NULL)
2851 *p++ = NUL;
2852
Bram Moolenaar5482f332005-04-17 20:18:43 +00002853 /* Skip non-ASCII words when "ascii" is TRUE. */
2854 if (ascii && has_non_ascii(line))
2855 continue;
2856
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002857 /* Convert from "SET" to 'encoding' when needed. */
2858 if (conv->vc_type != CONV_NONE)
2859 {
2860 pc = string_convert(conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002861 if (pc == NULL)
2862 {
2863 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
2864 fname, lnum, line);
2865 continue;
2866 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002867 w = pc;
2868 }
2869 else
2870 {
2871 pc = NULL;
2872 w = line;
2873 }
2874
2875 dw = (dicword_T *)alloc_clear((unsigned)sizeof(dicword_T)
2876 + STRLEN(w));
2877 if (dw == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002878 {
2879 vim_free(pc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002880 break;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002881 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002882 STRCPY(dw->dw_word, w);
2883 vim_free(pc);
2884
2885 hash = hash_hash(dw->dw_word);
2886 hi = hash_lookup(ht, dw->dw_word, hash);
2887 if (!HASHITEM_EMPTY(hi))
2888 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
2889 fname, lnum, line);
2890 else
2891 hash_add_item(ht, hi, dw->dw_word, hash);
2892
2893 if (p != NULL)
2894 dw->dw_affnm = vim_strsave(p);
2895 }
2896
2897 fclose(fd);
2898 return OK;
2899}
2900
2901/*
2902 * Free the structure filled by spell_read_dic().
2903 */
2904 static void
2905spell_free_dic(dic)
2906 hashtab_T *dic;
2907{
2908 int todo;
2909 dicword_T *dw;
2910 hashitem_T *hi;
2911
2912 todo = dic->ht_used;
2913 for (hi = dic->ht_array; todo > 0; ++hi)
2914 {
2915 if (!HASHITEM_EMPTY(hi))
2916 {
2917 --todo;
2918 dw = HI2DW(hi);
2919 vim_free(dw->dw_affnm);
2920 vim_free(dw);
2921 }
2922 }
2923 hash_clear(dic);
2924}
2925
2926/*
2927 * Take the affixes read by spell_read_aff() and add them to the new list.
2928 * Attempts to re-use the same number for identical affixes (ignoring the
2929 * condition, since we remove that). That is especially important when using
2930 * multiple regions.
2931 * Returns OK or FAIL;
2932 */
2933 static int
Bram Moolenaar5482f332005-04-17 20:18:43 +00002934get_new_aff(oldaff, gap, prefix)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002935 hashtab_T *oldaff; /* hashtable with affheader_T */
2936 garray_T *gap; /* table with new affixes */
Bram Moolenaar5482f332005-04-17 20:18:43 +00002937 int prefix; /* TRUE when doing prefixes, FALSE for
2938 suffixes */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002939{
2940 int oldtodo;
2941 affheader_T *oldah, *newah, *gapah;
2942 affentry_T *oldae, *newae;
2943 hashitem_T *oldhi;
2944 hashitem_T *hi;
2945 hashtab_T condht; /* conditions already found */
2946 char_u condkey[MAXLINELEN];
2947 int newnr;
2948 int gapnr;
2949 int retval = OK;
2950 char_u *p;
2951 garray_T tga;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00002952 int preword;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002953
2954 /*
2955 * Loop over all the old affix names.
2956 */
2957 oldtodo = oldaff->ht_used;
2958 for (oldhi = oldaff->ht_array; oldtodo > 0 && retval == OK; ++oldhi)
2959 {
2960 if (!HASHITEM_EMPTY(oldhi))
2961 {
2962 --oldtodo;
2963 oldah = (affheader_T *)oldhi->hi_key;
2964
2965 /* Put entries with the same condition under the same new affix
2966 * nr in "tga". Use hashtable "condht" to find them. */
2967 ga_init2(&tga, sizeof(affheader_T), 10);
2968 hash_init(&condht);
2969
2970 /*
2971 * Loop over all affixes with the same name.
2972 * The affixes with the same condition will get the same number,
2973 * since they can be used with the same words.
2974 * 1. build the lists of new affentry_T, with the headers in "tga".
2975 * 2. Check if some of the lists already exist in "gap", re-use
2976 * their number.
2977 * 3. Assign the new numbers to the old affixes.
2978 */
2979
2980 /* 1. build the lists of new affentry_T. */
2981 for (oldae = oldah->ah_first; oldae != NULL && retval == OK;
2982 oldae = oldae->ae_next)
2983 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00002984 preword = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002985 oldae->ae_add_nw = NULL;
Bram Moolenaar5482f332005-04-17 20:18:43 +00002986 oldae->ae_add_pw = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002987 if (oldae->ae_add != NULL)
2988 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00002989 /* Check for non-word characters in the affix. If there
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00002990 * is one a suffix will be turned into an addition, a
2991 * prefix may be turned into a leadstring.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002992 * This is stored with the old affix, that is where
2993 * trans_affixes() will check. */
2994 for (p = oldae->ae_add; *p != NUL; mb_ptr_adv(p))
2995 if (!spell_iswordc(p))
Bram Moolenaar5482f332005-04-17 20:18:43 +00002996 {
2997 oldae->ae_add_nw = p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002998 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00002999 }
3000
3001 if (prefix && oldae->ae_add_nw != NULL)
3002 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003003 /* If a prefix has non-word characters special
3004 * treatment is necessary:
3005 * - If it has only non-word characters it becomes a
3006 * leadstring.
3007 * - If it has a sequence of word characters followed
3008 * by a non-word char it becomes a "preword": "d'",
3009 * "de-", "d'ai", etc.
3010 * - if it has another mix of word and non-word
3011 * characters the part before the last word char
3012 * becomes a leadstring: "'d", etc.
3013 */
Bram Moolenaar5482f332005-04-17 20:18:43 +00003014 for (p = oldae->ae_add; *p != NUL; mb_ptr_adv(p))
3015 if (spell_iswordc(p))
3016 {
3017 oldae->ae_add_pw = p;
3018 break;
3019 }
3020 if (oldae->ae_add_pw != NULL)
3021 {
3022 /* Mixed prefix, set ae_add_nw to first non-word
3023 * char after ae_add_pw (if there is one). */
3024 oldae->ae_add_nw = NULL;
3025 for ( ; *p != NUL; mb_ptr_adv(p))
3026 if (!spell_iswordc(p))
3027 {
3028 oldae->ae_add_nw = p;
3029 break;
3030 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003031 if (oldae->ae_add_nw != NULL)
3032 {
3033 preword = TRUE;
3034 oldae->ae_add_pw = NULL;
3035 oldae->ae_add_nw = NULL;
3036 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003037 }
3038 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003039 }
3040
3041 if (oldae->ae_cond == NULL)
3042 /* hashtable requires a non-empty key */
3043 STRCPY(condkey, "---");
3044 else
3045 STRCPY(condkey, oldae->ae_cond);
3046
3047 /* Look for an existing list with this name and condition. */
3048 hi = hash_find(&condht, condkey);
3049 if (!HASHITEM_EMPTY(hi))
3050 /* Match with existing affix, use that one. */
3051 newnr = HI2AS(hi)->as_nr;
3052 else
3053 {
3054 /* Add a new affix number. */
3055 newnr = tga.ga_len;
3056 if (ga_grow(&tga, 1) == FAIL)
3057 retval = FAIL;
3058 else
3059 {
3060 newah = ((affheader_T *)tga.ga_data) + newnr;
3061 newah->ah_combine = oldah->ah_combine;
3062 newah->ah_first = NULL;
3063 ++tga.ga_len;
3064
3065 /* Add the new list to the condht hashtable. */
3066 add_affhash(&condht, condkey, newnr);
3067 }
3068 }
3069
3070 /* Add the new affentry_T to the list. */
3071 newah = ((affheader_T *)tga.ga_data) + newnr;
3072 newae = (affentry_T *)alloc_clear((unsigned)sizeof(affentry_T));
3073 if (newae == NULL)
3074 retval = FAIL;
3075 else
3076 {
3077 newae->ae_next = newah->ah_first;
3078 newah->ah_first = newae;
3079 if (oldae->ae_chop == NULL)
3080 newae->ae_chop = NULL;
3081 else
3082 newae->ae_chop = vim_strsave(oldae->ae_chop);
3083 if (oldae->ae_add == NULL)
3084 newae->ae_add = NULL;
3085 else
3086 newae->ae_add = vim_strsave(oldae->ae_add);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003087 newae->ae_preword = preword;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003088
3089 /* The condition is not copied, since the new affix is
3090 * only used for words where the condition matches. */
3091 }
3092 }
3093
3094 /* 2. Check if some of the lists already exist, re-use their
3095 * number. Otherwise add the list to "gap". */
3096 for (newnr = 0; newnr < tga.ga_len; ++newnr)
3097 {
3098 newah = ((affheader_T *)tga.ga_data) + newnr;
3099 for (gapnr = 0; gapnr < gap->ga_len; ++gapnr)
3100 {
3101 gapah = ((affheader_T *)gap->ga_data) + gapnr;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003102 if (newah->ah_combine == gapah->ah_combine
3103 && same_affentries(newah, gapah))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003104 /* Found an existing affheader_T entry with same
3105 * affentry_T list, use its number. */
3106 break;
3107 }
3108
3109 newah->ah_affnr = gapnr;
3110 if (gapnr == gap->ga_len)
3111 {
3112 /* This is a new affentry_T list, add it. */
3113 if (ga_grow(gap, 1) == FAIL)
3114 retval = FAIL;
3115 else
3116 {
3117 *(((affheader_T *)gap->ga_data) + gap->ga_len) = *newah;
3118 ++gap->ga_len;
3119 }
3120 }
3121 else
3122 {
3123 /* free unused affentry_T list */
3124 free_affixentries(newah->ah_first);
3125 }
3126 }
3127
3128 /* 3. Assign the new affix numbers to the old affixes. */
3129 for (oldae = oldah->ah_first; oldae != NULL && retval == OK;
3130 oldae = oldae->ae_next)
3131 {
3132 if (oldae->ae_cond == NULL)
3133 /* hashtable requires a non-empty key */
3134 STRCPY(condkey, "---");
3135 else
3136 STRCPY(condkey, oldae->ae_cond);
3137
3138 /* Look for an existing affix with this name and condition. */
3139 hi = hash_find(&condht, condkey);
3140 if (!HASHITEM_EMPTY(hi))
3141 /* Match with existing affix, use that one. */
3142 newnr = HI2AS(hi)->as_nr;
3143 else
3144 {
3145 EMSG(_(e_internal));
3146 retval = FAIL;
3147 }
3148 newah = ((affheader_T *)tga.ga_data) + newnr;
3149 oldae->ae_affnr = newah->ah_affnr;
3150 }
3151
3152 ga_clear(&tga);
3153 clear_affhash(&condht);
3154 }
3155 }
3156
3157 return retval;
3158}
3159
3160/*
3161 * Return TRUE if the affentry_T lists for "ah1" and "ah2" contain the same
3162 * items, ignoring the order.
3163 * Only compares the chop and add strings, not the condition.
3164 */
3165 static int
3166same_affentries(ah1, ah2)
3167 affheader_T *ah1;
3168 affheader_T *ah2;
3169{
3170 affentry_T *ae1, *ae2;
3171
3172 /* Check the length of the lists first. */
3173 ae2 = ah2->ah_first;
3174 for (ae1 = ah1->ah_first; ae1 != NULL; ae1 = ae1->ae_next)
3175 {
3176 if (ae2 == NULL)
3177 return FALSE; /* "ah1" list is longer */
3178 ae2 = ae2->ae_next;
3179 }
3180 if (ae2 != NULL)
3181 return FALSE; /* "ah2" list is longer */
3182
3183 /* Check that each entry in "ah1" appears in "ah2". */
3184 for (ae1 = ah1->ah_first; ae1 != NULL; ae1 = ae1->ae_next)
3185 {
3186 for (ae2 = ah2->ah_first; ae2 != NULL; ae2 = ae2->ae_next)
3187 {
3188 if ((ae1->ae_chop == NULL) == (ae2->ae_chop == NULL)
3189 && (ae1->ae_add == NULL) == (ae2->ae_add == NULL)
3190 && (ae1->ae_chop == NULL
3191 || STRCMP(ae1->ae_chop, ae2->ae_chop) == 0)
3192 && (ae1->ae_add == NULL
3193 || STRCMP(ae1->ae_add, ae2->ae_add) == 0))
3194 break;
3195 }
3196 if (ae2 == NULL)
3197 return FALSE;
3198 }
3199
3200 return TRUE;
3201}
3202
3203/*
3204 * Add a chop/add or cond hashtable entry.
3205 */
3206 static void
3207add_affhash(ht, key, newnr)
3208 hashtab_T *ht;
3209 char_u *key;
3210 int newnr;
3211{
3212 affhash_T *as;
3213
3214 as = (affhash_T *)alloc((unsigned)sizeof(affhash_T) + STRLEN(key));
3215 if (as != NULL)
3216 {
3217 as->as_nr = newnr;
3218 STRCPY(as->as_word, key);
3219 hash_add(ht, as->as_word);
3220 }
3221}
3222
3223/*
3224 * Clear the chop/add hashtable used to detect identical affixes.
3225 */
3226 static void
3227clear_affhash(ht)
3228 hashtab_T *ht;
3229{
3230 int todo;
3231 hashitem_T *hi;
3232
3233 todo = ht->ht_used;
3234 for (hi = ht->ht_array; todo > 0; ++hi)
3235 {
3236 if (!HASHITEM_EMPTY(hi))
3237 {
3238 --todo;
3239 vim_free(HI2AS(hi));
3240 }
3241 }
3242 hash_clear(ht);
3243}
3244
3245/*
3246 * Translate list of affix names for an old word to affix numbers in a new
3247 * basic word.
3248 * This checks if the conditions match with the old word. The result is that
3249 * the new affix does not need to store the condition.
3250 */
3251 static void
3252trans_affixes(dw, bw, oldaff, newwords)
3253 dicword_T *dw; /* old word */
3254 basicword_T *bw; /* basic word */
3255 afffile_T *oldaff; /* affixes for "oldwords" */
3256 hashtab_T *newwords; /* table with words */
3257{
3258 char_u key[2];
3259 char_u *p;
3260 char_u *affnm;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003261 garray_T *gap, *agap;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003262 hashitem_T *aff_hi;
3263 affheader_T *ah;
3264 affentry_T *ae;
3265 regmatch_T regmatch;
3266 int i;
3267 basicword_T *nbw;
3268 int alen;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003269 garray_T suffixga; /* list of words with non-word suffixes */
3270 garray_T prefixga; /* list of words with non-word prefixes */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003271 char_u nword[MAXWLEN];
3272 int flags;
3273 int n;
3274
Bram Moolenaar5482f332005-04-17 20:18:43 +00003275 ga_init2(&suffixga, (int)sizeof(basicword_T *), 5);
3276 ga_init2(&prefixga, (int)sizeof(basicword_T *), 5);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003277
3278 /* Loop over all the affix names of the old word. */
3279 key[1] = NUL;
3280 for (affnm = dw->dw_affnm; *affnm != NUL; ++affnm)
3281 {
3282 key[0] = *affnm;
3283 aff_hi = hash_find(&oldaff->af_pref, key);
3284 if (!HASHITEM_EMPTY(aff_hi))
3285 gap = &bw->bw_prefix; /* found a prefix */
3286 else
3287 {
3288 gap = &bw->bw_suffix; /* must be a suffix */
3289 aff_hi = hash_find(&oldaff->af_suff, key);
3290 if (HASHITEM_EMPTY(aff_hi))
3291 {
3292 smsg((char_u *)_("No affix entry '%s' for word %s"),
3293 key, dw->dw_word);
3294 continue;
3295 }
3296 }
3297
3298 /* Loop over all the affix entries for this affix name. */
3299 ah = HI2AH(aff_hi);
3300 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3301 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003302 /* Setup for regexp matching. Note that we don't ignore case.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003303 * This is weird, because the rules in an .aff file don't care
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003304 * about case, but it's necessary for compatibility with Myspell.
3305 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003306 regmatch.regprog = ae->ae_prog;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003307 regmatch.rm_ic = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003308 if (ae->ae_prog == NULL
3309 || vim_regexec(&regmatch, dw->dw_word, (colnr_T)0))
3310 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003311 if ((ae->ae_add_nw != NULL || ae->ae_add_pw != NULL)
3312 && (gap != &bw->bw_suffix || bw->bw_addstring == NULL))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003313 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003314 /*
3315 * Affix has a non-word character and isn't prepended to
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003316 * leader or appended to addition. Need to use another
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003317 * word with a leadstring and/or addstring.
3318 */
3319 if (gap == &bw->bw_suffix || ae->ae_add_nw == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003320 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003321 /* Suffix or prefix with only non-word chars.
3322 * Build the new basic word in "nword": Remove chop
3323 * string and append/prepend addition. */
3324 if (gap == &bw->bw_suffix)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003325 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003326 /* suffix goes at the end of the word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003327 STRCPY(nword, dw->dw_word);
3328 if (ae->ae_chop != NULL)
3329 {
3330 /* Remove chop string. */
3331 p = nword + STRLEN(nword);
3332 for (i = mb_charlen(ae->ae_chop); i > 0; --i)
3333 mb_ptr_back(nword, p);
3334 *p = NUL;
3335 }
3336 STRCAT(nword, ae->ae_add);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003337 agap = &suffixga;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003338 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003339 else
Bram Moolenaar5482f332005-04-17 20:18:43 +00003340 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003341 /* prefix goes before the word */
Bram Moolenaar5482f332005-04-17 20:18:43 +00003342 STRCPY(nword, ae->ae_add);
3343 p = dw->dw_word;
3344 if (ae->ae_chop != NULL)
3345 /* Skip chop string. */
3346 for (i = mb_charlen(ae->ae_chop); i > 0; --i)
3347 mb_ptr_adv( p);
3348 STRCAT(nword, p);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003349 agap = &prefixga;
3350 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003351
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003352 /* Create a basicword_T from the word. */
3353 nbw = get_basicword(nword, 1);
3354 if (nbw != NULL)
3355 {
3356 nbw->bw_region = bw->bw_region;
3357 nbw->bw_flags |= bw->bw_flags
3358 & ~(BWF_ONECAP | BWF_ALLCAP | BWF_KEEPCAP);
Bram Moolenaar5482f332005-04-17 20:18:43 +00003359
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003360 if (STRCMP(bw->bw_word, nbw->bw_word) != 0)
3361 /* Basic word differs, add new word entry. */
3362 (void)add_to_wordlist(newwords, nbw);
Bram Moolenaar5482f332005-04-17 20:18:43 +00003363 else
3364 {
3365 /* Basic word is the same, link "nbw" after
3366 * "bw". */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003367 nbw->bw_next = bw->bw_next;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003368 bw->bw_next = nbw;
3369 }
3370
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003371 /* Remember this word, we need to set bw_prefix
3372 * or bw_suffix later. */
3373 if (ga_grow(agap, 1) == OK)
3374 ((basicword_T **)agap->ga_data)
3375 [agap->ga_len++] = nbw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003376 }
3377 }
3378 else
3379 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003380 /* Prefix with both non-word and word characters: Turn
3381 * prefix into basic word, original word becomes an
3382 * addstring. */
3383
3384 /* Fold-case the word characters in the prefix into
3385 * nword[]. */
3386 alen = 0;
3387 for (p = ae->ae_add_pw; p < ae->ae_add_nw; p += n)
3388 {
3389#ifdef FEAT_MBYTE
3390 n = (*mb_ptr2len_check)(p);
3391#else
3392 n = 1;
3393#endif
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003394 (void)spell_casefold(p, n, nword + alen,
Bram Moolenaar5482f332005-04-17 20:18:43 +00003395 MAXWLEN - alen);
3396 alen += STRLEN(nword + alen);
3397 }
3398
3399 /* Allocate a new word entry. */
3400 nbw = (basicword_T *)alloc((unsigned)(
3401 sizeof(basicword_T) + alen + 1));
3402 if (nbw != NULL)
3403 {
3404 *nbw = *bw;
3405 ga_init2(&nbw->bw_prefix, sizeof(short_u), 1);
3406 ga_init2(&nbw->bw_suffix, sizeof(short_u), 1);
3407
3408 mch_memmove(nbw->bw_word, nword, alen);
3409 nbw->bw_word[alen] = NUL;
3410
3411 /* Use the cap type of the prefix. */
3412 alen = ae->ae_add_nw - ae->ae_add_pw;
3413 mch_memmove(nword, ae->ae_add_pw, alen);
3414 nword[alen] = NUL;
3415 flags = captype(nword, nword + STRLEN(nword));
3416 if (flags & BWF_KEEPCAP)
3417 nbw->bw_caseword = vim_strsave(nword);
3418 else
3419 nbw->bw_caseword = NULL;
3420 nbw->bw_flags &= ~(BWF_ONECAP | BWF_ALLCAP
3421 | BWF_KEEPCAP);
3422 nbw->bw_flags |= flags;
3423
3424 /* The addstring is the prefix after the word
3425 * characters, the original word excluding "chop",
3426 * plus any addition. */
3427 STRCPY(nword, ae->ae_add_nw);
3428 p = bw->bw_word;
3429 if (ae->ae_chop != NULL)
3430 p += STRLEN(ae->ae_chop);
3431 STRCAT(nword, p);
3432 if (bw->bw_addstring != NULL)
3433 STRCAT(nword, bw->bw_addstring);
3434 nbw->bw_addstring = vim_strsave(nword);
3435
3436 if (ae->ae_add_pw > ae->ae_add)
3437 nbw->bw_leadstring = vim_strnsave(ae->ae_add,
3438 ae->ae_add_pw - ae->ae_add);
3439 else
3440 nbw->bw_leadstring = NULL;
3441
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003442 (void)add_to_wordlist(newwords, nbw);
Bram Moolenaar5482f332005-04-17 20:18:43 +00003443
3444 /* Remember this word, we need to set bw_suffix
3445 * and bw_suffix later. */
3446 if (ga_grow(&prefixga, 1) == OK)
3447 ((basicword_T **)prefixga.ga_data)
3448 [prefixga.ga_len++] = nbw;
3449 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003450 }
3451 }
3452 else
3453 {
3454 /* Affix applies to this word, add the related affix
3455 * number. But only if it's not there yet. And keep the
3456 * list sorted, so that we can compare it later. */
3457 for (i = 0; i < gap->ga_len; ++i)
3458 {
3459 n = ((short_u *)gap->ga_data)[i];
3460 if (n >= ae->ae_affnr)
3461 {
3462 if (n == ae->ae_affnr)
3463 i = -1;
3464 break;
3465 }
3466 }
3467 if (i >= 0 && ga_grow(gap, 1) == OK)
3468 {
3469 if (i < gap->ga_len)
3470 mch_memmove(((short_u *)gap->ga_data) + i + 1,
3471 ((short_u *)gap->ga_data) + i,
3472 sizeof(short_u) * (gap->ga_len - i));
3473 ((short_u *)gap->ga_data)[i] = ae->ae_affnr;
3474 ++gap->ga_len;
3475 }
3476 }
3477 }
3478 }
3479 }
3480
3481 /*
3482 * For the words that we added for suffixes with non-word characters: Use
3483 * the prefix list of the main word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003484 */
Bram Moolenaar5482f332005-04-17 20:18:43 +00003485 for (i = 0; i < suffixga.ga_len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003486 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003487 nbw = ((basicword_T **)suffixga.ga_data)[i];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003488 if (ga_grow(&nbw->bw_prefix, bw->bw_prefix.ga_len) == OK)
3489 {
3490 mch_memmove(nbw->bw_prefix.ga_data, bw->bw_prefix.ga_data,
3491 bw->bw_prefix.ga_len * sizeof(short_u));
3492 nbw->bw_prefix.ga_len = bw->bw_prefix.ga_len;
3493 }
3494 }
3495
Bram Moolenaar5482f332005-04-17 20:18:43 +00003496 /*
3497 * For the words that we added for prefixes with non-word characters: Use
3498 * the suffix list of the main word.
3499 */
3500 for (i = 0; i < prefixga.ga_len; ++i)
3501 {
3502 nbw = ((basicword_T **)prefixga.ga_data)[i];
3503 if (ga_grow(&nbw->bw_suffix, bw->bw_suffix.ga_len) == OK)
3504 {
3505 mch_memmove(nbw->bw_suffix.ga_data, bw->bw_suffix.ga_data,
3506 bw->bw_suffix.ga_len * sizeof(short_u));
3507 nbw->bw_suffix.ga_len = bw->bw_suffix.ga_len;
3508 }
3509 }
3510
3511 ga_clear(&suffixga);
3512 ga_clear(&prefixga);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003513}
3514
3515/*
3516 * Go over all words in "oldwords" and change the old affix names to the new
3517 * affix numbers, check the conditions, fold case, extract the basic word and
3518 * additions.
3519 */
3520 static int
3521build_wordlist(newwords, oldwords, oldaff, regionmask)
3522 hashtab_T *newwords; /* basicword_T entries */
3523 hashtab_T *oldwords; /* dicword_T entries */
3524 afffile_T *oldaff; /* affixes for "oldwords" */
3525 int regionmask; /* value for bw_region */
3526{
3527 int todo;
3528 hashitem_T *old_hi;
3529 dicword_T *dw;
3530 basicword_T *bw;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003531 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003532
3533 todo = oldwords->ht_used;
3534 for (old_hi = oldwords->ht_array; todo > 0; ++old_hi)
3535 {
3536 if (!HASHITEM_EMPTY(old_hi))
3537 {
3538 --todo;
3539 dw = HI2DW(old_hi);
3540
3541 /* This takes time, print a message now and then. */
Bram Moolenaar5482f332005-04-17 20:18:43 +00003542 if ((todo & 0x3ff) == 0 || todo == (int)oldwords->ht_used - 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003543 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003544 sprintf((char *)message, _("%6d todo - %s"),
3545 todo, dw->dw_word);
3546 msg_start();
3547 msg_outtrans_attr(message, 0);
3548 msg_clr_eos();
3549 msg_didout = FALSE;
3550 msg_col = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003551 out_flush();
3552 ui_breakcheck();
3553 if (got_int)
3554 break;
3555 }
3556
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003557 bw = get_basicword(dw->dw_word, 10);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003558 if (bw == NULL)
3559 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003560 bw->bw_region = regionmask;
3561
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003562 (void)add_to_wordlist(newwords, bw);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003563
3564 /* Deal with any affix names on the old word, translate them
3565 * into affix numbers. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003566 if (dw->dw_affnm != NULL)
3567 trans_affixes(dw, bw, oldaff, newwords);
3568 }
3569 }
3570 if (todo > 0)
3571 return FAIL;
3572 return OK;
3573}
3574
3575/*
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003576 * Get a basicword_T from a word in original case.
3577 * Caller must set bw_region.
3578 * Returns NULL when something fails.
3579 */
3580 static basicword_T *
3581get_basicword(word, asize)
3582 char_u *word;
3583 int asize; /* growsize for affix garray */
3584{
3585 int dwlen;
3586 char_u foldword[MAXLINELEN];
3587 int flags;
3588 int clen;
3589 int leadlen;
3590 char_u *p;
3591 char_u leadstring[MAXLINELEN];
3592 int addlen;
3593 char_u addstring[MAXLINELEN];
3594 char_u *cp = NULL;
3595 int l;
3596 basicword_T *bw;
3597
3598 /* The basic words are always stored with folded case. */
3599 dwlen = STRLEN(word);
3600 (void)spell_casefold(word, dwlen, foldword, MAXLINELEN);
3601 flags = captype(word, word + dwlen);
3602
3603 /* Check for non-word characters before the word. */
3604 clen = 0;
3605 leadlen = 0;
3606 if (!spell_iswordc(foldword))
3607 {
3608 p = foldword;
3609 for (;;)
3610 {
3611 mb_ptr_adv(p);
3612 ++clen;
3613 if (*p == NUL) /* Only non-word chars (bad word!) */
3614 {
3615 if (p_verbose > 0)
3616 smsg((char_u *)_("Warning: word without word characters: \"%s\""),
3617 foldword);
3618 break;
3619 }
3620 if (spell_iswordc(p))
3621 {
3622 /* Move the leader to "leadstring" and remove it from
3623 * "foldword". */
3624 leadlen = p - foldword;
3625 mch_memmove(leadstring, foldword, leadlen);
3626 leadstring[leadlen] = NUL;
3627 mch_memmove(foldword, p, STRLEN(p) + 1);
3628 break;
3629 }
3630 }
3631 }
3632
3633 /* Check for non-word characters after word characters. */
3634 addlen = 0;
3635 for (p = foldword; spell_iswordc(p); mb_ptr_adv(p))
3636 {
3637 if (*p == NUL)
3638 break;
3639 ++clen;
3640 }
3641 if (*p != NUL)
3642 {
3643 /* Move the addition to "addstring" and truncate "foldword". */
3644 if (flags & BWF_KEEPCAP)
3645 {
3646 /* Preserve caps, need to skip the right number of
3647 * characters in the original word (case folding may
3648 * change the byte count). */
3649 l = 0;
3650 for (cp = word; l < clen; mb_ptr_adv(cp))
3651 ++l;
3652 addlen = STRLEN(cp);
3653 mch_memmove(addstring, cp, addlen + 1);
3654 }
3655 else
3656 {
3657 addlen = STRLEN(p);
3658 mch_memmove(addstring, p, addlen + 1);
3659 }
3660 *p = NUL;
3661 }
3662
3663 bw = (basicword_T *)alloc_clear((unsigned)sizeof(basicword_T)
3664 + STRLEN(foldword));
3665 if (bw == NULL)
3666 return NULL;
3667
3668 STRCPY(bw->bw_word, foldword);
3669
3670 if (leadlen > 0)
3671 bw->bw_leadstring = vim_strsave(leadstring);
3672 else
3673 bw->bw_leadstring = NULL;
3674 if (addlen > 0)
3675 bw->bw_addstring = vim_strsave(addstring);
3676 else
3677 bw->bw_addstring = NULL;
3678
3679 if (flags & BWF_KEEPCAP)
3680 {
3681 if (addlen == 0)
3682 /* use the whole word */
3683 bw->bw_caseword = vim_strsave(word + leadlen);
3684 else
3685 /* use only up to the addition */
3686 bw->bw_caseword = vim_strnsave(word + leadlen,
3687 cp - word - leadlen);
3688 }
3689
3690 bw->bw_flags = flags;
3691 ga_init2(&bw->bw_prefix, sizeof(short_u), asize);
3692 ga_init2(&bw->bw_suffix, sizeof(short_u), asize);
3693
3694 return bw;
3695}
3696
3697/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003698 * Go through the list of words and combine the ones that are identical except
3699 * for the region.
3700 */
3701 static void
3702combine_regions(newwords)
3703 hashtab_T *newwords;
3704{
3705 int todo;
3706 hashitem_T *hi;
3707 basicword_T *bw, *nbw, *pbw;
3708
3709 /* Loop over all basic words in the words table. */
3710 todo = newwords->ht_used;
3711 for (hi = newwords->ht_array; todo > 0; ++hi)
3712 {
3713 if (!HASHITEM_EMPTY(hi))
3714 {
3715 --todo;
3716
3717 /* Loop over the list of words for this basic word. Compare with
3718 * each following word in the same list. */
3719 for (bw = HI2BW(hi); bw != NULL; bw = bw->bw_next)
3720 {
3721 pbw = bw;
3722 for (nbw = pbw->bw_next; nbw != NULL; nbw = pbw->bw_next)
3723 {
3724 if (bw->bw_flags == nbw->bw_flags
3725 && (bw->bw_leadstring == NULL)
3726 == (nbw->bw_leadstring == NULL)
3727 && (bw->bw_addstring == NULL)
3728 == (nbw->bw_addstring == NULL)
3729 && ((bw->bw_flags & BWF_KEEPCAP) == 0
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003730 || bw->bw_caseword == NULL
3731 || nbw->bw_caseword == NULL
3732 || STRCMP(bw->bw_caseword,
3733 nbw->bw_caseword) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003734 && (bw->bw_leadstring == NULL
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003735 || STRCMP(bw->bw_leadstring,
3736 nbw->bw_leadstring) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003737 && (bw->bw_addstring == NULL
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003738 || STRCMP(bw->bw_addstring,
3739 nbw->bw_addstring) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003740 && same_affixes(bw, nbw)
3741 )
3742 {
3743 /* Match, combine regions and delete "nbw". */
3744 pbw->bw_next = nbw->bw_next;
3745 bw->bw_region |= nbw->bw_region;
3746 free_basicword(nbw);
3747 }
3748 else
3749 /* No match, continue with next one. */
3750 pbw = nbw;
3751 }
3752 }
3753 }
3754 }
3755}
3756
3757/*
3758 * Return TRUE when the prefixes and suffixes for "bw" and "nbw" are equal.
3759 */
3760 static int
3761same_affixes(bw, nbw)
3762 basicword_T *bw;
3763 basicword_T *nbw;
3764{
3765 return (bw->bw_prefix.ga_len == nbw->bw_prefix.ga_len
3766 && bw->bw_suffix.ga_len == nbw->bw_suffix.ga_len
3767 && (bw->bw_prefix.ga_len == 0
3768 || vim_memcmp(bw->bw_prefix.ga_data,
3769 nbw->bw_prefix.ga_data,
3770 bw->bw_prefix.ga_len * sizeof(short_u)) == 0)
3771 && (bw->bw_suffix.ga_len == 0
3772 || vim_memcmp(bw->bw_suffix.ga_data,
3773 nbw->bw_suffix.ga_data,
3774 bw->bw_suffix.ga_len * sizeof(short_u)) == 0));
3775}
3776
3777/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003778 * For each basic word with additions turn the suffixes into other additions
3779 * and/or new basic words. For each basic word with a leadstring turn the
3780 * prefixes into other leadstrings and/or new basic words.
3781 * The result is that no affixes apply to the additions or leadstring of a
3782 * word.
3783 * This is also needed when a word with an addition has a prefix and the word
3784 * with prefix also exists. E.g., "blurp's/D" (D is prefix "de") and
3785 * "deblurp". "deblurp" would match and no prefix would be tried.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003786 *
3787 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003788 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003789 static int
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003790expand_affixes(newwords, prefgap, suffgap)
3791 hashtab_T *newwords;
3792 garray_T *prefgap;
3793 garray_T *suffgap;
3794{
3795 int todo;
3796 hashitem_T *hi;
3797 basicword_T *bw;
3798 int pi, si;
3799 affentry_T *pae, *sae;
3800 garray_T add_words;
3801 int n;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003802 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003803 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003804
3805 ga_init2(&add_words, sizeof(basicword_T *), 10);
3806
3807 todo = newwords->ht_used;
3808 for (hi = newwords->ht_array; todo > 0; ++hi)
3809 {
3810 if (!HASHITEM_EMPTY(hi))
3811 {
3812 --todo;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003813
3814 /* This takes time, print a message now and then. */
3815 if ((todo & 0x3ff) == 0 || todo == (int)newwords->ht_used - 1)
3816 {
3817 sprintf((char *)message, _("%6d todo - %s"),
3818 todo, HI2BW(hi)->bw_word);
3819 msg_start();
3820 msg_outtrans_attr(message, 0);
3821 msg_clr_eos();
3822 msg_didout = FALSE;
3823 msg_col = 0;
3824 out_flush();
3825 ui_breakcheck();
3826 if (got_int)
3827 break;
3828 }
3829
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003830 for (bw = HI2BW(hi); bw != NULL; bw = bw->bw_next)
3831 {
3832 /*
3833 * Need to fix affixes if there is a leader or addition and
3834 * there are prefixes or suffixes.
3835 */
3836 if ((bw->bw_leadstring != NULL || bw->bw_addstring != NULL)
3837 && (bw->bw_prefix.ga_len != 0
3838 || bw->bw_suffix.ga_len != 0))
3839 {
3840 /* Loop over all prefix numbers, but first without a
3841 * prefix. */
3842 for (pi = -1; pi < bw->bw_prefix.ga_len; ++pi)
3843 {
3844 pae = NULL;
3845 if (pi >= 0)
3846 {
3847 n = ((short_u *)bw->bw_prefix.ga_data)[pi];
3848 pae = ((affheader_T *)prefgap->ga_data + n)
3849 ->ah_first;
3850 }
3851
3852 /* Loop over all entries for prefix "pi". Do it once
3853 * when there is no prefix (pi == -1). */
3854 do
3855 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003856 /* Skip prewords, they don't need to be expanded. */
3857 if (pae == NULL || !pae->ae_preword)
3858 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003859 /* Loop over all suffix numbers. Do without a
3860 * suffix first when there is a prefix. */
3861 for (si = (pi == -1 ? 0 : -1);
3862 si < bw->bw_suffix.ga_len; ++si)
3863 {
3864 sae = NULL;
3865 if (si >= 0)
3866 {
3867 n = ((short_u *)bw->bw_suffix.ga_data)[si];
3868 sae = ((affheader_T *)suffgap->ga_data + n)
3869 ->ah_first;
3870 }
3871
3872 /* Loop over all entries for suffix "si". Do
3873 * it once when there is no suffix (si == -1).
3874 */
3875 do
3876 {
3877 /* Expand the word for this combination of
3878 * prefixes and affixes. */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003879 if (expand_one_aff(bw, &add_words,
3880 pae, sae) == FAIL)
3881 {
3882 retval = FAIL;
3883 goto theend;
3884 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003885
3886 /* Advance to next suffix entry, if there
3887 * is one. */
3888 if (sae != NULL)
3889 sae = sae->ae_next;
3890 } while (sae != NULL);
3891 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003892 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003893
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003894 /* Advance to next prefix entry, if there is one. */
3895 if (pae != NULL)
3896 pae = pae->ae_next;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003897 } while (pae != NULL);
3898 }
3899 }
3900 }
3901 }
3902 }
3903
3904 /*
3905 * Add the new words afterwards, can't change "newwords" while going over
3906 * all its items.
3907 */
3908 for (pi = 0; pi < add_words.ga_len; ++pi)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003909 {
3910 retval = add_to_wordlist(newwords,
3911 ((basicword_T **)add_words.ga_data)[pi]);
3912 if (retval == FAIL)
3913 break;
3914 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003915
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003916theend:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003917 ga_clear(&add_words);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003918 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003919}
3920
3921/*
3922 * Add one word to "add_words" for basic word "bw" with additions, adding
3923 * prefix "pae" and suffix "sae". Either "pae" or "sae" can be NULL.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003924 * Don't do this when not necessary:
3925 * - no leadstring and adding prefix doesn't result in existing word.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003926 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003927 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003928 static int
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003929expand_one_aff(bw, add_words, pae, sae)
3930 basicword_T *bw;
3931 garray_T *add_words;
3932 affentry_T *pae;
3933 affentry_T *sae;
3934{
3935 char_u word[MAXWLEN + 1];
3936 char_u caseword[MAXWLEN + 1];
3937 int l = 0;
3938 int choplen = 0;
3939 int ll;
3940 basicword_T *nbw;
3941
3942 /* Prepend prefix to the basic word if there is a prefix and there is no
3943 * leadstring. */
3944 if (pae != NULL && bw->bw_leadstring == NULL)
3945 {
3946 if (pae->ae_add != NULL)
3947 {
3948 l = STRLEN(pae->ae_add);
3949 mch_memmove(word, pae->ae_add, l);
3950 }
3951 if (pae->ae_chop != NULL)
3952 choplen = STRLEN(pae->ae_chop);
3953 }
3954
3955 /* Copy the body of the word. */
3956 STRCPY(word + l, bw->bw_word + choplen);
3957
3958 /* Do the same for bw_caseword, if it's there. */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003959 if ((bw->bw_flags & BWF_KEEPCAP) && bw->bw_caseword != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003960 {
3961 if (l > 0)
3962 mch_memmove(caseword, pae->ae_add, l);
3963 STRCPY(caseword + l, bw->bw_caseword + choplen);
3964 }
3965
3966 /* Append suffix to the basic word if there is a suffix and there is no
3967 * addstring. */
3968 if (sae != 0 && bw->bw_addstring == NULL)
3969 {
3970 l = STRLEN(word);
3971 if (sae->ae_chop != NULL)
3972 l -= STRLEN(sae->ae_chop);
3973 if (sae->ae_add == NULL)
3974 word[l] = NUL;
3975 else
3976 STRCPY(word + l, sae->ae_add);
3977
3978 if (bw->bw_flags & BWF_KEEPCAP)
3979 {
3980 /* Do the same for the caseword. */
3981 l = STRLEN(caseword);
3982 if (sae->ae_chop != NULL)
3983 l -= STRLEN(sae->ae_chop);
3984 if (sae->ae_add == NULL)
3985 caseword[l] = NUL;
3986 else
3987 STRCPY(caseword + l, sae->ae_add);
3988 }
3989 }
3990
3991 nbw = (basicword_T *)alloc_clear((unsigned)
3992 sizeof(basicword_T) + STRLEN(word));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003993 if (nbw == NULL)
3994 return FAIL;
3995
3996 /* Add the new word to the list of words to be added later. */
3997 if (ga_grow(add_words, 1) == FAIL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003998 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003999 vim_free(nbw);
4000 return FAIL;
4001 }
4002 ((basicword_T **)add_words->ga_data)[add_words->ga_len++] = nbw;
4003
4004 /* Copy the (modified) basic word, flags and region. */
4005 STRCPY(nbw->bw_word, word);
4006 nbw->bw_flags = bw->bw_flags;
4007 nbw->bw_region = bw->bw_region;
4008
4009 /* Set the (modified) caseword. */
4010 if (bw->bw_flags & BWF_KEEPCAP)
4011 nbw->bw_caseword = vim_strsave(caseword);
4012 else
4013 nbw->bw_caseword = NULL;
4014
4015 if (bw->bw_leadstring != NULL)
4016 {
4017 if (pae != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004018 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004019 /* Prepend prefix to leadstring. */
4020 ll = STRLEN(bw->bw_leadstring);
4021 l = choplen = 0;
4022 if (pae->ae_add != NULL)
4023 l = STRLEN(pae->ae_add);
4024 if (pae->ae_chop != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004025 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004026 choplen = STRLEN(pae->ae_chop);
4027 if (choplen > ll) /* TODO: error? */
4028 choplen = ll;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004029 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004030 nbw->bw_leadstring = alloc((unsigned)(ll + l - choplen + 1));
4031 if (nbw->bw_leadstring != NULL)
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004032 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004033 if (l > 0)
4034 mch_memmove(nbw->bw_leadstring, pae->ae_add, l);
4035 STRCPY(nbw->bw_leadstring + l, bw->bw_leadstring + choplen);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004036 }
4037 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004038 else
4039 nbw->bw_leadstring = vim_strsave(bw->bw_leadstring);
4040 }
4041 else if (bw->bw_prefix.ga_len > 0)
4042 {
4043 /* There is no leadstring, copy the list of possible prefixes. */
4044 ga_init2(&nbw->bw_prefix, sizeof(short_u), 1);
4045 if (ga_grow(&nbw->bw_prefix, bw->bw_prefix.ga_len) == OK)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004046 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004047 mch_memmove(nbw->bw_prefix.ga_data, bw->bw_prefix.ga_data,
4048 bw->bw_prefix.ga_len * sizeof(short_u));
4049 nbw->bw_prefix.ga_len = bw->bw_prefix.ga_len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004050 }
4051 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004052
4053 if (bw->bw_addstring != NULL)
4054 {
4055 if (sae != NULL)
4056 {
4057 /* Append suffix to addstring. */
4058 l = STRLEN(bw->bw_addstring);
4059 if (sae->ae_chop != NULL)
4060 {
4061 l -= STRLEN(sae->ae_chop);
4062 if (l < 0) /* TODO: error? */
4063 l = 0;
4064 }
4065 if (sae->ae_add == NULL)
4066 ll = 0;
4067 else
4068 ll = STRLEN(sae->ae_add);
4069 nbw->bw_addstring = alloc((unsigned)(ll + l - choplen + 1));
4070 if (nbw->bw_addstring != NULL)
4071 {
4072 STRCPY(nbw->bw_addstring, bw->bw_addstring);
4073 if (sae->ae_add == NULL)
4074 nbw->bw_addstring[l] = NUL;
4075 else
4076 STRCPY(nbw->bw_addstring + l, sae->ae_add);
4077 }
4078 }
4079 else
4080 nbw->bw_addstring = vim_strsave(bw->bw_addstring);
4081 }
4082
4083 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004084}
4085
4086/*
4087 * Add basicword_T "*bw" to wordlist "newwords".
4088 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004089 static int
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004090add_to_wordlist(newwords, bw)
4091 hashtab_T *newwords;
4092 basicword_T *bw;
4093{
4094 hashitem_T *hi;
4095 basicword_T *bw2;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004096 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004097
4098 hi = hash_find(newwords, bw->bw_word);
4099 if (HASHITEM_EMPTY(hi))
4100 {
4101 /* New entry, add to hashlist. */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004102 retval = hash_add(newwords, bw->bw_word);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004103 bw->bw_next = NULL;
4104 }
4105 else
4106 {
4107 /* Existing entry, append to list of basic words. */
4108 bw2 = HI2BW(hi);
4109 bw->bw_next = bw2->bw_next;
4110 bw2->bw_next = bw;
4111 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004112 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004113}
4114
4115/*
4116 * Write a number to file "fd", MSB first, in "len" bytes.
4117 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004118 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004119put_bytes(fd, nr, len)
4120 FILE *fd;
4121 long_u nr;
4122 int len;
4123{
4124 int i;
4125
4126 for (i = len - 1; i >= 0; --i)
4127 putc((int)(nr >> (i * 8)), fd);
4128}
4129
4130/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004131 * Write affix info. <affitemcnt> <affitem> ...
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004132 */
4133 static void
4134write_affix(fd, ah)
4135 FILE *fd;
4136 affheader_T *ah;
4137{
4138 int i = 0;
4139 affentry_T *ae;
4140 char_u *p;
4141 int round;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004142 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004143
4144 /* Count the number of entries. */
4145 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
4146 ++i;
4147 put_bytes(fd, (long_u)i, 2); /* <affitemcnt> */
4148
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004149 /* <affitem>: <affflags> <affchoplen> <affchop> <affaddlen> <affadd> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004150 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004151 {
4152 flags = ah->ah_combine ? AFF_COMBINE : 0;
4153 if (ae->ae_preword)
4154 flags |= AFF_PREWORD;
4155 fputc(flags, fd); /* <affflags> */
4156
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004157 for (round = 1; round <= 2; ++round)
4158 {
4159 p = round == 1 ? ae->ae_chop : ae->ae_add;
4160 if (p == NULL)
4161 putc(0, fd); /* <affchoplen> / <affaddlen> */
4162 else
4163 {
4164 putc(STRLEN(p), fd); /* <affchoplen> / <affaddlen> */
4165 /* <affchop> / <affadd> */
4166 fwrite(p, STRLEN(p), (size_t)1, fd);
4167 }
4168 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004169 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004170}
4171
4172/*
4173 * Write list of affix NRs: <affixcnt> <affixNR> ...
4174 */
4175 static void
4176write_affixlist(fd, aff, bytes)
4177 FILE *fd;
4178 garray_T *aff;
4179 int bytes;
4180{
4181 int i;
4182
4183 if (aff->ga_len > 0)
4184 {
4185 putc(aff->ga_len, fd); /* <affixcnt> */
4186 for (i = 0; i < aff->ga_len; ++i)
4187 put_bytes(fd, (long_u )((short_u *)aff->ga_data)[i], bytes);
4188 }
4189}
4190
4191/*
4192 * Vim spell file format: <HEADER> <PREFIXLIST> <SUFFIXLIST>
4193 * <SUGGEST> <WORDLIST>
4194 *
4195 * <HEADER>: <fileID> <regioncnt> <regionname> ...
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004196 * <charflagslen> <charflags> <fcharslen> <fchars>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004197 *
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004198 * <fileID> 10 bytes "VIMspell04"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004199 * <regioncnt> 1 byte number of regions following (8 supported)
4200 * <regionname> 2 bytes Region name: ca, au, etc.
4201 * First <regionname> is region 1.
4202 *
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004203 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
4204 * <charflags> N bytes List of flags (first one is for character 128):
4205 * 0x01 word character
4206 * 0x01 upper-case character
4207 * <fcharslen> 2 bytes Number of bytes in <fchars>.
4208 * <fchars> N bytes Folded characters, first one is for character 128.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004209 *
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004210 *
4211 * <PREFIXLIST>: <affcount> <affix> ...
4212 * <SUFFIXLIST>: <affcount> <affix> ...
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004213 * list of possible affixes: prefixes and suffixes.
4214 *
4215 * <affcount> 2 bytes Number of affixes (MSB comes first).
4216 * When more than 256 an affixNR is 2 bytes.
4217 * This is separate for prefixes and suffixes!
4218 * First affixNR is 0.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004219 *
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004220 * <affix>: <affitemcnt> <affitem> ...
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004221 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004222 * <affitemcnt> 2 bytes Number of affixes with this affixNR (MSB first).
4223 *
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004224 * <affitem>: <affflags> <affchoplen> <affchop> <affaddlen> <affadd>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004225 *
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004226 * <affflags> 1 byte 0x01: prefix combines with suffix, AFF_COMBINE
4227 * 0x02: prefix includes word, AFF_PREWORD
4228 * 0x04-0x80: unset
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004229 * <affchoplen> 1 byte Length of <affchop> in bytes.
4230 * <affchop> N bytes To be removed from basic word.
4231 * <affaddlen> 1 byte Length of <affadd> in bytes.
4232 * <affadd> N bytes To be added to basic word.
4233 *
4234 *
4235 * <SUGGEST> : <suggestlen> <more> ...
4236 *
4237 * <suggestlen> 4 bytes Length of <SUGGEST> in bytes, excluding
4238 * <suggestlen>. MSB first.
4239 * <more> To be defined.
4240 *
4241 *
4242 * <WORDLIST>: <wordcount> <worditem> ...
4243 *
4244 * <wordcount> 4 bytes Number of <worditem> following. MSB first.
4245 *
4246 * <worditem>: <nr> <string> <flags> [<flags2>]
4247 * [<caselen> <caseword>]
4248 * [<affixcnt> <affixNR> ...] (prefixes)
4249 * [<affixcnt> <affixNR> ...] (suffixes)
4250 * [<region>]
4251 * [<addcnt> <add> ...]
4252 *
4253 * <nr> i 1 byte Number of bytes copied from previous word.
4254 * <string> N bytes Additional bytes for word, up to byte smaller than
4255 * 0x20 (space).
4256 * Must only contain case-folded word characters.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004257 * <flags> 1 byte 0x01: word is valid without addition, BWF_VALID
4258 * 0x02: has region byte, BWF_REGION
4259 * 0x04: first letter must be upper-case, BWF_ONECAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004260 * 0x08: has suffixes, <affixcnt> and <affixNR> follow
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004261 * BWF_SUFFIX
4262 * 0x10: more flags, <flags2> follows next, BWF_SECOND
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004263 * 0x20-0x80: can't be used, unset
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004264 * <flags2> 1 byte 0x01: has additions, <addcnt> and <add> follow,
4265 * BWF_ADDS
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004266 * 0x02: has prefixes, <affixcnt> and <affixNR> follow
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004267 * BWF_PREFIX
4268 * 0x04: all letters must be upper-case, BWF_ALLCAP
4269 * 0x08: case must match, BWF_KEEPCAP
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004270 * 0x10: has more than 255 additions, <addcnt> is two
4271 * bytes, BWF_ADDS_M
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004272 * 0x10-0x80: unset
4273 * <caselen> 1 byte Length of <caseword>.
4274 * <caseword> N bytes Word with matching case.
4275 * <affixcnt> 1 byte Number of affix NRs following.
4276 * <affixNR> 1 or 2 byte Number of possible affix for this word.
4277 * When using 2 bytes MSB comes first.
4278 * <region> 1 byte Bitmask for regions in which word is valid. When
4279 * omitted it's valid in all regions.
4280 * Lowest bit is for region 1.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004281 * <addcnt> 1 or 2 byte Number of <add> items following.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004282 *
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004283 * <add>: <addflags> <addlen> [<leadlen>] [<copylen>] [<addstring>] [<region>]
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004284 *
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004285 * <addflags> 1 byte 0x01: unset
4286 * 0x02: has region byte, ADD_REGION
4287 * 0x04: first letter must be upper-case, ADD_ONECAP
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004288 * 0x08: unset
4289 * 0x10: has a <leadlen>, ADD_LEADLEN
4290 * 0x20: has a <copylen>, ADD_COPYLEN
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004291 * 0x40: all letters must be upper-case, ADD_ALLCAP
4292 * 0x80: fixed case, <addstring> is the whole word
4293 * with matching case, ADD_KEEPCAP.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004294 * <addlen> 1 byte Length of <addstring> in bytes.
4295 * <leadlen> 1 byte Number of bytes at start of <addstring> that must
4296 * come before the start of the basic word.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004297 * <copylen> 1 byte Number of bytes copied from previous <addstring>.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004298 * <addstring> N bytes Word characters, before/in/after the word.
4299 *
4300 * All text characters are in 'encoding': <affchop>, <affadd>, <string>,
4301 * <caseword>> and <addstring>.
4302 * All other fields are ASCII: <regionname>
4303 * <string> is always case-folded.
4304 */
4305
4306/*
4307 * Write the Vim spell file "fname".
4308 */
4309 static void
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004310write_vim_spell(fname, prefga, suffga, newwords, regcount, regchars, ascii)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004311 char_u *fname;
4312 garray_T *prefga; /* prefixes, affheader_T entries */
4313 garray_T *suffga; /* suffixes, affheader_T entries */
4314 hashtab_T *newwords; /* basic words, basicword_T entries */
4315 int regcount; /* number of regions */
4316 char_u *regchars; /* region names */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004317 int ascii; /* TRUE for ascii spell file */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004318{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004319 winfo_T wif;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004320 garray_T *gap;
4321 hashitem_T *hi;
4322 char_u **wtab;
4323 int todo;
4324 int flags, aflags;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004325 basicword_T *bw, *bwf, *bw2 = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004326 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004327 int round;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004328 garray_T bwga;
4329
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004330 vim_memset(&wif, 0, sizeof(winfo_T));
4331
4332 wif.wif_fd = fopen((char *)fname, "w");
4333 if (wif.wif_fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004334 {
4335 EMSG2(_(e_notopen), fname);
4336 return;
4337 }
4338
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004339 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
4340 * <charflagslen> <charflags> <fcharslen> <fchars> */
4341 fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, wif.wif_fd); /* <fileID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004342
4343 /* write the region names if there is more than one */
4344 if (regcount > 1)
4345 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004346 putc(regcount, wif.wif_fd); /* <regioncnt> <regionname> ... */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004347 fwrite(regchars, (size_t)(regcount * 2), (size_t)1, wif.wif_fd);
4348 wif.wif_regionmask = (1 << regcount) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004349 }
4350 else
4351 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004352 putc(0, wif.wif_fd);
4353 wif.wif_regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004354 }
4355
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004356 /* Write the table with character flags and table for case folding.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004357 * <charflagslen> <charflags> <fcharlen> <fchars>
4358 * Skip this for ASCII, the table may conflict with the one used for
4359 * 'encoding'. */
4360 if (ascii)
4361 {
4362 putc(0, wif.wif_fd);
4363 putc(0, wif.wif_fd);
4364 putc(0, wif.wif_fd);
4365 }
4366 else
4367 write_spell_chartab(wif.wif_fd);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004368
4369 /* <PREFIXLIST>: <affcount> <affix> ...
4370 * <SUFFIXLIST>: <affcount> <affix> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004371 for (round = 1; round <= 2; ++round)
4372 {
4373 gap = round == 1 ? prefga : suffga;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004374 put_bytes(wif.wif_fd, (long_u)gap->ga_len, 2); /* <affcount> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004375
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004376 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004377 write_affix(wif.wif_fd, (affheader_T *)gap->ga_data + i);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004378 }
4379
4380 /* Number of bytes used for affix NR depends on affix count. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004381 wif.wif_prefm = (prefga->ga_len > 256) ? 2 : 1;
4382 wif.wif_suffm = (suffga->ga_len > 256) ? 2 : 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004383
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004384 /* <SUGGEST> : <suggestlen> <more> ...
4385 * TODO. Only write a zero length for now. */
4386 put_bytes(wif.wif_fd, 0L, 4); /* <suggestlen> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004387
4388 /*
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004389 * <WORDLIST>: <wordcount> <worditem> ...
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004390 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004391
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004392 /* number of basic words in 4 bytes */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004393 put_bytes(wif.wif_fd, newwords->ht_used, 4); /* <wordcount> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004394
4395 /*
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004396 * Sort the word list, so that we can copy as many bytes as possible from
4397 * the previous word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004398 */
4399 wtab = (char_u **)alloc((unsigned)(sizeof(char_u *) * newwords->ht_used));
4400 if (wtab != NULL)
4401 {
4402 /* Make a table with pointers to each word. */
4403 todo = newwords->ht_used;
4404 for (hi = newwords->ht_array; todo > 0; ++hi)
4405 if (!HASHITEM_EMPTY(hi))
4406 wtab[--todo] = hi->hi_key;
4407
4408 /* Sort. */
4409 sort_strings(wtab, (int)newwords->ht_used);
4410
4411 /* Now write each basic word to the spell file. */
4412 ga_init2(&bwga, sizeof(basicword_T *), 10);
Bram Moolenaar5482f332005-04-17 20:18:43 +00004413 for (todo = 0; (long_u)todo < newwords->ht_used; ++todo)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004414 {
4415 bwf = KEY2BW(wtab[todo]);
4416
4417 /*
4418 * Reorder the list of basicword_T words: make a list for words
4419 * with the same case-folded word. Put them together for same
4420 * caps (ONECAP, ALLCAP and various KEEPCAP words) and same
4421 * affixes. Each list will then be put as a basic word with
4422 * additions.
4423 * This won't take much space, since the basic word is the same
4424 * every time, only its length is written.
4425 */
4426 bwga.ga_len = 0;
4427 for (bw = bwf; bw != NULL; bw = bw->bw_next)
4428 {
4429 flags = bw->bw_flags & (BWF_ONECAP | BWF_KEEPCAP | BWF_ALLCAP);
4430
4431 /* Go through the lists we found so far. Break when the case
4432 * matches. */
4433 for (i = 0; i < bwga.ga_len; ++i)
4434 {
4435 bw2 = ((basicword_T **)bwga.ga_data)[i];
4436 aflags = bw2->bw_flags & (BWF_ONECAP | BWF_KEEPCAP
4437 | BWF_ALLCAP);
4438 if (flags == aflags
4439 && ((flags & BWF_KEEPCAP) == 0
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004440 || bw->bw_caseword == NULL
4441 || bw2->bw_caseword == NULL
4442 || STRCMP(bw->bw_caseword,
4443 bw2->bw_caseword) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004444 && same_affixes(bw, bw2))
4445 break;
4446 }
4447 if (i == bwga.ga_len)
4448 {
4449 /* No word with similar caps, make a new list. */
4450 if (ga_grow(&bwga, 1) == FAIL)
4451 break;
4452 ((basicword_T **)bwga.ga_data)[i] = bw;
4453 bw->bw_cnext = NULL;
4454 ++bwga.ga_len;
4455 }
4456 else
4457 {
4458 /* Add to list of words with similar caps. */
4459 bw->bw_cnext = bw2->bw_cnext;
4460 bw2->bw_cnext = bw;
4461 }
4462 }
4463
4464 /* Prefer the word with no caps to use as the first basic word.
4465 * At least one without KEEPCAP. */
4466 bw = NULL;
4467 for (i = 0; i < bwga.ga_len; ++i)
4468 {
4469 bw2 = ((basicword_T **)bwga.ga_data)[i];
4470 if (bw == NULL
4471 || (bw2->bw_flags & (BWF_ONECAP | BWF_KEEPCAP
4472 | BWF_ALLCAP)) == 0
4473 || (bw->bw_flags & BWF_KEEPCAP))
4474 bw = bw2;
4475 }
4476
4477 /* Write first basic word. If it's KEEPCAP then we need a word
4478 * without VALID flag first (makes it easier to read the list back
4479 * in). */
4480 if (bw->bw_flags & BWF_KEEPCAP)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004481 write_bword(&wif, bw, TRUE);
4482 write_bword(&wif, bw, FALSE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004483
4484 /* Write other basic words, with different caps. */
4485 for (i = 0; i < bwga.ga_len; ++i)
4486 {
4487 bw2 = ((basicword_T **)bwga.ga_data)[i];
4488 if (bw2 != bw)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004489 write_bword(&wif, bw2, FALSE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004490 }
4491 }
4492
4493 ga_clear(&bwga);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004494 vim_free(wtab);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004495 }
4496
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004497 fclose(wif.wif_fd);
4498
4499 /* Print a few statistics. */
4500 if (wif.wif_addmaxw == NULL)
4501 wif.wif_addmaxw = (char_u *)"";
4502 smsg((char_u *)_("Maximum number of adds on a word: %ld (%s)"),
4503 wif.wif_addmax, wif.wif_addmaxw);
4504 smsg((char_u *)_("Average number of adds on a word: %f"),
4505 (float)wif.wif_acount / (float)wif.wif_wcount);
4506}
4507
4508/*
4509 * Compare two basic words for their <addstring>.
4510 */
4511static int
4512#ifdef __BORLANDC__
4513_RTLENTRYF
4514#endif
4515bw_compare __ARGS((const void *s1, const void *s2));
4516
4517 static int
4518#ifdef __BORLANDC__
4519_RTLENTRYF
4520#endif
4521bw_compare(s1, s2)
4522 const void *s1;
4523 const void *s2;
4524{
4525 basicword_T *bw1 = *(basicword_T **)s1;
4526 basicword_T *bw2 = *(basicword_T **)s2;
4527 int i = 0;
4528
4529 /* compare the leadstrings */
4530 if (bw1->bw_leadstring == NULL)
4531 {
4532 if (bw2->bw_leadstring != NULL)
4533 return 1;
4534 }
4535 else if (bw2->bw_leadstring == NULL)
4536 return -1;
4537 else
4538 i = STRCMP(bw1->bw_leadstring, bw2->bw_leadstring);
4539
4540 if (i == 0)
4541 {
4542 /* leadstrings are identical, compare the addstrings */
4543 if (bw1->bw_addstring == NULL)
4544 {
4545 if (bw2->bw_addstring != NULL)
4546 return 1;
4547 }
4548 else if (bw2->bw_addstring == NULL)
4549 return -1;
4550 else
4551 i = STRCMP(bw1->bw_addstring, bw2->bw_addstring);
4552 }
4553 return i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004554}
4555
4556/*
4557 * Write basic word, followed by any additions.
4558 *
4559 * <worditem>: <nr> <string> <flags> [<flags2>]
4560 * [<caselen> <caseword>]
4561 * [<affixcnt> <affixNR> ...] (prefixes)
4562 * [<affixcnt> <affixNR> ...] (suffixes)
4563 * [<region>]
4564 * [<addcnt> <add> ...]
4565 */
4566 static void
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004567write_bword(wif, bwf, lowcap)
4568 winfo_T *wif; /* info for writing */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004569 basicword_T *bwf;
4570 int lowcap; /* write KEEPKAP word as not-valid */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004571{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004572 FILE *fd = wif->wif_fd;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004573 int flags;
4574 int aflags;
4575 int len;
4576 int leadlen, addlen;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004577 int copylen;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004578 int clen;
4579 int adds = 0;
4580 int i;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004581 int idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004582 basicword_T *bw, *bw2;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004583 basicword_T **wtab;
4584 int count;
4585 int l;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004586
4587 /* Check how many bytes can be copied from the previous word. */
4588 len = STRLEN(bwf->bw_word);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004589 if (wif->wif_prevbw == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004590 clen = 0;
4591 else
4592 for (clen = 0; clen < len
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004593 && wif->wif_prevbw->bw_word[clen] == bwf->bw_word[clen]; ++clen)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004594 ;
4595 putc(clen, fd); /* <nr> */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004596 wif->wif_prevbw = bwf;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004597 /* <string> */
4598 if (len > clen)
4599 fwrite(bwf->bw_word + clen, (size_t)(len - clen), (size_t)1, fd);
4600
4601 /* Try to find a word without additions to use first. */
4602 bw = bwf;
4603 for (bw2 = bwf; bw2 != NULL; bw2 = bw2->bw_cnext)
4604 {
4605 if (bw2->bw_addstring != NULL || bw2->bw_leadstring != NULL)
4606 ++adds;
4607 else
4608 bw = bw2;
4609 }
4610
4611 /* Flags: If there is no leadstring and no addstring the basic word is
4612 * valid, may have prefixes, suffixes and region. */
4613 flags = bw->bw_flags;
4614 if (bw->bw_addstring == NULL && bw->bw_leadstring == NULL)
4615 {
4616 flags |= BWF_VALID;
4617
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004618 /* Flags: add the region byte if the word isn't valid in all
4619 * regions. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004620 if (wif->wif_regionmask != 0 && (bw->bw_region & wif->wif_regionmask)
4621 != wif->wif_regionmask)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004622 flags |= BWF_REGION;
4623 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004624 /* Add the prefix/suffix list if there are prefixes/suffixes. */
4625 if (bw->bw_leadstring == NULL && bw->bw_prefix.ga_len > 0)
4626 flags |= BWF_PREFIX;
4627 if (bw->bw_addstring == NULL && bw->bw_suffix.ga_len > 0)
4628 flags |= BWF_SUFFIX;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004629
4630 /* Flags: may have additions. */
4631 if (adds > 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004632 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004633 flags |= BWF_ADDS;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004634 if (adds >= 256)
4635 flags |= BWF_ADDS_M;
4636 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004637
4638 /* The dummy word before a KEEPCAP word doesn't have any flags, they are
4639 * in the actual word that follows. */
4640 if (lowcap)
4641 flags = 0;
4642
4643 /* Flags: when the upper byte is not used we only write one flags
4644 * byte, if it's used then set an extra flag in the first byte and
4645 * also write the second byte. */
4646 if ((flags & 0xff00) == 0)
4647 putc(flags, fd); /* <flags> */
4648 else
4649 {
4650 putc(flags | BWF_SECOND, fd); /* <flags> */
4651 putc((int)((unsigned)flags >> 8), fd); /* <flags2> */
4652 }
4653
4654 /* First dummy word doesn't need anything but flags. */
4655 if (lowcap)
4656 return;
4657
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004658 if ((flags & BWF_KEEPCAP) && bw->bw_caseword != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004659 {
4660 len = STRLEN(bw->bw_caseword);
4661 putc(len, fd); /* <caselen> */
4662 for (i = 0; i < len; ++i)
4663 putc(bw->bw_caseword[i], fd); /* <caseword> */
4664 }
4665
4666 /* write prefix and suffix lists: <affixcnt> <affixNR> ... */
4667 if (flags & BWF_PREFIX)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004668 write_affixlist(fd, &bw->bw_prefix, wif->wif_prefm);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004669 if (flags & BWF_SUFFIX)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004670 write_affixlist(fd, &bw->bw_suffix, wif->wif_suffm);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004671
4672 if (flags & BWF_REGION)
4673 putc(bw->bw_region, fd); /* <region> */
4674
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004675 ++wif->wif_wcount;
4676
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004677 /*
4678 * Additions.
4679 */
4680 if (adds > 0)
4681 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004682 if (adds >= 256)
4683 put_bytes(fd, (long_u)adds, 2); /* 2 byte <addcnt> */
4684 else
4685 putc(adds, fd); /* 1 byte <addcnt> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004686
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004687 /* statistics */
4688 wif->wif_acount += adds;
4689 if (wif->wif_addmax < adds)
4690 {
4691 wif->wif_addmax = adds;
4692 wif->wif_addmaxw = bw->bw_word;
4693 }
4694
4695 /*
4696 * Sort the list of additions, so that we can copy as many bytes as
4697 * possible from the previous addstring.
4698 */
4699
4700 /* Make a table with pointers to each basic word that has additions. */
4701 wtab = (basicword_T **)alloc((unsigned)(sizeof(basicword_T *) * adds));
4702 if (wtab == NULL)
4703 return;
4704 count = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004705 for (bw = bwf; bw != NULL; bw = bw->bw_cnext)
4706 if (bw->bw_leadstring != NULL || bw->bw_addstring != NULL)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004707 wtab[count++] = bw;
4708
4709 /* Sort. */
4710 qsort((void *)wtab, (size_t)count, sizeof(basicword_T *), bw_compare);
4711
4712 /* Now write each basic word to the spell file. Copy bytes from the
4713 * previous leadstring/addstring if possible. */
4714 bw2 = NULL;
4715 for (idx = 0; idx < count; ++idx)
4716 {
4717 bw = wtab[idx];
4718
4719 /* <add>: <addflags> <addlen> [<leadlen>] [<copylen>]
4720 * [<addstring>] [<region>] */
4721 copylen = 0;
4722 if (bw->bw_leadstring == NULL)
4723 leadlen = 0;
4724 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004725 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004726 leadlen = STRLEN(bw->bw_leadstring);
4727 if (bw2 != NULL && bw2->bw_leadstring != NULL)
4728 for ( ; copylen < leadlen; ++copylen)
4729 if (bw->bw_leadstring[copylen]
4730 != bw2->bw_leadstring[copylen])
4731 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004732 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004733 if (bw->bw_addstring == NULL)
4734 addlen = 0;
4735 else
4736 {
4737 addlen = STRLEN(bw->bw_addstring);
4738 if (bw2 != NULL && copylen == leadlen
4739 && bw2->bw_addstring != NULL)
4740 {
4741 for (i = 0; i < addlen; ++i)
4742 if (bw->bw_addstring[i] != bw2->bw_addstring[i])
4743 break;
4744 copylen += i;
4745 }
4746 }
4747
4748 aflags = 0;
4749 /* Only copy bytes when it's more than one, the length itself
4750 * takes an extra byte. */
4751 if (copylen > 1)
4752 aflags |= ADD_COPYLEN;
4753 else
4754 copylen = 0;
4755
4756 if (bw->bw_flags & BWF_ONECAP)
4757 aflags |= ADD_ONECAP;
4758 if (bw->bw_flags & BWF_ALLCAP)
4759 aflags |= ADD_ALLCAP;
4760 if (bw->bw_flags & BWF_KEEPCAP)
4761 aflags |= ADD_KEEPCAP;
4762 if (wif->wif_regionmask != 0 && (bw->bw_region
4763 & wif->wif_regionmask) != wif->wif_regionmask)
4764 aflags |= ADD_REGION;
4765 if (leadlen > 0)
4766 aflags |= ADD_LEADLEN;
4767 putc(aflags, fd); /* <addflags> */
4768
4769 putc(leadlen + addlen, fd); /* <addlen> */
4770 if (aflags & ADD_LEADLEN)
4771 putc(leadlen, fd); /* <leadlen> */
4772 if (aflags & ADD_COPYLEN)
4773 putc(copylen, fd); /* <copylen> */
4774
4775 /* <addstring> */
4776 if (leadlen > copylen && bw->bw_leadstring != NULL)
4777 fwrite(bw->bw_leadstring + copylen,
4778 (size_t)(leadlen - copylen), (size_t)1, fd);
4779 if (leadlen + addlen > copylen && bw->bw_addstring != NULL)
4780 {
4781 if (copylen >= leadlen)
4782 l = copylen - leadlen;
4783 else
4784 l = 0;
4785 fwrite(bw->bw_addstring + l,
4786 (size_t)(addlen - l), (size_t)1, fd);
4787 }
4788
4789 if (aflags & ADD_REGION)
4790 putc(bw->bw_region, fd); /* <region> */
4791
4792 bw2 = bw;
4793 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004794
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004795 vim_free(wtab);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004796 }
4797}
4798
4799
4800/*
4801 * ":mkspell outfile infile ..."
4802 */
4803 void
4804ex_mkspell(eap)
4805 exarg_T *eap;
4806{
4807 int fcount;
4808 char_u **fnames;
4809 char_u fname[MAXPATHL];
4810 char_u wfname[MAXPATHL];
4811 afffile_T *(afile[8]);
4812 hashtab_T dfile[8];
4813 int i;
4814 int len;
4815 char_u region_name[16];
4816 struct stat st;
4817 int round;
4818 vimconv_T conv;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004819 int ascii = FALSE;
4820 char_u *arg = eap->arg;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004821 int error = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004822
Bram Moolenaar5482f332005-04-17 20:18:43 +00004823 if (STRNCMP(arg, "-ascii", 6) == 0)
4824 {
4825 ascii = TRUE;
4826 arg = skipwhite(arg + 6);
4827 }
4828
4829 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
4830 if (get_arglist_exp(arg, &fcount, &fnames) == FAIL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004831 return;
4832 if (fcount < 2)
4833 EMSG(_(e_invarg)); /* need at least output and input names */
4834 else if (fcount > 9)
4835 EMSG(_("E754: Only up to 8 regions supported"));
4836 else
4837 {
4838 /* Check for overwriting before doing things that may take a lot of
4839 * time. */
Bram Moolenaar5482f332005-04-17 20:18:43 +00004840 sprintf((char *)wfname, "%s.%s.spl", fnames[0],
4841 ascii ? (char_u *)"ascii" : p_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004842 if (!eap->forceit && mch_stat((char *)wfname, &st) >= 0)
4843 {
4844 EMSG(_(e_exists));
4845 goto theend;
4846 }
4847 if (mch_isdir(fnames[0]))
4848 {
4849 EMSG2(_(e_isadir2), fnames[0]);
4850 goto theend;
4851 }
4852
4853 /*
4854 * Init the aff and dic pointers.
4855 * Get the region names if there are more than 2 arguments.
4856 */
4857 for (i = 1; i < fcount; ++i)
4858 {
4859 afile[i - 1] = NULL;
4860 hash_init(&dfile[i - 1]);
4861 if (fcount > 2)
4862 {
4863 len = STRLEN(fnames[i]);
4864 if (STRLEN(gettail(fnames[i])) < 5 || fnames[i][len - 3] != '_')
4865 {
4866 EMSG2(_("E755: Invalid region in %s"), fnames[i]);
4867 goto theend;
4868 }
4869 else
4870 {
4871 region_name[(i - 1) * 2] = TOLOWER_ASC(fnames[i][len - 2]);
4872 region_name[(i - 1) * 2 + 1] =
4873 TOLOWER_ASC(fnames[i][len - 1]);
4874 }
4875 }
4876 }
4877
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004878 /* Clear the char type tables, don't want to use any of the currently
4879 * used spell properties. */
4880 init_spell_chartab();
4881
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004882 /*
4883 * Read all the .aff and .dic files.
4884 * Text is converted to 'encoding'.
4885 */
4886 for (i = 1; i < fcount; ++i)
4887 {
4888 /* Read the .aff file. Will init "conv" based on the "SET" line. */
4889 conv.vc_type = CONV_NONE;
4890 sprintf((char *)fname, "%s.aff", fnames[i]);
Bram Moolenaar5482f332005-04-17 20:18:43 +00004891 if ((afile[i - 1] = spell_read_aff(fname, &conv, ascii)) == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004892 break;
4893
4894 /* Read the .dic file. */
4895 sprintf((char *)fname, "%s.dic", fnames[i]);
Bram Moolenaar5482f332005-04-17 20:18:43 +00004896 if (spell_read_dic(&dfile[i - 1], fname, &conv, ascii) == FAIL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004897 break;
4898
4899 /* Free any conversion stuff. */
4900 convert_setup(&conv, NULL, NULL);
4901 }
4902
4903 /* Process the data when all the files could be read. */
4904 if (i == fcount)
4905 {
4906 garray_T prefga;
4907 garray_T suffga;
4908 garray_T *gap;
4909 hashtab_T newwords;
4910
4911 /*
4912 * Combine all the affixes into one new affix list. This is done
4913 * for prefixes and suffixes separately.
4914 * We need to do this for each region, try to re-use the same
4915 * affixes.
4916 * Since we number the new affix entries, a growarray will do. In
4917 * the affheader_T the ah_key is unused.
4918 */
4919 MSG(_("Combining affixes..."));
4920 out_flush();
4921 for (round = 1; round <= 2; ++round)
4922 {
4923 gap = round == 1 ? &prefga : &suffga;
4924 ga_init2(gap, sizeof(affheader_T), 50);
4925 for (i = 1; i < fcount; ++i)
4926 get_new_aff(round == 1 ? &afile[i - 1]->af_pref
Bram Moolenaar5482f332005-04-17 20:18:43 +00004927 : &afile[i - 1]->af_suff,
4928 gap, round == 1);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004929 }
4930
4931 /*
4932 * Go over all words and:
4933 * - change the old affix names to the new affix numbers
4934 * - check the conditions
4935 * - fold case
4936 * - extract the basic word and additions.
4937 * Do this for each region.
4938 */
4939 MSG(_("Building word list..."));
4940 out_flush();
4941 hash_init(&newwords);
4942
4943 for (i = 1; i < fcount; ++i)
4944 build_wordlist(&newwords, &dfile[i - 1], afile[i - 1],
4945 1 << (i - 1));
4946
4947 if (fcount > 2)
4948 {
4949 /* Combine words for the different regions into one. */
4950 MSG(_("Combining regions..."));
4951 out_flush();
4952 combine_regions(&newwords);
4953 }
4954
4955 /*
4956 * Affixes on a word with additions are clumsy, would require
4957 * inefficient searching. Turn the affixes into additions and/or
4958 * the expanded word.
4959 */
4960 MSG(_("Processing words..."));
4961 out_flush();
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004962 error = expand_affixes(&newwords, &prefga, &suffga) == FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004963
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004964 if (!error)
4965 {
4966 /* Write the info in the spell file. */
4967 smsg((char_u *)_("Writing spell file %s..."), wfname);
4968 out_flush();
4969 write_vim_spell(wfname, &prefga, &suffga, &newwords,
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004970 fcount - 1, region_name, ascii);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004971 MSG(_("Done!"));
4972 out_flush();
4973 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004974
4975 /* Free the allocated stuff. */
4976 free_wordtable(&newwords);
4977 for (round = 1; round <= 2; ++round)
4978 {
4979 gap = round == 1 ? &prefga: &suffga;
4980 for (i = 0; i < gap->ga_len; ++i)
4981 free_affixentries(((affheader_T *)gap->ga_data + i)
4982 ->ah_first);
4983 ga_clear(gap);
4984 }
4985 }
4986
4987 /* Free the .aff and .dic file structures. */
4988 for (i = 1; i < fcount; ++i)
4989 {
4990 if (afile[i - 1] != NULL)
4991 spell_free_aff(afile[i - 1]);
4992 spell_free_dic(&dfile[i - 1]);
4993 }
4994 }
4995
4996theend:
4997 FreeWild(fcount, fnames);
4998}
4999
5000 static void
5001free_wordtable(ht)
5002 hashtab_T *ht;
5003{
5004 int todo;
5005 basicword_T *bw, *nbw;
5006 hashitem_T *hi;
5007
5008 todo = ht->ht_used;
5009 for (hi = ht->ht_array; todo > 0; ++hi)
5010 {
5011 if (!HASHITEM_EMPTY(hi))
5012 {
5013 --todo;
5014 for (bw = HI2BW(hi); bw != NULL; bw = nbw)
5015 {
5016 nbw = bw->bw_next;
5017 free_basicword(bw);
5018 }
5019 }
5020 }
5021}
5022
5023/*
5024 * Free a basicword_T and what it contains.
5025 */
5026 static void
5027free_basicword(bw)
5028 basicword_T *bw;
5029{
5030 ga_clear(&bw->bw_prefix);
5031 ga_clear(&bw->bw_suffix);
5032 vim_free(bw->bw_caseword);
5033 vim_free(bw->bw_leadstring);
5034 vim_free(bw->bw_addstring);
5035 vim_free(bw);
5036}
5037
5038/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00005039 * Free a list of affentry_T and what they contain.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005040 */
5041 static void
5042free_affixentries(first)
5043 affentry_T *first;
5044{
5045 affentry_T *ap, *an;
5046
5047 for (ap = first; ap != NULL; ap = an)
5048 {
5049 an = ap->ae_next;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005050 free_affix_entry(ap);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005051 }
5052}
5053
Bram Moolenaar5482f332005-04-17 20:18:43 +00005054/*
5055 * Free one affentry_T and what it contains.
5056 */
5057 static void
5058free_affix_entry(ap)
5059 affentry_T *ap;
5060{
5061 vim_free(ap->ae_chop);
5062 vim_free(ap->ae_add);
5063 vim_free(ap->ae_cond);
5064 vim_free(ap->ae_prog);
5065 vim_free(ap);
5066}
5067
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005068#endif /* FEAT_MBYTE */
5069
5070#endif /* FEAT_SYN_HL */