blob: 2a14da3150745a6c67e4b80c8c5b8be0f44f14a9 [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";
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001335 vim_snprintf((char *)fname_enc, sizeof(fname_enc),
1336 "spell/%s.%s.spl", lang, p);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001337
1338 r = do_in_runtimepath(fname_enc, TRUE, spell_load_file, lp);
Bram Moolenaar5482f332005-04-17 20:18:43 +00001339 if (r == FAIL && !lp->sl_error)
1340 {
1341 /* Try loading the ASCII version. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001342 vim_snprintf((char *)fname_enc, sizeof(fname_enc),
1343 "spell/%s.ascii.spl", lang);
Bram Moolenaar5482f332005-04-17 20:18:43 +00001344
1345 r = do_in_runtimepath(fname_enc, TRUE, spell_load_file, lp);
1346 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001347 if (r == FAIL || lp->sl_error)
1348 {
1349 slang_free(lp);
1350 lp = NULL;
1351 if (r == FAIL)
1352 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
1353 fname_enc + 6);
1354 }
1355 else
1356 {
1357 lp->sl_next = first_lang;
1358 first_lang = lp;
1359 }
1360 }
1361
1362 return lp;
1363}
1364
1365/*
1366 * Allocate a new slang_T.
1367 * Caller must fill "sl_next".
1368 */
1369 static slang_T *
1370slang_alloc(lang)
1371 char_u *lang;
1372{
1373 slang_T *lp;
1374
1375 lp = (slang_T *)alloc(sizeof(slang_T));
1376 if (lp != NULL)
1377 {
1378 lp->sl_name = vim_strsave(lang);
1379 hash_init(&lp->sl_words);
1380 ga_init2(&lp->sl_preftab, sizeof(hashtab_T), 4);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001381 hash_init(&lp->sl_prewords);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001382 ga_init2(&lp->sl_sufftab, sizeof(hashtab_T), 4);
1383 lp->sl_prefzero = NULL;
1384 lp->sl_suffzero = NULL;
1385 lp->sl_try = NULL;
1386 ga_init2(&lp->sl_rep, sizeof(repentry_T), 4);
1387 lp->sl_regions[0] = NUL;
1388 lp->sl_block = NULL;
1389 lp->sl_error = FALSE;
1390 }
1391 return lp;
1392}
1393
1394/*
1395 * Free the contents of an slang_T and the structure itself.
1396 */
1397 static void
1398slang_free(lp)
1399 slang_T *lp;
1400{
1401 sblock_T *sp;
1402 int i;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001403 fword_T *fw;
1404 int todo;
1405 hashitem_T *hi;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001406
1407 vim_free(lp->sl_name);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001408
1409 /* The words themselves are in memory blocks referenced by "sl_block".
1410 * Only the hashtables for additions need to be cleared. */
1411 todo = lp->sl_words.ht_used;
1412 for (hi = lp->sl_words.ht_array; todo > 0; ++hi)
1413 {
1414 if (!HASHITEM_EMPTY(hi))
1415 {
1416 --todo;
1417 fw = HI2FWORD(hi);
1418 if (fw->fw_flags & BWF_ADDHASH)
1419 hash_clear((hashtab_T *)fw->fw_adds);
1420 }
1421 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001422 hash_clear(&lp->sl_words);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001423
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001424 for (i = 0; i < lp->sl_preftab.ga_len; ++i)
1425 hash_clear(((hashtab_T *)lp->sl_preftab.ga_data) + i);
1426 ga_clear(&lp->sl_preftab);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001427 hash_clear(&lp->sl_prewords);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001428 for (i = 0; i < lp->sl_sufftab.ga_len; ++i)
1429 hash_clear(((hashtab_T *)lp->sl_sufftab.ga_data) + i);
1430 ga_clear(&lp->sl_sufftab);
1431 ga_clear(&lp->sl_rep);
1432 vim_free(lp->sl_try);
1433 while (lp->sl_block != NULL)
1434 {
1435 sp = lp->sl_block;
1436 lp->sl_block = sp->sb_next;
1437 vim_free(sp);
1438 }
1439 vim_free(lp);
1440}
1441
1442/*
1443 * Load one spell file into an slang_T.
1444 * Invoked through do_in_runtimepath().
1445 */
1446 static void
1447spell_load_file(fname, cookie)
1448 char_u *fname;
1449 void *cookie; /* points to the slang_T to be filled */
1450{
1451 slang_T *lp = cookie;
1452 FILE *fd;
1453 char_u buf[MAXWLEN + 1];
1454 char_u cbuf[MAXWLEN + 1];
1455 char_u fbuf[MAXWLEN + 1];
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001456 char_u affixbuf[256 * 2 * 2]; /* max 2 * 256 affix nrs of 2 bytes */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001457 char_u *p;
1458 int itm;
1459 int i;
1460 int affcount;
1461 int affnr;
1462 int affflags;
1463 int affitemcnt;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001464 int prefixcnt, suffixcnt;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001465 int bl_used = SBLOCKSIZE;
1466 int widx;
Bram Moolenaar5482f332005-04-17 20:18:43 +00001467 int prefm = 0; /* 1 if <= 256 prefixes, sizeof(short_u) otherw. */
1468 int suffm = 0; /* 1 if <= 256 suffixes, sizeof(short_u) otherw. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001469 int wlen;
1470 int flags;
1471 affitem_T *ai, *ai2, **aip;
1472 int round;
1473 char_u *save_sourcing_name = sourcing_name;
1474 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001475 int cnt, ccnt;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001476 int choplen;
1477 int addlen;
1478 int leadlen;
1479 int wordcount;
1480 fword_T *fw, *fw2;
1481 garray_T *gap;
1482 hashtab_T *ht;
1483 hashitem_T *hi;
1484 hash_T hash;
1485 int adds;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001486 addword_T *aw, *naw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001487 int flen;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001488 int xlen;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001489 char_u *fol;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001490
1491 fd = fopen((char *)fname, "r");
1492 if (fd == NULL)
1493 {
1494 EMSG2(_(e_notopen), fname);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001495 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001496 }
1497
1498 /* Set sourcing_name, so that error messages mention the file name. */
1499 sourcing_name = fname;
1500 sourcing_lnum = 0;
1501
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001502 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
1503 * <charflagslen> <charflags> <fcharslen> <fchars> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001504 for (i = 0; i < VIMSPELLMAGICL; ++i)
1505 buf[i] = getc(fd); /* <fileID> */
1506 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
1507 {
1508 EMSG(_("E757: Wrong file ID in spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001509 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001510 }
1511
1512 cnt = getc(fd); /* <regioncnt> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001513 if (cnt < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001514 {
1515truncerr:
1516 EMSG(_("E758: Truncated spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001517 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001518 }
1519 if (cnt > 8)
1520 {
1521formerr:
1522 EMSG(_("E759: Format error in spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001523 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001524 }
1525 for (i = 0; i < cnt; ++i)
1526 {
1527 lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
1528 lp->sl_regions[i * 2 + 1] = getc(fd);
1529 }
1530 lp->sl_regions[cnt * 2] = NUL;
1531
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001532 cnt = getc(fd); /* <charflagslen> */
1533 if (cnt > 0)
1534 {
1535 p = (char_u *)getroom(lp, &bl_used, cnt);
1536 if (p == NULL)
1537 goto endFAIL;
1538 for (i = 0; i < cnt; ++i)
1539 p[i] = getc(fd); /* <charflags> */
1540
1541 ccnt = (getc(fd) << 8) + getc(fd); /* <fcharslen> */
1542 if (ccnt <= 0)
1543 goto formerr;
1544 fol = (char_u *)getroom(lp, &bl_used, ccnt + 1);
1545 if (fol == NULL)
1546 goto endFAIL;
1547 for (i = 0; i < ccnt; ++i)
1548 fol[i] = getc(fd); /* <fchars> */
1549 fol[i] = NUL;
1550
1551 /* Set the word-char flags and fill spell_isupper() table. */
1552 if (set_spell_charflags(p, cnt, fol) == FAIL)
1553 goto formerr;
1554 }
1555 else
1556 {
1557 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
1558 cnt = (getc(fd) << 8) + getc(fd);
1559 if (cnt != 0)
1560 goto formerr;
1561 }
1562
1563 /* round 1: <PREFIXLIST>: <affcount> <affix> ...
1564 * round 2: <SUFFIXLIST>: <affcount> <affix> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001565 for (round = 1; round <= 2; ++round)
1566 {
1567 affcount = (getc(fd) << 8) + getc(fd); /* <affcount> */
1568 if (affcount < 0)
1569 goto truncerr;
1570 if (round == 1)
1571 {
1572 gap = &lp->sl_preftab;
1573 aip = &lp->sl_prefzero;
1574 lp->sl_prefcnt = affcount;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001575 prefm = affcount > 256 ? 2 : 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001576 }
1577 else
1578 {
1579 gap = &lp->sl_sufftab;
1580 aip = &lp->sl_suffzero;
1581 lp->sl_suffcnt = affcount;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001582 suffm = affcount > 256 ? 2 : 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001583 }
1584
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001585 /*
1586 * For each affix NR there can be several affixes.
1587 */
1588 for (affnr = 0; affnr < affcount; ++affnr)
1589 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001590 /* <affix>: <affitemcnt> <affitem> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001591 affitemcnt = (getc(fd) << 8) + getc(fd); /* <affitemcnt> */
1592 if (affitemcnt < 0)
1593 goto truncerr;
1594 for (itm = 0; itm < affitemcnt; ++itm)
1595 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001596 /* <affitem>: <affflags> <affchoplen> <affchop>
1597 * <affaddlen> <affadd> */
1598 affflags = getc(fd); /* <affflags> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001599 choplen = getc(fd); /* <affchoplen> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001600 if (choplen < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001601 goto truncerr;
1602 if (choplen >= MAXWLEN)
1603 goto formerr;
1604 for (i = 0; i < choplen; ++i) /* <affchop> */
1605 buf[i] = getc(fd);
1606 buf[i] = NUL;
1607 addlen = getc(fd); /* <affaddlen> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001608 if (addlen < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001609 goto truncerr;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001610 if (affflags & AFF_PREWORD)
1611 xlen = addlen + 2; /* space for lead and trail string */
1612 else
1613 xlen = 0;
1614
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001615 /* Get room to store the affitem_T, chop and add strings. */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001616 ai = (affitem_T *)getroom(lp, &bl_used,
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001617 sizeof(affitem_T) + addlen + choplen + 1 + xlen);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001618 if (ai == NULL)
1619 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001620
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001621 ai->ai_nr = affnr;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001622 ai->ai_flags = affflags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001623 ai->ai_choplen = choplen;
1624 ai->ai_addlen = addlen;
1625
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001626 /* Chop string is at ai_add[ai_addlen + 1]. */
1627 p = ai->ai_add + addlen + 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001628 STRCPY(p, buf);
1629
1630 p = ai->ai_add;
1631 for (i = 0; i < addlen; ++i) /* <affadd> */
1632 p[i] = getc(fd);
1633 p[i] = NUL;
1634
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001635 if (affflags & AFF_PREWORD)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001636 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001637 int l, leadoff, trailoff;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001638
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001639 /*
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001640 * A preword is a prefix that's recognized as a word: it
1641 * contains a word characters folled by a non-word
1642 * character.
1643 * <affadd> is the whole prefix. Separate lead and trail
1644 * string, put the word itself at ai_add, so that it can
1645 * be used as hashtable key.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001646 */
1647 /* lead string: up to first word char */
1648 while (*p != NUL && !spell_iswordc(p))
1649 mb_ptr_adv(p);
1650 ai->ai_leadlen = p - ai->ai_add;
1651 leadoff = addlen + choplen + 2;
1652 mch_memmove(ai->ai_add + leadoff, ai->ai_add,
1653 ai->ai_leadlen);
1654 ai->ai_add[leadoff + ai->ai_leadlen] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001655
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001656 /* trail string: after last word char */
1657 while (*p != NUL && spell_iswordc(p))
1658 mb_ptr_adv(p);
1659 trailoff = leadoff + ai->ai_leadlen + 1;
1660 STRCPY(ai->ai_add + trailoff, p);
1661 ai->ai_taillen = STRLEN(p);
1662
1663 /* word itself */
1664 l = (p - ai->ai_add) - ai->ai_leadlen;
1665 mch_memmove(ai->ai_add, ai->ai_add + ai->ai_leadlen, l);
1666 ai->ai_add[l] = NUL;
1667 hash = hash_hash(ai->ai_add);
1668 hi = hash_lookup(&lp->sl_prewords, ai->ai_add, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001669 if (HASHITEM_EMPTY(hi))
1670 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001671 /* First preword with this word, add to hashtable. */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001672 hash_add_item(&lp->sl_prewords, hi, ai->ai_add, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001673 ai->ai_next = NULL;
1674 }
1675 else
1676 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001677 /* There already is a preword with this word, link in
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001678 * the list. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001679 ai2 = HI2AI(hi);
1680 ai->ai_next = ai2->ai_next;
1681 ai2->ai_next = ai;
1682 }
1683 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001684 else
1685 {
1686 /*
1687 * Add the affix to a hashtable. Which one depends on the
1688 * length of the added string in characters.
1689 */
1690#ifdef FEAT_MBYTE
1691 /* Change "addlen" from length in bytes to length in
1692 * chars. */
1693 if (has_mbyte)
1694 addlen = mb_charlen(p);
1695#endif
1696 if (addlen == 0)
1697 {
1698 /* Link in list of zero length affixes. */
1699 ai->ai_next = *aip;
1700 *aip = ai;
1701 }
1702 else
1703 {
1704 if (gap->ga_len < addlen)
1705 {
1706 /* Longer affix, need more hashtables. */
1707 if (ga_grow(gap, addlen - gap->ga_len) == FAIL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001708 goto endFAIL;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001709
1710 /* Re-allocating ga_data means that an ht_array
1711 * pointing to ht_smallarray becomes invalid. We
1712 * can recognize this: ht_mask is at its init
1713 * value. */
1714 for (i = 0; i < gap->ga_len; ++i)
1715 {
1716 ht = ((hashtab_T *)gap->ga_data) + i;
1717 if (ht->ht_mask == HT_INIT_SIZE - 1)
1718 ht->ht_array = ht->ht_smallarray;
1719 }
1720
1721 /* Init the newly used hashtable(s). */
1722 while (gap->ga_len < addlen)
1723 {
1724 hash_init(((hashtab_T *)gap->ga_data)
1725 + gap->ga_len);
1726 ++gap->ga_len;
1727 }
1728 }
1729 ht = ((hashtab_T *)gap->ga_data) + addlen - 1;
1730 hash = hash_hash(p);
1731 hi = hash_lookup(ht, p, hash);
1732 if (HASHITEM_EMPTY(hi))
1733 {
1734 /* First affix with this "ai_add", add to
1735 * hashtable. */
1736 hash_add_item(ht, hi, p, hash);
1737 ai->ai_next = NULL;
1738 }
1739 else
1740 {
1741 /* There already is an affix with this "ai_add",
1742 * link in the list. */
1743 ai2 = HI2AI(hi);
1744 ai->ai_next = ai2->ai_next;
1745 ai2->ai_next = ai;
1746 }
1747 }
1748 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001749 }
1750 }
1751 }
1752
1753 /* <SUGGEST> : <suggestlen> <more> ... */
1754 /* TODO, just skip this for now */
1755 i = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
1756 while (i-- > 0)
1757 if (getc(fd) == EOF) /* <suggestlen> */
1758 goto truncerr;
1759
1760 /* <WORDLIST>: <wordcount> <worditem> ... */ /* <wordcount> */
1761 wordcount = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8)
1762 + getc(fd);
1763 if (wordcount < 0)
1764 goto truncerr;
1765
1766 /* Init hashtable for this number of words, so that it doesn't need to
1767 * reallocate the table halfway. */
1768 hash_lock_size(&lp->sl_words, wordcount);
1769
1770 for (widx = 0; ; ++widx)
1771 {
1772 /* <worditem>: <nr> <string> <flags> [<flags2>]
1773 * [<caselen> <caseword>]
1774 * [<affixcnt> <affixNR> ...] (prefixes)
1775 * [<affixcnt> <affixNR> ...] (suffixes)
1776 * [<region>]
1777 * [<addcnt> <add> ...]
1778 */
1779 /* Use <nr> bytes from the previous word. */
1780 wlen = getc(fd); /* <nr> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001781 if (wlen < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001782 {
1783 if (widx >= wordcount) /* normal way to end the file */
1784 break;
1785 goto truncerr;
1786 }
1787
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001788 /* Read further word bytes until one below 0x20, that one must be the
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001789 * flags. Keep this fast! */
1790 for (;;)
1791 {
1792 if ((buf[wlen] = getc(fd)) < 0x20) /* <string> */
1793 break;
1794 if (++wlen == MAXWLEN)
1795 goto formerr;
1796 }
1797 flags = buf[wlen]; /* <flags> */
1798 buf[wlen] = NUL;
1799
1800 /* Get more flags if they're there. */
1801 if (flags & BWF_SECOND)
1802 flags += getc(fd) << 8; /* <flags2> */
1803
1804 if (flags & BWF_KEEPCAP)
1805 {
1806 /* Read <caselen> and <caseword> first, its length may differ from
1807 * the case-folded word. Note: this should only happen after the
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001808 * basic word without KEEPCAP! */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001809 wlen = getc(fd);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001810 if (wlen < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001811 goto truncerr;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001812 if (wlen >= MAXWLEN)
1813 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001814 for (i = 0; i < wlen; ++i)
1815 cbuf[i] = getc(fd);
1816 cbuf[i] = NUL;
1817 }
1818
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001819 /* Optional prefixes */
1820 p = affixbuf;
1821 if (flags & BWF_PREFIX)
1822 {
1823 cnt = getc(fd); /* <affixcnt> */
1824 if (cnt < 0)
1825 goto truncerr;
1826 prefixcnt = cnt;
1827 for (i = cnt * prefm; --i >= 0; ) /* <affixNR> */
1828 *p++ = getc(fd);
1829 }
1830 else
1831 prefixcnt = 0;
1832
1833 /* Optional suffixes */
1834 if (flags & BWF_SUFFIX)
1835 {
1836 cnt = getc(fd); /* <affixcnt> */
1837 if (cnt < 0)
1838 goto truncerr;
1839 suffixcnt = cnt;
1840 for (i = cnt * suffm; --i >= 0; ) /* <affixNR> */
1841 *p++ = getc(fd);
1842 }
1843 else
1844 suffixcnt = 0;
1845
1846 /* Find room to store the word in an fword_T. */
1847 fw = (fword_T *)getroom(lp, &bl_used, (int)sizeof(fword_T) + wlen
1848 + (p - affixbuf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001849 if (fw == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001850 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001851 mch_memmove(fw->fw_word, (flags & BWF_KEEPCAP) ? cbuf : buf, wlen + 1);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001852
1853 /* Put the affix NRs just after the word, if any. */
1854 if (p > affixbuf)
1855 mch_memmove(fw->fw_word + wlen + 1, affixbuf, p - affixbuf);
1856
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001857 fw->fw_flags = flags;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001858 fw->fw_prefixcnt = prefixcnt;
1859 fw->fw_suffixcnt = suffixcnt;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001860
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001861 /* We store the word in the hashtable case-folded. For a KEEPCAP word
1862 * the entry must already exist, because fw_word can't be used as the
1863 * key, it differs from "buf"! */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001864 hash = hash_hash(buf);
1865 hi = hash_lookup(&lp->sl_words, buf, hash);
1866 if (HASHITEM_EMPTY(hi))
1867 {
1868 if (hash_add_item(&lp->sl_words, hi, fw->fw_word, hash) == FAIL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001869 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001870 fw->fw_next = NULL;
1871 }
1872 else
1873 {
1874 /* Already have this basic word in the hashtable, this one will
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001875 * have different case flags and/or affixes. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001876 fw2 = HI2FWORD(hi);
1877 fw->fw_next = fw2->fw_next;
1878 fw2->fw_next = fw;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001879 --widx; /* don't count this one as a basic word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001880 }
1881
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001882 if (flags & BWF_REGION)
1883 fw->fw_region = getc(fd); /* <region> */
1884 else
1885 fw->fw_region = REGION_ALL;
1886
1887 fw->fw_adds = NULL;
1888 if (flags & BWF_ADDS)
1889 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001890 if (flags & BWF_ADDS_M)
1891 adds = (getc(fd) << 8) + getc(fd); /* <addcnt> */
1892 else
1893 adds = getc(fd); /* <addcnt> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001894 if (adds < 0)
1895 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001896
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001897 if (adds > 30)
1898 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001899 /* Use a hashtable to lookup the part until the next word end.
1900 * Thus for "de-bur-die" "de" is the basic word, "-bur" is key
1901 * in the addition hashtable, "-bur<NUL>die" the whole
1902 * addition and "aw_saveb" is '-'.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001903 * This uses more memory and involves some overhead, thus only
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001904 * do it when there are many additions (e.g., for French). */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001905 ht = (hashtab_T *)getroom(lp, &bl_used, sizeof(hashtab_T));
1906 if (ht == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001907 goto endFAIL;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001908 hash_init(ht);
1909 fw->fw_adds = (addword_T *)ht;
1910 fw->fw_flags |= BWF_ADDHASH;
1911
1912 /* Preset the size of the hashtable. It's never unlocked. */
1913 hash_lock_size(ht, adds + 1);
1914 }
1915 else
1916 ht = NULL;
1917
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001918 /*
1919 * Note: uses cbuf[] to copy bytes from previous addition.
1920 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001921 while (--adds >= 0)
1922 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001923 /* <add>: <addflags> <addlen> [<leadlen>] [<copylen>]
1924 * [<addstring>] [<region>] */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001925 flags = getc(fd); /* <addflags> */
1926 addlen = getc(fd); /* <addlen> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001927 if (addlen < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001928 goto truncerr;
1929 if (addlen >= MAXWLEN)
1930 goto formerr;
1931
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001932 if (flags & ADD_LEADLEN)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001933 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001934 leadlen = getc(fd); /* <leadlen> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001935 if (leadlen > addlen)
1936 goto formerr;
1937 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001938 else
1939 leadlen = 0;
1940
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001941 if (addlen > 0)
1942 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001943 if (flags & ADD_COPYLEN)
1944 i = getc(fd); /* <copylen> */
1945 else
1946 i = 0;
1947 for ( ; i < addlen; ++i) /* <addstring> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001948 cbuf[i] = getc(fd);
1949 cbuf[i] = NUL;
1950 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001951
1952 if (flags & ADD_KEEPCAP)
1953 {
1954 /* <addstring> is in original case, need to get
1955 * case-folded word too. */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001956 (void)spell_casefold(cbuf, addlen, fbuf, MAXWLEN);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001957 flen = addlen - leadlen + 1;
1958 addlen = STRLEN(fbuf);
1959 }
1960 else
1961 flen = 0;
1962
1963 aw = (addword_T *)getroom(lp, &bl_used,
1964 sizeof(addword_T) + addlen + flen);
1965 if (aw == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001966 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001967
1968 if (flags & ADD_KEEPCAP)
1969 {
1970 /* Put the addition in original case after the case-folded
1971 * string. */
1972 STRCPY(aw->aw_word, fbuf);
1973 STRCPY(aw->aw_word + addlen + 1, cbuf + leadlen);
1974 }
1975 else
1976 STRCPY(aw->aw_word, cbuf);
1977
1978 aw->aw_flags = flags;
1979 aw->aw_wordlen = addlen;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001980 aw->aw_leadlen = leadlen;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001981
1982 if (flags & ADD_REGION)
1983 aw->aw_region = getc(fd); /* <region> */
1984 else
1985 aw->aw_region = REGION_ALL;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001986
1987 if (ht == NULL)
1988 {
1989 /* Using simple linked list, put it in front. */
1990 aw->aw_next = fw->fw_adds;
1991 fw->fw_adds = aw;
1992 aw->aw_saveb = NUL;
1993 }
1994 else
1995 {
1996 /* Put addition in hashtable. For key we use the part up
1997 * to the next end-of-word. */
1998 if (leadlen == 0)
1999 {
2000 p = aw->aw_word;
2001 while (*p != NUL && !spell_iswordc(p))
2002 mb_ptr_adv(p);
2003 }
2004
2005 if (leadlen != 0 || *p == NUL)
2006 {
2007 /* Only non-word characters in addition, add it to the
2008 * list with the special key NOWC_KEY. Also do this
2009 * when there is a leadstring, it would get too
2010 * complicated. */
2011 hash = hash_hash(NOWC_KEY);
2012 hi = hash_lookup(ht, NOWC_KEY, hash);
2013 if (HASHITEM_EMPTY(hi))
2014 {
2015 /* we use a dummy item as the list header */
2016 naw = (addword_T *)getroom(lp, &bl_used,
2017 sizeof(addword_T) + STRLEN(NOWC_KEY));
2018 if (naw == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002019 goto endFAIL;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00002020 STRCPY(naw->aw_word, NOWC_KEY);
2021 hash_add_item(ht, hi, naw->aw_word, hash);
2022 naw->aw_next = aw;
2023 aw->aw_next = NULL;
2024 }
2025 else
2026 {
2027 naw = HI2ADDWORD(hi);
2028 aw->aw_next = naw->aw_next;
2029 naw->aw_next = aw;
2030 }
2031 aw->aw_saveb = NUL;
2032 }
2033 else
2034 {
2035 /* Truncate at next non-word character, store that
2036 * byte in "aw_saveb". */
2037 while (*p != NUL && spell_iswordc(p))
2038 mb_ptr_adv(p);
2039 aw->aw_saveb = *p;
2040 *p = NUL;
2041 hash = hash_hash(aw->aw_word);
2042 hi = hash_lookup(ht, aw->aw_word, hash);
2043 if (HASHITEM_EMPTY(hi))
2044 {
2045 hash_add_item(ht, hi, aw->aw_word, hash);
2046 aw->aw_next = NULL;
2047 }
2048 else
2049 {
2050 naw = HI2ADDWORD(hi);
2051 aw->aw_next = naw->aw_next;
2052 naw->aw_next = aw;
2053 }
2054 }
2055 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002056 }
2057 }
2058 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002059 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002060
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002061endFAIL:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002062 lp->sl_error = TRUE;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002063
2064endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002065 if (fd != NULL)
2066 fclose(fd);
2067 hash_unlock(&lp->sl_words);
2068 sourcing_name = save_sourcing_name;
2069 sourcing_lnum = save_sourcing_lnum;
2070}
2071
2072/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002073 * Get part of an sblock_T, at least "len" bytes long.
2074 * Returns NULL when out of memory.
2075 */
2076 static void *
2077getroom(lp, bl_used, len)
2078 slang_T *lp; /* lp->sl_block is current block or NULL */
2079 int *bl_used; /* used up from current block */
2080 int len; /* length needed */
2081{
2082 char_u *p;
2083 sblock_T *bl = lp->sl_block;
2084
2085 if (bl == NULL || *bl_used + len > SBLOCKSIZE)
2086 {
2087 /* Allocate a block of memory. This is not freed until spell_reload()
2088 * is called. */
2089 bl = (sblock_T *)alloc((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
2090 if (bl == NULL)
2091 return NULL;
2092 bl->sb_next = lp->sl_block;
2093 lp->sl_block = bl;
2094 *bl_used = 0;
2095 }
2096
2097 p = bl->sb_data + *bl_used;
2098 *bl_used += len;
2099
2100 return p;
2101}
2102
2103/*
2104 * Parse 'spelllang' and set buf->b_langp accordingly.
2105 * Returns an error message or NULL.
2106 */
2107 char_u *
2108did_set_spelllang(buf)
2109 buf_T *buf;
2110{
2111 garray_T ga;
2112 char_u *lang;
2113 char_u *e;
2114 char_u *region;
2115 int region_mask;
2116 slang_T *lp;
2117 int c;
2118 char_u lbuf[MAXWLEN + 1];
2119
2120 ga_init2(&ga, sizeof(langp_T), 2);
2121
2122 /* loop over comma separated languages. */
2123 for (lang = buf->b_p_spl; *lang != NUL; lang = e)
2124 {
2125 e = vim_strchr(lang, ',');
2126 if (e == NULL)
2127 e = lang + STRLEN(lang);
Bram Moolenaar5482f332005-04-17 20:18:43 +00002128 region = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002129 if (e > lang + 2)
2130 {
2131 if (e - lang >= MAXWLEN)
2132 {
2133 ga_clear(&ga);
2134 return e_invarg;
2135 }
2136 if (lang[2] == '_')
2137 region = lang + 3;
2138 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002139
2140 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2141 if (STRNICMP(lp->sl_name, lang, 2) == 0)
2142 break;
2143
2144 if (lp == NULL)
2145 {
2146 /* Not found, load the language. */
2147 STRNCPY(lbuf, lang, e - lang);
2148 lbuf[e - lang] = NUL;
2149 if (region != NULL)
2150 mch_memmove(lbuf + 2, lbuf + 5, e - lang - 4);
2151 lp = spell_load_lang(lbuf);
2152 }
2153
2154 if (lp != NULL)
2155 {
2156 if (region == NULL)
2157 region_mask = REGION_ALL;
2158 else
2159 {
2160 /* find region in sl_regions */
2161 c = find_region(lp->sl_regions, region);
2162 if (c == REGION_ALL)
2163 {
2164 c = *e;
2165 *e = NUL;
2166 smsg((char_u *)_("Warning: region %s not supported"), lang);
2167 *e = c;
2168 region_mask = REGION_ALL;
2169 }
2170 else
2171 region_mask = 1 << c;
2172 }
2173
2174 if (ga_grow(&ga, 1) == FAIL)
2175 {
2176 ga_clear(&ga);
2177 return e_outofmem;
2178 }
2179 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2180 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2181 ++ga.ga_len;
2182 }
2183
2184 if (*e == ',')
2185 ++e;
2186 }
2187
2188 /* Add a NULL entry to mark the end of the list. */
2189 if (ga_grow(&ga, 1) == FAIL)
2190 {
2191 ga_clear(&ga);
2192 return e_outofmem;
2193 }
2194 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
2195 ++ga.ga_len;
2196
2197 /* Everything is fine, store the new b_langp value. */
2198 ga_clear(&buf->b_langp);
2199 buf->b_langp = ga;
2200
2201 return NULL;
2202}
2203
2204/*
2205 * Find the region "region[2]" in "rp" (points to "sl_regions").
2206 * Each region is simply stored as the two characters of it's name.
2207 * Returns the index if found, REGION_ALL if not found.
2208 */
2209 static int
2210find_region(rp, region)
2211 char_u *rp;
2212 char_u *region;
2213{
2214 int i;
2215
2216 for (i = 0; ; i += 2)
2217 {
2218 if (rp[i] == NUL)
2219 return REGION_ALL;
2220 if (rp[i] == region[0] && rp[i + 1] == region[1])
2221 break;
2222 }
2223 return i / 2;
2224}
2225
2226/*
2227 * Return type of word:
2228 * w word 0
2229 * Word BWF_ONECAP
2230 * W WORD BWF_ALLCAP
2231 * WoRd wOrd BWF_KEEPCAP
2232 */
2233 static int
2234captype(word, end)
2235 char_u *word;
2236 char_u *end;
2237{
2238 char_u *p;
2239 int c;
2240 int firstcap;
2241 int allcap;
2242 int past_second = FALSE; /* past second word char */
2243
2244 /* find first letter */
2245 for (p = word; !spell_iswordc(p); mb_ptr_adv(p))
2246 if (p >= end)
2247 return 0; /* only non-word characters, illegal word */
2248#ifdef FEAT_MBYTE
2249 c = mb_ptr2char_adv(&p);
2250#else
2251 c = *p++;
2252#endif
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002253 firstcap = allcap = spell_isupper(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002254
2255 /*
2256 * Need to check all letters to find a word with mixed upper/lower.
2257 * But a word with an upper char only at start is a ONECAP.
2258 */
2259 for ( ; p < end; mb_ptr_adv(p))
2260 if (spell_iswordc(p))
2261 {
2262#ifdef FEAT_MBYTE
2263 c = mb_ptr2char(p);
2264#else
2265 c = *p;
2266#endif
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002267 if (!spell_isupper(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002268 {
2269 /* UUl -> KEEPCAP */
2270 if (past_second && allcap)
2271 return BWF_KEEPCAP;
2272 allcap = FALSE;
2273 }
2274 else if (!allcap)
2275 /* UlU -> KEEPCAP */
2276 return BWF_KEEPCAP;
2277 past_second = TRUE;
2278 }
2279
2280 if (allcap)
2281 return BWF_ALLCAP;
2282 if (firstcap)
2283 return BWF_ONECAP;
2284 return 0;
2285}
2286
2287# if defined(FEAT_MBYTE) || defined(PROTO)
2288/*
2289 * Clear all spelling tables and reload them.
2290 * Used after 'encoding' is set.
2291 */
2292 void
2293spell_reload()
2294{
2295 buf_T *buf;
2296 slang_T *lp;
2297
2298 /* Initialize the table for spell_iswordc(). */
2299 init_spell_chartab();
2300
2301 /* Unload all allocated memory. */
2302 while (first_lang != NULL)
2303 {
2304 lp = first_lang;
2305 first_lang = lp->sl_next;
2306 slang_free(lp);
2307 }
2308
2309 /* Go through all buffers and handle 'spelllang'. */
2310 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2311 {
2312 ga_clear(&buf->b_langp);
2313 if (*buf->b_p_spl != NUL)
2314 did_set_spelllang(buf);
2315 }
2316}
2317# endif
2318
2319/*
2320 * Recognizing words uses a two-step mechanism:
2321 * 1. Locate a basic word, made out of word characters only and separated by
2322 * non-word characters.
2323 * 2. When a basic word is found, check if (possibly required) additions
2324 * before and after the word are present.
2325 *
2326 * Both mechanisms use affixes (prefixes and suffixes) to reduce the number of
2327 * words. When no matching word was found in the hashtable the start of the
2328 * word is checked for matching prefixes and the end of the word for matching
2329 * suffixes. All matching affixes are removed and then the resulting word is
2330 * searched for. If found it is checked if it supports the used affix.
2331 */
2332
2333
2334#if defined(FEAT_MBYTE) || defined(PROTO)
2335/*
2336 * Functions for ":mkspell".
2337 * Only possible with the multi-byte feature.
2338 */
2339
2340#define MAXLINELEN 300 /* Maximum length in bytes of a line in a .aff
2341 and .dic file. */
2342/*
2343 * Main structure to store the contents of a ".aff" file.
2344 */
2345typedef struct afffile_S
2346{
2347 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
2348 char_u *af_try; /* "TRY" line in "af_enc" encoding */
2349 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
2350 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
2351 garray_T af_rep; /* list of repentry_T entries from REP lines */
2352} afffile_T;
2353
2354typedef struct affentry_S affentry_T;
2355
2356/* Affix header from ".aff" file. Used for af_pref and af_suff. */
2357typedef struct affheader_S
2358{
2359 char_u ah_key[2]; /* key for hashtable == name of affix entry */
2360 int ah_combine;
2361 affentry_T *ah_first; /* first affix entry */
2362 short_u ah_affnr; /* used in get_new_aff() */
2363} affheader_T;
2364
2365#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
2366
2367/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
2368struct affentry_S
2369{
2370 affentry_T *ae_next; /* next affix with same name/number */
2371 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
2372 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar5482f332005-04-17 20:18:43 +00002373 char_u *ae_add_nw; /* For a suffix: first non-word char in
2374 * "ae_add"; for a prefix with only non-word
2375 * chars: equal to "ae_add", for a prefix with
2376 * word and non-word chars: first non-word
2377 * char after word char. NULL otherwise. */
2378 char_u *ae_add_pw; /* For a prefix with both word and non-word
2379 * chars: first word char. NULL otherwise. */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00002380 char_u ae_preword; /* TRUE for a prefix with one word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002381 char_u *ae_cond; /* condition (NULL for ".") */
2382 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
2383 short_u ae_affnr; /* for old affix: new affix number */
2384};
2385
2386/*
2387 * Structure to store a word from a ".dic" file.
2388 */
2389typedef struct dicword_S
2390{
2391 char_u *dw_affnm; /* original affix names */
2392 char_u dw_word[1]; /* actually longer: the word in 'encoding' */
2393} dicword_T;
2394
2395static dicword_T dumdw;
2396#define HI2DW(hi) ((dicword_T *)((hi)->hi_key - (dumdw.dw_word - (char_u *)&dumdw)))
2397
2398/*
2399 * Structure to store a basic word for the spell file.
2400 * This is used for ":mkspell", not for spell checking.
2401 */
2402typedef struct basicword_S basicword_T;
2403struct basicword_S
2404{
2405 basicword_T *bw_next; /* next word with same basic word */
2406 basicword_T *bw_cnext; /* next word with same caps */
2407 int bw_flags; /* BWF_ flags */
2408 garray_T bw_prefix; /* table with prefix numbers */
2409 garray_T bw_suffix; /* table with suffix numbers */
2410 int bw_region; /* region bits */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002411 char_u *bw_caseword; /* keep-case word or NULL */
2412 char_u *bw_leadstring; /* must come before bw_word or NULL */
2413 char_u *bw_addstring; /* must come after bw_word or NULL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002414 char_u bw_word[1]; /* actually longer: word case folded */
2415};
2416
2417static basicword_T dumbw;
2418#define KEY2BW(p) ((basicword_T *)((p) - (dumbw.bw_word - (char_u *)&dumbw)))
2419#define HI2BW(hi) KEY2BW((hi)->hi_key)
2420
2421/* Store the affix number related with a certain string. */
2422typedef struct affhash_S
2423{
2424 short_u as_nr; /* the affix nr */
2425 char_u as_word[1]; /* actually longer */
2426} affhash_T;
2427
2428static affhash_T dumas;
2429#define HI2AS(hi) ((affhash_T *)((hi)->hi_key - (dumas.as_word - (char_u *)&dumas)))
2430
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002431/* info for writing the spell file */
2432typedef struct winfo_S
2433{
2434 FILE *wif_fd;
2435 basicword_T *wif_prevbw; /* last written basic word */
2436 int wif_regionmask; /* regions supported */
2437 int wif_prefm; /* 1 or 2 bytes used for prefix NR */
2438 int wif_suffm; /* 1 or 2 bytes used for suffix NR */
2439 long wif_wcount; /* written word count */
2440 long wif_acount; /* written addition count */
2441 long wif_addmax; /* max number of additions on one word */
2442 char_u *wif_addmaxw; /* word with max additions */
2443} winfo_T;
2444
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002445
Bram Moolenaar5482f332005-04-17 20:18:43 +00002446static afffile_T *spell_read_aff __ARGS((char_u *fname, vimconv_T *conv, int ascii));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002447static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar5482f332005-04-17 20:18:43 +00002448static int has_non_ascii __ARGS((char_u *s));
2449static int spell_read_dic __ARGS((hashtab_T *ht, char_u *fname, vimconv_T *conv, int ascii));
2450static int get_new_aff __ARGS((hashtab_T *oldaff, garray_T *gap, int prefix));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002451static void spell_free_dic __ARGS((hashtab_T *dic));
2452static int same_affentries __ARGS((affheader_T *ah1, affheader_T *ah2));
2453static void add_affhash __ARGS((hashtab_T *ht, char_u *key, int newnr));
2454static void clear_affhash __ARGS((hashtab_T *ht));
2455static void trans_affixes __ARGS((dicword_T *dw, basicword_T *bw, afffile_T *oldaff, hashtab_T *newwords));
2456static int build_wordlist __ARGS((hashtab_T *newwords, hashtab_T *oldwords, afffile_T *oldaff, int regionmask));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002457static basicword_T *get_basicword __ARGS((char_u *word, int asize));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002458static void combine_regions __ARGS((hashtab_T *newwords));
2459static int same_affixes __ARGS((basicword_T *bw, basicword_T *nbw));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002460static int expand_affixes __ARGS((hashtab_T *newwords, garray_T *prefgap, garray_T *suffgap));
2461static int expand_one_aff __ARGS((basicword_T *bw, garray_T *add_words, affentry_T *pae, affentry_T *sae));
2462static int add_to_wordlist __ARGS((hashtab_T *newwords, basicword_T *bw));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002463static void write_affix __ARGS((FILE *fd, affheader_T *ah));
2464static void write_affixlist __ARGS((FILE *fd, garray_T *aff, int bytes));
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00002465static void write_vim_spell __ARGS((char_u *fname, garray_T *prefga, garray_T *suffga, hashtab_T *newwords, int regcount, char_u *regchars, int ascii));
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002466static void write_bword __ARGS((winfo_T *wif, basicword_T *bw, int lowcap));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002467static void free_wordtable __ARGS((hashtab_T *ht));
2468static void free_basicword __ARGS((basicword_T *bw));
2469static void free_affixentries __ARGS((affentry_T *first));
Bram Moolenaar5482f332005-04-17 20:18:43 +00002470static void free_affix_entry __ARGS((affentry_T *ap));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002471
2472/*
2473 * Read an affix ".aff" file.
2474 * Returns an afffile_T, NULL for failure.
2475 */
2476 static afffile_T *
Bram Moolenaar5482f332005-04-17 20:18:43 +00002477spell_read_aff(fname, conv, ascii)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002478 char_u *fname;
2479 vimconv_T *conv; /* info for encoding conversion */
Bram Moolenaar5482f332005-04-17 20:18:43 +00002480 int ascii; /* Only accept ASCII characters */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002481{
2482 FILE *fd;
2483 afffile_T *aff;
2484 char_u rline[MAXLINELEN];
2485 char_u *line;
2486 char_u *pc = NULL;
2487 char_u *(items[6]);
2488 int itemcnt;
2489 char_u *p;
2490 int lnum = 0;
2491 affheader_T *cur_aff = NULL;
2492 int aff_todo = 0;
2493 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002494 char_u *low = NULL;
2495 char_u *fol = NULL;
2496 char_u *upp = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002497
2498 fd = fopen((char *)fname, "r");
2499 if (fd == NULL)
2500 {
2501 EMSG2(_(e_notopen), fname);
2502 return NULL;
2503 }
2504
2505 smsg((char_u *)_("Reading affix file %s..."), fname);
2506 out_flush();
2507
2508 aff = (afffile_T *)alloc_clear((unsigned)sizeof(afffile_T));
2509 if (aff == NULL)
2510 return NULL;
2511 hash_init(&aff->af_pref);
2512 hash_init(&aff->af_suff);
2513 ga_init2(&aff->af_rep, (int)sizeof(repentry_T), 20);
2514
2515 /*
2516 * Read all the lines in the file one by one.
2517 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002518 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002519 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002520 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002521 ++lnum;
2522
2523 /* Skip comment lines. */
2524 if (*rline == '#')
2525 continue;
2526
2527 /* Convert from "SET" to 'encoding' when needed. */
2528 vim_free(pc);
2529 if (conv->vc_type != CONV_NONE)
2530 {
2531 pc = string_convert(conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002532 if (pc == NULL)
2533 {
2534 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
2535 fname, lnum, rline);
2536 continue;
2537 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002538 line = pc;
2539 }
2540 else
2541 {
2542 pc = NULL;
2543 line = rline;
2544 }
2545
2546 /* Split the line up in white separated items. Put a NUL after each
2547 * item. */
2548 itemcnt = 0;
2549 for (p = line; ; )
2550 {
2551 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
2552 ++p;
2553 if (*p == NUL)
2554 break;
2555 items[itemcnt++] = p;
2556 while (*p > ' ') /* skip until white space or CR/NL */
2557 ++p;
2558 if (*p == NUL)
2559 break;
2560 *p++ = NUL;
2561 }
2562
2563 /* Handle non-empty lines. */
2564 if (itemcnt > 0)
2565 {
2566 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
2567 && aff->af_enc == NULL)
2568 {
2569 if (aff->af_enc != NULL)
2570 smsg((char_u *)_("Duplicate SET line ignored in %s line %d: %s"),
2571 fname, lnum, line);
2572 else
2573 {
2574 /* Setup for conversion from "ENC" to 'encoding'. */
2575 aff->af_enc = enc_canonize(items[1]);
Bram Moolenaar5482f332005-04-17 20:18:43 +00002576 if (aff->af_enc != NULL && !ascii
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002577 && convert_setup(conv, aff->af_enc, p_enc) == FAIL)
2578 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
2579 fname, aff->af_enc, p_enc);
2580 }
2581 }
2582 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2
2583 && aff->af_try == NULL)
2584 aff->af_try = vim_strsave(items[1]);
2585 else if ((STRCMP(items[0], "PFX") == 0
2586 || STRCMP(items[0], "SFX") == 0)
2587 && aff_todo == 0
2588 && itemcnt == 4)
2589 {
2590 /* New affix letter. */
2591 cur_aff = (affheader_T *)alloc((unsigned)sizeof(affheader_T));
2592 if (cur_aff == NULL)
2593 break;
2594 cur_aff->ah_key[0] = *items[1];
2595 cur_aff->ah_key[1] = NUL;
2596 if (items[1][1] != NUL)
2597 smsg((char_u *)_("Affix name too long in %s line %d: %s"),
2598 fname, lnum, items[1]);
2599 if (*items[2] == 'Y')
2600 cur_aff->ah_combine = TRUE;
2601 else if (*items[2] == 'N')
2602 cur_aff->ah_combine = FALSE;
2603 else if (p_verbose > 0)
2604 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
2605 fname, lnum, items[2]);
2606 cur_aff->ah_first = NULL;
2607 if (*items[0] == 'P')
2608 tp = &aff->af_pref;
2609 else
2610 tp = &aff->af_suff;
2611 if (!HASHITEM_EMPTY(hash_find(tp, cur_aff->ah_key)))
2612 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
2613 fname, lnum, items[1]);
2614 else
2615 hash_add(tp, cur_aff->ah_key);
2616
2617 aff_todo = atoi((char *)items[3]);
2618 }
2619 else if ((STRCMP(items[0], "PFX") == 0
2620 || STRCMP(items[0], "SFX") == 0)
2621 && aff_todo > 0
2622 && STRCMP(cur_aff->ah_key, items[1]) == 0
2623 && itemcnt == 5)
2624 {
2625 affentry_T *aff_entry;
2626
2627 /* New item for an affix letter. */
2628 --aff_todo;
2629 aff_entry = (affentry_T *)alloc_clear(
2630 (unsigned)sizeof(affentry_T));
2631 if (aff_entry == NULL)
2632 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00002633
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002634 if (STRCMP(items[2], "0") != 0)
2635 aff_entry->ae_chop = vim_strsave(items[2]);
2636 if (STRCMP(items[3], "0") != 0)
2637 aff_entry->ae_add = vim_strsave(items[3]);
2638 if (STRCMP(items[4], ".") != 0)
2639 {
2640 char_u buf[MAXLINELEN];
2641
2642 aff_entry->ae_cond = vim_strsave(items[4]);
2643 if (*items[0] == 'P')
2644 sprintf((char *)buf, "^%s", items[4]);
2645 else
2646 sprintf((char *)buf, "%s$", items[4]);
2647 aff_entry->ae_prog = vim_regcomp(buf, RE_MAGIC + RE_STRING);
2648 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00002649
2650 if (ascii && (has_non_ascii(aff_entry->ae_chop)
2651 || has_non_ascii(aff_entry->ae_add)))
2652 {
2653 /* Don't use an affix entry with non-ASCII characters when
2654 * "ascii" is TRUE. */
2655 free_affix_entry(aff_entry);
2656 }
2657 else
2658 {
2659 aff_entry->ae_next = cur_aff->ah_first;
2660 cur_aff->ah_first = aff_entry;
2661 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002662 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002663 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
2664 {
2665 if (fol != NULL)
2666 smsg((char_u *)_("Duplicate FOL in %s line %d"),
2667 fname, lnum);
2668 else
2669 fol = vim_strsave(items[1]);
2670 }
2671 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
2672 {
2673 if (low != NULL)
2674 smsg((char_u *)_("Duplicate LOW in %s line %d"),
2675 fname, lnum);
2676 else
2677 low = vim_strsave(items[1]);
2678 }
2679 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
2680 {
2681 if (upp != NULL)
2682 smsg((char_u *)_("Duplicate UPP in %s line %d"),
2683 fname, lnum);
2684 else
2685 upp = vim_strsave(items[1]);
2686 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002687 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
2688 /* Ignore REP count */;
2689 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
2690 {
2691 repentry_T *rp;
2692
2693 /* REP item */
2694 if (ga_grow(&aff->af_rep, 1) == FAIL)
2695 break;
2696 rp = ((repentry_T *)aff->af_rep.ga_data) + aff->af_rep.ga_len;
2697 rp->re_from = vim_strsave(items[1]);
2698 rp->re_to = vim_strsave(items[2]);
2699 ++aff->af_rep.ga_len;
2700 }
2701 else if (p_verbose > 0)
2702 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
2703 fname, lnum, items[0]);
2704 }
2705
2706 }
2707
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002708 if (fol != NULL || low != NULL || upp != NULL)
2709 {
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00002710 /* Don't write a word table for an ASCII file, so that we don't check
2711 * for conflicts with a word table that matches 'encoding'. */
2712 if (!ascii)
2713 {
2714 if (fol == NULL || low == NULL || upp == NULL)
2715 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
2716 else
2717 set_spell_chartab(fol, low, upp);
2718 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002719
2720 vim_free(fol);
2721 vim_free(low);
2722 vim_free(upp);
2723 }
2724
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002725 vim_free(pc);
2726 fclose(fd);
2727 return aff;
2728}
2729
2730/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00002731 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
2732 * When "s" is NULL FALSE is returned.
2733 */
2734 static int
2735has_non_ascii(s)
2736 char_u *s;
2737{
2738 char_u *p;
2739
2740 if (s != NULL)
2741 for (p = s; *p != NUL; ++p)
2742 if (*p >= 128)
2743 return TRUE;
2744 return FALSE;
2745}
2746
2747/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002748 * Free the structure filled by spell_read_aff().
2749 */
2750 static void
2751spell_free_aff(aff)
2752 afffile_T *aff;
2753{
2754 hashtab_T *ht;
2755 hashitem_T *hi;
2756 int todo;
2757 int i;
2758 repentry_T *rp;
2759 affheader_T *ah;
2760
2761 vim_free(aff->af_enc);
2762 vim_free(aff->af_try);
2763
2764 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
2765 {
2766 todo = ht->ht_used;
2767 for (hi = ht->ht_array; todo > 0; ++hi)
2768 {
2769 if (!HASHITEM_EMPTY(hi))
2770 {
2771 --todo;
2772 ah = HI2AH(hi);
2773 free_affixentries(ah->ah_first);
2774 vim_free(ah);
2775 }
2776 }
2777 if (ht == &aff->af_suff)
2778 break;
2779 }
2780 hash_clear(&aff->af_pref);
2781 hash_clear(&aff->af_suff);
2782
2783 for (i = 0; i < aff->af_rep.ga_len; ++i)
2784 {
2785 rp = ((repentry_T *)aff->af_rep.ga_data) + i;
2786 vim_free(rp->re_from);
2787 vim_free(rp->re_to);
2788 }
2789 ga_clear(&aff->af_rep);
2790
2791 vim_free(aff);
2792}
2793
2794/*
2795 * Read a dictionary ".dic" file.
2796 * Returns OK or FAIL;
2797 * Each entry in the hashtab_T is a dicword_T.
2798 */
2799 static int
Bram Moolenaar5482f332005-04-17 20:18:43 +00002800spell_read_dic(ht, fname, conv, ascii)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002801 hashtab_T *ht;
2802 char_u *fname;
2803 vimconv_T *conv; /* info for encoding conversion */
Bram Moolenaar5482f332005-04-17 20:18:43 +00002804 int ascii; /* only accept ASCII words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002805{
2806 char_u line[MAXLINELEN];
2807 char_u *p;
2808 dicword_T *dw;
2809 char_u *pc;
2810 char_u *w;
2811 int l;
2812 hash_T hash;
2813 hashitem_T *hi;
2814 FILE *fd;
2815 int lnum = 1;
2816
2817 fd = fopen((char *)fname, "r");
2818 if (fd == NULL)
2819 {
2820 EMSG2(_(e_notopen), fname);
2821 return FAIL;
2822 }
2823
2824 smsg((char_u *)_("Reading dictionary file %s..."), fname);
2825 out_flush();
2826
2827 /* Read and ignore the first line: word count. */
2828 (void)vim_fgets(line, MAXLINELEN, fd);
2829 if (!isdigit(*skipwhite(line)))
2830 EMSG2(_("E760: No word count in %s"), fname);
2831
2832 /*
2833 * Read all the lines in the file one by one.
2834 * The words are converted to 'encoding' here, before being added to
2835 * the hashtable.
2836 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002837 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002838 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002839 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002840 ++lnum;
2841
2842 /* Remove CR, LF and white space from end. */
2843 l = STRLEN(line);
2844 while (l > 0 && line[l - 1] <= ' ')
2845 --l;
2846 if (l == 0)
2847 continue; /* empty line */
2848 line[l] = NUL;
2849
2850 /* Find the optional affix names. */
2851 p = vim_strchr(line, '/');
2852 if (p != NULL)
2853 *p++ = NUL;
2854
Bram Moolenaar5482f332005-04-17 20:18:43 +00002855 /* Skip non-ASCII words when "ascii" is TRUE. */
2856 if (ascii && has_non_ascii(line))
2857 continue;
2858
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002859 /* Convert from "SET" to 'encoding' when needed. */
2860 if (conv->vc_type != CONV_NONE)
2861 {
2862 pc = string_convert(conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002863 if (pc == NULL)
2864 {
2865 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
2866 fname, lnum, line);
2867 continue;
2868 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002869 w = pc;
2870 }
2871 else
2872 {
2873 pc = NULL;
2874 w = line;
2875 }
2876
2877 dw = (dicword_T *)alloc_clear((unsigned)sizeof(dicword_T)
2878 + STRLEN(w));
2879 if (dw == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002880 {
2881 vim_free(pc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002882 break;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002883 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002884 STRCPY(dw->dw_word, w);
2885 vim_free(pc);
2886
2887 hash = hash_hash(dw->dw_word);
2888 hi = hash_lookup(ht, dw->dw_word, hash);
2889 if (!HASHITEM_EMPTY(hi))
2890 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
2891 fname, lnum, line);
2892 else
2893 hash_add_item(ht, hi, dw->dw_word, hash);
2894
2895 if (p != NULL)
2896 dw->dw_affnm = vim_strsave(p);
2897 }
2898
2899 fclose(fd);
2900 return OK;
2901}
2902
2903/*
2904 * Free the structure filled by spell_read_dic().
2905 */
2906 static void
2907spell_free_dic(dic)
2908 hashtab_T *dic;
2909{
2910 int todo;
2911 dicword_T *dw;
2912 hashitem_T *hi;
2913
2914 todo = dic->ht_used;
2915 for (hi = dic->ht_array; todo > 0; ++hi)
2916 {
2917 if (!HASHITEM_EMPTY(hi))
2918 {
2919 --todo;
2920 dw = HI2DW(hi);
2921 vim_free(dw->dw_affnm);
2922 vim_free(dw);
2923 }
2924 }
2925 hash_clear(dic);
2926}
2927
2928/*
2929 * Take the affixes read by spell_read_aff() and add them to the new list.
2930 * Attempts to re-use the same number for identical affixes (ignoring the
2931 * condition, since we remove that). That is especially important when using
2932 * multiple regions.
2933 * Returns OK or FAIL;
2934 */
2935 static int
Bram Moolenaar5482f332005-04-17 20:18:43 +00002936get_new_aff(oldaff, gap, prefix)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002937 hashtab_T *oldaff; /* hashtable with affheader_T */
2938 garray_T *gap; /* table with new affixes */
Bram Moolenaar5482f332005-04-17 20:18:43 +00002939 int prefix; /* TRUE when doing prefixes, FALSE for
2940 suffixes */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002941{
2942 int oldtodo;
2943 affheader_T *oldah, *newah, *gapah;
2944 affentry_T *oldae, *newae;
2945 hashitem_T *oldhi;
2946 hashitem_T *hi;
2947 hashtab_T condht; /* conditions already found */
2948 char_u condkey[MAXLINELEN];
2949 int newnr;
2950 int gapnr;
2951 int retval = OK;
2952 char_u *p;
2953 garray_T tga;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00002954 int preword;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002955
2956 /*
2957 * Loop over all the old affix names.
2958 */
2959 oldtodo = oldaff->ht_used;
2960 for (oldhi = oldaff->ht_array; oldtodo > 0 && retval == OK; ++oldhi)
2961 {
2962 if (!HASHITEM_EMPTY(oldhi))
2963 {
2964 --oldtodo;
2965 oldah = (affheader_T *)oldhi->hi_key;
2966
2967 /* Put entries with the same condition under the same new affix
2968 * nr in "tga". Use hashtable "condht" to find them. */
2969 ga_init2(&tga, sizeof(affheader_T), 10);
2970 hash_init(&condht);
2971
2972 /*
2973 * Loop over all affixes with the same name.
2974 * The affixes with the same condition will get the same number,
2975 * since they can be used with the same words.
2976 * 1. build the lists of new affentry_T, with the headers in "tga".
2977 * 2. Check if some of the lists already exist in "gap", re-use
2978 * their number.
2979 * 3. Assign the new numbers to the old affixes.
2980 */
2981
2982 /* 1. build the lists of new affentry_T. */
2983 for (oldae = oldah->ah_first; oldae != NULL && retval == OK;
2984 oldae = oldae->ae_next)
2985 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00002986 preword = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002987 oldae->ae_add_nw = NULL;
Bram Moolenaar5482f332005-04-17 20:18:43 +00002988 oldae->ae_add_pw = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002989 if (oldae->ae_add != NULL)
2990 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00002991 /* Check for non-word characters in the affix. If there
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00002992 * is one a suffix will be turned into an addition, a
2993 * prefix may be turned into a leadstring.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002994 * This is stored with the old affix, that is where
2995 * trans_affixes() will check. */
2996 for (p = oldae->ae_add; *p != NUL; mb_ptr_adv(p))
2997 if (!spell_iswordc(p))
Bram Moolenaar5482f332005-04-17 20:18:43 +00002998 {
2999 oldae->ae_add_nw = p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003000 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003001 }
3002
3003 if (prefix && oldae->ae_add_nw != NULL)
3004 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003005 /* If a prefix has non-word characters special
3006 * treatment is necessary:
3007 * - If it has only non-word characters it becomes a
3008 * leadstring.
3009 * - If it has a sequence of word characters followed
3010 * by a non-word char it becomes a "preword": "d'",
3011 * "de-", "d'ai", etc.
3012 * - if it has another mix of word and non-word
3013 * characters the part before the last word char
3014 * becomes a leadstring: "'d", etc.
3015 */
Bram Moolenaar5482f332005-04-17 20:18:43 +00003016 for (p = oldae->ae_add; *p != NUL; mb_ptr_adv(p))
3017 if (spell_iswordc(p))
3018 {
3019 oldae->ae_add_pw = p;
3020 break;
3021 }
3022 if (oldae->ae_add_pw != NULL)
3023 {
3024 /* Mixed prefix, set ae_add_nw to first non-word
3025 * char after ae_add_pw (if there is one). */
3026 oldae->ae_add_nw = NULL;
3027 for ( ; *p != NUL; mb_ptr_adv(p))
3028 if (!spell_iswordc(p))
3029 {
3030 oldae->ae_add_nw = p;
3031 break;
3032 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003033 if (oldae->ae_add_nw != NULL)
3034 {
3035 preword = TRUE;
3036 oldae->ae_add_pw = NULL;
3037 oldae->ae_add_nw = NULL;
3038 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003039 }
3040 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003041 }
3042
3043 if (oldae->ae_cond == NULL)
3044 /* hashtable requires a non-empty key */
3045 STRCPY(condkey, "---");
3046 else
3047 STRCPY(condkey, oldae->ae_cond);
3048
3049 /* Look for an existing list with this name and condition. */
3050 hi = hash_find(&condht, condkey);
3051 if (!HASHITEM_EMPTY(hi))
3052 /* Match with existing affix, use that one. */
3053 newnr = HI2AS(hi)->as_nr;
3054 else
3055 {
3056 /* Add a new affix number. */
3057 newnr = tga.ga_len;
3058 if (ga_grow(&tga, 1) == FAIL)
3059 retval = FAIL;
3060 else
3061 {
3062 newah = ((affheader_T *)tga.ga_data) + newnr;
3063 newah->ah_combine = oldah->ah_combine;
3064 newah->ah_first = NULL;
3065 ++tga.ga_len;
3066
3067 /* Add the new list to the condht hashtable. */
3068 add_affhash(&condht, condkey, newnr);
3069 }
3070 }
3071
3072 /* Add the new affentry_T to the list. */
3073 newah = ((affheader_T *)tga.ga_data) + newnr;
3074 newae = (affentry_T *)alloc_clear((unsigned)sizeof(affentry_T));
3075 if (newae == NULL)
3076 retval = FAIL;
3077 else
3078 {
3079 newae->ae_next = newah->ah_first;
3080 newah->ah_first = newae;
3081 if (oldae->ae_chop == NULL)
3082 newae->ae_chop = NULL;
3083 else
3084 newae->ae_chop = vim_strsave(oldae->ae_chop);
3085 if (oldae->ae_add == NULL)
3086 newae->ae_add = NULL;
3087 else
3088 newae->ae_add = vim_strsave(oldae->ae_add);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003089 newae->ae_preword = preword;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003090
3091 /* The condition is not copied, since the new affix is
3092 * only used for words where the condition matches. */
3093 }
3094 }
3095
3096 /* 2. Check if some of the lists already exist, re-use their
3097 * number. Otherwise add the list to "gap". */
3098 for (newnr = 0; newnr < tga.ga_len; ++newnr)
3099 {
3100 newah = ((affheader_T *)tga.ga_data) + newnr;
3101 for (gapnr = 0; gapnr < gap->ga_len; ++gapnr)
3102 {
3103 gapah = ((affheader_T *)gap->ga_data) + gapnr;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003104 if (newah->ah_combine == gapah->ah_combine
3105 && same_affentries(newah, gapah))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003106 /* Found an existing affheader_T entry with same
3107 * affentry_T list, use its number. */
3108 break;
3109 }
3110
3111 newah->ah_affnr = gapnr;
3112 if (gapnr == gap->ga_len)
3113 {
3114 /* This is a new affentry_T list, add it. */
3115 if (ga_grow(gap, 1) == FAIL)
3116 retval = FAIL;
3117 else
3118 {
3119 *(((affheader_T *)gap->ga_data) + gap->ga_len) = *newah;
3120 ++gap->ga_len;
3121 }
3122 }
3123 else
3124 {
3125 /* free unused affentry_T list */
3126 free_affixentries(newah->ah_first);
3127 }
3128 }
3129
3130 /* 3. Assign the new affix numbers to the old affixes. */
3131 for (oldae = oldah->ah_first; oldae != NULL && retval == OK;
3132 oldae = oldae->ae_next)
3133 {
3134 if (oldae->ae_cond == NULL)
3135 /* hashtable requires a non-empty key */
3136 STRCPY(condkey, "---");
3137 else
3138 STRCPY(condkey, oldae->ae_cond);
3139
3140 /* Look for an existing affix with this name and condition. */
3141 hi = hash_find(&condht, condkey);
3142 if (!HASHITEM_EMPTY(hi))
3143 /* Match with existing affix, use that one. */
3144 newnr = HI2AS(hi)->as_nr;
3145 else
3146 {
3147 EMSG(_(e_internal));
3148 retval = FAIL;
3149 }
3150 newah = ((affheader_T *)tga.ga_data) + newnr;
3151 oldae->ae_affnr = newah->ah_affnr;
3152 }
3153
3154 ga_clear(&tga);
3155 clear_affhash(&condht);
3156 }
3157 }
3158
3159 return retval;
3160}
3161
3162/*
3163 * Return TRUE if the affentry_T lists for "ah1" and "ah2" contain the same
3164 * items, ignoring the order.
3165 * Only compares the chop and add strings, not the condition.
3166 */
3167 static int
3168same_affentries(ah1, ah2)
3169 affheader_T *ah1;
3170 affheader_T *ah2;
3171{
3172 affentry_T *ae1, *ae2;
3173
3174 /* Check the length of the lists first. */
3175 ae2 = ah2->ah_first;
3176 for (ae1 = ah1->ah_first; ae1 != NULL; ae1 = ae1->ae_next)
3177 {
3178 if (ae2 == NULL)
3179 return FALSE; /* "ah1" list is longer */
3180 ae2 = ae2->ae_next;
3181 }
3182 if (ae2 != NULL)
3183 return FALSE; /* "ah2" list is longer */
3184
3185 /* Check that each entry in "ah1" appears in "ah2". */
3186 for (ae1 = ah1->ah_first; ae1 != NULL; ae1 = ae1->ae_next)
3187 {
3188 for (ae2 = ah2->ah_first; ae2 != NULL; ae2 = ae2->ae_next)
3189 {
3190 if ((ae1->ae_chop == NULL) == (ae2->ae_chop == NULL)
3191 && (ae1->ae_add == NULL) == (ae2->ae_add == NULL)
3192 && (ae1->ae_chop == NULL
3193 || STRCMP(ae1->ae_chop, ae2->ae_chop) == 0)
3194 && (ae1->ae_add == NULL
3195 || STRCMP(ae1->ae_add, ae2->ae_add) == 0))
3196 break;
3197 }
3198 if (ae2 == NULL)
3199 return FALSE;
3200 }
3201
3202 return TRUE;
3203}
3204
3205/*
3206 * Add a chop/add or cond hashtable entry.
3207 */
3208 static void
3209add_affhash(ht, key, newnr)
3210 hashtab_T *ht;
3211 char_u *key;
3212 int newnr;
3213{
3214 affhash_T *as;
3215
3216 as = (affhash_T *)alloc((unsigned)sizeof(affhash_T) + STRLEN(key));
3217 if (as != NULL)
3218 {
3219 as->as_nr = newnr;
3220 STRCPY(as->as_word, key);
3221 hash_add(ht, as->as_word);
3222 }
3223}
3224
3225/*
3226 * Clear the chop/add hashtable used to detect identical affixes.
3227 */
3228 static void
3229clear_affhash(ht)
3230 hashtab_T *ht;
3231{
3232 int todo;
3233 hashitem_T *hi;
3234
3235 todo = ht->ht_used;
3236 for (hi = ht->ht_array; todo > 0; ++hi)
3237 {
3238 if (!HASHITEM_EMPTY(hi))
3239 {
3240 --todo;
3241 vim_free(HI2AS(hi));
3242 }
3243 }
3244 hash_clear(ht);
3245}
3246
3247/*
3248 * Translate list of affix names for an old word to affix numbers in a new
3249 * basic word.
3250 * This checks if the conditions match with the old word. The result is that
3251 * the new affix does not need to store the condition.
3252 */
3253 static void
3254trans_affixes(dw, bw, oldaff, newwords)
3255 dicword_T *dw; /* old word */
3256 basicword_T *bw; /* basic word */
3257 afffile_T *oldaff; /* affixes for "oldwords" */
3258 hashtab_T *newwords; /* table with words */
3259{
3260 char_u key[2];
3261 char_u *p;
3262 char_u *affnm;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003263 garray_T *gap, *agap;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003264 hashitem_T *aff_hi;
3265 affheader_T *ah;
3266 affentry_T *ae;
3267 regmatch_T regmatch;
3268 int i;
3269 basicword_T *nbw;
3270 int alen;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003271 garray_T suffixga; /* list of words with non-word suffixes */
3272 garray_T prefixga; /* list of words with non-word prefixes */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003273 char_u nword[MAXWLEN];
3274 int flags;
3275 int n;
3276
Bram Moolenaar5482f332005-04-17 20:18:43 +00003277 ga_init2(&suffixga, (int)sizeof(basicword_T *), 5);
3278 ga_init2(&prefixga, (int)sizeof(basicword_T *), 5);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003279
3280 /* Loop over all the affix names of the old word. */
3281 key[1] = NUL;
3282 for (affnm = dw->dw_affnm; *affnm != NUL; ++affnm)
3283 {
3284 key[0] = *affnm;
3285 aff_hi = hash_find(&oldaff->af_pref, key);
3286 if (!HASHITEM_EMPTY(aff_hi))
3287 gap = &bw->bw_prefix; /* found a prefix */
3288 else
3289 {
3290 gap = &bw->bw_suffix; /* must be a suffix */
3291 aff_hi = hash_find(&oldaff->af_suff, key);
3292 if (HASHITEM_EMPTY(aff_hi))
3293 {
3294 smsg((char_u *)_("No affix entry '%s' for word %s"),
3295 key, dw->dw_word);
3296 continue;
3297 }
3298 }
3299
3300 /* Loop over all the affix entries for this affix name. */
3301 ah = HI2AH(aff_hi);
3302 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3303 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003304 /* Setup for regexp matching. Note that we don't ignore case.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003305 * This is weird, because the rules in an .aff file don't care
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003306 * about case, but it's necessary for compatibility with Myspell.
3307 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003308 regmatch.regprog = ae->ae_prog;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003309 regmatch.rm_ic = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003310 if (ae->ae_prog == NULL
3311 || vim_regexec(&regmatch, dw->dw_word, (colnr_T)0))
3312 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003313 if ((ae->ae_add_nw != NULL || ae->ae_add_pw != NULL)
3314 && (gap != &bw->bw_suffix || bw->bw_addstring == NULL))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003315 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003316 /*
3317 * Affix has a non-word character and isn't prepended to
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003318 * leader or appended to addition. Need to use another
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003319 * word with a leadstring and/or addstring.
3320 */
3321 if (gap == &bw->bw_suffix || ae->ae_add_nw == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003322 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003323 /* Suffix or prefix with only non-word chars.
3324 * Build the new basic word in "nword": Remove chop
3325 * string and append/prepend addition. */
3326 if (gap == &bw->bw_suffix)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003327 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003328 /* suffix goes at the end of the word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003329 STRCPY(nword, dw->dw_word);
3330 if (ae->ae_chop != NULL)
3331 {
3332 /* Remove chop string. */
3333 p = nword + STRLEN(nword);
3334 for (i = mb_charlen(ae->ae_chop); i > 0; --i)
3335 mb_ptr_back(nword, p);
3336 *p = NUL;
3337 }
3338 STRCAT(nword, ae->ae_add);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003339 agap = &suffixga;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003340 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003341 else
Bram Moolenaar5482f332005-04-17 20:18:43 +00003342 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003343 /* prefix goes before the word */
Bram Moolenaar5482f332005-04-17 20:18:43 +00003344 STRCPY(nword, ae->ae_add);
3345 p = dw->dw_word;
3346 if (ae->ae_chop != NULL)
3347 /* Skip chop string. */
3348 for (i = mb_charlen(ae->ae_chop); i > 0; --i)
3349 mb_ptr_adv( p);
3350 STRCAT(nword, p);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003351 agap = &prefixga;
3352 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003353
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003354 /* Create a basicword_T from the word. */
3355 nbw = get_basicword(nword, 1);
3356 if (nbw != NULL)
3357 {
3358 nbw->bw_region = bw->bw_region;
3359 nbw->bw_flags |= bw->bw_flags
3360 & ~(BWF_ONECAP | BWF_ALLCAP | BWF_KEEPCAP);
Bram Moolenaar5482f332005-04-17 20:18:43 +00003361
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003362 if (STRCMP(bw->bw_word, nbw->bw_word) != 0)
3363 /* Basic word differs, add new word entry. */
3364 (void)add_to_wordlist(newwords, nbw);
Bram Moolenaar5482f332005-04-17 20:18:43 +00003365 else
3366 {
3367 /* Basic word is the same, link "nbw" after
3368 * "bw". */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003369 nbw->bw_next = bw->bw_next;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003370 bw->bw_next = nbw;
3371 }
3372
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003373 /* Remember this word, we need to set bw_prefix
3374 * or bw_suffix later. */
3375 if (ga_grow(agap, 1) == OK)
3376 ((basicword_T **)agap->ga_data)
3377 [agap->ga_len++] = nbw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003378 }
3379 }
3380 else
3381 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003382 /* Prefix with both non-word and word characters: Turn
3383 * prefix into basic word, original word becomes an
3384 * addstring. */
3385
3386 /* Fold-case the word characters in the prefix into
3387 * nword[]. */
3388 alen = 0;
3389 for (p = ae->ae_add_pw; p < ae->ae_add_nw; p += n)
3390 {
3391#ifdef FEAT_MBYTE
3392 n = (*mb_ptr2len_check)(p);
3393#else
3394 n = 1;
3395#endif
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003396 (void)spell_casefold(p, n, nword + alen,
Bram Moolenaar5482f332005-04-17 20:18:43 +00003397 MAXWLEN - alen);
3398 alen += STRLEN(nword + alen);
3399 }
3400
3401 /* Allocate a new word entry. */
3402 nbw = (basicword_T *)alloc((unsigned)(
3403 sizeof(basicword_T) + alen + 1));
3404 if (nbw != NULL)
3405 {
3406 *nbw = *bw;
3407 ga_init2(&nbw->bw_prefix, sizeof(short_u), 1);
3408 ga_init2(&nbw->bw_suffix, sizeof(short_u), 1);
3409
3410 mch_memmove(nbw->bw_word, nword, alen);
3411 nbw->bw_word[alen] = NUL;
3412
3413 /* Use the cap type of the prefix. */
3414 alen = ae->ae_add_nw - ae->ae_add_pw;
3415 mch_memmove(nword, ae->ae_add_pw, alen);
3416 nword[alen] = NUL;
3417 flags = captype(nword, nword + STRLEN(nword));
3418 if (flags & BWF_KEEPCAP)
3419 nbw->bw_caseword = vim_strsave(nword);
3420 else
3421 nbw->bw_caseword = NULL;
3422 nbw->bw_flags &= ~(BWF_ONECAP | BWF_ALLCAP
3423 | BWF_KEEPCAP);
3424 nbw->bw_flags |= flags;
3425
3426 /* The addstring is the prefix after the word
3427 * characters, the original word excluding "chop",
3428 * plus any addition. */
3429 STRCPY(nword, ae->ae_add_nw);
3430 p = bw->bw_word;
3431 if (ae->ae_chop != NULL)
3432 p += STRLEN(ae->ae_chop);
3433 STRCAT(nword, p);
3434 if (bw->bw_addstring != NULL)
3435 STRCAT(nword, bw->bw_addstring);
3436 nbw->bw_addstring = vim_strsave(nword);
3437
3438 if (ae->ae_add_pw > ae->ae_add)
3439 nbw->bw_leadstring = vim_strnsave(ae->ae_add,
3440 ae->ae_add_pw - ae->ae_add);
3441 else
3442 nbw->bw_leadstring = NULL;
3443
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003444 (void)add_to_wordlist(newwords, nbw);
Bram Moolenaar5482f332005-04-17 20:18:43 +00003445
3446 /* Remember this word, we need to set bw_suffix
3447 * and bw_suffix later. */
3448 if (ga_grow(&prefixga, 1) == OK)
3449 ((basicword_T **)prefixga.ga_data)
3450 [prefixga.ga_len++] = nbw;
3451 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003452 }
3453 }
3454 else
3455 {
3456 /* Affix applies to this word, add the related affix
3457 * number. But only if it's not there yet. And keep the
3458 * list sorted, so that we can compare it later. */
3459 for (i = 0; i < gap->ga_len; ++i)
3460 {
3461 n = ((short_u *)gap->ga_data)[i];
3462 if (n >= ae->ae_affnr)
3463 {
3464 if (n == ae->ae_affnr)
3465 i = -1;
3466 break;
3467 }
3468 }
3469 if (i >= 0 && ga_grow(gap, 1) == OK)
3470 {
3471 if (i < gap->ga_len)
3472 mch_memmove(((short_u *)gap->ga_data) + i + 1,
3473 ((short_u *)gap->ga_data) + i,
3474 sizeof(short_u) * (gap->ga_len - i));
3475 ((short_u *)gap->ga_data)[i] = ae->ae_affnr;
3476 ++gap->ga_len;
3477 }
3478 }
3479 }
3480 }
3481 }
3482
3483 /*
3484 * For the words that we added for suffixes with non-word characters: Use
3485 * the prefix list of the main word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003486 */
Bram Moolenaar5482f332005-04-17 20:18:43 +00003487 for (i = 0; i < suffixga.ga_len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003488 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003489 nbw = ((basicword_T **)suffixga.ga_data)[i];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003490 if (ga_grow(&nbw->bw_prefix, bw->bw_prefix.ga_len) == OK)
3491 {
3492 mch_memmove(nbw->bw_prefix.ga_data, bw->bw_prefix.ga_data,
3493 bw->bw_prefix.ga_len * sizeof(short_u));
3494 nbw->bw_prefix.ga_len = bw->bw_prefix.ga_len;
3495 }
3496 }
3497
Bram Moolenaar5482f332005-04-17 20:18:43 +00003498 /*
3499 * For the words that we added for prefixes with non-word characters: Use
3500 * the suffix list of the main word.
3501 */
3502 for (i = 0; i < prefixga.ga_len; ++i)
3503 {
3504 nbw = ((basicword_T **)prefixga.ga_data)[i];
3505 if (ga_grow(&nbw->bw_suffix, bw->bw_suffix.ga_len) == OK)
3506 {
3507 mch_memmove(nbw->bw_suffix.ga_data, bw->bw_suffix.ga_data,
3508 bw->bw_suffix.ga_len * sizeof(short_u));
3509 nbw->bw_suffix.ga_len = bw->bw_suffix.ga_len;
3510 }
3511 }
3512
3513 ga_clear(&suffixga);
3514 ga_clear(&prefixga);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003515}
3516
3517/*
3518 * Go over all words in "oldwords" and change the old affix names to the new
3519 * affix numbers, check the conditions, fold case, extract the basic word and
3520 * additions.
3521 */
3522 static int
3523build_wordlist(newwords, oldwords, oldaff, regionmask)
3524 hashtab_T *newwords; /* basicword_T entries */
3525 hashtab_T *oldwords; /* dicword_T entries */
3526 afffile_T *oldaff; /* affixes for "oldwords" */
3527 int regionmask; /* value for bw_region */
3528{
3529 int todo;
3530 hashitem_T *old_hi;
3531 dicword_T *dw;
3532 basicword_T *bw;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003533 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003534
3535 todo = oldwords->ht_used;
3536 for (old_hi = oldwords->ht_array; todo > 0; ++old_hi)
3537 {
3538 if (!HASHITEM_EMPTY(old_hi))
3539 {
3540 --todo;
3541 dw = HI2DW(old_hi);
3542
3543 /* This takes time, print a message now and then. */
Bram Moolenaar5482f332005-04-17 20:18:43 +00003544 if ((todo & 0x3ff) == 0 || todo == (int)oldwords->ht_used - 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003545 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003546 sprintf((char *)message, _("%6d todo - %s"),
3547 todo, dw->dw_word);
3548 msg_start();
3549 msg_outtrans_attr(message, 0);
3550 msg_clr_eos();
3551 msg_didout = FALSE;
3552 msg_col = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003553 out_flush();
3554 ui_breakcheck();
3555 if (got_int)
3556 break;
3557 }
3558
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003559 bw = get_basicword(dw->dw_word, 10);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003560 if (bw == NULL)
3561 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003562 bw->bw_region = regionmask;
3563
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003564 (void)add_to_wordlist(newwords, bw);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003565
3566 /* Deal with any affix names on the old word, translate them
3567 * into affix numbers. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003568 if (dw->dw_affnm != NULL)
3569 trans_affixes(dw, bw, oldaff, newwords);
3570 }
3571 }
3572 if (todo > 0)
3573 return FAIL;
3574 return OK;
3575}
3576
3577/*
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003578 * Get a basicword_T from a word in original case.
3579 * Caller must set bw_region.
3580 * Returns NULL when something fails.
3581 */
3582 static basicword_T *
3583get_basicword(word, asize)
3584 char_u *word;
3585 int asize; /* growsize for affix garray */
3586{
3587 int dwlen;
3588 char_u foldword[MAXLINELEN];
3589 int flags;
3590 int clen;
3591 int leadlen;
3592 char_u *p;
3593 char_u leadstring[MAXLINELEN];
3594 int addlen;
3595 char_u addstring[MAXLINELEN];
3596 char_u *cp = NULL;
3597 int l;
3598 basicword_T *bw;
3599
3600 /* The basic words are always stored with folded case. */
3601 dwlen = STRLEN(word);
3602 (void)spell_casefold(word, dwlen, foldword, MAXLINELEN);
3603 flags = captype(word, word + dwlen);
3604
3605 /* Check for non-word characters before the word. */
3606 clen = 0;
3607 leadlen = 0;
3608 if (!spell_iswordc(foldword))
3609 {
3610 p = foldword;
3611 for (;;)
3612 {
3613 mb_ptr_adv(p);
3614 ++clen;
3615 if (*p == NUL) /* Only non-word chars (bad word!) */
3616 {
3617 if (p_verbose > 0)
3618 smsg((char_u *)_("Warning: word without word characters: \"%s\""),
3619 foldword);
3620 break;
3621 }
3622 if (spell_iswordc(p))
3623 {
3624 /* Move the leader to "leadstring" and remove it from
3625 * "foldword". */
3626 leadlen = p - foldword;
3627 mch_memmove(leadstring, foldword, leadlen);
3628 leadstring[leadlen] = NUL;
3629 mch_memmove(foldword, p, STRLEN(p) + 1);
3630 break;
3631 }
3632 }
3633 }
3634
3635 /* Check for non-word characters after word characters. */
3636 addlen = 0;
3637 for (p = foldword; spell_iswordc(p); mb_ptr_adv(p))
3638 {
3639 if (*p == NUL)
3640 break;
3641 ++clen;
3642 }
3643 if (*p != NUL)
3644 {
3645 /* Move the addition to "addstring" and truncate "foldword". */
3646 if (flags & BWF_KEEPCAP)
3647 {
3648 /* Preserve caps, need to skip the right number of
3649 * characters in the original word (case folding may
3650 * change the byte count). */
3651 l = 0;
3652 for (cp = word; l < clen; mb_ptr_adv(cp))
3653 ++l;
3654 addlen = STRLEN(cp);
3655 mch_memmove(addstring, cp, addlen + 1);
3656 }
3657 else
3658 {
3659 addlen = STRLEN(p);
3660 mch_memmove(addstring, p, addlen + 1);
3661 }
3662 *p = NUL;
3663 }
3664
3665 bw = (basicword_T *)alloc_clear((unsigned)sizeof(basicword_T)
3666 + STRLEN(foldword));
3667 if (bw == NULL)
3668 return NULL;
3669
3670 STRCPY(bw->bw_word, foldword);
3671
3672 if (leadlen > 0)
3673 bw->bw_leadstring = vim_strsave(leadstring);
3674 else
3675 bw->bw_leadstring = NULL;
3676 if (addlen > 0)
3677 bw->bw_addstring = vim_strsave(addstring);
3678 else
3679 bw->bw_addstring = NULL;
3680
3681 if (flags & BWF_KEEPCAP)
3682 {
3683 if (addlen == 0)
3684 /* use the whole word */
3685 bw->bw_caseword = vim_strsave(word + leadlen);
3686 else
3687 /* use only up to the addition */
3688 bw->bw_caseword = vim_strnsave(word + leadlen,
3689 cp - word - leadlen);
3690 }
3691
3692 bw->bw_flags = flags;
3693 ga_init2(&bw->bw_prefix, sizeof(short_u), asize);
3694 ga_init2(&bw->bw_suffix, sizeof(short_u), asize);
3695
3696 return bw;
3697}
3698
3699/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003700 * Go through the list of words and combine the ones that are identical except
3701 * for the region.
3702 */
3703 static void
3704combine_regions(newwords)
3705 hashtab_T *newwords;
3706{
3707 int todo;
3708 hashitem_T *hi;
3709 basicword_T *bw, *nbw, *pbw;
3710
3711 /* Loop over all basic words in the words table. */
3712 todo = newwords->ht_used;
3713 for (hi = newwords->ht_array; todo > 0; ++hi)
3714 {
3715 if (!HASHITEM_EMPTY(hi))
3716 {
3717 --todo;
3718
3719 /* Loop over the list of words for this basic word. Compare with
3720 * each following word in the same list. */
3721 for (bw = HI2BW(hi); bw != NULL; bw = bw->bw_next)
3722 {
3723 pbw = bw;
3724 for (nbw = pbw->bw_next; nbw != NULL; nbw = pbw->bw_next)
3725 {
3726 if (bw->bw_flags == nbw->bw_flags
3727 && (bw->bw_leadstring == NULL)
3728 == (nbw->bw_leadstring == NULL)
3729 && (bw->bw_addstring == NULL)
3730 == (nbw->bw_addstring == NULL)
3731 && ((bw->bw_flags & BWF_KEEPCAP) == 0
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003732 || bw->bw_caseword == NULL
3733 || nbw->bw_caseword == NULL
3734 || STRCMP(bw->bw_caseword,
3735 nbw->bw_caseword) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003736 && (bw->bw_leadstring == NULL
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003737 || STRCMP(bw->bw_leadstring,
3738 nbw->bw_leadstring) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003739 && (bw->bw_addstring == NULL
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003740 || STRCMP(bw->bw_addstring,
3741 nbw->bw_addstring) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003742 && same_affixes(bw, nbw)
3743 )
3744 {
3745 /* Match, combine regions and delete "nbw". */
3746 pbw->bw_next = nbw->bw_next;
3747 bw->bw_region |= nbw->bw_region;
3748 free_basicword(nbw);
3749 }
3750 else
3751 /* No match, continue with next one. */
3752 pbw = nbw;
3753 }
3754 }
3755 }
3756 }
3757}
3758
3759/*
3760 * Return TRUE when the prefixes and suffixes for "bw" and "nbw" are equal.
3761 */
3762 static int
3763same_affixes(bw, nbw)
3764 basicword_T *bw;
3765 basicword_T *nbw;
3766{
3767 return (bw->bw_prefix.ga_len == nbw->bw_prefix.ga_len
3768 && bw->bw_suffix.ga_len == nbw->bw_suffix.ga_len
3769 && (bw->bw_prefix.ga_len == 0
3770 || vim_memcmp(bw->bw_prefix.ga_data,
3771 nbw->bw_prefix.ga_data,
3772 bw->bw_prefix.ga_len * sizeof(short_u)) == 0)
3773 && (bw->bw_suffix.ga_len == 0
3774 || vim_memcmp(bw->bw_suffix.ga_data,
3775 nbw->bw_suffix.ga_data,
3776 bw->bw_suffix.ga_len * sizeof(short_u)) == 0));
3777}
3778
3779/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003780 * For each basic word with additions turn the suffixes into other additions
3781 * and/or new basic words. For each basic word with a leadstring turn the
3782 * prefixes into other leadstrings and/or new basic words.
3783 * The result is that no affixes apply to the additions or leadstring of a
3784 * word.
3785 * This is also needed when a word with an addition has a prefix and the word
3786 * with prefix also exists. E.g., "blurp's/D" (D is prefix "de") and
3787 * "deblurp". "deblurp" would match and no prefix would be tried.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003788 *
3789 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003790 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003791 static int
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003792expand_affixes(newwords, prefgap, suffgap)
3793 hashtab_T *newwords;
3794 garray_T *prefgap;
3795 garray_T *suffgap;
3796{
3797 int todo;
3798 hashitem_T *hi;
3799 basicword_T *bw;
3800 int pi, si;
3801 affentry_T *pae, *sae;
3802 garray_T add_words;
3803 int n;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003804 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003805 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003806
3807 ga_init2(&add_words, sizeof(basicword_T *), 10);
3808
3809 todo = newwords->ht_used;
3810 for (hi = newwords->ht_array; todo > 0; ++hi)
3811 {
3812 if (!HASHITEM_EMPTY(hi))
3813 {
3814 --todo;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003815
3816 /* This takes time, print a message now and then. */
3817 if ((todo & 0x3ff) == 0 || todo == (int)newwords->ht_used - 1)
3818 {
3819 sprintf((char *)message, _("%6d todo - %s"),
3820 todo, HI2BW(hi)->bw_word);
3821 msg_start();
3822 msg_outtrans_attr(message, 0);
3823 msg_clr_eos();
3824 msg_didout = FALSE;
3825 msg_col = 0;
3826 out_flush();
3827 ui_breakcheck();
3828 if (got_int)
3829 break;
3830 }
3831
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003832 for (bw = HI2BW(hi); bw != NULL; bw = bw->bw_next)
3833 {
3834 /*
3835 * Need to fix affixes if there is a leader or addition and
3836 * there are prefixes or suffixes.
3837 */
3838 if ((bw->bw_leadstring != NULL || bw->bw_addstring != NULL)
3839 && (bw->bw_prefix.ga_len != 0
3840 || bw->bw_suffix.ga_len != 0))
3841 {
3842 /* Loop over all prefix numbers, but first without a
3843 * prefix. */
3844 for (pi = -1; pi < bw->bw_prefix.ga_len; ++pi)
3845 {
3846 pae = NULL;
3847 if (pi >= 0)
3848 {
3849 n = ((short_u *)bw->bw_prefix.ga_data)[pi];
3850 pae = ((affheader_T *)prefgap->ga_data + n)
3851 ->ah_first;
3852 }
3853
3854 /* Loop over all entries for prefix "pi". Do it once
3855 * when there is no prefix (pi == -1). */
3856 do
3857 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003858 /* Skip prewords, they don't need to be expanded. */
3859 if (pae == NULL || !pae->ae_preword)
3860 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003861 /* Loop over all suffix numbers. Do without a
3862 * suffix first when there is a prefix. */
3863 for (si = (pi == -1 ? 0 : -1);
3864 si < bw->bw_suffix.ga_len; ++si)
3865 {
3866 sae = NULL;
3867 if (si >= 0)
3868 {
3869 n = ((short_u *)bw->bw_suffix.ga_data)[si];
3870 sae = ((affheader_T *)suffgap->ga_data + n)
3871 ->ah_first;
3872 }
3873
3874 /* Loop over all entries for suffix "si". Do
3875 * it once when there is no suffix (si == -1).
3876 */
3877 do
3878 {
3879 /* Expand the word for this combination of
3880 * prefixes and affixes. */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003881 if (expand_one_aff(bw, &add_words,
3882 pae, sae) == FAIL)
3883 {
3884 retval = FAIL;
3885 goto theend;
3886 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003887
3888 /* Advance to next suffix entry, if there
3889 * is one. */
3890 if (sae != NULL)
3891 sae = sae->ae_next;
3892 } while (sae != NULL);
3893 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003894 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003895
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003896 /* Advance to next prefix entry, if there is one. */
3897 if (pae != NULL)
3898 pae = pae->ae_next;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003899 } while (pae != NULL);
3900 }
3901 }
3902 }
3903 }
3904 }
3905
3906 /*
3907 * Add the new words afterwards, can't change "newwords" while going over
3908 * all its items.
3909 */
3910 for (pi = 0; pi < add_words.ga_len; ++pi)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003911 {
3912 retval = add_to_wordlist(newwords,
3913 ((basicword_T **)add_words.ga_data)[pi]);
3914 if (retval == FAIL)
3915 break;
3916 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003917
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003918theend:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003919 ga_clear(&add_words);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003920 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003921}
3922
3923/*
3924 * Add one word to "add_words" for basic word "bw" with additions, adding
3925 * prefix "pae" and suffix "sae". Either "pae" or "sae" can be NULL.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003926 * Don't do this when not necessary:
3927 * - no leadstring and adding prefix doesn't result in existing word.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003928 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003929 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003930 static int
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003931expand_one_aff(bw, add_words, pae, sae)
3932 basicword_T *bw;
3933 garray_T *add_words;
3934 affentry_T *pae;
3935 affentry_T *sae;
3936{
3937 char_u word[MAXWLEN + 1];
3938 char_u caseword[MAXWLEN + 1];
3939 int l = 0;
3940 int choplen = 0;
3941 int ll;
3942 basicword_T *nbw;
3943
3944 /* Prepend prefix to the basic word if there is a prefix and there is no
3945 * leadstring. */
3946 if (pae != NULL && bw->bw_leadstring == NULL)
3947 {
3948 if (pae->ae_add != NULL)
3949 {
3950 l = STRLEN(pae->ae_add);
3951 mch_memmove(word, pae->ae_add, l);
3952 }
3953 if (pae->ae_chop != NULL)
3954 choplen = STRLEN(pae->ae_chop);
3955 }
3956
3957 /* Copy the body of the word. */
3958 STRCPY(word + l, bw->bw_word + choplen);
3959
3960 /* Do the same for bw_caseword, if it's there. */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003961 if ((bw->bw_flags & BWF_KEEPCAP) && bw->bw_caseword != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003962 {
3963 if (l > 0)
3964 mch_memmove(caseword, pae->ae_add, l);
3965 STRCPY(caseword + l, bw->bw_caseword + choplen);
3966 }
3967
3968 /* Append suffix to the basic word if there is a suffix and there is no
3969 * addstring. */
3970 if (sae != 0 && bw->bw_addstring == NULL)
3971 {
3972 l = STRLEN(word);
3973 if (sae->ae_chop != NULL)
3974 l -= STRLEN(sae->ae_chop);
3975 if (sae->ae_add == NULL)
3976 word[l] = NUL;
3977 else
3978 STRCPY(word + l, sae->ae_add);
3979
3980 if (bw->bw_flags & BWF_KEEPCAP)
3981 {
3982 /* Do the same for the caseword. */
3983 l = STRLEN(caseword);
3984 if (sae->ae_chop != NULL)
3985 l -= STRLEN(sae->ae_chop);
3986 if (sae->ae_add == NULL)
3987 caseword[l] = NUL;
3988 else
3989 STRCPY(caseword + l, sae->ae_add);
3990 }
3991 }
3992
3993 nbw = (basicword_T *)alloc_clear((unsigned)
3994 sizeof(basicword_T) + STRLEN(word));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003995 if (nbw == NULL)
3996 return FAIL;
3997
3998 /* Add the new word to the list of words to be added later. */
3999 if (ga_grow(add_words, 1) == FAIL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004000 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004001 vim_free(nbw);
4002 return FAIL;
4003 }
4004 ((basicword_T **)add_words->ga_data)[add_words->ga_len++] = nbw;
4005
4006 /* Copy the (modified) basic word, flags and region. */
4007 STRCPY(nbw->bw_word, word);
4008 nbw->bw_flags = bw->bw_flags;
4009 nbw->bw_region = bw->bw_region;
4010
4011 /* Set the (modified) caseword. */
4012 if (bw->bw_flags & BWF_KEEPCAP)
4013 nbw->bw_caseword = vim_strsave(caseword);
4014 else
4015 nbw->bw_caseword = NULL;
4016
4017 if (bw->bw_leadstring != NULL)
4018 {
4019 if (pae != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004020 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004021 /* Prepend prefix to leadstring. */
4022 ll = STRLEN(bw->bw_leadstring);
4023 l = choplen = 0;
4024 if (pae->ae_add != NULL)
4025 l = STRLEN(pae->ae_add);
4026 if (pae->ae_chop != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004027 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004028 choplen = STRLEN(pae->ae_chop);
4029 if (choplen > ll) /* TODO: error? */
4030 choplen = ll;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004031 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004032 nbw->bw_leadstring = alloc((unsigned)(ll + l - choplen + 1));
4033 if (nbw->bw_leadstring != NULL)
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004034 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004035 if (l > 0)
4036 mch_memmove(nbw->bw_leadstring, pae->ae_add, l);
4037 STRCPY(nbw->bw_leadstring + l, bw->bw_leadstring + choplen);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004038 }
4039 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004040 else
4041 nbw->bw_leadstring = vim_strsave(bw->bw_leadstring);
4042 }
4043 else if (bw->bw_prefix.ga_len > 0)
4044 {
4045 /* There is no leadstring, copy the list of possible prefixes. */
4046 ga_init2(&nbw->bw_prefix, sizeof(short_u), 1);
4047 if (ga_grow(&nbw->bw_prefix, bw->bw_prefix.ga_len) == OK)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004048 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004049 mch_memmove(nbw->bw_prefix.ga_data, bw->bw_prefix.ga_data,
4050 bw->bw_prefix.ga_len * sizeof(short_u));
4051 nbw->bw_prefix.ga_len = bw->bw_prefix.ga_len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004052 }
4053 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004054
4055 if (bw->bw_addstring != NULL)
4056 {
4057 if (sae != NULL)
4058 {
4059 /* Append suffix to addstring. */
4060 l = STRLEN(bw->bw_addstring);
4061 if (sae->ae_chop != NULL)
4062 {
4063 l -= STRLEN(sae->ae_chop);
4064 if (l < 0) /* TODO: error? */
4065 l = 0;
4066 }
4067 if (sae->ae_add == NULL)
4068 ll = 0;
4069 else
4070 ll = STRLEN(sae->ae_add);
4071 nbw->bw_addstring = alloc((unsigned)(ll + l - choplen + 1));
4072 if (nbw->bw_addstring != NULL)
4073 {
4074 STRCPY(nbw->bw_addstring, bw->bw_addstring);
4075 if (sae->ae_add == NULL)
4076 nbw->bw_addstring[l] = NUL;
4077 else
4078 STRCPY(nbw->bw_addstring + l, sae->ae_add);
4079 }
4080 }
4081 else
4082 nbw->bw_addstring = vim_strsave(bw->bw_addstring);
4083 }
4084
4085 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004086}
4087
4088/*
4089 * Add basicword_T "*bw" to wordlist "newwords".
4090 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004091 static int
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004092add_to_wordlist(newwords, bw)
4093 hashtab_T *newwords;
4094 basicword_T *bw;
4095{
4096 hashitem_T *hi;
4097 basicword_T *bw2;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004098 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004099
4100 hi = hash_find(newwords, bw->bw_word);
4101 if (HASHITEM_EMPTY(hi))
4102 {
4103 /* New entry, add to hashlist. */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004104 retval = hash_add(newwords, bw->bw_word);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004105 bw->bw_next = NULL;
4106 }
4107 else
4108 {
4109 /* Existing entry, append to list of basic words. */
4110 bw2 = HI2BW(hi);
4111 bw->bw_next = bw2->bw_next;
4112 bw2->bw_next = bw;
4113 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004114 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004115}
4116
4117/*
4118 * Write a number to file "fd", MSB first, in "len" bytes.
4119 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004120 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004121put_bytes(fd, nr, len)
4122 FILE *fd;
4123 long_u nr;
4124 int len;
4125{
4126 int i;
4127
4128 for (i = len - 1; i >= 0; --i)
4129 putc((int)(nr >> (i * 8)), fd);
4130}
4131
4132/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004133 * Write affix info. <affitemcnt> <affitem> ...
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004134 */
4135 static void
4136write_affix(fd, ah)
4137 FILE *fd;
4138 affheader_T *ah;
4139{
4140 int i = 0;
4141 affentry_T *ae;
4142 char_u *p;
4143 int round;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004144 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004145
4146 /* Count the number of entries. */
4147 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
4148 ++i;
4149 put_bytes(fd, (long_u)i, 2); /* <affitemcnt> */
4150
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004151 /* <affitem>: <affflags> <affchoplen> <affchop> <affaddlen> <affadd> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004152 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004153 {
4154 flags = ah->ah_combine ? AFF_COMBINE : 0;
4155 if (ae->ae_preword)
4156 flags |= AFF_PREWORD;
4157 fputc(flags, fd); /* <affflags> */
4158
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004159 for (round = 1; round <= 2; ++round)
4160 {
4161 p = round == 1 ? ae->ae_chop : ae->ae_add;
4162 if (p == NULL)
4163 putc(0, fd); /* <affchoplen> / <affaddlen> */
4164 else
4165 {
4166 putc(STRLEN(p), fd); /* <affchoplen> / <affaddlen> */
4167 /* <affchop> / <affadd> */
4168 fwrite(p, STRLEN(p), (size_t)1, fd);
4169 }
4170 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004171 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004172}
4173
4174/*
4175 * Write list of affix NRs: <affixcnt> <affixNR> ...
4176 */
4177 static void
4178write_affixlist(fd, aff, bytes)
4179 FILE *fd;
4180 garray_T *aff;
4181 int bytes;
4182{
4183 int i;
4184
4185 if (aff->ga_len > 0)
4186 {
4187 putc(aff->ga_len, fd); /* <affixcnt> */
4188 for (i = 0; i < aff->ga_len; ++i)
4189 put_bytes(fd, (long_u )((short_u *)aff->ga_data)[i], bytes);
4190 }
4191}
4192
4193/*
4194 * Vim spell file format: <HEADER> <PREFIXLIST> <SUFFIXLIST>
4195 * <SUGGEST> <WORDLIST>
4196 *
4197 * <HEADER>: <fileID> <regioncnt> <regionname> ...
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004198 * <charflagslen> <charflags> <fcharslen> <fchars>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004199 *
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004200 * <fileID> 10 bytes "VIMspell04"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004201 * <regioncnt> 1 byte number of regions following (8 supported)
4202 * <regionname> 2 bytes Region name: ca, au, etc.
4203 * First <regionname> is region 1.
4204 *
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004205 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
4206 * <charflags> N bytes List of flags (first one is for character 128):
4207 * 0x01 word character
4208 * 0x01 upper-case character
4209 * <fcharslen> 2 bytes Number of bytes in <fchars>.
4210 * <fchars> N bytes Folded characters, first one is for character 128.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004211 *
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004212 *
4213 * <PREFIXLIST>: <affcount> <affix> ...
4214 * <SUFFIXLIST>: <affcount> <affix> ...
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004215 * list of possible affixes: prefixes and suffixes.
4216 *
4217 * <affcount> 2 bytes Number of affixes (MSB comes first).
4218 * When more than 256 an affixNR is 2 bytes.
4219 * This is separate for prefixes and suffixes!
4220 * First affixNR is 0.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004221 *
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004222 * <affix>: <affitemcnt> <affitem> ...
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004223 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004224 * <affitemcnt> 2 bytes Number of affixes with this affixNR (MSB first).
4225 *
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004226 * <affitem>: <affflags> <affchoplen> <affchop> <affaddlen> <affadd>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004227 *
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004228 * <affflags> 1 byte 0x01: prefix combines with suffix, AFF_COMBINE
4229 * 0x02: prefix includes word, AFF_PREWORD
4230 * 0x04-0x80: unset
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004231 * <affchoplen> 1 byte Length of <affchop> in bytes.
4232 * <affchop> N bytes To be removed from basic word.
4233 * <affaddlen> 1 byte Length of <affadd> in bytes.
4234 * <affadd> N bytes To be added to basic word.
4235 *
4236 *
4237 * <SUGGEST> : <suggestlen> <more> ...
4238 *
4239 * <suggestlen> 4 bytes Length of <SUGGEST> in bytes, excluding
4240 * <suggestlen>. MSB first.
4241 * <more> To be defined.
4242 *
4243 *
4244 * <WORDLIST>: <wordcount> <worditem> ...
4245 *
4246 * <wordcount> 4 bytes Number of <worditem> following. MSB first.
4247 *
4248 * <worditem>: <nr> <string> <flags> [<flags2>]
4249 * [<caselen> <caseword>]
4250 * [<affixcnt> <affixNR> ...] (prefixes)
4251 * [<affixcnt> <affixNR> ...] (suffixes)
4252 * [<region>]
4253 * [<addcnt> <add> ...]
4254 *
4255 * <nr> i 1 byte Number of bytes copied from previous word.
4256 * <string> N bytes Additional bytes for word, up to byte smaller than
4257 * 0x20 (space).
4258 * Must only contain case-folded word characters.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004259 * <flags> 1 byte 0x01: word is valid without addition, BWF_VALID
4260 * 0x02: has region byte, BWF_REGION
4261 * 0x04: first letter must be upper-case, BWF_ONECAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004262 * 0x08: has suffixes, <affixcnt> and <affixNR> follow
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004263 * BWF_SUFFIX
4264 * 0x10: more flags, <flags2> follows next, BWF_SECOND
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004265 * 0x20-0x80: can't be used, unset
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004266 * <flags2> 1 byte 0x01: has additions, <addcnt> and <add> follow,
4267 * BWF_ADDS
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004268 * 0x02: has prefixes, <affixcnt> and <affixNR> follow
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004269 * BWF_PREFIX
4270 * 0x04: all letters must be upper-case, BWF_ALLCAP
4271 * 0x08: case must match, BWF_KEEPCAP
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004272 * 0x10: has more than 255 additions, <addcnt> is two
4273 * bytes, BWF_ADDS_M
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004274 * 0x10-0x80: unset
4275 * <caselen> 1 byte Length of <caseword>.
4276 * <caseword> N bytes Word with matching case.
4277 * <affixcnt> 1 byte Number of affix NRs following.
4278 * <affixNR> 1 or 2 byte Number of possible affix for this word.
4279 * When using 2 bytes MSB comes first.
4280 * <region> 1 byte Bitmask for regions in which word is valid. When
4281 * omitted it's valid in all regions.
4282 * Lowest bit is for region 1.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004283 * <addcnt> 1 or 2 byte Number of <add> items following.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004284 *
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004285 * <add>: <addflags> <addlen> [<leadlen>] [<copylen>] [<addstring>] [<region>]
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004286 *
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004287 * <addflags> 1 byte 0x01: unset
4288 * 0x02: has region byte, ADD_REGION
4289 * 0x04: first letter must be upper-case, ADD_ONECAP
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004290 * 0x08: unset
4291 * 0x10: has a <leadlen>, ADD_LEADLEN
4292 * 0x20: has a <copylen>, ADD_COPYLEN
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004293 * 0x40: all letters must be upper-case, ADD_ALLCAP
4294 * 0x80: fixed case, <addstring> is the whole word
4295 * with matching case, ADD_KEEPCAP.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004296 * <addlen> 1 byte Length of <addstring> in bytes.
4297 * <leadlen> 1 byte Number of bytes at start of <addstring> that must
4298 * come before the start of the basic word.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004299 * <copylen> 1 byte Number of bytes copied from previous <addstring>.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004300 * <addstring> N bytes Word characters, before/in/after the word.
4301 *
4302 * All text characters are in 'encoding': <affchop>, <affadd>, <string>,
4303 * <caseword>> and <addstring>.
4304 * All other fields are ASCII: <regionname>
4305 * <string> is always case-folded.
4306 */
4307
4308/*
4309 * Write the Vim spell file "fname".
4310 */
4311 static void
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004312write_vim_spell(fname, prefga, suffga, newwords, regcount, regchars, ascii)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004313 char_u *fname;
4314 garray_T *prefga; /* prefixes, affheader_T entries */
4315 garray_T *suffga; /* suffixes, affheader_T entries */
4316 hashtab_T *newwords; /* basic words, basicword_T entries */
4317 int regcount; /* number of regions */
4318 char_u *regchars; /* region names */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004319 int ascii; /* TRUE for ascii spell file */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004320{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004321 winfo_T wif;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004322 garray_T *gap;
4323 hashitem_T *hi;
4324 char_u **wtab;
4325 int todo;
4326 int flags, aflags;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004327 basicword_T *bw, *bwf, *bw2 = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004328 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004329 int round;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004330 garray_T bwga;
4331
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004332 vim_memset(&wif, 0, sizeof(winfo_T));
4333
4334 wif.wif_fd = fopen((char *)fname, "w");
4335 if (wif.wif_fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004336 {
4337 EMSG2(_(e_notopen), fname);
4338 return;
4339 }
4340
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004341 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
4342 * <charflagslen> <charflags> <fcharslen> <fchars> */
4343 fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, wif.wif_fd); /* <fileID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004344
4345 /* write the region names if there is more than one */
4346 if (regcount > 1)
4347 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004348 putc(regcount, wif.wif_fd); /* <regioncnt> <regionname> ... */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004349 fwrite(regchars, (size_t)(regcount * 2), (size_t)1, wif.wif_fd);
4350 wif.wif_regionmask = (1 << regcount) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004351 }
4352 else
4353 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004354 putc(0, wif.wif_fd);
4355 wif.wif_regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004356 }
4357
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004358 /* Write the table with character flags and table for case folding.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004359 * <charflagslen> <charflags> <fcharlen> <fchars>
4360 * Skip this for ASCII, the table may conflict with the one used for
4361 * 'encoding'. */
4362 if (ascii)
4363 {
4364 putc(0, wif.wif_fd);
4365 putc(0, wif.wif_fd);
4366 putc(0, wif.wif_fd);
4367 }
4368 else
4369 write_spell_chartab(wif.wif_fd);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004370
4371 /* <PREFIXLIST>: <affcount> <affix> ...
4372 * <SUFFIXLIST>: <affcount> <affix> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004373 for (round = 1; round <= 2; ++round)
4374 {
4375 gap = round == 1 ? prefga : suffga;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004376 put_bytes(wif.wif_fd, (long_u)gap->ga_len, 2); /* <affcount> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004377
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004378 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004379 write_affix(wif.wif_fd, (affheader_T *)gap->ga_data + i);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004380 }
4381
4382 /* Number of bytes used for affix NR depends on affix count. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004383 wif.wif_prefm = (prefga->ga_len > 256) ? 2 : 1;
4384 wif.wif_suffm = (suffga->ga_len > 256) ? 2 : 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004385
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004386 /* <SUGGEST> : <suggestlen> <more> ...
4387 * TODO. Only write a zero length for now. */
4388 put_bytes(wif.wif_fd, 0L, 4); /* <suggestlen> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004389
4390 /*
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004391 * <WORDLIST>: <wordcount> <worditem> ...
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004392 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004393
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004394 /* number of basic words in 4 bytes */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004395 put_bytes(wif.wif_fd, newwords->ht_used, 4); /* <wordcount> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004396
4397 /*
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004398 * Sort the word list, so that we can copy as many bytes as possible from
4399 * the previous word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004400 */
4401 wtab = (char_u **)alloc((unsigned)(sizeof(char_u *) * newwords->ht_used));
4402 if (wtab != NULL)
4403 {
4404 /* Make a table with pointers to each word. */
4405 todo = newwords->ht_used;
4406 for (hi = newwords->ht_array; todo > 0; ++hi)
4407 if (!HASHITEM_EMPTY(hi))
4408 wtab[--todo] = hi->hi_key;
4409
4410 /* Sort. */
4411 sort_strings(wtab, (int)newwords->ht_used);
4412
4413 /* Now write each basic word to the spell file. */
4414 ga_init2(&bwga, sizeof(basicword_T *), 10);
Bram Moolenaar5482f332005-04-17 20:18:43 +00004415 for (todo = 0; (long_u)todo < newwords->ht_used; ++todo)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004416 {
4417 bwf = KEY2BW(wtab[todo]);
4418
4419 /*
4420 * Reorder the list of basicword_T words: make a list for words
4421 * with the same case-folded word. Put them together for same
4422 * caps (ONECAP, ALLCAP and various KEEPCAP words) and same
4423 * affixes. Each list will then be put as a basic word with
4424 * additions.
4425 * This won't take much space, since the basic word is the same
4426 * every time, only its length is written.
4427 */
4428 bwga.ga_len = 0;
4429 for (bw = bwf; bw != NULL; bw = bw->bw_next)
4430 {
4431 flags = bw->bw_flags & (BWF_ONECAP | BWF_KEEPCAP | BWF_ALLCAP);
4432
4433 /* Go through the lists we found so far. Break when the case
4434 * matches. */
4435 for (i = 0; i < bwga.ga_len; ++i)
4436 {
4437 bw2 = ((basicword_T **)bwga.ga_data)[i];
4438 aflags = bw2->bw_flags & (BWF_ONECAP | BWF_KEEPCAP
4439 | BWF_ALLCAP);
4440 if (flags == aflags
4441 && ((flags & BWF_KEEPCAP) == 0
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004442 || bw->bw_caseword == NULL
4443 || bw2->bw_caseword == NULL
4444 || STRCMP(bw->bw_caseword,
4445 bw2->bw_caseword) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004446 && same_affixes(bw, bw2))
4447 break;
4448 }
4449 if (i == bwga.ga_len)
4450 {
4451 /* No word with similar caps, make a new list. */
4452 if (ga_grow(&bwga, 1) == FAIL)
4453 break;
4454 ((basicword_T **)bwga.ga_data)[i] = bw;
4455 bw->bw_cnext = NULL;
4456 ++bwga.ga_len;
4457 }
4458 else
4459 {
4460 /* Add to list of words with similar caps. */
4461 bw->bw_cnext = bw2->bw_cnext;
4462 bw2->bw_cnext = bw;
4463 }
4464 }
4465
4466 /* Prefer the word with no caps to use as the first basic word.
4467 * At least one without KEEPCAP. */
4468 bw = NULL;
4469 for (i = 0; i < bwga.ga_len; ++i)
4470 {
4471 bw2 = ((basicword_T **)bwga.ga_data)[i];
4472 if (bw == NULL
4473 || (bw2->bw_flags & (BWF_ONECAP | BWF_KEEPCAP
4474 | BWF_ALLCAP)) == 0
4475 || (bw->bw_flags & BWF_KEEPCAP))
4476 bw = bw2;
4477 }
4478
4479 /* Write first basic word. If it's KEEPCAP then we need a word
4480 * without VALID flag first (makes it easier to read the list back
4481 * in). */
4482 if (bw->bw_flags & BWF_KEEPCAP)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004483 write_bword(&wif, bw, TRUE);
4484 write_bword(&wif, bw, FALSE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004485
4486 /* Write other basic words, with different caps. */
4487 for (i = 0; i < bwga.ga_len; ++i)
4488 {
4489 bw2 = ((basicword_T **)bwga.ga_data)[i];
4490 if (bw2 != bw)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004491 write_bword(&wif, bw2, FALSE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004492 }
4493 }
4494
4495 ga_clear(&bwga);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004496 vim_free(wtab);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004497 }
4498
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004499 fclose(wif.wif_fd);
4500
4501 /* Print a few statistics. */
4502 if (wif.wif_addmaxw == NULL)
4503 wif.wif_addmaxw = (char_u *)"";
4504 smsg((char_u *)_("Maximum number of adds on a word: %ld (%s)"),
4505 wif.wif_addmax, wif.wif_addmaxw);
4506 smsg((char_u *)_("Average number of adds on a word: %f"),
4507 (float)wif.wif_acount / (float)wif.wif_wcount);
4508}
4509
4510/*
4511 * Compare two basic words for their <addstring>.
4512 */
4513static int
4514#ifdef __BORLANDC__
4515_RTLENTRYF
4516#endif
4517bw_compare __ARGS((const void *s1, const void *s2));
4518
4519 static int
4520#ifdef __BORLANDC__
4521_RTLENTRYF
4522#endif
4523bw_compare(s1, s2)
4524 const void *s1;
4525 const void *s2;
4526{
4527 basicword_T *bw1 = *(basicword_T **)s1;
4528 basicword_T *bw2 = *(basicword_T **)s2;
4529 int i = 0;
4530
4531 /* compare the leadstrings */
4532 if (bw1->bw_leadstring == NULL)
4533 {
4534 if (bw2->bw_leadstring != NULL)
4535 return 1;
4536 }
4537 else if (bw2->bw_leadstring == NULL)
4538 return -1;
4539 else
4540 i = STRCMP(bw1->bw_leadstring, bw2->bw_leadstring);
4541
4542 if (i == 0)
4543 {
4544 /* leadstrings are identical, compare the addstrings */
4545 if (bw1->bw_addstring == NULL)
4546 {
4547 if (bw2->bw_addstring != NULL)
4548 return 1;
4549 }
4550 else if (bw2->bw_addstring == NULL)
4551 return -1;
4552 else
4553 i = STRCMP(bw1->bw_addstring, bw2->bw_addstring);
4554 }
4555 return i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004556}
4557
4558/*
4559 * Write basic word, followed by any additions.
4560 *
4561 * <worditem>: <nr> <string> <flags> [<flags2>]
4562 * [<caselen> <caseword>]
4563 * [<affixcnt> <affixNR> ...] (prefixes)
4564 * [<affixcnt> <affixNR> ...] (suffixes)
4565 * [<region>]
4566 * [<addcnt> <add> ...]
4567 */
4568 static void
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004569write_bword(wif, bwf, lowcap)
4570 winfo_T *wif; /* info for writing */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004571 basicword_T *bwf;
4572 int lowcap; /* write KEEPKAP word as not-valid */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004573{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004574 FILE *fd = wif->wif_fd;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004575 int flags;
4576 int aflags;
4577 int len;
4578 int leadlen, addlen;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004579 int copylen;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004580 int clen;
4581 int adds = 0;
4582 int i;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004583 int idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004584 basicword_T *bw, *bw2;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004585 basicword_T **wtab;
4586 int count;
4587 int l;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004588
4589 /* Check how many bytes can be copied from the previous word. */
4590 len = STRLEN(bwf->bw_word);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004591 if (wif->wif_prevbw == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004592 clen = 0;
4593 else
4594 for (clen = 0; clen < len
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004595 && wif->wif_prevbw->bw_word[clen] == bwf->bw_word[clen]; ++clen)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004596 ;
4597 putc(clen, fd); /* <nr> */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004598 wif->wif_prevbw = bwf;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004599 /* <string> */
4600 if (len > clen)
4601 fwrite(bwf->bw_word + clen, (size_t)(len - clen), (size_t)1, fd);
4602
4603 /* Try to find a word without additions to use first. */
4604 bw = bwf;
4605 for (bw2 = bwf; bw2 != NULL; bw2 = bw2->bw_cnext)
4606 {
4607 if (bw2->bw_addstring != NULL || bw2->bw_leadstring != NULL)
4608 ++adds;
4609 else
4610 bw = bw2;
4611 }
4612
4613 /* Flags: If there is no leadstring and no addstring the basic word is
4614 * valid, may have prefixes, suffixes and region. */
4615 flags = bw->bw_flags;
4616 if (bw->bw_addstring == NULL && bw->bw_leadstring == NULL)
4617 {
4618 flags |= BWF_VALID;
4619
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004620 /* Flags: add the region byte if the word isn't valid in all
4621 * regions. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004622 if (wif->wif_regionmask != 0 && (bw->bw_region & wif->wif_regionmask)
4623 != wif->wif_regionmask)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004624 flags |= BWF_REGION;
4625 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004626 /* Add the prefix/suffix list if there are prefixes/suffixes. */
4627 if (bw->bw_leadstring == NULL && bw->bw_prefix.ga_len > 0)
4628 flags |= BWF_PREFIX;
4629 if (bw->bw_addstring == NULL && bw->bw_suffix.ga_len > 0)
4630 flags |= BWF_SUFFIX;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004631
4632 /* Flags: may have additions. */
4633 if (adds > 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004634 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004635 flags |= BWF_ADDS;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004636 if (adds >= 256)
4637 flags |= BWF_ADDS_M;
4638 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004639
4640 /* The dummy word before a KEEPCAP word doesn't have any flags, they are
4641 * in the actual word that follows. */
4642 if (lowcap)
4643 flags = 0;
4644
4645 /* Flags: when the upper byte is not used we only write one flags
4646 * byte, if it's used then set an extra flag in the first byte and
4647 * also write the second byte. */
4648 if ((flags & 0xff00) == 0)
4649 putc(flags, fd); /* <flags> */
4650 else
4651 {
4652 putc(flags | BWF_SECOND, fd); /* <flags> */
4653 putc((int)((unsigned)flags >> 8), fd); /* <flags2> */
4654 }
4655
4656 /* First dummy word doesn't need anything but flags. */
4657 if (lowcap)
4658 return;
4659
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004660 if ((flags & BWF_KEEPCAP) && bw->bw_caseword != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004661 {
4662 len = STRLEN(bw->bw_caseword);
4663 putc(len, fd); /* <caselen> */
4664 for (i = 0; i < len; ++i)
4665 putc(bw->bw_caseword[i], fd); /* <caseword> */
4666 }
4667
4668 /* write prefix and suffix lists: <affixcnt> <affixNR> ... */
4669 if (flags & BWF_PREFIX)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004670 write_affixlist(fd, &bw->bw_prefix, wif->wif_prefm);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004671 if (flags & BWF_SUFFIX)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004672 write_affixlist(fd, &bw->bw_suffix, wif->wif_suffm);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004673
4674 if (flags & BWF_REGION)
4675 putc(bw->bw_region, fd); /* <region> */
4676
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004677 ++wif->wif_wcount;
4678
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004679 /*
4680 * Additions.
4681 */
4682 if (adds > 0)
4683 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004684 if (adds >= 256)
4685 put_bytes(fd, (long_u)adds, 2); /* 2 byte <addcnt> */
4686 else
4687 putc(adds, fd); /* 1 byte <addcnt> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004688
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004689 /* statistics */
4690 wif->wif_acount += adds;
4691 if (wif->wif_addmax < adds)
4692 {
4693 wif->wif_addmax = adds;
4694 wif->wif_addmaxw = bw->bw_word;
4695 }
4696
4697 /*
4698 * Sort the list of additions, so that we can copy as many bytes as
4699 * possible from the previous addstring.
4700 */
4701
4702 /* Make a table with pointers to each basic word that has additions. */
4703 wtab = (basicword_T **)alloc((unsigned)(sizeof(basicword_T *) * adds));
4704 if (wtab == NULL)
4705 return;
4706 count = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004707 for (bw = bwf; bw != NULL; bw = bw->bw_cnext)
4708 if (bw->bw_leadstring != NULL || bw->bw_addstring != NULL)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004709 wtab[count++] = bw;
4710
4711 /* Sort. */
4712 qsort((void *)wtab, (size_t)count, sizeof(basicword_T *), bw_compare);
4713
4714 /* Now write each basic word to the spell file. Copy bytes from the
4715 * previous leadstring/addstring if possible. */
4716 bw2 = NULL;
4717 for (idx = 0; idx < count; ++idx)
4718 {
4719 bw = wtab[idx];
4720
4721 /* <add>: <addflags> <addlen> [<leadlen>] [<copylen>]
4722 * [<addstring>] [<region>] */
4723 copylen = 0;
4724 if (bw->bw_leadstring == NULL)
4725 leadlen = 0;
4726 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004727 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004728 leadlen = STRLEN(bw->bw_leadstring);
4729 if (bw2 != NULL && bw2->bw_leadstring != NULL)
4730 for ( ; copylen < leadlen; ++copylen)
4731 if (bw->bw_leadstring[copylen]
4732 != bw2->bw_leadstring[copylen])
4733 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004734 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004735 if (bw->bw_addstring == NULL)
4736 addlen = 0;
4737 else
4738 {
4739 addlen = STRLEN(bw->bw_addstring);
4740 if (bw2 != NULL && copylen == leadlen
4741 && bw2->bw_addstring != NULL)
4742 {
4743 for (i = 0; i < addlen; ++i)
4744 if (bw->bw_addstring[i] != bw2->bw_addstring[i])
4745 break;
4746 copylen += i;
4747 }
4748 }
4749
4750 aflags = 0;
4751 /* Only copy bytes when it's more than one, the length itself
4752 * takes an extra byte. */
4753 if (copylen > 1)
4754 aflags |= ADD_COPYLEN;
4755 else
4756 copylen = 0;
4757
4758 if (bw->bw_flags & BWF_ONECAP)
4759 aflags |= ADD_ONECAP;
4760 if (bw->bw_flags & BWF_ALLCAP)
4761 aflags |= ADD_ALLCAP;
4762 if (bw->bw_flags & BWF_KEEPCAP)
4763 aflags |= ADD_KEEPCAP;
4764 if (wif->wif_regionmask != 0 && (bw->bw_region
4765 & wif->wif_regionmask) != wif->wif_regionmask)
4766 aflags |= ADD_REGION;
4767 if (leadlen > 0)
4768 aflags |= ADD_LEADLEN;
4769 putc(aflags, fd); /* <addflags> */
4770
4771 putc(leadlen + addlen, fd); /* <addlen> */
4772 if (aflags & ADD_LEADLEN)
4773 putc(leadlen, fd); /* <leadlen> */
4774 if (aflags & ADD_COPYLEN)
4775 putc(copylen, fd); /* <copylen> */
4776
4777 /* <addstring> */
4778 if (leadlen > copylen && bw->bw_leadstring != NULL)
4779 fwrite(bw->bw_leadstring + copylen,
4780 (size_t)(leadlen - copylen), (size_t)1, fd);
4781 if (leadlen + addlen > copylen && bw->bw_addstring != NULL)
4782 {
4783 if (copylen >= leadlen)
4784 l = copylen - leadlen;
4785 else
4786 l = 0;
4787 fwrite(bw->bw_addstring + l,
4788 (size_t)(addlen - l), (size_t)1, fd);
4789 }
4790
4791 if (aflags & ADD_REGION)
4792 putc(bw->bw_region, fd); /* <region> */
4793
4794 bw2 = bw;
4795 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004796
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004797 vim_free(wtab);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004798 }
4799}
4800
4801
4802/*
4803 * ":mkspell outfile infile ..."
4804 */
4805 void
4806ex_mkspell(eap)
4807 exarg_T *eap;
4808{
4809 int fcount;
4810 char_u **fnames;
4811 char_u fname[MAXPATHL];
4812 char_u wfname[MAXPATHL];
4813 afffile_T *(afile[8]);
4814 hashtab_T dfile[8];
4815 int i;
4816 int len;
4817 char_u region_name[16];
4818 struct stat st;
4819 int round;
4820 vimconv_T conv;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004821 int ascii = FALSE;
4822 char_u *arg = eap->arg;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004823 int error = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004824
Bram Moolenaar5482f332005-04-17 20:18:43 +00004825 if (STRNCMP(arg, "-ascii", 6) == 0)
4826 {
4827 ascii = TRUE;
4828 arg = skipwhite(arg + 6);
4829 }
4830
4831 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
4832 if (get_arglist_exp(arg, &fcount, &fnames) == FAIL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004833 return;
4834 if (fcount < 2)
4835 EMSG(_(e_invarg)); /* need at least output and input names */
4836 else if (fcount > 9)
4837 EMSG(_("E754: Only up to 8 regions supported"));
4838 else
4839 {
4840 /* Check for overwriting before doing things that may take a lot of
4841 * time. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004842 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
Bram Moolenaar5482f332005-04-17 20:18:43 +00004843 ascii ? (char_u *)"ascii" : p_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004844 if (!eap->forceit && mch_stat((char *)wfname, &st) >= 0)
4845 {
4846 EMSG(_(e_exists));
4847 goto theend;
4848 }
4849 if (mch_isdir(fnames[0]))
4850 {
4851 EMSG2(_(e_isadir2), fnames[0]);
4852 goto theend;
4853 }
4854
4855 /*
4856 * Init the aff and dic pointers.
4857 * Get the region names if there are more than 2 arguments.
4858 */
4859 for (i = 1; i < fcount; ++i)
4860 {
4861 afile[i - 1] = NULL;
4862 hash_init(&dfile[i - 1]);
4863 if (fcount > 2)
4864 {
4865 len = STRLEN(fnames[i]);
4866 if (STRLEN(gettail(fnames[i])) < 5 || fnames[i][len - 3] != '_')
4867 {
4868 EMSG2(_("E755: Invalid region in %s"), fnames[i]);
4869 goto theend;
4870 }
4871 else
4872 {
4873 region_name[(i - 1) * 2] = TOLOWER_ASC(fnames[i][len - 2]);
4874 region_name[(i - 1) * 2 + 1] =
4875 TOLOWER_ASC(fnames[i][len - 1]);
4876 }
4877 }
4878 }
4879
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004880 /* Clear the char type tables, don't want to use any of the currently
4881 * used spell properties. */
4882 init_spell_chartab();
4883
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004884 /*
4885 * Read all the .aff and .dic files.
4886 * Text is converted to 'encoding'.
4887 */
4888 for (i = 1; i < fcount; ++i)
4889 {
4890 /* Read the .aff file. Will init "conv" based on the "SET" line. */
4891 conv.vc_type = CONV_NONE;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004892 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", fnames[i]);
Bram Moolenaar5482f332005-04-17 20:18:43 +00004893 if ((afile[i - 1] = spell_read_aff(fname, &conv, ascii)) == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004894 break;
4895
4896 /* Read the .dic file. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004897 vim_snprintf((char *)fname, sizeof(fname), "%s.dic", fnames[i]);
Bram Moolenaar5482f332005-04-17 20:18:43 +00004898 if (spell_read_dic(&dfile[i - 1], fname, &conv, ascii) == FAIL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004899 break;
4900
4901 /* Free any conversion stuff. */
4902 convert_setup(&conv, NULL, NULL);
4903 }
4904
4905 /* Process the data when all the files could be read. */
4906 if (i == fcount)
4907 {
4908 garray_T prefga;
4909 garray_T suffga;
4910 garray_T *gap;
4911 hashtab_T newwords;
4912
4913 /*
4914 * Combine all the affixes into one new affix list. This is done
4915 * for prefixes and suffixes separately.
4916 * We need to do this for each region, try to re-use the same
4917 * affixes.
4918 * Since we number the new affix entries, a growarray will do. In
4919 * the affheader_T the ah_key is unused.
4920 */
4921 MSG(_("Combining affixes..."));
4922 out_flush();
4923 for (round = 1; round <= 2; ++round)
4924 {
4925 gap = round == 1 ? &prefga : &suffga;
4926 ga_init2(gap, sizeof(affheader_T), 50);
4927 for (i = 1; i < fcount; ++i)
4928 get_new_aff(round == 1 ? &afile[i - 1]->af_pref
Bram Moolenaar5482f332005-04-17 20:18:43 +00004929 : &afile[i - 1]->af_suff,
4930 gap, round == 1);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004931 }
4932
4933 /*
4934 * Go over all words and:
4935 * - change the old affix names to the new affix numbers
4936 * - check the conditions
4937 * - fold case
4938 * - extract the basic word and additions.
4939 * Do this for each region.
4940 */
4941 MSG(_("Building word list..."));
4942 out_flush();
4943 hash_init(&newwords);
4944
4945 for (i = 1; i < fcount; ++i)
4946 build_wordlist(&newwords, &dfile[i - 1], afile[i - 1],
4947 1 << (i - 1));
4948
4949 if (fcount > 2)
4950 {
4951 /* Combine words for the different regions into one. */
4952 MSG(_("Combining regions..."));
4953 out_flush();
4954 combine_regions(&newwords);
4955 }
4956
4957 /*
4958 * Affixes on a word with additions are clumsy, would require
4959 * inefficient searching. Turn the affixes into additions and/or
4960 * the expanded word.
4961 */
4962 MSG(_("Processing words..."));
4963 out_flush();
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004964 error = expand_affixes(&newwords, &prefga, &suffga) == FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004965
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004966 if (!error)
4967 {
4968 /* Write the info in the spell file. */
4969 smsg((char_u *)_("Writing spell file %s..."), wfname);
4970 out_flush();
4971 write_vim_spell(wfname, &prefga, &suffga, &newwords,
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004972 fcount - 1, region_name, ascii);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004973 MSG(_("Done!"));
4974 out_flush();
4975 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004976
4977 /* Free the allocated stuff. */
4978 free_wordtable(&newwords);
4979 for (round = 1; round <= 2; ++round)
4980 {
4981 gap = round == 1 ? &prefga: &suffga;
4982 for (i = 0; i < gap->ga_len; ++i)
4983 free_affixentries(((affheader_T *)gap->ga_data + i)
4984 ->ah_first);
4985 ga_clear(gap);
4986 }
4987 }
4988
4989 /* Free the .aff and .dic file structures. */
4990 for (i = 1; i < fcount; ++i)
4991 {
4992 if (afile[i - 1] != NULL)
4993 spell_free_aff(afile[i - 1]);
4994 spell_free_dic(&dfile[i - 1]);
4995 }
4996 }
4997
4998theend:
4999 FreeWild(fcount, fnames);
5000}
5001
5002 static void
5003free_wordtable(ht)
5004 hashtab_T *ht;
5005{
5006 int todo;
5007 basicword_T *bw, *nbw;
5008 hashitem_T *hi;
5009
5010 todo = ht->ht_used;
5011 for (hi = ht->ht_array; todo > 0; ++hi)
5012 {
5013 if (!HASHITEM_EMPTY(hi))
5014 {
5015 --todo;
5016 for (bw = HI2BW(hi); bw != NULL; bw = nbw)
5017 {
5018 nbw = bw->bw_next;
5019 free_basicword(bw);
5020 }
5021 }
5022 }
5023}
5024
5025/*
5026 * Free a basicword_T and what it contains.
5027 */
5028 static void
5029free_basicword(bw)
5030 basicword_T *bw;
5031{
5032 ga_clear(&bw->bw_prefix);
5033 ga_clear(&bw->bw_suffix);
5034 vim_free(bw->bw_caseword);
5035 vim_free(bw->bw_leadstring);
5036 vim_free(bw->bw_addstring);
5037 vim_free(bw);
5038}
5039
5040/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00005041 * Free a list of affentry_T and what they contain.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005042 */
5043 static void
5044free_affixentries(first)
5045 affentry_T *first;
5046{
5047 affentry_T *ap, *an;
5048
5049 for (ap = first; ap != NULL; ap = an)
5050 {
5051 an = ap->ae_next;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005052 free_affix_entry(ap);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005053 }
5054}
5055
Bram Moolenaar5482f332005-04-17 20:18:43 +00005056/*
5057 * Free one affentry_T and what it contains.
5058 */
5059 static void
5060free_affix_entry(ap)
5061 affentry_T *ap;
5062{
5063 vim_free(ap->ae_chop);
5064 vim_free(ap->ae_add);
5065 vim_free(ap->ae_cond);
5066 vim_free(ap->ae_prog);
5067 vim_free(ap);
5068}
5069
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005070#endif /* FEAT_MBYTE */
5071
5072#endif /* FEAT_SYN_HL */