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