blob: 9d010f912e4d66a5782a3ef538f6ce80b71ccac3 [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)
Bram Moolenaard6ec8452005-05-31 22:02:19 +00002604 {
2605 verbose_enter();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002606 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
2607 fname, lnum, items[2]);
Bram Moolenaard6ec8452005-05-31 22:02:19 +00002608 verbose_leave();
2609 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002610 cur_aff->ah_first = NULL;
2611 if (*items[0] == 'P')
2612 tp = &aff->af_pref;
2613 else
2614 tp = &aff->af_suff;
2615 if (!HASHITEM_EMPTY(hash_find(tp, cur_aff->ah_key)))
2616 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
2617 fname, lnum, items[1]);
2618 else
2619 hash_add(tp, cur_aff->ah_key);
2620
2621 aff_todo = atoi((char *)items[3]);
2622 }
2623 else if ((STRCMP(items[0], "PFX") == 0
2624 || STRCMP(items[0], "SFX") == 0)
2625 && aff_todo > 0
2626 && STRCMP(cur_aff->ah_key, items[1]) == 0
2627 && itemcnt == 5)
2628 {
2629 affentry_T *aff_entry;
2630
2631 /* New item for an affix letter. */
2632 --aff_todo;
2633 aff_entry = (affentry_T *)alloc_clear(
2634 (unsigned)sizeof(affentry_T));
2635 if (aff_entry == NULL)
2636 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00002637
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002638 if (STRCMP(items[2], "0") != 0)
2639 aff_entry->ae_chop = vim_strsave(items[2]);
2640 if (STRCMP(items[3], "0") != 0)
2641 aff_entry->ae_add = vim_strsave(items[3]);
2642 if (STRCMP(items[4], ".") != 0)
2643 {
2644 char_u buf[MAXLINELEN];
2645
2646 aff_entry->ae_cond = vim_strsave(items[4]);
2647 if (*items[0] == 'P')
2648 sprintf((char *)buf, "^%s", items[4]);
2649 else
2650 sprintf((char *)buf, "%s$", items[4]);
2651 aff_entry->ae_prog = vim_regcomp(buf, RE_MAGIC + RE_STRING);
2652 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00002653
2654 if (ascii && (has_non_ascii(aff_entry->ae_chop)
2655 || has_non_ascii(aff_entry->ae_add)))
2656 {
2657 /* Don't use an affix entry with non-ASCII characters when
2658 * "ascii" is TRUE. */
2659 free_affix_entry(aff_entry);
2660 }
2661 else
2662 {
2663 aff_entry->ae_next = cur_aff->ah_first;
2664 cur_aff->ah_first = aff_entry;
2665 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002666 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002667 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
2668 {
2669 if (fol != NULL)
2670 smsg((char_u *)_("Duplicate FOL in %s line %d"),
2671 fname, lnum);
2672 else
2673 fol = vim_strsave(items[1]);
2674 }
2675 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
2676 {
2677 if (low != NULL)
2678 smsg((char_u *)_("Duplicate LOW in %s line %d"),
2679 fname, lnum);
2680 else
2681 low = vim_strsave(items[1]);
2682 }
2683 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
2684 {
2685 if (upp != NULL)
2686 smsg((char_u *)_("Duplicate UPP in %s line %d"),
2687 fname, lnum);
2688 else
2689 upp = vim_strsave(items[1]);
2690 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002691 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
2692 /* Ignore REP count */;
2693 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
2694 {
2695 repentry_T *rp;
2696
2697 /* REP item */
2698 if (ga_grow(&aff->af_rep, 1) == FAIL)
2699 break;
2700 rp = ((repentry_T *)aff->af_rep.ga_data) + aff->af_rep.ga_len;
2701 rp->re_from = vim_strsave(items[1]);
2702 rp->re_to = vim_strsave(items[2]);
2703 ++aff->af_rep.ga_len;
2704 }
2705 else if (p_verbose > 0)
Bram Moolenaard6ec8452005-05-31 22:02:19 +00002706 {
2707 verbose_enter();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002708 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
2709 fname, lnum, items[0]);
Bram Moolenaard6ec8452005-05-31 22:02:19 +00002710 verbose_leave();
2711 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002712 }
2713
2714 }
2715
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002716 if (fol != NULL || low != NULL || upp != NULL)
2717 {
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00002718 /* Don't write a word table for an ASCII file, so that we don't check
2719 * for conflicts with a word table that matches 'encoding'. */
2720 if (!ascii)
2721 {
2722 if (fol == NULL || low == NULL || upp == NULL)
2723 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
2724 else
2725 set_spell_chartab(fol, low, upp);
2726 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002727
2728 vim_free(fol);
2729 vim_free(low);
2730 vim_free(upp);
2731 }
2732
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002733 vim_free(pc);
2734 fclose(fd);
2735 return aff;
2736}
2737
2738/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00002739 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
2740 * When "s" is NULL FALSE is returned.
2741 */
2742 static int
2743has_non_ascii(s)
2744 char_u *s;
2745{
2746 char_u *p;
2747
2748 if (s != NULL)
2749 for (p = s; *p != NUL; ++p)
2750 if (*p >= 128)
2751 return TRUE;
2752 return FALSE;
2753}
2754
2755/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002756 * Free the structure filled by spell_read_aff().
2757 */
2758 static void
2759spell_free_aff(aff)
2760 afffile_T *aff;
2761{
2762 hashtab_T *ht;
2763 hashitem_T *hi;
2764 int todo;
2765 int i;
2766 repentry_T *rp;
2767 affheader_T *ah;
2768
2769 vim_free(aff->af_enc);
2770 vim_free(aff->af_try);
2771
2772 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
2773 {
2774 todo = ht->ht_used;
2775 for (hi = ht->ht_array; todo > 0; ++hi)
2776 {
2777 if (!HASHITEM_EMPTY(hi))
2778 {
2779 --todo;
2780 ah = HI2AH(hi);
2781 free_affixentries(ah->ah_first);
2782 vim_free(ah);
2783 }
2784 }
2785 if (ht == &aff->af_suff)
2786 break;
2787 }
2788 hash_clear(&aff->af_pref);
2789 hash_clear(&aff->af_suff);
2790
2791 for (i = 0; i < aff->af_rep.ga_len; ++i)
2792 {
2793 rp = ((repentry_T *)aff->af_rep.ga_data) + i;
2794 vim_free(rp->re_from);
2795 vim_free(rp->re_to);
2796 }
2797 ga_clear(&aff->af_rep);
2798
2799 vim_free(aff);
2800}
2801
2802/*
2803 * Read a dictionary ".dic" file.
2804 * Returns OK or FAIL;
2805 * Each entry in the hashtab_T is a dicword_T.
2806 */
2807 static int
Bram Moolenaar5482f332005-04-17 20:18:43 +00002808spell_read_dic(ht, fname, conv, ascii)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002809 hashtab_T *ht;
2810 char_u *fname;
2811 vimconv_T *conv; /* info for encoding conversion */
Bram Moolenaar5482f332005-04-17 20:18:43 +00002812 int ascii; /* only accept ASCII words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002813{
2814 char_u line[MAXLINELEN];
2815 char_u *p;
2816 dicword_T *dw;
2817 char_u *pc;
2818 char_u *w;
2819 int l;
2820 hash_T hash;
2821 hashitem_T *hi;
2822 FILE *fd;
2823 int lnum = 1;
2824
2825 fd = fopen((char *)fname, "r");
2826 if (fd == NULL)
2827 {
2828 EMSG2(_(e_notopen), fname);
2829 return FAIL;
2830 }
2831
2832 smsg((char_u *)_("Reading dictionary file %s..."), fname);
2833 out_flush();
2834
2835 /* Read and ignore the first line: word count. */
2836 (void)vim_fgets(line, MAXLINELEN, fd);
2837 if (!isdigit(*skipwhite(line)))
2838 EMSG2(_("E760: No word count in %s"), fname);
2839
2840 /*
2841 * Read all the lines in the file one by one.
2842 * The words are converted to 'encoding' here, before being added to
2843 * the hashtable.
2844 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002845 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002846 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002847 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002848 ++lnum;
2849
2850 /* Remove CR, LF and white space from end. */
2851 l = STRLEN(line);
2852 while (l > 0 && line[l - 1] <= ' ')
2853 --l;
2854 if (l == 0)
2855 continue; /* empty line */
2856 line[l] = NUL;
2857
2858 /* Find the optional affix names. */
2859 p = vim_strchr(line, '/');
2860 if (p != NULL)
2861 *p++ = NUL;
2862
Bram Moolenaar5482f332005-04-17 20:18:43 +00002863 /* Skip non-ASCII words when "ascii" is TRUE. */
2864 if (ascii && has_non_ascii(line))
2865 continue;
2866
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002867 /* Convert from "SET" to 'encoding' when needed. */
2868 if (conv->vc_type != CONV_NONE)
2869 {
2870 pc = string_convert(conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002871 if (pc == NULL)
2872 {
2873 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
2874 fname, lnum, line);
2875 continue;
2876 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002877 w = pc;
2878 }
2879 else
2880 {
2881 pc = NULL;
2882 w = line;
2883 }
2884
2885 dw = (dicword_T *)alloc_clear((unsigned)sizeof(dicword_T)
2886 + STRLEN(w));
2887 if (dw == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002888 {
2889 vim_free(pc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002890 break;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002891 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002892 STRCPY(dw->dw_word, w);
2893 vim_free(pc);
2894
2895 hash = hash_hash(dw->dw_word);
2896 hi = hash_lookup(ht, dw->dw_word, hash);
2897 if (!HASHITEM_EMPTY(hi))
2898 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
2899 fname, lnum, line);
2900 else
2901 hash_add_item(ht, hi, dw->dw_word, hash);
2902
2903 if (p != NULL)
2904 dw->dw_affnm = vim_strsave(p);
2905 }
2906
2907 fclose(fd);
2908 return OK;
2909}
2910
2911/*
2912 * Free the structure filled by spell_read_dic().
2913 */
2914 static void
2915spell_free_dic(dic)
2916 hashtab_T *dic;
2917{
2918 int todo;
2919 dicword_T *dw;
2920 hashitem_T *hi;
2921
2922 todo = dic->ht_used;
2923 for (hi = dic->ht_array; todo > 0; ++hi)
2924 {
2925 if (!HASHITEM_EMPTY(hi))
2926 {
2927 --todo;
2928 dw = HI2DW(hi);
2929 vim_free(dw->dw_affnm);
2930 vim_free(dw);
2931 }
2932 }
2933 hash_clear(dic);
2934}
2935
2936/*
2937 * Take the affixes read by spell_read_aff() and add them to the new list.
2938 * Attempts to re-use the same number for identical affixes (ignoring the
2939 * condition, since we remove that). That is especially important when using
2940 * multiple regions.
2941 * Returns OK or FAIL;
2942 */
2943 static int
Bram Moolenaar5482f332005-04-17 20:18:43 +00002944get_new_aff(oldaff, gap, prefix)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002945 hashtab_T *oldaff; /* hashtable with affheader_T */
2946 garray_T *gap; /* table with new affixes */
Bram Moolenaar5482f332005-04-17 20:18:43 +00002947 int prefix; /* TRUE when doing prefixes, FALSE for
2948 suffixes */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002949{
2950 int oldtodo;
2951 affheader_T *oldah, *newah, *gapah;
2952 affentry_T *oldae, *newae;
2953 hashitem_T *oldhi;
2954 hashitem_T *hi;
2955 hashtab_T condht; /* conditions already found */
2956 char_u condkey[MAXLINELEN];
2957 int newnr;
2958 int gapnr;
2959 int retval = OK;
2960 char_u *p;
2961 garray_T tga;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00002962 int preword;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002963
2964 /*
2965 * Loop over all the old affix names.
2966 */
2967 oldtodo = oldaff->ht_used;
2968 for (oldhi = oldaff->ht_array; oldtodo > 0 && retval == OK; ++oldhi)
2969 {
2970 if (!HASHITEM_EMPTY(oldhi))
2971 {
2972 --oldtodo;
2973 oldah = (affheader_T *)oldhi->hi_key;
2974
2975 /* Put entries with the same condition under the same new affix
2976 * nr in "tga". Use hashtable "condht" to find them. */
2977 ga_init2(&tga, sizeof(affheader_T), 10);
2978 hash_init(&condht);
2979
2980 /*
2981 * Loop over all affixes with the same name.
2982 * The affixes with the same condition will get the same number,
2983 * since they can be used with the same words.
2984 * 1. build the lists of new affentry_T, with the headers in "tga".
2985 * 2. Check if some of the lists already exist in "gap", re-use
2986 * their number.
2987 * 3. Assign the new numbers to the old affixes.
2988 */
2989
2990 /* 1. build the lists of new affentry_T. */
2991 for (oldae = oldah->ah_first; oldae != NULL && retval == OK;
2992 oldae = oldae->ae_next)
2993 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00002994 preword = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002995 oldae->ae_add_nw = NULL;
Bram Moolenaar5482f332005-04-17 20:18:43 +00002996 oldae->ae_add_pw = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002997 if (oldae->ae_add != NULL)
2998 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00002999 /* Check for non-word characters in the affix. If there
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003000 * is one a suffix will be turned into an addition, a
3001 * prefix may be turned into a leadstring.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003002 * This is stored with the old affix, that is where
3003 * trans_affixes() will check. */
3004 for (p = oldae->ae_add; *p != NUL; mb_ptr_adv(p))
3005 if (!spell_iswordc(p))
Bram Moolenaar5482f332005-04-17 20:18:43 +00003006 {
3007 oldae->ae_add_nw = p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003008 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003009 }
3010
3011 if (prefix && oldae->ae_add_nw != NULL)
3012 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003013 /* If a prefix has non-word characters special
3014 * treatment is necessary:
3015 * - If it has only non-word characters it becomes a
3016 * leadstring.
3017 * - If it has a sequence of word characters followed
3018 * by a non-word char it becomes a "preword": "d'",
3019 * "de-", "d'ai", etc.
3020 * - if it has another mix of word and non-word
3021 * characters the part before the last word char
3022 * becomes a leadstring: "'d", etc.
3023 */
Bram Moolenaar5482f332005-04-17 20:18:43 +00003024 for (p = oldae->ae_add; *p != NUL; mb_ptr_adv(p))
3025 if (spell_iswordc(p))
3026 {
3027 oldae->ae_add_pw = p;
3028 break;
3029 }
3030 if (oldae->ae_add_pw != NULL)
3031 {
3032 /* Mixed prefix, set ae_add_nw to first non-word
3033 * char after ae_add_pw (if there is one). */
3034 oldae->ae_add_nw = NULL;
3035 for ( ; *p != NUL; mb_ptr_adv(p))
3036 if (!spell_iswordc(p))
3037 {
3038 oldae->ae_add_nw = p;
3039 break;
3040 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003041 if (oldae->ae_add_nw != NULL)
3042 {
3043 preword = TRUE;
3044 oldae->ae_add_pw = NULL;
3045 oldae->ae_add_nw = NULL;
3046 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003047 }
3048 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003049 }
3050
3051 if (oldae->ae_cond == NULL)
3052 /* hashtable requires a non-empty key */
3053 STRCPY(condkey, "---");
3054 else
3055 STRCPY(condkey, oldae->ae_cond);
3056
3057 /* Look for an existing list with this name and condition. */
3058 hi = hash_find(&condht, condkey);
3059 if (!HASHITEM_EMPTY(hi))
3060 /* Match with existing affix, use that one. */
3061 newnr = HI2AS(hi)->as_nr;
3062 else
3063 {
3064 /* Add a new affix number. */
3065 newnr = tga.ga_len;
3066 if (ga_grow(&tga, 1) == FAIL)
3067 retval = FAIL;
3068 else
3069 {
3070 newah = ((affheader_T *)tga.ga_data) + newnr;
3071 newah->ah_combine = oldah->ah_combine;
3072 newah->ah_first = NULL;
3073 ++tga.ga_len;
3074
3075 /* Add the new list to the condht hashtable. */
3076 add_affhash(&condht, condkey, newnr);
3077 }
3078 }
3079
3080 /* Add the new affentry_T to the list. */
3081 newah = ((affheader_T *)tga.ga_data) + newnr;
3082 newae = (affentry_T *)alloc_clear((unsigned)sizeof(affentry_T));
3083 if (newae == NULL)
3084 retval = FAIL;
3085 else
3086 {
3087 newae->ae_next = newah->ah_first;
3088 newah->ah_first = newae;
3089 if (oldae->ae_chop == NULL)
3090 newae->ae_chop = NULL;
3091 else
3092 newae->ae_chop = vim_strsave(oldae->ae_chop);
3093 if (oldae->ae_add == NULL)
3094 newae->ae_add = NULL;
3095 else
3096 newae->ae_add = vim_strsave(oldae->ae_add);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003097 newae->ae_preword = preword;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003098
3099 /* The condition is not copied, since the new affix is
3100 * only used for words where the condition matches. */
3101 }
3102 }
3103
3104 /* 2. Check if some of the lists already exist, re-use their
3105 * number. Otherwise add the list to "gap". */
3106 for (newnr = 0; newnr < tga.ga_len; ++newnr)
3107 {
3108 newah = ((affheader_T *)tga.ga_data) + newnr;
3109 for (gapnr = 0; gapnr < gap->ga_len; ++gapnr)
3110 {
3111 gapah = ((affheader_T *)gap->ga_data) + gapnr;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003112 if (newah->ah_combine == gapah->ah_combine
3113 && same_affentries(newah, gapah))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003114 /* Found an existing affheader_T entry with same
3115 * affentry_T list, use its number. */
3116 break;
3117 }
3118
3119 newah->ah_affnr = gapnr;
3120 if (gapnr == gap->ga_len)
3121 {
3122 /* This is a new affentry_T list, add it. */
3123 if (ga_grow(gap, 1) == FAIL)
3124 retval = FAIL;
3125 else
3126 {
3127 *(((affheader_T *)gap->ga_data) + gap->ga_len) = *newah;
3128 ++gap->ga_len;
3129 }
3130 }
3131 else
3132 {
3133 /* free unused affentry_T list */
3134 free_affixentries(newah->ah_first);
3135 }
3136 }
3137
3138 /* 3. Assign the new affix numbers to the old affixes. */
3139 for (oldae = oldah->ah_first; oldae != NULL && retval == OK;
3140 oldae = oldae->ae_next)
3141 {
3142 if (oldae->ae_cond == NULL)
3143 /* hashtable requires a non-empty key */
3144 STRCPY(condkey, "---");
3145 else
3146 STRCPY(condkey, oldae->ae_cond);
3147
3148 /* Look for an existing affix with this name and condition. */
3149 hi = hash_find(&condht, condkey);
3150 if (!HASHITEM_EMPTY(hi))
3151 /* Match with existing affix, use that one. */
3152 newnr = HI2AS(hi)->as_nr;
3153 else
3154 {
3155 EMSG(_(e_internal));
3156 retval = FAIL;
3157 }
3158 newah = ((affheader_T *)tga.ga_data) + newnr;
3159 oldae->ae_affnr = newah->ah_affnr;
3160 }
3161
3162 ga_clear(&tga);
3163 clear_affhash(&condht);
3164 }
3165 }
3166
3167 return retval;
3168}
3169
3170/*
3171 * Return TRUE if the affentry_T lists for "ah1" and "ah2" contain the same
3172 * items, ignoring the order.
3173 * Only compares the chop and add strings, not the condition.
3174 */
3175 static int
3176same_affentries(ah1, ah2)
3177 affheader_T *ah1;
3178 affheader_T *ah2;
3179{
3180 affentry_T *ae1, *ae2;
3181
3182 /* Check the length of the lists first. */
3183 ae2 = ah2->ah_first;
3184 for (ae1 = ah1->ah_first; ae1 != NULL; ae1 = ae1->ae_next)
3185 {
3186 if (ae2 == NULL)
3187 return FALSE; /* "ah1" list is longer */
3188 ae2 = ae2->ae_next;
3189 }
3190 if (ae2 != NULL)
3191 return FALSE; /* "ah2" list is longer */
3192
3193 /* Check that each entry in "ah1" appears in "ah2". */
3194 for (ae1 = ah1->ah_first; ae1 != NULL; ae1 = ae1->ae_next)
3195 {
3196 for (ae2 = ah2->ah_first; ae2 != NULL; ae2 = ae2->ae_next)
3197 {
3198 if ((ae1->ae_chop == NULL) == (ae2->ae_chop == NULL)
3199 && (ae1->ae_add == NULL) == (ae2->ae_add == NULL)
3200 && (ae1->ae_chop == NULL
3201 || STRCMP(ae1->ae_chop, ae2->ae_chop) == 0)
3202 && (ae1->ae_add == NULL
3203 || STRCMP(ae1->ae_add, ae2->ae_add) == 0))
3204 break;
3205 }
3206 if (ae2 == NULL)
3207 return FALSE;
3208 }
3209
3210 return TRUE;
3211}
3212
3213/*
3214 * Add a chop/add or cond hashtable entry.
3215 */
3216 static void
3217add_affhash(ht, key, newnr)
3218 hashtab_T *ht;
3219 char_u *key;
3220 int newnr;
3221{
3222 affhash_T *as;
3223
3224 as = (affhash_T *)alloc((unsigned)sizeof(affhash_T) + STRLEN(key));
3225 if (as != NULL)
3226 {
3227 as->as_nr = newnr;
3228 STRCPY(as->as_word, key);
3229 hash_add(ht, as->as_word);
3230 }
3231}
3232
3233/*
3234 * Clear the chop/add hashtable used to detect identical affixes.
3235 */
3236 static void
3237clear_affhash(ht)
3238 hashtab_T *ht;
3239{
3240 int todo;
3241 hashitem_T *hi;
3242
3243 todo = ht->ht_used;
3244 for (hi = ht->ht_array; todo > 0; ++hi)
3245 {
3246 if (!HASHITEM_EMPTY(hi))
3247 {
3248 --todo;
3249 vim_free(HI2AS(hi));
3250 }
3251 }
3252 hash_clear(ht);
3253}
3254
3255/*
3256 * Translate list of affix names for an old word to affix numbers in a new
3257 * basic word.
3258 * This checks if the conditions match with the old word. The result is that
3259 * the new affix does not need to store the condition.
3260 */
3261 static void
3262trans_affixes(dw, bw, oldaff, newwords)
3263 dicword_T *dw; /* old word */
3264 basicword_T *bw; /* basic word */
3265 afffile_T *oldaff; /* affixes for "oldwords" */
3266 hashtab_T *newwords; /* table with words */
3267{
3268 char_u key[2];
3269 char_u *p;
3270 char_u *affnm;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003271 garray_T *gap, *agap;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003272 hashitem_T *aff_hi;
3273 affheader_T *ah;
3274 affentry_T *ae;
3275 regmatch_T regmatch;
3276 int i;
3277 basicword_T *nbw;
3278 int alen;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003279 garray_T suffixga; /* list of words with non-word suffixes */
3280 garray_T prefixga; /* list of words with non-word prefixes */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003281 char_u nword[MAXWLEN];
3282 int flags;
3283 int n;
3284
Bram Moolenaar5482f332005-04-17 20:18:43 +00003285 ga_init2(&suffixga, (int)sizeof(basicword_T *), 5);
3286 ga_init2(&prefixga, (int)sizeof(basicword_T *), 5);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003287
3288 /* Loop over all the affix names of the old word. */
3289 key[1] = NUL;
3290 for (affnm = dw->dw_affnm; *affnm != NUL; ++affnm)
3291 {
3292 key[0] = *affnm;
3293 aff_hi = hash_find(&oldaff->af_pref, key);
3294 if (!HASHITEM_EMPTY(aff_hi))
3295 gap = &bw->bw_prefix; /* found a prefix */
3296 else
3297 {
3298 gap = &bw->bw_suffix; /* must be a suffix */
3299 aff_hi = hash_find(&oldaff->af_suff, key);
3300 if (HASHITEM_EMPTY(aff_hi))
3301 {
3302 smsg((char_u *)_("No affix entry '%s' for word %s"),
3303 key, dw->dw_word);
3304 continue;
3305 }
3306 }
3307
3308 /* Loop over all the affix entries for this affix name. */
3309 ah = HI2AH(aff_hi);
3310 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3311 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003312 /* Setup for regexp matching. Note that we don't ignore case.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003313 * This is weird, because the rules in an .aff file don't care
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003314 * about case, but it's necessary for compatibility with Myspell.
3315 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003316 regmatch.regprog = ae->ae_prog;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003317 regmatch.rm_ic = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003318 if (ae->ae_prog == NULL
3319 || vim_regexec(&regmatch, dw->dw_word, (colnr_T)0))
3320 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003321 if ((ae->ae_add_nw != NULL || ae->ae_add_pw != NULL)
3322 && (gap != &bw->bw_suffix || bw->bw_addstring == NULL))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003323 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003324 /*
3325 * Affix has a non-word character and isn't prepended to
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003326 * leader or appended to addition. Need to use another
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003327 * word with a leadstring and/or addstring.
3328 */
3329 if (gap == &bw->bw_suffix || ae->ae_add_nw == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003330 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003331 /* Suffix or prefix with only non-word chars.
3332 * Build the new basic word in "nword": Remove chop
3333 * string and append/prepend addition. */
3334 if (gap == &bw->bw_suffix)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003335 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003336 /* suffix goes at the end of the word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003337 STRCPY(nword, dw->dw_word);
3338 if (ae->ae_chop != NULL)
3339 {
3340 /* Remove chop string. */
3341 p = nword + STRLEN(nword);
3342 for (i = mb_charlen(ae->ae_chop); i > 0; --i)
3343 mb_ptr_back(nword, p);
3344 *p = NUL;
3345 }
3346 STRCAT(nword, ae->ae_add);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003347 agap = &suffixga;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003348 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003349 else
Bram Moolenaar5482f332005-04-17 20:18:43 +00003350 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003351 /* prefix goes before the word */
Bram Moolenaar5482f332005-04-17 20:18:43 +00003352 STRCPY(nword, ae->ae_add);
3353 p = dw->dw_word;
3354 if (ae->ae_chop != NULL)
3355 /* Skip chop string. */
3356 for (i = mb_charlen(ae->ae_chop); i > 0; --i)
3357 mb_ptr_adv( p);
3358 STRCAT(nword, p);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003359 agap = &prefixga;
3360 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003361
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003362 /* Create a basicword_T from the word. */
3363 nbw = get_basicword(nword, 1);
3364 if (nbw != NULL)
3365 {
3366 nbw->bw_region = bw->bw_region;
3367 nbw->bw_flags |= bw->bw_flags
3368 & ~(BWF_ONECAP | BWF_ALLCAP | BWF_KEEPCAP);
Bram Moolenaar5482f332005-04-17 20:18:43 +00003369
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003370 if (STRCMP(bw->bw_word, nbw->bw_word) != 0)
3371 /* Basic word differs, add new word entry. */
3372 (void)add_to_wordlist(newwords, nbw);
Bram Moolenaar5482f332005-04-17 20:18:43 +00003373 else
3374 {
3375 /* Basic word is the same, link "nbw" after
3376 * "bw". */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003377 nbw->bw_next = bw->bw_next;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003378 bw->bw_next = nbw;
3379 }
3380
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003381 /* Remember this word, we need to set bw_prefix
3382 * or bw_suffix later. */
3383 if (ga_grow(agap, 1) == OK)
3384 ((basicword_T **)agap->ga_data)
3385 [agap->ga_len++] = nbw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003386 }
3387 }
3388 else
3389 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003390 /* Prefix with both non-word and word characters: Turn
3391 * prefix into basic word, original word becomes an
3392 * addstring. */
3393
3394 /* Fold-case the word characters in the prefix into
3395 * nword[]. */
3396 alen = 0;
3397 for (p = ae->ae_add_pw; p < ae->ae_add_nw; p += n)
3398 {
3399#ifdef FEAT_MBYTE
3400 n = (*mb_ptr2len_check)(p);
3401#else
3402 n = 1;
3403#endif
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003404 (void)spell_casefold(p, n, nword + alen,
Bram Moolenaar5482f332005-04-17 20:18:43 +00003405 MAXWLEN - alen);
3406 alen += STRLEN(nword + alen);
3407 }
3408
3409 /* Allocate a new word entry. */
3410 nbw = (basicword_T *)alloc((unsigned)(
3411 sizeof(basicword_T) + alen + 1));
3412 if (nbw != NULL)
3413 {
3414 *nbw = *bw;
3415 ga_init2(&nbw->bw_prefix, sizeof(short_u), 1);
3416 ga_init2(&nbw->bw_suffix, sizeof(short_u), 1);
3417
3418 mch_memmove(nbw->bw_word, nword, alen);
3419 nbw->bw_word[alen] = NUL;
3420
3421 /* Use the cap type of the prefix. */
3422 alen = ae->ae_add_nw - ae->ae_add_pw;
3423 mch_memmove(nword, ae->ae_add_pw, alen);
3424 nword[alen] = NUL;
3425 flags = captype(nword, nword + STRLEN(nword));
3426 if (flags & BWF_KEEPCAP)
3427 nbw->bw_caseword = vim_strsave(nword);
3428 else
3429 nbw->bw_caseword = NULL;
3430 nbw->bw_flags &= ~(BWF_ONECAP | BWF_ALLCAP
3431 | BWF_KEEPCAP);
3432 nbw->bw_flags |= flags;
3433
3434 /* The addstring is the prefix after the word
3435 * characters, the original word excluding "chop",
3436 * plus any addition. */
3437 STRCPY(nword, ae->ae_add_nw);
3438 p = bw->bw_word;
3439 if (ae->ae_chop != NULL)
3440 p += STRLEN(ae->ae_chop);
3441 STRCAT(nword, p);
3442 if (bw->bw_addstring != NULL)
3443 STRCAT(nword, bw->bw_addstring);
3444 nbw->bw_addstring = vim_strsave(nword);
3445
3446 if (ae->ae_add_pw > ae->ae_add)
3447 nbw->bw_leadstring = vim_strnsave(ae->ae_add,
3448 ae->ae_add_pw - ae->ae_add);
3449 else
3450 nbw->bw_leadstring = NULL;
3451
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003452 (void)add_to_wordlist(newwords, nbw);
Bram Moolenaar5482f332005-04-17 20:18:43 +00003453
3454 /* Remember this word, we need to set bw_suffix
3455 * and bw_suffix later. */
3456 if (ga_grow(&prefixga, 1) == OK)
3457 ((basicword_T **)prefixga.ga_data)
3458 [prefixga.ga_len++] = nbw;
3459 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003460 }
3461 }
3462 else
3463 {
3464 /* Affix applies to this word, add the related affix
3465 * number. But only if it's not there yet. And keep the
3466 * list sorted, so that we can compare it later. */
3467 for (i = 0; i < gap->ga_len; ++i)
3468 {
3469 n = ((short_u *)gap->ga_data)[i];
3470 if (n >= ae->ae_affnr)
3471 {
3472 if (n == ae->ae_affnr)
3473 i = -1;
3474 break;
3475 }
3476 }
3477 if (i >= 0 && ga_grow(gap, 1) == OK)
3478 {
3479 if (i < gap->ga_len)
3480 mch_memmove(((short_u *)gap->ga_data) + i + 1,
3481 ((short_u *)gap->ga_data) + i,
3482 sizeof(short_u) * (gap->ga_len - i));
3483 ((short_u *)gap->ga_data)[i] = ae->ae_affnr;
3484 ++gap->ga_len;
3485 }
3486 }
3487 }
3488 }
3489 }
3490
3491 /*
3492 * For the words that we added for suffixes with non-word characters: Use
3493 * the prefix list of the main word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003494 */
Bram Moolenaar5482f332005-04-17 20:18:43 +00003495 for (i = 0; i < suffixga.ga_len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003496 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003497 nbw = ((basicword_T **)suffixga.ga_data)[i];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003498 if (ga_grow(&nbw->bw_prefix, bw->bw_prefix.ga_len) == OK)
3499 {
3500 mch_memmove(nbw->bw_prefix.ga_data, bw->bw_prefix.ga_data,
3501 bw->bw_prefix.ga_len * sizeof(short_u));
3502 nbw->bw_prefix.ga_len = bw->bw_prefix.ga_len;
3503 }
3504 }
3505
Bram Moolenaar5482f332005-04-17 20:18:43 +00003506 /*
3507 * For the words that we added for prefixes with non-word characters: Use
3508 * the suffix list of the main word.
3509 */
3510 for (i = 0; i < prefixga.ga_len; ++i)
3511 {
3512 nbw = ((basicword_T **)prefixga.ga_data)[i];
3513 if (ga_grow(&nbw->bw_suffix, bw->bw_suffix.ga_len) == OK)
3514 {
3515 mch_memmove(nbw->bw_suffix.ga_data, bw->bw_suffix.ga_data,
3516 bw->bw_suffix.ga_len * sizeof(short_u));
3517 nbw->bw_suffix.ga_len = bw->bw_suffix.ga_len;
3518 }
3519 }
3520
3521 ga_clear(&suffixga);
3522 ga_clear(&prefixga);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003523}
3524
3525/*
3526 * Go over all words in "oldwords" and change the old affix names to the new
3527 * affix numbers, check the conditions, fold case, extract the basic word and
3528 * additions.
3529 */
3530 static int
3531build_wordlist(newwords, oldwords, oldaff, regionmask)
3532 hashtab_T *newwords; /* basicword_T entries */
3533 hashtab_T *oldwords; /* dicword_T entries */
3534 afffile_T *oldaff; /* affixes for "oldwords" */
3535 int regionmask; /* value for bw_region */
3536{
3537 int todo;
3538 hashitem_T *old_hi;
3539 dicword_T *dw;
3540 basicword_T *bw;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003541 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003542
3543 todo = oldwords->ht_used;
3544 for (old_hi = oldwords->ht_array; todo > 0; ++old_hi)
3545 {
3546 if (!HASHITEM_EMPTY(old_hi))
3547 {
3548 --todo;
3549 dw = HI2DW(old_hi);
3550
3551 /* This takes time, print a message now and then. */
Bram Moolenaar5482f332005-04-17 20:18:43 +00003552 if ((todo & 0x3ff) == 0 || todo == (int)oldwords->ht_used - 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003553 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003554 sprintf((char *)message, _("%6d todo - %s"),
3555 todo, dw->dw_word);
3556 msg_start();
3557 msg_outtrans_attr(message, 0);
3558 msg_clr_eos();
3559 msg_didout = FALSE;
3560 msg_col = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003561 out_flush();
3562 ui_breakcheck();
3563 if (got_int)
3564 break;
3565 }
3566
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003567 bw = get_basicword(dw->dw_word, 10);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003568 if (bw == NULL)
3569 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003570 bw->bw_region = regionmask;
3571
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003572 (void)add_to_wordlist(newwords, bw);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003573
3574 /* Deal with any affix names on the old word, translate them
3575 * into affix numbers. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003576 if (dw->dw_affnm != NULL)
3577 trans_affixes(dw, bw, oldaff, newwords);
3578 }
3579 }
3580 if (todo > 0)
3581 return FAIL;
3582 return OK;
3583}
3584
3585/*
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003586 * Get a basicword_T from a word in original case.
3587 * Caller must set bw_region.
3588 * Returns NULL when something fails.
3589 */
3590 static basicword_T *
3591get_basicword(word, asize)
3592 char_u *word;
3593 int asize; /* growsize for affix garray */
3594{
3595 int dwlen;
3596 char_u foldword[MAXLINELEN];
3597 int flags;
3598 int clen;
3599 int leadlen;
3600 char_u *p;
3601 char_u leadstring[MAXLINELEN];
3602 int addlen;
3603 char_u addstring[MAXLINELEN];
3604 char_u *cp = NULL;
3605 int l;
3606 basicword_T *bw;
3607
3608 /* The basic words are always stored with folded case. */
3609 dwlen = STRLEN(word);
3610 (void)spell_casefold(word, dwlen, foldword, MAXLINELEN);
3611 flags = captype(word, word + dwlen);
3612
3613 /* Check for non-word characters before the word. */
3614 clen = 0;
3615 leadlen = 0;
3616 if (!spell_iswordc(foldword))
3617 {
3618 p = foldword;
3619 for (;;)
3620 {
3621 mb_ptr_adv(p);
3622 ++clen;
3623 if (*p == NUL) /* Only non-word chars (bad word!) */
3624 {
3625 if (p_verbose > 0)
Bram Moolenaard6ec8452005-05-31 22:02:19 +00003626 {
3627 verbose_enter();
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003628 smsg((char_u *)_("Warning: word without word characters: \"%s\""),
3629 foldword);
Bram Moolenaard6ec8452005-05-31 22:02:19 +00003630 verbose_leave();
3631 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003632 break;
3633 }
3634 if (spell_iswordc(p))
3635 {
3636 /* Move the leader to "leadstring" and remove it from
3637 * "foldword". */
3638 leadlen = p - foldword;
3639 mch_memmove(leadstring, foldword, leadlen);
3640 leadstring[leadlen] = NUL;
3641 mch_memmove(foldword, p, STRLEN(p) + 1);
3642 break;
3643 }
3644 }
3645 }
3646
3647 /* Check for non-word characters after word characters. */
3648 addlen = 0;
3649 for (p = foldword; spell_iswordc(p); mb_ptr_adv(p))
3650 {
3651 if (*p == NUL)
3652 break;
3653 ++clen;
3654 }
3655 if (*p != NUL)
3656 {
3657 /* Move the addition to "addstring" and truncate "foldword". */
3658 if (flags & BWF_KEEPCAP)
3659 {
3660 /* Preserve caps, need to skip the right number of
3661 * characters in the original word (case folding may
3662 * change the byte count). */
3663 l = 0;
3664 for (cp = word; l < clen; mb_ptr_adv(cp))
3665 ++l;
3666 addlen = STRLEN(cp);
3667 mch_memmove(addstring, cp, addlen + 1);
3668 }
3669 else
3670 {
3671 addlen = STRLEN(p);
3672 mch_memmove(addstring, p, addlen + 1);
3673 }
3674 *p = NUL;
3675 }
3676
3677 bw = (basicword_T *)alloc_clear((unsigned)sizeof(basicword_T)
3678 + STRLEN(foldword));
3679 if (bw == NULL)
3680 return NULL;
3681
3682 STRCPY(bw->bw_word, foldword);
3683
3684 if (leadlen > 0)
3685 bw->bw_leadstring = vim_strsave(leadstring);
3686 else
3687 bw->bw_leadstring = NULL;
3688 if (addlen > 0)
3689 bw->bw_addstring = vim_strsave(addstring);
3690 else
3691 bw->bw_addstring = NULL;
3692
3693 if (flags & BWF_KEEPCAP)
3694 {
3695 if (addlen == 0)
3696 /* use the whole word */
3697 bw->bw_caseword = vim_strsave(word + leadlen);
3698 else
3699 /* use only up to the addition */
3700 bw->bw_caseword = vim_strnsave(word + leadlen,
3701 cp - word - leadlen);
3702 }
3703
3704 bw->bw_flags = flags;
3705 ga_init2(&bw->bw_prefix, sizeof(short_u), asize);
3706 ga_init2(&bw->bw_suffix, sizeof(short_u), asize);
3707
3708 return bw;
3709}
3710
3711/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003712 * Go through the list of words and combine the ones that are identical except
3713 * for the region.
3714 */
3715 static void
3716combine_regions(newwords)
3717 hashtab_T *newwords;
3718{
3719 int todo;
3720 hashitem_T *hi;
3721 basicword_T *bw, *nbw, *pbw;
3722
3723 /* Loop over all basic words in the words table. */
3724 todo = newwords->ht_used;
3725 for (hi = newwords->ht_array; todo > 0; ++hi)
3726 {
3727 if (!HASHITEM_EMPTY(hi))
3728 {
3729 --todo;
3730
3731 /* Loop over the list of words for this basic word. Compare with
3732 * each following word in the same list. */
3733 for (bw = HI2BW(hi); bw != NULL; bw = bw->bw_next)
3734 {
3735 pbw = bw;
3736 for (nbw = pbw->bw_next; nbw != NULL; nbw = pbw->bw_next)
3737 {
3738 if (bw->bw_flags == nbw->bw_flags
3739 && (bw->bw_leadstring == NULL)
3740 == (nbw->bw_leadstring == NULL)
3741 && (bw->bw_addstring == NULL)
3742 == (nbw->bw_addstring == NULL)
3743 && ((bw->bw_flags & BWF_KEEPCAP) == 0
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003744 || bw->bw_caseword == NULL
3745 || nbw->bw_caseword == NULL
3746 || STRCMP(bw->bw_caseword,
3747 nbw->bw_caseword) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003748 && (bw->bw_leadstring == NULL
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003749 || STRCMP(bw->bw_leadstring,
3750 nbw->bw_leadstring) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003751 && (bw->bw_addstring == NULL
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003752 || STRCMP(bw->bw_addstring,
3753 nbw->bw_addstring) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003754 && same_affixes(bw, nbw)
3755 )
3756 {
3757 /* Match, combine regions and delete "nbw". */
3758 pbw->bw_next = nbw->bw_next;
3759 bw->bw_region |= nbw->bw_region;
3760 free_basicword(nbw);
3761 }
3762 else
3763 /* No match, continue with next one. */
3764 pbw = nbw;
3765 }
3766 }
3767 }
3768 }
3769}
3770
3771/*
3772 * Return TRUE when the prefixes and suffixes for "bw" and "nbw" are equal.
3773 */
3774 static int
3775same_affixes(bw, nbw)
3776 basicword_T *bw;
3777 basicword_T *nbw;
3778{
3779 return (bw->bw_prefix.ga_len == nbw->bw_prefix.ga_len
3780 && bw->bw_suffix.ga_len == nbw->bw_suffix.ga_len
3781 && (bw->bw_prefix.ga_len == 0
3782 || vim_memcmp(bw->bw_prefix.ga_data,
3783 nbw->bw_prefix.ga_data,
3784 bw->bw_prefix.ga_len * sizeof(short_u)) == 0)
3785 && (bw->bw_suffix.ga_len == 0
3786 || vim_memcmp(bw->bw_suffix.ga_data,
3787 nbw->bw_suffix.ga_data,
3788 bw->bw_suffix.ga_len * sizeof(short_u)) == 0));
3789}
3790
3791/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003792 * For each basic word with additions turn the suffixes into other additions
3793 * and/or new basic words. For each basic word with a leadstring turn the
3794 * prefixes into other leadstrings and/or new basic words.
3795 * The result is that no affixes apply to the additions or leadstring of a
3796 * word.
3797 * This is also needed when a word with an addition has a prefix and the word
3798 * with prefix also exists. E.g., "blurp's/D" (D is prefix "de") and
3799 * "deblurp". "deblurp" would match and no prefix would be tried.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003800 *
3801 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003802 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003803 static int
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003804expand_affixes(newwords, prefgap, suffgap)
3805 hashtab_T *newwords;
3806 garray_T *prefgap;
3807 garray_T *suffgap;
3808{
3809 int todo;
3810 hashitem_T *hi;
3811 basicword_T *bw;
3812 int pi, si;
3813 affentry_T *pae, *sae;
3814 garray_T add_words;
3815 int n;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003816 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003817 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003818
3819 ga_init2(&add_words, sizeof(basicword_T *), 10);
3820
3821 todo = newwords->ht_used;
3822 for (hi = newwords->ht_array; todo > 0; ++hi)
3823 {
3824 if (!HASHITEM_EMPTY(hi))
3825 {
3826 --todo;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003827
3828 /* This takes time, print a message now and then. */
3829 if ((todo & 0x3ff) == 0 || todo == (int)newwords->ht_used - 1)
3830 {
3831 sprintf((char *)message, _("%6d todo - %s"),
3832 todo, HI2BW(hi)->bw_word);
3833 msg_start();
3834 msg_outtrans_attr(message, 0);
3835 msg_clr_eos();
3836 msg_didout = FALSE;
3837 msg_col = 0;
3838 out_flush();
3839 ui_breakcheck();
3840 if (got_int)
3841 break;
3842 }
3843
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003844 for (bw = HI2BW(hi); bw != NULL; bw = bw->bw_next)
3845 {
3846 /*
3847 * Need to fix affixes if there is a leader or addition and
3848 * there are prefixes or suffixes.
3849 */
3850 if ((bw->bw_leadstring != NULL || bw->bw_addstring != NULL)
3851 && (bw->bw_prefix.ga_len != 0
3852 || bw->bw_suffix.ga_len != 0))
3853 {
3854 /* Loop over all prefix numbers, but first without a
3855 * prefix. */
3856 for (pi = -1; pi < bw->bw_prefix.ga_len; ++pi)
3857 {
3858 pae = NULL;
3859 if (pi >= 0)
3860 {
3861 n = ((short_u *)bw->bw_prefix.ga_data)[pi];
3862 pae = ((affheader_T *)prefgap->ga_data + n)
3863 ->ah_first;
3864 }
3865
3866 /* Loop over all entries for prefix "pi". Do it once
3867 * when there is no prefix (pi == -1). */
3868 do
3869 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003870 /* Skip prewords, they don't need to be expanded. */
3871 if (pae == NULL || !pae->ae_preword)
3872 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003873 /* Loop over all suffix numbers. Do without a
3874 * suffix first when there is a prefix. */
3875 for (si = (pi == -1 ? 0 : -1);
3876 si < bw->bw_suffix.ga_len; ++si)
3877 {
3878 sae = NULL;
3879 if (si >= 0)
3880 {
3881 n = ((short_u *)bw->bw_suffix.ga_data)[si];
3882 sae = ((affheader_T *)suffgap->ga_data + n)
3883 ->ah_first;
3884 }
3885
3886 /* Loop over all entries for suffix "si". Do
3887 * it once when there is no suffix (si == -1).
3888 */
3889 do
3890 {
3891 /* Expand the word for this combination of
3892 * prefixes and affixes. */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003893 if (expand_one_aff(bw, &add_words,
3894 pae, sae) == FAIL)
3895 {
3896 retval = FAIL;
3897 goto theend;
3898 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003899
3900 /* Advance to next suffix entry, if there
3901 * is one. */
3902 if (sae != NULL)
3903 sae = sae->ae_next;
3904 } while (sae != NULL);
3905 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003906 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003907
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003908 /* Advance to next prefix entry, if there is one. */
3909 if (pae != NULL)
3910 pae = pae->ae_next;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003911 } while (pae != NULL);
3912 }
3913 }
3914 }
3915 }
3916 }
3917
3918 /*
3919 * Add the new words afterwards, can't change "newwords" while going over
3920 * all its items.
3921 */
3922 for (pi = 0; pi < add_words.ga_len; ++pi)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003923 {
3924 retval = add_to_wordlist(newwords,
3925 ((basicword_T **)add_words.ga_data)[pi]);
3926 if (retval == FAIL)
3927 break;
3928 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003929
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003930theend:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003931 ga_clear(&add_words);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003932 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003933}
3934
3935/*
3936 * Add one word to "add_words" for basic word "bw" with additions, adding
3937 * prefix "pae" and suffix "sae". Either "pae" or "sae" can be NULL.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003938 * Don't do this when not necessary:
3939 * - no leadstring and adding prefix doesn't result in existing word.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003940 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003941 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003942 static int
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003943expand_one_aff(bw, add_words, pae, sae)
3944 basicword_T *bw;
3945 garray_T *add_words;
3946 affentry_T *pae;
3947 affentry_T *sae;
3948{
3949 char_u word[MAXWLEN + 1];
3950 char_u caseword[MAXWLEN + 1];
3951 int l = 0;
3952 int choplen = 0;
3953 int ll;
3954 basicword_T *nbw;
3955
3956 /* Prepend prefix to the basic word if there is a prefix and there is no
3957 * leadstring. */
3958 if (pae != NULL && bw->bw_leadstring == NULL)
3959 {
3960 if (pae->ae_add != NULL)
3961 {
3962 l = STRLEN(pae->ae_add);
3963 mch_memmove(word, pae->ae_add, l);
3964 }
3965 if (pae->ae_chop != NULL)
3966 choplen = STRLEN(pae->ae_chop);
3967 }
3968
3969 /* Copy the body of the word. */
3970 STRCPY(word + l, bw->bw_word + choplen);
3971
3972 /* Do the same for bw_caseword, if it's there. */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003973 if ((bw->bw_flags & BWF_KEEPCAP) && bw->bw_caseword != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003974 {
3975 if (l > 0)
3976 mch_memmove(caseword, pae->ae_add, l);
3977 STRCPY(caseword + l, bw->bw_caseword + choplen);
3978 }
3979
3980 /* Append suffix to the basic word if there is a suffix and there is no
3981 * addstring. */
3982 if (sae != 0 && bw->bw_addstring == NULL)
3983 {
3984 l = STRLEN(word);
3985 if (sae->ae_chop != NULL)
3986 l -= STRLEN(sae->ae_chop);
3987 if (sae->ae_add == NULL)
3988 word[l] = NUL;
3989 else
3990 STRCPY(word + l, sae->ae_add);
3991
3992 if (bw->bw_flags & BWF_KEEPCAP)
3993 {
3994 /* Do the same for the caseword. */
3995 l = STRLEN(caseword);
3996 if (sae->ae_chop != NULL)
3997 l -= STRLEN(sae->ae_chop);
3998 if (sae->ae_add == NULL)
3999 caseword[l] = NUL;
4000 else
4001 STRCPY(caseword + l, sae->ae_add);
4002 }
4003 }
4004
4005 nbw = (basicword_T *)alloc_clear((unsigned)
4006 sizeof(basicword_T) + STRLEN(word));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004007 if (nbw == NULL)
4008 return FAIL;
4009
4010 /* Add the new word to the list of words to be added later. */
4011 if (ga_grow(add_words, 1) == FAIL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004012 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004013 vim_free(nbw);
4014 return FAIL;
4015 }
4016 ((basicword_T **)add_words->ga_data)[add_words->ga_len++] = nbw;
4017
4018 /* Copy the (modified) basic word, flags and region. */
4019 STRCPY(nbw->bw_word, word);
4020 nbw->bw_flags = bw->bw_flags;
4021 nbw->bw_region = bw->bw_region;
4022
4023 /* Set the (modified) caseword. */
4024 if (bw->bw_flags & BWF_KEEPCAP)
4025 nbw->bw_caseword = vim_strsave(caseword);
4026 else
4027 nbw->bw_caseword = NULL;
4028
4029 if (bw->bw_leadstring != NULL)
4030 {
4031 if (pae != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004032 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004033 /* Prepend prefix to leadstring. */
4034 ll = STRLEN(bw->bw_leadstring);
4035 l = choplen = 0;
4036 if (pae->ae_add != NULL)
4037 l = STRLEN(pae->ae_add);
4038 if (pae->ae_chop != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004039 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004040 choplen = STRLEN(pae->ae_chop);
4041 if (choplen > ll) /* TODO: error? */
4042 choplen = ll;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004043 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004044 nbw->bw_leadstring = alloc((unsigned)(ll + l - choplen + 1));
4045 if (nbw->bw_leadstring != NULL)
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004046 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004047 if (l > 0)
4048 mch_memmove(nbw->bw_leadstring, pae->ae_add, l);
4049 STRCPY(nbw->bw_leadstring + l, bw->bw_leadstring + choplen);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004050 }
4051 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004052 else
4053 nbw->bw_leadstring = vim_strsave(bw->bw_leadstring);
4054 }
4055 else if (bw->bw_prefix.ga_len > 0)
4056 {
4057 /* There is no leadstring, copy the list of possible prefixes. */
4058 ga_init2(&nbw->bw_prefix, sizeof(short_u), 1);
4059 if (ga_grow(&nbw->bw_prefix, bw->bw_prefix.ga_len) == OK)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004060 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004061 mch_memmove(nbw->bw_prefix.ga_data, bw->bw_prefix.ga_data,
4062 bw->bw_prefix.ga_len * sizeof(short_u));
4063 nbw->bw_prefix.ga_len = bw->bw_prefix.ga_len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004064 }
4065 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004066
4067 if (bw->bw_addstring != NULL)
4068 {
4069 if (sae != NULL)
4070 {
4071 /* Append suffix to addstring. */
4072 l = STRLEN(bw->bw_addstring);
4073 if (sae->ae_chop != NULL)
4074 {
4075 l -= STRLEN(sae->ae_chop);
4076 if (l < 0) /* TODO: error? */
4077 l = 0;
4078 }
4079 if (sae->ae_add == NULL)
4080 ll = 0;
4081 else
4082 ll = STRLEN(sae->ae_add);
4083 nbw->bw_addstring = alloc((unsigned)(ll + l - choplen + 1));
4084 if (nbw->bw_addstring != NULL)
4085 {
4086 STRCPY(nbw->bw_addstring, bw->bw_addstring);
4087 if (sae->ae_add == NULL)
4088 nbw->bw_addstring[l] = NUL;
4089 else
4090 STRCPY(nbw->bw_addstring + l, sae->ae_add);
4091 }
4092 }
4093 else
4094 nbw->bw_addstring = vim_strsave(bw->bw_addstring);
4095 }
4096
4097 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004098}
4099
4100/*
4101 * Add basicword_T "*bw" to wordlist "newwords".
4102 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004103 static int
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004104add_to_wordlist(newwords, bw)
4105 hashtab_T *newwords;
4106 basicword_T *bw;
4107{
4108 hashitem_T *hi;
4109 basicword_T *bw2;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004110 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004111
4112 hi = hash_find(newwords, bw->bw_word);
4113 if (HASHITEM_EMPTY(hi))
4114 {
4115 /* New entry, add to hashlist. */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004116 retval = hash_add(newwords, bw->bw_word);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004117 bw->bw_next = NULL;
4118 }
4119 else
4120 {
4121 /* Existing entry, append to list of basic words. */
4122 bw2 = HI2BW(hi);
4123 bw->bw_next = bw2->bw_next;
4124 bw2->bw_next = bw;
4125 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004126 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004127}
4128
4129/*
4130 * Write a number to file "fd", MSB first, in "len" bytes.
4131 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004132 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004133put_bytes(fd, nr, len)
4134 FILE *fd;
4135 long_u nr;
4136 int len;
4137{
4138 int i;
4139
4140 for (i = len - 1; i >= 0; --i)
4141 putc((int)(nr >> (i * 8)), fd);
4142}
4143
4144/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004145 * Write affix info. <affitemcnt> <affitem> ...
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004146 */
4147 static void
4148write_affix(fd, ah)
4149 FILE *fd;
4150 affheader_T *ah;
4151{
4152 int i = 0;
4153 affentry_T *ae;
4154 char_u *p;
4155 int round;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004156 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004157
4158 /* Count the number of entries. */
4159 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
4160 ++i;
4161 put_bytes(fd, (long_u)i, 2); /* <affitemcnt> */
4162
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004163 /* <affitem>: <affflags> <affchoplen> <affchop> <affaddlen> <affadd> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004164 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004165 {
4166 flags = ah->ah_combine ? AFF_COMBINE : 0;
4167 if (ae->ae_preword)
4168 flags |= AFF_PREWORD;
4169 fputc(flags, fd); /* <affflags> */
4170
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004171 for (round = 1; round <= 2; ++round)
4172 {
4173 p = round == 1 ? ae->ae_chop : ae->ae_add;
4174 if (p == NULL)
4175 putc(0, fd); /* <affchoplen> / <affaddlen> */
4176 else
4177 {
4178 putc(STRLEN(p), fd); /* <affchoplen> / <affaddlen> */
4179 /* <affchop> / <affadd> */
4180 fwrite(p, STRLEN(p), (size_t)1, fd);
4181 }
4182 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004183 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004184}
4185
4186/*
4187 * Write list of affix NRs: <affixcnt> <affixNR> ...
4188 */
4189 static void
4190write_affixlist(fd, aff, bytes)
4191 FILE *fd;
4192 garray_T *aff;
4193 int bytes;
4194{
4195 int i;
4196
4197 if (aff->ga_len > 0)
4198 {
4199 putc(aff->ga_len, fd); /* <affixcnt> */
4200 for (i = 0; i < aff->ga_len; ++i)
4201 put_bytes(fd, (long_u )((short_u *)aff->ga_data)[i], bytes);
4202 }
4203}
4204
4205/*
4206 * Vim spell file format: <HEADER> <PREFIXLIST> <SUFFIXLIST>
4207 * <SUGGEST> <WORDLIST>
4208 *
4209 * <HEADER>: <fileID> <regioncnt> <regionname> ...
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004210 * <charflagslen> <charflags> <fcharslen> <fchars>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004211 *
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004212 * <fileID> 10 bytes "VIMspell04"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004213 * <regioncnt> 1 byte number of regions following (8 supported)
4214 * <regionname> 2 bytes Region name: ca, au, etc.
4215 * First <regionname> is region 1.
4216 *
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004217 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
4218 * <charflags> N bytes List of flags (first one is for character 128):
4219 * 0x01 word character
4220 * 0x01 upper-case character
4221 * <fcharslen> 2 bytes Number of bytes in <fchars>.
4222 * <fchars> N bytes Folded characters, first one is for character 128.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004223 *
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004224 *
4225 * <PREFIXLIST>: <affcount> <affix> ...
4226 * <SUFFIXLIST>: <affcount> <affix> ...
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004227 * list of possible affixes: prefixes and suffixes.
4228 *
4229 * <affcount> 2 bytes Number of affixes (MSB comes first).
4230 * When more than 256 an affixNR is 2 bytes.
4231 * This is separate for prefixes and suffixes!
4232 * First affixNR is 0.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004233 *
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004234 * <affix>: <affitemcnt> <affitem> ...
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004235 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004236 * <affitemcnt> 2 bytes Number of affixes with this affixNR (MSB first).
4237 *
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004238 * <affitem>: <affflags> <affchoplen> <affchop> <affaddlen> <affadd>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004239 *
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004240 * <affflags> 1 byte 0x01: prefix combines with suffix, AFF_COMBINE
4241 * 0x02: prefix includes word, AFF_PREWORD
4242 * 0x04-0x80: unset
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004243 * <affchoplen> 1 byte Length of <affchop> in bytes.
4244 * <affchop> N bytes To be removed from basic word.
4245 * <affaddlen> 1 byte Length of <affadd> in bytes.
4246 * <affadd> N bytes To be added to basic word.
4247 *
4248 *
4249 * <SUGGEST> : <suggestlen> <more> ...
4250 *
4251 * <suggestlen> 4 bytes Length of <SUGGEST> in bytes, excluding
4252 * <suggestlen>. MSB first.
4253 * <more> To be defined.
4254 *
4255 *
4256 * <WORDLIST>: <wordcount> <worditem> ...
4257 *
4258 * <wordcount> 4 bytes Number of <worditem> following. MSB first.
4259 *
4260 * <worditem>: <nr> <string> <flags> [<flags2>]
4261 * [<caselen> <caseword>]
4262 * [<affixcnt> <affixNR> ...] (prefixes)
4263 * [<affixcnt> <affixNR> ...] (suffixes)
4264 * [<region>]
4265 * [<addcnt> <add> ...]
4266 *
4267 * <nr> i 1 byte Number of bytes copied from previous word.
4268 * <string> N bytes Additional bytes for word, up to byte smaller than
4269 * 0x20 (space).
4270 * Must only contain case-folded word characters.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004271 * <flags> 1 byte 0x01: word is valid without addition, BWF_VALID
4272 * 0x02: has region byte, BWF_REGION
4273 * 0x04: first letter must be upper-case, BWF_ONECAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004274 * 0x08: has suffixes, <affixcnt> and <affixNR> follow
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004275 * BWF_SUFFIX
4276 * 0x10: more flags, <flags2> follows next, BWF_SECOND
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004277 * 0x20-0x80: can't be used, unset
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004278 * <flags2> 1 byte 0x01: has additions, <addcnt> and <add> follow,
4279 * BWF_ADDS
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004280 * 0x02: has prefixes, <affixcnt> and <affixNR> follow
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004281 * BWF_PREFIX
4282 * 0x04: all letters must be upper-case, BWF_ALLCAP
4283 * 0x08: case must match, BWF_KEEPCAP
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004284 * 0x10: has more than 255 additions, <addcnt> is two
4285 * bytes, BWF_ADDS_M
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004286 * 0x10-0x80: unset
4287 * <caselen> 1 byte Length of <caseword>.
4288 * <caseword> N bytes Word with matching case.
4289 * <affixcnt> 1 byte Number of affix NRs following.
4290 * <affixNR> 1 or 2 byte Number of possible affix for this word.
4291 * When using 2 bytes MSB comes first.
4292 * <region> 1 byte Bitmask for regions in which word is valid. When
4293 * omitted it's valid in all regions.
4294 * Lowest bit is for region 1.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004295 * <addcnt> 1 or 2 byte Number of <add> items following.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004296 *
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004297 * <add>: <addflags> <addlen> [<leadlen>] [<copylen>] [<addstring>] [<region>]
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004298 *
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004299 * <addflags> 1 byte 0x01: unset
4300 * 0x02: has region byte, ADD_REGION
4301 * 0x04: first letter must be upper-case, ADD_ONECAP
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004302 * 0x08: unset
4303 * 0x10: has a <leadlen>, ADD_LEADLEN
4304 * 0x20: has a <copylen>, ADD_COPYLEN
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004305 * 0x40: all letters must be upper-case, ADD_ALLCAP
4306 * 0x80: fixed case, <addstring> is the whole word
4307 * with matching case, ADD_KEEPCAP.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004308 * <addlen> 1 byte Length of <addstring> in bytes.
4309 * <leadlen> 1 byte Number of bytes at start of <addstring> that must
4310 * come before the start of the basic word.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004311 * <copylen> 1 byte Number of bytes copied from previous <addstring>.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004312 * <addstring> N bytes Word characters, before/in/after the word.
4313 *
4314 * All text characters are in 'encoding': <affchop>, <affadd>, <string>,
4315 * <caseword>> and <addstring>.
4316 * All other fields are ASCII: <regionname>
4317 * <string> is always case-folded.
4318 */
4319
4320/*
4321 * Write the Vim spell file "fname".
4322 */
4323 static void
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004324write_vim_spell(fname, prefga, suffga, newwords, regcount, regchars, ascii)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004325 char_u *fname;
4326 garray_T *prefga; /* prefixes, affheader_T entries */
4327 garray_T *suffga; /* suffixes, affheader_T entries */
4328 hashtab_T *newwords; /* basic words, basicword_T entries */
4329 int regcount; /* number of regions */
4330 char_u *regchars; /* region names */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004331 int ascii; /* TRUE for ascii spell file */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004332{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004333 winfo_T wif;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004334 garray_T *gap;
4335 hashitem_T *hi;
4336 char_u **wtab;
4337 int todo;
4338 int flags, aflags;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004339 basicword_T *bw, *bwf, *bw2 = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004340 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004341 int round;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004342 garray_T bwga;
4343
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004344 vim_memset(&wif, 0, sizeof(winfo_T));
4345
4346 wif.wif_fd = fopen((char *)fname, "w");
4347 if (wif.wif_fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004348 {
4349 EMSG2(_(e_notopen), fname);
4350 return;
4351 }
4352
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004353 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
4354 * <charflagslen> <charflags> <fcharslen> <fchars> */
4355 fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, wif.wif_fd); /* <fileID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004356
4357 /* write the region names if there is more than one */
4358 if (regcount > 1)
4359 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004360 putc(regcount, wif.wif_fd); /* <regioncnt> <regionname> ... */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004361 fwrite(regchars, (size_t)(regcount * 2), (size_t)1, wif.wif_fd);
4362 wif.wif_regionmask = (1 << regcount) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004363 }
4364 else
4365 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004366 putc(0, wif.wif_fd);
4367 wif.wif_regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004368 }
4369
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004370 /* Write the table with character flags and table for case folding.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004371 * <charflagslen> <charflags> <fcharlen> <fchars>
4372 * Skip this for ASCII, the table may conflict with the one used for
4373 * 'encoding'. */
4374 if (ascii)
4375 {
4376 putc(0, wif.wif_fd);
4377 putc(0, wif.wif_fd);
4378 putc(0, wif.wif_fd);
4379 }
4380 else
4381 write_spell_chartab(wif.wif_fd);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004382
4383 /* <PREFIXLIST>: <affcount> <affix> ...
4384 * <SUFFIXLIST>: <affcount> <affix> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004385 for (round = 1; round <= 2; ++round)
4386 {
4387 gap = round == 1 ? prefga : suffga;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004388 put_bytes(wif.wif_fd, (long_u)gap->ga_len, 2); /* <affcount> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004389
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004390 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004391 write_affix(wif.wif_fd, (affheader_T *)gap->ga_data + i);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004392 }
4393
4394 /* Number of bytes used for affix NR depends on affix count. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004395 wif.wif_prefm = (prefga->ga_len > 256) ? 2 : 1;
4396 wif.wif_suffm = (suffga->ga_len > 256) ? 2 : 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004397
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004398 /* <SUGGEST> : <suggestlen> <more> ...
4399 * TODO. Only write a zero length for now. */
4400 put_bytes(wif.wif_fd, 0L, 4); /* <suggestlen> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004401
4402 /*
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004403 * <WORDLIST>: <wordcount> <worditem> ...
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004404 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004405
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004406 /* number of basic words in 4 bytes */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004407 put_bytes(wif.wif_fd, newwords->ht_used, 4); /* <wordcount> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004408
4409 /*
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004410 * Sort the word list, so that we can copy as many bytes as possible from
4411 * the previous word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004412 */
4413 wtab = (char_u **)alloc((unsigned)(sizeof(char_u *) * newwords->ht_used));
4414 if (wtab != NULL)
4415 {
4416 /* Make a table with pointers to each word. */
4417 todo = newwords->ht_used;
4418 for (hi = newwords->ht_array; todo > 0; ++hi)
4419 if (!HASHITEM_EMPTY(hi))
4420 wtab[--todo] = hi->hi_key;
4421
4422 /* Sort. */
4423 sort_strings(wtab, (int)newwords->ht_used);
4424
4425 /* Now write each basic word to the spell file. */
4426 ga_init2(&bwga, sizeof(basicword_T *), 10);
Bram Moolenaar5482f332005-04-17 20:18:43 +00004427 for (todo = 0; (long_u)todo < newwords->ht_used; ++todo)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004428 {
4429 bwf = KEY2BW(wtab[todo]);
4430
4431 /*
4432 * Reorder the list of basicword_T words: make a list for words
4433 * with the same case-folded word. Put them together for same
4434 * caps (ONECAP, ALLCAP and various KEEPCAP words) and same
4435 * affixes. Each list will then be put as a basic word with
4436 * additions.
4437 * This won't take much space, since the basic word is the same
4438 * every time, only its length is written.
4439 */
4440 bwga.ga_len = 0;
4441 for (bw = bwf; bw != NULL; bw = bw->bw_next)
4442 {
4443 flags = bw->bw_flags & (BWF_ONECAP | BWF_KEEPCAP | BWF_ALLCAP);
4444
4445 /* Go through the lists we found so far. Break when the case
4446 * matches. */
4447 for (i = 0; i < bwga.ga_len; ++i)
4448 {
4449 bw2 = ((basicword_T **)bwga.ga_data)[i];
4450 aflags = bw2->bw_flags & (BWF_ONECAP | BWF_KEEPCAP
4451 | BWF_ALLCAP);
4452 if (flags == aflags
4453 && ((flags & BWF_KEEPCAP) == 0
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004454 || bw->bw_caseword == NULL
4455 || bw2->bw_caseword == NULL
4456 || STRCMP(bw->bw_caseword,
4457 bw2->bw_caseword) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004458 && same_affixes(bw, bw2))
4459 break;
4460 }
4461 if (i == bwga.ga_len)
4462 {
4463 /* No word with similar caps, make a new list. */
4464 if (ga_grow(&bwga, 1) == FAIL)
4465 break;
4466 ((basicword_T **)bwga.ga_data)[i] = bw;
4467 bw->bw_cnext = NULL;
4468 ++bwga.ga_len;
4469 }
4470 else
4471 {
4472 /* Add to list of words with similar caps. */
4473 bw->bw_cnext = bw2->bw_cnext;
4474 bw2->bw_cnext = bw;
4475 }
4476 }
4477
4478 /* Prefer the word with no caps to use as the first basic word.
4479 * At least one without KEEPCAP. */
4480 bw = NULL;
4481 for (i = 0; i < bwga.ga_len; ++i)
4482 {
4483 bw2 = ((basicword_T **)bwga.ga_data)[i];
4484 if (bw == NULL
4485 || (bw2->bw_flags & (BWF_ONECAP | BWF_KEEPCAP
4486 | BWF_ALLCAP)) == 0
4487 || (bw->bw_flags & BWF_KEEPCAP))
4488 bw = bw2;
4489 }
4490
4491 /* Write first basic word. If it's KEEPCAP then we need a word
4492 * without VALID flag first (makes it easier to read the list back
4493 * in). */
4494 if (bw->bw_flags & BWF_KEEPCAP)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004495 write_bword(&wif, bw, TRUE);
4496 write_bword(&wif, bw, FALSE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004497
4498 /* Write other basic words, with different caps. */
4499 for (i = 0; i < bwga.ga_len; ++i)
4500 {
4501 bw2 = ((basicword_T **)bwga.ga_data)[i];
4502 if (bw2 != bw)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004503 write_bword(&wif, bw2, FALSE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004504 }
4505 }
4506
4507 ga_clear(&bwga);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004508 vim_free(wtab);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004509 }
4510
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004511 fclose(wif.wif_fd);
4512
4513 /* Print a few statistics. */
4514 if (wif.wif_addmaxw == NULL)
4515 wif.wif_addmaxw = (char_u *)"";
4516 smsg((char_u *)_("Maximum number of adds on a word: %ld (%s)"),
4517 wif.wif_addmax, wif.wif_addmaxw);
4518 smsg((char_u *)_("Average number of adds on a word: %f"),
4519 (float)wif.wif_acount / (float)wif.wif_wcount);
4520}
4521
4522/*
4523 * Compare two basic words for their <addstring>.
4524 */
4525static int
4526#ifdef __BORLANDC__
4527_RTLENTRYF
4528#endif
4529bw_compare __ARGS((const void *s1, const void *s2));
4530
4531 static int
4532#ifdef __BORLANDC__
4533_RTLENTRYF
4534#endif
4535bw_compare(s1, s2)
4536 const void *s1;
4537 const void *s2;
4538{
4539 basicword_T *bw1 = *(basicword_T **)s1;
4540 basicword_T *bw2 = *(basicword_T **)s2;
4541 int i = 0;
4542
4543 /* compare the leadstrings */
4544 if (bw1->bw_leadstring == NULL)
4545 {
4546 if (bw2->bw_leadstring != NULL)
4547 return 1;
4548 }
4549 else if (bw2->bw_leadstring == NULL)
4550 return -1;
4551 else
4552 i = STRCMP(bw1->bw_leadstring, bw2->bw_leadstring);
4553
4554 if (i == 0)
4555 {
4556 /* leadstrings are identical, compare the addstrings */
4557 if (bw1->bw_addstring == NULL)
4558 {
4559 if (bw2->bw_addstring != NULL)
4560 return 1;
4561 }
4562 else if (bw2->bw_addstring == NULL)
4563 return -1;
4564 else
4565 i = STRCMP(bw1->bw_addstring, bw2->bw_addstring);
4566 }
4567 return i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004568}
4569
4570/*
4571 * Write basic word, followed by any additions.
4572 *
4573 * <worditem>: <nr> <string> <flags> [<flags2>]
4574 * [<caselen> <caseword>]
4575 * [<affixcnt> <affixNR> ...] (prefixes)
4576 * [<affixcnt> <affixNR> ...] (suffixes)
4577 * [<region>]
4578 * [<addcnt> <add> ...]
4579 */
4580 static void
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004581write_bword(wif, bwf, lowcap)
4582 winfo_T *wif; /* info for writing */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004583 basicword_T *bwf;
4584 int lowcap; /* write KEEPKAP word as not-valid */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004585{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004586 FILE *fd = wif->wif_fd;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004587 int flags;
4588 int aflags;
4589 int len;
4590 int leadlen, addlen;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004591 int copylen;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004592 int clen;
4593 int adds = 0;
4594 int i;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004595 int idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004596 basicword_T *bw, *bw2;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004597 basicword_T **wtab;
4598 int count;
4599 int l;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004600
4601 /* Check how many bytes can be copied from the previous word. */
4602 len = STRLEN(bwf->bw_word);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004603 if (wif->wif_prevbw == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004604 clen = 0;
4605 else
4606 for (clen = 0; clen < len
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004607 && wif->wif_prevbw->bw_word[clen] == bwf->bw_word[clen]; ++clen)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004608 ;
4609 putc(clen, fd); /* <nr> */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004610 wif->wif_prevbw = bwf;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004611 /* <string> */
4612 if (len > clen)
4613 fwrite(bwf->bw_word + clen, (size_t)(len - clen), (size_t)1, fd);
4614
4615 /* Try to find a word without additions to use first. */
4616 bw = bwf;
4617 for (bw2 = bwf; bw2 != NULL; bw2 = bw2->bw_cnext)
4618 {
4619 if (bw2->bw_addstring != NULL || bw2->bw_leadstring != NULL)
4620 ++adds;
4621 else
4622 bw = bw2;
4623 }
4624
4625 /* Flags: If there is no leadstring and no addstring the basic word is
4626 * valid, may have prefixes, suffixes and region. */
4627 flags = bw->bw_flags;
4628 if (bw->bw_addstring == NULL && bw->bw_leadstring == NULL)
4629 {
4630 flags |= BWF_VALID;
4631
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004632 /* Flags: add the region byte if the word isn't valid in all
4633 * regions. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004634 if (wif->wif_regionmask != 0 && (bw->bw_region & wif->wif_regionmask)
4635 != wif->wif_regionmask)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004636 flags |= BWF_REGION;
4637 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004638 /* Add the prefix/suffix list if there are prefixes/suffixes. */
4639 if (bw->bw_leadstring == NULL && bw->bw_prefix.ga_len > 0)
4640 flags |= BWF_PREFIX;
4641 if (bw->bw_addstring == NULL && bw->bw_suffix.ga_len > 0)
4642 flags |= BWF_SUFFIX;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004643
4644 /* Flags: may have additions. */
4645 if (adds > 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004646 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004647 flags |= BWF_ADDS;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004648 if (adds >= 256)
4649 flags |= BWF_ADDS_M;
4650 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004651
4652 /* The dummy word before a KEEPCAP word doesn't have any flags, they are
4653 * in the actual word that follows. */
4654 if (lowcap)
4655 flags = 0;
4656
4657 /* Flags: when the upper byte is not used we only write one flags
4658 * byte, if it's used then set an extra flag in the first byte and
4659 * also write the second byte. */
4660 if ((flags & 0xff00) == 0)
4661 putc(flags, fd); /* <flags> */
4662 else
4663 {
4664 putc(flags | BWF_SECOND, fd); /* <flags> */
4665 putc((int)((unsigned)flags >> 8), fd); /* <flags2> */
4666 }
4667
4668 /* First dummy word doesn't need anything but flags. */
4669 if (lowcap)
4670 return;
4671
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004672 if ((flags & BWF_KEEPCAP) && bw->bw_caseword != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004673 {
4674 len = STRLEN(bw->bw_caseword);
4675 putc(len, fd); /* <caselen> */
4676 for (i = 0; i < len; ++i)
4677 putc(bw->bw_caseword[i], fd); /* <caseword> */
4678 }
4679
4680 /* write prefix and suffix lists: <affixcnt> <affixNR> ... */
4681 if (flags & BWF_PREFIX)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004682 write_affixlist(fd, &bw->bw_prefix, wif->wif_prefm);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004683 if (flags & BWF_SUFFIX)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004684 write_affixlist(fd, &bw->bw_suffix, wif->wif_suffm);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004685
4686 if (flags & BWF_REGION)
4687 putc(bw->bw_region, fd); /* <region> */
4688
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004689 ++wif->wif_wcount;
4690
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004691 /*
4692 * Additions.
4693 */
4694 if (adds > 0)
4695 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004696 if (adds >= 256)
4697 put_bytes(fd, (long_u)adds, 2); /* 2 byte <addcnt> */
4698 else
4699 putc(adds, fd); /* 1 byte <addcnt> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004700
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004701 /* statistics */
4702 wif->wif_acount += adds;
4703 if (wif->wif_addmax < adds)
4704 {
4705 wif->wif_addmax = adds;
4706 wif->wif_addmaxw = bw->bw_word;
4707 }
4708
4709 /*
4710 * Sort the list of additions, so that we can copy as many bytes as
4711 * possible from the previous addstring.
4712 */
4713
4714 /* Make a table with pointers to each basic word that has additions. */
4715 wtab = (basicword_T **)alloc((unsigned)(sizeof(basicword_T *) * adds));
4716 if (wtab == NULL)
4717 return;
4718 count = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004719 for (bw = bwf; bw != NULL; bw = bw->bw_cnext)
4720 if (bw->bw_leadstring != NULL || bw->bw_addstring != NULL)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004721 wtab[count++] = bw;
4722
4723 /* Sort. */
4724 qsort((void *)wtab, (size_t)count, sizeof(basicword_T *), bw_compare);
4725
4726 /* Now write each basic word to the spell file. Copy bytes from the
4727 * previous leadstring/addstring if possible. */
4728 bw2 = NULL;
4729 for (idx = 0; idx < count; ++idx)
4730 {
4731 bw = wtab[idx];
4732
4733 /* <add>: <addflags> <addlen> [<leadlen>] [<copylen>]
4734 * [<addstring>] [<region>] */
4735 copylen = 0;
4736 if (bw->bw_leadstring == NULL)
4737 leadlen = 0;
4738 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004739 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004740 leadlen = STRLEN(bw->bw_leadstring);
4741 if (bw2 != NULL && bw2->bw_leadstring != NULL)
4742 for ( ; copylen < leadlen; ++copylen)
4743 if (bw->bw_leadstring[copylen]
4744 != bw2->bw_leadstring[copylen])
4745 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004746 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004747 if (bw->bw_addstring == NULL)
4748 addlen = 0;
4749 else
4750 {
4751 addlen = STRLEN(bw->bw_addstring);
4752 if (bw2 != NULL && copylen == leadlen
4753 && bw2->bw_addstring != NULL)
4754 {
4755 for (i = 0; i < addlen; ++i)
4756 if (bw->bw_addstring[i] != bw2->bw_addstring[i])
4757 break;
4758 copylen += i;
4759 }
4760 }
4761
4762 aflags = 0;
4763 /* Only copy bytes when it's more than one, the length itself
4764 * takes an extra byte. */
4765 if (copylen > 1)
4766 aflags |= ADD_COPYLEN;
4767 else
4768 copylen = 0;
4769
4770 if (bw->bw_flags & BWF_ONECAP)
4771 aflags |= ADD_ONECAP;
4772 if (bw->bw_flags & BWF_ALLCAP)
4773 aflags |= ADD_ALLCAP;
4774 if (bw->bw_flags & BWF_KEEPCAP)
4775 aflags |= ADD_KEEPCAP;
4776 if (wif->wif_regionmask != 0 && (bw->bw_region
4777 & wif->wif_regionmask) != wif->wif_regionmask)
4778 aflags |= ADD_REGION;
4779 if (leadlen > 0)
4780 aflags |= ADD_LEADLEN;
4781 putc(aflags, fd); /* <addflags> */
4782
4783 putc(leadlen + addlen, fd); /* <addlen> */
4784 if (aflags & ADD_LEADLEN)
4785 putc(leadlen, fd); /* <leadlen> */
4786 if (aflags & ADD_COPYLEN)
4787 putc(copylen, fd); /* <copylen> */
4788
4789 /* <addstring> */
4790 if (leadlen > copylen && bw->bw_leadstring != NULL)
4791 fwrite(bw->bw_leadstring + copylen,
4792 (size_t)(leadlen - copylen), (size_t)1, fd);
4793 if (leadlen + addlen > copylen && bw->bw_addstring != NULL)
4794 {
4795 if (copylen >= leadlen)
4796 l = copylen - leadlen;
4797 else
4798 l = 0;
4799 fwrite(bw->bw_addstring + l,
4800 (size_t)(addlen - l), (size_t)1, fd);
4801 }
4802
4803 if (aflags & ADD_REGION)
4804 putc(bw->bw_region, fd); /* <region> */
4805
4806 bw2 = bw;
4807 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004808
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004809 vim_free(wtab);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004810 }
4811}
4812
4813
4814/*
4815 * ":mkspell outfile infile ..."
4816 */
4817 void
4818ex_mkspell(eap)
4819 exarg_T *eap;
4820{
4821 int fcount;
4822 char_u **fnames;
4823 char_u fname[MAXPATHL];
4824 char_u wfname[MAXPATHL];
4825 afffile_T *(afile[8]);
4826 hashtab_T dfile[8];
4827 int i;
4828 int len;
4829 char_u region_name[16];
4830 struct stat st;
4831 int round;
4832 vimconv_T conv;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004833 int ascii = FALSE;
4834 char_u *arg = eap->arg;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004835 int error = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004836
Bram Moolenaar5482f332005-04-17 20:18:43 +00004837 if (STRNCMP(arg, "-ascii", 6) == 0)
4838 {
4839 ascii = TRUE;
4840 arg = skipwhite(arg + 6);
4841 }
4842
4843 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
4844 if (get_arglist_exp(arg, &fcount, &fnames) == FAIL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004845 return;
4846 if (fcount < 2)
4847 EMSG(_(e_invarg)); /* need at least output and input names */
4848 else if (fcount > 9)
4849 EMSG(_("E754: Only up to 8 regions supported"));
4850 else
4851 {
4852 /* Check for overwriting before doing things that may take a lot of
4853 * time. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004854 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
Bram Moolenaar5482f332005-04-17 20:18:43 +00004855 ascii ? (char_u *)"ascii" : p_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004856 if (!eap->forceit && mch_stat((char *)wfname, &st) >= 0)
4857 {
4858 EMSG(_(e_exists));
4859 goto theend;
4860 }
4861 if (mch_isdir(fnames[0]))
4862 {
4863 EMSG2(_(e_isadir2), fnames[0]);
4864 goto theend;
4865 }
4866
4867 /*
4868 * Init the aff and dic pointers.
4869 * Get the region names if there are more than 2 arguments.
4870 */
4871 for (i = 1; i < fcount; ++i)
4872 {
4873 afile[i - 1] = NULL;
4874 hash_init(&dfile[i - 1]);
4875 if (fcount > 2)
4876 {
4877 len = STRLEN(fnames[i]);
4878 if (STRLEN(gettail(fnames[i])) < 5 || fnames[i][len - 3] != '_')
4879 {
4880 EMSG2(_("E755: Invalid region in %s"), fnames[i]);
4881 goto theend;
4882 }
4883 else
4884 {
4885 region_name[(i - 1) * 2] = TOLOWER_ASC(fnames[i][len - 2]);
4886 region_name[(i - 1) * 2 + 1] =
4887 TOLOWER_ASC(fnames[i][len - 1]);
4888 }
4889 }
4890 }
4891
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004892 /* Clear the char type tables, don't want to use any of the currently
4893 * used spell properties. */
4894 init_spell_chartab();
4895
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004896 /*
4897 * Read all the .aff and .dic files.
4898 * Text is converted to 'encoding'.
4899 */
4900 for (i = 1; i < fcount; ++i)
4901 {
4902 /* Read the .aff file. Will init "conv" based on the "SET" line. */
4903 conv.vc_type = CONV_NONE;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004904 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", fnames[i]);
Bram Moolenaar5482f332005-04-17 20:18:43 +00004905 if ((afile[i - 1] = spell_read_aff(fname, &conv, ascii)) == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004906 break;
4907
4908 /* Read the .dic file. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004909 vim_snprintf((char *)fname, sizeof(fname), "%s.dic", fnames[i]);
Bram Moolenaar5482f332005-04-17 20:18:43 +00004910 if (spell_read_dic(&dfile[i - 1], fname, &conv, ascii) == FAIL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004911 break;
4912
4913 /* Free any conversion stuff. */
4914 convert_setup(&conv, NULL, NULL);
4915 }
4916
4917 /* Process the data when all the files could be read. */
4918 if (i == fcount)
4919 {
4920 garray_T prefga;
4921 garray_T suffga;
4922 garray_T *gap;
4923 hashtab_T newwords;
4924
4925 /*
4926 * Combine all the affixes into one new affix list. This is done
4927 * for prefixes and suffixes separately.
4928 * We need to do this for each region, try to re-use the same
4929 * affixes.
4930 * Since we number the new affix entries, a growarray will do. In
4931 * the affheader_T the ah_key is unused.
4932 */
4933 MSG(_("Combining affixes..."));
4934 out_flush();
4935 for (round = 1; round <= 2; ++round)
4936 {
4937 gap = round == 1 ? &prefga : &suffga;
4938 ga_init2(gap, sizeof(affheader_T), 50);
4939 for (i = 1; i < fcount; ++i)
4940 get_new_aff(round == 1 ? &afile[i - 1]->af_pref
Bram Moolenaar5482f332005-04-17 20:18:43 +00004941 : &afile[i - 1]->af_suff,
4942 gap, round == 1);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004943 }
4944
4945 /*
4946 * Go over all words and:
4947 * - change the old affix names to the new affix numbers
4948 * - check the conditions
4949 * - fold case
4950 * - extract the basic word and additions.
4951 * Do this for each region.
4952 */
4953 MSG(_("Building word list..."));
4954 out_flush();
4955 hash_init(&newwords);
4956
4957 for (i = 1; i < fcount; ++i)
4958 build_wordlist(&newwords, &dfile[i - 1], afile[i - 1],
4959 1 << (i - 1));
4960
4961 if (fcount > 2)
4962 {
4963 /* Combine words for the different regions into one. */
4964 MSG(_("Combining regions..."));
4965 out_flush();
4966 combine_regions(&newwords);
4967 }
4968
4969 /*
4970 * Affixes on a word with additions are clumsy, would require
4971 * inefficient searching. Turn the affixes into additions and/or
4972 * the expanded word.
4973 */
4974 MSG(_("Processing words..."));
4975 out_flush();
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004976 error = expand_affixes(&newwords, &prefga, &suffga) == FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004977
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004978 if (!error)
4979 {
4980 /* Write the info in the spell file. */
4981 smsg((char_u *)_("Writing spell file %s..."), wfname);
4982 out_flush();
4983 write_vim_spell(wfname, &prefga, &suffga, &newwords,
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004984 fcount - 1, region_name, ascii);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004985 MSG(_("Done!"));
4986 out_flush();
4987 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004988
4989 /* Free the allocated stuff. */
4990 free_wordtable(&newwords);
4991 for (round = 1; round <= 2; ++round)
4992 {
4993 gap = round == 1 ? &prefga: &suffga;
4994 for (i = 0; i < gap->ga_len; ++i)
4995 free_affixentries(((affheader_T *)gap->ga_data + i)
4996 ->ah_first);
4997 ga_clear(gap);
4998 }
4999 }
5000
5001 /* Free the .aff and .dic file structures. */
5002 for (i = 1; i < fcount; ++i)
5003 {
5004 if (afile[i - 1] != NULL)
5005 spell_free_aff(afile[i - 1]);
5006 spell_free_dic(&dfile[i - 1]);
5007 }
5008 }
5009
5010theend:
5011 FreeWild(fcount, fnames);
5012}
5013
5014 static void
5015free_wordtable(ht)
5016 hashtab_T *ht;
5017{
5018 int todo;
5019 basicword_T *bw, *nbw;
5020 hashitem_T *hi;
5021
5022 todo = ht->ht_used;
5023 for (hi = ht->ht_array; todo > 0; ++hi)
5024 {
5025 if (!HASHITEM_EMPTY(hi))
5026 {
5027 --todo;
5028 for (bw = HI2BW(hi); bw != NULL; bw = nbw)
5029 {
5030 nbw = bw->bw_next;
5031 free_basicword(bw);
5032 }
5033 }
5034 }
5035}
5036
5037/*
5038 * Free a basicword_T and what it contains.
5039 */
5040 static void
5041free_basicword(bw)
5042 basicword_T *bw;
5043{
5044 ga_clear(&bw->bw_prefix);
5045 ga_clear(&bw->bw_suffix);
5046 vim_free(bw->bw_caseword);
5047 vim_free(bw->bw_leadstring);
5048 vim_free(bw->bw_addstring);
5049 vim_free(bw);
5050}
5051
5052/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00005053 * Free a list of affentry_T and what they contain.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005054 */
5055 static void
5056free_affixentries(first)
5057 affentry_T *first;
5058{
5059 affentry_T *ap, *an;
5060
5061 for (ap = first; ap != NULL; ap = an)
5062 {
5063 an = ap->ae_next;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005064 free_affix_entry(ap);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005065 }
5066}
5067
Bram Moolenaar5482f332005-04-17 20:18:43 +00005068/*
5069 * Free one affentry_T and what it contains.
5070 */
5071 static void
5072free_affix_entry(ap)
5073 affentry_T *ap;
5074{
5075 vim_free(ap->ae_chop);
5076 vim_free(ap->ae_add);
5077 vim_free(ap->ae_cond);
5078 vim_free(ap->ae_prog);
5079 vim_free(ap);
5080}
5081
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005082#endif /* FEAT_MBYTE */
5083
5084#endif /* FEAT_SYN_HL */