blob: e8ac35217c1001f6bd135a584031a46b809559b5 [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));
2463static void write_vim_spell __ARGS((char_u *fname, garray_T *prefga, garray_T *suffga, hashtab_T *newwords, int regcount, char_u *regchars));
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 {
2708 if (fol == NULL || low == NULL || upp == NULL)
2709 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
2710 else
2711 set_spell_chartab(fol, low, upp);
2712
2713 vim_free(fol);
2714 vim_free(low);
2715 vim_free(upp);
2716 }
2717
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002718 vim_free(pc);
2719 fclose(fd);
2720 return aff;
2721}
2722
2723/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00002724 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
2725 * When "s" is NULL FALSE is returned.
2726 */
2727 static int
2728has_non_ascii(s)
2729 char_u *s;
2730{
2731 char_u *p;
2732
2733 if (s != NULL)
2734 for (p = s; *p != NUL; ++p)
2735 if (*p >= 128)
2736 return TRUE;
2737 return FALSE;
2738}
2739
2740/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002741 * Free the structure filled by spell_read_aff().
2742 */
2743 static void
2744spell_free_aff(aff)
2745 afffile_T *aff;
2746{
2747 hashtab_T *ht;
2748 hashitem_T *hi;
2749 int todo;
2750 int i;
2751 repentry_T *rp;
2752 affheader_T *ah;
2753
2754 vim_free(aff->af_enc);
2755 vim_free(aff->af_try);
2756
2757 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
2758 {
2759 todo = ht->ht_used;
2760 for (hi = ht->ht_array; todo > 0; ++hi)
2761 {
2762 if (!HASHITEM_EMPTY(hi))
2763 {
2764 --todo;
2765 ah = HI2AH(hi);
2766 free_affixentries(ah->ah_first);
2767 vim_free(ah);
2768 }
2769 }
2770 if (ht == &aff->af_suff)
2771 break;
2772 }
2773 hash_clear(&aff->af_pref);
2774 hash_clear(&aff->af_suff);
2775
2776 for (i = 0; i < aff->af_rep.ga_len; ++i)
2777 {
2778 rp = ((repentry_T *)aff->af_rep.ga_data) + i;
2779 vim_free(rp->re_from);
2780 vim_free(rp->re_to);
2781 }
2782 ga_clear(&aff->af_rep);
2783
2784 vim_free(aff);
2785}
2786
2787/*
2788 * Read a dictionary ".dic" file.
2789 * Returns OK or FAIL;
2790 * Each entry in the hashtab_T is a dicword_T.
2791 */
2792 static int
Bram Moolenaar5482f332005-04-17 20:18:43 +00002793spell_read_dic(ht, fname, conv, ascii)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002794 hashtab_T *ht;
2795 char_u *fname;
2796 vimconv_T *conv; /* info for encoding conversion */
Bram Moolenaar5482f332005-04-17 20:18:43 +00002797 int ascii; /* only accept ASCII words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002798{
2799 char_u line[MAXLINELEN];
2800 char_u *p;
2801 dicword_T *dw;
2802 char_u *pc;
2803 char_u *w;
2804 int l;
2805 hash_T hash;
2806 hashitem_T *hi;
2807 FILE *fd;
2808 int lnum = 1;
2809
2810 fd = fopen((char *)fname, "r");
2811 if (fd == NULL)
2812 {
2813 EMSG2(_(e_notopen), fname);
2814 return FAIL;
2815 }
2816
2817 smsg((char_u *)_("Reading dictionary file %s..."), fname);
2818 out_flush();
2819
2820 /* Read and ignore the first line: word count. */
2821 (void)vim_fgets(line, MAXLINELEN, fd);
2822 if (!isdigit(*skipwhite(line)))
2823 EMSG2(_("E760: No word count in %s"), fname);
2824
2825 /*
2826 * Read all the lines in the file one by one.
2827 * The words are converted to 'encoding' here, before being added to
2828 * the hashtable.
2829 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002830 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002831 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002832 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002833 ++lnum;
2834
2835 /* Remove CR, LF and white space from end. */
2836 l = STRLEN(line);
2837 while (l > 0 && line[l - 1] <= ' ')
2838 --l;
2839 if (l == 0)
2840 continue; /* empty line */
2841 line[l] = NUL;
2842
2843 /* Find the optional affix names. */
2844 p = vim_strchr(line, '/');
2845 if (p != NULL)
2846 *p++ = NUL;
2847
Bram Moolenaar5482f332005-04-17 20:18:43 +00002848 /* Skip non-ASCII words when "ascii" is TRUE. */
2849 if (ascii && has_non_ascii(line))
2850 continue;
2851
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002852 /* Convert from "SET" to 'encoding' when needed. */
2853 if (conv->vc_type != CONV_NONE)
2854 {
2855 pc = string_convert(conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002856 if (pc == NULL)
2857 {
2858 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
2859 fname, lnum, line);
2860 continue;
2861 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002862 w = pc;
2863 }
2864 else
2865 {
2866 pc = NULL;
2867 w = line;
2868 }
2869
2870 dw = (dicword_T *)alloc_clear((unsigned)sizeof(dicword_T)
2871 + STRLEN(w));
2872 if (dw == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002873 {
2874 vim_free(pc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002875 break;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002876 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002877 STRCPY(dw->dw_word, w);
2878 vim_free(pc);
2879
2880 hash = hash_hash(dw->dw_word);
2881 hi = hash_lookup(ht, dw->dw_word, hash);
2882 if (!HASHITEM_EMPTY(hi))
2883 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
2884 fname, lnum, line);
2885 else
2886 hash_add_item(ht, hi, dw->dw_word, hash);
2887
2888 if (p != NULL)
2889 dw->dw_affnm = vim_strsave(p);
2890 }
2891
2892 fclose(fd);
2893 return OK;
2894}
2895
2896/*
2897 * Free the structure filled by spell_read_dic().
2898 */
2899 static void
2900spell_free_dic(dic)
2901 hashtab_T *dic;
2902{
2903 int todo;
2904 dicword_T *dw;
2905 hashitem_T *hi;
2906
2907 todo = dic->ht_used;
2908 for (hi = dic->ht_array; todo > 0; ++hi)
2909 {
2910 if (!HASHITEM_EMPTY(hi))
2911 {
2912 --todo;
2913 dw = HI2DW(hi);
2914 vim_free(dw->dw_affnm);
2915 vim_free(dw);
2916 }
2917 }
2918 hash_clear(dic);
2919}
2920
2921/*
2922 * Take the affixes read by spell_read_aff() and add them to the new list.
2923 * Attempts to re-use the same number for identical affixes (ignoring the
2924 * condition, since we remove that). That is especially important when using
2925 * multiple regions.
2926 * Returns OK or FAIL;
2927 */
2928 static int
Bram Moolenaar5482f332005-04-17 20:18:43 +00002929get_new_aff(oldaff, gap, prefix)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002930 hashtab_T *oldaff; /* hashtable with affheader_T */
2931 garray_T *gap; /* table with new affixes */
Bram Moolenaar5482f332005-04-17 20:18:43 +00002932 int prefix; /* TRUE when doing prefixes, FALSE for
2933 suffixes */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002934{
2935 int oldtodo;
2936 affheader_T *oldah, *newah, *gapah;
2937 affentry_T *oldae, *newae;
2938 hashitem_T *oldhi;
2939 hashitem_T *hi;
2940 hashtab_T condht; /* conditions already found */
2941 char_u condkey[MAXLINELEN];
2942 int newnr;
2943 int gapnr;
2944 int retval = OK;
2945 char_u *p;
2946 garray_T tga;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00002947 int preword;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002948
2949 /*
2950 * Loop over all the old affix names.
2951 */
2952 oldtodo = oldaff->ht_used;
2953 for (oldhi = oldaff->ht_array; oldtodo > 0 && retval == OK; ++oldhi)
2954 {
2955 if (!HASHITEM_EMPTY(oldhi))
2956 {
2957 --oldtodo;
2958 oldah = (affheader_T *)oldhi->hi_key;
2959
2960 /* Put entries with the same condition under the same new affix
2961 * nr in "tga". Use hashtable "condht" to find them. */
2962 ga_init2(&tga, sizeof(affheader_T), 10);
2963 hash_init(&condht);
2964
2965 /*
2966 * Loop over all affixes with the same name.
2967 * The affixes with the same condition will get the same number,
2968 * since they can be used with the same words.
2969 * 1. build the lists of new affentry_T, with the headers in "tga".
2970 * 2. Check if some of the lists already exist in "gap", re-use
2971 * their number.
2972 * 3. Assign the new numbers to the old affixes.
2973 */
2974
2975 /* 1. build the lists of new affentry_T. */
2976 for (oldae = oldah->ah_first; oldae != NULL && retval == OK;
2977 oldae = oldae->ae_next)
2978 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00002979 preword = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002980 oldae->ae_add_nw = NULL;
Bram Moolenaar5482f332005-04-17 20:18:43 +00002981 oldae->ae_add_pw = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002982 if (oldae->ae_add != NULL)
2983 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00002984 /* Check for non-word characters in the affix. If there
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00002985 * is one a suffix will be turned into an addition, a
2986 * prefix may be turned into a leadstring.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002987 * This is stored with the old affix, that is where
2988 * trans_affixes() will check. */
2989 for (p = oldae->ae_add; *p != NUL; mb_ptr_adv(p))
2990 if (!spell_iswordc(p))
Bram Moolenaar5482f332005-04-17 20:18:43 +00002991 {
2992 oldae->ae_add_nw = p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002993 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00002994 }
2995
2996 if (prefix && oldae->ae_add_nw != NULL)
2997 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00002998 /* If a prefix has non-word characters special
2999 * treatment is necessary:
3000 * - If it has only non-word characters it becomes a
3001 * leadstring.
3002 * - If it has a sequence of word characters followed
3003 * by a non-word char it becomes a "preword": "d'",
3004 * "de-", "d'ai", etc.
3005 * - if it has another mix of word and non-word
3006 * characters the part before the last word char
3007 * becomes a leadstring: "'d", etc.
3008 */
Bram Moolenaar5482f332005-04-17 20:18:43 +00003009 for (p = oldae->ae_add; *p != NUL; mb_ptr_adv(p))
3010 if (spell_iswordc(p))
3011 {
3012 oldae->ae_add_pw = p;
3013 break;
3014 }
3015 if (oldae->ae_add_pw != NULL)
3016 {
3017 /* Mixed prefix, set ae_add_nw to first non-word
3018 * char after ae_add_pw (if there is one). */
3019 oldae->ae_add_nw = NULL;
3020 for ( ; *p != NUL; mb_ptr_adv(p))
3021 if (!spell_iswordc(p))
3022 {
3023 oldae->ae_add_nw = p;
3024 break;
3025 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003026 if (oldae->ae_add_nw != NULL)
3027 {
3028 preword = TRUE;
3029 oldae->ae_add_pw = NULL;
3030 oldae->ae_add_nw = NULL;
3031 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003032 }
3033 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003034 }
3035
3036 if (oldae->ae_cond == NULL)
3037 /* hashtable requires a non-empty key */
3038 STRCPY(condkey, "---");
3039 else
3040 STRCPY(condkey, oldae->ae_cond);
3041
3042 /* Look for an existing list with this name and condition. */
3043 hi = hash_find(&condht, condkey);
3044 if (!HASHITEM_EMPTY(hi))
3045 /* Match with existing affix, use that one. */
3046 newnr = HI2AS(hi)->as_nr;
3047 else
3048 {
3049 /* Add a new affix number. */
3050 newnr = tga.ga_len;
3051 if (ga_grow(&tga, 1) == FAIL)
3052 retval = FAIL;
3053 else
3054 {
3055 newah = ((affheader_T *)tga.ga_data) + newnr;
3056 newah->ah_combine = oldah->ah_combine;
3057 newah->ah_first = NULL;
3058 ++tga.ga_len;
3059
3060 /* Add the new list to the condht hashtable. */
3061 add_affhash(&condht, condkey, newnr);
3062 }
3063 }
3064
3065 /* Add the new affentry_T to the list. */
3066 newah = ((affheader_T *)tga.ga_data) + newnr;
3067 newae = (affentry_T *)alloc_clear((unsigned)sizeof(affentry_T));
3068 if (newae == NULL)
3069 retval = FAIL;
3070 else
3071 {
3072 newae->ae_next = newah->ah_first;
3073 newah->ah_first = newae;
3074 if (oldae->ae_chop == NULL)
3075 newae->ae_chop = NULL;
3076 else
3077 newae->ae_chop = vim_strsave(oldae->ae_chop);
3078 if (oldae->ae_add == NULL)
3079 newae->ae_add = NULL;
3080 else
3081 newae->ae_add = vim_strsave(oldae->ae_add);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003082 newae->ae_preword = preword;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003083
3084 /* The condition is not copied, since the new affix is
3085 * only used for words where the condition matches. */
3086 }
3087 }
3088
3089 /* 2. Check if some of the lists already exist, re-use their
3090 * number. Otherwise add the list to "gap". */
3091 for (newnr = 0; newnr < tga.ga_len; ++newnr)
3092 {
3093 newah = ((affheader_T *)tga.ga_data) + newnr;
3094 for (gapnr = 0; gapnr < gap->ga_len; ++gapnr)
3095 {
3096 gapah = ((affheader_T *)gap->ga_data) + gapnr;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003097 if (newah->ah_combine == gapah->ah_combine
3098 && same_affentries(newah, gapah))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003099 /* Found an existing affheader_T entry with same
3100 * affentry_T list, use its number. */
3101 break;
3102 }
3103
3104 newah->ah_affnr = gapnr;
3105 if (gapnr == gap->ga_len)
3106 {
3107 /* This is a new affentry_T list, add it. */
3108 if (ga_grow(gap, 1) == FAIL)
3109 retval = FAIL;
3110 else
3111 {
3112 *(((affheader_T *)gap->ga_data) + gap->ga_len) = *newah;
3113 ++gap->ga_len;
3114 }
3115 }
3116 else
3117 {
3118 /* free unused affentry_T list */
3119 free_affixentries(newah->ah_first);
3120 }
3121 }
3122
3123 /* 3. Assign the new affix numbers to the old affixes. */
3124 for (oldae = oldah->ah_first; oldae != NULL && retval == OK;
3125 oldae = oldae->ae_next)
3126 {
3127 if (oldae->ae_cond == NULL)
3128 /* hashtable requires a non-empty key */
3129 STRCPY(condkey, "---");
3130 else
3131 STRCPY(condkey, oldae->ae_cond);
3132
3133 /* Look for an existing affix with this name and condition. */
3134 hi = hash_find(&condht, condkey);
3135 if (!HASHITEM_EMPTY(hi))
3136 /* Match with existing affix, use that one. */
3137 newnr = HI2AS(hi)->as_nr;
3138 else
3139 {
3140 EMSG(_(e_internal));
3141 retval = FAIL;
3142 }
3143 newah = ((affheader_T *)tga.ga_data) + newnr;
3144 oldae->ae_affnr = newah->ah_affnr;
3145 }
3146
3147 ga_clear(&tga);
3148 clear_affhash(&condht);
3149 }
3150 }
3151
3152 return retval;
3153}
3154
3155/*
3156 * Return TRUE if the affentry_T lists for "ah1" and "ah2" contain the same
3157 * items, ignoring the order.
3158 * Only compares the chop and add strings, not the condition.
3159 */
3160 static int
3161same_affentries(ah1, ah2)
3162 affheader_T *ah1;
3163 affheader_T *ah2;
3164{
3165 affentry_T *ae1, *ae2;
3166
3167 /* Check the length of the lists first. */
3168 ae2 = ah2->ah_first;
3169 for (ae1 = ah1->ah_first; ae1 != NULL; ae1 = ae1->ae_next)
3170 {
3171 if (ae2 == NULL)
3172 return FALSE; /* "ah1" list is longer */
3173 ae2 = ae2->ae_next;
3174 }
3175 if (ae2 != NULL)
3176 return FALSE; /* "ah2" list is longer */
3177
3178 /* Check that each entry in "ah1" appears in "ah2". */
3179 for (ae1 = ah1->ah_first; ae1 != NULL; ae1 = ae1->ae_next)
3180 {
3181 for (ae2 = ah2->ah_first; ae2 != NULL; ae2 = ae2->ae_next)
3182 {
3183 if ((ae1->ae_chop == NULL) == (ae2->ae_chop == NULL)
3184 && (ae1->ae_add == NULL) == (ae2->ae_add == NULL)
3185 && (ae1->ae_chop == NULL
3186 || STRCMP(ae1->ae_chop, ae2->ae_chop) == 0)
3187 && (ae1->ae_add == NULL
3188 || STRCMP(ae1->ae_add, ae2->ae_add) == 0))
3189 break;
3190 }
3191 if (ae2 == NULL)
3192 return FALSE;
3193 }
3194
3195 return TRUE;
3196}
3197
3198/*
3199 * Add a chop/add or cond hashtable entry.
3200 */
3201 static void
3202add_affhash(ht, key, newnr)
3203 hashtab_T *ht;
3204 char_u *key;
3205 int newnr;
3206{
3207 affhash_T *as;
3208
3209 as = (affhash_T *)alloc((unsigned)sizeof(affhash_T) + STRLEN(key));
3210 if (as != NULL)
3211 {
3212 as->as_nr = newnr;
3213 STRCPY(as->as_word, key);
3214 hash_add(ht, as->as_word);
3215 }
3216}
3217
3218/*
3219 * Clear the chop/add hashtable used to detect identical affixes.
3220 */
3221 static void
3222clear_affhash(ht)
3223 hashtab_T *ht;
3224{
3225 int todo;
3226 hashitem_T *hi;
3227
3228 todo = ht->ht_used;
3229 for (hi = ht->ht_array; todo > 0; ++hi)
3230 {
3231 if (!HASHITEM_EMPTY(hi))
3232 {
3233 --todo;
3234 vim_free(HI2AS(hi));
3235 }
3236 }
3237 hash_clear(ht);
3238}
3239
3240/*
3241 * Translate list of affix names for an old word to affix numbers in a new
3242 * basic word.
3243 * This checks if the conditions match with the old word. The result is that
3244 * the new affix does not need to store the condition.
3245 */
3246 static void
3247trans_affixes(dw, bw, oldaff, newwords)
3248 dicword_T *dw; /* old word */
3249 basicword_T *bw; /* basic word */
3250 afffile_T *oldaff; /* affixes for "oldwords" */
3251 hashtab_T *newwords; /* table with words */
3252{
3253 char_u key[2];
3254 char_u *p;
3255 char_u *affnm;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003256 garray_T *gap, *agap;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003257 hashitem_T *aff_hi;
3258 affheader_T *ah;
3259 affentry_T *ae;
3260 regmatch_T regmatch;
3261 int i;
3262 basicword_T *nbw;
3263 int alen;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003264 garray_T suffixga; /* list of words with non-word suffixes */
3265 garray_T prefixga; /* list of words with non-word prefixes */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003266 char_u nword[MAXWLEN];
3267 int flags;
3268 int n;
3269
Bram Moolenaar5482f332005-04-17 20:18:43 +00003270 ga_init2(&suffixga, (int)sizeof(basicword_T *), 5);
3271 ga_init2(&prefixga, (int)sizeof(basicword_T *), 5);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003272
3273 /* Loop over all the affix names of the old word. */
3274 key[1] = NUL;
3275 for (affnm = dw->dw_affnm; *affnm != NUL; ++affnm)
3276 {
3277 key[0] = *affnm;
3278 aff_hi = hash_find(&oldaff->af_pref, key);
3279 if (!HASHITEM_EMPTY(aff_hi))
3280 gap = &bw->bw_prefix; /* found a prefix */
3281 else
3282 {
3283 gap = &bw->bw_suffix; /* must be a suffix */
3284 aff_hi = hash_find(&oldaff->af_suff, key);
3285 if (HASHITEM_EMPTY(aff_hi))
3286 {
3287 smsg((char_u *)_("No affix entry '%s' for word %s"),
3288 key, dw->dw_word);
3289 continue;
3290 }
3291 }
3292
3293 /* Loop over all the affix entries for this affix name. */
3294 ah = HI2AH(aff_hi);
3295 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3296 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003297 /* Setup for regexp matching. Note that we don't ignore case.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003298 * This is weird, because the rules in an .aff file don't care
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003299 * about case, but it's necessary for compatibility with Myspell.
3300 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003301 regmatch.regprog = ae->ae_prog;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003302 regmatch.rm_ic = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003303 if (ae->ae_prog == NULL
3304 || vim_regexec(&regmatch, dw->dw_word, (colnr_T)0))
3305 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003306 if ((ae->ae_add_nw != NULL || ae->ae_add_pw != NULL)
3307 && (gap != &bw->bw_suffix || bw->bw_addstring == NULL))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003308 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003309 /*
3310 * Affix has a non-word character and isn't prepended to
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003311 * leader or appended to addition. Need to use another
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003312 * word with a leadstring and/or addstring.
3313 */
3314 if (gap == &bw->bw_suffix || ae->ae_add_nw == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003315 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003316 /* Suffix or prefix with only non-word chars.
3317 * Build the new basic word in "nword": Remove chop
3318 * string and append/prepend addition. */
3319 if (gap == &bw->bw_suffix)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003320 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003321 /* suffix goes at the end of the word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003322 STRCPY(nword, dw->dw_word);
3323 if (ae->ae_chop != NULL)
3324 {
3325 /* Remove chop string. */
3326 p = nword + STRLEN(nword);
3327 for (i = mb_charlen(ae->ae_chop); i > 0; --i)
3328 mb_ptr_back(nword, p);
3329 *p = NUL;
3330 }
3331 STRCAT(nword, ae->ae_add);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003332 agap = &suffixga;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003333 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003334 else
Bram Moolenaar5482f332005-04-17 20:18:43 +00003335 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003336 /* prefix goes before the word */
Bram Moolenaar5482f332005-04-17 20:18:43 +00003337 STRCPY(nword, ae->ae_add);
3338 p = dw->dw_word;
3339 if (ae->ae_chop != NULL)
3340 /* Skip chop string. */
3341 for (i = mb_charlen(ae->ae_chop); i > 0; --i)
3342 mb_ptr_adv( p);
3343 STRCAT(nword, p);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003344 agap = &prefixga;
3345 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003346
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003347 /* Create a basicword_T from the word. */
3348 nbw = get_basicword(nword, 1);
3349 if (nbw != NULL)
3350 {
3351 nbw->bw_region = bw->bw_region;
3352 nbw->bw_flags |= bw->bw_flags
3353 & ~(BWF_ONECAP | BWF_ALLCAP | BWF_KEEPCAP);
Bram Moolenaar5482f332005-04-17 20:18:43 +00003354
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003355 if (STRCMP(bw->bw_word, nbw->bw_word) != 0)
3356 /* Basic word differs, add new word entry. */
3357 (void)add_to_wordlist(newwords, nbw);
Bram Moolenaar5482f332005-04-17 20:18:43 +00003358 else
3359 {
3360 /* Basic word is the same, link "nbw" after
3361 * "bw". */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003362 nbw->bw_next = bw->bw_next;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003363 bw->bw_next = nbw;
3364 }
3365
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003366 /* Remember this word, we need to set bw_prefix
3367 * or bw_suffix later. */
3368 if (ga_grow(agap, 1) == OK)
3369 ((basicword_T **)agap->ga_data)
3370 [agap->ga_len++] = nbw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003371 }
3372 }
3373 else
3374 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003375 /* Prefix with both non-word and word characters: Turn
3376 * prefix into basic word, original word becomes an
3377 * addstring. */
3378
3379 /* Fold-case the word characters in the prefix into
3380 * nword[]. */
3381 alen = 0;
3382 for (p = ae->ae_add_pw; p < ae->ae_add_nw; p += n)
3383 {
3384#ifdef FEAT_MBYTE
3385 n = (*mb_ptr2len_check)(p);
3386#else
3387 n = 1;
3388#endif
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003389 (void)spell_casefold(p, n, nword + alen,
Bram Moolenaar5482f332005-04-17 20:18:43 +00003390 MAXWLEN - alen);
3391 alen += STRLEN(nword + alen);
3392 }
3393
3394 /* Allocate a new word entry. */
3395 nbw = (basicword_T *)alloc((unsigned)(
3396 sizeof(basicword_T) + alen + 1));
3397 if (nbw != NULL)
3398 {
3399 *nbw = *bw;
3400 ga_init2(&nbw->bw_prefix, sizeof(short_u), 1);
3401 ga_init2(&nbw->bw_suffix, sizeof(short_u), 1);
3402
3403 mch_memmove(nbw->bw_word, nword, alen);
3404 nbw->bw_word[alen] = NUL;
3405
3406 /* Use the cap type of the prefix. */
3407 alen = ae->ae_add_nw - ae->ae_add_pw;
3408 mch_memmove(nword, ae->ae_add_pw, alen);
3409 nword[alen] = NUL;
3410 flags = captype(nword, nword + STRLEN(nword));
3411 if (flags & BWF_KEEPCAP)
3412 nbw->bw_caseword = vim_strsave(nword);
3413 else
3414 nbw->bw_caseword = NULL;
3415 nbw->bw_flags &= ~(BWF_ONECAP | BWF_ALLCAP
3416 | BWF_KEEPCAP);
3417 nbw->bw_flags |= flags;
3418
3419 /* The addstring is the prefix after the word
3420 * characters, the original word excluding "chop",
3421 * plus any addition. */
3422 STRCPY(nword, ae->ae_add_nw);
3423 p = bw->bw_word;
3424 if (ae->ae_chop != NULL)
3425 p += STRLEN(ae->ae_chop);
3426 STRCAT(nword, p);
3427 if (bw->bw_addstring != NULL)
3428 STRCAT(nword, bw->bw_addstring);
3429 nbw->bw_addstring = vim_strsave(nword);
3430
3431 if (ae->ae_add_pw > ae->ae_add)
3432 nbw->bw_leadstring = vim_strnsave(ae->ae_add,
3433 ae->ae_add_pw - ae->ae_add);
3434 else
3435 nbw->bw_leadstring = NULL;
3436
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003437 (void)add_to_wordlist(newwords, nbw);
Bram Moolenaar5482f332005-04-17 20:18:43 +00003438
3439 /* Remember this word, we need to set bw_suffix
3440 * and bw_suffix later. */
3441 if (ga_grow(&prefixga, 1) == OK)
3442 ((basicword_T **)prefixga.ga_data)
3443 [prefixga.ga_len++] = nbw;
3444 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003445 }
3446 }
3447 else
3448 {
3449 /* Affix applies to this word, add the related affix
3450 * number. But only if it's not there yet. And keep the
3451 * list sorted, so that we can compare it later. */
3452 for (i = 0; i < gap->ga_len; ++i)
3453 {
3454 n = ((short_u *)gap->ga_data)[i];
3455 if (n >= ae->ae_affnr)
3456 {
3457 if (n == ae->ae_affnr)
3458 i = -1;
3459 break;
3460 }
3461 }
3462 if (i >= 0 && ga_grow(gap, 1) == OK)
3463 {
3464 if (i < gap->ga_len)
3465 mch_memmove(((short_u *)gap->ga_data) + i + 1,
3466 ((short_u *)gap->ga_data) + i,
3467 sizeof(short_u) * (gap->ga_len - i));
3468 ((short_u *)gap->ga_data)[i] = ae->ae_affnr;
3469 ++gap->ga_len;
3470 }
3471 }
3472 }
3473 }
3474 }
3475
3476 /*
3477 * For the words that we added for suffixes with non-word characters: Use
3478 * the prefix list of the main word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003479 */
Bram Moolenaar5482f332005-04-17 20:18:43 +00003480 for (i = 0; i < suffixga.ga_len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003481 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003482 nbw = ((basicword_T **)suffixga.ga_data)[i];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003483 if (ga_grow(&nbw->bw_prefix, bw->bw_prefix.ga_len) == OK)
3484 {
3485 mch_memmove(nbw->bw_prefix.ga_data, bw->bw_prefix.ga_data,
3486 bw->bw_prefix.ga_len * sizeof(short_u));
3487 nbw->bw_prefix.ga_len = bw->bw_prefix.ga_len;
3488 }
3489 }
3490
Bram Moolenaar5482f332005-04-17 20:18:43 +00003491 /*
3492 * For the words that we added for prefixes with non-word characters: Use
3493 * the suffix list of the main word.
3494 */
3495 for (i = 0; i < prefixga.ga_len; ++i)
3496 {
3497 nbw = ((basicword_T **)prefixga.ga_data)[i];
3498 if (ga_grow(&nbw->bw_suffix, bw->bw_suffix.ga_len) == OK)
3499 {
3500 mch_memmove(nbw->bw_suffix.ga_data, bw->bw_suffix.ga_data,
3501 bw->bw_suffix.ga_len * sizeof(short_u));
3502 nbw->bw_suffix.ga_len = bw->bw_suffix.ga_len;
3503 }
3504 }
3505
3506 ga_clear(&suffixga);
3507 ga_clear(&prefixga);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003508}
3509
3510/*
3511 * Go over all words in "oldwords" and change the old affix names to the new
3512 * affix numbers, check the conditions, fold case, extract the basic word and
3513 * additions.
3514 */
3515 static int
3516build_wordlist(newwords, oldwords, oldaff, regionmask)
3517 hashtab_T *newwords; /* basicword_T entries */
3518 hashtab_T *oldwords; /* dicword_T entries */
3519 afffile_T *oldaff; /* affixes for "oldwords" */
3520 int regionmask; /* value for bw_region */
3521{
3522 int todo;
3523 hashitem_T *old_hi;
3524 dicword_T *dw;
3525 basicword_T *bw;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003526 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003527
3528 todo = oldwords->ht_used;
3529 for (old_hi = oldwords->ht_array; todo > 0; ++old_hi)
3530 {
3531 if (!HASHITEM_EMPTY(old_hi))
3532 {
3533 --todo;
3534 dw = HI2DW(old_hi);
3535
3536 /* This takes time, print a message now and then. */
Bram Moolenaar5482f332005-04-17 20:18:43 +00003537 if ((todo & 0x3ff) == 0 || todo == (int)oldwords->ht_used - 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003538 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003539 sprintf((char *)message, _("%6d todo - %s"),
3540 todo, dw->dw_word);
3541 msg_start();
3542 msg_outtrans_attr(message, 0);
3543 msg_clr_eos();
3544 msg_didout = FALSE;
3545 msg_col = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003546 out_flush();
3547 ui_breakcheck();
3548 if (got_int)
3549 break;
3550 }
3551
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003552 bw = get_basicword(dw->dw_word, 10);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003553 if (bw == NULL)
3554 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003555 bw->bw_region = regionmask;
3556
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003557 (void)add_to_wordlist(newwords, bw);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003558
3559 /* Deal with any affix names on the old word, translate them
3560 * into affix numbers. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003561 if (dw->dw_affnm != NULL)
3562 trans_affixes(dw, bw, oldaff, newwords);
3563 }
3564 }
3565 if (todo > 0)
3566 return FAIL;
3567 return OK;
3568}
3569
3570/*
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003571 * Get a basicword_T from a word in original case.
3572 * Caller must set bw_region.
3573 * Returns NULL when something fails.
3574 */
3575 static basicword_T *
3576get_basicword(word, asize)
3577 char_u *word;
3578 int asize; /* growsize for affix garray */
3579{
3580 int dwlen;
3581 char_u foldword[MAXLINELEN];
3582 int flags;
3583 int clen;
3584 int leadlen;
3585 char_u *p;
3586 char_u leadstring[MAXLINELEN];
3587 int addlen;
3588 char_u addstring[MAXLINELEN];
3589 char_u *cp = NULL;
3590 int l;
3591 basicword_T *bw;
3592
3593 /* The basic words are always stored with folded case. */
3594 dwlen = STRLEN(word);
3595 (void)spell_casefold(word, dwlen, foldword, MAXLINELEN);
3596 flags = captype(word, word + dwlen);
3597
3598 /* Check for non-word characters before the word. */
3599 clen = 0;
3600 leadlen = 0;
3601 if (!spell_iswordc(foldword))
3602 {
3603 p = foldword;
3604 for (;;)
3605 {
3606 mb_ptr_adv(p);
3607 ++clen;
3608 if (*p == NUL) /* Only non-word chars (bad word!) */
3609 {
3610 if (p_verbose > 0)
3611 smsg((char_u *)_("Warning: word without word characters: \"%s\""),
3612 foldword);
3613 break;
3614 }
3615 if (spell_iswordc(p))
3616 {
3617 /* Move the leader to "leadstring" and remove it from
3618 * "foldword". */
3619 leadlen = p - foldword;
3620 mch_memmove(leadstring, foldword, leadlen);
3621 leadstring[leadlen] = NUL;
3622 mch_memmove(foldword, p, STRLEN(p) + 1);
3623 break;
3624 }
3625 }
3626 }
3627
3628 /* Check for non-word characters after word characters. */
3629 addlen = 0;
3630 for (p = foldword; spell_iswordc(p); mb_ptr_adv(p))
3631 {
3632 if (*p == NUL)
3633 break;
3634 ++clen;
3635 }
3636 if (*p != NUL)
3637 {
3638 /* Move the addition to "addstring" and truncate "foldword". */
3639 if (flags & BWF_KEEPCAP)
3640 {
3641 /* Preserve caps, need to skip the right number of
3642 * characters in the original word (case folding may
3643 * change the byte count). */
3644 l = 0;
3645 for (cp = word; l < clen; mb_ptr_adv(cp))
3646 ++l;
3647 addlen = STRLEN(cp);
3648 mch_memmove(addstring, cp, addlen + 1);
3649 }
3650 else
3651 {
3652 addlen = STRLEN(p);
3653 mch_memmove(addstring, p, addlen + 1);
3654 }
3655 *p = NUL;
3656 }
3657
3658 bw = (basicword_T *)alloc_clear((unsigned)sizeof(basicword_T)
3659 + STRLEN(foldword));
3660 if (bw == NULL)
3661 return NULL;
3662
3663 STRCPY(bw->bw_word, foldword);
3664
3665 if (leadlen > 0)
3666 bw->bw_leadstring = vim_strsave(leadstring);
3667 else
3668 bw->bw_leadstring = NULL;
3669 if (addlen > 0)
3670 bw->bw_addstring = vim_strsave(addstring);
3671 else
3672 bw->bw_addstring = NULL;
3673
3674 if (flags & BWF_KEEPCAP)
3675 {
3676 if (addlen == 0)
3677 /* use the whole word */
3678 bw->bw_caseword = vim_strsave(word + leadlen);
3679 else
3680 /* use only up to the addition */
3681 bw->bw_caseword = vim_strnsave(word + leadlen,
3682 cp - word - leadlen);
3683 }
3684
3685 bw->bw_flags = flags;
3686 ga_init2(&bw->bw_prefix, sizeof(short_u), asize);
3687 ga_init2(&bw->bw_suffix, sizeof(short_u), asize);
3688
3689 return bw;
3690}
3691
3692/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003693 * Go through the list of words and combine the ones that are identical except
3694 * for the region.
3695 */
3696 static void
3697combine_regions(newwords)
3698 hashtab_T *newwords;
3699{
3700 int todo;
3701 hashitem_T *hi;
3702 basicword_T *bw, *nbw, *pbw;
3703
3704 /* Loop over all basic words in the words table. */
3705 todo = newwords->ht_used;
3706 for (hi = newwords->ht_array; todo > 0; ++hi)
3707 {
3708 if (!HASHITEM_EMPTY(hi))
3709 {
3710 --todo;
3711
3712 /* Loop over the list of words for this basic word. Compare with
3713 * each following word in the same list. */
3714 for (bw = HI2BW(hi); bw != NULL; bw = bw->bw_next)
3715 {
3716 pbw = bw;
3717 for (nbw = pbw->bw_next; nbw != NULL; nbw = pbw->bw_next)
3718 {
3719 if (bw->bw_flags == nbw->bw_flags
3720 && (bw->bw_leadstring == NULL)
3721 == (nbw->bw_leadstring == NULL)
3722 && (bw->bw_addstring == NULL)
3723 == (nbw->bw_addstring == NULL)
3724 && ((bw->bw_flags & BWF_KEEPCAP) == 0
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003725 || bw->bw_caseword == NULL
3726 || nbw->bw_caseword == NULL
3727 || STRCMP(bw->bw_caseword,
3728 nbw->bw_caseword) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003729 && (bw->bw_leadstring == NULL
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003730 || STRCMP(bw->bw_leadstring,
3731 nbw->bw_leadstring) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003732 && (bw->bw_addstring == NULL
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003733 || STRCMP(bw->bw_addstring,
3734 nbw->bw_addstring) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003735 && same_affixes(bw, nbw)
3736 )
3737 {
3738 /* Match, combine regions and delete "nbw". */
3739 pbw->bw_next = nbw->bw_next;
3740 bw->bw_region |= nbw->bw_region;
3741 free_basicword(nbw);
3742 }
3743 else
3744 /* No match, continue with next one. */
3745 pbw = nbw;
3746 }
3747 }
3748 }
3749 }
3750}
3751
3752/*
3753 * Return TRUE when the prefixes and suffixes for "bw" and "nbw" are equal.
3754 */
3755 static int
3756same_affixes(bw, nbw)
3757 basicword_T *bw;
3758 basicword_T *nbw;
3759{
3760 return (bw->bw_prefix.ga_len == nbw->bw_prefix.ga_len
3761 && bw->bw_suffix.ga_len == nbw->bw_suffix.ga_len
3762 && (bw->bw_prefix.ga_len == 0
3763 || vim_memcmp(bw->bw_prefix.ga_data,
3764 nbw->bw_prefix.ga_data,
3765 bw->bw_prefix.ga_len * sizeof(short_u)) == 0)
3766 && (bw->bw_suffix.ga_len == 0
3767 || vim_memcmp(bw->bw_suffix.ga_data,
3768 nbw->bw_suffix.ga_data,
3769 bw->bw_suffix.ga_len * sizeof(short_u)) == 0));
3770}
3771
3772/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003773 * For each basic word with additions turn the suffixes into other additions
3774 * and/or new basic words. For each basic word with a leadstring turn the
3775 * prefixes into other leadstrings and/or new basic words.
3776 * The result is that no affixes apply to the additions or leadstring of a
3777 * word.
3778 * This is also needed when a word with an addition has a prefix and the word
3779 * with prefix also exists. E.g., "blurp's/D" (D is prefix "de") and
3780 * "deblurp". "deblurp" would match and no prefix would be tried.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003781 *
3782 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003783 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003784 static int
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003785expand_affixes(newwords, prefgap, suffgap)
3786 hashtab_T *newwords;
3787 garray_T *prefgap;
3788 garray_T *suffgap;
3789{
3790 int todo;
3791 hashitem_T *hi;
3792 basicword_T *bw;
3793 int pi, si;
3794 affentry_T *pae, *sae;
3795 garray_T add_words;
3796 int n;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003797 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003798 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003799
3800 ga_init2(&add_words, sizeof(basicword_T *), 10);
3801
3802 todo = newwords->ht_used;
3803 for (hi = newwords->ht_array; todo > 0; ++hi)
3804 {
3805 if (!HASHITEM_EMPTY(hi))
3806 {
3807 --todo;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003808
3809 /* This takes time, print a message now and then. */
3810 if ((todo & 0x3ff) == 0 || todo == (int)newwords->ht_used - 1)
3811 {
3812 sprintf((char *)message, _("%6d todo - %s"),
3813 todo, HI2BW(hi)->bw_word);
3814 msg_start();
3815 msg_outtrans_attr(message, 0);
3816 msg_clr_eos();
3817 msg_didout = FALSE;
3818 msg_col = 0;
3819 out_flush();
3820 ui_breakcheck();
3821 if (got_int)
3822 break;
3823 }
3824
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003825 for (bw = HI2BW(hi); bw != NULL; bw = bw->bw_next)
3826 {
3827 /*
3828 * Need to fix affixes if there is a leader or addition and
3829 * there are prefixes or suffixes.
3830 */
3831 if ((bw->bw_leadstring != NULL || bw->bw_addstring != NULL)
3832 && (bw->bw_prefix.ga_len != 0
3833 || bw->bw_suffix.ga_len != 0))
3834 {
3835 /* Loop over all prefix numbers, but first without a
3836 * prefix. */
3837 for (pi = -1; pi < bw->bw_prefix.ga_len; ++pi)
3838 {
3839 pae = NULL;
3840 if (pi >= 0)
3841 {
3842 n = ((short_u *)bw->bw_prefix.ga_data)[pi];
3843 pae = ((affheader_T *)prefgap->ga_data + n)
3844 ->ah_first;
3845 }
3846
3847 /* Loop over all entries for prefix "pi". Do it once
3848 * when there is no prefix (pi == -1). */
3849 do
3850 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003851 /* Skip prewords, they don't need to be expanded. */
3852 if (pae == NULL || !pae->ae_preword)
3853 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003854 /* Loop over all suffix numbers. Do without a
3855 * suffix first when there is a prefix. */
3856 for (si = (pi == -1 ? 0 : -1);
3857 si < bw->bw_suffix.ga_len; ++si)
3858 {
3859 sae = NULL;
3860 if (si >= 0)
3861 {
3862 n = ((short_u *)bw->bw_suffix.ga_data)[si];
3863 sae = ((affheader_T *)suffgap->ga_data + n)
3864 ->ah_first;
3865 }
3866
3867 /* Loop over all entries for suffix "si". Do
3868 * it once when there is no suffix (si == -1).
3869 */
3870 do
3871 {
3872 /* Expand the word for this combination of
3873 * prefixes and affixes. */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003874 if (expand_one_aff(bw, &add_words,
3875 pae, sae) == FAIL)
3876 {
3877 retval = FAIL;
3878 goto theend;
3879 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003880
3881 /* Advance to next suffix entry, if there
3882 * is one. */
3883 if (sae != NULL)
3884 sae = sae->ae_next;
3885 } while (sae != NULL);
3886 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003887 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003888
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003889 /* Advance to next prefix entry, if there is one. */
3890 if (pae != NULL)
3891 pae = pae->ae_next;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003892 } while (pae != NULL);
3893 }
3894 }
3895 }
3896 }
3897 }
3898
3899 /*
3900 * Add the new words afterwards, can't change "newwords" while going over
3901 * all its items.
3902 */
3903 for (pi = 0; pi < add_words.ga_len; ++pi)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003904 {
3905 retval = add_to_wordlist(newwords,
3906 ((basicword_T **)add_words.ga_data)[pi]);
3907 if (retval == FAIL)
3908 break;
3909 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003910
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003911theend:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003912 ga_clear(&add_words);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003913 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003914}
3915
3916/*
3917 * Add one word to "add_words" for basic word "bw" with additions, adding
3918 * prefix "pae" and suffix "sae". Either "pae" or "sae" can be NULL.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003919 * Don't do this when not necessary:
3920 * - no leadstring and adding prefix doesn't result in existing word.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003921 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003922 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003923 static int
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003924expand_one_aff(bw, add_words, pae, sae)
3925 basicword_T *bw;
3926 garray_T *add_words;
3927 affentry_T *pae;
3928 affentry_T *sae;
3929{
3930 char_u word[MAXWLEN + 1];
3931 char_u caseword[MAXWLEN + 1];
3932 int l = 0;
3933 int choplen = 0;
3934 int ll;
3935 basicword_T *nbw;
3936
3937 /* Prepend prefix to the basic word if there is a prefix and there is no
3938 * leadstring. */
3939 if (pae != NULL && bw->bw_leadstring == NULL)
3940 {
3941 if (pae->ae_add != NULL)
3942 {
3943 l = STRLEN(pae->ae_add);
3944 mch_memmove(word, pae->ae_add, l);
3945 }
3946 if (pae->ae_chop != NULL)
3947 choplen = STRLEN(pae->ae_chop);
3948 }
3949
3950 /* Copy the body of the word. */
3951 STRCPY(word + l, bw->bw_word + choplen);
3952
3953 /* Do the same for bw_caseword, if it's there. */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003954 if ((bw->bw_flags & BWF_KEEPCAP) && bw->bw_caseword != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003955 {
3956 if (l > 0)
3957 mch_memmove(caseword, pae->ae_add, l);
3958 STRCPY(caseword + l, bw->bw_caseword + choplen);
3959 }
3960
3961 /* Append suffix to the basic word if there is a suffix and there is no
3962 * addstring. */
3963 if (sae != 0 && bw->bw_addstring == NULL)
3964 {
3965 l = STRLEN(word);
3966 if (sae->ae_chop != NULL)
3967 l -= STRLEN(sae->ae_chop);
3968 if (sae->ae_add == NULL)
3969 word[l] = NUL;
3970 else
3971 STRCPY(word + l, sae->ae_add);
3972
3973 if (bw->bw_flags & BWF_KEEPCAP)
3974 {
3975 /* Do the same for the caseword. */
3976 l = STRLEN(caseword);
3977 if (sae->ae_chop != NULL)
3978 l -= STRLEN(sae->ae_chop);
3979 if (sae->ae_add == NULL)
3980 caseword[l] = NUL;
3981 else
3982 STRCPY(caseword + l, sae->ae_add);
3983 }
3984 }
3985
3986 nbw = (basicword_T *)alloc_clear((unsigned)
3987 sizeof(basicword_T) + STRLEN(word));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003988 if (nbw == NULL)
3989 return FAIL;
3990
3991 /* Add the new word to the list of words to be added later. */
3992 if (ga_grow(add_words, 1) == FAIL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003993 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003994 vim_free(nbw);
3995 return FAIL;
3996 }
3997 ((basicword_T **)add_words->ga_data)[add_words->ga_len++] = nbw;
3998
3999 /* Copy the (modified) basic word, flags and region. */
4000 STRCPY(nbw->bw_word, word);
4001 nbw->bw_flags = bw->bw_flags;
4002 nbw->bw_region = bw->bw_region;
4003
4004 /* Set the (modified) caseword. */
4005 if (bw->bw_flags & BWF_KEEPCAP)
4006 nbw->bw_caseword = vim_strsave(caseword);
4007 else
4008 nbw->bw_caseword = NULL;
4009
4010 if (bw->bw_leadstring != NULL)
4011 {
4012 if (pae != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004013 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004014 /* Prepend prefix to leadstring. */
4015 ll = STRLEN(bw->bw_leadstring);
4016 l = choplen = 0;
4017 if (pae->ae_add != NULL)
4018 l = STRLEN(pae->ae_add);
4019 if (pae->ae_chop != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004020 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004021 choplen = STRLEN(pae->ae_chop);
4022 if (choplen > ll) /* TODO: error? */
4023 choplen = ll;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004024 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004025 nbw->bw_leadstring = alloc((unsigned)(ll + l - choplen + 1));
4026 if (nbw->bw_leadstring != NULL)
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004027 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004028 if (l > 0)
4029 mch_memmove(nbw->bw_leadstring, pae->ae_add, l);
4030 STRCPY(nbw->bw_leadstring + l, bw->bw_leadstring + choplen);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004031 }
4032 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004033 else
4034 nbw->bw_leadstring = vim_strsave(bw->bw_leadstring);
4035 }
4036 else if (bw->bw_prefix.ga_len > 0)
4037 {
4038 /* There is no leadstring, copy the list of possible prefixes. */
4039 ga_init2(&nbw->bw_prefix, sizeof(short_u), 1);
4040 if (ga_grow(&nbw->bw_prefix, bw->bw_prefix.ga_len) == OK)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004041 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004042 mch_memmove(nbw->bw_prefix.ga_data, bw->bw_prefix.ga_data,
4043 bw->bw_prefix.ga_len * sizeof(short_u));
4044 nbw->bw_prefix.ga_len = bw->bw_prefix.ga_len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004045 }
4046 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004047
4048 if (bw->bw_addstring != NULL)
4049 {
4050 if (sae != NULL)
4051 {
4052 /* Append suffix to addstring. */
4053 l = STRLEN(bw->bw_addstring);
4054 if (sae->ae_chop != NULL)
4055 {
4056 l -= STRLEN(sae->ae_chop);
4057 if (l < 0) /* TODO: error? */
4058 l = 0;
4059 }
4060 if (sae->ae_add == NULL)
4061 ll = 0;
4062 else
4063 ll = STRLEN(sae->ae_add);
4064 nbw->bw_addstring = alloc((unsigned)(ll + l - choplen + 1));
4065 if (nbw->bw_addstring != NULL)
4066 {
4067 STRCPY(nbw->bw_addstring, bw->bw_addstring);
4068 if (sae->ae_add == NULL)
4069 nbw->bw_addstring[l] = NUL;
4070 else
4071 STRCPY(nbw->bw_addstring + l, sae->ae_add);
4072 }
4073 }
4074 else
4075 nbw->bw_addstring = vim_strsave(bw->bw_addstring);
4076 }
4077
4078 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004079}
4080
4081/*
4082 * Add basicword_T "*bw" to wordlist "newwords".
4083 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004084 static int
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004085add_to_wordlist(newwords, bw)
4086 hashtab_T *newwords;
4087 basicword_T *bw;
4088{
4089 hashitem_T *hi;
4090 basicword_T *bw2;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004091 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004092
4093 hi = hash_find(newwords, bw->bw_word);
4094 if (HASHITEM_EMPTY(hi))
4095 {
4096 /* New entry, add to hashlist. */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004097 retval = hash_add(newwords, bw->bw_word);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004098 bw->bw_next = NULL;
4099 }
4100 else
4101 {
4102 /* Existing entry, append to list of basic words. */
4103 bw2 = HI2BW(hi);
4104 bw->bw_next = bw2->bw_next;
4105 bw2->bw_next = bw;
4106 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004107 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004108}
4109
4110/*
4111 * Write a number to file "fd", MSB first, in "len" bytes.
4112 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004113 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004114put_bytes(fd, nr, len)
4115 FILE *fd;
4116 long_u nr;
4117 int len;
4118{
4119 int i;
4120
4121 for (i = len - 1; i >= 0; --i)
4122 putc((int)(nr >> (i * 8)), fd);
4123}
4124
4125/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004126 * Write affix info. <affitemcnt> <affitem> ...
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004127 */
4128 static void
4129write_affix(fd, ah)
4130 FILE *fd;
4131 affheader_T *ah;
4132{
4133 int i = 0;
4134 affentry_T *ae;
4135 char_u *p;
4136 int round;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004137 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004138
4139 /* Count the number of entries. */
4140 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
4141 ++i;
4142 put_bytes(fd, (long_u)i, 2); /* <affitemcnt> */
4143
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004144 /* <affitem>: <affflags> <affchoplen> <affchop> <affaddlen> <affadd> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004145 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004146 {
4147 flags = ah->ah_combine ? AFF_COMBINE : 0;
4148 if (ae->ae_preword)
4149 flags |= AFF_PREWORD;
4150 fputc(flags, fd); /* <affflags> */
4151
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004152 for (round = 1; round <= 2; ++round)
4153 {
4154 p = round == 1 ? ae->ae_chop : ae->ae_add;
4155 if (p == NULL)
4156 putc(0, fd); /* <affchoplen> / <affaddlen> */
4157 else
4158 {
4159 putc(STRLEN(p), fd); /* <affchoplen> / <affaddlen> */
4160 /* <affchop> / <affadd> */
4161 fwrite(p, STRLEN(p), (size_t)1, fd);
4162 }
4163 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004164 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004165}
4166
4167/*
4168 * Write list of affix NRs: <affixcnt> <affixNR> ...
4169 */
4170 static void
4171write_affixlist(fd, aff, bytes)
4172 FILE *fd;
4173 garray_T *aff;
4174 int bytes;
4175{
4176 int i;
4177
4178 if (aff->ga_len > 0)
4179 {
4180 putc(aff->ga_len, fd); /* <affixcnt> */
4181 for (i = 0; i < aff->ga_len; ++i)
4182 put_bytes(fd, (long_u )((short_u *)aff->ga_data)[i], bytes);
4183 }
4184}
4185
4186/*
4187 * Vim spell file format: <HEADER> <PREFIXLIST> <SUFFIXLIST>
4188 * <SUGGEST> <WORDLIST>
4189 *
4190 * <HEADER>: <fileID> <regioncnt> <regionname> ...
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004191 * <charflagslen> <charflags> <fcharslen> <fchars>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004192 *
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004193 * <fileID> 10 bytes "VIMspell04"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004194 * <regioncnt> 1 byte number of regions following (8 supported)
4195 * <regionname> 2 bytes Region name: ca, au, etc.
4196 * First <regionname> is region 1.
4197 *
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004198 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
4199 * <charflags> N bytes List of flags (first one is for character 128):
4200 * 0x01 word character
4201 * 0x01 upper-case character
4202 * <fcharslen> 2 bytes Number of bytes in <fchars>.
4203 * <fchars> N bytes Folded characters, first one is for character 128.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004204 *
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004205 *
4206 * <PREFIXLIST>: <affcount> <affix> ...
4207 * <SUFFIXLIST>: <affcount> <affix> ...
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004208 * list of possible affixes: prefixes and suffixes.
4209 *
4210 * <affcount> 2 bytes Number of affixes (MSB comes first).
4211 * When more than 256 an affixNR is 2 bytes.
4212 * This is separate for prefixes and suffixes!
4213 * First affixNR is 0.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004214 *
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004215 * <affix>: <affitemcnt> <affitem> ...
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004216 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004217 * <affitemcnt> 2 bytes Number of affixes with this affixNR (MSB first).
4218 *
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004219 * <affitem>: <affflags> <affchoplen> <affchop> <affaddlen> <affadd>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004220 *
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004221 * <affflags> 1 byte 0x01: prefix combines with suffix, AFF_COMBINE
4222 * 0x02: prefix includes word, AFF_PREWORD
4223 * 0x04-0x80: unset
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004224 * <affchoplen> 1 byte Length of <affchop> in bytes.
4225 * <affchop> N bytes To be removed from basic word.
4226 * <affaddlen> 1 byte Length of <affadd> in bytes.
4227 * <affadd> N bytes To be added to basic word.
4228 *
4229 *
4230 * <SUGGEST> : <suggestlen> <more> ...
4231 *
4232 * <suggestlen> 4 bytes Length of <SUGGEST> in bytes, excluding
4233 * <suggestlen>. MSB first.
4234 * <more> To be defined.
4235 *
4236 *
4237 * <WORDLIST>: <wordcount> <worditem> ...
4238 *
4239 * <wordcount> 4 bytes Number of <worditem> following. MSB first.
4240 *
4241 * <worditem>: <nr> <string> <flags> [<flags2>]
4242 * [<caselen> <caseword>]
4243 * [<affixcnt> <affixNR> ...] (prefixes)
4244 * [<affixcnt> <affixNR> ...] (suffixes)
4245 * [<region>]
4246 * [<addcnt> <add> ...]
4247 *
4248 * <nr> i 1 byte Number of bytes copied from previous word.
4249 * <string> N bytes Additional bytes for word, up to byte smaller than
4250 * 0x20 (space).
4251 * Must only contain case-folded word characters.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004252 * <flags> 1 byte 0x01: word is valid without addition, BWF_VALID
4253 * 0x02: has region byte, BWF_REGION
4254 * 0x04: first letter must be upper-case, BWF_ONECAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004255 * 0x08: has suffixes, <affixcnt> and <affixNR> follow
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004256 * BWF_SUFFIX
4257 * 0x10: more flags, <flags2> follows next, BWF_SECOND
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004258 * 0x20-0x80: can't be used, unset
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004259 * <flags2> 1 byte 0x01: has additions, <addcnt> and <add> follow,
4260 * BWF_ADDS
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004261 * 0x02: has prefixes, <affixcnt> and <affixNR> follow
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004262 * BWF_PREFIX
4263 * 0x04: all letters must be upper-case, BWF_ALLCAP
4264 * 0x08: case must match, BWF_KEEPCAP
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004265 * 0x10: has more than 255 additions, <addcnt> is two
4266 * bytes, BWF_ADDS_M
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004267 * 0x10-0x80: unset
4268 * <caselen> 1 byte Length of <caseword>.
4269 * <caseword> N bytes Word with matching case.
4270 * <affixcnt> 1 byte Number of affix NRs following.
4271 * <affixNR> 1 or 2 byte Number of possible affix for this word.
4272 * When using 2 bytes MSB comes first.
4273 * <region> 1 byte Bitmask for regions in which word is valid. When
4274 * omitted it's valid in all regions.
4275 * Lowest bit is for region 1.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004276 * <addcnt> 1 or 2 byte Number of <add> items following.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004277 *
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004278 * <add>: <addflags> <addlen> [<leadlen>] [<copylen>] [<addstring>] [<region>]
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004279 *
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004280 * <addflags> 1 byte 0x01: unset
4281 * 0x02: has region byte, ADD_REGION
4282 * 0x04: first letter must be upper-case, ADD_ONECAP
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004283 * 0x08: unset
4284 * 0x10: has a <leadlen>, ADD_LEADLEN
4285 * 0x20: has a <copylen>, ADD_COPYLEN
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004286 * 0x40: all letters must be upper-case, ADD_ALLCAP
4287 * 0x80: fixed case, <addstring> is the whole word
4288 * with matching case, ADD_KEEPCAP.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004289 * <addlen> 1 byte Length of <addstring> in bytes.
4290 * <leadlen> 1 byte Number of bytes at start of <addstring> that must
4291 * come before the start of the basic word.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004292 * <copylen> 1 byte Number of bytes copied from previous <addstring>.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004293 * <addstring> N bytes Word characters, before/in/after the word.
4294 *
4295 * All text characters are in 'encoding': <affchop>, <affadd>, <string>,
4296 * <caseword>> and <addstring>.
4297 * All other fields are ASCII: <regionname>
4298 * <string> is always case-folded.
4299 */
4300
4301/*
4302 * Write the Vim spell file "fname".
4303 */
4304 static void
4305write_vim_spell(fname, prefga, suffga, newwords, regcount, regchars)
4306 char_u *fname;
4307 garray_T *prefga; /* prefixes, affheader_T entries */
4308 garray_T *suffga; /* suffixes, affheader_T entries */
4309 hashtab_T *newwords; /* basic words, basicword_T entries */
4310 int regcount; /* number of regions */
4311 char_u *regchars; /* region names */
4312{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004313 winfo_T wif;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004314 garray_T *gap;
4315 hashitem_T *hi;
4316 char_u **wtab;
4317 int todo;
4318 int flags, aflags;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004319 basicword_T *bw, *bwf, *bw2 = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004320 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004321 int round;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004322 garray_T bwga;
4323
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004324 vim_memset(&wif, 0, sizeof(winfo_T));
4325
4326 wif.wif_fd = fopen((char *)fname, "w");
4327 if (wif.wif_fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004328 {
4329 EMSG2(_(e_notopen), fname);
4330 return;
4331 }
4332
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004333 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
4334 * <charflagslen> <charflags> <fcharslen> <fchars> */
4335 fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, wif.wif_fd); /* <fileID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004336
4337 /* write the region names if there is more than one */
4338 if (regcount > 1)
4339 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004340 putc(regcount, wif.wif_fd); /* <regioncnt> <regionname> ... */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004341 fwrite(regchars, (size_t)(regcount * 2), (size_t)1, wif.wif_fd);
4342 wif.wif_regionmask = (1 << regcount) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004343 }
4344 else
4345 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004346 putc(0, wif.wif_fd);
4347 wif.wif_regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004348 }
4349
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004350 /* Write the table with character flags and table for case folding.
4351 * <charflagslen> <charflags> <fcharlen> <fchars> */
4352 write_spell_chartab(wif.wif_fd);
4353
4354 /* <PREFIXLIST>: <affcount> <affix> ...
4355 * <SUFFIXLIST>: <affcount> <affix> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004356 for (round = 1; round <= 2; ++round)
4357 {
4358 gap = round == 1 ? prefga : suffga;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004359 put_bytes(wif.wif_fd, (long_u)gap->ga_len, 2); /* <affcount> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004360
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004361 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004362 write_affix(wif.wif_fd, (affheader_T *)gap->ga_data + i);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004363 }
4364
4365 /* Number of bytes used for affix NR depends on affix count. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004366 wif.wif_prefm = (prefga->ga_len > 256) ? 2 : 1;
4367 wif.wif_suffm = (suffga->ga_len > 256) ? 2 : 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004368
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004369 /* <SUGGEST> : <suggestlen> <more> ...
4370 * TODO. Only write a zero length for now. */
4371 put_bytes(wif.wif_fd, 0L, 4); /* <suggestlen> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004372
4373 /*
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004374 * <WORDLIST>: <wordcount> <worditem> ...
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004375 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004376
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004377 /* number of basic words in 4 bytes */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004378 put_bytes(wif.wif_fd, newwords->ht_used, 4); /* <wordcount> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004379
4380 /*
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004381 * Sort the word list, so that we can copy as many bytes as possible from
4382 * the previous word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004383 */
4384 wtab = (char_u **)alloc((unsigned)(sizeof(char_u *) * newwords->ht_used));
4385 if (wtab != NULL)
4386 {
4387 /* Make a table with pointers to each word. */
4388 todo = newwords->ht_used;
4389 for (hi = newwords->ht_array; todo > 0; ++hi)
4390 if (!HASHITEM_EMPTY(hi))
4391 wtab[--todo] = hi->hi_key;
4392
4393 /* Sort. */
4394 sort_strings(wtab, (int)newwords->ht_used);
4395
4396 /* Now write each basic word to the spell file. */
4397 ga_init2(&bwga, sizeof(basicword_T *), 10);
Bram Moolenaar5482f332005-04-17 20:18:43 +00004398 for (todo = 0; (long_u)todo < newwords->ht_used; ++todo)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004399 {
4400 bwf = KEY2BW(wtab[todo]);
4401
4402 /*
4403 * Reorder the list of basicword_T words: make a list for words
4404 * with the same case-folded word. Put them together for same
4405 * caps (ONECAP, ALLCAP and various KEEPCAP words) and same
4406 * affixes. Each list will then be put as a basic word with
4407 * additions.
4408 * This won't take much space, since the basic word is the same
4409 * every time, only its length is written.
4410 */
4411 bwga.ga_len = 0;
4412 for (bw = bwf; bw != NULL; bw = bw->bw_next)
4413 {
4414 flags = bw->bw_flags & (BWF_ONECAP | BWF_KEEPCAP | BWF_ALLCAP);
4415
4416 /* Go through the lists we found so far. Break when the case
4417 * matches. */
4418 for (i = 0; i < bwga.ga_len; ++i)
4419 {
4420 bw2 = ((basicword_T **)bwga.ga_data)[i];
4421 aflags = bw2->bw_flags & (BWF_ONECAP | BWF_KEEPCAP
4422 | BWF_ALLCAP);
4423 if (flags == aflags
4424 && ((flags & BWF_KEEPCAP) == 0
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004425 || bw->bw_caseword == NULL
4426 || bw2->bw_caseword == NULL
4427 || STRCMP(bw->bw_caseword,
4428 bw2->bw_caseword) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004429 && same_affixes(bw, bw2))
4430 break;
4431 }
4432 if (i == bwga.ga_len)
4433 {
4434 /* No word with similar caps, make a new list. */
4435 if (ga_grow(&bwga, 1) == FAIL)
4436 break;
4437 ((basicword_T **)bwga.ga_data)[i] = bw;
4438 bw->bw_cnext = NULL;
4439 ++bwga.ga_len;
4440 }
4441 else
4442 {
4443 /* Add to list of words with similar caps. */
4444 bw->bw_cnext = bw2->bw_cnext;
4445 bw2->bw_cnext = bw;
4446 }
4447 }
4448
4449 /* Prefer the word with no caps to use as the first basic word.
4450 * At least one without KEEPCAP. */
4451 bw = NULL;
4452 for (i = 0; i < bwga.ga_len; ++i)
4453 {
4454 bw2 = ((basicword_T **)bwga.ga_data)[i];
4455 if (bw == NULL
4456 || (bw2->bw_flags & (BWF_ONECAP | BWF_KEEPCAP
4457 | BWF_ALLCAP)) == 0
4458 || (bw->bw_flags & BWF_KEEPCAP))
4459 bw = bw2;
4460 }
4461
4462 /* Write first basic word. If it's KEEPCAP then we need a word
4463 * without VALID flag first (makes it easier to read the list back
4464 * in). */
4465 if (bw->bw_flags & BWF_KEEPCAP)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004466 write_bword(&wif, bw, TRUE);
4467 write_bword(&wif, bw, FALSE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004468
4469 /* Write other basic words, with different caps. */
4470 for (i = 0; i < bwga.ga_len; ++i)
4471 {
4472 bw2 = ((basicword_T **)bwga.ga_data)[i];
4473 if (bw2 != bw)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004474 write_bword(&wif, bw2, FALSE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004475 }
4476 }
4477
4478 ga_clear(&bwga);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004479 vim_free(wtab);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004480 }
4481
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004482 fclose(wif.wif_fd);
4483
4484 /* Print a few statistics. */
4485 if (wif.wif_addmaxw == NULL)
4486 wif.wif_addmaxw = (char_u *)"";
4487 smsg((char_u *)_("Maximum number of adds on a word: %ld (%s)"),
4488 wif.wif_addmax, wif.wif_addmaxw);
4489 smsg((char_u *)_("Average number of adds on a word: %f"),
4490 (float)wif.wif_acount / (float)wif.wif_wcount);
4491}
4492
4493/*
4494 * Compare two basic words for their <addstring>.
4495 */
4496static int
4497#ifdef __BORLANDC__
4498_RTLENTRYF
4499#endif
4500bw_compare __ARGS((const void *s1, const void *s2));
4501
4502 static int
4503#ifdef __BORLANDC__
4504_RTLENTRYF
4505#endif
4506bw_compare(s1, s2)
4507 const void *s1;
4508 const void *s2;
4509{
4510 basicword_T *bw1 = *(basicword_T **)s1;
4511 basicword_T *bw2 = *(basicword_T **)s2;
4512 int i = 0;
4513
4514 /* compare the leadstrings */
4515 if (bw1->bw_leadstring == NULL)
4516 {
4517 if (bw2->bw_leadstring != NULL)
4518 return 1;
4519 }
4520 else if (bw2->bw_leadstring == NULL)
4521 return -1;
4522 else
4523 i = STRCMP(bw1->bw_leadstring, bw2->bw_leadstring);
4524
4525 if (i == 0)
4526 {
4527 /* leadstrings are identical, compare the addstrings */
4528 if (bw1->bw_addstring == NULL)
4529 {
4530 if (bw2->bw_addstring != NULL)
4531 return 1;
4532 }
4533 else if (bw2->bw_addstring == NULL)
4534 return -1;
4535 else
4536 i = STRCMP(bw1->bw_addstring, bw2->bw_addstring);
4537 }
4538 return i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004539}
4540
4541/*
4542 * Write basic word, followed by any additions.
4543 *
4544 * <worditem>: <nr> <string> <flags> [<flags2>]
4545 * [<caselen> <caseword>]
4546 * [<affixcnt> <affixNR> ...] (prefixes)
4547 * [<affixcnt> <affixNR> ...] (suffixes)
4548 * [<region>]
4549 * [<addcnt> <add> ...]
4550 */
4551 static void
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004552write_bword(wif, bwf, lowcap)
4553 winfo_T *wif; /* info for writing */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004554 basicword_T *bwf;
4555 int lowcap; /* write KEEPKAP word as not-valid */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004556{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004557 FILE *fd = wif->wif_fd;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004558 int flags;
4559 int aflags;
4560 int len;
4561 int leadlen, addlen;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004562 int copylen;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004563 int clen;
4564 int adds = 0;
4565 int i;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004566 int idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004567 basicword_T *bw, *bw2;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004568 basicword_T **wtab;
4569 int count;
4570 int l;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004571
4572 /* Check how many bytes can be copied from the previous word. */
4573 len = STRLEN(bwf->bw_word);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004574 if (wif->wif_prevbw == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004575 clen = 0;
4576 else
4577 for (clen = 0; clen < len
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004578 && wif->wif_prevbw->bw_word[clen] == bwf->bw_word[clen]; ++clen)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004579 ;
4580 putc(clen, fd); /* <nr> */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004581 wif->wif_prevbw = bwf;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004582 /* <string> */
4583 if (len > clen)
4584 fwrite(bwf->bw_word + clen, (size_t)(len - clen), (size_t)1, fd);
4585
4586 /* Try to find a word without additions to use first. */
4587 bw = bwf;
4588 for (bw2 = bwf; bw2 != NULL; bw2 = bw2->bw_cnext)
4589 {
4590 if (bw2->bw_addstring != NULL || bw2->bw_leadstring != NULL)
4591 ++adds;
4592 else
4593 bw = bw2;
4594 }
4595
4596 /* Flags: If there is no leadstring and no addstring the basic word is
4597 * valid, may have prefixes, suffixes and region. */
4598 flags = bw->bw_flags;
4599 if (bw->bw_addstring == NULL && bw->bw_leadstring == NULL)
4600 {
4601 flags |= BWF_VALID;
4602
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004603 /* Flags: add the region byte if the word isn't valid in all
4604 * regions. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004605 if (wif->wif_regionmask != 0 && (bw->bw_region & wif->wif_regionmask)
4606 != wif->wif_regionmask)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004607 flags |= BWF_REGION;
4608 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004609 /* Add the prefix/suffix list if there are prefixes/suffixes. */
4610 if (bw->bw_leadstring == NULL && bw->bw_prefix.ga_len > 0)
4611 flags |= BWF_PREFIX;
4612 if (bw->bw_addstring == NULL && bw->bw_suffix.ga_len > 0)
4613 flags |= BWF_SUFFIX;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004614
4615 /* Flags: may have additions. */
4616 if (adds > 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004617 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004618 flags |= BWF_ADDS;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004619 if (adds >= 256)
4620 flags |= BWF_ADDS_M;
4621 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004622
4623 /* The dummy word before a KEEPCAP word doesn't have any flags, they are
4624 * in the actual word that follows. */
4625 if (lowcap)
4626 flags = 0;
4627
4628 /* Flags: when the upper byte is not used we only write one flags
4629 * byte, if it's used then set an extra flag in the first byte and
4630 * also write the second byte. */
4631 if ((flags & 0xff00) == 0)
4632 putc(flags, fd); /* <flags> */
4633 else
4634 {
4635 putc(flags | BWF_SECOND, fd); /* <flags> */
4636 putc((int)((unsigned)flags >> 8), fd); /* <flags2> */
4637 }
4638
4639 /* First dummy word doesn't need anything but flags. */
4640 if (lowcap)
4641 return;
4642
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004643 if ((flags & BWF_KEEPCAP) && bw->bw_caseword != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004644 {
4645 len = STRLEN(bw->bw_caseword);
4646 putc(len, fd); /* <caselen> */
4647 for (i = 0; i < len; ++i)
4648 putc(bw->bw_caseword[i], fd); /* <caseword> */
4649 }
4650
4651 /* write prefix and suffix lists: <affixcnt> <affixNR> ... */
4652 if (flags & BWF_PREFIX)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004653 write_affixlist(fd, &bw->bw_prefix, wif->wif_prefm);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004654 if (flags & BWF_SUFFIX)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004655 write_affixlist(fd, &bw->bw_suffix, wif->wif_suffm);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004656
4657 if (flags & BWF_REGION)
4658 putc(bw->bw_region, fd); /* <region> */
4659
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004660 ++wif->wif_wcount;
4661
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004662 /*
4663 * Additions.
4664 */
4665 if (adds > 0)
4666 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004667 if (adds >= 256)
4668 put_bytes(fd, (long_u)adds, 2); /* 2 byte <addcnt> */
4669 else
4670 putc(adds, fd); /* 1 byte <addcnt> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004671
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004672 /* statistics */
4673 wif->wif_acount += adds;
4674 if (wif->wif_addmax < adds)
4675 {
4676 wif->wif_addmax = adds;
4677 wif->wif_addmaxw = bw->bw_word;
4678 }
4679
4680 /*
4681 * Sort the list of additions, so that we can copy as many bytes as
4682 * possible from the previous addstring.
4683 */
4684
4685 /* Make a table with pointers to each basic word that has additions. */
4686 wtab = (basicword_T **)alloc((unsigned)(sizeof(basicword_T *) * adds));
4687 if (wtab == NULL)
4688 return;
4689 count = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004690 for (bw = bwf; bw != NULL; bw = bw->bw_cnext)
4691 if (bw->bw_leadstring != NULL || bw->bw_addstring != NULL)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004692 wtab[count++] = bw;
4693
4694 /* Sort. */
4695 qsort((void *)wtab, (size_t)count, sizeof(basicword_T *), bw_compare);
4696
4697 /* Now write each basic word to the spell file. Copy bytes from the
4698 * previous leadstring/addstring if possible. */
4699 bw2 = NULL;
4700 for (idx = 0; idx < count; ++idx)
4701 {
4702 bw = wtab[idx];
4703
4704 /* <add>: <addflags> <addlen> [<leadlen>] [<copylen>]
4705 * [<addstring>] [<region>] */
4706 copylen = 0;
4707 if (bw->bw_leadstring == NULL)
4708 leadlen = 0;
4709 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004710 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004711 leadlen = STRLEN(bw->bw_leadstring);
4712 if (bw2 != NULL && bw2->bw_leadstring != NULL)
4713 for ( ; copylen < leadlen; ++copylen)
4714 if (bw->bw_leadstring[copylen]
4715 != bw2->bw_leadstring[copylen])
4716 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004717 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004718 if (bw->bw_addstring == NULL)
4719 addlen = 0;
4720 else
4721 {
4722 addlen = STRLEN(bw->bw_addstring);
4723 if (bw2 != NULL && copylen == leadlen
4724 && bw2->bw_addstring != NULL)
4725 {
4726 for (i = 0; i < addlen; ++i)
4727 if (bw->bw_addstring[i] != bw2->bw_addstring[i])
4728 break;
4729 copylen += i;
4730 }
4731 }
4732
4733 aflags = 0;
4734 /* Only copy bytes when it's more than one, the length itself
4735 * takes an extra byte. */
4736 if (copylen > 1)
4737 aflags |= ADD_COPYLEN;
4738 else
4739 copylen = 0;
4740
4741 if (bw->bw_flags & BWF_ONECAP)
4742 aflags |= ADD_ONECAP;
4743 if (bw->bw_flags & BWF_ALLCAP)
4744 aflags |= ADD_ALLCAP;
4745 if (bw->bw_flags & BWF_KEEPCAP)
4746 aflags |= ADD_KEEPCAP;
4747 if (wif->wif_regionmask != 0 && (bw->bw_region
4748 & wif->wif_regionmask) != wif->wif_regionmask)
4749 aflags |= ADD_REGION;
4750 if (leadlen > 0)
4751 aflags |= ADD_LEADLEN;
4752 putc(aflags, fd); /* <addflags> */
4753
4754 putc(leadlen + addlen, fd); /* <addlen> */
4755 if (aflags & ADD_LEADLEN)
4756 putc(leadlen, fd); /* <leadlen> */
4757 if (aflags & ADD_COPYLEN)
4758 putc(copylen, fd); /* <copylen> */
4759
4760 /* <addstring> */
4761 if (leadlen > copylen && bw->bw_leadstring != NULL)
4762 fwrite(bw->bw_leadstring + copylen,
4763 (size_t)(leadlen - copylen), (size_t)1, fd);
4764 if (leadlen + addlen > copylen && bw->bw_addstring != NULL)
4765 {
4766 if (copylen >= leadlen)
4767 l = copylen - leadlen;
4768 else
4769 l = 0;
4770 fwrite(bw->bw_addstring + l,
4771 (size_t)(addlen - l), (size_t)1, fd);
4772 }
4773
4774 if (aflags & ADD_REGION)
4775 putc(bw->bw_region, fd); /* <region> */
4776
4777 bw2 = bw;
4778 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004779
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004780 vim_free(wtab);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004781 }
4782}
4783
4784
4785/*
4786 * ":mkspell outfile infile ..."
4787 */
4788 void
4789ex_mkspell(eap)
4790 exarg_T *eap;
4791{
4792 int fcount;
4793 char_u **fnames;
4794 char_u fname[MAXPATHL];
4795 char_u wfname[MAXPATHL];
4796 afffile_T *(afile[8]);
4797 hashtab_T dfile[8];
4798 int i;
4799 int len;
4800 char_u region_name[16];
4801 struct stat st;
4802 int round;
4803 vimconv_T conv;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004804 int ascii = FALSE;
4805 char_u *arg = eap->arg;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004806 int error = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004807
Bram Moolenaar5482f332005-04-17 20:18:43 +00004808 if (STRNCMP(arg, "-ascii", 6) == 0)
4809 {
4810 ascii = TRUE;
4811 arg = skipwhite(arg + 6);
4812 }
4813
4814 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
4815 if (get_arglist_exp(arg, &fcount, &fnames) == FAIL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004816 return;
4817 if (fcount < 2)
4818 EMSG(_(e_invarg)); /* need at least output and input names */
4819 else if (fcount > 9)
4820 EMSG(_("E754: Only up to 8 regions supported"));
4821 else
4822 {
4823 /* Check for overwriting before doing things that may take a lot of
4824 * time. */
Bram Moolenaar5482f332005-04-17 20:18:43 +00004825 sprintf((char *)wfname, "%s.%s.spl", fnames[0],
4826 ascii ? (char_u *)"ascii" : p_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004827 if (!eap->forceit && mch_stat((char *)wfname, &st) >= 0)
4828 {
4829 EMSG(_(e_exists));
4830 goto theend;
4831 }
4832 if (mch_isdir(fnames[0]))
4833 {
4834 EMSG2(_(e_isadir2), fnames[0]);
4835 goto theend;
4836 }
4837
4838 /*
4839 * Init the aff and dic pointers.
4840 * Get the region names if there are more than 2 arguments.
4841 */
4842 for (i = 1; i < fcount; ++i)
4843 {
4844 afile[i - 1] = NULL;
4845 hash_init(&dfile[i - 1]);
4846 if (fcount > 2)
4847 {
4848 len = STRLEN(fnames[i]);
4849 if (STRLEN(gettail(fnames[i])) < 5 || fnames[i][len - 3] != '_')
4850 {
4851 EMSG2(_("E755: Invalid region in %s"), fnames[i]);
4852 goto theend;
4853 }
4854 else
4855 {
4856 region_name[(i - 1) * 2] = TOLOWER_ASC(fnames[i][len - 2]);
4857 region_name[(i - 1) * 2 + 1] =
4858 TOLOWER_ASC(fnames[i][len - 1]);
4859 }
4860 }
4861 }
4862
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004863 /* Clear the char type tables, don't want to use any of the currently
4864 * used spell properties. */
4865 init_spell_chartab();
4866
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004867 /*
4868 * Read all the .aff and .dic files.
4869 * Text is converted to 'encoding'.
4870 */
4871 for (i = 1; i < fcount; ++i)
4872 {
4873 /* Read the .aff file. Will init "conv" based on the "SET" line. */
4874 conv.vc_type = CONV_NONE;
4875 sprintf((char *)fname, "%s.aff", fnames[i]);
Bram Moolenaar5482f332005-04-17 20:18:43 +00004876 if ((afile[i - 1] = spell_read_aff(fname, &conv, ascii)) == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004877 break;
4878
4879 /* Read the .dic file. */
4880 sprintf((char *)fname, "%s.dic", fnames[i]);
Bram Moolenaar5482f332005-04-17 20:18:43 +00004881 if (spell_read_dic(&dfile[i - 1], fname, &conv, ascii) == FAIL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004882 break;
4883
4884 /* Free any conversion stuff. */
4885 convert_setup(&conv, NULL, NULL);
4886 }
4887
4888 /* Process the data when all the files could be read. */
4889 if (i == fcount)
4890 {
4891 garray_T prefga;
4892 garray_T suffga;
4893 garray_T *gap;
4894 hashtab_T newwords;
4895
4896 /*
4897 * Combine all the affixes into one new affix list. This is done
4898 * for prefixes and suffixes separately.
4899 * We need to do this for each region, try to re-use the same
4900 * affixes.
4901 * Since we number the new affix entries, a growarray will do. In
4902 * the affheader_T the ah_key is unused.
4903 */
4904 MSG(_("Combining affixes..."));
4905 out_flush();
4906 for (round = 1; round <= 2; ++round)
4907 {
4908 gap = round == 1 ? &prefga : &suffga;
4909 ga_init2(gap, sizeof(affheader_T), 50);
4910 for (i = 1; i < fcount; ++i)
4911 get_new_aff(round == 1 ? &afile[i - 1]->af_pref
Bram Moolenaar5482f332005-04-17 20:18:43 +00004912 : &afile[i - 1]->af_suff,
4913 gap, round == 1);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004914 }
4915
4916 /*
4917 * Go over all words and:
4918 * - change the old affix names to the new affix numbers
4919 * - check the conditions
4920 * - fold case
4921 * - extract the basic word and additions.
4922 * Do this for each region.
4923 */
4924 MSG(_("Building word list..."));
4925 out_flush();
4926 hash_init(&newwords);
4927
4928 for (i = 1; i < fcount; ++i)
4929 build_wordlist(&newwords, &dfile[i - 1], afile[i - 1],
4930 1 << (i - 1));
4931
4932 if (fcount > 2)
4933 {
4934 /* Combine words for the different regions into one. */
4935 MSG(_("Combining regions..."));
4936 out_flush();
4937 combine_regions(&newwords);
4938 }
4939
4940 /*
4941 * Affixes on a word with additions are clumsy, would require
4942 * inefficient searching. Turn the affixes into additions and/or
4943 * the expanded word.
4944 */
4945 MSG(_("Processing words..."));
4946 out_flush();
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004947 error = expand_affixes(&newwords, &prefga, &suffga) == FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004948
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004949 if (!error)
4950 {
4951 /* Write the info in the spell file. */
4952 smsg((char_u *)_("Writing spell file %s..."), wfname);
4953 out_flush();
4954 write_vim_spell(wfname, &prefga, &suffga, &newwords,
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004955 fcount - 1, region_name);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004956 MSG(_("Done!"));
4957 out_flush();
4958 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004959
4960 /* Free the allocated stuff. */
4961 free_wordtable(&newwords);
4962 for (round = 1; round <= 2; ++round)
4963 {
4964 gap = round == 1 ? &prefga: &suffga;
4965 for (i = 0; i < gap->ga_len; ++i)
4966 free_affixentries(((affheader_T *)gap->ga_data + i)
4967 ->ah_first);
4968 ga_clear(gap);
4969 }
4970 }
4971
4972 /* Free the .aff and .dic file structures. */
4973 for (i = 1; i < fcount; ++i)
4974 {
4975 if (afile[i - 1] != NULL)
4976 spell_free_aff(afile[i - 1]);
4977 spell_free_dic(&dfile[i - 1]);
4978 }
4979 }
4980
4981theend:
4982 FreeWild(fcount, fnames);
4983}
4984
4985 static void
4986free_wordtable(ht)
4987 hashtab_T *ht;
4988{
4989 int todo;
4990 basicword_T *bw, *nbw;
4991 hashitem_T *hi;
4992
4993 todo = ht->ht_used;
4994 for (hi = ht->ht_array; todo > 0; ++hi)
4995 {
4996 if (!HASHITEM_EMPTY(hi))
4997 {
4998 --todo;
4999 for (bw = HI2BW(hi); bw != NULL; bw = nbw)
5000 {
5001 nbw = bw->bw_next;
5002 free_basicword(bw);
5003 }
5004 }
5005 }
5006}
5007
5008/*
5009 * Free a basicword_T and what it contains.
5010 */
5011 static void
5012free_basicword(bw)
5013 basicword_T *bw;
5014{
5015 ga_clear(&bw->bw_prefix);
5016 ga_clear(&bw->bw_suffix);
5017 vim_free(bw->bw_caseword);
5018 vim_free(bw->bw_leadstring);
5019 vim_free(bw->bw_addstring);
5020 vim_free(bw);
5021}
5022
5023/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00005024 * Free a list of affentry_T and what they contain.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005025 */
5026 static void
5027free_affixentries(first)
5028 affentry_T *first;
5029{
5030 affentry_T *ap, *an;
5031
5032 for (ap = first; ap != NULL; ap = an)
5033 {
5034 an = ap->ae_next;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005035 free_affix_entry(ap);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005036 }
5037}
5038
Bram Moolenaar5482f332005-04-17 20:18:43 +00005039/*
5040 * Free one affentry_T and what it contains.
5041 */
5042 static void
5043free_affix_entry(ap)
5044 affentry_T *ap;
5045{
5046 vim_free(ap->ae_chop);
5047 vim_free(ap->ae_add);
5048 vim_free(ap->ae_cond);
5049 vim_free(ap->ae_prog);
5050 vim_free(ap);
5051}
5052
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005053#endif /* FEAT_MBYTE */
5054
5055#endif /* FEAT_SYN_HL */