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 *
|
300
|
13 * The spell checking mechanism uses a tree (aka trie). Each node in the tree
|
|
14 * has a list of bytes that can appear (siblings). For each byte there is a
|
|
15 * pointer to the node with the byte that follows in the word (child).
|
|
16 * A NUL byte is used where the word may end.
|
|
17 *
|
|
18 * There are two trees: one with case-folded words and one with words in
|
|
19 * original case. The second one is only used for keep-case words and is
|
|
20 * usually small.
|
|
21 *
|
|
22 * Thanks to Olaf Seibert for providing an example implementation of this tree
|
|
23 * and the compression mechanism.
|
243
|
24 *
|
|
25 * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
|
|
26 *
|
236
|
27 * Why doesn't Vim use aspell/ispell/myspell/etc.?
|
|
28 * See ":help develop-spell".
|
|
29 */
|
|
30
|
300
|
31 /*
|
|
32 * Vim spell file format: <HEADER> <SUGGEST> <LWORDTREE> <KWORDTREE>
|
|
33 *
|
|
34 * <HEADER>: <fileID> <regioncnt> <regionname> ...
|
|
35 * <charflagslen> <charflags> <fcharslen> <fchars>
|
|
36 *
|
|
37 * <fileID> 10 bytes "VIMspell05"
|
|
38 * <regioncnt> 1 byte number of regions following (8 supported)
|
307
|
39 * <regionname> 2 bytes Region name: ca, au, etc. Lower case.
|
300
|
40 * First <regionname> is region 1.
|
|
41 *
|
|
42 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
|
|
43 * <charflags> N bytes List of flags (first one is for character 128):
|
|
44 * 0x01 word character
|
307
|
45 * 0x02 upper-case character
|
300
|
46 * <fcharslen> 2 bytes Number of bytes in <fchars>.
|
|
47 * <fchars> N bytes Folded characters, first one is for character 128.
|
|
48 *
|
|
49 *
|
|
50 * <SUGGEST> : <suggestlen> <more> ...
|
|
51 *
|
|
52 * <suggestlen> 4 bytes Length of <SUGGEST> in bytes, excluding
|
|
53 * <suggestlen>. MSB first.
|
|
54 * <more> To be defined.
|
|
55 *
|
|
56 *
|
|
57 * <LWORDTREE>: <wordtree>
|
|
58 *
|
|
59 * <wordtree>: <nodecount> <nodedata> ...
|
|
60 *
|
|
61 * <nodecount> 4 bytes Number of nodes following. MSB first.
|
|
62 *
|
|
63 * <nodedata>: <siblingcount> <sibling> ...
|
|
64 *
|
|
65 * <siblingcount> 1 byte Number of siblings in this node. The siblings
|
|
66 * follow in sorted order.
|
|
67 *
|
|
68 * <sibling>: <byte> [<nodeidx> <xbyte> | <flags> [<region>]]
|
|
69 *
|
|
70 * <byte> 1 byte Byte value of the sibling. Special cases:
|
|
71 * BY_NOFLAGS: End of word without flags and for all
|
|
72 * regions.
|
|
73 * BY_FLAGS: End of word, <flags> follow.
|
|
74 * BY_INDEX: Child of sibling is shared, <nodeidx>
|
|
75 * and <xbyte> follow.
|
|
76 *
|
|
77 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
|
|
78 *
|
|
79 * <xbyte> 1 byte byte value of the sibling.
|
|
80 *
|
|
81 * <flags> 1 byte bitmask of:
|
|
82 * WF_ALLCAP word must have only capitals
|
|
83 * WF_ONECAP first char of word must be capital
|
|
84 * WF_RARE rare word
|
|
85 * WF_REGION <region> follows
|
|
86 *
|
|
87 * <region> 1 byte Bitmask for regions in which word is valid. When
|
|
88 * omitted it's valid in all regions.
|
|
89 * Lowest bit is for region 1.
|
|
90 *
|
|
91 * <KWORDTREE>: <wordtree>
|
|
92 *
|
|
93 *
|
|
94 * All text characters are in 'encoding', but stored as single bytes.
|
|
95 * The region name is ASCII.
|
|
96 */
|
|
97
|
223
|
98 #if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
|
|
99 # include <io.h> /* for lseek(), must be before vim.h */
|
|
100 #endif
|
|
101
|
|
102 #include "vim.h"
|
|
103
|
|
104 #if defined(FEAT_SYN_HL) || defined(PROTO)
|
|
105
|
|
106 #ifdef HAVE_FCNTL_H
|
|
107 # include <fcntl.h>
|
|
108 #endif
|
|
109
|
300
|
110 #define MAXWLEN 250 /* assume max. word len is this many bytes */
|
226
|
111
|
300
|
112 /* Flags used for a word. */
|
|
113 #define WF_REGION 0x01 /* region byte follows */
|
|
114 #define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
|
|
115 #define WF_ALLCAP 0x04 /* word must be all capitals */
|
|
116 #define WF_RARE 0x08 /* rare word */
|
307
|
117 #define WF_BANNED 0x10 /* bad word */
|
|
118
|
|
119 #define WF_KEEPCAP 0x100 /* keep-case word (not stored in file) */
|
300
|
120
|
|
121 #define BY_NOFLAGS 0 /* end of word without flags or region */
|
|
122 #define BY_FLAGS 1 /* end of word, flag byte follows */
|
|
123 #define BY_INDEX 2 /* child is shared, index follows */
|
|
124 #define BY_SPECIAL BY_INDEX /* hightest special byte value */
|
236
|
125
|
243
|
126 /* Info from "REP" entries in ".aff" file used in af_rep.
|
|
127 * TODO: This is not used yet. Either use it or remove it. */
|
236
|
128 typedef struct repentry_S
|
|
129 {
|
|
130 char_u *re_from;
|
|
131 char_u *re_to;
|
|
132 } repentry_T;
|
|
133
|
|
134 /*
|
243
|
135 * Structure used to store words and other info for one language, loaded from
|
|
136 * a .spl file.
|
300
|
137 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
|
|
138 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
|
|
139 *
|
|
140 * The "byts" array stores the possible bytes in each tree node, preceded by
|
|
141 * the number of possible bytes, sorted on byte value:
|
|
142 * <len> <byte1> <byte2> ...
|
|
143 * The "idxs" array stores the index of the child node corresponding to the
|
|
144 * byte in "byts".
|
|
145 * Exception: when the byte is zero, the word may end here and "idxs" holds
|
|
146 * the flags and region for the word. There may be several zeros in sequence
|
|
147 * for alternative flag/region combinations.
|
236
|
148 */
|
|
149 typedef struct slang_S slang_T;
|
|
150 struct slang_S
|
|
151 {
|
|
152 slang_T *sl_next; /* next language */
|
|
153 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
|
300
|
154 char_u *sl_fbyts; /* case-folded word bytes */
|
|
155 int *sl_fidxs; /* case-folded word indexes */
|
|
156 char_u *sl_kbyts; /* keep-case word bytes */
|
|
157 int *sl_kidxs; /* keep-case word indexes */
|
243
|
158 char_u *sl_try; /* "TRY" from .aff file TODO: not used */
|
|
159 garray_T sl_rep; /* list of repentry_T entries from REP lines
|
|
160 * TODO not used */
|
236
|
161 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
|
|
162 int sl_error; /* error while loading */
|
|
163 };
|
|
164
|
243
|
165 /* First language that is loaded, start of the linked list of loaded
|
|
166 * languages. */
|
236
|
167 static slang_T *first_lang = NULL;
|
|
168
|
|
169 /*
|
|
170 * Structure used in "b_langp", filled from 'spelllang'.
|
|
171 */
|
|
172 typedef struct langp_S
|
|
173 {
|
|
174 slang_T *lp_slang; /* info for this language (NULL for last one) */
|
|
175 int lp_region; /* bitmask for region or REGION_ALL */
|
|
176 } langp_T;
|
|
177
|
|
178 #define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
|
|
179
|
307
|
180 #define REGION_ALL 0xff /* word valid in all regions */
|
|
181
|
|
182 /* Result values. Lower number is accepted over higher one. */
|
|
183 #define SP_BANNED -1
|
236
|
184 #define SP_OK 0
|
307
|
185 #define SP_RARE 1
|
|
186 #define SP_LOCAL 2
|
|
187 #define SP_BAD 3
|
236
|
188
|
300
|
189 #define VIMSPELLMAGIC "VIMspell05" /* string at start of Vim spell file */
|
236
|
190 #define VIMSPELLMAGICL 10
|
|
191
|
|
192 /*
|
|
193 * Structure to store info for word matching.
|
|
194 */
|
|
195 typedef struct matchinf_S
|
|
196 {
|
|
197 langp_T *mi_lp; /* info for language and region */
|
|
198 slang_T *mi_slang; /* info for the language */
|
243
|
199
|
|
200 /* pointers to original text to be checked */
|
236
|
201 char_u *mi_word; /* start of word being checked */
|
300
|
202 char_u *mi_end; /* end of matching word */
|
243
|
203 char_u *mi_fend; /* next char to be added to mi_fword */
|
300
|
204 char_u *mi_cend; /* char after what was used for
|
|
205 mi_capflags */
|
243
|
206
|
|
207 /* case-folded text */
|
|
208 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
|
300
|
209 int mi_fwordlen; /* nr of valid bytes in mi_fword */
|
243
|
210
|
|
211 /* others */
|
236
|
212 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
|
300
|
213 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
|
236
|
214 } matchinf_T;
|
|
215
|
307
|
216 /*
|
|
217 * The tables used for recognizing word characters according to spelling.
|
|
218 * These are only used for the first 256 characters of 'encoding'.
|
|
219 */
|
|
220 typedef struct spelltab_S
|
|
221 {
|
|
222 char_u st_isw[256]; /* flags: is word char */
|
|
223 char_u st_isu[256]; /* flags: is uppercase char */
|
|
224 char_u st_fold[256]; /* chars: folded case */
|
|
225 } spelltab_T;
|
|
226
|
|
227 static spelltab_T spelltab;
|
|
228 static int did_set_spelltab;
|
|
229
|
|
230 #define SPELL_ISWORD 1
|
|
231 #define SPELL_ISUPPER 2
|
|
232
|
|
233 static void clear_spell_chartab __ARGS((spelltab_T *sp));
|
|
234 static int set_spell_finish __ARGS((spelltab_T *new_st));
|
|
235
|
|
236 /*
|
|
237 * Return TRUE if "p" points to a word character or "c" is a word character
|
|
238 * for spelling.
|
|
239 * Checking for a word character is done very often, avoid the function call
|
|
240 * overhead.
|
|
241 */
|
|
242 #ifdef FEAT_MBYTE
|
|
243 # define SPELL_ISWORDP(p) ((has_mbyte && MB_BYTE2LEN(*(p)) > 1) \
|
|
244 ? (mb_get_class(p) >= 2) : spelltab.st_isw[*(p)])
|
|
245 #else
|
|
246 # define SPELL_ISWORDP(p) (spelltab.st_isw[*(p)])
|
|
247 #endif
|
|
248
|
236
|
249 static slang_T *slang_alloc __ARGS((char_u *lang));
|
|
250 static void slang_free __ARGS((slang_T *lp));
|
300
|
251 static void find_word __ARGS((matchinf_T *mip, int keepcap));
|
307
|
252 static void spell_load_lang __ARGS((char_u *lang));
|
236
|
253 static void spell_load_file __ARGS((char_u *fname, void *cookie));
|
300
|
254 static int read_tree __ARGS((FILE *fd, char_u *byts, int *idxs, int maxidx, int startidx));
|
236
|
255 static int find_region __ARGS((char_u *rp, char_u *region));
|
|
256 static int captype __ARGS((char_u *word, char_u *end));
|
307
|
257 static int set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
|
|
258 #ifdef FEAT_MBYTE
|
|
259 static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
|
|
260 static void write_spell_chartab __ARGS((FILE *fd));
|
|
261 #endif
|
|
262 static int spell_isupper __ARGS((int c));
|
|
263 static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
|
|
264
|
|
265 static char *e_format = N_("E759: Format error in spell file");
|
236
|
266
|
|
267 /*
|
|
268 * Main spell-checking function.
|
300
|
269 * "ptr" points to a character that could be the start of a word.
|
236
|
270 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
|
|
271 * or when it's OK it remains unchanged.
|
|
272 * This must only be called when 'spelllang' is not empty.
|
|
273 * Returns the length of the word in bytes, also when it's OK, so that the
|
|
274 * caller can skip over the word.
|
|
275 */
|
|
276 int
|
300
|
277 spell_check(wp, ptr, attrp)
|
236
|
278 win_T *wp; /* current window */
|
|
279 char_u *ptr;
|
|
280 int *attrp;
|
|
281 {
|
|
282 matchinf_T mi; /* Most things are put in "mi" so that it can
|
|
283 be passed to functions quickly. */
|
|
284
|
307
|
285 /* A word never starts at a space or a control character. Return quickly
|
|
286 * then, skipping over the character. */
|
|
287 if (*ptr <= ' ')
|
|
288 return 1;
|
236
|
289
|
300
|
290 /* A word starting with a number is always OK. Also skip hexadecimal
|
|
291 * numbers 0xFF99 and 0X99FF. */
|
|
292 if (*ptr >= '0' && *ptr <= '9')
|
|
293 {
|
|
294 if (*ptr == '0' && (ptr[1] == 'x' || ptr[2] == 'X'))
|
|
295 mi.mi_end = skiphex(ptr);
|
|
296 else
|
|
297 mi.mi_end = skipdigits(ptr);
|
|
298 }
|
|
299 else
|
236
|
300 {
|
307
|
301 /* Find the end of the word. */
|
|
302 mi.mi_word = ptr;
|
300
|
303 mi.mi_fend = ptr;
|
307
|
304 if (SPELL_ISWORDP(mi.mi_fend))
|
300
|
305 {
|
|
306 /* Make case-folded copy of the characters until the next non-word
|
|
307 * character. */
|
|
308 do
|
|
309 {
|
|
310 mb_ptr_adv(mi.mi_fend);
|
307
|
311 } while (*mi.mi_fend != NUL && SPELL_ISWORDP(mi.mi_fend));
|
236
|
312
|
300
|
313 /* Check the caps type of the word. */
|
|
314 mi.mi_capflags = captype(ptr, mi.mi_fend);
|
|
315 }
|
|
316 else
|
307
|
317 /* No word characters, caps type is always zero. */
|
300
|
318 mi.mi_capflags = 0;
|
307
|
319
|
|
320 /* We always use the characters up to the next non-word character,
|
|
321 * also for bad words. */
|
|
322 mi.mi_end = mi.mi_fend;
|
300
|
323 mi.mi_cend = mi.mi_fend;
|
|
324
|
307
|
325 /* Include one non-word character so that we can check for the
|
|
326 * word end. */
|
|
327 if (*mi.mi_fend != NUL)
|
|
328 mb_ptr_adv(mi.mi_fend);
|
|
329
|
|
330 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
|
|
331 MAXWLEN + 1);
|
|
332 mi.mi_fwordlen = STRLEN(mi.mi_fword);
|
|
333
|
300
|
334 /* The word is bad unless we recognize it. */
|
|
335 mi.mi_result = SP_BAD;
|
236
|
336
|
300
|
337 /*
|
|
338 * Loop over the languages specified in 'spelllang'.
|
|
339 * We check them all, because a matching word may be longer than an
|
|
340 * already found matching word.
|
|
341 */
|
|
342 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
|
|
343 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
|
243
|
344 {
|
300
|
345 /* Check for a matching word in case-folded words. */
|
|
346 find_word(&mi, FALSE);
|
|
347
|
|
348 /* Try keep-case words. */
|
|
349 find_word(&mi, TRUE);
|
|
350 }
|
243
|
351
|
300
|
352 if (mi.mi_result != SP_OK)
|
|
353 {
|
|
354 /* When we are at a non-word character there is no error, just
|
|
355 * skip over the character (try looking for a word after it). */
|
307
|
356 if (!SPELL_ISWORDP(ptr))
|
243
|
357 {
|
300
|
358 #ifdef FEAT_MBYTE
|
|
359 if (has_mbyte)
|
|
360 return mb_ptr2len_check(ptr);
|
|
361 #endif
|
|
362 return 1;
|
243
|
363 }
|
|
364
|
307
|
365 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
|
300
|
366 *attrp = highlight_attr[HLF_SPB];
|
|
367 else if (mi.mi_result == SP_RARE)
|
|
368 *attrp = highlight_attr[HLF_SPR];
|
|
369 else
|
|
370 *attrp = highlight_attr[HLF_SPL];
|
243
|
371 }
|
|
372 }
|
|
373
|
300
|
374 return (int)(mi.mi_end - ptr);
|
236
|
375 }
|
|
376
|
|
377 /*
|
300
|
378 * Check if the word at "mip->mi_word" is in the tree.
|
|
379 * When "keepcap" is TRUE check in keep-case word tree.
|
|
380 *
|
|
381 * For a match mip->mi_result is updated.
|
243
|
382 */
|
|
383 static void
|
300
|
384 find_word(mip, keepcap)
|
243
|
385 matchinf_T *mip;
|
300
|
386 int keepcap;
|
243
|
387 {
|
300
|
388 int arridx = 0;
|
|
389 int endlen[MAXWLEN]; /* length at possible word endings */
|
|
390 int endidx[MAXWLEN]; /* possible word endings */
|
|
391 int endidxcnt = 0;
|
|
392 int len;
|
|
393 int wlen = 0;
|
|
394 int flen;
|
|
395 int c;
|
|
396 char_u *ptr;
|
|
397 unsigned lo, hi, m;
|
243
|
398 #ifdef FEAT_MBYTE
|
300
|
399 char_u *s;
|
307
|
400 #endif
|
300
|
401 char_u *p;
|
307
|
402 int res = SP_BAD;
|
|
403 int valid;
|
300
|
404 slang_T *slang = mip->mi_lp->lp_slang;
|
|
405 unsigned flags;
|
|
406 char_u *byts;
|
|
407 int *idxs;
|
243
|
408
|
300
|
409 if (keepcap)
|
236
|
410 {
|
300
|
411 /* Check for word with matching case in keep-case tree. */
|
|
412 ptr = mip->mi_word;
|
|
413 flen = 9999; /* no case folding, always enough bytes */
|
|
414 byts = slang->sl_kbyts;
|
|
415 idxs = slang->sl_kidxs;
|
236
|
416 }
|
|
417 else
|
|
418 {
|
300
|
419 /* Check for case-folded in case-folded tree. */
|
|
420 ptr = mip->mi_fword;
|
|
421 flen = mip->mi_fwordlen; /* available case-folded bytes */
|
|
422 byts = slang->sl_fbyts;
|
|
423 idxs = slang->sl_fidxs;
|
243
|
424 }
|
|
425
|
300
|
426 if (byts == NULL)
|
|
427 return; /* array is empty */
|
236
|
428
|
|
429 /*
|
307
|
430 * Repeat advancing in the tree until:
|
|
431 * - there is a byte that doesn't match,
|
|
432 * - we reach the end of the tree,
|
|
433 * - or we reach the end of the line.
|
236
|
434 */
|
300
|
435 for (;;)
|
236
|
436 {
|
300
|
437 if (flen == 0 && *mip->mi_fend != NUL)
|
236
|
438 {
|
300
|
439 /* Need to fold at least one more character. Do until next
|
|
440 * non-word character for efficiency. */
|
307
|
441 p = mip->mi_fend;
|
300
|
442 do
|
236
|
443 {
|
307
|
444 mb_ptr_adv(mip->mi_fend);
|
|
445 } while (*mip->mi_fend != NUL && SPELL_ISWORDP(mip->mi_fend));
|
|
446
|
|
447 /* Include the non-word character so that we can check for the
|
|
448 * word end. */
|
|
449 if (*mip->mi_fend != NUL)
|
|
450 mb_ptr_adv(mip->mi_fend);
|
|
451
|
|
452 (void)spell_casefold(p, (int)(mip->mi_fend - p),
|
300
|
453 mip->mi_fword + mip->mi_fwordlen,
|
|
454 MAXWLEN - mip->mi_fwordlen);
|
|
455 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
|
|
456 mip->mi_fwordlen += flen;
|
|
457 }
|
|
458
|
|
459 len = byts[arridx++];
|
|
460
|
|
461 /* If the first possible byte is a zero the word could end here.
|
|
462 * Remember this index, we first check for the longest word. */
|
|
463 if (byts[arridx] == 0)
|
|
464 {
|
307
|
465 if (endidxcnt == MAXWLEN)
|
|
466 {
|
|
467 /* Must be a corrupted spell file. */
|
|
468 EMSG(_(e_format));
|
|
469 return;
|
|
470 }
|
300
|
471 endlen[endidxcnt] = wlen;
|
|
472 endidx[endidxcnt++] = arridx++;
|
|
473 --len;
|
|
474
|
|
475 /* Skip over the zeros, there can be several flag/region
|
|
476 * combinations. */
|
|
477 while (len > 0 && byts[arridx] == 0)
|
|
478 {
|
|
479 ++arridx;
|
|
480 --len;
|
|
481 }
|
|
482 if (len == 0)
|
|
483 break; /* no children, word must end here */
|
|
484 }
|
|
485
|
|
486 /* Stop looking at end of the line. */
|
|
487 if (ptr[wlen] == NUL)
|
|
488 break;
|
|
489
|
|
490 /* Perform a binary search in the list of accepted bytes. */
|
|
491 c = ptr[wlen];
|
|
492 lo = arridx;
|
|
493 hi = arridx + len - 1;
|
|
494 while (lo < hi)
|
|
495 {
|
|
496 m = (lo + hi) / 2;
|
|
497 if (byts[m] > c)
|
|
498 hi = m - 1;
|
|
499 else if (byts[m] < c)
|
|
500 lo = m + 1;
|
|
501 else
|
|
502 {
|
|
503 lo = hi = m;
|
|
504 break;
|
236
|
505 }
|
|
506 }
|
300
|
507
|
|
508 /* Stop if there is no matching byte. */
|
|
509 if (hi < lo || byts[lo] != c)
|
|
510 break;
|
|
511
|
|
512 /* Continue at the child (if there is one). */
|
|
513 arridx = idxs[lo];
|
|
514 ++wlen;
|
|
515 --flen;
|
236
|
516 }
|
|
517
|
300
|
518 /*
|
|
519 * Verify that one of the possible endings is valid. Try the longest
|
|
520 * first.
|
|
521 */
|
|
522 while (endidxcnt > 0)
|
|
523 {
|
|
524 --endidxcnt;
|
|
525 arridx = endidx[endidxcnt];
|
|
526 wlen = endlen[endidxcnt];
|
236
|
527
|
300
|
528 #ifdef FEAT_MBYTE
|
|
529 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
|
|
530 continue; /* not at first byte of character */
|
|
531 #endif
|
307
|
532 if (SPELL_ISWORDP(ptr + wlen))
|
300
|
533 continue; /* next char is a word character */
|
|
534
|
|
535 #ifdef FEAT_MBYTE
|
|
536 if (!keepcap && has_mbyte)
|
|
537 {
|
|
538 /* Compute byte length in original word, length may change
|
|
539 * when folding case. */
|
|
540 p = mip->mi_word;
|
|
541 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
|
|
542 mb_ptr_adv(p);
|
|
543 wlen = p - mip->mi_word;
|
|
544 }
|
|
545 #endif
|
236
|
546
|
300
|
547 /* Check flags and region. Repeat this if there are more
|
|
548 * flags/region alternatives until there is a match. */
|
|
549 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0; --len)
|
|
550 {
|
|
551 flags = idxs[arridx];
|
|
552 if (keepcap)
|
|
553 {
|
|
554 /* For "keepcap" tree the case is always right. */
|
|
555 valid = TRUE;
|
|
556 }
|
|
557 else
|
|
558 {
|
|
559 /* Check that the word is in the required case. */
|
|
560 if (mip->mi_cend != mip->mi_word + wlen)
|
|
561 {
|
|
562 /* mi_capflags was set for a different word
|
|
563 * length, need to do it again. */
|
|
564 mip->mi_cend = mip->mi_word + wlen;
|
|
565 mip->mi_capflags = captype(mip->mi_word,
|
|
566 mip->mi_cend);
|
|
567 }
|
|
568
|
|
569 valid = (mip->mi_capflags == WF_ALLCAP
|
|
570 || ((flags & WF_ALLCAP) == 0
|
|
571 && ((flags & WF_ONECAP) == 0
|
|
572 || mip->mi_capflags == WF_ONECAP)));
|
|
573 }
|
236
|
574
|
307
|
575 if (valid)
|
300
|
576 {
|
307
|
577 if (flags & WF_BANNED)
|
|
578 res = SP_BANNED;
|
|
579 else if (flags & WF_REGION)
|
300
|
580 {
|
|
581 /* Check region. */
|
|
582 if ((mip->mi_lp->lp_region & (flags >> 8)) != 0)
|
|
583 res = SP_OK;
|
|
584 else
|
|
585 res = SP_LOCAL;
|
|
586 }
|
|
587 else if (flags & WF_RARE)
|
|
588 res = SP_RARE;
|
|
589 else
|
|
590 res = SP_OK;
|
307
|
591
|
|
592 /* Always use the longest match and the best result. */
|
|
593 if (mip->mi_result > res)
|
|
594 {
|
|
595 mip->mi_result = res;
|
|
596 mip->mi_end = mip->mi_word + wlen;
|
|
597 }
|
|
598 else if (mip->mi_result == res
|
|
599 && mip->mi_end < mip->mi_word + wlen)
|
|
600 mip->mi_end = mip->mi_word + wlen;
|
|
601
|
|
602 if (res == SP_OK)
|
|
603 break;
|
300
|
604 }
|
307
|
605 else
|
|
606 res = SP_BAD;
|
|
607
|
300
|
608 ++arridx;
|
|
609 }
|
|
610
|
307
|
611 if (res == SP_OK)
|
300
|
612 break;
|
|
613 }
|
236
|
614 }
|
|
615
|
300
|
616
|
236
|
617 /*
|
|
618 * Move to next spell error.
|
|
619 * Return OK if found, FAIL otherwise.
|
|
620 */
|
|
621 int
|
|
622 spell_move_to(dir, allwords)
|
|
623 int dir; /* FORWARD or BACKWARD */
|
|
624 int allwords; /* TRUE for "[s" and "]s" */
|
|
625 {
|
249
|
626 linenr_T lnum;
|
|
627 pos_T found_pos;
|
236
|
628 char_u *line;
|
|
629 char_u *p;
|
|
630 int attr = 0;
|
|
631 int len;
|
249
|
632 int has_syntax = syntax_present(curbuf);
|
|
633 int col;
|
|
634 int can_spell;
|
236
|
635
|
|
636 if (!curwin->w_p_spell || *curwin->w_buffer->b_p_spl == NUL)
|
|
637 {
|
|
638 EMSG(_("E756: Spell checking not enabled"));
|
|
639 return FAIL;
|
|
640 }
|
|
641
|
249
|
642 /*
|
|
643 * Start looking for bad word at the start of the line, because we can't
|
|
644 * start halfway a word, we don't know where it starts or ends.
|
|
645 *
|
|
646 * When searching backwards, we continue in the line to find the last
|
|
647 * bad word (in the cursor line: before the cursor).
|
|
648 */
|
|
649 lnum = curwin->w_cursor.lnum;
|
|
650 found_pos.lnum = 0;
|
236
|
651
|
|
652 while (!got_int)
|
|
653 {
|
249
|
654 line = ml_get(lnum);
|
|
655 p = line;
|
|
656
|
236
|
657 while (*p != NUL)
|
|
658 {
|
300
|
659 /* When searching backward don't search after the cursor. */
|
|
660 if (dir == BACKWARD
|
|
661 && lnum == curwin->w_cursor.lnum
|
|
662 && (colnr_T)(p - line) >= curwin->w_cursor.col)
|
|
663 break;
|
249
|
664
|
300
|
665 /* start of word */
|
|
666 len = spell_check(curwin, p, &attr);
|
249
|
667
|
300
|
668 if (attr != 0)
|
|
669 {
|
|
670 /* We found a bad word. Check the attribute. */
|
|
671 /* TODO: check for syntax @Spell cluster. */
|
|
672 if (allwords || attr == highlight_attr[HLF_SPB])
|
236
|
673 {
|
300
|
674 /* When searching forward only accept a bad word after
|
|
675 * the cursor. */
|
|
676 if (dir == BACKWARD
|
|
677 || lnum > curwin->w_cursor.lnum
|
|
678 || (lnum == curwin->w_cursor.lnum
|
|
679 && (colnr_T)(p - line)
|
|
680 > curwin->w_cursor.col))
|
236
|
681 {
|
300
|
682 if (has_syntax)
|
249
|
683 {
|
300
|
684 col = p - line;
|
|
685 (void)syn_get_id(lnum, (colnr_T)col,
|
|
686 FALSE, &can_spell);
|
249
|
687
|
300
|
688 /* have to get the line again, a multi-line
|
|
689 * regexp may make it invalid */
|
|
690 line = ml_get(lnum);
|
|
691 p = line + col;
|
|
692 }
|
|
693 else
|
|
694 can_spell = TRUE;
|
249
|
695
|
300
|
696 if (can_spell)
|
|
697 {
|
|
698 found_pos.lnum = lnum;
|
|
699 found_pos.col = p - line;
|
249
|
700 #ifdef FEAT_VIRTUALEDIT
|
300
|
701 found_pos.coladd = 0;
|
249
|
702 #endif
|
300
|
703 if (dir == FORWARD)
|
|
704 {
|
|
705 /* No need to search further. */
|
|
706 curwin->w_cursor = found_pos;
|
|
707 return OK;
|
249
|
708 }
|
|
709 }
|
236
|
710 }
|
|
711 }
|
300
|
712 attr = 0;
|
236
|
713 }
|
|
714
|
300
|
715 /* advance to character after the word */
|
|
716 p += len;
|
|
717 if (*p == NUL)
|
|
718 break;
|
236
|
719 }
|
|
720
|
|
721 /* Advance to next line. */
|
249
|
722 if (dir == BACKWARD)
|
|
723 {
|
|
724 if (found_pos.lnum != 0)
|
|
725 {
|
|
726 /* Use the last match in the line. */
|
|
727 curwin->w_cursor = found_pos;
|
|
728 return OK;
|
|
729 }
|
|
730 if (lnum == 1)
|
|
731 return FAIL;
|
|
732 --lnum;
|
|
733 }
|
|
734 else
|
|
735 {
|
|
736 if (lnum == curbuf->b_ml.ml_line_count)
|
|
737 return FAIL;
|
|
738 ++lnum;
|
|
739 }
|
236
|
740
|
|
741 line_breakcheck();
|
|
742 }
|
|
743
|
|
744 return FAIL; /* interrupted */
|
|
745 }
|
|
746
|
|
747 /*
|
307
|
748 * Load word list(s) for "lang" from Vim spell file(s).
|
|
749 * "lang" must be the language without the region: "en".
|
236
|
750 */
|
307
|
751 static void
|
236
|
752 spell_load_lang(lang)
|
|
753 char_u *lang;
|
|
754 {
|
|
755 char_u fname_enc[80];
|
|
756 char_u *p;
|
|
757 int r;
|
307
|
758 char_u langcp[MAXWLEN + 1];
|
|
759
|
|
760 /* Copy the language name to pass it to spell_load_file() as a cookie.
|
|
761 * It's truncated when an error is detected. */
|
|
762 STRCPY(langcp, lang);
|
|
763
|
|
764 /* Find all spell files for "lang" in 'runtimepath' and load them.
|
|
765 * Use 'encoding', except that we use "latin1" for "latin9". */
|
236
|
766 #ifdef FEAT_MBYTE
|
307
|
767 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
|
|
768 p = p_enc;
|
|
769 else
|
236
|
770 #endif
|
307
|
771 p = (char_u *)"latin1";
|
|
772 vim_snprintf((char *)fname_enc, sizeof(fname_enc),
|
272
|
773 "spell/%s.%s.spl", lang, p);
|
307
|
774 r = do_in_runtimepath(fname_enc, TRUE, spell_load_file, &langcp);
|
|
775
|
|
776 if (r == FAIL && *langcp != NUL)
|
|
777 {
|
|
778 /* Try loading the ASCII version. */
|
|
779 vim_snprintf((char *)fname_enc, sizeof(fname_enc),
|
272
|
780 "spell/%s.ascii.spl", lang);
|
307
|
781 r = do_in_runtimepath(fname_enc, TRUE, spell_load_file, &langcp);
|
|
782 }
|
|
783
|
|
784 if (r == FAIL)
|
|
785 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
|
236
|
786 fname_enc + 6);
|
|
787 }
|
|
788
|
|
789 /*
|
|
790 * Allocate a new slang_T.
|
|
791 * Caller must fill "sl_next".
|
|
792 */
|
|
793 static slang_T *
|
|
794 slang_alloc(lang)
|
|
795 char_u *lang;
|
|
796 {
|
|
797 slang_T *lp;
|
|
798
|
300
|
799 lp = (slang_T *)alloc_clear(sizeof(slang_T));
|
236
|
800 if (lp != NULL)
|
|
801 {
|
|
802 lp->sl_name = vim_strsave(lang);
|
|
803 ga_init2(&lp->sl_rep, sizeof(repentry_T), 4);
|
|
804 }
|
|
805 return lp;
|
|
806 }
|
|
807
|
|
808 /*
|
|
809 * Free the contents of an slang_T and the structure itself.
|
|
810 */
|
|
811 static void
|
|
812 slang_free(lp)
|
|
813 slang_T *lp;
|
|
814 {
|
|
815 vim_free(lp->sl_name);
|
300
|
816 vim_free(lp->sl_fbyts);
|
|
817 vim_free(lp->sl_kbyts);
|
|
818 vim_free(lp->sl_fidxs);
|
|
819 vim_free(lp->sl_kidxs);
|
236
|
820 ga_clear(&lp->sl_rep);
|
|
821 vim_free(lp->sl_try);
|
|
822 vim_free(lp);
|
|
823 }
|
|
824
|
|
825 /*
|
307
|
826 * Load one spell file and store the info into a slang_T.
|
236
|
827 * Invoked through do_in_runtimepath().
|
|
828 */
|
|
829 static void
|
|
830 spell_load_file(fname, cookie)
|
|
831 char_u *fname;
|
307
|
832 void *cookie; /* points to the language name */
|
236
|
833 {
|
307
|
834 char_u *lang = cookie;
|
236
|
835 FILE *fd;
|
|
836 char_u buf[MAXWLEN + 1];
|
|
837 char_u *p;
|
|
838 int i;
|
300
|
839 int len;
|
236
|
840 int round;
|
|
841 char_u *save_sourcing_name = sourcing_name;
|
|
842 linenr_T save_sourcing_lnum = sourcing_lnum;
|
255
|
843 int cnt, ccnt;
|
|
844 char_u *fol;
|
307
|
845 slang_T *lp = NULL;
|
236
|
846
|
|
847 fd = fopen((char *)fname, "r");
|
|
848 if (fd == NULL)
|
|
849 {
|
|
850 EMSG2(_(e_notopen), fname);
|
255
|
851 goto endFAIL;
|
236
|
852 }
|
|
853
|
307
|
854 lp = slang_alloc(lang);
|
|
855 if (lp == NULL)
|
|
856 goto endFAIL;
|
|
857
|
236
|
858 /* Set sourcing_name, so that error messages mention the file name. */
|
|
859 sourcing_name = fname;
|
|
860 sourcing_lnum = 0;
|
|
861
|
255
|
862 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
|
|
863 * <charflagslen> <charflags> <fcharslen> <fchars> */
|
236
|
864 for (i = 0; i < VIMSPELLMAGICL; ++i)
|
|
865 buf[i] = getc(fd); /* <fileID> */
|
|
866 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
|
|
867 {
|
|
868 EMSG(_("E757: Wrong file ID in spell file"));
|
255
|
869 goto endFAIL;
|
236
|
870 }
|
|
871
|
|
872 cnt = getc(fd); /* <regioncnt> */
|
255
|
873 if (cnt < 0)
|
236
|
874 {
|
|
875 truncerr:
|
|
876 EMSG(_("E758: Truncated spell file"));
|
255
|
877 goto endFAIL;
|
236
|
878 }
|
|
879 if (cnt > 8)
|
|
880 {
|
|
881 formerr:
|
307
|
882 EMSG(_(e_format));
|
255
|
883 goto endFAIL;
|
236
|
884 }
|
|
885 for (i = 0; i < cnt; ++i)
|
|
886 {
|
|
887 lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
|
|
888 lp->sl_regions[i * 2 + 1] = getc(fd);
|
|
889 }
|
|
890 lp->sl_regions[cnt * 2] = NUL;
|
|
891
|
255
|
892 cnt = getc(fd); /* <charflagslen> */
|
|
893 if (cnt > 0)
|
|
894 {
|
300
|
895 p = alloc((unsigned)cnt);
|
255
|
896 if (p == NULL)
|
|
897 goto endFAIL;
|
|
898 for (i = 0; i < cnt; ++i)
|
|
899 p[i] = getc(fd); /* <charflags> */
|
|
900
|
|
901 ccnt = (getc(fd) << 8) + getc(fd); /* <fcharslen> */
|
|
902 if (ccnt <= 0)
|
300
|
903 {
|
|
904 vim_free(p);
|
255
|
905 goto formerr;
|
300
|
906 }
|
|
907 fol = alloc((unsigned)ccnt + 1);
|
255
|
908 if (fol == NULL)
|
300
|
909 {
|
|
910 vim_free(p);
|
255
|
911 goto endFAIL;
|
300
|
912 }
|
255
|
913 for (i = 0; i < ccnt; ++i)
|
|
914 fol[i] = getc(fd); /* <fchars> */
|
|
915 fol[i] = NUL;
|
|
916
|
|
917 /* Set the word-char flags and fill spell_isupper() table. */
|
300
|
918 i = set_spell_charflags(p, cnt, fol);
|
|
919 vim_free(p);
|
|
920 vim_free(fol);
|
|
921 if (i == FAIL)
|
255
|
922 goto formerr;
|
|
923 }
|
|
924 else
|
|
925 {
|
|
926 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
|
|
927 cnt = (getc(fd) << 8) + getc(fd);
|
|
928 if (cnt != 0)
|
|
929 goto formerr;
|
|
930 }
|
|
931
|
236
|
932 /* <SUGGEST> : <suggestlen> <more> ... */
|
|
933 /* TODO, just skip this for now */
|
|
934 i = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
|
|
935 while (i-- > 0)
|
|
936 if (getc(fd) == EOF) /* <suggestlen> */
|
|
937 goto truncerr;
|
|
938
|
300
|
939 /* round 1: <LWORDTREE>
|
|
940 * round 2: <KWORDTREE> */
|
|
941 for (round = 1; round <= 2; ++round)
|
236
|
942 {
|
300
|
943 /* The tree size was computed when writing the file, so that we can
|
|
944 * allocate it as one long block. <nodecount> */
|
|
945 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
|
|
946 if (len < 0)
|
|
947 goto truncerr;
|
|
948 if (len > 0)
|
236
|
949 {
|
300
|
950 /* Allocate the byte array. */
|
|
951 p = lalloc((long_u)len, TRUE);
|
|
952 if (p == NULL)
|
|
953 goto endFAIL;
|
|
954 if (round == 1)
|
|
955 lp->sl_fbyts = p;
|
|
956 else
|
|
957 lp->sl_kbyts = p;
|
236
|
958
|
300
|
959 /* Allocate the index array. */
|
|
960 p = lalloc_clear((long_u)(len * sizeof(int)), TRUE);
|
|
961 if (p == NULL)
|
|
962 goto endFAIL;
|
|
963 if (round == 1)
|
|
964 lp->sl_fidxs = (int *)p;
|
|
965 else
|
|
966 lp->sl_kidxs = (int *)p;
|
|
967
|
|
968
|
|
969 /* Read the tree and store it in the array. */
|
|
970 i = read_tree(fd,
|
|
971 round == 1 ? lp->sl_fbyts : lp->sl_kbyts,
|
|
972 round == 1 ? lp->sl_fidxs : lp->sl_kidxs,
|
|
973 len, 0);
|
|
974 if (i == -1)
|
|
975 goto truncerr;
|
|
976 if (i < 0)
|
236
|
977 goto formerr;
|
|
978 }
|
300
|
979 }
|
243
|
980
|
307
|
981 lp->sl_next = first_lang;
|
|
982 first_lang = lp;
|
|
983
|
255
|
984 goto endOK;
|
|
985
|
|
986 endFAIL:
|
307
|
987 /* truncating the name signals the error to spell_load_lang() */
|
|
988 *lang = NUL;
|
|
989 if (lp != NULL)
|
|
990 slang_free(lp);
|
255
|
991
|
|
992 endOK:
|
236
|
993 if (fd != NULL)
|
|
994 fclose(fd);
|
|
995 sourcing_name = save_sourcing_name;
|
|
996 sourcing_lnum = save_sourcing_lnum;
|
|
997 }
|
|
998
|
|
999 /*
|
300
|
1000 * Read one row of siblings from the spell file and store it in the byte array
|
|
1001 * "byts" and index array "idxs". Recursively read the children.
|
|
1002 *
|
|
1003 * NOTE: The code here must match put_tree().
|
|
1004 *
|
|
1005 * Returns the index follosing the siblings.
|
|
1006 * Returns -1 if the file is shorter than expected.
|
|
1007 * Returns -2 if there is a format error.
|
236
|
1008 */
|
300
|
1009 static int
|
|
1010 read_tree(fd, byts, idxs, maxidx, startidx)
|
|
1011 FILE *fd;
|
|
1012 char_u *byts;
|
|
1013 int *idxs;
|
|
1014 int maxidx; /* size of arrays */
|
|
1015 int startidx; /* current index in "byts" and "idxs" */
|
236
|
1016 {
|
300
|
1017 int len;
|
|
1018 int i;
|
|
1019 int n;
|
|
1020 int idx = startidx;
|
|
1021 int c;
|
|
1022 #define SHARED_MASK 0x8000000
|
236
|
1023
|
300
|
1024 len = getc(fd); /* <siblingcount> */
|
|
1025 if (len <= 0)
|
|
1026 return -1;
|
|
1027
|
|
1028 if (startidx + len >= maxidx)
|
|
1029 return -2;
|
|
1030 byts[idx++] = len;
|
|
1031
|
|
1032 /* Read the byte values, flag/region bytes and shared indexes. */
|
|
1033 for (i = 1; i <= len; ++i)
|
236
|
1034 {
|
300
|
1035 c = getc(fd); /* <byte> */
|
|
1036 if (c < 0)
|
|
1037 return -1;
|
|
1038 if (c <= BY_SPECIAL)
|
|
1039 {
|
|
1040 if (c == BY_NOFLAGS)
|
|
1041 {
|
|
1042 /* No flags, all regions. */
|
|
1043 idxs[idx] = 0;
|
|
1044 c = 0;
|
|
1045 }
|
|
1046 else if (c == BY_FLAGS)
|
|
1047 {
|
|
1048 /* Read flags and option region. */
|
|
1049 c = getc(fd); /* <flags> */
|
|
1050 if (c & WF_REGION)
|
|
1051 c = (getc(fd) << 8) + c; /* <region> */
|
|
1052 idxs[idx] = c;
|
|
1053 c = 0;
|
|
1054 }
|
|
1055 else /* c == BY_INDEX */
|
|
1056 {
|
|
1057 /* <nodeidx> */
|
|
1058 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
|
|
1059 if (n < 0 || n >= maxidx)
|
|
1060 return -2;
|
|
1061 idxs[idx] = n + SHARED_MASK;
|
|
1062 c = getc(fd); /* <xbyte> */
|
|
1063 }
|
|
1064 }
|
|
1065 byts[idx++] = c;
|
236
|
1066 }
|
|
1067
|
300
|
1068 /* Recursively read the children for non-shared siblings.
|
|
1069 * Skip the end-of-word ones (zero byte value) and the shared ones (and
|
|
1070 * remove SHARED_MASK) */
|
|
1071 for (i = 1; i <= len; ++i)
|
|
1072 if (byts[startidx + i] != 0)
|
|
1073 {
|
|
1074 if (idxs[startidx + i] & SHARED_MASK)
|
|
1075 idxs[startidx + i] &= ~SHARED_MASK;
|
|
1076 else
|
|
1077 {
|
|
1078 idxs[startidx + i] = idx;
|
|
1079 idx = read_tree(fd, byts, idxs, maxidx, idx);
|
|
1080 if (idx < 0)
|
|
1081 break;
|
|
1082 }
|
|
1083 }
|
236
|
1084
|
300
|
1085 return idx;
|
236
|
1086 }
|
|
1087
|
|
1088 /*
|
|
1089 * Parse 'spelllang' and set buf->b_langp accordingly.
|
|
1090 * Returns an error message or NULL.
|
|
1091 */
|
|
1092 char_u *
|
|
1093 did_set_spelllang(buf)
|
|
1094 buf_T *buf;
|
|
1095 {
|
|
1096 garray_T ga;
|
|
1097 char_u *lang;
|
|
1098 char_u *e;
|
|
1099 char_u *region;
|
|
1100 int region_mask;
|
|
1101 slang_T *lp;
|
|
1102 int c;
|
|
1103 char_u lbuf[MAXWLEN + 1];
|
|
1104
|
|
1105 ga_init2(&ga, sizeof(langp_T), 2);
|
|
1106
|
|
1107 /* loop over comma separated languages. */
|
|
1108 for (lang = buf->b_p_spl; *lang != NUL; lang = e)
|
|
1109 {
|
|
1110 e = vim_strchr(lang, ',');
|
|
1111 if (e == NULL)
|
|
1112 e = lang + STRLEN(lang);
|
240
|
1113 region = NULL;
|
236
|
1114 if (e > lang + 2)
|
|
1115 {
|
|
1116 if (e - lang >= MAXWLEN)
|
|
1117 {
|
|
1118 ga_clear(&ga);
|
|
1119 return e_invarg;
|
|
1120 }
|
|
1121 if (lang[2] == '_')
|
|
1122 region = lang + 3;
|
|
1123 }
|
|
1124
|
307
|
1125 /* Check if we loaded this language before. */
|
236
|
1126 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
|
|
1127 if (STRNICMP(lp->sl_name, lang, 2) == 0)
|
|
1128 break;
|
|
1129
|
|
1130 if (lp == NULL)
|
|
1131 {
|
|
1132 /* Not found, load the language. */
|
|
1133 STRNCPY(lbuf, lang, e - lang);
|
|
1134 lbuf[e - lang] = NUL;
|
|
1135 if (region != NULL)
|
|
1136 mch_memmove(lbuf + 2, lbuf + 5, e - lang - 4);
|
307
|
1137 spell_load_lang(lbuf);
|
236
|
1138 }
|
|
1139
|
307
|
1140 /*
|
|
1141 * Loop over the languages, there can be several files for each.
|
|
1142 */
|
|
1143 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
|
|
1144 if (STRNICMP(lp->sl_name, lang, 2) == 0)
|
236
|
1145 {
|
307
|
1146 if (region == NULL)
|
|
1147 region_mask = REGION_ALL;
|
|
1148 else
|
236
|
1149 {
|
307
|
1150 /* find region in sl_regions */
|
|
1151 c = find_region(lp->sl_regions, region);
|
|
1152 if (c == REGION_ALL)
|
|
1153 {
|
|
1154 c = *e;
|
|
1155 *e = NUL;
|
|
1156 smsg((char_u *)_("Warning: region %s not supported"),
|
|
1157 lang);
|
|
1158 *e = c;
|
|
1159 region_mask = REGION_ALL;
|
|
1160 }
|
|
1161 else
|
|
1162 region_mask = 1 << c;
|
236
|
1163 }
|
307
|
1164
|
|
1165 if (ga_grow(&ga, 1) == FAIL)
|
|
1166 {
|
|
1167 ga_clear(&ga);
|
|
1168 return e_outofmem;
|
|
1169 }
|
|
1170 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
|
|
1171 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
|
|
1172 ++ga.ga_len;
|
236
|
1173 }
|
|
1174
|
|
1175 if (*e == ',')
|
|
1176 ++e;
|
|
1177 }
|
|
1178
|
|
1179 /* Add a NULL entry to mark the end of the list. */
|
|
1180 if (ga_grow(&ga, 1) == FAIL)
|
|
1181 {
|
|
1182 ga_clear(&ga);
|
|
1183 return e_outofmem;
|
|
1184 }
|
|
1185 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
|
|
1186 ++ga.ga_len;
|
|
1187
|
|
1188 /* Everything is fine, store the new b_langp value. */
|
|
1189 ga_clear(&buf->b_langp);
|
|
1190 buf->b_langp = ga;
|
|
1191
|
|
1192 return NULL;
|
|
1193 }
|
|
1194
|
|
1195 /*
|
|
1196 * Find the region "region[2]" in "rp" (points to "sl_regions").
|
|
1197 * Each region is simply stored as the two characters of it's name.
|
|
1198 * Returns the index if found, REGION_ALL if not found.
|
|
1199 */
|
|
1200 static int
|
|
1201 find_region(rp, region)
|
|
1202 char_u *rp;
|
|
1203 char_u *region;
|
|
1204 {
|
|
1205 int i;
|
|
1206
|
|
1207 for (i = 0; ; i += 2)
|
|
1208 {
|
|
1209 if (rp[i] == NUL)
|
|
1210 return REGION_ALL;
|
|
1211 if (rp[i] == region[0] && rp[i + 1] == region[1])
|
|
1212 break;
|
|
1213 }
|
|
1214 return i / 2;
|
|
1215 }
|
|
1216
|
|
1217 /*
|
|
1218 * Return type of word:
|
|
1219 * w word 0
|
300
|
1220 * Word WF_ONECAP
|
|
1221 * W WORD WF_ALLCAP
|
|
1222 * WoRd wOrd WF_KEEPCAP
|
236
|
1223 */
|
|
1224 static int
|
|
1225 captype(word, end)
|
|
1226 char_u *word;
|
|
1227 char_u *end;
|
|
1228 {
|
|
1229 char_u *p;
|
|
1230 int c;
|
|
1231 int firstcap;
|
|
1232 int allcap;
|
|
1233 int past_second = FALSE; /* past second word char */
|
|
1234
|
|
1235 /* find first letter */
|
307
|
1236 for (p = word; !SPELL_ISWORDP(p); mb_ptr_adv(p))
|
236
|
1237 if (p >= end)
|
|
1238 return 0; /* only non-word characters, illegal word */
|
|
1239 #ifdef FEAT_MBYTE
|
|
1240 c = mb_ptr2char_adv(&p);
|
|
1241 #else
|
|
1242 c = *p++;
|
|
1243 #endif
|
255
|
1244 firstcap = allcap = spell_isupper(c);
|
236
|
1245
|
|
1246 /*
|
|
1247 * Need to check all letters to find a word with mixed upper/lower.
|
|
1248 * But a word with an upper char only at start is a ONECAP.
|
|
1249 */
|
|
1250 for ( ; p < end; mb_ptr_adv(p))
|
307
|
1251 if (SPELL_ISWORDP(p))
|
236
|
1252 {
|
|
1253 #ifdef FEAT_MBYTE
|
|
1254 c = mb_ptr2char(p);
|
|
1255 #else
|
|
1256 c = *p;
|
|
1257 #endif
|
255
|
1258 if (!spell_isupper(c))
|
236
|
1259 {
|
|
1260 /* UUl -> KEEPCAP */
|
|
1261 if (past_second && allcap)
|
300
|
1262 return WF_KEEPCAP;
|
236
|
1263 allcap = FALSE;
|
|
1264 }
|
|
1265 else if (!allcap)
|
|
1266 /* UlU -> KEEPCAP */
|
300
|
1267 return WF_KEEPCAP;
|
236
|
1268 past_second = TRUE;
|
|
1269 }
|
|
1270
|
|
1271 if (allcap)
|
300
|
1272 return WF_ALLCAP;
|
236
|
1273 if (firstcap)
|
300
|
1274 return WF_ONECAP;
|
236
|
1275 return 0;
|
|
1276 }
|
|
1277
|
|
1278 # if defined(FEAT_MBYTE) || defined(PROTO)
|
|
1279 /*
|
|
1280 * Clear all spelling tables and reload them.
|
307
|
1281 * Used after 'encoding' is set and when ":mkspell" was used.
|
236
|
1282 */
|
|
1283 void
|
|
1284 spell_reload()
|
|
1285 {
|
|
1286 buf_T *buf;
|
|
1287 slang_T *lp;
|
|
1288
|
307
|
1289 /* Initialize the table for SPELL_ISWORDP(). */
|
236
|
1290 init_spell_chartab();
|
|
1291
|
|
1292 /* Unload all allocated memory. */
|
|
1293 while (first_lang != NULL)
|
|
1294 {
|
|
1295 lp = first_lang;
|
|
1296 first_lang = lp->sl_next;
|
|
1297 slang_free(lp);
|
|
1298 }
|
|
1299
|
|
1300 /* Go through all buffers and handle 'spelllang'. */
|
|
1301 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
|
|
1302 {
|
|
1303 ga_clear(&buf->b_langp);
|
|
1304 if (*buf->b_p_spl != NUL)
|
|
1305 did_set_spelllang(buf);
|
|
1306 }
|
|
1307 }
|
|
1308 # endif
|
|
1309
|
|
1310
|
|
1311 #if defined(FEAT_MBYTE) || defined(PROTO)
|
|
1312 /*
|
|
1313 * Functions for ":mkspell".
|
|
1314 * Only possible with the multi-byte feature.
|
|
1315 */
|
|
1316
|
300
|
1317 #define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
|
236
|
1318 and .dic file. */
|
|
1319 /*
|
|
1320 * Main structure to store the contents of a ".aff" file.
|
|
1321 */
|
|
1322 typedef struct afffile_S
|
|
1323 {
|
|
1324 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
|
|
1325 char_u *af_try; /* "TRY" line in "af_enc" encoding */
|
307
|
1326 int af_rar; /* ID for rare word */
|
|
1327 int af_huh; /* ID for keep-case word */
|
236
|
1328 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
|
|
1329 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
|
|
1330 garray_T af_rep; /* list of repentry_T entries from REP lines */
|
|
1331 } afffile_T;
|
|
1332
|
|
1333 typedef struct affentry_S affentry_T;
|
|
1334 /* Affix entry from ".aff" file. Used for prefixes and suffixes. */
|
|
1335 struct affentry_S
|
|
1336 {
|
|
1337 affentry_T *ae_next; /* next affix with same name/number */
|
|
1338 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
|
|
1339 char_u *ae_add; /* text to add to basic word (can be NULL) */
|
|
1340 char_u *ae_cond; /* condition (NULL for ".") */
|
|
1341 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
|
300
|
1342 };
|
|
1343
|
|
1344 /* Affix header from ".aff" file. Used for af_pref and af_suff. */
|
|
1345 typedef struct affheader_S
|
|
1346 {
|
|
1347 char_u ah_key[2]; /* key for hashtable == name of affix entry */
|
|
1348 int ah_combine; /* suffix may combine with prefix */
|
|
1349 affentry_T *ah_first; /* first affix entry */
|
|
1350 } affheader_T;
|
|
1351
|
|
1352 #define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
|
|
1353
|
|
1354 /*
|
|
1355 * Structure that is used to store the items in the word tree. This avoids
|
|
1356 * the need to keep track of each allocated thing, it's freed all at once
|
|
1357 * after ":mkspell" is done.
|
|
1358 */
|
|
1359 #define SBLOCKSIZE 16000 /* size of sb_data */
|
|
1360 typedef struct sblock_S sblock_T;
|
|
1361 struct sblock_S
|
|
1362 {
|
|
1363 sblock_T *sb_next; /* next block in list */
|
|
1364 int sb_used; /* nr of bytes already in use */
|
|
1365 char_u sb_data[1]; /* data, actually longer */
|
236
|
1366 };
|
|
1367
|
|
1368 /*
|
300
|
1369 * A node in the tree.
|
236
|
1370 */
|
300
|
1371 typedef struct wordnode_S wordnode_T;
|
|
1372 struct wordnode_S
|
236
|
1373 {
|
300
|
1374 char_u wn_hashkey[6]; /* room for the hash key */
|
|
1375 wordnode_T *wn_next; /* next node with same hash key */
|
|
1376 wordnode_T *wn_child; /* child (next byte in word) */
|
|
1377 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
|
|
1378 always sorted) */
|
|
1379 wordnode_T *wn_wnode; /* parent node that will write this node */
|
|
1380 int wn_index; /* index in written nodes (valid after first
|
|
1381 round) */
|
|
1382 char_u wn_byte; /* Byte for this node. NUL for word end */
|
|
1383 char_u wn_flags; /* when wn_byte is NUL: WF_ flags */
|
|
1384 char_u wn_region; /* when wn_byte is NUL: region mask */
|
236
|
1385 };
|
|
1386
|
300
|
1387 #define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
|
236
|
1388
|
300
|
1389 /*
|
|
1390 * Info used while reading the spell files.
|
|
1391 */
|
|
1392 typedef struct spellinfo_S
|
249
|
1393 {
|
300
|
1394 wordnode_T *si_foldroot; /* tree with case-folded words */
|
|
1395 wordnode_T *si_keeproot; /* tree with keep-case words */
|
|
1396 sblock_T *si_blocks; /* memory blocks used */
|
|
1397 int si_ascii; /* handling only ASCII words */
|
|
1398 int si_region; /* region mask */
|
|
1399 vimconv_T si_conv; /* for conversion to 'encoding' */
|
302
|
1400 int si_memtot; /* runtime memory used */
|
300
|
1401 } spellinfo_T;
|
249
|
1402
|
300
|
1403 static afffile_T *spell_read_aff __ARGS((char_u *fname, spellinfo_T *spin));
|
240
|
1404 static int has_non_ascii __ARGS((char_u *s));
|
300
|
1405 static void spell_free_aff __ARGS((afffile_T *aff));
|
|
1406 static int spell_read_dic __ARGS((char_u *fname, spellinfo_T *spin, afffile_T *affile));
|
307
|
1407 static int store_aff_word __ARGS((char_u *word, spellinfo_T *spin, char_u *afflist, hashtab_T *ht, hashtab_T *xht, int comb, int flags));
|
300
|
1408 static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin));
|
|
1409 static void *getroom __ARGS((sblock_T **blp, size_t len));
|
|
1410 static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s));
|
|
1411 static void free_blocks __ARGS((sblock_T *bl));
|
|
1412 static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp));
|
307
|
1413 static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags));
|
300
|
1414 static int tree_add_word __ARGS((char_u *word, wordnode_T *tree, int flags, int region, sblock_T **blp));
|
|
1415 static void wordtree_compress __ARGS((wordnode_T *root));
|
|
1416 static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot));
|
|
1417 static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
|
|
1418 static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin, int regcount, char_u *regchars));
|
|
1419 static int put_tree __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask));
|
236
|
1420
|
|
1421 /*
|
|
1422 * Read an affix ".aff" file.
|
|
1423 * Returns an afffile_T, NULL for failure.
|
|
1424 */
|
|
1425 static afffile_T *
|
300
|
1426 spell_read_aff(fname, spin)
|
236
|
1427 char_u *fname;
|
300
|
1428 spellinfo_T *spin;
|
236
|
1429 {
|
|
1430 FILE *fd;
|
|
1431 afffile_T *aff;
|
|
1432 char_u rline[MAXLINELEN];
|
|
1433 char_u *line;
|
|
1434 char_u *pc = NULL;
|
|
1435 char_u *(items[6]);
|
|
1436 int itemcnt;
|
|
1437 char_u *p;
|
|
1438 int lnum = 0;
|
|
1439 affheader_T *cur_aff = NULL;
|
|
1440 int aff_todo = 0;
|
|
1441 hashtab_T *tp;
|
255
|
1442 char_u *low = NULL;
|
|
1443 char_u *fol = NULL;
|
|
1444 char_u *upp = NULL;
|
307
|
1445 static char *e_affname = N_("Affix name too long in %s line %d: %s");
|
236
|
1446
|
300
|
1447 /*
|
|
1448 * Open the file.
|
|
1449 */
|
236
|
1450 fd = fopen((char *)fname, "r");
|
|
1451 if (fd == NULL)
|
|
1452 {
|
|
1453 EMSG2(_(e_notopen), fname);
|
|
1454 return NULL;
|
|
1455 }
|
|
1456
|
|
1457 smsg((char_u *)_("Reading affix file %s..."), fname);
|
|
1458 out_flush();
|
|
1459
|
300
|
1460 /*
|
|
1461 * Allocate and init the afffile_T structure.
|
|
1462 */
|
|
1463 aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T));
|
236
|
1464 if (aff == NULL)
|
|
1465 return NULL;
|
|
1466 hash_init(&aff->af_pref);
|
|
1467 hash_init(&aff->af_suff);
|
|
1468 ga_init2(&aff->af_rep, (int)sizeof(repentry_T), 20);
|
|
1469
|
|
1470 /*
|
|
1471 * Read all the lines in the file one by one.
|
|
1472 */
|
255
|
1473 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
|
236
|
1474 {
|
255
|
1475 line_breakcheck();
|
236
|
1476 ++lnum;
|
|
1477
|
|
1478 /* Skip comment lines. */
|
|
1479 if (*rline == '#')
|
|
1480 continue;
|
|
1481
|
|
1482 /* Convert from "SET" to 'encoding' when needed. */
|
|
1483 vim_free(pc);
|
300
|
1484 if (spin->si_conv.vc_type != CONV_NONE)
|
236
|
1485 {
|
300
|
1486 pc = string_convert(&spin->si_conv, rline, NULL);
|
255
|
1487 if (pc == NULL)
|
|
1488 {
|
|
1489 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
|
|
1490 fname, lnum, rline);
|
|
1491 continue;
|
|
1492 }
|
236
|
1493 line = pc;
|
|
1494 }
|
|
1495 else
|
|
1496 {
|
|
1497 pc = NULL;
|
|
1498 line = rline;
|
|
1499 }
|
|
1500
|
|
1501 /* Split the line up in white separated items. Put a NUL after each
|
|
1502 * item. */
|
|
1503 itemcnt = 0;
|
|
1504 for (p = line; ; )
|
|
1505 {
|
|
1506 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
|
|
1507 ++p;
|
|
1508 if (*p == NUL)
|
|
1509 break;
|
300
|
1510 if (itemcnt == 6) /* too many items */
|
|
1511 break;
|
236
|
1512 items[itemcnt++] = p;
|
300
|
1513 while (*p > ' ') /* skip until white space or CR/NL */
|
236
|
1514 ++p;
|
|
1515 if (*p == NUL)
|
|
1516 break;
|
|
1517 *p++ = NUL;
|
|
1518 }
|
|
1519
|
|
1520 /* Handle non-empty lines. */
|
|
1521 if (itemcnt > 0)
|
|
1522 {
|
|
1523 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
|
|
1524 && aff->af_enc == NULL)
|
|
1525 {
|
300
|
1526 /* Setup for conversion from "ENC" to 'encoding'. */
|
|
1527 aff->af_enc = enc_canonize(items[1]);
|
|
1528 if (aff->af_enc != NULL && !spin->si_ascii
|
|
1529 && convert_setup(&spin->si_conv, aff->af_enc,
|
|
1530 p_enc) == FAIL)
|
|
1531 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
|
|
1532 fname, aff->af_enc, p_enc);
|
236
|
1533 }
|
302
|
1534 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
|
|
1535 {
|
|
1536 /* ignored */
|
|
1537 }
|
236
|
1538 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2
|
|
1539 && aff->af_try == NULL)
|
300
|
1540 {
|
|
1541 aff->af_try = getroom_save(&spin->si_blocks, items[1]);
|
|
1542 }
|
307
|
1543 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
|
|
1544 && aff->af_rar == 0)
|
|
1545 {
|
|
1546 aff->af_rar = items[1][0];
|
|
1547 if (items[1][1] != NUL)
|
|
1548 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
|
|
1549 }
|
|
1550 else if (STRCMP(items[0], "HUH") == 0 && itemcnt == 2
|
|
1551 && aff->af_huh == 0)
|
|
1552 {
|
|
1553 aff->af_huh = items[1][0];
|
|
1554 if (items[1][1] != NUL)
|
|
1555 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
|
|
1556 }
|
236
|
1557 else if ((STRCMP(items[0], "PFX") == 0
|
|
1558 || STRCMP(items[0], "SFX") == 0)
|
|
1559 && aff_todo == 0
|
|
1560 && itemcnt == 4)
|
|
1561 {
|
|
1562 /* New affix letter. */
|
300
|
1563 cur_aff = (affheader_T *)getroom(&spin->si_blocks,
|
|
1564 sizeof(affheader_T));
|
236
|
1565 if (cur_aff == NULL)
|
|
1566 break;
|
|
1567 cur_aff->ah_key[0] = *items[1];
|
|
1568 cur_aff->ah_key[1] = NUL;
|
|
1569 if (items[1][1] != NUL)
|
307
|
1570 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
|
236
|
1571 if (*items[2] == 'Y')
|
|
1572 cur_aff->ah_combine = TRUE;
|
300
|
1573 else if (*items[2] != 'N')
|
236
|
1574 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
|
|
1575 fname, lnum, items[2]);
|
|
1576 if (*items[0] == 'P')
|
|
1577 tp = &aff->af_pref;
|
|
1578 else
|
|
1579 tp = &aff->af_suff;
|
300
|
1580 aff_todo = atoi((char *)items[3]);
|
236
|
1581 if (!HASHITEM_EMPTY(hash_find(tp, cur_aff->ah_key)))
|
300
|
1582 {
|
236
|
1583 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
|
|
1584 fname, lnum, items[1]);
|
300
|
1585 aff_todo = 0;
|
|
1586 }
|
236
|
1587 else
|
|
1588 hash_add(tp, cur_aff->ah_key);
|
|
1589 }
|
|
1590 else if ((STRCMP(items[0], "PFX") == 0
|
|
1591 || STRCMP(items[0], "SFX") == 0)
|
|
1592 && aff_todo > 0
|
|
1593 && STRCMP(cur_aff->ah_key, items[1]) == 0
|
|
1594 && itemcnt == 5)
|
|
1595 {
|
|
1596 affentry_T *aff_entry;
|
|
1597
|
|
1598 /* New item for an affix letter. */
|
|
1599 --aff_todo;
|
300
|
1600 aff_entry = (affentry_T *)getroom(&spin->si_blocks,
|
|
1601 sizeof(affentry_T));
|
236
|
1602 if (aff_entry == NULL)
|
|
1603 break;
|
240
|
1604
|
236
|
1605 if (STRCMP(items[2], "0") != 0)
|
300
|
1606 aff_entry->ae_chop = getroom_save(&spin->si_blocks,
|
|
1607 items[2]);
|
236
|
1608 if (STRCMP(items[3], "0") != 0)
|
300
|
1609 aff_entry->ae_add = getroom_save(&spin->si_blocks,
|
|
1610 items[3]);
|
236
|
1611
|
300
|
1612 /* Don't use an affix entry with non-ASCII characters when
|
|
1613 * "spin->si_ascii" is TRUE. */
|
|
1614 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
|
240
|
1615 || has_non_ascii(aff_entry->ae_add)))
|
|
1616 {
|
|
1617 aff_entry->ae_next = cur_aff->ah_first;
|
|
1618 cur_aff->ah_first = aff_entry;
|
300
|
1619
|
|
1620 if (STRCMP(items[4], ".") != 0)
|
|
1621 {
|
|
1622 char_u buf[MAXLINELEN];
|
|
1623
|
|
1624 aff_entry->ae_cond = getroom_save(&spin->si_blocks,
|
|
1625 items[4]);
|
|
1626 if (*items[0] == 'P')
|
|
1627 sprintf((char *)buf, "^%s", items[4]);
|
|
1628 else
|
|
1629 sprintf((char *)buf, "%s$", items[4]);
|
|
1630 aff_entry->ae_prog = vim_regcomp(buf,
|
|
1631 RE_MAGIC + RE_STRING);
|
|
1632 }
|
240
|
1633 }
|
236
|
1634 }
|
255
|
1635 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
|
|
1636 {
|
|
1637 if (fol != NULL)
|
|
1638 smsg((char_u *)_("Duplicate FOL in %s line %d"),
|
|
1639 fname, lnum);
|
|
1640 else
|
|
1641 fol = vim_strsave(items[1]);
|
|
1642 }
|
|
1643 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
|
|
1644 {
|
|
1645 if (low != NULL)
|
|
1646 smsg((char_u *)_("Duplicate LOW in %s line %d"),
|
|
1647 fname, lnum);
|
|
1648 else
|
|
1649 low = vim_strsave(items[1]);
|
|
1650 }
|
|
1651 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
|
|
1652 {
|
|
1653 if (upp != NULL)
|
|
1654 smsg((char_u *)_("Duplicate UPP in %s line %d"),
|
|
1655 fname, lnum);
|
|
1656 else
|
|
1657 upp = vim_strsave(items[1]);
|
|
1658 }
|
236
|
1659 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
|
|
1660 /* Ignore REP count */;
|
|
1661 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
|
|
1662 {
|
|
1663 repentry_T *rp;
|
|
1664
|
|
1665 /* REP item */
|
|
1666 if (ga_grow(&aff->af_rep, 1) == FAIL)
|
|
1667 break;
|
|
1668 rp = ((repentry_T *)aff->af_rep.ga_data) + aff->af_rep.ga_len;
|
300
|
1669 rp->re_from = getroom_save(&spin->si_blocks, items[1]);
|
|
1670 rp->re_to = getroom_save(&spin->si_blocks, items[2]);
|
236
|
1671 ++aff->af_rep.ga_len;
|
|
1672 }
|
300
|
1673 else
|
236
|
1674 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
|
|
1675 fname, lnum, items[0]);
|
|
1676 }
|
|
1677 }
|
|
1678
|
255
|
1679 if (fol != NULL || low != NULL || upp != NULL)
|
|
1680 {
|
260
|
1681 /* Don't write a word table for an ASCII file, so that we don't check
|
|
1682 * for conflicts with a word table that matches 'encoding'. */
|
300
|
1683 if (!spin->si_ascii)
|
260
|
1684 {
|
|
1685 if (fol == NULL || low == NULL || upp == NULL)
|
|
1686 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
|
|
1687 else
|
|
1688 set_spell_chartab(fol, low, upp);
|
|
1689 }
|
255
|
1690
|
|
1691 vim_free(fol);
|
|
1692 vim_free(low);
|
|
1693 vim_free(upp);
|
|
1694 }
|
|
1695
|
236
|
1696 vim_free(pc);
|
|
1697 fclose(fd);
|
|
1698 return aff;
|
|
1699 }
|
|
1700
|
|
1701 /*
|
240
|
1702 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
|
|
1703 * When "s" is NULL FALSE is returned.
|
|
1704 */
|
|
1705 static int
|
|
1706 has_non_ascii(s)
|
|
1707 char_u *s;
|
|
1708 {
|
|
1709 char_u *p;
|
|
1710
|
|
1711 if (s != NULL)
|
|
1712 for (p = s; *p != NUL; ++p)
|
|
1713 if (*p >= 128)
|
|
1714 return TRUE;
|
|
1715 return FALSE;
|
|
1716 }
|
|
1717
|
|
1718 /*
|
236
|
1719 * Free the structure filled by spell_read_aff().
|
|
1720 */
|
|
1721 static void
|
|
1722 spell_free_aff(aff)
|
|
1723 afffile_T *aff;
|
|
1724 {
|
|
1725 hashtab_T *ht;
|
|
1726 hashitem_T *hi;
|
|
1727 int todo;
|
|
1728 affheader_T *ah;
|
300
|
1729 affentry_T *ae;
|
236
|
1730
|
|
1731 vim_free(aff->af_enc);
|
|
1732
|
300
|
1733 /* All this trouble to foree the "ae_prog" items... */
|
236
|
1734 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
|
|
1735 {
|
|
1736 todo = ht->ht_used;
|
|
1737 for (hi = ht->ht_array; todo > 0; ++hi)
|
|
1738 {
|
|
1739 if (!HASHITEM_EMPTY(hi))
|
|
1740 {
|
|
1741 --todo;
|
|
1742 ah = HI2AH(hi);
|
300
|
1743 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
|
|
1744 vim_free(ae->ae_prog);
|
236
|
1745 }
|
|
1746 }
|
|
1747 if (ht == &aff->af_suff)
|
|
1748 break;
|
|
1749 }
|
300
|
1750
|
236
|
1751 hash_clear(&aff->af_pref);
|
|
1752 hash_clear(&aff->af_suff);
|
|
1753 ga_clear(&aff->af_rep);
|
|
1754 }
|
|
1755
|
|
1756 /*
|
300
|
1757 * Read dictionary file "fname".
|
236
|
1758 * Returns OK or FAIL;
|
|
1759 */
|
|
1760 static int
|
300
|
1761 spell_read_dic(fname, spin, affile)
|
236
|
1762 char_u *fname;
|
300
|
1763 spellinfo_T *spin;
|
|
1764 afffile_T *affile;
|
236
|
1765 {
|
300
|
1766 hashtab_T ht;
|
236
|
1767 char_u line[MAXLINELEN];
|
300
|
1768 char_u *afflist;
|
|
1769 char_u *dw;
|
236
|
1770 char_u *pc;
|
|
1771 char_u *w;
|
|
1772 int l;
|
|
1773 hash_T hash;
|
|
1774 hashitem_T *hi;
|
|
1775 FILE *fd;
|
|
1776 int lnum = 1;
|
300
|
1777 int non_ascii = 0;
|
|
1778 int retval = OK;
|
|
1779 char_u message[MAXLINELEN + MAXWLEN];
|
307
|
1780 int flags;
|
236
|
1781
|
300
|
1782 /*
|
|
1783 * Open the file.
|
|
1784 */
|
236
|
1785 fd = fopen((char *)fname, "r");
|
|
1786 if (fd == NULL)
|
|
1787 {
|
|
1788 EMSG2(_(e_notopen), fname);
|
|
1789 return FAIL;
|
|
1790 }
|
|
1791
|
300
|
1792 /* The hashtable is only used to detect duplicated words. */
|
|
1793 hash_init(&ht);
|
|
1794
|
236
|
1795 smsg((char_u *)_("Reading dictionary file %s..."), fname);
|
|
1796 out_flush();
|
|
1797
|
|
1798 /* Read and ignore the first line: word count. */
|
|
1799 (void)vim_fgets(line, MAXLINELEN, fd);
|
|
1800 if (!isdigit(*skipwhite(line)))
|
|
1801 EMSG2(_("E760: No word count in %s"), fname);
|
|
1802
|
|
1803 /*
|
|
1804 * Read all the lines in the file one by one.
|
|
1805 * The words are converted to 'encoding' here, before being added to
|
|
1806 * the hashtable.
|
|
1807 */
|
255
|
1808 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
|
236
|
1809 {
|
255
|
1810 line_breakcheck();
|
236
|
1811 ++lnum;
|
|
1812
|
300
|
1813 /* Remove CR, LF and white space from the end. White space halfway
|
|
1814 * the word is kept to allow e.g., "et al.". */
|
236
|
1815 l = STRLEN(line);
|
|
1816 while (l > 0 && line[l - 1] <= ' ')
|
|
1817 --l;
|
|
1818 if (l == 0)
|
|
1819 continue; /* empty line */
|
|
1820 line[l] = NUL;
|
|
1821
|
300
|
1822 /* This takes time, print a message now and then. */
|
|
1823 if ((lnum & 0x3ff) == 0)
|
|
1824 {
|
|
1825 vim_snprintf((char *)message, sizeof(message),
|
|
1826 _("line %6d - %s"), lnum, line);
|
|
1827 msg_start();
|
|
1828 msg_outtrans_attr(message, 0);
|
|
1829 msg_clr_eos();
|
|
1830 msg_didout = FALSE;
|
|
1831 msg_col = 0;
|
|
1832 out_flush();
|
|
1833 }
|
|
1834
|
236
|
1835 /* Find the optional affix names. */
|
300
|
1836 afflist = vim_strchr(line, '/');
|
|
1837 if (afflist != NULL)
|
|
1838 *afflist++ = NUL;
|
236
|
1839
|
300
|
1840 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
|
|
1841 if (spin->si_ascii && has_non_ascii(line))
|
|
1842 {
|
|
1843 ++non_ascii;
|
240
|
1844 continue;
|
300
|
1845 }
|
240
|
1846
|
236
|
1847 /* Convert from "SET" to 'encoding' when needed. */
|
300
|
1848 if (spin->si_conv.vc_type != CONV_NONE)
|
236
|
1849 {
|
300
|
1850 pc = string_convert(&spin->si_conv, line, NULL);
|
255
|
1851 if (pc == NULL)
|
|
1852 {
|
|
1853 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
|
|
1854 fname, lnum, line);
|
|
1855 continue;
|
|
1856 }
|
236
|
1857 w = pc;
|
|
1858 }
|
|
1859 else
|
|
1860 {
|
|
1861 pc = NULL;
|
|
1862 w = line;
|
|
1863 }
|
|
1864
|
300
|
1865 /* Store the word in the hashtable to be able to find duplicates. */
|
|
1866 dw = (char_u *)getroom_save(&spin->si_blocks, w);
|
236
|
1867 if (dw == NULL)
|
300
|
1868 retval = FAIL;
|
|
1869 vim_free(pc);
|
|
1870 if (retval == FAIL)
|
236
|
1871 break;
|
|
1872
|
300
|
1873 hash = hash_hash(dw);
|
|
1874 hi = hash_lookup(&ht, dw, hash);
|
236
|
1875 if (!HASHITEM_EMPTY(hi))
|
|
1876 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
|
300
|
1877 fname, lnum, line);
|
236
|
1878 else
|
300
|
1879 hash_add_item(&ht, hi, dw, hash);
|
|
1880
|
307
|
1881 flags = 0;
|
|
1882 if (afflist != NULL)
|
|
1883 {
|
|
1884 /* Check for affix name that stands for keep-case word and stands
|
|
1885 * for rare word (if defined). */
|
|
1886 if (affile->af_huh != NUL
|
|
1887 && vim_strchr(afflist, affile->af_huh) != NULL)
|
|
1888 flags |= WF_KEEPCAP;
|
|
1889 if (affile->af_rar != NUL
|
|
1890 && vim_strchr(afflist, affile->af_rar) != NULL)
|
|
1891 flags |= WF_RARE;
|
|
1892 }
|
|
1893
|
300
|
1894 /* Add the word to the word tree(s). */
|
307
|
1895 if (store_word(dw, spin, flags) == FAIL)
|
300
|
1896 retval = FAIL;
|
236
|
1897
|
300
|
1898 if (afflist != NULL)
|
|
1899 {
|
|
1900 /* Find all matching suffixes and add the resulting words.
|
|
1901 * Additionally do matching prefixes that combine. */
|
|
1902 if (store_aff_word(dw, spin, afflist,
|
307
|
1903 &affile->af_suff, &affile->af_pref,
|
|
1904 FALSE, flags) == FAIL)
|
300
|
1905 retval = FAIL;
|
|
1906
|
|
1907 /* Find all matching prefixes and add the resulting words. */
|
|
1908 if (store_aff_word(dw, spin, afflist,
|
307
|
1909 &affile->af_pref, NULL, FALSE, flags) == FAIL)
|
300
|
1910 retval = FAIL;
|
|
1911 }
|
236
|
1912 }
|
|
1913
|
300
|
1914 if (spin->si_ascii && non_ascii > 0)
|
|
1915 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
|
|
1916 non_ascii);
|
|
1917 hash_clear(&ht);
|
|
1918
|
236
|
1919 fclose(fd);
|
300
|
1920 return retval;
|
236
|
1921 }
|
|
1922
|
|
1923 /*
|
300
|
1924 * Apply affixes to a word and store the resulting words.
|
|
1925 * "ht" is the hashtable with affentry_T that need to be applied, either
|
|
1926 * prefixes or suffixes.
|
|
1927 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
|
|
1928 * the resulting words for combining affixes.
|
|
1929 *
|
|
1930 * Returns FAIL when out of memory.
|
236
|
1931 */
|
300
|
1932 static int
|
307
|
1933 store_aff_word(word, spin, afflist, ht, xht, comb, flags)
|
300
|
1934 char_u *word; /* basic word start */
|
|
1935 spellinfo_T *spin; /* spell info */
|
|
1936 char_u *afflist; /* list of names of supported affixes */
|
|
1937 hashtab_T *ht;
|
|
1938 hashtab_T *xht;
|
|
1939 int comb; /* only use affixes that combine */
|
307
|
1940 int flags; /* flags for the word */
|
236
|
1941 {
|
|
1942 int todo;
|
|
1943 hashitem_T *hi;
|
300
|
1944 affheader_T *ah;
|
|
1945 affentry_T *ae;
|
|
1946 regmatch_T regmatch;
|
|
1947 char_u newword[MAXWLEN];
|
|
1948 int retval = OK;
|
|
1949 int i;
|
|
1950 char_u *p;
|
236
|
1951
|
300
|
1952 todo = ht->ht_used;
|
|
1953 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
|
236
|
1954 {
|
|
1955 if (!HASHITEM_EMPTY(hi))
|
|
1956 {
|
|
1957 --todo;
|
300
|
1958 ah = HI2AH(hi);
|
236
|
1959
|
300
|
1960 /* Check that the affix combines, if required, and that the word
|
|
1961 * supports this affix. */
|
|
1962 if ((!comb || ah->ah_combine)
|
|
1963 && vim_strchr(afflist, *ah->ah_key) != NULL)
|
236
|
1964 {
|
300
|
1965 /* Loop over all affix entries with this name. */
|
|
1966 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
|
236
|
1967 {
|
300
|
1968 /* Check the condition. It's not logical to match case
|
|
1969 * here, but it is required for compatibility with
|
|
1970 * Myspell. */
|
|
1971 regmatch.regprog = ae->ae_prog;
|
|
1972 regmatch.rm_ic = FALSE;
|
|
1973 if (ae->ae_prog == NULL
|
|
1974 || vim_regexec(®match, word, (colnr_T)0))
|
|
1975 {
|
|
1976 /* Match. Remove the chop and add the affix. */
|
|
1977 if (xht == NULL)
|
240
|
1978 {
|
300
|
1979 /* prefix: chop/add at the start of the word */
|
|
1980 if (ae->ae_add == NULL)
|
|
1981 *newword = NUL;
|
|
1982 else
|
|
1983 STRCPY(newword, ae->ae_add);
|
|
1984 p = word;
|
|
1985 if (ae->ae_chop != NULL)
|
|
1986 /* Skip chop string. */
|
|
1987 for (i = mb_charlen(ae->ae_chop); i > 0; --i)
|
|
1988 mb_ptr_adv(p);
|
|
1989 STRCAT(newword, p);
|
|
1990 }
|
|
1991 else
|
|
1992 {
|
|
1993 /* suffix: chop/add at the end of the word */
|
|
1994 STRCPY(newword, word);
|
|
1995 if (ae->ae_chop != NULL)
|
|
1996 {
|
|
1997 /* Remove chop string. */
|
|
1998 p = newword + STRLEN(newword);
|
|
1999 for (i = mb_charlen(ae->ae_chop); i > 0; --i)
|
|
2000 mb_ptr_back(newword, p);
|
|
2001 *p = NUL;
|
|
2002 }
|
|
2003 if (ae->ae_add != NULL)
|
|
2004 STRCAT(newword, ae->ae_add);
|
240
|
2005 }
|
|
2006
|
300
|
2007 /* Store the modified word. */
|
307
|
2008 if (store_word(newword, spin, flags) == FAIL)
|
300
|
2009 retval = FAIL;
|
236
|
2010
|
300
|
2011 /* When added a suffix and combining is allowed also
|
|
2012 * try adding prefixes additionally. */
|
|
2013 if (xht != NULL && ah->ah_combine)
|
|
2014 if (store_aff_word(newword, spin, afflist,
|
307
|
2015 xht, NULL, TRUE, flags) == FAIL)
|
300
|
2016 retval = FAIL;
|
236
|
2017 }
|
|
2018 }
|
|
2019 }
|
|
2020 }
|
|
2021 }
|
|
2022
|
|
2023 return retval;
|
|
2024 }
|
|
2025
|
|
2026 /*
|
300
|
2027 * Read a file with a list of words.
|
236
|
2028 */
|
|
2029 static int
|
300
|
2030 spell_read_wordfile(fname, spin)
|
|
2031 char_u *fname;
|
|
2032 spellinfo_T *spin;
|
236
|
2033 {
|
300
|
2034 FILE *fd;
|
|
2035 long lnum = 0;
|
|
2036 char_u rline[MAXLINELEN];
|
|
2037 char_u *line;
|
|
2038 char_u *pc = NULL;
|
|
2039 int l;
|
|
2040 int retval = OK;
|
|
2041 int did_word = FALSE;
|
|
2042 int non_ascii = 0;
|
|
2043 char_u *enc;
|
307
|
2044 int flags;
|
236
|
2045
|
300
|
2046 /*
|
|
2047 * Open the file.
|
|
2048 */
|
|
2049 fd = fopen((char *)fname, "r");
|
|
2050 if (fd == NULL)
|
236
|
2051 {
|
300
|
2052 EMSG2(_(e_notopen), fname);
|
|
2053 return FAIL;
|
236
|
2054 }
|
|
2055
|
300
|
2056 smsg((char_u *)_("Reading word file %s..."), fname);
|
|
2057 out_flush();
|
|
2058
|
|
2059 /*
|
|
2060 * Read all the lines in the file one by one.
|
|
2061 */
|
|
2062 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
|
|
2063 {
|
|
2064 line_breakcheck();
|
|
2065 ++lnum;
|
|
2066
|
|
2067 /* Skip comment lines. */
|
|
2068 if (*rline == '#')
|
|
2069 continue;
|
|
2070
|
|
2071 /* Remove CR, LF and white space from the end. */
|
|
2072 l = STRLEN(rline);
|
|
2073 while (l > 0 && rline[l - 1] <= ' ')
|
|
2074 --l;
|
|
2075 if (l == 0)
|
|
2076 continue; /* empty or blank line */
|
|
2077 rline[l] = NUL;
|
|
2078
|
|
2079 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
|
|
2080 vim_free(pc);
|
|
2081 if (spin->si_conv.vc_type != CONV_NONE)
|
|
2082 {
|
|
2083 pc = string_convert(&spin->si_conv, rline, NULL);
|
|
2084 if (pc == NULL)
|
|
2085 {
|
|
2086 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
|
|
2087 fname, lnum, rline);
|
|
2088 continue;
|
|
2089 }
|
|
2090 line = pc;
|
|
2091 }
|
|
2092 else
|
|
2093 {
|
|
2094 pc = NULL;
|
|
2095 line = rline;
|
|
2096 }
|
|
2097
|
307
|
2098 flags = 0;
|
|
2099
|
|
2100 if (*line == '/')
|
300
|
2101 {
|
307
|
2102 ++line;
|
|
2103 if (STRNCMP(line, "encoding=", 9) == 0)
|
300
|
2104 {
|
|
2105 if (spin->si_conv.vc_type != CONV_NONE)
|
|
2106 smsg((char_u *)_("Duplicate =encoding= line ignored in %s line %d: %s"),
|
|
2107 fname, lnum, line);
|
|
2108 else if (did_word)
|
|
2109 smsg((char_u *)_("=encoding= line after word ignored in %s line %d: %s"),
|
|
2110 fname, lnum, line);
|
|
2111 else
|
|
2112 {
|
|
2113 /* Setup for conversion to 'encoding'. */
|
|
2114 enc = enc_canonize(line + 10);
|
|
2115 if (enc != NULL && !spin->si_ascii
|
|
2116 && convert_setup(&spin->si_conv, enc,
|
|
2117 p_enc) == FAIL)
|
|
2118 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
|
|
2119 fname, line + 10, p_enc);
|
|
2120 vim_free(enc);
|
|
2121 }
|
307
|
2122 continue;
|
300
|
2123 }
|
307
|
2124
|
|
2125 if (*line == '=')
|
|
2126 {
|
|
2127 /* keep-case word */
|
|
2128 flags |= WF_KEEPCAP;
|
|
2129 ++line;
|
|
2130 }
|
|
2131
|
|
2132 if (*line == '!')
|
|
2133 {
|
|
2134 /* Bad, bad, wicked word. */
|
|
2135 flags |= WF_BANNED;
|
|
2136 ++line;
|
|
2137 }
|
|
2138 else if (*line == '?')
|
|
2139 {
|
|
2140 /* Rare word. */
|
|
2141 flags |= WF_RARE;
|
|
2142 ++line;
|
|
2143 }
|
|
2144
|
|
2145 if (flags == 0)
|
|
2146 {
|
|
2147 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
|
300
|
2148 fname, lnum, line);
|
307
|
2149 continue;
|
|
2150 }
|
300
|
2151 }
|
|
2152
|
|
2153 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
|
|
2154 if (spin->si_ascii && has_non_ascii(line))
|
|
2155 {
|
|
2156 ++non_ascii;
|
|
2157 continue;
|
|
2158 }
|
|
2159
|
|
2160 /* Normal word: store it. */
|
307
|
2161 if (store_word(line, spin, flags) == FAIL)
|
300
|
2162 {
|
|
2163 retval = FAIL;
|
|
2164 break;
|
|
2165 }
|
|
2166 did_word = TRUE;
|
|
2167 }
|
|
2168
|
|
2169 vim_free(pc);
|
|
2170 fclose(fd);
|
|
2171
|
|
2172 if (spin->si_ascii && non_ascii > 0)
|
|
2173 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
|
|
2174 non_ascii);
|
|
2175 return retval;
|
236
|
2176 }
|
|
2177
|
|
2178 /*
|
300
|
2179 * Get part of an sblock_T, "len" bytes long.
|
|
2180 * This avoids calling free() for every little struct we use.
|
|
2181 * The memory is cleared to all zeros.
|
|
2182 * Returns NULL when out of memory.
|
|
2183 */
|
|
2184 static void *
|
|
2185 getroom(blp, len)
|
|
2186 sblock_T **blp;
|
|
2187 size_t len; /* length needed */
|
|
2188 {
|
|
2189 char_u *p;
|
|
2190 sblock_T *bl = *blp;
|
|
2191
|
|
2192 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
|
|
2193 {
|
|
2194 /* Allocate a block of memory. This is not freed until much later. */
|
|
2195 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
|
|
2196 if (bl == NULL)
|
|
2197 return NULL;
|
|
2198 bl->sb_next = *blp;
|
|
2199 *blp = bl;
|
|
2200 bl->sb_used = 0;
|
|
2201 }
|
|
2202
|
|
2203 p = bl->sb_data + bl->sb_used;
|
|
2204 bl->sb_used += len;
|
|
2205
|
|
2206 return p;
|
|
2207 }
|
|
2208
|
|
2209 /*
|
|
2210 * Make a copy of a string into memory allocated with getroom().
|
|
2211 */
|
|
2212 static char_u *
|
|
2213 getroom_save(blp, s)
|
|
2214 sblock_T **blp;
|
|
2215 char_u *s;
|
|
2216 {
|
|
2217 char_u *sc;
|
|
2218
|
|
2219 sc = (char_u *)getroom(blp, STRLEN(s) + 1);
|
|
2220 if (sc != NULL)
|
|
2221 STRCPY(sc, s);
|
|
2222 return sc;
|
|
2223 }
|
|
2224
|
|
2225
|
|
2226 /*
|
|
2227 * Free the list of allocated sblock_T.
|
236
|
2228 */
|
|
2229 static void
|
300
|
2230 free_blocks(bl)
|
|
2231 sblock_T *bl;
|
236
|
2232 {
|
300
|
2233 sblock_T *next;
|
236
|
2234
|
300
|
2235 while (bl != NULL)
|
236
|
2236 {
|
300
|
2237 next = bl->sb_next;
|
|
2238 vim_free(bl);
|
|
2239 bl = next;
|
236
|
2240 }
|
|
2241 }
|
|
2242
|
|
2243 /*
|
300
|
2244 * Allocate the root of a word tree.
|
236
|
2245 */
|
300
|
2246 static wordnode_T *
|
|
2247 wordtree_alloc(blp)
|
|
2248 sblock_T **blp;
|
236
|
2249 {
|
300
|
2250 return (wordnode_T *)getroom(blp, sizeof(wordnode_T));
|
236
|
2251 }
|
|
2252
|
|
2253 /*
|
300
|
2254 * Store a word in the tree(s).
|
307
|
2255 * Always store it in the case-folded tree. A keep-case word can also be used
|
|
2256 * with all caps.
|
300
|
2257 * For a keep-case word also store it in the keep-case tree.
|
236
|
2258 */
|
|
2259 static int
|
307
|
2260 store_word(word, spin, flags)
|
300
|
2261 char_u *word;
|
|
2262 spellinfo_T *spin;
|
307
|
2263 int flags; /* extra flags, WF_BANNED */
|
236
|
2264 {
|
300
|
2265 int len = STRLEN(word);
|
|
2266 int ct = captype(word, word + len);
|
|
2267 char_u foldword[MAXWLEN];
|
|
2268 int res;
|
236
|
2269
|
307
|
2270 if (flags & WF_KEEPCAP)
|
|
2271 res = OK; /* keep-case specified, don't add as fold-case */
|
|
2272 else
|
|
2273 {
|
|
2274 (void)spell_casefold(word, len, foldword, MAXWLEN);
|
|
2275 res = tree_add_word(foldword, spin->si_foldroot,
|
|
2276 (ct == WF_KEEPCAP ? WF_ALLCAP : ct) | flags,
|
|
2277 spin->si_region, &spin->si_blocks);
|
|
2278 }
|
|
2279
|
|
2280 if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP))
|
|
2281 res = tree_add_word(word, spin->si_keeproot, flags,
|
|
2282 spin->si_region, &spin->si_blocks);
|
300
|
2283 return res;
|
236
|
2284 }
|
|
2285
|
|
2286 /*
|
300
|
2287 * Add word "word" to a word tree at "root".
|
255
|
2288 * Returns FAIL when out of memory.
|
236
|
2289 */
|
255
|
2290 static int
|
300
|
2291 tree_add_word(word, root, flags, region, blp)
|
|
2292 char_u *word;
|
|
2293 wordnode_T *root;
|
|
2294 int flags;
|
|
2295 int region;
|
|
2296 sblock_T **blp;
|
236
|
2297 {
|
300
|
2298 wordnode_T *node = root;
|
|
2299 wordnode_T *np;
|
|
2300 wordnode_T **prev = NULL;
|
|
2301 int i;
|
255
|
2302
|
300
|
2303 /* Add each byte of the word to the tree, including the NUL at the end. */
|
|
2304 for (i = 0; ; ++i)
|
255
|
2305 {
|
300
|
2306 /* Look for the sibling that has the same character. They are sorted
|
|
2307 * on byte value, thus stop searching when a sibling is found with a
|
|
2308 * higher byte value. For zero bytes (end of word) check that the
|
|
2309 * flags are equal, there is a separate zero byte for each flag value.
|
|
2310 */
|
|
2311 while (node != NULL && (node->wn_byte < word[i]
|
307
|
2312 || (node->wn_byte == 0 && node->wn_flags != (flags & 0xff))))
|
236
|
2313 {
|
300
|
2314 prev = &node->wn_sibling;
|
|
2315 node = *prev;
|
236
|
2316 }
|
300
|
2317 if (node == NULL || node->wn_byte != word[i])
|
255
|
2318 {
|
300
|
2319 /* Allocate a new node. */
|
|
2320 np = (wordnode_T *)getroom(blp, sizeof(wordnode_T));
|
|
2321 if (np == NULL)
|
|
2322 return FAIL;
|
|
2323 np->wn_byte = word[i];
|
|
2324 *prev = np;
|
|
2325 np->wn_sibling = node;
|
|
2326 node = np;
|
255
|
2327 }
|
300
|
2328
|
|
2329 if (word[i] == NUL)
|
|
2330 {
|
|
2331 node->wn_flags = flags;
|
|
2332 node->wn_region |= region;
|
|
2333 break;
|
|
2334 }
|
|
2335 prev = &node->wn_child;
|
|
2336 node = *prev;
|
255
|
2337 }
|
|
2338
|
|
2339 return OK;
|
236
|
2340 }
|
|
2341
|
|
2342 /*
|
300
|
2343 * Compress a tree: find tails that are identical and can be shared.
|
|
2344 */
|
|
2345 static void
|
|
2346 wordtree_compress(root)
|
|
2347 wordnode_T *root;
|
|
2348 {
|
|
2349 hashtab_T ht;
|
|
2350 int n;
|
|
2351 int tot = 0;
|
|
2352
|
|
2353 if (root != NULL)
|
|
2354 {
|
|
2355 hash_init(&ht);
|
|
2356 n = node_compress(root, &ht, &tot);
|
|
2357 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
|
|
2358 n, tot, (tot - n) * 100 / tot);
|
|
2359 hash_clear(&ht);
|
|
2360 }
|
|
2361 }
|
|
2362
|
|
2363 /*
|
|
2364 * Compress a node, its siblings and its children, depth first.
|
|
2365 * Returns the number of compressed nodes.
|
236
|
2366 */
|
255
|
2367 static int
|
300
|
2368 node_compress(node, ht, tot)
|
|
2369 wordnode_T *node;
|
|
2370 hashtab_T *ht;
|
|
2371 int *tot; /* total count of nodes before compressing,
|
|
2372 incremented while going through the tree */
|
236
|
2373 {
|
300
|
2374 wordnode_T *np;
|
|
2375 wordnode_T *tp;
|
|
2376 wordnode_T *child;
|
|
2377 hash_T hash;
|
236
|
2378 hashitem_T *hi;
|
300
|
2379 int len = 0;
|
|
2380 unsigned nr, n;
|
|
2381 int compressed = 0;
|
236
|
2382
|
300
|
2383 /*
|
|
2384 * Go through the list of siblings. Compress each child and then try
|
|
2385 * finding an identical child to replace it.
|
|
2386 * Note that with "child" we mean not just the node that is pointed to,
|
|
2387 * but the whole list of siblings, of which the node is the first.
|
|
2388 */
|
|
2389 for (np = node; np != NULL; np = np->wn_sibling)
|
236
|
2390 {
|
300
|
2391 ++len;
|
|
2392 if ((child = np->wn_child) != NULL)
|
|
2393 {
|
|
2394 /* Compress the child. This fills wn_hashkey. */
|
|
2395 compressed += node_compress(child, ht, tot);
|
|
2396
|
|
2397 /* Try to find an identical child. */
|
|
2398 hash = hash_hash(child->wn_hashkey);
|
|
2399 hi = hash_lookup(ht, child->wn_hashkey, hash);
|
|
2400 tp = NULL;
|
|
2401 if (!HASHITEM_EMPTY(hi))
|
|
2402 {
|
|
2403 /* There are children with an identical hash value. Now check
|
|
2404 * if there is one that is really identical. */
|
|
2405 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_next)
|
|
2406 if (node_equal(child, tp))
|
|
2407 {
|
|
2408 /* Found one! Now use that child in place of the
|
|
2409 * current one. This means the current child is
|
|
2410 * dropped from the tree. */
|
|
2411 np->wn_child = tp;
|
|
2412 ++compressed;
|
|
2413 break;
|
|
2414 }
|
|
2415 if (tp == NULL)
|
|
2416 {
|
|
2417 /* No other child with this hash value equals the child of
|
|
2418 * the node, add it to the linked list after the first
|
|
2419 * item. */
|
|
2420 tp = HI2WN(hi);
|
|
2421 child->wn_next = tp->wn_next;
|
|
2422 tp->wn_next = child;
|
|
2423 }
|
|
2424 }
|
|
2425 else
|
|
2426 /* No other child has this hash value, add it to the
|
|
2427 * hashtable. */
|
|
2428 hash_add_item(ht, hi, child->wn_hashkey, hash);
|
|
2429 }
|
236
|
2430 }
|
300
|
2431 *tot += len;
|
|
2432
|
|
2433 /*
|
|
2434 * Make a hash key for the node and its siblings, so that we can quickly
|
|
2435 * find a lookalike node. This must be done after compressing the sibling
|
|
2436 * list, otherwise the hash key would become invalid by the compression.
|
|
2437 */
|
|
2438 node->wn_hashkey[0] = len;
|
|
2439 nr = 0;
|
|
2440 for (np = node; np != NULL; np = np->wn_sibling)
|
236
|
2441 {
|
300
|
2442 if (np->wn_byte == NUL)
|
|
2443 /* end node: only use wn_flags and wn_region */
|
|
2444 n = np->wn_flags + (np->wn_region << 8);
|
|
2445 else
|
|
2446 /* byte node: use the byte value and the child pointer */
|
|
2447 n = np->wn_byte + ((long_u)np->wn_child << 8);
|
|
2448 nr = nr * 101 + n;
|
236
|
2449 }
|
300
|
2450
|
|
2451 /* Avoid NUL bytes, it terminates the hash key. */
|
|
2452 n = nr & 0xff;
|
|
2453 node->wn_hashkey[1] = n == 0 ? 1 : n;
|
|
2454 n = (nr >> 8) & 0xff;
|
|
2455 node->wn_hashkey[2] = n == 0 ? 1 : n;
|
|
2456 n = (nr >> 16) & 0xff;
|
|
2457 node->wn_hashkey[3] = n == 0 ? 1 : n;
|
|
2458 n = (nr >> 24) & 0xff;
|
|
2459 node->wn_hashkey[4] = n == 0 ? 1 : n;
|
|
2460 node->wn_hashkey[5] = NUL;
|
|
2461
|
|
2462 return compressed;
|
|
2463 }
|
|
2464
|
|
2465 /*
|
|
2466 * Return TRUE when two nodes have identical siblings and children.
|
|
2467 */
|
|
2468 static int
|
|
2469 node_equal(n1, n2)
|
|
2470 wordnode_T *n1;
|
|
2471 wordnode_T *n2;
|
|
2472 {
|
|
2473 wordnode_T *p1;
|
|
2474 wordnode_T *p2;
|
|
2475
|
|
2476 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
|
|
2477 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
|
|
2478 if (p1->wn_byte != p2->wn_byte
|
|
2479 || (p1->wn_byte == NUL
|
|
2480 ? (p1->wn_flags != p2->wn_flags
|
|
2481 || p1->wn_region != p2->wn_region)
|
|
2482 : (p1->wn_child != p2->wn_child)))
|
|
2483 break;
|
|
2484
|
|
2485 return p1 == NULL && p2 == NULL;
|
236
|
2486 }
|
|
2487
|
|
2488 /*
|
|
2489 * Write a number to file "fd", MSB first, in "len" bytes.
|
|
2490 */
|
255
|
2491 void
|
236
|
2492 put_bytes(fd, nr, len)
|
|
2493 FILE *fd;
|
|
2494 long_u nr;
|
|
2495 int len;
|
|
2496 {
|
|
2497 int i;
|
|
2498
|
|
2499 for (i = len - 1; i >= 0; --i)
|
|
2500 putc((int)(nr >> (i * 8)), fd);
|
|
2501 }
|
|
2502
|
|
2503 /*
|
|
2504 * Write the Vim spell file "fname".
|
|
2505 */
|
|
2506 static void
|
300
|
2507 write_vim_spell(fname, spin, regcount, regchars)
|
236
|
2508 char_u *fname;
|
300
|
2509 spellinfo_T *spin;
|
236
|
2510 int regcount; /* number of regions */
|
|
2511 char_u *regchars; /* region names */
|
|
2512 {
|
300
|
2513 FILE *fd;
|
|
2514 int regionmask;
|
236
|
2515 int round;
|
300
|
2516 wordnode_T *tree;
|
|
2517 int nodecount;
|
236
|
2518
|
300
|
2519 fd = fopen((char *)fname, "w");
|
|
2520 if (fd == NULL)
|
236
|
2521 {
|
|
2522 EMSG2(_(e_notopen), fname);
|
|
2523 return;
|
|
2524 }
|
|
2525
|
255
|
2526 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
|
|
2527 * <charflagslen> <charflags> <fcharslen> <fchars> */
|
300
|
2528
|
|
2529 /* <fileID> */
|
|
2530 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
|
|
2531 EMSG(_(e_write));
|
236
|
2532
|
|
2533 /* write the region names if there is more than one */
|
|
2534 if (regcount > 1)
|
|
2535 {
|
300
|
2536 putc(regcount, fd); /* <regioncnt> <regionname> ... */
|
|
2537 fwrite(regchars, (size_t)(regcount * 2), (size_t)1, fd);
|
|
2538 regionmask = (1 << regcount) - 1;
|
236
|
2539 }
|
|
2540 else
|
|
2541 {
|
300
|
2542 putc(0, fd);
|
|
2543 regionmask = 0;
|
236
|
2544 }
|
|
2545
|
255
|
2546 /* Write the table with character flags and table for case folding.
|
260
|
2547 * <charflagslen> <charflags> <fcharlen> <fchars>
|
|
2548 * Skip this for ASCII, the table may conflict with the one used for
|
|
2549 * 'encoding'. */
|
300
|
2550 if (spin->si_ascii)
|
260
|
2551 {
|
300
|
2552 putc(0, fd);
|
|
2553 putc(0, fd);
|
|
2554 putc(0, fd);
|
260
|
2555 }
|
|
2556 else
|
300
|
2557 write_spell_chartab(fd);
|
255
|
2558
|
236
|
2559
|
255
|
2560 /* <SUGGEST> : <suggestlen> <more> ...
|
|
2561 * TODO. Only write a zero length for now. */
|
300
|
2562 put_bytes(fd, 0L, 4); /* <suggestlen> */
|
236
|
2563
|
302
|
2564 spin->si_memtot = 0;
|
|
2565
|
236
|
2566 /*
|
300
|
2567 * <LWORDTREE> <KWORDTREE>
|
236
|
2568 */
|
300
|
2569 for (round = 1; round <= 2; ++round)
|
236
|
2570 {
|
300
|
2571 tree = (round == 1) ? spin->si_foldroot : spin->si_keeproot;
|
236
|
2572
|
300
|
2573 /* Count the number of nodes. Needed to be able to allocate the
|
|
2574 * memory when reading the nodes. Also fills in the index for shared
|
|
2575 * nodes. */
|
|
2576 nodecount = put_tree(NULL, tree, 0, regionmask);
|
236
|
2577
|
300
|
2578 /* number of nodes in 4 bytes */
|
|
2579 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
|
302
|
2580 spin->si_memtot += nodecount + nodecount * sizeof(int);
|
236
|
2581
|
300
|
2582 /* Write the nodes. */
|
|
2583 (void)put_tree(fd, tree, 0, regionmask);
|
236
|
2584 }
|
|
2585
|
300
|
2586 fclose(fd);
|
236
|
2587 }
|
|
2588
|
|
2589 /*
|
300
|
2590 * Dump a word tree at node "node".
|
|
2591 *
|
|
2592 * This first writes the list of possible bytes (siblings). Then for each
|
|
2593 * byte recursively write the children.
|
|
2594 *
|
|
2595 * NOTE: The code here must match the code in read_tree(), since assumptions
|
|
2596 * are made about the indexes (so that we don't have to write them in the
|
|
2597 * file).
|
236
|
2598 *
|
300
|
2599 * Returns the number of nodes used.
|
236
|
2600 */
|
300
|
2601 static int
|
|
2602 put_tree(fd, node, index, regionmask)
|
|
2603 FILE *fd; /* NULL when only counting */
|
|
2604 wordnode_T *node;
|
|
2605 int index;
|
|
2606 int regionmask;
|
236
|
2607 {
|
300
|
2608 int newindex = index;
|
|
2609 int siblingcount = 0;
|
|
2610 wordnode_T *np;
|
236
|
2611 int flags;
|
300
|
2612
|
|
2613 /* If "node" is zero the tree is empty. */
|
|
2614 if (node == NULL)
|
|
2615 return 0;
|
|
2616
|
|
2617 /* Store the index where this node is written. */
|
|
2618 node->wn_index = index;
|
236
|
2619
|
300
|
2620 /* Count the number of siblings. */
|
|
2621 for (np = node; np != NULL; np = np->wn_sibling)
|
|
2622 ++siblingcount;
|
236
|
2623
|
300
|
2624 /* Write the sibling count. */
|
|
2625 if (fd != NULL)
|
|
2626 putc(siblingcount, fd); /* <siblingcount> */
|
236
|
2627
|
300
|
2628 /* Write each sibling byte and optionally extra info. */
|
|
2629 for (np = node; np != NULL; np = np->wn_sibling)
|
236
|
2630 {
|
300
|
2631 if (np->wn_byte == 0)
|
|
2632 {
|
|
2633 if (fd != NULL)
|
|
2634 {
|
|
2635 /* For a NUL byte (end of word) instead of the byte itself
|
|
2636 * we write the flag/region items. */
|
|
2637 flags = np->wn_flags;
|
|
2638 if (regionmask != 0 && np->wn_region != regionmask)
|
|
2639 flags |= WF_REGION;
|
|
2640 if (flags == 0)
|
|
2641 {
|
|
2642 /* word without flags or region */
|
|
2643 putc(BY_NOFLAGS, fd); /* <byte> */
|
|
2644 }
|
|
2645 else
|
|
2646 {
|
|
2647 putc(BY_FLAGS, fd); /* <byte> */
|
|
2648 putc(flags, fd); /* <flags> */
|
|
2649 if (flags & WF_REGION)
|
|
2650 putc(np->wn_region, fd); /* <regionmask> */
|
|
2651 }
|
|
2652 }
|
|
2653 }
|
|
2654 else
|
|
2655 {
|
|
2656 if (np->wn_child->wn_index != 0 && np->wn_child->wn_wnode != node)
|
|
2657 {
|
|
2658 /* The child is written elsewhere, write the reference. */
|
|
2659 if (fd != NULL)
|
|
2660 {
|
|
2661 putc(BY_INDEX, fd); /* <byte> */
|
|
2662 /* <nodeidx> */
|
|
2663 put_bytes(fd, (long_u)np->wn_child->wn_index, 3);
|
|
2664 }
|
|
2665 }
|
|
2666 else if (np->wn_child->wn_wnode == NULL)
|
|
2667 /* We will write the child below and give it an index. */
|
|
2668 np->wn_child->wn_wnode = node;
|
236
|
2669
|
300
|
2670 if (fd != NULL)
|
|
2671 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
|
|
2672 {
|
|
2673 EMSG(_(e_write));
|
|
2674 return 0;
|
|
2675 }
|
|
2676 }
|
236
|
2677 }
|
|
2678
|
300
|
2679 /* Space used in the array when reading: one for each sibling and one for
|
|
2680 * the count. */
|
|
2681 newindex += siblingcount + 1;
|
249
|
2682
|
300
|
2683 /* Recursively dump the children of each sibling. */
|
|
2684 for (np = node; np != NULL; np = np->wn_sibling)
|
|
2685 if (np->wn_byte != 0 && np->wn_child->wn_wnode == node)
|
|
2686 newindex = put_tree(fd, np->wn_child, newindex, regionmask);
|
249
|
2687
|
300
|
2688 return newindex;
|
236
|
2689 }
|
|
2690
|
|
2691
|
|
2692 /*
|
|
2693 * ":mkspell outfile infile ..."
|
|
2694 */
|
|
2695 void
|
|
2696 ex_mkspell(eap)
|
|
2697 exarg_T *eap;
|
|
2698 {
|
|
2699 int fcount;
|
|
2700 char_u **fnames;
|
|
2701 char_u fname[MAXPATHL];
|
|
2702 char_u wfname[MAXPATHL];
|
|
2703 afffile_T *(afile[8]);
|
|
2704 int i;
|
|
2705 int len;
|
|
2706 char_u region_name[16];
|
|
2707 struct stat st;
|
240
|
2708 char_u *arg = eap->arg;
|
255
|
2709 int error = FALSE;
|
300
|
2710 spellinfo_T spin;
|
|
2711
|
|
2712 vim_memset(&spin, 0, sizeof(spin));
|
240
|
2713
|
|
2714 if (STRNCMP(arg, "-ascii", 6) == 0)
|
|
2715 {
|
300
|
2716 spin.si_ascii = TRUE;
|
240
|
2717 arg = skipwhite(arg + 6);
|
|
2718 }
|
|
2719
|
|
2720 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
|
|
2721 if (get_arglist_exp(arg, &fcount, &fnames) == FAIL)
|
236
|
2722 return;
|
|
2723 if (fcount < 2)
|
|
2724 EMSG(_(e_invarg)); /* need at least output and input names */
|
|
2725 else if (fcount > 9)
|
|
2726 EMSG(_("E754: Only up to 8 regions supported"));
|
|
2727 else
|
|
2728 {
|
|
2729 /* Check for overwriting before doing things that may take a lot of
|
|
2730 * time. */
|
272
|
2731 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
|
300
|
2732 spin.si_ascii ? (char_u *)"ascii" : p_enc);
|
236
|
2733 if (!eap->forceit && mch_stat((char *)wfname, &st) >= 0)
|
|
2734 {
|
|
2735 EMSG(_(e_exists));
|
|
2736 goto theend;
|
|
2737 }
|
|
2738 if (mch_isdir(fnames[0]))
|
|
2739 {
|
|
2740 EMSG2(_(e_isadir2), fnames[0]);
|
|
2741 goto theend;
|
|
2742 }
|
|
2743
|
|
2744 /*
|
|
2745 * Init the aff and dic pointers.
|
|
2746 * Get the region names if there are more than 2 arguments.
|
|
2747 */
|
|
2748 for (i = 1; i < fcount; ++i)
|
|
2749 {
|
|
2750 afile[i - 1] = NULL;
|
300
|
2751
|
236
|
2752 if (fcount > 2)
|
|
2753 {
|
|
2754 len = STRLEN(fnames[i]);
|
|
2755 if (STRLEN(gettail(fnames[i])) < 5 || fnames[i][len - 3] != '_')
|
|
2756 {
|
|
2757 EMSG2(_("E755: Invalid region in %s"), fnames[i]);
|
|
2758 goto theend;
|
|
2759 }
|
|
2760 else
|
|
2761 {
|
|
2762 region_name[(i - 1) * 2] = TOLOWER_ASC(fnames[i][len - 2]);
|
|
2763 region_name[(i - 1) * 2 + 1] =
|
|
2764 TOLOWER_ASC(fnames[i][len - 1]);
|
|
2765 }
|
|
2766 }
|
|
2767 }
|
|
2768
|
255
|
2769 /* Clear the char type tables, don't want to use any of the currently
|
|
2770 * used spell properties. */
|
|
2771 init_spell_chartab();
|
|
2772
|
300
|
2773 spin.si_foldroot = wordtree_alloc(&spin.si_blocks);
|
|
2774 spin.si_keeproot = wordtree_alloc(&spin.si_blocks);
|
|
2775 if (spin.si_foldroot == NULL || spin.si_keeproot == NULL)
|
|
2776 {
|
|
2777 error = TRUE;
|
|
2778 goto theend;
|
|
2779 }
|
|
2780
|
236
|
2781 /*
|
|
2782 * Read all the .aff and .dic files.
|
|
2783 * Text is converted to 'encoding'.
|
300
|
2784 * Words are stored in the case-folded and keep-case trees.
|
236
|
2785 */
|
300
|
2786 for (i = 1; i < fcount && !error; ++i)
|
236
|
2787 {
|
300
|
2788 spin.si_conv.vc_type = CONV_NONE;
|
|
2789 spin.si_region = 1 << (i - 1);
|
|
2790
|
272
|
2791 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", fnames[i]);
|
300
|
2792 if (mch_stat((char *)fname, &st) >= 0)
|
|
2793 {
|
|
2794 /* Read the .aff file. Will init "spin->si_conv" based on the
|
|
2795 * "SET" line. */
|
|
2796 afile[i - 1] = spell_read_aff(fname, &spin);
|
|
2797 if (afile[i - 1] == NULL)
|
|
2798 error = TRUE;
|
|
2799 else
|
|
2800 {
|
|
2801 /* Read the .dic file and store the words in the trees. */
|
|
2802 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
|
|
2803 fnames[i]);
|
|
2804 if (spell_read_dic(fname, &spin, afile[i - 1]) == FAIL)
|
|
2805 error = TRUE;
|
|
2806 }
|
|
2807 }
|
|
2808 else
|
|
2809 {
|
|
2810 /* No .aff file, try reading the file as a word list. Store
|
|
2811 * the words in the trees. */
|
|
2812 if (spell_read_wordfile(fnames[i], &spin) == FAIL)
|
|
2813 error = TRUE;
|
|
2814 }
|
236
|
2815
|
|
2816 /* Free any conversion stuff. */
|
300
|
2817 convert_setup(&spin.si_conv, NULL, NULL);
|
236
|
2818 }
|
|
2819
|
300
|
2820 if (!error)
|
236
|
2821 {
|
|
2822 /*
|
300
|
2823 * Remove the dummy NUL from the start of the tree root.
|
236
|
2824 */
|
300
|
2825 spin.si_foldroot = spin.si_foldroot->wn_sibling;
|
|
2826 spin.si_keeproot = spin.si_keeproot->wn_sibling;
|
236
|
2827
|
|
2828 /*
|
300
|
2829 * Combine tails in the tree.
|
236
|
2830 */
|
300
|
2831 MSG(_("Compressing word tree..."));
|
236
|
2832 out_flush();
|
300
|
2833 wordtree_compress(spin.si_foldroot);
|
|
2834 wordtree_compress(spin.si_keeproot);
|
236
|
2835 }
|
|
2836
|
300
|
2837 if (!error)
|
|
2838 {
|
|
2839 /*
|
|
2840 * Write the info in the spell file.
|
|
2841 */
|
|
2842 smsg((char_u *)_("Writing spell file %s..."), wfname);
|
|
2843 out_flush();
|
|
2844 write_vim_spell(wfname, &spin, fcount - 1, region_name);
|
|
2845 MSG(_("Done!"));
|
302
|
2846
|
|
2847 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
|
|
2848 spin.si_memtot);
|
300
|
2849 out_flush();
|
307
|
2850
|
|
2851 /* May need to reload spell dictionaries */
|
|
2852 spell_reload();
|
300
|
2853 }
|
|
2854
|
|
2855 /* Free the allocated memory. */
|
|
2856 free_blocks(spin.si_blocks);
|
|
2857
|
|
2858 /* Free the .aff file structures. */
|
236
|
2859 for (i = 1; i < fcount; ++i)
|
|
2860 if (afile[i - 1] != NULL)
|
|
2861 spell_free_aff(afile[i - 1]);
|
|
2862 }
|
|
2863
|
|
2864 theend:
|
|
2865 FreeWild(fcount, fnames);
|
|
2866 }
|
|
2867
|
|
2868 #endif /* FEAT_MBYTE */
|
|
2869
|
300
|
2870
|
307
|
2871 /*
|
|
2872 * Init the chartab used for spelling for ASCII.
|
|
2873 * EBCDIC is not supported!
|
|
2874 */
|
|
2875 static void
|
|
2876 clear_spell_chartab(sp)
|
|
2877 spelltab_T *sp;
|
|
2878 {
|
|
2879 int i;
|
|
2880
|
|
2881 /* Init everything to FALSE. */
|
|
2882 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
|
|
2883 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
|
|
2884 for (i = 0; i < 256; ++i)
|
|
2885 sp->st_fold[i] = i;
|
|
2886
|
|
2887 /* We include digits. A word shouldn't start with a digit, but handling
|
|
2888 * that is done separately. */
|
|
2889 for (i = '0'; i <= '9'; ++i)
|
|
2890 sp->st_isw[i] = TRUE;
|
|
2891 for (i = 'A'; i <= 'Z'; ++i)
|
|
2892 {
|
|
2893 sp->st_isw[i] = TRUE;
|
|
2894 sp->st_isu[i] = TRUE;
|
|
2895 sp->st_fold[i] = i + 0x20;
|
|
2896 }
|
|
2897 for (i = 'a'; i <= 'z'; ++i)
|
|
2898 sp->st_isw[i] = TRUE;
|
|
2899 }
|
|
2900
|
|
2901 /*
|
|
2902 * Init the chartab used for spelling. Only depends on 'encoding'.
|
|
2903 * Called once while starting up and when 'encoding' changes.
|
|
2904 * The default is to use isalpha(), but the spell file should define the word
|
|
2905 * characters to make it possible that 'encoding' differs from the current
|
|
2906 * locale.
|
|
2907 */
|
|
2908 void
|
|
2909 init_spell_chartab()
|
|
2910 {
|
|
2911 int i;
|
|
2912
|
|
2913 did_set_spelltab = FALSE;
|
|
2914 clear_spell_chartab(&spelltab);
|
|
2915
|
|
2916 #ifdef FEAT_MBYTE
|
|
2917 if (enc_dbcs)
|
|
2918 {
|
|
2919 /* DBCS: assume double-wide characters are word characters. */
|
|
2920 for (i = 128; i <= 255; ++i)
|
|
2921 if (MB_BYTE2LEN(i) == 2)
|
|
2922 spelltab.st_isw[i] = TRUE;
|
|
2923 }
|
|
2924 else
|
|
2925 #endif
|
|
2926 {
|
|
2927 /* Rough guess: use isalpha() and isupper() for characters above 128. */
|
|
2928 for (i = 128; i < 256; ++i)
|
|
2929 {
|
|
2930 spelltab.st_isw[i] = MB_ISUPPER(i) || MB_ISLOWER(i);
|
|
2931 if (MB_ISUPPER(i))
|
|
2932 {
|
|
2933 spelltab.st_isu[i] = TRUE;
|
|
2934 spelltab.st_fold[i] = MB_TOLOWER(i);
|
|
2935 }
|
|
2936 }
|
|
2937 }
|
|
2938 }
|
|
2939
|
|
2940 #if defined(FEAT_MBYTE) || defined(PROTO)
|
|
2941 static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
|
|
2942 static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
|
|
2943
|
|
2944 /*
|
|
2945 * Set the spell character tables from strings in the affix file.
|
|
2946 */
|
|
2947 static int
|
|
2948 set_spell_chartab(fol, low, upp)
|
|
2949 char_u *fol;
|
|
2950 char_u *low;
|
|
2951 char_u *upp;
|
|
2952 {
|
|
2953 /* We build the new tables here first, so that we can compare with the
|
|
2954 * previous one. */
|
|
2955 spelltab_T new_st;
|
|
2956 char_u *pf = fol, *pl = low, *pu = upp;
|
|
2957 int f, l, u;
|
|
2958
|
|
2959 clear_spell_chartab(&new_st);
|
|
2960
|
|
2961 while (*pf != NUL)
|
|
2962 {
|
|
2963 if (*pl == NUL || *pu == NUL)
|
|
2964 {
|
|
2965 EMSG(_(e_affform));
|
|
2966 return FAIL;
|
|
2967 }
|
|
2968 #ifdef FEAT_MBYTE
|
|
2969 f = mb_ptr2char_adv(&pf);
|
|
2970 l = mb_ptr2char_adv(&pl);
|
|
2971 u = mb_ptr2char_adv(&pu);
|
|
2972 #else
|
|
2973 f = *pf++;
|
|
2974 l = *pl++;
|
|
2975 u = *pu++;
|
|
2976 #endif
|
|
2977 /* Every character that appears is a word character. */
|
|
2978 if (f < 256)
|
|
2979 new_st.st_isw[f] = TRUE;
|
|
2980 if (l < 256)
|
|
2981 new_st.st_isw[l] = TRUE;
|
|
2982 if (u < 256)
|
|
2983 new_st.st_isw[u] = TRUE;
|
|
2984
|
|
2985 /* if "LOW" and "FOL" are not the same the "LOW" char needs
|
|
2986 * case-folding */
|
|
2987 if (l < 256 && l != f)
|
|
2988 {
|
|
2989 if (f >= 256)
|
|
2990 {
|
|
2991 EMSG(_(e_affrange));
|
|
2992 return FAIL;
|
|
2993 }
|
|
2994 new_st.st_fold[l] = f;
|
|
2995 }
|
|
2996
|
|
2997 /* if "UPP" and "FOL" are not the same the "UPP" char needs
|
|
2998 * case-folding and it's upper case. */
|
|
2999 if (u < 256 && u != f)
|
|
3000 {
|
|
3001 if (f >= 256)
|
|
3002 {
|
|
3003 EMSG(_(e_affrange));
|
|
3004 return FAIL;
|
|
3005 }
|
|
3006 new_st.st_fold[u] = f;
|
|
3007 new_st.st_isu[u] = TRUE;
|
|
3008 }
|
|
3009 }
|
|
3010
|
|
3011 if (*pl != NUL || *pu != NUL)
|
|
3012 {
|
|
3013 EMSG(_(e_affform));
|
|
3014 return FAIL;
|
|
3015 }
|
|
3016
|
|
3017 return set_spell_finish(&new_st);
|
|
3018 }
|
|
3019 #endif
|
|
3020
|
|
3021 /*
|
|
3022 * Set the spell character tables from strings in the .spl file.
|
|
3023 */
|
|
3024 static int
|
|
3025 set_spell_charflags(flags, cnt, upp)
|
|
3026 char_u *flags;
|
|
3027 int cnt;
|
|
3028 char_u *upp;
|
|
3029 {
|
|
3030 /* We build the new tables here first, so that we can compare with the
|
|
3031 * previous one. */
|
|
3032 spelltab_T new_st;
|
|
3033 int i;
|
|
3034 char_u *p = upp;
|
|
3035
|
|
3036 clear_spell_chartab(&new_st);
|
|
3037
|
|
3038 for (i = 0; i < cnt; ++i)
|
|
3039 {
|
|
3040 new_st.st_isw[i + 128] = (flags[i] & SPELL_ISWORD) != 0;
|
|
3041 new_st.st_isu[i + 128] = (flags[i] & SPELL_ISUPPER) != 0;
|
|
3042
|
|
3043 if (*p == NUL)
|
|
3044 return FAIL;
|
|
3045 #ifdef FEAT_MBYTE
|
|
3046 new_st.st_fold[i + 128] = mb_ptr2char_adv(&p);
|
|
3047 #else
|
|
3048 new_st.st_fold[i + 128] = *p++;
|
|
3049 #endif
|
|
3050 }
|
|
3051
|
|
3052 return set_spell_finish(&new_st);
|
|
3053 }
|
|
3054
|
|
3055 static int
|
|
3056 set_spell_finish(new_st)
|
|
3057 spelltab_T *new_st;
|
|
3058 {
|
|
3059 int i;
|
|
3060
|
|
3061 if (did_set_spelltab)
|
|
3062 {
|
|
3063 /* check that it's the same table */
|
|
3064 for (i = 0; i < 256; ++i)
|
|
3065 {
|
|
3066 if (spelltab.st_isw[i] != new_st->st_isw[i]
|
|
3067 || spelltab.st_isu[i] != new_st->st_isu[i]
|
|
3068 || spelltab.st_fold[i] != new_st->st_fold[i])
|
|
3069 {
|
|
3070 EMSG(_("E763: Word characters differ between spell files"));
|
|
3071 return FAIL;
|
|
3072 }
|
|
3073 }
|
|
3074 }
|
|
3075 else
|
|
3076 {
|
|
3077 /* copy the new spelltab into the one being used */
|
|
3078 spelltab = *new_st;
|
|
3079 did_set_spelltab = TRUE;
|
|
3080 }
|
|
3081
|
|
3082 return OK;
|
|
3083 }
|
|
3084
|
|
3085 #if defined(FEAT_MBYTE) || defined(PROTO)
|
|
3086 /*
|
|
3087 * Write the current tables into the .spl file.
|
|
3088 * This makes sure the same characters are recognized as word characters when
|
|
3089 * generating an when using a spell file.
|
|
3090 */
|
|
3091 static void
|
|
3092 write_spell_chartab(fd)
|
|
3093 FILE *fd;
|
|
3094 {
|
|
3095 char_u charbuf[256 * 4];
|
|
3096 int len = 0;
|
|
3097 int flags;
|
|
3098 int i;
|
|
3099
|
|
3100 fputc(128, fd); /* <charflagslen> */
|
|
3101 for (i = 128; i < 256; ++i)
|
|
3102 {
|
|
3103 flags = 0;
|
|
3104 if (spelltab.st_isw[i])
|
|
3105 flags |= SPELL_ISWORD;
|
|
3106 if (spelltab.st_isu[i])
|
|
3107 flags |= SPELL_ISUPPER;
|
|
3108 fputc(flags, fd); /* <charflags> */
|
|
3109
|
|
3110 len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
|
|
3111 }
|
|
3112
|
|
3113 put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
|
|
3114 fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
|
|
3115 }
|
|
3116 #endif
|
|
3117
|
|
3118 /*
|
|
3119 * Return TRUE if "c" is an upper-case character for spelling.
|
|
3120 */
|
|
3121 static int
|
|
3122 spell_isupper(c)
|
|
3123 int c;
|
|
3124 {
|
|
3125 # ifdef FEAT_MBYTE
|
|
3126 if (enc_utf8)
|
|
3127 {
|
|
3128 /* For Unicode we can call utf_isupper(), but don't do that for ASCII,
|
|
3129 * because we don't want to use 'casemap' here. */
|
|
3130 if (c >= 128)
|
|
3131 return utf_isupper(c);
|
|
3132 }
|
|
3133 else if (has_mbyte && c > 256)
|
|
3134 {
|
|
3135 /* For characters above 255 we don't have something specfied.
|
|
3136 * Fall back to locale-dependent iswupper(). If not available
|
|
3137 * simply return FALSE. */
|
|
3138 # ifdef HAVE_ISWUPPER
|
|
3139 return iswupper(c);
|
|
3140 # else
|
|
3141 return FALSE;
|
|
3142 # endif
|
|
3143 }
|
|
3144 # endif
|
|
3145 return spelltab.st_isu[c];
|
|
3146 }
|
|
3147
|
|
3148 /*
|
|
3149 * Case-fold "p[len]" into "buf[buflen]". Used for spell checking.
|
|
3150 * When using a multi-byte 'encoding' the length may change!
|
|
3151 * Returns FAIL when something wrong.
|
|
3152 */
|
|
3153 static int
|
|
3154 spell_casefold(p, len, buf, buflen)
|
|
3155 char_u *p;
|
|
3156 int len;
|
|
3157 char_u *buf;
|
|
3158 int buflen;
|
|
3159 {
|
|
3160 int i;
|
|
3161
|
|
3162 if (len >= buflen)
|
|
3163 {
|
|
3164 buf[0] = NUL;
|
|
3165 return FAIL; /* result will not fit */
|
|
3166 }
|
|
3167
|
|
3168 #ifdef FEAT_MBYTE
|
|
3169 if (has_mbyte)
|
|
3170 {
|
|
3171 int c;
|
|
3172 int outi = 0;
|
|
3173
|
|
3174 /* Fold one character at a time. */
|
|
3175 for (i = 0; i < len; i += mb_ptr2len_check(p + i))
|
|
3176 {
|
|
3177 c = mb_ptr2char(p + i);
|
|
3178 if (enc_utf8)
|
|
3179 /* For Unicode case folding is always the same, no need to use
|
|
3180 * the table from the spell file. */
|
|
3181 c = utf_fold(c);
|
|
3182 else if (c < 256)
|
|
3183 /* Use the table from the spell file. */
|
|
3184 c = spelltab.st_fold[c];
|
|
3185 # ifdef HAVE_TOWLOWER
|
|
3186 else
|
|
3187 /* We don't know what to do, fall back to towlower(), it
|
|
3188 * depends on the current locale. */
|
|
3189 c = towlower(c);
|
|
3190 # endif
|
|
3191 if (outi + MB_MAXBYTES > buflen)
|
|
3192 {
|
|
3193 buf[outi] = NUL;
|
|
3194 return FAIL;
|
|
3195 }
|
|
3196 outi += mb_char2bytes(c, buf + outi);
|
|
3197 }
|
|
3198 buf[outi] = NUL;
|
|
3199 }
|
|
3200 else
|
|
3201 #endif
|
|
3202 {
|
|
3203 /* Be quick for non-multibyte encodings. */
|
|
3204 for (i = 0; i < len; ++i)
|
|
3205 buf[i] = spelltab.st_fold[p[i]];
|
|
3206 buf[i] = NUL;
|
|
3207 }
|
|
3208
|
|
3209 return OK;
|
|
3210 }
|
|
3211
|
|
3212
|
236
|
3213 #endif /* FEAT_SYN_HL */
|