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