xref: /vim-8.2.3635/src/spell.c (revision ea2d8d25)
1 /* vi:set ts=8 sts=4 sw=4 noet:
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
12  *
13  * See spellfile.c for the Vim spell file format.
14  *
15  * The spell checking mechanism uses a tree (aka trie).  Each node in the tree
16  * has a list of bytes that can appear (siblings).  For each byte there is a
17  * pointer to the node with the byte that follows in the word (child).
18  *
19  * A NUL byte is used where the word may end.  The bytes are sorted, so that
20  * binary searching can be used and the NUL bytes are at the start.  The
21  * number of possible bytes is stored before the list of bytes.
22  *
23  * The tree uses two arrays: "byts" stores the characters, "idxs" stores
24  * either the next index or flags.  The tree starts at index 0.  For example,
25  * to lookup "vi" this sequence is followed:
26  *	i = 0
27  *	len = byts[i]
28  *	n = where "v" appears in byts[i + 1] to byts[i + len]
29  *	i = idxs[n]
30  *	len = byts[i]
31  *	n = where "i" appears in byts[i + 1] to byts[i + len]
32  *	i = idxs[n]
33  *	len = byts[i]
34  *	find that byts[i + 1] is 0, idxs[i + 1] has flags for "vi".
35  *
36  * There are two word trees: one with case-folded words and one with words in
37  * original case.  The second one is only used for keep-case words and is
38  * usually small.
39  *
40  * There is one additional tree for when not all prefixes are applied when
41  * generating the .spl file.  This tree stores all the possible prefixes, as
42  * if they were words.  At each word (prefix) end the prefix nr is stored, the
43  * following word must support this prefix nr.  And the condition nr is
44  * stored, used to lookup the condition that the word must match with.
45  *
46  * Thanks to Olaf Seibert for providing an example implementation of this tree
47  * and the compression mechanism.
48  * LZ trie ideas:
49  *	http://www.irb.hr/hr/home/ristov/papers/RistovLZtrieRevision1.pdf
50  * More papers: http://www-igm.univ-mlv.fr/~laporte/publi_en.html
51  *
52  * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
53  *
54  * Why doesn't Vim use aspell/ispell/myspell/etc.?
55  * See ":help develop-spell".
56  */
57 
58 #define IN_SPELL_C
59 #include "vim.h"
60 
61 #if defined(FEAT_SPELL) || defined(PROTO)
62 
63 #ifndef UNIX		// it's in os_unix.h for Unix
64 # include <time.h>	// for time_t
65 #endif
66 
67 #define REGION_ALL 0xff		// word valid in all regions
68 
69 #define VIMSUGMAGIC "VIMsug"	// string at start of Vim .sug file
70 #define VIMSUGMAGICL 6
71 #define VIMSUGVERSION 1
72 
73 // Result values.  Lower number is accepted over higher one.
74 #define SP_BANNED	-1
75 #define SP_OK		0
76 #define SP_RARE		1
77 #define SP_LOCAL	2
78 #define SP_BAD		3
79 
80 /*
81  * Structure to store info for word matching.
82  */
83 typedef struct matchinf_S
84 {
85     langp_T	*mi_lp;			// info for language and region
86 
87     // pointers to original text to be checked
88     char_u	*mi_word;		// start of word being checked
89     char_u	*mi_end;		// end of matching word so far
90     char_u	*mi_fend;		// next char to be added to mi_fword
91     char_u	*mi_cend;		// char after what was used for
92 					// mi_capflags
93 
94     // case-folded text
95     char_u	mi_fword[MAXWLEN + 1];	// mi_word case-folded
96     int		mi_fwordlen;		// nr of valid bytes in mi_fword
97 
98     // for when checking word after a prefix
99     int		mi_prefarridx;		// index in sl_pidxs with list of
100 					// affixID/condition
101     int		mi_prefcnt;		// number of entries at mi_prefarridx
102     int		mi_prefixlen;		// byte length of prefix
103     int		mi_cprefixlen;		// byte length of prefix in original
104 					// case
105 
106     // for when checking a compound word
107     int		mi_compoff;		// start of following word offset
108     char_u	mi_compflags[MAXWLEN];	// flags for compound words used
109     int		mi_complen;		// nr of compound words used
110     int		mi_compextra;		// nr of COMPOUNDROOT words
111 
112     // others
113     int		mi_result;		// result so far: SP_BAD, SP_OK, etc.
114     int		mi_capflags;		// WF_ONECAP WF_ALLCAP WF_KEEPCAP
115     win_T	*mi_win;		// buffer being checked
116 
117     // for NOBREAK
118     int		mi_result2;		// "mi_resul" without following word
119     char_u	*mi_end2;		// "mi_end" without following word
120 } matchinf_T;
121 
122 
123 static int spell_mb_isword_class(int cl, win_T *wp);
124 
125 // mode values for find_word
126 #define FIND_FOLDWORD	    0	// find word case-folded
127 #define FIND_KEEPWORD	    1	// find keep-case word
128 #define FIND_PREFIX	    2	// find word after prefix
129 #define FIND_COMPOUND	    3	// find case-folded compound word
130 #define FIND_KEEPCOMPOUND   4	// find keep-case compound word
131 
132 static void find_word(matchinf_T *mip, int mode);
133 static void find_prefix(matchinf_T *mip, int mode);
134 static int fold_more(matchinf_T *mip);
135 static void spell_load_cb(char_u *fname, void *cookie);
136 static int count_syllables(slang_T *slang, char_u *word);
137 static void clear_midword(win_T *buf);
138 static void use_midword(slang_T *lp, win_T *buf);
139 static int find_region(char_u *rp, char_u *region);
140 static void spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res);
141 static void spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res);
142 static void spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res);
143 static void dump_word(slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum);
144 static linenr_T dump_prefixes(slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T startlnum);
145 
146 /*
147  * Main spell-checking function.
148  * "ptr" points to a character that could be the start of a word.
149  * "*attrp" is set to the highlight index for a badly spelled word.  For a
150  * non-word or when it's OK it remains unchanged.
151  * This must only be called when 'spelllang' is not empty.
152  *
153  * "capcol" is used to check for a Capitalised word after the end of a
154  * sentence.  If it's zero then perform the check.  Return the column where to
155  * check next, or -1 when no sentence end was found.  If it's NULL then don't
156  * worry.
157  *
158  * Returns the length of the word in bytes, also when it's OK, so that the
159  * caller can skip over the word.
160  */
161     int
162 spell_check(
163     win_T	*wp,		// current window
164     char_u	*ptr,
165     hlf_T	*attrp,
166     int		*capcol,	// column to check for Capital
167     int		docount)	// count good words
168 {
169     matchinf_T	mi;		// Most things are put in "mi" so that it can
170 				// be passed to functions quickly.
171     int		nrlen = 0;	// found a number first
172     int		c;
173     int		wrongcaplen = 0;
174     int		lpi;
175     int		count_word = docount;
176     int		use_camel_case = *wp->w_s->b_p_spo != NUL;
177     int		camel_case = 0;
178 
179     // A word never starts at a space or a control character.  Return quickly
180     // then, skipping over the character.
181     if (*ptr <= ' ')
182 	return 1;
183 
184     // Return here when loading language files failed.
185     if (wp->w_s->b_langp.ga_len == 0)
186 	return 1;
187 
188     CLEAR_FIELD(mi);
189 
190     // A number is always OK.  Also skip hexadecimal numbers 0xFF99 and
191     // 0X99FF.  But always do check spelling to find "3GPP" and "11
192     // julifeest".
193     if (*ptr >= '0' && *ptr <= '9')
194     {
195 	if (*ptr == '0' && (ptr[1] == 'b' || ptr[1] == 'B'))
196 	    mi.mi_end = skipbin(ptr + 2);
197 	else if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
198 	    mi.mi_end = skiphex(ptr + 2);
199 	else
200 	    mi.mi_end = skipdigits(ptr);
201 	nrlen = (int)(mi.mi_end - ptr);
202     }
203 
204     // Find the normal end of the word (until the next non-word character).
205     mi.mi_word = ptr;
206     mi.mi_fend = ptr;
207     if (spell_iswordp(mi.mi_fend, wp))
208     {
209 	int prev_upper;
210 	int this_upper = FALSE;  // init for gcc
211 
212 	if (use_camel_case)
213 	{
214 	    c = PTR2CHAR(mi.mi_fend);
215 	    this_upper = SPELL_ISUPPER(c);
216 	}
217 
218 	do
219 	{
220 	    MB_PTR_ADV(mi.mi_fend);
221 	    if (use_camel_case)
222 	    {
223 		prev_upper = this_upper;
224 		c = PTR2CHAR(mi.mi_fend);
225 		this_upper = SPELL_ISUPPER(c);
226 		camel_case = !prev_upper && this_upper;
227 	    }
228 	} while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp)
229 							       && !camel_case);
230 
231 	if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL)
232 	{
233 	    // Check word starting with capital letter.
234 	    c = PTR2CHAR(ptr);
235 	    if (!SPELL_ISUPPER(c))
236 		wrongcaplen = (int)(mi.mi_fend - ptr);
237 	}
238     }
239     if (capcol != NULL)
240 	*capcol = -1;
241 
242     // We always use the characters up to the next non-word character,
243     // also for bad words.
244     mi.mi_end = mi.mi_fend;
245 
246     // Check caps type later.
247     mi.mi_capflags = 0;
248     mi.mi_cend = NULL;
249     mi.mi_win = wp;
250 
251     // case-fold the word with one non-word character, so that we can check
252     // for the word end.
253     if (*mi.mi_fend != NUL)
254 	MB_PTR_ADV(mi.mi_fend);
255 
256     (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
257 							     MAXWLEN + 1);
258     mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
259 
260     if (camel_case)
261 	// Introduce a fake word end space into the folded word.
262 	mi.mi_fword[mi.mi_fwordlen - 1] = ' ';
263 
264     // The word is bad unless we recognize it.
265     mi.mi_result = SP_BAD;
266     mi.mi_result2 = SP_BAD;
267 
268     /*
269      * Loop over the languages specified in 'spelllang'.
270      * We check them all, because a word may be matched longer in another
271      * language.
272      */
273     for (lpi = 0; lpi < wp->w_s->b_langp.ga_len; ++lpi)
274     {
275 	mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, lpi);
276 
277 	// If reloading fails the language is still in the list but everything
278 	// has been cleared.
279 	if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
280 	    continue;
281 
282 	// Check for a matching word in case-folded words.
283 	find_word(&mi, FIND_FOLDWORD);
284 
285 	// Check for a matching word in keep-case words.
286 	find_word(&mi, FIND_KEEPWORD);
287 
288 	// Check for matching prefixes.
289 	find_prefix(&mi, FIND_FOLDWORD);
290 
291 	// For a NOBREAK language, may want to use a word without a following
292 	// word as a backup.
293 	if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
294 						   && mi.mi_result2 != SP_BAD)
295 	{
296 	    mi.mi_result = mi.mi_result2;
297 	    mi.mi_end = mi.mi_end2;
298 	}
299 
300 	// Count the word in the first language where it's found to be OK.
301 	if (count_word && mi.mi_result == SP_OK)
302 	{
303 	    count_common_word(mi.mi_lp->lp_slang, ptr,
304 						   (int)(mi.mi_end - ptr), 1);
305 	    count_word = FALSE;
306 	}
307     }
308 
309     if (mi.mi_result != SP_OK)
310     {
311 	// If we found a number skip over it.  Allows for "42nd".  Do flag
312 	// rare and local words, e.g., "3GPP".
313 	if (nrlen > 0)
314 	{
315 	    if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
316 		return nrlen;
317 	}
318 
319 	// When we are at a non-word character there is no error, just
320 	// skip over the character (try looking for a word after it).
321 	else if (!spell_iswordp_nmw(ptr, wp))
322 	{
323 	    if (capcol != NULL && wp->w_s->b_cap_prog != NULL)
324 	    {
325 		regmatch_T	regmatch;
326 		int		r;
327 
328 		// Check for end of sentence.
329 		regmatch.regprog = wp->w_s->b_cap_prog;
330 		regmatch.rm_ic = FALSE;
331 		r = vim_regexec(&regmatch, ptr, 0);
332 		wp->w_s->b_cap_prog = regmatch.regprog;
333 		if (r)
334 		    *capcol = (int)(regmatch.endp[0] - ptr);
335 	    }
336 
337 	    if (has_mbyte)
338 		return (*mb_ptr2len)(ptr);
339 	    return 1;
340 	}
341 	else if (mi.mi_end == ptr)
342 	    // Always include at least one character.  Required for when there
343 	    // is a mixup in "midword".
344 	    MB_PTR_ADV(mi.mi_end);
345 	else if (mi.mi_result == SP_BAD
346 		&& LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak)
347 	{
348 	    char_u	*p, *fp;
349 	    int		save_result = mi.mi_result;
350 
351 	    // First language in 'spelllang' is NOBREAK.  Find first position
352 	    // at which any word would be valid.
353 	    mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, 0);
354 	    if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
355 	    {
356 		p = mi.mi_word;
357 		fp = mi.mi_fword;
358 		for (;;)
359 		{
360 		    MB_PTR_ADV(p);
361 		    MB_PTR_ADV(fp);
362 		    if (p >= mi.mi_end)
363 			break;
364 		    mi.mi_compoff = (int)(fp - mi.mi_fword);
365 		    find_word(&mi, FIND_COMPOUND);
366 		    if (mi.mi_result != SP_BAD)
367 		    {
368 			mi.mi_end = p;
369 			break;
370 		    }
371 		}
372 		mi.mi_result = save_result;
373 	    }
374 	}
375 
376 	if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
377 	    *attrp = HLF_SPB;
378 	else if (mi.mi_result == SP_RARE)
379 	    *attrp = HLF_SPR;
380 	else
381 	    *attrp = HLF_SPL;
382     }
383 
384     if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
385     {
386 	// Report SpellCap only when the word isn't badly spelled.
387 	*attrp = HLF_SPC;
388 	return wrongcaplen;
389     }
390 
391     return (int)(mi.mi_end - ptr);
392 }
393 
394 /*
395  * Check if the word at "mip->mi_word" is in the tree.
396  * When "mode" is FIND_FOLDWORD check in fold-case word tree.
397  * When "mode" is FIND_KEEPWORD check in keep-case word tree.
398  * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
399  * tree.
400  *
401  * For a match mip->mi_result is updated.
402  */
403     static void
404 find_word(matchinf_T *mip, int mode)
405 {
406     idx_T	arridx = 0;
407     int		endlen[MAXWLEN];    // length at possible word endings
408     idx_T	endidx[MAXWLEN];    // possible word endings
409     int		endidxcnt = 0;
410     int		len;
411     int		wlen = 0;
412     int		flen;
413     int		c;
414     char_u	*ptr;
415     idx_T	lo, hi, m;
416     char_u	*s;
417     char_u	*p;
418     int		res = SP_BAD;
419     slang_T	*slang = mip->mi_lp->lp_slang;
420     unsigned	flags;
421     char_u	*byts;
422     idx_T	*idxs;
423     int		word_ends;
424     int		prefix_found;
425     int		nobreak_result;
426 
427     if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
428     {
429 	// Check for word with matching case in keep-case tree.
430 	ptr = mip->mi_word;
431 	flen = 9999;		    // no case folding, always enough bytes
432 	byts = slang->sl_kbyts;
433 	idxs = slang->sl_kidxs;
434 
435 	if (mode == FIND_KEEPCOMPOUND)
436 	    // Skip over the previously found word(s).
437 	    wlen += mip->mi_compoff;
438     }
439     else
440     {
441 	// Check for case-folded in case-folded tree.
442 	ptr = mip->mi_fword;
443 	flen = mip->mi_fwordlen;    // available case-folded bytes
444 	byts = slang->sl_fbyts;
445 	idxs = slang->sl_fidxs;
446 
447 	if (mode == FIND_PREFIX)
448 	{
449 	    // Skip over the prefix.
450 	    wlen = mip->mi_prefixlen;
451 	    flen -= mip->mi_prefixlen;
452 	}
453 	else if (mode == FIND_COMPOUND)
454 	{
455 	    // Skip over the previously found word(s).
456 	    wlen = mip->mi_compoff;
457 	    flen -= mip->mi_compoff;
458 	}
459 
460     }
461 
462     if (byts == NULL)
463 	return;			// array is empty
464 
465     /*
466      * Repeat advancing in the tree until:
467      * - there is a byte that doesn't match,
468      * - we reach the end of the tree,
469      * - or we reach the end of the line.
470      */
471     for (;;)
472     {
473 	if (flen <= 0 && *mip->mi_fend != NUL)
474 	    flen = fold_more(mip);
475 
476 	len = byts[arridx++];
477 
478 	// If the first possible byte is a zero the word could end here.
479 	// Remember this index, we first check for the longest word.
480 	if (byts[arridx] == 0)
481 	{
482 	    if (endidxcnt == MAXWLEN)
483 	    {
484 		// Must be a corrupted spell file.
485 		emsg(_(e_format));
486 		return;
487 	    }
488 	    endlen[endidxcnt] = wlen;
489 	    endidx[endidxcnt++] = arridx++;
490 	    --len;
491 
492 	    // Skip over the zeros, there can be several flag/region
493 	    // combinations.
494 	    while (len > 0 && byts[arridx] == 0)
495 	    {
496 		++arridx;
497 		--len;
498 	    }
499 	    if (len == 0)
500 		break;	    // no children, word must end here
501 	}
502 
503 	// Stop looking at end of the line.
504 	if (ptr[wlen] == NUL)
505 	    break;
506 
507 	// Perform a binary search in the list of accepted bytes.
508 	c = ptr[wlen];
509 	if (c == TAB)	    // <Tab> is handled like <Space>
510 	    c = ' ';
511 	lo = arridx;
512 	hi = arridx + len - 1;
513 	while (lo < hi)
514 	{
515 	    m = (lo + hi) / 2;
516 	    if (byts[m] > c)
517 		hi = m - 1;
518 	    else if (byts[m] < c)
519 		lo = m + 1;
520 	    else
521 	    {
522 		lo = hi = m;
523 		break;
524 	    }
525 	}
526 
527 	// Stop if there is no matching byte.
528 	if (hi < lo || byts[lo] != c)
529 	    break;
530 
531 	// Continue at the child (if there is one).
532 	arridx = idxs[lo];
533 	++wlen;
534 	--flen;
535 
536 	// One space in the good word may stand for several spaces in the
537 	// checked word.
538 	if (c == ' ')
539 	{
540 	    for (;;)
541 	    {
542 		if (flen <= 0 && *mip->mi_fend != NUL)
543 		    flen = fold_more(mip);
544 		if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
545 		    break;
546 		++wlen;
547 		--flen;
548 	    }
549 	}
550     }
551 
552     /*
553      * Verify that one of the possible endings is valid.  Try the longest
554      * first.
555      */
556     while (endidxcnt > 0)
557     {
558 	--endidxcnt;
559 	arridx = endidx[endidxcnt];
560 	wlen = endlen[endidxcnt];
561 
562 	if ((*mb_head_off)(ptr, ptr + wlen) > 0)
563 	    continue;	    // not at first byte of character
564 	if (spell_iswordp(ptr + wlen, mip->mi_win))
565 	{
566 	    if (slang->sl_compprog == NULL && !slang->sl_nobreak)
567 		continue;	    // next char is a word character
568 	    word_ends = FALSE;
569 	}
570 	else
571 	    word_ends = TRUE;
572 	// The prefix flag is before compound flags.  Once a valid prefix flag
573 	// has been found we try compound flags.
574 	prefix_found = FALSE;
575 
576 	if (mode != FIND_KEEPWORD && has_mbyte)
577 	{
578 	    // Compute byte length in original word, length may change
579 	    // when folding case.  This can be slow, take a shortcut when the
580 	    // case-folded word is equal to the keep-case word.
581 	    p = mip->mi_word;
582 	    if (STRNCMP(ptr, p, wlen) != 0)
583 	    {
584 		for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s))
585 		    MB_PTR_ADV(p);
586 		wlen = (int)(p - mip->mi_word);
587 	    }
588 	}
589 
590 	// Check flags and region.  For FIND_PREFIX check the condition and
591 	// prefix ID.
592 	// Repeat this if there are more flags/region alternatives until there
593 	// is a match.
594 	res = SP_BAD;
595 	for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
596 							      --len, ++arridx)
597 	{
598 	    flags = idxs[arridx];
599 
600 	    // For the fold-case tree check that the case of the checked word
601 	    // matches with what the word in the tree requires.
602 	    // For keep-case tree the case is always right.  For prefixes we
603 	    // don't bother to check.
604 	    if (mode == FIND_FOLDWORD)
605 	    {
606 		if (mip->mi_cend != mip->mi_word + wlen)
607 		{
608 		    // mi_capflags was set for a different word length, need
609 		    // to do it again.
610 		    mip->mi_cend = mip->mi_word + wlen;
611 		    mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
612 		}
613 
614 		if (mip->mi_capflags == WF_KEEPCAP
615 				|| !spell_valid_case(mip->mi_capflags, flags))
616 		    continue;
617 	    }
618 
619 	    // When mode is FIND_PREFIX the word must support the prefix:
620 	    // check the prefix ID and the condition.  Do that for the list at
621 	    // mip->mi_prefarridx that find_prefix() filled.
622 	    else if (mode == FIND_PREFIX && !prefix_found)
623 	    {
624 		c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
625 				    flags,
626 				    mip->mi_word + mip->mi_cprefixlen, slang,
627 				    FALSE);
628 		if (c == 0)
629 		    continue;
630 
631 		// Use the WF_RARE flag for a rare prefix.
632 		if (c & WF_RAREPFX)
633 		    flags |= WF_RARE;
634 		prefix_found = TRUE;
635 	    }
636 
637 	    if (slang->sl_nobreak)
638 	    {
639 		if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
640 			&& (flags & WF_BANNED) == 0)
641 		{
642 		    // NOBREAK: found a valid following word.  That's all we
643 		    // need to know, so return.
644 		    mip->mi_result = SP_OK;
645 		    break;
646 		}
647 	    }
648 
649 	    else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
650 								|| !word_ends))
651 	    {
652 		// If there is no compound flag or the word is shorter than
653 		// COMPOUNDMIN reject it quickly.
654 		// Makes you wonder why someone puts a compound flag on a word
655 		// that's too short...  Myspell compatibility requires this
656 		// anyway.
657 		if (((unsigned)flags >> 24) == 0
658 			     || wlen - mip->mi_compoff < slang->sl_compminlen)
659 		    continue;
660 		// For multi-byte chars check character length against
661 		// COMPOUNDMIN.
662 		if (has_mbyte
663 			&& slang->sl_compminlen > 0
664 			&& mb_charlen_len(mip->mi_word + mip->mi_compoff,
665 				wlen - mip->mi_compoff) < slang->sl_compminlen)
666 			continue;
667 
668 		// Limit the number of compound words to COMPOUNDWORDMAX if no
669 		// maximum for syllables is specified.
670 		if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
671 							   > slang->sl_compmax
672 					   && slang->sl_compsylmax == MAXWLEN)
673 		    continue;
674 
675 		// Don't allow compounding on a side where an affix was added,
676 		// unless COMPOUNDPERMITFLAG was used.
677 		if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
678 		    continue;
679 		if (!word_ends && (flags & WF_NOCOMPAFT))
680 		    continue;
681 
682 		// Quickly check if compounding is possible with this flag.
683 		if (!byte_in_str(mip->mi_complen == 0
684 					? slang->sl_compstartflags
685 					: slang->sl_compallflags,
686 					    ((unsigned)flags >> 24)))
687 		    continue;
688 
689 		// If there is a match with a CHECKCOMPOUNDPATTERN rule
690 		// discard the compound word.
691 		if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat))
692 		    continue;
693 
694 		if (mode == FIND_COMPOUND)
695 		{
696 		    int	    capflags;
697 
698 		    // Need to check the caps type of the appended compound
699 		    // word.
700 		    if (has_mbyte && STRNCMP(ptr, mip->mi_word,
701 							mip->mi_compoff) != 0)
702 		    {
703 			// case folding may have changed the length
704 			p = mip->mi_word;
705 			for (s = ptr; s < ptr + mip->mi_compoff; MB_PTR_ADV(s))
706 			    MB_PTR_ADV(p);
707 		    }
708 		    else
709 			p = mip->mi_word + mip->mi_compoff;
710 		    capflags = captype(p, mip->mi_word + wlen);
711 		    if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
712 						 && (flags & WF_FIXCAP) != 0))
713 			continue;
714 
715 		    if (capflags != WF_ALLCAP)
716 		    {
717 			// When the character before the word is a word
718 			// character we do not accept a Onecap word.  We do
719 			// accept a no-caps word, even when the dictionary
720 			// word specifies ONECAP.
721 			MB_PTR_BACK(mip->mi_word, p);
722 			if (spell_iswordp_nmw(p, mip->mi_win)
723 				? capflags == WF_ONECAP
724 				: (flags & WF_ONECAP) != 0
725 						     && capflags != WF_ONECAP)
726 			    continue;
727 		    }
728 		}
729 
730 		// If the word ends the sequence of compound flags of the
731 		// words must match with one of the COMPOUNDRULE items and
732 		// the number of syllables must not be too large.
733 		mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
734 		mip->mi_compflags[mip->mi_complen + 1] = NUL;
735 		if (word_ends)
736 		{
737 		    char_u	fword[MAXWLEN];
738 
739 		    if (slang->sl_compsylmax < MAXWLEN)
740 		    {
741 			// "fword" is only needed for checking syllables.
742 			if (ptr == mip->mi_word)
743 			    (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
744 			else
745 			    vim_strncpy(fword, ptr, endlen[endidxcnt]);
746 		    }
747 		    if (!can_compound(slang, fword, mip->mi_compflags))
748 			continue;
749 		}
750 		else if (slang->sl_comprules != NULL
751 			     && !match_compoundrule(slang, mip->mi_compflags))
752 		    // The compound flags collected so far do not match any
753 		    // COMPOUNDRULE, discard the compounded word.
754 		    continue;
755 	    }
756 
757 	    // Check NEEDCOMPOUND: can't use word without compounding.
758 	    else if (flags & WF_NEEDCOMP)
759 		continue;
760 
761 	    nobreak_result = SP_OK;
762 
763 	    if (!word_ends)
764 	    {
765 		int	save_result = mip->mi_result;
766 		char_u	*save_end = mip->mi_end;
767 		langp_T	*save_lp = mip->mi_lp;
768 		int	lpi;
769 
770 		// Check that a valid word follows.  If there is one and we
771 		// are compounding, it will set "mi_result", thus we are
772 		// always finished here.  For NOBREAK we only check that a
773 		// valid word follows.
774 		// Recursive!
775 		if (slang->sl_nobreak)
776 		    mip->mi_result = SP_BAD;
777 
778 		// Find following word in case-folded tree.
779 		mip->mi_compoff = endlen[endidxcnt];
780 		if (has_mbyte && mode == FIND_KEEPWORD)
781 		{
782 		    // Compute byte length in case-folded word from "wlen":
783 		    // byte length in keep-case word.  Length may change when
784 		    // folding case.  This can be slow, take a shortcut when
785 		    // the case-folded word is equal to the keep-case word.
786 		    p = mip->mi_fword;
787 		    if (STRNCMP(ptr, p, wlen) != 0)
788 		    {
789 			for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s))
790 			    MB_PTR_ADV(p);
791 			mip->mi_compoff = (int)(p - mip->mi_fword);
792 		    }
793 		}
794 #if 0 // Disabled, see below
795 		c = mip->mi_compoff;
796 #endif
797 		++mip->mi_complen;
798 		if (flags & WF_COMPROOT)
799 		    ++mip->mi_compextra;
800 
801 		// For NOBREAK we need to try all NOBREAK languages, at least
802 		// to find the ".add" file(s).
803 		for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi)
804 		{
805 		    if (slang->sl_nobreak)
806 		    {
807 			mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi);
808 			if (mip->mi_lp->lp_slang->sl_fidxs == NULL
809 					 || !mip->mi_lp->lp_slang->sl_nobreak)
810 			    continue;
811 		    }
812 
813 		    find_word(mip, FIND_COMPOUND);
814 
815 		    // When NOBREAK any word that matches is OK.  Otherwise we
816 		    // need to find the longest match, thus try with keep-case
817 		    // and prefix too.
818 		    if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
819 		    {
820 			// Find following word in keep-case tree.
821 			mip->mi_compoff = wlen;
822 			find_word(mip, FIND_KEEPCOMPOUND);
823 
824 #if 0	    // Disabled, a prefix must not appear halfway a compound word,
825 	    // unless the COMPOUNDPERMITFLAG is used and then it can't be a
826 	    // postponed prefix.
827 			if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
828 			{
829 			    // Check for following word with prefix.
830 			    mip->mi_compoff = c;
831 			    find_prefix(mip, FIND_COMPOUND);
832 			}
833 #endif
834 		    }
835 
836 		    if (!slang->sl_nobreak)
837 			break;
838 		}
839 		--mip->mi_complen;
840 		if (flags & WF_COMPROOT)
841 		    --mip->mi_compextra;
842 		mip->mi_lp = save_lp;
843 
844 		if (slang->sl_nobreak)
845 		{
846 		    nobreak_result = mip->mi_result;
847 		    mip->mi_result = save_result;
848 		    mip->mi_end = save_end;
849 		}
850 		else
851 		{
852 		    if (mip->mi_result == SP_OK)
853 			break;
854 		    continue;
855 		}
856 	    }
857 
858 	    if (flags & WF_BANNED)
859 		res = SP_BANNED;
860 	    else if (flags & WF_REGION)
861 	    {
862 		// Check region.
863 		if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
864 		    res = SP_OK;
865 		else
866 		    res = SP_LOCAL;
867 	    }
868 	    else if (flags & WF_RARE)
869 		res = SP_RARE;
870 	    else
871 		res = SP_OK;
872 
873 	    // Always use the longest match and the best result.  For NOBREAK
874 	    // we separately keep the longest match without a following good
875 	    // word as a fall-back.
876 	    if (nobreak_result == SP_BAD)
877 	    {
878 		if (mip->mi_result2 > res)
879 		{
880 		    mip->mi_result2 = res;
881 		    mip->mi_end2 = mip->mi_word + wlen;
882 		}
883 		else if (mip->mi_result2 == res
884 					&& mip->mi_end2 < mip->mi_word + wlen)
885 		    mip->mi_end2 = mip->mi_word + wlen;
886 	    }
887 	    else if (mip->mi_result > res)
888 	    {
889 		mip->mi_result = res;
890 		mip->mi_end = mip->mi_word + wlen;
891 	    }
892 	    else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
893 		mip->mi_end = mip->mi_word + wlen;
894 
895 	    if (mip->mi_result == SP_OK)
896 		break;
897 	}
898 
899 	if (mip->mi_result == SP_OK)
900 	    break;
901     }
902 }
903 
904 /*
905  * Return TRUE if there is a match between the word ptr[wlen] and
906  * CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another
907  * word.
908  * A match means that the first part of CHECKCOMPOUNDPATTERN matches at the
909  * end of ptr[wlen] and the second part matches after it.
910  */
911     int
912 match_checkcompoundpattern(
913     char_u	*ptr,
914     int		wlen,
915     garray_T	*gap)  // &sl_comppat
916 {
917     int		i;
918     char_u	*p;
919     int		len;
920 
921     for (i = 0; i + 1 < gap->ga_len; i += 2)
922     {
923 	p = ((char_u **)gap->ga_data)[i + 1];
924 	if (STRNCMP(ptr + wlen, p, STRLEN(p)) == 0)
925 	{
926 	    // Second part matches at start of following compound word, now
927 	    // check if first part matches at end of previous word.
928 	    p = ((char_u **)gap->ga_data)[i];
929 	    len = (int)STRLEN(p);
930 	    if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0)
931 		return TRUE;
932 	}
933     }
934     return FALSE;
935 }
936 
937 /*
938  * Return TRUE if "flags" is a valid sequence of compound flags and "word"
939  * does not have too many syllables.
940  */
941     int
942 can_compound(slang_T *slang, char_u *word, char_u *flags)
943 {
944     char_u	uflags[MAXWLEN * 2];
945     int		i;
946     char_u	*p;
947 
948     if (slang->sl_compprog == NULL)
949 	return FALSE;
950     if (enc_utf8)
951     {
952 	// Need to convert the single byte flags to utf8 characters.
953 	p = uflags;
954 	for (i = 0; flags[i] != NUL; ++i)
955 	    p += utf_char2bytes(flags[i], p);
956 	*p = NUL;
957 	p = uflags;
958     }
959     else
960 	p = flags;
961     if (!vim_regexec_prog(&slang->sl_compprog, FALSE, p, 0))
962 	return FALSE;
963 
964     // Count the number of syllables.  This may be slow, do it last.  If there
965     // are too many syllables AND the number of compound words is above
966     // COMPOUNDWORDMAX then compounding is not allowed.
967     if (slang->sl_compsylmax < MAXWLEN
968 		       && count_syllables(slang, word) > slang->sl_compsylmax)
969 	return (int)STRLEN(flags) < slang->sl_compmax;
970     return TRUE;
971 }
972 
973 /*
974  * Return TRUE if the compound flags in compflags[] match the start of any
975  * compound rule.  This is used to stop trying a compound if the flags
976  * collected so far can't possibly match any compound rule.
977  * Caller must check that slang->sl_comprules is not NULL.
978  */
979     int
980 match_compoundrule(slang_T *slang, char_u *compflags)
981 {
982     char_u	*p;
983     int		i;
984     int		c;
985 
986     // loop over all the COMPOUNDRULE entries
987     for (p = slang->sl_comprules; *p != NUL; ++p)
988     {
989 	// loop over the flags in the compound word we have made, match
990 	// them against the current rule entry
991 	for (i = 0; ; ++i)
992 	{
993 	    c = compflags[i];
994 	    if (c == NUL)
995 		// found a rule that matches for the flags we have so far
996 		return TRUE;
997 	    if (*p == '/' || *p == NUL)
998 		break;  // end of rule, it's too short
999 	    if (*p == '[')
1000 	    {
1001 		int match = FALSE;
1002 
1003 		// compare against all the flags in []
1004 		++p;
1005 		while (*p != ']' && *p != NUL)
1006 		    if (*p++ == c)
1007 			match = TRUE;
1008 		if (!match)
1009 		    break;  // none matches
1010 	    }
1011 	    else if (*p != c)
1012 		break;  // flag of word doesn't match flag in pattern
1013 	    ++p;
1014 	}
1015 
1016 	// Skip to the next "/", where the next pattern starts.
1017 	p = vim_strchr(p, '/');
1018 	if (p == NULL)
1019 	    break;
1020     }
1021 
1022     // Checked all the rules and none of them match the flags, so there
1023     // can't possibly be a compound starting with these flags.
1024     return FALSE;
1025 }
1026 
1027 /*
1028  * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1029  * ID in "flags" for the word "word".
1030  * The WF_RAREPFX flag is included in the return value for a rare prefix.
1031  */
1032     int
1033 valid_word_prefix(
1034     int		totprefcnt,	// nr of prefix IDs
1035     int		arridx,		// idx in sl_pidxs[]
1036     int		flags,
1037     char_u	*word,
1038     slang_T	*slang,
1039     int		cond_req)	// only use prefixes with a condition
1040 {
1041     int		prefcnt;
1042     int		pidx;
1043     regprog_T	**rp;
1044     int		prefid;
1045 
1046     prefid = (unsigned)flags >> 24;
1047     for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1048     {
1049 	pidx = slang->sl_pidxs[arridx + prefcnt];
1050 
1051 	// Check the prefix ID.
1052 	if (prefid != (pidx & 0xff))
1053 	    continue;
1054 
1055 	// Check if the prefix doesn't combine and the word already has a
1056 	// suffix.
1057 	if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1058 	    continue;
1059 
1060 	// Check the condition, if there is one.  The condition index is
1061 	// stored in the two bytes above the prefix ID byte.
1062 	rp = &slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
1063 	if (*rp != NULL)
1064 	{
1065 	    if (!vim_regexec_prog(rp, FALSE, word, 0))
1066 		continue;
1067 	}
1068 	else if (cond_req)
1069 	    continue;
1070 
1071 	// It's a match!  Return the WF_ flags.
1072 	return pidx;
1073     }
1074     return 0;
1075 }
1076 
1077 /*
1078  * Check if the word at "mip->mi_word" has a matching prefix.
1079  * If it does, then check the following word.
1080  *
1081  * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1082  * prefix in a compound word.
1083  *
1084  * For a match mip->mi_result is updated.
1085  */
1086     static void
1087 find_prefix(matchinf_T *mip, int mode)
1088 {
1089     idx_T	arridx = 0;
1090     int		len;
1091     int		wlen = 0;
1092     int		flen;
1093     int		c;
1094     char_u	*ptr;
1095     idx_T	lo, hi, m;
1096     slang_T	*slang = mip->mi_lp->lp_slang;
1097     char_u	*byts;
1098     idx_T	*idxs;
1099 
1100     byts = slang->sl_pbyts;
1101     if (byts == NULL)
1102 	return;			// array is empty
1103 
1104     // We use the case-folded word here, since prefixes are always
1105     // case-folded.
1106     ptr = mip->mi_fword;
1107     flen = mip->mi_fwordlen;    // available case-folded bytes
1108     if (mode == FIND_COMPOUND)
1109     {
1110 	// Skip over the previously found word(s).
1111 	ptr += mip->mi_compoff;
1112 	flen -= mip->mi_compoff;
1113     }
1114     idxs = slang->sl_pidxs;
1115 
1116     /*
1117      * Repeat advancing in the tree until:
1118      * - there is a byte that doesn't match,
1119      * - we reach the end of the tree,
1120      * - or we reach the end of the line.
1121      */
1122     for (;;)
1123     {
1124 	if (flen == 0 && *mip->mi_fend != NUL)
1125 	    flen = fold_more(mip);
1126 
1127 	len = byts[arridx++];
1128 
1129 	// If the first possible byte is a zero the prefix could end here.
1130 	// Check if the following word matches and supports the prefix.
1131 	if (byts[arridx] == 0)
1132 	{
1133 	    // There can be several prefixes with different conditions.  We
1134 	    // try them all, since we don't know which one will give the
1135 	    // longest match.  The word is the same each time, pass the list
1136 	    // of possible prefixes to find_word().
1137 	    mip->mi_prefarridx = arridx;
1138 	    mip->mi_prefcnt = len;
1139 	    while (len > 0 && byts[arridx] == 0)
1140 	    {
1141 		++arridx;
1142 		--len;
1143 	    }
1144 	    mip->mi_prefcnt -= len;
1145 
1146 	    // Find the word that comes after the prefix.
1147 	    mip->mi_prefixlen = wlen;
1148 	    if (mode == FIND_COMPOUND)
1149 		// Skip over the previously found word(s).
1150 		mip->mi_prefixlen += mip->mi_compoff;
1151 
1152 	    if (has_mbyte)
1153 	    {
1154 		// Case-folded length may differ from original length.
1155 		mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1156 					     mip->mi_prefixlen, mip->mi_word);
1157 	    }
1158 	    else
1159 		mip->mi_cprefixlen = mip->mi_prefixlen;
1160 	    find_word(mip, FIND_PREFIX);
1161 
1162 
1163 	    if (len == 0)
1164 		break;	    // no children, word must end here
1165 	}
1166 
1167 	// Stop looking at end of the line.
1168 	if (ptr[wlen] == NUL)
1169 	    break;
1170 
1171 	// Perform a binary search in the list of accepted bytes.
1172 	c = ptr[wlen];
1173 	lo = arridx;
1174 	hi = arridx + len - 1;
1175 	while (lo < hi)
1176 	{
1177 	    m = (lo + hi) / 2;
1178 	    if (byts[m] > c)
1179 		hi = m - 1;
1180 	    else if (byts[m] < c)
1181 		lo = m + 1;
1182 	    else
1183 	    {
1184 		lo = hi = m;
1185 		break;
1186 	    }
1187 	}
1188 
1189 	// Stop if there is no matching byte.
1190 	if (hi < lo || byts[lo] != c)
1191 	    break;
1192 
1193 	// Continue at the child (if there is one).
1194 	arridx = idxs[lo];
1195 	++wlen;
1196 	--flen;
1197     }
1198 }
1199 
1200 /*
1201  * Need to fold at least one more character.  Do until next non-word character
1202  * for efficiency.  Include the non-word character too.
1203  * Return the length of the folded chars in bytes.
1204  */
1205     static int
1206 fold_more(matchinf_T *mip)
1207 {
1208     int		flen;
1209     char_u	*p;
1210 
1211     p = mip->mi_fend;
1212     do
1213 	MB_PTR_ADV(mip->mi_fend);
1214     while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win));
1215 
1216     // Include the non-word character so that we can check for the word end.
1217     if (*mip->mi_fend != NUL)
1218 	MB_PTR_ADV(mip->mi_fend);
1219 
1220     (void)spell_casefold(p, (int)(mip->mi_fend - p),
1221 			     mip->mi_fword + mip->mi_fwordlen,
1222 			     MAXWLEN - mip->mi_fwordlen);
1223     flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
1224     mip->mi_fwordlen += flen;
1225     return flen;
1226 }
1227 
1228 /*
1229  * Check case flags for a word.  Return TRUE if the word has the requested
1230  * case.
1231  */
1232     int
1233 spell_valid_case(
1234     int	    wordflags,	    // flags for the checked word.
1235     int	    treeflags)	    // flags for the word in the spell tree
1236 {
1237     return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
1238 	    || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
1239 		&& ((treeflags & WF_ONECAP) == 0
1240 					   || (wordflags & WF_ONECAP) != 0)));
1241 }
1242 
1243 /*
1244  * Return TRUE if spell checking is not enabled.
1245  */
1246     int
1247 no_spell_checking(win_T *wp)
1248 {
1249     if (!wp->w_p_spell || *wp->w_s->b_p_spl == NUL
1250 					 || wp->w_s->b_langp.ga_len == 0)
1251     {
1252 	emsg(_(e_no_spell));
1253 	return TRUE;
1254     }
1255     return FALSE;
1256 }
1257 
1258 /*
1259  * Move to next spell error.
1260  * "curline" is FALSE for "[s", "]s", "[S" and "]S".
1261  * "curline" is TRUE to find word under/after cursor in the same line.
1262  * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1263  * to after badly spelled word before the cursor.
1264  * Return 0 if not found, length of the badly spelled word otherwise.
1265  */
1266     int
1267 spell_move_to(
1268     win_T	*wp,
1269     int		dir,		// FORWARD or BACKWARD
1270     int		allwords,	// TRUE for "[s"/"]s", FALSE for "[S"/"]S"
1271     int		curline,
1272     hlf_T	*attrp)		// return: attributes of bad word or NULL
1273 				// (only when "dir" is FORWARD)
1274 {
1275     linenr_T	lnum;
1276     pos_T	found_pos;
1277     int		found_len = 0;
1278     char_u	*line;
1279     char_u	*p;
1280     char_u	*endp;
1281     hlf_T	attr;
1282     int		len;
1283 #ifdef FEAT_SYN_HL
1284     int		has_syntax = syntax_present(wp);
1285 #endif
1286     int		col;
1287     int		can_spell;
1288     char_u	*buf = NULL;
1289     int		buflen = 0;
1290     int		skip = 0;
1291     int		capcol = -1;
1292     int		found_one = FALSE;
1293     int		wrapped = FALSE;
1294 
1295     if (no_spell_checking(wp))
1296 	return 0;
1297 
1298     /*
1299      * Start looking for bad word at the start of the line, because we can't
1300      * start halfway a word, we don't know where it starts or ends.
1301      *
1302      * When searching backwards, we continue in the line to find the last
1303      * bad word (in the cursor line: before the cursor).
1304      *
1305      * We concatenate the start of the next line, so that wrapped words work
1306      * (e.g. "et<line-break>cetera").  Doesn't work when searching backwards
1307      * though...
1308      */
1309     lnum = wp->w_cursor.lnum;
1310     CLEAR_POS(&found_pos);
1311 
1312     while (!got_int)
1313     {
1314 	line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1315 
1316 	len = (int)STRLEN(line);
1317 	if (buflen < len + MAXWLEN + 2)
1318 	{
1319 	    vim_free(buf);
1320 	    buflen = len + MAXWLEN + 2;
1321 	    buf = alloc(buflen);
1322 	    if (buf == NULL)
1323 		break;
1324 	}
1325 
1326 	// In first line check first word for Capital.
1327 	if (lnum == 1)
1328 	    capcol = 0;
1329 
1330 	// For checking first word with a capital skip white space.
1331 	if (capcol == 0)
1332 	    capcol = getwhitecols(line);
1333 	else if (curline && wp == curwin)
1334 	{
1335 	    // For spellbadword(): check if first word needs a capital.
1336 	    col = getwhitecols(line);
1337 	    if (check_need_cap(lnum, col))
1338 		capcol = col;
1339 
1340 	    // Need to get the line again, may have looked at the previous
1341 	    // one.
1342 	    line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1343 	}
1344 
1345 	// Copy the line into "buf" and append the start of the next line if
1346 	// possible.
1347 	STRCPY(buf, line);
1348 	if (lnum < wp->w_buffer->b_ml.ml_line_count)
1349 	    spell_cat_line(buf + STRLEN(buf),
1350 			  ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
1351 
1352 	p = buf + skip;
1353 	endp = buf + len;
1354 	while (p < endp)
1355 	{
1356 	    // When searching backward don't search after the cursor.  Unless
1357 	    // we wrapped around the end of the buffer.
1358 	    if (dir == BACKWARD
1359 		    && lnum == wp->w_cursor.lnum
1360 		    && !wrapped
1361 		    && (colnr_T)(p - buf) >= wp->w_cursor.col)
1362 		break;
1363 
1364 	    // start of word
1365 	    attr = HLF_COUNT;
1366 	    len = spell_check(wp, p, &attr, &capcol, FALSE);
1367 
1368 	    if (attr != HLF_COUNT)
1369 	    {
1370 		// We found a bad word.  Check the attribute.
1371 		if (allwords || attr == HLF_SPB)
1372 		{
1373 		    // When searching forward only accept a bad word after
1374 		    // the cursor.
1375 		    if (dir == BACKWARD
1376 			    || lnum != wp->w_cursor.lnum
1377 			    || (lnum == wp->w_cursor.lnum
1378 				&& (wrapped
1379 				    || (colnr_T)(curline ? p - buf + len
1380 						     : p - buf)
1381 						  > wp->w_cursor.col)))
1382 		    {
1383 #ifdef FEAT_SYN_HL
1384 			if (has_syntax)
1385 			{
1386 			    col = (int)(p - buf);
1387 			    (void)syn_get_id(wp, lnum, (colnr_T)col,
1388 						    FALSE, &can_spell, FALSE);
1389 			    if (!can_spell)
1390 				attr = HLF_COUNT;
1391 			}
1392 			else
1393 #endif
1394 			    can_spell = TRUE;
1395 
1396 			if (can_spell)
1397 			{
1398 			    found_one = TRUE;
1399 			    found_pos.lnum = lnum;
1400 			    found_pos.col = (int)(p - buf);
1401 			    found_pos.coladd = 0;
1402 			    if (dir == FORWARD)
1403 			    {
1404 				// No need to search further.
1405 				wp->w_cursor = found_pos;
1406 				vim_free(buf);
1407 				if (attrp != NULL)
1408 				    *attrp = attr;
1409 				return len;
1410 			    }
1411 			    else if (curline)
1412 				// Insert mode completion: put cursor after
1413 				// the bad word.
1414 				found_pos.col += len;
1415 			    found_len = len;
1416 			}
1417 		    }
1418 		    else
1419 			found_one = TRUE;
1420 		}
1421 	    }
1422 
1423 	    // advance to character after the word
1424 	    p += len;
1425 	    capcol -= len;
1426 	}
1427 
1428 	if (dir == BACKWARD && found_pos.lnum != 0)
1429 	{
1430 	    // Use the last match in the line (before the cursor).
1431 	    wp->w_cursor = found_pos;
1432 	    vim_free(buf);
1433 	    return found_len;
1434 	}
1435 
1436 	if (curline)
1437 	    break;	// only check cursor line
1438 
1439 	// If we are back at the starting line and searched it again there
1440 	// is no match, give up.
1441 	if (lnum == wp->w_cursor.lnum && wrapped)
1442 	    break;
1443 
1444 	// Advance to next line.
1445 	if (dir == BACKWARD)
1446 	{
1447 	    if (lnum > 1)
1448 		--lnum;
1449 	    else if (!p_ws)
1450 		break;	    // at first line and 'nowrapscan'
1451 	    else
1452 	    {
1453 		// Wrap around to the end of the buffer.  May search the
1454 		// starting line again and accept the last match.
1455 		lnum = wp->w_buffer->b_ml.ml_line_count;
1456 		wrapped = TRUE;
1457 		if (!shortmess(SHM_SEARCH))
1458 		    give_warning((char_u *)_(top_bot_msg), TRUE);
1459 	    }
1460 	    capcol = -1;
1461 	}
1462 	else
1463 	{
1464 	    if (lnum < wp->w_buffer->b_ml.ml_line_count)
1465 		++lnum;
1466 	    else if (!p_ws)
1467 		break;	    // at first line and 'nowrapscan'
1468 	    else
1469 	    {
1470 		// Wrap around to the start of the buffer.  May search the
1471 		// starting line again and accept the first match.
1472 		lnum = 1;
1473 		wrapped = TRUE;
1474 		if (!shortmess(SHM_SEARCH))
1475 		    give_warning((char_u *)_(bot_top_msg), TRUE);
1476 	    }
1477 
1478 	    // If we are back at the starting line and there is no match then
1479 	    // give up.
1480 	    if (lnum == wp->w_cursor.lnum && !found_one)
1481 		break;
1482 
1483 	    // Skip the characters at the start of the next line that were
1484 	    // included in a match crossing line boundaries.
1485 	    if (attr == HLF_COUNT)
1486 		skip = (int)(p - endp);
1487 	    else
1488 		skip = 0;
1489 
1490 	    // Capcol skips over the inserted space.
1491 	    --capcol;
1492 
1493 	    // But after empty line check first word in next line
1494 	    if (*skipwhite(line) == NUL)
1495 		capcol = 0;
1496 	}
1497 
1498 	line_breakcheck();
1499     }
1500 
1501     vim_free(buf);
1502     return 0;
1503 }
1504 
1505 /*
1506  * For spell checking: concatenate the start of the following line "line" into
1507  * "buf", blanking-out special characters.  Copy less then "maxlen" bytes.
1508  * Keep the blanks at the start of the next line, this is used in win_line()
1509  * to skip those bytes if the word was OK.
1510  */
1511     void
1512 spell_cat_line(char_u *buf, char_u *line, int maxlen)
1513 {
1514     char_u	*p;
1515     int		n;
1516 
1517     p = skipwhite(line);
1518     while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1519 	p = skipwhite(p + 1);
1520 
1521     if (*p != NUL)
1522     {
1523 	// Only worth concatenating if there is something else than spaces to
1524 	// concatenate.
1525 	n = (int)(p - line) + 1;
1526 	if (n < maxlen - 1)
1527 	{
1528 	    vim_memset(buf, ' ', n);
1529 	    vim_strncpy(buf +  n, p, maxlen - 1 - n);
1530 	}
1531     }
1532 }
1533 
1534 /*
1535  * Structure used for the cookie argument of do_in_runtimepath().
1536  */
1537 typedef struct spelload_S
1538 {
1539     char_u  sl_lang[MAXWLEN + 1];	// language name
1540     slang_T *sl_slang;			// resulting slang_T struct
1541     int	    sl_nobreak;			// NOBREAK language found
1542 } spelload_T;
1543 
1544 /*
1545  * Load word list(s) for "lang" from Vim spell file(s).
1546  * "lang" must be the language without the region: e.g., "en".
1547  */
1548     static void
1549 spell_load_lang(char_u *lang)
1550 {
1551     char_u	fname_enc[85];
1552     int		r;
1553     spelload_T	sl;
1554     int		round;
1555 
1556     // Copy the language name to pass it to spell_load_cb() as a cookie.
1557     // It's truncated when an error is detected.
1558     STRCPY(sl.sl_lang, lang);
1559     sl.sl_slang = NULL;
1560     sl.sl_nobreak = FALSE;
1561 
1562     // We may retry when no spell file is found for the language, an
1563     // autocommand may load it then.
1564     for (round = 1; round <= 2; ++round)
1565     {
1566 	/*
1567 	 * Find the first spell file for "lang" in 'runtimepath' and load it.
1568 	 */
1569 	vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
1570 #ifdef VMS
1571 					"spell/%s_%s.spl",
1572 #else
1573 					"spell/%s.%s.spl",
1574 #endif
1575 							   lang, spell_enc());
1576 	r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
1577 
1578 	if (r == FAIL && *sl.sl_lang != NUL)
1579 	{
1580 	    // Try loading the ASCII version.
1581 	    vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
1582 #ifdef VMS
1583 						  "spell/%s_ascii.spl",
1584 #else
1585 						  "spell/%s.ascii.spl",
1586 #endif
1587 									lang);
1588 	    r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
1589 
1590 	    if (r == FAIL && *sl.sl_lang != NUL && round == 1
1591 		    && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
1592 					      curbuf->b_fname, FALSE, curbuf))
1593 		continue;
1594 	    break;
1595 	}
1596 	break;
1597     }
1598 
1599     if (r == FAIL)
1600     {
1601 	smsg(
1602 #ifdef VMS
1603 	_("Warning: Cannot find word list \"%s_%s.spl\" or \"%s_ascii.spl\""),
1604 #else
1605 	_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
1606 #endif
1607 						     lang, spell_enc(), lang);
1608     }
1609     else if (sl.sl_slang != NULL)
1610     {
1611 	// At least one file was loaded, now load ALL the additions.
1612 	STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
1613 	do_in_runtimepath(fname_enc, DIP_ALL, spell_load_cb, &sl);
1614     }
1615 }
1616 
1617 /*
1618  * Return the encoding used for spell checking: Use 'encoding', except that we
1619  * use "latin1" for "latin9".  And limit to 60 characters (just in case).
1620  */
1621     char_u *
1622 spell_enc(void)
1623 {
1624 
1625     if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1626 	return p_enc;
1627     return (char_u *)"latin1";
1628 }
1629 
1630 /*
1631  * Get the name of the .spl file for the internal wordlist into
1632  * "fname[MAXPATHL]".
1633  */
1634     static void
1635 int_wordlist_spl(char_u *fname)
1636 {
1637     vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL,
1638 						  int_wordlist, spell_enc());
1639 }
1640 
1641 /*
1642  * Allocate a new slang_T for language "lang".  "lang" can be NULL.
1643  * Caller must fill "sl_next".
1644  */
1645     slang_T *
1646 slang_alloc(char_u *lang)
1647 {
1648     slang_T *lp;
1649 
1650     lp = ALLOC_CLEAR_ONE(slang_T);
1651     if (lp != NULL)
1652     {
1653 	if (lang != NULL)
1654 	    lp->sl_name = vim_strsave(lang);
1655 	ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
1656 	ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
1657 	lp->sl_compmax = MAXWLEN;
1658 	lp->sl_compsylmax = MAXWLEN;
1659 	hash_init(&lp->sl_wordcount);
1660     }
1661 
1662     return lp;
1663 }
1664 
1665 /*
1666  * Free the contents of an slang_T and the structure itself.
1667  */
1668     void
1669 slang_free(slang_T *lp)
1670 {
1671     vim_free(lp->sl_name);
1672     vim_free(lp->sl_fname);
1673     slang_clear(lp);
1674     vim_free(lp);
1675 }
1676 
1677 /*
1678  * Clear an slang_T so that the file can be reloaded.
1679  */
1680     void
1681 slang_clear(slang_T *lp)
1682 {
1683     garray_T	*gap;
1684     fromto_T	*ftp;
1685     salitem_T	*smp;
1686     int		i;
1687     int		round;
1688 
1689     VIM_CLEAR(lp->sl_fbyts);
1690     VIM_CLEAR(lp->sl_kbyts);
1691     VIM_CLEAR(lp->sl_pbyts);
1692 
1693     VIM_CLEAR(lp->sl_fidxs);
1694     VIM_CLEAR(lp->sl_kidxs);
1695     VIM_CLEAR(lp->sl_pidxs);
1696 
1697     for (round = 1; round <= 2; ++round)
1698     {
1699 	gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
1700 	while (gap->ga_len > 0)
1701 	{
1702 	    ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1703 	    vim_free(ftp->ft_from);
1704 	    vim_free(ftp->ft_to);
1705 	}
1706 	ga_clear(gap);
1707     }
1708 
1709     gap = &lp->sl_sal;
1710     if (lp->sl_sofo)
1711     {
1712 	// "ga_len" is set to 1 without adding an item for latin1
1713 	if (gap->ga_data != NULL)
1714 	    // SOFOFROM and SOFOTO items: free lists of wide characters.
1715 	    for (i = 0; i < gap->ga_len; ++i)
1716 		vim_free(((int **)gap->ga_data)[i]);
1717     }
1718     else
1719 	// SAL items: free salitem_T items
1720 	while (gap->ga_len > 0)
1721 	{
1722 	    smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1723 	    vim_free(smp->sm_lead);
1724 	    // Don't free sm_oneof and sm_rules, they point into sm_lead.
1725 	    vim_free(smp->sm_to);
1726 	    vim_free(smp->sm_lead_w);
1727 	    vim_free(smp->sm_oneof_w);
1728 	    vim_free(smp->sm_to_w);
1729 	}
1730     ga_clear(gap);
1731 
1732     for (i = 0; i < lp->sl_prefixcnt; ++i)
1733 	vim_regfree(lp->sl_prefprog[i]);
1734     lp->sl_prefixcnt = 0;
1735     VIM_CLEAR(lp->sl_prefprog);
1736 
1737     VIM_CLEAR(lp->sl_info);
1738 
1739     VIM_CLEAR(lp->sl_midword);
1740 
1741     vim_regfree(lp->sl_compprog);
1742     lp->sl_compprog = NULL;
1743     VIM_CLEAR(lp->sl_comprules);
1744     VIM_CLEAR(lp->sl_compstartflags);
1745     VIM_CLEAR(lp->sl_compallflags);
1746 
1747     VIM_CLEAR(lp->sl_syllable);
1748     ga_clear(&lp->sl_syl_items);
1749 
1750     ga_clear_strings(&lp->sl_comppat);
1751 
1752     hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
1753     hash_init(&lp->sl_wordcount);
1754 
1755     hash_clear_all(&lp->sl_map_hash, 0);
1756 
1757     // Clear info from .sug file.
1758     slang_clear_sug(lp);
1759 
1760     lp->sl_compmax = MAXWLEN;
1761     lp->sl_compminlen = 0;
1762     lp->sl_compsylmax = MAXWLEN;
1763     lp->sl_regions[0] = NUL;
1764 }
1765 
1766 /*
1767  * Clear the info from the .sug file in "lp".
1768  */
1769     void
1770 slang_clear_sug(slang_T *lp)
1771 {
1772     VIM_CLEAR(lp->sl_sbyts);
1773     VIM_CLEAR(lp->sl_sidxs);
1774     close_spellbuf(lp->sl_sugbuf);
1775     lp->sl_sugbuf = NULL;
1776     lp->sl_sugloaded = FALSE;
1777     lp->sl_sugtime = 0;
1778 }
1779 
1780 /*
1781  * Load one spell file and store the info into a slang_T.
1782  * Invoked through do_in_runtimepath().
1783  */
1784     static void
1785 spell_load_cb(char_u *fname, void *cookie)
1786 {
1787     spelload_T	*slp = (spelload_T *)cookie;
1788     slang_T	*slang;
1789 
1790     slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
1791     if (slang != NULL)
1792     {
1793 	// When a previously loaded file has NOBREAK also use it for the
1794 	// ".add" files.
1795 	if (slp->sl_nobreak && slang->sl_add)
1796 	    slang->sl_nobreak = TRUE;
1797 	else if (slang->sl_nobreak)
1798 	    slp->sl_nobreak = TRUE;
1799 
1800 	slp->sl_slang = slang;
1801     }
1802 }
1803 
1804 
1805 /*
1806  * Add a word to the hashtable of common words.
1807  * If it's already there then the counter is increased.
1808  */
1809     void
1810 count_common_word(
1811     slang_T	*lp,
1812     char_u	*word,
1813     int		len,	    // word length, -1 for up to NUL
1814     int		count)	    // 1 to count once, 10 to init
1815 {
1816     hash_T	hash;
1817     hashitem_T	*hi;
1818     wordcount_T	*wc;
1819     char_u	buf[MAXWLEN];
1820     char_u	*p;
1821 
1822     if (len == -1)
1823 	p = word;
1824     else if (len >= MAXWLEN)
1825 	return;
1826     else
1827     {
1828 	vim_strncpy(buf, word, len);
1829 	p = buf;
1830     }
1831 
1832     hash = hash_hash(p);
1833     hi = hash_lookup(&lp->sl_wordcount, p, hash);
1834     if (HASHITEM_EMPTY(hi))
1835     {
1836 	wc = alloc(sizeof(wordcount_T) + STRLEN(p));
1837 	if (wc == NULL)
1838 	    return;
1839 	STRCPY(wc->wc_word, p);
1840 	wc->wc_count = count;
1841 	hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
1842     }
1843     else
1844     {
1845 	wc = HI2WC(hi);
1846 	if ((wc->wc_count += count) < (unsigned)count)	// check for overflow
1847 	    wc->wc_count = MAXWORDCOUNT;
1848     }
1849 }
1850 
1851 /*
1852  * Return TRUE if byte "n" appears in "str".
1853  * Like strchr() but independent of locale.
1854  */
1855     int
1856 byte_in_str(char_u *str, int n)
1857 {
1858     char_u	*p;
1859 
1860     for (p = str; *p != NUL; ++p)
1861 	if (*p == n)
1862 	    return TRUE;
1863     return FALSE;
1864 }
1865 
1866 #define SY_MAXLEN   30
1867 typedef struct syl_item_S
1868 {
1869     char_u	sy_chars[SY_MAXLEN];	    // the sequence of chars
1870     int		sy_len;
1871 } syl_item_T;
1872 
1873 /*
1874  * Truncate "slang->sl_syllable" at the first slash and put the following items
1875  * in "slang->sl_syl_items".
1876  */
1877     int
1878 init_syl_tab(slang_T *slang)
1879 {
1880     char_u	*p;
1881     char_u	*s;
1882     int		l;
1883     syl_item_T	*syl;
1884 
1885     ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
1886     p = vim_strchr(slang->sl_syllable, '/');
1887     while (p != NULL)
1888     {
1889 	*p++ = NUL;
1890 	if (*p == NUL)	    // trailing slash
1891 	    break;
1892 	s = p;
1893 	p = vim_strchr(p, '/');
1894 	if (p == NULL)
1895 	    l = (int)STRLEN(s);
1896 	else
1897 	    l = (int)(p - s);
1898 	if (l >= SY_MAXLEN)
1899 	    return SP_FORMERROR;
1900 	if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
1901 	    return SP_OTHERERROR;
1902 	syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
1903 					       + slang->sl_syl_items.ga_len++;
1904 	vim_strncpy(syl->sy_chars, s, l);
1905 	syl->sy_len = l;
1906     }
1907     return OK;
1908 }
1909 
1910 /*
1911  * Count the number of syllables in "word".
1912  * When "word" contains spaces the syllables after the last space are counted.
1913  * Returns zero if syllables are not defines.
1914  */
1915     static int
1916 count_syllables(slang_T *slang, char_u *word)
1917 {
1918     int		cnt = 0;
1919     int		skip = FALSE;
1920     char_u	*p;
1921     int		len;
1922     int		i;
1923     syl_item_T	*syl;
1924     int		c;
1925 
1926     if (slang->sl_syllable == NULL)
1927 	return 0;
1928 
1929     for (p = word; *p != NUL; p += len)
1930     {
1931 	// When running into a space reset counter.
1932 	if (*p == ' ')
1933 	{
1934 	    len = 1;
1935 	    cnt = 0;
1936 	    continue;
1937 	}
1938 
1939 	// Find longest match of syllable items.
1940 	len = 0;
1941 	for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
1942 	{
1943 	    syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
1944 	    if (syl->sy_len > len
1945 			       && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
1946 		len = syl->sy_len;
1947 	}
1948 	if (len != 0)	// found a match, count syllable
1949 	{
1950 	    ++cnt;
1951 	    skip = FALSE;
1952 	}
1953 	else
1954 	{
1955 	    // No recognized syllable item, at least a syllable char then?
1956 	    c = mb_ptr2char(p);
1957 	    len = (*mb_ptr2len)(p);
1958 	    if (vim_strchr(slang->sl_syllable, c) == NULL)
1959 		skip = FALSE;	    // No, search for next syllable
1960 	    else if (!skip)
1961 	    {
1962 		++cnt;		    // Yes, count it
1963 		skip = TRUE;	    // don't count following syllable chars
1964 	    }
1965 	}
1966     }
1967     return cnt;
1968 }
1969 
1970 /*
1971  * Parse 'spelllang' and set w_s->b_langp accordingly.
1972  * Returns NULL if it's OK, an error message otherwise.
1973  */
1974     char *
1975 did_set_spelllang(win_T *wp)
1976 {
1977     garray_T	ga;
1978     char_u	*splp;
1979     char_u	*region;
1980     char_u	region_cp[3];
1981     int		filename;
1982     int		region_mask;
1983     slang_T	*slang;
1984     int		c;
1985     char_u	lang[MAXWLEN + 1];
1986     char_u	spf_name[MAXPATHL];
1987     int		len;
1988     char_u	*p;
1989     int		round;
1990     char_u	*spf;
1991     char_u	*use_region = NULL;
1992     int		dont_use_region = FALSE;
1993     int		nobreak = FALSE;
1994     int		i, j;
1995     langp_T	*lp, *lp2;
1996     static int	recursive = FALSE;
1997     char	*ret_msg = NULL;
1998     char_u	*spl_copy;
1999     bufref_T	bufref;
2000 
2001     set_bufref(&bufref, wp->w_buffer);
2002 
2003     // We don't want to do this recursively.  May happen when a language is
2004     // not available and the SpellFileMissing autocommand opens a new buffer
2005     // in which 'spell' is set.
2006     if (recursive)
2007 	return NULL;
2008     recursive = TRUE;
2009 
2010     ga_init2(&ga, sizeof(langp_T), 2);
2011     clear_midword(wp);
2012 
2013     // Make a copy of 'spelllang', the SpellFileMissing autocommands may change
2014     // it under our fingers.
2015     spl_copy = vim_strsave(wp->w_s->b_p_spl);
2016     if (spl_copy == NULL)
2017 	goto theend;
2018 
2019     wp->w_s->b_cjk = 0;
2020 
2021     // Loop over comma separated language names.
2022     for (splp = spl_copy; *splp != NUL; )
2023     {
2024 	// Get one language name.
2025 	copy_option_part(&splp, lang, MAXWLEN, ",");
2026 	region = NULL;
2027 	len = (int)STRLEN(lang);
2028 
2029 	if (!valid_spelllang(lang))
2030 	    continue;
2031 
2032 	if (STRCMP(lang, "cjk") == 0)
2033 	{
2034 	    wp->w_s->b_cjk = 1;
2035 	    continue;
2036 	}
2037 
2038 	// If the name ends in ".spl" use it as the name of the spell file.
2039 	// If there is a region name let "region" point to it and remove it
2040 	// from the name.
2041 	if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2042 	{
2043 	    filename = TRUE;
2044 
2045 	    // Locate a region and remove it from the file name.
2046 	    p = vim_strchr(gettail(lang), '_');
2047 	    if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
2048 						      && !ASCII_ISALPHA(p[3]))
2049 	    {
2050 		vim_strncpy(region_cp, p + 1, 2);
2051 		mch_memmove(p, p + 3, len - (p - lang) - 2);
2052 		region = region_cp;
2053 	    }
2054 	    else
2055 		dont_use_region = TRUE;
2056 
2057 	    // Check if we loaded this language before.
2058 	    FOR_ALL_SPELL_LANGS(slang)
2059 		if (fullpathcmp(lang, slang->sl_fname, FALSE, TRUE) == FPC_SAME)
2060 		    break;
2061 	}
2062 	else
2063 	{
2064 	    filename = FALSE;
2065 	    if (len > 3 && lang[len - 3] == '_')
2066 	    {
2067 		region = lang + len - 2;
2068 		len -= 3;
2069 		lang[len] = NUL;
2070 	    }
2071 	    else
2072 		dont_use_region = TRUE;
2073 
2074 	    // Check if we loaded this language before.
2075 	    FOR_ALL_SPELL_LANGS(slang)
2076 		if (STRICMP(lang, slang->sl_name) == 0)
2077 		    break;
2078 	}
2079 
2080 	if (region != NULL)
2081 	{
2082 	    // If the region differs from what was used before then don't
2083 	    // use it for 'spellfile'.
2084 	    if (use_region != NULL && STRCMP(region, use_region) != 0)
2085 		dont_use_region = TRUE;
2086 	    use_region = region;
2087 	}
2088 
2089 	// If not found try loading the language now.
2090 	if (slang == NULL)
2091 	{
2092 	    if (filename)
2093 		(void)spell_load_file(lang, lang, NULL, FALSE);
2094 	    else
2095 	    {
2096 		spell_load_lang(lang);
2097 		// SpellFileMissing autocommands may do anything, including
2098 		// destroying the buffer we are using...
2099 		if (!bufref_valid(&bufref))
2100 		{
2101 		    ret_msg = N_("E797: SpellFileMissing autocommand deleted buffer");
2102 		    goto theend;
2103 		}
2104 	    }
2105 	}
2106 
2107 	/*
2108 	 * Loop over the languages, there can be several files for "lang".
2109 	 */
2110 	FOR_ALL_SPELL_LANGS(slang)
2111 	    if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE, TRUE)
2112 								    == FPC_SAME
2113 			 : STRICMP(lang, slang->sl_name) == 0)
2114 	    {
2115 		region_mask = REGION_ALL;
2116 		if (!filename && region != NULL)
2117 		{
2118 		    // find region in sl_regions
2119 		    c = find_region(slang->sl_regions, region);
2120 		    if (c == REGION_ALL)
2121 		    {
2122 			if (slang->sl_add)
2123 			{
2124 			    if (*slang->sl_regions != NUL)
2125 				// This addition file is for other regions.
2126 				region_mask = 0;
2127 			}
2128 			else
2129 			    // This is probably an error.  Give a warning and
2130 			    // accept the words anyway.
2131 			    smsg(_("Warning: region %s not supported"),
2132 								      region);
2133 		    }
2134 		    else
2135 			region_mask = 1 << c;
2136 		}
2137 
2138 		if (region_mask != 0)
2139 		{
2140 		    if (ga_grow(&ga, 1) == FAIL)
2141 		    {
2142 			ga_clear(&ga);
2143 			ret_msg = e_outofmem;
2144 			goto theend;
2145 		    }
2146 		    LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
2147 		    LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2148 		    ++ga.ga_len;
2149 		    use_midword(slang, wp);
2150 		    if (slang->sl_nobreak)
2151 			nobreak = TRUE;
2152 		}
2153 	    }
2154     }
2155 
2156     // round 0: load int_wordlist, if possible.
2157     // round 1: load first name in 'spellfile'.
2158     // round 2: load second name in 'spellfile.
2159     // etc.
2160     spf = curwin->w_s->b_p_spf;
2161     for (round = 0; round == 0 || *spf != NUL; ++round)
2162     {
2163 	if (round == 0)
2164 	{
2165 	    // Internal wordlist, if there is one.
2166 	    if (int_wordlist == NULL)
2167 		continue;
2168 	    int_wordlist_spl(spf_name);
2169 	}
2170 	else
2171 	{
2172 	    // One entry in 'spellfile'.
2173 	    copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
2174 	    STRCAT(spf_name, ".spl");
2175 
2176 	    // If it was already found above then skip it.
2177 	    for (c = 0; c < ga.ga_len; ++c)
2178 	    {
2179 		p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
2180 		if (p != NULL && fullpathcmp(spf_name, p, FALSE, TRUE)
2181 								== FPC_SAME)
2182 		    break;
2183 	    }
2184 	    if (c < ga.ga_len)
2185 		continue;
2186 	}
2187 
2188 	// Check if it was loaded already.
2189 	FOR_ALL_SPELL_LANGS(slang)
2190 	    if (fullpathcmp(spf_name, slang->sl_fname, FALSE, TRUE)
2191 								== FPC_SAME)
2192 		break;
2193 	if (slang == NULL)
2194 	{
2195 	    // Not loaded, try loading it now.  The language name includes the
2196 	    // region name, the region is ignored otherwise.  for int_wordlist
2197 	    // use an arbitrary name.
2198 	    if (round == 0)
2199 		STRCPY(lang, "internal wordlist");
2200 	    else
2201 	    {
2202 		vim_strncpy(lang, gettail(spf_name), MAXWLEN);
2203 		p = vim_strchr(lang, '.');
2204 		if (p != NULL)
2205 		    *p = NUL;	// truncate at ".encoding.add"
2206 	    }
2207 	    slang = spell_load_file(spf_name, lang, NULL, TRUE);
2208 
2209 	    // If one of the languages has NOBREAK we assume the addition
2210 	    // files also have this.
2211 	    if (slang != NULL && nobreak)
2212 		slang->sl_nobreak = TRUE;
2213 	}
2214 	if (slang != NULL && ga_grow(&ga, 1) == OK)
2215 	{
2216 	    region_mask = REGION_ALL;
2217 	    if (use_region != NULL && !dont_use_region)
2218 	    {
2219 		// find region in sl_regions
2220 		c = find_region(slang->sl_regions, use_region);
2221 		if (c != REGION_ALL)
2222 		    region_mask = 1 << c;
2223 		else if (*slang->sl_regions != NUL)
2224 		    // This spell file is for other regions.
2225 		    region_mask = 0;
2226 	    }
2227 
2228 	    if (region_mask != 0)
2229 	    {
2230 		LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
2231 		LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
2232 		LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
2233 		LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2234 		++ga.ga_len;
2235 		use_midword(slang, wp);
2236 	    }
2237 	}
2238     }
2239 
2240     // Everything is fine, store the new b_langp value.
2241     ga_clear(&wp->w_s->b_langp);
2242     wp->w_s->b_langp = ga;
2243 
2244     // For each language figure out what language to use for sound folding and
2245     // REP items.  If the language doesn't support it itself use another one
2246     // with the same name.  E.g. for "en-math" use "en".
2247     for (i = 0; i < ga.ga_len; ++i)
2248     {
2249 	lp = LANGP_ENTRY(ga, i);
2250 
2251 	// sound folding
2252 	if (lp->lp_slang->sl_sal.ga_len > 0)
2253 	    // language does sound folding itself
2254 	    lp->lp_sallang = lp->lp_slang;
2255 	else
2256 	    // find first similar language that does sound folding
2257 	    for (j = 0; j < ga.ga_len; ++j)
2258 	    {
2259 		lp2 = LANGP_ENTRY(ga, j);
2260 		if (lp2->lp_slang->sl_sal.ga_len > 0
2261 			&& STRNCMP(lp->lp_slang->sl_name,
2262 					      lp2->lp_slang->sl_name, 2) == 0)
2263 		{
2264 		    lp->lp_sallang = lp2->lp_slang;
2265 		    break;
2266 		}
2267 	    }
2268 
2269 	// REP items
2270 	if (lp->lp_slang->sl_rep.ga_len > 0)
2271 	    // language has REP items itself
2272 	    lp->lp_replang = lp->lp_slang;
2273 	else
2274 	    // find first similar language that has REP items
2275 	    for (j = 0; j < ga.ga_len; ++j)
2276 	    {
2277 		lp2 = LANGP_ENTRY(ga, j);
2278 		if (lp2->lp_slang->sl_rep.ga_len > 0
2279 			&& STRNCMP(lp->lp_slang->sl_name,
2280 					      lp2->lp_slang->sl_name, 2) == 0)
2281 		{
2282 		    lp->lp_replang = lp2->lp_slang;
2283 		    break;
2284 		}
2285 	    }
2286     }
2287 
2288 theend:
2289     vim_free(spl_copy);
2290     recursive = FALSE;
2291     redraw_win_later(wp, NOT_VALID);
2292     return ret_msg;
2293 }
2294 
2295 /*
2296  * Clear the midword characters for buffer "buf".
2297  */
2298     static void
2299 clear_midword(win_T *wp)
2300 {
2301     CLEAR_FIELD(wp->w_s->b_spell_ismw);
2302     VIM_CLEAR(wp->w_s->b_spell_ismw_mb);
2303 }
2304 
2305 /*
2306  * Use the "sl_midword" field of language "lp" for buffer "buf".
2307  * They add up to any currently used midword characters.
2308  */
2309     static void
2310 use_midword(slang_T *lp, win_T *wp)
2311 {
2312     char_u	*p;
2313 
2314     if (lp->sl_midword == NULL)	    // there aren't any
2315 	return;
2316 
2317     for (p = lp->sl_midword; *p != NUL; )
2318 	if (has_mbyte)
2319 	{
2320 	    int	    c, l, n;
2321 	    char_u  *bp;
2322 
2323 	    c = mb_ptr2char(p);
2324 	    l = (*mb_ptr2len)(p);
2325 	    if (c < 256 && l <= 2)
2326 		wp->w_s->b_spell_ismw[c] = TRUE;
2327 	    else if (wp->w_s->b_spell_ismw_mb == NULL)
2328 		// First multi-byte char in "b_spell_ismw_mb".
2329 		wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
2330 	    else
2331 	    {
2332 		// Append multi-byte chars to "b_spell_ismw_mb".
2333 		n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
2334 		bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
2335 		if (bp != NULL)
2336 		{
2337 		    vim_free(wp->w_s->b_spell_ismw_mb);
2338 		    wp->w_s->b_spell_ismw_mb = bp;
2339 		    vim_strncpy(bp + n, p, l);
2340 		}
2341 	    }
2342 	    p += l;
2343 	}
2344 	else
2345 	    wp->w_s->b_spell_ismw[*p++] = TRUE;
2346 }
2347 
2348 /*
2349  * Find the region "region[2]" in "rp" (points to "sl_regions").
2350  * Each region is simply stored as the two characters of its name.
2351  * Returns the index if found (first is 0), REGION_ALL if not found.
2352  */
2353     static int
2354 find_region(char_u *rp, char_u *region)
2355 {
2356     int		i;
2357 
2358     for (i = 0; ; i += 2)
2359     {
2360 	if (rp[i] == NUL)
2361 	    return REGION_ALL;
2362 	if (rp[i] == region[0] && rp[i + 1] == region[1])
2363 	    break;
2364     }
2365     return i / 2;
2366 }
2367 
2368 /*
2369  * Return case type of word:
2370  * w word	0
2371  * Word		WF_ONECAP
2372  * W WORD	WF_ALLCAP
2373  * WoRd	wOrd	WF_KEEPCAP
2374  */
2375     int
2376 captype(
2377     char_u	*word,
2378     char_u	*end)	    // When NULL use up to NUL byte.
2379 {
2380     char_u	*p;
2381     int		c;
2382     int		firstcap;
2383     int		allcap;
2384     int		past_second = FALSE;	// past second word char
2385 
2386     // find first letter
2387     for (p = word; !spell_iswordp_nmw(p, curwin); MB_PTR_ADV(p))
2388 	if (end == NULL ? *p == NUL : p >= end)
2389 	    return 0;	    // only non-word characters, illegal word
2390     if (has_mbyte)
2391 	c = mb_ptr2char_adv(&p);
2392     else
2393 	c = *p++;
2394     firstcap = allcap = SPELL_ISUPPER(c);
2395 
2396     /*
2397      * Need to check all letters to find a word with mixed upper/lower.
2398      * But a word with an upper char only at start is a ONECAP.
2399      */
2400     for ( ; end == NULL ? *p != NUL : p < end; MB_PTR_ADV(p))
2401 	if (spell_iswordp_nmw(p, curwin))
2402 	{
2403 	    c = PTR2CHAR(p);
2404 	    if (!SPELL_ISUPPER(c))
2405 	    {
2406 		// UUl -> KEEPCAP
2407 		if (past_second && allcap)
2408 		    return WF_KEEPCAP;
2409 		allcap = FALSE;
2410 	    }
2411 	    else if (!allcap)
2412 		// UlU -> KEEPCAP
2413 		return WF_KEEPCAP;
2414 	    past_second = TRUE;
2415 	}
2416 
2417     if (allcap)
2418 	return WF_ALLCAP;
2419     if (firstcap)
2420 	return WF_ONECAP;
2421     return 0;
2422 }
2423 
2424 /*
2425  * Delete the internal wordlist and its .spl file.
2426  */
2427     void
2428 spell_delete_wordlist(void)
2429 {
2430     char_u	fname[MAXPATHL];
2431 
2432     if (int_wordlist != NULL)
2433     {
2434 	mch_remove(int_wordlist);
2435 	int_wordlist_spl(fname);
2436 	mch_remove(fname);
2437 	VIM_CLEAR(int_wordlist);
2438     }
2439 }
2440 
2441 /*
2442  * Free all languages.
2443  */
2444     void
2445 spell_free_all(void)
2446 {
2447     slang_T	*slang;
2448     buf_T	*buf;
2449 
2450     // Go through all buffers and handle 'spelllang'. <VN>
2451     FOR_ALL_BUFFERS(buf)
2452 	ga_clear(&buf->b_s.b_langp);
2453 
2454     while (first_lang != NULL)
2455     {
2456 	slang = first_lang;
2457 	first_lang = slang->sl_next;
2458 	slang_free(slang);
2459     }
2460 
2461     spell_delete_wordlist();
2462 
2463     VIM_CLEAR(repl_to);
2464     VIM_CLEAR(repl_from);
2465 }
2466 
2467 /*
2468  * Clear all spelling tables and reload them.
2469  * Used after 'encoding' is set and when ":mkspell" was used.
2470  */
2471     void
2472 spell_reload(void)
2473 {
2474     win_T	*wp;
2475 
2476     // Initialize the table for spell_iswordp().
2477     init_spell_chartab();
2478 
2479     // Unload all allocated memory.
2480     spell_free_all();
2481 
2482     // Go through all buffers and handle 'spelllang'.
2483     FOR_ALL_WINDOWS(wp)
2484     {
2485 	// Only load the wordlists when 'spelllang' is set and there is a
2486 	// window for this buffer in which 'spell' is set.
2487 	if (*wp->w_s->b_p_spl != NUL)
2488 	{
2489 		if (wp->w_p_spell)
2490 		{
2491 		    (void)did_set_spelllang(wp);
2492 		    break;
2493 		}
2494 	}
2495     }
2496 }
2497 
2498 /*
2499  * Open a spell buffer.  This is a nameless buffer that is not in the buffer
2500  * list and only contains text lines.  Can use a swapfile to reduce memory
2501  * use.
2502  * Most other fields are invalid!  Esp. watch out for string options being
2503  * NULL and there is no undo info.
2504  * Returns NULL when out of memory.
2505  */
2506     buf_T *
2507 open_spellbuf(void)
2508 {
2509     buf_T	*buf;
2510 
2511     buf = ALLOC_CLEAR_ONE(buf_T);
2512     if (buf != NULL)
2513     {
2514 	buf->b_spell = TRUE;
2515 	buf->b_p_swf = TRUE;	// may create a swap file
2516 #ifdef FEAT_CRYPT
2517 	buf->b_p_key = empty_option;
2518 #endif
2519 	ml_open(buf);
2520 	ml_open_file(buf);	// create swap file now
2521     }
2522     return buf;
2523 }
2524 
2525 /*
2526  * Close the buffer used for spell info.
2527  */
2528     void
2529 close_spellbuf(buf_T *buf)
2530 {
2531     if (buf != NULL)
2532     {
2533 	ml_close(buf, TRUE);
2534 	vim_free(buf);
2535     }
2536 }
2537 
2538 /*
2539  * Init the chartab used for spelling for ASCII.
2540  * EBCDIC is not supported!
2541  */
2542     void
2543 clear_spell_chartab(spelltab_T *sp)
2544 {
2545     int		i;
2546 
2547     // Init everything to FALSE (zero).
2548     CLEAR_FIELD(sp->st_isw);
2549     CLEAR_FIELD(sp->st_isu);
2550     for (i = 0; i < 256; ++i)
2551     {
2552 	sp->st_fold[i] = i;
2553 	sp->st_upper[i] = i;
2554     }
2555 
2556     // We include digits.  A word shouldn't start with a digit, but handling
2557     // that is done separately.
2558     for (i = '0'; i <= '9'; ++i)
2559 	sp->st_isw[i] = TRUE;
2560     for (i = 'A'; i <= 'Z'; ++i)
2561     {
2562 	sp->st_isw[i] = TRUE;
2563 	sp->st_isu[i] = TRUE;
2564 	sp->st_fold[i] = i + 0x20;
2565     }
2566     for (i = 'a'; i <= 'z'; ++i)
2567     {
2568 	sp->st_isw[i] = TRUE;
2569 	sp->st_upper[i] = i - 0x20;
2570     }
2571 }
2572 
2573 /*
2574  * Init the chartab used for spelling.  Only depends on 'encoding'.
2575  * Called once while starting up and when 'encoding' changes.
2576  * The default is to use isalpha(), but the spell file should define the word
2577  * characters to make it possible that 'encoding' differs from the current
2578  * locale.  For utf-8 we don't use isalpha() but our own functions.
2579  */
2580     void
2581 init_spell_chartab(void)
2582 {
2583     int	    i;
2584 
2585     did_set_spelltab = FALSE;
2586     clear_spell_chartab(&spelltab);
2587     if (enc_dbcs)
2588     {
2589 	// DBCS: assume double-wide characters are word characters.
2590 	for (i = 128; i <= 255; ++i)
2591 	    if (MB_BYTE2LEN(i) == 2)
2592 		spelltab.st_isw[i] = TRUE;
2593     }
2594     else if (enc_utf8)
2595     {
2596 	for (i = 128; i < 256; ++i)
2597 	{
2598 	    int f = utf_fold(i);
2599 	    int u = utf_toupper(i);
2600 
2601 	    spelltab.st_isu[i] = utf_isupper(i);
2602 	    spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
2603 	    // The folded/upper-cased value is different between latin1 and
2604 	    // utf8 for 0xb5, causing E763 for no good reason.  Use the latin1
2605 	    // value for utf-8 to avoid this.
2606 	    spelltab.st_fold[i] = (f < 256) ? f : i;
2607 	    spelltab.st_upper[i] = (u < 256) ? u : i;
2608 	}
2609     }
2610     else
2611     {
2612 	// Rough guess: use locale-dependent library functions.
2613 	for (i = 128; i < 256; ++i)
2614 	{
2615 	    if (MB_ISUPPER(i))
2616 	    {
2617 		spelltab.st_isw[i] = TRUE;
2618 		spelltab.st_isu[i] = TRUE;
2619 		spelltab.st_fold[i] = MB_TOLOWER(i);
2620 	    }
2621 	    else if (MB_ISLOWER(i))
2622 	    {
2623 		spelltab.st_isw[i] = TRUE;
2624 		spelltab.st_upper[i] = MB_TOUPPER(i);
2625 	    }
2626 	}
2627     }
2628 }
2629 
2630 
2631 /*
2632  * Return TRUE if "p" points to a word character.
2633  * As a special case we see "midword" characters as word character when it is
2634  * followed by a word character.  This finds they'there but not 'they there'.
2635  * Thus this only works properly when past the first character of the word.
2636  */
2637     int
2638 spell_iswordp(
2639     char_u	*p,
2640     win_T	*wp)	    // buffer used
2641 {
2642     char_u	*s;
2643     int		l;
2644     int		c;
2645 
2646     if (has_mbyte)
2647     {
2648 	l = mb_ptr2len(p);
2649 	s = p;
2650 	if (l == 1)
2651 	{
2652 	    // be quick for ASCII
2653 	    if (wp->w_s->b_spell_ismw[*p])
2654 		s = p + 1;		// skip a mid-word character
2655 	}
2656 	else
2657 	{
2658 	    c = mb_ptr2char(p);
2659 	    if (c < 256 ? wp->w_s->b_spell_ismw[c]
2660 		    : (wp->w_s->b_spell_ismw_mb != NULL
2661 			   && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
2662 		s = p + l;
2663 	}
2664 
2665 	c = mb_ptr2char(s);
2666 	if (c > 255)
2667 	    return spell_mb_isword_class(mb_get_class(s), wp);
2668 	return spelltab.st_isw[c];
2669     }
2670 
2671     return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
2672 }
2673 
2674 /*
2675  * Return TRUE if "p" points to a word character.
2676  * Unlike spell_iswordp() this doesn't check for "midword" characters.
2677  */
2678     int
2679 spell_iswordp_nmw(char_u *p, win_T *wp)
2680 {
2681     int		c;
2682 
2683     if (has_mbyte)
2684     {
2685 	c = mb_ptr2char(p);
2686 	if (c > 255)
2687 	    return spell_mb_isword_class(mb_get_class(p), wp);
2688 	return spelltab.st_isw[c];
2689     }
2690     return spelltab.st_isw[*p];
2691 }
2692 
2693 /*
2694  * Return TRUE if word class indicates a word character.
2695  * Only for characters above 255.
2696  * Unicode subscript and superscript are not considered word characters.
2697  * See also dbcs_class() and utf_class() in mbyte.c.
2698  */
2699     static int
2700 spell_mb_isword_class(int cl, win_T *wp)
2701 {
2702     if (wp->w_s->b_cjk)
2703 	// East Asian characters are not considered word characters.
2704 	return cl == 2 || cl == 0x2800;
2705     return cl >= 2 && cl != 0x2070 && cl != 0x2080 && cl != 3;
2706 }
2707 
2708 /*
2709  * Return TRUE if "p" points to a word character.
2710  * Wide version of spell_iswordp().
2711  */
2712     static int
2713 spell_iswordp_w(int *p, win_T *wp)
2714 {
2715     int		*s;
2716 
2717     if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
2718 		 : (wp->w_s->b_spell_ismw_mb != NULL
2719 			     && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
2720 	s = p + 1;
2721     else
2722 	s = p;
2723 
2724     if (*s > 255)
2725     {
2726 	if (enc_utf8)
2727 	    return spell_mb_isword_class(utf_class(*s), wp);
2728 	if (enc_dbcs)
2729 	    return spell_mb_isword_class(
2730 				dbcs_class((unsigned)*s >> 8, *s & 0xff), wp);
2731 	return 0;
2732     }
2733     return spelltab.st_isw[*s];
2734 }
2735 
2736 /*
2737  * Case-fold "str[len]" into "buf[buflen]".  The result is NUL terminated.
2738  * Uses the character definitions from the .spl file.
2739  * When using a multi-byte 'encoding' the length may change!
2740  * Returns FAIL when something wrong.
2741  */
2742     int
2743 spell_casefold(
2744     char_u	*str,
2745     int		len,
2746     char_u	*buf,
2747     int		buflen)
2748 {
2749     int		i;
2750 
2751     if (len >= buflen)
2752     {
2753 	buf[0] = NUL;
2754 	return FAIL;		// result will not fit
2755     }
2756 
2757     if (has_mbyte)
2758     {
2759 	int	outi = 0;
2760 	char_u	*p;
2761 	int	c;
2762 
2763 	// Fold one character at a time.
2764 	for (p = str; p < str + len; )
2765 	{
2766 	    if (outi + MB_MAXBYTES > buflen)
2767 	    {
2768 		buf[outi] = NUL;
2769 		return FAIL;
2770 	    }
2771 	    c = mb_cptr2char_adv(&p);
2772 	    outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
2773 	}
2774 	buf[outi] = NUL;
2775     }
2776     else
2777     {
2778 	// Be quick for non-multibyte encodings.
2779 	for (i = 0; i < len; ++i)
2780 	    buf[i] = spelltab.st_fold[str[i]];
2781 	buf[i] = NUL;
2782     }
2783 
2784     return OK;
2785 }
2786 
2787 /*
2788  * Check if the word at line "lnum" column "col" is required to start with a
2789  * capital.  This uses 'spellcapcheck' of the current buffer.
2790  */
2791     int
2792 check_need_cap(linenr_T lnum, colnr_T col)
2793 {
2794     int		need_cap = FALSE;
2795     char_u	*line;
2796     char_u	*line_copy = NULL;
2797     char_u	*p;
2798     colnr_T	endcol;
2799     regmatch_T	regmatch;
2800 
2801     if (curwin->w_s->b_cap_prog == NULL)
2802 	return FALSE;
2803 
2804     line = ml_get_curline();
2805     endcol = 0;
2806     if (getwhitecols(line) >= (int)col)
2807     {
2808 	// At start of line, check if previous line is empty or sentence
2809 	// ends there.
2810 	if (lnum == 1)
2811 	    need_cap = TRUE;
2812 	else
2813 	{
2814 	    line = ml_get(lnum - 1);
2815 	    if (*skipwhite(line) == NUL)
2816 		need_cap = TRUE;
2817 	    else
2818 	    {
2819 		// Append a space in place of the line break.
2820 		line_copy = concat_str(line, (char_u *)" ");
2821 		line = line_copy;
2822 		endcol = (colnr_T)STRLEN(line);
2823 	    }
2824 	}
2825     }
2826     else
2827 	endcol = col;
2828 
2829     if (endcol > 0)
2830     {
2831 	// Check if sentence ends before the bad word.
2832 	regmatch.regprog = curwin->w_s->b_cap_prog;
2833 	regmatch.rm_ic = FALSE;
2834 	p = line + endcol;
2835 	for (;;)
2836 	{
2837 	    MB_PTR_BACK(line, p);
2838 	    if (p == line || spell_iswordp_nmw(p, curwin))
2839 		break;
2840 	    if (vim_regexec(&regmatch, p, 0)
2841 					 && regmatch.endp[0] == line + endcol)
2842 	    {
2843 		need_cap = TRUE;
2844 		break;
2845 	    }
2846 	}
2847 	curwin->w_s->b_cap_prog = regmatch.regprog;
2848     }
2849 
2850     vim_free(line_copy);
2851 
2852     return need_cap;
2853 }
2854 
2855 
2856 /*
2857  * ":spellrepall"
2858  */
2859     void
2860 ex_spellrepall(exarg_T *eap UNUSED)
2861 {
2862     pos_T	pos = curwin->w_cursor;
2863     char_u	*frompat;
2864     int		addlen;
2865     char_u	*line;
2866     char_u	*p;
2867     int		save_ws = p_ws;
2868     linenr_T	prev_lnum = 0;
2869 
2870     if (repl_from == NULL || repl_to == NULL)
2871     {
2872 	emsg(_("E752: No previous spell replacement"));
2873 	return;
2874     }
2875     addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
2876 
2877     frompat = alloc(STRLEN(repl_from) + 7);
2878     if (frompat == NULL)
2879 	return;
2880     sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
2881     p_ws = FALSE;
2882 
2883     sub_nsubs = 0;
2884     sub_nlines = 0;
2885     curwin->w_cursor.lnum = 0;
2886     while (!got_int)
2887     {
2888 	if (do_search(NULL, '/', '/', frompat, 1L, SEARCH_KEEP, NULL) == 0
2889 						   || u_save_cursor() == FAIL)
2890 	    break;
2891 
2892 	// Only replace when the right word isn't there yet.  This happens
2893 	// when changing "etc" to "etc.".
2894 	line = ml_get_curline();
2895 	if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
2896 					       repl_to, STRLEN(repl_to)) != 0)
2897 	{
2898 	    p = alloc(STRLEN(line) + addlen + 1);
2899 	    if (p == NULL)
2900 		break;
2901 	    mch_memmove(p, line, curwin->w_cursor.col);
2902 	    STRCPY(p + curwin->w_cursor.col, repl_to);
2903 	    STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
2904 	    ml_replace(curwin->w_cursor.lnum, p, FALSE);
2905 	    changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
2906 
2907 	    if (curwin->w_cursor.lnum != prev_lnum)
2908 	    {
2909 		++sub_nlines;
2910 		prev_lnum = curwin->w_cursor.lnum;
2911 	    }
2912 	    ++sub_nsubs;
2913 	}
2914 	curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
2915     }
2916 
2917     p_ws = save_ws;
2918     curwin->w_cursor = pos;
2919     vim_free(frompat);
2920 
2921     if (sub_nsubs == 0)
2922 	semsg(_("E753: Not found: %s"), repl_from);
2923     else
2924 	do_sub_msg(FALSE);
2925 }
2926 
2927 /*
2928  * Make a copy of "word", with the first letter upper or lower cased, to
2929  * "wcopy[MAXWLEN]".  "word" must not be empty.
2930  * The result is NUL terminated.
2931  */
2932     void
2933 onecap_copy(
2934     char_u	*word,
2935     char_u	*wcopy,
2936     int		upper)	    // TRUE: first letter made upper case
2937 {
2938     char_u	*p;
2939     int		c;
2940     int		l;
2941 
2942     p = word;
2943     if (has_mbyte)
2944 	c = mb_cptr2char_adv(&p);
2945     else
2946 	c = *p++;
2947     if (upper)
2948 	c = SPELL_TOUPPER(c);
2949     else
2950 	c = SPELL_TOFOLD(c);
2951     if (has_mbyte)
2952 	l = mb_char2bytes(c, wcopy);
2953     else
2954     {
2955 	l = 1;
2956 	wcopy[0] = c;
2957     }
2958     vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
2959 }
2960 
2961 /*
2962  * Make a copy of "word" with all the letters upper cased into
2963  * "wcopy[MAXWLEN]".  The result is NUL terminated.
2964  */
2965     void
2966 allcap_copy(char_u *word, char_u *wcopy)
2967 {
2968     char_u	*s;
2969     char_u	*d;
2970     int		c;
2971 
2972     d = wcopy;
2973     for (s = word; *s != NUL; )
2974     {
2975 	if (has_mbyte)
2976 	    c = mb_cptr2char_adv(&s);
2977 	else
2978 	    c = *s++;
2979 
2980 	// We only change 0xdf to SS when we are certain latin1 is used.  It
2981 	// would cause weird errors in other 8-bit encodings.
2982 	if (enc_latin1like && c == 0xdf)
2983 	{
2984 	    c = 'S';
2985 	    if (d - wcopy >= MAXWLEN - 1)
2986 		break;
2987 	    *d++ = c;
2988 	}
2989 	else
2990 	    c = SPELL_TOUPPER(c);
2991 
2992 	if (has_mbyte)
2993 	{
2994 	    if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
2995 		break;
2996 	    d += mb_char2bytes(c, d);
2997 	}
2998 	else
2999 	{
3000 	    if (d - wcopy >= MAXWLEN - 1)
3001 		break;
3002 	    *d++ = c;
3003 	}
3004     }
3005     *d = NUL;
3006 }
3007 
3008 /*
3009  * Case-folding may change the number of bytes: Count nr of chars in
3010  * fword[flen] and return the byte length of that many chars in "word".
3011  */
3012     int
3013 nofold_len(char_u *fword, int flen, char_u *word)
3014 {
3015     char_u	*p;
3016     int		i = 0;
3017 
3018     for (p = fword; p < fword + flen; MB_PTR_ADV(p))
3019 	++i;
3020     for (p = word; i > 0; MB_PTR_ADV(p))
3021 	--i;
3022     return (int)(p - word);
3023 }
3024 
3025 /*
3026  * Copy "fword" to "cword", fixing case according to "flags".
3027  */
3028     void
3029 make_case_word(char_u *fword, char_u *cword, int flags)
3030 {
3031     if (flags & WF_ALLCAP)
3032 	// Make it all upper-case
3033 	allcap_copy(fword, cword);
3034     else if (flags & WF_ONECAP)
3035 	// Make the first letter upper-case
3036 	onecap_copy(fword, cword, TRUE);
3037     else
3038 	// Use goodword as-is.
3039 	STRCPY(cword, fword);
3040 }
3041 
3042 #if defined(FEAT_EVAL) || defined(PROTO)
3043 /*
3044  * Soundfold a string, for soundfold().
3045  * Result is in allocated memory, NULL for an error.
3046  */
3047     char_u *
3048 eval_soundfold(char_u *word)
3049 {
3050     langp_T	*lp;
3051     char_u	sound[MAXWLEN];
3052     int		lpi;
3053 
3054     if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
3055 	// Use the sound-folding of the first language that supports it.
3056 	for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
3057 	{
3058 	    lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
3059 	    if (lp->lp_slang->sl_sal.ga_len > 0)
3060 	    {
3061 		// soundfold the word
3062 		spell_soundfold(lp->lp_slang, word, FALSE, sound);
3063 		return vim_strsave(sound);
3064 	    }
3065 	}
3066 
3067     // No language with sound folding, return word as-is.
3068     return vim_strsave(word);
3069 }
3070 #endif
3071 
3072 /*
3073  * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
3074  *
3075  * There are many ways to turn a word into a sound-a-like representation.  The
3076  * oldest is Soundex (1918!).   A nice overview can be found in "Approximate
3077  * swedish name matching - survey and test of different algorithms" by Klas
3078  * Erikson.
3079  *
3080  * We support two methods:
3081  * 1. SOFOFROM/SOFOTO do a simple character mapping.
3082  * 2. SAL items define a more advanced sound-folding (and much slower).
3083  */
3084     void
3085 spell_soundfold(
3086     slang_T	*slang,
3087     char_u	*inword,
3088     int		folded,	    // "inword" is already case-folded
3089     char_u	*res)
3090 {
3091     char_u	fword[MAXWLEN];
3092     char_u	*word;
3093 
3094     if (slang->sl_sofo)
3095 	// SOFOFROM and SOFOTO used
3096 	spell_soundfold_sofo(slang, inword, res);
3097     else
3098     {
3099 	// SAL items used.  Requires the word to be case-folded.
3100 	if (folded)
3101 	    word = inword;
3102 	else
3103 	{
3104 	    (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
3105 	    word = fword;
3106 	}
3107 
3108 	if (has_mbyte)
3109 	    spell_soundfold_wsal(slang, word, res);
3110 	else
3111 	    spell_soundfold_sal(slang, word, res);
3112     }
3113 }
3114 
3115 /*
3116  * Perform sound folding of "inword" into "res" according to SOFOFROM and
3117  * SOFOTO lines.
3118  */
3119     static void
3120 spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res)
3121 {
3122     char_u	*s;
3123     int		ri = 0;
3124     int		c;
3125 
3126     if (has_mbyte)
3127     {
3128 	int	prevc = 0;
3129 	int	*ip;
3130 
3131 	// The sl_sal_first[] table contains the translation for chars up to
3132 	// 255, sl_sal the rest.
3133 	for (s = inword; *s != NUL; )
3134 	{
3135 	    c = mb_cptr2char_adv(&s);
3136 	    if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
3137 		c = ' ';
3138 	    else if (c < 256)
3139 		c = slang->sl_sal_first[c];
3140 	    else
3141 	    {
3142 		ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
3143 		if (ip == NULL)		// empty list, can't match
3144 		    c = NUL;
3145 		else
3146 		    for (;;)		// find "c" in the list
3147 		    {
3148 			if (*ip == 0)	// not found
3149 			{
3150 			    c = NUL;
3151 			    break;
3152 			}
3153 			if (*ip == c)	// match!
3154 			{
3155 			    c = ip[1];
3156 			    break;
3157 			}
3158 			ip += 2;
3159 		    }
3160 	    }
3161 
3162 	    if (c != NUL && c != prevc)
3163 	    {
3164 		ri += mb_char2bytes(c, res + ri);
3165 		if (ri + MB_MAXBYTES > MAXWLEN)
3166 		    break;
3167 		prevc = c;
3168 	    }
3169 	}
3170     }
3171     else
3172     {
3173 	// The sl_sal_first[] table contains the translation.
3174 	for (s = inword; (c = *s) != NUL; ++s)
3175 	{
3176 	    if (VIM_ISWHITE(c))
3177 		c = ' ';
3178 	    else
3179 		c = slang->sl_sal_first[c];
3180 	    if (c != NUL && (ri == 0 || res[ri - 1] != c))
3181 		res[ri++] = c;
3182 	}
3183     }
3184 
3185     res[ri] = NUL;
3186 }
3187 
3188     static void
3189 spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res)
3190 {
3191     salitem_T	*smp;
3192     char_u	word[MAXWLEN];
3193     char_u	*s = inword;
3194     char_u	*t;
3195     char_u	*pf;
3196     int		i, j, z;
3197     int		reslen;
3198     int		n, k = 0;
3199     int		z0;
3200     int		k0;
3201     int		n0;
3202     int		c;
3203     int		pri;
3204     int		p0 = -333;
3205     int		c0;
3206 
3207     // Remove accents, if wanted.  We actually remove all non-word characters.
3208     // But keep white space.  We need a copy, the word may be changed here.
3209     if (slang->sl_rem_accents)
3210     {
3211 	t = word;
3212 	while (*s != NUL)
3213 	{
3214 	    if (VIM_ISWHITE(*s))
3215 	    {
3216 		*t++ = ' ';
3217 		s = skipwhite(s);
3218 	    }
3219 	    else
3220 	    {
3221 		if (spell_iswordp_nmw(s, curwin))
3222 		    *t++ = *s;
3223 		++s;
3224 	    }
3225 	}
3226 	*t = NUL;
3227     }
3228     else
3229 	vim_strncpy(word, s, MAXWLEN - 1);
3230 
3231     smp = (salitem_T *)slang->sl_sal.ga_data;
3232 
3233     /*
3234      * This comes from Aspell phonet.cpp.  Converted from C++ to C.
3235      * Changed to keep spaces.
3236      */
3237     i = reslen = z = 0;
3238     while ((c = word[i]) != NUL)
3239     {
3240 	// Start with the first rule that has the character in the word.
3241 	n = slang->sl_sal_first[c];
3242 	z0 = 0;
3243 
3244 	if (n >= 0)
3245 	{
3246 	    // check all rules for the same letter
3247 	    for (; (s = smp[n].sm_lead)[0] == c; ++n)
3248 	    {
3249 		// Quickly skip entries that don't match the word.  Most
3250 		// entries are less then three chars, optimize for that.
3251 		k = smp[n].sm_leadlen;
3252 		if (k > 1)
3253 		{
3254 		    if (word[i + 1] != s[1])
3255 			continue;
3256 		    if (k > 2)
3257 		    {
3258 			for (j = 2; j < k; ++j)
3259 			    if (word[i + j] != s[j])
3260 				break;
3261 			if (j < k)
3262 			    continue;
3263 		    }
3264 		}
3265 
3266 		if ((pf = smp[n].sm_oneof) != NULL)
3267 		{
3268 		    // Check for match with one of the chars in "sm_oneof".
3269 		    while (*pf != NUL && *pf != word[i + k])
3270 			++pf;
3271 		    if (*pf == NUL)
3272 			continue;
3273 		    ++k;
3274 		}
3275 		s = smp[n].sm_rules;
3276 		pri = 5;    // default priority
3277 
3278 		p0 = *s;
3279 		k0 = k;
3280 		while (*s == '-' && k > 1)
3281 		{
3282 		    k--;
3283 		    s++;
3284 		}
3285 		if (*s == '<')
3286 		    s++;
3287 		if (VIM_ISDIGIT(*s))
3288 		{
3289 		    // determine priority
3290 		    pri = *s - '0';
3291 		    s++;
3292 		}
3293 		if (*s == '^' && *(s + 1) == '^')
3294 		    s++;
3295 
3296 		if (*s == NUL
3297 			|| (*s == '^'
3298 			    && (i == 0 || !(word[i - 1] == ' '
3299 				      || spell_iswordp(word + i - 1, curwin)))
3300 			    && (*(s + 1) != '$'
3301 				|| (!spell_iswordp(word + i + k0, curwin))))
3302 			|| (*s == '$' && i > 0
3303 			    && spell_iswordp(word + i - 1, curwin)
3304 			    && (!spell_iswordp(word + i + k0, curwin))))
3305 		{
3306 		    // search for followup rules, if:
3307 		    // followup and k > 1  and  NO '-' in searchstring
3308 		    c0 = word[i + k - 1];
3309 		    n0 = slang->sl_sal_first[c0];
3310 
3311 		    if (slang->sl_followup && k > 1 && n0 >= 0
3312 					   && p0 != '-' && word[i + k] != NUL)
3313 		    {
3314 			// test follow-up rule for "word[i + k]"
3315 			for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
3316 			{
3317 			    // Quickly skip entries that don't match the word.
3318 			    //
3319 			    k0 = smp[n0].sm_leadlen;
3320 			    if (k0 > 1)
3321 			    {
3322 				if (word[i + k] != s[1])
3323 				    continue;
3324 				if (k0 > 2)
3325 				{
3326 				    pf = word + i + k + 1;
3327 				    for (j = 2; j < k0; ++j)
3328 					if (*pf++ != s[j])
3329 					    break;
3330 				    if (j < k0)
3331 					continue;
3332 				}
3333 			    }
3334 			    k0 += k - 1;
3335 
3336 			    if ((pf = smp[n0].sm_oneof) != NULL)
3337 			    {
3338 				// Check for match with one of the chars in
3339 				// "sm_oneof".
3340 				while (*pf != NUL && *pf != word[i + k0])
3341 				    ++pf;
3342 				if (*pf == NUL)
3343 				    continue;
3344 				++k0;
3345 			    }
3346 
3347 			    p0 = 5;
3348 			    s = smp[n0].sm_rules;
3349 			    while (*s == '-')
3350 			    {
3351 				// "k0" gets NOT reduced because
3352 				// "if (k0 == k)"
3353 				s++;
3354 			    }
3355 			    if (*s == '<')
3356 				s++;
3357 			    if (VIM_ISDIGIT(*s))
3358 			    {
3359 				p0 = *s - '0';
3360 				s++;
3361 			    }
3362 
3363 			    if (*s == NUL
3364 				    // *s == '^' cuts
3365 				    || (*s == '$'
3366 					    && !spell_iswordp(word + i + k0,
3367 								     curwin)))
3368 			    {
3369 				if (k0 == k)
3370 				    // this is just a piece of the string
3371 				    continue;
3372 
3373 				if (p0 < pri)
3374 				    // priority too low
3375 				    continue;
3376 				// rule fits; stop search
3377 				break;
3378 			    }
3379 			}
3380 
3381 			if (p0 >= pri && smp[n0].sm_lead[0] == c0)
3382 			    continue;
3383 		    }
3384 
3385 		    // replace string
3386 		    s = smp[n].sm_to;
3387 		    if (s == NULL)
3388 			s = (char_u *)"";
3389 		    pf = smp[n].sm_rules;
3390 		    p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
3391 		    if (p0 == 1 && z == 0)
3392 		    {
3393 			// rule with '<' is used
3394 			if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
3395 						    || res[reslen - 1] == *s))
3396 			    reslen--;
3397 			z0 = 1;
3398 			z = 1;
3399 			k0 = 0;
3400 			while (*s != NUL && word[i + k0] != NUL)
3401 			{
3402 			    word[i + k0] = *s;
3403 			    k0++;
3404 			    s++;
3405 			}
3406 			if (k > k0)
3407 			    STRMOVE(word + i + k0, word + i + k);
3408 
3409 			// new "actual letter"
3410 			c = word[i];
3411 		    }
3412 		    else
3413 		    {
3414 			// no '<' rule used
3415 			i += k - 1;
3416 			z = 0;
3417 			while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
3418 			{
3419 			    if (reslen == 0 || res[reslen - 1] != *s)
3420 				res[reslen++] = *s;
3421 			    s++;
3422 			}
3423 			// new "actual letter"
3424 			c = *s;
3425 			if (strstr((char *)pf, "^^") != NULL)
3426 			{
3427 			    if (c != NUL)
3428 				res[reslen++] = c;
3429 			    STRMOVE(word, word + i + 1);
3430 			    i = 0;
3431 			    z0 = 1;
3432 			}
3433 		    }
3434 		    break;
3435 		}
3436 	    }
3437 	}
3438 	else if (VIM_ISWHITE(c))
3439 	{
3440 	    c = ' ';
3441 	    k = 1;
3442 	}
3443 
3444 	if (z0 == 0)
3445 	{
3446 	    if (k && !p0 && reslen < MAXWLEN && c != NUL
3447 		    && (!slang->sl_collapse || reslen == 0
3448 						     || res[reslen - 1] != c))
3449 		// condense only double letters
3450 		res[reslen++] = c;
3451 
3452 	    i++;
3453 	    z = 0;
3454 	    k = 0;
3455 	}
3456     }
3457 
3458     res[reslen] = NUL;
3459 }
3460 
3461 /*
3462  * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
3463  * Multi-byte version of spell_soundfold().
3464  */
3465     static void
3466 spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res)
3467 {
3468     salitem_T	*smp = (salitem_T *)slang->sl_sal.ga_data;
3469     int		word[MAXWLEN];
3470     int		wres[MAXWLEN];
3471     int		l;
3472     char_u	*s;
3473     int		*ws;
3474     char_u	*t;
3475     int		*pf;
3476     int		i, j, z;
3477     int		reslen;
3478     int		n, k = 0;
3479     int		z0;
3480     int		k0;
3481     int		n0;
3482     int		c;
3483     int		pri;
3484     int		p0 = -333;
3485     int		c0;
3486     int		did_white = FALSE;
3487     int		wordlen;
3488 
3489 
3490     /*
3491      * Convert the multi-byte string to a wide-character string.
3492      * Remove accents, if wanted.  We actually remove all non-word characters.
3493      * But keep white space.
3494      */
3495     wordlen = 0;
3496     for (s = inword; *s != NUL; )
3497     {
3498 	t = s;
3499 	c = mb_cptr2char_adv(&s);
3500 	if (slang->sl_rem_accents)
3501 	{
3502 	    if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
3503 	    {
3504 		if (did_white)
3505 		    continue;
3506 		c = ' ';
3507 		did_white = TRUE;
3508 	    }
3509 	    else
3510 	    {
3511 		did_white = FALSE;
3512 		if (!spell_iswordp_nmw(t, curwin))
3513 		    continue;
3514 	    }
3515 	}
3516 	word[wordlen++] = c;
3517     }
3518     word[wordlen] = NUL;
3519 
3520     /*
3521      * This algorithm comes from Aspell phonet.cpp.
3522      * Converted from C++ to C.  Added support for multi-byte chars.
3523      * Changed to keep spaces.
3524      */
3525     i = reslen = z = 0;
3526     while ((c = word[i]) != NUL)
3527     {
3528 	// Start with the first rule that has the character in the word.
3529 	n = slang->sl_sal_first[c & 0xff];
3530 	z0 = 0;
3531 
3532 	if (n >= 0)
3533 	{
3534 	    // Check all rules for the same index byte.
3535 	    // If c is 0x300 need extra check for the end of the array, as
3536 	    // (c & 0xff) is NUL.
3537 	    for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff)
3538 							 && ws[0] != NUL; ++n)
3539 	    {
3540 		// Quickly skip entries that don't match the word.  Most
3541 		// entries are less then three chars, optimize for that.
3542 		if (c != ws[0])
3543 		    continue;
3544 		k = smp[n].sm_leadlen;
3545 		if (k > 1)
3546 		{
3547 		    if (word[i + 1] != ws[1])
3548 			continue;
3549 		    if (k > 2)
3550 		    {
3551 			for (j = 2; j < k; ++j)
3552 			    if (word[i + j] != ws[j])
3553 				break;
3554 			if (j < k)
3555 			    continue;
3556 		    }
3557 		}
3558 
3559 		if ((pf = smp[n].sm_oneof_w) != NULL)
3560 		{
3561 		    // Check for match with one of the chars in "sm_oneof".
3562 		    while (*pf != NUL && *pf != word[i + k])
3563 			++pf;
3564 		    if (*pf == NUL)
3565 			continue;
3566 		    ++k;
3567 		}
3568 		s = smp[n].sm_rules;
3569 		pri = 5;    // default priority
3570 
3571 		p0 = *s;
3572 		k0 = k;
3573 		while (*s == '-' && k > 1)
3574 		{
3575 		    k--;
3576 		    s++;
3577 		}
3578 		if (*s == '<')
3579 		    s++;
3580 		if (VIM_ISDIGIT(*s))
3581 		{
3582 		    // determine priority
3583 		    pri = *s - '0';
3584 		    s++;
3585 		}
3586 		if (*s == '^' && *(s + 1) == '^')
3587 		    s++;
3588 
3589 		if (*s == NUL
3590 			|| (*s == '^'
3591 			    && (i == 0 || !(word[i - 1] == ' '
3592 				    || spell_iswordp_w(word + i - 1, curwin)))
3593 			    && (*(s + 1) != '$'
3594 				|| (!spell_iswordp_w(word + i + k0, curwin))))
3595 			|| (*s == '$' && i > 0
3596 			    && spell_iswordp_w(word + i - 1, curwin)
3597 			    && (!spell_iswordp_w(word + i + k0, curwin))))
3598 		{
3599 		    // search for followup rules, if:
3600 		    // followup and k > 1  and  NO '-' in searchstring
3601 		    c0 = word[i + k - 1];
3602 		    n0 = slang->sl_sal_first[c0 & 0xff];
3603 
3604 		    if (slang->sl_followup && k > 1 && n0 >= 0
3605 					   && p0 != '-' && word[i + k] != NUL)
3606 		    {
3607 			// Test follow-up rule for "word[i + k]"; loop over
3608 			// all entries with the same index byte.
3609 			for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
3610 							 == (c0 & 0xff); ++n0)
3611 			{
3612 			    // Quickly skip entries that don't match the word.
3613 			    if (c0 != ws[0])
3614 				continue;
3615 			    k0 = smp[n0].sm_leadlen;
3616 			    if (k0 > 1)
3617 			    {
3618 				if (word[i + k] != ws[1])
3619 				    continue;
3620 				if (k0 > 2)
3621 				{
3622 				    pf = word + i + k + 1;
3623 				    for (j = 2; j < k0; ++j)
3624 					if (*pf++ != ws[j])
3625 					    break;
3626 				    if (j < k0)
3627 					continue;
3628 				}
3629 			    }
3630 			    k0 += k - 1;
3631 
3632 			    if ((pf = smp[n0].sm_oneof_w) != NULL)
3633 			    {
3634 				// Check for match with one of the chars in
3635 				// "sm_oneof".
3636 				while (*pf != NUL && *pf != word[i + k0])
3637 				    ++pf;
3638 				if (*pf == NUL)
3639 				    continue;
3640 				++k0;
3641 			    }
3642 
3643 			    p0 = 5;
3644 			    s = smp[n0].sm_rules;
3645 			    while (*s == '-')
3646 			    {
3647 				// "k0" gets NOT reduced because
3648 				// "if (k0 == k)"
3649 				s++;
3650 			    }
3651 			    if (*s == '<')
3652 				s++;
3653 			    if (VIM_ISDIGIT(*s))
3654 			    {
3655 				p0 = *s - '0';
3656 				s++;
3657 			    }
3658 
3659 			    if (*s == NUL
3660 				    // *s == '^' cuts
3661 				    || (*s == '$'
3662 					 && !spell_iswordp_w(word + i + k0,
3663 								     curwin)))
3664 			    {
3665 				if (k0 == k)
3666 				    // this is just a piece of the string
3667 				    continue;
3668 
3669 				if (p0 < pri)
3670 				    // priority too low
3671 				    continue;
3672 				// rule fits; stop search
3673 				break;
3674 			    }
3675 			}
3676 
3677 			if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
3678 							       == (c0 & 0xff))
3679 			    continue;
3680 		    }
3681 
3682 		    // replace string
3683 		    ws = smp[n].sm_to_w;
3684 		    s = smp[n].sm_rules;
3685 		    p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
3686 		    if (p0 == 1 && z == 0)
3687 		    {
3688 			// rule with '<' is used
3689 			if (reslen > 0 && ws != NULL && *ws != NUL
3690 				&& (wres[reslen - 1] == c
3691 						    || wres[reslen - 1] == *ws))
3692 			    reslen--;
3693 			z0 = 1;
3694 			z = 1;
3695 			k0 = 0;
3696 			if (ws != NULL)
3697 			    while (*ws != NUL && word[i + k0] != NUL)
3698 			    {
3699 				word[i + k0] = *ws;
3700 				k0++;
3701 				ws++;
3702 			    }
3703 			if (k > k0)
3704 			    mch_memmove(word + i + k0, word + i + k,
3705 				    sizeof(int) * (wordlen - (i + k) + 1));
3706 
3707 			// new "actual letter"
3708 			c = word[i];
3709 		    }
3710 		    else
3711 		    {
3712 			// no '<' rule used
3713 			i += k - 1;
3714 			z = 0;
3715 			if (ws != NULL)
3716 			    while (*ws != NUL && ws[1] != NUL
3717 							  && reslen < MAXWLEN)
3718 			    {
3719 				if (reslen == 0 || wres[reslen - 1] != *ws)
3720 				    wres[reslen++] = *ws;
3721 				ws++;
3722 			    }
3723 			// new "actual letter"
3724 			if (ws == NULL)
3725 			    c = NUL;
3726 			else
3727 			    c = *ws;
3728 			if (strstr((char *)s, "^^") != NULL)
3729 			{
3730 			    if (c != NUL)
3731 				wres[reslen++] = c;
3732 			    mch_memmove(word, word + i + 1,
3733 				       sizeof(int) * (wordlen - (i + 1) + 1));
3734 			    i = 0;
3735 			    z0 = 1;
3736 			}
3737 		    }
3738 		    break;
3739 		}
3740 	    }
3741 	}
3742 	else if (VIM_ISWHITE(c))
3743 	{
3744 	    c = ' ';
3745 	    k = 1;
3746 	}
3747 
3748 	if (z0 == 0)
3749 	{
3750 	    if (k && !p0 && reslen < MAXWLEN && c != NUL
3751 		    && (!slang->sl_collapse || reslen == 0
3752 						     || wres[reslen - 1] != c))
3753 		// condense only double letters
3754 		wres[reslen++] = c;
3755 
3756 	    i++;
3757 	    z = 0;
3758 	    k = 0;
3759 	}
3760     }
3761 
3762     // Convert wide characters in "wres" to a multi-byte string in "res".
3763     l = 0;
3764     for (n = 0; n < reslen; ++n)
3765     {
3766 	l += mb_char2bytes(wres[n], res + l);
3767 	if (l + MB_MAXBYTES > MAXWLEN)
3768 	    break;
3769     }
3770     res[l] = NUL;
3771 }
3772 
3773 /*
3774  * ":spellinfo"
3775  */
3776     void
3777 ex_spellinfo(exarg_T *eap UNUSED)
3778 {
3779     int		lpi;
3780     langp_T	*lp;
3781     char_u	*p;
3782 
3783     if (no_spell_checking(curwin))
3784 	return;
3785 
3786     msg_start();
3787     for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
3788     {
3789 	lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
3790 	msg_puts("file: ");
3791 	msg_puts((char *)lp->lp_slang->sl_fname);
3792 	msg_putchar('\n');
3793 	p = lp->lp_slang->sl_info;
3794 	if (p != NULL)
3795 	{
3796 	    msg_puts((char *)p);
3797 	    msg_putchar('\n');
3798 	}
3799     }
3800     msg_end();
3801 }
3802 
3803 #define DUMPFLAG_KEEPCASE   1	// round 2: keep-case tree
3804 #define DUMPFLAG_COUNT	    2	// include word count
3805 #define DUMPFLAG_ICASE	    4	// ignore case when finding matches
3806 #define DUMPFLAG_ONECAP	    8	// pattern starts with capital
3807 #define DUMPFLAG_ALLCAP	    16	// pattern is all capitals
3808 
3809 /*
3810  * ":spelldump"
3811  */
3812     void
3813 ex_spelldump(exarg_T *eap)
3814 {
3815     char_u  *spl;
3816     long    dummy;
3817 
3818     if (no_spell_checking(curwin))
3819 	return;
3820     get_option_value((char_u*)"spl", &dummy, &spl, OPT_LOCAL);
3821 
3822     // Create a new empty buffer in a new window.
3823     do_cmdline_cmd((char_u *)"new");
3824 
3825     // enable spelling locally in the new window
3826     set_option_value((char_u*)"spell", TRUE, (char_u*)"", OPT_LOCAL);
3827     set_option_value((char_u*)"spl",  dummy, spl, OPT_LOCAL);
3828     vim_free(spl);
3829 
3830     if (!BUFEMPTY())
3831 	return;
3832 
3833     spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
3834 
3835     // Delete the empty line that we started with.
3836     if (curbuf->b_ml.ml_line_count > 1)
3837 	ml_delete(curbuf->b_ml.ml_line_count);
3838 
3839     redraw_later(NOT_VALID);
3840 }
3841 
3842 /*
3843  * Go through all possible words and:
3844  * 1. When "pat" is NULL: dump a list of all words in the current buffer.
3845  *	"ic" and "dir" are not used.
3846  * 2. When "pat" is not NULL: add matching words to insert mode completion.
3847  */
3848     void
3849 spell_dump_compl(
3850     char_u	*pat,	    // leading part of the word
3851     int		ic,	    // ignore case
3852     int		*dir,	    // direction for adding matches
3853     int		dumpflags_arg)	// DUMPFLAG_*
3854 {
3855     langp_T	*lp;
3856     slang_T	*slang;
3857     idx_T	arridx[MAXWLEN];
3858     int		curi[MAXWLEN];
3859     char_u	word[MAXWLEN];
3860     int		c;
3861     char_u	*byts;
3862     idx_T	*idxs;
3863     linenr_T	lnum = 0;
3864     int		round;
3865     int		depth;
3866     int		n;
3867     int		flags;
3868     char_u	*region_names = NULL;	    // region names being used
3869     int		do_region = TRUE;	    // dump region names and numbers
3870     char_u	*p;
3871     int		lpi;
3872     int		dumpflags = dumpflags_arg;
3873     int		patlen;
3874 
3875     // When ignoring case or when the pattern starts with capital pass this on
3876     // to dump_word().
3877     if (pat != NULL)
3878     {
3879 	if (ic)
3880 	    dumpflags |= DUMPFLAG_ICASE;
3881 	else
3882 	{
3883 	    n = captype(pat, NULL);
3884 	    if (n == WF_ONECAP)
3885 		dumpflags |= DUMPFLAG_ONECAP;
3886 	    else if (n == WF_ALLCAP && (int)STRLEN(pat) > mb_ptr2len(pat))
3887 		dumpflags |= DUMPFLAG_ALLCAP;
3888 	}
3889     }
3890 
3891     // Find out if we can support regions: All languages must support the same
3892     // regions or none at all.
3893     for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
3894     {
3895 	lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
3896 	p = lp->lp_slang->sl_regions;
3897 	if (p[0] != 0)
3898 	{
3899 	    if (region_names == NULL)	    // first language with regions
3900 		region_names = p;
3901 	    else if (STRCMP(region_names, p) != 0)
3902 	    {
3903 		do_region = FALSE;	    // region names are different
3904 		break;
3905 	    }
3906 	}
3907     }
3908 
3909     if (do_region && region_names != NULL)
3910     {
3911 	if (pat == NULL)
3912 	{
3913 	    vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
3914 	    ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
3915 	}
3916     }
3917     else
3918 	do_region = FALSE;
3919 
3920     /*
3921      * Loop over all files loaded for the entries in 'spelllang'.
3922      */
3923     for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
3924     {
3925 	lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
3926 	slang = lp->lp_slang;
3927 	if (slang->sl_fbyts == NULL)	    // reloading failed
3928 	    continue;
3929 
3930 	if (pat == NULL)
3931 	{
3932 	    vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
3933 	    ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
3934 	}
3935 
3936 	// When matching with a pattern and there are no prefixes only use
3937 	// parts of the tree that match "pat".
3938 	if (pat != NULL && slang->sl_pbyts == NULL)
3939 	    patlen = (int)STRLEN(pat);
3940 	else
3941 	    patlen = -1;
3942 
3943 	// round 1: case-folded tree
3944 	// round 2: keep-case tree
3945 	for (round = 1; round <= 2; ++round)
3946 	{
3947 	    if (round == 1)
3948 	    {
3949 		dumpflags &= ~DUMPFLAG_KEEPCASE;
3950 		byts = slang->sl_fbyts;
3951 		idxs = slang->sl_fidxs;
3952 	    }
3953 	    else
3954 	    {
3955 		dumpflags |= DUMPFLAG_KEEPCASE;
3956 		byts = slang->sl_kbyts;
3957 		idxs = slang->sl_kidxs;
3958 	    }
3959 	    if (byts == NULL)
3960 		continue;		// array is empty
3961 
3962 	    depth = 0;
3963 	    arridx[0] = 0;
3964 	    curi[0] = 1;
3965 	    while (depth >= 0 && !got_int
3966 				  && (pat == NULL || !ins_compl_interrupted()))
3967 	    {
3968 		if (curi[depth] > byts[arridx[depth]])
3969 		{
3970 		    // Done all bytes at this node, go up one level.
3971 		    --depth;
3972 		    line_breakcheck();
3973 		    ins_compl_check_keys(50, FALSE);
3974 		}
3975 		else
3976 		{
3977 		    // Do one more byte at this node.
3978 		    n = arridx[depth] + curi[depth];
3979 		    ++curi[depth];
3980 		    c = byts[n];
3981 		    if (c == 0)
3982 		    {
3983 			// End of word, deal with the word.
3984 			// Don't use keep-case words in the fold-case tree,
3985 			// they will appear in the keep-case tree.
3986 			// Only use the word when the region matches.
3987 			flags = (int)idxs[n];
3988 			if ((round == 2 || (flags & WF_KEEPCAP) == 0)
3989 				&& (flags & WF_NEEDCOMP) == 0
3990 				&& (do_region
3991 				    || (flags & WF_REGION) == 0
3992 				    || (((unsigned)flags >> 16)
3993 						       & lp->lp_region) != 0))
3994 			{
3995 			    word[depth] = NUL;
3996 			    if (!do_region)
3997 				flags &= ~WF_REGION;
3998 
3999 			    // Dump the basic word if there is no prefix or
4000 			    // when it's the first one.
4001 			    c = (unsigned)flags >> 24;
4002 			    if (c == 0 || curi[depth] == 2)
4003 			    {
4004 				dump_word(slang, word, pat, dir,
4005 						      dumpflags, flags, lnum);
4006 				if (pat == NULL)
4007 				    ++lnum;
4008 			    }
4009 
4010 			    // Apply the prefix, if there is one.
4011 			    if (c != 0)
4012 				lnum = dump_prefixes(slang, word, pat, dir,
4013 						      dumpflags, flags, lnum);
4014 			}
4015 		    }
4016 		    else
4017 		    {
4018 			// Normal char, go one level deeper.
4019 			word[depth++] = c;
4020 			arridx[depth] = idxs[n];
4021 			curi[depth] = 1;
4022 
4023 			// Check if this characters matches with the pattern.
4024 			// If not skip the whole tree below it.
4025 			// Always ignore case here, dump_word() will check
4026 			// proper case later.  This isn't exactly right when
4027 			// length changes for multi-byte characters with
4028 			// ignore case...
4029 			if (depth <= patlen
4030 					&& MB_STRNICMP(word, pat, depth) != 0)
4031 			    --depth;
4032 		    }
4033 		}
4034 	    }
4035 	}
4036     }
4037 }
4038 
4039 /*
4040  * Dump one word: apply case modifications and append a line to the buffer.
4041  * When "lnum" is zero add insert mode completion.
4042  */
4043     static void
4044 dump_word(
4045     slang_T	*slang,
4046     char_u	*word,
4047     char_u	*pat,
4048     int		*dir,
4049     int		dumpflags,
4050     int		wordflags,
4051     linenr_T	lnum)
4052 {
4053     int		keepcap = FALSE;
4054     char_u	*p;
4055     char_u	*tw;
4056     char_u	cword[MAXWLEN];
4057     char_u	badword[MAXWLEN + 10];
4058     int		i;
4059     int		flags = wordflags;
4060 
4061     if (dumpflags & DUMPFLAG_ONECAP)
4062 	flags |= WF_ONECAP;
4063     if (dumpflags & DUMPFLAG_ALLCAP)
4064 	flags |= WF_ALLCAP;
4065 
4066     if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
4067     {
4068 	// Need to fix case according to "flags".
4069 	make_case_word(word, cword, flags);
4070 	p = cword;
4071     }
4072     else
4073     {
4074 	p = word;
4075 	if ((dumpflags & DUMPFLAG_KEEPCASE)
4076 		&& ((captype(word, NULL) & WF_KEEPCAP) == 0
4077 						 || (flags & WF_FIXCAP) != 0))
4078 	    keepcap = TRUE;
4079     }
4080     tw = p;
4081 
4082     if (pat == NULL)
4083     {
4084 	// Add flags and regions after a slash.
4085 	if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
4086 	{
4087 	    STRCPY(badword, p);
4088 	    STRCAT(badword, "/");
4089 	    if (keepcap)
4090 		STRCAT(badword, "=");
4091 	    if (flags & WF_BANNED)
4092 		STRCAT(badword, "!");
4093 	    else if (flags & WF_RARE)
4094 		STRCAT(badword, "?");
4095 	    if (flags & WF_REGION)
4096 		for (i = 0; i < 7; ++i)
4097 		    if (flags & (0x10000 << i))
4098 			sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
4099 	    p = badword;
4100 	}
4101 
4102 	if (dumpflags & DUMPFLAG_COUNT)
4103 	{
4104 	    hashitem_T  *hi;
4105 
4106 	    // Include the word count for ":spelldump!".
4107 	    hi = hash_find(&slang->sl_wordcount, tw);
4108 	    if (!HASHITEM_EMPTY(hi))
4109 	    {
4110 		vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
4111 						     tw, HI2WC(hi)->wc_count);
4112 		p = IObuff;
4113 	    }
4114 	}
4115 
4116 	ml_append(lnum, p, (colnr_T)0, FALSE);
4117     }
4118     else if (((dumpflags & DUMPFLAG_ICASE)
4119 		    ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
4120 		    : STRNCMP(p, pat, STRLEN(pat)) == 0)
4121 		&& ins_compl_add_infercase(p, (int)STRLEN(p),
4122 					  p_ic, NULL, *dir, FALSE) == OK)
4123 	// if dir was BACKWARD then honor it just once
4124 	*dir = FORWARD;
4125 }
4126 
4127 /*
4128  * For ":spelldump": Find matching prefixes for "word".  Prepend each to
4129  * "word" and append a line to the buffer.
4130  * When "lnum" is zero add insert mode completion.
4131  * Return the updated line number.
4132  */
4133     static linenr_T
4134 dump_prefixes(
4135     slang_T	*slang,
4136     char_u	*word,	    // case-folded word
4137     char_u	*pat,
4138     int		*dir,
4139     int		dumpflags,
4140     int		flags,	    // flags with prefix ID
4141     linenr_T	startlnum)
4142 {
4143     idx_T	arridx[MAXWLEN];
4144     int		curi[MAXWLEN];
4145     char_u	prefix[MAXWLEN];
4146     char_u	word_up[MAXWLEN];
4147     int		has_word_up = FALSE;
4148     int		c;
4149     char_u	*byts;
4150     idx_T	*idxs;
4151     linenr_T	lnum = startlnum;
4152     int		depth;
4153     int		n;
4154     int		len;
4155     int		i;
4156 
4157     // If the word starts with a lower-case letter make the word with an
4158     // upper-case letter in word_up[].
4159     c = PTR2CHAR(word);
4160     if (SPELL_TOUPPER(c) != c)
4161     {
4162 	onecap_copy(word, word_up, TRUE);
4163 	has_word_up = TRUE;
4164     }
4165 
4166     byts = slang->sl_pbyts;
4167     idxs = slang->sl_pidxs;
4168     if (byts != NULL)		// array not is empty
4169     {
4170 	/*
4171 	 * Loop over all prefixes, building them byte-by-byte in prefix[].
4172 	 * When at the end of a prefix check that it supports "flags".
4173 	 */
4174 	depth = 0;
4175 	arridx[0] = 0;
4176 	curi[0] = 1;
4177 	while (depth >= 0 && !got_int)
4178 	{
4179 	    n = arridx[depth];
4180 	    len = byts[n];
4181 	    if (curi[depth] > len)
4182 	    {
4183 		// Done all bytes at this node, go up one level.
4184 		--depth;
4185 		line_breakcheck();
4186 	    }
4187 	    else
4188 	    {
4189 		// Do one more byte at this node.
4190 		n += curi[depth];
4191 		++curi[depth];
4192 		c = byts[n];
4193 		if (c == 0)
4194 		{
4195 		    // End of prefix, find out how many IDs there are.
4196 		    for (i = 1; i < len; ++i)
4197 			if (byts[n + i] != 0)
4198 			    break;
4199 		    curi[depth] += i - 1;
4200 
4201 		    c = valid_word_prefix(i, n, flags, word, slang, FALSE);
4202 		    if (c != 0)
4203 		    {
4204 			vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
4205 			dump_word(slang, prefix, pat, dir, dumpflags,
4206 				(c & WF_RAREPFX) ? (flags | WF_RARE)
4207 							       : flags, lnum);
4208 			if (lnum != 0)
4209 			    ++lnum;
4210 		    }
4211 
4212 		    // Check for prefix that matches the word when the
4213 		    // first letter is upper-case, but only if the prefix has
4214 		    // a condition.
4215 		    if (has_word_up)
4216 		    {
4217 			c = valid_word_prefix(i, n, flags, word_up, slang,
4218 									TRUE);
4219 			if (c != 0)
4220 			{
4221 			    vim_strncpy(prefix + depth, word_up,
4222 							 MAXWLEN - depth - 1);
4223 			    dump_word(slang, prefix, pat, dir, dumpflags,
4224 				    (c & WF_RAREPFX) ? (flags | WF_RARE)
4225 							       : flags, lnum);
4226 			    if (lnum != 0)
4227 				++lnum;
4228 			}
4229 		    }
4230 		}
4231 		else
4232 		{
4233 		    // Normal char, go one level deeper.
4234 		    prefix[depth++] = c;
4235 		    arridx[depth] = idxs[n];
4236 		    curi[depth] = 1;
4237 		}
4238 	    }
4239 	}
4240     }
4241 
4242     return lnum;
4243 }
4244 
4245 /*
4246  * Move "p" to the end of word "start".
4247  * Uses the spell-checking word characters.
4248  */
4249     char_u *
4250 spell_to_word_end(char_u *start, win_T *win)
4251 {
4252     char_u  *p = start;
4253 
4254     while (*p != NUL && spell_iswordp(p, win))
4255 	MB_PTR_ADV(p);
4256     return p;
4257 }
4258 
4259 /*
4260  * For Insert mode completion CTRL-X s:
4261  * Find start of the word in front of column "startcol".
4262  * We don't check if it is badly spelled, with completion we can only change
4263  * the word in front of the cursor.
4264  * Returns the column number of the word.
4265  */
4266     int
4267 spell_word_start(int startcol)
4268 {
4269     char_u	*line;
4270     char_u	*p;
4271     int		col = 0;
4272 
4273     if (no_spell_checking(curwin))
4274 	return startcol;
4275 
4276     // Find a word character before "startcol".
4277     line = ml_get_curline();
4278     for (p = line + startcol; p > line; )
4279     {
4280 	MB_PTR_BACK(line, p);
4281 	if (spell_iswordp_nmw(p, curwin))
4282 	    break;
4283     }
4284 
4285     // Go back to start of the word.
4286     while (p > line)
4287     {
4288 	col = (int)(p - line);
4289 	MB_PTR_BACK(line, p);
4290 	if (!spell_iswordp(p, curwin))
4291 	    break;
4292 	col = 0;
4293     }
4294 
4295     return col;
4296 }
4297 
4298 /*
4299  * Need to check for 'spellcapcheck' now, the word is removed before
4300  * expand_spelling() is called.  Therefore the ugly global variable.
4301  */
4302 static int spell_expand_need_cap;
4303 
4304     void
4305 spell_expand_check_cap(colnr_T col)
4306 {
4307     spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
4308 }
4309 
4310 /*
4311  * Get list of spelling suggestions.
4312  * Used for Insert mode completion CTRL-X ?.
4313  * Returns the number of matches.  The matches are in "matchp[]", array of
4314  * allocated strings.
4315  */
4316     int
4317 expand_spelling(
4318     linenr_T	lnum UNUSED,
4319     char_u	*pat,
4320     char_u	***matchp)
4321 {
4322     garray_T	ga;
4323 
4324     spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
4325     *matchp = ga.ga_data;
4326     return ga.ga_len;
4327 }
4328 
4329 /*
4330  * Return TRUE if "val" is a valid 'spelllang' value.
4331  */
4332     int
4333 valid_spelllang(char_u *val)
4334 {
4335     return valid_name(val, ".-_,@");
4336 }
4337 
4338 /*
4339  * Return TRUE if "val" is a valid 'spellfile' value.
4340  */
4341     int
4342 valid_spellfile(char_u *val)
4343 {
4344     char_u *s;
4345 
4346     for (s = val; *s != NUL; ++s)
4347 	if (!vim_isfilec(*s) && *s != ',')
4348 	    return FALSE;
4349     return TRUE;
4350 }
4351 
4352 /*
4353  * Handle side effects of setting 'spell'.
4354  * Return an error message or NULL for success.
4355  */
4356     char *
4357 did_set_spell_option(int is_spellfile)
4358 {
4359     char    *errmsg = NULL;
4360     win_T   *wp;
4361     int	    l;
4362 
4363     if (is_spellfile)
4364     {
4365 	l = (int)STRLEN(curwin->w_s->b_p_spf);
4366 	if (l > 0 && (l < 4
4367 			|| STRCMP(curwin->w_s->b_p_spf + l - 4, ".add") != 0))
4368 	    errmsg = e_invarg;
4369     }
4370 
4371     if (errmsg == NULL)
4372     {
4373 	FOR_ALL_WINDOWS(wp)
4374 	    if (wp->w_buffer == curbuf && wp->w_p_spell)
4375 	    {
4376 		errmsg = did_set_spelllang(wp);
4377 		break;
4378 	    }
4379     }
4380     return errmsg;
4381 }
4382 
4383 /*
4384  * Set curbuf->b_cap_prog to the regexp program for 'spellcapcheck'.
4385  * Return error message when failed, NULL when OK.
4386  */
4387     char *
4388 compile_cap_prog(synblock_T *synblock)
4389 {
4390     regprog_T   *rp = synblock->b_cap_prog;
4391     char_u	*re;
4392 
4393     if (synblock->b_p_spc == NULL || *synblock->b_p_spc == NUL)
4394 	synblock->b_cap_prog = NULL;
4395     else
4396     {
4397 	// Prepend a ^ so that we only match at one column
4398 	re = concat_str((char_u *)"^", synblock->b_p_spc);
4399 	if (re != NULL)
4400 	{
4401 	    synblock->b_cap_prog = vim_regcomp(re, RE_MAGIC);
4402 	    vim_free(re);
4403 	    if (synblock->b_cap_prog == NULL)
4404 	    {
4405 		synblock->b_cap_prog = rp; // restore the previous program
4406 		return e_invarg;
4407 	    }
4408 	}
4409     }
4410 
4411     vim_regfree(rp);
4412     return NULL;
4413 }
4414 
4415 #endif  // FEAT_SPELL
4416