blob: 1f4c60ed82218b17e5d3bad9bd0adbeece1c656d [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.
kylo252ae6f1d82022-02-16 19:24:07 +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
dundargocc57b5bc2022-11-02 13:30:51 +0000114 int mi_result2; // "mi_result" without following word
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100115 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
Bram Moolenaar4f135272021-06-11 19:07:40 +0200252 (void)spell_casefold(wp, ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000253 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 Moolenaar875339b2022-05-20 14:10:50 +0100256 if (camel_case && mi.mi_fwordlen > 0)
Bram Moolenaare0ebeda2020-06-10 22:17:58 +0200257 // 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 Moolenaar677658a2022-01-05 16:09:06 +0000481 emsg(_(e_format_error_in_spell_file));
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)
Bram Moolenaar4f135272021-06-11 19:07:40 +0200739 (void)spell_casefold(mip->mi_win,
740 ptr, wlen, fword, MAXWLEN);
Bram Moolenaar5195e452005-08-19 20:32:47 +0000741 else
742 vim_strncpy(fword, ptr, endlen[endidxcnt]);
743 }
744 if (!can_compound(slang, fword, mip->mi_compflags))
745 continue;
746 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000747 else if (slang->sl_comprules != NULL
748 && !match_compoundrule(slang, mip->mi_compflags))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100749 // The compound flags collected so far do not match any
750 // COMPOUNDRULE, discard the compounded word.
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000751 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000752 }
753
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100754 // Check NEEDCOMPOUND: can't use word without compounding.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000755 else if (flags & WF_NEEDCOMP)
756 continue;
757
Bram Moolenaar78622822005-08-23 21:00:13 +0000758 nobreak_result = SP_OK;
759
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000760 if (!word_ends)
761 {
Bram Moolenaar78622822005-08-23 21:00:13 +0000762 int save_result = mip->mi_result;
763 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000764 langp_T *save_lp = mip->mi_lp;
765 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +0000766
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100767 // Check that a valid word follows. If there is one and we
768 // are compounding, it will set "mi_result", thus we are
769 // always finished here. For NOBREAK we only check that a
770 // valid word follows.
771 // Recursive!
Bram Moolenaar78622822005-08-23 21:00:13 +0000772 if (slang->sl_nobreak)
773 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000774
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100775 // Find following word in case-folded tree.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000776 mip->mi_compoff = endlen[endidxcnt];
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000777 if (has_mbyte && mode == FIND_KEEPWORD)
778 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100779 // Compute byte length in case-folded word from "wlen":
780 // byte length in keep-case word. Length may change when
781 // folding case. This can be slow, take a shortcut when
782 // the case-folded word is equal to the keep-case word.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000783 p = mip->mi_fword;
784 if (STRNCMP(ptr, p, wlen) != 0)
785 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100786 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s))
787 MB_PTR_ADV(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000788 mip->mi_compoff = (int)(p - mip->mi_fword);
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000789 }
790 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100791#if 0 // Disabled, see below
Bram Moolenaard12a1322005-08-21 22:08:24 +0000792 c = mip->mi_compoff;
Bram Moolenaarba534352016-04-21 09:20:26 +0200793#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000794 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000795 if (flags & WF_COMPROOT)
796 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000797
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100798 // For NOBREAK we need to try all NOBREAK languages, at least
799 // to find the ".add" file(s).
Bram Moolenaar860cae12010-06-05 23:22:07 +0200800 for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +0000801 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000802 if (slang->sl_nobreak)
803 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200804 mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi);
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000805 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
806 || !mip->mi_lp->lp_slang->sl_nobreak)
807 continue;
808 }
Bram Moolenaard12a1322005-08-21 22:08:24 +0000809
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000810 find_word(mip, FIND_COMPOUND);
811
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100812 // When NOBREAK any word that matches is OK. Otherwise we
813 // need to find the longest match, thus try with keep-case
814 // and prefix too.
Bram Moolenaar78622822005-08-23 21:00:13 +0000815 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
816 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100817 // Find following word in keep-case tree.
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000818 mip->mi_compoff = wlen;
819 find_word(mip, FIND_KEEPCOMPOUND);
820
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100821#if 0 // Disabled, a prefix must not appear halfway a compound word,
822 // unless the COMPOUNDPERMITFLAG is used and then it can't be a
823 // postponed prefix.
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000824 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
825 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100826 // Check for following word with prefix.
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000827 mip->mi_compoff = c;
828 find_prefix(mip, FIND_COMPOUND);
829 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000830#endif
Bram Moolenaar78622822005-08-23 21:00:13 +0000831 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000832
833 if (!slang->sl_nobreak)
834 break;
Bram Moolenaar78622822005-08-23 21:00:13 +0000835 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000836 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000837 if (flags & WF_COMPROOT)
838 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000839 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000840
Bram Moolenaar78622822005-08-23 21:00:13 +0000841 if (slang->sl_nobreak)
842 {
843 nobreak_result = mip->mi_result;
844 mip->mi_result = save_result;
845 mip->mi_end = save_end;
846 }
847 else
848 {
849 if (mip->mi_result == SP_OK)
850 break;
851 continue;
852 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000853 }
854
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000855 if (flags & WF_BANNED)
856 res = SP_BANNED;
857 else if (flags & WF_REGION)
858 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100859 // Check region.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000860 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000861 res = SP_OK;
862 else
863 res = SP_LOCAL;
864 }
865 else if (flags & WF_RARE)
866 res = SP_RARE;
867 else
868 res = SP_OK;
869
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100870 // Always use the longest match and the best result. For NOBREAK
871 // we separately keep the longest match without a following good
872 // word as a fall-back.
Bram Moolenaar78622822005-08-23 21:00:13 +0000873 if (nobreak_result == SP_BAD)
874 {
875 if (mip->mi_result2 > res)
876 {
877 mip->mi_result2 = res;
878 mip->mi_end2 = mip->mi_word + wlen;
879 }
880 else if (mip->mi_result2 == res
881 && mip->mi_end2 < mip->mi_word + wlen)
882 mip->mi_end2 = mip->mi_word + wlen;
883 }
884 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000885 {
886 mip->mi_result = res;
887 mip->mi_end = mip->mi_word + wlen;
888 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000889 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000890 mip->mi_end = mip->mi_word + wlen;
891
Bram Moolenaar78622822005-08-23 21:00:13 +0000892 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000893 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000894 }
895
Bram Moolenaar78622822005-08-23 21:00:13 +0000896 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000897 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000898 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000899}
900
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000901/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000902 * Return TRUE if there is a match between the word ptr[wlen] and
903 * CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another
904 * word.
905 * A match means that the first part of CHECKCOMPOUNDPATTERN matches at the
906 * end of ptr[wlen] and the second part matches after it.
907 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +0200908 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100909match_checkcompoundpattern(
910 char_u *ptr,
911 int wlen,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100912 garray_T *gap) // &sl_comppat
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000913{
914 int i;
915 char_u *p;
916 int len;
917
918 for (i = 0; i + 1 < gap->ga_len; i += 2)
919 {
920 p = ((char_u **)gap->ga_data)[i + 1];
921 if (STRNCMP(ptr + wlen, p, STRLEN(p)) == 0)
922 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100923 // Second part matches at start of following compound word, now
924 // check if first part matches at end of previous word.
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000925 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar19c9c762008-12-09 21:34:39 +0000926 len = (int)STRLEN(p);
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000927 if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0)
928 return TRUE;
929 }
930 }
931 return FALSE;
932}
933
934/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000935 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
936 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000937 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +0200938 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100939can_compound(slang_T *slang, char_u *word, char_u *flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000940{
Bram Moolenaar6de68532005-08-24 22:08:48 +0000941 char_u uflags[MAXWLEN * 2];
942 int i;
Bram Moolenaar6de68532005-08-24 22:08:48 +0000943 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000944
945 if (slang->sl_compprog == NULL)
946 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +0000947 if (enc_utf8)
948 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100949 // Need to convert the single byte flags to utf8 characters.
Bram Moolenaar6de68532005-08-24 22:08:48 +0000950 p = uflags;
951 for (i = 0; flags[i] != NUL; ++i)
Bram Moolenaarace95982017-03-29 17:30:27 +0200952 p += utf_char2bytes(flags[i], p);
Bram Moolenaar6de68532005-08-24 22:08:48 +0000953 *p = NUL;
954 p = uflags;
955 }
956 else
Bram Moolenaar6de68532005-08-24 22:08:48 +0000957 p = flags;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100958 if (!vim_regexec_prog(&slang->sl_compprog, FALSE, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000959 return FALSE;
960
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100961 // Count the number of syllables. This may be slow, do it last. If there
962 // are too many syllables AND the number of compound words is above
963 // COMPOUNDWORDMAX then compounding is not allowed.
Bram Moolenaar5195e452005-08-19 20:32:47 +0000964 if (slang->sl_compsylmax < MAXWLEN
965 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +0000966 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000967 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000968}
969
970/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000971 * Return TRUE if the compound flags in compflags[] match the start of any
972 * compound rule. This is used to stop trying a compound if the flags
973 * collected so far can't possibly match any compound rule.
974 * Caller must check that slang->sl_comprules is not NULL.
975 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +0200976 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100977match_compoundrule(slang_T *slang, char_u *compflags)
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000978{
979 char_u *p;
980 int i;
981 int c;
982
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100983 // loop over all the COMPOUNDRULE entries
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000984 for (p = slang->sl_comprules; *p != NUL; ++p)
985 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100986 // loop over the flags in the compound word we have made, match
987 // them against the current rule entry
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000988 for (i = 0; ; ++i)
989 {
990 c = compflags[i];
991 if (c == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100992 // found a rule that matches for the flags we have so far
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000993 return TRUE;
994 if (*p == '/' || *p == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100995 break; // end of rule, it's too short
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000996 if (*p == '[')
997 {
998 int match = FALSE;
999
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001000 // compare against all the flags in []
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001001 ++p;
1002 while (*p != ']' && *p != NUL)
1003 if (*p++ == c)
1004 match = TRUE;
1005 if (!match)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001006 break; // none matches
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001007 }
1008 else if (*p != c)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001009 break; // flag of word doesn't match flag in pattern
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001010 ++p;
1011 }
1012
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001013 // Skip to the next "/", where the next pattern starts.
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001014 p = vim_strchr(p, '/');
1015 if (p == NULL)
1016 break;
1017 }
1018
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001019 // Checked all the rules and none of them match the flags, so there
1020 // can't possibly be a compound starting with these flags.
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001021 return FALSE;
1022}
1023
1024/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001025 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1026 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001027 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001028 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02001029 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001030valid_word_prefix(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001031 int totprefcnt, // nr of prefix IDs
1032 int arridx, // idx in sl_pidxs[]
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001033 int flags,
1034 char_u *word,
1035 slang_T *slang,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001036 int cond_req) // only use prefixes with a condition
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001037{
1038 int prefcnt;
1039 int pidx;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001040 regprog_T **rp;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001041 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001042
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001043 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001044 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1045 {
1046 pidx = slang->sl_pidxs[arridx + prefcnt];
1047
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001048 // Check the prefix ID.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001049 if (prefid != (pidx & 0xff))
1050 continue;
1051
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001052 // Check if the prefix doesn't combine and the word already has a
1053 // suffix.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001054 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1055 continue;
1056
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001057 // Check the condition, if there is one. The condition index is
1058 // stored in the two bytes above the prefix ID byte.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001059 rp = &slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
1060 if (*rp != NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001061 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001062 if (!vim_regexec_prog(rp, FALSE, word, 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001063 continue;
1064 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001065 else if (cond_req)
1066 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001067
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001068 // It's a match! Return the WF_ flags.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001069 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001070 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001071 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001072}
1073
1074/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001075 * Check if the word at "mip->mi_word" has a matching prefix.
1076 * If it does, then check the following word.
1077 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001078 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1079 * prefix in a compound word.
1080 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001081 * For a match mip->mi_result is updated.
1082 */
1083 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001084find_prefix(matchinf_T *mip, int mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001085{
1086 idx_T arridx = 0;
1087 int len;
1088 int wlen = 0;
1089 int flen;
1090 int c;
1091 char_u *ptr;
1092 idx_T lo, hi, m;
1093 slang_T *slang = mip->mi_lp->lp_slang;
1094 char_u *byts;
1095 idx_T *idxs;
1096
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001097 byts = slang->sl_pbyts;
1098 if (byts == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001099 return; // array is empty
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001100
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001101 // We use the case-folded word here, since prefixes are always
1102 // case-folded.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001103 ptr = mip->mi_fword;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001104 flen = mip->mi_fwordlen; // available case-folded bytes
Bram Moolenaard12a1322005-08-21 22:08:24 +00001105 if (mode == FIND_COMPOUND)
1106 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001107 // Skip over the previously found word(s).
Bram Moolenaard12a1322005-08-21 22:08:24 +00001108 ptr += mip->mi_compoff;
1109 flen -= mip->mi_compoff;
1110 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001111 idxs = slang->sl_pidxs;
1112
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001113 /*
1114 * Repeat advancing in the tree until:
1115 * - there is a byte that doesn't match,
1116 * - we reach the end of the tree,
1117 * - or we reach the end of the line.
1118 */
1119 for (;;)
1120 {
1121 if (flen == 0 && *mip->mi_fend != NUL)
1122 flen = fold_more(mip);
1123
1124 len = byts[arridx++];
1125
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001126 // If the first possible byte is a zero the prefix could end here.
1127 // Check if the following word matches and supports the prefix.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001128 if (byts[arridx] == 0)
1129 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001130 // There can be several prefixes with different conditions. We
1131 // try them all, since we don't know which one will give the
1132 // longest match. The word is the same each time, pass the list
1133 // of possible prefixes to find_word().
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001134 mip->mi_prefarridx = arridx;
1135 mip->mi_prefcnt = len;
1136 while (len > 0 && byts[arridx] == 0)
1137 {
1138 ++arridx;
1139 --len;
1140 }
1141 mip->mi_prefcnt -= len;
1142
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001143 // Find the word that comes after the prefix.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001144 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001145 if (mode == FIND_COMPOUND)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001146 // Skip over the previously found word(s).
Bram Moolenaard12a1322005-08-21 22:08:24 +00001147 mip->mi_prefixlen += mip->mi_compoff;
1148
Bram Moolenaar53805d12005-08-01 07:08:33 +00001149 if (has_mbyte)
1150 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001151 // Case-folded length may differ from original length.
Bram Moolenaard12a1322005-08-21 22:08:24 +00001152 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1153 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001154 }
1155 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001156 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001157 find_word(mip, FIND_PREFIX);
1158
1159
1160 if (len == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001161 break; // no children, word must end here
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001162 }
1163
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001164 // Stop looking at end of the line.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001165 if (ptr[wlen] == NUL)
1166 break;
1167
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001168 // Perform a binary search in the list of accepted bytes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001169 c = ptr[wlen];
1170 lo = arridx;
1171 hi = arridx + len - 1;
1172 while (lo < hi)
1173 {
1174 m = (lo + hi) / 2;
1175 if (byts[m] > c)
1176 hi = m - 1;
1177 else if (byts[m] < c)
1178 lo = m + 1;
1179 else
1180 {
1181 lo = hi = m;
1182 break;
1183 }
1184 }
1185
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001186 // Stop if there is no matching byte.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001187 if (hi < lo || byts[lo] != c)
1188 break;
1189
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001190 // Continue at the child (if there is one).
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001191 arridx = idxs[lo];
1192 ++wlen;
1193 --flen;
1194 }
1195}
1196
1197/*
1198 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001199 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001200 * Return the length of the folded chars in bytes.
1201 */
1202 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001203fold_more(matchinf_T *mip)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001204{
1205 int flen;
1206 char_u *p;
1207
1208 p = mip->mi_fend;
1209 do
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001210 MB_PTR_ADV(mip->mi_fend);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001211 while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001212
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001213 // Include the non-word character so that we can check for the word end.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001214 if (*mip->mi_fend != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001215 MB_PTR_ADV(mip->mi_fend);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001216
Bram Moolenaar4f135272021-06-11 19:07:40 +02001217 (void)spell_casefold(mip->mi_win, p, (int)(mip->mi_fend - p),
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001218 mip->mi_fword + mip->mi_fwordlen,
1219 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001220 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001221 mip->mi_fwordlen += flen;
1222 return flen;
1223}
1224
1225/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001226 * Check case flags for a word. Return TRUE if the word has the requested
1227 * case.
1228 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02001229 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001230spell_valid_case(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001231 int wordflags, // flags for the checked word.
1232 int treeflags) // flags for the word in the spell tree
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001233{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001234 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001235 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001236 && ((treeflags & WF_ONECAP) == 0
1237 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001238}
1239
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001240/*
Bram Moolenaaree09fcc2022-09-25 20:58:30 +01001241 * Return TRUE if spell checking is enabled for "wp".
1242 */
1243 int
1244spell_check_window(win_T *wp)
1245{
1246 return wp->w_p_spell
1247 && *wp->w_s->b_p_spl != NUL
1248 && wp->w_s->b_langp.ga_len > 0
1249 && *(char **)(wp->w_s->b_langp.ga_data) != NULL;
1250}
1251
1252/*
1253 * Return TRUE and give an error if spell checking is not enabled.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001254 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +02001255 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001256no_spell_checking(win_T *wp)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001257{
Bram Moolenaaree09fcc2022-09-25 20:58:30 +01001258 if (spell_check_window(wp))
1259 return FALSE;
1260 emsg(_(e_spell_checking_is_not_possible));
1261 return TRUE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001262}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001263
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001264/*
1265 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001266 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
1267 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001268 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1269 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00001270 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001271 */
1272 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001273spell_move_to(
1274 win_T *wp,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001275 int dir, // FORWARD or BACKWARD
1276 int allwords, // TRUE for "[s"/"]s", FALSE for "[S"/"]S"
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001277 int curline,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001278 hlf_T *attrp) // return: attributes of bad word or NULL
1279 // (only when "dir" is FORWARD)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001280{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001281 linenr_T lnum;
1282 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001283 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001284 char_u *line;
1285 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001286 char_u *endp;
Bram Moolenaar2813f382022-06-09 19:54:24 +01001287 hlf_T attr = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001288 int len;
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001289#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001290 int has_syntax = syntax_present(wp);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001291#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001292 int col;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001293 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001294 char_u *buf = NULL;
1295 int buflen = 0;
1296 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001297 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001298 int found_one = FALSE;
1299 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001300
Bram Moolenaar95529562005-08-25 21:21:38 +00001301 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00001302 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001303
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001304 /*
1305 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00001306 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001307 *
1308 * When searching backwards, we continue in the line to find the last
1309 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001310 *
1311 * We concatenate the start of the next line, so that wrapped words work
1312 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1313 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001314 */
Bram Moolenaar95529562005-08-25 21:21:38 +00001315 lnum = wp->w_cursor.lnum;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001316 CLEAR_POS(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001317
1318 while (!got_int)
1319 {
Bram Moolenaar2813f382022-06-09 19:54:24 +01001320 int empty_line;
1321
Bram Moolenaar95529562005-08-25 21:21:38 +00001322 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001323
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001324 len = (int)STRLEN(line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001325 if (buflen < len + MAXWLEN + 2)
1326 {
1327 vim_free(buf);
1328 buflen = len + MAXWLEN + 2;
1329 buf = alloc(buflen);
1330 if (buf == NULL)
1331 break;
1332 }
1333
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001334 // In first line check first word for Capital.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001335 if (lnum == 1)
1336 capcol = 0;
1337
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001338 // For checking first word with a capital skip white space.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001339 if (capcol == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001340 capcol = getwhitecols(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001341 else if (curline && wp == curwin)
1342 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001343 // For spellbadword(): check if first word needs a capital.
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001344 col = getwhitecols(line);
Luuk van Baal2ac64972023-05-25 17:14:42 +01001345 if (check_need_cap(curwin, lnum, col))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001346 capcol = col;
1347
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001348 // Need to get the line again, may have looked at the previous
1349 // one.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001350 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1351 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001352
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001353 // Copy the line into "buf" and append the start of the next line if
Bram Moolenaar2813f382022-06-09 19:54:24 +01001354 // possible. Note: this ml_get_buf() may make "line" invalid, check
1355 // for empty line first.
1356 empty_line = *skipwhite(line) == NUL;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001357 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00001358 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar5dd95a12006-05-13 12:09:24 +00001359 spell_cat_line(buf + STRLEN(buf),
1360 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001361
1362 p = buf + skip;
1363 endp = buf + len;
1364 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001365 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001366 // When searching backward don't search after the cursor. Unless
1367 // we wrapped around the end of the buffer.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001368 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00001369 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001370 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00001371 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001372 break;
1373
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001374 // start of word
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001375 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001376 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001377
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001378 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001379 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001380 // We found a bad word. Check the attribute.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001381 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001382 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001383 // When searching forward only accept a bad word after
1384 // the cursor.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001385 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001386 || lnum != wp->w_cursor.lnum
Bram Moolenaarfe154992022-03-22 20:42:12 +00001387 || (wrapped
1388 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00001389 : p - buf)
Bram Moolenaarfe154992022-03-22 20:42:12 +00001390 > wp->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001391 {
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001392#ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00001393 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001394 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001395 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001396 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00001397 FALSE, &can_spell, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001398 if (!can_spell)
1399 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001400 }
1401 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001402#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001403 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001404
Bram Moolenaar51485f02005-06-04 21:55:20 +00001405 if (can_spell)
1406 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00001407 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001408 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001409 found_pos.col = (int)(p - buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001410 found_pos.coladd = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001411 if (dir == FORWARD)
1412 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001413 // No need to search further.
Bram Moolenaar95529562005-08-25 21:21:38 +00001414 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001415 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001416 if (attrp != NULL)
1417 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001418 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001419 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001420 else if (curline)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001421 // Insert mode completion: put cursor after
1422 // the bad word.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001423 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001424 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001425 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001426 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00001427 else
1428 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001429 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001430 }
1431
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001432 // advance to character after the word
Bram Moolenaar51485f02005-06-04 21:55:20 +00001433 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001434 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001435 }
1436
Bram Moolenaar5195e452005-08-19 20:32:47 +00001437 if (dir == BACKWARD && found_pos.lnum != 0)
1438 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001439 // Use the last match in the line (before the cursor).
Bram Moolenaar95529562005-08-25 21:21:38 +00001440 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001441 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001442 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001443 }
1444
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001445 if (curline)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001446 break; // only check cursor line
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001447
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001448 // If we are back at the starting line and searched it again there
1449 // is no match, give up.
Bram Moolenaard3f78dc2017-02-25 14:21:10 +01001450 if (lnum == wp->w_cursor.lnum && wrapped)
1451 break;
1452
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001453 // Advance to next line.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001454 if (dir == BACKWARD)
1455 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001456 if (lnum > 1)
1457 --lnum;
1458 else if (!p_ws)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001459 break; // at first line and 'nowrapscan'
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001460 else
1461 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001462 // Wrap around to the end of the buffer. May search the
1463 // starting line again and accept the last match.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001464 lnum = wp->w_buffer->b_ml.ml_line_count;
1465 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001466 if (!shortmess(SHM_SEARCH))
1467 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001468 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001469 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001470 }
1471 else
1472 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001473 if (lnum < wp->w_buffer->b_ml.ml_line_count)
1474 ++lnum;
1475 else if (!p_ws)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001476 break; // at first line and 'nowrapscan'
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001477 else
1478 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001479 // Wrap around to the start of the buffer. May search the
1480 // starting line again and accept the first match.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001481 lnum = 1;
1482 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001483 if (!shortmess(SHM_SEARCH))
1484 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001485 }
1486
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001487 // If we are back at the starting line and there is no match then
1488 // give up.
Bram Moolenaard3f78dc2017-02-25 14:21:10 +01001489 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001490 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001491
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001492 // Skip the characters at the start of the next line that were
1493 // included in a match crossing line boundaries.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001494 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001495 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001496 else
1497 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001498
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001499 // Capcol skips over the inserted space.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001500 --capcol;
1501
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001502 // But after empty line check first word in next line
Bram Moolenaar2813f382022-06-09 19:54:24 +01001503 if (empty_line)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001504 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001505 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001506
1507 line_breakcheck();
1508 }
1509
Bram Moolenaar0c405862005-06-22 22:26:26 +00001510 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001511 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001512}
1513
1514/*
1515 * For spell checking: concatenate the start of the following line "line" into
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001516 * "buf", blanking-out special characters. Copy less than "maxlen" bytes.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00001517 * Keep the blanks at the start of the next line, this is used in win_line()
1518 * to skip those bytes if the word was OK.
Bram Moolenaar0c405862005-06-22 22:26:26 +00001519 */
1520 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001521spell_cat_line(char_u *buf, char_u *line, int maxlen)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001522{
1523 char_u *p;
1524 int n;
1525
1526 p = skipwhite(line);
1527 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1528 p = skipwhite(p + 1);
1529
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001530 if (*p == NUL)
1531 return;
1532
1533 // Only worth concatenating if there is something else than spaces to
1534 // concatenate.
1535 n = (int)(p - line) + 1;
1536 if (n < maxlen - 1)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001537 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001538 vim_memset(buf, ' ', n);
1539 vim_strncpy(buf + n, p, maxlen - 1 - n);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001540 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001541}
1542
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001543/*
1544 * Structure used for the cookie argument of do_in_runtimepath().
1545 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001546typedef struct spelload_S
1547{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001548 char_u sl_lang[MAXWLEN + 1]; // language name
1549 slang_T *sl_slang; // resulting slang_T struct
1550 int sl_nobreak; // NOBREAK language found
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001551} spelload_T;
1552
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001553/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001554 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001555 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001556 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001557 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001558spell_load_lang(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001559{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001560 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001561 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001562 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001563 int round;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001564
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001565 // Copy the language name to pass it to spell_load_cb() as a cookie.
1566 // It's truncated when an error is detected.
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001567 STRCPY(sl.sl_lang, lang);
1568 sl.sl_slang = NULL;
1569 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001570
Bram Moolenaaref976322022-09-28 11:48:30 +01001571 // Disallow deleting the current buffer. Autocommands can do weird things
1572 // and cause "lang" to be freed.
1573 ++curbuf->b_locked;
1574
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001575 // We may retry when no spell file is found for the language, an
1576 // autocommand may load it then.
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001577 for (round = 1; round <= 2; ++round)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001578 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001579 /*
1580 * Find the first spell file for "lang" in 'runtimepath' and load it.
1581 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001582 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001583#ifdef VMS
1584 "spell/%s_%s.spl",
1585#else
1586 "spell/%s.%s.spl",
1587#endif
1588 lang, spell_enc());
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001589 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001590
1591 if (r == FAIL && *sl.sl_lang != NUL)
1592 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001593 // Try loading the ASCII version.
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001594 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001595#ifdef VMS
1596 "spell/%s_ascii.spl",
1597#else
1598 "spell/%s.ascii.spl",
1599#endif
1600 lang);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001601 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001602
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001603 if (r == FAIL && *sl.sl_lang != NUL && round == 1
1604 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
1605 curbuf->b_fname, FALSE, curbuf))
1606 continue;
1607 break;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001608 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001609 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001610 }
1611
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001612 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001613 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001614 smsg(
Bram Moolenaar56f78042010-12-08 17:09:32 +01001615#ifdef VMS
1616 _("Warning: Cannot find word list \"%s_%s.spl\" or \"%s_ascii.spl\""),
1617#else
1618 _("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
1619#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00001620 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001621 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001622 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001623 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001624 // At least one file was loaded, now load ALL the additions.
Bram Moolenaarb765d632005-06-07 21:00:02 +00001625 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001626 do_in_runtimepath(fname_enc, DIP_ALL, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001627 }
Bram Moolenaaref976322022-09-28 11:48:30 +01001628
1629 --curbuf->b_locked;
Bram Moolenaarb765d632005-06-07 21:00:02 +00001630}
1631
1632/*
1633 * Return the encoding used for spell checking: Use 'encoding', except that we
1634 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1635 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001636 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001637spell_enc(void)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001638{
1639
Bram Moolenaarb765d632005-06-07 21:00:02 +00001640 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1641 return p_enc;
Bram Moolenaarb765d632005-06-07 21:00:02 +00001642 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001643}
1644
1645/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001646 * Get the name of the .spl file for the internal wordlist into
1647 * "fname[MAXPATHL]".
1648 */
1649 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001650int_wordlist_spl(char_u *fname)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001651{
Bram Moolenaar56f78042010-12-08 17:09:32 +01001652 vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001653 int_wordlist, spell_enc());
1654}
1655
1656/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001657 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001658 * Caller must fill "sl_next".
1659 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001660 slang_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001661slang_alloc(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001662{
1663 slang_T *lp;
1664
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001665 lp = ALLOC_CLEAR_ONE(slang_T);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001666 if (lp != NULL)
1667 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001668 if (lang != NULL)
1669 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001670 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001671 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001672 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001673 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001674 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001675 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001676
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001677 return lp;
1678}
1679
1680/*
1681 * Free the contents of an slang_T and the structure itself.
1682 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001683 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001684slang_free(slang_T *lp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001685{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001686 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001687 vim_free(lp->sl_fname);
1688 slang_clear(lp);
1689 vim_free(lp);
1690}
1691
1692/*
1693 * Clear an slang_T so that the file can be reloaded.
1694 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001695 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001696slang_clear(slang_T *lp)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001697{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001698 garray_T *gap;
1699 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001700 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001701 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001702 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001703
Bram Moolenaard23a8232018-02-10 18:45:26 +01001704 VIM_CLEAR(lp->sl_fbyts);
1705 VIM_CLEAR(lp->sl_kbyts);
1706 VIM_CLEAR(lp->sl_pbyts);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001707
Bram Moolenaard23a8232018-02-10 18:45:26 +01001708 VIM_CLEAR(lp->sl_fidxs);
1709 VIM_CLEAR(lp->sl_kidxs);
1710 VIM_CLEAR(lp->sl_pidxs);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001711
Bram Moolenaar4770d092006-01-12 23:22:24 +00001712 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001713 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001714 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
1715 while (gap->ga_len > 0)
1716 {
1717 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1718 vim_free(ftp->ft_from);
1719 vim_free(ftp->ft_to);
1720 }
1721 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001722 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001723
1724 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001725 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001726 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001727 // "ga_len" is set to 1 without adding an item for latin1
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001728 if (gap->ga_data != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001729 // SOFOFROM and SOFOTO items: free lists of wide characters.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001730 for (i = 0; i < gap->ga_len; ++i)
1731 vim_free(((int **)gap->ga_data)[i]);
1732 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001733 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001734 // SAL items: free salitem_T items
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001735 while (gap->ga_len > 0)
1736 {
1737 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1738 vim_free(smp->sm_lead);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001739 // Don't free sm_oneof and sm_rules, they point into sm_lead.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001740 vim_free(smp->sm_to);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001741 vim_free(smp->sm_lead_w);
1742 vim_free(smp->sm_oneof_w);
1743 vim_free(smp->sm_to_w);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001744 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001745 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001746
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001747 for (i = 0; i < lp->sl_prefixcnt; ++i)
Bram Moolenaar473de612013-06-08 18:19:48 +02001748 vim_regfree(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001749 lp->sl_prefixcnt = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001750 VIM_CLEAR(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001751
Bram Moolenaard23a8232018-02-10 18:45:26 +01001752 VIM_CLEAR(lp->sl_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001753
Bram Moolenaard23a8232018-02-10 18:45:26 +01001754 VIM_CLEAR(lp->sl_midword);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001755
Bram Moolenaar473de612013-06-08 18:19:48 +02001756 vim_regfree(lp->sl_compprog);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001757 lp->sl_compprog = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001758 VIM_CLEAR(lp->sl_comprules);
1759 VIM_CLEAR(lp->sl_compstartflags);
1760 VIM_CLEAR(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001761
Bram Moolenaard23a8232018-02-10 18:45:26 +01001762 VIM_CLEAR(lp->sl_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001763 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001764
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001765 ga_clear_strings(&lp->sl_comppat);
1766
Bram Moolenaar4770d092006-01-12 23:22:24 +00001767 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
1768 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00001769
Bram Moolenaar4770d092006-01-12 23:22:24 +00001770 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001771
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001772 // Clear info from .sug file.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001773 slang_clear_sug(lp);
1774
Bram Moolenaar5195e452005-08-19 20:32:47 +00001775 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001776 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001777 lp->sl_compsylmax = MAXWLEN;
1778 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001779}
1780
1781/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001782 * Clear the info from the .sug file in "lp".
1783 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001784 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001785slang_clear_sug(slang_T *lp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00001786{
Bram Moolenaard23a8232018-02-10 18:45:26 +01001787 VIM_CLEAR(lp->sl_sbyts);
1788 VIM_CLEAR(lp->sl_sidxs);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001789 close_spellbuf(lp->sl_sugbuf);
1790 lp->sl_sugbuf = NULL;
1791 lp->sl_sugloaded = FALSE;
1792 lp->sl_sugtime = 0;
1793}
1794
1795/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001796 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001797 * Invoked through do_in_runtimepath().
1798 */
1799 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001800spell_load_cb(char_u *fname, void *cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001801{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001802 spelload_T *slp = (spelload_T *)cookie;
1803 slang_T *slang;
1804
1805 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001806 if (slang == NULL)
1807 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001808
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001809 // When a previously loaded file has NOBREAK also use it for the
1810 // ".add" files.
1811 if (slp->sl_nobreak && slang->sl_add)
1812 slang->sl_nobreak = TRUE;
1813 else if (slang->sl_nobreak)
1814 slp->sl_nobreak = TRUE;
1815
1816 slp->sl_slang = slang;
Bram Moolenaarb765d632005-06-07 21:00:02 +00001817}
1818
Bram Moolenaar4770d092006-01-12 23:22:24 +00001819
1820/*
1821 * Add a word to the hashtable of common words.
1822 * If it's already there then the counter is increased.
1823 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001824 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001825count_common_word(
1826 slang_T *lp,
1827 char_u *word,
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001828 int len, // word length, -1 for up to NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001829 int count) // 1 to count once, 10 to init
Bram Moolenaar4770d092006-01-12 23:22:24 +00001830{
1831 hash_T hash;
1832 hashitem_T *hi;
1833 wordcount_T *wc;
1834 char_u buf[MAXWLEN];
1835 char_u *p;
1836
1837 if (len == -1)
1838 p = word;
Bram Moolenaar5bcc5a12019-08-06 22:48:02 +02001839 else if (len >= MAXWLEN)
1840 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001841 else
1842 {
1843 vim_strncpy(buf, word, len);
1844 p = buf;
1845 }
1846
1847 hash = hash_hash(p);
1848 hi = hash_lookup(&lp->sl_wordcount, p, hash);
1849 if (HASHITEM_EMPTY(hi))
1850 {
zeertzjq1b438a82023-02-01 13:11:15 +00001851 wc = alloc(offsetof(wordcount_T, wc_word) + STRLEN(p) + 1);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001852 if (wc == NULL)
1853 return;
1854 STRCPY(wc->wc_word, p);
1855 wc->wc_count = count;
1856 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
1857 }
1858 else
1859 {
1860 wc = HI2WC(hi);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001861 if ((wc->wc_count += count) < (unsigned)count) // check for overflow
Bram Moolenaar4770d092006-01-12 23:22:24 +00001862 wc->wc_count = MAXWORDCOUNT;
1863 }
1864}
1865
1866/*
Bram Moolenaar95529562005-08-25 21:21:38 +00001867 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00001868 * Like strchr() but independent of locale.
1869 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001870 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001871byte_in_str(char_u *str, int n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001872{
1873 char_u *p;
1874
1875 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00001876 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001877 return TRUE;
1878 return FALSE;
1879}
1880
Bram Moolenaar5195e452005-08-19 20:32:47 +00001881#define SY_MAXLEN 30
1882typedef struct syl_item_S
1883{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001884 char_u sy_chars[SY_MAXLEN]; // the sequence of chars
Bram Moolenaar5195e452005-08-19 20:32:47 +00001885 int sy_len;
1886} syl_item_T;
1887
1888/*
1889 * Truncate "slang->sl_syllable" at the first slash and put the following items
1890 * in "slang->sl_syl_items".
1891 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001892 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001893init_syl_tab(slang_T *slang)
Bram Moolenaar5195e452005-08-19 20:32:47 +00001894{
1895 char_u *p;
1896 char_u *s;
1897 int l;
1898 syl_item_T *syl;
1899
1900 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
1901 p = vim_strchr(slang->sl_syllable, '/');
1902 while (p != NULL)
1903 {
1904 *p++ = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001905 if (*p == NUL) // trailing slash
Bram Moolenaar5195e452005-08-19 20:32:47 +00001906 break;
1907 s = p;
1908 p = vim_strchr(p, '/');
1909 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001910 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001911 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001912 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001913 if (l >= SY_MAXLEN)
1914 return SP_FORMERROR;
1915 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001916 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001917 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
1918 + slang->sl_syl_items.ga_len++;
1919 vim_strncpy(syl->sy_chars, s, l);
1920 syl->sy_len = l;
1921 }
1922 return OK;
1923}
1924
1925/*
1926 * Count the number of syllables in "word".
1927 * When "word" contains spaces the syllables after the last space are counted.
1928 * Returns zero if syllables are not defines.
1929 */
1930 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001931count_syllables(slang_T *slang, char_u *word)
Bram Moolenaar5195e452005-08-19 20:32:47 +00001932{
1933 int cnt = 0;
1934 int skip = FALSE;
1935 char_u *p;
1936 int len;
1937 int i;
1938 syl_item_T *syl;
1939 int c;
1940
1941 if (slang->sl_syllable == NULL)
1942 return 0;
1943
1944 for (p = word; *p != NUL; p += len)
1945 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001946 // When running into a space reset counter.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001947 if (*p == ' ')
1948 {
1949 len = 1;
1950 cnt = 0;
1951 continue;
1952 }
1953
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001954 // Find longest match of syllable items.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001955 len = 0;
1956 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
1957 {
1958 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
1959 if (syl->sy_len > len
1960 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
1961 len = syl->sy_len;
1962 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001963 if (len != 0) // found a match, count syllable
Bram Moolenaar5195e452005-08-19 20:32:47 +00001964 {
1965 ++cnt;
1966 skip = FALSE;
1967 }
1968 else
1969 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001970 // No recognized syllable item, at least a syllable char then?
Bram Moolenaar5195e452005-08-19 20:32:47 +00001971 c = mb_ptr2char(p);
1972 len = (*mb_ptr2len)(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001973 if (vim_strchr(slang->sl_syllable, c) == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001974 skip = FALSE; // No, search for next syllable
Bram Moolenaar5195e452005-08-19 20:32:47 +00001975 else if (!skip)
1976 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001977 ++cnt; // Yes, count it
1978 skip = TRUE; // don't count following syllable chars
Bram Moolenaar5195e452005-08-19 20:32:47 +00001979 }
1980 }
1981 }
1982 return cnt;
1983}
1984
1985/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02001986 * Parse 'spelllang' and set w_s->b_langp accordingly.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001987 * Returns NULL if it's OK, an untranslated error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001988 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001989 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001990parse_spelllang(win_T *wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001991{
1992 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001993 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001994 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00001995 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001996 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001997 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001998 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001999 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002000 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002001 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002002 int len;
2003 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002004 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002005 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002006 char_u *use_region = NULL;
2007 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002008 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002009 int i, j;
2010 langp_T *lp, *lp2;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002011 static int recursive = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002012 char *ret_msg = NULL;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002013 char_u *spl_copy;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002014 bufref_T bufref;
2015
2016 set_bufref(&bufref, wp->w_buffer);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002017
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002018 // We don't want to do this recursively. May happen when a language is
2019 // not available and the SpellFileMissing autocommand opens a new buffer
2020 // in which 'spell' is set.
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002021 if (recursive)
2022 return NULL;
2023 recursive = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002024
2025 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002026 clear_midword(wp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002027
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002028 // Make a copy of 'spelllang', the SpellFileMissing autocommands may change
2029 // it under our fingers.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002030 spl_copy = vim_strsave(wp->w_s->b_p_spl);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002031 if (spl_copy == NULL)
2032 goto theend;
2033
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002034 wp->w_s->b_cjk = 0;
2035
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002036 // Loop over comma separated language names.
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002037 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002038 {
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002039 // Get one language name.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002040 copy_option_part(&splp, lang, MAXWLEN, ",");
Bram Moolenaar5482f332005-04-17 20:18:43 +00002041 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002042 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002043
Bram Moolenaarf154f3a2020-06-08 18:54:49 +02002044 if (!valid_spelllang(lang))
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002045 continue;
2046
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002047 if (STRCMP(lang, "cjk") == 0)
2048 {
2049 wp->w_s->b_cjk = 1;
2050 continue;
2051 }
2052
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002053 // If the name ends in ".spl" use it as the name of the spell file.
2054 // If there is a region name let "region" point to it and remove it
2055 // from the name.
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002056 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2057 {
2058 filename = TRUE;
2059
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002060 // Locate a region and remove it from the file name.
Bram Moolenaarb6356332005-07-18 21:40:44 +00002061 p = vim_strchr(gettail(lang), '_');
2062 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
2063 && !ASCII_ISALPHA(p[3]))
2064 {
2065 vim_strncpy(region_cp, p + 1, 2);
2066 mch_memmove(p, p + 3, len - (p - lang) - 2);
Bram Moolenaarb6356332005-07-18 21:40:44 +00002067 region = region_cp;
2068 }
2069 else
2070 dont_use_region = TRUE;
2071
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002072 // Check if we loaded this language before.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002073 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar99499b12019-05-23 21:35:48 +02002074 if (fullpathcmp(lang, slang->sl_fname, FALSE, TRUE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002075 break;
2076 }
2077 else
2078 {
2079 filename = FALSE;
2080 if (len > 3 && lang[len - 3] == '_')
2081 {
2082 region = lang + len - 2;
2083 len -= 3;
2084 lang[len] = NUL;
2085 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002086 else
2087 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002088
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002089 // Check if we loaded this language before.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002090 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002091 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002092 break;
2093 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002094
Bram Moolenaarb6356332005-07-18 21:40:44 +00002095 if (region != NULL)
2096 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002097 // If the region differs from what was used before then don't
2098 // use it for 'spellfile'.
Bram Moolenaarb6356332005-07-18 21:40:44 +00002099 if (use_region != NULL && STRCMP(region, use_region) != 0)
2100 dont_use_region = TRUE;
2101 use_region = region;
2102 }
2103
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002104 // If not found try loading the language now.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002105 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002106 {
2107 if (filename)
2108 (void)spell_load_file(lang, lang, NULL, FALSE);
2109 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002110 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002111 spell_load_lang(lang);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002112 // SpellFileMissing autocommands may do anything, including
Bram Moolenaarc3d27ad2022-11-14 20:52:14 +00002113 // destroying the buffer we are using or closing the window.
2114 if (!bufref_valid(&bufref) || !win_valid_any_tab(wp))
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002115 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002116 ret_msg = N_(e_spellfilemising_autocommand_deleted_buffer);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002117 goto theend;
2118 }
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002119 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002120 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002121
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002122 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002123 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002124 */
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002125 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar99499b12019-05-23 21:35:48 +02002126 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE, TRUE)
2127 == FPC_SAME
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002128 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002129 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002130 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002131 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002132 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002133 // find region in sl_regions
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002134 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002135 if (c == REGION_ALL)
2136 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002137 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002138 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002139 if (*slang->sl_regions != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002140 // This addition file is for other regions.
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002141 region_mask = 0;
2142 }
2143 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002144 // This is probably an error. Give a warning and
2145 // accept the words anyway.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002146 smsg(_("Warning: region %s not supported"),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002147 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002148 }
2149 else
2150 region_mask = 1 << c;
2151 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002152
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002153 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002154 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002155 if (ga_grow(&ga, 1) == FAIL)
2156 {
2157 ga_clear(&ga);
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002158 ret_msg = e_out_of_memory;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002159 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002160 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002161 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002162 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2163 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002164 use_midword(slang, wp);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002165 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002166 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002167 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002168 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002169 }
2170
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002171 // round 0: load int_wordlist, if possible.
2172 // round 1: load first name in 'spellfile'.
2173 // round 2: load second name in 'spellfile.
2174 // etc.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002175 spf = curwin->w_s->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002176 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002177 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002178 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002179 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002180 // Internal wordlist, if there is one.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002181 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002182 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002183 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002184 }
2185 else
2186 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002187 // One entry in 'spellfile'.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002188 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
2189 STRCAT(spf_name, ".spl");
2190
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002191 // If it was already found above then skip it.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002192 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002193 {
2194 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
Bram Moolenaar99499b12019-05-23 21:35:48 +02002195 if (p != NULL && fullpathcmp(spf_name, p, FALSE, TRUE)
2196 == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002197 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002198 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002199 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002200 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002201 }
2202
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002203 // Check if it was loaded already.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002204 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar99499b12019-05-23 21:35:48 +02002205 if (fullpathcmp(spf_name, slang->sl_fname, FALSE, TRUE)
2206 == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002207 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002208 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002209 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002210 // Not loaded, try loading it now. The language name includes the
2211 // region name, the region is ignored otherwise. for int_wordlist
2212 // use an arbitrary name.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002213 if (round == 0)
2214 STRCPY(lang, "internal wordlist");
2215 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00002216 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002217 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002218 p = vim_strchr(lang, '.');
2219 if (p != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002220 *p = NUL; // truncate at ".encoding.add"
Bram Moolenaar7887d882005-07-01 22:33:52 +00002221 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002222 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002223
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002224 // If one of the languages has NOBREAK we assume the addition
2225 // files also have this.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002226 if (slang != NULL && nobreak)
2227 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002228 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002229 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002230 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002231 region_mask = REGION_ALL;
2232 if (use_region != NULL && !dont_use_region)
2233 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002234 // find region in sl_regions
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002235 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002236 if (c != REGION_ALL)
2237 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002238 else if (*slang->sl_regions != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002239 // This spell file is for other regions.
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002240 region_mask = 0;
2241 }
2242
2243 if (region_mask != 0)
2244 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002245 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
2246 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
2247 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002248 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2249 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002250 use_midword(slang, wp);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002251 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002252 }
2253 }
2254
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002255 // Everything is fine, store the new b_langp value.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002256 ga_clear(&wp->w_s->b_langp);
2257 wp->w_s->b_langp = ga;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002258
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002259 // For each language figure out what language to use for sound folding and
2260 // REP items. If the language doesn't support it itself use another one
2261 // with the same name. E.g. for "en-math" use "en".
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002262 for (i = 0; i < ga.ga_len; ++i)
2263 {
2264 lp = LANGP_ENTRY(ga, i);
2265
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002266 // sound folding
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002267 if (lp->lp_slang->sl_sal.ga_len > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002268 // language does sound folding itself
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002269 lp->lp_sallang = lp->lp_slang;
2270 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002271 // find first similar language that does sound folding
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002272 for (j = 0; j < ga.ga_len; ++j)
2273 {
2274 lp2 = LANGP_ENTRY(ga, j);
2275 if (lp2->lp_slang->sl_sal.ga_len > 0
2276 && STRNCMP(lp->lp_slang->sl_name,
2277 lp2->lp_slang->sl_name, 2) == 0)
2278 {
2279 lp->lp_sallang = lp2->lp_slang;
2280 break;
2281 }
2282 }
2283
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002284 // REP items
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002285 if (lp->lp_slang->sl_rep.ga_len > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002286 // language has REP items itself
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002287 lp->lp_replang = lp->lp_slang;
2288 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002289 // find first similar language that has REP items
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002290 for (j = 0; j < ga.ga_len; ++j)
2291 {
2292 lp2 = LANGP_ENTRY(ga, j);
2293 if (lp2->lp_slang->sl_rep.ga_len > 0
2294 && STRNCMP(lp->lp_slang->sl_name,
2295 lp2->lp_slang->sl_name, 2) == 0)
2296 {
2297 lp->lp_replang = lp2->lp_slang;
2298 break;
2299 }
2300 }
2301 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002302 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002303
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002304theend:
2305 vim_free(spl_copy);
2306 recursive = FALSE;
2307 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002308}
2309
2310/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002311 * Clear the midword characters for buffer "buf".
2312 */
2313 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002314clear_midword(win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002315{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002316 CLEAR_FIELD(wp->w_s->b_spell_ismw);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002317 VIM_CLEAR(wp->w_s->b_spell_ismw_mb);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002318}
2319
2320/*
2321 * Use the "sl_midword" field of language "lp" for buffer "buf".
2322 * They add up to any currently used midword characters.
2323 */
2324 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002325use_midword(slang_T *lp, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002326{
2327 char_u *p;
2328
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002329 if (lp->sl_midword == NULL) // there aren't any
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002330 return;
2331
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002332 for (p = lp->sl_midword; *p != NUL; )
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002333 if (has_mbyte)
2334 {
2335 int c, l, n;
2336 char_u *bp;
2337
2338 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002339 l = (*mb_ptr2len)(p);
2340 if (c < 256 && l <= 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002341 wp->w_s->b_spell_ismw[c] = TRUE;
2342 else if (wp->w_s->b_spell_ismw_mb == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002343 // First multi-byte char in "b_spell_ismw_mb".
Bram Moolenaar860cae12010-06-05 23:22:07 +02002344 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002345 else
2346 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002347 // Append multi-byte chars to "b_spell_ismw_mb".
Bram Moolenaar860cae12010-06-05 23:22:07 +02002348 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
2349 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002350 if (bp != NULL)
2351 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002352 vim_free(wp->w_s->b_spell_ismw_mb);
2353 wp->w_s->b_spell_ismw_mb = bp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002354 vim_strncpy(bp + n, p, l);
2355 }
2356 }
2357 p += l;
2358 }
2359 else
Bram Moolenaar860cae12010-06-05 23:22:07 +02002360 wp->w_s->b_spell_ismw[*p++] = TRUE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002361}
2362
2363/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002364 * Find the region "region[2]" in "rp" (points to "sl_regions").
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01002365 * Each region is simply stored as the two characters of its name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002366 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002367 */
2368 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002369find_region(char_u *rp, char_u *region)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002370{
2371 int i;
2372
2373 for (i = 0; ; i += 2)
2374 {
2375 if (rp[i] == NUL)
2376 return REGION_ALL;
2377 if (rp[i] == region[0] && rp[i + 1] == region[1])
2378 break;
2379 }
2380 return i / 2;
2381}
2382
2383/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002384 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002385 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002386 * Word WF_ONECAP
2387 * W WORD WF_ALLCAP
2388 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002389 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002390 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002391captype(
2392 char_u *word,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002393 char_u *end) // When NULL use up to NUL byte.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002394{
2395 char_u *p;
2396 int c;
2397 int firstcap;
2398 int allcap;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002399 int past_second = FALSE; // past second word char
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002400
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002401 // find first letter
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002402 for (p = word; !spell_iswordp_nmw(p, curwin); MB_PTR_ADV(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002403 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002404 return 0; // only non-word characters, illegal word
Bram Moolenaarb765d632005-06-07 21:00:02 +00002405 if (has_mbyte)
2406 c = mb_ptr2char_adv(&p);
2407 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00002408 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002409 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002410
2411 /*
2412 * Need to check all letters to find a word with mixed upper/lower.
2413 * But a word with an upper char only at start is a ONECAP.
2414 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002415 for ( ; end == NULL ? *p != NUL : p < end; MB_PTR_ADV(p))
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002416 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002417 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002418 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002419 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002420 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002421 // UUl -> KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002422 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002423 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002424 allcap = FALSE;
2425 }
2426 else if (!allcap)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002427 // UlU -> KEEPCAP
Bram Moolenaar51485f02005-06-04 21:55:20 +00002428 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002429 past_second = TRUE;
2430 }
2431
2432 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002433 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002434 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002435 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002436 return 0;
2437}
2438
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002439/*
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002440 * Delete the internal wordlist and its .spl file.
2441 */
2442 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002443spell_delete_wordlist(void)
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002444{
2445 char_u fname[MAXPATHL];
2446
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002447 if (int_wordlist == NULL)
2448 return;
2449
2450 mch_remove(int_wordlist);
2451 int_wordlist_spl(fname);
2452 mch_remove(fname);
2453 VIM_CLEAR(int_wordlist);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002454}
2455
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002456/*
2457 * Free all languages.
2458 */
2459 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002460spell_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002461{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002462 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002463 buf_T *buf;
2464
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002465 // Go through all buffers and handle 'spelllang'. <VN>
Bram Moolenaar29323592016-07-24 22:04:11 +02002466 FOR_ALL_BUFFERS(buf)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002467 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002468
2469 while (first_lang != NULL)
2470 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002471 slang = first_lang;
2472 first_lang = slang->sl_next;
2473 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002474 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002475
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002476 spell_delete_wordlist();
Bram Moolenaar7887d882005-07-01 22:33:52 +00002477
Bram Moolenaard23a8232018-02-10 18:45:26 +01002478 VIM_CLEAR(repl_to);
2479 VIM_CLEAR(repl_from);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002480}
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002481
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002482/*
2483 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002484 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002485 */
2486 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002487spell_reload(void)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002488{
Bram Moolenaar3982c542005-06-08 21:56:31 +00002489 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002490
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002491 // Initialize the table for spell_iswordp().
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002492 init_spell_chartab();
2493
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002494 // Unload all allocated memory.
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002495 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002496
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002497 // Go through all buffers and handle 'spelllang'.
Bram Moolenaar29323592016-07-24 22:04:11 +02002498 FOR_ALL_WINDOWS(wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002499 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002500 // Only load the wordlists when 'spelllang' is set and there is a
2501 // window for this buffer in which 'spell' is set.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002502 if (*wp->w_s->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002503 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002504 if (wp->w_p_spell)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002505 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002506 (void)parse_spelllang(wp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00002507 break;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002508 }
2509 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002510 }
2511}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002512
Bram Moolenaarb765d632005-06-07 21:00:02 +00002513/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002514 * Open a spell buffer. This is a nameless buffer that is not in the buffer
2515 * list and only contains text lines. Can use a swapfile to reduce memory
2516 * use.
2517 * Most other fields are invalid! Esp. watch out for string options being
2518 * NULL and there is no undo info.
2519 * Returns NULL when out of memory.
2520 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002521 buf_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002522open_spellbuf(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002523{
2524 buf_T *buf;
2525
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002526 buf = ALLOC_CLEAR_ONE(buf_T);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002527 if (buf == NULL)
2528 return NULL;
2529
2530 buf->b_spell = TRUE;
2531 buf->b_p_swf = TRUE; // may create a swap file
Bram Moolenaar706d2de2013-07-17 17:35:13 +02002532#ifdef FEAT_CRYPT
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002533 buf->b_p_key = empty_option;
Bram Moolenaar706d2de2013-07-17 17:35:13 +02002534#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002535 ml_open(buf);
2536 ml_open_file(buf); // create swap file now
Bram Moolenaar4770d092006-01-12 23:22:24 +00002537 return buf;
2538}
2539
2540/*
2541 * Close the buffer used for spell info.
2542 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002543 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002544close_spellbuf(buf_T *buf)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002545{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002546 if (buf == NULL)
2547 return;
2548
2549 ml_close(buf, TRUE);
2550 vim_free(buf);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002551}
2552
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002553/*
2554 * Init the chartab used for spelling for ASCII.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002555 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002556 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002557clear_spell_chartab(spelltab_T *sp)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002558{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002559 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002560
Bram Moolenaara80faa82020-04-12 19:37:17 +02002561 // Init everything to FALSE (zero).
2562 CLEAR_FIELD(sp->st_isw);
2563 CLEAR_FIELD(sp->st_isu);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002564 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002565 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002566 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002567 sp->st_upper[i] = i;
2568 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002569
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002570 // We include digits. A word shouldn't start with a digit, but handling
2571 // that is done separately.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002572 for (i = '0'; i <= '9'; ++i)
2573 sp->st_isw[i] = TRUE;
2574 for (i = 'A'; i <= 'Z'; ++i)
2575 {
2576 sp->st_isw[i] = TRUE;
2577 sp->st_isu[i] = TRUE;
2578 sp->st_fold[i] = i + 0x20;
2579 }
2580 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002581 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002582 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002583 sp->st_upper[i] = i - 0x20;
2584 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002585}
2586
2587/*
2588 * Init the chartab used for spelling. Only depends on 'encoding'.
2589 * Called once while starting up and when 'encoding' changes.
2590 * The default is to use isalpha(), but the spell file should define the word
2591 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002592 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002593 */
2594 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002595init_spell_chartab(void)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002596{
2597 int i;
2598
2599 did_set_spelltab = FALSE;
2600 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002601 if (enc_dbcs)
2602 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002603 // DBCS: assume double-wide characters are word characters.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002604 for (i = 128; i <= 255; ++i)
2605 if (MB_BYTE2LEN(i) == 2)
2606 spelltab.st_isw[i] = TRUE;
2607 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002608 else if (enc_utf8)
2609 {
2610 for (i = 128; i < 256; ++i)
2611 {
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02002612 int f = utf_fold(i);
2613 int u = utf_toupper(i);
2614
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002615 spelltab.st_isu[i] = utf_isupper(i);
2616 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002617 // The folded/upper-cased value is different between latin1 and
2618 // utf8 for 0xb5, causing E763 for no good reason. Use the latin1
2619 // value for utf-8 to avoid this.
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02002620 spelltab.st_fold[i] = (f < 256) ? f : i;
2621 spelltab.st_upper[i] = (u < 256) ? u : i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002622 }
2623 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002624 else
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002625 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002626 // Rough guess: use locale-dependent library functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002627 for (i = 128; i < 256; ++i)
2628 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002629 if (MB_ISUPPER(i))
2630 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002631 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002632 spelltab.st_isu[i] = TRUE;
2633 spelltab.st_fold[i] = MB_TOLOWER(i);
2634 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002635 else if (MB_ISLOWER(i))
2636 {
2637 spelltab.st_isw[i] = TRUE;
2638 spelltab.st_upper[i] = MB_TOUPPER(i);
2639 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002640 }
2641 }
2642}
2643
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002644
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002645/*
Bram Moolenaarea408852005-06-25 22:49:46 +00002646 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002647 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00002648 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002649 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00002650 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02002651 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002652spell_iswordp(
2653 char_u *p,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002654 win_T *wp) // buffer used
Bram Moolenaarea408852005-06-25 22:49:46 +00002655{
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002656 char_u *s;
2657 int l;
2658 int c;
2659
2660 if (has_mbyte)
2661 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002662 l = mb_ptr2len(p);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002663 s = p;
2664 if (l == 1)
2665 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002666 // be quick for ASCII
Bram Moolenaar860cae12010-06-05 23:22:07 +02002667 if (wp->w_s->b_spell_ismw[*p])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002668 s = p + 1; // skip a mid-word character
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002669 }
2670 else
2671 {
2672 c = mb_ptr2char(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002673 if (c < 256 ? wp->w_s->b_spell_ismw[c]
2674 : (wp->w_s->b_spell_ismw_mb != NULL
2675 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002676 s = p + l;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002677 }
2678
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002679 c = mb_ptr2char(s);
2680 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002681 return spell_mb_isword_class(mb_get_class(s), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002682 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002683 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002684
Bram Moolenaar860cae12010-06-05 23:22:07 +02002685 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002686}
2687
2688/*
2689 * Return TRUE if "p" points to a word character.
2690 * Unlike spell_iswordp() this doesn't check for "midword" characters.
2691 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02002692 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002693spell_iswordp_nmw(char_u *p, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002694{
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002695 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002696
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002697 if (has_mbyte)
2698 {
2699 c = mb_ptr2char(p);
2700 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002701 return spell_mb_isword_class(mb_get_class(p), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002702 return spelltab.st_isw[c];
2703 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002704 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00002705}
2706
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002707/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002708 * Return TRUE if word class indicates a word character.
2709 * Only for characters above 255.
2710 * Unicode subscript and superscript are not considered word characters.
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002711 * See also dbcs_class() and utf_class() in mbyte.c.
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002712 */
2713 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002714spell_mb_isword_class(int cl, win_T *wp)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002715{
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002716 if (wp->w_s->b_cjk)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002717 // East Asian characters are not considered word characters.
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002718 return cl == 2 || cl == 0x2800;
Bram Moolenaar06e63772019-07-19 23:04:34 +02002719 return cl >= 2 && cl != 0x2070 && cl != 0x2080 && cl != 3;
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002720}
2721
2722/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002723 * Return TRUE if "p" points to a word character.
2724 * Wide version of spell_iswordp().
2725 */
2726 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002727spell_iswordp_w(int *p, win_T *wp)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002728{
2729 int *s;
2730
Bram Moolenaar860cae12010-06-05 23:22:07 +02002731 if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
2732 : (wp->w_s->b_spell_ismw_mb != NULL
2733 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002734 s = p + 1;
2735 else
2736 s = p;
2737
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002738 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002739 {
2740 if (enc_utf8)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002741 return spell_mb_isword_class(utf_class(*s), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002742 if (enc_dbcs)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002743 return spell_mb_isword_class(
2744 dbcs_class((unsigned)*s >> 8, *s & 0xff), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002745 return 0;
2746 }
2747 return spelltab.st_isw[*s];
2748}
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002749
Bram Moolenaarea408852005-06-25 22:49:46 +00002750/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002751 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
2752 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002753 * When using a multi-byte 'encoding' the length may change!
2754 * Returns FAIL when something wrong.
2755 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002756 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002757spell_casefold(
Bram Moolenaar4f135272021-06-11 19:07:40 +02002758 win_T *wp,
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002759 char_u *str,
2760 int len,
2761 char_u *buf,
2762 int buflen)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002763{
2764 int i;
2765
2766 if (len >= buflen)
2767 {
2768 buf[0] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002769 return FAIL; // result will not fit
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002770 }
2771
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002772 if (has_mbyte)
2773 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002774 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002775 char_u *p;
2776 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002777
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002778 // Fold one character at a time.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002779 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002780 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002781 if (outi + MB_MAXBYTES > buflen)
2782 {
2783 buf[outi] = NUL;
2784 return FAIL;
2785 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002786 c = mb_cptr2char_adv(&p);
Bram Moolenaar4f135272021-06-11 19:07:40 +02002787
2788 // Exception: greek capital sigma 0x03A3 folds to 0x03C3, except
2789 // when it is the last character in a word, then it folds to
2790 // 0x03C2.
2791 if (c == 0x03a3 || c == 0x03c2)
2792 {
2793 if (p == str + len || !spell_iswordp(p, wp))
2794 c = 0x03c2;
2795 else
2796 c = 0x03c3;
2797 }
2798 else
2799 c = SPELL_TOFOLD(c);
2800
2801 outi += mb_char2bytes(c, buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002802 }
2803 buf[outi] = NUL;
2804 }
2805 else
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002806 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002807 // Be quick for non-multibyte encodings.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002808 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002809 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002810 buf[i] = NUL;
2811 }
2812
2813 return OK;
2814}
2815
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002816/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002817 * Check if the word at line "lnum" column "col" is required to start with a
Luuk van Baal2ac64972023-05-25 17:14:42 +01002818 * capital. This uses 'spellcapcheck' of the buffer in window "wp".
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002819 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02002820 int
Luuk van Baal2ac64972023-05-25 17:14:42 +01002821check_need_cap(win_T *wp, linenr_T lnum, colnr_T col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002822{
Luuk van Baal2ac64972023-05-25 17:14:42 +01002823 if (wp->w_s->b_cap_prog == NULL)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002824 return FALSE;
2825
Luuk van Baal2ac64972023-05-25 17:14:42 +01002826 int need_cap = FALSE;
2827 char_u *line = col ? ml_get_buf(wp->w_buffer, lnum, FALSE) : NULL;
2828 char_u *line_copy = NULL;
2829 colnr_T endcol = 0;
2830
2831 if (col == 0 || getwhitecols(line) >= col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002832 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002833 // At start of line, check if previous line is empty or sentence
2834 // ends there.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002835 if (lnum == 1)
2836 need_cap = TRUE;
2837 else
2838 {
Luuk van Baal2ac64972023-05-25 17:14:42 +01002839 line = ml_get_buf(wp->w_buffer, lnum - 1, FALSE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002840 if (*skipwhite(line) == NUL)
2841 need_cap = TRUE;
2842 else
2843 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002844 // Append a space in place of the line break.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002845 line_copy = concat_str(line, (char_u *)" ");
Luuk van Baal2ac64972023-05-25 17:14:42 +01002846 if (line_copy == NULL)
2847 return FALSE;
2848
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002849 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002850 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002851 }
2852 }
2853 }
2854 else
2855 endcol = col;
2856
2857 if (endcol > 0)
2858 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002859 // Check if sentence ends before the bad word.
Luuk van Baal2ac64972023-05-25 17:14:42 +01002860 regmatch_T regmatch;
2861 regmatch.regprog = wp->w_s->b_cap_prog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002862 regmatch.rm_ic = FALSE;
Luuk van Baal2ac64972023-05-25 17:14:42 +01002863 char_u *p = line + endcol;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002864 for (;;)
2865 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002866 MB_PTR_BACK(line, p);
Luuk van Baal2ac64972023-05-25 17:14:42 +01002867 if (p == line || spell_iswordp_nmw(p, wp))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002868 break;
2869 if (vim_regexec(&regmatch, p, 0)
2870 && regmatch.endp[0] == line + endcol)
2871 {
2872 need_cap = TRUE;
2873 break;
2874 }
2875 }
Luuk van Baal2ac64972023-05-25 17:14:42 +01002876 wp->w_s->b_cap_prog = regmatch.regprog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002877 }
2878
2879 vim_free(line_copy);
2880
2881 return need_cap;
2882}
2883
2884
2885/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002886 * ":spellrepall"
2887 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002888 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002889ex_spellrepall(exarg_T *eap UNUSED)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002890{
2891 pos_T pos = curwin->w_cursor;
2892 char_u *frompat;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002893 char_u *line;
2894 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002895 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002896 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002897
2898 if (repl_from == NULL || repl_to == NULL)
2899 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002900 emsg(_(e_no_previous_spell_replacement));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002901 return;
2902 }
zeertzjq59f70382023-06-06 15:59:59 +01002903 size_t repl_from_len = STRLEN(repl_from);
2904 size_t repl_to_len = STRLEN(repl_to);
2905 int addlen = (int)(repl_to_len - repl_from_len);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002906
zeertzjq59f70382023-06-06 15:59:59 +01002907 frompat = alloc(repl_from_len + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002908 if (frompat == NULL)
2909 return;
2910 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
2911 p_ws = FALSE;
2912
Bram Moolenaar5195e452005-08-19 20:32:47 +00002913 sub_nsubs = 0;
2914 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002915 curwin->w_cursor.lnum = 0;
2916 while (!got_int)
2917 {
Bram Moolenaarc036e872020-02-21 21:30:52 +01002918 if (do_search(NULL, '/', '/', frompat, 1L, SEARCH_KEEP, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002919 || u_save_cursor() == FAIL)
2920 break;
2921
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002922 // Only replace when the right word isn't there yet. This happens
2923 // when changing "etc" to "etc.".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002924 line = ml_get_curline();
2925 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
zeertzjq59f70382023-06-06 15:59:59 +01002926 repl_to, repl_to_len) != 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002927 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002928 p = alloc(STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002929 if (p == NULL)
2930 break;
2931 mch_memmove(p, line, curwin->w_cursor.col);
2932 STRCPY(p + curwin->w_cursor.col, repl_to);
zeertzjq59f70382023-06-06 15:59:59 +01002933 STRCAT(p, line + curwin->w_cursor.col + repl_from_len);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002934 ml_replace(curwin->w_cursor.lnum, p, FALSE);
2935 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Martin Tournoijba43e762022-10-13 22:12:15 +01002936#if defined(FEAT_PROP_POPUP)
LemonBoyb7a70122022-05-13 12:41:50 +01002937 if (curbuf->b_has_textprop && addlen != 0)
2938 adjust_prop_columns(curwin->w_cursor.lnum,
2939 curwin->w_cursor.col, addlen, APC_SUBSTITUTE);
Martin Tournoijba43e762022-10-13 22:12:15 +01002940#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002941
2942 if (curwin->w_cursor.lnum != prev_lnum)
2943 {
2944 ++sub_nlines;
2945 prev_lnum = curwin->w_cursor.lnum;
2946 }
2947 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002948 }
zeertzjq59f70382023-06-06 15:59:59 +01002949 curwin->w_cursor.col += (colnr_T)repl_to_len;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002950 }
2951
2952 p_ws = save_ws;
2953 curwin->w_cursor = pos;
2954 vim_free(frompat);
2955
Bram Moolenaar5195e452005-08-19 20:32:47 +00002956 if (sub_nsubs == 0)
Bram Moolenaar677658a2022-01-05 16:09:06 +00002957 semsg(_(e_not_found_str), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002958 else
2959 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002960}
2961
2962/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002963 * Make a copy of "word", with the first letter upper or lower cased, to
2964 * "wcopy[MAXWLEN]". "word" must not be empty.
2965 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002966 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002967 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002968onecap_copy(
2969 char_u *word,
2970 char_u *wcopy,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002971 int upper) // TRUE: first letter made upper case
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002972{
2973 char_u *p;
2974 int c;
2975 int l;
2976
2977 p = word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002978 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002979 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002980 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002981 c = *p++;
2982 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002983 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002984 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002985 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002986 if (has_mbyte)
2987 l = mb_char2bytes(c, wcopy);
2988 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002989 {
2990 l = 1;
2991 wcopy[0] = c;
2992 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002993 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002994}
2995
2996/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002997 * Make a copy of "word" with all the letters upper cased into
2998 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002999 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02003000 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003001allcap_copy(char_u *word, char_u *wcopy)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003002{
3003 char_u *s;
3004 char_u *d;
3005 int c;
3006
3007 d = wcopy;
3008 for (s = word; *s != NUL; )
3009 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003010 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003011 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003012 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003013 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +00003014
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003015 // We only change 0xdf to SS when we are certain latin1 is used. It
3016 // would cause weird errors in other 8-bit encodings.
Bram Moolenaar78622822005-08-23 21:00:13 +00003017 if (enc_latin1like && c == 0xdf)
3018 {
3019 c = 'S';
3020 if (d - wcopy >= MAXWLEN - 1)
3021 break;
3022 *d++ = c;
3023 }
3024 else
Bram Moolenaar78622822005-08-23 21:00:13 +00003025 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003026
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003027 if (has_mbyte)
3028 {
3029 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
3030 break;
3031 d += mb_char2bytes(c, d);
3032 }
3033 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003034 {
3035 if (d - wcopy >= MAXWLEN - 1)
3036 break;
3037 *d++ = c;
3038 }
3039 }
3040 *d = NUL;
3041}
3042
3043/*
Bram Moolenaar53805d12005-08-01 07:08:33 +00003044 * Case-folding may change the number of bytes: Count nr of chars in
3045 * fword[flen] and return the byte length of that many chars in "word".
3046 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02003047 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003048nofold_len(char_u *fword, int flen, char_u *word)
Bram Moolenaar53805d12005-08-01 07:08:33 +00003049{
3050 char_u *p;
3051 int i = 0;
3052
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003053 for (p = fword; p < fword + flen; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00003054 ++i;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003055 for (p = word; i > 0; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00003056 --i;
3057 return (int)(p - word);
3058}
Bram Moolenaar53805d12005-08-01 07:08:33 +00003059
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003060/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003061 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003062 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02003063 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003064make_case_word(char_u *fword, char_u *cword, int flags)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003065{
3066 if (flags & WF_ALLCAP)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003067 // Make it all upper-case
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003068 allcap_copy(fword, cword);
3069 else if (flags & WF_ONECAP)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003070 // Make the first letter upper-case
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003071 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003072 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003073 // Use goodword as-is.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003074 STRCPY(cword, fword);
3075}
3076
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003077#if defined(FEAT_EVAL) || defined(PROTO)
3078/*
3079 * Soundfold a string, for soundfold().
3080 * Result is in allocated memory, NULL for an error.
3081 */
3082 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003083eval_soundfold(char_u *word)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003084{
3085 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003086 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003087 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003088
Bram Moolenaar860cae12010-06-05 23:22:07 +02003089 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003090 // Use the sound-folding of the first language that supports it.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003091 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003092 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003093 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003094 if (lp->lp_slang->sl_sal.ga_len > 0)
3095 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003096 // soundfold the word
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003097 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003098 return vim_strsave(sound);
3099 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003100 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003101
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003102 // No language with sound folding, return word as-is.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003103 return vim_strsave(word);
3104}
3105#endif
3106
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003107/*
3108 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +00003109 *
3110 * There are many ways to turn a word into a sound-a-like representation. The
3111 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
3112 * swedish name matching - survey and test of different algorithms" by Klas
3113 * Erikson.
3114 *
3115 * We support two methods:
3116 * 1. SOFOFROM/SOFOTO do a simple character mapping.
3117 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003118 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003119 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003120spell_soundfold(
3121 slang_T *slang,
3122 char_u *inword,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003123 int folded, // "inword" is already case-folded
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003124 char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003125{
3126 char_u fword[MAXWLEN];
3127 char_u *word;
3128
3129 if (slang->sl_sofo)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003130 // SOFOFROM and SOFOTO used
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003131 spell_soundfold_sofo(slang, inword, res);
3132 else
3133 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003134 // SAL items used. Requires the word to be case-folded.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003135 if (folded)
3136 word = inword;
3137 else
3138 {
Bram Moolenaar4f135272021-06-11 19:07:40 +02003139 (void)spell_casefold(curwin,
3140 inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003141 word = fword;
3142 }
3143
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003144 if (has_mbyte)
3145 spell_soundfold_wsal(slang, word, res);
3146 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003147 spell_soundfold_sal(slang, word, res);
3148 }
3149}
3150
3151/*
3152 * Perform sound folding of "inword" into "res" according to SOFOFROM and
3153 * SOFOTO lines.
3154 */
3155 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003156spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003157{
3158 char_u *s;
3159 int ri = 0;
3160 int c;
3161
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003162 if (has_mbyte)
3163 {
3164 int prevc = 0;
3165 int *ip;
3166
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003167 // The sl_sal_first[] table contains the translation for chars up to
3168 // 255, sl_sal the rest.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003169 for (s = inword; *s != NUL; )
3170 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003171 c = mb_cptr2char_adv(&s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003172 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003173 c = ' ';
3174 else if (c < 256)
3175 c = slang->sl_sal_first[c];
3176 else
3177 {
3178 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003179 if (ip == NULL) // empty list, can't match
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003180 c = NUL;
3181 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003182 for (;;) // find "c" in the list
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003183 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003184 if (*ip == 0) // not found
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003185 {
3186 c = NUL;
3187 break;
3188 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003189 if (*ip == c) // match!
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003190 {
3191 c = ip[1];
3192 break;
3193 }
3194 ip += 2;
3195 }
3196 }
3197
3198 if (c != NUL && c != prevc)
3199 {
3200 ri += mb_char2bytes(c, res + ri);
3201 if (ri + MB_MAXBYTES > MAXWLEN)
3202 break;
3203 prevc = c;
3204 }
3205 }
3206 }
3207 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003208 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003209 // The sl_sal_first[] table contains the translation.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003210 for (s = inword; (c = *s) != NUL; ++s)
3211 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003212 if (VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003213 c = ' ';
3214 else
3215 c = slang->sl_sal_first[c];
3216 if (c != NUL && (ri == 0 || res[ri - 1] != c))
3217 res[ri++] = c;
3218 }
3219 }
3220
3221 res[ri] = NUL;
3222}
3223
3224 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003225spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003226{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003227 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003228 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003229 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003230 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003231 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003232 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003233 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003234 int n, k = 0;
3235 int z0;
3236 int k0;
3237 int n0;
3238 int c;
3239 int pri;
3240 int p0 = -333;
3241 int c0;
3242
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003243 // Remove accents, if wanted. We actually remove all non-word characters.
3244 // But keep white space. We need a copy, the word may be changed here.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003245 if (slang->sl_rem_accents)
3246 {
3247 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003248 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003249 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003250 if (VIM_ISWHITE(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003251 {
3252 *t++ = ' ';
3253 s = skipwhite(s);
3254 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003255 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003256 {
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003257 if (spell_iswordp_nmw(s, curwin))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003258 *t++ = *s;
3259 ++s;
3260 }
3261 }
3262 *t = NUL;
3263 }
3264 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003265 vim_strncpy(word, s, MAXWLEN - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003266
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003267 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003268
3269 /*
3270 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003271 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003272 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003273 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003274 while ((c = word[i]) != NUL)
3275 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003276 // Start with the first rule that has the character in the word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003277 n = slang->sl_sal_first[c];
3278 z0 = 0;
3279
3280 if (n >= 0)
3281 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003282 // check all rules for the same letter
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003283 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003284 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003285 // Quickly skip entries that don't match the word. Most
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003286 // entries are less than three chars, optimize for that.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003287 k = smp[n].sm_leadlen;
3288 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003289 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003290 if (word[i + 1] != s[1])
3291 continue;
3292 if (k > 2)
3293 {
3294 for (j = 2; j < k; ++j)
3295 if (word[i + j] != s[j])
3296 break;
3297 if (j < k)
3298 continue;
3299 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003300 }
3301
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003302 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003303 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003304 // Check for match with one of the chars in "sm_oneof".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003305 while (*pf != NUL && *pf != word[i + k])
3306 ++pf;
3307 if (*pf == NUL)
3308 continue;
3309 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003310 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003311 s = smp[n].sm_rules;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003312 pri = 5; // default priority
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003313
3314 p0 = *s;
3315 k0 = k;
3316 while (*s == '-' && k > 1)
3317 {
3318 k--;
3319 s++;
3320 }
3321 if (*s == '<')
3322 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003323 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003324 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003325 // determine priority
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003326 pri = *s - '0';
3327 s++;
3328 }
3329 if (*s == '^' && *(s + 1) == '^')
3330 s++;
3331
3332 if (*s == NUL
3333 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003334 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02003335 || spell_iswordp(word + i - 1, curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003336 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003337 || (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003338 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02003339 && spell_iswordp(word + i - 1, curwin)
3340 && (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003341 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003342 // search for followup rules, if:
3343 // followup and k > 1 and NO '-' in searchstring
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003344 c0 = word[i + k - 1];
3345 n0 = slang->sl_sal_first[c0];
3346
3347 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003348 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003349 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003350 // test follow-up rule for "word[i + k]"
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003351 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003352 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003353 // Quickly skip entries that don't match the word.
3354 //
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003355 k0 = smp[n0].sm_leadlen;
3356 if (k0 > 1)
3357 {
3358 if (word[i + k] != s[1])
3359 continue;
3360 if (k0 > 2)
3361 {
3362 pf = word + i + k + 1;
3363 for (j = 2; j < k0; ++j)
3364 if (*pf++ != s[j])
3365 break;
3366 if (j < k0)
3367 continue;
3368 }
3369 }
3370 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003371
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003372 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003373 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003374 // Check for match with one of the chars in
3375 // "sm_oneof".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003376 while (*pf != NUL && *pf != word[i + k0])
3377 ++pf;
3378 if (*pf == NUL)
3379 continue;
3380 ++k0;
3381 }
3382
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003383 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003384 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003385 while (*s == '-')
3386 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003387 // "k0" gets NOT reduced because
3388 // "if (k0 == k)"
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003389 s++;
3390 }
3391 if (*s == '<')
3392 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003393 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003394 {
3395 p0 = *s - '0';
3396 s++;
3397 }
3398
3399 if (*s == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003400 // *s == '^' cuts
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003401 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003402 && !spell_iswordp(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02003403 curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003404 {
3405 if (k0 == k)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003406 // this is just a piece of the string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003407 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003408
3409 if (p0 < pri)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003410 // priority too low
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003411 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003412 // rule fits; stop search
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003413 break;
3414 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003415 }
3416
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003417 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003418 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003419 }
3420
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003421 // replace string
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003422 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003423 if (s == NULL)
3424 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003425 pf = smp[n].sm_rules;
3426 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003427 if (p0 == 1 && z == 0)
3428 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003429 // rule with '<' is used
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003430 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
3431 || res[reslen - 1] == *s))
3432 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003433 z0 = 1;
3434 z = 1;
3435 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003436 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003437 {
3438 word[i + k0] = *s;
3439 k0++;
3440 s++;
3441 }
3442 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +00003443 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003444
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003445 // new "actual letter"
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003446 c = word[i];
3447 }
3448 else
3449 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003450 // no '<' rule used
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003451 i += k - 1;
3452 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003453 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003454 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003455 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003456 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003457 s++;
3458 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003459 // new "actual letter"
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003460 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003461 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003462 {
3463 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003464 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003465 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003466 i = 0;
3467 z0 = 1;
3468 }
3469 }
3470 break;
3471 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003472 }
3473 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01003474 else if (VIM_ISWHITE(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003475 {
3476 c = ' ';
3477 k = 1;
3478 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003479
3480 if (z0 == 0)
3481 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003482 if (k && !p0 && reslen < MAXWLEN && c != NUL
3483 && (!slang->sl_collapse || reslen == 0
3484 || res[reslen - 1] != c))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003485 // condense only double letters
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003486 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003487
3488 i++;
3489 z = 0;
3490 k = 0;
3491 }
3492 }
3493
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003494 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003495}
3496
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003497/*
3498 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
3499 * Multi-byte version of spell_soundfold().
3500 */
3501 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003502spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003503{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003504 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003505 int word[MAXWLEN];
3506 int wres[MAXWLEN];
3507 int l;
3508 char_u *s;
3509 int *ws;
3510 char_u *t;
3511 int *pf;
3512 int i, j, z;
3513 int reslen;
3514 int n, k = 0;
3515 int z0;
3516 int k0;
3517 int n0;
3518 int c;
3519 int pri;
3520 int p0 = -333;
3521 int c0;
3522 int did_white = FALSE;
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003523 int wordlen;
3524
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003525
3526 /*
3527 * Convert the multi-byte string to a wide-character string.
3528 * Remove accents, if wanted. We actually remove all non-word characters.
3529 * But keep white space.
3530 */
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003531 wordlen = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003532 for (s = inword; *s != NUL; )
3533 {
3534 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003535 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003536 if (slang->sl_rem_accents)
3537 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003538 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003539 {
3540 if (did_white)
3541 continue;
3542 c = ' ';
3543 did_white = TRUE;
3544 }
3545 else
3546 {
3547 did_white = FALSE;
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003548 if (!spell_iswordp_nmw(t, curwin))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003549 continue;
3550 }
3551 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003552 word[wordlen++] = c;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003553 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003554 word[wordlen] = NUL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003555
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003556 /*
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003557 * This algorithm comes from Aspell phonet.cpp.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003558 * Converted from C++ to C. Added support for multi-byte chars.
3559 * Changed to keep spaces.
3560 */
3561 i = reslen = z = 0;
3562 while ((c = word[i]) != NUL)
3563 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003564 // Start with the first rule that has the character in the word.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003565 n = slang->sl_sal_first[c & 0xff];
3566 z0 = 0;
3567
3568 if (n >= 0)
3569 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003570 // Check all rules for the same index byte.
3571 // If c is 0x300 need extra check for the end of the array, as
3572 // (c & 0xff) is NUL.
Bram Moolenaar95e85792010-08-01 15:37:02 +02003573 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff)
3574 && ws[0] != NUL; ++n)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003575 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003576 // Quickly skip entries that don't match the word. Most
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003577 // entries are less than three chars, optimize for that.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003578 if (c != ws[0])
3579 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003580 k = smp[n].sm_leadlen;
3581 if (k > 1)
3582 {
3583 if (word[i + 1] != ws[1])
3584 continue;
3585 if (k > 2)
3586 {
3587 for (j = 2; j < k; ++j)
3588 if (word[i + j] != ws[j])
3589 break;
3590 if (j < k)
3591 continue;
3592 }
3593 }
3594
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003595 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003596 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003597 // Check for match with one of the chars in "sm_oneof".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003598 while (*pf != NUL && *pf != word[i + k])
3599 ++pf;
3600 if (*pf == NUL)
3601 continue;
3602 ++k;
3603 }
3604 s = smp[n].sm_rules;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003605 pri = 5; // default priority
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003606
3607 p0 = *s;
3608 k0 = k;
3609 while (*s == '-' && k > 1)
3610 {
3611 k--;
3612 s++;
3613 }
3614 if (*s == '<')
3615 s++;
3616 if (VIM_ISDIGIT(*s))
3617 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003618 // determine priority
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003619 pri = *s - '0';
3620 s++;
3621 }
3622 if (*s == '^' && *(s + 1) == '^')
3623 s++;
3624
3625 if (*s == NUL
3626 || (*s == '^'
3627 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02003628 || spell_iswordp_w(word + i - 1, curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003629 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003630 || (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003631 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02003632 && spell_iswordp_w(word + i - 1, curwin)
3633 && (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003634 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003635 // search for followup rules, if:
3636 // followup and k > 1 and NO '-' in searchstring
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003637 c0 = word[i + k - 1];
3638 n0 = slang->sl_sal_first[c0 & 0xff];
3639
3640 if (slang->sl_followup && k > 1 && n0 >= 0
3641 && p0 != '-' && word[i + k] != NUL)
3642 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003643 // Test follow-up rule for "word[i + k]"; loop over
3644 // all entries with the same index byte.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003645 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
3646 == (c0 & 0xff); ++n0)
3647 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003648 // Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003649 if (c0 != ws[0])
3650 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003651 k0 = smp[n0].sm_leadlen;
3652 if (k0 > 1)
3653 {
3654 if (word[i + k] != ws[1])
3655 continue;
3656 if (k0 > 2)
3657 {
3658 pf = word + i + k + 1;
3659 for (j = 2; j < k0; ++j)
3660 if (*pf++ != ws[j])
3661 break;
3662 if (j < k0)
3663 continue;
3664 }
3665 }
3666 k0 += k - 1;
3667
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003668 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003669 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003670 // Check for match with one of the chars in
3671 // "sm_oneof".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003672 while (*pf != NUL && *pf != word[i + k0])
3673 ++pf;
3674 if (*pf == NUL)
3675 continue;
3676 ++k0;
3677 }
3678
3679 p0 = 5;
3680 s = smp[n0].sm_rules;
3681 while (*s == '-')
3682 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003683 // "k0" gets NOT reduced because
3684 // "if (k0 == k)"
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003685 s++;
3686 }
3687 if (*s == '<')
3688 s++;
3689 if (VIM_ISDIGIT(*s))
3690 {
3691 p0 = *s - '0';
3692 s++;
3693 }
3694
3695 if (*s == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003696 // *s == '^' cuts
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003697 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003698 && !spell_iswordp_w(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02003699 curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003700 {
3701 if (k0 == k)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003702 // this is just a piece of the string
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003703 continue;
3704
3705 if (p0 < pri)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003706 // priority too low
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003707 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003708 // rule fits; stop search
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003709 break;
3710 }
3711 }
3712
3713 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
3714 == (c0 & 0xff))
3715 continue;
3716 }
3717
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003718 // replace string
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003719 ws = smp[n].sm_to_w;
3720 s = smp[n].sm_rules;
3721 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
3722 if (p0 == 1 && z == 0)
3723 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003724 // rule with '<' is used
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003725 if (reslen > 0 && ws != NULL && *ws != NUL
3726 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003727 || wres[reslen - 1] == *ws))
3728 reslen--;
3729 z0 = 1;
3730 z = 1;
3731 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003732 if (ws != NULL)
3733 while (*ws != NUL && word[i + k0] != NUL)
3734 {
3735 word[i + k0] = *ws;
3736 k0++;
3737 ws++;
3738 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003739 if (k > k0)
3740 mch_memmove(word + i + k0, word + i + k,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003741 sizeof(int) * (wordlen - (i + k) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003742
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003743 // new "actual letter"
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003744 c = word[i];
3745 }
3746 else
3747 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003748 // no '<' rule used
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003749 i += k - 1;
3750 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003751 if (ws != NULL)
3752 while (*ws != NUL && ws[1] != NUL
3753 && reslen < MAXWLEN)
3754 {
3755 if (reslen == 0 || wres[reslen - 1] != *ws)
3756 wres[reslen++] = *ws;
3757 ws++;
3758 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003759 // new "actual letter"
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003760 if (ws == NULL)
3761 c = NUL;
3762 else
3763 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003764 if (strstr((char *)s, "^^") != NULL)
3765 {
3766 if (c != NUL)
3767 wres[reslen++] = c;
3768 mch_memmove(word, word + i + 1,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003769 sizeof(int) * (wordlen - (i + 1) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003770 i = 0;
3771 z0 = 1;
3772 }
3773 }
3774 break;
3775 }
3776 }
3777 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01003778 else if (VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003779 {
3780 c = ' ';
3781 k = 1;
3782 }
3783
3784 if (z0 == 0)
3785 {
3786 if (k && !p0 && reslen < MAXWLEN && c != NUL
3787 && (!slang->sl_collapse || reslen == 0
3788 || wres[reslen - 1] != c))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003789 // condense only double letters
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003790 wres[reslen++] = c;
3791
3792 i++;
3793 z = 0;
3794 k = 0;
3795 }
3796 }
3797
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003798 // Convert wide characters in "wres" to a multi-byte string in "res".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003799 l = 0;
3800 for (n = 0; n < reslen; ++n)
3801 {
3802 l += mb_char2bytes(wres[n], res + l);
3803 if (l + MB_MAXBYTES > MAXWLEN)
3804 break;
3805 }
3806 res[l] = NUL;
3807}
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003808
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003809/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003810 * ":spellinfo"
3811 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003812 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003813ex_spellinfo(exarg_T *eap UNUSED)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003814{
3815 int lpi;
3816 langp_T *lp;
3817 char_u *p;
3818
3819 if (no_spell_checking(curwin))
3820 return;
3821
3822 msg_start();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003823 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003824 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003825 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003826 msg_puts("file: ");
3827 msg_puts((char *)lp->lp_slang->sl_fname);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003828 msg_putchar('\n');
3829 p = lp->lp_slang->sl_info;
3830 if (p != NULL)
3831 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003832 msg_puts((char *)p);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003833 msg_putchar('\n');
3834 }
3835 }
3836 msg_end();
3837}
3838
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003839#define DUMPFLAG_KEEPCASE 1 // round 2: keep-case tree
3840#define DUMPFLAG_COUNT 2 // include word count
3841#define DUMPFLAG_ICASE 4 // ignore case when finding matches
3842#define DUMPFLAG_ONECAP 8 // pattern starts with capital
3843#define DUMPFLAG_ALLCAP 16 // pattern is all capitals
Bram Moolenaar4770d092006-01-12 23:22:24 +00003844
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003845/*
3846 * ":spelldump"
3847 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003848 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003849ex_spelldump(exarg_T *eap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003850{
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003851 char_u *spl;
3852 long dummy;
3853
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003854 if (no_spell_checking(curwin))
3855 return;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003856 (void)get_option_value((char_u*)"spl", &dummy, &spl, NULL, OPT_LOCAL);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003857
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003858 // Create a new empty buffer in a new window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003859 do_cmdline_cmd((char_u *)"new");
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003860
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003861 // enable spelling locally in the new window
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003862 set_option_value_give_err((char_u*)"spell", TRUE, (char_u*)"", OPT_LOCAL);
3863 set_option_value_give_err((char_u*)"spl", dummy, spl, OPT_LOCAL);
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003864 vim_free(spl);
3865
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003866 if (!BUFEMPTY())
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003867 return;
3868
Bram Moolenaar860cae12010-06-05 23:22:07 +02003869 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003870
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003871 // Delete the empty line that we started with.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003872 if (curbuf->b_ml.ml_line_count > 1)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003873 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003874
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003875 redraw_later(UPD_NOT_VALID);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003876}
3877
3878/*
3879 * Go through all possible words and:
3880 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
3881 * "ic" and "dir" are not used.
3882 * 2. When "pat" is not NULL: add matching words to insert mode completion.
3883 */
3884 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003885spell_dump_compl(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003886 char_u *pat, // leading part of the word
3887 int ic, // ignore case
3888 int *dir, // direction for adding matches
3889 int dumpflags_arg) // DUMPFLAG_*
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003890{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003891 langp_T *lp;
3892 slang_T *slang;
3893 idx_T arridx[MAXWLEN];
3894 int curi[MAXWLEN];
3895 char_u word[MAXWLEN];
3896 int c;
3897 char_u *byts;
3898 idx_T *idxs;
3899 linenr_T lnum = 0;
3900 int round;
3901 int depth;
3902 int n;
3903 int flags;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003904 char_u *region_names = NULL; // region names being used
3905 int do_region = TRUE; // dump region names and numbers
Bram Moolenaar7887d882005-07-01 22:33:52 +00003906 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003907 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003908 int dumpflags = dumpflags_arg;
3909 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003910
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003911 // When ignoring case or when the pattern starts with capital pass this on
3912 // to dump_word().
Bram Moolenaard0131a82006-03-04 21:46:13 +00003913 if (pat != NULL)
3914 {
3915 if (ic)
3916 dumpflags |= DUMPFLAG_ICASE;
3917 else
3918 {
3919 n = captype(pat, NULL);
3920 if (n == WF_ONECAP)
3921 dumpflags |= DUMPFLAG_ONECAP;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01003922 else if (n == WF_ALLCAP && (int)STRLEN(pat) > mb_ptr2len(pat))
Bram Moolenaard0131a82006-03-04 21:46:13 +00003923 dumpflags |= DUMPFLAG_ALLCAP;
3924 }
3925 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003926
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003927 // Find out if we can support regions: All languages must support the same
3928 // regions or none at all.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003929 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003930 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003931 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003932 p = lp->lp_slang->sl_regions;
3933 if (p[0] != 0)
3934 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003935 if (region_names == NULL) // first language with regions
Bram Moolenaar7887d882005-07-01 22:33:52 +00003936 region_names = p;
3937 else if (STRCMP(region_names, p) != 0)
3938 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003939 do_region = FALSE; // region names are different
Bram Moolenaar7887d882005-07-01 22:33:52 +00003940 break;
3941 }
3942 }
3943 }
3944
LemonBoye98fb642023-08-15 23:07:55 +02003945 if (do_region && region_names != NULL && pat == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003946 {
LemonBoye98fb642023-08-15 23:07:55 +02003947 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
3948 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003949 }
3950 else
3951 do_region = FALSE;
3952
3953 /*
3954 * Loop over all files loaded for the entries in 'spelllang'.
3955 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003956 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003957 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003958 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003959 slang = lp->lp_slang;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003960 if (slang->sl_fbyts == NULL) // reloading failed
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003961 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003962
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003963 if (pat == NULL)
3964 {
3965 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
3966 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
3967 }
3968
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003969 // When matching with a pattern and there are no prefixes only use
3970 // parts of the tree that match "pat".
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003971 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003972 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003973 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003974 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003975
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003976 // round 1: case-folded tree
3977 // round 2: keep-case tree
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003978 for (round = 1; round <= 2; ++round)
3979 {
3980 if (round == 1)
3981 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003982 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003983 byts = slang->sl_fbyts;
3984 idxs = slang->sl_fidxs;
3985 }
3986 else
3987 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003988 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003989 byts = slang->sl_kbyts;
3990 idxs = slang->sl_kidxs;
3991 }
3992 if (byts == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003993 continue; // array is empty
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003994
3995 depth = 0;
3996 arridx[0] = 0;
3997 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003998 while (depth >= 0 && !got_int
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003999 && (pat == NULL || !ins_compl_interrupted()))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004000 {
4001 if (curi[depth] > byts[arridx[depth]])
4002 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004003 // Done all bytes at this node, go up one level.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004004 --depth;
4005 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02004006 ins_compl_check_keys(50, FALSE);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004007 }
4008 else
4009 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004010 // Do one more byte at this node.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004011 n = arridx[depth] + curi[depth];
4012 ++curi[depth];
4013 c = byts[n];
Bram Moolenaar54e5fed2022-07-04 13:37:07 +01004014 if (c == 0 || depth >= MAXWLEN - 1)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004015 {
Bram Moolenaar54e5fed2022-07-04 13:37:07 +01004016 // End of word or reached maximum length, deal with the
4017 // word.
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004018 // Don't use keep-case words in the fold-case tree,
4019 // they will appear in the keep-case tree.
4020 // Only use the word when the region matches.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004021 flags = (int)idxs[n];
4022 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004023 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +00004024 && (do_region
4025 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004026 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004027 & lp->lp_region) != 0))
4028 {
4029 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004030 if (!do_region)
4031 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004032
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004033 // Dump the basic word if there is no prefix or
4034 // when it's the first one.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004035 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004036 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004037 {
4038 dump_word(slang, word, pat, dir,
4039 dumpflags, flags, lnum);
4040 if (pat == NULL)
4041 ++lnum;
4042 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004043
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004044 // Apply the prefix, if there is one.
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004045 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004046 lnum = dump_prefixes(slang, word, pat, dir,
4047 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004048 }
4049 }
4050 else
4051 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004052 // Normal char, go one level deeper.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004053 word[depth++] = c;
4054 arridx[depth] = idxs[n];
4055 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004056
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004057 // Check if this character matches with the pattern.
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004058 // If not skip the whole tree below it.
4059 // Always ignore case here, dump_word() will check
4060 // proper case later. This isn't exactly right when
4061 // length changes for multi-byte characters with
4062 // ignore case...
Bram Moolenaard0131a82006-03-04 21:46:13 +00004063 if (depth <= patlen
4064 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004065 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004066 }
4067 }
4068 }
4069 }
4070 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004071}
4072
4073/*
4074 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004075 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004076 */
4077 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004078dump_word(
4079 slang_T *slang,
4080 char_u *word,
4081 char_u *pat,
4082 int *dir,
4083 int dumpflags,
4084 int wordflags,
4085 linenr_T lnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004086{
4087 int keepcap = FALSE;
4088 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004089 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004090 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +00004091 char_u badword[MAXWLEN + 10];
4092 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +00004093 int flags = wordflags;
4094
4095 if (dumpflags & DUMPFLAG_ONECAP)
4096 flags |= WF_ONECAP;
4097 if (dumpflags & DUMPFLAG_ALLCAP)
4098 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004099
Bram Moolenaar4770d092006-01-12 23:22:24 +00004100 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004101 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004102 // Need to fix case according to "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004103 make_case_word(word, cword, flags);
4104 p = cword;
4105 }
4106 else
4107 {
4108 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004109 if ((dumpflags & DUMPFLAG_KEEPCASE)
4110 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004111 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004112 keepcap = TRUE;
4113 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004114 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004115
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004116 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004117 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004118 // Add flags and regions after a slash.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004119 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004120 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004121 STRCPY(badword, p);
4122 STRCAT(badword, "/");
4123 if (keepcap)
4124 STRCAT(badword, "=");
4125 if (flags & WF_BANNED)
4126 STRCAT(badword, "!");
4127 else if (flags & WF_RARE)
4128 STRCAT(badword, "?");
4129 if (flags & WF_REGION)
4130 for (i = 0; i < 7; ++i)
4131 if (flags & (0x10000 << i))
4132 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
4133 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004134 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004135
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004136 if (dumpflags & DUMPFLAG_COUNT)
4137 {
4138 hashitem_T *hi;
4139
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004140 // Include the word count for ":spelldump!".
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004141 hi = hash_find(&slang->sl_wordcount, tw);
4142 if (!HASHITEM_EMPTY(hi))
4143 {
4144 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
4145 tw, HI2WC(hi)->wc_count);
4146 p = IObuff;
4147 }
4148 }
4149
4150 ml_append(lnum, p, (colnr_T)0, FALSE);
4151 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00004152 else if (((dumpflags & DUMPFLAG_ICASE)
4153 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
4154 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004155 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004156 p_ic, NULL, *dir, FALSE) == OK)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004157 // if dir was BACKWARD then honor it just once
Bram Moolenaard0131a82006-03-04 21:46:13 +00004158 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004159}
4160
4161/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004162 * For ":spelldump": Find matching prefixes for "word". Prepend each to
4163 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004164 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004165 * Return the updated line number.
4166 */
4167 static linenr_T
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004168dump_prefixes(
4169 slang_T *slang,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004170 char_u *word, // case-folded word
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004171 char_u *pat,
4172 int *dir,
4173 int dumpflags,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004174 int flags, // flags with prefix ID
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004175 linenr_T startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004176{
4177 idx_T arridx[MAXWLEN];
4178 int curi[MAXWLEN];
4179 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +00004180 char_u word_up[MAXWLEN];
4181 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004182 int c;
4183 char_u *byts;
4184 idx_T *idxs;
4185 linenr_T lnum = startlnum;
4186 int depth;
4187 int n;
4188 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004189 int i;
4190
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004191 // If the word starts with a lower-case letter make the word with an
4192 // upper-case letter in word_up[].
Bram Moolenaar53805d12005-08-01 07:08:33 +00004193 c = PTR2CHAR(word);
4194 if (SPELL_TOUPPER(c) != c)
4195 {
4196 onecap_copy(word, word_up, TRUE);
4197 has_word_up = TRUE;
4198 }
4199
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004200 byts = slang->sl_pbyts;
4201 idxs = slang->sl_pidxs;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004202 if (byts != NULL) // array not is empty
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004203 {
4204 /*
4205 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004206 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004207 */
4208 depth = 0;
4209 arridx[0] = 0;
4210 curi[0] = 1;
4211 while (depth >= 0 && !got_int)
4212 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004213 n = arridx[depth];
4214 len = byts[n];
4215 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004216 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004217 // Done all bytes at this node, go up one level.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004218 --depth;
4219 line_breakcheck();
4220 }
4221 else
4222 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004223 // Do one more byte at this node.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004224 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004225 ++curi[depth];
4226 c = byts[n];
4227 if (c == 0)
4228 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004229 // End of prefix, find out how many IDs there are.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004230 for (i = 1; i < len; ++i)
4231 if (byts[n + i] != 0)
4232 break;
4233 curi[depth] += i - 1;
4234
Bram Moolenaar53805d12005-08-01 07:08:33 +00004235 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
4236 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004237 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004238 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004239 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004240 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004241 : flags, lnum);
4242 if (lnum != 0)
4243 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004244 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00004245
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004246 // Check for prefix that matches the word when the
4247 // first letter is upper-case, but only if the prefix has
4248 // a condition.
Bram Moolenaar53805d12005-08-01 07:08:33 +00004249 if (has_word_up)
4250 {
4251 c = valid_word_prefix(i, n, flags, word_up, slang,
4252 TRUE);
4253 if (c != 0)
4254 {
4255 vim_strncpy(prefix + depth, word_up,
4256 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004257 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004258 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004259 : flags, lnum);
4260 if (lnum != 0)
4261 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004262 }
4263 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004264 }
4265 else
4266 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004267 // Normal char, go one level deeper.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004268 prefix[depth++] = c;
4269 arridx[depth] = idxs[n];
4270 curi[depth] = 1;
4271 }
4272 }
4273 }
4274 }
4275
4276 return lnum;
4277}
4278
Bram Moolenaar95529562005-08-25 21:21:38 +00004279/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004280 * Move "p" to the end of word "start".
4281 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +00004282 */
4283 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004284spell_to_word_end(char_u *start, win_T *win)
Bram Moolenaar95529562005-08-25 21:21:38 +00004285{
4286 char_u *p = start;
4287
Bram Moolenaar860cae12010-06-05 23:22:07 +02004288 while (*p != NUL && spell_iswordp(p, win))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004289 MB_PTR_ADV(p);
Bram Moolenaar95529562005-08-25 21:21:38 +00004290 return p;
4291}
4292
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004293/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004294 * For Insert mode completion CTRL-X s:
4295 * Find start of the word in front of column "startcol".
4296 * We don't check if it is badly spelled, with completion we can only change
4297 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004298 * Returns the column number of the word.
4299 */
4300 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004301spell_word_start(int startcol)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004302{
4303 char_u *line;
4304 char_u *p;
4305 int col = 0;
4306
Bram Moolenaar95529562005-08-25 21:21:38 +00004307 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004308 return startcol;
4309
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004310 // Find a word character before "startcol".
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004311 line = ml_get_curline();
4312 for (p = line + startcol; p > line; )
4313 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004314 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004315 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004316 break;
4317 }
4318
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004319 // Go back to start of the word.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004320 while (p > line)
4321 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004322 col = (int)(p - line);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004323 MB_PTR_BACK(line, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004324 if (!spell_iswordp(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004325 break;
4326 col = 0;
4327 }
4328
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004329 return col;
4330}
4331
4332/*
Bram Moolenaar4effc802005-09-30 21:12:02 +00004333 * Need to check for 'spellcapcheck' now, the word is removed before
4334 * expand_spelling() is called. Therefore the ugly global variable.
4335 */
4336static int spell_expand_need_cap;
4337
4338 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004339spell_expand_check_cap(colnr_T col)
Bram Moolenaar4effc802005-09-30 21:12:02 +00004340{
Luuk van Baal2ac64972023-05-25 17:14:42 +01004341 spell_expand_need_cap = check_need_cap(curwin, curwin->w_cursor.lnum, col);
Bram Moolenaar4effc802005-09-30 21:12:02 +00004342}
4343
4344/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004345 * Get list of spelling suggestions.
4346 * Used for Insert mode completion CTRL-X ?.
4347 * Returns the number of matches. The matches are in "matchp[]", array of
4348 * allocated strings.
4349 */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004350 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004351expand_spelling(
4352 linenr_T lnum UNUSED,
4353 char_u *pat,
4354 char_u ***matchp)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004355{
4356 garray_T ga;
4357
Bram Moolenaar4770d092006-01-12 23:22:24 +00004358 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004359 *matchp = ga.ga_data;
4360 return ga.ga_len;
4361}
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004362
Bram Moolenaare677df82019-09-02 22:31:11 +02004363/*
Bram Moolenaarf154f3a2020-06-08 18:54:49 +02004364 * Return TRUE if "val" is a valid 'spelllang' value.
Bram Moolenaare677df82019-09-02 22:31:11 +02004365 */
4366 int
Bram Moolenaarf154f3a2020-06-08 18:54:49 +02004367valid_spelllang(char_u *val)
Bram Moolenaare677df82019-09-02 22:31:11 +02004368{
4369 return valid_name(val, ".-_,@");
4370}
4371
4372/*
4373 * Return TRUE if "val" is a valid 'spellfile' value.
4374 */
4375 int
4376valid_spellfile(char_u *val)
4377{
4378 char_u *s;
4379
4380 for (s = val; *s != NUL; ++s)
Bram Moolenaarbc49c5f2022-08-04 13:01:48 +01004381 if (!vim_is_fname_char(*s))
Bram Moolenaare677df82019-09-02 22:31:11 +02004382 return FALSE;
4383 return TRUE;
4384}
4385
4386/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004387 * Handle side effects of setting 'spell' or 'spellfile'
Bram Moolenaare677df82019-09-02 22:31:11 +02004388 * Return an error message or NULL for success.
4389 */
4390 char *
4391did_set_spell_option(int is_spellfile)
4392{
4393 char *errmsg = NULL;
4394 win_T *wp;
4395 int l;
4396
4397 if (is_spellfile)
4398 {
4399 l = (int)STRLEN(curwin->w_s->b_p_spf);
4400 if (l > 0 && (l < 4
4401 || STRCMP(curwin->w_s->b_p_spf + l - 4, ".add") != 0))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004402 errmsg = e_invalid_argument;
Bram Moolenaare677df82019-09-02 22:31:11 +02004403 }
4404
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00004405 if (errmsg != NULL)
4406 return errmsg;
4407
4408 FOR_ALL_WINDOWS(wp)
4409 if (wp->w_buffer == curbuf && wp->w_p_spell)
4410 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004411 errmsg = parse_spelllang(wp);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00004412 break;
4413 }
Bram Moolenaare677df82019-09-02 22:31:11 +02004414 return errmsg;
4415}
4416
4417/*
4418 * Set curbuf->b_cap_prog to the regexp program for 'spellcapcheck'.
4419 * Return error message when failed, NULL when OK.
4420 */
4421 char *
4422compile_cap_prog(synblock_T *synblock)
4423{
4424 regprog_T *rp = synblock->b_cap_prog;
4425 char_u *re;
4426
Bram Moolenaar53efb182019-10-13 19:49:26 +02004427 if (synblock->b_p_spc == NULL || *synblock->b_p_spc == NUL)
Bram Moolenaare677df82019-09-02 22:31:11 +02004428 synblock->b_cap_prog = NULL;
4429 else
4430 {
4431 // Prepend a ^ so that we only match at one column
4432 re = concat_str((char_u *)"^", synblock->b_p_spc);
4433 if (re != NULL)
4434 {
4435 synblock->b_cap_prog = vim_regcomp(re, RE_MAGIC);
4436 vim_free(re);
4437 if (synblock->b_cap_prog == NULL)
4438 {
4439 synblock->b_cap_prog = rp; // restore the previous program
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004440 return e_invalid_argument;
Bram Moolenaare677df82019-09-02 22:31:11 +02004441 }
4442 }
4443 }
4444
4445 vim_regfree(rp);
4446 return NULL;
4447}
4448
4449#endif // FEAT_SPELL