blob: de478a3ed0664b39b23563a616b123137f828a7e [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaare19defe2005-03-21 08:23:33 +00002 *
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
Bram Moolenaarfc735152005-03-22 22:54:12 +000012 *
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020013 * See spellfile.c for the Vim spell file format.
14 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000015 * 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).
Bram Moolenaar9f30f502005-06-14 22:01:04 +000018 *
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".
Bram Moolenaar51485f02005-06-04 21:55:20 +000035 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000036 * There are two word trees: one with case-folded words and one with words in
Bram Moolenaar51485f02005-06-04 21:55:20 +000037 * original case. The second one is only used for keep-case words and is
38 * usually small.
39 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +000040 * There is one additional tree for when not all prefixes are applied when
Bram Moolenaar1d73c882005-06-19 22:48:47 +000041 * 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 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000046 * Thanks to Olaf Seibert for providing an example implementation of this tree
47 * and the compression mechanism.
Bram Moolenaar4770d092006-01-12 23:22:24 +000048 * 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
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000051 *
52 * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000053 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000054 * Why doesn't Vim use aspell/ispell/myspell/etc.?
55 * See ":help develop-spell".
56 */
57
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020058#define IN_SPELL_C
Bram Moolenaare19defe2005-03-21 08:23:33 +000059#include "vim.h"
60
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000061#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaare19defe2005-03-21 08:23:33 +000062
Bram Moolenaar4770d092006-01-12 23:22:24 +000063#ifndef UNIX /* it's in os_unix.h for Unix */
64# include <time.h> /* for time_t */
65#endif
66
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000067#define REGION_ALL 0xff /* word valid in all regions */
68
Bram Moolenaar4770d092006-01-12 23:22:24 +000069#define VIMSUGMAGIC "VIMsug" /* string at start of Vim .sug file */
70#define VIMSUGMAGICL 6
71#define VIMSUGVERSION 1
72
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000073/* Result values. Lower number is accepted over higher one. */
74#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000075#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000076#define SP_RARE 1
77#define SP_LOCAL 2
78#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000079
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000080/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000081 * Structure to store info for word matching.
82 */
83typedef struct matchinf_S
84{
85 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000086
87 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000088 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +000089 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000090 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +000091 char_u *mi_cend; /* char after what was used for
92 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000093
94 /* case-folded text */
95 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +000096 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000097
Bram Moolenaar1d73c882005-06-19 22:48:47 +000098 /* for when checking word after a prefix */
99 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000100 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000101 int mi_prefcnt; /* number of entries at mi_prefarridx */
102 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000103 int mi_cprefixlen; /* byte length of prefix in original
104 case */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000105
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000106 /* for when checking a compound word */
107 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000108 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
109 int mi_complen; /* nr of compound words used */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000110 int mi_compextra; /* nr of COMPOUNDROOT words */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000111
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000112 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000113 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000114 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200115 win_T *mi_win; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000116
117 /* for NOBREAK */
118 int mi_result2; /* "mi_resul" without following word */
119 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000120} matchinf_T;
121
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000122
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100123static int spell_mb_isword_class(int cl, win_T *wp);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000124
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000125/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000126#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 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000131
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100132static void find_word(matchinf_T *mip, int mode);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void find_prefix(matchinf_T *mip, int mode);
134static int fold_more(matchinf_T *mip);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100135static void spell_load_cb(char_u *fname, void *cookie);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100136static int count_syllables(slang_T *slang, char_u *word);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void clear_midword(win_T *buf);
138static void use_midword(slang_T *lp, win_T *buf);
139static int find_region(char_u *rp, char_u *region);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100140static void spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res);
141static void spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100142static void spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static void dump_word(slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum);
144static linenr_T dump_prefixes(slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T startlnum);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000145
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000146/*
147 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000148 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000149 * "*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.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000151 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000152 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000153 * "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.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000157 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000158 * 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
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100162spell_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 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000168{
169 matchinf_T mi; /* Most things are put in "mi" so that it can
170 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000171 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000172 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000173 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000174 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +0000175 int count_word = docount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000176
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000177 /* 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;
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000181
182 /* Return here when loading language files failed. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200183 if (wp->w_s->b_langp.ga_len == 0)
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000184 return 1;
185
Bram Moolenaar5195e452005-08-19 20:32:47 +0000186 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000187
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000188 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +0000189 * 0X99FF. But always do check spelling to find "3GPP" and "11
190 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000191 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000192 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100193 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'))
Bram Moolenaar3982c542005-06-08 21:56:31 +0000196 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000197 else
198 mi.mi_end = skipdigits(ptr);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000199 nrlen = (int)(mi.mi_end - ptr);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000200 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000201
Bram Moolenaar0c405862005-06-22 22:26:26 +0000202 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000203 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +0000204 mi.mi_fend = ptr;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200205 if (spell_iswordp(mi.mi_fend, wp))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000206 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000207 do
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100208 MB_PTR_ADV(mi.mi_fend);
Bram Moolenaarabab0b02019-03-30 18:47:01 +0100209 while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000210
Bram Moolenaar860cae12010-06-05 23:22:07 +0200211 if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000212 {
213 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000214 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000215 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000216 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000217 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000218 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000219 if (capcol != NULL)
220 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000221
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000222 /* We always use the characters up to the next non-word character,
223 * also for bad words. */
224 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000225
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000226 /* Check caps type later. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200227 mi.mi_capflags = 0;
228 mi.mi_cend = NULL;
229 mi.mi_win = wp;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000230
Bram Moolenaar5195e452005-08-19 20:32:47 +0000231 /* case-fold the word with one non-word character, so that we can check
232 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000233 if (*mi.mi_fend != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100234 MB_PTR_ADV(mi.mi_fend);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000235
236 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
237 MAXWLEN + 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000238 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000239
240 /* The word is bad unless we recognize it. */
241 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +0000242 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000243
244 /*
245 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000246 * We check them all, because a word may be matched longer in another
247 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000248 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200249 for (lpi = 0; lpi < wp->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000250 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200251 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, lpi);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000252
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
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000258 /* 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. */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000265 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +0000266
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 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000275
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 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000283 }
284
285 if (mi.mi_result != SP_OK)
286 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000287 /* If we found a number skip over it. Allows for "42nd". Do flag
288 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000289 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000290 {
291 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
292 return nrlen;
293 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000294
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). */
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100297 else if (!spell_iswordp_nmw(ptr, wp))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000298 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200299 if (capcol != NULL && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000300 {
301 regmatch_T regmatch;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100302 int r;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000303
304 /* Check for end of sentence. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200305 regmatch.regprog = wp->w_s->b_cap_prog;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000306 regmatch.rm_ic = FALSE;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100307 r = vim_regexec(&regmatch, ptr, 0);
308 wp->w_s->b_cap_prog = regmatch.regprog;
309 if (r)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000310 *capcol = (int)(regmatch.endp[0] - ptr);
311 }
312
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000313 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000314 return (*mb_ptr2len)(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000315 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000316 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000317 else if (mi.mi_end == ptr)
318 /* Always include at least one character. Required for when there
319 * is a mixup in "midword". */
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100320 MB_PTR_ADV(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +0000321 else if (mi.mi_result == SP_BAD
Bram Moolenaar860cae12010-06-05 23:22:07 +0200322 && LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak)
Bram Moolenaar78622822005-08-23 21:00:13 +0000323 {
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. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200329 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000330 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +0000331 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000332 p = mi.mi_word;
333 fp = mi.mi_fword;
334 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +0000335 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100336 MB_PTR_ADV(p);
337 MB_PTR_ADV(fp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000338 if (p >= mi.mi_end)
339 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000340 mi.mi_compoff = (int)(fp - mi.mi_fword);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000341 find_word(&mi, FIND_COMPOUND);
342 if (mi.mi_result != SP_BAD)
343 {
344 mi.mi_end = p;
345 break;
346 }
Bram Moolenaar78622822005-08-23 21:00:13 +0000347 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000348 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +0000349 }
Bram Moolenaar78622822005-08-23 21:00:13 +0000350 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000351
352 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000353 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000354 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000355 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000356 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000357 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000358 }
359
Bram Moolenaar5195e452005-08-19 20:32:47 +0000360 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. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000363 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000364 return wrongcaplen;
365 }
366
Bram Moolenaar51485f02005-06-04 21:55:20 +0000367 return (int)(mi.mi_end - ptr);
368}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000369
Bram Moolenaar51485f02005-06-04 21:55:20 +0000370/*
371 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000372 * 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.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000376 *
377 * For a match mip->mi_result is updated.
378 */
379 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100380find_word(matchinf_T *mip, int mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000381{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000382 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000383 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000384 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000385 int endidxcnt = 0;
386 int len;
387 int wlen = 0;
388 int flen;
389 int c;
390 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000391 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000392 char_u *s;
Bram Moolenaare52325c2005-08-22 22:54:29 +0000393 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000394 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000395 slang_T *slang = mip->mi_lp->lp_slang;
396 unsigned flags;
397 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000398 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000399 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000400 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +0000401 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000402
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000403 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000404 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000405 /* 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;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000410
411 if (mode == FIND_KEEPCOMPOUND)
412 /* Skip over the previously found word(s). */
413 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000414 }
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;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000422
423 if (mode == FIND_PREFIX)
424 {
425 /* Skip over the prefix. */
426 wlen = mip->mi_prefixlen;
427 flen -= mip->mi_prefixlen;
428 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000429 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
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000436 }
437
Bram Moolenaar51485f02005-06-04 21:55:20 +0000438 if (byts == NULL)
439 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000440
Bram Moolenaar51485f02005-06-04 21:55:20 +0000441 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000442 * 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.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000446 */
447 for (;;)
448 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000449 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000450 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000451
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 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000458 if (endidxcnt == MAXWLEN)
459 {
460 /* Must be a corrupted spell file. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100461 emsg(_(e_format));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000462 return;
463 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000464 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];
Bram Moolenaar0c405862005-06-22 22:26:26 +0000485 if (c == TAB) /* <Tab> is handled like <Space> */
486 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +0000487 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;
Bram Moolenaar0c405862005-06-22 22:26:26 +0000511
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 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000526 }
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
Bram Moolenaar51485f02005-06-04 21:55:20 +0000538 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
539 continue; /* not at first byte of character */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200540 if (spell_iswordp(ptr + wlen, mip->mi_win))
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000541 {
Bram Moolenaar78622822005-08-23 21:00:13 +0000542 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000543 continue; /* next char is a word character */
544 word_ends = FALSE;
545 }
546 else
547 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000548 /* The prefix flag is before compound flags. Once a valid prefix flag
549 * has been found we try compound flags. */
550 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000551
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000552 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000553 {
554 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000555 * when folding case. This can be slow, take a shortcut when the
556 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000557 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000558 if (STRNCMP(ptr, p, wlen) != 0)
559 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100560 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s))
561 MB_PTR_ADV(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000562 wlen = (int)(p - mip->mi_word);
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000563 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000564 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000565
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000566 /* 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)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000573 {
574 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000575
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000576 /* 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)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000581 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000582 if (mip->mi_cend != mip->mi_word + wlen)
583 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000584 /* mi_capflags was set for a different word length, need
585 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000586 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000587 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000588 }
589
Bram Moolenaar0c405862005-06-22 22:26:26 +0000590 if (mip->mi_capflags == WF_KEEPCAP
591 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000592 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000593 }
594
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000595 /* 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
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000597 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000598 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000599 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000600 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000601 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +0000602 mip->mi_word + mip->mi_cprefixlen, slang,
603 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000604 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000605 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000606
607 /* Use the WF_RARE flag for a rare prefix. */
608 if (c & WF_RAREPFX)
609 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000610 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000611 }
612
Bram Moolenaar78622822005-08-23 21:00:13 +0000613 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))
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000627 {
Bram Moolenaar2113a1d2006-09-11 19:38:08 +0000628 /* If there is no compound flag or the word is shorter than
Bram Moolenaar5195e452005-08-19 20:32:47 +0000629 * COMPOUNDMIN reject it quickly.
630 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000631 * that's too short... Myspell compatibility requires this
632 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000633 if (((unsigned)flags >> 24) == 0
634 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000635 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000636 /* For multi-byte chars check character length against
637 * COMPOUNDMIN. */
638 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000639 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000640 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
641 wlen - mip->mi_compoff) < slang->sl_compminlen)
642 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000643
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000644 /* Limit the number of compound words to COMPOUNDWORDMAX if no
Bram Moolenaare52325c2005-08-22 22:54:29 +0000645 * maximum for syllables is specified. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000646 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
647 > slang->sl_compmax
Bram Moolenaare52325c2005-08-22 22:54:29 +0000648 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000649 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000650
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000651 /* 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
Bram Moolenaard12a1322005-08-21 22:08:24 +0000658 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000659 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +0000660 ? slang->sl_compstartflags
661 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +0000662 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000663 continue;
664
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000665 /* 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
Bram Moolenaare52325c2005-08-22 22:54:29 +0000670 if (mode == FIND_COMPOUND)
671 {
672 int capflags;
673
674 /* Need to check the caps type of the appended compound
675 * word. */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000676 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;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100681 for (s = ptr; s < ptr + mip->mi_compoff; MB_PTR_ADV(s))
682 MB_PTR_ADV(p);
Bram Moolenaare52325c2005-08-22 22:54:29 +0000683 }
684 else
Bram Moolenaare52325c2005-08-22 22:54:29 +0000685 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. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100697 MB_PTR_BACK(mip->mi_word, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100698 if (spell_iswordp_nmw(p, mip->mi_win)
Bram Moolenaare52325c2005-08-22 22:54:29 +0000699 ? capflags == WF_ONECAP
700 : (flags & WF_ONECAP) != 0
701 && capflags != WF_ONECAP)
702 continue;
703 }
704 }
705
Bram Moolenaar5195e452005-08-19 20:32:47 +0000706 /* If the word ends the sequence of compound flags of the
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000707 * words must match with one of the COMPOUNDRULE items and
Bram Moolenaar5195e452005-08-19 20:32:47 +0000708 * 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 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000726 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;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000731 }
732
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000733 /* Check NEEDCOMPOUND: can't use word without compounding. */
734 else if (flags & WF_NEEDCOMP)
735 continue;
736
Bram Moolenaar78622822005-08-23 21:00:13 +0000737 nobreak_result = SP_OK;
738
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000739 if (!word_ends)
740 {
Bram Moolenaar78622822005-08-23 21:00:13 +0000741 int save_result = mip->mi_result;
742 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000743 langp_T *save_lp = mip->mi_lp;
744 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +0000745
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.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000750 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +0000751 if (slang->sl_nobreak)
752 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000753
754 /* Find following word in case-folded tree. */
755 mip->mi_compoff = endlen[endidxcnt];
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000756 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 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100765 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s))
766 MB_PTR_ADV(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000767 mip->mi_compoff = (int)(p - mip->mi_fword);
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000768 }
769 }
Bram Moolenaarba534352016-04-21 09:20:26 +0200770#if 0 /* Disabled, see below */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000771 c = mip->mi_compoff;
Bram Moolenaarba534352016-04-21 09:20:26 +0200772#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000773 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000774 if (flags & WF_COMPROOT)
775 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000776
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000777 /* For NOBREAK we need to try all NOBREAK languages, at least
778 * to find the ".add" file(s). */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200779 for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +0000780 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000781 if (slang->sl_nobreak)
782 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200783 mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi);
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000784 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
785 || !mip->mi_lp->lp_slang->sl_nobreak)
786 continue;
787 }
Bram Moolenaard12a1322005-08-21 22:08:24 +0000788
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000789 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. */
Bram Moolenaar78622822005-08-23 21:00:13 +0000794 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
795 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000796 /* Find following word in keep-case tree. */
797 mip->mi_compoff = wlen;
798 find_word(mip, FIND_KEEPCOMPOUND);
799
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000800#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. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000803 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 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000809#endif
Bram Moolenaar78622822005-08-23 21:00:13 +0000810 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000811
812 if (!slang->sl_nobreak)
813 break;
Bram Moolenaar78622822005-08-23 21:00:13 +0000814 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000815 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000816 if (flags & WF_COMPROOT)
817 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000818 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000819
Bram Moolenaar78622822005-08-23 21:00:13 +0000820 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 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000832 }
833
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000834 if (flags & WF_BANNED)
835 res = SP_BANNED;
836 else if (flags & WF_REGION)
837 {
838 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000839 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000840 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
Bram Moolenaar78622822005-08-23 21:00:13 +0000849 /* 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)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000864 {
865 mip->mi_result = res;
866 mip->mi_end = mip->mi_word + wlen;
867 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000868 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000869 mip->mi_end = mip->mi_word + wlen;
870
Bram Moolenaar78622822005-08-23 21:00:13 +0000871 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000872 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000873 }
874
Bram Moolenaar78622822005-08-23 21:00:13 +0000875 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000876 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000877 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000878}
879
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000880/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000881 * 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 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +0200887 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100888match_checkcompoundpattern(
889 char_u *ptr,
890 int wlen,
891 garray_T *gap) /* &sl_comppat */
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000892{
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];
Bram Moolenaar19c9c762008-12-09 21:34:39 +0000905 len = (int)STRLEN(p);
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000906 if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0)
907 return TRUE;
908 }
909 }
910 return FALSE;
911}
912
913/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000914 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
915 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000916 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +0200917 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100918can_compound(slang_T *slang, char_u *word, char_u *flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000919{
Bram Moolenaar6de68532005-08-24 22:08:48 +0000920 char_u uflags[MAXWLEN * 2];
921 int i;
Bram Moolenaar6de68532005-08-24 22:08:48 +0000922 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000923
924 if (slang->sl_compprog == NULL)
925 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +0000926 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)
Bram Moolenaarace95982017-03-29 17:30:27 +0200931 p += utf_char2bytes(flags[i], p);
Bram Moolenaar6de68532005-08-24 22:08:48 +0000932 *p = NUL;
933 p = uflags;
934 }
935 else
Bram Moolenaar6de68532005-08-24 22:08:48 +0000936 p = flags;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100937 if (!vim_regexec_prog(&slang->sl_compprog, FALSE, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000938 return FALSE;
939
Bram Moolenaare52325c2005-08-22 22:54:29 +0000940 /* 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
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000942 * COMPOUNDWORDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000943 if (slang->sl_compsylmax < MAXWLEN
944 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +0000945 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000946 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000947}
948
949/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000950 * 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 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +0200955 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100956match_compoundrule(slang_T *slang, char_u *compflags)
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000957{
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/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001004 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1005 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001006 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001007 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02001008 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001009valid_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 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001016{
1017 int prefcnt;
1018 int pidx;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001019 regprog_T **rp;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001020 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001021
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001022 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001023 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
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001031 /* 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
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001036 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001037 * stored in the two bytes above the prefix ID byte. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001038 rp = &slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
1039 if (*rp != NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001040 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001041 if (!vim_regexec_prog(rp, FALSE, word, 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001042 continue;
1043 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001044 else if (cond_req)
1045 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001046
Bram Moolenaar53805d12005-08-01 07:08:33 +00001047 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001048 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001049 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001050 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001051}
1052
1053/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001054 * Check if the word at "mip->mi_word" has a matching prefix.
1055 * If it does, then check the following word.
1056 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001057 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1058 * prefix in a compound word.
1059 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001060 * For a match mip->mi_result is updated.
1061 */
1062 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001063find_prefix(matchinf_T *mip, int mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001064{
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
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001076 byts = slang->sl_pbyts;
1077 if (byts == NULL)
1078 return; /* array is empty */
1079
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001080 /* 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 */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001084 if (mode == FIND_COMPOUND)
1085 {
1086 /* Skip over the previously found word(s). */
1087 ptr += mip->mi_compoff;
1088 flen -= mip->mi_compoff;
1089 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001090 idxs = slang->sl_pidxs;
1091
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001092 /*
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;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001124 if (mode == FIND_COMPOUND)
1125 /* Skip over the previously found word(s). */
1126 mip->mi_prefixlen += mip->mi_compoff;
1127
Bram Moolenaar53805d12005-08-01 07:08:33 +00001128 if (has_mbyte)
1129 {
1130 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001131 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1132 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001133 }
1134 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001135 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001136 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
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001178 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001179 * Return the length of the folded chars in bytes.
1180 */
1181 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001182fold_more(matchinf_T *mip)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001183{
1184 int flen;
1185 char_u *p;
1186
1187 p = mip->mi_fend;
1188 do
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001189 MB_PTR_ADV(mip->mi_fend);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001190 while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001191
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001192 /* Include the non-word character so that we can check for the word end. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001193 if (*mip->mi_fend != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001194 MB_PTR_ADV(mip->mi_fend);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001195
1196 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1197 mip->mi_fword + mip->mi_fwordlen,
1198 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001199 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001200 mip->mi_fwordlen += flen;
1201 return flen;
1202}
1203
1204/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001205 * Check case flags for a word. Return TRUE if the word has the requested
1206 * case.
1207 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02001208 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001209spell_valid_case(
1210 int wordflags, /* flags for the checked word. */
1211 int treeflags) /* flags for the word in the spell tree */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001212{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001213 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001214 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001215 && ((treeflags & WF_ONECAP) == 0
1216 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001217}
1218
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001219/*
1220 * Return TRUE if spell checking is not enabled.
1221 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02001222 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001223no_spell_checking(win_T *wp)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001224{
Bram Moolenaar860cae12010-06-05 23:22:07 +02001225 if (!wp->w_p_spell || *wp->w_s->b_p_spl == NUL
1226 || wp->w_s->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001227 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001228 emsg(_("E756: Spell checking is not enabled"));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001229 return TRUE;
1230 }
1231 return FALSE;
1232}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001233
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001234/*
1235 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001236 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
1237 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001238 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1239 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00001240 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001241 */
1242 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001243spell_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
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001249 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001250{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001251 linenr_T lnum;
1252 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001253 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001254 char_u *line;
1255 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001256 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001257 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001258 int len;
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001259#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001260 int has_syntax = syntax_present(wp);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001261#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001262 int col;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001263 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001264 char_u *buf = NULL;
1265 int buflen = 0;
1266 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001267 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001268 int found_one = FALSE;
1269 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001270
Bram Moolenaar95529562005-08-25 21:21:38 +00001271 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00001272 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001273
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001274 /*
1275 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00001276 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001277 *
1278 * When searching backwards, we continue in the line to find the last
1279 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001280 *
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...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001284 */
Bram Moolenaar95529562005-08-25 21:21:38 +00001285 lnum = wp->w_cursor.lnum;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001286 CLEAR_POS(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001287
1288 while (!got_int)
1289 {
Bram Moolenaar95529562005-08-25 21:21:38 +00001290 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001291
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001292 len = (int)STRLEN(line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001293 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
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001302 /* 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)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001308 capcol = getwhitecols(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001309 else if (curline && wp == curwin)
1310 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001311 /* For spellbadword(): check if first word needs a capital. */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001312 col = getwhitecols(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001313 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 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001320
Bram Moolenaar0c405862005-06-22 22:26:26 +00001321 /* Copy the line into "buf" and append the start of the next line if
1322 * possible. */
1323 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00001324 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar5dd95a12006-05-13 12:09:24 +00001325 spell_cat_line(buf + STRLEN(buf),
1326 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001327
1328 p = buf + skip;
1329 endp = buf + len;
1330 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001331 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001332 /* When searching backward don't search after the cursor. Unless
1333 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001334 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00001335 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001336 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00001337 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001338 break;
1339
1340 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001341 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001342 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001343
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001344 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001345 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001346 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001347 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001348 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001349 /* When searching forward only accept a bad word after
1350 * the cursor. */
1351 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001352 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00001353 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001354 && (wrapped
1355 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00001356 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001357 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001358 {
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001359#ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00001360 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001361 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001362 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001363 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00001364 FALSE, &can_spell, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001365 if (!can_spell)
1366 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001367 }
1368 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001369#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001370 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001371
Bram Moolenaar51485f02005-06-04 21:55:20 +00001372 if (can_spell)
1373 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00001374 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001375 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001376 found_pos.col = (int)(p - buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001377 found_pos.coladd = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001378 if (dir == FORWARD)
1379 {
1380 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00001381 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001382 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001383 if (attrp != NULL)
1384 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001385 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001386 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001387 else if (curline)
1388 /* Insert mode completion: put cursor after
1389 * the bad word. */
1390 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001391 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001392 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001393 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00001394 else
1395 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001396 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001397 }
1398
Bram Moolenaar51485f02005-06-04 21:55:20 +00001399 /* advance to character after the word */
1400 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001401 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001402 }
1403
Bram Moolenaar5195e452005-08-19 20:32:47 +00001404 if (dir == BACKWARD && found_pos.lnum != 0)
1405 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001406 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00001407 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001408 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001409 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001410 }
1411
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001412 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001413 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001414
Bram Moolenaard3f78dc2017-02-25 14:21:10 +01001415 /* 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
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001420 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001421 if (dir == BACKWARD)
1422 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001423 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;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001433 if (!shortmess(SHM_SEARCH))
1434 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001435 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001436 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001437 }
1438 else
1439 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001440 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;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001450 if (!shortmess(SHM_SEARCH))
1451 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001452 }
1453
1454 /* If we are back at the starting line and there is no match then
1455 * give up. */
Bram Moolenaard3f78dc2017-02-25 14:21:10 +01001456 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001457 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001458
1459 /* Skip the characters at the start of the next line that were
1460 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001461 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001462 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001463 else
1464 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001465
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001466 /* Capcol skips over the inserted space. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001467 --capcol;
1468
1469 /* But after empty line check first word in next line */
1470 if (*skipwhite(line) == NUL)
1471 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001472 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001473
1474 line_breakcheck();
1475 }
1476
Bram Moolenaar0c405862005-06-22 22:26:26 +00001477 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001478 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001479}
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.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00001484 * 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.
Bram Moolenaar0c405862005-06-22 22:26:26 +00001486 */
1487 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001488spell_cat_line(char_u *buf, char_u *line, int maxlen)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001489{
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 {
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00001499 /* 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 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00001507 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001508}
1509
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001510/*
1511 * Structure used for the cookie argument of do_in_runtimepath().
1512 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001513typedef 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
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001520/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001521 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001522 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001523 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001524 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001525spell_load_lang(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001526{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001527 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001528 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001529 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001530 int round;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001531
Bram Moolenaarb765d632005-06-07 21:00:02 +00001532 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001533 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001534 STRCPY(sl.sl_lang, lang);
1535 sl.sl_slang = NULL;
1536 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001537
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001538 /* 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)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001541 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001542 /*
1543 * Find the first spell file for "lang" in 'runtimepath' and load it.
1544 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001545 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001546#ifdef VMS
1547 "spell/%s_%s.spl",
1548#else
1549 "spell/%s.%s.spl",
1550#endif
1551 lang, spell_enc());
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001552 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001553
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,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001558#ifdef VMS
1559 "spell/%s_ascii.spl",
1560#else
1561 "spell/%s.ascii.spl",
1562#endif
1563 lang);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001564 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001565
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001566 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;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001571 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001572 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001573 }
1574
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001575 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001576 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001577 smsg(
Bram Moolenaar56f78042010-12-08 17:09:32 +01001578#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
Bram Moolenaar5195e452005-08-19 20:32:47 +00001583 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001584 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001585 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001586 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001587 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001588 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001589 do_in_runtimepath(fname_enc, DIP_ALL, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001590 }
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 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001597 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001598spell_enc(void)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001599{
1600
Bram Moolenaarb765d632005-06-07 21:00:02 +00001601 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1602 return p_enc;
Bram Moolenaarb765d632005-06-07 21:00:02 +00001603 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001604}
1605
1606/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001607 * Get the name of the .spl file for the internal wordlist into
1608 * "fname[MAXPATHL]".
1609 */
1610 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001611int_wordlist_spl(char_u *fname)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001612{
Bram Moolenaar56f78042010-12-08 17:09:32 +01001613 vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001614 int_wordlist, spell_enc());
1615}
1616
1617/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001618 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001619 * Caller must fill "sl_next".
1620 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001621 slang_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001622slang_alloc(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001623{
1624 slang_T *lp;
1625
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001626 lp = ALLOC_CLEAR_ONE(slang_T);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001627 if (lp != NULL)
1628 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001629 if (lang != NULL)
1630 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001631 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001632 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001633 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001634 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001635 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001636 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001637
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001638 return lp;
1639}
1640
1641/*
1642 * Free the contents of an slang_T and the structure itself.
1643 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001644 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001645slang_free(slang_T *lp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001646{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001647 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001648 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 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001656 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001657slang_clear(slang_T *lp)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001658{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001659 garray_T *gap;
1660 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001661 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001662 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001663 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001664
Bram Moolenaard23a8232018-02-10 18:45:26 +01001665 VIM_CLEAR(lp->sl_fbyts);
1666 VIM_CLEAR(lp->sl_kbyts);
1667 VIM_CLEAR(lp->sl_pbyts);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001668
Bram Moolenaard23a8232018-02-10 18:45:26 +01001669 VIM_CLEAR(lp->sl_fidxs);
1670 VIM_CLEAR(lp->sl_kidxs);
1671 VIM_CLEAR(lp->sl_pidxs);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001672
Bram Moolenaar4770d092006-01-12 23:22:24 +00001673 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001674 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001675 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);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001683 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001684
1685 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001686 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001687 {
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 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001694 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);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001702 vim_free(smp->sm_lead_w);
1703 vim_free(smp->sm_oneof_w);
1704 vim_free(smp->sm_to_w);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001705 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001706 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001707
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001708 for (i = 0; i < lp->sl_prefixcnt; ++i)
Bram Moolenaar473de612013-06-08 18:19:48 +02001709 vim_regfree(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001710 lp->sl_prefixcnt = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001711 VIM_CLEAR(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001712
Bram Moolenaard23a8232018-02-10 18:45:26 +01001713 VIM_CLEAR(lp->sl_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001714
Bram Moolenaard23a8232018-02-10 18:45:26 +01001715 VIM_CLEAR(lp->sl_midword);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001716
Bram Moolenaar473de612013-06-08 18:19:48 +02001717 vim_regfree(lp->sl_compprog);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001718 lp->sl_compprog = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001719 VIM_CLEAR(lp->sl_comprules);
1720 VIM_CLEAR(lp->sl_compstartflags);
1721 VIM_CLEAR(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001722
Bram Moolenaard23a8232018-02-10 18:45:26 +01001723 VIM_CLEAR(lp->sl_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001724 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001725
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001726 ga_clear_strings(&lp->sl_comppat);
1727
Bram Moolenaar4770d092006-01-12 23:22:24 +00001728 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
1729 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00001730
Bram Moolenaar4770d092006-01-12 23:22:24 +00001731 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001732
Bram Moolenaar4770d092006-01-12 23:22:24 +00001733 /* Clear info from .sug file. */
1734 slang_clear_sug(lp);
1735
Bram Moolenaar5195e452005-08-19 20:32:47 +00001736 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001737 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001738 lp->sl_compsylmax = MAXWLEN;
1739 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001740}
1741
1742/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001743 * Clear the info from the .sug file in "lp".
1744 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001745 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001746slang_clear_sug(slang_T *lp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00001747{
Bram Moolenaard23a8232018-02-10 18:45:26 +01001748 VIM_CLEAR(lp->sl_sbyts);
1749 VIM_CLEAR(lp->sl_sidxs);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001750 close_spellbuf(lp->sl_sugbuf);
1751 lp->sl_sugbuf = NULL;
1752 lp->sl_sugloaded = FALSE;
1753 lp->sl_sugtime = 0;
1754}
1755
1756/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001757 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001758 * Invoked through do_in_runtimepath().
1759 */
1760 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001761spell_load_cb(char_u *fname, void *cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001762{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001763 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 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00001778}
1779
Bram Moolenaar4770d092006-01-12 23:22:24 +00001780
1781/*
1782 * Add a word to the hashtable of common words.
1783 * If it's already there then the counter is increased.
1784 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001785 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001786count_common_word(
1787 slang_T *lp,
1788 char_u *word,
1789 int len, /* word length, -1 for upto NUL */
1790 int count) /* 1 to count once, 10 to init */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001791{
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;
Bram Moolenaar5bcc5a12019-08-06 22:48:02 +02001800 else if (len >= MAXWLEN)
1801 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001802 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 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001812 wc = alloc(sizeof(wordcount_T) + STRLEN(p));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001813 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/*
Bram Moolenaar95529562005-08-25 21:21:38 +00001828 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00001829 * Like strchr() but independent of locale.
1830 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001831 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001832byte_in_str(char_u *str, int n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001833{
1834 char_u *p;
1835
1836 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00001837 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001838 return TRUE;
1839 return FALSE;
1840}
1841
Bram Moolenaar5195e452005-08-19 20:32:47 +00001842#define SY_MAXLEN 30
1843typedef 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 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001853 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001854init_syl_tab(slang_T *slang)
Bram Moolenaar5195e452005-08-19 20:32:47 +00001855{
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;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001866 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001867 break;
1868 s = p;
1869 p = vim_strchr(p, '/');
1870 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001871 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001872 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001873 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001874 if (l >= SY_MAXLEN)
1875 return SP_FORMERROR;
1876 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001877 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001878 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
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001892count_syllables(slang_T *slang, char_u *word)
Bram Moolenaar5195e452005-08-19 20:32:47 +00001893{
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? */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001932 c = mb_ptr2char(p);
1933 len = (*mb_ptr2len)(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001934 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/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02001947 * Parse 'spelllang' and set w_s->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001948 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001949 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001950 char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001951did_set_spelllang(win_T *wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001952{
1953 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001954 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001955 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00001956 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001957 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001958 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001959 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001960 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001961 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001962 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001963 int len;
1964 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00001965 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001966 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001967 char_u *use_region = NULL;
1968 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001969 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001970 int i, j;
1971 langp_T *lp, *lp2;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001972 static int recursive = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001973 char *ret_msg = NULL;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001974 char_u *spl_copy;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001975 bufref_T bufref;
1976
1977 set_bufref(&bufref, wp->w_buffer);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001978
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;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001985
1986 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001987 clear_midword(wp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001988
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001989 /* Make a copy of 'spelllang', the SpellFileMissing autocommands may change
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001990 * it under our fingers. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001991 spl_copy = vim_strsave(wp->w_s->b_p_spl);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001992 if (spl_copy == NULL)
1993 goto theend;
1994
Bram Moolenaarcc63c642013-11-12 04:44:01 +01001995 wp->w_s->b_cjk = 0;
1996
1997 /* Loop over comma separated language names. */
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001998 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001999 {
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002000 // Get one language name.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002001 copy_option_part(&splp, lang, MAXWLEN, ",");
Bram Moolenaar5482f332005-04-17 20:18:43 +00002002 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002003 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002004
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002005 if (!valid_spellang(lang))
2006 continue;
2007
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002008 if (STRCMP(lang, "cjk") == 0)
2009 {
2010 wp->w_s->b_cjk = 1;
2011 continue;
2012 }
2013
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002014 /* 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
Bram Moolenaarb6356332005-07-18 21:40:44 +00002021 /* 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);
Bram Moolenaarb6356332005-07-18 21:40:44 +00002028 region = region_cp;
2029 }
2030 else
2031 dont_use_region = TRUE;
2032
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002033 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002034 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaar99499b12019-05-23 21:35:48 +02002035 if (fullpathcmp(lang, slang->sl_fname, FALSE, TRUE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002036 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 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002047 else
2048 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002049
2050 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002051 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2052 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002053 break;
2054 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002055
Bram Moolenaarb6356332005-07-18 21:40:44 +00002056 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
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002065 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002066 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002067 {
2068 if (filename)
2069 (void)spell_load_file(lang, lang, NULL, FALSE);
2070 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002071 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002072 spell_load_lang(lang);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002073 /* SpellFileMissing autocommands may do anything, including
2074 * destroying the buffer we are using... */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002075 if (!bufref_valid(&bufref))
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002076 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002077 ret_msg = N_("E797: SpellFileMissing autocommand deleted buffer");
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002078 goto theend;
2079 }
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002080 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002081 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002082
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002083 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002084 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002085 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002086 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaar99499b12019-05-23 21:35:48 +02002087 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE, TRUE)
2088 == FPC_SAME
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002089 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002090 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002091 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002092 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002093 {
2094 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002095 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002096 if (c == REGION_ALL)
2097 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002098 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002099 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002100 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002101 /* 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. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002107 smsg(_("Warning: region %s not supported"),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002108 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002109 }
2110 else
2111 region_mask = 1 << c;
2112 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002113
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002114 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002115 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002116 if (ga_grow(&ga, 1) == FAIL)
2117 {
2118 ga_clear(&ga);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002119 ret_msg = e_outofmem;
2120 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002121 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002122 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002123 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2124 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002125 use_midword(slang, wp);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002126 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002127 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002128 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002129 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002130 }
2131
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002132 /* 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. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002136 spf = curwin->w_s->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002137 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002138 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002139 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002140 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002141 /* Internal wordlist, if there is one. */
2142 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002143 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002144 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002145 }
2146 else
2147 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002148 /* 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)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002154 {
2155 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
Bram Moolenaar99499b12019-05-23 21:35:48 +02002156 if (p != NULL && fullpathcmp(spf_name, p, FALSE, TRUE)
2157 == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002158 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002159 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002160 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002161 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002162 }
2163
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002164 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002165 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaar99499b12019-05-23 21:35:48 +02002166 if (fullpathcmp(spf_name, slang->sl_fname, FALSE, TRUE)
2167 == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002168 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002169 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002170 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002171 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002172 * 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
Bram Moolenaar7887d882005-07-01 22:33:52 +00002177 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002178 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002179 p = vim_strchr(lang, '.');
2180 if (p != NULL)
2181 *p = NUL; /* truncate at ".encoding.add" */
2182 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002183 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002184
2185 /* If one of the languages has NOBREAK we assume the addition
2186 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002187 if (slang != NULL && nobreak)
2188 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002189 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002190 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002191 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002192 region_mask = REGION_ALL;
2193 if (use_region != NULL && !dont_use_region)
2194 {
2195 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002196 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002197 if (c != REGION_ALL)
2198 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002199 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002200 /* This spell file is for other regions. */
2201 region_mask = 0;
2202 }
2203
2204 if (region_mask != 0)
2205 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002206 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;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002209 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2210 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002211 use_midword(slang, wp);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002212 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002213 }
2214 }
2215
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002216 /* Everything is fine, store the new b_langp value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002217 ga_clear(&wp->w_s->b_langp);
2218 wp->w_s->b_langp = ga;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002219
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002220 /* 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
Bram Moolenaar4770d092006-01-12 23:22:24 +00002250 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002251 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
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002264theend:
2265 vim_free(spl_copy);
2266 recursive = FALSE;
Bram Moolenaarbe578ed2014-05-13 14:03:40 +02002267 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002268 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002269}
2270
2271/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002272 * Clear the midword characters for buffer "buf".
2273 */
2274 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002275clear_midword(win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002276{
Bram Moolenaar860cae12010-06-05 23:22:07 +02002277 vim_memset(wp->w_s->b_spell_ismw, 0, 256);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002278 VIM_CLEAR(wp->w_s->b_spell_ismw_mb);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002279}
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
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002286use_midword(slang_T *lp, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002287{
2288 char_u *p;
2289
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002290 if (lp->sl_midword == NULL) /* there aren't any */
2291 return;
2292
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002293 for (p = lp->sl_midword; *p != NUL; )
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002294 if (has_mbyte)
2295 {
2296 int c, l, n;
2297 char_u *bp;
2298
2299 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002300 l = (*mb_ptr2len)(p);
2301 if (c < 256 && l <= 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002302 wp->w_s->b_spell_ismw[c] = TRUE;
2303 else if (wp->w_s->b_spell_ismw_mb == NULL)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002304 /* First multi-byte char in "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002305 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002306 else
2307 {
2308 /* Append multi-byte chars to "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002309 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
2310 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002311 if (bp != NULL)
2312 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002313 vim_free(wp->w_s->b_spell_ismw_mb);
2314 wp->w_s->b_spell_ismw_mb = bp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002315 vim_strncpy(bp + n, p, l);
2316 }
2317 }
2318 p += l;
2319 }
2320 else
Bram Moolenaar860cae12010-06-05 23:22:07 +02002321 wp->w_s->b_spell_ismw[*p++] = TRUE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002322}
2323
2324/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002325 * Find the region "region[2]" in "rp" (points to "sl_regions").
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01002326 * Each region is simply stored as the two characters of its name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002327 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002328 */
2329 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002330find_region(char_u *rp, char_u *region)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002331{
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/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002345 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002346 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002347 * Word WF_ONECAP
2348 * W WORD WF_ALLCAP
2349 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002350 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002351 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002352captype(
2353 char_u *word,
2354 char_u *end) /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002355{
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 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002363 for (p = word; !spell_iswordp_nmw(p, curwin); MB_PTR_ADV(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002364 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002365 return 0; /* only non-word characters, illegal word */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002366 if (has_mbyte)
2367 c = mb_ptr2char_adv(&p);
2368 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00002369 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002370 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002371
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 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002376 for ( ; end == NULL ? *p != NUL : p < end; MB_PTR_ADV(p))
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002377 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002378 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002379 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002380 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002381 {
2382 /* UUl -> KEEPCAP */
2383 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002384 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002385 allcap = FALSE;
2386 }
2387 else if (!allcap)
2388 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002389 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002390 past_second = TRUE;
2391 }
2392
2393 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002394 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002395 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002396 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002397 return 0;
2398}
2399
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002400/*
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002401 * Delete the internal wordlist and its .spl file.
2402 */
2403 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002404spell_delete_wordlist(void)
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002405{
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);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002413 VIM_CLEAR(int_wordlist);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002414 }
2415}
2416
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002417/*
2418 * Free all languages.
2419 */
2420 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002421spell_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002422{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002423 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002424 buf_T *buf;
2425
Bram Moolenaar60bb4e12010-09-18 13:36:49 +02002426 /* Go through all buffers and handle 'spelllang'. <VN> */
Bram Moolenaar29323592016-07-24 22:04:11 +02002427 FOR_ALL_BUFFERS(buf)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002428 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002429
2430 while (first_lang != NULL)
2431 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002432 slang = first_lang;
2433 first_lang = slang->sl_next;
2434 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002435 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002436
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002437 spell_delete_wordlist();
Bram Moolenaar7887d882005-07-01 22:33:52 +00002438
Bram Moolenaard23a8232018-02-10 18:45:26 +01002439 VIM_CLEAR(repl_to);
2440 VIM_CLEAR(repl_from);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002441}
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002442
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002443/*
2444 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002445 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002446 */
2447 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002448spell_reload(void)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002449{
Bram Moolenaar3982c542005-06-08 21:56:31 +00002450 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002451
Bram Moolenaarea408852005-06-25 22:49:46 +00002452 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002453 init_spell_chartab();
2454
2455 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002456 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002457
2458 /* Go through all buffers and handle 'spelllang'. */
Bram Moolenaar29323592016-07-24 22:04:11 +02002459 FOR_ALL_WINDOWS(wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002460 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002461 /* Only load the wordlists when 'spelllang' is set and there is a
2462 * window for this buffer in which 'spell' is set. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002463 if (*wp->w_s->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002464 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002465 if (wp->w_p_spell)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002466 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002467 (void)did_set_spelllang(wp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00002468 break;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002469 }
2470 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002471 }
2472}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002473
Bram Moolenaarb765d632005-06-07 21:00:02 +00002474/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002475 * 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 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002482 buf_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002483open_spellbuf(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002484{
2485 buf_T *buf;
2486
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002487 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002488 if (buf != NULL)
2489 {
2490 buf->b_spell = TRUE;
2491 buf->b_p_swf = TRUE; /* may create a swap file */
Bram Moolenaar706d2de2013-07-17 17:35:13 +02002492#ifdef FEAT_CRYPT
2493 buf->b_p_key = empty_option;
2494#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00002495 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 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002504 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002505close_spellbuf(buf_T *buf)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002506{
2507 if (buf != NULL)
2508 {
2509 ml_close(buf, TRUE);
2510 vim_free(buf);
2511 }
2512}
2513
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002514/*
2515 * Init the chartab used for spelling for ASCII.
2516 * EBCDIC is not supported!
2517 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002518 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002519clear_spell_chartab(spelltab_T *sp)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002520{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002521 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002522
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)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002527 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002528 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002529 sp->st_upper[i] = i;
2530 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002531
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)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002543 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002544 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002545 sp->st_upper[i] = i - 0x20;
2546 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002547}
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
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002554 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002555 */
2556 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002557init_spell_chartab(void)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002558{
2559 int i;
2560
2561 did_set_spelltab = FALSE;
2562 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002563 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 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002570 else if (enc_utf8)
2571 {
2572 for (i = 128; i < 256; ++i)
2573 {
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02002574 int f = utf_fold(i);
2575 int u = utf_toupper(i);
2576
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002577 spelltab.st_isu[i] = utf_isupper(i);
2578 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02002579 /* 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;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002584 }
2585 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002586 else
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002587 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002588 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002589 for (i = 128; i < 256; ++i)
2590 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002591 if (MB_ISUPPER(i))
2592 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002593 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002594 spelltab.st_isu[i] = TRUE;
2595 spelltab.st_fold[i] = MB_TOLOWER(i);
2596 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002597 else if (MB_ISLOWER(i))
2598 {
2599 spelltab.st_isw[i] = TRUE;
2600 spelltab.st_upper[i] = MB_TOUPPER(i);
2601 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002602 }
2603 }
2604}
2605
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002606
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002607/*
Bram Moolenaarea408852005-06-25 22:49:46 +00002608 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002609 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00002610 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002611 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00002612 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02002613 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002614spell_iswordp(
2615 char_u *p,
2616 win_T *wp) /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00002617{
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002618 char_u *s;
2619 int l;
2620 int c;
2621
2622 if (has_mbyte)
2623 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02002624 l = MB_PTR2LEN(p);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002625 s = p;
2626 if (l == 1)
2627 {
2628 /* be quick for ASCII */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002629 if (wp->w_s->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002630 s = p + 1; /* skip a mid-word character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002631 }
2632 else
2633 {
2634 c = mb_ptr2char(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002635 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))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002638 s = p + l;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002639 }
2640
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002641 c = mb_ptr2char(s);
2642 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002643 return spell_mb_isword_class(mb_get_class(s), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002644 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002645 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002646
Bram Moolenaar860cae12010-06-05 23:22:07 +02002647 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002648}
2649
2650/*
2651 * Return TRUE if "p" points to a word character.
2652 * Unlike spell_iswordp() this doesn't check for "midword" characters.
2653 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02002654 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002655spell_iswordp_nmw(char_u *p, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002656{
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002657 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002658
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002659 if (has_mbyte)
2660 {
2661 c = mb_ptr2char(p);
2662 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002663 return spell_mb_isword_class(mb_get_class(p), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002664 return spelltab.st_isw[c];
2665 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002666 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00002667}
2668
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002669/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002670 * 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.
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002673 * See also dbcs_class() and utf_class() in mbyte.c.
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002674 */
2675 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002676spell_mb_isword_class(int cl, win_T *wp)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002677{
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002678 if (wp->w_s->b_cjk)
2679 /* East Asian characters are not considered word characters. */
2680 return cl == 2 || cl == 0x2800;
Bram Moolenaar06e63772019-07-19 23:04:34 +02002681 return cl >= 2 && cl != 0x2070 && cl != 0x2080 && cl != 3;
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002682}
2683
2684/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002685 * Return TRUE if "p" points to a word character.
2686 * Wide version of spell_iswordp().
2687 */
2688 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002689spell_iswordp_w(int *p, win_T *wp)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002690{
2691 int *s;
2692
Bram Moolenaar860cae12010-06-05 23:22:07 +02002693 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))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002696 s = p + 1;
2697 else
2698 s = p;
2699
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002700 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002701 {
2702 if (enc_utf8)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002703 return spell_mb_isword_class(utf_class(*s), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002704 if (enc_dbcs)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002705 return spell_mb_isword_class(
2706 dbcs_class((unsigned)*s >> 8, *s & 0xff), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002707 return 0;
2708 }
2709 return spelltab.st_isw[*s];
2710}
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002711
Bram Moolenaarea408852005-06-25 22:49:46 +00002712/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002713 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
2714 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002715 * When using a multi-byte 'encoding' the length may change!
2716 * Returns FAIL when something wrong.
2717 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002718 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002719spell_casefold(
2720 char_u *str,
2721 int len,
2722 char_u *buf,
2723 int buflen)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002724{
2725 int i;
2726
2727 if (len >= buflen)
2728 {
2729 buf[0] = NUL;
2730 return FAIL; /* result will not fit */
2731 }
2732
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002733 if (has_mbyte)
2734 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002735 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002736 char_u *p;
2737 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002738
2739 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002740 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002741 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002742 if (outi + MB_MAXBYTES > buflen)
2743 {
2744 buf[outi] = NUL;
2745 return FAIL;
2746 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002747 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002748 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002749 }
2750 buf[outi] = NUL;
2751 }
2752 else
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002753 {
2754 /* Be quick for non-multibyte encodings. */
2755 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002756 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002757 buf[i] = NUL;
2758 }
2759
2760 return OK;
2761}
2762
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002763/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002764 * 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 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02002767 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002768check_need_cap(linenr_T lnum, colnr_T col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002769{
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
Bram Moolenaar860cae12010-06-05 23:22:07 +02002777 if (curwin->w_s->b_cap_prog == NULL)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002778 return FALSE;
2779
2780 line = ml_get_curline();
2781 endcol = 0;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002782 if (getwhitecols(line) >= (int)col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002783 {
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;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002798 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002799 }
2800 }
2801 }
2802 else
2803 endcol = col;
2804
2805 if (endcol > 0)
2806 {
2807 /* Check if sentence ends before the bad word. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002808 regmatch.regprog = curwin->w_s->b_cap_prog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002809 regmatch.rm_ic = FALSE;
2810 p = line + endcol;
2811 for (;;)
2812 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002813 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002814 if (p == line || spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002815 break;
2816 if (vim_regexec(&regmatch, p, 0)
2817 && regmatch.endp[0] == line + endcol)
2818 {
2819 need_cap = TRUE;
2820 break;
2821 }
2822 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002823 curwin->w_s->b_cap_prog = regmatch.regprog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002824 }
2825
2826 vim_free(line_copy);
2827
2828 return need_cap;
2829}
2830
2831
2832/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002833 * ":spellrepall"
2834 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002835 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002836ex_spellrepall(exarg_T *eap UNUSED)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002837{
2838 pos_T pos = curwin->w_cursor;
2839 char_u *frompat;
2840 int addlen;
2841 char_u *line;
2842 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002843 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002844 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002845
2846 if (repl_from == NULL || repl_to == NULL)
2847 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002848 emsg(_("E752: No previous spell replacement"));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002849 return;
2850 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002851 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002852
Bram Moolenaar964b3742019-05-24 18:54:09 +02002853 frompat = alloc(STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002854 if (frompat == NULL)
2855 return;
2856 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
2857 p_ws = FALSE;
2858
Bram Moolenaar5195e452005-08-19 20:32:47 +00002859 sub_nsubs = 0;
2860 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002861 curwin->w_cursor.lnum = 0;
2862 while (!got_int)
2863 {
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02002864 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002865 || 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 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002874 p = alloc(STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002875 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);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002882
2883 if (curwin->w_cursor.lnum != prev_lnum)
2884 {
2885 ++sub_nlines;
2886 prev_lnum = curwin->w_cursor.lnum;
2887 }
2888 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002889 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002890 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002891 }
2892
2893 p_ws = save_ws;
2894 curwin->w_cursor = pos;
2895 vim_free(frompat);
2896
Bram Moolenaar5195e452005-08-19 20:32:47 +00002897 if (sub_nsubs == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002898 semsg(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002899 else
2900 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002901}
2902
2903/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002904 * 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.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002907 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002908 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002909onecap_copy(
2910 char_u *word,
2911 char_u *wcopy,
2912 int upper) /* TRUE: first letter made upper case */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002913{
2914 char_u *p;
2915 int c;
2916 int l;
2917
2918 p = word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002919 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002920 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002921 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002922 c = *p++;
2923 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002924 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002925 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002926 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002927 if (has_mbyte)
2928 l = mb_char2bytes(c, wcopy);
2929 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002930 {
2931 l = 1;
2932 wcopy[0] = c;
2933 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002934 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002935}
2936
2937/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002938 * Make a copy of "word" with all the letters upper cased into
2939 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002940 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02002941 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002942allcap_copy(char_u *word, char_u *wcopy)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002943{
2944 char_u *s;
2945 char_u *d;
2946 int c;
2947
2948 d = wcopy;
2949 for (s = word; *s != NUL; )
2950 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002951 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002952 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002953 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002954 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +00002955
Bram Moolenaard3184b52011-09-02 14:18:20 +02002956 /* We only change 0xdf to SS when we are certain latin1 is used. It
Bram Moolenaar78622822005-08-23 21:00:13 +00002957 * 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
Bram Moolenaar78622822005-08-23 21:00:13 +00002966 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002967
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002968 if (has_mbyte)
2969 {
2970 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
2971 break;
2972 d += mb_char2bytes(c, d);
2973 }
2974 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002975 {
2976 if (d - wcopy >= MAXWLEN - 1)
2977 break;
2978 *d++ = c;
2979 }
2980 }
2981 *d = NUL;
2982}
2983
2984/*
Bram Moolenaar53805d12005-08-01 07:08:33 +00002985 * 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 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02002988 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002989nofold_len(char_u *fword, int flen, char_u *word)
Bram Moolenaar53805d12005-08-01 07:08:33 +00002990{
2991 char_u *p;
2992 int i = 0;
2993
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002994 for (p = fword; p < fword + flen; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00002995 ++i;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002996 for (p = word; i > 0; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00002997 --i;
2998 return (int)(p - word);
2999}
Bram Moolenaar53805d12005-08-01 07:08:33 +00003000
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003001/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003002 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003003 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02003004 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003005make_case_word(char_u *fword, char_u *cword, int flags)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003006{
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 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003012 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003013 else
3014 /* Use goodword as-is. */
3015 STRCPY(cword, fword);
3016}
3017
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003018#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 *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003024eval_soundfold(char_u *word)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003025{
3026 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003027 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003028 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003029
Bram Moolenaar860cae12010-06-05 23:22:07 +02003030 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003031 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003032 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003033 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003034 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003035 if (lp->lp_slang->sl_sal.ga_len > 0)
3036 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003037 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003038 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003039 return vim_strsave(sound);
3040 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003041 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003042
3043 /* No language with sound folding, return word as-is. */
3044 return vim_strsave(word);
3045}
3046#endif
3047
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003048/*
3049 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +00003050 *
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).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003059 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003060 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003061spell_soundfold(
3062 slang_T *slang,
3063 char_u *inword,
3064 int folded, /* "inword" is already case-folded */
3065 char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003066{
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 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003080 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003081 word = fword;
3082 }
3083
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003084 if (has_mbyte)
3085 spell_soundfold_wsal(slang, word, res);
3086 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003087 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
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003096spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003097{
3098 char_u *s;
3099 int ri = 0;
3100 int c;
3101
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003102 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 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003111 c = mb_cptr2char_adv(&s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003112 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003113 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
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003148 {
3149 /* The sl_sal_first[] table contains the translation. */
3150 for (s = inword; (c = *s) != NUL; ++s)
3151 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003152 if (VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003153 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
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003165spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003166{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003167 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003168 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003169 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003170 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003171 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003172 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003173 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003174 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
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003183 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003184 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003185 if (slang->sl_rem_accents)
3186 {
3187 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003188 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003189 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003190 if (VIM_ISWHITE(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003191 {
3192 *t++ = ' ';
3193 s = skipwhite(s);
3194 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003195 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003196 {
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003197 if (spell_iswordp_nmw(s, curwin))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003198 *t++ = *s;
3199 ++s;
3200 }
3201 }
3202 *t = NUL;
3203 }
3204 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003205 vim_strncpy(word, s, MAXWLEN - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003206
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003207 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003208
3209 /*
3210 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003211 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003212 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003213 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003214 while ((c = word[i]) != NUL)
3215 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003216 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003217 n = slang->sl_sal_first[c];
3218 z0 = 0;
3219
3220 if (n >= 0)
3221 {
3222 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003223 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003224 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003225 /* 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)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003229 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003230 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 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003240 }
3241
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003242 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003243 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003244 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003245 while (*pf != NUL && *pf != word[i + k])
3246 ++pf;
3247 if (*pf == NUL)
3248 continue;
3249 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003250 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003251 s = smp[n].sm_rules;
3252 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003253
3254 p0 = *s;
3255 k0 = k;
3256 while (*s == '-' && k > 1)
3257 {
3258 k--;
3259 s++;
3260 }
3261 if (*s == '<')
3262 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003263 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003264 {
3265 /* determine priority */
3266 pri = *s - '0';
3267 s++;
3268 }
3269 if (*s == '^' && *(s + 1) == '^')
3270 s++;
3271
3272 if (*s == NUL
3273 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003274 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02003275 || spell_iswordp(word + i - 1, curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003276 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003277 || (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003278 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02003279 && spell_iswordp(word + i - 1, curwin)
3280 && (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003281 {
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
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003288 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003289 {
3290 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003291 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003292 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003293 /* 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;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003311
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003312 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003313 {
3314 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003315 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003316 while (*pf != NUL && *pf != word[i + k0])
3317 ++pf;
3318 if (*pf == NUL)
3319 continue;
3320 ++k0;
3321 }
3322
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003323 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003324 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003325 while (*s == '-')
3326 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003327 /* "k0" gets NOT reduced because
3328 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003329 s++;
3330 }
3331 if (*s == '<')
3332 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003333 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003334 {
3335 p0 = *s - '0';
3336 s++;
3337 }
3338
3339 if (*s == NUL
3340 /* *s == '^' cuts */
3341 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003342 && !spell_iswordp(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02003343 curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003344 {
3345 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003346 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003347 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003348
3349 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003350 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003351 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003352 /* rule fits; stop search */
3353 break;
3354 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003355 }
3356
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003357 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003358 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003359 }
3360
3361 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003362 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003363 if (s == NULL)
3364 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003365 pf = smp[n].sm_rules;
3366 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003367 if (p0 == 1 && z == 0)
3368 {
3369 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003370 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
3371 || res[reslen - 1] == *s))
3372 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003373 z0 = 1;
3374 z = 1;
3375 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003376 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003377 {
3378 word[i + k0] = *s;
3379 k0++;
3380 s++;
3381 }
3382 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +00003383 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003384
3385 /* new "actual letter" */
3386 c = word[i];
3387 }
3388 else
3389 {
3390 /* no '<' rule used */
3391 i += k - 1;
3392 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003393 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003394 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003395 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003396 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003397 s++;
3398 }
3399 /* new "actual letter" */
3400 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003401 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003402 {
3403 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003404 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003405 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003406 i = 0;
3407 z0 = 1;
3408 }
3409 }
3410 break;
3411 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003412 }
3413 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01003414 else if (VIM_ISWHITE(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003415 {
3416 c = ' ';
3417 k = 1;
3418 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003419
3420 if (z0 == 0)
3421 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003422 if (k && !p0 && reslen < MAXWLEN && c != NUL
3423 && (!slang->sl_collapse || reslen == 0
3424 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003425 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003426 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003427
3428 i++;
3429 z = 0;
3430 k = 0;
3431 }
3432 }
3433
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003434 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003435}
3436
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003437/*
3438 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
3439 * Multi-byte version of spell_soundfold().
3440 */
3441 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003442spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003443{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003444 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003445 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;
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003463 int wordlen;
3464
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003465
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 */
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003471 wordlen = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003472 for (s = inword; *s != NUL; )
3473 {
3474 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003475 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003476 if (slang->sl_rem_accents)
3477 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003478 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003479 {
3480 if (did_white)
3481 continue;
3482 c = ' ';
3483 did_white = TRUE;
3484 }
3485 else
3486 {
3487 did_white = FALSE;
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003488 if (!spell_iswordp_nmw(t, curwin))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003489 continue;
3490 }
3491 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003492 word[wordlen++] = c;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003493 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003494 word[wordlen] = NUL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003495
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003496 /*
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003497 * This algorithm comes from Aspell phonet.cpp.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003498 * 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 {
Bram Moolenaar95e85792010-08-01 15:37:02 +02003510 /* 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)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003515 {
3516 /* Quickly skip entries that don't match the word. Most
3517 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003518 if (c != ws[0])
3519 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003520 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
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003535 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003536 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003537 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003538 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] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02003568 || spell_iswordp_w(word + i - 1, curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003569 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003570 || (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003571 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02003572 && spell_iswordp_w(word + i - 1, curwin)
3573 && (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003574 {
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 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003583 /* Test follow-up rule for "word[i + k]"; loop over
3584 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003585 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
3586 == (c0 & 0xff); ++n0)
3587 {
3588 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003589 */
3590 if (c0 != ws[0])
3591 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003592 k0 = smp[n0].sm_leadlen;
3593 if (k0 > 1)
3594 {
3595 if (word[i + k] != ws[1])
3596 continue;
3597 if (k0 > 2)
3598 {
3599 pf = word + i + k + 1;
3600 for (j = 2; j < k0; ++j)
3601 if (*pf++ != ws[j])
3602 break;
3603 if (j < k0)
3604 continue;
3605 }
3606 }
3607 k0 += k - 1;
3608
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003609 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003610 {
3611 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003612 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003613 while (*pf != NUL && *pf != word[i + k0])
3614 ++pf;
3615 if (*pf == NUL)
3616 continue;
3617 ++k0;
3618 }
3619
3620 p0 = 5;
3621 s = smp[n0].sm_rules;
3622 while (*s == '-')
3623 {
3624 /* "k0" gets NOT reduced because
3625 * "if (k0 == k)" */
3626 s++;
3627 }
3628 if (*s == '<')
3629 s++;
3630 if (VIM_ISDIGIT(*s))
3631 {
3632 p0 = *s - '0';
3633 s++;
3634 }
3635
3636 if (*s == NUL
3637 /* *s == '^' cuts */
3638 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003639 && !spell_iswordp_w(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02003640 curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003641 {
3642 if (k0 == k)
3643 /* this is just a piece of the string */
3644 continue;
3645
3646 if (p0 < pri)
3647 /* priority too low */
3648 continue;
3649 /* rule fits; stop search */
3650 break;
3651 }
3652 }
3653
3654 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
3655 == (c0 & 0xff))
3656 continue;
3657 }
3658
3659 /* replace string */
3660 ws = smp[n].sm_to_w;
3661 s = smp[n].sm_rules;
3662 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
3663 if (p0 == 1 && z == 0)
3664 {
3665 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003666 if (reslen > 0 && ws != NULL && *ws != NUL
3667 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003668 || wres[reslen - 1] == *ws))
3669 reslen--;
3670 z0 = 1;
3671 z = 1;
3672 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003673 if (ws != NULL)
3674 while (*ws != NUL && word[i + k0] != NUL)
3675 {
3676 word[i + k0] = *ws;
3677 k0++;
3678 ws++;
3679 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003680 if (k > k0)
3681 mch_memmove(word + i + k0, word + i + k,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003682 sizeof(int) * (wordlen - (i + k) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003683
3684 /* new "actual letter" */
3685 c = word[i];
3686 }
3687 else
3688 {
3689 /* no '<' rule used */
3690 i += k - 1;
3691 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003692 if (ws != NULL)
3693 while (*ws != NUL && ws[1] != NUL
3694 && reslen < MAXWLEN)
3695 {
3696 if (reslen == 0 || wres[reslen - 1] != *ws)
3697 wres[reslen++] = *ws;
3698 ws++;
3699 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003700 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003701 if (ws == NULL)
3702 c = NUL;
3703 else
3704 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003705 if (strstr((char *)s, "^^") != NULL)
3706 {
3707 if (c != NUL)
3708 wres[reslen++] = c;
3709 mch_memmove(word, word + i + 1,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003710 sizeof(int) * (wordlen - (i + 1) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003711 i = 0;
3712 z0 = 1;
3713 }
3714 }
3715 break;
3716 }
3717 }
3718 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01003719 else if (VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003720 {
3721 c = ' ';
3722 k = 1;
3723 }
3724
3725 if (z0 == 0)
3726 {
3727 if (k && !p0 && reslen < MAXWLEN && c != NUL
3728 && (!slang->sl_collapse || reslen == 0
3729 || wres[reslen - 1] != c))
3730 /* condense only double letters */
3731 wres[reslen++] = c;
3732
3733 i++;
3734 z = 0;
3735 k = 0;
3736 }
3737 }
3738
3739 /* Convert wide characters in "wres" to a multi-byte string in "res". */
3740 l = 0;
3741 for (n = 0; n < reslen; ++n)
3742 {
3743 l += mb_char2bytes(wres[n], res + l);
3744 if (l + MB_MAXBYTES > MAXWLEN)
3745 break;
3746 }
3747 res[l] = NUL;
3748}
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003749
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003750/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003751 * ":spellinfo"
3752 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003753 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003754ex_spellinfo(exarg_T *eap UNUSED)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003755{
3756 int lpi;
3757 langp_T *lp;
3758 char_u *p;
3759
3760 if (no_spell_checking(curwin))
3761 return;
3762
3763 msg_start();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003764 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003765 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003766 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003767 msg_puts("file: ");
3768 msg_puts((char *)lp->lp_slang->sl_fname);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003769 msg_putchar('\n');
3770 p = lp->lp_slang->sl_info;
3771 if (p != NULL)
3772 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003773 msg_puts((char *)p);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003774 msg_putchar('\n');
3775 }
3776 }
3777 msg_end();
3778}
3779
Bram Moolenaar4770d092006-01-12 23:22:24 +00003780#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
3781#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003782#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +00003783#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
3784#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003785
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003786/*
3787 * ":spelldump"
3788 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003789 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003790ex_spelldump(exarg_T *eap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003791{
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003792 char_u *spl;
3793 long dummy;
3794
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003795 if (no_spell_checking(curwin))
3796 return;
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003797 get_option_value((char_u*)"spl", &dummy, &spl, OPT_LOCAL);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003798
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003799 /* Create a new empty buffer in a new window. */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003800 do_cmdline_cmd((char_u *)"new");
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003801
3802 /* enable spelling locally in the new window */
3803 set_option_value((char_u*)"spell", TRUE, (char_u*)"", OPT_LOCAL);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003804 set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL);
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003805 vim_free(spl);
3806
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003807 if (!BUFEMPTY())
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003808 return;
3809
Bram Moolenaar860cae12010-06-05 23:22:07 +02003810 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003811
3812 /* Delete the empty line that we started with. */
3813 if (curbuf->b_ml.ml_line_count > 1)
3814 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
3815
3816 redraw_later(NOT_VALID);
3817}
3818
3819/*
3820 * Go through all possible words and:
3821 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
3822 * "ic" and "dir" are not used.
3823 * 2. When "pat" is not NULL: add matching words to insert mode completion.
3824 */
3825 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003826spell_dump_compl(
3827 char_u *pat, /* leading part of the word */
3828 int ic, /* ignore case */
3829 int *dir, /* direction for adding matches */
3830 int dumpflags_arg) /* DUMPFLAG_* */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003831{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003832 langp_T *lp;
3833 slang_T *slang;
3834 idx_T arridx[MAXWLEN];
3835 int curi[MAXWLEN];
3836 char_u word[MAXWLEN];
3837 int c;
3838 char_u *byts;
3839 idx_T *idxs;
3840 linenr_T lnum = 0;
3841 int round;
3842 int depth;
3843 int n;
3844 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003845 char_u *region_names = NULL; /* region names being used */
3846 int do_region = TRUE; /* dump region names and numbers */
3847 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003848 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003849 int dumpflags = dumpflags_arg;
3850 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003851
Bram Moolenaard0131a82006-03-04 21:46:13 +00003852 /* When ignoring case or when the pattern starts with capital pass this on
3853 * to dump_word(). */
3854 if (pat != NULL)
3855 {
3856 if (ic)
3857 dumpflags |= DUMPFLAG_ICASE;
3858 else
3859 {
3860 n = captype(pat, NULL);
3861 if (n == WF_ONECAP)
3862 dumpflags |= DUMPFLAG_ONECAP;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01003863 else if (n == WF_ALLCAP && (int)STRLEN(pat) > mb_ptr2len(pat))
Bram Moolenaard0131a82006-03-04 21:46:13 +00003864 dumpflags |= DUMPFLAG_ALLCAP;
3865 }
3866 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003867
Bram Moolenaar7887d882005-07-01 22:33:52 +00003868 /* Find out if we can support regions: All languages must support the same
3869 * regions or none at all. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003870 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003871 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003872 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003873 p = lp->lp_slang->sl_regions;
3874 if (p[0] != 0)
3875 {
3876 if (region_names == NULL) /* first language with regions */
3877 region_names = p;
3878 else if (STRCMP(region_names, p) != 0)
3879 {
3880 do_region = FALSE; /* region names are different */
3881 break;
3882 }
3883 }
3884 }
3885
3886 if (do_region && region_names != NULL)
3887 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003888 if (pat == NULL)
3889 {
3890 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
3891 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
3892 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00003893 }
3894 else
3895 do_region = FALSE;
3896
3897 /*
3898 * Loop over all files loaded for the entries in 'spelllang'.
3899 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003900 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003901 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003902 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003903 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003904 if (slang->sl_fbyts == NULL) /* reloading failed */
3905 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003906
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003907 if (pat == NULL)
3908 {
3909 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
3910 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
3911 }
3912
3913 /* When matching with a pattern and there are no prefixes only use
3914 * parts of the tree that match "pat". */
3915 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003916 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003917 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003918 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003919
3920 /* round 1: case-folded tree
3921 * round 2: keep-case tree */
3922 for (round = 1; round <= 2; ++round)
3923 {
3924 if (round == 1)
3925 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003926 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003927 byts = slang->sl_fbyts;
3928 idxs = slang->sl_fidxs;
3929 }
3930 else
3931 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003932 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003933 byts = slang->sl_kbyts;
3934 idxs = slang->sl_kidxs;
3935 }
3936 if (byts == NULL)
3937 continue; /* array is empty */
3938
3939 depth = 0;
3940 arridx[0] = 0;
3941 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003942 while (depth >= 0 && !got_int
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003943 && (pat == NULL || !ins_compl_interrupted()))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003944 {
3945 if (curi[depth] > byts[arridx[depth]])
3946 {
3947 /* Done all bytes at this node, go up one level. */
3948 --depth;
3949 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003950 ins_compl_check_keys(50, FALSE);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003951 }
3952 else
3953 {
3954 /* Do one more byte at this node. */
3955 n = arridx[depth] + curi[depth];
3956 ++curi[depth];
3957 c = byts[n];
3958 if (c == 0)
3959 {
3960 /* End of word, deal with the word.
3961 * Don't use keep-case words in the fold-case tree,
3962 * they will appear in the keep-case tree.
3963 * Only use the word when the region matches. */
3964 flags = (int)idxs[n];
3965 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003966 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +00003967 && (do_region
3968 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003969 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003970 & lp->lp_region) != 0))
3971 {
3972 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003973 if (!do_region)
3974 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003975
3976 /* Dump the basic word if there is no prefix or
3977 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003978 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003979 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003980 {
3981 dump_word(slang, word, pat, dir,
3982 dumpflags, flags, lnum);
3983 if (pat == NULL)
3984 ++lnum;
3985 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003986
3987 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003988 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003989 lnum = dump_prefixes(slang, word, pat, dir,
3990 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003991 }
3992 }
3993 else
3994 {
3995 /* Normal char, go one level deeper. */
3996 word[depth++] = c;
3997 arridx[depth] = idxs[n];
3998 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003999
4000 /* Check if this characters matches with the pattern.
4001 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +00004002 * Always ignore case here, dump_word() will check
4003 * proper case later. This isn't exactly right when
4004 * length changes for multi-byte characters with
4005 * ignore case... */
4006 if (depth <= patlen
4007 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004008 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004009 }
4010 }
4011 }
4012 }
4013 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004014}
4015
4016/*
4017 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004018 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004019 */
4020 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004021dump_word(
4022 slang_T *slang,
4023 char_u *word,
4024 char_u *pat,
4025 int *dir,
4026 int dumpflags,
4027 int wordflags,
4028 linenr_T lnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004029{
4030 int keepcap = FALSE;
4031 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004032 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004033 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +00004034 char_u badword[MAXWLEN + 10];
4035 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +00004036 int flags = wordflags;
4037
4038 if (dumpflags & DUMPFLAG_ONECAP)
4039 flags |= WF_ONECAP;
4040 if (dumpflags & DUMPFLAG_ALLCAP)
4041 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004042
Bram Moolenaar4770d092006-01-12 23:22:24 +00004043 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004044 {
4045 /* Need to fix case according to "flags". */
4046 make_case_word(word, cword, flags);
4047 p = cword;
4048 }
4049 else
4050 {
4051 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004052 if ((dumpflags & DUMPFLAG_KEEPCASE)
4053 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004054 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004055 keepcap = TRUE;
4056 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004057 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004058
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004059 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004060 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004061 /* Add flags and regions after a slash. */
4062 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004063 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004064 STRCPY(badword, p);
4065 STRCAT(badword, "/");
4066 if (keepcap)
4067 STRCAT(badword, "=");
4068 if (flags & WF_BANNED)
4069 STRCAT(badword, "!");
4070 else if (flags & WF_RARE)
4071 STRCAT(badword, "?");
4072 if (flags & WF_REGION)
4073 for (i = 0; i < 7; ++i)
4074 if (flags & (0x10000 << i))
4075 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
4076 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004077 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004078
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004079 if (dumpflags & DUMPFLAG_COUNT)
4080 {
4081 hashitem_T *hi;
4082
4083 /* Include the word count for ":spelldump!". */
4084 hi = hash_find(&slang->sl_wordcount, tw);
4085 if (!HASHITEM_EMPTY(hi))
4086 {
4087 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
4088 tw, HI2WC(hi)->wc_count);
4089 p = IObuff;
4090 }
4091 }
4092
4093 ml_append(lnum, p, (colnr_T)0, FALSE);
4094 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00004095 else if (((dumpflags & DUMPFLAG_ICASE)
4096 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
4097 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004098 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004099 p_ic, NULL, *dir, FALSE) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +00004100 /* if dir was BACKWARD then honor it just once */
4101 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004102}
4103
4104/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004105 * For ":spelldump": Find matching prefixes for "word". Prepend each to
4106 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004107 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004108 * Return the updated line number.
4109 */
4110 static linenr_T
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004111dump_prefixes(
4112 slang_T *slang,
4113 char_u *word, /* case-folded word */
4114 char_u *pat,
4115 int *dir,
4116 int dumpflags,
4117 int flags, /* flags with prefix ID */
4118 linenr_T startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004119{
4120 idx_T arridx[MAXWLEN];
4121 int curi[MAXWLEN];
4122 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +00004123 char_u word_up[MAXWLEN];
4124 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004125 int c;
4126 char_u *byts;
4127 idx_T *idxs;
4128 linenr_T lnum = startlnum;
4129 int depth;
4130 int n;
4131 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004132 int i;
4133
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004134 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +00004135 * upper-case letter in word_up[]. */
4136 c = PTR2CHAR(word);
4137 if (SPELL_TOUPPER(c) != c)
4138 {
4139 onecap_copy(word, word_up, TRUE);
4140 has_word_up = TRUE;
4141 }
4142
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004143 byts = slang->sl_pbyts;
4144 idxs = slang->sl_pidxs;
4145 if (byts != NULL) /* array not is empty */
4146 {
4147 /*
4148 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004149 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004150 */
4151 depth = 0;
4152 arridx[0] = 0;
4153 curi[0] = 1;
4154 while (depth >= 0 && !got_int)
4155 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004156 n = arridx[depth];
4157 len = byts[n];
4158 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004159 {
4160 /* Done all bytes at this node, go up one level. */
4161 --depth;
4162 line_breakcheck();
4163 }
4164 else
4165 {
4166 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004167 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004168 ++curi[depth];
4169 c = byts[n];
4170 if (c == 0)
4171 {
4172 /* End of prefix, find out how many IDs there are. */
4173 for (i = 1; i < len; ++i)
4174 if (byts[n + i] != 0)
4175 break;
4176 curi[depth] += i - 1;
4177
Bram Moolenaar53805d12005-08-01 07:08:33 +00004178 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
4179 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004180 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004181 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004182 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004183 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004184 : flags, lnum);
4185 if (lnum != 0)
4186 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004187 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00004188
4189 /* Check for prefix that matches the word when the
4190 * first letter is upper-case, but only if the prefix has
4191 * a condition. */
4192 if (has_word_up)
4193 {
4194 c = valid_word_prefix(i, n, flags, word_up, slang,
4195 TRUE);
4196 if (c != 0)
4197 {
4198 vim_strncpy(prefix + depth, word_up,
4199 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004200 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004201 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004202 : flags, lnum);
4203 if (lnum != 0)
4204 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004205 }
4206 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004207 }
4208 else
4209 {
4210 /* Normal char, go one level deeper. */
4211 prefix[depth++] = c;
4212 arridx[depth] = idxs[n];
4213 curi[depth] = 1;
4214 }
4215 }
4216 }
4217 }
4218
4219 return lnum;
4220}
4221
Bram Moolenaar95529562005-08-25 21:21:38 +00004222/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004223 * Move "p" to the end of word "start".
4224 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +00004225 */
4226 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004227spell_to_word_end(char_u *start, win_T *win)
Bram Moolenaar95529562005-08-25 21:21:38 +00004228{
4229 char_u *p = start;
4230
Bram Moolenaar860cae12010-06-05 23:22:07 +02004231 while (*p != NUL && spell_iswordp(p, win))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004232 MB_PTR_ADV(p);
Bram Moolenaar95529562005-08-25 21:21:38 +00004233 return p;
4234}
4235
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004236/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004237 * For Insert mode completion CTRL-X s:
4238 * Find start of the word in front of column "startcol".
4239 * We don't check if it is badly spelled, with completion we can only change
4240 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004241 * Returns the column number of the word.
4242 */
4243 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004244spell_word_start(int startcol)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004245{
4246 char_u *line;
4247 char_u *p;
4248 int col = 0;
4249
Bram Moolenaar95529562005-08-25 21:21:38 +00004250 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004251 return startcol;
4252
4253 /* Find a word character before "startcol". */
4254 line = ml_get_curline();
4255 for (p = line + startcol; p > line; )
4256 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004257 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004258 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004259 break;
4260 }
4261
4262 /* Go back to start of the word. */
4263 while (p > line)
4264 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004265 col = (int)(p - line);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004266 MB_PTR_BACK(line, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004267 if (!spell_iswordp(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004268 break;
4269 col = 0;
4270 }
4271
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004272 return col;
4273}
4274
4275/*
Bram Moolenaar4effc802005-09-30 21:12:02 +00004276 * Need to check for 'spellcapcheck' now, the word is removed before
4277 * expand_spelling() is called. Therefore the ugly global variable.
4278 */
4279static int spell_expand_need_cap;
4280
4281 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004282spell_expand_check_cap(colnr_T col)
Bram Moolenaar4effc802005-09-30 21:12:02 +00004283{
4284 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
4285}
4286
4287/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004288 * Get list of spelling suggestions.
4289 * Used for Insert mode completion CTRL-X ?.
4290 * Returns the number of matches. The matches are in "matchp[]", array of
4291 * allocated strings.
4292 */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004293 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004294expand_spelling(
4295 linenr_T lnum UNUSED,
4296 char_u *pat,
4297 char_u ***matchp)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004298{
4299 garray_T ga;
4300
Bram Moolenaar4770d092006-01-12 23:22:24 +00004301 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004302 *matchp = ga.ga_data;
4303 return ga.ga_len;
4304}
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004305
Bram Moolenaare677df82019-09-02 22:31:11 +02004306/*
4307 * Return TRUE if "val" is a valid 'spellang' value.
4308 */
4309 int
4310valid_spellang(char_u *val)
4311{
4312 return valid_name(val, ".-_,@");
4313}
4314
4315/*
4316 * Return TRUE if "val" is a valid 'spellfile' value.
4317 */
4318 int
4319valid_spellfile(char_u *val)
4320{
4321 char_u *s;
4322
4323 for (s = val; *s != NUL; ++s)
4324 if (!vim_isfilec(*s) && *s != ',')
4325 return FALSE;
4326 return TRUE;
4327}
4328
4329/*
4330 * Handle side effects of setting 'spell'.
4331 * Return an error message or NULL for success.
4332 */
4333 char *
4334did_set_spell_option(int is_spellfile)
4335{
4336 char *errmsg = NULL;
4337 win_T *wp;
4338 int l;
4339
4340 if (is_spellfile)
4341 {
4342 l = (int)STRLEN(curwin->w_s->b_p_spf);
4343 if (l > 0 && (l < 4
4344 || STRCMP(curwin->w_s->b_p_spf + l - 4, ".add") != 0))
4345 errmsg = e_invarg;
4346 }
4347
4348 if (errmsg == NULL)
4349 {
4350 FOR_ALL_WINDOWS(wp)
4351 if (wp->w_buffer == curbuf && wp->w_p_spell)
4352 {
4353 errmsg = did_set_spelllang(wp);
4354 break;
4355 }
4356 }
4357 return errmsg;
4358}
4359
4360/*
4361 * Set curbuf->b_cap_prog to the regexp program for 'spellcapcheck'.
4362 * Return error message when failed, NULL when OK.
4363 */
4364 char *
4365compile_cap_prog(synblock_T *synblock)
4366{
4367 regprog_T *rp = synblock->b_cap_prog;
4368 char_u *re;
4369
4370 if (*synblock->b_p_spc == NUL)
4371 synblock->b_cap_prog = NULL;
4372 else
4373 {
4374 // Prepend a ^ so that we only match at one column
4375 re = concat_str((char_u *)"^", synblock->b_p_spc);
4376 if (re != NULL)
4377 {
4378 synblock->b_cap_prog = vim_regcomp(re, RE_MAGIC);
4379 vim_free(re);
4380 if (synblock->b_cap_prog == NULL)
4381 {
4382 synblock->b_cap_prog = rp; // restore the previous program
4383 return e_invarg;
4384 }
4385 }
4386 }
4387
4388 vim_regfree(rp);
4389 return NULL;
4390}
4391
4392#endif // FEAT_SPELL