blob: b474a38b3c2e2f0093125314bb0246152e99d052 [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 Moolenaar0d6f5d92019-12-05 21:33:15 +010063#ifndef UNIX // it's in os_unix.h for Unix
64# include <time.h> // for time_t
Bram Moolenaar4770d092006-01-12 23:22:24 +000065#endif
66
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010067#define REGION_ALL 0xff // word valid in all regions
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000068
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010069// Result values. Lower number is accepted over higher one.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000070#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000071#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000072#define SP_RARE 1
73#define SP_LOCAL 2
74#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000075
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000076/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000077 * Structure to store info for word matching.
78 */
79typedef struct matchinf_S
80{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010081 langp_T *mi_lp; // info for language and region
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000082
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010083 // pointers to original text to be checked
84 char_u *mi_word; // start of word being checked
85 char_u *mi_end; // end of matching word so far
86 char_u *mi_fend; // next char to be added to mi_fword
87 char_u *mi_cend; // char after what was used for
88 // mi_capflags
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000089
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010090 // case-folded text
91 char_u mi_fword[MAXWLEN + 1]; // mi_word case-folded
92 int mi_fwordlen; // nr of valid bytes in mi_fword
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000093
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010094 // for when checking word after a prefix
95 int mi_prefarridx; // index in sl_pidxs with list of
96 // affixID/condition
97 int mi_prefcnt; // number of entries at mi_prefarridx
98 int mi_prefixlen; // byte length of prefix
99 int mi_cprefixlen; // byte length of prefix in original
100 // case
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000101
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100102 // for when checking a compound word
103 int mi_compoff; // start of following word offset
104 char_u mi_compflags[MAXWLEN]; // flags for compound words used
105 int mi_complen; // nr of compound words used
106 int mi_compextra; // nr of COMPOUNDROOT words
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000107
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100108 // others
109 int mi_result; // result so far: SP_BAD, SP_OK, etc.
110 int mi_capflags; // WF_ONECAP WF_ALLCAP WF_KEEPCAP
111 win_T *mi_win; // buffer being checked
Bram Moolenaar78622822005-08-23 21:00:13 +0000112
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100113 // for NOBREAK
114 int mi_result2; // "mi_resul" without following word
115 char_u *mi_end2; // "mi_end" without following word
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000116} matchinf_T;
117
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000118
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100119static int spell_mb_isword_class(int cl, win_T *wp);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000120
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100121// mode values for find_word
122#define FIND_FOLDWORD 0 // find word case-folded
123#define FIND_KEEPWORD 1 // find keep-case word
124#define FIND_PREFIX 2 // find word after prefix
125#define FIND_COMPOUND 3 // find case-folded compound word
126#define FIND_KEEPCOMPOUND 4 // find keep-case compound word
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000127
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100128static void find_word(matchinf_T *mip, int mode);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100129static void find_prefix(matchinf_T *mip, int mode);
130static int fold_more(matchinf_T *mip);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100131static void spell_load_cb(char_u *fname, void *cookie);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100132static int count_syllables(slang_T *slang, char_u *word);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void clear_midword(win_T *buf);
134static void use_midword(slang_T *lp, win_T *buf);
135static int find_region(char_u *rp, char_u *region);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100136static void spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res);
137static void spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100138static void spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100139static void dump_word(slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum);
140static 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 +0000141
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000142/*
143 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000144 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000145 * "*attrp" is set to the highlight index for a badly spelled word. For a
146 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000147 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000148 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000149 * "capcol" is used to check for a Capitalised word after the end of a
150 * sentence. If it's zero then perform the check. Return the column where to
151 * check next, or -1 when no sentence end was found. If it's NULL then don't
152 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000153 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000154 * Returns the length of the word in bytes, also when it's OK, so that the
155 * caller can skip over the word.
156 */
157 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100158spell_check(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100159 win_T *wp, // current window
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100160 char_u *ptr,
161 hlf_T *attrp,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100162 int *capcol, // column to check for Capital
163 int docount) // count good words
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000164{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100165 matchinf_T mi; // Most things are put in "mi" so that it can
166 // be passed to functions quickly.
167 int nrlen = 0; // found a number first
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000168 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000169 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000170 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +0000171 int count_word = docount;
Bram Moolenaare0ebeda2020-06-10 22:17:58 +0200172 int use_camel_case = *wp->w_s->b_p_spo != NUL;
173 int camel_case = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000174
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100175 // A word never starts at a space or a control character. Return quickly
176 // then, skipping over the character.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000177 if (*ptr <= ' ')
178 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000179
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100180 // Return here when loading language files failed.
Bram Moolenaar860cae12010-06-05 23:22:07 +0200181 if (wp->w_s->b_langp.ga_len == 0)
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000182 return 1;
183
Bram Moolenaara80faa82020-04-12 19:37:17 +0200184 CLEAR_FIELD(mi);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000185
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100186 // A number is always OK. Also skip hexadecimal numbers 0xFF99 and
187 // 0X99FF. But always do check spelling to find "3GPP" and "11
188 // julifeest".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000189 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000190 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100191 if (*ptr == '0' && (ptr[1] == 'b' || ptr[1] == 'B'))
192 mi.mi_end = skipbin(ptr + 2);
193 else if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
Bram Moolenaar3982c542005-06-08 21:56:31 +0000194 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000195 else
196 mi.mi_end = skipdigits(ptr);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000197 nrlen = (int)(mi.mi_end - ptr);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000198 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000199
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100200 // Find the normal end of the word (until the next non-word character).
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000201 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +0000202 mi.mi_fend = ptr;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200203 if (spell_iswordp(mi.mi_fend, wp))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000204 {
Bram Moolenaare0ebeda2020-06-10 22:17:58 +0200205 int prev_upper;
Bram Moolenaar2d4070d2020-06-11 12:30:13 +0200206 int this_upper = FALSE; // init for gcc
Bram Moolenaare0ebeda2020-06-10 22:17:58 +0200207
208 if (use_camel_case)
209 {
210 c = PTR2CHAR(mi.mi_fend);
211 this_upper = SPELL_ISUPPER(c);
212 }
213
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000214 do
Bram Moolenaare0ebeda2020-06-10 22:17:58 +0200215 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100216 MB_PTR_ADV(mi.mi_fend);
Bram Moolenaare0ebeda2020-06-10 22:17:58 +0200217 if (use_camel_case)
218 {
219 prev_upper = this_upper;
220 c = PTR2CHAR(mi.mi_fend);
221 this_upper = SPELL_ISUPPER(c);
222 camel_case = !prev_upper && this_upper;
223 }
224 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp)
225 && !camel_case);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000226
Bram Moolenaar860cae12010-06-05 23:22:07 +0200227 if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000228 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100229 // Check word starting with capital letter.
Bram Moolenaar53805d12005-08-01 07:08:33 +0000230 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000231 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000232 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000233 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000234 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000235 if (capcol != NULL)
236 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000237
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100238 // We always use the characters up to the next non-word character,
239 // also for bad words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000240 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000241
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100242 // Check caps type later.
Bram Moolenaar860cae12010-06-05 23:22:07 +0200243 mi.mi_capflags = 0;
244 mi.mi_cend = NULL;
245 mi.mi_win = wp;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000246
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100247 // case-fold the word with one non-word character, so that we can check
248 // for the word end.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000249 if (*mi.mi_fend != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100250 MB_PTR_ADV(mi.mi_fend);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000251
252 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
253 MAXWLEN + 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000254 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000255
Bram Moolenaare0ebeda2020-06-10 22:17:58 +0200256 if (camel_case)
257 // Introduce a fake word end space into the folded word.
258 mi.mi_fword[mi.mi_fwordlen - 1] = ' ';
259
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100260 // The word is bad unless we recognize it.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000261 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +0000262 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000263
264 /*
265 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000266 * We check them all, because a word may be matched longer in another
267 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000268 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200269 for (lpi = 0; lpi < wp->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000270 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200271 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, lpi);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000272
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100273 // If reloading fails the language is still in the list but everything
274 // has been cleared.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000275 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
276 continue;
277
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100278 // Check for a matching word in case-folded words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000279 find_word(&mi, FIND_FOLDWORD);
280
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100281 // Check for a matching word in keep-case words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000282 find_word(&mi, FIND_KEEPWORD);
283
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100284 // Check for matching prefixes.
Bram Moolenaard12a1322005-08-21 22:08:24 +0000285 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +0000286
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100287 // For a NOBREAK language, may want to use a word without a following
288 // word as a backup.
Bram Moolenaar78622822005-08-23 21:00:13 +0000289 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
290 && mi.mi_result2 != SP_BAD)
291 {
292 mi.mi_result = mi.mi_result2;
293 mi.mi_end = mi.mi_end2;
294 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000295
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100296 // Count the word in the first language where it's found to be OK.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000297 if (count_word && mi.mi_result == SP_OK)
298 {
299 count_common_word(mi.mi_lp->lp_slang, ptr,
300 (int)(mi.mi_end - ptr), 1);
301 count_word = FALSE;
302 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000303 }
304
305 if (mi.mi_result != SP_OK)
306 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100307 // If we found a number skip over it. Allows for "42nd". Do flag
308 // rare and local words, e.g., "3GPP".
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000309 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000310 {
311 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
312 return nrlen;
313 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000314
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100315 // When we are at a non-word character there is no error, just
316 // skip over the character (try looking for a word after it).
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100317 else if (!spell_iswordp_nmw(ptr, wp))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000318 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200319 if (capcol != NULL && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000320 {
321 regmatch_T regmatch;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100322 int r;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000323
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100324 // Check for end of sentence.
Bram Moolenaar860cae12010-06-05 23:22:07 +0200325 regmatch.regprog = wp->w_s->b_cap_prog;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000326 regmatch.rm_ic = FALSE;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100327 r = vim_regexec(&regmatch, ptr, 0);
328 wp->w_s->b_cap_prog = regmatch.regprog;
329 if (r)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000330 *capcol = (int)(regmatch.endp[0] - ptr);
331 }
332
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000333 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000334 return (*mb_ptr2len)(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000335 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000336 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000337 else if (mi.mi_end == ptr)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100338 // Always include at least one character. Required for when there
339 // is a mixup in "midword".
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100340 MB_PTR_ADV(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +0000341 else if (mi.mi_result == SP_BAD
Bram Moolenaar860cae12010-06-05 23:22:07 +0200342 && LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak)
Bram Moolenaar78622822005-08-23 21:00:13 +0000343 {
344 char_u *p, *fp;
345 int save_result = mi.mi_result;
346
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100347 // First language in 'spelllang' is NOBREAK. Find first position
348 // at which any word would be valid.
Bram Moolenaar860cae12010-06-05 23:22:07 +0200349 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000350 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +0000351 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000352 p = mi.mi_word;
353 fp = mi.mi_fword;
354 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +0000355 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100356 MB_PTR_ADV(p);
357 MB_PTR_ADV(fp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000358 if (p >= mi.mi_end)
359 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000360 mi.mi_compoff = (int)(fp - mi.mi_fword);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000361 find_word(&mi, FIND_COMPOUND);
362 if (mi.mi_result != SP_BAD)
363 {
364 mi.mi_end = p;
365 break;
366 }
Bram Moolenaar78622822005-08-23 21:00:13 +0000367 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000368 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +0000369 }
Bram Moolenaar78622822005-08-23 21:00:13 +0000370 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000371
372 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000373 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000374 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000375 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000376 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000377 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000378 }
379
Bram Moolenaar5195e452005-08-19 20:32:47 +0000380 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
381 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100382 // Report SpellCap only when the word isn't badly spelled.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000383 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000384 return wrongcaplen;
385 }
386
Bram Moolenaar51485f02005-06-04 21:55:20 +0000387 return (int)(mi.mi_end - ptr);
388}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000389
Bram Moolenaar51485f02005-06-04 21:55:20 +0000390/*
391 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000392 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
393 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
394 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
395 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000396 *
397 * For a match mip->mi_result is updated.
398 */
399 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100400find_word(matchinf_T *mip, int mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000401{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000402 idx_T arridx = 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100403 int endlen[MAXWLEN]; // length at possible word endings
404 idx_T endidx[MAXWLEN]; // possible word endings
Bram Moolenaar51485f02005-06-04 21:55:20 +0000405 int endidxcnt = 0;
406 int len;
407 int wlen = 0;
408 int flen;
409 int c;
410 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000411 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000412 char_u *s;
Bram Moolenaare52325c2005-08-22 22:54:29 +0000413 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000414 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000415 slang_T *slang = mip->mi_lp->lp_slang;
416 unsigned flags;
417 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000418 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000419 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000420 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +0000421 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000422
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000423 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000424 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100425 // Check for word with matching case in keep-case tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000426 ptr = mip->mi_word;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100427 flen = 9999; // no case folding, always enough bytes
Bram Moolenaar51485f02005-06-04 21:55:20 +0000428 byts = slang->sl_kbyts;
429 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000430
431 if (mode == FIND_KEEPCOMPOUND)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100432 // Skip over the previously found word(s).
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000433 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000434 }
435 else
436 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100437 // Check for case-folded in case-folded tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000438 ptr = mip->mi_fword;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100439 flen = mip->mi_fwordlen; // available case-folded bytes
Bram Moolenaar51485f02005-06-04 21:55:20 +0000440 byts = slang->sl_fbyts;
441 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000442
443 if (mode == FIND_PREFIX)
444 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100445 // Skip over the prefix.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000446 wlen = mip->mi_prefixlen;
447 flen -= mip->mi_prefixlen;
448 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000449 else if (mode == FIND_COMPOUND)
450 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100451 // Skip over the previously found word(s).
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000452 wlen = mip->mi_compoff;
453 flen -= mip->mi_compoff;
454 }
455
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000456 }
457
Bram Moolenaar51485f02005-06-04 21:55:20 +0000458 if (byts == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100459 return; // array is empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000460
Bram Moolenaar51485f02005-06-04 21:55:20 +0000461 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000462 * Repeat advancing in the tree until:
463 * - there is a byte that doesn't match,
464 * - we reach the end of the tree,
465 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000466 */
467 for (;;)
468 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000469 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000470 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000471
472 len = byts[arridx++];
473
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100474 // If the first possible byte is a zero the word could end here.
475 // Remember this index, we first check for the longest word.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000476 if (byts[arridx] == 0)
477 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000478 if (endidxcnt == MAXWLEN)
479 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100480 // Must be a corrupted spell file.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100481 emsg(_(e_format));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000482 return;
483 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000484 endlen[endidxcnt] = wlen;
485 endidx[endidxcnt++] = arridx++;
486 --len;
487
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100488 // Skip over the zeros, there can be several flag/region
489 // combinations.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000490 while (len > 0 && byts[arridx] == 0)
491 {
492 ++arridx;
493 --len;
494 }
495 if (len == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100496 break; // no children, word must end here
Bram Moolenaar51485f02005-06-04 21:55:20 +0000497 }
498
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100499 // Stop looking at end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000500 if (ptr[wlen] == NUL)
501 break;
502
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100503 // Perform a binary search in the list of accepted bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000504 c = ptr[wlen];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100505 if (c == TAB) // <Tab> is handled like <Space>
Bram Moolenaar0c405862005-06-22 22:26:26 +0000506 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +0000507 lo = arridx;
508 hi = arridx + len - 1;
509 while (lo < hi)
510 {
511 m = (lo + hi) / 2;
512 if (byts[m] > c)
513 hi = m - 1;
514 else if (byts[m] < c)
515 lo = m + 1;
516 else
517 {
518 lo = hi = m;
519 break;
520 }
521 }
522
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100523 // Stop if there is no matching byte.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000524 if (hi < lo || byts[lo] != c)
525 break;
526
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100527 // Continue at the child (if there is one).
Bram Moolenaar51485f02005-06-04 21:55:20 +0000528 arridx = idxs[lo];
529 ++wlen;
530 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +0000531
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100532 // One space in the good word may stand for several spaces in the
533 // checked word.
Bram Moolenaar0c405862005-06-22 22:26:26 +0000534 if (c == ' ')
535 {
536 for (;;)
537 {
538 if (flen <= 0 && *mip->mi_fend != NUL)
539 flen = fold_more(mip);
540 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
541 break;
542 ++wlen;
543 --flen;
544 }
545 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000546 }
547
548 /*
549 * Verify that one of the possible endings is valid. Try the longest
550 * first.
551 */
552 while (endidxcnt > 0)
553 {
554 --endidxcnt;
555 arridx = endidx[endidxcnt];
556 wlen = endlen[endidxcnt];
557
Bram Moolenaar51485f02005-06-04 21:55:20 +0000558 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100559 continue; // not at first byte of character
Bram Moolenaar860cae12010-06-05 23:22:07 +0200560 if (spell_iswordp(ptr + wlen, mip->mi_win))
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000561 {
Bram Moolenaar78622822005-08-23 21:00:13 +0000562 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100563 continue; // next char is a word character
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000564 word_ends = FALSE;
565 }
566 else
567 word_ends = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100568 // The prefix flag is before compound flags. Once a valid prefix flag
569 // has been found we try compound flags.
Bram Moolenaard12a1322005-08-21 22:08:24 +0000570 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000571
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000572 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000573 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100574 // Compute byte length in original word, length may change
575 // when folding case. This can be slow, take a shortcut when the
576 // case-folded word is equal to the keep-case word.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000577 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000578 if (STRNCMP(ptr, p, wlen) != 0)
579 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100580 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s))
581 MB_PTR_ADV(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000582 wlen = (int)(p - mip->mi_word);
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000583 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000584 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000585
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100586 // Check flags and region. For FIND_PREFIX check the condition and
587 // prefix ID.
588 // Repeat this if there are more flags/region alternatives until there
589 // is a match.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000590 res = SP_BAD;
591 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
592 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000593 {
594 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000595
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100596 // For the fold-case tree check that the case of the checked word
597 // matches with what the word in the tree requires.
598 // For keep-case tree the case is always right. For prefixes we
599 // don't bother to check.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000600 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000601 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000602 if (mip->mi_cend != mip->mi_word + wlen)
603 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100604 // mi_capflags was set for a different word length, need
605 // to do it again.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000606 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000607 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000608 }
609
Bram Moolenaar0c405862005-06-22 22:26:26 +0000610 if (mip->mi_capflags == WF_KEEPCAP
611 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000612 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000613 }
614
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100615 // When mode is FIND_PREFIX the word must support the prefix:
616 // check the prefix ID and the condition. Do that for the list at
617 // mip->mi_prefarridx that find_prefix() filled.
Bram Moolenaard12a1322005-08-21 22:08:24 +0000618 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000619 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000620 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000621 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +0000622 mip->mi_word + mip->mi_cprefixlen, slang,
623 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000624 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000625 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000626
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100627 // Use the WF_RARE flag for a rare prefix.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000628 if (c & WF_RAREPFX)
629 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000630 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000631 }
632
Bram Moolenaar78622822005-08-23 21:00:13 +0000633 if (slang->sl_nobreak)
634 {
635 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
636 && (flags & WF_BANNED) == 0)
637 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100638 // NOBREAK: found a valid following word. That's all we
639 // need to know, so return.
Bram Moolenaar78622822005-08-23 21:00:13 +0000640 mip->mi_result = SP_OK;
641 break;
642 }
643 }
644
645 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
646 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000647 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100648 // If there is no compound flag or the word is shorter than
649 // COMPOUNDMIN reject it quickly.
650 // Makes you wonder why someone puts a compound flag on a word
651 // that's too short... Myspell compatibility requires this
652 // anyway.
Bram Moolenaare52325c2005-08-22 22:54:29 +0000653 if (((unsigned)flags >> 24) == 0
654 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000655 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100656 // For multi-byte chars check character length against
657 // COMPOUNDMIN.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000658 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000659 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000660 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
661 wlen - mip->mi_compoff) < slang->sl_compminlen)
662 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000663
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100664 // Limit the number of compound words to COMPOUNDWORDMAX if no
665 // maximum for syllables is specified.
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000666 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
667 > slang->sl_compmax
Bram Moolenaare52325c2005-08-22 22:54:29 +0000668 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000669 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000670
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100671 // Don't allow compounding on a side where an affix was added,
672 // unless COMPOUNDPERMITFLAG was used.
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000673 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
674 continue;
675 if (!word_ends && (flags & WF_NOCOMPAFT))
676 continue;
677
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100678 // Quickly check if compounding is possible with this flag.
Bram Moolenaar6de68532005-08-24 22:08:48 +0000679 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +0000680 ? slang->sl_compstartflags
681 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +0000682 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000683 continue;
684
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100685 // If there is a match with a CHECKCOMPOUNDPATTERN rule
686 // discard the compound word.
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000687 if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat))
688 continue;
689
Bram Moolenaare52325c2005-08-22 22:54:29 +0000690 if (mode == FIND_COMPOUND)
691 {
692 int capflags;
693
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100694 // Need to check the caps type of the appended compound
695 // word.
Bram Moolenaare52325c2005-08-22 22:54:29 +0000696 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
697 mip->mi_compoff) != 0)
698 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100699 // case folding may have changed the length
Bram Moolenaare52325c2005-08-22 22:54:29 +0000700 p = mip->mi_word;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100701 for (s = ptr; s < ptr + mip->mi_compoff; MB_PTR_ADV(s))
702 MB_PTR_ADV(p);
Bram Moolenaare52325c2005-08-22 22:54:29 +0000703 }
704 else
Bram Moolenaare52325c2005-08-22 22:54:29 +0000705 p = mip->mi_word + mip->mi_compoff;
706 capflags = captype(p, mip->mi_word + wlen);
707 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
708 && (flags & WF_FIXCAP) != 0))
709 continue;
710
711 if (capflags != WF_ALLCAP)
712 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100713 // When the character before the word is a word
714 // character we do not accept a Onecap word. We do
715 // accept a no-caps word, even when the dictionary
716 // word specifies ONECAP.
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100717 MB_PTR_BACK(mip->mi_word, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100718 if (spell_iswordp_nmw(p, mip->mi_win)
Bram Moolenaare52325c2005-08-22 22:54:29 +0000719 ? capflags == WF_ONECAP
720 : (flags & WF_ONECAP) != 0
721 && capflags != WF_ONECAP)
722 continue;
723 }
724 }
725
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100726 // If the word ends the sequence of compound flags of the
727 // words must match with one of the COMPOUNDRULE items and
728 // the number of syllables must not be too large.
Bram Moolenaar5195e452005-08-19 20:32:47 +0000729 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
730 mip->mi_compflags[mip->mi_complen + 1] = NUL;
731 if (word_ends)
732 {
733 char_u fword[MAXWLEN];
734
735 if (slang->sl_compsylmax < MAXWLEN)
736 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100737 // "fword" is only needed for checking syllables.
Bram Moolenaar5195e452005-08-19 20:32:47 +0000738 if (ptr == mip->mi_word)
739 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
740 else
741 vim_strncpy(fword, ptr, endlen[endidxcnt]);
742 }
743 if (!can_compound(slang, fword, mip->mi_compflags))
744 continue;
745 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000746 else if (slang->sl_comprules != NULL
747 && !match_compoundrule(slang, mip->mi_compflags))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100748 // The compound flags collected so far do not match any
749 // COMPOUNDRULE, discard the compounded word.
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000750 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000751 }
752
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100753 // Check NEEDCOMPOUND: can't use word without compounding.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000754 else if (flags & WF_NEEDCOMP)
755 continue;
756
Bram Moolenaar78622822005-08-23 21:00:13 +0000757 nobreak_result = SP_OK;
758
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000759 if (!word_ends)
760 {
Bram Moolenaar78622822005-08-23 21:00:13 +0000761 int save_result = mip->mi_result;
762 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000763 langp_T *save_lp = mip->mi_lp;
764 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +0000765
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100766 // Check that a valid word follows. If there is one and we
767 // are compounding, it will set "mi_result", thus we are
768 // always finished here. For NOBREAK we only check that a
769 // valid word follows.
770 // Recursive!
Bram Moolenaar78622822005-08-23 21:00:13 +0000771 if (slang->sl_nobreak)
772 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000773
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100774 // Find following word in case-folded tree.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000775 mip->mi_compoff = endlen[endidxcnt];
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000776 if (has_mbyte && mode == FIND_KEEPWORD)
777 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100778 // Compute byte length in case-folded word from "wlen":
779 // byte length in keep-case word. Length may change when
780 // folding case. This can be slow, take a shortcut when
781 // the case-folded word is equal to the keep-case word.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000782 p = mip->mi_fword;
783 if (STRNCMP(ptr, p, wlen) != 0)
784 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100785 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s))
786 MB_PTR_ADV(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000787 mip->mi_compoff = (int)(p - mip->mi_fword);
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000788 }
789 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100790#if 0 // Disabled, see below
Bram Moolenaard12a1322005-08-21 22:08:24 +0000791 c = mip->mi_compoff;
Bram Moolenaarba534352016-04-21 09:20:26 +0200792#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000793 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000794 if (flags & WF_COMPROOT)
795 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000796
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100797 // For NOBREAK we need to try all NOBREAK languages, at least
798 // to find the ".add" file(s).
Bram Moolenaar860cae12010-06-05 23:22:07 +0200799 for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +0000800 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000801 if (slang->sl_nobreak)
802 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200803 mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi);
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000804 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
805 || !mip->mi_lp->lp_slang->sl_nobreak)
806 continue;
807 }
Bram Moolenaard12a1322005-08-21 22:08:24 +0000808
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000809 find_word(mip, FIND_COMPOUND);
810
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100811 // When NOBREAK any word that matches is OK. Otherwise we
812 // need to find the longest match, thus try with keep-case
813 // and prefix too.
Bram Moolenaar78622822005-08-23 21:00:13 +0000814 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
815 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100816 // Find following word in keep-case tree.
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000817 mip->mi_compoff = wlen;
818 find_word(mip, FIND_KEEPCOMPOUND);
819
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100820#if 0 // Disabled, a prefix must not appear halfway a compound word,
821 // unless the COMPOUNDPERMITFLAG is used and then it can't be a
822 // postponed prefix.
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000823 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
824 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100825 // Check for following word with prefix.
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000826 mip->mi_compoff = c;
827 find_prefix(mip, FIND_COMPOUND);
828 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000829#endif
Bram Moolenaar78622822005-08-23 21:00:13 +0000830 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000831
832 if (!slang->sl_nobreak)
833 break;
Bram Moolenaar78622822005-08-23 21:00:13 +0000834 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000835 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000836 if (flags & WF_COMPROOT)
837 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000838 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000839
Bram Moolenaar78622822005-08-23 21:00:13 +0000840 if (slang->sl_nobreak)
841 {
842 nobreak_result = mip->mi_result;
843 mip->mi_result = save_result;
844 mip->mi_end = save_end;
845 }
846 else
847 {
848 if (mip->mi_result == SP_OK)
849 break;
850 continue;
851 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000852 }
853
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000854 if (flags & WF_BANNED)
855 res = SP_BANNED;
856 else if (flags & WF_REGION)
857 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100858 // Check region.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000859 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000860 res = SP_OK;
861 else
862 res = SP_LOCAL;
863 }
864 else if (flags & WF_RARE)
865 res = SP_RARE;
866 else
867 res = SP_OK;
868
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100869 // Always use the longest match and the best result. For NOBREAK
870 // we separately keep the longest match without a following good
871 // word as a fall-back.
Bram Moolenaar78622822005-08-23 21:00:13 +0000872 if (nobreak_result == SP_BAD)
873 {
874 if (mip->mi_result2 > res)
875 {
876 mip->mi_result2 = res;
877 mip->mi_end2 = mip->mi_word + wlen;
878 }
879 else if (mip->mi_result2 == res
880 && mip->mi_end2 < mip->mi_word + wlen)
881 mip->mi_end2 = mip->mi_word + wlen;
882 }
883 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000884 {
885 mip->mi_result = res;
886 mip->mi_end = mip->mi_word + wlen;
887 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000888 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000889 mip->mi_end = mip->mi_word + wlen;
890
Bram Moolenaar78622822005-08-23 21:00:13 +0000891 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000892 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000893 }
894
Bram Moolenaar78622822005-08-23 21:00:13 +0000895 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000896 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000897 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000898}
899
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000900/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000901 * Return TRUE if there is a match between the word ptr[wlen] and
902 * CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another
903 * word.
904 * A match means that the first part of CHECKCOMPOUNDPATTERN matches at the
905 * end of ptr[wlen] and the second part matches after it.
906 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +0200907 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100908match_checkcompoundpattern(
909 char_u *ptr,
910 int wlen,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100911 garray_T *gap) // &sl_comppat
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000912{
913 int i;
914 char_u *p;
915 int len;
916
917 for (i = 0; i + 1 < gap->ga_len; i += 2)
918 {
919 p = ((char_u **)gap->ga_data)[i + 1];
920 if (STRNCMP(ptr + wlen, p, STRLEN(p)) == 0)
921 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100922 // Second part matches at start of following compound word, now
923 // check if first part matches at end of previous word.
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000924 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar19c9c762008-12-09 21:34:39 +0000925 len = (int)STRLEN(p);
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000926 if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0)
927 return TRUE;
928 }
929 }
930 return FALSE;
931}
932
933/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000934 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
935 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000936 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +0200937 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100938can_compound(slang_T *slang, char_u *word, char_u *flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000939{
Bram Moolenaar6de68532005-08-24 22:08:48 +0000940 char_u uflags[MAXWLEN * 2];
941 int i;
Bram Moolenaar6de68532005-08-24 22:08:48 +0000942 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000943
944 if (slang->sl_compprog == NULL)
945 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +0000946 if (enc_utf8)
947 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100948 // Need to convert the single byte flags to utf8 characters.
Bram Moolenaar6de68532005-08-24 22:08:48 +0000949 p = uflags;
950 for (i = 0; flags[i] != NUL; ++i)
Bram Moolenaarace95982017-03-29 17:30:27 +0200951 p += utf_char2bytes(flags[i], p);
Bram Moolenaar6de68532005-08-24 22:08:48 +0000952 *p = NUL;
953 p = uflags;
954 }
955 else
Bram Moolenaar6de68532005-08-24 22:08:48 +0000956 p = flags;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100957 if (!vim_regexec_prog(&slang->sl_compprog, FALSE, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000958 return FALSE;
959
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100960 // Count the number of syllables. This may be slow, do it last. If there
961 // are too many syllables AND the number of compound words is above
962 // COMPOUNDWORDMAX then compounding is not allowed.
Bram Moolenaar5195e452005-08-19 20:32:47 +0000963 if (slang->sl_compsylmax < MAXWLEN
964 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +0000965 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000966 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000967}
968
969/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000970 * Return TRUE if the compound flags in compflags[] match the start of any
971 * compound rule. This is used to stop trying a compound if the flags
972 * collected so far can't possibly match any compound rule.
973 * Caller must check that slang->sl_comprules is not NULL.
974 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +0200975 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100976match_compoundrule(slang_T *slang, char_u *compflags)
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000977{
978 char_u *p;
979 int i;
980 int c;
981
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100982 // loop over all the COMPOUNDRULE entries
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000983 for (p = slang->sl_comprules; *p != NUL; ++p)
984 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100985 // loop over the flags in the compound word we have made, match
986 // them against the current rule entry
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000987 for (i = 0; ; ++i)
988 {
989 c = compflags[i];
990 if (c == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100991 // found a rule that matches for the flags we have so far
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000992 return TRUE;
993 if (*p == '/' || *p == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100994 break; // end of rule, it's too short
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000995 if (*p == '[')
996 {
997 int match = FALSE;
998
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100999 // compare against all the flags in []
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001000 ++p;
1001 while (*p != ']' && *p != NUL)
1002 if (*p++ == c)
1003 match = TRUE;
1004 if (!match)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001005 break; // none matches
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001006 }
1007 else if (*p != c)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001008 break; // flag of word doesn't match flag in pattern
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001009 ++p;
1010 }
1011
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001012 // Skip to the next "/", where the next pattern starts.
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001013 p = vim_strchr(p, '/');
1014 if (p == NULL)
1015 break;
1016 }
1017
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001018 // Checked all the rules and none of them match the flags, so there
1019 // can't possibly be a compound starting with these flags.
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001020 return FALSE;
1021}
1022
1023/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001024 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1025 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001026 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001027 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02001028 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001029valid_word_prefix(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001030 int totprefcnt, // nr of prefix IDs
1031 int arridx, // idx in sl_pidxs[]
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001032 int flags,
1033 char_u *word,
1034 slang_T *slang,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001035 int cond_req) // only use prefixes with a condition
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001036{
1037 int prefcnt;
1038 int pidx;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001039 regprog_T **rp;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001040 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001041
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001042 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001043 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1044 {
1045 pidx = slang->sl_pidxs[arridx + prefcnt];
1046
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001047 // Check the prefix ID.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001048 if (prefid != (pidx & 0xff))
1049 continue;
1050
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001051 // Check if the prefix doesn't combine and the word already has a
1052 // suffix.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001053 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1054 continue;
1055
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001056 // Check the condition, if there is one. The condition index is
1057 // stored in the two bytes above the prefix ID byte.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001058 rp = &slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
1059 if (*rp != NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001060 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001061 if (!vim_regexec_prog(rp, FALSE, word, 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001062 continue;
1063 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001064 else if (cond_req)
1065 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001066
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001067 // It's a match! Return the WF_ flags.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001068 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001069 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001070 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001071}
1072
1073/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001074 * Check if the word at "mip->mi_word" has a matching prefix.
1075 * If it does, then check the following word.
1076 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001077 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1078 * prefix in a compound word.
1079 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001080 * For a match mip->mi_result is updated.
1081 */
1082 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001083find_prefix(matchinf_T *mip, int mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001084{
1085 idx_T arridx = 0;
1086 int len;
1087 int wlen = 0;
1088 int flen;
1089 int c;
1090 char_u *ptr;
1091 idx_T lo, hi, m;
1092 slang_T *slang = mip->mi_lp->lp_slang;
1093 char_u *byts;
1094 idx_T *idxs;
1095
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001096 byts = slang->sl_pbyts;
1097 if (byts == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001098 return; // array is empty
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001099
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001100 // We use the case-folded word here, since prefixes are always
1101 // case-folded.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001102 ptr = mip->mi_fword;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001103 flen = mip->mi_fwordlen; // available case-folded bytes
Bram Moolenaard12a1322005-08-21 22:08:24 +00001104 if (mode == FIND_COMPOUND)
1105 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001106 // Skip over the previously found word(s).
Bram Moolenaard12a1322005-08-21 22:08:24 +00001107 ptr += mip->mi_compoff;
1108 flen -= mip->mi_compoff;
1109 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001110 idxs = slang->sl_pidxs;
1111
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001112 /*
1113 * Repeat advancing in the tree until:
1114 * - there is a byte that doesn't match,
1115 * - we reach the end of the tree,
1116 * - or we reach the end of the line.
1117 */
1118 for (;;)
1119 {
1120 if (flen == 0 && *mip->mi_fend != NUL)
1121 flen = fold_more(mip);
1122
1123 len = byts[arridx++];
1124
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001125 // If the first possible byte is a zero the prefix could end here.
1126 // Check if the following word matches and supports the prefix.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001127 if (byts[arridx] == 0)
1128 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001129 // There can be several prefixes with different conditions. We
1130 // try them all, since we don't know which one will give the
1131 // longest match. The word is the same each time, pass the list
1132 // of possible prefixes to find_word().
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001133 mip->mi_prefarridx = arridx;
1134 mip->mi_prefcnt = len;
1135 while (len > 0 && byts[arridx] == 0)
1136 {
1137 ++arridx;
1138 --len;
1139 }
1140 mip->mi_prefcnt -= len;
1141
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001142 // Find the word that comes after the prefix.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001143 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001144 if (mode == FIND_COMPOUND)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001145 // Skip over the previously found word(s).
Bram Moolenaard12a1322005-08-21 22:08:24 +00001146 mip->mi_prefixlen += mip->mi_compoff;
1147
Bram Moolenaar53805d12005-08-01 07:08:33 +00001148 if (has_mbyte)
1149 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001150 // Case-folded length may differ from original length.
Bram Moolenaard12a1322005-08-21 22:08:24 +00001151 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1152 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001153 }
1154 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001155 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001156 find_word(mip, FIND_PREFIX);
1157
1158
1159 if (len == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001160 break; // no children, word must end here
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001161 }
1162
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001163 // Stop looking at end of the line.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001164 if (ptr[wlen] == NUL)
1165 break;
1166
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001167 // Perform a binary search in the list of accepted bytes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001168 c = ptr[wlen];
1169 lo = arridx;
1170 hi = arridx + len - 1;
1171 while (lo < hi)
1172 {
1173 m = (lo + hi) / 2;
1174 if (byts[m] > c)
1175 hi = m - 1;
1176 else if (byts[m] < c)
1177 lo = m + 1;
1178 else
1179 {
1180 lo = hi = m;
1181 break;
1182 }
1183 }
1184
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001185 // Stop if there is no matching byte.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001186 if (hi < lo || byts[lo] != c)
1187 break;
1188
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001189 // Continue at the child (if there is one).
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001190 arridx = idxs[lo];
1191 ++wlen;
1192 --flen;
1193 }
1194}
1195
1196/*
1197 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001198 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001199 * Return the length of the folded chars in bytes.
1200 */
1201 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001202fold_more(matchinf_T *mip)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001203{
1204 int flen;
1205 char_u *p;
1206
1207 p = mip->mi_fend;
1208 do
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001209 MB_PTR_ADV(mip->mi_fend);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001210 while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001211
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001212 // Include the non-word character so that we can check for the word end.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001213 if (*mip->mi_fend != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001214 MB_PTR_ADV(mip->mi_fend);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001215
1216 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1217 mip->mi_fword + mip->mi_fwordlen,
1218 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001219 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001220 mip->mi_fwordlen += flen;
1221 return flen;
1222}
1223
1224/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001225 * Check case flags for a word. Return TRUE if the word has the requested
1226 * case.
1227 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02001228 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001229spell_valid_case(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001230 int wordflags, // flags for the checked word.
1231 int treeflags) // flags for the word in the spell tree
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001232{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001233 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001234 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001235 && ((treeflags & WF_ONECAP) == 0
1236 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001237}
1238
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001239/*
1240 * Return TRUE if spell checking is not enabled.
1241 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02001242 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001243no_spell_checking(win_T *wp)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001244{
Bram Moolenaar860cae12010-06-05 23:22:07 +02001245 if (!wp->w_p_spell || *wp->w_s->b_p_spl == NUL
1246 || wp->w_s->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001247 {
Bram Moolenaar152e79e2020-06-10 15:32:08 +02001248 emsg(_(e_no_spell));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001249 return TRUE;
1250 }
1251 return FALSE;
1252}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001253
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001254/*
1255 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001256 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
1257 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001258 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1259 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00001260 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001261 */
1262 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001263spell_move_to(
1264 win_T *wp,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001265 int dir, // FORWARD or BACKWARD
1266 int allwords, // TRUE for "[s"/"]s", FALSE for "[S"/"]S"
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001267 int curline,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001268 hlf_T *attrp) // return: attributes of bad word or NULL
1269 // (only when "dir" is FORWARD)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001270{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001271 linenr_T lnum;
1272 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001273 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001274 char_u *line;
1275 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001276 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001277 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001278 int len;
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001279#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001280 int has_syntax = syntax_present(wp);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001281#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001282 int col;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001283 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001284 char_u *buf = NULL;
1285 int buflen = 0;
1286 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001287 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001288 int found_one = FALSE;
1289 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001290
Bram Moolenaar95529562005-08-25 21:21:38 +00001291 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00001292 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001293
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001294 /*
1295 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00001296 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001297 *
1298 * When searching backwards, we continue in the line to find the last
1299 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001300 *
1301 * We concatenate the start of the next line, so that wrapped words work
1302 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1303 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001304 */
Bram Moolenaar95529562005-08-25 21:21:38 +00001305 lnum = wp->w_cursor.lnum;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001306 CLEAR_POS(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001307
1308 while (!got_int)
1309 {
Bram Moolenaar95529562005-08-25 21:21:38 +00001310 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001311
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001312 len = (int)STRLEN(line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001313 if (buflen < len + MAXWLEN + 2)
1314 {
1315 vim_free(buf);
1316 buflen = len + MAXWLEN + 2;
1317 buf = alloc(buflen);
1318 if (buf == NULL)
1319 break;
1320 }
1321
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001322 // In first line check first word for Capital.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001323 if (lnum == 1)
1324 capcol = 0;
1325
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001326 // For checking first word with a capital skip white space.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001327 if (capcol == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001328 capcol = getwhitecols(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001329 else if (curline && wp == curwin)
1330 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001331 // For spellbadword(): check if first word needs a capital.
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001332 col = getwhitecols(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001333 if (check_need_cap(lnum, col))
1334 capcol = col;
1335
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001336 // Need to get the line again, may have looked at the previous
1337 // one.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001338 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1339 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001340
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001341 // Copy the line into "buf" and append the start of the next line if
1342 // possible.
Bram Moolenaar0c405862005-06-22 22:26:26 +00001343 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00001344 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar5dd95a12006-05-13 12:09:24 +00001345 spell_cat_line(buf + STRLEN(buf),
1346 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001347
1348 p = buf + skip;
1349 endp = buf + len;
1350 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001351 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001352 // When searching backward don't search after the cursor. Unless
1353 // we wrapped around the end of the buffer.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001354 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00001355 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001356 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00001357 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001358 break;
1359
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001360 // start of word
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001361 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001362 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001363
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001364 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001365 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001366 // We found a bad word. Check the attribute.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001367 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001368 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001369 // When searching forward only accept a bad word after
1370 // the cursor.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001371 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001372 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00001373 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001374 && (wrapped
1375 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00001376 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001377 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001378 {
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001379#ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00001380 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001381 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001382 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001383 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00001384 FALSE, &can_spell, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001385 if (!can_spell)
1386 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001387 }
1388 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001389#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001390 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001391
Bram Moolenaar51485f02005-06-04 21:55:20 +00001392 if (can_spell)
1393 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00001394 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001395 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001396 found_pos.col = (int)(p - buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001397 found_pos.coladd = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001398 if (dir == FORWARD)
1399 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001400 // No need to search further.
Bram Moolenaar95529562005-08-25 21:21:38 +00001401 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001402 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001403 if (attrp != NULL)
1404 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001405 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001406 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001407 else if (curline)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001408 // Insert mode completion: put cursor after
1409 // the bad word.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001410 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001411 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001412 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001413 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00001414 else
1415 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001416 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001417 }
1418
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001419 // advance to character after the word
Bram Moolenaar51485f02005-06-04 21:55:20 +00001420 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001421 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001422 }
1423
Bram Moolenaar5195e452005-08-19 20:32:47 +00001424 if (dir == BACKWARD && found_pos.lnum != 0)
1425 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001426 // Use the last match in the line (before the cursor).
Bram Moolenaar95529562005-08-25 21:21:38 +00001427 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001428 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001429 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001430 }
1431
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001432 if (curline)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001433 break; // only check cursor line
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001434
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001435 // If we are back at the starting line and searched it again there
1436 // is no match, give up.
Bram Moolenaard3f78dc2017-02-25 14:21:10 +01001437 if (lnum == wp->w_cursor.lnum && wrapped)
1438 break;
1439
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001440 // Advance to next line.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001441 if (dir == BACKWARD)
1442 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001443 if (lnum > 1)
1444 --lnum;
1445 else if (!p_ws)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001446 break; // at first line and 'nowrapscan'
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001447 else
1448 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001449 // Wrap around to the end of the buffer. May search the
1450 // starting line again and accept the last match.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001451 lnum = wp->w_buffer->b_ml.ml_line_count;
1452 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001453 if (!shortmess(SHM_SEARCH))
1454 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001455 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001456 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001457 }
1458 else
1459 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001460 if (lnum < wp->w_buffer->b_ml.ml_line_count)
1461 ++lnum;
1462 else if (!p_ws)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001463 break; // at first line and 'nowrapscan'
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001464 else
1465 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001466 // Wrap around to the start of the buffer. May search the
1467 // starting line again and accept the first match.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001468 lnum = 1;
1469 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001470 if (!shortmess(SHM_SEARCH))
1471 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001472 }
1473
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001474 // If we are back at the starting line and there is no match then
1475 // give up.
Bram Moolenaard3f78dc2017-02-25 14:21:10 +01001476 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001477 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001478
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001479 // Skip the characters at the start of the next line that were
1480 // included in a match crossing line boundaries.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001481 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001482 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001483 else
1484 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001485
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001486 // Capcol skips over the inserted space.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001487 --capcol;
1488
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001489 // But after empty line check first word in next line
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001490 if (*skipwhite(line) == NUL)
1491 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001492 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001493
1494 line_breakcheck();
1495 }
1496
Bram Moolenaar0c405862005-06-22 22:26:26 +00001497 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001498 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001499}
1500
1501/*
1502 * For spell checking: concatenate the start of the following line "line" into
1503 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00001504 * Keep the blanks at the start of the next line, this is used in win_line()
1505 * to skip those bytes if the word was OK.
Bram Moolenaar0c405862005-06-22 22:26:26 +00001506 */
1507 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001508spell_cat_line(char_u *buf, char_u *line, int maxlen)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001509{
1510 char_u *p;
1511 int n;
1512
1513 p = skipwhite(line);
1514 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1515 p = skipwhite(p + 1);
1516
1517 if (*p != NUL)
1518 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001519 // Only worth concatenating if there is something else than spaces to
1520 // concatenate.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00001521 n = (int)(p - line) + 1;
1522 if (n < maxlen - 1)
1523 {
1524 vim_memset(buf, ' ', n);
1525 vim_strncpy(buf + n, p, maxlen - 1 - n);
1526 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00001527 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001528}
1529
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001530/*
1531 * Structure used for the cookie argument of do_in_runtimepath().
1532 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001533typedef struct spelload_S
1534{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001535 char_u sl_lang[MAXWLEN + 1]; // language name
1536 slang_T *sl_slang; // resulting slang_T struct
1537 int sl_nobreak; // NOBREAK language found
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001538} spelload_T;
1539
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001540/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001541 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001542 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001543 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001544 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001545spell_load_lang(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001546{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001547 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001548 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001549 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001550 int round;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001551
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001552 // Copy the language name to pass it to spell_load_cb() as a cookie.
1553 // It's truncated when an error is detected.
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001554 STRCPY(sl.sl_lang, lang);
1555 sl.sl_slang = NULL;
1556 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001557
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001558 // We may retry when no spell file is found for the language, an
1559 // autocommand may load it then.
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001560 for (round = 1; round <= 2; ++round)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001561 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001562 /*
1563 * Find the first spell file for "lang" in 'runtimepath' and load it.
1564 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001565 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001566#ifdef VMS
1567 "spell/%s_%s.spl",
1568#else
1569 "spell/%s.%s.spl",
1570#endif
1571 lang, spell_enc());
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001572 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001573
1574 if (r == FAIL && *sl.sl_lang != NUL)
1575 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001576 // Try loading the ASCII version.
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001577 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001578#ifdef VMS
1579 "spell/%s_ascii.spl",
1580#else
1581 "spell/%s.ascii.spl",
1582#endif
1583 lang);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001584 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001585
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001586 if (r == FAIL && *sl.sl_lang != NUL && round == 1
1587 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
1588 curbuf->b_fname, FALSE, curbuf))
1589 continue;
1590 break;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001591 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001592 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001593 }
1594
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001595 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001596 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001597 smsg(
Bram Moolenaar56f78042010-12-08 17:09:32 +01001598#ifdef VMS
1599 _("Warning: Cannot find word list \"%s_%s.spl\" or \"%s_ascii.spl\""),
1600#else
1601 _("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
1602#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00001603 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001604 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001605 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001606 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001607 // At least one file was loaded, now load ALL the additions.
Bram Moolenaarb765d632005-06-07 21:00:02 +00001608 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001609 do_in_runtimepath(fname_enc, DIP_ALL, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001610 }
1611}
1612
1613/*
1614 * Return the encoding used for spell checking: Use 'encoding', except that we
1615 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1616 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001617 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001618spell_enc(void)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001619{
1620
Bram Moolenaarb765d632005-06-07 21:00:02 +00001621 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1622 return p_enc;
Bram Moolenaarb765d632005-06-07 21:00:02 +00001623 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001624}
1625
1626/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001627 * Get the name of the .spl file for the internal wordlist into
1628 * "fname[MAXPATHL]".
1629 */
1630 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001631int_wordlist_spl(char_u *fname)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001632{
Bram Moolenaar56f78042010-12-08 17:09:32 +01001633 vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001634 int_wordlist, spell_enc());
1635}
1636
1637/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001638 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001639 * Caller must fill "sl_next".
1640 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001641 slang_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001642slang_alloc(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001643{
1644 slang_T *lp;
1645
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001646 lp = ALLOC_CLEAR_ONE(slang_T);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001647 if (lp != NULL)
1648 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001649 if (lang != NULL)
1650 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001651 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001652 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001653 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001654 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001655 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001656 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001657
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001658 return lp;
1659}
1660
1661/*
1662 * Free the contents of an slang_T and the structure itself.
1663 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001664 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001665slang_free(slang_T *lp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001666{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001667 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001668 vim_free(lp->sl_fname);
1669 slang_clear(lp);
1670 vim_free(lp);
1671}
1672
1673/*
1674 * Clear an slang_T so that the file can be reloaded.
1675 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001676 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001677slang_clear(slang_T *lp)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001678{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001679 garray_T *gap;
1680 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001681 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001682 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001683 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001684
Bram Moolenaard23a8232018-02-10 18:45:26 +01001685 VIM_CLEAR(lp->sl_fbyts);
1686 VIM_CLEAR(lp->sl_kbyts);
1687 VIM_CLEAR(lp->sl_pbyts);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001688
Bram Moolenaard23a8232018-02-10 18:45:26 +01001689 VIM_CLEAR(lp->sl_fidxs);
1690 VIM_CLEAR(lp->sl_kidxs);
1691 VIM_CLEAR(lp->sl_pidxs);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001692
Bram Moolenaar4770d092006-01-12 23:22:24 +00001693 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001694 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001695 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
1696 while (gap->ga_len > 0)
1697 {
1698 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1699 vim_free(ftp->ft_from);
1700 vim_free(ftp->ft_to);
1701 }
1702 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001703 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001704
1705 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001706 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001707 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001708 // "ga_len" is set to 1 without adding an item for latin1
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001709 if (gap->ga_data != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001710 // SOFOFROM and SOFOTO items: free lists of wide characters.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001711 for (i = 0; i < gap->ga_len; ++i)
1712 vim_free(((int **)gap->ga_data)[i]);
1713 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001714 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001715 // SAL items: free salitem_T items
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001716 while (gap->ga_len > 0)
1717 {
1718 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1719 vim_free(smp->sm_lead);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001720 // Don't free sm_oneof and sm_rules, they point into sm_lead.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001721 vim_free(smp->sm_to);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001722 vim_free(smp->sm_lead_w);
1723 vim_free(smp->sm_oneof_w);
1724 vim_free(smp->sm_to_w);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001725 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001726 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001727
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001728 for (i = 0; i < lp->sl_prefixcnt; ++i)
Bram Moolenaar473de612013-06-08 18:19:48 +02001729 vim_regfree(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001730 lp->sl_prefixcnt = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001731 VIM_CLEAR(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001732
Bram Moolenaard23a8232018-02-10 18:45:26 +01001733 VIM_CLEAR(lp->sl_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001734
Bram Moolenaard23a8232018-02-10 18:45:26 +01001735 VIM_CLEAR(lp->sl_midword);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001736
Bram Moolenaar473de612013-06-08 18:19:48 +02001737 vim_regfree(lp->sl_compprog);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001738 lp->sl_compprog = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001739 VIM_CLEAR(lp->sl_comprules);
1740 VIM_CLEAR(lp->sl_compstartflags);
1741 VIM_CLEAR(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001742
Bram Moolenaard23a8232018-02-10 18:45:26 +01001743 VIM_CLEAR(lp->sl_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001744 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001745
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001746 ga_clear_strings(&lp->sl_comppat);
1747
Bram Moolenaar4770d092006-01-12 23:22:24 +00001748 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
1749 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00001750
Bram Moolenaar4770d092006-01-12 23:22:24 +00001751 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001752
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001753 // Clear info from .sug file.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001754 slang_clear_sug(lp);
1755
Bram Moolenaar5195e452005-08-19 20:32:47 +00001756 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001757 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001758 lp->sl_compsylmax = MAXWLEN;
1759 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001760}
1761
1762/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001763 * Clear the info from the .sug file in "lp".
1764 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001765 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001766slang_clear_sug(slang_T *lp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00001767{
Bram Moolenaard23a8232018-02-10 18:45:26 +01001768 VIM_CLEAR(lp->sl_sbyts);
1769 VIM_CLEAR(lp->sl_sidxs);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001770 close_spellbuf(lp->sl_sugbuf);
1771 lp->sl_sugbuf = NULL;
1772 lp->sl_sugloaded = FALSE;
1773 lp->sl_sugtime = 0;
1774}
1775
1776/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001777 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001778 * Invoked through do_in_runtimepath().
1779 */
1780 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001781spell_load_cb(char_u *fname, void *cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001782{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001783 spelload_T *slp = (spelload_T *)cookie;
1784 slang_T *slang;
1785
1786 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
1787 if (slang != NULL)
1788 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001789 // When a previously loaded file has NOBREAK also use it for the
1790 // ".add" files.
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001791 if (slp->sl_nobreak && slang->sl_add)
1792 slang->sl_nobreak = TRUE;
1793 else if (slang->sl_nobreak)
1794 slp->sl_nobreak = TRUE;
1795
1796 slp->sl_slang = slang;
1797 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00001798}
1799
Bram Moolenaar4770d092006-01-12 23:22:24 +00001800
1801/*
1802 * Add a word to the hashtable of common words.
1803 * If it's already there then the counter is increased.
1804 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001805 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001806count_common_word(
1807 slang_T *lp,
1808 char_u *word,
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001809 int len, // word length, -1 for up to NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001810 int count) // 1 to count once, 10 to init
Bram Moolenaar4770d092006-01-12 23:22:24 +00001811{
1812 hash_T hash;
1813 hashitem_T *hi;
1814 wordcount_T *wc;
1815 char_u buf[MAXWLEN];
1816 char_u *p;
1817
1818 if (len == -1)
1819 p = word;
Bram Moolenaar5bcc5a12019-08-06 22:48:02 +02001820 else if (len >= MAXWLEN)
1821 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001822 else
1823 {
1824 vim_strncpy(buf, word, len);
1825 p = buf;
1826 }
1827
1828 hash = hash_hash(p);
1829 hi = hash_lookup(&lp->sl_wordcount, p, hash);
1830 if (HASHITEM_EMPTY(hi))
1831 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001832 wc = alloc(sizeof(wordcount_T) + STRLEN(p));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001833 if (wc == NULL)
1834 return;
1835 STRCPY(wc->wc_word, p);
1836 wc->wc_count = count;
1837 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
1838 }
1839 else
1840 {
1841 wc = HI2WC(hi);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001842 if ((wc->wc_count += count) < (unsigned)count) // check for overflow
Bram Moolenaar4770d092006-01-12 23:22:24 +00001843 wc->wc_count = MAXWORDCOUNT;
1844 }
1845}
1846
1847/*
Bram Moolenaar95529562005-08-25 21:21:38 +00001848 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00001849 * Like strchr() but independent of locale.
1850 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001851 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001852byte_in_str(char_u *str, int n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001853{
1854 char_u *p;
1855
1856 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00001857 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001858 return TRUE;
1859 return FALSE;
1860}
1861
Bram Moolenaar5195e452005-08-19 20:32:47 +00001862#define SY_MAXLEN 30
1863typedef struct syl_item_S
1864{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001865 char_u sy_chars[SY_MAXLEN]; // the sequence of chars
Bram Moolenaar5195e452005-08-19 20:32:47 +00001866 int sy_len;
1867} syl_item_T;
1868
1869/*
1870 * Truncate "slang->sl_syllable" at the first slash and put the following items
1871 * in "slang->sl_syl_items".
1872 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001873 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001874init_syl_tab(slang_T *slang)
Bram Moolenaar5195e452005-08-19 20:32:47 +00001875{
1876 char_u *p;
1877 char_u *s;
1878 int l;
1879 syl_item_T *syl;
1880
1881 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
1882 p = vim_strchr(slang->sl_syllable, '/');
1883 while (p != NULL)
1884 {
1885 *p++ = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001886 if (*p == NUL) // trailing slash
Bram Moolenaar5195e452005-08-19 20:32:47 +00001887 break;
1888 s = p;
1889 p = vim_strchr(p, '/');
1890 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001891 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001892 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001893 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001894 if (l >= SY_MAXLEN)
1895 return SP_FORMERROR;
1896 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001897 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001898 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
1899 + slang->sl_syl_items.ga_len++;
1900 vim_strncpy(syl->sy_chars, s, l);
1901 syl->sy_len = l;
1902 }
1903 return OK;
1904}
1905
1906/*
1907 * Count the number of syllables in "word".
1908 * When "word" contains spaces the syllables after the last space are counted.
1909 * Returns zero if syllables are not defines.
1910 */
1911 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001912count_syllables(slang_T *slang, char_u *word)
Bram Moolenaar5195e452005-08-19 20:32:47 +00001913{
1914 int cnt = 0;
1915 int skip = FALSE;
1916 char_u *p;
1917 int len;
1918 int i;
1919 syl_item_T *syl;
1920 int c;
1921
1922 if (slang->sl_syllable == NULL)
1923 return 0;
1924
1925 for (p = word; *p != NUL; p += len)
1926 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001927 // When running into a space reset counter.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001928 if (*p == ' ')
1929 {
1930 len = 1;
1931 cnt = 0;
1932 continue;
1933 }
1934
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001935 // Find longest match of syllable items.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001936 len = 0;
1937 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
1938 {
1939 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
1940 if (syl->sy_len > len
1941 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
1942 len = syl->sy_len;
1943 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001944 if (len != 0) // found a match, count syllable
Bram Moolenaar5195e452005-08-19 20:32:47 +00001945 {
1946 ++cnt;
1947 skip = FALSE;
1948 }
1949 else
1950 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001951 // No recognized syllable item, at least a syllable char then?
Bram Moolenaar5195e452005-08-19 20:32:47 +00001952 c = mb_ptr2char(p);
1953 len = (*mb_ptr2len)(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001954 if (vim_strchr(slang->sl_syllable, c) == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001955 skip = FALSE; // No, search for next syllable
Bram Moolenaar5195e452005-08-19 20:32:47 +00001956 else if (!skip)
1957 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001958 ++cnt; // Yes, count it
1959 skip = TRUE; // don't count following syllable chars
Bram Moolenaar5195e452005-08-19 20:32:47 +00001960 }
1961 }
1962 }
1963 return cnt;
1964}
1965
1966/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02001967 * Parse 'spelllang' and set w_s->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001968 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001969 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001970 char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001971did_set_spelllang(win_T *wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001972{
1973 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001974 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001975 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00001976 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001977 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001978 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001979 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001980 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001981 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001982 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001983 int len;
1984 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00001985 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001986 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001987 char_u *use_region = NULL;
1988 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001989 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001990 int i, j;
1991 langp_T *lp, *lp2;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001992 static int recursive = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001993 char *ret_msg = NULL;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001994 char_u *spl_copy;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001995 bufref_T bufref;
1996
1997 set_bufref(&bufref, wp->w_buffer);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001998
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001999 // We don't want to do this recursively. May happen when a language is
2000 // not available and the SpellFileMissing autocommand opens a new buffer
2001 // in which 'spell' is set.
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002002 if (recursive)
2003 return NULL;
2004 recursive = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002005
2006 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002007 clear_midword(wp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002008
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002009 // Make a copy of 'spelllang', the SpellFileMissing autocommands may change
2010 // it under our fingers.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002011 spl_copy = vim_strsave(wp->w_s->b_p_spl);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002012 if (spl_copy == NULL)
2013 goto theend;
2014
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002015 wp->w_s->b_cjk = 0;
2016
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002017 // Loop over comma separated language names.
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002018 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002019 {
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002020 // Get one language name.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002021 copy_option_part(&splp, lang, MAXWLEN, ",");
Bram Moolenaar5482f332005-04-17 20:18:43 +00002022 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002023 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002024
Bram Moolenaarf154f3a2020-06-08 18:54:49 +02002025 if (!valid_spelllang(lang))
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002026 continue;
2027
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002028 if (STRCMP(lang, "cjk") == 0)
2029 {
2030 wp->w_s->b_cjk = 1;
2031 continue;
2032 }
2033
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002034 // If the name ends in ".spl" use it as the name of the spell file.
2035 // If there is a region name let "region" point to it and remove it
2036 // from the name.
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002037 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2038 {
2039 filename = TRUE;
2040
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002041 // Locate a region and remove it from the file name.
Bram Moolenaarb6356332005-07-18 21:40:44 +00002042 p = vim_strchr(gettail(lang), '_');
2043 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
2044 && !ASCII_ISALPHA(p[3]))
2045 {
2046 vim_strncpy(region_cp, p + 1, 2);
2047 mch_memmove(p, p + 3, len - (p - lang) - 2);
Bram Moolenaarb6356332005-07-18 21:40:44 +00002048 region = region_cp;
2049 }
2050 else
2051 dont_use_region = TRUE;
2052
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002053 // Check if we loaded this language before.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002054 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar99499b12019-05-23 21:35:48 +02002055 if (fullpathcmp(lang, slang->sl_fname, FALSE, TRUE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002056 break;
2057 }
2058 else
2059 {
2060 filename = FALSE;
2061 if (len > 3 && lang[len - 3] == '_')
2062 {
2063 region = lang + len - 2;
2064 len -= 3;
2065 lang[len] = NUL;
2066 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002067 else
2068 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002069
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002070 // Check if we loaded this language before.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002071 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002072 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002073 break;
2074 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002075
Bram Moolenaarb6356332005-07-18 21:40:44 +00002076 if (region != NULL)
2077 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002078 // If the region differs from what was used before then don't
2079 // use it for 'spellfile'.
Bram Moolenaarb6356332005-07-18 21:40:44 +00002080 if (use_region != NULL && STRCMP(region, use_region) != 0)
2081 dont_use_region = TRUE;
2082 use_region = region;
2083 }
2084
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002085 // If not found try loading the language now.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002086 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002087 {
2088 if (filename)
2089 (void)spell_load_file(lang, lang, NULL, FALSE);
2090 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002091 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002092 spell_load_lang(lang);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002093 // SpellFileMissing autocommands may do anything, including
2094 // destroying the buffer we are using...
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002095 if (!bufref_valid(&bufref))
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002096 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002097 ret_msg = N_("E797: SpellFileMissing autocommand deleted buffer");
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002098 goto theend;
2099 }
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002100 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002101 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002102
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002103 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002104 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002105 */
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002106 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar99499b12019-05-23 21:35:48 +02002107 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE, TRUE)
2108 == FPC_SAME
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002109 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002110 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002111 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002112 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002113 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002114 // find region in sl_regions
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002115 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002116 if (c == REGION_ALL)
2117 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002118 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002119 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002120 if (*slang->sl_regions != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002121 // This addition file is for other regions.
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002122 region_mask = 0;
2123 }
2124 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002125 // This is probably an error. Give a warning and
2126 // accept the words anyway.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002127 smsg(_("Warning: region %s not supported"),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002128 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002129 }
2130 else
2131 region_mask = 1 << c;
2132 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002133
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002134 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002135 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002136 if (ga_grow(&ga, 1) == FAIL)
2137 {
2138 ga_clear(&ga);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002139 ret_msg = e_outofmem;
2140 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002141 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002142 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002143 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2144 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002145 use_midword(slang, wp);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002146 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002147 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002148 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002149 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002150 }
2151
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002152 // round 0: load int_wordlist, if possible.
2153 // round 1: load first name in 'spellfile'.
2154 // round 2: load second name in 'spellfile.
2155 // etc.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002156 spf = curwin->w_s->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002157 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002158 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002159 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002160 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002161 // Internal wordlist, if there is one.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002162 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002163 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002164 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002165 }
2166 else
2167 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002168 // One entry in 'spellfile'.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002169 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
2170 STRCAT(spf_name, ".spl");
2171
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002172 // If it was already found above then skip it.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002173 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002174 {
2175 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
Bram Moolenaar99499b12019-05-23 21:35:48 +02002176 if (p != NULL && fullpathcmp(spf_name, p, FALSE, TRUE)
2177 == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002178 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002179 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002180 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002181 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002182 }
2183
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002184 // Check if it was loaded already.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002185 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar99499b12019-05-23 21:35:48 +02002186 if (fullpathcmp(spf_name, slang->sl_fname, FALSE, TRUE)
2187 == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002188 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002189 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002190 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002191 // Not loaded, try loading it now. The language name includes the
2192 // region name, the region is ignored otherwise. for int_wordlist
2193 // use an arbitrary name.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002194 if (round == 0)
2195 STRCPY(lang, "internal wordlist");
2196 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00002197 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002198 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002199 p = vim_strchr(lang, '.');
2200 if (p != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002201 *p = NUL; // truncate at ".encoding.add"
Bram Moolenaar7887d882005-07-01 22:33:52 +00002202 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002203 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002204
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002205 // If one of the languages has NOBREAK we assume the addition
2206 // files also have this.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002207 if (slang != NULL && nobreak)
2208 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002209 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002210 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002211 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002212 region_mask = REGION_ALL;
2213 if (use_region != NULL && !dont_use_region)
2214 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002215 // find region in sl_regions
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002216 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002217 if (c != REGION_ALL)
2218 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002219 else if (*slang->sl_regions != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002220 // This spell file is for other regions.
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002221 region_mask = 0;
2222 }
2223
2224 if (region_mask != 0)
2225 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002226 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
2227 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
2228 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002229 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2230 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002231 use_midword(slang, wp);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002232 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002233 }
2234 }
2235
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002236 // Everything is fine, store the new b_langp value.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002237 ga_clear(&wp->w_s->b_langp);
2238 wp->w_s->b_langp = ga;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002239
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002240 // For each language figure out what language to use for sound folding and
2241 // REP items. If the language doesn't support it itself use another one
2242 // with the same name. E.g. for "en-math" use "en".
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002243 for (i = 0; i < ga.ga_len; ++i)
2244 {
2245 lp = LANGP_ENTRY(ga, i);
2246
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002247 // sound folding
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002248 if (lp->lp_slang->sl_sal.ga_len > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002249 // language does sound folding itself
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002250 lp->lp_sallang = lp->lp_slang;
2251 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002252 // find first similar language that does sound folding
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002253 for (j = 0; j < ga.ga_len; ++j)
2254 {
2255 lp2 = LANGP_ENTRY(ga, j);
2256 if (lp2->lp_slang->sl_sal.ga_len > 0
2257 && STRNCMP(lp->lp_slang->sl_name,
2258 lp2->lp_slang->sl_name, 2) == 0)
2259 {
2260 lp->lp_sallang = lp2->lp_slang;
2261 break;
2262 }
2263 }
2264
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002265 // REP items
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002266 if (lp->lp_slang->sl_rep.ga_len > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002267 // language has REP items itself
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002268 lp->lp_replang = lp->lp_slang;
2269 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002270 // find first similar language that has REP items
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002271 for (j = 0; j < ga.ga_len; ++j)
2272 {
2273 lp2 = LANGP_ENTRY(ga, j);
2274 if (lp2->lp_slang->sl_rep.ga_len > 0
2275 && STRNCMP(lp->lp_slang->sl_name,
2276 lp2->lp_slang->sl_name, 2) == 0)
2277 {
2278 lp->lp_replang = lp2->lp_slang;
2279 break;
2280 }
2281 }
2282 }
Bram Moolenaard569a9e2020-09-28 23:13:15 +02002283 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002284
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002285theend:
2286 vim_free(spl_copy);
2287 recursive = FALSE;
2288 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002289}
2290
2291/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002292 * Clear the midword characters for buffer "buf".
2293 */
2294 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002295clear_midword(win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002296{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002297 CLEAR_FIELD(wp->w_s->b_spell_ismw);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002298 VIM_CLEAR(wp->w_s->b_spell_ismw_mb);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002299}
2300
2301/*
2302 * Use the "sl_midword" field of language "lp" for buffer "buf".
2303 * They add up to any currently used midword characters.
2304 */
2305 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002306use_midword(slang_T *lp, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002307{
2308 char_u *p;
2309
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002310 if (lp->sl_midword == NULL) // there aren't any
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002311 return;
2312
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002313 for (p = lp->sl_midword; *p != NUL; )
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002314 if (has_mbyte)
2315 {
2316 int c, l, n;
2317 char_u *bp;
2318
2319 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002320 l = (*mb_ptr2len)(p);
2321 if (c < 256 && l <= 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002322 wp->w_s->b_spell_ismw[c] = TRUE;
2323 else if (wp->w_s->b_spell_ismw_mb == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002324 // First multi-byte char in "b_spell_ismw_mb".
Bram Moolenaar860cae12010-06-05 23:22:07 +02002325 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002326 else
2327 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002328 // Append multi-byte chars to "b_spell_ismw_mb".
Bram Moolenaar860cae12010-06-05 23:22:07 +02002329 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
2330 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002331 if (bp != NULL)
2332 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002333 vim_free(wp->w_s->b_spell_ismw_mb);
2334 wp->w_s->b_spell_ismw_mb = bp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002335 vim_strncpy(bp + n, p, l);
2336 }
2337 }
2338 p += l;
2339 }
2340 else
Bram Moolenaar860cae12010-06-05 23:22:07 +02002341 wp->w_s->b_spell_ismw[*p++] = TRUE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002342}
2343
2344/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002345 * Find the region "region[2]" in "rp" (points to "sl_regions").
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01002346 * Each region is simply stored as the two characters of its name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002347 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002348 */
2349 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002350find_region(char_u *rp, char_u *region)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002351{
2352 int i;
2353
2354 for (i = 0; ; i += 2)
2355 {
2356 if (rp[i] == NUL)
2357 return REGION_ALL;
2358 if (rp[i] == region[0] && rp[i + 1] == region[1])
2359 break;
2360 }
2361 return i / 2;
2362}
2363
2364/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002365 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002366 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002367 * Word WF_ONECAP
2368 * W WORD WF_ALLCAP
2369 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002370 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002371 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002372captype(
2373 char_u *word,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002374 char_u *end) // When NULL use up to NUL byte.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002375{
2376 char_u *p;
2377 int c;
2378 int firstcap;
2379 int allcap;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002380 int past_second = FALSE; // past second word char
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002381
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002382 // find first letter
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002383 for (p = word; !spell_iswordp_nmw(p, curwin); MB_PTR_ADV(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002384 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002385 return 0; // only non-word characters, illegal word
Bram Moolenaarb765d632005-06-07 21:00:02 +00002386 if (has_mbyte)
2387 c = mb_ptr2char_adv(&p);
2388 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00002389 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002390 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002391
2392 /*
2393 * Need to check all letters to find a word with mixed upper/lower.
2394 * But a word with an upper char only at start is a ONECAP.
2395 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002396 for ( ; end == NULL ? *p != NUL : p < end; MB_PTR_ADV(p))
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002397 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002398 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002399 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002400 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002401 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002402 // UUl -> KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002403 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002404 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002405 allcap = FALSE;
2406 }
2407 else if (!allcap)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002408 // UlU -> KEEPCAP
Bram Moolenaar51485f02005-06-04 21:55:20 +00002409 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002410 past_second = TRUE;
2411 }
2412
2413 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002414 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002415 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002416 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002417 return 0;
2418}
2419
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002420/*
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002421 * Delete the internal wordlist and its .spl file.
2422 */
2423 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002424spell_delete_wordlist(void)
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002425{
2426 char_u fname[MAXPATHL];
2427
2428 if (int_wordlist != NULL)
2429 {
2430 mch_remove(int_wordlist);
2431 int_wordlist_spl(fname);
2432 mch_remove(fname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002433 VIM_CLEAR(int_wordlist);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002434 }
2435}
2436
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002437/*
2438 * Free all languages.
2439 */
2440 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002441spell_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002442{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002443 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002444 buf_T *buf;
2445
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002446 // Go through all buffers and handle 'spelllang'. <VN>
Bram Moolenaar29323592016-07-24 22:04:11 +02002447 FOR_ALL_BUFFERS(buf)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002448 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002449
2450 while (first_lang != NULL)
2451 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002452 slang = first_lang;
2453 first_lang = slang->sl_next;
2454 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002455 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002456
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002457 spell_delete_wordlist();
Bram Moolenaar7887d882005-07-01 22:33:52 +00002458
Bram Moolenaard23a8232018-02-10 18:45:26 +01002459 VIM_CLEAR(repl_to);
2460 VIM_CLEAR(repl_from);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002461}
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002462
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002463/*
2464 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002465 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002466 */
2467 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002468spell_reload(void)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002469{
Bram Moolenaar3982c542005-06-08 21:56:31 +00002470 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002471
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002472 // Initialize the table for spell_iswordp().
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002473 init_spell_chartab();
2474
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002475 // Unload all allocated memory.
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002476 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002477
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002478 // Go through all buffers and handle 'spelllang'.
Bram Moolenaar29323592016-07-24 22:04:11 +02002479 FOR_ALL_WINDOWS(wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002480 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002481 // Only load the wordlists when 'spelllang' is set and there is a
2482 // window for this buffer in which 'spell' is set.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002483 if (*wp->w_s->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002484 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002485 if (wp->w_p_spell)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002486 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002487 (void)did_set_spelllang(wp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00002488 break;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002489 }
2490 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002491 }
2492}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002493
Bram Moolenaarb765d632005-06-07 21:00:02 +00002494/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002495 * Open a spell buffer. This is a nameless buffer that is not in the buffer
2496 * list and only contains text lines. Can use a swapfile to reduce memory
2497 * use.
2498 * Most other fields are invalid! Esp. watch out for string options being
2499 * NULL and there is no undo info.
2500 * Returns NULL when out of memory.
2501 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002502 buf_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002503open_spellbuf(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002504{
2505 buf_T *buf;
2506
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002507 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002508 if (buf != NULL)
2509 {
2510 buf->b_spell = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002511 buf->b_p_swf = TRUE; // may create a swap file
Bram Moolenaar706d2de2013-07-17 17:35:13 +02002512#ifdef FEAT_CRYPT
2513 buf->b_p_key = empty_option;
2514#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00002515 ml_open(buf);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002516 ml_open_file(buf); // create swap file now
Bram Moolenaar4770d092006-01-12 23:22:24 +00002517 }
2518 return buf;
2519}
2520
2521/*
2522 * Close the buffer used for spell info.
2523 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002524 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002525close_spellbuf(buf_T *buf)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002526{
2527 if (buf != NULL)
2528 {
2529 ml_close(buf, TRUE);
2530 vim_free(buf);
2531 }
2532}
2533
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002534/*
2535 * Init the chartab used for spelling for ASCII.
2536 * EBCDIC is not supported!
2537 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002538 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002539clear_spell_chartab(spelltab_T *sp)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002540{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002541 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002542
Bram Moolenaara80faa82020-04-12 19:37:17 +02002543 // Init everything to FALSE (zero).
2544 CLEAR_FIELD(sp->st_isw);
2545 CLEAR_FIELD(sp->st_isu);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002546 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002547 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002548 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002549 sp->st_upper[i] = i;
2550 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002551
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002552 // We include digits. A word shouldn't start with a digit, but handling
2553 // that is done separately.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002554 for (i = '0'; i <= '9'; ++i)
2555 sp->st_isw[i] = TRUE;
2556 for (i = 'A'; i <= 'Z'; ++i)
2557 {
2558 sp->st_isw[i] = TRUE;
2559 sp->st_isu[i] = TRUE;
2560 sp->st_fold[i] = i + 0x20;
2561 }
2562 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002563 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002564 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002565 sp->st_upper[i] = i - 0x20;
2566 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002567}
2568
2569/*
2570 * Init the chartab used for spelling. Only depends on 'encoding'.
2571 * Called once while starting up and when 'encoding' changes.
2572 * The default is to use isalpha(), but the spell file should define the word
2573 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002574 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002575 */
2576 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002577init_spell_chartab(void)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002578{
2579 int i;
2580
2581 did_set_spelltab = FALSE;
2582 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002583 if (enc_dbcs)
2584 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002585 // DBCS: assume double-wide characters are word characters.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002586 for (i = 128; i <= 255; ++i)
2587 if (MB_BYTE2LEN(i) == 2)
2588 spelltab.st_isw[i] = TRUE;
2589 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002590 else if (enc_utf8)
2591 {
2592 for (i = 128; i < 256; ++i)
2593 {
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02002594 int f = utf_fold(i);
2595 int u = utf_toupper(i);
2596
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002597 spelltab.st_isu[i] = utf_isupper(i);
2598 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002599 // The folded/upper-cased value is different between latin1 and
2600 // utf8 for 0xb5, causing E763 for no good reason. Use the latin1
2601 // value for utf-8 to avoid this.
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02002602 spelltab.st_fold[i] = (f < 256) ? f : i;
2603 spelltab.st_upper[i] = (u < 256) ? u : i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002604 }
2605 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002606 else
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002607 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002608 // Rough guess: use locale-dependent library functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002609 for (i = 128; i < 256; ++i)
2610 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002611 if (MB_ISUPPER(i))
2612 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002613 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002614 spelltab.st_isu[i] = TRUE;
2615 spelltab.st_fold[i] = MB_TOLOWER(i);
2616 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002617 else if (MB_ISLOWER(i))
2618 {
2619 spelltab.st_isw[i] = TRUE;
2620 spelltab.st_upper[i] = MB_TOUPPER(i);
2621 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002622 }
2623 }
2624}
2625
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002626
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002627/*
Bram Moolenaarea408852005-06-25 22:49:46 +00002628 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002629 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00002630 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002631 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00002632 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02002633 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002634spell_iswordp(
2635 char_u *p,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002636 win_T *wp) // buffer used
Bram Moolenaarea408852005-06-25 22:49:46 +00002637{
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002638 char_u *s;
2639 int l;
2640 int c;
2641
2642 if (has_mbyte)
2643 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002644 l = mb_ptr2len(p);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002645 s = p;
2646 if (l == 1)
2647 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002648 // be quick for ASCII
Bram Moolenaar860cae12010-06-05 23:22:07 +02002649 if (wp->w_s->b_spell_ismw[*p])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002650 s = p + 1; // skip a mid-word character
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002651 }
2652 else
2653 {
2654 c = mb_ptr2char(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002655 if (c < 256 ? wp->w_s->b_spell_ismw[c]
2656 : (wp->w_s->b_spell_ismw_mb != NULL
2657 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002658 s = p + l;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002659 }
2660
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002661 c = mb_ptr2char(s);
2662 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002663 return spell_mb_isword_class(mb_get_class(s), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002664 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002665 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002666
Bram Moolenaar860cae12010-06-05 23:22:07 +02002667 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002668}
2669
2670/*
2671 * Return TRUE if "p" points to a word character.
2672 * Unlike spell_iswordp() this doesn't check for "midword" characters.
2673 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02002674 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002675spell_iswordp_nmw(char_u *p, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002676{
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002677 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002678
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002679 if (has_mbyte)
2680 {
2681 c = mb_ptr2char(p);
2682 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002683 return spell_mb_isword_class(mb_get_class(p), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002684 return spelltab.st_isw[c];
2685 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002686 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00002687}
2688
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002689/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002690 * Return TRUE if word class indicates a word character.
2691 * Only for characters above 255.
2692 * Unicode subscript and superscript are not considered word characters.
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002693 * See also dbcs_class() and utf_class() in mbyte.c.
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002694 */
2695 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002696spell_mb_isword_class(int cl, win_T *wp)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002697{
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002698 if (wp->w_s->b_cjk)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002699 // East Asian characters are not considered word characters.
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002700 return cl == 2 || cl == 0x2800;
Bram Moolenaar06e63772019-07-19 23:04:34 +02002701 return cl >= 2 && cl != 0x2070 && cl != 0x2080 && cl != 3;
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002702}
2703
2704/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002705 * Return TRUE if "p" points to a word character.
2706 * Wide version of spell_iswordp().
2707 */
2708 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002709spell_iswordp_w(int *p, win_T *wp)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002710{
2711 int *s;
2712
Bram Moolenaar860cae12010-06-05 23:22:07 +02002713 if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
2714 : (wp->w_s->b_spell_ismw_mb != NULL
2715 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002716 s = p + 1;
2717 else
2718 s = p;
2719
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002720 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002721 {
2722 if (enc_utf8)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002723 return spell_mb_isword_class(utf_class(*s), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002724 if (enc_dbcs)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002725 return spell_mb_isword_class(
2726 dbcs_class((unsigned)*s >> 8, *s & 0xff), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002727 return 0;
2728 }
2729 return spelltab.st_isw[*s];
2730}
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002731
Bram Moolenaarea408852005-06-25 22:49:46 +00002732/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002733 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
2734 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002735 * When using a multi-byte 'encoding' the length may change!
2736 * Returns FAIL when something wrong.
2737 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002738 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002739spell_casefold(
2740 char_u *str,
2741 int len,
2742 char_u *buf,
2743 int buflen)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002744{
2745 int i;
2746
2747 if (len >= buflen)
2748 {
2749 buf[0] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002750 return FAIL; // result will not fit
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002751 }
2752
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002753 if (has_mbyte)
2754 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002755 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002756 char_u *p;
2757 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002758
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002759 // Fold one character at a time.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002760 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002761 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002762 if (outi + MB_MAXBYTES > buflen)
2763 {
2764 buf[outi] = NUL;
2765 return FAIL;
2766 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002767 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002768 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002769 }
2770 buf[outi] = NUL;
2771 }
2772 else
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002773 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002774 // Be quick for non-multibyte encodings.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002775 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002776 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002777 buf[i] = NUL;
2778 }
2779
2780 return OK;
2781}
2782
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002783/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002784 * Check if the word at line "lnum" column "col" is required to start with a
2785 * capital. This uses 'spellcapcheck' of the current buffer.
2786 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02002787 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002788check_need_cap(linenr_T lnum, colnr_T col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002789{
2790 int need_cap = FALSE;
2791 char_u *line;
2792 char_u *line_copy = NULL;
2793 char_u *p;
2794 colnr_T endcol;
2795 regmatch_T regmatch;
2796
Bram Moolenaar860cae12010-06-05 23:22:07 +02002797 if (curwin->w_s->b_cap_prog == NULL)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002798 return FALSE;
2799
2800 line = ml_get_curline();
2801 endcol = 0;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002802 if (getwhitecols(line) >= (int)col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002803 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002804 // At start of line, check if previous line is empty or sentence
2805 // ends there.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002806 if (lnum == 1)
2807 need_cap = TRUE;
2808 else
2809 {
2810 line = ml_get(lnum - 1);
2811 if (*skipwhite(line) == NUL)
2812 need_cap = TRUE;
2813 else
2814 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002815 // Append a space in place of the line break.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002816 line_copy = concat_str(line, (char_u *)" ");
2817 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002818 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002819 }
2820 }
2821 }
2822 else
2823 endcol = col;
2824
2825 if (endcol > 0)
2826 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002827 // Check if sentence ends before the bad word.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002828 regmatch.regprog = curwin->w_s->b_cap_prog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002829 regmatch.rm_ic = FALSE;
2830 p = line + endcol;
2831 for (;;)
2832 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002833 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002834 if (p == line || spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002835 break;
2836 if (vim_regexec(&regmatch, p, 0)
2837 && regmatch.endp[0] == line + endcol)
2838 {
2839 need_cap = TRUE;
2840 break;
2841 }
2842 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002843 curwin->w_s->b_cap_prog = regmatch.regprog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002844 }
2845
2846 vim_free(line_copy);
2847
2848 return need_cap;
2849}
2850
2851
2852/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002853 * ":spellrepall"
2854 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002855 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002856ex_spellrepall(exarg_T *eap UNUSED)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002857{
2858 pos_T pos = curwin->w_cursor;
2859 char_u *frompat;
2860 int addlen;
2861 char_u *line;
2862 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002863 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002864 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002865
2866 if (repl_from == NULL || repl_to == NULL)
2867 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002868 emsg(_("E752: No previous spell replacement"));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002869 return;
2870 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002871 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002872
Bram Moolenaar964b3742019-05-24 18:54:09 +02002873 frompat = alloc(STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002874 if (frompat == NULL)
2875 return;
2876 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
2877 p_ws = FALSE;
2878
Bram Moolenaar5195e452005-08-19 20:32:47 +00002879 sub_nsubs = 0;
2880 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002881 curwin->w_cursor.lnum = 0;
2882 while (!got_int)
2883 {
Bram Moolenaarc036e872020-02-21 21:30:52 +01002884 if (do_search(NULL, '/', '/', frompat, 1L, SEARCH_KEEP, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002885 || u_save_cursor() == FAIL)
2886 break;
2887
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002888 // Only replace when the right word isn't there yet. This happens
2889 // when changing "etc" to "etc.".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002890 line = ml_get_curline();
2891 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
2892 repl_to, STRLEN(repl_to)) != 0)
2893 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002894 p = alloc(STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002895 if (p == NULL)
2896 break;
2897 mch_memmove(p, line, curwin->w_cursor.col);
2898 STRCPY(p + curwin->w_cursor.col, repl_to);
2899 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
2900 ml_replace(curwin->w_cursor.lnum, p, FALSE);
2901 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002902
2903 if (curwin->w_cursor.lnum != prev_lnum)
2904 {
2905 ++sub_nlines;
2906 prev_lnum = curwin->w_cursor.lnum;
2907 }
2908 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002909 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002910 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002911 }
2912
2913 p_ws = save_ws;
2914 curwin->w_cursor = pos;
2915 vim_free(frompat);
2916
Bram Moolenaar5195e452005-08-19 20:32:47 +00002917 if (sub_nsubs == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002918 semsg(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002919 else
2920 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002921}
2922
2923/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002924 * Make a copy of "word", with the first letter upper or lower cased, to
2925 * "wcopy[MAXWLEN]". "word" must not be empty.
2926 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002927 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002928 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002929onecap_copy(
2930 char_u *word,
2931 char_u *wcopy,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002932 int upper) // TRUE: first letter made upper case
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002933{
2934 char_u *p;
2935 int c;
2936 int l;
2937
2938 p = word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002939 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002940 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002941 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002942 c = *p++;
2943 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002944 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002945 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002946 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002947 if (has_mbyte)
2948 l = mb_char2bytes(c, wcopy);
2949 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002950 {
2951 l = 1;
2952 wcopy[0] = c;
2953 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002954 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002955}
2956
2957/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002958 * Make a copy of "word" with all the letters upper cased into
2959 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002960 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02002961 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002962allcap_copy(char_u *word, char_u *wcopy)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002963{
2964 char_u *s;
2965 char_u *d;
2966 int c;
2967
2968 d = wcopy;
2969 for (s = word; *s != NUL; )
2970 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002971 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002972 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002973 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002974 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +00002975
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002976 // We only change 0xdf to SS when we are certain latin1 is used. It
2977 // would cause weird errors in other 8-bit encodings.
Bram Moolenaar78622822005-08-23 21:00:13 +00002978 if (enc_latin1like && c == 0xdf)
2979 {
2980 c = 'S';
2981 if (d - wcopy >= MAXWLEN - 1)
2982 break;
2983 *d++ = c;
2984 }
2985 else
Bram Moolenaar78622822005-08-23 21:00:13 +00002986 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002987
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002988 if (has_mbyte)
2989 {
2990 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
2991 break;
2992 d += mb_char2bytes(c, d);
2993 }
2994 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002995 {
2996 if (d - wcopy >= MAXWLEN - 1)
2997 break;
2998 *d++ = c;
2999 }
3000 }
3001 *d = NUL;
3002}
3003
3004/*
Bram Moolenaar53805d12005-08-01 07:08:33 +00003005 * Case-folding may change the number of bytes: Count nr of chars in
3006 * fword[flen] and return the byte length of that many chars in "word".
3007 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02003008 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003009nofold_len(char_u *fword, int flen, char_u *word)
Bram Moolenaar53805d12005-08-01 07:08:33 +00003010{
3011 char_u *p;
3012 int i = 0;
3013
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003014 for (p = fword; p < fword + flen; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00003015 ++i;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003016 for (p = word; i > 0; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00003017 --i;
3018 return (int)(p - word);
3019}
Bram Moolenaar53805d12005-08-01 07:08:33 +00003020
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003021/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003022 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003023 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02003024 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003025make_case_word(char_u *fword, char_u *cword, int flags)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003026{
3027 if (flags & WF_ALLCAP)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003028 // Make it all upper-case
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003029 allcap_copy(fword, cword);
3030 else if (flags & WF_ONECAP)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003031 // Make the first letter upper-case
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003032 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003033 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003034 // Use goodword as-is.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003035 STRCPY(cword, fword);
3036}
3037
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003038#if defined(FEAT_EVAL) || defined(PROTO)
3039/*
3040 * Soundfold a string, for soundfold().
3041 * Result is in allocated memory, NULL for an error.
3042 */
3043 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003044eval_soundfold(char_u *word)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003045{
3046 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003047 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003048 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003049
Bram Moolenaar860cae12010-06-05 23:22:07 +02003050 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003051 // Use the sound-folding of the first language that supports it.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003052 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003053 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003054 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003055 if (lp->lp_slang->sl_sal.ga_len > 0)
3056 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003057 // soundfold the word
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003058 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003059 return vim_strsave(sound);
3060 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003061 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003062
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003063 // No language with sound folding, return word as-is.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003064 return vim_strsave(word);
3065}
3066#endif
3067
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003068/*
3069 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +00003070 *
3071 * There are many ways to turn a word into a sound-a-like representation. The
3072 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
3073 * swedish name matching - survey and test of different algorithms" by Klas
3074 * Erikson.
3075 *
3076 * We support two methods:
3077 * 1. SOFOFROM/SOFOTO do a simple character mapping.
3078 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003079 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003080 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003081spell_soundfold(
3082 slang_T *slang,
3083 char_u *inword,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003084 int folded, // "inword" is already case-folded
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003085 char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003086{
3087 char_u fword[MAXWLEN];
3088 char_u *word;
3089
3090 if (slang->sl_sofo)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003091 // SOFOFROM and SOFOTO used
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003092 spell_soundfold_sofo(slang, inword, res);
3093 else
3094 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003095 // SAL items used. Requires the word to be case-folded.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003096 if (folded)
3097 word = inword;
3098 else
3099 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003100 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003101 word = fword;
3102 }
3103
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003104 if (has_mbyte)
3105 spell_soundfold_wsal(slang, word, res);
3106 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003107 spell_soundfold_sal(slang, word, res);
3108 }
3109}
3110
3111/*
3112 * Perform sound folding of "inword" into "res" according to SOFOFROM and
3113 * SOFOTO lines.
3114 */
3115 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003116spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003117{
3118 char_u *s;
3119 int ri = 0;
3120 int c;
3121
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003122 if (has_mbyte)
3123 {
3124 int prevc = 0;
3125 int *ip;
3126
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003127 // The sl_sal_first[] table contains the translation for chars up to
3128 // 255, sl_sal the rest.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003129 for (s = inword; *s != NUL; )
3130 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003131 c = mb_cptr2char_adv(&s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003132 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003133 c = ' ';
3134 else if (c < 256)
3135 c = slang->sl_sal_first[c];
3136 else
3137 {
3138 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003139 if (ip == NULL) // empty list, can't match
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003140 c = NUL;
3141 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003142 for (;;) // find "c" in the list
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003143 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003144 if (*ip == 0) // not found
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003145 {
3146 c = NUL;
3147 break;
3148 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003149 if (*ip == c) // match!
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003150 {
3151 c = ip[1];
3152 break;
3153 }
3154 ip += 2;
3155 }
3156 }
3157
3158 if (c != NUL && c != prevc)
3159 {
3160 ri += mb_char2bytes(c, res + ri);
3161 if (ri + MB_MAXBYTES > MAXWLEN)
3162 break;
3163 prevc = c;
3164 }
3165 }
3166 }
3167 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003168 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003169 // The sl_sal_first[] table contains the translation.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003170 for (s = inword; (c = *s) != NUL; ++s)
3171 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003172 if (VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003173 c = ' ';
3174 else
3175 c = slang->sl_sal_first[c];
3176 if (c != NUL && (ri == 0 || res[ri - 1] != c))
3177 res[ri++] = c;
3178 }
3179 }
3180
3181 res[ri] = NUL;
3182}
3183
3184 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003185spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003186{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003187 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003188 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003189 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003190 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003191 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003192 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003193 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003194 int n, k = 0;
3195 int z0;
3196 int k0;
3197 int n0;
3198 int c;
3199 int pri;
3200 int p0 = -333;
3201 int c0;
3202
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003203 // Remove accents, if wanted. We actually remove all non-word characters.
3204 // But keep white space. We need a copy, the word may be changed here.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003205 if (slang->sl_rem_accents)
3206 {
3207 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003208 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003209 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003210 if (VIM_ISWHITE(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003211 {
3212 *t++ = ' ';
3213 s = skipwhite(s);
3214 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003215 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003216 {
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003217 if (spell_iswordp_nmw(s, curwin))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003218 *t++ = *s;
3219 ++s;
3220 }
3221 }
3222 *t = NUL;
3223 }
3224 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003225 vim_strncpy(word, s, MAXWLEN - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003226
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003227 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003228
3229 /*
3230 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003231 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003232 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003233 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003234 while ((c = word[i]) != NUL)
3235 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003236 // Start with the first rule that has the character in the word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003237 n = slang->sl_sal_first[c];
3238 z0 = 0;
3239
3240 if (n >= 0)
3241 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003242 // check all rules for the same letter
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003243 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003244 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003245 // Quickly skip entries that don't match the word. Most
3246 // entries are less then three chars, optimize for that.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003247 k = smp[n].sm_leadlen;
3248 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003249 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003250 if (word[i + 1] != s[1])
3251 continue;
3252 if (k > 2)
3253 {
3254 for (j = 2; j < k; ++j)
3255 if (word[i + j] != s[j])
3256 break;
3257 if (j < k)
3258 continue;
3259 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003260 }
3261
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003262 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003263 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003264 // Check for match with one of the chars in "sm_oneof".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003265 while (*pf != NUL && *pf != word[i + k])
3266 ++pf;
3267 if (*pf == NUL)
3268 continue;
3269 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003270 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003271 s = smp[n].sm_rules;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003272 pri = 5; // default priority
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003273
3274 p0 = *s;
3275 k0 = k;
3276 while (*s == '-' && k > 1)
3277 {
3278 k--;
3279 s++;
3280 }
3281 if (*s == '<')
3282 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003283 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003284 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003285 // determine priority
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003286 pri = *s - '0';
3287 s++;
3288 }
3289 if (*s == '^' && *(s + 1) == '^')
3290 s++;
3291
3292 if (*s == NUL
3293 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003294 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02003295 || spell_iswordp(word + i - 1, curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003296 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003297 || (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003298 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02003299 && spell_iswordp(word + i - 1, curwin)
3300 && (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003301 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003302 // search for followup rules, if:
3303 // followup and k > 1 and NO '-' in searchstring
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003304 c0 = word[i + k - 1];
3305 n0 = slang->sl_sal_first[c0];
3306
3307 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003308 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003309 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003310 // test follow-up rule for "word[i + k]"
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003311 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003312 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003313 // Quickly skip entries that don't match the word.
3314 //
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003315 k0 = smp[n0].sm_leadlen;
3316 if (k0 > 1)
3317 {
3318 if (word[i + k] != s[1])
3319 continue;
3320 if (k0 > 2)
3321 {
3322 pf = word + i + k + 1;
3323 for (j = 2; j < k0; ++j)
3324 if (*pf++ != s[j])
3325 break;
3326 if (j < k0)
3327 continue;
3328 }
3329 }
3330 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003331
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003332 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003333 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003334 // Check for match with one of the chars in
3335 // "sm_oneof".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003336 while (*pf != NUL && *pf != word[i + k0])
3337 ++pf;
3338 if (*pf == NUL)
3339 continue;
3340 ++k0;
3341 }
3342
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003343 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003344 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003345 while (*s == '-')
3346 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003347 // "k0" gets NOT reduced because
3348 // "if (k0 == k)"
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003349 s++;
3350 }
3351 if (*s == '<')
3352 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003353 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003354 {
3355 p0 = *s - '0';
3356 s++;
3357 }
3358
3359 if (*s == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003360 // *s == '^' cuts
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003361 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003362 && !spell_iswordp(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02003363 curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003364 {
3365 if (k0 == k)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003366 // this is just a piece of the string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003367 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003368
3369 if (p0 < pri)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003370 // priority too low
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003371 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003372 // rule fits; stop search
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003373 break;
3374 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003375 }
3376
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003377 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003378 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003379 }
3380
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003381 // replace string
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003382 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003383 if (s == NULL)
3384 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003385 pf = smp[n].sm_rules;
3386 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003387 if (p0 == 1 && z == 0)
3388 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003389 // rule with '<' is used
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003390 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
3391 || res[reslen - 1] == *s))
3392 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003393 z0 = 1;
3394 z = 1;
3395 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003396 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003397 {
3398 word[i + k0] = *s;
3399 k0++;
3400 s++;
3401 }
3402 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +00003403 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003404
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003405 // new "actual letter"
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003406 c = word[i];
3407 }
3408 else
3409 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003410 // no '<' rule used
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003411 i += k - 1;
3412 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003413 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003414 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003415 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003416 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003417 s++;
3418 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003419 // new "actual letter"
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003420 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003421 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003422 {
3423 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003424 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003425 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003426 i = 0;
3427 z0 = 1;
3428 }
3429 }
3430 break;
3431 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003432 }
3433 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01003434 else if (VIM_ISWHITE(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003435 {
3436 c = ' ';
3437 k = 1;
3438 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003439
3440 if (z0 == 0)
3441 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003442 if (k && !p0 && reslen < MAXWLEN && c != NUL
3443 && (!slang->sl_collapse || reslen == 0
3444 || res[reslen - 1] != c))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003445 // condense only double letters
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003446 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003447
3448 i++;
3449 z = 0;
3450 k = 0;
3451 }
3452 }
3453
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003454 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003455}
3456
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003457/*
3458 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
3459 * Multi-byte version of spell_soundfold().
3460 */
3461 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003462spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003463{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003464 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003465 int word[MAXWLEN];
3466 int wres[MAXWLEN];
3467 int l;
3468 char_u *s;
3469 int *ws;
3470 char_u *t;
3471 int *pf;
3472 int i, j, z;
3473 int reslen;
3474 int n, k = 0;
3475 int z0;
3476 int k0;
3477 int n0;
3478 int c;
3479 int pri;
3480 int p0 = -333;
3481 int c0;
3482 int did_white = FALSE;
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003483 int wordlen;
3484
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003485
3486 /*
3487 * Convert the multi-byte string to a wide-character string.
3488 * Remove accents, if wanted. We actually remove all non-word characters.
3489 * But keep white space.
3490 */
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003491 wordlen = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003492 for (s = inword; *s != NUL; )
3493 {
3494 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003495 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003496 if (slang->sl_rem_accents)
3497 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003498 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003499 {
3500 if (did_white)
3501 continue;
3502 c = ' ';
3503 did_white = TRUE;
3504 }
3505 else
3506 {
3507 did_white = FALSE;
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003508 if (!spell_iswordp_nmw(t, curwin))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003509 continue;
3510 }
3511 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003512 word[wordlen++] = c;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003513 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003514 word[wordlen] = NUL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003515
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003516 /*
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003517 * This algorithm comes from Aspell phonet.cpp.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003518 * Converted from C++ to C. Added support for multi-byte chars.
3519 * Changed to keep spaces.
3520 */
3521 i = reslen = z = 0;
3522 while ((c = word[i]) != NUL)
3523 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003524 // Start with the first rule that has the character in the word.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003525 n = slang->sl_sal_first[c & 0xff];
3526 z0 = 0;
3527
3528 if (n >= 0)
3529 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003530 // Check all rules for the same index byte.
3531 // If c is 0x300 need extra check for the end of the array, as
3532 // (c & 0xff) is NUL.
Bram Moolenaar95e85792010-08-01 15:37:02 +02003533 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff)
3534 && ws[0] != NUL; ++n)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003535 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003536 // Quickly skip entries that don't match the word. Most
3537 // entries are less then three chars, optimize for that.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003538 if (c != ws[0])
3539 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003540 k = smp[n].sm_leadlen;
3541 if (k > 1)
3542 {
3543 if (word[i + 1] != ws[1])
3544 continue;
3545 if (k > 2)
3546 {
3547 for (j = 2; j < k; ++j)
3548 if (word[i + j] != ws[j])
3549 break;
3550 if (j < k)
3551 continue;
3552 }
3553 }
3554
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003555 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003556 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003557 // Check for match with one of the chars in "sm_oneof".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003558 while (*pf != NUL && *pf != word[i + k])
3559 ++pf;
3560 if (*pf == NUL)
3561 continue;
3562 ++k;
3563 }
3564 s = smp[n].sm_rules;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003565 pri = 5; // default priority
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003566
3567 p0 = *s;
3568 k0 = k;
3569 while (*s == '-' && k > 1)
3570 {
3571 k--;
3572 s++;
3573 }
3574 if (*s == '<')
3575 s++;
3576 if (VIM_ISDIGIT(*s))
3577 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003578 // determine priority
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003579 pri = *s - '0';
3580 s++;
3581 }
3582 if (*s == '^' && *(s + 1) == '^')
3583 s++;
3584
3585 if (*s == NUL
3586 || (*s == '^'
3587 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02003588 || spell_iswordp_w(word + i - 1, curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003589 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003590 || (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003591 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02003592 && spell_iswordp_w(word + i - 1, curwin)
3593 && (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003594 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003595 // search for followup rules, if:
3596 // followup and k > 1 and NO '-' in searchstring
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003597 c0 = word[i + k - 1];
3598 n0 = slang->sl_sal_first[c0 & 0xff];
3599
3600 if (slang->sl_followup && k > 1 && n0 >= 0
3601 && p0 != '-' && word[i + k] != NUL)
3602 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003603 // Test follow-up rule for "word[i + k]"; loop over
3604 // all entries with the same index byte.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003605 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
3606 == (c0 & 0xff); ++n0)
3607 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003608 // Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003609 if (c0 != ws[0])
3610 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003611 k0 = smp[n0].sm_leadlen;
3612 if (k0 > 1)
3613 {
3614 if (word[i + k] != ws[1])
3615 continue;
3616 if (k0 > 2)
3617 {
3618 pf = word + i + k + 1;
3619 for (j = 2; j < k0; ++j)
3620 if (*pf++ != ws[j])
3621 break;
3622 if (j < k0)
3623 continue;
3624 }
3625 }
3626 k0 += k - 1;
3627
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003628 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003629 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003630 // Check for match with one of the chars in
3631 // "sm_oneof".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003632 while (*pf != NUL && *pf != word[i + k0])
3633 ++pf;
3634 if (*pf == NUL)
3635 continue;
3636 ++k0;
3637 }
3638
3639 p0 = 5;
3640 s = smp[n0].sm_rules;
3641 while (*s == '-')
3642 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003643 // "k0" gets NOT reduced because
3644 // "if (k0 == k)"
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003645 s++;
3646 }
3647 if (*s == '<')
3648 s++;
3649 if (VIM_ISDIGIT(*s))
3650 {
3651 p0 = *s - '0';
3652 s++;
3653 }
3654
3655 if (*s == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003656 // *s == '^' cuts
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003657 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003658 && !spell_iswordp_w(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02003659 curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003660 {
3661 if (k0 == k)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003662 // this is just a piece of the string
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003663 continue;
3664
3665 if (p0 < pri)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003666 // priority too low
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003667 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003668 // rule fits; stop search
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003669 break;
3670 }
3671 }
3672
3673 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
3674 == (c0 & 0xff))
3675 continue;
3676 }
3677
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003678 // replace string
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003679 ws = smp[n].sm_to_w;
3680 s = smp[n].sm_rules;
3681 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
3682 if (p0 == 1 && z == 0)
3683 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003684 // rule with '<' is used
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003685 if (reslen > 0 && ws != NULL && *ws != NUL
3686 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003687 || wres[reslen - 1] == *ws))
3688 reslen--;
3689 z0 = 1;
3690 z = 1;
3691 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003692 if (ws != NULL)
3693 while (*ws != NUL && word[i + k0] != NUL)
3694 {
3695 word[i + k0] = *ws;
3696 k0++;
3697 ws++;
3698 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003699 if (k > k0)
3700 mch_memmove(word + i + k0, word + i + k,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003701 sizeof(int) * (wordlen - (i + k) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003702
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003703 // new "actual letter"
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003704 c = word[i];
3705 }
3706 else
3707 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003708 // no '<' rule used
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003709 i += k - 1;
3710 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003711 if (ws != NULL)
3712 while (*ws != NUL && ws[1] != NUL
3713 && reslen < MAXWLEN)
3714 {
3715 if (reslen == 0 || wres[reslen - 1] != *ws)
3716 wres[reslen++] = *ws;
3717 ws++;
3718 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003719 // new "actual letter"
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003720 if (ws == NULL)
3721 c = NUL;
3722 else
3723 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003724 if (strstr((char *)s, "^^") != NULL)
3725 {
3726 if (c != NUL)
3727 wres[reslen++] = c;
3728 mch_memmove(word, word + i + 1,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003729 sizeof(int) * (wordlen - (i + 1) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003730 i = 0;
3731 z0 = 1;
3732 }
3733 }
3734 break;
3735 }
3736 }
3737 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01003738 else if (VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003739 {
3740 c = ' ';
3741 k = 1;
3742 }
3743
3744 if (z0 == 0)
3745 {
3746 if (k && !p0 && reslen < MAXWLEN && c != NUL
3747 && (!slang->sl_collapse || reslen == 0
3748 || wres[reslen - 1] != c))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003749 // condense only double letters
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003750 wres[reslen++] = c;
3751
3752 i++;
3753 z = 0;
3754 k = 0;
3755 }
3756 }
3757
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003758 // Convert wide characters in "wres" to a multi-byte string in "res".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003759 l = 0;
3760 for (n = 0; n < reslen; ++n)
3761 {
3762 l += mb_char2bytes(wres[n], res + l);
3763 if (l + MB_MAXBYTES > MAXWLEN)
3764 break;
3765 }
3766 res[l] = NUL;
3767}
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003768
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003769/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003770 * ":spellinfo"
3771 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003772 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003773ex_spellinfo(exarg_T *eap UNUSED)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003774{
3775 int lpi;
3776 langp_T *lp;
3777 char_u *p;
3778
3779 if (no_spell_checking(curwin))
3780 return;
3781
3782 msg_start();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003783 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003784 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003785 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003786 msg_puts("file: ");
3787 msg_puts((char *)lp->lp_slang->sl_fname);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003788 msg_putchar('\n');
3789 p = lp->lp_slang->sl_info;
3790 if (p != NULL)
3791 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003792 msg_puts((char *)p);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003793 msg_putchar('\n');
3794 }
3795 }
3796 msg_end();
3797}
3798
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003799#define DUMPFLAG_KEEPCASE 1 // round 2: keep-case tree
3800#define DUMPFLAG_COUNT 2 // include word count
3801#define DUMPFLAG_ICASE 4 // ignore case when finding matches
3802#define DUMPFLAG_ONECAP 8 // pattern starts with capital
3803#define DUMPFLAG_ALLCAP 16 // pattern is all capitals
Bram Moolenaar4770d092006-01-12 23:22:24 +00003804
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003805/*
3806 * ":spelldump"
3807 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003808 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003809ex_spelldump(exarg_T *eap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003810{
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003811 char_u *spl;
3812 long dummy;
3813
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003814 if (no_spell_checking(curwin))
3815 return;
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003816 get_option_value((char_u*)"spl", &dummy, &spl, OPT_LOCAL);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003817
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003818 // Create a new empty buffer in a new window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003819 do_cmdline_cmd((char_u *)"new");
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003820
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003821 // enable spelling locally in the new window
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003822 set_option_value((char_u*)"spell", TRUE, (char_u*)"", OPT_LOCAL);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003823 set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL);
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003824 vim_free(spl);
3825
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003826 if (!BUFEMPTY())
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003827 return;
3828
Bram Moolenaar860cae12010-06-05 23:22:07 +02003829 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003830
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003831 // Delete the empty line that we started with.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003832 if (curbuf->b_ml.ml_line_count > 1)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003833 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003834
3835 redraw_later(NOT_VALID);
3836}
3837
3838/*
3839 * Go through all possible words and:
3840 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
3841 * "ic" and "dir" are not used.
3842 * 2. When "pat" is not NULL: add matching words to insert mode completion.
3843 */
3844 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003845spell_dump_compl(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003846 char_u *pat, // leading part of the word
3847 int ic, // ignore case
3848 int *dir, // direction for adding matches
3849 int dumpflags_arg) // DUMPFLAG_*
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003850{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003851 langp_T *lp;
3852 slang_T *slang;
3853 idx_T arridx[MAXWLEN];
3854 int curi[MAXWLEN];
3855 char_u word[MAXWLEN];
3856 int c;
3857 char_u *byts;
3858 idx_T *idxs;
3859 linenr_T lnum = 0;
3860 int round;
3861 int depth;
3862 int n;
3863 int flags;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003864 char_u *region_names = NULL; // region names being used
3865 int do_region = TRUE; // dump region names and numbers
Bram Moolenaar7887d882005-07-01 22:33:52 +00003866 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003867 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003868 int dumpflags = dumpflags_arg;
3869 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003870
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003871 // When ignoring case or when the pattern starts with capital pass this on
3872 // to dump_word().
Bram Moolenaard0131a82006-03-04 21:46:13 +00003873 if (pat != NULL)
3874 {
3875 if (ic)
3876 dumpflags |= DUMPFLAG_ICASE;
3877 else
3878 {
3879 n = captype(pat, NULL);
3880 if (n == WF_ONECAP)
3881 dumpflags |= DUMPFLAG_ONECAP;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01003882 else if (n == WF_ALLCAP && (int)STRLEN(pat) > mb_ptr2len(pat))
Bram Moolenaard0131a82006-03-04 21:46:13 +00003883 dumpflags |= DUMPFLAG_ALLCAP;
3884 }
3885 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003886
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003887 // Find out if we can support regions: All languages must support the same
3888 // regions or none at all.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003889 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003890 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003891 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003892 p = lp->lp_slang->sl_regions;
3893 if (p[0] != 0)
3894 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003895 if (region_names == NULL) // first language with regions
Bram Moolenaar7887d882005-07-01 22:33:52 +00003896 region_names = p;
3897 else if (STRCMP(region_names, p) != 0)
3898 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003899 do_region = FALSE; // region names are different
Bram Moolenaar7887d882005-07-01 22:33:52 +00003900 break;
3901 }
3902 }
3903 }
3904
3905 if (do_region && region_names != NULL)
3906 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003907 if (pat == NULL)
3908 {
3909 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
3910 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
3911 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00003912 }
3913 else
3914 do_region = FALSE;
3915
3916 /*
3917 * Loop over all files loaded for the entries in 'spelllang'.
3918 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003919 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003920 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003921 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003922 slang = lp->lp_slang;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003923 if (slang->sl_fbyts == NULL) // reloading failed
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003924 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003925
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003926 if (pat == NULL)
3927 {
3928 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
3929 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
3930 }
3931
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003932 // When matching with a pattern and there are no prefixes only use
3933 // parts of the tree that match "pat".
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003934 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003935 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003936 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003937 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003938
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003939 // round 1: case-folded tree
3940 // round 2: keep-case tree
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003941 for (round = 1; round <= 2; ++round)
3942 {
3943 if (round == 1)
3944 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003945 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003946 byts = slang->sl_fbyts;
3947 idxs = slang->sl_fidxs;
3948 }
3949 else
3950 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003951 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003952 byts = slang->sl_kbyts;
3953 idxs = slang->sl_kidxs;
3954 }
3955 if (byts == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003956 continue; // array is empty
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003957
3958 depth = 0;
3959 arridx[0] = 0;
3960 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003961 while (depth >= 0 && !got_int
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003962 && (pat == NULL || !ins_compl_interrupted()))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003963 {
3964 if (curi[depth] > byts[arridx[depth]])
3965 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003966 // Done all bytes at this node, go up one level.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003967 --depth;
3968 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003969 ins_compl_check_keys(50, FALSE);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003970 }
3971 else
3972 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003973 // Do one more byte at this node.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003974 n = arridx[depth] + curi[depth];
3975 ++curi[depth];
3976 c = byts[n];
3977 if (c == 0)
3978 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003979 // End of word, deal with the word.
3980 // Don't use keep-case words in the fold-case tree,
3981 // they will appear in the keep-case tree.
3982 // Only use the word when the region matches.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003983 flags = (int)idxs[n];
3984 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003985 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +00003986 && (do_region
3987 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003988 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003989 & lp->lp_region) != 0))
3990 {
3991 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003992 if (!do_region)
3993 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003994
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003995 // Dump the basic word if there is no prefix or
3996 // when it's the first one.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003997 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003998 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003999 {
4000 dump_word(slang, word, pat, dir,
4001 dumpflags, flags, lnum);
4002 if (pat == NULL)
4003 ++lnum;
4004 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004005
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004006 // Apply the prefix, if there is one.
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004007 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004008 lnum = dump_prefixes(slang, word, pat, dir,
4009 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004010 }
4011 }
4012 else
4013 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004014 // Normal char, go one level deeper.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004015 word[depth++] = c;
4016 arridx[depth] = idxs[n];
4017 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004018
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004019 // Check if this characters matches with the pattern.
4020 // If not skip the whole tree below it.
4021 // Always ignore case here, dump_word() will check
4022 // proper case later. This isn't exactly right when
4023 // length changes for multi-byte characters with
4024 // ignore case...
Bram Moolenaard0131a82006-03-04 21:46:13 +00004025 if (depth <= patlen
4026 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004027 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004028 }
4029 }
4030 }
4031 }
4032 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004033}
4034
4035/*
4036 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004037 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004038 */
4039 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004040dump_word(
4041 slang_T *slang,
4042 char_u *word,
4043 char_u *pat,
4044 int *dir,
4045 int dumpflags,
4046 int wordflags,
4047 linenr_T lnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004048{
4049 int keepcap = FALSE;
4050 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004051 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004052 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +00004053 char_u badword[MAXWLEN + 10];
4054 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +00004055 int flags = wordflags;
4056
4057 if (dumpflags & DUMPFLAG_ONECAP)
4058 flags |= WF_ONECAP;
4059 if (dumpflags & DUMPFLAG_ALLCAP)
4060 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004061
Bram Moolenaar4770d092006-01-12 23:22:24 +00004062 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004063 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004064 // Need to fix case according to "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004065 make_case_word(word, cword, flags);
4066 p = cword;
4067 }
4068 else
4069 {
4070 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004071 if ((dumpflags & DUMPFLAG_KEEPCASE)
4072 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004073 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004074 keepcap = TRUE;
4075 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004076 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004077
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004078 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004079 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004080 // Add flags and regions after a slash.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004081 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004082 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004083 STRCPY(badword, p);
4084 STRCAT(badword, "/");
4085 if (keepcap)
4086 STRCAT(badword, "=");
4087 if (flags & WF_BANNED)
4088 STRCAT(badword, "!");
4089 else if (flags & WF_RARE)
4090 STRCAT(badword, "?");
4091 if (flags & WF_REGION)
4092 for (i = 0; i < 7; ++i)
4093 if (flags & (0x10000 << i))
4094 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
4095 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004096 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004097
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004098 if (dumpflags & DUMPFLAG_COUNT)
4099 {
4100 hashitem_T *hi;
4101
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004102 // Include the word count for ":spelldump!".
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004103 hi = hash_find(&slang->sl_wordcount, tw);
4104 if (!HASHITEM_EMPTY(hi))
4105 {
4106 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
4107 tw, HI2WC(hi)->wc_count);
4108 p = IObuff;
4109 }
4110 }
4111
4112 ml_append(lnum, p, (colnr_T)0, FALSE);
4113 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00004114 else if (((dumpflags & DUMPFLAG_ICASE)
4115 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
4116 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004117 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004118 p_ic, NULL, *dir, FALSE) == OK)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004119 // if dir was BACKWARD then honor it just once
Bram Moolenaard0131a82006-03-04 21:46:13 +00004120 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004121}
4122
4123/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004124 * For ":spelldump": Find matching prefixes for "word". Prepend each to
4125 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004126 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004127 * Return the updated line number.
4128 */
4129 static linenr_T
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004130dump_prefixes(
4131 slang_T *slang,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004132 char_u *word, // case-folded word
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004133 char_u *pat,
4134 int *dir,
4135 int dumpflags,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004136 int flags, // flags with prefix ID
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004137 linenr_T startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004138{
4139 idx_T arridx[MAXWLEN];
4140 int curi[MAXWLEN];
4141 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +00004142 char_u word_up[MAXWLEN];
4143 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004144 int c;
4145 char_u *byts;
4146 idx_T *idxs;
4147 linenr_T lnum = startlnum;
4148 int depth;
4149 int n;
4150 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004151 int i;
4152
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004153 // If the word starts with a lower-case letter make the word with an
4154 // upper-case letter in word_up[].
Bram Moolenaar53805d12005-08-01 07:08:33 +00004155 c = PTR2CHAR(word);
4156 if (SPELL_TOUPPER(c) != c)
4157 {
4158 onecap_copy(word, word_up, TRUE);
4159 has_word_up = TRUE;
4160 }
4161
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004162 byts = slang->sl_pbyts;
4163 idxs = slang->sl_pidxs;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004164 if (byts != NULL) // array not is empty
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004165 {
4166 /*
4167 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004168 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004169 */
4170 depth = 0;
4171 arridx[0] = 0;
4172 curi[0] = 1;
4173 while (depth >= 0 && !got_int)
4174 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004175 n = arridx[depth];
4176 len = byts[n];
4177 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004178 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004179 // Done all bytes at this node, go up one level.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004180 --depth;
4181 line_breakcheck();
4182 }
4183 else
4184 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004185 // Do one more byte at this node.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004186 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004187 ++curi[depth];
4188 c = byts[n];
4189 if (c == 0)
4190 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004191 // End of prefix, find out how many IDs there are.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004192 for (i = 1; i < len; ++i)
4193 if (byts[n + i] != 0)
4194 break;
4195 curi[depth] += i - 1;
4196
Bram Moolenaar53805d12005-08-01 07:08:33 +00004197 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
4198 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004199 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004200 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004201 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004202 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004203 : flags, lnum);
4204 if (lnum != 0)
4205 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004206 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00004207
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004208 // Check for prefix that matches the word when the
4209 // first letter is upper-case, but only if the prefix has
4210 // a condition.
Bram Moolenaar53805d12005-08-01 07:08:33 +00004211 if (has_word_up)
4212 {
4213 c = valid_word_prefix(i, n, flags, word_up, slang,
4214 TRUE);
4215 if (c != 0)
4216 {
4217 vim_strncpy(prefix + depth, word_up,
4218 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004219 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004220 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004221 : flags, lnum);
4222 if (lnum != 0)
4223 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004224 }
4225 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004226 }
4227 else
4228 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004229 // Normal char, go one level deeper.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004230 prefix[depth++] = c;
4231 arridx[depth] = idxs[n];
4232 curi[depth] = 1;
4233 }
4234 }
4235 }
4236 }
4237
4238 return lnum;
4239}
4240
Bram Moolenaar95529562005-08-25 21:21:38 +00004241/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004242 * Move "p" to the end of word "start".
4243 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +00004244 */
4245 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004246spell_to_word_end(char_u *start, win_T *win)
Bram Moolenaar95529562005-08-25 21:21:38 +00004247{
4248 char_u *p = start;
4249
Bram Moolenaar860cae12010-06-05 23:22:07 +02004250 while (*p != NUL && spell_iswordp(p, win))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004251 MB_PTR_ADV(p);
Bram Moolenaar95529562005-08-25 21:21:38 +00004252 return p;
4253}
4254
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004255/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004256 * For Insert mode completion CTRL-X s:
4257 * Find start of the word in front of column "startcol".
4258 * We don't check if it is badly spelled, with completion we can only change
4259 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004260 * Returns the column number of the word.
4261 */
4262 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004263spell_word_start(int startcol)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004264{
4265 char_u *line;
4266 char_u *p;
4267 int col = 0;
4268
Bram Moolenaar95529562005-08-25 21:21:38 +00004269 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004270 return startcol;
4271
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004272 // Find a word character before "startcol".
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004273 line = ml_get_curline();
4274 for (p = line + startcol; p > line; )
4275 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004276 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004277 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004278 break;
4279 }
4280
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004281 // Go back to start of the word.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004282 while (p > line)
4283 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004284 col = (int)(p - line);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004285 MB_PTR_BACK(line, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004286 if (!spell_iswordp(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004287 break;
4288 col = 0;
4289 }
4290
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004291 return col;
4292}
4293
4294/*
Bram Moolenaar4effc802005-09-30 21:12:02 +00004295 * Need to check for 'spellcapcheck' now, the word is removed before
4296 * expand_spelling() is called. Therefore the ugly global variable.
4297 */
4298static int spell_expand_need_cap;
4299
4300 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004301spell_expand_check_cap(colnr_T col)
Bram Moolenaar4effc802005-09-30 21:12:02 +00004302{
4303 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
4304}
4305
4306/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004307 * Get list of spelling suggestions.
4308 * Used for Insert mode completion CTRL-X ?.
4309 * Returns the number of matches. The matches are in "matchp[]", array of
4310 * allocated strings.
4311 */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004312 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004313expand_spelling(
4314 linenr_T lnum UNUSED,
4315 char_u *pat,
4316 char_u ***matchp)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004317{
4318 garray_T ga;
4319
Bram Moolenaar4770d092006-01-12 23:22:24 +00004320 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004321 *matchp = ga.ga_data;
4322 return ga.ga_len;
4323}
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004324
Bram Moolenaare677df82019-09-02 22:31:11 +02004325/*
Bram Moolenaarf154f3a2020-06-08 18:54:49 +02004326 * Return TRUE if "val" is a valid 'spelllang' value.
Bram Moolenaare677df82019-09-02 22:31:11 +02004327 */
4328 int
Bram Moolenaarf154f3a2020-06-08 18:54:49 +02004329valid_spelllang(char_u *val)
Bram Moolenaare677df82019-09-02 22:31:11 +02004330{
4331 return valid_name(val, ".-_,@");
4332}
4333
4334/*
4335 * Return TRUE if "val" is a valid 'spellfile' value.
4336 */
4337 int
4338valid_spellfile(char_u *val)
4339{
4340 char_u *s;
4341
4342 for (s = val; *s != NUL; ++s)
Bram Moolenaarb2620202020-10-30 19:25:09 +01004343 if (!vim_isfilec(*s) && *s != ',' && *s != ' ')
Bram Moolenaare677df82019-09-02 22:31:11 +02004344 return FALSE;
4345 return TRUE;
4346}
4347
4348/*
4349 * Handle side effects of setting 'spell'.
4350 * Return an error message or NULL for success.
4351 */
4352 char *
4353did_set_spell_option(int is_spellfile)
4354{
4355 char *errmsg = NULL;
4356 win_T *wp;
4357 int l;
4358
4359 if (is_spellfile)
4360 {
4361 l = (int)STRLEN(curwin->w_s->b_p_spf);
4362 if (l > 0 && (l < 4
4363 || STRCMP(curwin->w_s->b_p_spf + l - 4, ".add") != 0))
4364 errmsg = e_invarg;
4365 }
4366
4367 if (errmsg == NULL)
4368 {
4369 FOR_ALL_WINDOWS(wp)
4370 if (wp->w_buffer == curbuf && wp->w_p_spell)
4371 {
4372 errmsg = did_set_spelllang(wp);
4373 break;
4374 }
4375 }
4376 return errmsg;
4377}
4378
4379/*
4380 * Set curbuf->b_cap_prog to the regexp program for 'spellcapcheck'.
4381 * Return error message when failed, NULL when OK.
4382 */
4383 char *
4384compile_cap_prog(synblock_T *synblock)
4385{
4386 regprog_T *rp = synblock->b_cap_prog;
4387 char_u *re;
4388
Bram Moolenaar53efb182019-10-13 19:49:26 +02004389 if (synblock->b_p_spc == NULL || *synblock->b_p_spc == NUL)
Bram Moolenaare677df82019-09-02 22:31:11 +02004390 synblock->b_cap_prog = NULL;
4391 else
4392 {
4393 // Prepend a ^ so that we only match at one column
4394 re = concat_str((char_u *)"^", synblock->b_p_spc);
4395 if (re != NULL)
4396 {
4397 synblock->b_cap_prog = vim_regcomp(re, RE_MAGIC);
4398 vim_free(re);
4399 if (synblock->b_cap_prog == NULL)
4400 {
4401 synblock->b_cap_prog = rp; // restore the previous program
4402 return e_invarg;
4403 }
4404 }
4405 }
4406
4407 vim_regfree(rp);
4408 return NULL;
4409}
4410
4411#endif // FEAT_SPELL