blob: cc71b8d7dabec85b6e792211d5c7278d2408bf6d [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 Moolenaar402d2fe2005-04-15 21:00:38 +0000100 */
101typedef struct slang_S slang_T;
102struct slang_S
103{
104 slang_T *sl_next; /* next language */
105 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
106 hashtab_T sl_words; /* main word table, fword_T */
107 int sl_prefcnt; /* number of prefix NRs */
108 garray_T sl_preftab; /* list of hashtables to lookup prefixes */
109 affitem_T *sl_prefzero; /* list of prefixes with zero add length */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000110 hashtab_T sl_prewords; /* prefixes that include a word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000111 int sl_suffcnt; /* number of suffix NRs */
112 garray_T sl_sufftab; /* list of hashtables to lookup suffixes */
113 affitem_T *sl_suffzero; /* list of suffixes with zero add length */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000114 char_u *sl_try; /* "TRY" from .aff file TODO: not used */
115 garray_T sl_rep; /* list of repentry_T entries from REP lines
116 * TODO not used */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000117 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
118 sblock_T *sl_block; /* list with allocated memory blocks */
119 int sl_error; /* error while loading */
120};
121
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000122/* First language that is loaded, start of the linked list of loaded
123 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000124static slang_T *first_lang = NULL;
125
126/*
127 * Structure to store an addition to a basic word.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000128 * There are many of these, keep it small!
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000129 */
130typedef struct addword_S addword_T;
131struct addword_S
132{
133 addword_T *aw_next; /* next addition */
134 char_u aw_flags; /* ADD_ flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000135 char_u aw_region; /* region for word with this addition */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000136 char_u aw_leadlen; /* byte length of lead in aw_word */
137 char_u aw_wordlen; /* byte length of first word in aw_word */
138 char_u aw_saveb; /* saved byte where aw_word[] is truncated at
139 end of hashtable key; NUL when not using
140 hashtable */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000141 char_u aw_word[1]; /* text, actually longer: case-folded addition
142 plus, with ADD_KEEPCAP: keep-case addition */
143};
144
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000145/* Get addword_T pointer from hashitem that uses aw_word */
146static addword_T dumaw;
147#define HI2ADDWORD(hi) ((addword_T *)((hi)->hi_key - (dumaw.aw_word - (char_u *)&dumaw)))
148
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000149/*
150 * Structure to store a basic word.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000151 * There are many of these, keep it small!
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000152 */
153typedef struct fword_S fword_T;
154struct fword_S
155{
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000156 fword_T *fw_next; /* same basic word with different caps and/or
157 * affixes */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000158 addword_T *fw_adds; /* first addword_T entry */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000159 short_u fw_flags; /* BWF_ flags */
160 char_u fw_region; /* region bits */
161 char_u fw_prefixcnt; /* number of prefix NRs */
162 char_u fw_suffixcnt; /* number of suffix NRs */
163 char_u fw_word[1]; /* actually longer:
164 * 0: case folded word or keep-case word when
165 * (flags & BWF_KEEPCAP)
166 * + word length + 1: list of prefix NRs
167 * + fw_prefixcnt [* 2]: list of suffix NRs
168 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000169};
170
171/* Get fword_T pointer from hashitem that uses fw_word */
172static fword_T dumfw;
173#define HI2FWORD(hi) ((fword_T *)((hi)->hi_key - (dumfw.fw_word - (char_u *)&dumfw)))
174
175#define REGION_ALL 0xff
176
177
178/*
179 * Structure used in "b_langp", filled from 'spelllang'.
180 */
181typedef struct langp_S
182{
183 slang_T *lp_slang; /* info for this language (NULL for last one) */
184 int lp_region; /* bitmask for region or REGION_ALL */
185} langp_T;
186
187#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
188
189#define SP_OK 0
190#define SP_BAD 1
191#define SP_RARE 2
192#define SP_LOCAL 3
193
194/* flags used for basic words in the spell file */
195#define BWF_VALID 0x01 /* word is valid without additions */
196#define BWF_REGION 0x02 /* region byte follows */
197#define BWF_ONECAP 0x04 /* first letter must be capital */
198#define BWF_SUFFIX 0x08 /* has suffix NR list */
199#define BWF_SECOND 0x10 /* second flags byte follows */
200
201#define BWF_ADDS 0x0100 /* there are additions */
202#define BWF_PREFIX 0x0200 /* has prefix NR list */
203#define BWF_ALLCAP 0x0400 /* all letters must be capital (not used
204 for single-letter words) */
205#define BWF_KEEPCAP 0x0800 /* Keep case as-is */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000206#define BWF_ADDS_M 0x1000 /* there are more than 255 additions */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000207
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000208#define BWF_ADDHASH 0x8000 /* Internal: use hashtab for additions */
209
210#define NOWC_KEY (char_u *)"x" /* hashtab key used for additions without
211 any word character */
212
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000213/* flags used for addition in the spell file */
214#define ADD_REGION 0x02 /* region byte follows */
215#define ADD_ONECAP 0x04 /* first letter must be capital */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000216#define ADD_LEADLEN 0x10 /* there is a leadlen byte */
217#define ADD_COPYLEN 0x20 /* there is a copylen byte */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000218#define ADD_ALLCAP 0x40 /* all letters must be capital (not used
219 for single-letter words) */
220#define ADD_KEEPCAP 0x80 /* fixed case */
221
222/* Translate ADD_ flags to BWF_ flags.
223 * (Needed to keep ADD_ flags in one byte.) */
224#define ADD2BWF(x) (((x) & 0x0f) | (((x) & 0xf0) << 4))
225
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000226#define VIMSPELLMAGIC "VIMspell03" /* string at start of Vim spell file */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000227#define VIMSPELLMAGICL 10
228
229/*
230 * Structure to store info for word matching.
231 */
232typedef struct matchinf_S
233{
234 langp_T *mi_lp; /* info for language and region */
235 slang_T *mi_slang; /* info for the language */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000236
237 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000238 char_u *mi_line; /* start of line containing word */
239 char_u *mi_word; /* start of word being checked */
240 char_u *mi_end; /* first non-word char after mi_word */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000241 char_u *mi_wend; /* end of matching word (is mi_end
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000242 * or further) */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000243 char_u *mi_fend; /* next char to be added to mi_fword */
244
245 /* case-folded text */
246 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
247 int mi_fendlen; /* byte length of first word in
248 mi_fword */
249 int mi_faddlen; /* byte length of text in mi_fword
250 after first word */
251 char_u *mi_cword; /* word to check, points in mi_fword */
252 char_u *mi_awend; /* after next word, to check for
253 addition (NULL when not done yet) */
254 int mi_did_awend; /* did compute mi_awend */
255
256 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000257 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
258 int mi_capflags; /* BWF_ONECAP BWF_ALLCAP BWF_KEEPCAP */
259} matchinf_T;
260
261static int word_match __ARGS((matchinf_T *mip));
262static int check_adds __ARGS((matchinf_T *mip, fword_T *fw, int req_pref, int req_suf));
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000263static void fill_awend __ARGS((matchinf_T *mip));
264static void fold_addchars __ARGS((matchinf_T *mip, int addlen));
265static int supports_affix __ARGS((int cnt, char_u *afflist, int afflistlen, int nr));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000266static int prefix_match __ARGS((matchinf_T *mip));
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000267static int noprefix_match __ARGS((matchinf_T *mip, char_u *pword, char_u *cstart, affitem_T *ai));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000268static int suffix_match __ARGS((matchinf_T *mip));
269static int match_caps __ARGS((int flags, char_u *caseword, matchinf_T *mip, char_u *cword, char_u *end));
270static slang_T *slang_alloc __ARGS((char_u *lang));
271static void slang_free __ARGS((slang_T *lp));
272static slang_T *spell_load_lang __ARGS((char_u *lang));
273static void spell_load_file __ARGS((char_u *fname, void *cookie));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000274static void *getroom __ARGS((slang_T *lp, int *bl_used, int len));
275static int find_region __ARGS((char_u *rp, char_u *region));
276static int captype __ARGS((char_u *word, char_u *end));
277
278/*
279 * Main spell-checking function.
280 * "ptr" points to the start of a word.
281 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
282 * or when it's OK it remains unchanged.
283 * This must only be called when 'spelllang' is not empty.
284 * Returns the length of the word in bytes, also when it's OK, so that the
285 * caller can skip over the word.
286 */
287 int
288spell_check(wp, line, ptr, attrp)
289 win_T *wp; /* current window */
290 char_u *line; /* start of line where "ptr" points into */
291 char_u *ptr;
292 int *attrp;
293{
294 matchinf_T mi; /* Most things are put in "mi" so that it can
295 be passed to functions quickly. */
296
297 /* Find the end of the word. We already know that *ptr is a word char. */
298 mi.mi_word = ptr;
299 mi.mi_end = ptr;
300 do
301 {
302 mb_ptr_adv(mi.mi_end);
303 } while (*mi.mi_end != NUL && spell_iswordc(mi.mi_end));
304
305 /* A word starting with a number is always OK. */
306 if (*ptr >= '0' && *ptr <= '9')
307 return (int)(mi.mi_end - ptr);
308
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000309 /* Make case-folded copy of the word. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000310 (void)str_foldcase(ptr, mi.mi_end - ptr, mi.mi_fword, MAXWLEN + 1);
311 mi.mi_cword = mi.mi_fword;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000312 mi.mi_fendlen = STRLEN(mi.mi_fword);
313 mi.mi_faddlen = 0;
314 mi.mi_fend = mi.mi_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000315
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000316 /* Check the caps type of the word. */
317 mi.mi_capflags = captype(ptr, mi.mi_end);
318
319 /* The word is bad unless we recognize it. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000320 mi.mi_result = SP_BAD;
321 mi.mi_wend = mi.mi_end;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000322
323 mi.mi_awend = NULL;
324 mi.mi_did_awend = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000325 mi.mi_line = line;
326
327 /*
328 * Loop over the languages specified in 'spelllang'.
329 * We check them all, because a matching word may have additions that are
330 * longer than an already found matching word.
331 */
332 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
333 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
334 {
335 /*
336 * Check for a matching word.
337 * If not found or wrong region try removing prefixes (and then
338 * suffixes).
339 * If still not found or wrong region try removing suffixes.
340 */
341 mi.mi_slang = mi.mi_lp->lp_slang;
342 if (!word_match(&mi) || mi.mi_result != SP_OK)
343 if (!prefix_match(&mi) || mi.mi_result != SP_OK)
344 suffix_match(&mi);
345 }
346
347 if (mi.mi_result != SP_OK)
348 {
349 if (mi.mi_result == SP_BAD)
350 *attrp = highlight_attr[HLF_SPB];
351 else if (mi.mi_result == SP_RARE)
352 *attrp = highlight_attr[HLF_SPR];
353 else
354 *attrp = highlight_attr[HLF_SPL];
355 }
356
357 return (int)(mi.mi_wend - ptr);
358}
359
360/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000361 * Check if the word "mip->mi_word" matches.
362 * "mip->mi_fword" is the same word case-folded;
363 *
364 * This checks the word as a whole and for prefixes that include a word.
365 *
366 * Note that when called mi_fword only contains the word up to mip->mi_end,
367 * but when checking additions it gets longer.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000368 */
369 static int
370word_match(mip)
371 matchinf_T *mip;
372{
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000373 hash_T fhash = hash_hash(mip->mi_fword);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000374 hashitem_T *hi;
375 fword_T *fw;
376 int valid = FALSE;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000377 char_u *p;
378 char_u pword[MAXWLEN + 1];
379 int charlen;
380 int capflags_save;
381 affitem_T *ai;
382 char_u *cstart;
383 int addlen;
384 int n;
385 char_u *save_end;
386 int cc;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000387
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000388 hi = hash_lookup(&mip->mi_slang->sl_words, mip->mi_fword, fhash);
389 if (!HASHITEM_EMPTY(hi))
390 {
391 /*
392 * Find a basic word for which the case of "mi_word" is correct.
393 * If it is, check additions and use the longest one.
394 */
395 for (fw = HI2FWORD(hi); fw != NULL; fw = fw->fw_next)
396 if (match_caps(fw->fw_flags, fw->fw_word, mip,
397 mip->mi_word, mip->mi_end))
398 valid |= check_adds(mip, fw, -1, -1);
399 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000400
401 /*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000402 * Try finding a matching preword for "mip->mi_word". These are
403 * prefixes that have a non-word character after a word character:
404 * "d'", "de-", "'s-", "l'de-". But not "'s".
405 * Also need to do this when a matching word was already found, because we
406 * might find a longer match this way (French: "qu" and "qu'a-t-elle").
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000407 */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000408 cc = mip->mi_fword[mip->mi_fendlen];
409 mip->mi_fword[mip->mi_fendlen] = NUL;
410 hi = hash_lookup(&mip->mi_slang->sl_prewords, mip->mi_fword, fhash);
411 mip->mi_fword[mip->mi_fendlen] = cc;
412 if (!HASHITEM_EMPTY(hi))
413 {
414 capflags_save = mip->mi_capflags;
415
416 /* Go through the list of matching prewords. */
417 for (ai = HI2AI(hi); ai != NULL; ai = ai->ai_next)
418 {
419 /* Check that the lead string matches before the word. */
420 p = ai->ai_add + ai->ai_addlen + ai->ai_choplen + 2;
421 if (ai->ai_leadlen > 0)
422 {
423 if (mip->mi_word - mip->mi_line < ai->ai_leadlen
424 || STRNCMP(mip->mi_word - ai->ai_leadlen, p,
425 ai->ai_leadlen) != 0)
426 continue;
427 p += ai->ai_leadlen + 1; /* advance "p" to tail */
428 }
429 else
430 ++p; /* advance "p" to tail */
431
432 /* Check that the tail string matches after the word. Need
433 * to fold case first. */
434 if (ai->ai_taillen > 0)
435 {
436 if (ai->ai_taillen >= mip->mi_faddlen)
437 {
438 fold_addchars(mip, ai->ai_taillen);
439 if (ai->ai_taillen > mip->mi_faddlen)
440 continue; /* not enough chars, can't match */
441 }
442 if (STRNCMP(mip->mi_fword + mip->mi_fendlen,
443 p, ai->ai_taillen) != 0)
444 continue;
445 }
446
447 /*
448 * This preword matches. Remove the preword and check that
449 * the resulting word exits.
450 */
451
452 /* Find the place in the original word where the tail ends,
453 * needed for case checks. */
454#ifdef FEAT_MBYTE
455 charlen = mb_charlen(p);
456#else
457 charlen = ai->ai_taillen;
458#endif
459 cstart = mip->mi_end;
460 for (n = 0; n < charlen; ++n)
461 mb_ptr_adv(cstart);
462
463 /* The new word starts with the chop. Then add up to the next
464 * non-word char. */
465 mch_memmove(pword, ai->ai_add + ai->ai_addlen + 1,
466 ai->ai_choplen);
467 p = mip->mi_fword + mip->mi_fendlen + ai->ai_taillen;
468 addlen = ai->ai_taillen;
469 while (spell_iswordc(p))
470 {
471 ++charlen;
472#ifdef FEAT_MBYTE
473 addlen += (*mb_ptr2len_check)(p);
474#else
475 ++addlen;
476#endif
477 mb_ptr_adv(p);
478 if (addlen >= mip->mi_faddlen)
479 {
480 /* Get more folded characters in mip->mi_fword. */
481 fold_addchars(mip, addlen);
482 if (addlen >= mip->mi_faddlen)
483 break; /* not enough chars, can't match */
484 }
485 }
486 mch_memmove(pword + ai->ai_choplen,
487 mip->mi_fword + mip->mi_fendlen + ai->ai_taillen,
488 addlen - ai->ai_taillen);
489 pword[ai->ai_choplen + addlen - ai->ai_taillen] = NUL;
490
491 /* Need to set mi_end to find additions. Also set mi_fendlen
492 * and mi_faddlen. */
493 save_end = mip->mi_end;
494 while (--charlen >= 0)
495 mb_ptr_adv(mip->mi_end);
496 mip->mi_fendlen += addlen;
497 mip->mi_faddlen -= addlen;
498
499 /* Find the word "pword", caseword "cstart". */
500 n = noprefix_match(mip, pword, cstart, ai);
501 mip->mi_end = save_end;
502 mip->mi_fendlen -= addlen;
503 mip->mi_faddlen += addlen;
504 if (n)
505 valid = TRUE;
506
507 /* If we found a valid word, we still need to try other
508 * suffixes, because it may have an addition that's longer. */
509 }
510
511 mip->mi_capflags = capflags_save;
512 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000513
514 return valid;
515}
516
517/*
518 * Check a matching basic word for additions.
519 * Return TRUE if we have a valid match.
520 */
521 static int
522check_adds(mip, fw, req_pref, req_suf)
523 matchinf_T *mip;
524 fword_T *fw;
525 int req_pref; /* required prefix nr, -1 if none */
526 int req_suf; /* required suffix nr, -1 if none */
527{
528 int valid = FALSE;
529 addword_T *aw;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000530 addword_T *naw = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000531 char_u *p;
532 int addlen;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000533 int cc;
534 hashitem_T *hi;
535 char_u *cp = NULL;
536 int n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000537
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000538 /* Check if required prefixes and suffixes are supported. These are on
539 * the basic word, not on each addition. */
540 if (req_pref >= 0 || req_suf >= 0)
541 {
542 /* Prefix NRs are stored just after the word in fw_word. */
543 cp = fw->fw_word + STRLEN(fw->fw_word) + 1;
544 if (req_pref >= 0 && !supports_affix(mip->mi_slang->sl_prefcnt,
545 cp, fw->fw_prefixcnt, req_pref))
546 return FALSE;
547 if (req_suf >= 0)
548 {
549 /* Suffix NRs are stored just after the Prefix NRs. */
550 if (fw->fw_prefixcnt > 0)
551 {
552 if (mip->mi_slang->sl_prefcnt > 256)
553 cp += fw->fw_prefixcnt * 2;
554 else
555 cp += fw->fw_prefixcnt;
556 }
557 if (!supports_affix(mip->mi_slang->sl_suffcnt,
558 cp, fw->fw_suffixcnt, req_suf))
559 return FALSE;
560 }
561 }
562
563 /* A word may be valid without an addition. */
564 if (fw->fw_flags & BWF_VALID)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000565 {
566 valid = TRUE;
567 if (mip->mi_result != SP_OK)
568 {
569 if ((fw->fw_region & mip->mi_lp->lp_region) == 0)
570 mip->mi_result = SP_LOCAL;
571 else
572 mip->mi_result = SP_OK;
573 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000574 /* Set word end, required when matching a word after a preword. */
575 if (mip->mi_wend < mip->mi_end)
576 mip->mi_wend = mip->mi_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000577 }
578
579 /*
580 * Check additions, both before and after the word.
581 * This may make the word longer, thus we also need to check
582 * when we already found a matching word.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000583 * When the BWF_ADDHASH flag is present then fw_adds points to a hashtable
584 * for quick lookup. Otherwise it points to the list of all possible
585 * additions.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000586 */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000587 if (fw->fw_flags & BWF_ADDHASH)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000588 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000589 /* Locate the text up to the next end-of-word. */
590 if (!mip->mi_did_awend)
591 fill_awend(mip);
592 if (mip->mi_awend == NULL)
593 return valid; /* there is no next word */
594
595 cc = *mip->mi_awend;
596 *mip->mi_awend = NUL;
597 hi = hash_find((hashtab_T *)fw->fw_adds,
598 mip->mi_fword + mip->mi_fendlen);
599 *mip->mi_awend = cc;
600 if (HASHITEM_EMPTY(hi))
601 return valid; /* no matching addition */
602 aw = HI2ADDWORD(hi);
603
604 /* Also check additions without word characters. If they are there,
605 * skip the first dummy entry. */
606 hi = hash_find((hashtab_T *)fw->fw_adds, NOWC_KEY);
607 if (!HASHITEM_EMPTY(hi))
608 naw = HI2ADDWORD(hi)->aw_next;
609 }
610 else
611 aw = fw->fw_adds;
612
613 for ( ; ; aw = aw->aw_next)
614 {
615 if (aw == NULL)
616 {
617 /* At end of list: may also try additions without word chars. */
618 if (naw == NULL)
619 break;
620 aw = naw;
621 naw = NULL;
622 }
623
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000624 if (aw->aw_leadlen > 0)
625 {
626 /* There is a leader, verify that it matches. */
627 if (aw->aw_leadlen > mip->mi_word - mip->mi_line
628 || STRNCMP(mip->mi_word - aw->aw_leadlen,
629 aw->aw_word, aw->aw_leadlen) != 0)
630 continue;
631 if (mip->mi_word - aw->aw_leadlen > mip->mi_line)
632 {
633 /* There must not be a word character just before the
634 * leader. */
635 p = mip->mi_word - aw->aw_leadlen;
636 mb_ptr_back(mip->mi_line, p);
637 if (spell_iswordc(p))
638 continue;
639 }
640 /* Leader matches. Addition is rest of "aw_word". */
641 p = aw->aw_word + aw->aw_leadlen;
642 }
643 else
644 /* No leader, use whole of "aw_word" for addition. */
645 p = aw->aw_word;
646
647 addlen = aw->aw_wordlen - aw->aw_leadlen;
648 if (addlen > 0)
649 {
650 /* Check for matching addition and no word character after it.
651 * First make sure we have enough case-folded chars to compare
652 * with. */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000653 if (addlen >= mip->mi_faddlen)
654 fold_addchars(mip, addlen);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000655
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000656 /* Put back the saved char, if needed. */
657 if (aw->aw_saveb != NUL)
658 {
659 cp = p + STRLEN(p);
660 *cp = aw->aw_saveb;
661 }
662 n = STRNCMP(mip->mi_fword + mip->mi_fendlen, p, addlen);
663 if (aw->aw_saveb != NUL)
664 *cp = NUL;
665
666 if (n != 0 || (mip->mi_fword[mip->mi_fendlen + addlen] != NUL
667 && spell_iswordc(mip->mi_fword + mip->mi_fendlen + addlen)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000668 continue;
669
670 /* Compute the length in the original word, before case folding. */
671#ifdef FEAT_MBYTE
672 if (has_mbyte)
673 {
674 int l;
675
676 p = mip->mi_end;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000677 for (l = 0; l < addlen; l += (*mb_ptr2len_check)(mip->mi_fword
678 + mip->mi_fendlen + l))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000679 mb_ptr_adv(p);
680 addlen = p - mip->mi_end;
681 }
682#endif
683
684 /* Check case of the addition. */
685 if (!match_caps(ADD2BWF(aw->aw_flags),
686 aw->aw_word + aw->aw_wordlen + 1, mip,
687 mip->mi_end, mip->mi_end + addlen))
688 continue;
689 }
690
691 /* Match! Use the new length if it's longer. */
692 if (mip->mi_wend < mip->mi_end + addlen)
693 mip->mi_wend = mip->mi_end + addlen;
694
695 valid = TRUE;
696 if (mip->mi_result != SP_OK)
697 {
698 if ((aw->aw_region & mip->mi_lp->lp_region) == 0)
699 mip->mi_result = SP_LOCAL;
700 else
701 mip->mi_result = SP_OK;
702 }
703 }
704
705 return valid;
706}
707
708/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000709 * Locate the text up to the next end-of-word after mip->mi_end.
710 */
711 static void
712fill_awend(mip)
713 matchinf_T *mip;
714{
715 char_u *p = mip->mi_end;
716 int addlen = 0;
717 int find_word = TRUE;
718
719 mip->mi_did_awend = TRUE;
720 if (mip->mi_faddlen == 0)
721 fold_addchars(mip, 0); /* need to fold first char */
722
723 /* 1: find_word == TRUE: skip over non-word characters after mi_end.
724 * 2: find_word == FALSE: skip over following word characters. */
725 for (p = mip->mi_fword + mip->mi_fendlen; *p != NUL; mb_ptr_adv(p))
726 {
727 if (spell_iswordc(p) == find_word)
728 {
729 if (!find_word)
730 break; /* done */
731 find_word = !find_word;
732 }
733#ifdef FEAT_MBYTE
734 addlen += (*mb_ptr2len_check)(p);
735#else
736 ++addlen;
737#endif
738 if (addlen >= mip->mi_faddlen)
739 fold_addchars(mip, addlen); /* need to fold more chars */
740 }
741
742 /* If there are extra chars store the result. */
743 if (addlen != 0)
744 mip->mi_awend = p;
745}
746
747/*
748 * Fold enough characters of the checked text to be able to compare with an
749 * addition of length "addlen" plus one character (to be able to check the
750 * next character to be a non-word char).
751 * When there are not enough characters (end of line) mip->mi_faddlen will be
752 * smaller than "addlen".
753 */
754 static void
755fold_addchars(mip, addlen)
756 matchinf_T *mip;
757 int addlen;
758{
759 int l;
760 char_u *p = mip->mi_fword + mip->mi_fendlen;
761
762 while (mip->mi_faddlen <= addlen)
763 {
764 if (*mip->mi_fend == NUL) /* end of the line */
765 {
766 p[mip->mi_faddlen] = NUL;
767 break;
768 }
769#ifdef FEAT_MBYTE
770 if (has_mbyte)
771 l = (*mb_ptr2len_check)(mip->mi_fend);
772 else
773#endif
774 l = 1;
775 (void)str_foldcase(mip->mi_fend, l, p + mip->mi_faddlen,
776 MAXWLEN - mip->mi_fendlen - mip->mi_faddlen);
777 mip->mi_fend += l;
778 mip->mi_faddlen += STRLEN(p + mip->mi_faddlen);
779 }
780}
781
782/*
783 * Return TRUE if affix "nr" appears in affix list "afflist[afflistlen]".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000784 */
785 static int
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000786supports_affix(cnt, afflist, afflistlen, nr)
787 int cnt; /* total affix NR count */
788 char_u *afflist;
789 int afflistlen; /* affix count in "afflist" */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000790 int nr;
791{
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000792 char_u *pc = afflist;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000793 int i;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000794 int nr_msb, nr_lsb;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000795
796 if (cnt <= 256)
797 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000798 /* one byte affix numbers */
799 for (i = afflistlen; --i >= 0; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000800 if (*pc++ == nr)
801 return TRUE;
802 }
803 else
804 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000805 /* two byte affix numbers, MSB first */
806 nr_msb = (unsigned)nr >> 8;
807 nr_lsb = nr & 0xff;
808 for (i = afflistlen; --i >= 0; )
809 {
810 if (*pc++ == nr_msb && *pc == nr_lsb)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000811 return TRUE;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000812 ++pc;
813 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000814 }
815 return FALSE;
816}
817
818/*
819 * Try finding a match for "mip->mi_cword" by removing prefixes.
820 */
821 static int
822prefix_match(mip)
823 matchinf_T *mip;
824{
825 int len = 0;
826 int charlen = 0;
827 int cc;
828 affitem_T *ai;
829 char_u pword[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000830 hashtab_T *ht;
831 hashitem_T *hi;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000832 int found_valid = FALSE;
833 int cstart_charlen = 0;
834 char_u *cstart = mip->mi_word;
835 int capflags_save = mip->mi_capflags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000836
837 /*
838 * Check for prefixes with different character lengths.
839 * Start with zero length (only chop off).
840 */
841 for (charlen = 0; charlen <= mip->mi_slang->sl_preftab.ga_len; ++charlen)
842 {
843 if (charlen > 0)
844 {
845#ifdef FEAT_MBYTE
846 if (has_mbyte)
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000847 len += (*mb_ptr2len_check)(mip->mi_cword + len);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000848 else
849#endif
850 len += 1;
851 }
852 if (mip->mi_cword[len] == NUL) /* end of word, no prefix possible */
853 break;
854
855 if (charlen == 0)
856 ai = mip->mi_slang->sl_prefzero;
857 else
858 {
859 /* Get pointer to hashtab for prefix of this many chars. */
860 ht = ((hashtab_T *)mip->mi_slang->sl_preftab.ga_data) + charlen - 1;
861 if (ht->ht_used == 0)
862 continue;
863
864 cc = mip->mi_cword[len];
865 mip->mi_cword[len] = NUL;
866 hi = hash_find(ht, mip->mi_cword);
867 mip->mi_cword[len] = cc;
868
869 if (HASHITEM_EMPTY(hi))
870 ai = NULL;
871 else
872 ai = HI2AI(hi);
873 }
874
875 /* Loop over all matching prefixes. */
876 for ( ; ai != NULL; ai = ai->ai_next)
877 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000878 /* Create the basic word from the chop string and the word after
879 * the matching add string. */
880 mch_memmove(pword, ai->ai_add + ai->ai_addlen + 1, ai->ai_choplen);
881 mch_memmove(pword + ai->ai_choplen, mip->mi_cword + ai->ai_addlen,
882 mip->mi_fendlen - ai->ai_addlen);
883 pword[mip->mi_fendlen - ai->ai_addlen] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000884
885 /* Adjust the word start for case checks, we only check the
886 * part after the prefix. */
887 while (cstart_charlen < charlen)
888 {
889 mb_ptr_adv(cstart);
890 ++cstart_charlen;
891 }
892
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000893 /* Find the word "pword", caseword "cstart". */
894 found_valid |= noprefix_match(mip, pword, cstart, ai);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000895
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000896 if (found_valid && mip->mi_result == SP_OK)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000897 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000898 /* Found a valid word, no need to try other suffixes. */
899 mip->mi_capflags = capflags_save;
900 return TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000901 }
902 }
903 }
904
905 mip->mi_capflags = capflags_save;
906 return FALSE;
907}
908
909/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000910 * Check for matching word after removing a prefix.
911 * Return TRUE if found.
912 */
913 static int
914noprefix_match(mip, pword, cstart, ai)
915 matchinf_T *mip;
916 char_u *pword; /* case-folded word */
917 char_u *cstart; /* original word after removed prefix */
918 affitem_T *ai; /* the prefix item */
919{
920 hashitem_T *hi;
921 fword_T *fw;
922 int found_valid = FALSE;
923 char_u *word;
924 int i;
925 int fendlen;
926
927 /* Removing the prefix may change the caps, e.g. for
928 * "deAlf" removing "de" makes it ONECAP. */
929 mip->mi_capflags = captype(cstart, mip->mi_end);
930
931 /* Find the basic word. */
932 hi = hash_find(&mip->mi_slang->sl_words, pword);
933 if (!HASHITEM_EMPTY(hi))
934 {
935 /* Check if the word supports this prefix. */
936 for (fw = HI2FWORD(hi); fw != NULL; fw = fw->fw_next)
937 if (match_caps(fw->fw_flags, fw->fw_word, mip,
938 cstart, mip->mi_end))
939 found_valid |= check_adds(mip, fw, ai->ai_nr, -1);
940
941 if (found_valid && mip->mi_result == SP_OK)
942 /* Found a valid word, no need to try other suffixes. */
943 return TRUE;
944 }
945
946 /* No matching basic word without prefix. When combining is
947 * allowed try with suffixes. */
948 if (ai->ai_flags & AFF_COMBINE)
949 {
950 /* Pass the word with prefix removed to suffix_match(). */
951 mip->mi_cword = pword;
952 word = mip->mi_word;
953 mip->mi_word = cstart;
954 fendlen = mip->mi_fendlen;
955 mip->mi_fendlen = STRLEN(pword);
956 i = suffix_match(mip);
957 mip->mi_cword = mip->mi_fword;
958 mip->mi_word = word;
959 mip->mi_fendlen = fendlen;
960 if (i)
961 return TRUE;
962 }
963
964 return FALSE;
965}
966
967/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000968 * Try finding a match for "mip->mi_cword" by removing suffixes.
969 */
970 static int
971suffix_match(mip)
972 matchinf_T *mip;
973{
974 char_u *sufp;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000975 char_u *endw = mip->mi_cword + mip->mi_fendlen;
976 int endw_c = *endw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000977 int charlen;
978 affitem_T *ai;
979 char_u pword[MAXWLEN + 1];
980 fword_T *fw;
981 hashtab_T *ht;
982 hashitem_T *hi;
983 int tlen;
984 int cend_charlen = 0;
985 char_u *cend = mip->mi_end;
986 int found_valid = FALSE;
987 int capflags_save = mip->mi_capflags;
988
989 /*
990 * Try suffixes of different length, starting with an empty suffix (chop
991 * only, thus adds something).
992 * Stop checking if there are no suffixes with so many characters.
993 */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000994 sufp = endw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000995 for (charlen = 0; charlen <= mip->mi_slang->sl_sufftab.ga_len; ++charlen)
996 {
997 /* Move the pointer to the possible suffix back one character, unless
998 * doing the first round (empty suffix). */
999 if (charlen > 0)
1000 {
1001 mb_ptr_back(mip->mi_cword, sufp);
1002 if (sufp <= mip->mi_cword) /* start of word, no suffix possible */
1003 break;
1004 }
1005
1006 if (charlen == 0)
1007 ai = mip->mi_slang->sl_suffzero;
1008 else
1009 {
1010 /* Get pointer to hashtab for suffix of this many chars. */
1011 ht = ((hashtab_T *)mip->mi_slang->sl_sufftab.ga_data) + charlen - 1;
1012 if (ht->ht_used == 0)
1013 continue;
1014
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001015 *endw = NUL; /* truncate after possible suffix */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001016 hi = hash_find(ht, sufp);
1017 if (HASHITEM_EMPTY(hi))
1018 ai = NULL;
1019 else
1020 ai = HI2AI(hi);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001021 *endw = endw_c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001022 }
1023
1024 if (ai != NULL)
1025 {
1026 /* Found a list of matching suffixes. Now check that there is one
1027 * we can use. */
1028 tlen = sufp - mip->mi_cword; /* length of word without suffix */
1029 mch_memmove(pword, mip->mi_cword, tlen);
1030
1031 for ( ; ai != NULL; ai = ai->ai_next)
1032 {
1033 /* Found a matching suffix. Create the basic word by removing
1034 * the suffix and adding the chop string. */
1035 if (ai->ai_choplen == 0)
1036 pword[tlen] = NUL;
1037 else
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001038 mch_memmove(pword + tlen, ai->ai_add + ai->ai_addlen + 1,
1039 ai->ai_choplen + 1);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001040
1041 /* Find the basic word. */
1042 hi = hash_find(&mip->mi_slang->sl_words, pword);
1043 if (!HASHITEM_EMPTY(hi))
1044 {
1045 /* Adjust the end for case checks, we only check the part
1046 * before the suffix. */
1047 while (cend_charlen < charlen)
1048 {
1049 mb_ptr_back(mip->mi_word, cend);
1050 ++cend_charlen;
1051 }
1052
1053 /* Removing the suffix may change the caps, e.g. for
1054 * "UFOs" removing 's' makes it ALLCAP. */
1055 mip->mi_capflags = captype(mip->mi_word, cend);
1056
1057 /* Check if the word supports this suffix. */
1058 for (fw = HI2FWORD(hi); fw != NULL; fw = fw->fw_next)
1059 if (match_caps(fw->fw_flags, fw->fw_word, mip,
1060 mip->mi_word, cend))
1061 found_valid |= check_adds(mip, fw, -1, ai->ai_nr);
1062
1063 if (found_valid && mip->mi_result == SP_OK)
1064 {
1065 /* Found a valid word, no need to try other suffixes. */
1066 mip->mi_capflags = capflags_save;
1067 return TRUE;
1068 }
1069 }
1070 }
1071 }
1072 }
1073
1074 mip->mi_capflags = capflags_save;
1075 return FALSE;
1076}
1077
1078/*
1079 * Return TRUE if case of "cword" meets the requirements of case flags
1080 * "flags".
1081 */
1082 static int
1083match_caps(flags, caseword, mip, cword, end)
1084 int flags; /* flags required by basic word or addition */
1085 char_u *caseword; /* word with case as required */
1086 matchinf_T *mip;
1087 char_u *cword; /* word to compare against "caseword" */
1088 char_u *end; /* end of "cword" */
1089{
1090 char_u *p;
1091 int c;
1092 int len;
1093 int capflags = mip->mi_capflags; /* flags of checked word */
1094 int past_second;
1095
1096 if ((capflags & BWF_KEEPCAP) == 0 && end > mip->mi_end)
1097 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001098 /* If "end" is past "mip->mi_end" we need to adjust the caps type for
1099 * characters after the basic word. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001100#ifdef FEAT_MBYTE
1101 past_second = (mip->mi_word + (*mb_ptr2len_check)(mip->mi_word)
1102 < mip->mi_end);
1103#else
1104 past_second = mip->mi_word + 1 < mip->mi_end;
1105#endif
1106 for (p = mip->mi_end; p < end; )
1107 {
1108 if (!spell_iswordc(p))
1109 mb_ptr_adv(p);
1110 else
1111 {
1112#ifdef FEAT_MBYTE
1113 if (has_mbyte)
1114 c = mb_ptr2char_adv(&p);
1115 else
1116#endif
1117 c = *p++;
1118 if (MB_ISUPPER(c))
1119 {
1120 if (capflags == 0 || (capflags & BWF_ONECAP))
1121 {
1122 capflags = BWF_KEEPCAP; /* lU or UlU */
1123 break;
1124 }
1125 }
1126 else
1127 {
1128 if (capflags & BWF_ALLCAP)
1129 {
1130 if (past_second)
1131 {
1132 capflags = BWF_KEEPCAP; /* UUl */
1133 break;
1134 }
1135 capflags = BWF_ONECAP; /* Uu */
1136 }
1137 }
1138 past_second = TRUE;
1139 }
1140 }
1141 }
1142
1143 if (capflags == BWF_ALLCAP)
1144 return TRUE; /* All caps is always OK. */
1145
1146 if (flags & BWF_KEEPCAP)
1147 {
1148 len = STRLEN(caseword);
1149 return (len == end - cword && STRNCMP(caseword, cword, len) == 0);
1150 }
1151
1152 if (flags & BWF_ALLCAP)
1153 return FALSE; /* need ALLCAP, already checked above */
1154
1155 if (flags & BWF_ONECAP)
1156 return capflags == BWF_ONECAP;
1157
1158 return capflags != BWF_KEEPCAP; /* no case check, only KEEPCAP is bad */
1159}
1160
1161/*
1162 * Move to next spell error.
1163 * Return OK if found, FAIL otherwise.
1164 */
1165 int
1166spell_move_to(dir, allwords)
1167 int dir; /* FORWARD or BACKWARD */
1168 int allwords; /* TRUE for "[s" and "]s" */
1169{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001170 linenr_T lnum;
1171 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001172 char_u *line;
1173 char_u *p;
1174 int wc;
1175 int nwc;
1176 int attr = 0;
1177 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001178 int has_syntax = syntax_present(curbuf);
1179 int col;
1180 int can_spell;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001181
1182 if (!curwin->w_p_spell || *curwin->w_buffer->b_p_spl == NUL)
1183 {
1184 EMSG(_("E756: Spell checking not enabled"));
1185 return FAIL;
1186 }
1187
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001188 /*
1189 * Start looking for bad word at the start of the line, because we can't
1190 * start halfway a word, we don't know where it starts or ends.
1191 *
1192 * When searching backwards, we continue in the line to find the last
1193 * bad word (in the cursor line: before the cursor).
1194 */
1195 lnum = curwin->w_cursor.lnum;
1196 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001197
1198 while (!got_int)
1199 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001200 line = ml_get(lnum);
1201 p = line;
1202 wc = FALSE;
1203
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001204 while (*p != NUL)
1205 {
1206 nwc = spell_iswordc(p);
1207 if (!wc && nwc)
1208 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001209 /* When searching backward don't search after the cursor. */
1210 if (dir == BACKWARD
1211 && lnum == curwin->w_cursor.lnum
1212 && (colnr_T)(p - line) >= curwin->w_cursor.col)
1213 break;
1214
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001215 /* start of word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001216 len = spell_check(curwin, line, p, &attr);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001217
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001218 if (attr != 0)
1219 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001220 /* We found a bad word. Check the attribute. */
1221 /* TODO: check for syntax @Spell cluster. */
1222 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001223 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001224 /* When searching forward only accept a bad word after
1225 * the cursor. */
1226 if (dir == BACKWARD
1227 || lnum > curwin->w_cursor.lnum
1228 || (lnum == curwin->w_cursor.lnum
1229 && (colnr_T)(p - line)
1230 > curwin->w_cursor.col))
1231 {
1232 if (has_syntax)
1233 {
1234 col = p - line;
1235 (void)syn_get_id(lnum, (colnr_T)col,
1236 FALSE, &can_spell);
1237
1238 /* have to get the line again, a multi-line
1239 * regexp may make it invalid */
1240 line = ml_get(lnum);
1241 p = line + col;
1242 }
1243 else
1244 can_spell = TRUE;
1245
1246 if (can_spell)
1247 {
1248 found_pos.lnum = lnum;
1249 found_pos.col = p - line;
1250#ifdef FEAT_VIRTUALEDIT
1251 found_pos.coladd = 0;
1252#endif
1253 if (dir == FORWARD)
1254 {
1255 /* No need to search further. */
1256 curwin->w_cursor = found_pos;
1257 return OK;
1258 }
1259 }
1260 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001261 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001262 attr = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001263 }
1264 p += len;
1265 if (*p == NUL)
1266 break;
1267 nwc = FALSE;
1268 }
1269
1270 /* advance to next character */
1271 mb_ptr_adv(p);
1272 wc = nwc;
1273 }
1274
1275 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001276 if (dir == BACKWARD)
1277 {
1278 if (found_pos.lnum != 0)
1279 {
1280 /* Use the last match in the line. */
1281 curwin->w_cursor = found_pos;
1282 return OK;
1283 }
1284 if (lnum == 1)
1285 return FAIL;
1286 --lnum;
1287 }
1288 else
1289 {
1290 if (lnum == curbuf->b_ml.ml_line_count)
1291 return FAIL;
1292 ++lnum;
1293 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001294
1295 line_breakcheck();
1296 }
1297
1298 return FAIL; /* interrupted */
1299}
1300
1301/*
1302 * Load word list for "lang" from a Vim spell file.
1303 * "lang" must be the language without the region: "en" or "en-rare".
1304 */
1305 static slang_T *
1306spell_load_lang(lang)
1307 char_u *lang;
1308{
1309 slang_T *lp;
1310 char_u fname_enc[80];
1311 char_u *p;
1312 int r;
1313
1314 lp = slang_alloc(lang);
1315 if (lp != NULL)
1316 {
1317 /* Find all spell files for "lang" in 'runtimepath' and load them.
1318 * Use 'encoding', except that we use "latin1" for "latin9". */
1319#ifdef FEAT_MBYTE
1320 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1321 p = p_enc;
1322 else
1323#endif
1324 p = (char_u *)"latin1";
1325 sprintf((char *)fname_enc, "spell/%s.%s.spl", lang, p);
1326
1327 r = do_in_runtimepath(fname_enc, TRUE, spell_load_file, lp);
Bram Moolenaar5482f332005-04-17 20:18:43 +00001328 if (r == FAIL && !lp->sl_error)
1329 {
1330 /* Try loading the ASCII version. */
1331 sprintf((char *)fname_enc, "spell/%s.ascii.spl", lang);
1332
1333 r = do_in_runtimepath(fname_enc, TRUE, spell_load_file, lp);
1334 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001335 if (r == FAIL || lp->sl_error)
1336 {
1337 slang_free(lp);
1338 lp = NULL;
1339 if (r == FAIL)
1340 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
1341 fname_enc + 6);
1342 }
1343 else
1344 {
1345 lp->sl_next = first_lang;
1346 first_lang = lp;
1347 }
1348 }
1349
1350 return lp;
1351}
1352
1353/*
1354 * Allocate a new slang_T.
1355 * Caller must fill "sl_next".
1356 */
1357 static slang_T *
1358slang_alloc(lang)
1359 char_u *lang;
1360{
1361 slang_T *lp;
1362
1363 lp = (slang_T *)alloc(sizeof(slang_T));
1364 if (lp != NULL)
1365 {
1366 lp->sl_name = vim_strsave(lang);
1367 hash_init(&lp->sl_words);
1368 ga_init2(&lp->sl_preftab, sizeof(hashtab_T), 4);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001369 hash_init(&lp->sl_prewords);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001370 ga_init2(&lp->sl_sufftab, sizeof(hashtab_T), 4);
1371 lp->sl_prefzero = NULL;
1372 lp->sl_suffzero = NULL;
1373 lp->sl_try = NULL;
1374 ga_init2(&lp->sl_rep, sizeof(repentry_T), 4);
1375 lp->sl_regions[0] = NUL;
1376 lp->sl_block = NULL;
1377 lp->sl_error = FALSE;
1378 }
1379 return lp;
1380}
1381
1382/*
1383 * Free the contents of an slang_T and the structure itself.
1384 */
1385 static void
1386slang_free(lp)
1387 slang_T *lp;
1388{
1389 sblock_T *sp;
1390 int i;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001391 fword_T *fw;
1392 int todo;
1393 hashitem_T *hi;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001394
1395 vim_free(lp->sl_name);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001396
1397 /* The words themselves are in memory blocks referenced by "sl_block".
1398 * Only the hashtables for additions need to be cleared. */
1399 todo = lp->sl_words.ht_used;
1400 for (hi = lp->sl_words.ht_array; todo > 0; ++hi)
1401 {
1402 if (!HASHITEM_EMPTY(hi))
1403 {
1404 --todo;
1405 fw = HI2FWORD(hi);
1406 if (fw->fw_flags & BWF_ADDHASH)
1407 hash_clear((hashtab_T *)fw->fw_adds);
1408 }
1409 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001410 hash_clear(&lp->sl_words);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001411
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001412 for (i = 0; i < lp->sl_preftab.ga_len; ++i)
1413 hash_clear(((hashtab_T *)lp->sl_preftab.ga_data) + i);
1414 ga_clear(&lp->sl_preftab);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001415 hash_clear(&lp->sl_prewords);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001416 for (i = 0; i < lp->sl_sufftab.ga_len; ++i)
1417 hash_clear(((hashtab_T *)lp->sl_sufftab.ga_data) + i);
1418 ga_clear(&lp->sl_sufftab);
1419 ga_clear(&lp->sl_rep);
1420 vim_free(lp->sl_try);
1421 while (lp->sl_block != NULL)
1422 {
1423 sp = lp->sl_block;
1424 lp->sl_block = sp->sb_next;
1425 vim_free(sp);
1426 }
1427 vim_free(lp);
1428}
1429
1430/*
1431 * Load one spell file into an slang_T.
1432 * Invoked through do_in_runtimepath().
1433 */
1434 static void
1435spell_load_file(fname, cookie)
1436 char_u *fname;
1437 void *cookie; /* points to the slang_T to be filled */
1438{
1439 slang_T *lp = cookie;
1440 FILE *fd;
1441 char_u buf[MAXWLEN + 1];
1442 char_u cbuf[MAXWLEN + 1];
1443 char_u fbuf[MAXWLEN + 1];
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001444 char_u affixbuf[256 * 2 * 2]; /* max 2 * 256 affix nrs of 2 bytes */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001445 char_u *p;
1446 int itm;
1447 int i;
1448 int affcount;
1449 int affnr;
1450 int affflags;
1451 int affitemcnt;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001452 int prefixcnt, suffixcnt;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001453 int bl_used = SBLOCKSIZE;
1454 int widx;
Bram Moolenaar5482f332005-04-17 20:18:43 +00001455 int prefm = 0; /* 1 if <= 256 prefixes, sizeof(short_u) otherw. */
1456 int suffm = 0; /* 1 if <= 256 suffixes, sizeof(short_u) otherw. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001457 int wlen;
1458 int flags;
1459 affitem_T *ai, *ai2, **aip;
1460 int round;
1461 char_u *save_sourcing_name = sourcing_name;
1462 linenr_T save_sourcing_lnum = sourcing_lnum;
1463 int cnt;
1464 int choplen;
1465 int addlen;
1466 int leadlen;
1467 int wordcount;
1468 fword_T *fw, *fw2;
1469 garray_T *gap;
1470 hashtab_T *ht;
1471 hashitem_T *hi;
1472 hash_T hash;
1473 int adds;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001474 addword_T *aw, *naw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001475 int flen;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001476 int xlen;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001477
1478 fd = fopen((char *)fname, "r");
1479 if (fd == NULL)
1480 {
1481 EMSG2(_(e_notopen), fname);
1482 goto errorend;
1483 }
1484
1485 /* Set sourcing_name, so that error messages mention the file name. */
1486 sourcing_name = fname;
1487 sourcing_lnum = 0;
1488
1489 /* <HEADER>: <fileID> <regioncnt> <regionname> ... */
1490 for (i = 0; i < VIMSPELLMAGICL; ++i)
1491 buf[i] = getc(fd); /* <fileID> */
1492 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
1493 {
1494 EMSG(_("E757: Wrong file ID in spell file"));
1495 goto errorend;
1496 }
1497
1498 cnt = getc(fd); /* <regioncnt> */
1499 if (cnt == EOF)
1500 {
1501truncerr:
1502 EMSG(_("E758: Truncated spell file"));
1503 goto errorend;
1504 }
1505 if (cnt > 8)
1506 {
1507formerr:
1508 EMSG(_("E759: Format error in spell file"));
1509 goto errorend;
1510 }
1511 for (i = 0; i < cnt; ++i)
1512 {
1513 lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
1514 lp->sl_regions[i * 2 + 1] = getc(fd);
1515 }
1516 lp->sl_regions[cnt * 2] = NUL;
1517
1518 /* round 1: <PREFIXLIST>: <affcount> <afftotcnt> <affix> ...
1519 * round 2: <SUFFIXLIST>: <affcount> <afftotcnt> <affix> ... */
1520 for (round = 1; round <= 2; ++round)
1521 {
1522 affcount = (getc(fd) << 8) + getc(fd); /* <affcount> */
1523 if (affcount < 0)
1524 goto truncerr;
1525 if (round == 1)
1526 {
1527 gap = &lp->sl_preftab;
1528 aip = &lp->sl_prefzero;
1529 lp->sl_prefcnt = affcount;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001530 prefm = affcount > 256 ? 2 : 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001531 }
1532 else
1533 {
1534 gap = &lp->sl_sufftab;
1535 aip = &lp->sl_suffzero;
1536 lp->sl_suffcnt = affcount;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001537 suffm = affcount > 256 ? 2 : 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001538 }
1539
1540 i = (getc(fd) << 8) + getc(fd); /* <afftotcnt> */
1541 /* afftotcnt is not used */
1542
1543 /*
1544 * For each affix NR there can be several affixes.
1545 */
1546 for (affnr = 0; affnr < affcount; ++affnr)
1547 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001548 /* <affix>: <affitemcnt> <affitem> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001549 affitemcnt = (getc(fd) << 8) + getc(fd); /* <affitemcnt> */
1550 if (affitemcnt < 0)
1551 goto truncerr;
1552 for (itm = 0; itm < affitemcnt; ++itm)
1553 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001554 /* <affitem>: <affflags> <affchoplen> <affchop>
1555 * <affaddlen> <affadd> */
1556 affflags = getc(fd); /* <affflags> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001557 choplen = getc(fd); /* <affchoplen> */
1558 if (choplen == EOF)
1559 goto truncerr;
1560 if (choplen >= MAXWLEN)
1561 goto formerr;
1562 for (i = 0; i < choplen; ++i) /* <affchop> */
1563 buf[i] = getc(fd);
1564 buf[i] = NUL;
1565 addlen = getc(fd); /* <affaddlen> */
1566 if (addlen == EOF)
1567 goto truncerr;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001568 if (affflags & AFF_PREWORD)
1569 xlen = addlen + 2; /* space for lead and trail string */
1570 else
1571 xlen = 0;
1572
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001573 /* Get room to store the affitem_T, chop and add strings. */
1574 p = (char_u *)getroom(lp, &bl_used,
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001575 sizeof(affitem_T) + addlen + choplen + 1 + xlen);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001576 if (p == NULL)
1577 goto errorend;
1578
1579 ai = (affitem_T *)p;
1580 ai->ai_nr = affnr;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001581 ai->ai_flags = affflags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001582 ai->ai_choplen = choplen;
1583 ai->ai_addlen = addlen;
1584
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001585 /* Chop string is at ai_add[ai_addlen + 1]. */
1586 p = ai->ai_add + addlen + 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001587 STRCPY(p, buf);
1588
1589 p = ai->ai_add;
1590 for (i = 0; i < addlen; ++i) /* <affadd> */
1591 p[i] = getc(fd);
1592 p[i] = NUL;
1593
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001594 if (affflags & AFF_PREWORD)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001595 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001596 int l, leadoff, trailoff;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001597
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001598 /*
1599 * Separate lead and trail string, put word at ai_add, so
1600 * that it can be used as hashtable key.
1601 */
1602 /* lead string: up to first word char */
1603 while (*p != NUL && !spell_iswordc(p))
1604 mb_ptr_adv(p);
1605 ai->ai_leadlen = p - ai->ai_add;
1606 leadoff = addlen + choplen + 2;
1607 mch_memmove(ai->ai_add + leadoff, ai->ai_add,
1608 ai->ai_leadlen);
1609 ai->ai_add[leadoff + ai->ai_leadlen] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001610
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001611 /* trail string: after last word char */
1612 while (*p != NUL && spell_iswordc(p))
1613 mb_ptr_adv(p);
1614 trailoff = leadoff + ai->ai_leadlen + 1;
1615 STRCPY(ai->ai_add + trailoff, p);
1616 ai->ai_taillen = STRLEN(p);
1617
1618 /* word itself */
1619 l = (p - ai->ai_add) - ai->ai_leadlen;
1620 mch_memmove(ai->ai_add, ai->ai_add + ai->ai_leadlen, l);
1621 ai->ai_add[l] = NUL;
1622 hash = hash_hash(ai->ai_add);
1623 hi = hash_lookup(&lp->sl_prewords, ai->ai_add, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001624 if (HASHITEM_EMPTY(hi))
1625 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001626 /* First affix with this word, add to hashtable. */
1627 hash_add_item(&lp->sl_prewords, hi, ai->ai_add, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001628 ai->ai_next = NULL;
1629 }
1630 else
1631 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001632 /* There already is an affix with this word, link in
1633 * the list. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001634 ai2 = HI2AI(hi);
1635 ai->ai_next = ai2->ai_next;
1636 ai2->ai_next = ai;
1637 }
1638 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001639 else
1640 {
1641 /*
1642 * Add the affix to a hashtable. Which one depends on the
1643 * length of the added string in characters.
1644 */
1645#ifdef FEAT_MBYTE
1646 /* Change "addlen" from length in bytes to length in
1647 * chars. */
1648 if (has_mbyte)
1649 addlen = mb_charlen(p);
1650#endif
1651 if (addlen == 0)
1652 {
1653 /* Link in list of zero length affixes. */
1654 ai->ai_next = *aip;
1655 *aip = ai;
1656 }
1657 else
1658 {
1659 if (gap->ga_len < addlen)
1660 {
1661 /* Longer affix, need more hashtables. */
1662 if (ga_grow(gap, addlen - gap->ga_len) == FAIL)
1663 goto errorend;
1664
1665 /* Re-allocating ga_data means that an ht_array
1666 * pointing to ht_smallarray becomes invalid. We
1667 * can recognize this: ht_mask is at its init
1668 * value. */
1669 for (i = 0; i < gap->ga_len; ++i)
1670 {
1671 ht = ((hashtab_T *)gap->ga_data) + i;
1672 if (ht->ht_mask == HT_INIT_SIZE - 1)
1673 ht->ht_array = ht->ht_smallarray;
1674 }
1675
1676 /* Init the newly used hashtable(s). */
1677 while (gap->ga_len < addlen)
1678 {
1679 hash_init(((hashtab_T *)gap->ga_data)
1680 + gap->ga_len);
1681 ++gap->ga_len;
1682 }
1683 }
1684 ht = ((hashtab_T *)gap->ga_data) + addlen - 1;
1685 hash = hash_hash(p);
1686 hi = hash_lookup(ht, p, hash);
1687 if (HASHITEM_EMPTY(hi))
1688 {
1689 /* First affix with this "ai_add", add to
1690 * hashtable. */
1691 hash_add_item(ht, hi, p, hash);
1692 ai->ai_next = NULL;
1693 }
1694 else
1695 {
1696 /* There already is an affix with this "ai_add",
1697 * link in the list. */
1698 ai2 = HI2AI(hi);
1699 ai->ai_next = ai2->ai_next;
1700 ai2->ai_next = ai;
1701 }
1702 }
1703 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001704 }
1705 }
1706 }
1707
1708 /* <SUGGEST> : <suggestlen> <more> ... */
1709 /* TODO, just skip this for now */
1710 i = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
1711 while (i-- > 0)
1712 if (getc(fd) == EOF) /* <suggestlen> */
1713 goto truncerr;
1714
1715 /* <WORDLIST>: <wordcount> <worditem> ... */ /* <wordcount> */
1716 wordcount = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8)
1717 + getc(fd);
1718 if (wordcount < 0)
1719 goto truncerr;
1720
1721 /* Init hashtable for this number of words, so that it doesn't need to
1722 * reallocate the table halfway. */
1723 hash_lock_size(&lp->sl_words, wordcount);
1724
1725 for (widx = 0; ; ++widx)
1726 {
1727 /* <worditem>: <nr> <string> <flags> [<flags2>]
1728 * [<caselen> <caseword>]
1729 * [<affixcnt> <affixNR> ...] (prefixes)
1730 * [<affixcnt> <affixNR> ...] (suffixes)
1731 * [<region>]
1732 * [<addcnt> <add> ...]
1733 */
1734 /* Use <nr> bytes from the previous word. */
1735 wlen = getc(fd); /* <nr> */
1736 if (wlen == EOF)
1737 {
1738 if (widx >= wordcount) /* normal way to end the file */
1739 break;
1740 goto truncerr;
1741 }
1742
1743 /* Read further word bytes until one below 0x20, that must be the
1744 * flags. Keep this fast! */
1745 for (;;)
1746 {
1747 if ((buf[wlen] = getc(fd)) < 0x20) /* <string> */
1748 break;
1749 if (++wlen == MAXWLEN)
1750 goto formerr;
1751 }
1752 flags = buf[wlen]; /* <flags> */
1753 buf[wlen] = NUL;
1754
1755 /* Get more flags if they're there. */
1756 if (flags & BWF_SECOND)
1757 flags += getc(fd) << 8; /* <flags2> */
1758
1759 if (flags & BWF_KEEPCAP)
1760 {
1761 /* Read <caselen> and <caseword> first, its length may differ from
1762 * the case-folded word. Note: this should only happen after the
1763 * basic word! */
1764 wlen = getc(fd);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001765 if (wlen < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001766 goto truncerr;
1767 for (i = 0; i < wlen; ++i)
1768 cbuf[i] = getc(fd);
1769 cbuf[i] = NUL;
1770 }
1771
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001772 /* Optional prefixes */
1773 p = affixbuf;
1774 if (flags & BWF_PREFIX)
1775 {
1776 cnt = getc(fd); /* <affixcnt> */
1777 if (cnt < 0)
1778 goto truncerr;
1779 prefixcnt = cnt;
1780 for (i = cnt * prefm; --i >= 0; ) /* <affixNR> */
1781 *p++ = getc(fd);
1782 }
1783 else
1784 prefixcnt = 0;
1785
1786 /* Optional suffixes */
1787 if (flags & BWF_SUFFIX)
1788 {
1789 cnt = getc(fd); /* <affixcnt> */
1790 if (cnt < 0)
1791 goto truncerr;
1792 suffixcnt = cnt;
1793 for (i = cnt * suffm; --i >= 0; ) /* <affixNR> */
1794 *p++ = getc(fd);
1795 }
1796 else
1797 suffixcnt = 0;
1798
1799 /* Find room to store the word in an fword_T. */
1800 fw = (fword_T *)getroom(lp, &bl_used, (int)sizeof(fword_T) + wlen
1801 + (p - affixbuf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001802 if (fw == NULL)
1803 goto errorend;
1804 mch_memmove(fw->fw_word, (flags & BWF_KEEPCAP) ? cbuf : buf, wlen + 1);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001805
1806 /* Put the affix NRs just after the word, if any. */
1807 if (p > affixbuf)
1808 mch_memmove(fw->fw_word + wlen + 1, affixbuf, p - affixbuf);
1809
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001810 fw->fw_flags = flags;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001811 fw->fw_prefixcnt = prefixcnt;
1812 fw->fw_suffixcnt = suffixcnt;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001813
1814 hash = hash_hash(buf);
1815 hi = hash_lookup(&lp->sl_words, buf, hash);
1816 if (HASHITEM_EMPTY(hi))
1817 {
1818 if (hash_add_item(&lp->sl_words, hi, fw->fw_word, hash) == FAIL)
1819 goto errorend;
1820 fw->fw_next = NULL;
1821 }
1822 else
1823 {
1824 /* Already have this basic word in the hashtable, this one will
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001825 * have different case flags and/or affixes. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001826 fw2 = HI2FWORD(hi);
1827 fw->fw_next = fw2->fw_next;
1828 fw2->fw_next = fw;
1829 --widx; /* don't count this one */
1830 }
1831
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001832 if (flags & BWF_REGION)
1833 fw->fw_region = getc(fd); /* <region> */
1834 else
1835 fw->fw_region = REGION_ALL;
1836
1837 fw->fw_adds = NULL;
1838 if (flags & BWF_ADDS)
1839 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001840 if (flags & BWF_ADDS_M)
1841 adds = (getc(fd) << 8) + getc(fd); /* <addcnt> */
1842 else
1843 adds = getc(fd); /* <addcnt> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001844
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001845 if (adds > 30)
1846 {
1847 /* Use a hashtable to loopup the part until the next word end.
1848 * This uses more memory and involves some overhead, thus only
1849 * do it when there are many additions (e.g., for French). */
1850 ht = (hashtab_T *)getroom(lp, &bl_used, sizeof(hashtab_T));
1851 if (ht == NULL)
1852 goto errorend;
1853 hash_init(ht);
1854 fw->fw_adds = (addword_T *)ht;
1855 fw->fw_flags |= BWF_ADDHASH;
1856
1857 /* Preset the size of the hashtable. It's never unlocked. */
1858 hash_lock_size(ht, adds + 1);
1859 }
1860 else
1861 ht = NULL;
1862
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001863 while (--adds >= 0)
1864 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001865 /* <add>: <addflags> <addlen> [<leadlen>] [<copylen>]
1866 * [<addstring>] [<region>] */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001867 flags = getc(fd); /* <addflags> */
1868 addlen = getc(fd); /* <addlen> */
1869 if (addlen == EOF)
1870 goto truncerr;
1871 if (addlen >= MAXWLEN)
1872 goto formerr;
1873
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001874 if (flags & ADD_LEADLEN)
1875 leadlen = getc(fd); /* <leadlen> */
1876 else
1877 leadlen = 0;
1878
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001879 if (addlen > 0)
1880 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001881 if (flags & ADD_COPYLEN)
1882 i = getc(fd); /* <copylen> */
1883 else
1884 i = 0;
1885 for ( ; i < addlen; ++i) /* <addstring> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001886 cbuf[i] = getc(fd);
1887 cbuf[i] = NUL;
1888 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001889
1890 if (flags & ADD_KEEPCAP)
1891 {
1892 /* <addstring> is in original case, need to get
1893 * case-folded word too. */
1894 (void)str_foldcase(cbuf, addlen, fbuf, MAXWLEN);
1895 flen = addlen - leadlen + 1;
1896 addlen = STRLEN(fbuf);
1897 }
1898 else
1899 flen = 0;
1900
1901 aw = (addword_T *)getroom(lp, &bl_used,
1902 sizeof(addword_T) + addlen + flen);
1903 if (aw == NULL)
1904 goto errorend;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001905
1906 if (flags & ADD_KEEPCAP)
1907 {
1908 /* Put the addition in original case after the case-folded
1909 * string. */
1910 STRCPY(aw->aw_word, fbuf);
1911 STRCPY(aw->aw_word + addlen + 1, cbuf + leadlen);
1912 }
1913 else
1914 STRCPY(aw->aw_word, cbuf);
1915
1916 aw->aw_flags = flags;
1917 aw->aw_wordlen = addlen;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001918 aw->aw_leadlen = leadlen;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001919
1920 if (flags & ADD_REGION)
1921 aw->aw_region = getc(fd); /* <region> */
1922 else
1923 aw->aw_region = REGION_ALL;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001924
1925 if (ht == NULL)
1926 {
1927 /* Using simple linked list, put it in front. */
1928 aw->aw_next = fw->fw_adds;
1929 fw->fw_adds = aw;
1930 aw->aw_saveb = NUL;
1931 }
1932 else
1933 {
1934 /* Put addition in hashtable. For key we use the part up
1935 * to the next end-of-word. */
1936 if (leadlen == 0)
1937 {
1938 p = aw->aw_word;
1939 while (*p != NUL && !spell_iswordc(p))
1940 mb_ptr_adv(p);
1941 }
1942
1943 if (leadlen != 0 || *p == NUL)
1944 {
1945 /* Only non-word characters in addition, add it to the
1946 * list with the special key NOWC_KEY. Also do this
1947 * when there is a leadstring, it would get too
1948 * complicated. */
1949 hash = hash_hash(NOWC_KEY);
1950 hi = hash_lookup(ht, NOWC_KEY, hash);
1951 if (HASHITEM_EMPTY(hi))
1952 {
1953 /* we use a dummy item as the list header */
1954 naw = (addword_T *)getroom(lp, &bl_used,
1955 sizeof(addword_T) + STRLEN(NOWC_KEY));
1956 if (naw == NULL)
1957 goto errorend;
1958 STRCPY(naw->aw_word, NOWC_KEY);
1959 hash_add_item(ht, hi, naw->aw_word, hash);
1960 naw->aw_next = aw;
1961 aw->aw_next = NULL;
1962 }
1963 else
1964 {
1965 naw = HI2ADDWORD(hi);
1966 aw->aw_next = naw->aw_next;
1967 naw->aw_next = aw;
1968 }
1969 aw->aw_saveb = NUL;
1970 }
1971 else
1972 {
1973 /* Truncate at next non-word character, store that
1974 * byte in "aw_saveb". */
1975 while (*p != NUL && spell_iswordc(p))
1976 mb_ptr_adv(p);
1977 aw->aw_saveb = *p;
1978 *p = NUL;
1979 hash = hash_hash(aw->aw_word);
1980 hi = hash_lookup(ht, aw->aw_word, hash);
1981 if (HASHITEM_EMPTY(hi))
1982 {
1983 hash_add_item(ht, hi, aw->aw_word, hash);
1984 aw->aw_next = NULL;
1985 }
1986 else
1987 {
1988 naw = HI2ADDWORD(hi);
1989 aw->aw_next = naw->aw_next;
1990 naw->aw_next = aw;
1991 }
1992 }
1993 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001994 }
1995 }
1996 }
1997 goto end_OK;
1998
1999errorend:
2000 lp->sl_error = TRUE;
2001end_OK:
2002 if (fd != NULL)
2003 fclose(fd);
2004 hash_unlock(&lp->sl_words);
2005 sourcing_name = save_sourcing_name;
2006 sourcing_lnum = save_sourcing_lnum;
2007}
2008
2009/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002010 * Get part of an sblock_T, at least "len" bytes long.
2011 * Returns NULL when out of memory.
2012 */
2013 static void *
2014getroom(lp, bl_used, len)
2015 slang_T *lp; /* lp->sl_block is current block or NULL */
2016 int *bl_used; /* used up from current block */
2017 int len; /* length needed */
2018{
2019 char_u *p;
2020 sblock_T *bl = lp->sl_block;
2021
2022 if (bl == NULL || *bl_used + len > SBLOCKSIZE)
2023 {
2024 /* Allocate a block of memory. This is not freed until spell_reload()
2025 * is called. */
2026 bl = (sblock_T *)alloc((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
2027 if (bl == NULL)
2028 return NULL;
2029 bl->sb_next = lp->sl_block;
2030 lp->sl_block = bl;
2031 *bl_used = 0;
2032 }
2033
2034 p = bl->sb_data + *bl_used;
2035 *bl_used += len;
2036
2037 return p;
2038}
2039
2040/*
2041 * Parse 'spelllang' and set buf->b_langp accordingly.
2042 * Returns an error message or NULL.
2043 */
2044 char_u *
2045did_set_spelllang(buf)
2046 buf_T *buf;
2047{
2048 garray_T ga;
2049 char_u *lang;
2050 char_u *e;
2051 char_u *region;
2052 int region_mask;
2053 slang_T *lp;
2054 int c;
2055 char_u lbuf[MAXWLEN + 1];
2056
2057 ga_init2(&ga, sizeof(langp_T), 2);
2058
2059 /* loop over comma separated languages. */
2060 for (lang = buf->b_p_spl; *lang != NUL; lang = e)
2061 {
2062 e = vim_strchr(lang, ',');
2063 if (e == NULL)
2064 e = lang + STRLEN(lang);
Bram Moolenaar5482f332005-04-17 20:18:43 +00002065 region = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002066 if (e > lang + 2)
2067 {
2068 if (e - lang >= MAXWLEN)
2069 {
2070 ga_clear(&ga);
2071 return e_invarg;
2072 }
2073 if (lang[2] == '_')
2074 region = lang + 3;
2075 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002076
2077 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2078 if (STRNICMP(lp->sl_name, lang, 2) == 0)
2079 break;
2080
2081 if (lp == NULL)
2082 {
2083 /* Not found, load the language. */
2084 STRNCPY(lbuf, lang, e - lang);
2085 lbuf[e - lang] = NUL;
2086 if (region != NULL)
2087 mch_memmove(lbuf + 2, lbuf + 5, e - lang - 4);
2088 lp = spell_load_lang(lbuf);
2089 }
2090
2091 if (lp != NULL)
2092 {
2093 if (region == NULL)
2094 region_mask = REGION_ALL;
2095 else
2096 {
2097 /* find region in sl_regions */
2098 c = find_region(lp->sl_regions, region);
2099 if (c == REGION_ALL)
2100 {
2101 c = *e;
2102 *e = NUL;
2103 smsg((char_u *)_("Warning: region %s not supported"), lang);
2104 *e = c;
2105 region_mask = REGION_ALL;
2106 }
2107 else
2108 region_mask = 1 << c;
2109 }
2110
2111 if (ga_grow(&ga, 1) == FAIL)
2112 {
2113 ga_clear(&ga);
2114 return e_outofmem;
2115 }
2116 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2117 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2118 ++ga.ga_len;
2119 }
2120
2121 if (*e == ',')
2122 ++e;
2123 }
2124
2125 /* Add a NULL entry to mark the end of the list. */
2126 if (ga_grow(&ga, 1) == FAIL)
2127 {
2128 ga_clear(&ga);
2129 return e_outofmem;
2130 }
2131 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
2132 ++ga.ga_len;
2133
2134 /* Everything is fine, store the new b_langp value. */
2135 ga_clear(&buf->b_langp);
2136 buf->b_langp = ga;
2137
2138 return NULL;
2139}
2140
2141/*
2142 * Find the region "region[2]" in "rp" (points to "sl_regions").
2143 * Each region is simply stored as the two characters of it's name.
2144 * Returns the index if found, REGION_ALL if not found.
2145 */
2146 static int
2147find_region(rp, region)
2148 char_u *rp;
2149 char_u *region;
2150{
2151 int i;
2152
2153 for (i = 0; ; i += 2)
2154 {
2155 if (rp[i] == NUL)
2156 return REGION_ALL;
2157 if (rp[i] == region[0] && rp[i + 1] == region[1])
2158 break;
2159 }
2160 return i / 2;
2161}
2162
2163/*
2164 * Return type of word:
2165 * w word 0
2166 * Word BWF_ONECAP
2167 * W WORD BWF_ALLCAP
2168 * WoRd wOrd BWF_KEEPCAP
2169 */
2170 static int
2171captype(word, end)
2172 char_u *word;
2173 char_u *end;
2174{
2175 char_u *p;
2176 int c;
2177 int firstcap;
2178 int allcap;
2179 int past_second = FALSE; /* past second word char */
2180
2181 /* find first letter */
2182 for (p = word; !spell_iswordc(p); mb_ptr_adv(p))
2183 if (p >= end)
2184 return 0; /* only non-word characters, illegal word */
2185#ifdef FEAT_MBYTE
2186 c = mb_ptr2char_adv(&p);
2187#else
2188 c = *p++;
2189#endif
2190 firstcap = allcap = MB_ISUPPER(c);
2191
2192 /*
2193 * Need to check all letters to find a word with mixed upper/lower.
2194 * But a word with an upper char only at start is a ONECAP.
2195 */
2196 for ( ; p < end; mb_ptr_adv(p))
2197 if (spell_iswordc(p))
2198 {
2199#ifdef FEAT_MBYTE
2200 c = mb_ptr2char(p);
2201#else
2202 c = *p;
2203#endif
2204 if (!MB_ISUPPER(c))
2205 {
2206 /* UUl -> KEEPCAP */
2207 if (past_second && allcap)
2208 return BWF_KEEPCAP;
2209 allcap = FALSE;
2210 }
2211 else if (!allcap)
2212 /* UlU -> KEEPCAP */
2213 return BWF_KEEPCAP;
2214 past_second = TRUE;
2215 }
2216
2217 if (allcap)
2218 return BWF_ALLCAP;
2219 if (firstcap)
2220 return BWF_ONECAP;
2221 return 0;
2222}
2223
2224# if defined(FEAT_MBYTE) || defined(PROTO)
2225/*
2226 * Clear all spelling tables and reload them.
2227 * Used after 'encoding' is set.
2228 */
2229 void
2230spell_reload()
2231{
2232 buf_T *buf;
2233 slang_T *lp;
2234
2235 /* Initialize the table for spell_iswordc(). */
2236 init_spell_chartab();
2237
2238 /* Unload all allocated memory. */
2239 while (first_lang != NULL)
2240 {
2241 lp = first_lang;
2242 first_lang = lp->sl_next;
2243 slang_free(lp);
2244 }
2245
2246 /* Go through all buffers and handle 'spelllang'. */
2247 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2248 {
2249 ga_clear(&buf->b_langp);
2250 if (*buf->b_p_spl != NUL)
2251 did_set_spelllang(buf);
2252 }
2253}
2254# endif
2255
2256/*
2257 * Recognizing words uses a two-step mechanism:
2258 * 1. Locate a basic word, made out of word characters only and separated by
2259 * non-word characters.
2260 * 2. When a basic word is found, check if (possibly required) additions
2261 * before and after the word are present.
2262 *
2263 * Both mechanisms use affixes (prefixes and suffixes) to reduce the number of
2264 * words. When no matching word was found in the hashtable the start of the
2265 * word is checked for matching prefixes and the end of the word for matching
2266 * suffixes. All matching affixes are removed and then the resulting word is
2267 * searched for. If found it is checked if it supports the used affix.
2268 */
2269
2270
2271#if defined(FEAT_MBYTE) || defined(PROTO)
2272/*
2273 * Functions for ":mkspell".
2274 * Only possible with the multi-byte feature.
2275 */
2276
2277#define MAXLINELEN 300 /* Maximum length in bytes of a line in a .aff
2278 and .dic file. */
2279/*
2280 * Main structure to store the contents of a ".aff" file.
2281 */
2282typedef struct afffile_S
2283{
2284 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
2285 char_u *af_try; /* "TRY" line in "af_enc" encoding */
2286 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
2287 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
2288 garray_T af_rep; /* list of repentry_T entries from REP lines */
2289} afffile_T;
2290
2291typedef struct affentry_S affentry_T;
2292
2293/* Affix header from ".aff" file. Used for af_pref and af_suff. */
2294typedef struct affheader_S
2295{
2296 char_u ah_key[2]; /* key for hashtable == name of affix entry */
2297 int ah_combine;
2298 affentry_T *ah_first; /* first affix entry */
2299 short_u ah_affnr; /* used in get_new_aff() */
2300} affheader_T;
2301
2302#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
2303
2304/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
2305struct affentry_S
2306{
2307 affentry_T *ae_next; /* next affix with same name/number */
2308 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
2309 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar5482f332005-04-17 20:18:43 +00002310 char_u *ae_add_nw; /* For a suffix: first non-word char in
2311 * "ae_add"; for a prefix with only non-word
2312 * chars: equal to "ae_add", for a prefix with
2313 * word and non-word chars: first non-word
2314 * char after word char. NULL otherwise. */
2315 char_u *ae_add_pw; /* For a prefix with both word and non-word
2316 * chars: first word char. NULL otherwise. */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00002317 char_u ae_preword; /* TRUE for a prefix with one word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002318 char_u *ae_cond; /* condition (NULL for ".") */
2319 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
2320 short_u ae_affnr; /* for old affix: new affix number */
2321};
2322
2323/*
2324 * Structure to store a word from a ".dic" file.
2325 */
2326typedef struct dicword_S
2327{
2328 char_u *dw_affnm; /* original affix names */
2329 char_u dw_word[1]; /* actually longer: the word in 'encoding' */
2330} dicword_T;
2331
2332static dicword_T dumdw;
2333#define HI2DW(hi) ((dicword_T *)((hi)->hi_key - (dumdw.dw_word - (char_u *)&dumdw)))
2334
2335/*
2336 * Structure to store a basic word for the spell file.
2337 * This is used for ":mkspell", not for spell checking.
2338 */
2339typedef struct basicword_S basicword_T;
2340struct basicword_S
2341{
2342 basicword_T *bw_next; /* next word with same basic word */
2343 basicword_T *bw_cnext; /* next word with same caps */
2344 int bw_flags; /* BWF_ flags */
2345 garray_T bw_prefix; /* table with prefix numbers */
2346 garray_T bw_suffix; /* table with suffix numbers */
2347 int bw_region; /* region bits */
2348 char_u *bw_caseword; /* keep-case word */
2349 char_u *bw_leadstring; /* must come before bw_word */
2350 char_u *bw_addstring; /* must come after bw_word */
2351 char_u bw_word[1]; /* actually longer: word case folded */
2352};
2353
2354static basicword_T dumbw;
2355#define KEY2BW(p) ((basicword_T *)((p) - (dumbw.bw_word - (char_u *)&dumbw)))
2356#define HI2BW(hi) KEY2BW((hi)->hi_key)
2357
2358/* Store the affix number related with a certain string. */
2359typedef struct affhash_S
2360{
2361 short_u as_nr; /* the affix nr */
2362 char_u as_word[1]; /* actually longer */
2363} affhash_T;
2364
2365static affhash_T dumas;
2366#define HI2AS(hi) ((affhash_T *)((hi)->hi_key - (dumas.as_word - (char_u *)&dumas)))
2367
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002368/* info for writing the spell file */
2369typedef struct winfo_S
2370{
2371 FILE *wif_fd;
2372 basicword_T *wif_prevbw; /* last written basic word */
2373 int wif_regionmask; /* regions supported */
2374 int wif_prefm; /* 1 or 2 bytes used for prefix NR */
2375 int wif_suffm; /* 1 or 2 bytes used for suffix NR */
2376 long wif_wcount; /* written word count */
2377 long wif_acount; /* written addition count */
2378 long wif_addmax; /* max number of additions on one word */
2379 char_u *wif_addmaxw; /* word with max additions */
2380} winfo_T;
2381
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002382
Bram Moolenaar5482f332005-04-17 20:18:43 +00002383static afffile_T *spell_read_aff __ARGS((char_u *fname, vimconv_T *conv, int ascii));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002384static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar5482f332005-04-17 20:18:43 +00002385static int has_non_ascii __ARGS((char_u *s));
2386static int spell_read_dic __ARGS((hashtab_T *ht, char_u *fname, vimconv_T *conv, int ascii));
2387static int get_new_aff __ARGS((hashtab_T *oldaff, garray_T *gap, int prefix));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002388static void spell_free_dic __ARGS((hashtab_T *dic));
2389static int same_affentries __ARGS((affheader_T *ah1, affheader_T *ah2));
2390static void add_affhash __ARGS((hashtab_T *ht, char_u *key, int newnr));
2391static void clear_affhash __ARGS((hashtab_T *ht));
2392static void trans_affixes __ARGS((dicword_T *dw, basicword_T *bw, afffile_T *oldaff, hashtab_T *newwords));
2393static int build_wordlist __ARGS((hashtab_T *newwords, hashtab_T *oldwords, afffile_T *oldaff, int regionmask));
2394static void combine_regions __ARGS((hashtab_T *newwords));
2395static int same_affixes __ARGS((basicword_T *bw, basicword_T *nbw));
2396static void expand_affixes __ARGS((hashtab_T *newwords, garray_T *prefgap, garray_T *suffgap));
2397static void expand_one_aff __ARGS((basicword_T *bw, garray_T *add_words, affentry_T *pae, affentry_T *sae));
2398static void add_to_wordlist __ARGS((hashtab_T *newwords, basicword_T *bw));
2399static void put_bytes __ARGS((FILE *fd, long_u nr, int len));
2400static void write_affix __ARGS((FILE *fd, affheader_T *ah));
2401static void write_affixlist __ARGS((FILE *fd, garray_T *aff, int bytes));
2402static void write_vim_spell __ARGS((char_u *fname, garray_T *prefga, garray_T *suffga, hashtab_T *newwords, int regcount, char_u *regchars));
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002403static void write_bword __ARGS((winfo_T *wif, basicword_T *bw, int lowcap));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002404static void free_wordtable __ARGS((hashtab_T *ht));
2405static void free_basicword __ARGS((basicword_T *bw));
2406static void free_affixentries __ARGS((affentry_T *first));
Bram Moolenaar5482f332005-04-17 20:18:43 +00002407static void free_affix_entry __ARGS((affentry_T *ap));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002408
2409/*
2410 * Read an affix ".aff" file.
2411 * Returns an afffile_T, NULL for failure.
2412 */
2413 static afffile_T *
Bram Moolenaar5482f332005-04-17 20:18:43 +00002414spell_read_aff(fname, conv, ascii)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002415 char_u *fname;
2416 vimconv_T *conv; /* info for encoding conversion */
Bram Moolenaar5482f332005-04-17 20:18:43 +00002417 int ascii; /* Only accept ASCII characters */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002418{
2419 FILE *fd;
2420 afffile_T *aff;
2421 char_u rline[MAXLINELEN];
2422 char_u *line;
2423 char_u *pc = NULL;
2424 char_u *(items[6]);
2425 int itemcnt;
2426 char_u *p;
2427 int lnum = 0;
2428 affheader_T *cur_aff = NULL;
2429 int aff_todo = 0;
2430 hashtab_T *tp;
2431
2432 fd = fopen((char *)fname, "r");
2433 if (fd == NULL)
2434 {
2435 EMSG2(_(e_notopen), fname);
2436 return NULL;
2437 }
2438
2439 smsg((char_u *)_("Reading affix file %s..."), fname);
2440 out_flush();
2441
2442 aff = (afffile_T *)alloc_clear((unsigned)sizeof(afffile_T));
2443 if (aff == NULL)
2444 return NULL;
2445 hash_init(&aff->af_pref);
2446 hash_init(&aff->af_suff);
2447 ga_init2(&aff->af_rep, (int)sizeof(repentry_T), 20);
2448
2449 /*
2450 * Read all the lines in the file one by one.
2451 */
2452 while (!vim_fgets(rline, MAXLINELEN, fd))
2453 {
2454 ++lnum;
2455
2456 /* Skip comment lines. */
2457 if (*rline == '#')
2458 continue;
2459
2460 /* Convert from "SET" to 'encoding' when needed. */
2461 vim_free(pc);
2462 if (conv->vc_type != CONV_NONE)
2463 {
2464 pc = string_convert(conv, rline, NULL);
2465 line = pc;
2466 }
2467 else
2468 {
2469 pc = NULL;
2470 line = rline;
2471 }
2472
2473 /* Split the line up in white separated items. Put a NUL after each
2474 * item. */
2475 itemcnt = 0;
2476 for (p = line; ; )
2477 {
2478 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
2479 ++p;
2480 if (*p == NUL)
2481 break;
2482 items[itemcnt++] = p;
2483 while (*p > ' ') /* skip until white space or CR/NL */
2484 ++p;
2485 if (*p == NUL)
2486 break;
2487 *p++ = NUL;
2488 }
2489
2490 /* Handle non-empty lines. */
2491 if (itemcnt > 0)
2492 {
2493 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
2494 && aff->af_enc == NULL)
2495 {
2496 if (aff->af_enc != NULL)
2497 smsg((char_u *)_("Duplicate SET line ignored in %s line %d: %s"),
2498 fname, lnum, line);
2499 else
2500 {
2501 /* Setup for conversion from "ENC" to 'encoding'. */
2502 aff->af_enc = enc_canonize(items[1]);
Bram Moolenaar5482f332005-04-17 20:18:43 +00002503 if (aff->af_enc != NULL && !ascii
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002504 && convert_setup(conv, aff->af_enc, p_enc) == FAIL)
2505 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
2506 fname, aff->af_enc, p_enc);
2507 }
2508 }
2509 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2
2510 && aff->af_try == NULL)
2511 aff->af_try = vim_strsave(items[1]);
2512 else if ((STRCMP(items[0], "PFX") == 0
2513 || STRCMP(items[0], "SFX") == 0)
2514 && aff_todo == 0
2515 && itemcnt == 4)
2516 {
2517 /* New affix letter. */
2518 cur_aff = (affheader_T *)alloc((unsigned)sizeof(affheader_T));
2519 if (cur_aff == NULL)
2520 break;
2521 cur_aff->ah_key[0] = *items[1];
2522 cur_aff->ah_key[1] = NUL;
2523 if (items[1][1] != NUL)
2524 smsg((char_u *)_("Affix name too long in %s line %d: %s"),
2525 fname, lnum, items[1]);
2526 if (*items[2] == 'Y')
2527 cur_aff->ah_combine = TRUE;
2528 else if (*items[2] == 'N')
2529 cur_aff->ah_combine = FALSE;
2530 else if (p_verbose > 0)
2531 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
2532 fname, lnum, items[2]);
2533 cur_aff->ah_first = NULL;
2534 if (*items[0] == 'P')
2535 tp = &aff->af_pref;
2536 else
2537 tp = &aff->af_suff;
2538 if (!HASHITEM_EMPTY(hash_find(tp, cur_aff->ah_key)))
2539 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
2540 fname, lnum, items[1]);
2541 else
2542 hash_add(tp, cur_aff->ah_key);
2543
2544 aff_todo = atoi((char *)items[3]);
2545 }
2546 else if ((STRCMP(items[0], "PFX") == 0
2547 || STRCMP(items[0], "SFX") == 0)
2548 && aff_todo > 0
2549 && STRCMP(cur_aff->ah_key, items[1]) == 0
2550 && itemcnt == 5)
2551 {
2552 affentry_T *aff_entry;
2553
2554 /* New item for an affix letter. */
2555 --aff_todo;
2556 aff_entry = (affentry_T *)alloc_clear(
2557 (unsigned)sizeof(affentry_T));
2558 if (aff_entry == NULL)
2559 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00002560
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002561 if (STRCMP(items[2], "0") != 0)
2562 aff_entry->ae_chop = vim_strsave(items[2]);
2563 if (STRCMP(items[3], "0") != 0)
2564 aff_entry->ae_add = vim_strsave(items[3]);
2565 if (STRCMP(items[4], ".") != 0)
2566 {
2567 char_u buf[MAXLINELEN];
2568
2569 aff_entry->ae_cond = vim_strsave(items[4]);
2570 if (*items[0] == 'P')
2571 sprintf((char *)buf, "^%s", items[4]);
2572 else
2573 sprintf((char *)buf, "%s$", items[4]);
2574 aff_entry->ae_prog = vim_regcomp(buf, RE_MAGIC + RE_STRING);
2575 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00002576
2577 if (ascii && (has_non_ascii(aff_entry->ae_chop)
2578 || has_non_ascii(aff_entry->ae_add)))
2579 {
2580 /* Don't use an affix entry with non-ASCII characters when
2581 * "ascii" is TRUE. */
2582 free_affix_entry(aff_entry);
2583 }
2584 else
2585 {
2586 aff_entry->ae_next = cur_aff->ah_first;
2587 cur_aff->ah_first = aff_entry;
2588 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002589 }
2590 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
2591 /* Ignore REP count */;
2592 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
2593 {
2594 repentry_T *rp;
2595
2596 /* REP item */
2597 if (ga_grow(&aff->af_rep, 1) == FAIL)
2598 break;
2599 rp = ((repentry_T *)aff->af_rep.ga_data) + aff->af_rep.ga_len;
2600 rp->re_from = vim_strsave(items[1]);
2601 rp->re_to = vim_strsave(items[2]);
2602 ++aff->af_rep.ga_len;
2603 }
2604 else if (p_verbose > 0)
2605 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
2606 fname, lnum, items[0]);
2607 }
2608
2609 }
2610
2611 vim_free(pc);
2612 fclose(fd);
2613 return aff;
2614}
2615
2616/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00002617 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
2618 * When "s" is NULL FALSE is returned.
2619 */
2620 static int
2621has_non_ascii(s)
2622 char_u *s;
2623{
2624 char_u *p;
2625
2626 if (s != NULL)
2627 for (p = s; *p != NUL; ++p)
2628 if (*p >= 128)
2629 return TRUE;
2630 return FALSE;
2631}
2632
2633/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002634 * Free the structure filled by spell_read_aff().
2635 */
2636 static void
2637spell_free_aff(aff)
2638 afffile_T *aff;
2639{
2640 hashtab_T *ht;
2641 hashitem_T *hi;
2642 int todo;
2643 int i;
2644 repentry_T *rp;
2645 affheader_T *ah;
2646
2647 vim_free(aff->af_enc);
2648 vim_free(aff->af_try);
2649
2650 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
2651 {
2652 todo = ht->ht_used;
2653 for (hi = ht->ht_array; todo > 0; ++hi)
2654 {
2655 if (!HASHITEM_EMPTY(hi))
2656 {
2657 --todo;
2658 ah = HI2AH(hi);
2659 free_affixentries(ah->ah_first);
2660 vim_free(ah);
2661 }
2662 }
2663 if (ht == &aff->af_suff)
2664 break;
2665 }
2666 hash_clear(&aff->af_pref);
2667 hash_clear(&aff->af_suff);
2668
2669 for (i = 0; i < aff->af_rep.ga_len; ++i)
2670 {
2671 rp = ((repentry_T *)aff->af_rep.ga_data) + i;
2672 vim_free(rp->re_from);
2673 vim_free(rp->re_to);
2674 }
2675 ga_clear(&aff->af_rep);
2676
2677 vim_free(aff);
2678}
2679
2680/*
2681 * Read a dictionary ".dic" file.
2682 * Returns OK or FAIL;
2683 * Each entry in the hashtab_T is a dicword_T.
2684 */
2685 static int
Bram Moolenaar5482f332005-04-17 20:18:43 +00002686spell_read_dic(ht, fname, conv, ascii)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002687 hashtab_T *ht;
2688 char_u *fname;
2689 vimconv_T *conv; /* info for encoding conversion */
Bram Moolenaar5482f332005-04-17 20:18:43 +00002690 int ascii; /* only accept ASCII words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002691{
2692 char_u line[MAXLINELEN];
2693 char_u *p;
2694 dicword_T *dw;
2695 char_u *pc;
2696 char_u *w;
2697 int l;
2698 hash_T hash;
2699 hashitem_T *hi;
2700 FILE *fd;
2701 int lnum = 1;
2702
2703 fd = fopen((char *)fname, "r");
2704 if (fd == NULL)
2705 {
2706 EMSG2(_(e_notopen), fname);
2707 return FAIL;
2708 }
2709
2710 smsg((char_u *)_("Reading dictionary file %s..."), fname);
2711 out_flush();
2712
2713 /* Read and ignore the first line: word count. */
2714 (void)vim_fgets(line, MAXLINELEN, fd);
2715 if (!isdigit(*skipwhite(line)))
2716 EMSG2(_("E760: No word count in %s"), fname);
2717
2718 /*
2719 * Read all the lines in the file one by one.
2720 * The words are converted to 'encoding' here, before being added to
2721 * the hashtable.
2722 */
2723 while (!vim_fgets(line, MAXLINELEN, fd))
2724 {
2725 ++lnum;
2726
2727 /* Remove CR, LF and white space from end. */
2728 l = STRLEN(line);
2729 while (l > 0 && line[l - 1] <= ' ')
2730 --l;
2731 if (l == 0)
2732 continue; /* empty line */
2733 line[l] = NUL;
2734
2735 /* Find the optional affix names. */
2736 p = vim_strchr(line, '/');
2737 if (p != NULL)
2738 *p++ = NUL;
2739
Bram Moolenaar5482f332005-04-17 20:18:43 +00002740 /* Skip non-ASCII words when "ascii" is TRUE. */
2741 if (ascii && has_non_ascii(line))
2742 continue;
2743
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002744 /* Convert from "SET" to 'encoding' when needed. */
2745 if (conv->vc_type != CONV_NONE)
2746 {
2747 pc = string_convert(conv, line, NULL);
2748 w = pc;
2749 }
2750 else
2751 {
2752 pc = NULL;
2753 w = line;
2754 }
2755
2756 dw = (dicword_T *)alloc_clear((unsigned)sizeof(dicword_T)
2757 + STRLEN(w));
2758 if (dw == NULL)
2759 break;
2760 STRCPY(dw->dw_word, w);
2761 vim_free(pc);
2762
2763 hash = hash_hash(dw->dw_word);
2764 hi = hash_lookup(ht, dw->dw_word, hash);
2765 if (!HASHITEM_EMPTY(hi))
2766 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
2767 fname, lnum, line);
2768 else
2769 hash_add_item(ht, hi, dw->dw_word, hash);
2770
2771 if (p != NULL)
2772 dw->dw_affnm = vim_strsave(p);
2773 }
2774
2775 fclose(fd);
2776 return OK;
2777}
2778
2779/*
2780 * Free the structure filled by spell_read_dic().
2781 */
2782 static void
2783spell_free_dic(dic)
2784 hashtab_T *dic;
2785{
2786 int todo;
2787 dicword_T *dw;
2788 hashitem_T *hi;
2789
2790 todo = dic->ht_used;
2791 for (hi = dic->ht_array; todo > 0; ++hi)
2792 {
2793 if (!HASHITEM_EMPTY(hi))
2794 {
2795 --todo;
2796 dw = HI2DW(hi);
2797 vim_free(dw->dw_affnm);
2798 vim_free(dw);
2799 }
2800 }
2801 hash_clear(dic);
2802}
2803
2804/*
2805 * Take the affixes read by spell_read_aff() and add them to the new list.
2806 * Attempts to re-use the same number for identical affixes (ignoring the
2807 * condition, since we remove that). That is especially important when using
2808 * multiple regions.
2809 * Returns OK or FAIL;
2810 */
2811 static int
Bram Moolenaar5482f332005-04-17 20:18:43 +00002812get_new_aff(oldaff, gap, prefix)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002813 hashtab_T *oldaff; /* hashtable with affheader_T */
2814 garray_T *gap; /* table with new affixes */
Bram Moolenaar5482f332005-04-17 20:18:43 +00002815 int prefix; /* TRUE when doing prefixes, FALSE for
2816 suffixes */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002817{
2818 int oldtodo;
2819 affheader_T *oldah, *newah, *gapah;
2820 affentry_T *oldae, *newae;
2821 hashitem_T *oldhi;
2822 hashitem_T *hi;
2823 hashtab_T condht; /* conditions already found */
2824 char_u condkey[MAXLINELEN];
2825 int newnr;
2826 int gapnr;
2827 int retval = OK;
2828 char_u *p;
2829 garray_T tga;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00002830 int preword;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002831
2832 /*
2833 * Loop over all the old affix names.
2834 */
2835 oldtodo = oldaff->ht_used;
2836 for (oldhi = oldaff->ht_array; oldtodo > 0 && retval == OK; ++oldhi)
2837 {
2838 if (!HASHITEM_EMPTY(oldhi))
2839 {
2840 --oldtodo;
2841 oldah = (affheader_T *)oldhi->hi_key;
2842
2843 /* Put entries with the same condition under the same new affix
2844 * nr in "tga". Use hashtable "condht" to find them. */
2845 ga_init2(&tga, sizeof(affheader_T), 10);
2846 hash_init(&condht);
2847
2848 /*
2849 * Loop over all affixes with the same name.
2850 * The affixes with the same condition will get the same number,
2851 * since they can be used with the same words.
2852 * 1. build the lists of new affentry_T, with the headers in "tga".
2853 * 2. Check if some of the lists already exist in "gap", re-use
2854 * their number.
2855 * 3. Assign the new numbers to the old affixes.
2856 */
2857
2858 /* 1. build the lists of new affentry_T. */
2859 for (oldae = oldah->ah_first; oldae != NULL && retval == OK;
2860 oldae = oldae->ae_next)
2861 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00002862 preword = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002863 oldae->ae_add_nw = NULL;
Bram Moolenaar5482f332005-04-17 20:18:43 +00002864 oldae->ae_add_pw = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002865 if (oldae->ae_add != NULL)
2866 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00002867 /* Check for non-word characters in the affix. If there
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00002868 * is one a suffix will be turned into an addition, a
2869 * prefix may be turned into a leadstring.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002870 * This is stored with the old affix, that is where
2871 * trans_affixes() will check. */
2872 for (p = oldae->ae_add; *p != NUL; mb_ptr_adv(p))
2873 if (!spell_iswordc(p))
Bram Moolenaar5482f332005-04-17 20:18:43 +00002874 {
2875 oldae->ae_add_nw = p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002876 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00002877 }
2878
2879 if (prefix && oldae->ae_add_nw != NULL)
2880 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00002881 /* If a prefix has non-word characters special
2882 * treatment is necessary:
2883 * - If it has only non-word characters it becomes a
2884 * leadstring.
2885 * - If it has a sequence of word characters followed
2886 * by a non-word char it becomes a "preword": "d'",
2887 * "de-", "d'ai", etc.
2888 * - if it has another mix of word and non-word
2889 * characters the part before the last word char
2890 * becomes a leadstring: "'d", etc.
2891 */
Bram Moolenaar5482f332005-04-17 20:18:43 +00002892 for (p = oldae->ae_add; *p != NUL; mb_ptr_adv(p))
2893 if (spell_iswordc(p))
2894 {
2895 oldae->ae_add_pw = p;
2896 break;
2897 }
2898 if (oldae->ae_add_pw != NULL)
2899 {
2900 /* Mixed prefix, set ae_add_nw to first non-word
2901 * char after ae_add_pw (if there is one). */
2902 oldae->ae_add_nw = NULL;
2903 for ( ; *p != NUL; mb_ptr_adv(p))
2904 if (!spell_iswordc(p))
2905 {
2906 oldae->ae_add_nw = p;
2907 break;
2908 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00002909 if (oldae->ae_add_nw != NULL)
2910 {
2911 preword = TRUE;
2912 oldae->ae_add_pw = NULL;
2913 oldae->ae_add_nw = NULL;
2914 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00002915 }
2916 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002917 }
2918
2919 if (oldae->ae_cond == NULL)
2920 /* hashtable requires a non-empty key */
2921 STRCPY(condkey, "---");
2922 else
2923 STRCPY(condkey, oldae->ae_cond);
2924
2925 /* Look for an existing list with this name and condition. */
2926 hi = hash_find(&condht, condkey);
2927 if (!HASHITEM_EMPTY(hi))
2928 /* Match with existing affix, use that one. */
2929 newnr = HI2AS(hi)->as_nr;
2930 else
2931 {
2932 /* Add a new affix number. */
2933 newnr = tga.ga_len;
2934 if (ga_grow(&tga, 1) == FAIL)
2935 retval = FAIL;
2936 else
2937 {
2938 newah = ((affheader_T *)tga.ga_data) + newnr;
2939 newah->ah_combine = oldah->ah_combine;
2940 newah->ah_first = NULL;
2941 ++tga.ga_len;
2942
2943 /* Add the new list to the condht hashtable. */
2944 add_affhash(&condht, condkey, newnr);
2945 }
2946 }
2947
2948 /* Add the new affentry_T to the list. */
2949 newah = ((affheader_T *)tga.ga_data) + newnr;
2950 newae = (affentry_T *)alloc_clear((unsigned)sizeof(affentry_T));
2951 if (newae == NULL)
2952 retval = FAIL;
2953 else
2954 {
2955 newae->ae_next = newah->ah_first;
2956 newah->ah_first = newae;
2957 if (oldae->ae_chop == NULL)
2958 newae->ae_chop = NULL;
2959 else
2960 newae->ae_chop = vim_strsave(oldae->ae_chop);
2961 if (oldae->ae_add == NULL)
2962 newae->ae_add = NULL;
2963 else
2964 newae->ae_add = vim_strsave(oldae->ae_add);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00002965 newae->ae_preword = preword;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002966
2967 /* The condition is not copied, since the new affix is
2968 * only used for words where the condition matches. */
2969 }
2970 }
2971
2972 /* 2. Check if some of the lists already exist, re-use their
2973 * number. Otherwise add the list to "gap". */
2974 for (newnr = 0; newnr < tga.ga_len; ++newnr)
2975 {
2976 newah = ((affheader_T *)tga.ga_data) + newnr;
2977 for (gapnr = 0; gapnr < gap->ga_len; ++gapnr)
2978 {
2979 gapah = ((affheader_T *)gap->ga_data) + gapnr;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00002980 if (newah->ah_combine == gapah->ah_combine
2981 && same_affentries(newah, gapah))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002982 /* Found an existing affheader_T entry with same
2983 * affentry_T list, use its number. */
2984 break;
2985 }
2986
2987 newah->ah_affnr = gapnr;
2988 if (gapnr == gap->ga_len)
2989 {
2990 /* This is a new affentry_T list, add it. */
2991 if (ga_grow(gap, 1) == FAIL)
2992 retval = FAIL;
2993 else
2994 {
2995 *(((affheader_T *)gap->ga_data) + gap->ga_len) = *newah;
2996 ++gap->ga_len;
2997 }
2998 }
2999 else
3000 {
3001 /* free unused affentry_T list */
3002 free_affixentries(newah->ah_first);
3003 }
3004 }
3005
3006 /* 3. Assign the new affix numbers to the old affixes. */
3007 for (oldae = oldah->ah_first; oldae != NULL && retval == OK;
3008 oldae = oldae->ae_next)
3009 {
3010 if (oldae->ae_cond == NULL)
3011 /* hashtable requires a non-empty key */
3012 STRCPY(condkey, "---");
3013 else
3014 STRCPY(condkey, oldae->ae_cond);
3015
3016 /* Look for an existing affix with this name and condition. */
3017 hi = hash_find(&condht, condkey);
3018 if (!HASHITEM_EMPTY(hi))
3019 /* Match with existing affix, use that one. */
3020 newnr = HI2AS(hi)->as_nr;
3021 else
3022 {
3023 EMSG(_(e_internal));
3024 retval = FAIL;
3025 }
3026 newah = ((affheader_T *)tga.ga_data) + newnr;
3027 oldae->ae_affnr = newah->ah_affnr;
3028 }
3029
3030 ga_clear(&tga);
3031 clear_affhash(&condht);
3032 }
3033 }
3034
3035 return retval;
3036}
3037
3038/*
3039 * Return TRUE if the affentry_T lists for "ah1" and "ah2" contain the same
3040 * items, ignoring the order.
3041 * Only compares the chop and add strings, not the condition.
3042 */
3043 static int
3044same_affentries(ah1, ah2)
3045 affheader_T *ah1;
3046 affheader_T *ah2;
3047{
3048 affentry_T *ae1, *ae2;
3049
3050 /* Check the length of the lists first. */
3051 ae2 = ah2->ah_first;
3052 for (ae1 = ah1->ah_first; ae1 != NULL; ae1 = ae1->ae_next)
3053 {
3054 if (ae2 == NULL)
3055 return FALSE; /* "ah1" list is longer */
3056 ae2 = ae2->ae_next;
3057 }
3058 if (ae2 != NULL)
3059 return FALSE; /* "ah2" list is longer */
3060
3061 /* Check that each entry in "ah1" appears in "ah2". */
3062 for (ae1 = ah1->ah_first; ae1 != NULL; ae1 = ae1->ae_next)
3063 {
3064 for (ae2 = ah2->ah_first; ae2 != NULL; ae2 = ae2->ae_next)
3065 {
3066 if ((ae1->ae_chop == NULL) == (ae2->ae_chop == NULL)
3067 && (ae1->ae_add == NULL) == (ae2->ae_add == NULL)
3068 && (ae1->ae_chop == NULL
3069 || STRCMP(ae1->ae_chop, ae2->ae_chop) == 0)
3070 && (ae1->ae_add == NULL
3071 || STRCMP(ae1->ae_add, ae2->ae_add) == 0))
3072 break;
3073 }
3074 if (ae2 == NULL)
3075 return FALSE;
3076 }
3077
3078 return TRUE;
3079}
3080
3081/*
3082 * Add a chop/add or cond hashtable entry.
3083 */
3084 static void
3085add_affhash(ht, key, newnr)
3086 hashtab_T *ht;
3087 char_u *key;
3088 int newnr;
3089{
3090 affhash_T *as;
3091
3092 as = (affhash_T *)alloc((unsigned)sizeof(affhash_T) + STRLEN(key));
3093 if (as != NULL)
3094 {
3095 as->as_nr = newnr;
3096 STRCPY(as->as_word, key);
3097 hash_add(ht, as->as_word);
3098 }
3099}
3100
3101/*
3102 * Clear the chop/add hashtable used to detect identical affixes.
3103 */
3104 static void
3105clear_affhash(ht)
3106 hashtab_T *ht;
3107{
3108 int todo;
3109 hashitem_T *hi;
3110
3111 todo = ht->ht_used;
3112 for (hi = ht->ht_array; todo > 0; ++hi)
3113 {
3114 if (!HASHITEM_EMPTY(hi))
3115 {
3116 --todo;
3117 vim_free(HI2AS(hi));
3118 }
3119 }
3120 hash_clear(ht);
3121}
3122
3123/*
3124 * Translate list of affix names for an old word to affix numbers in a new
3125 * basic word.
3126 * This checks if the conditions match with the old word. The result is that
3127 * the new affix does not need to store the condition.
3128 */
3129 static void
3130trans_affixes(dw, bw, oldaff, newwords)
3131 dicword_T *dw; /* old word */
3132 basicword_T *bw; /* basic word */
3133 afffile_T *oldaff; /* affixes for "oldwords" */
3134 hashtab_T *newwords; /* table with words */
3135{
3136 char_u key[2];
3137 char_u *p;
3138 char_u *affnm;
3139 garray_T *gap;
3140 hashitem_T *aff_hi;
3141 affheader_T *ah;
3142 affentry_T *ae;
3143 regmatch_T regmatch;
3144 int i;
3145 basicword_T *nbw;
3146 int alen;
3147 int wlen;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003148 garray_T suffixga; /* list of words with non-word suffixes */
3149 garray_T prefixga; /* list of words with non-word prefixes */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003150 char_u nword[MAXWLEN];
3151 int flags;
3152 int n;
3153
Bram Moolenaar5482f332005-04-17 20:18:43 +00003154 ga_init2(&suffixga, (int)sizeof(basicword_T *), 5);
3155 ga_init2(&prefixga, (int)sizeof(basicword_T *), 5);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003156
3157 /* Loop over all the affix names of the old word. */
3158 key[1] = NUL;
3159 for (affnm = dw->dw_affnm; *affnm != NUL; ++affnm)
3160 {
3161 key[0] = *affnm;
3162 aff_hi = hash_find(&oldaff->af_pref, key);
3163 if (!HASHITEM_EMPTY(aff_hi))
3164 gap = &bw->bw_prefix; /* found a prefix */
3165 else
3166 {
3167 gap = &bw->bw_suffix; /* must be a suffix */
3168 aff_hi = hash_find(&oldaff->af_suff, key);
3169 if (HASHITEM_EMPTY(aff_hi))
3170 {
3171 smsg((char_u *)_("No affix entry '%s' for word %s"),
3172 key, dw->dw_word);
3173 continue;
3174 }
3175 }
3176
3177 /* Loop over all the affix entries for this affix name. */
3178 ah = HI2AH(aff_hi);
3179 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3180 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003181 /* Setup for regexp matching. Note that we don't ignore case.
3182 * This is weird, because he rules in an .aff file don't care
3183 * about case, but it's necessary for compatibility with Myspell.
3184 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003185 regmatch.regprog = ae->ae_prog;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003186 regmatch.rm_ic = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003187 if (ae->ae_prog == NULL
3188 || vim_regexec(&regmatch, dw->dw_word, (colnr_T)0))
3189 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003190 if ((ae->ae_add_nw != NULL || ae->ae_add_pw != NULL)
3191 && (gap != &bw->bw_suffix || bw->bw_addstring == NULL))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003192 {
3193 /* Affix has a non-word character and isn't prepended to
3194 * leader or appended to addition. Need to use another
3195 * word with an addition. It's a copy of the basicword_T
3196 * "bw". */
3197 if (gap == &bw->bw_suffix)
3198 {
3199 alen = ae->ae_add_nw - ae->ae_add;
3200 nbw = (basicword_T *)alloc((unsigned)(
3201 sizeof(basicword_T) + STRLEN(bw->bw_word)
3202 + alen + 1));
3203 if (nbw != NULL)
3204 {
3205 *nbw = *bw;
3206 ga_init2(&nbw->bw_prefix, sizeof(short_u), 1);
3207 ga_init2(&nbw->bw_suffix, sizeof(short_u), 1);
3208
3209 /* Adding the suffix may change the caps. */
3210 STRCPY(nword, dw->dw_word);
3211 if (ae->ae_chop != NULL)
3212 {
3213 /* Remove chop string. */
3214 p = nword + STRLEN(nword);
3215 for (i = mb_charlen(ae->ae_chop); i > 0; --i)
3216 mb_ptr_back(nword, p);
3217 *p = NUL;
3218 }
3219 STRCAT(nword, ae->ae_add);
3220 flags = captype(nword, nword + STRLEN(nword));
3221 if (flags & BWF_KEEPCAP)
3222 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003223 /* "caseword" excludes the addition */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003224 nword[STRLEN(dw->dw_word) + alen] = NUL;
3225 nbw->bw_caseword = vim_strsave(nword);
3226 }
3227 nbw->bw_flags &= ~(BWF_ONECAP | BWF_ALLCAP
3228 | BWF_KEEPCAP);
3229 nbw->bw_flags |= flags;
3230
3231 if (bw->bw_leadstring != NULL)
3232 nbw->bw_leadstring =
3233 vim_strsave(bw->bw_leadstring);
3234 nbw->bw_addstring = vim_strsave(ae->ae_add_nw);
3235
3236 STRCPY(nbw->bw_word, bw->bw_word);
3237 if (alen > 0 || ae->ae_chop != NULL)
3238 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003239 /* Suffix starts with word character and/or
3240 * chop off something. Append it to the word.
3241 * Add new word entry. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003242 wlen = STRLEN(nbw->bw_word);
3243 if (ae->ae_chop != NULL)
3244 wlen -= STRLEN(ae->ae_chop);
3245 mch_memmove(nbw->bw_word + wlen, ae->ae_add,
3246 alen);
3247 nbw->bw_word[wlen + alen] = NUL;
3248 add_to_wordlist(newwords, nbw);
3249 }
3250 else
3251 /* Basic word is the same, link "nbw" after
3252 * "bw". */
3253 bw->bw_next = nbw;
3254
3255 /* Remember this word, we need to set bw_prefix
Bram Moolenaar5482f332005-04-17 20:18:43 +00003256 * and bw_prefix later. */
3257 if (ga_grow(&suffixga, 1) == OK)
3258 ((basicword_T **)suffixga.ga_data)
3259 [suffixga.ga_len++] = nbw;
3260 }
3261 }
3262 else if (ae->ae_add_nw == NULL)
3263 {
3264 /* Prefix that starts with non-word char(s) and may be
3265 * followed by word chars: Make a leadstring and
3266 * prepend word chars before the word. */
3267 alen = STRLEN(ae->ae_add_pw);
3268 nbw = (basicword_T *)alloc((unsigned)(
3269 sizeof(basicword_T) + STRLEN(bw->bw_word)
3270 + alen + 1));
3271 if (nbw != NULL)
3272 {
3273 *nbw = *bw;
3274 ga_init2(&nbw->bw_prefix, sizeof(short_u), 1);
3275 ga_init2(&nbw->bw_suffix, sizeof(short_u), 1);
3276
3277 /* Adding the prefix may change the caps. */
3278 STRCPY(nword, ae->ae_add);
3279 p = dw->dw_word;
3280 if (ae->ae_chop != NULL)
3281 /* Skip chop string. */
3282 for (i = mb_charlen(ae->ae_chop); i > 0; --i)
3283 mb_ptr_adv( p);
3284 STRCAT(nword, p);
3285
3286 flags = captype(nword, nword + STRLEN(nword));
3287 if (flags & BWF_KEEPCAP)
3288 /* "caseword" excludes the addition */
3289 nbw->bw_caseword = vim_strsave(nword
3290 + (ae->ae_add_pw - ae->ae_add));
3291 else
3292 nbw->bw_caseword = NULL;
3293 nbw->bw_flags &= ~(BWF_ONECAP | BWF_ALLCAP
3294 | BWF_KEEPCAP);
3295 nbw->bw_flags |= flags;
3296
3297 if (bw->bw_addstring != NULL)
3298 nbw->bw_addstring =
3299 vim_strsave(bw->bw_addstring);
3300 else
3301 nbw->bw_addstring = NULL;
3302 nbw->bw_leadstring = vim_strnsave(ae->ae_add,
3303 ae->ae_add_pw - ae->ae_add);
3304
3305 if (alen > 0 || ae->ae_chop != NULL)
3306 {
3307 /* Prefix ends in word character and/or chop
3308 * off something. Prepend it to the word.
3309 * Add new word entry. */
3310 STRCPY(nbw->bw_word, ae->ae_add_pw);
3311 p = bw->bw_word;
3312 if (ae->ae_chop != NULL)
3313 p += STRLEN(ae->ae_chop);
3314 STRCAT(nbw->bw_word, p);
3315 add_to_wordlist(newwords, nbw);
3316 }
3317 else
3318 {
3319 /* Basic word is the same, link "nbw" after
3320 * "bw". */
3321 STRCPY(nbw->bw_word, bw->bw_word);
3322 bw->bw_next = nbw;
3323 }
3324
3325 /* Remember this word, we need to set bw_suffix
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003326 * and bw_suffix later. */
Bram Moolenaar5482f332005-04-17 20:18:43 +00003327 if (ga_grow(&prefixga, 1) == OK)
3328 ((basicword_T **)prefixga.ga_data)
3329 [prefixga.ga_len++] = nbw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003330 }
3331 }
3332 else
3333 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003334 /* Prefix with both non-word and word characters: Turn
3335 * prefix into basic word, original word becomes an
3336 * addstring. */
3337
3338 /* Fold-case the word characters in the prefix into
3339 * nword[]. */
3340 alen = 0;
3341 for (p = ae->ae_add_pw; p < ae->ae_add_nw; p += n)
3342 {
3343#ifdef FEAT_MBYTE
3344 n = (*mb_ptr2len_check)(p);
3345#else
3346 n = 1;
3347#endif
3348 (void)str_foldcase(p, n, nword + alen,
3349 MAXWLEN - alen);
3350 alen += STRLEN(nword + alen);
3351 }
3352
3353 /* Allocate a new word entry. */
3354 nbw = (basicword_T *)alloc((unsigned)(
3355 sizeof(basicword_T) + alen + 1));
3356 if (nbw != NULL)
3357 {
3358 *nbw = *bw;
3359 ga_init2(&nbw->bw_prefix, sizeof(short_u), 1);
3360 ga_init2(&nbw->bw_suffix, sizeof(short_u), 1);
3361
3362 mch_memmove(nbw->bw_word, nword, alen);
3363 nbw->bw_word[alen] = NUL;
3364
3365 /* Use the cap type of the prefix. */
3366 alen = ae->ae_add_nw - ae->ae_add_pw;
3367 mch_memmove(nword, ae->ae_add_pw, alen);
3368 nword[alen] = NUL;
3369 flags = captype(nword, nword + STRLEN(nword));
3370 if (flags & BWF_KEEPCAP)
3371 nbw->bw_caseword = vim_strsave(nword);
3372 else
3373 nbw->bw_caseword = NULL;
3374 nbw->bw_flags &= ~(BWF_ONECAP | BWF_ALLCAP
3375 | BWF_KEEPCAP);
3376 nbw->bw_flags |= flags;
3377
3378 /* The addstring is the prefix after the word
3379 * characters, the original word excluding "chop",
3380 * plus any addition. */
3381 STRCPY(nword, ae->ae_add_nw);
3382 p = bw->bw_word;
3383 if (ae->ae_chop != NULL)
3384 p += STRLEN(ae->ae_chop);
3385 STRCAT(nword, p);
3386 if (bw->bw_addstring != NULL)
3387 STRCAT(nword, bw->bw_addstring);
3388 nbw->bw_addstring = vim_strsave(nword);
3389
3390 if (ae->ae_add_pw > ae->ae_add)
3391 nbw->bw_leadstring = vim_strnsave(ae->ae_add,
3392 ae->ae_add_pw - ae->ae_add);
3393 else
3394 nbw->bw_leadstring = NULL;
3395
3396 add_to_wordlist(newwords, nbw);
3397
3398 /* Remember this word, we need to set bw_suffix
3399 * and bw_suffix later. */
3400 if (ga_grow(&prefixga, 1) == OK)
3401 ((basicword_T **)prefixga.ga_data)
3402 [prefixga.ga_len++] = nbw;
3403 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003404 }
3405 }
3406 else
3407 {
3408 /* Affix applies to this word, add the related affix
3409 * number. But only if it's not there yet. And keep the
3410 * list sorted, so that we can compare it later. */
3411 for (i = 0; i < gap->ga_len; ++i)
3412 {
3413 n = ((short_u *)gap->ga_data)[i];
3414 if (n >= ae->ae_affnr)
3415 {
3416 if (n == ae->ae_affnr)
3417 i = -1;
3418 break;
3419 }
3420 }
3421 if (i >= 0 && ga_grow(gap, 1) == OK)
3422 {
3423 if (i < gap->ga_len)
3424 mch_memmove(((short_u *)gap->ga_data) + i + 1,
3425 ((short_u *)gap->ga_data) + i,
3426 sizeof(short_u) * (gap->ga_len - i));
3427 ((short_u *)gap->ga_data)[i] = ae->ae_affnr;
3428 ++gap->ga_len;
3429 }
3430 }
3431 }
3432 }
3433 }
3434
3435 /*
3436 * For the words that we added for suffixes with non-word characters: Use
3437 * the prefix list of the main word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003438 */
Bram Moolenaar5482f332005-04-17 20:18:43 +00003439 for (i = 0; i < suffixga.ga_len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003440 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003441 nbw = ((basicword_T **)suffixga.ga_data)[i];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003442 if (ga_grow(&nbw->bw_prefix, bw->bw_prefix.ga_len) == OK)
3443 {
3444 mch_memmove(nbw->bw_prefix.ga_data, bw->bw_prefix.ga_data,
3445 bw->bw_prefix.ga_len * sizeof(short_u));
3446 nbw->bw_prefix.ga_len = bw->bw_prefix.ga_len;
3447 }
3448 }
3449
Bram Moolenaar5482f332005-04-17 20:18:43 +00003450 /*
3451 * For the words that we added for prefixes with non-word characters: Use
3452 * the suffix list of the main word.
3453 */
3454 for (i = 0; i < prefixga.ga_len; ++i)
3455 {
3456 nbw = ((basicword_T **)prefixga.ga_data)[i];
3457 if (ga_grow(&nbw->bw_suffix, bw->bw_suffix.ga_len) == OK)
3458 {
3459 mch_memmove(nbw->bw_suffix.ga_data, bw->bw_suffix.ga_data,
3460 bw->bw_suffix.ga_len * sizeof(short_u));
3461 nbw->bw_suffix.ga_len = bw->bw_suffix.ga_len;
3462 }
3463 }
3464
3465 ga_clear(&suffixga);
3466 ga_clear(&prefixga);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003467}
3468
3469/*
3470 * Go over all words in "oldwords" and change the old affix names to the new
3471 * affix numbers, check the conditions, fold case, extract the basic word and
3472 * additions.
3473 */
3474 static int
3475build_wordlist(newwords, oldwords, oldaff, regionmask)
3476 hashtab_T *newwords; /* basicword_T entries */
3477 hashtab_T *oldwords; /* dicword_T entries */
3478 afffile_T *oldaff; /* affixes for "oldwords" */
3479 int regionmask; /* value for bw_region */
3480{
3481 int todo;
3482 hashitem_T *old_hi;
3483 dicword_T *dw;
3484 basicword_T *bw;
3485 char_u foldword[MAXLINELEN];
3486 int leadlen;
3487 char_u leadstring[MAXLINELEN];
3488 int addlen;
3489 char_u addstring[MAXLINELEN];
3490 int dwlen;
3491 char_u *p;
3492 int clen;
3493 int flags;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003494 char_u *cp = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003495 int l;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003496 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003497
3498 todo = oldwords->ht_used;
3499 for (old_hi = oldwords->ht_array; todo > 0; ++old_hi)
3500 {
3501 if (!HASHITEM_EMPTY(old_hi))
3502 {
3503 --todo;
3504 dw = HI2DW(old_hi);
3505
3506 /* This takes time, print a message now and then. */
Bram Moolenaar5482f332005-04-17 20:18:43 +00003507 if ((todo & 0x3ff) == 0 || todo == (int)oldwords->ht_used - 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003508 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003509 sprintf((char *)message, _("%6d todo - %s"),
3510 todo, dw->dw_word);
3511 msg_start();
3512 msg_outtrans_attr(message, 0);
3513 msg_clr_eos();
3514 msg_didout = FALSE;
3515 msg_col = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003516 out_flush();
3517 ui_breakcheck();
3518 if (got_int)
3519 break;
3520 }
3521
3522 /* The basic words are always stored with folded case. */
3523 dwlen = STRLEN(dw->dw_word);
3524 (void)str_foldcase(dw->dw_word, dwlen, foldword, MAXLINELEN);
3525 flags = captype(dw->dw_word, dw->dw_word + dwlen);
3526
3527 /* Check for non-word characters before the word. */
3528 clen = 0;
3529 leadlen = 0;
3530 if (!spell_iswordc(foldword))
3531 {
3532 p = foldword;
3533 for (;;)
3534 {
3535 mb_ptr_adv(p);
3536 ++clen;
3537 if (*p == NUL) /* Only non-word chars (bad word!) */
3538 {
3539 if (p_verbose > 0)
3540 smsg((char_u *)_("Warning: word without word characters: \"%s\""),
3541 foldword);
3542 break;
3543 }
3544 if (spell_iswordc(p))
3545 {
3546 /* Move the leader to "leadstring" and remove it from
3547 * "foldword". */
3548 leadlen = p - foldword;
3549 mch_memmove(leadstring, foldword, leadlen);
3550 leadstring[leadlen] = NUL;
3551 mch_memmove(foldword, p, STRLEN(p) + 1);
3552 break;
3553 }
3554 }
3555 }
3556
3557 /* Check for non-word characters after word characters. */
3558 addlen = 0;
3559 for (p = foldword; spell_iswordc(p); mb_ptr_adv(p))
3560 {
3561 if (*p == NUL)
3562 break;
3563 ++clen;
3564 }
3565 if (*p != NUL)
3566 {
3567 /* Move the addition to "addstring" and truncate "foldword". */
3568 if (flags & BWF_KEEPCAP)
3569 {
3570 /* Preserve caps, need to skip the right number of
3571 * characters in the original word (case folding may
3572 * change the byte count). */
3573 l = 0;
3574 for (cp = dw->dw_word; l < clen; mb_ptr_adv(cp))
3575 ++l;
3576 addlen = STRLEN(cp);
3577 mch_memmove(addstring, cp, addlen + 1);
3578 }
3579 else
3580 {
3581 addlen = STRLEN(p);
3582 mch_memmove(addstring, p, addlen + 1);
3583 }
3584 *p = NUL;
3585 }
3586
3587 bw = (basicword_T *)alloc_clear((unsigned)sizeof(basicword_T)
3588 + STRLEN(foldword));
3589 if (bw == NULL)
3590 break;
3591 STRCPY(bw->bw_word, foldword);
3592 bw->bw_region = regionmask;
3593
3594 if (leadlen > 0)
3595 bw->bw_leadstring = vim_strsave(leadstring);
3596 else
3597 bw->bw_leadstring = NULL;
3598 if (addlen > 0)
3599 bw->bw_addstring = vim_strsave(addstring);
3600 else
3601 bw->bw_addstring = NULL;
3602
3603 add_to_wordlist(newwords, bw);
3604
3605 if (flags & BWF_KEEPCAP)
3606 {
3607 if (addlen == 0)
3608 /* use the whole word */
3609 bw->bw_caseword = vim_strsave(dw->dw_word + leadlen);
3610 else
3611 /* use only up to the addition */
3612 bw->bw_caseword = vim_strnsave(dw->dw_word + leadlen,
3613 cp - dw->dw_word - leadlen);
3614 if (bw->bw_caseword == NULL) /* out of memory */
3615 flags &= ~BWF_KEEPCAP;
3616 }
3617 bw->bw_flags = flags;
3618
3619 /* Deal with any affix names on the old word, translate them
3620 * into affix numbers. */
3621 ga_init2(&bw->bw_prefix, sizeof(short_u), 10);
3622 ga_init2(&bw->bw_suffix, sizeof(short_u), 10);
3623 if (dw->dw_affnm != NULL)
3624 trans_affixes(dw, bw, oldaff, newwords);
3625 }
3626 }
3627 if (todo > 0)
3628 return FAIL;
3629 return OK;
3630}
3631
3632/*
3633 * Go through the list of words and combine the ones that are identical except
3634 * for the region.
3635 */
3636 static void
3637combine_regions(newwords)
3638 hashtab_T *newwords;
3639{
3640 int todo;
3641 hashitem_T *hi;
3642 basicword_T *bw, *nbw, *pbw;
3643
3644 /* Loop over all basic words in the words table. */
3645 todo = newwords->ht_used;
3646 for (hi = newwords->ht_array; todo > 0; ++hi)
3647 {
3648 if (!HASHITEM_EMPTY(hi))
3649 {
3650 --todo;
3651
3652 /* Loop over the list of words for this basic word. Compare with
3653 * each following word in the same list. */
3654 for (bw = HI2BW(hi); bw != NULL; bw = bw->bw_next)
3655 {
3656 pbw = bw;
3657 for (nbw = pbw->bw_next; nbw != NULL; nbw = pbw->bw_next)
3658 {
3659 if (bw->bw_flags == nbw->bw_flags
3660 && (bw->bw_leadstring == NULL)
3661 == (nbw->bw_leadstring == NULL)
3662 && (bw->bw_addstring == NULL)
3663 == (nbw->bw_addstring == NULL)
3664 && ((bw->bw_flags & BWF_KEEPCAP) == 0
3665 || (STRCMP(bw->bw_caseword,
3666 nbw->bw_caseword) == 0))
3667 && (bw->bw_leadstring == NULL
3668 || (STRCMP(bw->bw_leadstring,
3669 nbw->bw_leadstring) == 0))
3670 && (bw->bw_addstring == NULL
3671 || (STRCMP(bw->bw_addstring,
3672 nbw->bw_addstring) == 0))
3673 && same_affixes(bw, nbw)
3674 )
3675 {
3676 /* Match, combine regions and delete "nbw". */
3677 pbw->bw_next = nbw->bw_next;
3678 bw->bw_region |= nbw->bw_region;
3679 free_basicword(nbw);
3680 }
3681 else
3682 /* No match, continue with next one. */
3683 pbw = nbw;
3684 }
3685 }
3686 }
3687 }
3688}
3689
3690/*
3691 * Return TRUE when the prefixes and suffixes for "bw" and "nbw" are equal.
3692 */
3693 static int
3694same_affixes(bw, nbw)
3695 basicword_T *bw;
3696 basicword_T *nbw;
3697{
3698 return (bw->bw_prefix.ga_len == nbw->bw_prefix.ga_len
3699 && bw->bw_suffix.ga_len == nbw->bw_suffix.ga_len
3700 && (bw->bw_prefix.ga_len == 0
3701 || vim_memcmp(bw->bw_prefix.ga_data,
3702 nbw->bw_prefix.ga_data,
3703 bw->bw_prefix.ga_len * sizeof(short_u)) == 0)
3704 && (bw->bw_suffix.ga_len == 0
3705 || vim_memcmp(bw->bw_suffix.ga_data,
3706 nbw->bw_suffix.ga_data,
3707 bw->bw_suffix.ga_len * sizeof(short_u)) == 0));
3708}
3709
3710/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003711 * For each basic word with additions turn the suffixes into other additions
3712 * and/or new basic words. For each basic word with a leadstring turn the
3713 * prefixes into other leadstrings and/or new basic words.
3714 * The result is that no affixes apply to the additions or leadstring of a
3715 * word.
3716 * This is also needed when a word with an addition has a prefix and the word
3717 * with prefix also exists. E.g., "blurp's/D" (D is prefix "de") and
3718 * "deblurp". "deblurp" would match and no prefix would be tried.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003719 */
3720 static void
3721expand_affixes(newwords, prefgap, suffgap)
3722 hashtab_T *newwords;
3723 garray_T *prefgap;
3724 garray_T *suffgap;
3725{
3726 int todo;
3727 hashitem_T *hi;
3728 basicword_T *bw;
3729 int pi, si;
3730 affentry_T *pae, *sae;
3731 garray_T add_words;
3732 int n;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003733 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003734
3735 ga_init2(&add_words, sizeof(basicword_T *), 10);
3736
3737 todo = newwords->ht_used;
3738 for (hi = newwords->ht_array; todo > 0; ++hi)
3739 {
3740 if (!HASHITEM_EMPTY(hi))
3741 {
3742 --todo;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003743
3744 /* This takes time, print a message now and then. */
3745 if ((todo & 0x3ff) == 0 || todo == (int)newwords->ht_used - 1)
3746 {
3747 sprintf((char *)message, _("%6d todo - %s"),
3748 todo, HI2BW(hi)->bw_word);
3749 msg_start();
3750 msg_outtrans_attr(message, 0);
3751 msg_clr_eos();
3752 msg_didout = FALSE;
3753 msg_col = 0;
3754 out_flush();
3755 ui_breakcheck();
3756 if (got_int)
3757 break;
3758 }
3759
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003760 for (bw = HI2BW(hi); bw != NULL; bw = bw->bw_next)
3761 {
3762 /*
3763 * Need to fix affixes if there is a leader or addition and
3764 * there are prefixes or suffixes.
3765 */
3766 if ((bw->bw_leadstring != NULL || bw->bw_addstring != NULL)
3767 && (bw->bw_prefix.ga_len != 0
3768 || bw->bw_suffix.ga_len != 0))
3769 {
3770 /* Loop over all prefix numbers, but first without a
3771 * prefix. */
3772 for (pi = -1; pi < bw->bw_prefix.ga_len; ++pi)
3773 {
3774 pae = NULL;
3775 if (pi >= 0)
3776 {
3777 n = ((short_u *)bw->bw_prefix.ga_data)[pi];
3778 pae = ((affheader_T *)prefgap->ga_data + n)
3779 ->ah_first;
3780 }
3781
3782 /* Loop over all entries for prefix "pi". Do it once
3783 * when there is no prefix (pi == -1). */
3784 do
3785 {
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003786 /* Skip prewords, they don't need to be expanded. */
3787 if (pae == NULL || !pae->ae_preword)
3788 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003789 /* Loop over all suffix numbers. Do without a
3790 * suffix first when there is a prefix. */
3791 for (si = (pi == -1 ? 0 : -1);
3792 si < bw->bw_suffix.ga_len; ++si)
3793 {
3794 sae = NULL;
3795 if (si >= 0)
3796 {
3797 n = ((short_u *)bw->bw_suffix.ga_data)[si];
3798 sae = ((affheader_T *)suffgap->ga_data + n)
3799 ->ah_first;
3800 }
3801
3802 /* Loop over all entries for suffix "si". Do
3803 * it once when there is no suffix (si == -1).
3804 */
3805 do
3806 {
3807 /* Expand the word for this combination of
3808 * prefixes and affixes. */
3809 expand_one_aff(bw, &add_words, pae, sae);
3810
3811 /* Advance to next suffix entry, if there
3812 * is one. */
3813 if (sae != NULL)
3814 sae = sae->ae_next;
3815 } while (sae != NULL);
3816 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003817 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003818
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003819 /* Advance to next prefix entry, if there is one. */
3820 if (pae != NULL)
3821 pae = pae->ae_next;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003822 } while (pae != NULL);
3823 }
3824 }
3825 }
3826 }
3827 }
3828
3829 /*
3830 * Add the new words afterwards, can't change "newwords" while going over
3831 * all its items.
3832 */
3833 for (pi = 0; pi < add_words.ga_len; ++pi)
3834 add_to_wordlist(newwords, ((basicword_T **)add_words.ga_data)[pi]);
3835
3836 ga_clear(&add_words);
3837}
3838
3839/*
3840 * Add one word to "add_words" for basic word "bw" with additions, adding
3841 * prefix "pae" and suffix "sae". Either "pae" or "sae" can be NULL.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003842 * Don't do this when not necessary:
3843 * - no leadstring and adding prefix doesn't result in existing word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003844 */
3845 static void
3846expand_one_aff(bw, add_words, pae, sae)
3847 basicword_T *bw;
3848 garray_T *add_words;
3849 affentry_T *pae;
3850 affentry_T *sae;
3851{
3852 char_u word[MAXWLEN + 1];
3853 char_u caseword[MAXWLEN + 1];
3854 int l = 0;
3855 int choplen = 0;
3856 int ll;
3857 basicword_T *nbw;
3858
3859 /* Prepend prefix to the basic word if there is a prefix and there is no
3860 * leadstring. */
3861 if (pae != NULL && bw->bw_leadstring == NULL)
3862 {
3863 if (pae->ae_add != NULL)
3864 {
3865 l = STRLEN(pae->ae_add);
3866 mch_memmove(word, pae->ae_add, l);
3867 }
3868 if (pae->ae_chop != NULL)
3869 choplen = STRLEN(pae->ae_chop);
3870 }
3871
3872 /* Copy the body of the word. */
3873 STRCPY(word + l, bw->bw_word + choplen);
3874
3875 /* Do the same for bw_caseword, if it's there. */
3876 if (bw->bw_flags & BWF_KEEPCAP)
3877 {
3878 if (l > 0)
3879 mch_memmove(caseword, pae->ae_add, l);
3880 STRCPY(caseword + l, bw->bw_caseword + choplen);
3881 }
3882
3883 /* Append suffix to the basic word if there is a suffix and there is no
3884 * addstring. */
3885 if (sae != 0 && bw->bw_addstring == NULL)
3886 {
3887 l = STRLEN(word);
3888 if (sae->ae_chop != NULL)
3889 l -= STRLEN(sae->ae_chop);
3890 if (sae->ae_add == NULL)
3891 word[l] = NUL;
3892 else
3893 STRCPY(word + l, sae->ae_add);
3894
3895 if (bw->bw_flags & BWF_KEEPCAP)
3896 {
3897 /* Do the same for the caseword. */
3898 l = STRLEN(caseword);
3899 if (sae->ae_chop != NULL)
3900 l -= STRLEN(sae->ae_chop);
3901 if (sae->ae_add == NULL)
3902 caseword[l] = NUL;
3903 else
3904 STRCPY(caseword + l, sae->ae_add);
3905 }
3906 }
3907
3908 nbw = (basicword_T *)alloc_clear((unsigned)
3909 sizeof(basicword_T) + STRLEN(word));
3910 if (nbw != NULL)
3911 {
3912 /* Add the new word to the list of words to be added later. */
3913 if (ga_grow(add_words, 1) == FAIL)
3914 {
3915 vim_free(nbw);
3916 return;
3917 }
3918 ((basicword_T **)add_words->ga_data)[add_words->ga_len++] = nbw;
3919
3920 /* Copy the (modified) basic word, flags and region. */
3921 STRCPY(nbw->bw_word, word);
3922 nbw->bw_flags = bw->bw_flags;
3923 nbw->bw_region = bw->bw_region;
3924
3925 /* Set the (modified) caseword. */
3926 if (bw->bw_flags & BWF_KEEPCAP)
3927 if ((nbw->bw_caseword = vim_strsave(caseword)) == NULL)
3928 nbw->bw_flags &= ~BWF_KEEPCAP;
3929
3930 if (bw->bw_leadstring != NULL)
3931 {
3932 if (pae != NULL)
3933 {
3934 /* Prepend prefix to leadstring. */
3935 ll = STRLEN(bw->bw_leadstring);
3936 l = choplen = 0;
3937 if (pae->ae_add != NULL)
3938 l = STRLEN(pae->ae_add);
3939 if (pae->ae_chop != NULL)
3940 {
3941 choplen = STRLEN(pae->ae_chop);
3942 if (choplen > ll) /* TODO: error? */
3943 choplen = ll;
3944 }
3945 nbw->bw_leadstring = alloc((unsigned)(ll + l - choplen + 1));
3946 if (nbw->bw_leadstring != NULL)
3947 {
3948 if (l > 0)
3949 mch_memmove(nbw->bw_leadstring, pae->ae_add, l);
3950 STRCPY(nbw->bw_leadstring + l, bw->bw_leadstring + choplen);
3951 }
3952 }
3953 else
3954 nbw->bw_leadstring = vim_strsave(bw->bw_leadstring);
3955 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003956 else if (bw->bw_prefix.ga_len > 0)
3957 {
3958 /* There is no leadstring, copy the list of possible prefixes. */
3959 ga_init2(&nbw->bw_prefix, sizeof(short_u), 1);
3960 if (ga_grow(&nbw->bw_prefix, bw->bw_prefix.ga_len) == OK)
3961 {
3962 mch_memmove(nbw->bw_prefix.ga_data, bw->bw_prefix.ga_data,
3963 bw->bw_prefix.ga_len * sizeof(short_u));
3964 nbw->bw_prefix.ga_len = bw->bw_prefix.ga_len;
3965 }
3966 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003967
3968 if (bw->bw_addstring != NULL)
3969 {
3970 if (sae != NULL)
3971 {
3972 /* Append suffix to addstring. */
3973 l = STRLEN(bw->bw_addstring);
3974 if (sae->ae_chop != NULL)
3975 {
3976 l -= STRLEN(sae->ae_chop);
3977 if (l < 0) /* TODO: error? */
3978 l = 0;
3979 }
3980 if (sae->ae_add == NULL)
3981 ll = 0;
3982 else
3983 ll = STRLEN(sae->ae_add);
3984 nbw->bw_addstring = alloc((unsigned)(ll + l - choplen + 1));
3985 if (nbw->bw_addstring != NULL)
3986 {
3987 STRCPY(nbw->bw_addstring, bw->bw_addstring);
3988 if (sae->ae_add == NULL)
3989 nbw->bw_addstring[l] = NUL;
3990 else
3991 STRCPY(nbw->bw_addstring + l, sae->ae_add);
3992 }
3993 }
3994 else
3995 nbw->bw_addstring = vim_strsave(bw->bw_addstring);
3996 }
3997 }
3998}
3999
4000/*
4001 * Add basicword_T "*bw" to wordlist "newwords".
4002 */
4003 static void
4004add_to_wordlist(newwords, bw)
4005 hashtab_T *newwords;
4006 basicword_T *bw;
4007{
4008 hashitem_T *hi;
4009 basicword_T *bw2;
4010
4011 hi = hash_find(newwords, bw->bw_word);
4012 if (HASHITEM_EMPTY(hi))
4013 {
4014 /* New entry, add to hashlist. */
4015 hash_add(newwords, bw->bw_word);
4016 bw->bw_next = NULL;
4017 }
4018 else
4019 {
4020 /* Existing entry, append to list of basic words. */
4021 bw2 = HI2BW(hi);
4022 bw->bw_next = bw2->bw_next;
4023 bw2->bw_next = bw;
4024 }
4025}
4026
4027/*
4028 * Write a number to file "fd", MSB first, in "len" bytes.
4029 */
4030 static void
4031put_bytes(fd, nr, len)
4032 FILE *fd;
4033 long_u nr;
4034 int len;
4035{
4036 int i;
4037
4038 for (i = len - 1; i >= 0; --i)
4039 putc((int)(nr >> (i * 8)), fd);
4040}
4041
4042/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004043 * Write affix info. <affitemcnt> <affitem> ...
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004044 */
4045 static void
4046write_affix(fd, ah)
4047 FILE *fd;
4048 affheader_T *ah;
4049{
4050 int i = 0;
4051 affentry_T *ae;
4052 char_u *p;
4053 int round;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004054 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004055
4056 /* Count the number of entries. */
4057 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
4058 ++i;
4059 put_bytes(fd, (long_u)i, 2); /* <affitemcnt> */
4060
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004061 /* <affitem>: <affflags> <affchoplen> <affchop> <affaddlen> <affadd> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004062 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004063 {
4064 flags = ah->ah_combine ? AFF_COMBINE : 0;
4065 if (ae->ae_preword)
4066 flags |= AFF_PREWORD;
4067 fputc(flags, fd); /* <affflags> */
4068
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004069 for (round = 1; round <= 2; ++round)
4070 {
4071 p = round == 1 ? ae->ae_chop : ae->ae_add;
4072 if (p == NULL)
4073 putc(0, fd); /* <affchoplen> / <affaddlen> */
4074 else
4075 {
4076 putc(STRLEN(p), fd); /* <affchoplen> / <affaddlen> */
4077 /* <affchop> / <affadd> */
4078 fwrite(p, STRLEN(p), (size_t)1, fd);
4079 }
4080 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004081 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004082}
4083
4084/*
4085 * Write list of affix NRs: <affixcnt> <affixNR> ...
4086 */
4087 static void
4088write_affixlist(fd, aff, bytes)
4089 FILE *fd;
4090 garray_T *aff;
4091 int bytes;
4092{
4093 int i;
4094
4095 if (aff->ga_len > 0)
4096 {
4097 putc(aff->ga_len, fd); /* <affixcnt> */
4098 for (i = 0; i < aff->ga_len; ++i)
4099 put_bytes(fd, (long_u )((short_u *)aff->ga_data)[i], bytes);
4100 }
4101}
4102
4103/*
4104 * Vim spell file format: <HEADER> <PREFIXLIST> <SUFFIXLIST>
4105 * <SUGGEST> <WORDLIST>
4106 *
4107 * <HEADER>: <fileID> <regioncnt> <regionname> ...
4108 *
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004109 * <fileID> 10 bytes "VIMspell03"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004110 * <regioncnt> 1 byte number of regions following (8 supported)
4111 * <regionname> 2 bytes Region name: ca, au, etc.
4112 * First <regionname> is region 1.
4113 *
4114 *
4115 * <PREFIXLIST>: <affcount> <afftotcnt> <affix> ...
4116 * <SUFFIXLIST>: <affcount> <afftotcnt> <affix> ...
4117 * list of possible affixes: prefixes and suffixes.
4118 *
4119 * <affcount> 2 bytes Number of affixes (MSB comes first).
4120 * When more than 256 an affixNR is 2 bytes.
4121 * This is separate for prefixes and suffixes!
4122 * First affixNR is 0.
4123 * <afftotcnt> 2 bytes Total number of affix items (MSB comes first).
4124 *
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004125 * <affix>: <affitemcnt> <affitem> ...
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004126 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004127 * <affitemcnt> 2 bytes Number of affixes with this affixNR (MSB first).
4128 *
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004129 * <affitem>: <affflags> <affchoplen> <affchop> <affaddlen> <affadd>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004130 *
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004131 * <affflags> 1 byte 0x01: prefix combines with suffix, AFF_COMBINE
4132 * 0x02: prefix includes word, AFF_PREWORD
4133 * 0x04-0x80: unset
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004134 * <affchoplen> 1 byte Length of <affchop> in bytes.
4135 * <affchop> N bytes To be removed from basic word.
4136 * <affaddlen> 1 byte Length of <affadd> in bytes.
4137 * <affadd> N bytes To be added to basic word.
4138 *
4139 *
4140 * <SUGGEST> : <suggestlen> <more> ...
4141 *
4142 * <suggestlen> 4 bytes Length of <SUGGEST> in bytes, excluding
4143 * <suggestlen>. MSB first.
4144 * <more> To be defined.
4145 *
4146 *
4147 * <WORDLIST>: <wordcount> <worditem> ...
4148 *
4149 * <wordcount> 4 bytes Number of <worditem> following. MSB first.
4150 *
4151 * <worditem>: <nr> <string> <flags> [<flags2>]
4152 * [<caselen> <caseword>]
4153 * [<affixcnt> <affixNR> ...] (prefixes)
4154 * [<affixcnt> <affixNR> ...] (suffixes)
4155 * [<region>]
4156 * [<addcnt> <add> ...]
4157 *
4158 * <nr> i 1 byte Number of bytes copied from previous word.
4159 * <string> N bytes Additional bytes for word, up to byte smaller than
4160 * 0x20 (space).
4161 * Must only contain case-folded word characters.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004162 * <flags> 1 byte 0x01: word is valid without addition, BWF_VALID
4163 * 0x02: has region byte, BWF_REGION
4164 * 0x04: first letter must be upper-case, BWF_ONECAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004165 * 0x08: has suffixes, <affixcnt> and <affixNR> follow
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004166 * BWF_SUFFIX
4167 * 0x10: more flags, <flags2> follows next, BWF_SECOND
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004168 * 0x20-0x80: can't be used, unset
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004169 * <flags2> 1 byte 0x01: has additions, <addcnt> and <add> follow,
4170 * BWF_ADDS
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004171 * 0x02: has prefixes, <affixcnt> and <affixNR> follow
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004172 * BWF_PREFIX
4173 * 0x04: all letters must be upper-case, BWF_ALLCAP
4174 * 0x08: case must match, BWF_KEEPCAP
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004175 * 0x10: has more than 255 additions, <addcnt> is two
4176 * bytes, BWF_ADDS_M
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004177 * 0x10-0x80: unset
4178 * <caselen> 1 byte Length of <caseword>.
4179 * <caseword> N bytes Word with matching case.
4180 * <affixcnt> 1 byte Number of affix NRs following.
4181 * <affixNR> 1 or 2 byte Number of possible affix for this word.
4182 * When using 2 bytes MSB comes first.
4183 * <region> 1 byte Bitmask for regions in which word is valid. When
4184 * omitted it's valid in all regions.
4185 * Lowest bit is for region 1.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004186 * <addcnt> 1 or 2 byte Number of <add> items following.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004187 *
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004188 * <add>: <addflags> <addlen> [<leadlen>] [<copylen>] [<addstring>] [<region>]
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004189 *
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004190 * <addflags> 1 byte 0x01: unset
4191 * 0x02: has region byte, ADD_REGION
4192 * 0x04: first letter must be upper-case, ADD_ONECAP
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004193 * 0x08: unset
4194 * 0x10: has a <leadlen>, ADD_LEADLEN
4195 * 0x20: has a <copylen>, ADD_COPYLEN
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004196 * 0x40: all letters must be upper-case, ADD_ALLCAP
4197 * 0x80: fixed case, <addstring> is the whole word
4198 * with matching case, ADD_KEEPCAP.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004199 * <addlen> 1 byte Length of <addstring> in bytes.
4200 * <leadlen> 1 byte Number of bytes at start of <addstring> that must
4201 * come before the start of the basic word.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004202 * <copylen> 1 byte Number of bytes copied from previous <addstring>.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004203 * <addstring> N bytes Word characters, before/in/after the word.
4204 *
4205 * All text characters are in 'encoding': <affchop>, <affadd>, <string>,
4206 * <caseword>> and <addstring>.
4207 * All other fields are ASCII: <regionname>
4208 * <string> is always case-folded.
4209 */
4210
4211/*
4212 * Write the Vim spell file "fname".
4213 */
4214 static void
4215write_vim_spell(fname, prefga, suffga, newwords, regcount, regchars)
4216 char_u *fname;
4217 garray_T *prefga; /* prefixes, affheader_T entries */
4218 garray_T *suffga; /* suffixes, affheader_T entries */
4219 hashtab_T *newwords; /* basic words, basicword_T entries */
4220 int regcount; /* number of regions */
4221 char_u *regchars; /* region names */
4222{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004223 winfo_T wif;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004224 garray_T *gap;
4225 hashitem_T *hi;
4226 char_u **wtab;
4227 int todo;
4228 int flags, aflags;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004229 basicword_T *bw, *bwf, *bw2 = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004230 int i;
4231 int cnt;
4232 affentry_T *ae;
4233 int round;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004234 garray_T bwga;
4235
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004236 vim_memset(&wif, 0, sizeof(winfo_T));
4237
4238 wif.wif_fd = fopen((char *)fname, "w");
4239 if (wif.wif_fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004240 {
4241 EMSG2(_(e_notopen), fname);
4242 return;
4243 }
4244
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004245 fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, wif.wif_fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004246
4247 /* write the region names if there is more than one */
4248 if (regcount > 1)
4249 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004250 putc(regcount, wif.wif_fd);
4251 fwrite(regchars, (size_t)(regcount * 2), (size_t)1, wif.wif_fd);
4252 wif.wif_regionmask = (1 << regcount) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004253 }
4254 else
4255 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004256 putc(0, wif.wif_fd);
4257 wif.wif_regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004258 }
4259
4260 /* Write the prefix and suffix lists. */
4261 for (round = 1; round <= 2; ++round)
4262 {
4263 gap = round == 1 ? prefga : suffga;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004264 put_bytes(wif.wif_fd, (long_u)gap->ga_len, 2); /* <affcount> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004265
4266 /* Count the total number of affix items. */
4267 cnt = 0;
4268 for (i = 0; i < gap->ga_len; ++i)
4269 for (ae = ((affheader_T *)gap->ga_data + i)->ah_first;
4270 ae != NULL; ae = ae->ae_next)
4271 ++cnt;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004272 put_bytes(wif.wif_fd, (long_u)cnt, 2); /* <afftotcnt> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004273
4274 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004275 write_affix(wif.wif_fd, (affheader_T *)gap->ga_data + i);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004276 }
4277
4278 /* Number of bytes used for affix NR depends on affix count. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004279 wif.wif_prefm = (prefga->ga_len > 256) ? 2 : 1;
4280 wif.wif_suffm = (suffga->ga_len > 256) ? 2 : 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004281
4282 /* Write the suggest info. TODO */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004283 put_bytes(wif.wif_fd, 0L, 4);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004284
4285 /*
4286 * Write the word list. <wordcount> <worditem> ...
4287 */
4288 /* number of basic words in 4 bytes */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004289 put_bytes(wif.wif_fd, newwords->ht_used, 4); /* <wordcount> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004290
4291 /*
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004292 * Sort the word list, so that we can copy as many bytes as possible from
4293 * the previous word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004294 */
4295 wtab = (char_u **)alloc((unsigned)(sizeof(char_u *) * newwords->ht_used));
4296 if (wtab != NULL)
4297 {
4298 /* Make a table with pointers to each word. */
4299 todo = newwords->ht_used;
4300 for (hi = newwords->ht_array; todo > 0; ++hi)
4301 if (!HASHITEM_EMPTY(hi))
4302 wtab[--todo] = hi->hi_key;
4303
4304 /* Sort. */
4305 sort_strings(wtab, (int)newwords->ht_used);
4306
4307 /* Now write each basic word to the spell file. */
4308 ga_init2(&bwga, sizeof(basicword_T *), 10);
Bram Moolenaar5482f332005-04-17 20:18:43 +00004309 for (todo = 0; (long_u)todo < newwords->ht_used; ++todo)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004310 {
4311 bwf = KEY2BW(wtab[todo]);
4312
4313 /*
4314 * Reorder the list of basicword_T words: make a list for words
4315 * with the same case-folded word. Put them together for same
4316 * caps (ONECAP, ALLCAP and various KEEPCAP words) and same
4317 * affixes. Each list will then be put as a basic word with
4318 * additions.
4319 * This won't take much space, since the basic word is the same
4320 * every time, only its length is written.
4321 */
4322 bwga.ga_len = 0;
4323 for (bw = bwf; bw != NULL; bw = bw->bw_next)
4324 {
4325 flags = bw->bw_flags & (BWF_ONECAP | BWF_KEEPCAP | BWF_ALLCAP);
4326
4327 /* Go through the lists we found so far. Break when the case
4328 * matches. */
4329 for (i = 0; i < bwga.ga_len; ++i)
4330 {
4331 bw2 = ((basicword_T **)bwga.ga_data)[i];
4332 aflags = bw2->bw_flags & (BWF_ONECAP | BWF_KEEPCAP
4333 | BWF_ALLCAP);
4334 if (flags == aflags
4335 && ((flags & BWF_KEEPCAP) == 0
4336 || (STRCMP(bw->bw_caseword,
4337 bw2->bw_caseword) == 0))
4338 && same_affixes(bw, bw2))
4339 break;
4340 }
4341 if (i == bwga.ga_len)
4342 {
4343 /* No word with similar caps, make a new list. */
4344 if (ga_grow(&bwga, 1) == FAIL)
4345 break;
4346 ((basicword_T **)bwga.ga_data)[i] = bw;
4347 bw->bw_cnext = NULL;
4348 ++bwga.ga_len;
4349 }
4350 else
4351 {
4352 /* Add to list of words with similar caps. */
4353 bw->bw_cnext = bw2->bw_cnext;
4354 bw2->bw_cnext = bw;
4355 }
4356 }
4357
4358 /* Prefer the word with no caps to use as the first basic word.
4359 * At least one without KEEPCAP. */
4360 bw = NULL;
4361 for (i = 0; i < bwga.ga_len; ++i)
4362 {
4363 bw2 = ((basicword_T **)bwga.ga_data)[i];
4364 if (bw == NULL
4365 || (bw2->bw_flags & (BWF_ONECAP | BWF_KEEPCAP
4366 | BWF_ALLCAP)) == 0
4367 || (bw->bw_flags & BWF_KEEPCAP))
4368 bw = bw2;
4369 }
4370
4371 /* Write first basic word. If it's KEEPCAP then we need a word
4372 * without VALID flag first (makes it easier to read the list back
4373 * in). */
4374 if (bw->bw_flags & BWF_KEEPCAP)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004375 write_bword(&wif, bw, TRUE);
4376 write_bword(&wif, bw, FALSE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004377
4378 /* Write other basic words, with different caps. */
4379 for (i = 0; i < bwga.ga_len; ++i)
4380 {
4381 bw2 = ((basicword_T **)bwga.ga_data)[i];
4382 if (bw2 != bw)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004383 write_bword(&wif, bw2, FALSE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004384 }
4385 }
4386
4387 ga_clear(&bwga);
4388 }
4389
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004390 fclose(wif.wif_fd);
4391
4392 /* Print a few statistics. */
4393 if (wif.wif_addmaxw == NULL)
4394 wif.wif_addmaxw = (char_u *)"";
4395 smsg((char_u *)_("Maximum number of adds on a word: %ld (%s)"),
4396 wif.wif_addmax, wif.wif_addmaxw);
4397 smsg((char_u *)_("Average number of adds on a word: %f"),
4398 (float)wif.wif_acount / (float)wif.wif_wcount);
4399}
4400
4401/*
4402 * Compare two basic words for their <addstring>.
4403 */
4404static int
4405#ifdef __BORLANDC__
4406_RTLENTRYF
4407#endif
4408bw_compare __ARGS((const void *s1, const void *s2));
4409
4410 static int
4411#ifdef __BORLANDC__
4412_RTLENTRYF
4413#endif
4414bw_compare(s1, s2)
4415 const void *s1;
4416 const void *s2;
4417{
4418 basicword_T *bw1 = *(basicword_T **)s1;
4419 basicword_T *bw2 = *(basicword_T **)s2;
4420 int i = 0;
4421
4422 /* compare the leadstrings */
4423 if (bw1->bw_leadstring == NULL)
4424 {
4425 if (bw2->bw_leadstring != NULL)
4426 return 1;
4427 }
4428 else if (bw2->bw_leadstring == NULL)
4429 return -1;
4430 else
4431 i = STRCMP(bw1->bw_leadstring, bw2->bw_leadstring);
4432
4433 if (i == 0)
4434 {
4435 /* leadstrings are identical, compare the addstrings */
4436 if (bw1->bw_addstring == NULL)
4437 {
4438 if (bw2->bw_addstring != NULL)
4439 return 1;
4440 }
4441 else if (bw2->bw_addstring == NULL)
4442 return -1;
4443 else
4444 i = STRCMP(bw1->bw_addstring, bw2->bw_addstring);
4445 }
4446 return i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004447}
4448
4449/*
4450 * Write basic word, followed by any additions.
4451 *
4452 * <worditem>: <nr> <string> <flags> [<flags2>]
4453 * [<caselen> <caseword>]
4454 * [<affixcnt> <affixNR> ...] (prefixes)
4455 * [<affixcnt> <affixNR> ...] (suffixes)
4456 * [<region>]
4457 * [<addcnt> <add> ...]
4458 */
4459 static void
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004460write_bword(wif, bwf, lowcap)
4461 winfo_T *wif; /* info for writing */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004462 basicword_T *bwf;
4463 int lowcap; /* write KEEPKAP word as not-valid */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004464{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004465 FILE *fd = wif->wif_fd;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004466 int flags;
4467 int aflags;
4468 int len;
4469 int leadlen, addlen;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004470 int copylen;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004471 int clen;
4472 int adds = 0;
4473 int i;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004474 int idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004475 basicword_T *bw, *bw2;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004476 basicword_T **wtab;
4477 int count;
4478 int l;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004479
4480 /* Check how many bytes can be copied from the previous word. */
4481 len = STRLEN(bwf->bw_word);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004482 if (wif->wif_prevbw == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004483 clen = 0;
4484 else
4485 for (clen = 0; clen < len
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004486 && wif->wif_prevbw->bw_word[clen] == bwf->bw_word[clen]; ++clen)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004487 ;
4488 putc(clen, fd); /* <nr> */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004489 wif->wif_prevbw = bwf;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004490 /* <string> */
4491 if (len > clen)
4492 fwrite(bwf->bw_word + clen, (size_t)(len - clen), (size_t)1, fd);
4493
4494 /* Try to find a word without additions to use first. */
4495 bw = bwf;
4496 for (bw2 = bwf; bw2 != NULL; bw2 = bw2->bw_cnext)
4497 {
4498 if (bw2->bw_addstring != NULL || bw2->bw_leadstring != NULL)
4499 ++adds;
4500 else
4501 bw = bw2;
4502 }
4503
4504 /* Flags: If there is no leadstring and no addstring the basic word is
4505 * valid, may have prefixes, suffixes and region. */
4506 flags = bw->bw_flags;
4507 if (bw->bw_addstring == NULL && bw->bw_leadstring == NULL)
4508 {
4509 flags |= BWF_VALID;
4510
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004511 /* Flags: add the region byte if the word isn't valid in all
4512 * regions. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004513 if (wif->wif_regionmask != 0 && (bw->bw_region & wif->wif_regionmask)
4514 != wif->wif_regionmask)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004515 flags |= BWF_REGION;
4516 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004517 /* Add the prefix/suffix list if there are prefixes/suffixes. */
4518 if (bw->bw_leadstring == NULL && bw->bw_prefix.ga_len > 0)
4519 flags |= BWF_PREFIX;
4520 if (bw->bw_addstring == NULL && bw->bw_suffix.ga_len > 0)
4521 flags |= BWF_SUFFIX;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004522
4523 /* Flags: may have additions. */
4524 if (adds > 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004525 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004526 flags |= BWF_ADDS;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004527 if (adds >= 256)
4528 flags |= BWF_ADDS_M;
4529 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004530
4531 /* The dummy word before a KEEPCAP word doesn't have any flags, they are
4532 * in the actual word that follows. */
4533 if (lowcap)
4534 flags = 0;
4535
4536 /* Flags: when the upper byte is not used we only write one flags
4537 * byte, if it's used then set an extra flag in the first byte and
4538 * also write the second byte. */
4539 if ((flags & 0xff00) == 0)
4540 putc(flags, fd); /* <flags> */
4541 else
4542 {
4543 putc(flags | BWF_SECOND, fd); /* <flags> */
4544 putc((int)((unsigned)flags >> 8), fd); /* <flags2> */
4545 }
4546
4547 /* First dummy word doesn't need anything but flags. */
4548 if (lowcap)
4549 return;
4550
4551 if (flags & BWF_KEEPCAP)
4552 {
4553 len = STRLEN(bw->bw_caseword);
4554 putc(len, fd); /* <caselen> */
4555 for (i = 0; i < len; ++i)
4556 putc(bw->bw_caseword[i], fd); /* <caseword> */
4557 }
4558
4559 /* write prefix and suffix lists: <affixcnt> <affixNR> ... */
4560 if (flags & BWF_PREFIX)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004561 write_affixlist(fd, &bw->bw_prefix, wif->wif_prefm);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004562 if (flags & BWF_SUFFIX)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004563 write_affixlist(fd, &bw->bw_suffix, wif->wif_suffm);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004564
4565 if (flags & BWF_REGION)
4566 putc(bw->bw_region, fd); /* <region> */
4567
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004568 ++wif->wif_wcount;
4569
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004570 /*
4571 * Additions.
4572 */
4573 if (adds > 0)
4574 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004575 if (adds >= 256)
4576 put_bytes(fd, (long_u)adds, 2); /* 2 byte <addcnt> */
4577 else
4578 putc(adds, fd); /* 1 byte <addcnt> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004579
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004580 /* statistics */
4581 wif->wif_acount += adds;
4582 if (wif->wif_addmax < adds)
4583 {
4584 wif->wif_addmax = adds;
4585 wif->wif_addmaxw = bw->bw_word;
4586 }
4587
4588 /*
4589 * Sort the list of additions, so that we can copy as many bytes as
4590 * possible from the previous addstring.
4591 */
4592
4593 /* Make a table with pointers to each basic word that has additions. */
4594 wtab = (basicword_T **)alloc((unsigned)(sizeof(basicword_T *) * adds));
4595 if (wtab == NULL)
4596 return;
4597 count = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004598 for (bw = bwf; bw != NULL; bw = bw->bw_cnext)
4599 if (bw->bw_leadstring != NULL || bw->bw_addstring != NULL)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004600 wtab[count++] = bw;
4601
4602 /* Sort. */
4603 qsort((void *)wtab, (size_t)count, sizeof(basicword_T *), bw_compare);
4604
4605 /* Now write each basic word to the spell file. Copy bytes from the
4606 * previous leadstring/addstring if possible. */
4607 bw2 = NULL;
4608 for (idx = 0; idx < count; ++idx)
4609 {
4610 bw = wtab[idx];
4611
4612 /* <add>: <addflags> <addlen> [<leadlen>] [<copylen>]
4613 * [<addstring>] [<region>] */
4614 copylen = 0;
4615 if (bw->bw_leadstring == NULL)
4616 leadlen = 0;
4617 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004618 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004619 leadlen = STRLEN(bw->bw_leadstring);
4620 if (bw2 != NULL && bw2->bw_leadstring != NULL)
4621 for ( ; copylen < leadlen; ++copylen)
4622 if (bw->bw_leadstring[copylen]
4623 != bw2->bw_leadstring[copylen])
4624 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004625 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004626 if (bw->bw_addstring == NULL)
4627 addlen = 0;
4628 else
4629 {
4630 addlen = STRLEN(bw->bw_addstring);
4631 if (bw2 != NULL && copylen == leadlen
4632 && bw2->bw_addstring != NULL)
4633 {
4634 for (i = 0; i < addlen; ++i)
4635 if (bw->bw_addstring[i] != bw2->bw_addstring[i])
4636 break;
4637 copylen += i;
4638 }
4639 }
4640
4641 aflags = 0;
4642 /* Only copy bytes when it's more than one, the length itself
4643 * takes an extra byte. */
4644 if (copylen > 1)
4645 aflags |= ADD_COPYLEN;
4646 else
4647 copylen = 0;
4648
4649 if (bw->bw_flags & BWF_ONECAP)
4650 aflags |= ADD_ONECAP;
4651 if (bw->bw_flags & BWF_ALLCAP)
4652 aflags |= ADD_ALLCAP;
4653 if (bw->bw_flags & BWF_KEEPCAP)
4654 aflags |= ADD_KEEPCAP;
4655 if (wif->wif_regionmask != 0 && (bw->bw_region
4656 & wif->wif_regionmask) != wif->wif_regionmask)
4657 aflags |= ADD_REGION;
4658 if (leadlen > 0)
4659 aflags |= ADD_LEADLEN;
4660 putc(aflags, fd); /* <addflags> */
4661
4662 putc(leadlen + addlen, fd); /* <addlen> */
4663 if (aflags & ADD_LEADLEN)
4664 putc(leadlen, fd); /* <leadlen> */
4665 if (aflags & ADD_COPYLEN)
4666 putc(copylen, fd); /* <copylen> */
4667
4668 /* <addstring> */
4669 if (leadlen > copylen && bw->bw_leadstring != NULL)
4670 fwrite(bw->bw_leadstring + copylen,
4671 (size_t)(leadlen - copylen), (size_t)1, fd);
4672 if (leadlen + addlen > copylen && bw->bw_addstring != NULL)
4673 {
4674 if (copylen >= leadlen)
4675 l = copylen - leadlen;
4676 else
4677 l = 0;
4678 fwrite(bw->bw_addstring + l,
4679 (size_t)(addlen - l), (size_t)1, fd);
4680 }
4681
4682 if (aflags & ADD_REGION)
4683 putc(bw->bw_region, fd); /* <region> */
4684
4685 bw2 = bw;
4686 }
4687 vim_free(wtab);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004688 }
4689}
4690
4691
4692/*
4693 * ":mkspell outfile infile ..."
4694 */
4695 void
4696ex_mkspell(eap)
4697 exarg_T *eap;
4698{
4699 int fcount;
4700 char_u **fnames;
4701 char_u fname[MAXPATHL];
4702 char_u wfname[MAXPATHL];
4703 afffile_T *(afile[8]);
4704 hashtab_T dfile[8];
4705 int i;
4706 int len;
4707 char_u region_name[16];
4708 struct stat st;
4709 int round;
4710 vimconv_T conv;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004711 int ascii = FALSE;
4712 char_u *arg = eap->arg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004713
Bram Moolenaar5482f332005-04-17 20:18:43 +00004714 if (STRNCMP(arg, "-ascii", 6) == 0)
4715 {
4716 ascii = TRUE;
4717 arg = skipwhite(arg + 6);
4718 }
4719
4720 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
4721 if (get_arglist_exp(arg, &fcount, &fnames) == FAIL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004722 return;
4723 if (fcount < 2)
4724 EMSG(_(e_invarg)); /* need at least output and input names */
4725 else if (fcount > 9)
4726 EMSG(_("E754: Only up to 8 regions supported"));
4727 else
4728 {
4729 /* Check for overwriting before doing things that may take a lot of
4730 * time. */
Bram Moolenaar5482f332005-04-17 20:18:43 +00004731 sprintf((char *)wfname, "%s.%s.spl", fnames[0],
4732 ascii ? (char_u *)"ascii" : p_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004733 if (!eap->forceit && mch_stat((char *)wfname, &st) >= 0)
4734 {
4735 EMSG(_(e_exists));
4736 goto theend;
4737 }
4738 if (mch_isdir(fnames[0]))
4739 {
4740 EMSG2(_(e_isadir2), fnames[0]);
4741 goto theend;
4742 }
4743
4744 /*
4745 * Init the aff and dic pointers.
4746 * Get the region names if there are more than 2 arguments.
4747 */
4748 for (i = 1; i < fcount; ++i)
4749 {
4750 afile[i - 1] = NULL;
4751 hash_init(&dfile[i - 1]);
4752 if (fcount > 2)
4753 {
4754 len = STRLEN(fnames[i]);
4755 if (STRLEN(gettail(fnames[i])) < 5 || fnames[i][len - 3] != '_')
4756 {
4757 EMSG2(_("E755: Invalid region in %s"), fnames[i]);
4758 goto theend;
4759 }
4760 else
4761 {
4762 region_name[(i - 1) * 2] = TOLOWER_ASC(fnames[i][len - 2]);
4763 region_name[(i - 1) * 2 + 1] =
4764 TOLOWER_ASC(fnames[i][len - 1]);
4765 }
4766 }
4767 }
4768
4769 /*
4770 * Read all the .aff and .dic files.
4771 * Text is converted to 'encoding'.
4772 */
4773 for (i = 1; i < fcount; ++i)
4774 {
4775 /* Read the .aff file. Will init "conv" based on the "SET" line. */
4776 conv.vc_type = CONV_NONE;
4777 sprintf((char *)fname, "%s.aff", fnames[i]);
Bram Moolenaar5482f332005-04-17 20:18:43 +00004778 if ((afile[i - 1] = spell_read_aff(fname, &conv, ascii)) == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004779 break;
4780
4781 /* Read the .dic file. */
4782 sprintf((char *)fname, "%s.dic", fnames[i]);
Bram Moolenaar5482f332005-04-17 20:18:43 +00004783 if (spell_read_dic(&dfile[i - 1], fname, &conv, ascii) == FAIL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004784 break;
4785
4786 /* Free any conversion stuff. */
4787 convert_setup(&conv, NULL, NULL);
4788 }
4789
4790 /* Process the data when all the files could be read. */
4791 if (i == fcount)
4792 {
4793 garray_T prefga;
4794 garray_T suffga;
4795 garray_T *gap;
4796 hashtab_T newwords;
4797
4798 /*
4799 * Combine all the affixes into one new affix list. This is done
4800 * for prefixes and suffixes separately.
4801 * We need to do this for each region, try to re-use the same
4802 * affixes.
4803 * Since we number the new affix entries, a growarray will do. In
4804 * the affheader_T the ah_key is unused.
4805 */
4806 MSG(_("Combining affixes..."));
4807 out_flush();
4808 for (round = 1; round <= 2; ++round)
4809 {
4810 gap = round == 1 ? &prefga : &suffga;
4811 ga_init2(gap, sizeof(affheader_T), 50);
4812 for (i = 1; i < fcount; ++i)
4813 get_new_aff(round == 1 ? &afile[i - 1]->af_pref
Bram Moolenaar5482f332005-04-17 20:18:43 +00004814 : &afile[i - 1]->af_suff,
4815 gap, round == 1);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004816 }
4817
4818 /*
4819 * Go over all words and:
4820 * - change the old affix names to the new affix numbers
4821 * - check the conditions
4822 * - fold case
4823 * - extract the basic word and additions.
4824 * Do this for each region.
4825 */
4826 MSG(_("Building word list..."));
4827 out_flush();
4828 hash_init(&newwords);
4829
4830 for (i = 1; i < fcount; ++i)
4831 build_wordlist(&newwords, &dfile[i - 1], afile[i - 1],
4832 1 << (i - 1));
4833
4834 if (fcount > 2)
4835 {
4836 /* Combine words for the different regions into one. */
4837 MSG(_("Combining regions..."));
4838 out_flush();
4839 combine_regions(&newwords);
4840 }
4841
4842 /*
4843 * Affixes on a word with additions are clumsy, would require
4844 * inefficient searching. Turn the affixes into additions and/or
4845 * the expanded word.
4846 */
4847 MSG(_("Processing words..."));
4848 out_flush();
4849 expand_affixes(&newwords, &prefga, &suffga);
4850
4851 /* Write the info in the spell file. */
4852 smsg((char_u *)_("Writing spell file %s..."), wfname);
4853 out_flush();
4854 write_vim_spell(wfname, &prefga, &suffga, &newwords,
4855 fcount - 1, region_name);
4856 MSG(_("Done!"));
4857 out_flush();
4858
4859 /* Free the allocated stuff. */
4860 free_wordtable(&newwords);
4861 for (round = 1; round <= 2; ++round)
4862 {
4863 gap = round == 1 ? &prefga: &suffga;
4864 for (i = 0; i < gap->ga_len; ++i)
4865 free_affixentries(((affheader_T *)gap->ga_data + i)
4866 ->ah_first);
4867 ga_clear(gap);
4868 }
4869 }
4870
4871 /* Free the .aff and .dic file structures. */
4872 for (i = 1; i < fcount; ++i)
4873 {
4874 if (afile[i - 1] != NULL)
4875 spell_free_aff(afile[i - 1]);
4876 spell_free_dic(&dfile[i - 1]);
4877 }
4878 }
4879
4880theend:
4881 FreeWild(fcount, fnames);
4882}
4883
4884 static void
4885free_wordtable(ht)
4886 hashtab_T *ht;
4887{
4888 int todo;
4889 basicword_T *bw, *nbw;
4890 hashitem_T *hi;
4891
4892 todo = ht->ht_used;
4893 for (hi = ht->ht_array; todo > 0; ++hi)
4894 {
4895 if (!HASHITEM_EMPTY(hi))
4896 {
4897 --todo;
4898 for (bw = HI2BW(hi); bw != NULL; bw = nbw)
4899 {
4900 nbw = bw->bw_next;
4901 free_basicword(bw);
4902 }
4903 }
4904 }
4905}
4906
4907/*
4908 * Free a basicword_T and what it contains.
4909 */
4910 static void
4911free_basicword(bw)
4912 basicword_T *bw;
4913{
4914 ga_clear(&bw->bw_prefix);
4915 ga_clear(&bw->bw_suffix);
4916 vim_free(bw->bw_caseword);
4917 vim_free(bw->bw_leadstring);
4918 vim_free(bw->bw_addstring);
4919 vim_free(bw);
4920}
4921
4922/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00004923 * Free a list of affentry_T and what they contain.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004924 */
4925 static void
4926free_affixentries(first)
4927 affentry_T *first;
4928{
4929 affentry_T *ap, *an;
4930
4931 for (ap = first; ap != NULL; ap = an)
4932 {
4933 an = ap->ae_next;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004934 free_affix_entry(ap);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004935 }
4936}
4937
Bram Moolenaar5482f332005-04-17 20:18:43 +00004938/*
4939 * Free one affentry_T and what it contains.
4940 */
4941 static void
4942free_affix_entry(ap)
4943 affentry_T *ap;
4944{
4945 vim_free(ap->ae_chop);
4946 vim_free(ap->ae_add);
4947 vim_free(ap->ae_cond);
4948 vim_free(ap->ae_prog);
4949 vim_free(ap);
4950}
4951
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004952#endif /* FEAT_MBYTE */
4953
4954#endif /* FEAT_SYN_HL */