223
|
1 /* 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
|
226
|
12 *
|
|
13 * Terminology:
|
|
14 * "dword" is a dictionary word, made out of letters and digits.
|
|
15 * "nword" is a word with a character that's not a letter or digit.
|
|
16 * "word" is either a "dword" or an "nword".
|
223
|
17 */
|
|
18
|
236
|
19 /*
|
|
20 * Why doesn't Vim use aspell/ispell/myspell/etc.?
|
|
21 * See ":help develop-spell".
|
|
22 */
|
|
23
|
223
|
24 #if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
|
|
25 # include <io.h> /* for lseek(), must be before vim.h */
|
|
26 #endif
|
|
27
|
|
28 #include "vim.h"
|
|
29
|
|
30 #if defined(FEAT_SYN_HL) || defined(PROTO)
|
|
31
|
|
32 #ifdef HAVE_FCNTL_H
|
|
33 # include <fcntl.h>
|
|
34 #endif
|
|
35
|
226
|
36 #define MAXWLEN 100 /* assume max. word len is this many bytes */
|
|
37
|
223
|
38 /*
|
|
39 * Structure that is used to store the text from the language file. This
|
236
|
40 * avoids the need to allocate space for each individual word. It's allocated
|
|
41 * in big chunks for speed.
|
|
42 */
|
|
43 #define SBLOCKSIZE 4096 /* default size of sb_data */
|
|
44 typedef struct sblock_S sblock_T;
|
|
45 struct sblock_S
|
|
46 {
|
|
47 sblock_T *sb_next; /* next block in list */
|
|
48 char_u sb_data[1]; /* data, actually longer */
|
|
49 };
|
|
50
|
|
51 /* Info from "REP" entries in ".aff" file used in af_rep. */
|
|
52 typedef struct repentry_S
|
|
53 {
|
|
54 char_u *re_from;
|
|
55 char_u *re_to;
|
|
56 } repentry_T;
|
|
57
|
|
58 /*
|
|
59 * Structure to store affix info.
|
|
60 */
|
|
61 typedef struct affitem_S affitem_T;
|
|
62 struct affitem_S
|
|
63 {
|
|
64 affitem_T *ai_next; /* next affix with same ai_add[] or NULL */
|
|
65 short_u ai_nr; /* affix number */
|
|
66 char_u ai_combine; /* prefix combines with suffix */
|
|
67 char_u ai_choplen; /* length of ai_chop in bytes */
|
|
68 char_u ai_addlen; /* length of ai_add in bytes */
|
|
69 char_u *ai_chop; /* text chopped off basic word (can be NULL) */
|
|
70 char_u ai_add[1]; /* text added to basic word (actually longer) */
|
|
71 };
|
|
72
|
|
73 /* Get affitem_T pointer from hashitem that uses ai_add */
|
|
74 static affitem_T dumai;
|
|
75 #define HI2AI(hi) ((affitem_T *)((hi)->hi_key - (dumai.ai_add - (char_u *)&dumai)))
|
|
76
|
|
77 /*
|
|
78 * Structure used to store words and other info for one language.
|
|
79 */
|
|
80 typedef struct slang_S slang_T;
|
|
81 struct slang_S
|
|
82 {
|
|
83 slang_T *sl_next; /* next language */
|
|
84 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
|
|
85 hashtab_T sl_words; /* main word table, fword_T */
|
|
86 int sl_prefcnt; /* number of prefix NRs */
|
|
87 garray_T sl_preftab; /* list of hashtables to lookup prefixes */
|
|
88 affitem_T *sl_prefzero; /* list of prefixes with zero add length */
|
|
89 int sl_suffcnt; /* number of suffix NRs */
|
|
90 garray_T sl_sufftab; /* list of hashtables to lookup suffixes */
|
|
91 affitem_T *sl_suffzero; /* list of suffixes with zero add length */
|
|
92 char_u *sl_try; /* "TRY" from .aff file */
|
|
93 garray_T sl_rep; /* list of repentry_T entries from REP lines */
|
|
94 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
|
|
95 sblock_T *sl_block; /* list with allocated memory blocks */
|
|
96 int sl_error; /* error while loading */
|
|
97 };
|
|
98
|
|
99 static slang_T *first_lang = NULL;
|
|
100
|
|
101 /*
|
|
102 * Structure to store an addition to a basic word.
|
|
103 */
|
|
104 typedef struct addword_S addword_T;
|
|
105 struct addword_S
|
|
106 {
|
|
107 addword_T *aw_next; /* next addition */
|
|
108 char_u aw_flags; /* ADD_ flags */
|
|
109 char_u aw_leadlen; /* length of lead in bytes */
|
|
110 char_u aw_wordlen; /* length of aw_word in bytes */
|
|
111 char_u aw_region; /* region for word with this addition */
|
|
112 char_u aw_word[1]; /* text, actually longer: case-folded addition
|
|
113 plus, with ADD_KEEPCAP: keep-case addition */
|
|
114 };
|
|
115
|
|
116 /*
|
|
117 * Structure to store a basic word.
|
|
118 */
|
|
119 typedef struct fword_S fword_T;
|
|
120 struct fword_S
|
|
121 {
|
|
122 fword_T *fw_next; /* same basic word with different caps */
|
|
123 char_u fw_region; /* region bits */
|
|
124 char_u fw_prefixcnt; /* number of prefix numbers */
|
|
125 char_u fw_suffixcnt; /* number of suffix numbers */
|
|
126 short_u fw_flags; /* BWF_ flags */
|
|
127 void *fw_prefix; /* table with prefix numbers */
|
|
128 void *fw_suffix; /* table with suffix numbers */
|
|
129 addword_T *fw_adds; /* first addword_T entry */
|
|
130 char_u fw_word[1]; /* actually longer: case folded word, or
|
|
131 keep-case word when (flags & BWF_KEEPCAP) */
|
|
132 };
|
|
133
|
|
134 /* Get fword_T pointer from hashitem that uses fw_word */
|
|
135 static fword_T dumfw;
|
|
136 #define HI2FWORD(hi) ((fword_T *)((hi)->hi_key - (dumfw.fw_word - (char_u *)&dumfw)))
|
|
137
|
|
138 #define REGION_ALL 0xff
|
|
139
|
|
140
|
|
141 /*
|
|
142 * Structure used in "b_langp", filled from 'spelllang'.
|
|
143 */
|
|
144 typedef struct langp_S
|
|
145 {
|
|
146 slang_T *lp_slang; /* info for this language (NULL for last one) */
|
|
147 int lp_region; /* bitmask for region or REGION_ALL */
|
|
148 } langp_T;
|
|
149
|
|
150 #define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
|
|
151
|
|
152 #define SP_OK 0
|
|
153 #define SP_BAD 1
|
|
154 #define SP_RARE 2
|
|
155 #define SP_LOCAL 3
|
|
156
|
|
157 /* flags used for basic words in the spell file */
|
|
158 #define BWF_VALID 0x01 /* word is valid without additions */
|
|
159 #define BWF_REGION 0x02 /* region byte follows */
|
|
160 #define BWF_ONECAP 0x04 /* first letter must be capital */
|
|
161 #define BWF_SUFFIX 0x08 /* has suffix NR list */
|
|
162 #define BWF_SECOND 0x10 /* second flags byte follows */
|
|
163
|
|
164 #define BWF_ADDS 0x0100 /* there are additions */
|
|
165 #define BWF_PREFIX 0x0200 /* has prefix NR list */
|
|
166 #define BWF_ALLCAP 0x0400 /* all letters must be capital (not used
|
|
167 for single-letter words) */
|
|
168 #define BWF_KEEPCAP 0x0800 /* Keep case as-is */
|
|
169
|
|
170 /* flags used for addition in the spell file */
|
|
171 #define ADD_REGION 0x02 /* region byte follows */
|
|
172 #define ADD_ONECAP 0x04 /* first letter must be capital */
|
|
173 #define ADD_ALLCAP 0x40 /* all letters must be capital (not used
|
|
174 for single-letter words) */
|
|
175 #define ADD_KEEPCAP 0x80 /* fixed case */
|
|
176
|
|
177 /* Translate ADD_ flags to BWF_ flags.
|
|
178 * (Needed to keep ADD_ flags in one byte.) */
|
|
179 #define ADD2BWF(x) (((x) & 0x0f) | (((x) & 0xf0) << 4))
|
|
180
|
|
181 #define VIMSPELLMAGIC "VIMspell01" /* string at start of Vim spell file */
|
|
182 #define VIMSPELLMAGICL 10
|
|
183
|
|
184 /*
|
|
185 * Structure to store info for word matching.
|
|
186 */
|
|
187 typedef struct matchinf_S
|
|
188 {
|
|
189 langp_T *mi_lp; /* info for language and region */
|
|
190 slang_T *mi_slang; /* info for the language */
|
|
191 char_u *mi_line; /* start of line containing word */
|
|
192 char_u *mi_word; /* start of word being checked */
|
|
193 char_u *mi_end; /* first non-word char after mi_word */
|
|
194 char_u *mi_wend; /* end of matching word (is "mi_end"
|
|
195 * or further) */
|
|
196 char_u *mi_cword; /* word to check, can be "mi_fword" */
|
|
197 char_u mi_fword[MAXWLEN + 1]; /* "mi_word" to "mi_end" case-folded */
|
|
198 int mi_faddlen; /* length of valid bytes in "mi_fadd" */
|
|
199 char_u *mi_faddp; /* next char to be added to "mi_fadd" */
|
|
200 char_u mi_fadd[MAXWLEN + 1]; /* "mi_end" and further case-folded */
|
|
201 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
|
|
202 int mi_capflags; /* BWF_ONECAP BWF_ALLCAP BWF_KEEPCAP */
|
|
203 } matchinf_T;
|
|
204
|
|
205 static int word_match __ARGS((matchinf_T *mip));
|
|
206 static int check_adds __ARGS((matchinf_T *mip, fword_T *fw, int req_pref, int req_suf));
|
|
207 static int supports_afffix __ARGS((int cnt, void *afffix, int afffixcnt, int nr));
|
|
208 static int prefix_match __ARGS((matchinf_T *mip));
|
|
209 static int suffix_match __ARGS((matchinf_T *mip));
|
|
210 static int match_caps __ARGS((int flags, char_u *caseword, matchinf_T *mip, char_u *cword, char_u *end));
|
|
211 static slang_T *slang_alloc __ARGS((char_u *lang));
|
|
212 static void slang_free __ARGS((slang_T *lp));
|
|
213 static slang_T *spell_load_lang __ARGS((char_u *lang));
|
|
214 static void spell_load_file __ARGS((char_u *fname, void *cookie));
|
|
215 static int spell_load_affixes __ARGS((FILE *fd, slang_T *lp, int *bl_usedp, int affm, void **affp));
|
|
216 static void *getroom __ARGS((slang_T *lp, int *bl_used, int len));
|
|
217 static int find_region __ARGS((char_u *rp, char_u *region));
|
|
218 static int captype __ARGS((char_u *word, char_u *end));
|
|
219
|
|
220 /*
|
|
221 * Main spell-checking function.
|
|
222 * "ptr" points to the start of a word.
|
|
223 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
|
|
224 * or when it's OK it remains unchanged.
|
|
225 * This must only be called when 'spelllang' is not empty.
|
|
226 * Returns the length of the word in bytes, also when it's OK, so that the
|
|
227 * caller can skip over the word.
|
|
228 */
|
|
229 int
|
|
230 spell_check(wp, line, ptr, attrp)
|
|
231 win_T *wp; /* current window */
|
|
232 char_u *line; /* start of line where "ptr" points into */
|
|
233 char_u *ptr;
|
|
234 int *attrp;
|
|
235 {
|
|
236 matchinf_T mi; /* Most things are put in "mi" so that it can
|
|
237 be passed to functions quickly. */
|
|
238
|
|
239 /* Find the end of the word. We already know that *ptr is a word char. */
|
|
240 mi.mi_word = ptr;
|
|
241 mi.mi_end = ptr;
|
|
242 do
|
|
243 {
|
|
244 mb_ptr_adv(mi.mi_end);
|
|
245 } while (*mi.mi_end != NUL && spell_iswordc(mi.mi_end));
|
|
246
|
|
247 /* A word starting with a number is always OK. */
|
|
248 if (*ptr >= '0' && *ptr <= '9')
|
|
249 return (int)(mi.mi_end - ptr);
|
|
250
|
|
251 /* Make case-folded copy of the Word. Compute its hash value. */
|
|
252 (void)str_foldcase(ptr, mi.mi_end - ptr, mi.mi_fword, MAXWLEN + 1);
|
|
253 mi.mi_cword = mi.mi_fword;
|
|
254
|
|
255 /* The word is bad unless we find it in the dictionary. */
|
|
256 mi.mi_result = SP_BAD;
|
|
257 mi.mi_wend = mi.mi_end;
|
|
258 mi.mi_faddp = mi.mi_end;
|
|
259 mi.mi_faddlen = 0;
|
|
260 mi.mi_capflags = captype(ptr, mi.mi_end);
|
|
261 mi.mi_line = line;
|
|
262
|
|
263 /*
|
|
264 * Loop over the languages specified in 'spelllang'.
|
|
265 * We check them all, because a matching word may have additions that are
|
|
266 * longer than an already found matching word.
|
|
267 */
|
|
268 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
|
|
269 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
|
|
270 {
|
|
271 /*
|
|
272 * Check for a matching word.
|
|
273 * If not found or wrong region try removing prefixes (and then
|
|
274 * suffixes).
|
|
275 * If still not found or wrong region try removing suffixes.
|
|
276 */
|
|
277 mi.mi_slang = mi.mi_lp->lp_slang;
|
|
278 if (!word_match(&mi) || mi.mi_result != SP_OK)
|
|
279 if (!prefix_match(&mi) || mi.mi_result != SP_OK)
|
|
280 suffix_match(&mi);
|
|
281 }
|
|
282
|
|
283 if (mi.mi_result != SP_OK)
|
|
284 {
|
|
285 if (mi.mi_result == SP_BAD)
|
|
286 *attrp = highlight_attr[HLF_SPB];
|
|
287 else if (mi.mi_result == SP_RARE)
|
|
288 *attrp = highlight_attr[HLF_SPR];
|
|
289 else
|
|
290 *attrp = highlight_attr[HLF_SPL];
|
|
291 }
|
|
292
|
|
293 return (int)(mi.mi_wend - ptr);
|
|
294 }
|
|
295
|
|
296 /*
|
|
297 * Check if the word "mip->mi_cword" matches.
|
|
298 */
|
|
299 static int
|
|
300 word_match(mip)
|
|
301 matchinf_T *mip;
|
|
302 {
|
|
303 hash_T fhash = hash_hash(mip->mi_cword);
|
|
304 hashitem_T *hi;
|
|
305 fword_T *fw;
|
|
306 int valid = FALSE;
|
|
307
|
|
308 hi = hash_lookup(&mip->mi_slang->sl_words, mip->mi_cword, fhash);
|
|
309 if (HASHITEM_EMPTY(hi))
|
|
310 return FALSE;
|
|
311
|
|
312 /*
|
|
313 * Find a basic word for which the case of word "cword" is correct.
|
|
314 * If it is, check additions and use the longest one.
|
|
315 */
|
|
316 for (fw = HI2FWORD(hi); fw != NULL; fw = fw->fw_next)
|
|
317 if (match_caps(fw->fw_flags, fw->fw_word, mip,
|
|
318 mip->mi_word, mip->mi_end))
|
|
319 valid |= check_adds(mip, fw, -1, -1);
|
|
320
|
|
321 return valid;
|
|
322 }
|
|
323
|
|
324 /*
|
|
325 * Check a matching basic word for additions.
|
|
326 * Return TRUE if we have a valid match.
|
|
327 */
|
|
328 static int
|
|
329 check_adds(mip, fw, req_pref, req_suf)
|
|
330 matchinf_T *mip;
|
|
331 fword_T *fw;
|
|
332 int req_pref; /* required prefix nr, -1 if none */
|
|
333 int req_suf; /* required suffix nr, -1 if none */
|
|
334 {
|
|
335 int valid = FALSE;
|
|
336 addword_T *aw;
|
|
337 char_u *p;
|
|
338 int addlen;
|
|
339 int fl;
|
|
340
|
|
341 /* A word may be valid without additions. */
|
|
342 if ((fw->fw_flags & BWF_VALID)
|
|
343 && (req_pref < 0 || supports_afffix(mip->mi_slang->sl_prefcnt,
|
|
344 fw->fw_prefix, fw->fw_prefixcnt, req_pref))
|
|
345 && (req_suf < 0 || supports_afffix(mip->mi_slang->sl_suffcnt,
|
|
346 fw->fw_suffix, fw->fw_suffixcnt, req_suf)))
|
|
347 {
|
|
348 valid = TRUE;
|
|
349 if (mip->mi_result != SP_OK)
|
|
350 {
|
|
351 if ((fw->fw_region & mip->mi_lp->lp_region) == 0)
|
|
352 mip->mi_result = SP_LOCAL;
|
|
353 else
|
|
354 mip->mi_result = SP_OK;
|
|
355 }
|
|
356 }
|
|
357
|
|
358 /*
|
|
359 * Check additions, both before and after the word.
|
|
360 * This may make the word longer, thus we also need to check
|
|
361 * when we already found a matching word.
|
|
362 */
|
|
363 for (aw = fw->fw_adds; aw != NULL; aw = aw->aw_next)
|
|
364 {
|
|
365 if (aw->aw_leadlen > 0)
|
|
366 {
|
|
367 /* There is a leader, verify that it matches. */
|
|
368 if (aw->aw_leadlen > mip->mi_word - mip->mi_line
|
|
369 || STRNCMP(mip->mi_word - aw->aw_leadlen,
|
|
370 aw->aw_word, aw->aw_leadlen) != 0)
|
|
371 continue;
|
|
372 if (mip->mi_word - aw->aw_leadlen > mip->mi_line)
|
|
373 {
|
|
374 /* There must not be a word character just before the
|
|
375 * leader. */
|
|
376 p = mip->mi_word - aw->aw_leadlen;
|
|
377 mb_ptr_back(mip->mi_line, p);
|
|
378 if (spell_iswordc(p))
|
|
379 continue;
|
|
380 }
|
|
381 /* Leader matches. Addition is rest of "aw_word". */
|
|
382 p = aw->aw_word + aw->aw_leadlen;
|
|
383 }
|
|
384 else
|
|
385 /* No leader, use whole of "aw_word" for addition. */
|
|
386 p = aw->aw_word;
|
|
387
|
|
388 addlen = aw->aw_wordlen - aw->aw_leadlen;
|
|
389 if (addlen > 0)
|
|
390 {
|
|
391 /* Check for matching addition and no word character after it.
|
|
392 * First make sure we have enough case-folded chars to compare
|
|
393 * with. */
|
|
394 while (mip->mi_faddlen <= addlen)
|
|
395 {
|
|
396 if (*mip->mi_faddp == NUL)
|
|
397 {
|
|
398 mip->mi_fadd[mip->mi_faddlen] = NUL;
|
|
399 break;
|
|
400 }
|
|
401 #ifdef FEAT_MBYTE
|
|
402 fl = (*mb_ptr2len_check)(mip->mi_faddp);
|
|
403 #else
|
|
404 fl = 1;
|
|
405 #endif
|
|
406 (void)str_foldcase(mip->mi_faddp, fl,
|
|
407 mip->mi_fadd + mip->mi_faddlen,
|
|
408 MAXWLEN - mip->mi_faddlen);
|
|
409 mip->mi_faddp += fl;
|
|
410 mip->mi_faddlen += STRLEN(mip->mi_fadd + mip->mi_faddlen);
|
|
411 }
|
|
412
|
|
413 if (STRNCMP(mip->mi_fadd, p, addlen) != 0
|
|
414 || (mip->mi_fadd[addlen] != NUL
|
|
415 && spell_iswordc(mip->mi_fadd + addlen)))
|
|
416 continue;
|
|
417
|
|
418 /* Compute the length in the original word, before case folding. */
|
|
419 #ifdef FEAT_MBYTE
|
|
420 if (has_mbyte)
|
|
421 {
|
|
422 int l;
|
|
423
|
|
424 p = mip->mi_end;
|
|
425 for (l = 0; l < addlen;
|
|
426 l += (*mb_ptr2len_check)(mip->mi_fadd + l))
|
|
427 mb_ptr_adv(p);
|
|
428 addlen = p - mip->mi_end;
|
|
429 }
|
|
430 #endif
|
|
431
|
|
432 /* Check case of the addition. */
|
|
433 if (!match_caps(ADD2BWF(aw->aw_flags),
|
|
434 aw->aw_word + aw->aw_wordlen + 1, mip,
|
|
435 mip->mi_end, mip->mi_end + addlen))
|
|
436 continue;
|
|
437 }
|
|
438
|
|
439 /* Match! Use the new length if it's longer. */
|
|
440 if (mip->mi_wend < mip->mi_end + addlen)
|
|
441 mip->mi_wend = mip->mi_end + addlen;
|
|
442
|
|
443 valid = TRUE;
|
|
444 if (mip->mi_result != SP_OK)
|
|
445 {
|
|
446 if ((aw->aw_region & mip->mi_lp->lp_region) == 0)
|
|
447 mip->mi_result = SP_LOCAL;
|
|
448 else
|
|
449 mip->mi_result = SP_OK;
|
|
450 }
|
|
451 }
|
|
452
|
|
453 return valid;
|
|
454 }
|
|
455
|
|
456 /*
|
|
457 * Return TRUE if word "fw" supports afffix "nr".
|
|
458 */
|
|
459 static int
|
|
460 supports_afffix(cnt, afffix, afffixcnt, nr)
|
|
461 int cnt;
|
|
462 void *afffix;
|
|
463 int afffixcnt;
|
|
464 int nr;
|
|
465 {
|
|
466 char_u *pc;
|
|
467 short_u *ps;
|
|
468 int i;
|
|
469
|
|
470 if (cnt <= 256)
|
|
471 {
|
|
472 /* char_u affix numbers */
|
|
473 pc = afffix;
|
|
474 for (i = afffixcnt; --i >= 0; )
|
|
475 if (*pc++ == nr)
|
|
476 return TRUE;
|
|
477 }
|
|
478 else
|
|
479 {
|
|
480 /* short_u affix numbers */
|
|
481 ps = afffix;
|
|
482 for (i = afffixcnt; --i >= 0; )
|
|
483 if (*ps++ == nr)
|
|
484 return TRUE;
|
|
485 }
|
|
486 return FALSE;
|
|
487 }
|
|
488
|
|
489 /*
|
|
490 * Try finding a match for "mip->mi_cword" by removing prefixes.
|
|
491 */
|
|
492 static int
|
|
493 prefix_match(mip)
|
|
494 matchinf_T *mip;
|
|
495 {
|
|
496 int len = 0;
|
|
497 int charlen = 0;
|
|
498 int cc;
|
|
499 affitem_T *ai;
|
|
500 char_u pword[MAXWLEN + 1];
|
|
501 fword_T *fw;
|
|
502 hashtab_T *ht;
|
|
503 hashitem_T *hi;
|
|
504 int i;
|
|
505 int found_valid = FALSE;
|
|
506 int cstart_charlen = 0;
|
|
507 char_u *cstart = mip->mi_word;
|
|
508 int capflags_save = mip->mi_capflags;
|
|
509 char_u *p;
|
|
510
|
|
511 /*
|
|
512 * Check for prefixes with different character lengths.
|
|
513 * Start with zero length (only chop off).
|
|
514 */
|
|
515 for (charlen = 0; charlen <= mip->mi_slang->sl_preftab.ga_len; ++charlen)
|
|
516 {
|
|
517 if (charlen > 0)
|
|
518 {
|
|
519 #ifdef FEAT_MBYTE
|
|
520 if (has_mbyte)
|
|
521 len += mb_ptr2len_check(mip->mi_cword + len);
|
|
522 else
|
|
523 #endif
|
|
524 len += 1;
|
|
525 }
|
|
526 if (mip->mi_cword[len] == NUL) /* end of word, no prefix possible */
|
|
527 break;
|
|
528
|
|
529 if (charlen == 0)
|
|
530 ai = mip->mi_slang->sl_prefzero;
|
|
531 else
|
|
532 {
|
|
533 /* Get pointer to hashtab for prefix of this many chars. */
|
|
534 ht = ((hashtab_T *)mip->mi_slang->sl_preftab.ga_data) + charlen - 1;
|
|
535 if (ht->ht_used == 0)
|
|
536 continue;
|
|
537
|
|
538 cc = mip->mi_cword[len];
|
|
539 mip->mi_cword[len] = NUL;
|
|
540 hi = hash_find(ht, mip->mi_cword);
|
|
541 mip->mi_cword[len] = cc;
|
|
542
|
|
543 if (HASHITEM_EMPTY(hi))
|
|
544 ai = NULL;
|
|
545 else
|
|
546 ai = HI2AI(hi);
|
|
547 }
|
|
548
|
|
549 /* Loop over all matching prefixes. */
|
|
550 for ( ; ai != NULL; ai = ai->ai_next)
|
|
551 {
|
|
552 /* Create the basic word by removing the prefix and adding the
|
|
553 * chop string. */
|
|
554 mch_memmove(pword, ai->ai_chop, ai->ai_choplen);
|
|
555 STRCPY(pword + ai->ai_choplen, mip->mi_cword + ai->ai_addlen);
|
|
556
|
|
557 /* Adjust the word start for case checks, we only check the
|
|
558 * part after the prefix. */
|
|
559 while (cstart_charlen < charlen)
|
|
560 {
|
|
561 mb_ptr_adv(cstart);
|
|
562 ++cstart_charlen;
|
|
563 }
|
|
564
|
|
565 /* Removing the prefix may change the caps, e.g. for
|
|
566 * "deAlf" removing "de" makes it ONECAP. */
|
|
567 mip->mi_capflags = captype(cstart, mip->mi_end);
|
|
568
|
|
569 /* Find the basic word. */
|
|
570 hi = hash_find(&mip->mi_slang->sl_words, pword);
|
|
571 if (!HASHITEM_EMPTY(hi))
|
|
572 {
|
|
573 /* Check if the word supports this prefix. */
|
|
574 for (fw = HI2FWORD(hi); fw != NULL; fw = fw->fw_next)
|
|
575 if (match_caps(fw->fw_flags, fw->fw_word, mip,
|
|
576 cstart, mip->mi_end))
|
|
577 found_valid |= check_adds(mip, fw, ai->ai_nr, -1);
|
|
578
|
|
579 if (found_valid && mip->mi_result == SP_OK)
|
|
580 {
|
|
581 /* Found a valid word, no need to try other suffixes. */
|
|
582 mip->mi_capflags = capflags_save;
|
|
583 return TRUE;
|
|
584 }
|
|
585 }
|
|
586
|
|
587 /* No matching basic word without prefix. When combining is
|
|
588 * allowed try with suffixes. */
|
|
589 if (ai->ai_combine)
|
|
590 {
|
|
591 /* Pass the word with prefix removed to suffix_match(). */
|
|
592 mip->mi_cword = pword;
|
|
593 p = mip->mi_word;
|
|
594 mip->mi_word = cstart;
|
|
595 i = suffix_match(mip);
|
|
596 mip->mi_cword = mip->mi_fword;
|
|
597 mip->mi_word = p;
|
|
598 if (i)
|
|
599 {
|
|
600 mip->mi_capflags = capflags_save;
|
|
601 return TRUE;
|
|
602 }
|
|
603 }
|
|
604 }
|
|
605 }
|
|
606
|
|
607 mip->mi_capflags = capflags_save;
|
|
608 return FALSE;
|
|
609 }
|
|
610
|
|
611 /*
|
|
612 * Try finding a match for "mip->mi_cword" by removing suffixes.
|
|
613 */
|
|
614 static int
|
|
615 suffix_match(mip)
|
|
616 matchinf_T *mip;
|
|
617 {
|
|
618 char_u *sufp;
|
|
619 int charlen;
|
|
620 affitem_T *ai;
|
|
621 char_u pword[MAXWLEN + 1];
|
|
622 fword_T *fw;
|
|
623 hashtab_T *ht;
|
|
624 hashitem_T *hi;
|
|
625 int tlen;
|
|
626 int cend_charlen = 0;
|
|
627 char_u *cend = mip->mi_end;
|
|
628 int found_valid = FALSE;
|
|
629 int capflags_save = mip->mi_capflags;
|
|
630
|
|
631 /*
|
|
632 * Try suffixes of different length, starting with an empty suffix (chop
|
|
633 * only, thus adds something).
|
|
634 * Stop checking if there are no suffixes with so many characters.
|
|
635 */
|
|
636 sufp = mip->mi_cword + STRLEN(mip->mi_cword);
|
|
637 for (charlen = 0; charlen <= mip->mi_slang->sl_sufftab.ga_len; ++charlen)
|
|
638 {
|
|
639 /* Move the pointer to the possible suffix back one character, unless
|
|
640 * doing the first round (empty suffix). */
|
|
641 if (charlen > 0)
|
|
642 {
|
|
643 mb_ptr_back(mip->mi_cword, sufp);
|
|
644 if (sufp <= mip->mi_cword) /* start of word, no suffix possible */
|
|
645 break;
|
|
646 }
|
|
647
|
|
648 if (charlen == 0)
|
|
649 ai = mip->mi_slang->sl_suffzero;
|
|
650 else
|
|
651 {
|
|
652 /* Get pointer to hashtab for suffix of this many chars. */
|
|
653 ht = ((hashtab_T *)mip->mi_slang->sl_sufftab.ga_data) + charlen - 1;
|
|
654 if (ht->ht_used == 0)
|
|
655 continue;
|
|
656
|
|
657 hi = hash_find(ht, sufp);
|
|
658 if (HASHITEM_EMPTY(hi))
|
|
659 ai = NULL;
|
|
660 else
|
|
661 ai = HI2AI(hi);
|
|
662 }
|
|
663
|
|
664 if (ai != NULL)
|
|
665 {
|
|
666 /* Found a list of matching suffixes. Now check that there is one
|
|
667 * we can use. */
|
|
668 tlen = sufp - mip->mi_cword; /* length of word without suffix */
|
|
669 mch_memmove(pword, mip->mi_cword, tlen);
|
|
670
|
|
671 for ( ; ai != NULL; ai = ai->ai_next)
|
|
672 {
|
|
673 /* Found a matching suffix. Create the basic word by removing
|
|
674 * the suffix and adding the chop string. */
|
|
675 if (ai->ai_choplen == 0)
|
|
676 pword[tlen] = NUL;
|
|
677 else
|
|
678 mch_memmove(pword + tlen, ai->ai_chop, ai->ai_choplen + 1);
|
|
679
|
|
680 /* Find the basic word. */
|
|
681 hi = hash_find(&mip->mi_slang->sl_words, pword);
|
|
682 if (!HASHITEM_EMPTY(hi))
|
|
683 {
|
|
684 /* Adjust the end for case checks, we only check the part
|
|
685 * before the suffix. */
|
|
686 while (cend_charlen < charlen)
|
|
687 {
|
|
688 mb_ptr_back(mip->mi_word, cend);
|
|
689 ++cend_charlen;
|
|
690 }
|
|
691
|
|
692 /* Removing the suffix may change the caps, e.g. for
|
|
693 * "UFOs" removing 's' makes it ALLCAP. */
|
|
694 mip->mi_capflags = captype(mip->mi_word, cend);
|
|
695
|
|
696 /* Check if the word supports this suffix. */
|
|
697 for (fw = HI2FWORD(hi); fw != NULL; fw = fw->fw_next)
|
|
698 if (match_caps(fw->fw_flags, fw->fw_word, mip,
|
|
699 mip->mi_word, cend))
|
|
700 found_valid |= check_adds(mip, fw, -1, ai->ai_nr);
|
|
701
|
|
702 if (found_valid && mip->mi_result == SP_OK)
|
|
703 {
|
|
704 /* Found a valid word, no need to try other suffixes. */
|
|
705 mip->mi_capflags = capflags_save;
|
|
706 return TRUE;
|
|
707 }
|
|
708 }
|
|
709 }
|
|
710 }
|
|
711 }
|
|
712
|
|
713 mip->mi_capflags = capflags_save;
|
|
714 return FALSE;
|
|
715 }
|
|
716
|
|
717 /*
|
|
718 * Return TRUE if case of "cword" meets the requirements of case flags
|
|
719 * "flags".
|
|
720 */
|
|
721 static int
|
|
722 match_caps(flags, caseword, mip, cword, end)
|
|
723 int flags; /* flags required by basic word or addition */
|
|
724 char_u *caseword; /* word with case as required */
|
|
725 matchinf_T *mip;
|
|
726 char_u *cword; /* word to compare against "caseword" */
|
|
727 char_u *end; /* end of "cword" */
|
|
728 {
|
|
729 char_u *p;
|
|
730 int c;
|
|
731 int len;
|
|
732 int capflags = mip->mi_capflags; /* flags of checked word */
|
|
733 int past_second;
|
|
734
|
|
735 if ((capflags & BWF_KEEPCAP) == 0 && end > mip->mi_end)
|
|
736 {
|
|
737 /* If "end" is past "mip->mi_end" we need to check the characters
|
|
738 * after the basic word. */
|
|
739 #ifdef FEAT_MBYTE
|
|
740 past_second = (mip->mi_word + (*mb_ptr2len_check)(mip->mi_word)
|
|
741 < mip->mi_end);
|
|
742 #else
|
|
743 past_second = mip->mi_word + 1 < mip->mi_end;
|
|
744 #endif
|
|
745 for (p = mip->mi_end; p < end; )
|
|
746 {
|
|
747 if (!spell_iswordc(p))
|
|
748 mb_ptr_adv(p);
|
|
749 else
|
|
750 {
|
|
751 #ifdef FEAT_MBYTE
|
|
752 if (has_mbyte)
|
|
753 c = mb_ptr2char_adv(&p);
|
|
754 else
|
|
755 #endif
|
|
756 c = *p++;
|
|
757 if (MB_ISUPPER(c))
|
|
758 {
|
|
759 if (capflags == 0 || (capflags & BWF_ONECAP))
|
|
760 {
|
|
761 capflags = BWF_KEEPCAP; /* lU or UlU */
|
|
762 break;
|
|
763 }
|
|
764 }
|
|
765 else
|
|
766 {
|
|
767 if (capflags & BWF_ALLCAP)
|
|
768 {
|
|
769 if (past_second)
|
|
770 {
|
|
771 capflags = BWF_KEEPCAP; /* UUl */
|
|
772 break;
|
|
773 }
|
|
774 capflags = BWF_ONECAP; /* Uu */
|
|
775 }
|
|
776 }
|
|
777 past_second = TRUE;
|
|
778 }
|
|
779 }
|
|
780 }
|
|
781
|
|
782 if (capflags == BWF_ALLCAP)
|
|
783 return TRUE; /* All caps is always OK. */
|
|
784
|
|
785 if (flags & BWF_KEEPCAP)
|
|
786 {
|
|
787 len = STRLEN(caseword);
|
|
788 return (len == end - cword && STRNCMP(caseword, cword, len) == 0);
|
|
789 }
|
|
790
|
|
791 if (flags & BWF_ALLCAP)
|
|
792 return FALSE; /* need ALLCAP, already checked above */
|
|
793
|
|
794 if (flags & BWF_ONECAP)
|
|
795 return capflags == BWF_ONECAP;
|
|
796
|
|
797 return capflags != BWF_KEEPCAP; /* no case check, only KEEPCAP is bad */
|
|
798 }
|
|
799
|
|
800 /*
|
|
801 * Move to next spell error.
|
|
802 * Return OK if found, FAIL otherwise.
|
|
803 */
|
|
804 int
|
|
805 spell_move_to(dir, allwords)
|
|
806 int dir; /* FORWARD or BACKWARD */
|
|
807 int allwords; /* TRUE for "[s" and "]s" */
|
|
808 {
|
|
809 pos_T pos;
|
|
810 char_u *line;
|
|
811 char_u *p;
|
|
812 int wc;
|
|
813 int nwc;
|
|
814 int attr = 0;
|
|
815 int len;
|
|
816
|
|
817 if (!curwin->w_p_spell || *curwin->w_buffer->b_p_spl == NUL)
|
|
818 {
|
|
819 EMSG(_("E756: Spell checking not enabled"));
|
|
820 return FAIL;
|
|
821 }
|
|
822
|
|
823 /* TODO: moving backwards */
|
|
824
|
|
825 /* Start looking for bad word at the start of the line, because we can't
|
|
826 * start halfway a word and know where it ends. */
|
|
827 pos = curwin->w_cursor;
|
|
828 pos.col = 0;
|
|
829 wc = FALSE;
|
|
830
|
|
831 while (!got_int)
|
|
832 {
|
|
833 line = ml_get(pos.lnum);
|
|
834 p = line + pos.col;
|
|
835 while (*p != NUL)
|
|
836 {
|
|
837 nwc = spell_iswordc(p);
|
|
838 if (!wc && nwc)
|
|
839 {
|
|
840 /* start of word */
|
|
841 /* TODO: check for bad word attr */
|
|
842 len = spell_check(curwin, line, p, &attr);
|
|
843 if (attr != 0)
|
|
844 {
|
|
845 if (curwin->w_cursor.lnum < pos.lnum
|
|
846 || (curwin->w_cursor.lnum == pos.lnum
|
|
847 && curwin->w_cursor.col < (colnr_T)(p - line)))
|
|
848 {
|
|
849 curwin->w_cursor.lnum = pos.lnum;
|
|
850 curwin->w_cursor.col = p - line;
|
|
851 return OK;
|
|
852 }
|
|
853 attr = 0; /* bad word is before or at cursor */
|
|
854 }
|
|
855 p += len;
|
|
856 if (*p == NUL)
|
|
857 break;
|
|
858 nwc = FALSE;
|
|
859 }
|
|
860
|
|
861 /* advance to next character */
|
|
862 mb_ptr_adv(p);
|
|
863 wc = nwc;
|
|
864 }
|
|
865
|
|
866 /* Advance to next line. */
|
|
867 if (pos.lnum == curbuf->b_ml.ml_line_count)
|
|
868 return FAIL;
|
|
869 ++pos.lnum;
|
|
870 pos.col = 0;
|
|
871 wc = FALSE;
|
|
872
|
|
873 line_breakcheck();
|
|
874 }
|
|
875
|
|
876 return FAIL; /* interrupted */
|
|
877 }
|
|
878
|
|
879 /*
|
|
880 * Load word list for "lang" from a Vim spell file.
|
|
881 * "lang" must be the language without the region: "en" or "en-rare".
|
|
882 */
|
|
883 static slang_T *
|
|
884 spell_load_lang(lang)
|
|
885 char_u *lang;
|
|
886 {
|
|
887 slang_T *lp;
|
|
888 char_u fname_enc[80];
|
|
889 char_u *p;
|
|
890 int r;
|
|
891
|
|
892 lp = slang_alloc(lang);
|
|
893 if (lp != NULL)
|
|
894 {
|
|
895 /* Find all spell files for "lang" in 'runtimepath' and load them.
|
|
896 * Use 'encoding', except that we use "latin1" for "latin9". */
|
|
897 #ifdef FEAT_MBYTE
|
|
898 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
|
|
899 p = p_enc;
|
|
900 else
|
|
901 #endif
|
|
902 p = (char_u *)"latin1";
|
|
903 sprintf((char *)fname_enc, "spell/%s.%s.spl", lang, p);
|
|
904
|
|
905 r = do_in_runtimepath(fname_enc, TRUE, spell_load_file, lp);
|
|
906 if (r == FAIL || lp->sl_error)
|
|
907 {
|
|
908 slang_free(lp);
|
|
909 lp = NULL;
|
|
910 if (r == FAIL)
|
|
911 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
|
|
912 fname_enc + 6);
|
|
913 }
|
|
914 else
|
|
915 {
|
|
916 lp->sl_next = first_lang;
|
|
917 first_lang = lp;
|
|
918 }
|
|
919 }
|
|
920
|
|
921 return lp;
|
|
922 }
|
|
923
|
|
924 /*
|
|
925 * Allocate a new slang_T.
|
|
926 * Caller must fill "sl_next".
|
|
927 */
|
|
928 static slang_T *
|
|
929 slang_alloc(lang)
|
|
930 char_u *lang;
|
|
931 {
|
|
932 slang_T *lp;
|
|
933
|
|
934 lp = (slang_T *)alloc(sizeof(slang_T));
|
|
935 if (lp != NULL)
|
|
936 {
|
|
937 lp->sl_name = vim_strsave(lang);
|
|
938 hash_init(&lp->sl_words);
|
|
939 ga_init2(&lp->sl_preftab, sizeof(hashtab_T), 4);
|
|
940 ga_init2(&lp->sl_sufftab, sizeof(hashtab_T), 4);
|
|
941 lp->sl_prefzero = NULL;
|
|
942 lp->sl_suffzero = NULL;
|
|
943 lp->sl_try = NULL;
|
|
944 ga_init2(&lp->sl_rep, sizeof(repentry_T), 4);
|
|
945 lp->sl_regions[0] = NUL;
|
|
946 lp->sl_block = NULL;
|
|
947 lp->sl_error = FALSE;
|
|
948 }
|
|
949 return lp;
|
|
950 }
|
|
951
|
|
952 /*
|
|
953 * Free the contents of an slang_T and the structure itself.
|
|
954 */
|
|
955 static void
|
|
956 slang_free(lp)
|
|
957 slang_T *lp;
|
|
958 {
|
|
959 sblock_T *sp;
|
|
960 int i;
|
|
961
|
|
962 vim_free(lp->sl_name);
|
|
963 hash_clear(&lp->sl_words);
|
|
964 for (i = 0; i < lp->sl_preftab.ga_len; ++i)
|
|
965 hash_clear(((hashtab_T *)lp->sl_preftab.ga_data) + i);
|
|
966 ga_clear(&lp->sl_preftab);
|
|
967 for (i = 0; i < lp->sl_sufftab.ga_len; ++i)
|
|
968 hash_clear(((hashtab_T *)lp->sl_sufftab.ga_data) + i);
|
|
969 ga_clear(&lp->sl_sufftab);
|
|
970 ga_clear(&lp->sl_rep);
|
|
971 vim_free(lp->sl_try);
|
|
972 while (lp->sl_block != NULL)
|
|
973 {
|
|
974 sp = lp->sl_block;
|
|
975 lp->sl_block = sp->sb_next;
|
|
976 vim_free(sp);
|
|
977 }
|
|
978 vim_free(lp);
|
|
979 }
|
|
980
|
|
981 /*
|
|
982 * Load one spell file into an slang_T.
|
|
983 * Invoked through do_in_runtimepath().
|
|
984 */
|
|
985 static void
|
|
986 spell_load_file(fname, cookie)
|
|
987 char_u *fname;
|
|
988 void *cookie; /* points to the slang_T to be filled */
|
|
989 {
|
|
990 slang_T *lp = cookie;
|
|
991 FILE *fd;
|
|
992 char_u buf[MAXWLEN + 1];
|
|
993 char_u cbuf[MAXWLEN + 1];
|
|
994 char_u fbuf[MAXWLEN + 1];
|
|
995 char_u *p;
|
|
996 int itm;
|
|
997 int i;
|
|
998 int affcount;
|
|
999 int affnr;
|
|
1000 int affflags;
|
|
1001 int affitemcnt;
|
|
1002 int bl_used = SBLOCKSIZE;
|
|
1003 int widx;
|
|
1004 int prefm; /* 1 if <= 256 prefixes, sizeof(short_u) otherw. */
|
|
1005 int suffm; /* 1 if <= 256 suffixes, sizeof(short_u) otherw. */
|
|
1006 int wlen;
|
|
1007 int flags;
|
|
1008 affitem_T *ai, *ai2, **aip;
|
|
1009 int round;
|
|
1010 char_u *save_sourcing_name = sourcing_name;
|
|
1011 linenr_T save_sourcing_lnum = sourcing_lnum;
|
|
1012 int cnt;
|
|
1013 int choplen;
|
|
1014 int addlen;
|
|
1015 int leadlen;
|
|
1016 int wordcount;
|
|
1017 fword_T *fw, *fw2;
|
|
1018 garray_T *gap;
|
|
1019 hashtab_T *ht;
|
|
1020 hashitem_T *hi;
|
|
1021 hash_T hash;
|
|
1022 int adds;
|
|
1023 addword_T *aw;
|
|
1024 int flen;
|
|
1025
|
|
1026 fd = fopen((char *)fname, "r");
|
|
1027 if (fd == NULL)
|
|
1028 {
|
|
1029 EMSG2(_(e_notopen), fname);
|
|
1030 goto errorend;
|
|
1031 }
|
|
1032
|
|
1033 /* Set sourcing_name, so that error messages mention the file name. */
|
|
1034 sourcing_name = fname;
|
|
1035 sourcing_lnum = 0;
|
|
1036
|
|
1037 /* <HEADER>: <fileID> <regioncnt> <regionname> ... */
|
|
1038 for (i = 0; i < VIMSPELLMAGICL; ++i)
|
|
1039 buf[i] = getc(fd); /* <fileID> */
|
|
1040 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
|
|
1041 {
|
|
1042 EMSG(_("E757: Wrong file ID in spell file"));
|
|
1043 goto errorend;
|
|
1044 }
|
|
1045
|
|
1046 cnt = getc(fd); /* <regioncnt> */
|
|
1047 if (cnt == EOF)
|
|
1048 {
|
|
1049 truncerr:
|
|
1050 EMSG(_("E758: Truncated spell file"));
|
|
1051 goto errorend;
|
|
1052 }
|
|
1053 if (cnt > 8)
|
|
1054 {
|
|
1055 formerr:
|
|
1056 EMSG(_("E759: Format error in spell file"));
|
|
1057 goto errorend;
|
|
1058 }
|
|
1059 for (i = 0; i < cnt; ++i)
|
|
1060 {
|
|
1061 lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
|
|
1062 lp->sl_regions[i * 2 + 1] = getc(fd);
|
|
1063 }
|
|
1064 lp->sl_regions[cnt * 2] = NUL;
|
|
1065
|
|
1066 /* round 1: <PREFIXLIST>: <affcount> <afftotcnt> <affix> ...
|
|
1067 * round 2: <SUFFIXLIST>: <affcount> <afftotcnt> <affix> ... */
|
|
1068 for (round = 1; round <= 2; ++round)
|
|
1069 {
|
|
1070 affcount = (getc(fd) << 8) + getc(fd); /* <affcount> */
|
|
1071 if (affcount < 0)
|
|
1072 goto truncerr;
|
|
1073 if (round == 1)
|
|
1074 {
|
|
1075 gap = &lp->sl_preftab;
|
|
1076 aip = &lp->sl_prefzero;
|
|
1077 lp->sl_prefcnt = affcount;
|
|
1078 prefm = affcount > 256 ? sizeof(short_u) : 1;
|
|
1079 }
|
|
1080 else
|
|
1081 {
|
|
1082 gap = &lp->sl_sufftab;
|
|
1083 aip = &lp->sl_suffzero;
|
|
1084 lp->sl_suffcnt = affcount;
|
|
1085 suffm = affcount > 256 ? sizeof(short_u) : 1;
|
|
1086 }
|
|
1087
|
|
1088 i = (getc(fd) << 8) + getc(fd); /* <afftotcnt> */
|
|
1089 /* afftotcnt is not used */
|
|
1090
|
|
1091 /*
|
|
1092 * For each affix NR there can be several affixes.
|
|
1093 */
|
|
1094 for (affnr = 0; affnr < affcount; ++affnr)
|
|
1095 {
|
|
1096 /* <affix>: <affflags> <affitemcnt> <affitem> ... */
|
|
1097 affflags = getc(fd); /* <affflags> */
|
|
1098 if (affflags == EOF)
|
|
1099 goto truncerr;
|
|
1100 affitemcnt = (getc(fd) << 8) + getc(fd); /* <affitemcnt> */
|
|
1101 if (affitemcnt < 0)
|
|
1102 goto truncerr;
|
|
1103 for (itm = 0; itm < affitemcnt; ++itm)
|
|
1104 {
|
|
1105 /* <affitem>: <affchoplen> <affchop> <affaddlen> <affadd> */
|
|
1106 choplen = getc(fd); /* <affchoplen> */
|
|
1107 if (choplen == EOF)
|
|
1108 goto truncerr;
|
|
1109 if (choplen >= MAXWLEN)
|
|
1110 goto formerr;
|
|
1111 for (i = 0; i < choplen; ++i) /* <affchop> */
|
|
1112 buf[i] = getc(fd);
|
|
1113 buf[i] = NUL;
|
|
1114 addlen = getc(fd); /* <affaddlen> */
|
|
1115 if (addlen == EOF)
|
|
1116 goto truncerr;
|
|
1117 /* Get room to store the affitem_T, chop and add strings. */
|
|
1118 p = (char_u *)getroom(lp, &bl_used,
|
|
1119 sizeof(affitem_T) + choplen + addlen + 1);
|
|
1120 if (p == NULL)
|
|
1121 goto errorend;
|
|
1122
|
|
1123 ai = (affitem_T *)p;
|
|
1124 ai->ai_nr = affnr;
|
|
1125 ai->ai_combine = affflags;
|
|
1126 ai->ai_choplen = choplen;
|
|
1127 ai->ai_addlen = addlen;
|
|
1128
|
|
1129 p += sizeof(affitem_T) + addlen;
|
|
1130 ai->ai_chop = p;
|
|
1131 STRCPY(p, buf);
|
|
1132
|
|
1133 p = ai->ai_add;
|
|
1134 for (i = 0; i < addlen; ++i) /* <affadd> */
|
|
1135 p[i] = getc(fd);
|
|
1136 p[i] = NUL;
|
|
1137
|
|
1138 /*
|
|
1139 * Add the affix to a hashtable. Which one depends on the
|
|
1140 * length of the added string in characters.
|
|
1141 */
|
|
1142 #ifdef FEAT_MBYTE
|
|
1143 /* Change "addlen" from length in bytes to length in chars. */
|
|
1144 if (has_mbyte)
|
|
1145 addlen = mb_charlen(p);
|
|
1146 #endif
|
|
1147 if (addlen == 0)
|
|
1148 {
|
|
1149 /* Link in list of zero length affixes. */
|
|
1150 ai->ai_next = *aip;
|
|
1151 *aip = ai;
|
|
1152 }
|
|
1153 else
|
|
1154 {
|
|
1155 if (gap->ga_len < addlen)
|
|
1156 {
|
|
1157 /* Longer affix, need more hashtables. */
|
|
1158 if (ga_grow(gap, addlen - gap->ga_len) == FAIL)
|
|
1159 goto errorend;
|
|
1160
|
|
1161 /* Re-allocating ga_data means that an ht_array
|
|
1162 * pointing to ht_smallarray becomes invalid. We can
|
|
1163 * recognize this: ht_mask is at its init value. */
|
|
1164 for (i = 0; i < gap->ga_len; ++i)
|
|
1165 {
|
|
1166 ht = ((hashtab_T *)gap->ga_data) + i;
|
|
1167 if (ht->ht_mask == HT_INIT_SIZE - 1)
|
|
1168 ht->ht_array = ht->ht_smallarray;
|
|
1169 }
|
|
1170
|
|
1171 /* Init the newly used hashtable(s). */
|
|
1172 while (gap->ga_len < addlen)
|
|
1173 {
|
|
1174 hash_init(((hashtab_T *)gap->ga_data)
|
|
1175 + gap->ga_len);
|
|
1176 ++gap->ga_len;
|
|
1177 }
|
|
1178 }
|
|
1179 ht = ((hashtab_T *)gap->ga_data) + addlen - 1;
|
|
1180 hash = hash_hash(p);
|
|
1181 hi = hash_lookup(ht, p, hash);
|
|
1182 if (HASHITEM_EMPTY(hi))
|
|
1183 {
|
|
1184 /* First affix with this "ai_add", add to hashtable. */
|
|
1185 hash_add_item(ht, hi, p, hash);
|
|
1186 ai->ai_next = NULL;
|
|
1187 }
|
|
1188 else
|
|
1189 {
|
|
1190 /* There already is an affix with this "ai_add", link
|
|
1191 * in the list. */
|
|
1192 ai2 = HI2AI(hi);
|
|
1193 ai->ai_next = ai2->ai_next;
|
|
1194 ai2->ai_next = ai;
|
|
1195 }
|
|
1196 }
|
|
1197 }
|
|
1198 }
|
|
1199 }
|
|
1200
|
|
1201 /* <SUGGEST> : <suggestlen> <more> ... */
|
|
1202 /* TODO, just skip this for now */
|
|
1203 i = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
|
|
1204 while (i-- > 0)
|
|
1205 if (getc(fd) == EOF) /* <suggestlen> */
|
|
1206 goto truncerr;
|
|
1207
|
|
1208 /* <WORDLIST>: <wordcount> <worditem> ... */ /* <wordcount> */
|
|
1209 wordcount = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8)
|
|
1210 + getc(fd);
|
|
1211 if (wordcount < 0)
|
|
1212 goto truncerr;
|
|
1213
|
|
1214 /* Init hashtable for this number of words, so that it doesn't need to
|
|
1215 * reallocate the table halfway. */
|
|
1216 hash_lock_size(&lp->sl_words, wordcount);
|
|
1217
|
|
1218 for (widx = 0; ; ++widx)
|
|
1219 {
|
|
1220 /* <worditem>: <nr> <string> <flags> [<flags2>]
|
|
1221 * [<caselen> <caseword>]
|
|
1222 * [<affixcnt> <affixNR> ...] (prefixes)
|
|
1223 * [<affixcnt> <affixNR> ...] (suffixes)
|
|
1224 * [<region>]
|
|
1225 * [<addcnt> <add> ...]
|
|
1226 */
|
|
1227 /* Use <nr> bytes from the previous word. */
|
|
1228 wlen = getc(fd); /* <nr> */
|
|
1229 if (wlen == EOF)
|
|
1230 {
|
|
1231 if (widx >= wordcount) /* normal way to end the file */
|
|
1232 break;
|
|
1233 goto truncerr;
|
|
1234 }
|
|
1235
|
|
1236 /* Read further word bytes until one below 0x20, that must be the
|
|
1237 * flags. Keep this fast! */
|
|
1238 for (;;)
|
|
1239 {
|
|
1240 if ((buf[wlen] = getc(fd)) < 0x20) /* <string> */
|
|
1241 break;
|
|
1242 if (++wlen == MAXWLEN)
|
|
1243 goto formerr;
|
|
1244 }
|
|
1245 flags = buf[wlen]; /* <flags> */
|
|
1246 buf[wlen] = NUL;
|
|
1247
|
|
1248 /* Get more flags if they're there. */
|
|
1249 if (flags & BWF_SECOND)
|
|
1250 flags += getc(fd) << 8; /* <flags2> */
|
|
1251
|
|
1252 if (flags & BWF_KEEPCAP)
|
|
1253 {
|
|
1254 /* Read <caselen> and <caseword> first, its length may differ from
|
|
1255 * the case-folded word. Note: this should only happen after the
|
|
1256 * basic word! */
|
|
1257 wlen = getc(fd);
|
|
1258 if (wlen == EOF)
|
|
1259 goto truncerr;
|
|
1260 for (i = 0; i < wlen; ++i)
|
|
1261 cbuf[i] = getc(fd);
|
|
1262 cbuf[i] = NUL;
|
|
1263 }
|
|
1264
|
|
1265 /* Find room to store the word in a fword_T. */
|
|
1266 fw = (fword_T *)getroom(lp, &bl_used, (int)sizeof(fword_T) + wlen);
|
|
1267 if (fw == NULL)
|
|
1268 goto errorend;
|
|
1269 mch_memmove(fw->fw_word, (flags & BWF_KEEPCAP) ? cbuf : buf, wlen + 1);
|
|
1270 fw->fw_flags = flags;
|
|
1271
|
|
1272 hash = hash_hash(buf);
|
|
1273 hi = hash_lookup(&lp->sl_words, buf, hash);
|
|
1274 if (HASHITEM_EMPTY(hi))
|
|
1275 {
|
|
1276 if (hash_add_item(&lp->sl_words, hi, fw->fw_word, hash) == FAIL)
|
|
1277 goto errorend;
|
|
1278 fw->fw_next = NULL;
|
|
1279 }
|
|
1280 else
|
|
1281 {
|
|
1282 /* Already have this basic word in the hashtable, this one will
|
|
1283 * have different case flags. */
|
|
1284 fw2 = HI2FWORD(hi);
|
|
1285 fw->fw_next = fw2->fw_next;
|
|
1286 fw2->fw_next = fw;
|
|
1287 --widx; /* don't count this one */
|
|
1288 }
|
|
1289
|
|
1290 /* Optional prefixes and suffixes. */
|
|
1291 if (flags & BWF_PREFIX)
|
|
1292 fw->fw_prefixcnt = spell_load_affixes(fd, lp, &bl_used,
|
|
1293 prefm, &fw->fw_prefix);
|
|
1294 else
|
|
1295 fw->fw_prefixcnt = 0;
|
|
1296 if (flags & BWF_SUFFIX)
|
|
1297 fw->fw_suffixcnt = spell_load_affixes(fd, lp, &bl_used,
|
|
1298 suffm, &fw->fw_suffix);
|
|
1299 else
|
|
1300 fw->fw_suffixcnt = 0;
|
|
1301
|
|
1302 if (flags & BWF_REGION)
|
|
1303 fw->fw_region = getc(fd); /* <region> */
|
|
1304 else
|
|
1305 fw->fw_region = REGION_ALL;
|
|
1306
|
|
1307 fw->fw_adds = NULL;
|
|
1308 if (flags & BWF_ADDS)
|
|
1309 {
|
|
1310 adds = (getc(fd) << 8) + getc(fd); /* <addcnt> */
|
|
1311
|
|
1312 while (--adds >= 0)
|
|
1313 {
|
|
1314 /* <add>: <addflags> <addlen> [<leadlen> <addstring>]
|
|
1315 * [<region>] */
|
|
1316 flags = getc(fd); /* <addflags> */
|
|
1317 addlen = getc(fd); /* <addlen> */
|
|
1318 if (addlen == EOF)
|
|
1319 goto truncerr;
|
|
1320 if (addlen >= MAXWLEN)
|
|
1321 goto formerr;
|
|
1322
|
|
1323 if (addlen > 0)
|
|
1324 {
|
|
1325 leadlen = getc(fd); /* <leadlen> */
|
|
1326 for (i = 0; i < addlen; ++i) /* <addstring> */
|
|
1327 cbuf[i] = getc(fd);
|
|
1328 cbuf[i] = NUL;
|
|
1329 }
|
|
1330 else
|
|
1331 leadlen = 0;
|
|
1332
|
|
1333 if (flags & ADD_KEEPCAP)
|
|
1334 {
|
|
1335 /* <addstring> is in original case, need to get
|
|
1336 * case-folded word too. */
|
|
1337 (void)str_foldcase(cbuf, addlen, fbuf, MAXWLEN);
|
|
1338 flen = addlen - leadlen + 1;
|
|
1339 addlen = STRLEN(fbuf);
|
|
1340 }
|
|
1341 else
|
|
1342 flen = 0;
|
|
1343
|
|
1344 aw = (addword_T *)getroom(lp, &bl_used,
|
|
1345 sizeof(addword_T) + addlen + flen);
|
|
1346 if (aw == NULL)
|
|
1347 goto errorend;
|
|
1348 aw->aw_next = fw->fw_adds;
|
|
1349 fw->fw_adds = aw;
|
|
1350 aw->aw_leadlen = leadlen;
|
|
1351
|
|
1352 if (flags & ADD_KEEPCAP)
|
|
1353 {
|
|
1354 /* Put the addition in original case after the case-folded
|
|
1355 * string. */
|
|
1356 STRCPY(aw->aw_word, fbuf);
|
|
1357 STRCPY(aw->aw_word + addlen + 1, cbuf + leadlen);
|
|
1358 }
|
|
1359 else
|
|
1360 STRCPY(aw->aw_word, cbuf);
|
|
1361
|
|
1362 aw->aw_flags = flags;
|
|
1363 aw->aw_wordlen = addlen;
|
|
1364
|
|
1365 if (flags & ADD_REGION)
|
|
1366 aw->aw_region = getc(fd); /* <region> */
|
|
1367 else
|
|
1368 aw->aw_region = REGION_ALL;
|
|
1369 }
|
|
1370 }
|
|
1371 }
|
|
1372 goto end_OK;
|
|
1373
|
|
1374 errorend:
|
|
1375 lp->sl_error = TRUE;
|
|
1376 end_OK:
|
|
1377 if (fd != NULL)
|
|
1378 fclose(fd);
|
|
1379 hash_unlock(&lp->sl_words);
|
|
1380 sourcing_name = save_sourcing_name;
|
|
1381 sourcing_lnum = save_sourcing_lnum;
|
|
1382 }
|
|
1383
|
|
1384 /*
|
|
1385 * Read a list of affixes from the spell file.
|
|
1386 */
|
|
1387 static int
|
|
1388 spell_load_affixes(fd, lp, bl_usedp, affm, affp)
|
|
1389 FILE *fd;
|
|
1390 slang_T *lp;
|
|
1391 int *bl_usedp;
|
|
1392 int affm;
|
|
1393 void **affp;
|
|
1394 {
|
|
1395 int cnt;
|
|
1396 int i, n;
|
|
1397 char_u *p;
|
|
1398
|
|
1399 cnt = getc(fd); /* <affixcnt> */
|
|
1400 if (cnt == EOF)
|
|
1401 return 0;
|
|
1402
|
|
1403 /* Get room to store the affixNR list, either as char_u (1
|
|
1404 * byte) or short_u (2 bytes). */
|
|
1405 p = (char_u *)getroom(lp, bl_usedp, cnt * affm);
|
|
1406 if (p == NULL)
|
|
1407 return 0;
|
|
1408 *affp = p;
|
|
1409 for (n = 0; n < cnt; ++n)
|
|
1410 {
|
|
1411 i = getc(fd); /* <affixNR> */
|
|
1412 if (affm > 1)
|
|
1413 {
|
|
1414 i = (i << 8) + getc(fd);
|
|
1415 *(short_u *)p = i;
|
|
1416 p += sizeof(short_u);
|
|
1417 }
|
|
1418 else
|
|
1419 {
|
|
1420 *(char_u *)p = i;
|
|
1421 ++p;
|
|
1422 }
|
|
1423 }
|
|
1424 return cnt;
|
|
1425 }
|
|
1426
|
|
1427 /*
|
|
1428 * Get part of an sblock_T, at least "len" bytes long.
|
|
1429 * Returns NULL when out of memory.
|
|
1430 */
|
|
1431 static void *
|
|
1432 getroom(lp, bl_used, len)
|
|
1433 slang_T *lp; /* lp->sl_block is current block or NULL */
|
|
1434 int *bl_used; /* used up from current block */
|
|
1435 int len; /* length needed */
|
|
1436 {
|
|
1437 char_u *p;
|
|
1438 sblock_T *bl = lp->sl_block;
|
|
1439
|
|
1440 if (bl == NULL || *bl_used + len > SBLOCKSIZE)
|
|
1441 {
|
|
1442 /* Allocate a block of memory. This is not freed until spell_reload()
|
|
1443 * is called. */
|
|
1444 bl = (sblock_T *)alloc((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
|
|
1445 if (bl == NULL)
|
|
1446 return NULL;
|
|
1447 bl->sb_next = lp->sl_block;
|
|
1448 lp->sl_block = bl;
|
|
1449 *bl_used = 0;
|
|
1450 }
|
|
1451
|
|
1452 p = bl->sb_data + *bl_used;
|
|
1453 *bl_used += len;
|
|
1454
|
|
1455 return p;
|
|
1456 }
|
|
1457
|
|
1458 /*
|
|
1459 * Parse 'spelllang' and set buf->b_langp accordingly.
|
|
1460 * Returns an error message or NULL.
|
|
1461 */
|
|
1462 char_u *
|
|
1463 did_set_spelllang(buf)
|
|
1464 buf_T *buf;
|
|
1465 {
|
|
1466 garray_T ga;
|
|
1467 char_u *lang;
|
|
1468 char_u *e;
|
|
1469 char_u *region;
|
|
1470 int region_mask;
|
|
1471 slang_T *lp;
|
|
1472 int c;
|
|
1473 char_u lbuf[MAXWLEN + 1];
|
|
1474
|
|
1475 ga_init2(&ga, sizeof(langp_T), 2);
|
|
1476
|
|
1477 /* loop over comma separated languages. */
|
|
1478 for (lang = buf->b_p_spl; *lang != NUL; lang = e)
|
|
1479 {
|
|
1480 e = vim_strchr(lang, ',');
|
|
1481 if (e == NULL)
|
|
1482 e = lang + STRLEN(lang);
|
|
1483 if (e > lang + 2)
|
|
1484 {
|
|
1485 if (e - lang >= MAXWLEN)
|
|
1486 {
|
|
1487 ga_clear(&ga);
|
|
1488 return e_invarg;
|
|
1489 }
|
|
1490 if (lang[2] == '_')
|
|
1491 region = lang + 3;
|
|
1492 }
|
|
1493 else
|
|
1494 region = NULL;
|
|
1495
|
|
1496 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
|
|
1497 if (STRNICMP(lp->sl_name, lang, 2) == 0)
|
|
1498 break;
|
|
1499
|
|
1500 if (lp == NULL)
|
|
1501 {
|
|
1502 /* Not found, load the language. */
|
|
1503 STRNCPY(lbuf, lang, e - lang);
|
|
1504 lbuf[e - lang] = NUL;
|
|
1505 if (region != NULL)
|
|
1506 mch_memmove(lbuf + 2, lbuf + 5, e - lang - 4);
|
|
1507 lp = spell_load_lang(lbuf);
|
|
1508 }
|
|
1509
|
|
1510 if (lp != NULL)
|
|
1511 {
|
|
1512 if (region == NULL)
|
|
1513 region_mask = REGION_ALL;
|
|
1514 else
|
|
1515 {
|
|
1516 /* find region in sl_regions */
|
|
1517 c = find_region(lp->sl_regions, region);
|
|
1518 if (c == REGION_ALL)
|
|
1519 {
|
|
1520 c = *e;
|
|
1521 *e = NUL;
|
|
1522 smsg((char_u *)_("Warning: region %s not supported"), lang);
|
|
1523 *e = c;
|
|
1524 region_mask = REGION_ALL;
|
|
1525 }
|
|
1526 else
|
|
1527 region_mask = 1 << c;
|
|
1528 }
|
|
1529
|
|
1530 if (ga_grow(&ga, 1) == FAIL)
|
|
1531 {
|
|
1532 ga_clear(&ga);
|
|
1533 return e_outofmem;
|
|
1534 }
|
|
1535 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
|
|
1536 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
|
|
1537 ++ga.ga_len;
|
|
1538 }
|
|
1539
|
|
1540 if (*e == ',')
|
|
1541 ++e;
|
|
1542 }
|
|
1543
|
|
1544 /* Add a NULL entry to mark the end of the list. */
|
|
1545 if (ga_grow(&ga, 1) == FAIL)
|
|
1546 {
|
|
1547 ga_clear(&ga);
|
|
1548 return e_outofmem;
|
|
1549 }
|
|
1550 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
|
|
1551 ++ga.ga_len;
|
|
1552
|
|
1553 /* Everything is fine, store the new b_langp value. */
|
|
1554 ga_clear(&buf->b_langp);
|
|
1555 buf->b_langp = ga;
|
|
1556
|
|
1557 return NULL;
|
|
1558 }
|
|
1559
|
|
1560 /*
|
|
1561 * Find the region "region[2]" in "rp" (points to "sl_regions").
|
|
1562 * Each region is simply stored as the two characters of it's name.
|
|
1563 * Returns the index if found, REGION_ALL if not found.
|
|
1564 */
|
|
1565 static int
|
|
1566 find_region(rp, region)
|
|
1567 char_u *rp;
|
|
1568 char_u *region;
|
|
1569 {
|
|
1570 int i;
|
|
1571
|
|
1572 for (i = 0; ; i += 2)
|
|
1573 {
|
|
1574 if (rp[i] == NUL)
|
|
1575 return REGION_ALL;
|
|
1576 if (rp[i] == region[0] && rp[i + 1] == region[1])
|
|
1577 break;
|
|
1578 }
|
|
1579 return i / 2;
|
|
1580 }
|
|
1581
|
|
1582 /*
|
|
1583 * Return type of word:
|
|
1584 * w word 0
|
|
1585 * Word BWF_ONECAP
|
|
1586 * W WORD BWF_ALLCAP
|
|
1587 * WoRd wOrd BWF_KEEPCAP
|
|
1588 */
|
|
1589 static int
|
|
1590 captype(word, end)
|
|
1591 char_u *word;
|
|
1592 char_u *end;
|
|
1593 {
|
|
1594 char_u *p;
|
|
1595 int c;
|
|
1596 int firstcap;
|
|
1597 int allcap;
|
|
1598 int past_second = FALSE; /* past second word char */
|
|
1599
|
|
1600 /* find first letter */
|
|
1601 for (p = word; !spell_iswordc(p); mb_ptr_adv(p))
|
|
1602 if (p >= end)
|
|
1603 return 0; /* only non-word characters, illegal word */
|
|
1604 #ifdef FEAT_MBYTE
|
|
1605 c = mb_ptr2char_adv(&p);
|
|
1606 #else
|
|
1607 c = *p++;
|
|
1608 #endif
|
|
1609 firstcap = allcap = MB_ISUPPER(c);
|
|
1610
|
|
1611 /*
|
|
1612 * Need to check all letters to find a word with mixed upper/lower.
|
|
1613 * But a word with an upper char only at start is a ONECAP.
|
|
1614 */
|
|
1615 for ( ; p < end; mb_ptr_adv(p))
|
|
1616 if (spell_iswordc(p))
|
|
1617 {
|
|
1618 #ifdef FEAT_MBYTE
|
|
1619 c = mb_ptr2char(p);
|
|
1620 #else
|
|
1621 c = *p;
|
|
1622 #endif
|
|
1623 if (!MB_ISUPPER(c))
|
|
1624 {
|
|
1625 /* UUl -> KEEPCAP */
|
|
1626 if (past_second && allcap)
|
|
1627 return BWF_KEEPCAP;
|
|
1628 allcap = FALSE;
|
|
1629 }
|
|
1630 else if (!allcap)
|
|
1631 /* UlU -> KEEPCAP */
|
|
1632 return BWF_KEEPCAP;
|
|
1633 past_second = TRUE;
|
|
1634 }
|
|
1635
|
|
1636 if (allcap)
|
|
1637 return BWF_ALLCAP;
|
|
1638 if (firstcap)
|
|
1639 return BWF_ONECAP;
|
|
1640 return 0;
|
|
1641 }
|
|
1642
|
|
1643 # if defined(FEAT_MBYTE) || defined(PROTO)
|
|
1644 /*
|
|
1645 * Clear all spelling tables and reload them.
|
|
1646 * Used after 'encoding' is set.
|
|
1647 */
|
|
1648 void
|
|
1649 spell_reload()
|
|
1650 {
|
|
1651 buf_T *buf;
|
|
1652 slang_T *lp;
|
|
1653
|
|
1654 /* Initialize the table for spell_iswordc(). */
|
|
1655 init_spell_chartab();
|
|
1656
|
|
1657 /* Unload all allocated memory. */
|
|
1658 while (first_lang != NULL)
|
|
1659 {
|
|
1660 lp = first_lang;
|
|
1661 first_lang = lp->sl_next;
|
|
1662 slang_free(lp);
|
|
1663 }
|
|
1664
|
|
1665 /* Go through all buffers and handle 'spelllang'. */
|
|
1666 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
|
|
1667 {
|
|
1668 ga_clear(&buf->b_langp);
|
|
1669 if (*buf->b_p_spl != NUL)
|
|
1670 did_set_spelllang(buf);
|
|
1671 }
|
|
1672 }
|
|
1673 # endif
|
|
1674
|
|
1675 /*
|
|
1676 * Recognizing words uses a two-step mechanism:
|
|
1677 * 1. Locate a basic word, made out of word characters only and separated by
|
|
1678 * non-word characters.
|
|
1679 * 2. When a basic word is found, check if (possibly required) additions
|
|
1680 * before and after the word are present.
|
|
1681 *
|
|
1682 * Both mechanisms use affixes (prefixes and suffixes) to reduce the number of
|
|
1683 * words. When no matching word was found in the hashtable the start of the
|
|
1684 * word is checked for matching prefixes and the end of the word for matching
|
|
1685 * suffixes. All matching affixes are removed and then the resulting word is
|
|
1686 * searched for. If found it is checked if it supports the used affix.
|
|
1687 */
|
|
1688
|
|
1689
|
|
1690 #if defined(FEAT_MBYTE) || defined(PROTO)
|
|
1691 /*
|
|
1692 * Functions for ":mkspell".
|
|
1693 * Only possible with the multi-byte feature.
|
|
1694 */
|
|
1695
|
|
1696 #define MAXLINELEN 300 /* Maximum length in bytes of a line in a .aff
|
|
1697 and .dic file. */
|
|
1698 /*
|
|
1699 * Main structure to store the contents of a ".aff" file.
|
|
1700 */
|
|
1701 typedef struct afffile_S
|
|
1702 {
|
|
1703 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
|
|
1704 char_u *af_try; /* "TRY" line in "af_enc" encoding */
|
|
1705 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
|
|
1706 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
|
|
1707 garray_T af_rep; /* list of repentry_T entries from REP lines */
|
|
1708 } afffile_T;
|
|
1709
|
|
1710 typedef struct affentry_S affentry_T;
|
|
1711
|
|
1712 /* Affix header from ".aff" file. Used for af_pref and af_suff. */
|
|
1713 typedef struct affheader_S
|
|
1714 {
|
|
1715 char_u ah_key[2]; /* key for hashtable == name of affix entry */
|
|
1716 int ah_combine;
|
|
1717 affentry_T *ah_first; /* first affix entry */
|
|
1718 short_u ah_affnr; /* used in get_new_aff() */
|
|
1719 } affheader_T;
|
|
1720
|
|
1721 #define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
|
|
1722
|
|
1723 /* Affix entry from ".aff" file. Used for prefixes and suffixes. */
|
|
1724 struct affentry_S
|
|
1725 {
|
|
1726 affentry_T *ae_next; /* next affix with same name/number */
|
|
1727 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
|
|
1728 char_u *ae_add; /* text to add to basic word (can be NULL) */
|
|
1729 char_u *ae_add_nw; /* first non-word character in "ae_add" */
|
|
1730 char_u *ae_cond; /* condition (NULL for ".") */
|
|
1731 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
|
|
1732 short_u ae_affnr; /* for old affix: new affix number */
|
|
1733 };
|
|
1734
|
|
1735 /*
|
|
1736 * Structure to store a word from a ".dic" file.
|
|
1737 */
|
|
1738 typedef struct dicword_S
|
|
1739 {
|
|
1740 char_u *dw_affnm; /* original affix names */
|
|
1741 char_u dw_word[1]; /* actually longer: the word in 'encoding' */
|
|
1742 } dicword_T;
|
|
1743
|
|
1744 static dicword_T dumdw;
|
|
1745 #define HI2DW(hi) ((dicword_T *)((hi)->hi_key - (dumdw.dw_word - (char_u *)&dumdw)))
|
|
1746
|
|
1747 /*
|
|
1748 * Structure to store a basic word for the spell file.
|
|
1749 * This is used for ":mkspell", not for spell checking.
|
|
1750 */
|
|
1751 typedef struct basicword_S basicword_T;
|
|
1752 struct basicword_S
|
|
1753 {
|
|
1754 basicword_T *bw_next; /* next word with same basic word */
|
|
1755 basicword_T *bw_cnext; /* next word with same caps */
|
|
1756 int bw_flags; /* BWF_ flags */
|
|
1757 garray_T bw_prefix; /* table with prefix numbers */
|
|
1758 garray_T bw_suffix; /* table with suffix numbers */
|
|
1759 int bw_region; /* region bits */
|
|
1760 char_u *bw_caseword; /* keep-case word */
|
|
1761 char_u *bw_leadstring; /* must come before bw_word */
|
|
1762 char_u *bw_addstring; /* must come after bw_word */
|
|
1763 char_u bw_word[1]; /* actually longer: word case folded */
|
|
1764 };
|
|
1765
|
|
1766 static basicword_T dumbw;
|
|
1767 #define KEY2BW(p) ((basicword_T *)((p) - (dumbw.bw_word - (char_u *)&dumbw)))
|
|
1768 #define HI2BW(hi) KEY2BW((hi)->hi_key)
|
|
1769
|
|
1770 /* Store the affix number related with a certain string. */
|
|
1771 typedef struct affhash_S
|
|
1772 {
|
|
1773 short_u as_nr; /* the affix nr */
|
|
1774 char_u as_word[1]; /* actually longer */
|
|
1775 } affhash_T;
|
|
1776
|
|
1777 static affhash_T dumas;
|
|
1778 #define HI2AS(hi) ((affhash_T *)((hi)->hi_key - (dumas.as_word - (char_u *)&dumas)))
|
|
1779
|
|
1780
|
|
1781 static afffile_T *spell_read_aff __ARGS((char_u *fname, vimconv_T *conv));
|
|
1782 static void spell_free_aff __ARGS((afffile_T *aff));
|
|
1783 static int spell_read_dic __ARGS((hashtab_T *ht, char_u *fname, vimconv_T *conv));
|
|
1784 static int get_new_aff __ARGS((hashtab_T *oldaff, garray_T *gap));
|
|
1785 static void spell_free_dic __ARGS((hashtab_T *dic));
|
|
1786 static int same_affentries __ARGS((affheader_T *ah1, affheader_T *ah2));
|
|
1787 static void add_affhash __ARGS((hashtab_T *ht, char_u *key, int newnr));
|
|
1788 static void clear_affhash __ARGS((hashtab_T *ht));
|
|
1789 static void trans_affixes __ARGS((dicword_T *dw, basicword_T *bw, afffile_T *oldaff, hashtab_T *newwords));
|
|
1790 static int build_wordlist __ARGS((hashtab_T *newwords, hashtab_T *oldwords, afffile_T *oldaff, int regionmask));
|
|
1791 static void combine_regions __ARGS((hashtab_T *newwords));
|
|
1792 static int same_affixes __ARGS((basicword_T *bw, basicword_T *nbw));
|
|
1793 static void expand_affixes __ARGS((hashtab_T *newwords, garray_T *prefgap, garray_T *suffgap));
|
|
1794 static void expand_one_aff __ARGS((basicword_T *bw, garray_T *add_words, affentry_T *pae, affentry_T *sae));
|
|
1795 static void add_to_wordlist __ARGS((hashtab_T *newwords, basicword_T *bw));
|
|
1796 static void put_bytes __ARGS((FILE *fd, long_u nr, int len));
|
|
1797 static void write_affix __ARGS((FILE *fd, affheader_T *ah));
|
|
1798 static void write_affixlist __ARGS((FILE *fd, garray_T *aff, int bytes));
|
|
1799 static void write_vim_spell __ARGS((char_u *fname, garray_T *prefga, garray_T *suffga, hashtab_T *newwords, int regcount, char_u *regchars));
|
|
1800 static void write_bword __ARGS((FILE *fd, basicword_T *bw, int lowcap, basicword_T **prevbw, int regionmask, int prefm, int suffm));
|
|
1801 static void free_wordtable __ARGS((hashtab_T *ht));
|
|
1802 static void free_basicword __ARGS((basicword_T *bw));
|
|
1803 static void free_affixentries __ARGS((affentry_T *first));
|
|
1804
|
|
1805 /*
|
|
1806 * Read an affix ".aff" file.
|
|
1807 * Returns an afffile_T, NULL for failure.
|
|
1808 */
|
|
1809 static afffile_T *
|
|
1810 spell_read_aff(fname, conv)
|
|
1811 char_u *fname;
|
|
1812 vimconv_T *conv; /* info for encoding conversion */
|
|
1813 {
|
|
1814 FILE *fd;
|
|
1815 afffile_T *aff;
|
|
1816 char_u rline[MAXLINELEN];
|
|
1817 char_u *line;
|
|
1818 char_u *pc = NULL;
|
|
1819 char_u *(items[6]);
|
|
1820 int itemcnt;
|
|
1821 char_u *p;
|
|
1822 int lnum = 0;
|
|
1823 affheader_T *cur_aff = NULL;
|
|
1824 int aff_todo = 0;
|
|
1825 hashtab_T *tp;
|
|
1826
|
|
1827 fd = fopen((char *)fname, "r");
|
|
1828 if (fd == NULL)
|
|
1829 {
|
|
1830 EMSG2(_(e_notopen), fname);
|
|
1831 return NULL;
|
|
1832 }
|
|
1833
|
|
1834 smsg((char_u *)_("Reading affix file %s..."), fname);
|
|
1835 out_flush();
|
|
1836
|
|
1837 aff = (afffile_T *)alloc_clear((unsigned)sizeof(afffile_T));
|
|
1838 if (aff == NULL)
|
|
1839 return NULL;
|
|
1840 hash_init(&aff->af_pref);
|
|
1841 hash_init(&aff->af_suff);
|
|
1842 ga_init2(&aff->af_rep, (int)sizeof(repentry_T), 20);
|
|
1843
|
|
1844 /*
|
|
1845 * Read all the lines in the file one by one.
|
|
1846 */
|
|
1847 while (!vim_fgets(rline, MAXLINELEN, fd))
|
|
1848 {
|
|
1849 ++lnum;
|
|
1850
|
|
1851 /* Skip comment lines. */
|
|
1852 if (*rline == '#')
|
|
1853 continue;
|
|
1854
|
|
1855 /* Convert from "SET" to 'encoding' when needed. */
|
|
1856 vim_free(pc);
|
|
1857 if (conv->vc_type != CONV_NONE)
|
|
1858 {
|
|
1859 pc = string_convert(conv, rline, NULL);
|
|
1860 line = pc;
|
|
1861 }
|
|
1862 else
|
|
1863 {
|
|
1864 pc = NULL;
|
|
1865 line = rline;
|
|
1866 }
|
|
1867
|
|
1868 /* Split the line up in white separated items. Put a NUL after each
|
|
1869 * item. */
|
|
1870 itemcnt = 0;
|
|
1871 for (p = line; ; )
|
|
1872 {
|
|
1873 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
|
|
1874 ++p;
|
|
1875 if (*p == NUL)
|
|
1876 break;
|
|
1877 items[itemcnt++] = p;
|
|
1878 while (*p > ' ') /* skip until white space or CR/NL */
|
|
1879 ++p;
|
|
1880 if (*p == NUL)
|
|
1881 break;
|
|
1882 *p++ = NUL;
|
|
1883 }
|
|
1884
|
|
1885 /* Handle non-empty lines. */
|
|
1886 if (itemcnt > 0)
|
|
1887 {
|
|
1888 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
|
|
1889 && aff->af_enc == NULL)
|
|
1890 {
|
|
1891 if (aff->af_enc != NULL)
|
|
1892 smsg((char_u *)_("Duplicate SET line ignored in %s line %d: %s"),
|
|
1893 fname, lnum, line);
|
|
1894 else
|
|
1895 {
|
|
1896 /* Setup for conversion from "ENC" to 'encoding'. */
|
|
1897 aff->af_enc = enc_canonize(items[1]);
|
|
1898 if (aff->af_enc != NULL
|
|
1899 && convert_setup(conv, aff->af_enc, p_enc) == FAIL)
|
|
1900 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
|
|
1901 fname, aff->af_enc, p_enc);
|
|
1902 }
|
|
1903 }
|
|
1904 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2
|
|
1905 && aff->af_try == NULL)
|
|
1906 aff->af_try = vim_strsave(items[1]);
|
|
1907 else if ((STRCMP(items[0], "PFX") == 0
|
|
1908 || STRCMP(items[0], "SFX") == 0)
|
|
1909 && aff_todo == 0
|
|
1910 && itemcnt == 4)
|
|
1911 {
|
|
1912 /* New affix letter. */
|
|
1913 cur_aff = (affheader_T *)alloc((unsigned)sizeof(affheader_T));
|
|
1914 if (cur_aff == NULL)
|
|
1915 break;
|
|
1916 cur_aff->ah_key[0] = *items[1];
|
|
1917 cur_aff->ah_key[1] = NUL;
|
|
1918 if (items[1][1] != NUL)
|
|
1919 smsg((char_u *)_("Affix name too long in %s line %d: %s"),
|
|
1920 fname, lnum, items[1]);
|
|
1921 if (*items[2] == 'Y')
|
|
1922 cur_aff->ah_combine = TRUE;
|
|
1923 else if (*items[2] == 'N')
|
|
1924 cur_aff->ah_combine = FALSE;
|
|
1925 else if (p_verbose > 0)
|
|
1926 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
|
|
1927 fname, lnum, items[2]);
|
|
1928 cur_aff->ah_first = NULL;
|
|
1929 if (*items[0] == 'P')
|
|
1930 tp = &aff->af_pref;
|
|
1931 else
|
|
1932 tp = &aff->af_suff;
|
|
1933 if (!HASHITEM_EMPTY(hash_find(tp, cur_aff->ah_key)))
|
|
1934 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
|
|
1935 fname, lnum, items[1]);
|
|
1936 else
|
|
1937 hash_add(tp, cur_aff->ah_key);
|
|
1938
|
|
1939 aff_todo = atoi((char *)items[3]);
|
|
1940 }
|
|
1941 else if ((STRCMP(items[0], "PFX") == 0
|
|
1942 || STRCMP(items[0], "SFX") == 0)
|
|
1943 && aff_todo > 0
|
|
1944 && STRCMP(cur_aff->ah_key, items[1]) == 0
|
|
1945 && itemcnt == 5)
|
|
1946 {
|
|
1947 affentry_T *aff_entry;
|
|
1948
|
|
1949 /* New item for an affix letter. */
|
|
1950 --aff_todo;
|
|
1951 aff_entry = (affentry_T *)alloc_clear(
|
|
1952 (unsigned)sizeof(affentry_T));
|
|
1953 if (aff_entry == NULL)
|
|
1954 break;
|
|
1955 aff_entry->ae_next = cur_aff->ah_first;
|
|
1956 cur_aff->ah_first = aff_entry;
|
|
1957 if (STRCMP(items[2], "0") != 0)
|
|
1958 aff_entry->ae_chop = vim_strsave(items[2]);
|
|
1959 if (STRCMP(items[3], "0") != 0)
|
|
1960 aff_entry->ae_add = vim_strsave(items[3]);
|
|
1961 if (STRCMP(items[4], ".") != 0)
|
|
1962 {
|
|
1963 char_u buf[MAXLINELEN];
|
|
1964
|
|
1965 aff_entry->ae_cond = vim_strsave(items[4]);
|
|
1966 if (*items[0] == 'P')
|
|
1967 sprintf((char *)buf, "^%s", items[4]);
|
|
1968 else
|
|
1969 sprintf((char *)buf, "%s$", items[4]);
|
|
1970 aff_entry->ae_prog = vim_regcomp(buf, RE_MAGIC + RE_STRING);
|
|
1971 }
|
|
1972 }
|
|
1973 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
|
|
1974 /* Ignore REP count */;
|
|
1975 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
|
|
1976 {
|
|
1977 repentry_T *rp;
|
|
1978
|
|
1979 /* REP item */
|
|
1980 if (ga_grow(&aff->af_rep, 1) == FAIL)
|
|
1981 break;
|
|
1982 rp = ((repentry_T *)aff->af_rep.ga_data) + aff->af_rep.ga_len;
|
|
1983 rp->re_from = vim_strsave(items[1]);
|
|
1984 rp->re_to = vim_strsave(items[2]);
|
|
1985 ++aff->af_rep.ga_len;
|
|
1986 }
|
|
1987 else if (p_verbose > 0)
|
|
1988 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
|
|
1989 fname, lnum, items[0]);
|
|
1990 }
|
|
1991
|
|
1992 }
|
|
1993
|
|
1994 vim_free(pc);
|
|
1995 fclose(fd);
|
|
1996 return aff;
|
|
1997 }
|
|
1998
|
|
1999 /*
|
|
2000 * Free the structure filled by spell_read_aff().
|
|
2001 */
|
|
2002 static void
|
|
2003 spell_free_aff(aff)
|
|
2004 afffile_T *aff;
|
|
2005 {
|
|
2006 hashtab_T *ht;
|
|
2007 hashitem_T *hi;
|
|
2008 int todo;
|
|
2009 int i;
|
|
2010 repentry_T *rp;
|
|
2011 affheader_T *ah;
|
|
2012
|
|
2013 vim_free(aff->af_enc);
|
|
2014 vim_free(aff->af_try);
|
|
2015
|
|
2016 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
|
|
2017 {
|
|
2018 todo = ht->ht_used;
|
|
2019 for (hi = ht->ht_array; todo > 0; ++hi)
|
|
2020 {
|
|
2021 if (!HASHITEM_EMPTY(hi))
|
|
2022 {
|
|
2023 --todo;
|
|
2024 ah = HI2AH(hi);
|
|
2025 free_affixentries(ah->ah_first);
|
|
2026 vim_free(ah);
|
|
2027 }
|
|
2028 }
|
|
2029 if (ht == &aff->af_suff)
|
|
2030 break;
|
|
2031 }
|
|
2032 hash_clear(&aff->af_pref);
|
|
2033 hash_clear(&aff->af_suff);
|
|
2034
|
|
2035 for (i = 0; i < aff->af_rep.ga_len; ++i)
|
|
2036 {
|
|
2037 rp = ((repentry_T *)aff->af_rep.ga_data) + i;
|
|
2038 vim_free(rp->re_from);
|
|
2039 vim_free(rp->re_to);
|
|
2040 }
|
|
2041 ga_clear(&aff->af_rep);
|
|
2042
|
|
2043 vim_free(aff);
|
|
2044 }
|
|
2045
|
|
2046 /*
|
|
2047 * Read a dictionary ".dic" file.
|
|
2048 * Returns OK or FAIL;
|
|
2049 * Each entry in the hashtab_T is a dicword_T.
|
|
2050 */
|
|
2051 static int
|
|
2052 spell_read_dic(ht, fname, conv)
|
|
2053 hashtab_T *ht;
|
|
2054 char_u *fname;
|
|
2055 vimconv_T *conv; /* info for encoding conversion */
|
|
2056 {
|
|
2057 char_u line[MAXLINELEN];
|
|
2058 char_u *p;
|
|
2059 dicword_T *dw;
|
|
2060 char_u *pc;
|
|
2061 char_u *w;
|
|
2062 int l;
|
|
2063 hash_T hash;
|
|
2064 hashitem_T *hi;
|
|
2065 FILE *fd;
|
|
2066 int lnum = 1;
|
|
2067
|
|
2068 fd = fopen((char *)fname, "r");
|
|
2069 if (fd == NULL)
|
|
2070 {
|
|
2071 EMSG2(_(e_notopen), fname);
|
|
2072 return FAIL;
|
|
2073 }
|
|
2074
|
|
2075 smsg((char_u *)_("Reading dictionary file %s..."), fname);
|
|
2076 out_flush();
|
|
2077
|
|
2078 /* Read and ignore the first line: word count. */
|
|
2079 (void)vim_fgets(line, MAXLINELEN, fd);
|
|
2080 if (!isdigit(*skipwhite(line)))
|
|
2081 EMSG2(_("E760: No word count in %s"), fname);
|
|
2082
|
|
2083 /*
|
|
2084 * Read all the lines in the file one by one.
|
|
2085 * The words are converted to 'encoding' here, before being added to
|
|
2086 * the hashtable.
|
|
2087 */
|
|
2088 while (!vim_fgets(line, MAXLINELEN, fd))
|
|
2089 {
|
|
2090 ++lnum;
|
|
2091
|
|
2092 /* Remove CR, LF and white space from end. */
|
|
2093 l = STRLEN(line);
|
|
2094 while (l > 0 && line[l - 1] <= ' ')
|
|
2095 --l;
|
|
2096 if (l == 0)
|
|
2097 continue; /* empty line */
|
|
2098 line[l] = NUL;
|
|
2099
|
|
2100 /* Find the optional affix names. */
|
|
2101 p = vim_strchr(line, '/');
|
|
2102 if (p != NULL)
|
|
2103 *p++ = NUL;
|
|
2104
|
|
2105 /* Convert from "SET" to 'encoding' when needed. */
|
|
2106 if (conv->vc_type != CONV_NONE)
|
|
2107 {
|
|
2108 pc = string_convert(conv, line, NULL);
|
|
2109 w = pc;
|
|
2110 }
|
|
2111 else
|
|
2112 {
|
|
2113 pc = NULL;
|
|
2114 w = line;
|
|
2115 }
|
|
2116
|
|
2117 dw = (dicword_T *)alloc_clear((unsigned)sizeof(dicword_T)
|
|
2118 + STRLEN(w));
|
|
2119 if (dw == NULL)
|
|
2120 break;
|
|
2121 STRCPY(dw->dw_word, w);
|
|
2122 vim_free(pc);
|
|
2123
|
|
2124 hash = hash_hash(dw->dw_word);
|
|
2125 hi = hash_lookup(ht, dw->dw_word, hash);
|
|
2126 if (!HASHITEM_EMPTY(hi))
|
|
2127 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
|
|
2128 fname, lnum, line);
|
|
2129 else
|
|
2130 hash_add_item(ht, hi, dw->dw_word, hash);
|
|
2131
|
|
2132 if (p != NULL)
|
|
2133 dw->dw_affnm = vim_strsave(p);
|
|
2134 }
|
|
2135
|
|
2136 fclose(fd);
|
|
2137 return OK;
|
|
2138 }
|
|
2139
|
|
2140 /*
|
|
2141 * Free the structure filled by spell_read_dic().
|
|
2142 */
|
|
2143 static void
|
|
2144 spell_free_dic(dic)
|
|
2145 hashtab_T *dic;
|
|
2146 {
|
|
2147 int todo;
|
|
2148 dicword_T *dw;
|
|
2149 hashitem_T *hi;
|
|
2150
|
|
2151 todo = dic->ht_used;
|
|
2152 for (hi = dic->ht_array; todo > 0; ++hi)
|
|
2153 {
|
|
2154 if (!HASHITEM_EMPTY(hi))
|
|
2155 {
|
|
2156 --todo;
|
|
2157 dw = HI2DW(hi);
|
|
2158 vim_free(dw->dw_affnm);
|
|
2159 vim_free(dw);
|
|
2160 }
|
|
2161 }
|
|
2162 hash_clear(dic);
|
|
2163 }
|
|
2164
|
|
2165 /*
|
|
2166 * Take the affixes read by spell_read_aff() and add them to the new list.
|
|
2167 * Attempts to re-use the same number for identical affixes (ignoring the
|
|
2168 * condition, since we remove that). That is especially important when using
|
|
2169 * multiple regions.
|
|
2170 * Returns OK or FAIL;
|
|
2171 */
|
|
2172 static int
|
|
2173 get_new_aff(oldaff, gap)
|
|
2174 hashtab_T *oldaff; /* hashtable with affheader_T */
|
|
2175 garray_T *gap; /* table with new affixes */
|
|
2176 {
|
|
2177 int oldtodo;
|
|
2178 affheader_T *oldah, *newah, *gapah;
|
|
2179 affentry_T *oldae, *newae;
|
|
2180 hashitem_T *oldhi;
|
|
2181 hashitem_T *hi;
|
|
2182 hashtab_T condht; /* conditions already found */
|
|
2183 char_u condkey[MAXLINELEN];
|
|
2184 int newnr;
|
|
2185 int gapnr;
|
|
2186 int retval = OK;
|
|
2187 char_u *p;
|
|
2188 garray_T tga;
|
|
2189
|
|
2190 /*
|
|
2191 * Loop over all the old affix names.
|
|
2192 */
|
|
2193 oldtodo = oldaff->ht_used;
|
|
2194 for (oldhi = oldaff->ht_array; oldtodo > 0 && retval == OK; ++oldhi)
|
|
2195 {
|
|
2196 if (!HASHITEM_EMPTY(oldhi))
|
|
2197 {
|
|
2198 --oldtodo;
|
|
2199 oldah = (affheader_T *)oldhi->hi_key;
|
|
2200
|
|
2201 /* Put entries with the same condition under the same new affix
|
|
2202 * nr in "tga". Use hashtable "condht" to find them. */
|
|
2203 ga_init2(&tga, sizeof(affheader_T), 10);
|
|
2204 hash_init(&condht);
|
|
2205
|
|
2206 /*
|
|
2207 * Loop over all affixes with the same name.
|
|
2208 * The affixes with the same condition will get the same number,
|
|
2209 * since they can be used with the same words.
|
|
2210 * 1. build the lists of new affentry_T, with the headers in "tga".
|
|
2211 * 2. Check if some of the lists already exist in "gap", re-use
|
|
2212 * their number.
|
|
2213 * 3. Assign the new numbers to the old affixes.
|
|
2214 */
|
|
2215
|
|
2216 /* 1. build the lists of new affentry_T. */
|
|
2217 for (oldae = oldah->ah_first; oldae != NULL && retval == OK;
|
|
2218 oldae = oldae->ae_next)
|
|
2219 {
|
|
2220 oldae->ae_add_nw = NULL;
|
|
2221 if (oldae->ae_add != NULL)
|
|
2222 {
|
|
2223 /* Check for non-word characters in the suffix. If there
|
|
2224 * is one this affix will be turned into an addition.
|
|
2225 * This is stored with the old affix, that is where
|
|
2226 * trans_affixes() will check. */
|
|
2227 for (p = oldae->ae_add; *p != NUL; mb_ptr_adv(p))
|
|
2228 if (!spell_iswordc(p))
|
|
2229 break;
|
|
2230 if (*p != NUL)
|
|
2231 oldae->ae_add_nw = p;
|
|
2232 }
|
|
2233
|
|
2234 if (oldae->ae_cond == NULL)
|
|
2235 /* hashtable requires a non-empty key */
|
|
2236 STRCPY(condkey, "---");
|
|
2237 else
|
|
2238 STRCPY(condkey, oldae->ae_cond);
|
|
2239
|
|
2240 /* Look for an existing list with this name and condition. */
|
|
2241 hi = hash_find(&condht, condkey);
|
|
2242 if (!HASHITEM_EMPTY(hi))
|
|
2243 /* Match with existing affix, use that one. */
|
|
2244 newnr = HI2AS(hi)->as_nr;
|
|
2245 else
|
|
2246 {
|
|
2247 /* Add a new affix number. */
|
|
2248 newnr = tga.ga_len;
|
|
2249 if (ga_grow(&tga, 1) == FAIL)
|
|
2250 retval = FAIL;
|
|
2251 else
|
|
2252 {
|
|
2253 newah = ((affheader_T *)tga.ga_data) + newnr;
|
|
2254 newah->ah_combine = oldah->ah_combine;
|
|
2255 newah->ah_first = NULL;
|
|
2256 ++tga.ga_len;
|
|
2257
|
|
2258 /* Add the new list to the condht hashtable. */
|
|
2259 add_affhash(&condht, condkey, newnr);
|
|
2260 }
|
|
2261 }
|
|
2262
|
|
2263 /* Add the new affentry_T to the list. */
|
|
2264 newah = ((affheader_T *)tga.ga_data) + newnr;
|
|
2265 newae = (affentry_T *)alloc_clear((unsigned)sizeof(affentry_T));
|
|
2266 if (newae == NULL)
|
|
2267 retval = FAIL;
|
|
2268 else
|
|
2269 {
|
|
2270 newae->ae_next = newah->ah_first;
|
|
2271 newah->ah_first = newae;
|
|
2272 if (oldae->ae_chop == NULL)
|
|
2273 newae->ae_chop = NULL;
|
|
2274 else
|
|
2275 newae->ae_chop = vim_strsave(oldae->ae_chop);
|
|
2276 if (oldae->ae_add == NULL)
|
|
2277 newae->ae_add = NULL;
|
|
2278 else
|
|
2279 newae->ae_add = vim_strsave(oldae->ae_add);
|
|
2280
|
|
2281 /* The condition is not copied, since the new affix is
|
|
2282 * only used for words where the condition matches. */
|
|
2283 }
|
|
2284 }
|
|
2285
|
|
2286 /* 2. Check if some of the lists already exist, re-use their
|
|
2287 * number. Otherwise add the list to "gap". */
|
|
2288 for (newnr = 0; newnr < tga.ga_len; ++newnr)
|
|
2289 {
|
|
2290 newah = ((affheader_T *)tga.ga_data) + newnr;
|
|
2291 for (gapnr = 0; gapnr < gap->ga_len; ++gapnr)
|
|
2292 {
|
|
2293 gapah = ((affheader_T *)gap->ga_data) + gapnr;
|
|
2294 if (same_affentries(newah, gapah))
|
|
2295 /* Found an existing affheader_T entry with same
|
|
2296 * affentry_T list, use its number. */
|
|
2297 break;
|
|
2298 }
|
|
2299
|
|
2300 newah->ah_affnr = gapnr;
|
|
2301 if (gapnr == gap->ga_len)
|
|
2302 {
|
|
2303 /* This is a new affentry_T list, add it. */
|
|
2304 if (ga_grow(gap, 1) == FAIL)
|
|
2305 retval = FAIL;
|
|
2306 else
|
|
2307 {
|
|
2308 *(((affheader_T *)gap->ga_data) + gap->ga_len) = *newah;
|
|
2309 ++gap->ga_len;
|
|
2310 }
|
|
2311 }
|
|
2312 else
|
|
2313 {
|
|
2314 /* free unused affentry_T list */
|
|
2315 free_affixentries(newah->ah_first);
|
|
2316 }
|
|
2317 }
|
|
2318
|
|
2319 /* 3. Assign the new affix numbers to the old affixes. */
|
|
2320 for (oldae = oldah->ah_first; oldae != NULL && retval == OK;
|
|
2321 oldae = oldae->ae_next)
|
|
2322 {
|
|
2323 if (oldae->ae_cond == NULL)
|
|
2324 /* hashtable requires a non-empty key */
|
|
2325 STRCPY(condkey, "---");
|
|
2326 else
|
|
2327 STRCPY(condkey, oldae->ae_cond);
|
|
2328
|
|
2329 /* Look for an existing affix with this name and condition. */
|
|
2330 hi = hash_find(&condht, condkey);
|
|
2331 if (!HASHITEM_EMPTY(hi))
|
|
2332 /* Match with existing affix, use that one. */
|
|
2333 newnr = HI2AS(hi)->as_nr;
|
|
2334 else
|
|
2335 {
|
|
2336 EMSG(_(e_internal));
|
|
2337 retval = FAIL;
|
|
2338 }
|
|
2339 newah = ((affheader_T *)tga.ga_data) + newnr;
|
|
2340 oldae->ae_affnr = newah->ah_affnr;
|
|
2341 }
|
|
2342
|
|
2343 ga_clear(&tga);
|
|
2344 clear_affhash(&condht);
|
|
2345 }
|
|
2346 }
|
|
2347
|
|
2348 return retval;
|
|
2349 }
|
|
2350
|
|
2351 /*
|
|
2352 * Return TRUE if the affentry_T lists for "ah1" and "ah2" contain the same
|
|
2353 * items, ignoring the order.
|
|
2354 * Only compares the chop and add strings, not the condition.
|
|
2355 */
|
|
2356 static int
|
|
2357 same_affentries(ah1, ah2)
|
|
2358 affheader_T *ah1;
|
|
2359 affheader_T *ah2;
|
|
2360 {
|
|
2361 affentry_T *ae1, *ae2;
|
|
2362
|
|
2363 /* Check the length of the lists first. */
|
|
2364 ae2 = ah2->ah_first;
|
|
2365 for (ae1 = ah1->ah_first; ae1 != NULL; ae1 = ae1->ae_next)
|
|
2366 {
|
|
2367 if (ae2 == NULL)
|
|
2368 return FALSE; /* "ah1" list is longer */
|
|
2369 ae2 = ae2->ae_next;
|
|
2370 }
|
|
2371 if (ae2 != NULL)
|
|
2372 return FALSE; /* "ah2" list is longer */
|
|
2373
|
|
2374 /* Check that each entry in "ah1" appears in "ah2". */
|
|
2375 for (ae1 = ah1->ah_first; ae1 != NULL; ae1 = ae1->ae_next)
|
|
2376 {
|
|
2377 for (ae2 = ah2->ah_first; ae2 != NULL; ae2 = ae2->ae_next)
|
|
2378 {
|
|
2379 if ((ae1->ae_chop == NULL) == (ae2->ae_chop == NULL)
|
|
2380 && (ae1->ae_add == NULL) == (ae2->ae_add == NULL)
|
|
2381 && (ae1->ae_chop == NULL
|
|
2382 || STRCMP(ae1->ae_chop, ae2->ae_chop) == 0)
|
|
2383 && (ae1->ae_add == NULL
|
|
2384 || STRCMP(ae1->ae_add, ae2->ae_add) == 0))
|
|
2385 break;
|
|
2386 }
|
|
2387 if (ae2 == NULL)
|
|
2388 return FALSE;
|
|
2389 }
|
|
2390
|
|
2391 return TRUE;
|
|
2392 }
|
|
2393
|
|
2394 /*
|
|
2395 * Add a chop/add or cond hashtable entry.
|
|
2396 */
|
|
2397 static void
|
|
2398 add_affhash(ht, key, newnr)
|
|
2399 hashtab_T *ht;
|
|
2400 char_u *key;
|
|
2401 int newnr;
|
|
2402 {
|
|
2403 affhash_T *as;
|
|
2404
|
|
2405 as = (affhash_T *)alloc((unsigned)sizeof(affhash_T) + STRLEN(key));
|
|
2406 if (as != NULL)
|
|
2407 {
|
|
2408 as->as_nr = newnr;
|
|
2409 STRCPY(as->as_word, key);
|
|
2410 hash_add(ht, as->as_word);
|
|
2411 }
|
|
2412 }
|
|
2413
|
|
2414 /*
|
|
2415 * Clear the chop/add hashtable used to detect identical affixes.
|
|
2416 */
|
|
2417 static void
|
|
2418 clear_affhash(ht)
|
|
2419 hashtab_T *ht;
|
|
2420 {
|
|
2421 int todo;
|
|
2422 hashitem_T *hi;
|
|
2423
|
|
2424 todo = ht->ht_used;
|
|
2425 for (hi = ht->ht_array; todo > 0; ++hi)
|
|
2426 {
|
|
2427 if (!HASHITEM_EMPTY(hi))
|
|
2428 {
|
|
2429 --todo;
|
|
2430 vim_free(HI2AS(hi));
|
|
2431 }
|
|
2432 }
|
|
2433 hash_clear(ht);
|
|
2434 }
|
|
2435
|
|
2436 /*
|
|
2437 * Translate list of affix names for an old word to affix numbers in a new
|
|
2438 * basic word.
|
|
2439 * This checks if the conditions match with the old word. The result is that
|
|
2440 * the new affix does not need to store the condition.
|
|
2441 */
|
|
2442 static void
|
|
2443 trans_affixes(dw, bw, oldaff, newwords)
|
|
2444 dicword_T *dw; /* old word */
|
|
2445 basicword_T *bw; /* basic word */
|
|
2446 afffile_T *oldaff; /* affixes for "oldwords" */
|
|
2447 hashtab_T *newwords; /* table with words */
|
|
2448 {
|
|
2449 char_u key[2];
|
|
2450 char_u *p;
|
|
2451 char_u *affnm;
|
|
2452 garray_T *gap;
|
|
2453 hashitem_T *aff_hi;
|
|
2454 affheader_T *ah;
|
|
2455 affentry_T *ae;
|
|
2456 regmatch_T regmatch;
|
|
2457 int i;
|
|
2458 basicword_T *nbw;
|
|
2459 int alen;
|
|
2460 int wlen;
|
|
2461 garray_T fixga;
|
|
2462 char_u nword[MAXWLEN];
|
|
2463 int flags;
|
|
2464 int n;
|
|
2465
|
|
2466 ga_init2(&fixga, (int)sizeof(basicword_T *), 5);
|
|
2467
|
|
2468 /* Loop over all the affix names of the old word. */
|
|
2469 key[1] = NUL;
|
|
2470 for (affnm = dw->dw_affnm; *affnm != NUL; ++affnm)
|
|
2471 {
|
|
2472 key[0] = *affnm;
|
|
2473 aff_hi = hash_find(&oldaff->af_pref, key);
|
|
2474 if (!HASHITEM_EMPTY(aff_hi))
|
|
2475 gap = &bw->bw_prefix; /* found a prefix */
|
|
2476 else
|
|
2477 {
|
|
2478 gap = &bw->bw_suffix; /* must be a suffix */
|
|
2479 aff_hi = hash_find(&oldaff->af_suff, key);
|
|
2480 if (HASHITEM_EMPTY(aff_hi))
|
|
2481 {
|
|
2482 smsg((char_u *)_("No affix entry '%s' for word %s"),
|
|
2483 key, dw->dw_word);
|
|
2484 continue;
|
|
2485 }
|
|
2486 }
|
|
2487
|
|
2488 /* Loop over all the affix entries for this affix name. */
|
|
2489 ah = HI2AH(aff_hi);
|
|
2490 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
|
|
2491 {
|
|
2492 regmatch.regprog = ae->ae_prog;
|
|
2493 regmatch.rm_ic = FALSE; /* TODO: Should this be TRUE??? */
|
|
2494 if (ae->ae_prog == NULL
|
|
2495 || vim_regexec(®match, dw->dw_word, (colnr_T)0))
|
|
2496 {
|
|
2497 if (ae->ae_add_nw != NULL && (gap == &bw->bw_suffix
|
|
2498 ? bw->bw_addstring : bw->bw_leadstring) == NULL)
|
|
2499 {
|
|
2500 /* Affix has a non-word character and isn't prepended to
|
|
2501 * leader or appended to addition. Need to use another
|
|
2502 * word with an addition. It's a copy of the basicword_T
|
|
2503 * "bw". */
|
|
2504 if (gap == &bw->bw_suffix)
|
|
2505 {
|
|
2506 alen = ae->ae_add_nw - ae->ae_add;
|
|
2507 nbw = (basicword_T *)alloc((unsigned)(
|
|
2508 sizeof(basicword_T) + STRLEN(bw->bw_word)
|
|
2509 + alen + 1));
|
|
2510 if (nbw != NULL)
|
|
2511 {
|
|
2512 *nbw = *bw;
|
|
2513 ga_init2(&nbw->bw_prefix, sizeof(short_u), 1);
|
|
2514 ga_init2(&nbw->bw_suffix, sizeof(short_u), 1);
|
|
2515
|
|
2516 /* Adding the suffix may change the caps. */
|
|
2517 STRCPY(nword, dw->dw_word);
|
|
2518 if (ae->ae_chop != NULL)
|
|
2519 {
|
|
2520 /* Remove chop string. */
|
|
2521 p = nword + STRLEN(nword);
|
|
2522 for (i = mb_charlen(ae->ae_chop); i > 0; --i)
|
|
2523 mb_ptr_back(nword, p);
|
|
2524 *p = NUL;
|
|
2525 }
|
|
2526 STRCAT(nword, ae->ae_add);
|
|
2527 flags = captype(nword, nword + STRLEN(nword));
|
|
2528 if (flags & BWF_KEEPCAP)
|
|
2529 {
|
|
2530 nword[STRLEN(dw->dw_word) + alen] = NUL;
|
|
2531 nbw->bw_caseword = vim_strsave(nword);
|
|
2532 }
|
|
2533 nbw->bw_flags &= ~(BWF_ONECAP | BWF_ALLCAP
|
|
2534 | BWF_KEEPCAP);
|
|
2535 nbw->bw_flags |= flags;
|
|
2536
|
|
2537 if (bw->bw_leadstring != NULL)
|
|
2538 nbw->bw_leadstring =
|
|
2539 vim_strsave(bw->bw_leadstring);
|
|
2540 nbw->bw_addstring = vim_strsave(ae->ae_add_nw);
|
|
2541
|
|
2542 STRCPY(nbw->bw_word, bw->bw_word);
|
|
2543 if (alen > 0 || ae->ae_chop != NULL)
|
|
2544 {
|
|
2545 /* Suffix starts with word character. Append
|
|
2546 * it to the word. Add new word entry. */
|
|
2547 wlen = STRLEN(nbw->bw_word);
|
|
2548 if (ae->ae_chop != NULL)
|
|
2549 wlen -= STRLEN(ae->ae_chop);
|
|
2550 mch_memmove(nbw->bw_word + wlen, ae->ae_add,
|
|
2551 alen);
|
|
2552 nbw->bw_word[wlen + alen] = NUL;
|
|
2553 add_to_wordlist(newwords, nbw);
|
|
2554 }
|
|
2555 else
|
|
2556 /* Basic word is the same, link "nbw" after
|
|
2557 * "bw". */
|
|
2558 bw->bw_next = nbw;
|
|
2559
|
|
2560 /* Remember this word, we need to set bw_prefix
|
|
2561 * and bw_suffix later. */
|
|
2562 if (ga_grow(&fixga, 1) == OK)
|
|
2563 ((basicword_T **)fixga.ga_data)[fixga.ga_len++]
|
|
2564 = nbw;
|
|
2565 }
|
|
2566 }
|
|
2567 else
|
|
2568 {
|
|
2569 /* TODO: prefix with non-word char */
|
|
2570 }
|
|
2571 }
|
|
2572 else
|
|
2573 {
|
|
2574 /* Affix applies to this word, add the related affix
|
|
2575 * number. But only if it's not there yet. And keep the
|
|
2576 * list sorted, so that we can compare it later. */
|
|
2577 for (i = 0; i < gap->ga_len; ++i)
|
|
2578 {
|
|
2579 n = ((short_u *)gap->ga_data)[i];
|
|
2580 if (n >= ae->ae_affnr)
|
|
2581 {
|
|
2582 if (n == ae->ae_affnr)
|
|
2583 i = -1;
|
|
2584 break;
|
|
2585 }
|
|
2586 }
|
|
2587 if (i >= 0 && ga_grow(gap, 1) == OK)
|
|
2588 {
|
|
2589 if (i < gap->ga_len)
|
|
2590 mch_memmove(((short_u *)gap->ga_data) + i + 1,
|
|
2591 ((short_u *)gap->ga_data) + i,
|
|
2592 sizeof(short_u) * (gap->ga_len - i));
|
|
2593 ((short_u *)gap->ga_data)[i] = ae->ae_affnr;
|
|
2594 ++gap->ga_len;
|
|
2595 }
|
|
2596 }
|
|
2597 }
|
|
2598 }
|
|
2599 }
|
|
2600
|
|
2601 /*
|
|
2602 * For the words that we added for suffixes with non-word characters: Use
|
|
2603 * the prefix list of the main word.
|
|
2604 * TODO: do the same for prefixes.
|
|
2605 */
|
|
2606 for (i = 0; i < fixga.ga_len; ++i)
|
|
2607 {
|
|
2608 nbw = ((basicword_T **)fixga.ga_data)[i];
|
|
2609 if (ga_grow(&nbw->bw_prefix, bw->bw_prefix.ga_len) == OK)
|
|
2610 {
|
|
2611 mch_memmove(nbw->bw_prefix.ga_data, bw->bw_prefix.ga_data,
|
|
2612 bw->bw_prefix.ga_len * sizeof(short_u));
|
|
2613 nbw->bw_prefix.ga_len = bw->bw_prefix.ga_len;
|
|
2614 }
|
|
2615 }
|
|
2616
|
|
2617 ga_clear(&fixga);
|
|
2618 }
|
|
2619
|
|
2620 /*
|
|
2621 * Go over all words in "oldwords" and change the old affix names to the new
|
|
2622 * affix numbers, check the conditions, fold case, extract the basic word and
|
|
2623 * additions.
|
|
2624 */
|
|
2625 static int
|
|
2626 build_wordlist(newwords, oldwords, oldaff, regionmask)
|
|
2627 hashtab_T *newwords; /* basicword_T entries */
|
|
2628 hashtab_T *oldwords; /* dicword_T entries */
|
|
2629 afffile_T *oldaff; /* affixes for "oldwords" */
|
|
2630 int regionmask; /* value for bw_region */
|
|
2631 {
|
|
2632 int todo;
|
|
2633 hashitem_T *old_hi;
|
|
2634 dicword_T *dw;
|
|
2635 basicword_T *bw;
|
|
2636 char_u foldword[MAXLINELEN];
|
|
2637 int leadlen;
|
|
2638 char_u leadstring[MAXLINELEN];
|
|
2639 int addlen;
|
|
2640 char_u addstring[MAXLINELEN];
|
|
2641 int dwlen;
|
|
2642 char_u *p;
|
|
2643 int clen;
|
|
2644 int flags;
|
|
2645 char_u *cp;
|
|
2646 int l;
|
|
2647
|
|
2648 todo = oldwords->ht_used;
|
|
2649 for (old_hi = oldwords->ht_array; todo > 0; ++old_hi)
|
|
2650 {
|
|
2651 if (!HASHITEM_EMPTY(old_hi))
|
|
2652 {
|
|
2653 --todo;
|
|
2654 dw = HI2DW(old_hi);
|
|
2655
|
|
2656 /* This takes time, print a message now and then. */
|
|
2657 if ((todo & 0x3ff) == 0 || todo == oldwords->ht_used - 1)
|
|
2658 {
|
|
2659 if (todo != oldwords->ht_used - 1)
|
|
2660 {
|
|
2661 msg_didout = FALSE;
|
|
2662 msg_col = 0;
|
|
2663 }
|
|
2664 smsg((char_u *)_("%6d todo - %s"), todo, dw->dw_word);
|
|
2665 out_flush();
|
|
2666 ui_breakcheck();
|
|
2667 if (got_int)
|
|
2668 break;
|
|
2669 }
|
|
2670
|
|
2671 /* The basic words are always stored with folded case. */
|
|
2672 dwlen = STRLEN(dw->dw_word);
|
|
2673 (void)str_foldcase(dw->dw_word, dwlen, foldword, MAXLINELEN);
|
|
2674 flags = captype(dw->dw_word, dw->dw_word + dwlen);
|
|
2675
|
|
2676 /* Check for non-word characters before the word. */
|
|
2677 clen = 0;
|
|
2678 leadlen = 0;
|
|
2679 if (!spell_iswordc(foldword))
|
|
2680 {
|
|
2681 p = foldword;
|
|
2682 for (;;)
|
|
2683 {
|
|
2684 mb_ptr_adv(p);
|
|
2685 ++clen;
|
|
2686 if (*p == NUL) /* Only non-word chars (bad word!) */
|
|
2687 {
|
|
2688 if (p_verbose > 0)
|
|
2689 smsg((char_u *)_("Warning: word without word characters: \"%s\""),
|
|
2690 foldword);
|
|
2691 break;
|
|
2692 }
|
|
2693 if (spell_iswordc(p))
|
|
2694 {
|
|
2695 /* Move the leader to "leadstring" and remove it from
|
|
2696 * "foldword". */
|
|
2697 leadlen = p - foldword;
|
|
2698 mch_memmove(leadstring, foldword, leadlen);
|
|
2699 leadstring[leadlen] = NUL;
|
|
2700 mch_memmove(foldword, p, STRLEN(p) + 1);
|
|
2701 break;
|
|
2702 }
|
|
2703 }
|
|
2704 }
|
|
2705
|
|
2706 /* Check for non-word characters after word characters. */
|
|
2707 addlen = 0;
|
|
2708 for (p = foldword; spell_iswordc(p); mb_ptr_adv(p))
|
|
2709 {
|
|
2710 if (*p == NUL)
|
|
2711 break;
|
|
2712 ++clen;
|
|
2713 }
|
|
2714 if (*p != NUL)
|
|
2715 {
|
|
2716 /* Move the addition to "addstring" and truncate "foldword". */
|
|
2717 if (flags & BWF_KEEPCAP)
|
|
2718 {
|
|
2719 /* Preserve caps, need to skip the right number of
|
|
2720 * characters in the original word (case folding may
|
|
2721 * change the byte count). */
|
|
2722 l = 0;
|
|
2723 for (cp = dw->dw_word; l < clen; mb_ptr_adv(cp))
|
|
2724 ++l;
|
|
2725 addlen = STRLEN(cp);
|
|
2726 mch_memmove(addstring, cp, addlen + 1);
|
|
2727 }
|
|
2728 else
|
|
2729 {
|
|
2730 addlen = STRLEN(p);
|
|
2731 mch_memmove(addstring, p, addlen + 1);
|
|
2732 }
|
|
2733 *p = NUL;
|
|
2734 }
|
|
2735
|
|
2736 bw = (basicword_T *)alloc_clear((unsigned)sizeof(basicword_T)
|
|
2737 + STRLEN(foldword));
|
|
2738 if (bw == NULL)
|
|
2739 break;
|
|
2740 STRCPY(bw->bw_word, foldword);
|
|
2741 bw->bw_region = regionmask;
|
|
2742
|
|
2743 if (leadlen > 0)
|
|
2744 bw->bw_leadstring = vim_strsave(leadstring);
|
|
2745 else
|
|
2746 bw->bw_leadstring = NULL;
|
|
2747 if (addlen > 0)
|
|
2748 bw->bw_addstring = vim_strsave(addstring);
|
|
2749 else
|
|
2750 bw->bw_addstring = NULL;
|
|
2751
|
|
2752 add_to_wordlist(newwords, bw);
|
|
2753
|
|
2754 if (flags & BWF_KEEPCAP)
|
|
2755 {
|
|
2756 if (addlen == 0)
|
|
2757 /* use the whole word */
|
|
2758 bw->bw_caseword = vim_strsave(dw->dw_word + leadlen);
|
|
2759 else
|
|
2760 /* use only up to the addition */
|
|
2761 bw->bw_caseword = vim_strnsave(dw->dw_word + leadlen,
|
|
2762 cp - dw->dw_word - leadlen);
|
|
2763 if (bw->bw_caseword == NULL) /* out of memory */
|
|
2764 flags &= ~BWF_KEEPCAP;
|
|
2765 }
|
|
2766 bw->bw_flags = flags;
|
|
2767
|
|
2768 /* Deal with any affix names on the old word, translate them
|
|
2769 * into affix numbers. */
|
|
2770 ga_init2(&bw->bw_prefix, sizeof(short_u), 10);
|
|
2771 ga_init2(&bw->bw_suffix, sizeof(short_u), 10);
|
|
2772 if (dw->dw_affnm != NULL)
|
|
2773 trans_affixes(dw, bw, oldaff, newwords);
|
|
2774 }
|
|
2775 }
|
|
2776 if (todo > 0)
|
|
2777 return FAIL;
|
|
2778 return OK;
|
|
2779 }
|
|
2780
|
|
2781 /*
|
|
2782 * Go through the list of words and combine the ones that are identical except
|
|
2783 * for the region.
|
|
2784 */
|
|
2785 static void
|
|
2786 combine_regions(newwords)
|
|
2787 hashtab_T *newwords;
|
|
2788 {
|
|
2789 int todo;
|
|
2790 hashitem_T *hi;
|
|
2791 basicword_T *bw, *nbw, *pbw;
|
|
2792
|
|
2793 /* Loop over all basic words in the words table. */
|
|
2794 todo = newwords->ht_used;
|
|
2795 for (hi = newwords->ht_array; todo > 0; ++hi)
|
|
2796 {
|
|
2797 if (!HASHITEM_EMPTY(hi))
|
|
2798 {
|
|
2799 --todo;
|
|
2800
|
|
2801 /* Loop over the list of words for this basic word. Compare with
|
|
2802 * each following word in the same list. */
|
|
2803 for (bw = HI2BW(hi); bw != NULL; bw = bw->bw_next)
|
|
2804 {
|
|
2805 pbw = bw;
|
|
2806 for (nbw = pbw->bw_next; nbw != NULL; nbw = pbw->bw_next)
|
|
2807 {
|
|
2808 if (bw->bw_flags == nbw->bw_flags
|
|
2809 && (bw->bw_leadstring == NULL)
|
|
2810 == (nbw->bw_leadstring == NULL)
|
|
2811 && (bw->bw_addstring == NULL)
|
|
2812 == (nbw->bw_addstring == NULL)
|
|
2813 && ((bw->bw_flags & BWF_KEEPCAP) == 0
|
|
2814 || (STRCMP(bw->bw_caseword,
|
|
2815 nbw->bw_caseword) == 0))
|
|
2816 && (bw->bw_leadstring == NULL
|
|
2817 || (STRCMP(bw->bw_leadstring,
|
|
2818 nbw->bw_leadstring) == 0))
|
|
2819 && (bw->bw_addstring == NULL
|
|
2820 || (STRCMP(bw->bw_addstring,
|
|
2821 nbw->bw_addstring) == 0))
|
|
2822 && same_affixes(bw, nbw)
|
|
2823 )
|
|
2824 {
|
|
2825 /* Match, combine regions and delete "nbw". */
|
|
2826 pbw->bw_next = nbw->bw_next;
|
|
2827 bw->bw_region |= nbw->bw_region;
|
|
2828 free_basicword(nbw);
|
|
2829 }
|
|
2830 else
|
|
2831 /* No match, continue with next one. */
|
|
2832 pbw = nbw;
|
|
2833 }
|
|
2834 }
|
|
2835 }
|
|
2836 }
|
|
2837 }
|
|
2838
|
|
2839 /*
|
|
2840 * Return TRUE when the prefixes and suffixes for "bw" and "nbw" are equal.
|
|
2841 */
|
|
2842 static int
|
|
2843 same_affixes(bw, nbw)
|
|
2844 basicword_T *bw;
|
|
2845 basicword_T *nbw;
|
|
2846 {
|
|
2847 return (bw->bw_prefix.ga_len == nbw->bw_prefix.ga_len
|
|
2848 && bw->bw_suffix.ga_len == nbw->bw_suffix.ga_len
|
|
2849 && (bw->bw_prefix.ga_len == 0
|
|
2850 || vim_memcmp(bw->bw_prefix.ga_data,
|
|
2851 nbw->bw_prefix.ga_data,
|
|
2852 bw->bw_prefix.ga_len * sizeof(short_u)) == 0)
|
|
2853 && (bw->bw_suffix.ga_len == 0
|
|
2854 || vim_memcmp(bw->bw_suffix.ga_data,
|
|
2855 nbw->bw_suffix.ga_data,
|
|
2856 bw->bw_suffix.ga_len * sizeof(short_u)) == 0));
|
|
2857 }
|
|
2858
|
|
2859 /*
|
|
2860 * For each basic word with additions turn the affixes into other additions
|
|
2861 * and/or new basic words. The result is that no affixes apply to a word with
|
|
2862 * additions.
|
|
2863 */
|
|
2864 static void
|
|
2865 expand_affixes(newwords, prefgap, suffgap)
|
|
2866 hashtab_T *newwords;
|
|
2867 garray_T *prefgap;
|
|
2868 garray_T *suffgap;
|
|
2869 {
|
|
2870 int todo;
|
|
2871 hashitem_T *hi;
|
|
2872 basicword_T *bw;
|
|
2873 int pi, si;
|
|
2874 affentry_T *pae, *sae;
|
|
2875 garray_T add_words;
|
|
2876 int n;
|
|
2877
|
|
2878 ga_init2(&add_words, sizeof(basicword_T *), 10);
|
|
2879
|
|
2880 todo = newwords->ht_used;
|
|
2881 for (hi = newwords->ht_array; todo > 0; ++hi)
|
|
2882 {
|
|
2883 if (!HASHITEM_EMPTY(hi))
|
|
2884 {
|
|
2885 --todo;
|
|
2886 for (bw = HI2BW(hi); bw != NULL; bw = bw->bw_next)
|
|
2887 {
|
|
2888 /*
|
|
2889 * Need to fix affixes if there is a leader or addition and
|
|
2890 * there are prefixes or suffixes.
|
|
2891 */
|
|
2892 if ((bw->bw_leadstring != NULL || bw->bw_addstring != NULL)
|
|
2893 && (bw->bw_prefix.ga_len != 0
|
|
2894 || bw->bw_suffix.ga_len != 0))
|
|
2895 {
|
|
2896 /* Loop over all prefix numbers, but first without a
|
|
2897 * prefix. */
|
|
2898 for (pi = -1; pi < bw->bw_prefix.ga_len; ++pi)
|
|
2899 {
|
|
2900 pae = NULL;
|
|
2901 if (pi >= 0)
|
|
2902 {
|
|
2903 n = ((short_u *)bw->bw_prefix.ga_data)[pi];
|
|
2904 pae = ((affheader_T *)prefgap->ga_data + n)
|
|
2905 ->ah_first;
|
|
2906 }
|
|
2907
|
|
2908 /* Loop over all entries for prefix "pi". Do it once
|
|
2909 * when there is no prefix (pi == -1). */
|
|
2910 do
|
|
2911 {
|
|
2912 /* Loop over all suffix numbers. Do without a
|
|
2913 * suffix first when there is a prefix. */
|
|
2914 for (si = (pi == -1 ? 0 : -1);
|
|
2915 si < bw->bw_suffix.ga_len; ++si)
|
|
2916 {
|
|
2917 sae = NULL;
|
|
2918 if (si >= 0)
|
|
2919 {
|
|
2920 n = ((short_u *)bw->bw_suffix.ga_data)[si];
|
|
2921 sae = ((affheader_T *)suffgap->ga_data + n)
|
|
2922 ->ah_first;
|
|
2923 }
|
|
2924
|
|
2925 /* Loop over all entries for suffix "si". Do
|
|
2926 * it once when there is no suffix (si == -1).
|
|
2927 */
|
|
2928 do
|
|
2929 {
|
|
2930 /* Expand the word for this combination of
|
|
2931 * prefixes and affixes. */
|
|
2932 expand_one_aff(bw, &add_words, pae, sae);
|
|
2933
|
|
2934 /* Advance to next suffix entry, if there
|
|
2935 * is one. */
|
|
2936 if (sae != NULL)
|
|
2937 sae = sae->ae_next;
|
|
2938 } while (sae != NULL);
|
|
2939 }
|
|
2940
|
|
2941 /* Advance to next prefix entry, if there is one. */
|
|
2942 if (pae != NULL)
|
|
2943 pae = pae->ae_next;
|
|
2944 } while (pae != NULL);
|
|
2945 }
|
|
2946 }
|
|
2947 }
|
|
2948 }
|
|
2949 }
|
|
2950
|
|
2951 /*
|
|
2952 * Add the new words afterwards, can't change "newwords" while going over
|
|
2953 * all its items.
|
|
2954 */
|
|
2955 for (pi = 0; pi < add_words.ga_len; ++pi)
|
|
2956 add_to_wordlist(newwords, ((basicword_T **)add_words.ga_data)[pi]);
|
|
2957
|
|
2958 ga_clear(&add_words);
|
|
2959 }
|
|
2960
|
|
2961 /*
|
|
2962 * Add one word to "add_words" for basic word "bw" with additions, adding
|
|
2963 * prefix "pae" and suffix "sae". Either "pae" or "sae" can be NULL.
|
|
2964 */
|
|
2965 static void
|
|
2966 expand_one_aff(bw, add_words, pae, sae)
|
|
2967 basicword_T *bw;
|
|
2968 garray_T *add_words;
|
|
2969 affentry_T *pae;
|
|
2970 affentry_T *sae;
|
|
2971 {
|
|
2972 char_u word[MAXWLEN + 1];
|
|
2973 char_u caseword[MAXWLEN + 1];
|
|
2974 int l = 0;
|
|
2975 int choplen = 0;
|
|
2976 int ll;
|
|
2977 basicword_T *nbw;
|
|
2978
|
|
2979 /* Prepend prefix to the basic word if there is a prefix and there is no
|
|
2980 * leadstring. */
|
|
2981 if (pae != NULL && bw->bw_leadstring == NULL)
|
|
2982 {
|
|
2983 if (pae->ae_add != NULL)
|
|
2984 {
|
|
2985 l = STRLEN(pae->ae_add);
|
|
2986 mch_memmove(word, pae->ae_add, l);
|
|
2987 }
|
|
2988 if (pae->ae_chop != NULL)
|
|
2989 choplen = STRLEN(pae->ae_chop);
|
|
2990 }
|
|
2991
|
|
2992 /* Copy the body of the word. */
|
|
2993 STRCPY(word + l, bw->bw_word + choplen);
|
|
2994
|
|
2995 /* Do the same for bw_caseword, if it's there. */
|
|
2996 if (bw->bw_flags & BWF_KEEPCAP)
|
|
2997 {
|
|
2998 if (l > 0)
|
|
2999 mch_memmove(caseword, pae->ae_add, l);
|
|
3000 STRCPY(caseword + l, bw->bw_caseword + choplen);
|
|
3001 }
|
|
3002
|
|
3003 /* Append suffix to the basic word if there is a suffix and there is no
|
|
3004 * addstring. */
|
|
3005 if (sae != 0 && bw->bw_addstring == NULL)
|
|
3006 {
|
|
3007 l = STRLEN(word);
|
|
3008 if (sae->ae_chop != NULL)
|
|
3009 l -= STRLEN(sae->ae_chop);
|
|
3010 if (sae->ae_add == NULL)
|
|
3011 word[l] = NUL;
|
|
3012 else
|
|
3013 STRCPY(word + l, sae->ae_add);
|
|
3014
|
|
3015 if (bw->bw_flags & BWF_KEEPCAP)
|
|
3016 {
|
|
3017 /* Do the same for the caseword. */
|
|
3018 l = STRLEN(caseword);
|
|
3019 if (sae->ae_chop != NULL)
|
|
3020 l -= STRLEN(sae->ae_chop);
|
|
3021 if (sae->ae_add == NULL)
|
|
3022 caseword[l] = NUL;
|
|
3023 else
|
|
3024 STRCPY(caseword + l, sae->ae_add);
|
|
3025 }
|
|
3026 }
|
|
3027
|
|
3028 nbw = (basicword_T *)alloc_clear((unsigned)
|
|
3029 sizeof(basicword_T) + STRLEN(word));
|
|
3030 if (nbw != NULL)
|
|
3031 {
|
|
3032 /* Add the new word to the list of words to be added later. */
|
|
3033 if (ga_grow(add_words, 1) == FAIL)
|
|
3034 {
|
|
3035 vim_free(nbw);
|
|
3036 return;
|
|
3037 }
|
|
3038 ((basicword_T **)add_words->ga_data)[add_words->ga_len++] = nbw;
|
|
3039
|
|
3040 /* Copy the (modified) basic word, flags and region. */
|
|
3041 STRCPY(nbw->bw_word, word);
|
|
3042 nbw->bw_flags = bw->bw_flags;
|
|
3043 nbw->bw_region = bw->bw_region;
|
|
3044
|
|
3045 /* Set the (modified) caseword. */
|
|
3046 if (bw->bw_flags & BWF_KEEPCAP)
|
|
3047 if ((nbw->bw_caseword = vim_strsave(caseword)) == NULL)
|
|
3048 nbw->bw_flags &= ~BWF_KEEPCAP;
|
|
3049
|
|
3050 if (bw->bw_leadstring != NULL)
|
|
3051 {
|
|
3052 if (pae != NULL)
|
|
3053 {
|
|
3054 /* Prepend prefix to leadstring. */
|
|
3055 ll = STRLEN(bw->bw_leadstring);
|
|
3056 l = choplen = 0;
|
|
3057 if (pae->ae_add != NULL)
|
|
3058 l = STRLEN(pae->ae_add);
|
|
3059 if (pae->ae_chop != NULL)
|
|
3060 {
|
|
3061 choplen = STRLEN(pae->ae_chop);
|
|
3062 if (choplen > ll) /* TODO: error? */
|
|
3063 choplen = ll;
|
|
3064 }
|
|
3065 nbw->bw_leadstring = alloc((unsigned)(ll + l - choplen + 1));
|
|
3066 if (nbw->bw_leadstring != NULL)
|
|
3067 {
|
|
3068 if (l > 0)
|
|
3069 mch_memmove(nbw->bw_leadstring, pae->ae_add, l);
|
|
3070 STRCPY(nbw->bw_leadstring + l, bw->bw_leadstring + choplen);
|
|
3071 }
|
|
3072 }
|
|
3073 else
|
|
3074 nbw->bw_leadstring = vim_strsave(bw->bw_leadstring);
|
|
3075 }
|
|
3076
|
|
3077 if (bw->bw_addstring != NULL)
|
|
3078 {
|
|
3079 if (sae != NULL)
|
|
3080 {
|
|
3081 /* Append suffix to addstring. */
|
|
3082 l = STRLEN(bw->bw_addstring);
|
|
3083 if (sae->ae_chop != NULL)
|
|
3084 {
|
|
3085 l -= STRLEN(sae->ae_chop);
|
|
3086 if (l < 0) /* TODO: error? */
|
|
3087 l = 0;
|
|
3088 }
|
|
3089 if (sae->ae_add == NULL)
|
|
3090 ll = 0;
|
|
3091 else
|
|
3092 ll = STRLEN(sae->ae_add);
|
|
3093 nbw->bw_addstring = alloc((unsigned)(ll + l - choplen + 1));
|
|
3094 if (nbw->bw_addstring != NULL)
|
|
3095 {
|
|
3096 STRCPY(nbw->bw_addstring, bw->bw_addstring);
|
|
3097 if (sae->ae_add == NULL)
|
|
3098 nbw->bw_addstring[l] = NUL;
|
|
3099 else
|
|
3100 STRCPY(nbw->bw_addstring + l, sae->ae_add);
|
|
3101 }
|
|
3102 }
|
|
3103 else
|
|
3104 nbw->bw_addstring = vim_strsave(bw->bw_addstring);
|
|
3105 }
|
|
3106 }
|
|
3107 }
|
|
3108
|
|
3109 /*
|
|
3110 * Add basicword_T "*bw" to wordlist "newwords".
|
|
3111 */
|
|
3112 static void
|
|
3113 add_to_wordlist(newwords, bw)
|
|
3114 hashtab_T *newwords;
|
|
3115 basicword_T *bw;
|
|
3116 {
|
|
3117 hashitem_T *hi;
|
|
3118 basicword_T *bw2;
|
|
3119
|
|
3120 hi = hash_find(newwords, bw->bw_word);
|
|
3121 if (HASHITEM_EMPTY(hi))
|
|
3122 {
|
|
3123 /* New entry, add to hashlist. */
|
|
3124 hash_add(newwords, bw->bw_word);
|
|
3125 bw->bw_next = NULL;
|
|
3126 }
|
|
3127 else
|
|
3128 {
|
|
3129 /* Existing entry, append to list of basic words. */
|
|
3130 bw2 = HI2BW(hi);
|
|
3131 bw->bw_next = bw2->bw_next;
|
|
3132 bw2->bw_next = bw;
|
|
3133 }
|
|
3134 }
|
|
3135
|
|
3136 /*
|
|
3137 * Write a number to file "fd", MSB first, in "len" bytes.
|
|
3138 */
|
|
3139 static void
|
|
3140 put_bytes(fd, nr, len)
|
|
3141 FILE *fd;
|
|
3142 long_u nr;
|
|
3143 int len;
|
|
3144 {
|
|
3145 int i;
|
|
3146
|
|
3147 for (i = len - 1; i >= 0; --i)
|
|
3148 putc((int)(nr >> (i * 8)), fd);
|
|
3149 }
|
|
3150
|
|
3151 /*
|
|
3152 * Write affix info. <affflags> <affitemcnt> <affitem> ...
|
|
3153 */
|
|
3154 static void
|
|
3155 write_affix(fd, ah)
|
|
3156 FILE *fd;
|
|
3157 affheader_T *ah;
|
|
3158 {
|
|
3159 int i = 0;
|
|
3160 affentry_T *ae;
|
|
3161 char_u *p;
|
|
3162 int round;
|
|
3163
|
|
3164 fputc(ah->ah_combine ? 1 : 0, fd); /* <affflags> */
|
|
3165
|
|
3166 /* Count the number of entries. */
|
|
3167 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
|
|
3168 ++i;
|
|
3169 put_bytes(fd, (long_u)i, 2); /* <affitemcnt> */
|
|
3170
|
|
3171 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
|
|
3172 for (round = 1; round <= 2; ++round)
|
|
3173 {
|
|
3174 p = round == 1 ? ae->ae_chop : ae->ae_add;
|
|
3175 if (p == NULL)
|
|
3176 putc(0, fd); /* <affchoplen> / <affaddlen> */
|
|
3177 else
|
|
3178 {
|
|
3179 putc(STRLEN(p), fd); /* <affchoplen> / <affaddlen> */
|
|
3180 /* <affchop> / <affadd> */
|
|
3181 fwrite(p, STRLEN(p), (size_t)1, fd);
|
|
3182 }
|
|
3183 }
|
|
3184 }
|
|
3185
|
|
3186 /*
|
|
3187 * Write list of affix NRs: <affixcnt> <affixNR> ...
|
|
3188 */
|
|
3189 static void
|
|
3190 write_affixlist(fd, aff, bytes)
|
|
3191 FILE *fd;
|
|
3192 garray_T *aff;
|
|
3193 int bytes;
|
|
3194 {
|
|
3195 int i;
|
|
3196
|
|
3197 if (aff->ga_len > 0)
|
|
3198 {
|
|
3199 putc(aff->ga_len, fd); /* <affixcnt> */
|
|
3200 for (i = 0; i < aff->ga_len; ++i)
|
|
3201 put_bytes(fd, (long_u )((short_u *)aff->ga_data)[i], bytes);
|
|
3202 }
|
|
3203 }
|
|
3204
|
|
3205 /*
|
|
3206 * Vim spell file format: <HEADER> <PREFIXLIST> <SUFFIXLIST>
|
|
3207 * <SUGGEST> <WORDLIST>
|
|
3208 *
|
|
3209 * <HEADER>: <fileID> <regioncnt> <regionname> ...
|
|
3210 *
|
|
3211 * <fileID> 10 bytes "VIMspell01"
|
|
3212 * <regioncnt> 1 byte number of regions following (8 supported)
|
|
3213 * <regionname> 2 bytes Region name: ca, au, etc.
|
|
3214 * First <regionname> is region 1.
|
|
3215 *
|
|
3216 *
|
|
3217 * <PREFIXLIST>: <affcount> <afftotcnt> <affix> ...
|
|
3218 * <SUFFIXLIST>: <affcount> <afftotcnt> <affix> ...
|
|
3219 * list of possible affixes: prefixes and suffixes.
|
|
3220 *
|
|
3221 * <affcount> 2 bytes Number of affixes (MSB comes first).
|
|
3222 * When more than 256 an affixNR is 2 bytes.
|
|
3223 * This is separate for prefixes and suffixes!
|
|
3224 * First affixNR is 0.
|
|
3225 * <afftotcnt> 2 bytes Total number of affix items (MSB comes first).
|
|
3226 *
|
|
3227 * <affix>: <affflags> <affitemcnt> <affitem> ...
|
|
3228 *
|
|
3229 * <affflags> 1 byte 0x01: prefix combines with suffix.
|
|
3230 * 0x02-0x80: unset
|
|
3231 * <affitemcnt> 2 bytes Number of affixes with this affixNR (MSB first).
|
|
3232 *
|
|
3233 * <affitem>: <affchoplen> <affchop> <affaddlen> <affadd>
|
|
3234 *
|
|
3235 * <affchoplen> 1 byte Length of <affchop> in bytes.
|
|
3236 * <affchop> N bytes To be removed from basic word.
|
|
3237 * <affaddlen> 1 byte Length of <affadd> in bytes.
|
|
3238 * <affadd> N bytes To be added to basic word.
|
|
3239 *
|
|
3240 *
|
|
3241 * <SUGGEST> : <suggestlen> <more> ...
|
|
3242 *
|
|
3243 * <suggestlen> 4 bytes Length of <SUGGEST> in bytes, excluding
|
|
3244 * <suggestlen>. MSB first.
|
|
3245 * <more> To be defined.
|
|
3246 *
|
|
3247 *
|
|
3248 * <WORDLIST>: <wordcount> <worditem> ...
|
|
3249 *
|
|
3250 * <wordcount> 4 bytes Number of <worditem> following. MSB first.
|
|
3251 *
|
|
3252 * <worditem>: <nr> <string> <flags> [<flags2>]
|
|
3253 * [<caselen> <caseword>]
|
|
3254 * [<affixcnt> <affixNR> ...] (prefixes)
|
|
3255 * [<affixcnt> <affixNR> ...] (suffixes)
|
|
3256 * [<region>]
|
|
3257 * [<addcnt> <add> ...]
|
|
3258 *
|
|
3259 * <nr> i 1 byte Number of bytes copied from previous word.
|
|
3260 * <string> N bytes Additional bytes for word, up to byte smaller than
|
|
3261 * 0x20 (space).
|
|
3262 * Must only contain case-folded word characters.
|
|
3263 * <flags> 1 byte 0x01: word is valid without addition
|
|
3264 * 0x02: has region byte
|
|
3265 * 0x04: first letter must be upper-case
|
|
3266 * 0x08: has suffixes, <affixcnt> and <affixNR> follow
|
|
3267 * 0x10: more flags, <flags2> follows next
|
|
3268 * 0x20-0x80: can't be used, unset
|
|
3269 * <flags2> 1 byte 0x01: has additions, <addcnt> and <add> follow
|
|
3270 * 0x02: has prefixes, <affixcnt> and <affixNR> follow
|
|
3271 * 0x04: all letters must be upper-case
|
|
3272 * 0x08: case must match
|
|
3273 * 0x10-0x80: unset
|
|
3274 * <caselen> 1 byte Length of <caseword>.
|
|
3275 * <caseword> N bytes Word with matching case.
|
|
3276 * <affixcnt> 1 byte Number of affix NRs following.
|
|
3277 * <affixNR> 1 or 2 byte Number of possible affix for this word.
|
|
3278 * When using 2 bytes MSB comes first.
|
|
3279 * <region> 1 byte Bitmask for regions in which word is valid. When
|
|
3280 * omitted it's valid in all regions.
|
|
3281 * Lowest bit is for region 1.
|
|
3282 * <addcnt> 2 bytes Number of <add> items following.
|
|
3283 *
|
|
3284 * <add>: <addflags> <addlen> [<leadlen> <addstring>] [<region>]
|
|
3285 *
|
|
3286 * <addflags> 1 byte 0x01: fixed case, <addstring> is the whole word
|
|
3287 * with matching case.
|
|
3288 * 0x02: first letter must be upper-case
|
|
3289 * 0x04: all letters must be upper-case
|
|
3290 * 0x08: has region byte
|
|
3291 * 0x10-0x80: unset
|
|
3292 * <addlen> 1 byte Length of <addstring> in bytes.
|
|
3293 * <leadlen> 1 byte Number of bytes at start of <addstring> that must
|
|
3294 * come before the start of the basic word.
|
|
3295 * <addstring> N bytes Word characters, before/in/after the word.
|
|
3296 *
|
|
3297 * All text characters are in 'encoding': <affchop>, <affadd>, <string>,
|
|
3298 * <caseword>> and <addstring>.
|
|
3299 * All other fields are ASCII: <regionname>
|
|
3300 * <string> is always case-folded.
|
|
3301 */
|
|
3302
|
|
3303 /*
|
|
3304 * Write the Vim spell file "fname".
|
|
3305 */
|
|
3306 static void
|
|
3307 write_vim_spell(fname, prefga, suffga, newwords, regcount, regchars)
|
|
3308 char_u *fname;
|
|
3309 garray_T *prefga; /* prefixes, affheader_T entries */
|
|
3310 garray_T *suffga; /* suffixes, affheader_T entries */
|
|
3311 hashtab_T *newwords; /* basic words, basicword_T entries */
|
|
3312 int regcount; /* number of regions */
|
|
3313 char_u *regchars; /* region names */
|
|
3314 {
|
|
3315 FILE *fd;
|
|
3316 garray_T *gap;
|
|
3317 hashitem_T *hi;
|
|
3318 char_u **wtab;
|
|
3319 int todo;
|
|
3320 int flags, aflags;
|
|
3321 basicword_T *bw, *bwf, *bw2, *prevbw = NULL;
|
|
3322 int regionmask; /* mask for all relevant region bits */
|
|
3323 int i;
|
|
3324 int cnt;
|
|
3325 affentry_T *ae;
|
|
3326 int round;
|
|
3327 int prefm, suffm;
|
|
3328 garray_T bwga;
|
|
3329
|
|
3330 fd = fopen((char *)fname, "w");
|
|
3331 if (fd == NULL)
|
|
3332 {
|
|
3333 EMSG2(_(e_notopen), fname);
|
|
3334 return;
|
|
3335 }
|
|
3336
|
|
3337 fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd);
|
|
3338
|
|
3339 /* write the region names if there is more than one */
|
|
3340 if (regcount > 1)
|
|
3341 {
|
|
3342 putc(regcount, fd);
|
|
3343 fwrite(regchars, (size_t)(regcount * 2), (size_t)1, fd);
|
|
3344 regionmask = (1 << regcount) - 1;
|
|
3345 }
|
|
3346 else
|
|
3347 {
|
|
3348 putc(0, fd);
|
|
3349 regionmask = 0;
|
|
3350 }
|
|
3351
|
|
3352 /* Write the prefix and suffix lists. */
|
|
3353 for (round = 1; round <= 2; ++round)
|
|
3354 {
|
|
3355 gap = round == 1 ? prefga : suffga;
|
|
3356 put_bytes(fd, (long_u)gap->ga_len, 2); /* <affcount> */
|
|
3357
|
|
3358 /* Count the total number of affix items. */
|
|
3359 cnt = 0;
|
|
3360 for (i = 0; i < gap->ga_len; ++i)
|
|
3361 for (ae = ((affheader_T *)gap->ga_data + i)->ah_first;
|
|
3362 ae != NULL; ae = ae->ae_next)
|
|
3363 ++cnt;
|
|
3364 put_bytes(fd, (long_u)cnt, 2); /* <afftotcnt> */
|
|
3365
|
|
3366 for (i = 0; i < gap->ga_len; ++i)
|
|
3367 write_affix(fd, (affheader_T *)gap->ga_data + i);
|
|
3368 }
|
|
3369
|
|
3370 /* Number of bytes used for affix NR depends on affix count. */
|
|
3371 prefm = (prefga->ga_len > 256) ? 2 : 1;
|
|
3372 suffm = (suffga->ga_len > 256) ? 2 : 1;
|
|
3373
|
|
3374 /* Write the suggest info. TODO */
|
|
3375 put_bytes(fd, 0L, 4);
|
|
3376
|
|
3377 /*
|
|
3378 * Write the word list. <wordcount> <worditem> ...
|
|
3379 */
|
|
3380 /* number of basic words in 4 bytes */
|
|
3381 put_bytes(fd, newwords->ht_used, 4); /* <wordcount> */
|
|
3382
|
|
3383 /*
|
|
3384 * Sort the word list, so that we can reuse as many bytes as possible.
|
|
3385 */
|
|
3386 wtab = (char_u **)alloc((unsigned)(sizeof(char_u *) * newwords->ht_used));
|
|
3387 if (wtab != NULL)
|
|
3388 {
|
|
3389 /* Make a table with pointers to each word. */
|
|
3390 todo = newwords->ht_used;
|
|
3391 for (hi = newwords->ht_array; todo > 0; ++hi)
|
|
3392 if (!HASHITEM_EMPTY(hi))
|
|
3393 wtab[--todo] = hi->hi_key;
|
|
3394
|
|
3395 /* Sort. */
|
|
3396 sort_strings(wtab, (int)newwords->ht_used);
|
|
3397
|
|
3398 /* Now write each basic word to the spell file. */
|
|
3399 ga_init2(&bwga, sizeof(basicword_T *), 10);
|
|
3400 for (todo = 0; todo < newwords->ht_used; ++todo)
|
|
3401 {
|
|
3402 bwf = KEY2BW(wtab[todo]);
|
|
3403
|
|
3404 /*
|
|
3405 * Reorder the list of basicword_T words: make a list for words
|
|
3406 * with the same case-folded word. Put them together for same
|
|
3407 * caps (ONECAP, ALLCAP and various KEEPCAP words) and same
|
|
3408 * affixes. Each list will then be put as a basic word with
|
|
3409 * additions.
|
|
3410 * This won't take much space, since the basic word is the same
|
|
3411 * every time, only its length is written.
|
|
3412 */
|
|
3413 bwga.ga_len = 0;
|
|
3414 for (bw = bwf; bw != NULL; bw = bw->bw_next)
|
|
3415 {
|
|
3416 flags = bw->bw_flags & (BWF_ONECAP | BWF_KEEPCAP | BWF_ALLCAP);
|
|
3417
|
|
3418 /* Go through the lists we found so far. Break when the case
|
|
3419 * matches. */
|
|
3420 for (i = 0; i < bwga.ga_len; ++i)
|
|
3421 {
|
|
3422 bw2 = ((basicword_T **)bwga.ga_data)[i];
|
|
3423 aflags = bw2->bw_flags & (BWF_ONECAP | BWF_KEEPCAP
|
|
3424 | BWF_ALLCAP);
|
|
3425 if (flags == aflags
|
|
3426 && ((flags & BWF_KEEPCAP) == 0
|
|
3427 || (STRCMP(bw->bw_caseword,
|
|
3428 bw2->bw_caseword) == 0))
|
|
3429 && same_affixes(bw, bw2))
|
|
3430 break;
|
|
3431 }
|
|
3432 if (i == bwga.ga_len)
|
|
3433 {
|
|
3434 /* No word with similar caps, make a new list. */
|
|
3435 if (ga_grow(&bwga, 1) == FAIL)
|
|
3436 break;
|
|
3437 ((basicword_T **)bwga.ga_data)[i] = bw;
|
|
3438 bw->bw_cnext = NULL;
|
|
3439 ++bwga.ga_len;
|
|
3440 }
|
|
3441 else
|
|
3442 {
|
|
3443 /* Add to list of words with similar caps. */
|
|
3444 bw->bw_cnext = bw2->bw_cnext;
|
|
3445 bw2->bw_cnext = bw;
|
|
3446 }
|
|
3447 }
|
|
3448
|
|
3449 /* Prefer the word with no caps to use as the first basic word.
|
|
3450 * At least one without KEEPCAP. */
|
|
3451 bw = NULL;
|
|
3452 for (i = 0; i < bwga.ga_len; ++i)
|
|
3453 {
|
|
3454 bw2 = ((basicword_T **)bwga.ga_data)[i];
|
|
3455 if (bw == NULL
|
|
3456 || (bw2->bw_flags & (BWF_ONECAP | BWF_KEEPCAP
|
|
3457 | BWF_ALLCAP)) == 0
|
|
3458 || (bw->bw_flags & BWF_KEEPCAP))
|
|
3459 bw = bw2;
|
|
3460 }
|
|
3461
|
|
3462 /* Write first basic word. If it's KEEPCAP then we need a word
|
|
3463 * without VALID flag first (makes it easier to read the list back
|
|
3464 * in). */
|
|
3465 if (bw->bw_flags & BWF_KEEPCAP)
|
|
3466 write_bword(fd, bw, TRUE, &prevbw, regionmask, prefm, suffm);
|
|
3467 write_bword(fd, bw, FALSE, &prevbw, regionmask, prefm, suffm);
|
|
3468
|
|
3469 /* Write other basic words, with different caps. */
|
|
3470 for (i = 0; i < bwga.ga_len; ++i)
|
|
3471 {
|
|
3472 bw2 = ((basicword_T **)bwga.ga_data)[i];
|
|
3473 if (bw2 != bw)
|
|
3474 write_bword(fd, bw2, FALSE, &prevbw, regionmask,
|
|
3475 prefm, suffm);
|
|
3476 }
|
|
3477 }
|
|
3478
|
|
3479 ga_clear(&bwga);
|
|
3480 }
|
|
3481
|
|
3482 fclose(fd);
|
|
3483 }
|
|
3484
|
|
3485 /*
|
|
3486 * Write basic word, followed by any additions.
|
|
3487 *
|
|
3488 * <worditem>: <nr> <string> <flags> [<flags2>]
|
|
3489 * [<caselen> <caseword>]
|
|
3490 * [<affixcnt> <affixNR> ...] (prefixes)
|
|
3491 * [<affixcnt> <affixNR> ...] (suffixes)
|
|
3492 * [<region>]
|
|
3493 * [<addcnt> <add> ...]
|
|
3494 */
|
|
3495 static void
|
|
3496 write_bword(fd, bwf, lowcap, prevbw, regionmask, prefm, suffm)
|
|
3497 FILE *fd;
|
|
3498 basicword_T *bwf;
|
|
3499 int lowcap; /* write KEEPKAP word as not-valid */
|
|
3500 basicword_T **prevbw; /* last written basic word */
|
|
3501 int regionmask; /* mask that includes all possible regions */
|
|
3502 int prefm;
|
|
3503 int suffm;
|
|
3504 {
|
|
3505 int flags;
|
|
3506 int aflags;
|
|
3507 int len;
|
|
3508 int leadlen, addlen;
|
|
3509 int clen;
|
|
3510 int adds = 0;
|
|
3511 int i;
|
|
3512 basicword_T *bw, *bw2;
|
|
3513
|
|
3514 /* Check how many bytes can be copied from the previous word. */
|
|
3515 len = STRLEN(bwf->bw_word);
|
|
3516 if (*prevbw == NULL)
|
|
3517 clen = 0;
|
|
3518 else
|
|
3519 for (clen = 0; clen < len
|
|
3520 && (*prevbw)->bw_word[clen] == bwf->bw_word[clen]; ++clen)
|
|
3521 ;
|
|
3522 putc(clen, fd); /* <nr> */
|
|
3523 *prevbw = bwf;
|
|
3524 /* <string> */
|
|
3525 if (len > clen)
|
|
3526 fwrite(bwf->bw_word + clen, (size_t)(len - clen), (size_t)1, fd);
|
|
3527
|
|
3528 /* Try to find a word without additions to use first. */
|
|
3529 bw = bwf;
|
|
3530 for (bw2 = bwf; bw2 != NULL; bw2 = bw2->bw_cnext)
|
|
3531 {
|
|
3532 if (bw2->bw_addstring != NULL || bw2->bw_leadstring != NULL)
|
|
3533 ++adds;
|
|
3534 else
|
|
3535 bw = bw2;
|
|
3536 }
|
|
3537
|
|
3538 /* Flags: If there is no leadstring and no addstring the basic word is
|
|
3539 * valid, may have prefixes, suffixes and region. */
|
|
3540 flags = bw->bw_flags;
|
|
3541 if (bw->bw_addstring == NULL && bw->bw_leadstring == NULL)
|
|
3542 {
|
|
3543 flags |= BWF_VALID;
|
|
3544
|
|
3545 /* Add the prefix/suffix list if there are prefixes/suffixes. */
|
|
3546 if (bw->bw_prefix.ga_len > 0)
|
|
3547 flags |= BWF_PREFIX;
|
|
3548 if (bw->bw_suffix.ga_len > 0)
|
|
3549 flags |= BWF_SUFFIX;
|
|
3550
|
|
3551 /* Flags: add the region byte if the word isn't valid in all
|
|
3552 * regions. */
|
|
3553 if (regionmask != 0 && (bw->bw_region & regionmask) != regionmask)
|
|
3554 flags |= BWF_REGION;
|
|
3555 }
|
|
3556
|
|
3557 /* Flags: may have additions. */
|
|
3558 if (adds > 0)
|
|
3559 flags |= BWF_ADDS;
|
|
3560
|
|
3561 /* The dummy word before a KEEPCAP word doesn't have any flags, they are
|
|
3562 * in the actual word that follows. */
|
|
3563 if (lowcap)
|
|
3564 flags = 0;
|
|
3565
|
|
3566 /* Flags: when the upper byte is not used we only write one flags
|
|
3567 * byte, if it's used then set an extra flag in the first byte and
|
|
3568 * also write the second byte. */
|
|
3569 if ((flags & 0xff00) == 0)
|
|
3570 putc(flags, fd); /* <flags> */
|
|
3571 else
|
|
3572 {
|
|
3573 putc(flags | BWF_SECOND, fd); /* <flags> */
|
|
3574 putc((int)((unsigned)flags >> 8), fd); /* <flags2> */
|
|
3575 }
|
|
3576
|
|
3577 /* First dummy word doesn't need anything but flags. */
|
|
3578 if (lowcap)
|
|
3579 return;
|
|
3580
|
|
3581 if (flags & BWF_KEEPCAP)
|
|
3582 {
|
|
3583 len = STRLEN(bw->bw_caseword);
|
|
3584 putc(len, fd); /* <caselen> */
|
|
3585 for (i = 0; i < len; ++i)
|
|
3586 putc(bw->bw_caseword[i], fd); /* <caseword> */
|
|
3587 }
|
|
3588
|
|
3589 /* write prefix and suffix lists: <affixcnt> <affixNR> ... */
|
|
3590 if (flags & BWF_PREFIX)
|
|
3591 write_affixlist(fd, &bw->bw_prefix, prefm);
|
|
3592 if (flags & BWF_SUFFIX)
|
|
3593 write_affixlist(fd, &bw->bw_suffix, suffm);
|
|
3594
|
|
3595 if (flags & BWF_REGION)
|
|
3596 putc(bw->bw_region, fd); /* <region> */
|
|
3597
|
|
3598 /*
|
|
3599 * Additions.
|
|
3600 */
|
|
3601 if (adds > 0)
|
|
3602 {
|
|
3603 put_bytes(fd, (long_u)adds, 2); /* <addcnt> */
|
|
3604
|
|
3605 for (bw = bwf; bw != NULL; bw = bw->bw_cnext)
|
|
3606 if (bw->bw_leadstring != NULL || bw->bw_addstring != NULL)
|
|
3607 {
|
|
3608 /* <add>: <addflags> <addlen> [<leadlen> <addstring>]
|
|
3609 * [<region>] */
|
|
3610 aflags = 0;
|
|
3611 if (bw->bw_flags & BWF_ONECAP)
|
|
3612 aflags |= ADD_ONECAP;
|
|
3613 if (bw->bw_flags & BWF_ALLCAP)
|
|
3614 aflags |= ADD_ALLCAP;
|
|
3615 if (bw->bw_flags & BWF_KEEPCAP)
|
|
3616 aflags |= ADD_KEEPCAP;
|
|
3617 if (regionmask != 0
|
|
3618 && (bw->bw_region & regionmask) != regionmask)
|
|
3619 aflags |= ADD_REGION;
|
|
3620 putc(aflags, fd); /* <addflags> */
|
|
3621
|
|
3622 if (bw->bw_leadstring == NULL)
|
|
3623 leadlen = 0;
|
|
3624 else
|
|
3625 leadlen = STRLEN(bw->bw_leadstring);
|
|
3626 if (bw->bw_addstring == NULL)
|
|
3627 addlen = 0;
|
|
3628 else
|
|
3629 addlen = STRLEN(bw->bw_addstring);
|
|
3630 putc(leadlen + addlen, fd); /* <addlen> */
|
|
3631 putc(leadlen, fd); /* <leadlen> */
|
|
3632 /* <addstring> */
|
|
3633 if (bw->bw_leadstring != NULL)
|
|
3634 fwrite(bw->bw_leadstring, (size_t)leadlen, (size_t)1, fd);
|
|
3635 if (bw->bw_addstring != NULL)
|
|
3636 fwrite(bw->bw_addstring, (size_t)addlen, (size_t)1, fd);
|
|
3637
|
|
3638 if (aflags & ADD_REGION)
|
|
3639 putc(bw->bw_region, fd); /* <region> */
|
|
3640 }
|
|
3641 }
|
|
3642 }
|
|
3643
|
|
3644
|
|
3645 /*
|
|
3646 * ":mkspell outfile infile ..."
|
|
3647 */
|
|
3648 void
|
|
3649 ex_mkspell(eap)
|
|
3650 exarg_T *eap;
|
|
3651 {
|
|
3652 int fcount;
|
|
3653 char_u **fnames;
|
|
3654 char_u fname[MAXPATHL];
|
|
3655 char_u wfname[MAXPATHL];
|
|
3656 afffile_T *(afile[8]);
|
|
3657 hashtab_T dfile[8];
|
|
3658 int i;
|
|
3659 int len;
|
|
3660 char_u region_name[16];
|
|
3661 struct stat st;
|
|
3662 int round;
|
|
3663 vimconv_T conv;
|
|
3664
|
|
3665 /* Expand all the arguments (e.g., $VIMRUNTIME). */
|
|
3666 if (get_arglist_exp(eap->arg, &fcount, &fnames) == FAIL)
|
|
3667 return;
|
|
3668 if (fcount < 2)
|
|
3669 EMSG(_(e_invarg)); /* need at least output and input names */
|
|
3670 else if (fcount > 9)
|
|
3671 EMSG(_("E754: Only up to 8 regions supported"));
|
|
3672 else
|
|
3673 {
|
|
3674 /* Check for overwriting before doing things that may take a lot of
|
|
3675 * time. */
|
|
3676 sprintf((char *)wfname, "%s.%s.spl", fnames[0], p_enc);
|
|
3677 if (!eap->forceit && mch_stat((char *)wfname, &st) >= 0)
|
|
3678 {
|
|
3679 EMSG(_(e_exists));
|
|
3680 goto theend;
|
|
3681 }
|
|
3682 if (mch_isdir(fnames[0]))
|
|
3683 {
|
|
3684 EMSG2(_(e_isadir2), fnames[0]);
|
|
3685 goto theend;
|
|
3686 }
|
|
3687
|
|
3688 /*
|
|
3689 * Init the aff and dic pointers.
|
|
3690 * Get the region names if there are more than 2 arguments.
|
|
3691 */
|
|
3692 for (i = 1; i < fcount; ++i)
|
|
3693 {
|
|
3694 afile[i - 1] = NULL;
|
|
3695 hash_init(&dfile[i - 1]);
|
|
3696 if (fcount > 2)
|
|
3697 {
|
|
3698 len = STRLEN(fnames[i]);
|
|
3699 if (STRLEN(gettail(fnames[i])) < 5 || fnames[i][len - 3] != '_')
|
|
3700 {
|
|
3701 EMSG2(_("E755: Invalid region in %s"), fnames[i]);
|
|
3702 goto theend;
|
|
3703 }
|
|
3704 else
|
|
3705 {
|
|
3706 region_name[(i - 1) * 2] = TOLOWER_ASC(fnames[i][len - 2]);
|
|
3707 region_name[(i - 1) * 2 + 1] =
|
|
3708 TOLOWER_ASC(fnames[i][len - 1]);
|
|
3709 }
|
|
3710 }
|
|
3711 }
|
|
3712
|
|
3713 /*
|
|
3714 * Read all the .aff and .dic files.
|
|
3715 * Text is converted to 'encoding'.
|
|
3716 */
|
|
3717 for (i = 1; i < fcount; ++i)
|
|
3718 {
|
|
3719 /* Read the .aff file. Will init "conv" based on the "SET" line. */
|
|
3720 conv.vc_type = CONV_NONE;
|
|
3721 sprintf((char *)fname, "%s.aff", fnames[i]);
|
|
3722 if ((afile[i - 1] = spell_read_aff(fname, &conv)) == NULL)
|
|
3723 break;
|
|
3724
|
|
3725 /* Read the .dic file. */
|
|
3726 sprintf((char *)fname, "%s.dic", fnames[i]);
|
|
3727 if (spell_read_dic(&dfile[i - 1], fname, &conv) == FAIL)
|
|
3728 break;
|
|
3729
|
|
3730 /* Free any conversion stuff. */
|
|
3731 convert_setup(&conv, NULL, NULL);
|
|
3732 }
|
|
3733
|
|
3734 /* Process the data when all the files could be read. */
|
|
3735 if (i == fcount)
|
|
3736 {
|
|
3737 garray_T prefga;
|
|
3738 garray_T suffga;
|
|
3739 garray_T *gap;
|
|
3740 hashtab_T newwords;
|
|
3741
|
|
3742 /*
|
|
3743 * Combine all the affixes into one new affix list. This is done
|
|
3744 * for prefixes and suffixes separately.
|
|
3745 * We need to do this for each region, try to re-use the same
|
|
3746 * affixes.
|
|
3747 * Since we number the new affix entries, a growarray will do. In
|
|
3748 * the affheader_T the ah_key is unused.
|
|
3749 */
|
|
3750 MSG(_("Combining affixes..."));
|
|
3751 out_flush();
|
|
3752 for (round = 1; round <= 2; ++round)
|
|
3753 {
|
|
3754 gap = round == 1 ? &prefga : &suffga;
|
|
3755 ga_init2(gap, sizeof(affheader_T), 50);
|
|
3756 for (i = 1; i < fcount; ++i)
|
|
3757 get_new_aff(round == 1 ? &afile[i - 1]->af_pref
|
|
3758 : &afile[i - 1]->af_suff, gap);
|
|
3759 }
|
|
3760
|
|
3761 /*
|
|
3762 * Go over all words and:
|
|
3763 * - change the old affix names to the new affix numbers
|
|
3764 * - check the conditions
|
|
3765 * - fold case
|
|
3766 * - extract the basic word and additions.
|
|
3767 * Do this for each region.
|
|
3768 */
|
|
3769 MSG(_("Building word list..."));
|
|
3770 out_flush();
|
|
3771 hash_init(&newwords);
|
|
3772
|
|
3773 for (i = 1; i < fcount; ++i)
|
|
3774 build_wordlist(&newwords, &dfile[i - 1], afile[i - 1],
|
|
3775 1 << (i - 1));
|
|
3776
|
|
3777 if (fcount > 2)
|
|
3778 {
|
|
3779 /* Combine words for the different regions into one. */
|
|
3780 MSG(_("Combining regions..."));
|
|
3781 out_flush();
|
|
3782 combine_regions(&newwords);
|
|
3783 }
|
|
3784
|
|
3785 /*
|
|
3786 * Affixes on a word with additions are clumsy, would require
|
|
3787 * inefficient searching. Turn the affixes into additions and/or
|
|
3788 * the expanded word.
|
|
3789 */
|
|
3790 MSG(_("Processing words..."));
|
|
3791 out_flush();
|
|
3792 expand_affixes(&newwords, &prefga, &suffga);
|
|
3793
|
|
3794 /* Write the info in the spell file. */
|
|
3795 smsg((char_u *)_("Writing spell file %s..."), wfname);
|
|
3796 out_flush();
|
|
3797 write_vim_spell(wfname, &prefga, &suffga, &newwords,
|
|
3798 fcount - 1, region_name);
|
|
3799 MSG(_("Done!"));
|
|
3800 out_flush();
|
|
3801
|
|
3802 /* Free the allocated stuff. */
|
|
3803 free_wordtable(&newwords);
|
|
3804 for (round = 1; round <= 2; ++round)
|
|
3805 {
|
|
3806 gap = round == 1 ? &prefga: &suffga;
|
|
3807 for (i = 0; i < gap->ga_len; ++i)
|
|
3808 free_affixentries(((affheader_T *)gap->ga_data + i)
|
|
3809 ->ah_first);
|
|
3810 ga_clear(gap);
|
|
3811 }
|
|
3812 }
|
|
3813
|
|
3814 /* Free the .aff and .dic file structures. */
|
|
3815 for (i = 1; i < fcount; ++i)
|
|
3816 {
|
|
3817 if (afile[i - 1] != NULL)
|
|
3818 spell_free_aff(afile[i - 1]);
|
|
3819 spell_free_dic(&dfile[i - 1]);
|
|
3820 }
|
|
3821 }
|
|
3822
|
|
3823 theend:
|
|
3824 FreeWild(fcount, fnames);
|
|
3825 }
|
|
3826
|
|
3827 static void
|
|
3828 free_wordtable(ht)
|
|
3829 hashtab_T *ht;
|
|
3830 {
|
|
3831 int todo;
|
|
3832 basicword_T *bw, *nbw;
|
|
3833 hashitem_T *hi;
|
|
3834
|
|
3835 todo = ht->ht_used;
|
|
3836 for (hi = ht->ht_array; todo > 0; ++hi)
|
|
3837 {
|
|
3838 if (!HASHITEM_EMPTY(hi))
|
|
3839 {
|
|
3840 --todo;
|
|
3841 for (bw = HI2BW(hi); bw != NULL; bw = nbw)
|
|
3842 {
|
|
3843 nbw = bw->bw_next;
|
|
3844 free_basicword(bw);
|
|
3845 }
|
|
3846 }
|
|
3847 }
|
|
3848 }
|
|
3849
|
|
3850 /*
|
|
3851 * Free a basicword_T and what it contains.
|
|
3852 */
|
|
3853 static void
|
|
3854 free_basicword(bw)
|
|
3855 basicword_T *bw;
|
|
3856 {
|
|
3857 ga_clear(&bw->bw_prefix);
|
|
3858 ga_clear(&bw->bw_suffix);
|
|
3859 vim_free(bw->bw_caseword);
|
|
3860 vim_free(bw->bw_leadstring);
|
|
3861 vim_free(bw->bw_addstring);
|
|
3862 vim_free(bw);
|
|
3863 }
|
|
3864
|
|
3865 /*
|
|
3866 * Free a list of affentry_T.
|
|
3867 */
|
|
3868 static void
|
|
3869 free_affixentries(first)
|
|
3870 affentry_T *first;
|
|
3871 {
|
|
3872 affentry_T *ap, *an;
|
|
3873
|
|
3874 for (ap = first; ap != NULL; ap = an)
|
|
3875 {
|
|
3876 an = ap->ae_next;
|
|
3877 vim_free(ap->ae_chop);
|
|
3878 vim_free(ap->ae_add);
|
|
3879 vim_free(ap->ae_cond);
|
|
3880 vim_free(ap->ae_prog);
|
|
3881 vim_free(ap);
|
|
3882 }
|
|
3883 }
|
|
3884
|
|
3885 #endif /* FEAT_MBYTE */
|
|
3886
|
|
3887 #endif /* FEAT_SYN_HL */
|
|
3888
|
|
3889 #if 0 /* old spell code with words in .spl file */
|
|
3890 /*
|
|
3891 * Structure that is used to store the text from the language file. This
|
|
3892 * avoids the need to allocate space for each individual word. It's allocated
|
|
3893 * in big chunks for speed.
|
223
|
3894 */
|
|
3895 #define SBLOCKSIZE 4096 /* default size of sb_data */
|
|
3896 typedef struct sblock_S sblock_T;
|
|
3897 struct sblock_S
|
|
3898 {
|
|
3899 sblock_T *sb_next; /* next block in list */
|
|
3900 char_u sb_data[1]; /* data, actually longer */
|
|
3901 };
|
|
3902
|
226
|
3903 /* Structure to store words and additions. Used twice : once for case-folded
|
|
3904 * and once for keep-case words. */
|
|
3905 typedef struct winfo_S
|
|
3906 {
|
|
3907 hashtab_T wi_ht; /* hashtable with all words, both dword_T and
|
|
3908 nword_T (check flags for DW_NWORD) */
|
|
3909 garray_T wi_add; /* table with pointers to additions in a
|
|
3910 dword_T */
|
|
3911 int wi_addlen; /* longest addition length */
|
|
3912 } winfo_T;
|
|
3913
|
223
|
3914 /*
|
|
3915 * Structure used to store words and other info for one language.
|
|
3916 */
|
|
3917 typedef struct slang_S slang_T;
|
|
3918 struct slang_S
|
|
3919 {
|
|
3920 slang_T *sl_next; /* next language */
|
|
3921 char_u sl_name[2]; /* language name "en", "nl", etc. */
|
226
|
3922 winfo_T sl_fwords; /* case-folded words and additions */
|
|
3923 winfo_T sl_kwords; /* keep-case words and additions */
|
|
3924 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
|
223
|
3925 sblock_T *sl_block; /* list with allocated memory blocks */
|
|
3926 };
|
|
3927
|
|
3928 static slang_T *first_lang = NULL;
|
|
3929
|
226
|
3930 /* Entry for dword in "sl_ht". Also used for part of an nword, starting with
|
|
3931 * the first non-word character. And used for additions in wi_add. */
|
|
3932 typedef struct dword_S
|
|
3933 {
|
|
3934 char_u dw_region; /* one bit per region where it's valid */
|
236
|
3935 char_u dw_flags; /* DW_ flags */
|
226
|
3936 char_u dw_word[1]; /* actually longer, NUL terminated */
|
|
3937 } dword_T;
|
|
3938
|
|
3939 #define REGION_ALL 0xff
|
|
3940
|
|
3941 #define HI2DWORD(hi) (dword_T *)(hi->hi_key - 2)
|
|
3942
|
|
3943 /* Entry for a nword in "sl_ht". Note that the last three items must be
|
|
3944 * identical to dword_T, so that they can be in the same hashtable. */
|
|
3945 typedef struct nword_S
|
|
3946 {
|
|
3947 garray_T nw_ga; /* table with pointers to dword_T for part
|
|
3948 starting with non-word character */
|
|
3949 int nw_maxlen; /* longest nword length (after the dword) */
|
|
3950 char_u nw_region; /* one bit per region where it's valid */
|
236
|
3951 char_u nw_flags; /* DW_ flags */
|
226
|
3952 char_u nw_word[1]; /* actually longer, NUL terminated */
|
|
3953 } nword_T;
|
|
3954
|
|
3955 /* Get nword_T pointer from hashitem that uses nw_word */
|
|
3956 static nword_T dumnw;
|
|
3957 #define HI2NWORD(hi) ((nword_T *)((hi)->hi_key - (dumnw.nw_word - (char_u *)&dumnw)))
|
|
3958
|
|
3959 #define DW_CAP 0x01 /* word must start with capital */
|
|
3960 #define DW_RARE 0x02 /* rare word */
|
|
3961 #define DW_NWORD 0x04 /* this is an nword_T */
|
|
3962 #define DW_DWORD 0x08 /* (also) use as dword without nword */
|
|
3963
|
223
|
3964 /*
|
|
3965 * Structure used in "b_langp", filled from 'spelllang'.
|
|
3966 */
|
|
3967 typedef struct langp_S
|
|
3968 {
|
|
3969 slang_T *lp_slang; /* info for this language (NULL for last one) */
|
|
3970 int lp_region; /* bitmask for region or REGION_ALL */
|
|
3971 } langp_T;
|
|
3972
|
|
3973 #define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
|
226
|
3974 #define DWORD_ENTRY(gap, i) *(((dword_T **)(gap)->ga_data) + i)
|
223
|
3975
|
|
3976 #define SP_OK 0
|
|
3977 #define SP_BAD 1
|
|
3978 #define SP_RARE 2
|
|
3979 #define SP_LOCAL 3
|
|
3980
|
226
|
3981 static char *e_invchar2 = N_("E753: Invalid character in \"%s\"");
|
|
3982
|
223
|
3983 static slang_T *spell_load_lang __ARGS((char_u *lang));
|
|
3984 static void spell_load_file __ARGS((char_u *fname));
|
|
3985 static int find_region __ARGS((char_u *rp, char_u *region));
|
|
3986
|
|
3987 /*
|
|
3988 * Main spell-checking function.
|
|
3989 * "ptr" points to the start of a word.
|
|
3990 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
|
|
3991 * or when it's OK it remains unchanged.
|
|
3992 * This must only be called when 'spelllang' is not empty.
|
|
3993 * Returns the length of the word in bytes, also when it's OK, so that the
|
|
3994 * caller can skip over the word.
|
|
3995 */
|
|
3996 int
|
|
3997 spell_check(wp, ptr, attrp)
|
|
3998 win_T *wp; /* current window */
|
|
3999 char_u *ptr;
|
|
4000 int *attrp;
|
|
4001 {
|
226
|
4002 char_u *e; /* end of word */
|
|
4003 char_u *ne; /* new end of word */
|
|
4004 char_u *me; /* max. end of match */
|
223
|
4005 langp_T *lp;
|
|
4006 int result;
|
|
4007 int len = 0;
|
|
4008 hashitem_T *hi;
|
226
|
4009 int round;
|
|
4010 char_u kword[MAXWLEN + 1]; /* word copy */
|
|
4011 char_u fword[MAXWLEN + 1]; /* word with case folded */
|
|
4012 char_u match[MAXWLEN + 1]; /* fword with additional chars */
|
|
4013 char_u kwordclen[MAXWLEN + 1]; /* len of orig chars after kword[] */
|
|
4014 char_u fwordclen[MAXWLEN + 1]; /* len of chars after fword[] */
|
|
4015 char_u *clen;
|
|
4016 int cidx = 0; /* char index in xwordclen[] */
|
|
4017 hash_T fhash; /* hash for fword */
|
|
4018 hash_T khash; /* hash for kword */
|
|
4019 int match_len = 0; /* length of match[] */
|
|
4020 int fmatch_len = 0; /* length of nword match in chars */
|
223
|
4021 garray_T *gap;
|
226
|
4022 int l, t;
|
|
4023 char_u *p, *tp;
|
223
|
4024 int n;
|
226
|
4025 dword_T *dw;
|
|
4026 dword_T *tdw;
|
|
4027 winfo_T *wi;
|
|
4028 nword_T *nw;
|
|
4029 int w_isupper;
|
223
|
4030
|
|
4031 /* Find the end of the word. We already know that *ptr is a word char. */
|
|
4032 e = ptr;
|
|
4033 do
|
|
4034 {
|
|
4035 mb_ptr_adv(e);
|
|
4036 ++len;
|
226
|
4037 } while (*e != NUL && spell_iswordc(e));
|
|
4038
|
|
4039 /* A word starting with a number is always OK. */
|
|
4040 if (*ptr >= '0' && *ptr <= '9')
|
|
4041 return (int)(e - ptr);
|
|
4042
|
|
4043 #ifdef FEAT_MBYTE
|
|
4044 w_isupper = MB_ISUPPER(mb_ptr2char(ptr));
|
|
4045 #else
|
|
4046 w_isupper = MB_ISUPPER(*ptr);
|
|
4047 #endif
|
|
4048
|
|
4049 /* Make a copy of the word so that it can be NUL terminated.
|
|
4050 * Compute hash value. */
|
|
4051 mch_memmove(kword, ptr, e - ptr);
|
|
4052 kword[e - ptr] = NUL;
|
|
4053 khash = hash_hash(kword);
|
|
4054
|
|
4055 /* Make case-folded copy of the Word. Compute its hash value. */
|
|
4056 (void)str_foldcase(ptr, e - ptr, fword, MAXWLEN + 1);
|
|
4057 fhash = hash_hash(fword);
|
|
4058
|
|
4059 /* Further case-folded characters to check for an nword match go in
|
|
4060 * match[]. */
|
|
4061 me = e;
|
|
4062
|
|
4063 /* "ne" is the end for the longest match */
|
|
4064 ne = e;
|
223
|
4065
|
|
4066 /* The word is bad unless we find it in the dictionary. */
|
|
4067 result = SP_BAD;
|
|
4068
|
|
4069 /*
|
|
4070 * Loop over the languages specified in 'spelllang'.
|
226
|
4071 * We check them all, because a matching nword may be longer than an
|
|
4072 * already found dword or nword.
|
223
|
4073 */
|
226
|
4074 for (lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0); lp->lp_slang != NULL; ++lp)
|
223
|
4075 {
|
226
|
4076 /*
|
|
4077 * Check for a matching word in the hashtable.
|
|
4078 * Check both the keep-case word and the fold-case word.
|
|
4079 */
|
|
4080 for (round = 0; round <= 1; ++round)
|
223
|
4081 {
|
226
|
4082 if (round == 0)
|
|
4083 {
|
|
4084 wi = &lp->lp_slang->sl_kwords;
|
|
4085 hi = hash_lookup(&wi->wi_ht, kword, khash);
|
|
4086 }
|
|
4087 else
|
|
4088 {
|
|
4089 wi = &lp->lp_slang->sl_fwords;
|
|
4090 hi = hash_lookup(&wi->wi_ht, fword, fhash);
|
|
4091 }
|
223
|
4092 if (!HASHITEM_EMPTY(hi))
|
|
4093 {
|
226
|
4094 /*
|
|
4095 * If this is an nword entry, check for match with remainder.
|
|
4096 */
|
|
4097 dw = HI2DWORD(hi);
|
|
4098 if (dw->dw_flags & DW_NWORD)
|
223
|
4099 {
|
226
|
4100 /* If the word is not defined as a dword we must find an
|
|
4101 * nword. */
|
|
4102 if ((dw->dw_flags & DW_DWORD) == 0)
|
|
4103 dw = NULL;
|
|
4104
|
|
4105 /* Fold more characters when needed for the nword. Need
|
|
4106 * to do one extra to check for a non-word character after
|
|
4107 * the nword. Also keep the byte-size of each character,
|
|
4108 * both before and after folding case. */
|
|
4109 nw = HI2NWORD(hi);
|
|
4110 while ((round == 0
|
|
4111 ? me - e <= nw->nw_maxlen
|
|
4112 : match_len <= nw->nw_maxlen)
|
|
4113 && *me != NUL)
|
|
4114 {
|
223
|
4115 #ifdef FEAT_MBYTE
|
226
|
4116 l = mb_ptr2len_check(me);
|
223
|
4117 #else
|
226
|
4118 l = 1;
|
223
|
4119 #endif
|
226
|
4120 (void)str_foldcase(me, l, match + match_len,
|
|
4121 MAXWLEN - match_len + 1);
|
|
4122 me += l;
|
|
4123 kwordclen[cidx] = l;
|
|
4124 fwordclen[cidx] = STRLEN(match + match_len);
|
|
4125 match_len += fwordclen[cidx];
|
|
4126 ++cidx;
|
|
4127 }
|
|
4128
|
|
4129 if (round == 0)
|
|
4130 {
|
|
4131 clen = kwordclen;
|
|
4132 tp = e;
|
|
4133 }
|
|
4134 else
|
|
4135 {
|
|
4136 clen = fwordclen;
|
|
4137 tp = match;
|
|
4138 }
|
|
4139
|
|
4140 /* Match with each item. The longest match wins:
|
|
4141 * "you've" is longer than "you". */
|
|
4142 gap = &nw->nw_ga;
|
|
4143 for (t = 0; t < gap->ga_len; ++t)
|
223
|
4144 {
|
226
|
4145 /* Skip entries with wrong case for first char.
|
|
4146 * Continue if it's a rare word without a captial. */
|
|
4147 tdw = DWORD_ENTRY(gap, t);
|
|
4148 if ((tdw->dw_flags & (DW_CAP | DW_RARE)) == DW_CAP
|
|
4149 && !w_isupper)
|
|
4150 continue;
|
|
4151
|
|
4152 p = tdw->dw_word;
|
|
4153 l = 0;
|
|
4154 for (n = 0; p[n] != 0; n += clen[l++])
|
|
4155 if (vim_memcmp(p + n, tp + n, clen[l]) != 0)
|
|
4156 break;
|
|
4157
|
|
4158 /* Use a match if it's longer than previous matches
|
|
4159 * and the next character is not a word character. */
|
|
4160 if (p[n] == 0 && l > fmatch_len && (tp[n] == 0
|
|
4161 || !spell_iswordc(tp + n)))
|
|
4162 {
|
|
4163 dw = tdw;
|
|
4164 fmatch_len = l;
|
|
4165 if (round == 0)
|
|
4166 ne = tp + n;
|
|
4167 else
|
|
4168 {
|
|
4169 /* Need to use the length of the original
|
|
4170 * chars, not the fold-case ones. */
|
|
4171 ne = e;
|
|
4172 for (l = 0; l < fmatch_len; ++l)
|
|
4173 ne += kwordclen[l];
|
|
4174 }
|
|
4175 if ((lp->lp_region & tdw->dw_region) == 0)
|
|
4176 result = SP_LOCAL;
|
|
4177 else if ((tdw->dw_flags & DW_CAP) && !w_isupper)
|
|
4178 result = SP_RARE;
|
|
4179 else
|
|
4180 result = SP_OK;
|
|
4181 }
|
|
4182 }
|
|
4183
|
|
4184 }
|
|
4185
|
|
4186 if (dw != NULL)
|
|
4187 {
|
|
4188 if (dw->dw_flags & DW_CAP)
|
|
4189 {
|
|
4190 /* Need to check first letter is uppercase. If it is,
|
|
4191 * check region. If it isn't it may be a rare word.
|
|
4192 * */
|
|
4193 if (w_isupper)
|
|
4194 {
|
|
4195 if ((dw->dw_region & lp->lp_region) == 0)
|
|
4196 result = SP_LOCAL;
|
|
4197 else
|
|
4198 result = SP_OK;
|
|
4199 }
|
|
4200 else if (dw->dw_flags & DW_RARE)
|
|
4201 result = SP_RARE;
|
|
4202 }
|
|
4203 else
|
|
4204 {
|
|
4205 if ((dw->dw_region & lp->lp_region) == 0)
|
223
|
4206 result = SP_LOCAL;
|
226
|
4207 else if (dw->dw_flags & DW_RARE)
|
|
4208 result = SP_RARE;
|
223
|
4209 else
|
|
4210 result = SP_OK;
|
|
4211 }
|
|
4212 }
|
|
4213 }
|
|
4214 }
|
|
4215
|
226
|
4216 /*
|
|
4217 * Check for an addition.
|
|
4218 * Only after a dword, not after an nword.
|
|
4219 * Check both the keep-case word and the fold-case word.
|
|
4220 */
|
|
4221 if (fmatch_len == 0)
|
|
4222 for (round = 0; round <= 1; ++round)
|
223
|
4223 {
|
226
|
4224 if (round == 0)
|
|
4225 wi = &lp->lp_slang->sl_kwords;
|
|
4226 else
|
|
4227 wi = &lp->lp_slang->sl_fwords;
|
|
4228 gap = &wi->wi_add;
|
|
4229 if (gap->ga_len == 0) /* no additions, skip quickly */
|
|
4230 continue;
|
|
4231
|
|
4232 /* Fold characters when needed for the addition. Need to do one
|
|
4233 * extra to check for a word character after the addition. */
|
|
4234 while ((round == 0
|
|
4235 ? me - e <= wi->wi_addlen
|
|
4236 : match_len <= wi->wi_addlen)
|
|
4237 && *me != NUL)
|
223
|
4238 {
|
226
|
4239 #ifdef FEAT_MBYTE
|
|
4240 l = mb_ptr2len_check(me);
|
|
4241 #else
|
|
4242 l = 1;
|
|
4243 #endif
|
|
4244 (void)str_foldcase(me, l, match + match_len,
|
|
4245 MAXWLEN - match_len + 1);
|
|
4246 me += l;
|
|
4247 kwordclen[cidx] = l;
|
|
4248 fwordclen[cidx] = STRLEN(match + match_len);
|
|
4249 match_len += fwordclen[cidx];
|
|
4250 ++cidx;
|
223
|
4251 }
|
226
|
4252
|
|
4253 if (round == 0)
|
|
4254 {
|
|
4255 clen = kwordclen;
|
|
4256 tp = e;
|
|
4257 }
|
|
4258 else
|
|
4259 {
|
|
4260 clen = fwordclen;
|
|
4261 tp = match;
|
|
4262 }
|
223
|
4263
|
226
|
4264 /* Addition lookup. Uses a linear search, there should be
|
|
4265 * very few. If there is a match adjust "ne" to the end.
|
|
4266 * This doesn't change whether a word was good or bad, only
|
|
4267 * the length. */
|
|
4268 for (t = 0; t < gap->ga_len; ++t)
|
|
4269 {
|
|
4270 tdw = DWORD_ENTRY(gap, t);
|
|
4271 p = tdw->dw_word;
|
|
4272 l = 0;
|
|
4273 for (n = 0; p[n] != 0; n += clen[l++])
|
|
4274 if (vim_memcmp(p + n, tp + n, clen[l]) != 0)
|
|
4275 break;
|
|
4276
|
|
4277 /* Use a match if it's longer than previous matches
|
|
4278 * and the next character is not a word character. */
|
|
4279 if (p[n] == 0 && l > fmatch_len
|
|
4280 && (tp[n] == 0 || !spell_iswordc(tp + n)))
|
|
4281 {
|
|
4282 fmatch_len = l;
|
|
4283 if (round == 0)
|
|
4284 ne = tp + n;
|
|
4285 else
|
|
4286 {
|
|
4287 /* Need to use the length of the original
|
|
4288 * chars, not the fold-case ones. */
|
|
4289 ne = e;
|
|
4290 for (l = 0; l < fmatch_len; ++l)
|
|
4291 ne += kwordclen[l];
|
|
4292 }
|
|
4293 }
|
|
4294 }
|
223
|
4295 }
|
|
4296 }
|
|
4297
|
|
4298 if (result != SP_OK)
|
|
4299 {
|
|
4300 if (result == SP_BAD)
|
|
4301 *attrp = highlight_attr[HLF_SPB];
|
|
4302 else if (result == SP_RARE)
|
|
4303 *attrp = highlight_attr[HLF_SPR];
|
|
4304 else
|
|
4305 *attrp = highlight_attr[HLF_SPL];
|
|
4306 }
|
|
4307
|
226
|
4308 return (int)(ne - ptr);
|
223
|
4309 }
|
|
4310
|
|
4311 static slang_T *load_lp; /* passed from spell_load_lang() to
|
|
4312 spell_load_file() */
|
|
4313
|
|
4314 /*
|
|
4315 * Load language "lang[2]".
|
|
4316 */
|
|
4317 static slang_T *
|
|
4318 spell_load_lang(lang)
|
|
4319 char_u *lang;
|
|
4320 {
|
|
4321 slang_T *lp;
|
|
4322 char_u fname_enc[80];
|
|
4323 char_u fname_ascii[20];
|
|
4324 char_u *p;
|
226
|
4325 int r;
|
223
|
4326
|
|
4327 lp = (slang_T *)alloc(sizeof(slang_T));
|
|
4328 if (lp != NULL)
|
|
4329 {
|
|
4330 lp->sl_name[0] = lang[0];
|
|
4331 lp->sl_name[1] = lang[1];
|
226
|
4332 hash_init(&lp->sl_fwords.wi_ht);
|
|
4333 ga_init2(&lp->sl_fwords.wi_add, sizeof(dword_T *), 4);
|
|
4334 lp->sl_fwords.wi_addlen = 0;
|
|
4335 hash_init(&lp->sl_kwords.wi_ht);
|
|
4336 ga_init2(&lp->sl_kwords.wi_add, sizeof(dword_T *), 4);
|
|
4337 lp->sl_kwords.wi_addlen = 0;
|
223
|
4338 lp->sl_regions[0] = NUL;
|
|
4339 lp->sl_block = NULL;
|
|
4340
|
|
4341 /* Find all spell files for "lang" in 'runtimepath' and load them.
|
|
4342 * Use 'encoding', except that we use "latin1" for "latin9". */
|
|
4343 #ifdef FEAT_MBYTE
|
|
4344 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
|
|
4345 p = p_enc;
|
|
4346 else
|
|
4347 #endif
|
|
4348 p = (char_u *)"latin1";
|
|
4349 load_lp = lp;
|
|
4350 sprintf((char *)fname_enc, "spell/%c%c.%s.spl", lang[0], lang[1], p);
|
226
|
4351 r = do_in_runtimepath(fname_enc, TRUE, spell_load_file);
|
|
4352 if (r == FAIL)
|
223
|
4353 {
|
|
4354 /* Try again to find an ASCII spell file. */
|
|
4355 sprintf((char *)fname_ascii, "spell/%c%c.spl", lang[0], lang[1]);
|
226
|
4356 r = do_in_runtimepath(fname_ascii, TRUE, spell_load_file);
|
|
4357 }
|
|
4358
|
|
4359 if (r == FAIL)
|
|
4360 {
|
|
4361 vim_free(lp);
|
|
4362 lp = NULL;
|
|
4363 smsg((char_u *)_("Warning: Cannot find dictionary \"%s\""),
|
223
|
4364 fname_enc + 6);
|
|
4365 }
|
|
4366 else
|
|
4367 {
|
|
4368 lp->sl_next = first_lang;
|
|
4369 first_lang = lp;
|
|
4370 }
|
|
4371 }
|
|
4372
|
|
4373 return lp;
|
|
4374 }
|
|
4375
|
|
4376 /*
|
|
4377 * Load one spell file into "load_lp".
|
|
4378 * Invoked through do_in_runtimepath().
|
|
4379 */
|
|
4380 static void
|
|
4381 spell_load_file(fname)
|
|
4382 char_u *fname;
|
|
4383 {
|
|
4384 int fd;
|
|
4385 size_t len;
|
229
|
4386 int l;
|
226
|
4387 char_u *p = NULL, *np;
|
|
4388 sblock_T *bl = NULL;
|
|
4389 int bl_used = 0;
|
223
|
4390 size_t rest = 0;
|
226
|
4391 char_u *rbuf; /* read buffer */
|
|
4392 char_u *rbuf_end; /* past last valid char in "rbuf" */
|
223
|
4393 hash_T hash;
|
|
4394 hashitem_T *hi;
|
|
4395 int c;
|
226
|
4396 int cc;
|
223
|
4397 int region = REGION_ALL;
|
226
|
4398 int wlen;
|
|
4399 winfo_T *wi;
|
233
|
4400 dword_T *dw, *edw = NULL;
|
226
|
4401 nword_T *nw = NULL;
|
|
4402 int flags;
|
|
4403 char_u *save_sourcing_name = sourcing_name;
|
|
4404 linenr_T save_sourcing_lnum = sourcing_lnum;
|
|
4405
|
|
4406 rbuf = alloc((unsigned)(SBLOCKSIZE + MAXWLEN + 1));
|
|
4407 if (rbuf == NULL)
|
|
4408 return;
|
223
|
4409
|
|
4410 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
|
|
4411 if (fd < 0)
|
|
4412 {
|
|
4413 EMSG2(_(e_notopen), fname);
|
226
|
4414 goto theend;
|
223
|
4415 }
|
|
4416
|
226
|
4417 sourcing_name = fname;
|
|
4418 sourcing_lnum = 0;
|
|
4419
|
223
|
4420 /* Get the length of the whole file. */
|
|
4421 len = lseek(fd, (off_t)0, SEEK_END);
|
|
4422 lseek(fd, (off_t)0, SEEK_SET);
|
|
4423
|
226
|
4424 /*
|
|
4425 * Read the file one block at a time.
|
223
|
4426 * "rest" is the length of an incomplete line at the previous block.
|
226
|
4427 * "p" points to the remainder.
|
|
4428 */
|
223
|
4429 while (len > 0)
|
|
4430 {
|
226
|
4431 /* Read a block from the file. Prepend the remainder of the previous
|
|
4432 * block, if any. */
|
|
4433 if (rest > 0)
|
|
4434 {
|
|
4435 if (rest > MAXWLEN) /* truncate long line (should be comment) */
|
|
4436 rest = MAXWLEN;
|
|
4437 mch_memmove(rbuf, p, rest);
|
|
4438 --sourcing_lnum;
|
|
4439 }
|
223
|
4440 if (len > SBLOCKSIZE)
|
|
4441 l = SBLOCKSIZE;
|
|
4442 else
|
|
4443 l = len;
|
|
4444 len -= l;
|
226
|
4445 if (read(fd, rbuf + rest, l) != l)
|
223
|
4446 {
|
|
4447 EMSG2(_(e_notread), fname);
|
|
4448 break;
|
|
4449 }
|
226
|
4450 rbuf_end = rbuf + l + rest;
|
223
|
4451 rest = 0;
|
|
4452
|
|
4453 /* Deal with each line that was read until we finish the block. */
|
226
|
4454 for (p = rbuf; p < rbuf_end; p = np)
|
223
|
4455 {
|
226
|
4456 ++sourcing_lnum;
|
|
4457
|
|
4458 /* "np" points to the first char after the line (CR, NL or white
|
|
4459 * space). */
|
|
4460 for (np = p; np < rbuf_end && *np >= ' '; mb_ptr_adv(np))
|
|
4461 ;
|
|
4462 if (np >= rbuf_end)
|
223
|
4463 {
|
226
|
4464 /* Incomplete line or end of file. */
|
223
|
4465 rest = np - p;
|
|
4466 if (len == 0)
|
226
|
4467 EMSG(_("E751: Truncated spell file"));
|
223
|
4468 break;
|
|
4469 }
|
|
4470 *np = NUL; /* terminate the line with a NUL */
|
|
4471
|
226
|
4472 if (*p == '-')
|
223
|
4473 {
|
226
|
4474 /*
|
|
4475 * Region marker: ---, -xx, -xx-yy, etc.
|
|
4476 */
|
|
4477 ++p;
|
|
4478 if (*p == '-')
|
223
|
4479 {
|
226
|
4480 if (p[1] != '-' || p[2] != NUL)
|
|
4481 {
|
|
4482 EMSG2(_(e_invchar2), p - 1);
|
|
4483 len = 0;
|
|
4484 break;
|
|
4485 }
|
|
4486 region = REGION_ALL;
|
|
4487 }
|
|
4488 else
|
|
4489 {
|
|
4490 char_u *rp = load_lp->sl_regions;
|
|
4491 int r;
|
223
|
4492
|
226
|
4493 /* Start of a region. The region may be repeated:
|
|
4494 * "-ca-uk". Fill "region" with the bit mask for the
|
|
4495 * ones we find. */
|
|
4496 region = 0;
|
|
4497 for (;;)
|
223
|
4498 {
|
226
|
4499 r = find_region(rp, p);
|
|
4500 if (r == REGION_ALL)
|
|
4501 {
|
|
4502 /* new region, add it to sl_regions[] */
|
|
4503 r = STRLEN(rp);
|
|
4504 if (r >= 16)
|
|
4505 {
|
|
4506 EMSG2(_("E752: Too many regions: %s"), p);
|
|
4507 len = 0;
|
223
|
4508 break;
|
226
|
4509 }
|
|
4510 else
|
|
4511 {
|
|
4512 rp[r] = p[0];
|
|
4513 rp[r + 1] = p[1];
|
|
4514 rp[r + 2] = NUL;
|
|
4515 r = 1 << (r / 2);
|
|
4516 }
|
223
|
4517 }
|
|
4518 else
|
226
|
4519 r = 1 << r;
|
|
4520
|
|
4521 region |= r;
|
|
4522 if (p[2] != '-')
|
223
|
4523 {
|
226
|
4524 if (p[2] > ' ')
|
|
4525 {
|
|
4526 EMSG2(_(e_invchar2), p - 1);
|
|
4527 len = 0;
|
|
4528 }
|
|
4529 break;
|
223
|
4530 }
|
226
|
4531 p += 3;
|
223
|
4532 }
|
|
4533 }
|
226
|
4534 }
|
|
4535 else if (*p != '#' && *p != NUL)
|
|
4536 {
|
|
4537 /*
|
|
4538 * Not an empty line or comment.
|
|
4539 */
|
|
4540 if (*p == '!')
|
223
|
4541 {
|
226
|
4542 wi = &load_lp->sl_kwords; /* keep case */
|
223
|
4543 ++p;
|
226
|
4544 }
|
|
4545 else
|
|
4546 wi = &load_lp->sl_fwords; /* fold case */
|
|
4547
|
|
4548 flags = 0;
|
|
4549 c = *p;
|
|
4550 if (c == '>') /* rare word */
|
|
4551 {
|
|
4552 flags = DW_RARE;
|
|
4553 ++p;
|
|
4554 }
|
|
4555 else if (*p == '+') /* addition */
|
|
4556 ++p;
|
|
4557
|
|
4558 if (c != '+' && !spell_iswordc(p))
|
|
4559 {
|
|
4560 EMSG2(_(e_invchar2), p);
|
|
4561 len = 0;
|
|
4562 break;
|
|
4563 }
|
223
|
4564
|
226
|
4565 /* Make sure there is room for the word. Folding case may
|
|
4566 * double the size. */
|
|
4567 wlen = np - p;
|
|
4568 if (bl == NULL || bl_used + sizeof(dword_T) + wlen
|
|
4569 #ifdef FEAT_MBYTE
|
|
4570 * (has_mbyte ? 2 : 1)
|
|
4571 #endif
|
|
4572 >= SBLOCKSIZE)
|
|
4573 {
|
|
4574 /* Allocate a block of memory to store the dword_T in.
|
|
4575 * This is not freed until spell_reload() is called. */
|
|
4576 bl = (sblock_T *)alloc((unsigned)(sizeof(sblock_T)
|
|
4577 + SBLOCKSIZE));
|
|
4578 if (bl == NULL)
|
|
4579 {
|
|
4580 len = 0;
|
|
4581 break;
|
|
4582 }
|
|
4583 bl->sb_next = load_lp->sl_block;
|
|
4584 load_lp->sl_block = bl;
|
|
4585 bl_used = 0;
|
|
4586 }
|
|
4587 dw = (dword_T *)(bl->sb_data + bl_used);
|
223
|
4588
|
226
|
4589 /* For fold-case words fold the case and check for start
|
|
4590 * with uppercase letter. */
|
|
4591 if (wi == &load_lp->sl_fwords)
|
|
4592 {
|
|
4593 #ifdef FEAT_MBYTE
|
|
4594 if (MB_ISUPPER(mb_ptr2char(p)))
|
|
4595 #else
|
|
4596 if (MB_ISUPPER(*p))
|
|
4597 #endif
|
|
4598 flags |= DW_CAP;
|
|
4599
|
|
4600 /* Fold case. */
|
|
4601 (void)str_foldcase(p, np - p, dw->dw_word, wlen
|
|
4602 #ifdef FEAT_MBYTE
|
|
4603 * (has_mbyte ? 2 : 1)
|
|
4604 #endif
|
|
4605 + 1);
|
|
4606 #ifdef FEAT_MBYTE
|
|
4607 /* case folding may change length of word */
|
|
4608 wlen = STRLEN(dw->dw_word);
|
|
4609 #endif
|
223
|
4610 }
|
|
4611 else
|
|
4612 {
|
226
|
4613 /* Keep case: copy the word as-is. */
|
|
4614 mch_memmove(dw->dw_word, p, wlen + 1);
|
|
4615 }
|
|
4616
|
|
4617 if (c == '+')
|
|
4618 {
|
|
4619 garray_T *gap = &wi->wi_add;
|
|
4620
|
|
4621 /* Addition. TODO: search for matching entry? */
|
|
4622 if (wi->wi_addlen < wlen)
|
|
4623 wi->wi_addlen = wlen;
|
|
4624 if (ga_grow(gap, 1) == FAIL)
|
223
|
4625 {
|
226
|
4626 len = 0;
|
|
4627 break;
|
223
|
4628 }
|
226
|
4629 *(((dword_T **)gap->ga_data) + gap->ga_len) = dw;
|
|
4630 ++gap->ga_len;
|
|
4631 dw->dw_region = region;
|
|
4632 dw->dw_flags = flags;
|
|
4633 bl_used += sizeof(dword_T) + wlen;
|
|
4634 }
|
|
4635 else
|
|
4636 {
|
|
4637 /*
|
|
4638 * Check for a non-word character. If found it's
|
|
4639 * going to be an nword.
|
|
4640 * For an nword we split in two: the leading dword and
|
|
4641 * the remainder. The dword goes in the hashtable
|
|
4642 * with an nword_T, the remainder is put in the
|
|
4643 * dword_T (starting with the first non-word
|
|
4644 * character).
|
|
4645 */
|
|
4646 cc = NUL;
|
|
4647 for (p = dw->dw_word; *p != NUL; mb_ptr_adv(p))
|
|
4648 if (!spell_iswordc(p))
|
|
4649 {
|
|
4650 cc = *p;
|
|
4651 *p = NUL;
|
|
4652 break;
|
|
4653 }
|
223
|
4654
|
226
|
4655 /* check if we already have this dword */
|
|
4656 hash = hash_hash(dw->dw_word);
|
|
4657 hi = hash_lookup(&wi->wi_ht, dw->dw_word, hash);
|
223
|
4658 if (!HASHITEM_EMPTY(hi))
|
|
4659 {
|
226
|
4660 /* Existing entry. */
|
|
4661 edw = HI2DWORD(hi);
|
|
4662 if ((edw->dw_flags & (DW_CAP | DW_RARE))
|
|
4663 == (dw->dw_flags & (DW_CAP | DW_RARE)))
|
223
|
4664 {
|
|
4665 if (p_verbose > 0)
|
|
4666 smsg((char_u *)_("Warning: duplicate word \"%s\" in %s"),
|
226
|
4667 dw->dw_word, fname);
|
|
4668 }
|
|
4669 }
|
|
4670
|
|
4671 if (cc != NUL) /* nword */
|
|
4672 {
|
|
4673 if (HASHITEM_EMPTY(hi)
|
|
4674 || (edw->dw_flags & DW_NWORD) == 0)
|
|
4675 {
|
|
4676 sblock_T *sb;
|
|
4677
|
|
4678 /* Need to allocate a new nword_T. Put it in an
|
|
4679 * sblock_T, so that we can free it later. */
|
|
4680 sb = (sblock_T *)alloc(
|
|
4681 (unsigned)(sizeof(sblock_T)
|
|
4682 + sizeof(nword_T) + wlen));
|
|
4683 if (sb == NULL)
|
|
4684 {
|
|
4685 len = 0;
|
|
4686 break;
|
|
4687 }
|
|
4688 sb->sb_next = load_lp->sl_block;
|
|
4689 load_lp->sl_block = sb;
|
|
4690 nw = (nword_T *)sb->sb_data;
|
|
4691
|
|
4692 ga_init2(&nw->nw_ga, sizeof(dword_T *), 4);
|
|
4693 nw->nw_maxlen = 0;
|
|
4694 STRCPY(nw->nw_word, dw->dw_word);
|
|
4695 if (!HASHITEM_EMPTY(hi))
|
|
4696 {
|
|
4697 /* Note: the nw_region and nw_flags is for
|
|
4698 * the dword that matches with the start
|
|
4699 * of this nword, not for the nword
|
|
4700 * itself! */
|
|
4701 nw->nw_region = edw->dw_region;
|
|
4702 nw->nw_flags = edw->dw_flags | DW_NWORD;
|
|
4703
|
|
4704 /* Remove the dword item so that we can
|
|
4705 * add it as an nword. */
|
|
4706 hash_remove(&wi->wi_ht, hi);
|
|
4707 hi = hash_lookup(&wi->wi_ht,
|
|
4708 nw->nw_word, hash);
|
|
4709 }
|
|
4710 else
|
|
4711 {
|
|
4712 nw->nw_region = 0;
|
|
4713 nw->nw_flags = DW_NWORD;
|
|
4714 }
|
223
|
4715 }
|
|
4716 else
|
226
|
4717 nw = HI2NWORD(hi);
|
|
4718 }
|
|
4719
|
|
4720 if (HASHITEM_EMPTY(hi))
|
|
4721 {
|
|
4722 /* Add new dword or nword entry. */
|
|
4723 hash_add_item(&wi->wi_ht, hi, cc == NUL
|
|
4724 ? dw->dw_word : nw->nw_word, hash);
|
|
4725 if (cc == NUL)
|
|
4726 {
|
|
4727 /* New dword: init the values and count the
|
|
4728 * used space. */
|
|
4729 dw->dw_flags = DW_DWORD | flags;
|
|
4730 dw->dw_region = region;
|
|
4731 bl_used += sizeof(dword_T) + wlen;
|
|
4732 }
|
|
4733 }
|
|
4734 else if (cc == NUL)
|
|
4735 {
|
|
4736 /* existing dword: add the region and flags */
|
|
4737 dw = edw;
|
|
4738 dw->dw_region |= region;
|
|
4739 dw->dw_flags |= DW_DWORD | flags;
|
223
|
4740 }
|
226
|
4741
|
|
4742 if (cc != NUL)
|
|
4743 {
|
|
4744 /* Use the dword for the non-word character and
|
|
4745 * following characters. */
|
|
4746 dw->dw_region = region;
|
|
4747 dw->dw_flags = flags;
|
|
4748 STRCPY(dw->dw_word + 1, p + 1);
|
|
4749 dw->dw_word[0] = cc;
|
|
4750 l = wlen - (p - dw->dw_word);
|
|
4751 bl_used += sizeof(dword_T) + l;
|
|
4752 if (nw->nw_maxlen < l)
|
|
4753 nw->nw_maxlen = l;
|
|
4754
|
|
4755 /* Add the dword to the growarray in the nword. */
|
|
4756 if (ga_grow(&nw->nw_ga, 1) == FAIL)
|
|
4757 {
|
|
4758 len = 0;
|
|
4759 break;
|
|
4760 }
|
|
4761 *((dword_T **)nw->nw_ga.ga_data + nw->nw_ga.ga_len)
|
|
4762 = dw;
|
|
4763 ++nw->nw_ga.ga_len;
|
|
4764 }
|
223
|
4765 }
|
|
4766 }
|
|
4767
|
226
|
4768 /* Skip over CR and NL characters and trailing white space. */
|
|
4769 while (np < rbuf_end && *np <= ' ')
|
223
|
4770 ++np;
|
|
4771 }
|
|
4772 }
|
|
4773
|
|
4774 close(fd);
|
226
|
4775 theend:
|
|
4776 sourcing_name = save_sourcing_name;
|
|
4777 sourcing_lnum = save_sourcing_lnum;
|
|
4778 vim_free(rbuf);
|
223
|
4779 }
|
|
4780
|
236
|
4781
|
|
4782 #endif
|