blob: 8558d3908572df75dc9fefad87c4690f78b03d91 [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#define VIMSUGMAGIC "VIMsug" // string at start of Vim .sug file
Bram Moolenaar4770d092006-01-12 23:22:24 +000070#define VIMSUGMAGICL 6
71#define VIMSUGVERSION 1
72
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010073// Result values. Lower number is accepted over higher one.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000074#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000075#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000076#define SP_RARE 1
77#define SP_LOCAL 2
78#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000079
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000080/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000081 * Structure to store info for word matching.
82 */
83typedef struct matchinf_S
84{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010085 langp_T *mi_lp; // info for language and region
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000086
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010087 // pointers to original text to be checked
88 char_u *mi_word; // start of word being checked
89 char_u *mi_end; // end of matching word so far
90 char_u *mi_fend; // next char to be added to mi_fword
91 char_u *mi_cend; // char after what was used for
92 // mi_capflags
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000093
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010094 // case-folded text
95 char_u mi_fword[MAXWLEN + 1]; // mi_word case-folded
96 int mi_fwordlen; // nr of valid bytes in mi_fword
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000097
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010098 // for when checking word after a prefix
99 int mi_prefarridx; // index in sl_pidxs with list of
100 // affixID/condition
101 int mi_prefcnt; // number of entries at mi_prefarridx
102 int mi_prefixlen; // byte length of prefix
103 int mi_cprefixlen; // byte length of prefix in original
104 // case
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000105
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100106 // for when checking a compound word
107 int mi_compoff; // start of following word offset
108 char_u mi_compflags[MAXWLEN]; // flags for compound words used
109 int mi_complen; // nr of compound words used
110 int mi_compextra; // nr of COMPOUNDROOT words
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000111
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100112 // others
113 int mi_result; // result so far: SP_BAD, SP_OK, etc.
114 int mi_capflags; // WF_ONECAP WF_ALLCAP WF_KEEPCAP
115 win_T *mi_win; // buffer being checked
Bram Moolenaar78622822005-08-23 21:00:13 +0000116
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100117 // for NOBREAK
118 int mi_result2; // "mi_resul" without following word
119 char_u *mi_end2; // "mi_end" without following word
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000120} matchinf_T;
121
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000122
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100123static int spell_mb_isword_class(int cl, win_T *wp);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000124
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100125// mode values for find_word
126#define FIND_FOLDWORD 0 // find word case-folded
127#define FIND_KEEPWORD 1 // find keep-case word
128#define FIND_PREFIX 2 // find word after prefix
129#define FIND_COMPOUND 3 // find case-folded compound word
130#define FIND_KEEPCOMPOUND 4 // find keep-case compound word
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000131
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100132static void find_word(matchinf_T *mip, int mode);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void find_prefix(matchinf_T *mip, int mode);
134static int fold_more(matchinf_T *mip);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100135static void spell_load_cb(char_u *fname, void *cookie);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100136static int count_syllables(slang_T *slang, char_u *word);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void clear_midword(win_T *buf);
138static void use_midword(slang_T *lp, win_T *buf);
139static int find_region(char_u *rp, char_u *region);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100140static void spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res);
141static void spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100142static void spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static void dump_word(slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum);
144static linenr_T dump_prefixes(slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T startlnum);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000145
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000146/*
147 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000148 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000149 * "*attrp" is set to the highlight index for a badly spelled word. For a
150 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000151 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000152 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000153 * "capcol" is used to check for a Capitalised word after the end of a
154 * sentence. If it's zero then perform the check. Return the column where to
155 * check next, or -1 when no sentence end was found. If it's NULL then don't
156 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000157 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000158 * Returns the length of the word in bytes, also when it's OK, so that the
159 * caller can skip over the word.
160 */
161 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100162spell_check(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100163 win_T *wp, // current window
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100164 char_u *ptr,
165 hlf_T *attrp,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100166 int *capcol, // column to check for Capital
167 int docount) // count good words
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000168{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100169 matchinf_T mi; // Most things are put in "mi" so that it can
170 // be passed to functions quickly.
171 int nrlen = 0; // found a number first
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000172 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000173 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000174 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +0000175 int count_word = docount;
Bram Moolenaare0ebeda2020-06-10 22:17:58 +0200176 int use_camel_case = *wp->w_s->b_p_spo != NUL;
177 int camel_case = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000178
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100179 // A word never starts at a space or a control character. Return quickly
180 // then, skipping over the character.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000181 if (*ptr <= ' ')
182 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000183
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100184 // Return here when loading language files failed.
Bram Moolenaar860cae12010-06-05 23:22:07 +0200185 if (wp->w_s->b_langp.ga_len == 0)
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000186 return 1;
187
Bram Moolenaara80faa82020-04-12 19:37:17 +0200188 CLEAR_FIELD(mi);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000189
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100190 // A number is always OK. Also skip hexadecimal numbers 0xFF99 and
191 // 0X99FF. But always do check spelling to find "3GPP" and "11
192 // julifeest".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000193 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000194 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100195 if (*ptr == '0' && (ptr[1] == 'b' || ptr[1] == 'B'))
196 mi.mi_end = skipbin(ptr + 2);
197 else if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
Bram Moolenaar3982c542005-06-08 21:56:31 +0000198 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000199 else
200 mi.mi_end = skipdigits(ptr);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000201 nrlen = (int)(mi.mi_end - ptr);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000202 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000203
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100204 // Find the normal end of the word (until the next non-word character).
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000205 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +0000206 mi.mi_fend = ptr;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200207 if (spell_iswordp(mi.mi_fend, wp))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000208 {
Bram Moolenaare0ebeda2020-06-10 22:17:58 +0200209 int prev_upper;
210 int this_upper;
211
212 if (use_camel_case)
213 {
214 c = PTR2CHAR(mi.mi_fend);
215 this_upper = SPELL_ISUPPER(c);
216 }
217
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000218 do
Bram Moolenaare0ebeda2020-06-10 22:17:58 +0200219 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100220 MB_PTR_ADV(mi.mi_fend);
Bram Moolenaare0ebeda2020-06-10 22:17:58 +0200221 if (use_camel_case)
222 {
223 prev_upper = this_upper;
224 c = PTR2CHAR(mi.mi_fend);
225 this_upper = SPELL_ISUPPER(c);
226 camel_case = !prev_upper && this_upper;
227 }
228 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp)
229 && !camel_case);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000230
Bram Moolenaar860cae12010-06-05 23:22:07 +0200231 if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000232 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100233 // Check word starting with capital letter.
Bram Moolenaar53805d12005-08-01 07:08:33 +0000234 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000235 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000236 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000237 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000238 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000239 if (capcol != NULL)
240 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000241
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100242 // We always use the characters up to the next non-word character,
243 // also for bad words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000244 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000245
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100246 // Check caps type later.
Bram Moolenaar860cae12010-06-05 23:22:07 +0200247 mi.mi_capflags = 0;
248 mi.mi_cend = NULL;
249 mi.mi_win = wp;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000250
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100251 // case-fold the word with one non-word character, so that we can check
252 // for the word end.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000253 if (*mi.mi_fend != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100254 MB_PTR_ADV(mi.mi_fend);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000255
256 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
257 MAXWLEN + 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000258 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000259
Bram Moolenaare0ebeda2020-06-10 22:17:58 +0200260 if (camel_case)
261 // Introduce a fake word end space into the folded word.
262 mi.mi_fword[mi.mi_fwordlen - 1] = ' ';
263
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100264 // The word is bad unless we recognize it.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000265 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +0000266 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000267
268 /*
269 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000270 * We check them all, because a word may be matched longer in another
271 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000272 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200273 for (lpi = 0; lpi < wp->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000274 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200275 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, lpi);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000276
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100277 // If reloading fails the language is still in the list but everything
278 // has been cleared.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000279 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
280 continue;
281
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100282 // Check for a matching word in case-folded words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000283 find_word(&mi, FIND_FOLDWORD);
284
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100285 // Check for a matching word in keep-case words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000286 find_word(&mi, FIND_KEEPWORD);
287
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100288 // Check for matching prefixes.
Bram Moolenaard12a1322005-08-21 22:08:24 +0000289 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +0000290
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100291 // For a NOBREAK language, may want to use a word without a following
292 // word as a backup.
Bram Moolenaar78622822005-08-23 21:00:13 +0000293 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
294 && mi.mi_result2 != SP_BAD)
295 {
296 mi.mi_result = mi.mi_result2;
297 mi.mi_end = mi.mi_end2;
298 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000299
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100300 // Count the word in the first language where it's found to be OK.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000301 if (count_word && mi.mi_result == SP_OK)
302 {
303 count_common_word(mi.mi_lp->lp_slang, ptr,
304 (int)(mi.mi_end - ptr), 1);
305 count_word = FALSE;
306 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000307 }
308
309 if (mi.mi_result != SP_OK)
310 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100311 // If we found a number skip over it. Allows for "42nd". Do flag
312 // rare and local words, e.g., "3GPP".
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000313 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000314 {
315 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
316 return nrlen;
317 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000318
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100319 // When we are at a non-word character there is no error, just
320 // skip over the character (try looking for a word after it).
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100321 else if (!spell_iswordp_nmw(ptr, wp))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000322 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200323 if (capcol != NULL && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000324 {
325 regmatch_T regmatch;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100326 int r;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000327
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100328 // Check for end of sentence.
Bram Moolenaar860cae12010-06-05 23:22:07 +0200329 regmatch.regprog = wp->w_s->b_cap_prog;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000330 regmatch.rm_ic = FALSE;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100331 r = vim_regexec(&regmatch, ptr, 0);
332 wp->w_s->b_cap_prog = regmatch.regprog;
333 if (r)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000334 *capcol = (int)(regmatch.endp[0] - ptr);
335 }
336
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000337 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000338 return (*mb_ptr2len)(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000339 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000340 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000341 else if (mi.mi_end == ptr)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100342 // Always include at least one character. Required for when there
343 // is a mixup in "midword".
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100344 MB_PTR_ADV(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +0000345 else if (mi.mi_result == SP_BAD
Bram Moolenaar860cae12010-06-05 23:22:07 +0200346 && LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak)
Bram Moolenaar78622822005-08-23 21:00:13 +0000347 {
348 char_u *p, *fp;
349 int save_result = mi.mi_result;
350
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100351 // First language in 'spelllang' is NOBREAK. Find first position
352 // at which any word would be valid.
Bram Moolenaar860cae12010-06-05 23:22:07 +0200353 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000354 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +0000355 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000356 p = mi.mi_word;
357 fp = mi.mi_fword;
358 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +0000359 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100360 MB_PTR_ADV(p);
361 MB_PTR_ADV(fp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000362 if (p >= mi.mi_end)
363 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000364 mi.mi_compoff = (int)(fp - mi.mi_fword);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000365 find_word(&mi, FIND_COMPOUND);
366 if (mi.mi_result != SP_BAD)
367 {
368 mi.mi_end = p;
369 break;
370 }
Bram Moolenaar78622822005-08-23 21:00:13 +0000371 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000372 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +0000373 }
Bram Moolenaar78622822005-08-23 21:00:13 +0000374 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000375
376 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000377 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000378 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000379 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000380 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000381 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000382 }
383
Bram Moolenaar5195e452005-08-19 20:32:47 +0000384 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
385 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100386 // Report SpellCap only when the word isn't badly spelled.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000387 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000388 return wrongcaplen;
389 }
390
Bram Moolenaar51485f02005-06-04 21:55:20 +0000391 return (int)(mi.mi_end - ptr);
392}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000393
Bram Moolenaar51485f02005-06-04 21:55:20 +0000394/*
395 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000396 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
397 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
398 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
399 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000400 *
401 * For a match mip->mi_result is updated.
402 */
403 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100404find_word(matchinf_T *mip, int mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000405{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000406 idx_T arridx = 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100407 int endlen[MAXWLEN]; // length at possible word endings
408 idx_T endidx[MAXWLEN]; // possible word endings
Bram Moolenaar51485f02005-06-04 21:55:20 +0000409 int endidxcnt = 0;
410 int len;
411 int wlen = 0;
412 int flen;
413 int c;
414 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000415 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000416 char_u *s;
Bram Moolenaare52325c2005-08-22 22:54:29 +0000417 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000418 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000419 slang_T *slang = mip->mi_lp->lp_slang;
420 unsigned flags;
421 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000422 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000423 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000424 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +0000425 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000426
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000427 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000428 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100429 // Check for word with matching case in keep-case tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000430 ptr = mip->mi_word;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100431 flen = 9999; // no case folding, always enough bytes
Bram Moolenaar51485f02005-06-04 21:55:20 +0000432 byts = slang->sl_kbyts;
433 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000434
435 if (mode == FIND_KEEPCOMPOUND)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100436 // Skip over the previously found word(s).
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000437 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000438 }
439 else
440 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100441 // Check for case-folded in case-folded tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000442 ptr = mip->mi_fword;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100443 flen = mip->mi_fwordlen; // available case-folded bytes
Bram Moolenaar51485f02005-06-04 21:55:20 +0000444 byts = slang->sl_fbyts;
445 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000446
447 if (mode == FIND_PREFIX)
448 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100449 // Skip over the prefix.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000450 wlen = mip->mi_prefixlen;
451 flen -= mip->mi_prefixlen;
452 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000453 else if (mode == FIND_COMPOUND)
454 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100455 // Skip over the previously found word(s).
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000456 wlen = mip->mi_compoff;
457 flen -= mip->mi_compoff;
458 }
459
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000460 }
461
Bram Moolenaar51485f02005-06-04 21:55:20 +0000462 if (byts == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100463 return; // array is empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000464
Bram Moolenaar51485f02005-06-04 21:55:20 +0000465 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000466 * Repeat advancing in the tree until:
467 * - there is a byte that doesn't match,
468 * - we reach the end of the tree,
469 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000470 */
471 for (;;)
472 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000473 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000474 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000475
476 len = byts[arridx++];
477
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100478 // If the first possible byte is a zero the word could end here.
479 // Remember this index, we first check for the longest word.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000480 if (byts[arridx] == 0)
481 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000482 if (endidxcnt == MAXWLEN)
483 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100484 // Must be a corrupted spell file.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100485 emsg(_(e_format));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000486 return;
487 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000488 endlen[endidxcnt] = wlen;
489 endidx[endidxcnt++] = arridx++;
490 --len;
491
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100492 // Skip over the zeros, there can be several flag/region
493 // combinations.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000494 while (len > 0 && byts[arridx] == 0)
495 {
496 ++arridx;
497 --len;
498 }
499 if (len == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100500 break; // no children, word must end here
Bram Moolenaar51485f02005-06-04 21:55:20 +0000501 }
502
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100503 // Stop looking at end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000504 if (ptr[wlen] == NUL)
505 break;
506
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100507 // Perform a binary search in the list of accepted bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000508 c = ptr[wlen];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100509 if (c == TAB) // <Tab> is handled like <Space>
Bram Moolenaar0c405862005-06-22 22:26:26 +0000510 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +0000511 lo = arridx;
512 hi = arridx + len - 1;
513 while (lo < hi)
514 {
515 m = (lo + hi) / 2;
516 if (byts[m] > c)
517 hi = m - 1;
518 else if (byts[m] < c)
519 lo = m + 1;
520 else
521 {
522 lo = hi = m;
523 break;
524 }
525 }
526
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100527 // Stop if there is no matching byte.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000528 if (hi < lo || byts[lo] != c)
529 break;
530
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100531 // Continue at the child (if there is one).
Bram Moolenaar51485f02005-06-04 21:55:20 +0000532 arridx = idxs[lo];
533 ++wlen;
534 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +0000535
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100536 // One space in the good word may stand for several spaces in the
537 // checked word.
Bram Moolenaar0c405862005-06-22 22:26:26 +0000538 if (c == ' ')
539 {
540 for (;;)
541 {
542 if (flen <= 0 && *mip->mi_fend != NUL)
543 flen = fold_more(mip);
544 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
545 break;
546 ++wlen;
547 --flen;
548 }
549 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000550 }
551
552 /*
553 * Verify that one of the possible endings is valid. Try the longest
554 * first.
555 */
556 while (endidxcnt > 0)
557 {
558 --endidxcnt;
559 arridx = endidx[endidxcnt];
560 wlen = endlen[endidxcnt];
561
Bram Moolenaar51485f02005-06-04 21:55:20 +0000562 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100563 continue; // not at first byte of character
Bram Moolenaar860cae12010-06-05 23:22:07 +0200564 if (spell_iswordp(ptr + wlen, mip->mi_win))
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000565 {
Bram Moolenaar78622822005-08-23 21:00:13 +0000566 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100567 continue; // next char is a word character
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000568 word_ends = FALSE;
569 }
570 else
571 word_ends = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100572 // The prefix flag is before compound flags. Once a valid prefix flag
573 // has been found we try compound flags.
Bram Moolenaard12a1322005-08-21 22:08:24 +0000574 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000575
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000576 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000577 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100578 // Compute byte length in original word, length may change
579 // when folding case. This can be slow, take a shortcut when the
580 // case-folded word is equal to the keep-case word.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000581 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000582 if (STRNCMP(ptr, p, wlen) != 0)
583 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100584 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s))
585 MB_PTR_ADV(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000586 wlen = (int)(p - mip->mi_word);
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000587 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000588 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000589
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100590 // Check flags and region. For FIND_PREFIX check the condition and
591 // prefix ID.
592 // Repeat this if there are more flags/region alternatives until there
593 // is a match.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000594 res = SP_BAD;
595 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
596 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000597 {
598 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000599
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100600 // For the fold-case tree check that the case of the checked word
601 // matches with what the word in the tree requires.
602 // For keep-case tree the case is always right. For prefixes we
603 // don't bother to check.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000604 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000605 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000606 if (mip->mi_cend != mip->mi_word + wlen)
607 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100608 // mi_capflags was set for a different word length, need
609 // to do it again.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000610 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000611 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000612 }
613
Bram Moolenaar0c405862005-06-22 22:26:26 +0000614 if (mip->mi_capflags == WF_KEEPCAP
615 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000616 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000617 }
618
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100619 // When mode is FIND_PREFIX the word must support the prefix:
620 // check the prefix ID and the condition. Do that for the list at
621 // mip->mi_prefarridx that find_prefix() filled.
Bram Moolenaard12a1322005-08-21 22:08:24 +0000622 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000623 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000624 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000625 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +0000626 mip->mi_word + mip->mi_cprefixlen, slang,
627 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000628 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000629 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000630
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100631 // Use the WF_RARE flag for a rare prefix.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000632 if (c & WF_RAREPFX)
633 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000634 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000635 }
636
Bram Moolenaar78622822005-08-23 21:00:13 +0000637 if (slang->sl_nobreak)
638 {
639 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
640 && (flags & WF_BANNED) == 0)
641 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100642 // NOBREAK: found a valid following word. That's all we
643 // need to know, so return.
Bram Moolenaar78622822005-08-23 21:00:13 +0000644 mip->mi_result = SP_OK;
645 break;
646 }
647 }
648
649 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
650 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000651 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100652 // If there is no compound flag or the word is shorter than
653 // COMPOUNDMIN reject it quickly.
654 // Makes you wonder why someone puts a compound flag on a word
655 // that's too short... Myspell compatibility requires this
656 // anyway.
Bram Moolenaare52325c2005-08-22 22:54:29 +0000657 if (((unsigned)flags >> 24) == 0
658 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000659 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100660 // For multi-byte chars check character length against
661 // COMPOUNDMIN.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000662 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000663 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000664 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
665 wlen - mip->mi_compoff) < slang->sl_compminlen)
666 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000667
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100668 // Limit the number of compound words to COMPOUNDWORDMAX if no
669 // maximum for syllables is specified.
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000670 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
671 > slang->sl_compmax
Bram Moolenaare52325c2005-08-22 22:54:29 +0000672 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000673 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000674
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100675 // Don't allow compounding on a side where an affix was added,
676 // unless COMPOUNDPERMITFLAG was used.
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000677 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
678 continue;
679 if (!word_ends && (flags & WF_NOCOMPAFT))
680 continue;
681
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100682 // Quickly check if compounding is possible with this flag.
Bram Moolenaar6de68532005-08-24 22:08:48 +0000683 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +0000684 ? slang->sl_compstartflags
685 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +0000686 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000687 continue;
688
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100689 // If there is a match with a CHECKCOMPOUNDPATTERN rule
690 // discard the compound word.
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000691 if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat))
692 continue;
693
Bram Moolenaare52325c2005-08-22 22:54:29 +0000694 if (mode == FIND_COMPOUND)
695 {
696 int capflags;
697
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100698 // Need to check the caps type of the appended compound
699 // word.
Bram Moolenaare52325c2005-08-22 22:54:29 +0000700 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
701 mip->mi_compoff) != 0)
702 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100703 // case folding may have changed the length
Bram Moolenaare52325c2005-08-22 22:54:29 +0000704 p = mip->mi_word;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100705 for (s = ptr; s < ptr + mip->mi_compoff; MB_PTR_ADV(s))
706 MB_PTR_ADV(p);
Bram Moolenaare52325c2005-08-22 22:54:29 +0000707 }
708 else
Bram Moolenaare52325c2005-08-22 22:54:29 +0000709 p = mip->mi_word + mip->mi_compoff;
710 capflags = captype(p, mip->mi_word + wlen);
711 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
712 && (flags & WF_FIXCAP) != 0))
713 continue;
714
715 if (capflags != WF_ALLCAP)
716 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100717 // When the character before the word is a word
718 // character we do not accept a Onecap word. We do
719 // accept a no-caps word, even when the dictionary
720 // word specifies ONECAP.
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100721 MB_PTR_BACK(mip->mi_word, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100722 if (spell_iswordp_nmw(p, mip->mi_win)
Bram Moolenaare52325c2005-08-22 22:54:29 +0000723 ? capflags == WF_ONECAP
724 : (flags & WF_ONECAP) != 0
725 && capflags != WF_ONECAP)
726 continue;
727 }
728 }
729
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100730 // If the word ends the sequence of compound flags of the
731 // words must match with one of the COMPOUNDRULE items and
732 // the number of syllables must not be too large.
Bram Moolenaar5195e452005-08-19 20:32:47 +0000733 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
734 mip->mi_compflags[mip->mi_complen + 1] = NUL;
735 if (word_ends)
736 {
737 char_u fword[MAXWLEN];
738
739 if (slang->sl_compsylmax < MAXWLEN)
740 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100741 // "fword" is only needed for checking syllables.
Bram Moolenaar5195e452005-08-19 20:32:47 +0000742 if (ptr == mip->mi_word)
743 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
744 else
745 vim_strncpy(fword, ptr, endlen[endidxcnt]);
746 }
747 if (!can_compound(slang, fword, mip->mi_compflags))
748 continue;
749 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000750 else if (slang->sl_comprules != NULL
751 && !match_compoundrule(slang, mip->mi_compflags))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100752 // The compound flags collected so far do not match any
753 // COMPOUNDRULE, discard the compounded word.
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000754 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000755 }
756
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100757 // Check NEEDCOMPOUND: can't use word without compounding.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000758 else if (flags & WF_NEEDCOMP)
759 continue;
760
Bram Moolenaar78622822005-08-23 21:00:13 +0000761 nobreak_result = SP_OK;
762
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000763 if (!word_ends)
764 {
Bram Moolenaar78622822005-08-23 21:00:13 +0000765 int save_result = mip->mi_result;
766 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000767 langp_T *save_lp = mip->mi_lp;
768 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +0000769
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100770 // Check that a valid word follows. If there is one and we
771 // are compounding, it will set "mi_result", thus we are
772 // always finished here. For NOBREAK we only check that a
773 // valid word follows.
774 // Recursive!
Bram Moolenaar78622822005-08-23 21:00:13 +0000775 if (slang->sl_nobreak)
776 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000777
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100778 // Find following word in case-folded tree.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000779 mip->mi_compoff = endlen[endidxcnt];
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000780 if (has_mbyte && mode == FIND_KEEPWORD)
781 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100782 // Compute byte length in case-folded word from "wlen":
783 // byte length in keep-case word. Length may change when
784 // folding case. This can be slow, take a shortcut when
785 // the case-folded word is equal to the keep-case word.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000786 p = mip->mi_fword;
787 if (STRNCMP(ptr, p, wlen) != 0)
788 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100789 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s))
790 MB_PTR_ADV(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000791 mip->mi_compoff = (int)(p - mip->mi_fword);
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000792 }
793 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100794#if 0 // Disabled, see below
Bram Moolenaard12a1322005-08-21 22:08:24 +0000795 c = mip->mi_compoff;
Bram Moolenaarba534352016-04-21 09:20:26 +0200796#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000797 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000798 if (flags & WF_COMPROOT)
799 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000800
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100801 // For NOBREAK we need to try all NOBREAK languages, at least
802 // to find the ".add" file(s).
Bram Moolenaar860cae12010-06-05 23:22:07 +0200803 for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +0000804 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000805 if (slang->sl_nobreak)
806 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200807 mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi);
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000808 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
809 || !mip->mi_lp->lp_slang->sl_nobreak)
810 continue;
811 }
Bram Moolenaard12a1322005-08-21 22:08:24 +0000812
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000813 find_word(mip, FIND_COMPOUND);
814
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100815 // When NOBREAK any word that matches is OK. Otherwise we
816 // need to find the longest match, thus try with keep-case
817 // and prefix too.
Bram Moolenaar78622822005-08-23 21:00:13 +0000818 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
819 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100820 // Find following word in keep-case tree.
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000821 mip->mi_compoff = wlen;
822 find_word(mip, FIND_KEEPCOMPOUND);
823
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100824#if 0 // Disabled, a prefix must not appear halfway a compound word,
825 // unless the COMPOUNDPERMITFLAG is used and then it can't be a
826 // postponed prefix.
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000827 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
828 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100829 // Check for following word with prefix.
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000830 mip->mi_compoff = c;
831 find_prefix(mip, FIND_COMPOUND);
832 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000833#endif
Bram Moolenaar78622822005-08-23 21:00:13 +0000834 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000835
836 if (!slang->sl_nobreak)
837 break;
Bram Moolenaar78622822005-08-23 21:00:13 +0000838 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000839 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000840 if (flags & WF_COMPROOT)
841 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000842 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000843
Bram Moolenaar78622822005-08-23 21:00:13 +0000844 if (slang->sl_nobreak)
845 {
846 nobreak_result = mip->mi_result;
847 mip->mi_result = save_result;
848 mip->mi_end = save_end;
849 }
850 else
851 {
852 if (mip->mi_result == SP_OK)
853 break;
854 continue;
855 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000856 }
857
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000858 if (flags & WF_BANNED)
859 res = SP_BANNED;
860 else if (flags & WF_REGION)
861 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100862 // Check region.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000863 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000864 res = SP_OK;
865 else
866 res = SP_LOCAL;
867 }
868 else if (flags & WF_RARE)
869 res = SP_RARE;
870 else
871 res = SP_OK;
872
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100873 // Always use the longest match and the best result. For NOBREAK
874 // we separately keep the longest match without a following good
875 // word as a fall-back.
Bram Moolenaar78622822005-08-23 21:00:13 +0000876 if (nobreak_result == SP_BAD)
877 {
878 if (mip->mi_result2 > res)
879 {
880 mip->mi_result2 = res;
881 mip->mi_end2 = mip->mi_word + wlen;
882 }
883 else if (mip->mi_result2 == res
884 && mip->mi_end2 < mip->mi_word + wlen)
885 mip->mi_end2 = mip->mi_word + wlen;
886 }
887 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000888 {
889 mip->mi_result = res;
890 mip->mi_end = mip->mi_word + wlen;
891 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000892 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000893 mip->mi_end = mip->mi_word + wlen;
894
Bram Moolenaar78622822005-08-23 21:00:13 +0000895 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000896 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000897 }
898
Bram Moolenaar78622822005-08-23 21:00:13 +0000899 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000900 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000901 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000902}
903
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000904/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000905 * Return TRUE if there is a match between the word ptr[wlen] and
906 * CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another
907 * word.
908 * A match means that the first part of CHECKCOMPOUNDPATTERN matches at the
909 * end of ptr[wlen] and the second part matches after it.
910 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +0200911 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100912match_checkcompoundpattern(
913 char_u *ptr,
914 int wlen,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100915 garray_T *gap) // &sl_comppat
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000916{
917 int i;
918 char_u *p;
919 int len;
920
921 for (i = 0; i + 1 < gap->ga_len; i += 2)
922 {
923 p = ((char_u **)gap->ga_data)[i + 1];
924 if (STRNCMP(ptr + wlen, p, STRLEN(p)) == 0)
925 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100926 // Second part matches at start of following compound word, now
927 // check if first part matches at end of previous word.
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000928 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar19c9c762008-12-09 21:34:39 +0000929 len = (int)STRLEN(p);
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000930 if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0)
931 return TRUE;
932 }
933 }
934 return FALSE;
935}
936
937/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000938 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
939 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000940 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +0200941 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100942can_compound(slang_T *slang, char_u *word, char_u *flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000943{
Bram Moolenaar6de68532005-08-24 22:08:48 +0000944 char_u uflags[MAXWLEN * 2];
945 int i;
Bram Moolenaar6de68532005-08-24 22:08:48 +0000946 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000947
948 if (slang->sl_compprog == NULL)
949 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +0000950 if (enc_utf8)
951 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100952 // Need to convert the single byte flags to utf8 characters.
Bram Moolenaar6de68532005-08-24 22:08:48 +0000953 p = uflags;
954 for (i = 0; flags[i] != NUL; ++i)
Bram Moolenaarace95982017-03-29 17:30:27 +0200955 p += utf_char2bytes(flags[i], p);
Bram Moolenaar6de68532005-08-24 22:08:48 +0000956 *p = NUL;
957 p = uflags;
958 }
959 else
Bram Moolenaar6de68532005-08-24 22:08:48 +0000960 p = flags;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100961 if (!vim_regexec_prog(&slang->sl_compprog, FALSE, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000962 return FALSE;
963
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100964 // Count the number of syllables. This may be slow, do it last. If there
965 // are too many syllables AND the number of compound words is above
966 // COMPOUNDWORDMAX then compounding is not allowed.
Bram Moolenaar5195e452005-08-19 20:32:47 +0000967 if (slang->sl_compsylmax < MAXWLEN
968 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +0000969 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000970 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000971}
972
973/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000974 * Return TRUE if the compound flags in compflags[] match the start of any
975 * compound rule. This is used to stop trying a compound if the flags
976 * collected so far can't possibly match any compound rule.
977 * Caller must check that slang->sl_comprules is not NULL.
978 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +0200979 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100980match_compoundrule(slang_T *slang, char_u *compflags)
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000981{
982 char_u *p;
983 int i;
984 int c;
985
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100986 // loop over all the COMPOUNDRULE entries
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000987 for (p = slang->sl_comprules; *p != NUL; ++p)
988 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100989 // loop over the flags in the compound word we have made, match
990 // them against the current rule entry
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000991 for (i = 0; ; ++i)
992 {
993 c = compflags[i];
994 if (c == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100995 // found a rule that matches for the flags we have so far
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000996 return TRUE;
997 if (*p == '/' || *p == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100998 break; // end of rule, it's too short
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000999 if (*p == '[')
1000 {
1001 int match = FALSE;
1002
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001003 // compare against all the flags in []
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001004 ++p;
1005 while (*p != ']' && *p != NUL)
1006 if (*p++ == c)
1007 match = TRUE;
1008 if (!match)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001009 break; // none matches
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001010 }
1011 else if (*p != c)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001012 break; // flag of word doesn't match flag in pattern
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001013 ++p;
1014 }
1015
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001016 // Skip to the next "/", where the next pattern starts.
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001017 p = vim_strchr(p, '/');
1018 if (p == NULL)
1019 break;
1020 }
1021
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001022 // Checked all the rules and none of them match the flags, so there
1023 // can't possibly be a compound starting with these flags.
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001024 return FALSE;
1025}
1026
1027/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001028 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1029 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001030 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001031 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02001032 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001033valid_word_prefix(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001034 int totprefcnt, // nr of prefix IDs
1035 int arridx, // idx in sl_pidxs[]
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001036 int flags,
1037 char_u *word,
1038 slang_T *slang,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001039 int cond_req) // only use prefixes with a condition
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001040{
1041 int prefcnt;
1042 int pidx;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001043 regprog_T **rp;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001044 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001045
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001046 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001047 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1048 {
1049 pidx = slang->sl_pidxs[arridx + prefcnt];
1050
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001051 // Check the prefix ID.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001052 if (prefid != (pidx & 0xff))
1053 continue;
1054
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001055 // Check if the prefix doesn't combine and the word already has a
1056 // suffix.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001057 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1058 continue;
1059
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001060 // Check the condition, if there is one. The condition index is
1061 // stored in the two bytes above the prefix ID byte.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001062 rp = &slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
1063 if (*rp != NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001064 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001065 if (!vim_regexec_prog(rp, FALSE, word, 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001066 continue;
1067 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001068 else if (cond_req)
1069 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001070
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001071 // It's a match! Return the WF_ flags.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001072 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001073 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001074 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001075}
1076
1077/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001078 * Check if the word at "mip->mi_word" has a matching prefix.
1079 * If it does, then check the following word.
1080 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001081 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1082 * prefix in a compound word.
1083 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001084 * For a match mip->mi_result is updated.
1085 */
1086 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001087find_prefix(matchinf_T *mip, int mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001088{
1089 idx_T arridx = 0;
1090 int len;
1091 int wlen = 0;
1092 int flen;
1093 int c;
1094 char_u *ptr;
1095 idx_T lo, hi, m;
1096 slang_T *slang = mip->mi_lp->lp_slang;
1097 char_u *byts;
1098 idx_T *idxs;
1099
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001100 byts = slang->sl_pbyts;
1101 if (byts == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001102 return; // array is empty
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001103
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001104 // We use the case-folded word here, since prefixes are always
1105 // case-folded.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001106 ptr = mip->mi_fword;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001107 flen = mip->mi_fwordlen; // available case-folded bytes
Bram Moolenaard12a1322005-08-21 22:08:24 +00001108 if (mode == FIND_COMPOUND)
1109 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001110 // Skip over the previously found word(s).
Bram Moolenaard12a1322005-08-21 22:08:24 +00001111 ptr += mip->mi_compoff;
1112 flen -= mip->mi_compoff;
1113 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001114 idxs = slang->sl_pidxs;
1115
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001116 /*
1117 * Repeat advancing in the tree until:
1118 * - there is a byte that doesn't match,
1119 * - we reach the end of the tree,
1120 * - or we reach the end of the line.
1121 */
1122 for (;;)
1123 {
1124 if (flen == 0 && *mip->mi_fend != NUL)
1125 flen = fold_more(mip);
1126
1127 len = byts[arridx++];
1128
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001129 // If the first possible byte is a zero the prefix could end here.
1130 // Check if the following word matches and supports the prefix.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001131 if (byts[arridx] == 0)
1132 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001133 // There can be several prefixes with different conditions. We
1134 // try them all, since we don't know which one will give the
1135 // longest match. The word is the same each time, pass the list
1136 // of possible prefixes to find_word().
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001137 mip->mi_prefarridx = arridx;
1138 mip->mi_prefcnt = len;
1139 while (len > 0 && byts[arridx] == 0)
1140 {
1141 ++arridx;
1142 --len;
1143 }
1144 mip->mi_prefcnt -= len;
1145
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001146 // Find the word that comes after the prefix.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001147 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001148 if (mode == FIND_COMPOUND)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001149 // Skip over the previously found word(s).
Bram Moolenaard12a1322005-08-21 22:08:24 +00001150 mip->mi_prefixlen += mip->mi_compoff;
1151
Bram Moolenaar53805d12005-08-01 07:08:33 +00001152 if (has_mbyte)
1153 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001154 // Case-folded length may differ from original length.
Bram Moolenaard12a1322005-08-21 22:08:24 +00001155 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1156 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001157 }
1158 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001159 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001160 find_word(mip, FIND_PREFIX);
1161
1162
1163 if (len == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001164 break; // no children, word must end here
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001165 }
1166
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001167 // Stop looking at end of the line.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001168 if (ptr[wlen] == NUL)
1169 break;
1170
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001171 // Perform a binary search in the list of accepted bytes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001172 c = ptr[wlen];
1173 lo = arridx;
1174 hi = arridx + len - 1;
1175 while (lo < hi)
1176 {
1177 m = (lo + hi) / 2;
1178 if (byts[m] > c)
1179 hi = m - 1;
1180 else if (byts[m] < c)
1181 lo = m + 1;
1182 else
1183 {
1184 lo = hi = m;
1185 break;
1186 }
1187 }
1188
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001189 // Stop if there is no matching byte.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001190 if (hi < lo || byts[lo] != c)
1191 break;
1192
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001193 // Continue at the child (if there is one).
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001194 arridx = idxs[lo];
1195 ++wlen;
1196 --flen;
1197 }
1198}
1199
1200/*
1201 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001202 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001203 * Return the length of the folded chars in bytes.
1204 */
1205 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001206fold_more(matchinf_T *mip)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001207{
1208 int flen;
1209 char_u *p;
1210
1211 p = mip->mi_fend;
1212 do
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001213 MB_PTR_ADV(mip->mi_fend);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001214 while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001215
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001216 // Include the non-word character so that we can check for the word end.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001217 if (*mip->mi_fend != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001218 MB_PTR_ADV(mip->mi_fend);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001219
1220 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1221 mip->mi_fword + mip->mi_fwordlen,
1222 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001223 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001224 mip->mi_fwordlen += flen;
1225 return flen;
1226}
1227
1228/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001229 * Check case flags for a word. Return TRUE if the word has the requested
1230 * case.
1231 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02001232 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001233spell_valid_case(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001234 int wordflags, // flags for the checked word.
1235 int treeflags) // flags for the word in the spell tree
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001236{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001237 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001238 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001239 && ((treeflags & WF_ONECAP) == 0
1240 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001241}
1242
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001243/*
1244 * Return TRUE if spell checking is not enabled.
1245 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02001246 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001247no_spell_checking(win_T *wp)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001248{
Bram Moolenaar860cae12010-06-05 23:22:07 +02001249 if (!wp->w_p_spell || *wp->w_s->b_p_spl == NUL
1250 || wp->w_s->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001251 {
Bram Moolenaar152e79e2020-06-10 15:32:08 +02001252 emsg(_(e_no_spell));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001253 return TRUE;
1254 }
1255 return FALSE;
1256}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001257
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001258/*
1259 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001260 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
1261 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001262 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1263 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00001264 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001265 */
1266 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001267spell_move_to(
1268 win_T *wp,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001269 int dir, // FORWARD or BACKWARD
1270 int allwords, // TRUE for "[s"/"]s", FALSE for "[S"/"]S"
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001271 int curline,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001272 hlf_T *attrp) // return: attributes of bad word or NULL
1273 // (only when "dir" is FORWARD)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001274{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001275 linenr_T lnum;
1276 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001277 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001278 char_u *line;
1279 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001280 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001281 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001282 int len;
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001283#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001284 int has_syntax = syntax_present(wp);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001285#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001286 int col;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001287 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001288 char_u *buf = NULL;
1289 int buflen = 0;
1290 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001291 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001292 int found_one = FALSE;
1293 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001294
Bram Moolenaar95529562005-08-25 21:21:38 +00001295 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00001296 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001297
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001298 /*
1299 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00001300 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001301 *
1302 * When searching backwards, we continue in the line to find the last
1303 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001304 *
1305 * We concatenate the start of the next line, so that wrapped words work
1306 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1307 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001308 */
Bram Moolenaar95529562005-08-25 21:21:38 +00001309 lnum = wp->w_cursor.lnum;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001310 CLEAR_POS(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001311
1312 while (!got_int)
1313 {
Bram Moolenaar95529562005-08-25 21:21:38 +00001314 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001315
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001316 len = (int)STRLEN(line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001317 if (buflen < len + MAXWLEN + 2)
1318 {
1319 vim_free(buf);
1320 buflen = len + MAXWLEN + 2;
1321 buf = alloc(buflen);
1322 if (buf == NULL)
1323 break;
1324 }
1325
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001326 // In first line check first word for Capital.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001327 if (lnum == 1)
1328 capcol = 0;
1329
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001330 // For checking first word with a capital skip white space.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001331 if (capcol == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001332 capcol = getwhitecols(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001333 else if (curline && wp == curwin)
1334 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001335 // For spellbadword(): check if first word needs a capital.
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001336 col = getwhitecols(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001337 if (check_need_cap(lnum, col))
1338 capcol = col;
1339
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001340 // Need to get the line again, may have looked at the previous
1341 // one.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001342 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1343 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001344
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001345 // Copy the line into "buf" and append the start of the next line if
1346 // possible.
Bram Moolenaar0c405862005-06-22 22:26:26 +00001347 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00001348 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar5dd95a12006-05-13 12:09:24 +00001349 spell_cat_line(buf + STRLEN(buf),
1350 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001351
1352 p = buf + skip;
1353 endp = buf + len;
1354 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001355 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001356 // When searching backward don't search after the cursor. Unless
1357 // we wrapped around the end of the buffer.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001358 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00001359 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001360 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00001361 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001362 break;
1363
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001364 // start of word
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001365 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001366 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001367
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001368 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001369 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001370 // We found a bad word. Check the attribute.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001371 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001372 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001373 // When searching forward only accept a bad word after
1374 // the cursor.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001375 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001376 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00001377 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001378 && (wrapped
1379 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00001380 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001381 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001382 {
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001383#ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00001384 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001385 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001386 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001387 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00001388 FALSE, &can_spell, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001389 if (!can_spell)
1390 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001391 }
1392 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001393#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001394 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001395
Bram Moolenaar51485f02005-06-04 21:55:20 +00001396 if (can_spell)
1397 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00001398 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001399 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001400 found_pos.col = (int)(p - buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001401 found_pos.coladd = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001402 if (dir == FORWARD)
1403 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001404 // No need to search further.
Bram Moolenaar95529562005-08-25 21:21:38 +00001405 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001406 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001407 if (attrp != NULL)
1408 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001409 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001410 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001411 else if (curline)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001412 // Insert mode completion: put cursor after
1413 // the bad word.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001414 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001415 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001416 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001417 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00001418 else
1419 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001420 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001421 }
1422
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001423 // advance to character after the word
Bram Moolenaar51485f02005-06-04 21:55:20 +00001424 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001425 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001426 }
1427
Bram Moolenaar5195e452005-08-19 20:32:47 +00001428 if (dir == BACKWARD && found_pos.lnum != 0)
1429 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001430 // Use the last match in the line (before the cursor).
Bram Moolenaar95529562005-08-25 21:21:38 +00001431 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001432 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001433 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001434 }
1435
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001436 if (curline)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001437 break; // only check cursor line
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001438
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001439 // If we are back at the starting line and searched it again there
1440 // is no match, give up.
Bram Moolenaard3f78dc2017-02-25 14:21:10 +01001441 if (lnum == wp->w_cursor.lnum && wrapped)
1442 break;
1443
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001444 // Advance to next line.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001445 if (dir == BACKWARD)
1446 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001447 if (lnum > 1)
1448 --lnum;
1449 else if (!p_ws)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001450 break; // at first line and 'nowrapscan'
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001451 else
1452 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001453 // Wrap around to the end of the buffer. May search the
1454 // starting line again and accept the last match.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001455 lnum = wp->w_buffer->b_ml.ml_line_count;
1456 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001457 if (!shortmess(SHM_SEARCH))
1458 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001459 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001460 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001461 }
1462 else
1463 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001464 if (lnum < wp->w_buffer->b_ml.ml_line_count)
1465 ++lnum;
1466 else if (!p_ws)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001467 break; // at first line and 'nowrapscan'
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001468 else
1469 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001470 // Wrap around to the start of the buffer. May search the
1471 // starting line again and accept the first match.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001472 lnum = 1;
1473 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001474 if (!shortmess(SHM_SEARCH))
1475 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001476 }
1477
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001478 // If we are back at the starting line and there is no match then
1479 // give up.
Bram Moolenaard3f78dc2017-02-25 14:21:10 +01001480 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001481 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001482
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001483 // Skip the characters at the start of the next line that were
1484 // included in a match crossing line boundaries.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001485 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001486 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001487 else
1488 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001489
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001490 // Capcol skips over the inserted space.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001491 --capcol;
1492
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001493 // But after empty line check first word in next line
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001494 if (*skipwhite(line) == NUL)
1495 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001496 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001497
1498 line_breakcheck();
1499 }
1500
Bram Moolenaar0c405862005-06-22 22:26:26 +00001501 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001502 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001503}
1504
1505/*
1506 * For spell checking: concatenate the start of the following line "line" into
1507 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00001508 * Keep the blanks at the start of the next line, this is used in win_line()
1509 * to skip those bytes if the word was OK.
Bram Moolenaar0c405862005-06-22 22:26:26 +00001510 */
1511 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001512spell_cat_line(char_u *buf, char_u *line, int maxlen)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001513{
1514 char_u *p;
1515 int n;
1516
1517 p = skipwhite(line);
1518 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1519 p = skipwhite(p + 1);
1520
1521 if (*p != NUL)
1522 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001523 // Only worth concatenating if there is something else than spaces to
1524 // concatenate.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00001525 n = (int)(p - line) + 1;
1526 if (n < maxlen - 1)
1527 {
1528 vim_memset(buf, ' ', n);
1529 vim_strncpy(buf + n, p, maxlen - 1 - n);
1530 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00001531 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001532}
1533
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001534/*
1535 * Structure used for the cookie argument of do_in_runtimepath().
1536 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001537typedef struct spelload_S
1538{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001539 char_u sl_lang[MAXWLEN + 1]; // language name
1540 slang_T *sl_slang; // resulting slang_T struct
1541 int sl_nobreak; // NOBREAK language found
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001542} spelload_T;
1543
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001544/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001545 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001546 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001547 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001548 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001549spell_load_lang(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001550{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001551 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001552 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001553 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001554 int round;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001555
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001556 // Copy the language name to pass it to spell_load_cb() as a cookie.
1557 // It's truncated when an error is detected.
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001558 STRCPY(sl.sl_lang, lang);
1559 sl.sl_slang = NULL;
1560 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001561
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001562 // We may retry when no spell file is found for the language, an
1563 // autocommand may load it then.
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001564 for (round = 1; round <= 2; ++round)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001565 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001566 /*
1567 * Find the first spell file for "lang" in 'runtimepath' and load it.
1568 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001569 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001570#ifdef VMS
1571 "spell/%s_%s.spl",
1572#else
1573 "spell/%s.%s.spl",
1574#endif
1575 lang, spell_enc());
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001576 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001577
1578 if (r == FAIL && *sl.sl_lang != NUL)
1579 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001580 // Try loading the ASCII version.
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001581 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001582#ifdef VMS
1583 "spell/%s_ascii.spl",
1584#else
1585 "spell/%s.ascii.spl",
1586#endif
1587 lang);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001588 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001589
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001590 if (r == FAIL && *sl.sl_lang != NUL && round == 1
1591 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
1592 curbuf->b_fname, FALSE, curbuf))
1593 continue;
1594 break;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001595 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001596 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001597 }
1598
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001599 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001600 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001601 smsg(
Bram Moolenaar56f78042010-12-08 17:09:32 +01001602#ifdef VMS
1603 _("Warning: Cannot find word list \"%s_%s.spl\" or \"%s_ascii.spl\""),
1604#else
1605 _("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
1606#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00001607 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001608 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001609 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001610 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001611 // At least one file was loaded, now load ALL the additions.
Bram Moolenaarb765d632005-06-07 21:00:02 +00001612 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001613 do_in_runtimepath(fname_enc, DIP_ALL, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001614 }
1615}
1616
1617/*
1618 * Return the encoding used for spell checking: Use 'encoding', except that we
1619 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1620 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001621 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001622spell_enc(void)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001623{
1624
Bram Moolenaarb765d632005-06-07 21:00:02 +00001625 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1626 return p_enc;
Bram Moolenaarb765d632005-06-07 21:00:02 +00001627 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001628}
1629
1630/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001631 * Get the name of the .spl file for the internal wordlist into
1632 * "fname[MAXPATHL]".
1633 */
1634 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001635int_wordlist_spl(char_u *fname)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001636{
Bram Moolenaar56f78042010-12-08 17:09:32 +01001637 vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001638 int_wordlist, spell_enc());
1639}
1640
1641/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001642 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001643 * Caller must fill "sl_next".
1644 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001645 slang_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001646slang_alloc(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001647{
1648 slang_T *lp;
1649
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001650 lp = ALLOC_CLEAR_ONE(slang_T);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001651 if (lp != NULL)
1652 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001653 if (lang != NULL)
1654 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001655 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001656 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001657 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001658 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001659 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001660 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001661
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001662 return lp;
1663}
1664
1665/*
1666 * Free the contents of an slang_T and the structure itself.
1667 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001668 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001669slang_free(slang_T *lp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001670{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001671 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001672 vim_free(lp->sl_fname);
1673 slang_clear(lp);
1674 vim_free(lp);
1675}
1676
1677/*
1678 * Clear an slang_T so that the file can be reloaded.
1679 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001680 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001681slang_clear(slang_T *lp)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001682{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001683 garray_T *gap;
1684 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001685 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001686 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001687 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001688
Bram Moolenaard23a8232018-02-10 18:45:26 +01001689 VIM_CLEAR(lp->sl_fbyts);
1690 VIM_CLEAR(lp->sl_kbyts);
1691 VIM_CLEAR(lp->sl_pbyts);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001692
Bram Moolenaard23a8232018-02-10 18:45:26 +01001693 VIM_CLEAR(lp->sl_fidxs);
1694 VIM_CLEAR(lp->sl_kidxs);
1695 VIM_CLEAR(lp->sl_pidxs);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001696
Bram Moolenaar4770d092006-01-12 23:22:24 +00001697 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001698 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001699 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
1700 while (gap->ga_len > 0)
1701 {
1702 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1703 vim_free(ftp->ft_from);
1704 vim_free(ftp->ft_to);
1705 }
1706 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001707 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001708
1709 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001710 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001711 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001712 // "ga_len" is set to 1 without adding an item for latin1
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001713 if (gap->ga_data != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001714 // SOFOFROM and SOFOTO items: free lists of wide characters.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001715 for (i = 0; i < gap->ga_len; ++i)
1716 vim_free(((int **)gap->ga_data)[i]);
1717 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001718 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001719 // SAL items: free salitem_T items
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001720 while (gap->ga_len > 0)
1721 {
1722 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1723 vim_free(smp->sm_lead);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001724 // Don't free sm_oneof and sm_rules, they point into sm_lead.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001725 vim_free(smp->sm_to);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001726 vim_free(smp->sm_lead_w);
1727 vim_free(smp->sm_oneof_w);
1728 vim_free(smp->sm_to_w);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001729 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001730 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001731
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001732 for (i = 0; i < lp->sl_prefixcnt; ++i)
Bram Moolenaar473de612013-06-08 18:19:48 +02001733 vim_regfree(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001734 lp->sl_prefixcnt = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001735 VIM_CLEAR(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001736
Bram Moolenaard23a8232018-02-10 18:45:26 +01001737 VIM_CLEAR(lp->sl_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001738
Bram Moolenaard23a8232018-02-10 18:45:26 +01001739 VIM_CLEAR(lp->sl_midword);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001740
Bram Moolenaar473de612013-06-08 18:19:48 +02001741 vim_regfree(lp->sl_compprog);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001742 lp->sl_compprog = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001743 VIM_CLEAR(lp->sl_comprules);
1744 VIM_CLEAR(lp->sl_compstartflags);
1745 VIM_CLEAR(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001746
Bram Moolenaard23a8232018-02-10 18:45:26 +01001747 VIM_CLEAR(lp->sl_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001748 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001749
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001750 ga_clear_strings(&lp->sl_comppat);
1751
Bram Moolenaar4770d092006-01-12 23:22:24 +00001752 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
1753 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00001754
Bram Moolenaar4770d092006-01-12 23:22:24 +00001755 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001756
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001757 // Clear info from .sug file.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001758 slang_clear_sug(lp);
1759
Bram Moolenaar5195e452005-08-19 20:32:47 +00001760 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001761 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001762 lp->sl_compsylmax = MAXWLEN;
1763 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001764}
1765
1766/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001767 * Clear the info from the .sug file in "lp".
1768 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001769 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001770slang_clear_sug(slang_T *lp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00001771{
Bram Moolenaard23a8232018-02-10 18:45:26 +01001772 VIM_CLEAR(lp->sl_sbyts);
1773 VIM_CLEAR(lp->sl_sidxs);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001774 close_spellbuf(lp->sl_sugbuf);
1775 lp->sl_sugbuf = NULL;
1776 lp->sl_sugloaded = FALSE;
1777 lp->sl_sugtime = 0;
1778}
1779
1780/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001781 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001782 * Invoked through do_in_runtimepath().
1783 */
1784 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001785spell_load_cb(char_u *fname, void *cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001786{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001787 spelload_T *slp = (spelload_T *)cookie;
1788 slang_T *slang;
1789
1790 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
1791 if (slang != NULL)
1792 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001793 // When a previously loaded file has NOBREAK also use it for the
1794 // ".add" files.
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001795 if (slp->sl_nobreak && slang->sl_add)
1796 slang->sl_nobreak = TRUE;
1797 else if (slang->sl_nobreak)
1798 slp->sl_nobreak = TRUE;
1799
1800 slp->sl_slang = slang;
1801 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00001802}
1803
Bram Moolenaar4770d092006-01-12 23:22:24 +00001804
1805/*
1806 * Add a word to the hashtable of common words.
1807 * If it's already there then the counter is increased.
1808 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001809 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001810count_common_word(
1811 slang_T *lp,
1812 char_u *word,
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001813 int len, // word length, -1 for up to NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001814 int count) // 1 to count once, 10 to init
Bram Moolenaar4770d092006-01-12 23:22:24 +00001815{
1816 hash_T hash;
1817 hashitem_T *hi;
1818 wordcount_T *wc;
1819 char_u buf[MAXWLEN];
1820 char_u *p;
1821
1822 if (len == -1)
1823 p = word;
Bram Moolenaar5bcc5a12019-08-06 22:48:02 +02001824 else if (len >= MAXWLEN)
1825 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001826 else
1827 {
1828 vim_strncpy(buf, word, len);
1829 p = buf;
1830 }
1831
1832 hash = hash_hash(p);
1833 hi = hash_lookup(&lp->sl_wordcount, p, hash);
1834 if (HASHITEM_EMPTY(hi))
1835 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001836 wc = alloc(sizeof(wordcount_T) + STRLEN(p));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001837 if (wc == NULL)
1838 return;
1839 STRCPY(wc->wc_word, p);
1840 wc->wc_count = count;
1841 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
1842 }
1843 else
1844 {
1845 wc = HI2WC(hi);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001846 if ((wc->wc_count += count) < (unsigned)count) // check for overflow
Bram Moolenaar4770d092006-01-12 23:22:24 +00001847 wc->wc_count = MAXWORDCOUNT;
1848 }
1849}
1850
1851/*
Bram Moolenaar95529562005-08-25 21:21:38 +00001852 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00001853 * Like strchr() but independent of locale.
1854 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001855 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001856byte_in_str(char_u *str, int n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001857{
1858 char_u *p;
1859
1860 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00001861 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001862 return TRUE;
1863 return FALSE;
1864}
1865
Bram Moolenaar5195e452005-08-19 20:32:47 +00001866#define SY_MAXLEN 30
1867typedef struct syl_item_S
1868{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001869 char_u sy_chars[SY_MAXLEN]; // the sequence of chars
Bram Moolenaar5195e452005-08-19 20:32:47 +00001870 int sy_len;
1871} syl_item_T;
1872
1873/*
1874 * Truncate "slang->sl_syllable" at the first slash and put the following items
1875 * in "slang->sl_syl_items".
1876 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001877 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001878init_syl_tab(slang_T *slang)
Bram Moolenaar5195e452005-08-19 20:32:47 +00001879{
1880 char_u *p;
1881 char_u *s;
1882 int l;
1883 syl_item_T *syl;
1884
1885 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
1886 p = vim_strchr(slang->sl_syllable, '/');
1887 while (p != NULL)
1888 {
1889 *p++ = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001890 if (*p == NUL) // trailing slash
Bram Moolenaar5195e452005-08-19 20:32:47 +00001891 break;
1892 s = p;
1893 p = vim_strchr(p, '/');
1894 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001895 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001896 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001897 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001898 if (l >= SY_MAXLEN)
1899 return SP_FORMERROR;
1900 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001901 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001902 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
1903 + slang->sl_syl_items.ga_len++;
1904 vim_strncpy(syl->sy_chars, s, l);
1905 syl->sy_len = l;
1906 }
1907 return OK;
1908}
1909
1910/*
1911 * Count the number of syllables in "word".
1912 * When "word" contains spaces the syllables after the last space are counted.
1913 * Returns zero if syllables are not defines.
1914 */
1915 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001916count_syllables(slang_T *slang, char_u *word)
Bram Moolenaar5195e452005-08-19 20:32:47 +00001917{
1918 int cnt = 0;
1919 int skip = FALSE;
1920 char_u *p;
1921 int len;
1922 int i;
1923 syl_item_T *syl;
1924 int c;
1925
1926 if (slang->sl_syllable == NULL)
1927 return 0;
1928
1929 for (p = word; *p != NUL; p += len)
1930 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001931 // When running into a space reset counter.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001932 if (*p == ' ')
1933 {
1934 len = 1;
1935 cnt = 0;
1936 continue;
1937 }
1938
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001939 // Find longest match of syllable items.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001940 len = 0;
1941 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
1942 {
1943 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
1944 if (syl->sy_len > len
1945 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
1946 len = syl->sy_len;
1947 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001948 if (len != 0) // found a match, count syllable
Bram Moolenaar5195e452005-08-19 20:32:47 +00001949 {
1950 ++cnt;
1951 skip = FALSE;
1952 }
1953 else
1954 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001955 // No recognized syllable item, at least a syllable char then?
Bram Moolenaar5195e452005-08-19 20:32:47 +00001956 c = mb_ptr2char(p);
1957 len = (*mb_ptr2len)(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001958 if (vim_strchr(slang->sl_syllable, c) == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001959 skip = FALSE; // No, search for next syllable
Bram Moolenaar5195e452005-08-19 20:32:47 +00001960 else if (!skip)
1961 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001962 ++cnt; // Yes, count it
1963 skip = TRUE; // don't count following syllable chars
Bram Moolenaar5195e452005-08-19 20:32:47 +00001964 }
1965 }
1966 }
1967 return cnt;
1968}
1969
1970/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02001971 * Parse 'spelllang' and set w_s->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001972 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001973 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001974 char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001975did_set_spelllang(win_T *wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001976{
1977 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001978 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001979 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00001980 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001981 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001982 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001983 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001984 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001985 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001986 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001987 int len;
1988 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00001989 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001990 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001991 char_u *use_region = NULL;
1992 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001993 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001994 int i, j;
1995 langp_T *lp, *lp2;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001996 static int recursive = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001997 char *ret_msg = NULL;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001998 char_u *spl_copy;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001999 bufref_T bufref;
2000
2001 set_bufref(&bufref, wp->w_buffer);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002002
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002003 // We don't want to do this recursively. May happen when a language is
2004 // not available and the SpellFileMissing autocommand opens a new buffer
2005 // in which 'spell' is set.
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002006 if (recursive)
2007 return NULL;
2008 recursive = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002009
2010 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002011 clear_midword(wp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002012
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002013 // Make a copy of 'spelllang', the SpellFileMissing autocommands may change
2014 // it under our fingers.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002015 spl_copy = vim_strsave(wp->w_s->b_p_spl);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002016 if (spl_copy == NULL)
2017 goto theend;
2018
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002019 wp->w_s->b_cjk = 0;
2020
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002021 // Loop over comma separated language names.
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002022 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002023 {
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002024 // Get one language name.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002025 copy_option_part(&splp, lang, MAXWLEN, ",");
Bram Moolenaar5482f332005-04-17 20:18:43 +00002026 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002027 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002028
Bram Moolenaarf154f3a2020-06-08 18:54:49 +02002029 if (!valid_spelllang(lang))
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002030 continue;
2031
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002032 if (STRCMP(lang, "cjk") == 0)
2033 {
2034 wp->w_s->b_cjk = 1;
2035 continue;
2036 }
2037
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002038 // If the name ends in ".spl" use it as the name of the spell file.
2039 // If there is a region name let "region" point to it and remove it
2040 // from the name.
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002041 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2042 {
2043 filename = TRUE;
2044
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002045 // Locate a region and remove it from the file name.
Bram Moolenaarb6356332005-07-18 21:40:44 +00002046 p = vim_strchr(gettail(lang), '_');
2047 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
2048 && !ASCII_ISALPHA(p[3]))
2049 {
2050 vim_strncpy(region_cp, p + 1, 2);
2051 mch_memmove(p, p + 3, len - (p - lang) - 2);
Bram Moolenaarb6356332005-07-18 21:40:44 +00002052 region = region_cp;
2053 }
2054 else
2055 dont_use_region = TRUE;
2056
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002057 // Check if we loaded this language before.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002058 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar99499b12019-05-23 21:35:48 +02002059 if (fullpathcmp(lang, slang->sl_fname, FALSE, TRUE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002060 break;
2061 }
2062 else
2063 {
2064 filename = FALSE;
2065 if (len > 3 && lang[len - 3] == '_')
2066 {
2067 region = lang + len - 2;
2068 len -= 3;
2069 lang[len] = NUL;
2070 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002071 else
2072 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002073
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002074 // Check if we loaded this language before.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002075 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002076 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002077 break;
2078 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002079
Bram Moolenaarb6356332005-07-18 21:40:44 +00002080 if (region != NULL)
2081 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002082 // If the region differs from what was used before then don't
2083 // use it for 'spellfile'.
Bram Moolenaarb6356332005-07-18 21:40:44 +00002084 if (use_region != NULL && STRCMP(region, use_region) != 0)
2085 dont_use_region = TRUE;
2086 use_region = region;
2087 }
2088
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002089 // If not found try loading the language now.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002090 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002091 {
2092 if (filename)
2093 (void)spell_load_file(lang, lang, NULL, FALSE);
2094 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002095 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002096 spell_load_lang(lang);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002097 // SpellFileMissing autocommands may do anything, including
2098 // destroying the buffer we are using...
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002099 if (!bufref_valid(&bufref))
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002100 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002101 ret_msg = N_("E797: SpellFileMissing autocommand deleted buffer");
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002102 goto theend;
2103 }
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002104 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002105 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002106
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002107 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002108 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002109 */
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002110 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar99499b12019-05-23 21:35:48 +02002111 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE, TRUE)
2112 == FPC_SAME
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002113 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002114 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002115 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002116 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002117 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002118 // find region in sl_regions
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002119 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002120 if (c == REGION_ALL)
2121 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002122 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002123 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002124 if (*slang->sl_regions != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002125 // This addition file is for other regions.
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002126 region_mask = 0;
2127 }
2128 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002129 // This is probably an error. Give a warning and
2130 // accept the words anyway.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002131 smsg(_("Warning: region %s not supported"),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002132 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002133 }
2134 else
2135 region_mask = 1 << c;
2136 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002137
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002138 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002139 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002140 if (ga_grow(&ga, 1) == FAIL)
2141 {
2142 ga_clear(&ga);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002143 ret_msg = e_outofmem;
2144 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002145 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002146 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002147 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2148 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002149 use_midword(slang, wp);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002150 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002151 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002152 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002153 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002154 }
2155
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002156 // round 0: load int_wordlist, if possible.
2157 // round 1: load first name in 'spellfile'.
2158 // round 2: load second name in 'spellfile.
2159 // etc.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002160 spf = curwin->w_s->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002161 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002162 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002163 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002164 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002165 // Internal wordlist, if there is one.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002166 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002167 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002168 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002169 }
2170 else
2171 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002172 // One entry in 'spellfile'.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002173 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
2174 STRCAT(spf_name, ".spl");
2175
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002176 // If it was already found above then skip it.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002177 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002178 {
2179 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
Bram Moolenaar99499b12019-05-23 21:35:48 +02002180 if (p != NULL && fullpathcmp(spf_name, p, FALSE, TRUE)
2181 == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002182 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002183 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002184 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002185 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002186 }
2187
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002188 // Check if it was loaded already.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002189 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar99499b12019-05-23 21:35:48 +02002190 if (fullpathcmp(spf_name, slang->sl_fname, FALSE, TRUE)
2191 == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002192 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002193 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002194 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002195 // Not loaded, try loading it now. The language name includes the
2196 // region name, the region is ignored otherwise. for int_wordlist
2197 // use an arbitrary name.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002198 if (round == 0)
2199 STRCPY(lang, "internal wordlist");
2200 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00002201 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002202 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002203 p = vim_strchr(lang, '.');
2204 if (p != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002205 *p = NUL; // truncate at ".encoding.add"
Bram Moolenaar7887d882005-07-01 22:33:52 +00002206 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002207 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002208
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002209 // If one of the languages has NOBREAK we assume the addition
2210 // files also have this.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002211 if (slang != NULL && nobreak)
2212 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002213 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002214 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002215 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002216 region_mask = REGION_ALL;
2217 if (use_region != NULL && !dont_use_region)
2218 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002219 // find region in sl_regions
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002220 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002221 if (c != REGION_ALL)
2222 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002223 else if (*slang->sl_regions != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002224 // This spell file is for other regions.
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002225 region_mask = 0;
2226 }
2227
2228 if (region_mask != 0)
2229 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002230 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
2231 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
2232 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002233 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2234 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002235 use_midword(slang, wp);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002236 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002237 }
2238 }
2239
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002240 // Everything is fine, store the new b_langp value.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002241 ga_clear(&wp->w_s->b_langp);
2242 wp->w_s->b_langp = ga;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002243
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002244 // For each language figure out what language to use for sound folding and
2245 // REP items. If the language doesn't support it itself use another one
2246 // with the same name. E.g. for "en-math" use "en".
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002247 for (i = 0; i < ga.ga_len; ++i)
2248 {
2249 lp = LANGP_ENTRY(ga, i);
2250
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002251 // sound folding
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002252 if (lp->lp_slang->sl_sal.ga_len > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002253 // language does sound folding itself
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002254 lp->lp_sallang = lp->lp_slang;
2255 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002256 // find first similar language that does sound folding
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002257 for (j = 0; j < ga.ga_len; ++j)
2258 {
2259 lp2 = LANGP_ENTRY(ga, j);
2260 if (lp2->lp_slang->sl_sal.ga_len > 0
2261 && STRNCMP(lp->lp_slang->sl_name,
2262 lp2->lp_slang->sl_name, 2) == 0)
2263 {
2264 lp->lp_sallang = lp2->lp_slang;
2265 break;
2266 }
2267 }
2268
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002269 // REP items
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002270 if (lp->lp_slang->sl_rep.ga_len > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002271 // language has REP items itself
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002272 lp->lp_replang = lp->lp_slang;
2273 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002274 // find first similar language that has REP items
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002275 for (j = 0; j < ga.ga_len; ++j)
2276 {
2277 lp2 = LANGP_ENTRY(ga, j);
2278 if (lp2->lp_slang->sl_rep.ga_len > 0
2279 && STRNCMP(lp->lp_slang->sl_name,
2280 lp2->lp_slang->sl_name, 2) == 0)
2281 {
2282 lp->lp_replang = lp2->lp_slang;
2283 break;
2284 }
2285 }
2286 }
2287
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002288theend:
2289 vim_free(spl_copy);
2290 recursive = FALSE;
Bram Moolenaarbe578ed2014-05-13 14:03:40 +02002291 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002292 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002293}
2294
2295/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002296 * Clear the midword characters for buffer "buf".
2297 */
2298 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002299clear_midword(win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002300{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002301 CLEAR_FIELD(wp->w_s->b_spell_ismw);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002302 VIM_CLEAR(wp->w_s->b_spell_ismw_mb);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002303}
2304
2305/*
2306 * Use the "sl_midword" field of language "lp" for buffer "buf".
2307 * They add up to any currently used midword characters.
2308 */
2309 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002310use_midword(slang_T *lp, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002311{
2312 char_u *p;
2313
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002314 if (lp->sl_midword == NULL) // there aren't any
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002315 return;
2316
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002317 for (p = lp->sl_midword; *p != NUL; )
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002318 if (has_mbyte)
2319 {
2320 int c, l, n;
2321 char_u *bp;
2322
2323 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002324 l = (*mb_ptr2len)(p);
2325 if (c < 256 && l <= 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002326 wp->w_s->b_spell_ismw[c] = TRUE;
2327 else if (wp->w_s->b_spell_ismw_mb == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002328 // First multi-byte char in "b_spell_ismw_mb".
Bram Moolenaar860cae12010-06-05 23:22:07 +02002329 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002330 else
2331 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002332 // Append multi-byte chars to "b_spell_ismw_mb".
Bram Moolenaar860cae12010-06-05 23:22:07 +02002333 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
2334 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002335 if (bp != NULL)
2336 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002337 vim_free(wp->w_s->b_spell_ismw_mb);
2338 wp->w_s->b_spell_ismw_mb = bp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002339 vim_strncpy(bp + n, p, l);
2340 }
2341 }
2342 p += l;
2343 }
2344 else
Bram Moolenaar860cae12010-06-05 23:22:07 +02002345 wp->w_s->b_spell_ismw[*p++] = TRUE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002346}
2347
2348/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002349 * Find the region "region[2]" in "rp" (points to "sl_regions").
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01002350 * Each region is simply stored as the two characters of its name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002351 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002352 */
2353 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002354find_region(char_u *rp, char_u *region)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002355{
2356 int i;
2357
2358 for (i = 0; ; i += 2)
2359 {
2360 if (rp[i] == NUL)
2361 return REGION_ALL;
2362 if (rp[i] == region[0] && rp[i + 1] == region[1])
2363 break;
2364 }
2365 return i / 2;
2366}
2367
2368/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002369 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002370 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002371 * Word WF_ONECAP
2372 * W WORD WF_ALLCAP
2373 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002374 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002375 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002376captype(
2377 char_u *word,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002378 char_u *end) // When NULL use up to NUL byte.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002379{
2380 char_u *p;
2381 int c;
2382 int firstcap;
2383 int allcap;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002384 int past_second = FALSE; // past second word char
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002385
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002386 // find first letter
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002387 for (p = word; !spell_iswordp_nmw(p, curwin); MB_PTR_ADV(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002388 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002389 return 0; // only non-word characters, illegal word
Bram Moolenaarb765d632005-06-07 21:00:02 +00002390 if (has_mbyte)
2391 c = mb_ptr2char_adv(&p);
2392 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00002393 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002394 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002395
2396 /*
2397 * Need to check all letters to find a word with mixed upper/lower.
2398 * But a word with an upper char only at start is a ONECAP.
2399 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002400 for ( ; end == NULL ? *p != NUL : p < end; MB_PTR_ADV(p))
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002401 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002402 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002403 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002404 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002405 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002406 // UUl -> KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002407 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002408 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002409 allcap = FALSE;
2410 }
2411 else if (!allcap)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002412 // UlU -> KEEPCAP
Bram Moolenaar51485f02005-06-04 21:55:20 +00002413 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002414 past_second = TRUE;
2415 }
2416
2417 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002418 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002419 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002420 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002421 return 0;
2422}
2423
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002424/*
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002425 * Delete the internal wordlist and its .spl file.
2426 */
2427 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002428spell_delete_wordlist(void)
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002429{
2430 char_u fname[MAXPATHL];
2431
2432 if (int_wordlist != NULL)
2433 {
2434 mch_remove(int_wordlist);
2435 int_wordlist_spl(fname);
2436 mch_remove(fname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002437 VIM_CLEAR(int_wordlist);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002438 }
2439}
2440
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002441/*
2442 * Free all languages.
2443 */
2444 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002445spell_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002446{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002447 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002448 buf_T *buf;
2449
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002450 // Go through all buffers and handle 'spelllang'. <VN>
Bram Moolenaar29323592016-07-24 22:04:11 +02002451 FOR_ALL_BUFFERS(buf)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002452 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002453
2454 while (first_lang != NULL)
2455 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002456 slang = first_lang;
2457 first_lang = slang->sl_next;
2458 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002459 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002460
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002461 spell_delete_wordlist();
Bram Moolenaar7887d882005-07-01 22:33:52 +00002462
Bram Moolenaard23a8232018-02-10 18:45:26 +01002463 VIM_CLEAR(repl_to);
2464 VIM_CLEAR(repl_from);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002465}
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002466
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002467/*
2468 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002469 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002470 */
2471 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002472spell_reload(void)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002473{
Bram Moolenaar3982c542005-06-08 21:56:31 +00002474 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002475
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002476 // Initialize the table for spell_iswordp().
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002477 init_spell_chartab();
2478
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002479 // Unload all allocated memory.
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002480 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002481
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002482 // Go through all buffers and handle 'spelllang'.
Bram Moolenaar29323592016-07-24 22:04:11 +02002483 FOR_ALL_WINDOWS(wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002484 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002485 // Only load the wordlists when 'spelllang' is set and there is a
2486 // window for this buffer in which 'spell' is set.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002487 if (*wp->w_s->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002488 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002489 if (wp->w_p_spell)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002490 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002491 (void)did_set_spelllang(wp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00002492 break;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002493 }
2494 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002495 }
2496}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002497
Bram Moolenaarb765d632005-06-07 21:00:02 +00002498/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002499 * Open a spell buffer. This is a nameless buffer that is not in the buffer
2500 * list and only contains text lines. Can use a swapfile to reduce memory
2501 * use.
2502 * Most other fields are invalid! Esp. watch out for string options being
2503 * NULL and there is no undo info.
2504 * Returns NULL when out of memory.
2505 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002506 buf_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002507open_spellbuf(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002508{
2509 buf_T *buf;
2510
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002511 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002512 if (buf != NULL)
2513 {
2514 buf->b_spell = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002515 buf->b_p_swf = TRUE; // may create a swap file
Bram Moolenaar706d2de2013-07-17 17:35:13 +02002516#ifdef FEAT_CRYPT
2517 buf->b_p_key = empty_option;
2518#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00002519 ml_open(buf);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002520 ml_open_file(buf); // create swap file now
Bram Moolenaar4770d092006-01-12 23:22:24 +00002521 }
2522 return buf;
2523}
2524
2525/*
2526 * Close the buffer used for spell info.
2527 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002528 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002529close_spellbuf(buf_T *buf)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002530{
2531 if (buf != NULL)
2532 {
2533 ml_close(buf, TRUE);
2534 vim_free(buf);
2535 }
2536}
2537
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002538/*
2539 * Init the chartab used for spelling for ASCII.
2540 * EBCDIC is not supported!
2541 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002542 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002543clear_spell_chartab(spelltab_T *sp)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002544{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002545 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002546
Bram Moolenaara80faa82020-04-12 19:37:17 +02002547 // Init everything to FALSE (zero).
2548 CLEAR_FIELD(sp->st_isw);
2549 CLEAR_FIELD(sp->st_isu);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002550 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002551 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002552 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002553 sp->st_upper[i] = i;
2554 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002555
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002556 // We include digits. A word shouldn't start with a digit, but handling
2557 // that is done separately.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002558 for (i = '0'; i <= '9'; ++i)
2559 sp->st_isw[i] = TRUE;
2560 for (i = 'A'; i <= 'Z'; ++i)
2561 {
2562 sp->st_isw[i] = TRUE;
2563 sp->st_isu[i] = TRUE;
2564 sp->st_fold[i] = i + 0x20;
2565 }
2566 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002567 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002568 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002569 sp->st_upper[i] = i - 0x20;
2570 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002571}
2572
2573/*
2574 * Init the chartab used for spelling. Only depends on 'encoding'.
2575 * Called once while starting up and when 'encoding' changes.
2576 * The default is to use isalpha(), but the spell file should define the word
2577 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002578 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002579 */
2580 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002581init_spell_chartab(void)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002582{
2583 int i;
2584
2585 did_set_spelltab = FALSE;
2586 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002587 if (enc_dbcs)
2588 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002589 // DBCS: assume double-wide characters are word characters.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002590 for (i = 128; i <= 255; ++i)
2591 if (MB_BYTE2LEN(i) == 2)
2592 spelltab.st_isw[i] = TRUE;
2593 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002594 else if (enc_utf8)
2595 {
2596 for (i = 128; i < 256; ++i)
2597 {
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02002598 int f = utf_fold(i);
2599 int u = utf_toupper(i);
2600
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002601 spelltab.st_isu[i] = utf_isupper(i);
2602 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002603 // The folded/upper-cased value is different between latin1 and
2604 // utf8 for 0xb5, causing E763 for no good reason. Use the latin1
2605 // value for utf-8 to avoid this.
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02002606 spelltab.st_fold[i] = (f < 256) ? f : i;
2607 spelltab.st_upper[i] = (u < 256) ? u : i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002608 }
2609 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002610 else
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002611 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002612 // Rough guess: use locale-dependent library functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002613 for (i = 128; i < 256; ++i)
2614 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002615 if (MB_ISUPPER(i))
2616 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002617 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002618 spelltab.st_isu[i] = TRUE;
2619 spelltab.st_fold[i] = MB_TOLOWER(i);
2620 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002621 else if (MB_ISLOWER(i))
2622 {
2623 spelltab.st_isw[i] = TRUE;
2624 spelltab.st_upper[i] = MB_TOUPPER(i);
2625 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002626 }
2627 }
2628}
2629
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002630
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002631/*
Bram Moolenaarea408852005-06-25 22:49:46 +00002632 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002633 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00002634 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002635 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00002636 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02002637 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002638spell_iswordp(
2639 char_u *p,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002640 win_T *wp) // buffer used
Bram Moolenaarea408852005-06-25 22:49:46 +00002641{
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002642 char_u *s;
2643 int l;
2644 int c;
2645
2646 if (has_mbyte)
2647 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002648 l = mb_ptr2len(p);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002649 s = p;
2650 if (l == 1)
2651 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002652 // be quick for ASCII
Bram Moolenaar860cae12010-06-05 23:22:07 +02002653 if (wp->w_s->b_spell_ismw[*p])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002654 s = p + 1; // skip a mid-word character
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002655 }
2656 else
2657 {
2658 c = mb_ptr2char(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002659 if (c < 256 ? wp->w_s->b_spell_ismw[c]
2660 : (wp->w_s->b_spell_ismw_mb != NULL
2661 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002662 s = p + l;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002663 }
2664
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002665 c = mb_ptr2char(s);
2666 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002667 return spell_mb_isword_class(mb_get_class(s), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002668 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002669 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002670
Bram Moolenaar860cae12010-06-05 23:22:07 +02002671 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002672}
2673
2674/*
2675 * Return TRUE if "p" points to a word character.
2676 * Unlike spell_iswordp() this doesn't check for "midword" characters.
2677 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02002678 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002679spell_iswordp_nmw(char_u *p, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002680{
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002681 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002682
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002683 if (has_mbyte)
2684 {
2685 c = mb_ptr2char(p);
2686 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002687 return spell_mb_isword_class(mb_get_class(p), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002688 return spelltab.st_isw[c];
2689 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002690 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00002691}
2692
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002693/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002694 * Return TRUE if word class indicates a word character.
2695 * Only for characters above 255.
2696 * Unicode subscript and superscript are not considered word characters.
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002697 * See also dbcs_class() and utf_class() in mbyte.c.
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002698 */
2699 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002700spell_mb_isword_class(int cl, win_T *wp)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002701{
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002702 if (wp->w_s->b_cjk)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002703 // East Asian characters are not considered word characters.
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002704 return cl == 2 || cl == 0x2800;
Bram Moolenaar06e63772019-07-19 23:04:34 +02002705 return cl >= 2 && cl != 0x2070 && cl != 0x2080 && cl != 3;
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002706}
2707
2708/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002709 * Return TRUE if "p" points to a word character.
2710 * Wide version of spell_iswordp().
2711 */
2712 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002713spell_iswordp_w(int *p, win_T *wp)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002714{
2715 int *s;
2716
Bram Moolenaar860cae12010-06-05 23:22:07 +02002717 if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
2718 : (wp->w_s->b_spell_ismw_mb != NULL
2719 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002720 s = p + 1;
2721 else
2722 s = p;
2723
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002724 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002725 {
2726 if (enc_utf8)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002727 return spell_mb_isword_class(utf_class(*s), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002728 if (enc_dbcs)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002729 return spell_mb_isword_class(
2730 dbcs_class((unsigned)*s >> 8, *s & 0xff), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002731 return 0;
2732 }
2733 return spelltab.st_isw[*s];
2734}
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002735
Bram Moolenaarea408852005-06-25 22:49:46 +00002736/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002737 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
2738 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002739 * When using a multi-byte 'encoding' the length may change!
2740 * Returns FAIL when something wrong.
2741 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002742 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002743spell_casefold(
2744 char_u *str,
2745 int len,
2746 char_u *buf,
2747 int buflen)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002748{
2749 int i;
2750
2751 if (len >= buflen)
2752 {
2753 buf[0] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002754 return FAIL; // result will not fit
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002755 }
2756
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002757 if (has_mbyte)
2758 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002759 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002760 char_u *p;
2761 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002762
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002763 // Fold one character at a time.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002764 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002765 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002766 if (outi + MB_MAXBYTES > buflen)
2767 {
2768 buf[outi] = NUL;
2769 return FAIL;
2770 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002771 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002772 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002773 }
2774 buf[outi] = NUL;
2775 }
2776 else
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002777 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002778 // Be quick for non-multibyte encodings.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002779 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002780 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002781 buf[i] = NUL;
2782 }
2783
2784 return OK;
2785}
2786
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002787/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002788 * Check if the word at line "lnum" column "col" is required to start with a
2789 * capital. This uses 'spellcapcheck' of the current buffer.
2790 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02002791 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002792check_need_cap(linenr_T lnum, colnr_T col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002793{
2794 int need_cap = FALSE;
2795 char_u *line;
2796 char_u *line_copy = NULL;
2797 char_u *p;
2798 colnr_T endcol;
2799 regmatch_T regmatch;
2800
Bram Moolenaar860cae12010-06-05 23:22:07 +02002801 if (curwin->w_s->b_cap_prog == NULL)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002802 return FALSE;
2803
2804 line = ml_get_curline();
2805 endcol = 0;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002806 if (getwhitecols(line) >= (int)col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002807 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002808 // At start of line, check if previous line is empty or sentence
2809 // ends there.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002810 if (lnum == 1)
2811 need_cap = TRUE;
2812 else
2813 {
2814 line = ml_get(lnum - 1);
2815 if (*skipwhite(line) == NUL)
2816 need_cap = TRUE;
2817 else
2818 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002819 // Append a space in place of the line break.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002820 line_copy = concat_str(line, (char_u *)" ");
2821 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002822 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002823 }
2824 }
2825 }
2826 else
2827 endcol = col;
2828
2829 if (endcol > 0)
2830 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002831 // Check if sentence ends before the bad word.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002832 regmatch.regprog = curwin->w_s->b_cap_prog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002833 regmatch.rm_ic = FALSE;
2834 p = line + endcol;
2835 for (;;)
2836 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002837 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002838 if (p == line || spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002839 break;
2840 if (vim_regexec(&regmatch, p, 0)
2841 && regmatch.endp[0] == line + endcol)
2842 {
2843 need_cap = TRUE;
2844 break;
2845 }
2846 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002847 curwin->w_s->b_cap_prog = regmatch.regprog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002848 }
2849
2850 vim_free(line_copy);
2851
2852 return need_cap;
2853}
2854
2855
2856/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002857 * ":spellrepall"
2858 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002859 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002860ex_spellrepall(exarg_T *eap UNUSED)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002861{
2862 pos_T pos = curwin->w_cursor;
2863 char_u *frompat;
2864 int addlen;
2865 char_u *line;
2866 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002867 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002868 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002869
2870 if (repl_from == NULL || repl_to == NULL)
2871 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002872 emsg(_("E752: No previous spell replacement"));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002873 return;
2874 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002875 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002876
Bram Moolenaar964b3742019-05-24 18:54:09 +02002877 frompat = alloc(STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002878 if (frompat == NULL)
2879 return;
2880 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
2881 p_ws = FALSE;
2882
Bram Moolenaar5195e452005-08-19 20:32:47 +00002883 sub_nsubs = 0;
2884 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002885 curwin->w_cursor.lnum = 0;
2886 while (!got_int)
2887 {
Bram Moolenaarc036e872020-02-21 21:30:52 +01002888 if (do_search(NULL, '/', '/', frompat, 1L, SEARCH_KEEP, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002889 || u_save_cursor() == FAIL)
2890 break;
2891
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002892 // Only replace when the right word isn't there yet. This happens
2893 // when changing "etc" to "etc.".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002894 line = ml_get_curline();
2895 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
2896 repl_to, STRLEN(repl_to)) != 0)
2897 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002898 p = alloc(STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002899 if (p == NULL)
2900 break;
2901 mch_memmove(p, line, curwin->w_cursor.col);
2902 STRCPY(p + curwin->w_cursor.col, repl_to);
2903 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
2904 ml_replace(curwin->w_cursor.lnum, p, FALSE);
2905 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002906
2907 if (curwin->w_cursor.lnum != prev_lnum)
2908 {
2909 ++sub_nlines;
2910 prev_lnum = curwin->w_cursor.lnum;
2911 }
2912 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002913 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002914 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002915 }
2916
2917 p_ws = save_ws;
2918 curwin->w_cursor = pos;
2919 vim_free(frompat);
2920
Bram Moolenaar5195e452005-08-19 20:32:47 +00002921 if (sub_nsubs == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002922 semsg(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002923 else
2924 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002925}
2926
2927/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002928 * Make a copy of "word", with the first letter upper or lower cased, to
2929 * "wcopy[MAXWLEN]". "word" must not be empty.
2930 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002931 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002932 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002933onecap_copy(
2934 char_u *word,
2935 char_u *wcopy,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002936 int upper) // TRUE: first letter made upper case
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002937{
2938 char_u *p;
2939 int c;
2940 int l;
2941
2942 p = word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002943 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002944 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002945 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002946 c = *p++;
2947 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002948 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002949 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002950 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002951 if (has_mbyte)
2952 l = mb_char2bytes(c, wcopy);
2953 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002954 {
2955 l = 1;
2956 wcopy[0] = c;
2957 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002958 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002959}
2960
2961/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002962 * Make a copy of "word" with all the letters upper cased into
2963 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002964 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02002965 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002966allcap_copy(char_u *word, char_u *wcopy)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002967{
2968 char_u *s;
2969 char_u *d;
2970 int c;
2971
2972 d = wcopy;
2973 for (s = word; *s != NUL; )
2974 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002975 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002976 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002977 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002978 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +00002979
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002980 // We only change 0xdf to SS when we are certain latin1 is used. It
2981 // would cause weird errors in other 8-bit encodings.
Bram Moolenaar78622822005-08-23 21:00:13 +00002982 if (enc_latin1like && c == 0xdf)
2983 {
2984 c = 'S';
2985 if (d - wcopy >= MAXWLEN - 1)
2986 break;
2987 *d++ = c;
2988 }
2989 else
Bram Moolenaar78622822005-08-23 21:00:13 +00002990 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002991
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002992 if (has_mbyte)
2993 {
2994 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
2995 break;
2996 d += mb_char2bytes(c, d);
2997 }
2998 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002999 {
3000 if (d - wcopy >= MAXWLEN - 1)
3001 break;
3002 *d++ = c;
3003 }
3004 }
3005 *d = NUL;
3006}
3007
3008/*
Bram Moolenaar53805d12005-08-01 07:08:33 +00003009 * Case-folding may change the number of bytes: Count nr of chars in
3010 * fword[flen] and return the byte length of that many chars in "word".
3011 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02003012 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003013nofold_len(char_u *fword, int flen, char_u *word)
Bram Moolenaar53805d12005-08-01 07:08:33 +00003014{
3015 char_u *p;
3016 int i = 0;
3017
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003018 for (p = fword; p < fword + flen; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00003019 ++i;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003020 for (p = word; i > 0; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00003021 --i;
3022 return (int)(p - word);
3023}
Bram Moolenaar53805d12005-08-01 07:08:33 +00003024
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003025/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003026 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003027 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02003028 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003029make_case_word(char_u *fword, char_u *cword, int flags)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003030{
3031 if (flags & WF_ALLCAP)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003032 // Make it all upper-case
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003033 allcap_copy(fword, cword);
3034 else if (flags & WF_ONECAP)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003035 // Make the first letter upper-case
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003036 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003037 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003038 // Use goodword as-is.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003039 STRCPY(cword, fword);
3040}
3041
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003042#if defined(FEAT_EVAL) || defined(PROTO)
3043/*
3044 * Soundfold a string, for soundfold().
3045 * Result is in allocated memory, NULL for an error.
3046 */
3047 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003048eval_soundfold(char_u *word)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003049{
3050 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003051 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003052 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003053
Bram Moolenaar860cae12010-06-05 23:22:07 +02003054 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003055 // Use the sound-folding of the first language that supports it.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003056 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003057 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003058 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003059 if (lp->lp_slang->sl_sal.ga_len > 0)
3060 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003061 // soundfold the word
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003062 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003063 return vim_strsave(sound);
3064 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003065 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003066
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003067 // No language with sound folding, return word as-is.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003068 return vim_strsave(word);
3069}
3070#endif
3071
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003072/*
3073 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +00003074 *
3075 * There are many ways to turn a word into a sound-a-like representation. The
3076 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
3077 * swedish name matching - survey and test of different algorithms" by Klas
3078 * Erikson.
3079 *
3080 * We support two methods:
3081 * 1. SOFOFROM/SOFOTO do a simple character mapping.
3082 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003083 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003084 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003085spell_soundfold(
3086 slang_T *slang,
3087 char_u *inword,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003088 int folded, // "inword" is already case-folded
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003089 char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003090{
3091 char_u fword[MAXWLEN];
3092 char_u *word;
3093
3094 if (slang->sl_sofo)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003095 // SOFOFROM and SOFOTO used
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003096 spell_soundfold_sofo(slang, inword, res);
3097 else
3098 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003099 // SAL items used. Requires the word to be case-folded.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003100 if (folded)
3101 word = inword;
3102 else
3103 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003104 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003105 word = fword;
3106 }
3107
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003108 if (has_mbyte)
3109 spell_soundfold_wsal(slang, word, res);
3110 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003111 spell_soundfold_sal(slang, word, res);
3112 }
3113}
3114
3115/*
3116 * Perform sound folding of "inword" into "res" according to SOFOFROM and
3117 * SOFOTO lines.
3118 */
3119 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003120spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003121{
3122 char_u *s;
3123 int ri = 0;
3124 int c;
3125
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003126 if (has_mbyte)
3127 {
3128 int prevc = 0;
3129 int *ip;
3130
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003131 // The sl_sal_first[] table contains the translation for chars up to
3132 // 255, sl_sal the rest.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003133 for (s = inword; *s != NUL; )
3134 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003135 c = mb_cptr2char_adv(&s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003136 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003137 c = ' ';
3138 else if (c < 256)
3139 c = slang->sl_sal_first[c];
3140 else
3141 {
3142 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003143 if (ip == NULL) // empty list, can't match
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003144 c = NUL;
3145 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003146 for (;;) // find "c" in the list
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003147 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003148 if (*ip == 0) // not found
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003149 {
3150 c = NUL;
3151 break;
3152 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003153 if (*ip == c) // match!
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003154 {
3155 c = ip[1];
3156 break;
3157 }
3158 ip += 2;
3159 }
3160 }
3161
3162 if (c != NUL && c != prevc)
3163 {
3164 ri += mb_char2bytes(c, res + ri);
3165 if (ri + MB_MAXBYTES > MAXWLEN)
3166 break;
3167 prevc = c;
3168 }
3169 }
3170 }
3171 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003172 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003173 // The sl_sal_first[] table contains the translation.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003174 for (s = inword; (c = *s) != NUL; ++s)
3175 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003176 if (VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003177 c = ' ';
3178 else
3179 c = slang->sl_sal_first[c];
3180 if (c != NUL && (ri == 0 || res[ri - 1] != c))
3181 res[ri++] = c;
3182 }
3183 }
3184
3185 res[ri] = NUL;
3186}
3187
3188 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003189spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003190{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003191 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003192 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003193 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003194 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003195 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003196 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003197 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003198 int n, k = 0;
3199 int z0;
3200 int k0;
3201 int n0;
3202 int c;
3203 int pri;
3204 int p0 = -333;
3205 int c0;
3206
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003207 // Remove accents, if wanted. We actually remove all non-word characters.
3208 // But keep white space. We need a copy, the word may be changed here.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003209 if (slang->sl_rem_accents)
3210 {
3211 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003212 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003213 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003214 if (VIM_ISWHITE(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003215 {
3216 *t++ = ' ';
3217 s = skipwhite(s);
3218 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003219 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003220 {
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003221 if (spell_iswordp_nmw(s, curwin))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003222 *t++ = *s;
3223 ++s;
3224 }
3225 }
3226 *t = NUL;
3227 }
3228 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003229 vim_strncpy(word, s, MAXWLEN - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003230
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003231 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003232
3233 /*
3234 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003235 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003236 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003237 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003238 while ((c = word[i]) != NUL)
3239 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003240 // Start with the first rule that has the character in the word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003241 n = slang->sl_sal_first[c];
3242 z0 = 0;
3243
3244 if (n >= 0)
3245 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003246 // check all rules for the same letter
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003247 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003248 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003249 // Quickly skip entries that don't match the word. Most
3250 // entries are less then three chars, optimize for that.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003251 k = smp[n].sm_leadlen;
3252 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003253 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003254 if (word[i + 1] != s[1])
3255 continue;
3256 if (k > 2)
3257 {
3258 for (j = 2; j < k; ++j)
3259 if (word[i + j] != s[j])
3260 break;
3261 if (j < k)
3262 continue;
3263 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003264 }
3265
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003266 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003267 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003268 // Check for match with one of the chars in "sm_oneof".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003269 while (*pf != NUL && *pf != word[i + k])
3270 ++pf;
3271 if (*pf == NUL)
3272 continue;
3273 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003274 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003275 s = smp[n].sm_rules;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003276 pri = 5; // default priority
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003277
3278 p0 = *s;
3279 k0 = k;
3280 while (*s == '-' && k > 1)
3281 {
3282 k--;
3283 s++;
3284 }
3285 if (*s == '<')
3286 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003287 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003288 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003289 // determine priority
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003290 pri = *s - '0';
3291 s++;
3292 }
3293 if (*s == '^' && *(s + 1) == '^')
3294 s++;
3295
3296 if (*s == NUL
3297 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003298 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02003299 || spell_iswordp(word + i - 1, curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003300 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003301 || (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003302 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02003303 && spell_iswordp(word + i - 1, curwin)
3304 && (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003305 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003306 // search for followup rules, if:
3307 // followup and k > 1 and NO '-' in searchstring
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003308 c0 = word[i + k - 1];
3309 n0 = slang->sl_sal_first[c0];
3310
3311 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003312 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003313 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003314 // test follow-up rule for "word[i + k]"
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003315 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003316 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003317 // Quickly skip entries that don't match the word.
3318 //
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003319 k0 = smp[n0].sm_leadlen;
3320 if (k0 > 1)
3321 {
3322 if (word[i + k] != s[1])
3323 continue;
3324 if (k0 > 2)
3325 {
3326 pf = word + i + k + 1;
3327 for (j = 2; j < k0; ++j)
3328 if (*pf++ != s[j])
3329 break;
3330 if (j < k0)
3331 continue;
3332 }
3333 }
3334 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003335
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003336 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003337 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003338 // Check for match with one of the chars in
3339 // "sm_oneof".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003340 while (*pf != NUL && *pf != word[i + k0])
3341 ++pf;
3342 if (*pf == NUL)
3343 continue;
3344 ++k0;
3345 }
3346
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003347 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003348 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003349 while (*s == '-')
3350 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003351 // "k0" gets NOT reduced because
3352 // "if (k0 == k)"
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003353 s++;
3354 }
3355 if (*s == '<')
3356 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003357 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003358 {
3359 p0 = *s - '0';
3360 s++;
3361 }
3362
3363 if (*s == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003364 // *s == '^' cuts
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003365 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003366 && !spell_iswordp(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02003367 curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003368 {
3369 if (k0 == k)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003370 // this is just a piece of the string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003371 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003372
3373 if (p0 < pri)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003374 // priority too low
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003375 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003376 // rule fits; stop search
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003377 break;
3378 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003379 }
3380
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003381 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003382 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003383 }
3384
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003385 // replace string
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003386 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003387 if (s == NULL)
3388 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003389 pf = smp[n].sm_rules;
3390 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003391 if (p0 == 1 && z == 0)
3392 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003393 // rule with '<' is used
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003394 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
3395 || res[reslen - 1] == *s))
3396 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003397 z0 = 1;
3398 z = 1;
3399 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003400 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003401 {
3402 word[i + k0] = *s;
3403 k0++;
3404 s++;
3405 }
3406 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +00003407 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003408
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003409 // new "actual letter"
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003410 c = word[i];
3411 }
3412 else
3413 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003414 // no '<' rule used
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003415 i += k - 1;
3416 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003417 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003418 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003419 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003420 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003421 s++;
3422 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003423 // new "actual letter"
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003424 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003425 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003426 {
3427 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003428 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003429 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003430 i = 0;
3431 z0 = 1;
3432 }
3433 }
3434 break;
3435 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003436 }
3437 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01003438 else if (VIM_ISWHITE(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003439 {
3440 c = ' ';
3441 k = 1;
3442 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003443
3444 if (z0 == 0)
3445 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003446 if (k && !p0 && reslen < MAXWLEN && c != NUL
3447 && (!slang->sl_collapse || reslen == 0
3448 || res[reslen - 1] != c))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003449 // condense only double letters
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003450 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003451
3452 i++;
3453 z = 0;
3454 k = 0;
3455 }
3456 }
3457
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003458 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003459}
3460
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003461/*
3462 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
3463 * Multi-byte version of spell_soundfold().
3464 */
3465 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003466spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003467{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003468 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003469 int word[MAXWLEN];
3470 int wres[MAXWLEN];
3471 int l;
3472 char_u *s;
3473 int *ws;
3474 char_u *t;
3475 int *pf;
3476 int i, j, z;
3477 int reslen;
3478 int n, k = 0;
3479 int z0;
3480 int k0;
3481 int n0;
3482 int c;
3483 int pri;
3484 int p0 = -333;
3485 int c0;
3486 int did_white = FALSE;
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003487 int wordlen;
3488
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003489
3490 /*
3491 * Convert the multi-byte string to a wide-character string.
3492 * Remove accents, if wanted. We actually remove all non-word characters.
3493 * But keep white space.
3494 */
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003495 wordlen = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003496 for (s = inword; *s != NUL; )
3497 {
3498 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003499 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003500 if (slang->sl_rem_accents)
3501 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003502 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003503 {
3504 if (did_white)
3505 continue;
3506 c = ' ';
3507 did_white = TRUE;
3508 }
3509 else
3510 {
3511 did_white = FALSE;
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003512 if (!spell_iswordp_nmw(t, curwin))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003513 continue;
3514 }
3515 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003516 word[wordlen++] = c;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003517 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003518 word[wordlen] = NUL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003519
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003520 /*
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003521 * This algorithm comes from Aspell phonet.cpp.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003522 * Converted from C++ to C. Added support for multi-byte chars.
3523 * Changed to keep spaces.
3524 */
3525 i = reslen = z = 0;
3526 while ((c = word[i]) != NUL)
3527 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003528 // Start with the first rule that has the character in the word.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003529 n = slang->sl_sal_first[c & 0xff];
3530 z0 = 0;
3531
3532 if (n >= 0)
3533 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003534 // Check all rules for the same index byte.
3535 // If c is 0x300 need extra check for the end of the array, as
3536 // (c & 0xff) is NUL.
Bram Moolenaar95e85792010-08-01 15:37:02 +02003537 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff)
3538 && ws[0] != NUL; ++n)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003539 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003540 // Quickly skip entries that don't match the word. Most
3541 // entries are less then three chars, optimize for that.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003542 if (c != ws[0])
3543 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003544 k = smp[n].sm_leadlen;
3545 if (k > 1)
3546 {
3547 if (word[i + 1] != ws[1])
3548 continue;
3549 if (k > 2)
3550 {
3551 for (j = 2; j < k; ++j)
3552 if (word[i + j] != ws[j])
3553 break;
3554 if (j < k)
3555 continue;
3556 }
3557 }
3558
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003559 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003560 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003561 // Check for match with one of the chars in "sm_oneof".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003562 while (*pf != NUL && *pf != word[i + k])
3563 ++pf;
3564 if (*pf == NUL)
3565 continue;
3566 ++k;
3567 }
3568 s = smp[n].sm_rules;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003569 pri = 5; // default priority
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003570
3571 p0 = *s;
3572 k0 = k;
3573 while (*s == '-' && k > 1)
3574 {
3575 k--;
3576 s++;
3577 }
3578 if (*s == '<')
3579 s++;
3580 if (VIM_ISDIGIT(*s))
3581 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003582 // determine priority
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003583 pri = *s - '0';
3584 s++;
3585 }
3586 if (*s == '^' && *(s + 1) == '^')
3587 s++;
3588
3589 if (*s == NUL
3590 || (*s == '^'
3591 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02003592 || spell_iswordp_w(word + i - 1, curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003593 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003594 || (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003595 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02003596 && spell_iswordp_w(word + i - 1, curwin)
3597 && (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003598 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003599 // search for followup rules, if:
3600 // followup and k > 1 and NO '-' in searchstring
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003601 c0 = word[i + k - 1];
3602 n0 = slang->sl_sal_first[c0 & 0xff];
3603
3604 if (slang->sl_followup && k > 1 && n0 >= 0
3605 && p0 != '-' && word[i + k] != NUL)
3606 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003607 // Test follow-up rule for "word[i + k]"; loop over
3608 // all entries with the same index byte.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003609 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
3610 == (c0 & 0xff); ++n0)
3611 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003612 // Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003613 if (c0 != ws[0])
3614 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003615 k0 = smp[n0].sm_leadlen;
3616 if (k0 > 1)
3617 {
3618 if (word[i + k] != ws[1])
3619 continue;
3620 if (k0 > 2)
3621 {
3622 pf = word + i + k + 1;
3623 for (j = 2; j < k0; ++j)
3624 if (*pf++ != ws[j])
3625 break;
3626 if (j < k0)
3627 continue;
3628 }
3629 }
3630 k0 += k - 1;
3631
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003632 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003633 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003634 // Check for match with one of the chars in
3635 // "sm_oneof".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003636 while (*pf != NUL && *pf != word[i + k0])
3637 ++pf;
3638 if (*pf == NUL)
3639 continue;
3640 ++k0;
3641 }
3642
3643 p0 = 5;
3644 s = smp[n0].sm_rules;
3645 while (*s == '-')
3646 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003647 // "k0" gets NOT reduced because
3648 // "if (k0 == k)"
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003649 s++;
3650 }
3651 if (*s == '<')
3652 s++;
3653 if (VIM_ISDIGIT(*s))
3654 {
3655 p0 = *s - '0';
3656 s++;
3657 }
3658
3659 if (*s == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003660 // *s == '^' cuts
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003661 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003662 && !spell_iswordp_w(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02003663 curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003664 {
3665 if (k0 == k)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003666 // this is just a piece of the string
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003667 continue;
3668
3669 if (p0 < pri)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003670 // priority too low
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003671 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003672 // rule fits; stop search
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003673 break;
3674 }
3675 }
3676
3677 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
3678 == (c0 & 0xff))
3679 continue;
3680 }
3681
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003682 // replace string
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003683 ws = smp[n].sm_to_w;
3684 s = smp[n].sm_rules;
3685 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
3686 if (p0 == 1 && z == 0)
3687 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003688 // rule with '<' is used
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003689 if (reslen > 0 && ws != NULL && *ws != NUL
3690 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003691 || wres[reslen - 1] == *ws))
3692 reslen--;
3693 z0 = 1;
3694 z = 1;
3695 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003696 if (ws != NULL)
3697 while (*ws != NUL && word[i + k0] != NUL)
3698 {
3699 word[i + k0] = *ws;
3700 k0++;
3701 ws++;
3702 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003703 if (k > k0)
3704 mch_memmove(word + i + k0, word + i + k,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003705 sizeof(int) * (wordlen - (i + k) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003706
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003707 // new "actual letter"
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003708 c = word[i];
3709 }
3710 else
3711 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003712 // no '<' rule used
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003713 i += k - 1;
3714 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003715 if (ws != NULL)
3716 while (*ws != NUL && ws[1] != NUL
3717 && reslen < MAXWLEN)
3718 {
3719 if (reslen == 0 || wres[reslen - 1] != *ws)
3720 wres[reslen++] = *ws;
3721 ws++;
3722 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003723 // new "actual letter"
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003724 if (ws == NULL)
3725 c = NUL;
3726 else
3727 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003728 if (strstr((char *)s, "^^") != NULL)
3729 {
3730 if (c != NUL)
3731 wres[reslen++] = c;
3732 mch_memmove(word, word + i + 1,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003733 sizeof(int) * (wordlen - (i + 1) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003734 i = 0;
3735 z0 = 1;
3736 }
3737 }
3738 break;
3739 }
3740 }
3741 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01003742 else if (VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003743 {
3744 c = ' ';
3745 k = 1;
3746 }
3747
3748 if (z0 == 0)
3749 {
3750 if (k && !p0 && reslen < MAXWLEN && c != NUL
3751 && (!slang->sl_collapse || reslen == 0
3752 || wres[reslen - 1] != c))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003753 // condense only double letters
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003754 wres[reslen++] = c;
3755
3756 i++;
3757 z = 0;
3758 k = 0;
3759 }
3760 }
3761
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003762 // Convert wide characters in "wres" to a multi-byte string in "res".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003763 l = 0;
3764 for (n = 0; n < reslen; ++n)
3765 {
3766 l += mb_char2bytes(wres[n], res + l);
3767 if (l + MB_MAXBYTES > MAXWLEN)
3768 break;
3769 }
3770 res[l] = NUL;
3771}
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003772
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003773/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003774 * ":spellinfo"
3775 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003776 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003777ex_spellinfo(exarg_T *eap UNUSED)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003778{
3779 int lpi;
3780 langp_T *lp;
3781 char_u *p;
3782
3783 if (no_spell_checking(curwin))
3784 return;
3785
3786 msg_start();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003787 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003788 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003789 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003790 msg_puts("file: ");
3791 msg_puts((char *)lp->lp_slang->sl_fname);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003792 msg_putchar('\n');
3793 p = lp->lp_slang->sl_info;
3794 if (p != NULL)
3795 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003796 msg_puts((char *)p);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003797 msg_putchar('\n');
3798 }
3799 }
3800 msg_end();
3801}
3802
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003803#define DUMPFLAG_KEEPCASE 1 // round 2: keep-case tree
3804#define DUMPFLAG_COUNT 2 // include word count
3805#define DUMPFLAG_ICASE 4 // ignore case when finding matches
3806#define DUMPFLAG_ONECAP 8 // pattern starts with capital
3807#define DUMPFLAG_ALLCAP 16 // pattern is all capitals
Bram Moolenaar4770d092006-01-12 23:22:24 +00003808
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003809/*
3810 * ":spelldump"
3811 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003812 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003813ex_spelldump(exarg_T *eap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003814{
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003815 char_u *spl;
3816 long dummy;
3817
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003818 if (no_spell_checking(curwin))
3819 return;
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003820 get_option_value((char_u*)"spl", &dummy, &spl, OPT_LOCAL);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003821
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003822 // Create a new empty buffer in a new window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003823 do_cmdline_cmd((char_u *)"new");
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003824
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003825 // enable spelling locally in the new window
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003826 set_option_value((char_u*)"spell", TRUE, (char_u*)"", OPT_LOCAL);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003827 set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL);
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003828 vim_free(spl);
3829
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003830 if (!BUFEMPTY())
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003831 return;
3832
Bram Moolenaar860cae12010-06-05 23:22:07 +02003833 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003834
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003835 // Delete the empty line that we started with.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003836 if (curbuf->b_ml.ml_line_count > 1)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003837 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003838
3839 redraw_later(NOT_VALID);
3840}
3841
3842/*
3843 * Go through all possible words and:
3844 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
3845 * "ic" and "dir" are not used.
3846 * 2. When "pat" is not NULL: add matching words to insert mode completion.
3847 */
3848 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003849spell_dump_compl(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003850 char_u *pat, // leading part of the word
3851 int ic, // ignore case
3852 int *dir, // direction for adding matches
3853 int dumpflags_arg) // DUMPFLAG_*
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003854{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003855 langp_T *lp;
3856 slang_T *slang;
3857 idx_T arridx[MAXWLEN];
3858 int curi[MAXWLEN];
3859 char_u word[MAXWLEN];
3860 int c;
3861 char_u *byts;
3862 idx_T *idxs;
3863 linenr_T lnum = 0;
3864 int round;
3865 int depth;
3866 int n;
3867 int flags;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003868 char_u *region_names = NULL; // region names being used
3869 int do_region = TRUE; // dump region names and numbers
Bram Moolenaar7887d882005-07-01 22:33:52 +00003870 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003871 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003872 int dumpflags = dumpflags_arg;
3873 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003874
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003875 // When ignoring case or when the pattern starts with capital pass this on
3876 // to dump_word().
Bram Moolenaard0131a82006-03-04 21:46:13 +00003877 if (pat != NULL)
3878 {
3879 if (ic)
3880 dumpflags |= DUMPFLAG_ICASE;
3881 else
3882 {
3883 n = captype(pat, NULL);
3884 if (n == WF_ONECAP)
3885 dumpflags |= DUMPFLAG_ONECAP;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01003886 else if (n == WF_ALLCAP && (int)STRLEN(pat) > mb_ptr2len(pat))
Bram Moolenaard0131a82006-03-04 21:46:13 +00003887 dumpflags |= DUMPFLAG_ALLCAP;
3888 }
3889 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003890
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003891 // Find out if we can support regions: All languages must support the same
3892 // regions or none at all.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003893 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003894 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003895 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003896 p = lp->lp_slang->sl_regions;
3897 if (p[0] != 0)
3898 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003899 if (region_names == NULL) // first language with regions
Bram Moolenaar7887d882005-07-01 22:33:52 +00003900 region_names = p;
3901 else if (STRCMP(region_names, p) != 0)
3902 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003903 do_region = FALSE; // region names are different
Bram Moolenaar7887d882005-07-01 22:33:52 +00003904 break;
3905 }
3906 }
3907 }
3908
3909 if (do_region && region_names != NULL)
3910 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003911 if (pat == NULL)
3912 {
3913 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
3914 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
3915 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00003916 }
3917 else
3918 do_region = FALSE;
3919
3920 /*
3921 * Loop over all files loaded for the entries in 'spelllang'.
3922 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003923 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003924 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003925 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003926 slang = lp->lp_slang;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003927 if (slang->sl_fbyts == NULL) // reloading failed
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003928 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003929
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003930 if (pat == NULL)
3931 {
3932 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
3933 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
3934 }
3935
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003936 // When matching with a pattern and there are no prefixes only use
3937 // parts of the tree that match "pat".
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003938 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003939 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003940 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003941 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003942
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003943 // round 1: case-folded tree
3944 // round 2: keep-case tree
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003945 for (round = 1; round <= 2; ++round)
3946 {
3947 if (round == 1)
3948 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003949 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003950 byts = slang->sl_fbyts;
3951 idxs = slang->sl_fidxs;
3952 }
3953 else
3954 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003955 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003956 byts = slang->sl_kbyts;
3957 idxs = slang->sl_kidxs;
3958 }
3959 if (byts == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003960 continue; // array is empty
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003961
3962 depth = 0;
3963 arridx[0] = 0;
3964 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003965 while (depth >= 0 && !got_int
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003966 && (pat == NULL || !ins_compl_interrupted()))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003967 {
3968 if (curi[depth] > byts[arridx[depth]])
3969 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003970 // Done all bytes at this node, go up one level.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003971 --depth;
3972 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003973 ins_compl_check_keys(50, FALSE);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003974 }
3975 else
3976 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003977 // Do one more byte at this node.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003978 n = arridx[depth] + curi[depth];
3979 ++curi[depth];
3980 c = byts[n];
3981 if (c == 0)
3982 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003983 // End of word, deal with the word.
3984 // Don't use keep-case words in the fold-case tree,
3985 // they will appear in the keep-case tree.
3986 // Only use the word when the region matches.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003987 flags = (int)idxs[n];
3988 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003989 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +00003990 && (do_region
3991 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003992 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003993 & lp->lp_region) != 0))
3994 {
3995 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003996 if (!do_region)
3997 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003998
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003999 // Dump the basic word if there is no prefix or
4000 // when it's the first one.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004001 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004002 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004003 {
4004 dump_word(slang, word, pat, dir,
4005 dumpflags, flags, lnum);
4006 if (pat == NULL)
4007 ++lnum;
4008 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004009
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004010 // Apply the prefix, if there is one.
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004011 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004012 lnum = dump_prefixes(slang, word, pat, dir,
4013 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004014 }
4015 }
4016 else
4017 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004018 // Normal char, go one level deeper.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004019 word[depth++] = c;
4020 arridx[depth] = idxs[n];
4021 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004022
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004023 // Check if this characters matches with the pattern.
4024 // If not skip the whole tree below it.
4025 // Always ignore case here, dump_word() will check
4026 // proper case later. This isn't exactly right when
4027 // length changes for multi-byte characters with
4028 // ignore case...
Bram Moolenaard0131a82006-03-04 21:46:13 +00004029 if (depth <= patlen
4030 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004031 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004032 }
4033 }
4034 }
4035 }
4036 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004037}
4038
4039/*
4040 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004041 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004042 */
4043 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004044dump_word(
4045 slang_T *slang,
4046 char_u *word,
4047 char_u *pat,
4048 int *dir,
4049 int dumpflags,
4050 int wordflags,
4051 linenr_T lnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004052{
4053 int keepcap = FALSE;
4054 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004055 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004056 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +00004057 char_u badword[MAXWLEN + 10];
4058 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +00004059 int flags = wordflags;
4060
4061 if (dumpflags & DUMPFLAG_ONECAP)
4062 flags |= WF_ONECAP;
4063 if (dumpflags & DUMPFLAG_ALLCAP)
4064 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004065
Bram Moolenaar4770d092006-01-12 23:22:24 +00004066 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004067 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004068 // Need to fix case according to "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004069 make_case_word(word, cword, flags);
4070 p = cword;
4071 }
4072 else
4073 {
4074 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004075 if ((dumpflags & DUMPFLAG_KEEPCASE)
4076 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004077 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004078 keepcap = TRUE;
4079 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004080 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004081
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004082 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004083 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004084 // Add flags and regions after a slash.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004085 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004086 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004087 STRCPY(badword, p);
4088 STRCAT(badword, "/");
4089 if (keepcap)
4090 STRCAT(badword, "=");
4091 if (flags & WF_BANNED)
4092 STRCAT(badword, "!");
4093 else if (flags & WF_RARE)
4094 STRCAT(badword, "?");
4095 if (flags & WF_REGION)
4096 for (i = 0; i < 7; ++i)
4097 if (flags & (0x10000 << i))
4098 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
4099 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004100 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004101
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004102 if (dumpflags & DUMPFLAG_COUNT)
4103 {
4104 hashitem_T *hi;
4105
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004106 // Include the word count for ":spelldump!".
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004107 hi = hash_find(&slang->sl_wordcount, tw);
4108 if (!HASHITEM_EMPTY(hi))
4109 {
4110 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
4111 tw, HI2WC(hi)->wc_count);
4112 p = IObuff;
4113 }
4114 }
4115
4116 ml_append(lnum, p, (colnr_T)0, FALSE);
4117 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00004118 else if (((dumpflags & DUMPFLAG_ICASE)
4119 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
4120 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004121 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004122 p_ic, NULL, *dir, FALSE) == OK)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004123 // if dir was BACKWARD then honor it just once
Bram Moolenaard0131a82006-03-04 21:46:13 +00004124 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004125}
4126
4127/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004128 * For ":spelldump": Find matching prefixes for "word". Prepend each to
4129 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004130 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004131 * Return the updated line number.
4132 */
4133 static linenr_T
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004134dump_prefixes(
4135 slang_T *slang,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004136 char_u *word, // case-folded word
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004137 char_u *pat,
4138 int *dir,
4139 int dumpflags,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004140 int flags, // flags with prefix ID
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004141 linenr_T startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004142{
4143 idx_T arridx[MAXWLEN];
4144 int curi[MAXWLEN];
4145 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +00004146 char_u word_up[MAXWLEN];
4147 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004148 int c;
4149 char_u *byts;
4150 idx_T *idxs;
4151 linenr_T lnum = startlnum;
4152 int depth;
4153 int n;
4154 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004155 int i;
4156
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004157 // If the word starts with a lower-case letter make the word with an
4158 // upper-case letter in word_up[].
Bram Moolenaar53805d12005-08-01 07:08:33 +00004159 c = PTR2CHAR(word);
4160 if (SPELL_TOUPPER(c) != c)
4161 {
4162 onecap_copy(word, word_up, TRUE);
4163 has_word_up = TRUE;
4164 }
4165
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004166 byts = slang->sl_pbyts;
4167 idxs = slang->sl_pidxs;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004168 if (byts != NULL) // array not is empty
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004169 {
4170 /*
4171 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004172 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004173 */
4174 depth = 0;
4175 arridx[0] = 0;
4176 curi[0] = 1;
4177 while (depth >= 0 && !got_int)
4178 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004179 n = arridx[depth];
4180 len = byts[n];
4181 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004182 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004183 // Done all bytes at this node, go up one level.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004184 --depth;
4185 line_breakcheck();
4186 }
4187 else
4188 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004189 // Do one more byte at this node.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004190 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004191 ++curi[depth];
4192 c = byts[n];
4193 if (c == 0)
4194 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004195 // End of prefix, find out how many IDs there are.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004196 for (i = 1; i < len; ++i)
4197 if (byts[n + i] != 0)
4198 break;
4199 curi[depth] += i - 1;
4200
Bram Moolenaar53805d12005-08-01 07:08:33 +00004201 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
4202 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004203 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004204 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004205 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004206 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004207 : flags, lnum);
4208 if (lnum != 0)
4209 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004210 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00004211
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004212 // Check for prefix that matches the word when the
4213 // first letter is upper-case, but only if the prefix has
4214 // a condition.
Bram Moolenaar53805d12005-08-01 07:08:33 +00004215 if (has_word_up)
4216 {
4217 c = valid_word_prefix(i, n, flags, word_up, slang,
4218 TRUE);
4219 if (c != 0)
4220 {
4221 vim_strncpy(prefix + depth, word_up,
4222 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004223 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004224 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004225 : flags, lnum);
4226 if (lnum != 0)
4227 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004228 }
4229 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004230 }
4231 else
4232 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004233 // Normal char, go one level deeper.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004234 prefix[depth++] = c;
4235 arridx[depth] = idxs[n];
4236 curi[depth] = 1;
4237 }
4238 }
4239 }
4240 }
4241
4242 return lnum;
4243}
4244
Bram Moolenaar95529562005-08-25 21:21:38 +00004245/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004246 * Move "p" to the end of word "start".
4247 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +00004248 */
4249 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004250spell_to_word_end(char_u *start, win_T *win)
Bram Moolenaar95529562005-08-25 21:21:38 +00004251{
4252 char_u *p = start;
4253
Bram Moolenaar860cae12010-06-05 23:22:07 +02004254 while (*p != NUL && spell_iswordp(p, win))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004255 MB_PTR_ADV(p);
Bram Moolenaar95529562005-08-25 21:21:38 +00004256 return p;
4257}
4258
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004259/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004260 * For Insert mode completion CTRL-X s:
4261 * Find start of the word in front of column "startcol".
4262 * We don't check if it is badly spelled, with completion we can only change
4263 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004264 * Returns the column number of the word.
4265 */
4266 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004267spell_word_start(int startcol)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004268{
4269 char_u *line;
4270 char_u *p;
4271 int col = 0;
4272
Bram Moolenaar95529562005-08-25 21:21:38 +00004273 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004274 return startcol;
4275
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004276 // Find a word character before "startcol".
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004277 line = ml_get_curline();
4278 for (p = line + startcol; p > line; )
4279 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004280 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004281 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004282 break;
4283 }
4284
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004285 // Go back to start of the word.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004286 while (p > line)
4287 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004288 col = (int)(p - line);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004289 MB_PTR_BACK(line, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004290 if (!spell_iswordp(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004291 break;
4292 col = 0;
4293 }
4294
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004295 return col;
4296}
4297
4298/*
Bram Moolenaar4effc802005-09-30 21:12:02 +00004299 * Need to check for 'spellcapcheck' now, the word is removed before
4300 * expand_spelling() is called. Therefore the ugly global variable.
4301 */
4302static int spell_expand_need_cap;
4303
4304 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004305spell_expand_check_cap(colnr_T col)
Bram Moolenaar4effc802005-09-30 21:12:02 +00004306{
4307 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
4308}
4309
4310/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004311 * Get list of spelling suggestions.
4312 * Used for Insert mode completion CTRL-X ?.
4313 * Returns the number of matches. The matches are in "matchp[]", array of
4314 * allocated strings.
4315 */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004316 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004317expand_spelling(
4318 linenr_T lnum UNUSED,
4319 char_u *pat,
4320 char_u ***matchp)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004321{
4322 garray_T ga;
4323
Bram Moolenaar4770d092006-01-12 23:22:24 +00004324 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004325 *matchp = ga.ga_data;
4326 return ga.ga_len;
4327}
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004328
Bram Moolenaare677df82019-09-02 22:31:11 +02004329/*
Bram Moolenaarf154f3a2020-06-08 18:54:49 +02004330 * Return TRUE if "val" is a valid 'spelllang' value.
Bram Moolenaare677df82019-09-02 22:31:11 +02004331 */
4332 int
Bram Moolenaarf154f3a2020-06-08 18:54:49 +02004333valid_spelllang(char_u *val)
Bram Moolenaare677df82019-09-02 22:31:11 +02004334{
4335 return valid_name(val, ".-_,@");
4336}
4337
4338/*
4339 * Return TRUE if "val" is a valid 'spellfile' value.
4340 */
4341 int
4342valid_spellfile(char_u *val)
4343{
4344 char_u *s;
4345
4346 for (s = val; *s != NUL; ++s)
4347 if (!vim_isfilec(*s) && *s != ',')
4348 return FALSE;
4349 return TRUE;
4350}
4351
4352/*
4353 * Handle side effects of setting 'spell'.
4354 * Return an error message or NULL for success.
4355 */
4356 char *
4357did_set_spell_option(int is_spellfile)
4358{
4359 char *errmsg = NULL;
4360 win_T *wp;
4361 int l;
4362
4363 if (is_spellfile)
4364 {
4365 l = (int)STRLEN(curwin->w_s->b_p_spf);
4366 if (l > 0 && (l < 4
4367 || STRCMP(curwin->w_s->b_p_spf + l - 4, ".add") != 0))
4368 errmsg = e_invarg;
4369 }
4370
4371 if (errmsg == NULL)
4372 {
4373 FOR_ALL_WINDOWS(wp)
4374 if (wp->w_buffer == curbuf && wp->w_p_spell)
4375 {
4376 errmsg = did_set_spelllang(wp);
4377 break;
4378 }
4379 }
4380 return errmsg;
4381}
4382
4383/*
4384 * Set curbuf->b_cap_prog to the regexp program for 'spellcapcheck'.
4385 * Return error message when failed, NULL when OK.
4386 */
4387 char *
4388compile_cap_prog(synblock_T *synblock)
4389{
4390 regprog_T *rp = synblock->b_cap_prog;
4391 char_u *re;
4392
Bram Moolenaar53efb182019-10-13 19:49:26 +02004393 if (synblock->b_p_spc == NULL || *synblock->b_p_spc == NUL)
Bram Moolenaare677df82019-09-02 22:31:11 +02004394 synblock->b_cap_prog = NULL;
4395 else
4396 {
4397 // Prepend a ^ so that we only match at one column
4398 re = concat_str((char_u *)"^", synblock->b_p_spc);
4399 if (re != NULL)
4400 {
4401 synblock->b_cap_prog = vim_regcomp(re, RE_MAGIC);
4402 vim_free(re);
4403 if (synblock->b_cap_prog == NULL)
4404 {
4405 synblock->b_cap_prog = rp; // restore the previous program
4406 return e_invarg;
4407 }
4408 }
4409 }
4410
4411 vim_regfree(rp);
4412 return NULL;
4413}
4414
4415#endif // FEAT_SPELL