blob: dab6aeaf4f6618c10423973e7e0d7e384a35294e [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 Moolenaar402d2fe2005-04-15 21:00:38 +0000176
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100177 // A word never starts at a space or a control character. Return quickly
178 // then, skipping over the character.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000179 if (*ptr <= ' ')
180 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000181
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100182 // Return here when loading language files failed.
Bram Moolenaar860cae12010-06-05 23:22:07 +0200183 if (wp->w_s->b_langp.ga_len == 0)
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000184 return 1;
185
Bram Moolenaara80faa82020-04-12 19:37:17 +0200186 CLEAR_FIELD(mi);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000187
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100188 // A number is always OK. Also skip hexadecimal numbers 0xFF99 and
189 // 0X99FF. But always do check spelling to find "3GPP" and "11
190 // julifeest".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000191 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000192 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100193 if (*ptr == '0' && (ptr[1] == 'b' || ptr[1] == 'B'))
194 mi.mi_end = skipbin(ptr + 2);
195 else if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
Bram Moolenaar3982c542005-06-08 21:56:31 +0000196 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000197 else
198 mi.mi_end = skipdigits(ptr);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000199 nrlen = (int)(mi.mi_end - ptr);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000200 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000201
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100202 // Find the normal end of the word (until the next non-word character).
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000203 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +0000204 mi.mi_fend = ptr;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200205 if (spell_iswordp(mi.mi_fend, wp))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000206 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000207 do
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100208 MB_PTR_ADV(mi.mi_fend);
Bram Moolenaarabab0b02019-03-30 18:47:01 +0100209 while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000210
Bram Moolenaar860cae12010-06-05 23:22:07 +0200211 if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000212 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100213 // Check word starting with capital letter.
Bram Moolenaar53805d12005-08-01 07:08:33 +0000214 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000215 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000216 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000217 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000218 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000219 if (capcol != NULL)
220 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000221
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100222 // We always use the characters up to the next non-word character,
223 // also for bad words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000224 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000225
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100226 // Check caps type later.
Bram Moolenaar860cae12010-06-05 23:22:07 +0200227 mi.mi_capflags = 0;
228 mi.mi_cend = NULL;
229 mi.mi_win = wp;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000230
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100231 // case-fold the word with one non-word character, so that we can check
232 // for the word end.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000233 if (*mi.mi_fend != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100234 MB_PTR_ADV(mi.mi_fend);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000235
236 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
237 MAXWLEN + 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000238 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000239
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100240 // The word is bad unless we recognize it.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000241 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +0000242 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000243
244 /*
245 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000246 * We check them all, because a word may be matched longer in another
247 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000248 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200249 for (lpi = 0; lpi < wp->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000250 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200251 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, lpi);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000252
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100253 // If reloading fails the language is still in the list but everything
254 // has been cleared.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000255 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
256 continue;
257
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100258 // Check for a matching word in case-folded words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000259 find_word(&mi, FIND_FOLDWORD);
260
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100261 // Check for a matching word in keep-case words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000262 find_word(&mi, FIND_KEEPWORD);
263
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100264 // Check for matching prefixes.
Bram Moolenaard12a1322005-08-21 22:08:24 +0000265 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +0000266
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100267 // For a NOBREAK language, may want to use a word without a following
268 // word as a backup.
Bram Moolenaar78622822005-08-23 21:00:13 +0000269 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
270 && mi.mi_result2 != SP_BAD)
271 {
272 mi.mi_result = mi.mi_result2;
273 mi.mi_end = mi.mi_end2;
274 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000275
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100276 // Count the word in the first language where it's found to be OK.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000277 if (count_word && mi.mi_result == SP_OK)
278 {
279 count_common_word(mi.mi_lp->lp_slang, ptr,
280 (int)(mi.mi_end - ptr), 1);
281 count_word = FALSE;
282 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000283 }
284
285 if (mi.mi_result != SP_OK)
286 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100287 // If we found a number skip over it. Allows for "42nd". Do flag
288 // rare and local words, e.g., "3GPP".
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000289 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000290 {
291 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
292 return nrlen;
293 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000294
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100295 // When we are at a non-word character there is no error, just
296 // skip over the character (try looking for a word after it).
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100297 else if (!spell_iswordp_nmw(ptr, wp))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000298 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200299 if (capcol != NULL && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000300 {
301 regmatch_T regmatch;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100302 int r;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000303
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100304 // Check for end of sentence.
Bram Moolenaar860cae12010-06-05 23:22:07 +0200305 regmatch.regprog = wp->w_s->b_cap_prog;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000306 regmatch.rm_ic = FALSE;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100307 r = vim_regexec(&regmatch, ptr, 0);
308 wp->w_s->b_cap_prog = regmatch.regprog;
309 if (r)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000310 *capcol = (int)(regmatch.endp[0] - ptr);
311 }
312
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000313 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000314 return (*mb_ptr2len)(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000315 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000316 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000317 else if (mi.mi_end == ptr)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100318 // Always include at least one character. Required for when there
319 // is a mixup in "midword".
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100320 MB_PTR_ADV(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +0000321 else if (mi.mi_result == SP_BAD
Bram Moolenaar860cae12010-06-05 23:22:07 +0200322 && LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak)
Bram Moolenaar78622822005-08-23 21:00:13 +0000323 {
324 char_u *p, *fp;
325 int save_result = mi.mi_result;
326
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100327 // First language in 'spelllang' is NOBREAK. Find first position
328 // at which any word would be valid.
Bram Moolenaar860cae12010-06-05 23:22:07 +0200329 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000330 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +0000331 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000332 p = mi.mi_word;
333 fp = mi.mi_fword;
334 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +0000335 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100336 MB_PTR_ADV(p);
337 MB_PTR_ADV(fp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000338 if (p >= mi.mi_end)
339 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000340 mi.mi_compoff = (int)(fp - mi.mi_fword);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000341 find_word(&mi, FIND_COMPOUND);
342 if (mi.mi_result != SP_BAD)
343 {
344 mi.mi_end = p;
345 break;
346 }
Bram Moolenaar78622822005-08-23 21:00:13 +0000347 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000348 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +0000349 }
Bram Moolenaar78622822005-08-23 21:00:13 +0000350 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000351
352 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000353 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000354 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000355 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000356 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000357 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000358 }
359
Bram Moolenaar5195e452005-08-19 20:32:47 +0000360 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
361 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100362 // Report SpellCap only when the word isn't badly spelled.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000363 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000364 return wrongcaplen;
365 }
366
Bram Moolenaar51485f02005-06-04 21:55:20 +0000367 return (int)(mi.mi_end - ptr);
368}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000369
Bram Moolenaar51485f02005-06-04 21:55:20 +0000370/*
371 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000372 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
373 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
374 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
375 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000376 *
377 * For a match mip->mi_result is updated.
378 */
379 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100380find_word(matchinf_T *mip, int mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000381{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000382 idx_T arridx = 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100383 int endlen[MAXWLEN]; // length at possible word endings
384 idx_T endidx[MAXWLEN]; // possible word endings
Bram Moolenaar51485f02005-06-04 21:55:20 +0000385 int endidxcnt = 0;
386 int len;
387 int wlen = 0;
388 int flen;
389 int c;
390 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000391 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000392 char_u *s;
Bram Moolenaare52325c2005-08-22 22:54:29 +0000393 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000394 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000395 slang_T *slang = mip->mi_lp->lp_slang;
396 unsigned flags;
397 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000398 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000399 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000400 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +0000401 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000402
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000403 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000404 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100405 // Check for word with matching case in keep-case tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000406 ptr = mip->mi_word;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100407 flen = 9999; // no case folding, always enough bytes
Bram Moolenaar51485f02005-06-04 21:55:20 +0000408 byts = slang->sl_kbyts;
409 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000410
411 if (mode == FIND_KEEPCOMPOUND)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100412 // Skip over the previously found word(s).
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000413 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000414 }
415 else
416 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100417 // Check for case-folded in case-folded tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000418 ptr = mip->mi_fword;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100419 flen = mip->mi_fwordlen; // available case-folded bytes
Bram Moolenaar51485f02005-06-04 21:55:20 +0000420 byts = slang->sl_fbyts;
421 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000422
423 if (mode == FIND_PREFIX)
424 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100425 // Skip over the prefix.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000426 wlen = mip->mi_prefixlen;
427 flen -= mip->mi_prefixlen;
428 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000429 else if (mode == FIND_COMPOUND)
430 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100431 // Skip over the previously found word(s).
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000432 wlen = mip->mi_compoff;
433 flen -= mip->mi_compoff;
434 }
435
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000436 }
437
Bram Moolenaar51485f02005-06-04 21:55:20 +0000438 if (byts == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100439 return; // array is empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000440
Bram Moolenaar51485f02005-06-04 21:55:20 +0000441 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000442 * Repeat advancing in the tree until:
443 * - there is a byte that doesn't match,
444 * - we reach the end of the tree,
445 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000446 */
447 for (;;)
448 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000449 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000450 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000451
452 len = byts[arridx++];
453
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100454 // If the first possible byte is a zero the word could end here.
455 // Remember this index, we first check for the longest word.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000456 if (byts[arridx] == 0)
457 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000458 if (endidxcnt == MAXWLEN)
459 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100460 // Must be a corrupted spell file.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100461 emsg(_(e_format));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000462 return;
463 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000464 endlen[endidxcnt] = wlen;
465 endidx[endidxcnt++] = arridx++;
466 --len;
467
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100468 // Skip over the zeros, there can be several flag/region
469 // combinations.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000470 while (len > 0 && byts[arridx] == 0)
471 {
472 ++arridx;
473 --len;
474 }
475 if (len == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100476 break; // no children, word must end here
Bram Moolenaar51485f02005-06-04 21:55:20 +0000477 }
478
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100479 // Stop looking at end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000480 if (ptr[wlen] == NUL)
481 break;
482
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100483 // Perform a binary search in the list of accepted bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000484 c = ptr[wlen];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100485 if (c == TAB) // <Tab> is handled like <Space>
Bram Moolenaar0c405862005-06-22 22:26:26 +0000486 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +0000487 lo = arridx;
488 hi = arridx + len - 1;
489 while (lo < hi)
490 {
491 m = (lo + hi) / 2;
492 if (byts[m] > c)
493 hi = m - 1;
494 else if (byts[m] < c)
495 lo = m + 1;
496 else
497 {
498 lo = hi = m;
499 break;
500 }
501 }
502
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100503 // Stop if there is no matching byte.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000504 if (hi < lo || byts[lo] != c)
505 break;
506
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100507 // Continue at the child (if there is one).
Bram Moolenaar51485f02005-06-04 21:55:20 +0000508 arridx = idxs[lo];
509 ++wlen;
510 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +0000511
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100512 // One space in the good word may stand for several spaces in the
513 // checked word.
Bram Moolenaar0c405862005-06-22 22:26:26 +0000514 if (c == ' ')
515 {
516 for (;;)
517 {
518 if (flen <= 0 && *mip->mi_fend != NUL)
519 flen = fold_more(mip);
520 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
521 break;
522 ++wlen;
523 --flen;
524 }
525 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000526 }
527
528 /*
529 * Verify that one of the possible endings is valid. Try the longest
530 * first.
531 */
532 while (endidxcnt > 0)
533 {
534 --endidxcnt;
535 arridx = endidx[endidxcnt];
536 wlen = endlen[endidxcnt];
537
Bram Moolenaar51485f02005-06-04 21:55:20 +0000538 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100539 continue; // not at first byte of character
Bram Moolenaar860cae12010-06-05 23:22:07 +0200540 if (spell_iswordp(ptr + wlen, mip->mi_win))
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000541 {
Bram Moolenaar78622822005-08-23 21:00:13 +0000542 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100543 continue; // next char is a word character
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000544 word_ends = FALSE;
545 }
546 else
547 word_ends = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100548 // The prefix flag is before compound flags. Once a valid prefix flag
549 // has been found we try compound flags.
Bram Moolenaard12a1322005-08-21 22:08:24 +0000550 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000551
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000552 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000553 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100554 // Compute byte length in original word, length may change
555 // when folding case. This can be slow, take a shortcut when the
556 // case-folded word is equal to the keep-case word.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000557 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000558 if (STRNCMP(ptr, p, wlen) != 0)
559 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100560 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s))
561 MB_PTR_ADV(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000562 wlen = (int)(p - mip->mi_word);
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000563 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000564 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000565
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100566 // Check flags and region. For FIND_PREFIX check the condition and
567 // prefix ID.
568 // Repeat this if there are more flags/region alternatives until there
569 // is a match.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000570 res = SP_BAD;
571 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
572 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000573 {
574 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000575
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100576 // For the fold-case tree check that the case of the checked word
577 // matches with what the word in the tree requires.
578 // For keep-case tree the case is always right. For prefixes we
579 // don't bother to check.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000580 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000581 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000582 if (mip->mi_cend != mip->mi_word + wlen)
583 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100584 // mi_capflags was set for a different word length, need
585 // to do it again.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000586 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000587 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000588 }
589
Bram Moolenaar0c405862005-06-22 22:26:26 +0000590 if (mip->mi_capflags == WF_KEEPCAP
591 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000592 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000593 }
594
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100595 // When mode is FIND_PREFIX the word must support the prefix:
596 // check the prefix ID and the condition. Do that for the list at
597 // mip->mi_prefarridx that find_prefix() filled.
Bram Moolenaard12a1322005-08-21 22:08:24 +0000598 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000599 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000600 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000601 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +0000602 mip->mi_word + mip->mi_cprefixlen, slang,
603 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000604 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000605 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000606
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100607 // Use the WF_RARE flag for a rare prefix.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000608 if (c & WF_RAREPFX)
609 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000610 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000611 }
612
Bram Moolenaar78622822005-08-23 21:00:13 +0000613 if (slang->sl_nobreak)
614 {
615 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
616 && (flags & WF_BANNED) == 0)
617 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100618 // NOBREAK: found a valid following word. That's all we
619 // need to know, so return.
Bram Moolenaar78622822005-08-23 21:00:13 +0000620 mip->mi_result = SP_OK;
621 break;
622 }
623 }
624
625 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
626 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000627 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100628 // If there is no compound flag or the word is shorter than
629 // COMPOUNDMIN reject it quickly.
630 // Makes you wonder why someone puts a compound flag on a word
631 // that's too short... Myspell compatibility requires this
632 // anyway.
Bram Moolenaare52325c2005-08-22 22:54:29 +0000633 if (((unsigned)flags >> 24) == 0
634 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000635 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100636 // For multi-byte chars check character length against
637 // COMPOUNDMIN.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000638 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000639 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000640 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
641 wlen - mip->mi_compoff) < slang->sl_compminlen)
642 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000643
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100644 // Limit the number of compound words to COMPOUNDWORDMAX if no
645 // maximum for syllables is specified.
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000646 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
647 > slang->sl_compmax
Bram Moolenaare52325c2005-08-22 22:54:29 +0000648 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000649 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000650
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100651 // Don't allow compounding on a side where an affix was added,
652 // unless COMPOUNDPERMITFLAG was used.
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000653 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
654 continue;
655 if (!word_ends && (flags & WF_NOCOMPAFT))
656 continue;
657
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100658 // Quickly check if compounding is possible with this flag.
Bram Moolenaar6de68532005-08-24 22:08:48 +0000659 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +0000660 ? slang->sl_compstartflags
661 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +0000662 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000663 continue;
664
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100665 // If there is a match with a CHECKCOMPOUNDPATTERN rule
666 // discard the compound word.
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000667 if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat))
668 continue;
669
Bram Moolenaare52325c2005-08-22 22:54:29 +0000670 if (mode == FIND_COMPOUND)
671 {
672 int capflags;
673
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100674 // Need to check the caps type of the appended compound
675 // word.
Bram Moolenaare52325c2005-08-22 22:54:29 +0000676 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
677 mip->mi_compoff) != 0)
678 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100679 // case folding may have changed the length
Bram Moolenaare52325c2005-08-22 22:54:29 +0000680 p = mip->mi_word;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100681 for (s = ptr; s < ptr + mip->mi_compoff; MB_PTR_ADV(s))
682 MB_PTR_ADV(p);
Bram Moolenaare52325c2005-08-22 22:54:29 +0000683 }
684 else
Bram Moolenaare52325c2005-08-22 22:54:29 +0000685 p = mip->mi_word + mip->mi_compoff;
686 capflags = captype(p, mip->mi_word + wlen);
687 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
688 && (flags & WF_FIXCAP) != 0))
689 continue;
690
691 if (capflags != WF_ALLCAP)
692 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100693 // When the character before the word is a word
694 // character we do not accept a Onecap word. We do
695 // accept a no-caps word, even when the dictionary
696 // word specifies ONECAP.
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100697 MB_PTR_BACK(mip->mi_word, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100698 if (spell_iswordp_nmw(p, mip->mi_win)
Bram Moolenaare52325c2005-08-22 22:54:29 +0000699 ? capflags == WF_ONECAP
700 : (flags & WF_ONECAP) != 0
701 && capflags != WF_ONECAP)
702 continue;
703 }
704 }
705
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100706 // If the word ends the sequence of compound flags of the
707 // words must match with one of the COMPOUNDRULE items and
708 // the number of syllables must not be too large.
Bram Moolenaar5195e452005-08-19 20:32:47 +0000709 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
710 mip->mi_compflags[mip->mi_complen + 1] = NUL;
711 if (word_ends)
712 {
713 char_u fword[MAXWLEN];
714
715 if (slang->sl_compsylmax < MAXWLEN)
716 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100717 // "fword" is only needed for checking syllables.
Bram Moolenaar5195e452005-08-19 20:32:47 +0000718 if (ptr == mip->mi_word)
719 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
720 else
721 vim_strncpy(fword, ptr, endlen[endidxcnt]);
722 }
723 if (!can_compound(slang, fword, mip->mi_compflags))
724 continue;
725 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000726 else if (slang->sl_comprules != NULL
727 && !match_compoundrule(slang, mip->mi_compflags))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100728 // The compound flags collected so far do not match any
729 // COMPOUNDRULE, discard the compounded word.
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000730 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000731 }
732
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100733 // Check NEEDCOMPOUND: can't use word without compounding.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000734 else if (flags & WF_NEEDCOMP)
735 continue;
736
Bram Moolenaar78622822005-08-23 21:00:13 +0000737 nobreak_result = SP_OK;
738
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000739 if (!word_ends)
740 {
Bram Moolenaar78622822005-08-23 21:00:13 +0000741 int save_result = mip->mi_result;
742 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000743 langp_T *save_lp = mip->mi_lp;
744 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +0000745
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100746 // Check that a valid word follows. If there is one and we
747 // are compounding, it will set "mi_result", thus we are
748 // always finished here. For NOBREAK we only check that a
749 // valid word follows.
750 // Recursive!
Bram Moolenaar78622822005-08-23 21:00:13 +0000751 if (slang->sl_nobreak)
752 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000753
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100754 // Find following word in case-folded tree.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000755 mip->mi_compoff = endlen[endidxcnt];
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000756 if (has_mbyte && mode == FIND_KEEPWORD)
757 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100758 // Compute byte length in case-folded word from "wlen":
759 // byte length in keep-case word. Length may change when
760 // folding case. This can be slow, take a shortcut when
761 // the case-folded word is equal to the keep-case word.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000762 p = mip->mi_fword;
763 if (STRNCMP(ptr, p, wlen) != 0)
764 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100765 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s))
766 MB_PTR_ADV(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000767 mip->mi_compoff = (int)(p - mip->mi_fword);
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000768 }
769 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100770#if 0 // Disabled, see below
Bram Moolenaard12a1322005-08-21 22:08:24 +0000771 c = mip->mi_compoff;
Bram Moolenaarba534352016-04-21 09:20:26 +0200772#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000773 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000774 if (flags & WF_COMPROOT)
775 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000776
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100777 // For NOBREAK we need to try all NOBREAK languages, at least
778 // to find the ".add" file(s).
Bram Moolenaar860cae12010-06-05 23:22:07 +0200779 for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +0000780 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000781 if (slang->sl_nobreak)
782 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200783 mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi);
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000784 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
785 || !mip->mi_lp->lp_slang->sl_nobreak)
786 continue;
787 }
Bram Moolenaard12a1322005-08-21 22:08:24 +0000788
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000789 find_word(mip, FIND_COMPOUND);
790
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100791 // When NOBREAK any word that matches is OK. Otherwise we
792 // need to find the longest match, thus try with keep-case
793 // and prefix too.
Bram Moolenaar78622822005-08-23 21:00:13 +0000794 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
795 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100796 // Find following word in keep-case tree.
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000797 mip->mi_compoff = wlen;
798 find_word(mip, FIND_KEEPCOMPOUND);
799
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100800#if 0 // Disabled, a prefix must not appear halfway a compound word,
801 // unless the COMPOUNDPERMITFLAG is used and then it can't be a
802 // postponed prefix.
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000803 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
804 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100805 // Check for following word with prefix.
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000806 mip->mi_compoff = c;
807 find_prefix(mip, FIND_COMPOUND);
808 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000809#endif
Bram Moolenaar78622822005-08-23 21:00:13 +0000810 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000811
812 if (!slang->sl_nobreak)
813 break;
Bram Moolenaar78622822005-08-23 21:00:13 +0000814 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000815 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000816 if (flags & WF_COMPROOT)
817 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000818 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000819
Bram Moolenaar78622822005-08-23 21:00:13 +0000820 if (slang->sl_nobreak)
821 {
822 nobreak_result = mip->mi_result;
823 mip->mi_result = save_result;
824 mip->mi_end = save_end;
825 }
826 else
827 {
828 if (mip->mi_result == SP_OK)
829 break;
830 continue;
831 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000832 }
833
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000834 if (flags & WF_BANNED)
835 res = SP_BANNED;
836 else if (flags & WF_REGION)
837 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100838 // Check region.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000839 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000840 res = SP_OK;
841 else
842 res = SP_LOCAL;
843 }
844 else if (flags & WF_RARE)
845 res = SP_RARE;
846 else
847 res = SP_OK;
848
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100849 // Always use the longest match and the best result. For NOBREAK
850 // we separately keep the longest match without a following good
851 // word as a fall-back.
Bram Moolenaar78622822005-08-23 21:00:13 +0000852 if (nobreak_result == SP_BAD)
853 {
854 if (mip->mi_result2 > res)
855 {
856 mip->mi_result2 = res;
857 mip->mi_end2 = mip->mi_word + wlen;
858 }
859 else if (mip->mi_result2 == res
860 && mip->mi_end2 < mip->mi_word + wlen)
861 mip->mi_end2 = mip->mi_word + wlen;
862 }
863 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000864 {
865 mip->mi_result = res;
866 mip->mi_end = mip->mi_word + wlen;
867 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000868 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000869 mip->mi_end = mip->mi_word + wlen;
870
Bram Moolenaar78622822005-08-23 21:00:13 +0000871 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000872 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000873 }
874
Bram Moolenaar78622822005-08-23 21:00:13 +0000875 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000876 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000877 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000878}
879
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000880/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000881 * Return TRUE if there is a match between the word ptr[wlen] and
882 * CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another
883 * word.
884 * A match means that the first part of CHECKCOMPOUNDPATTERN matches at the
885 * end of ptr[wlen] and the second part matches after it.
886 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +0200887 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100888match_checkcompoundpattern(
889 char_u *ptr,
890 int wlen,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100891 garray_T *gap) // &sl_comppat
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000892{
893 int i;
894 char_u *p;
895 int len;
896
897 for (i = 0; i + 1 < gap->ga_len; i += 2)
898 {
899 p = ((char_u **)gap->ga_data)[i + 1];
900 if (STRNCMP(ptr + wlen, p, STRLEN(p)) == 0)
901 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100902 // Second part matches at start of following compound word, now
903 // check if first part matches at end of previous word.
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000904 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar19c9c762008-12-09 21:34:39 +0000905 len = (int)STRLEN(p);
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000906 if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0)
907 return TRUE;
908 }
909 }
910 return FALSE;
911}
912
913/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000914 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
915 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000916 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +0200917 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100918can_compound(slang_T *slang, char_u *word, char_u *flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000919{
Bram Moolenaar6de68532005-08-24 22:08:48 +0000920 char_u uflags[MAXWLEN * 2];
921 int i;
Bram Moolenaar6de68532005-08-24 22:08:48 +0000922 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000923
924 if (slang->sl_compprog == NULL)
925 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +0000926 if (enc_utf8)
927 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100928 // Need to convert the single byte flags to utf8 characters.
Bram Moolenaar6de68532005-08-24 22:08:48 +0000929 p = uflags;
930 for (i = 0; flags[i] != NUL; ++i)
Bram Moolenaarace95982017-03-29 17:30:27 +0200931 p += utf_char2bytes(flags[i], p);
Bram Moolenaar6de68532005-08-24 22:08:48 +0000932 *p = NUL;
933 p = uflags;
934 }
935 else
Bram Moolenaar6de68532005-08-24 22:08:48 +0000936 p = flags;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100937 if (!vim_regexec_prog(&slang->sl_compprog, FALSE, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000938 return FALSE;
939
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100940 // Count the number of syllables. This may be slow, do it last. If there
941 // are too many syllables AND the number of compound words is above
942 // COMPOUNDWORDMAX then compounding is not allowed.
Bram Moolenaar5195e452005-08-19 20:32:47 +0000943 if (slang->sl_compsylmax < MAXWLEN
944 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +0000945 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000946 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000947}
948
949/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000950 * Return TRUE if the compound flags in compflags[] match the start of any
951 * compound rule. This is used to stop trying a compound if the flags
952 * collected so far can't possibly match any compound rule.
953 * Caller must check that slang->sl_comprules is not NULL.
954 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +0200955 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100956match_compoundrule(slang_T *slang, char_u *compflags)
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000957{
958 char_u *p;
959 int i;
960 int c;
961
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100962 // loop over all the COMPOUNDRULE entries
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000963 for (p = slang->sl_comprules; *p != NUL; ++p)
964 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100965 // loop over the flags in the compound word we have made, match
966 // them against the current rule entry
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000967 for (i = 0; ; ++i)
968 {
969 c = compflags[i];
970 if (c == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100971 // found a rule that matches for the flags we have so far
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000972 return TRUE;
973 if (*p == '/' || *p == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100974 break; // end of rule, it's too short
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000975 if (*p == '[')
976 {
977 int match = FALSE;
978
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100979 // compare against all the flags in []
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000980 ++p;
981 while (*p != ']' && *p != NUL)
982 if (*p++ == c)
983 match = TRUE;
984 if (!match)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100985 break; // none matches
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000986 }
987 else if (*p != c)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100988 break; // flag of word doesn't match flag in pattern
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000989 ++p;
990 }
991
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100992 // Skip to the next "/", where the next pattern starts.
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000993 p = vim_strchr(p, '/');
994 if (p == NULL)
995 break;
996 }
997
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100998 // Checked all the rules and none of them match the flags, so there
999 // can't possibly be a compound starting with these flags.
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001000 return FALSE;
1001}
1002
1003/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001004 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1005 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001006 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001007 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02001008 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001009valid_word_prefix(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001010 int totprefcnt, // nr of prefix IDs
1011 int arridx, // idx in sl_pidxs[]
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001012 int flags,
1013 char_u *word,
1014 slang_T *slang,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001015 int cond_req) // only use prefixes with a condition
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001016{
1017 int prefcnt;
1018 int pidx;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001019 regprog_T **rp;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001020 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001021
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001022 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001023 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1024 {
1025 pidx = slang->sl_pidxs[arridx + prefcnt];
1026
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001027 // Check the prefix ID.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001028 if (prefid != (pidx & 0xff))
1029 continue;
1030
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001031 // Check if the prefix doesn't combine and the word already has a
1032 // suffix.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001033 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1034 continue;
1035
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001036 // Check the condition, if there is one. The condition index is
1037 // stored in the two bytes above the prefix ID byte.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001038 rp = &slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
1039 if (*rp != NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001040 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001041 if (!vim_regexec_prog(rp, FALSE, word, 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001042 continue;
1043 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001044 else if (cond_req)
1045 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001046
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001047 // It's a match! Return the WF_ flags.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001048 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001049 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001050 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001051}
1052
1053/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001054 * Check if the word at "mip->mi_word" has a matching prefix.
1055 * If it does, then check the following word.
1056 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001057 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1058 * prefix in a compound word.
1059 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001060 * For a match mip->mi_result is updated.
1061 */
1062 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001063find_prefix(matchinf_T *mip, int mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001064{
1065 idx_T arridx = 0;
1066 int len;
1067 int wlen = 0;
1068 int flen;
1069 int c;
1070 char_u *ptr;
1071 idx_T lo, hi, m;
1072 slang_T *slang = mip->mi_lp->lp_slang;
1073 char_u *byts;
1074 idx_T *idxs;
1075
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001076 byts = slang->sl_pbyts;
1077 if (byts == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001078 return; // array is empty
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001079
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001080 // We use the case-folded word here, since prefixes are always
1081 // case-folded.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001082 ptr = mip->mi_fword;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001083 flen = mip->mi_fwordlen; // available case-folded bytes
Bram Moolenaard12a1322005-08-21 22:08:24 +00001084 if (mode == FIND_COMPOUND)
1085 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001086 // Skip over the previously found word(s).
Bram Moolenaard12a1322005-08-21 22:08:24 +00001087 ptr += mip->mi_compoff;
1088 flen -= mip->mi_compoff;
1089 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001090 idxs = slang->sl_pidxs;
1091
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001092 /*
1093 * Repeat advancing in the tree until:
1094 * - there is a byte that doesn't match,
1095 * - we reach the end of the tree,
1096 * - or we reach the end of the line.
1097 */
1098 for (;;)
1099 {
1100 if (flen == 0 && *mip->mi_fend != NUL)
1101 flen = fold_more(mip);
1102
1103 len = byts[arridx++];
1104
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001105 // If the first possible byte is a zero the prefix could end here.
1106 // Check if the following word matches and supports the prefix.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001107 if (byts[arridx] == 0)
1108 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001109 // There can be several prefixes with different conditions. We
1110 // try them all, since we don't know which one will give the
1111 // longest match. The word is the same each time, pass the list
1112 // of possible prefixes to find_word().
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001113 mip->mi_prefarridx = arridx;
1114 mip->mi_prefcnt = len;
1115 while (len > 0 && byts[arridx] == 0)
1116 {
1117 ++arridx;
1118 --len;
1119 }
1120 mip->mi_prefcnt -= len;
1121
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001122 // Find the word that comes after the prefix.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001123 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001124 if (mode == FIND_COMPOUND)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001125 // Skip over the previously found word(s).
Bram Moolenaard12a1322005-08-21 22:08:24 +00001126 mip->mi_prefixlen += mip->mi_compoff;
1127
Bram Moolenaar53805d12005-08-01 07:08:33 +00001128 if (has_mbyte)
1129 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001130 // Case-folded length may differ from original length.
Bram Moolenaard12a1322005-08-21 22:08:24 +00001131 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1132 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001133 }
1134 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001135 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001136 find_word(mip, FIND_PREFIX);
1137
1138
1139 if (len == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001140 break; // no children, word must end here
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001141 }
1142
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001143 // Stop looking at end of the line.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001144 if (ptr[wlen] == NUL)
1145 break;
1146
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001147 // Perform a binary search in the list of accepted bytes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001148 c = ptr[wlen];
1149 lo = arridx;
1150 hi = arridx + len - 1;
1151 while (lo < hi)
1152 {
1153 m = (lo + hi) / 2;
1154 if (byts[m] > c)
1155 hi = m - 1;
1156 else if (byts[m] < c)
1157 lo = m + 1;
1158 else
1159 {
1160 lo = hi = m;
1161 break;
1162 }
1163 }
1164
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001165 // Stop if there is no matching byte.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001166 if (hi < lo || byts[lo] != c)
1167 break;
1168
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001169 // Continue at the child (if there is one).
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001170 arridx = idxs[lo];
1171 ++wlen;
1172 --flen;
1173 }
1174}
1175
1176/*
1177 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001178 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001179 * Return the length of the folded chars in bytes.
1180 */
1181 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001182fold_more(matchinf_T *mip)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001183{
1184 int flen;
1185 char_u *p;
1186
1187 p = mip->mi_fend;
1188 do
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001189 MB_PTR_ADV(mip->mi_fend);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001190 while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001191
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001192 // Include the non-word character so that we can check for the word end.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001193 if (*mip->mi_fend != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001194 MB_PTR_ADV(mip->mi_fend);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001195
1196 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1197 mip->mi_fword + mip->mi_fwordlen,
1198 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001199 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001200 mip->mi_fwordlen += flen;
1201 return flen;
1202}
1203
1204/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001205 * Check case flags for a word. Return TRUE if the word has the requested
1206 * case.
1207 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02001208 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001209spell_valid_case(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001210 int wordflags, // flags for the checked word.
1211 int treeflags) // flags for the word in the spell tree
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001212{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001213 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001214 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001215 && ((treeflags & WF_ONECAP) == 0
1216 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001217}
1218
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001219/*
1220 * Return TRUE if spell checking is not enabled.
1221 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02001222 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001223no_spell_checking(win_T *wp)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001224{
Bram Moolenaar860cae12010-06-05 23:22:07 +02001225 if (!wp->w_p_spell || *wp->w_s->b_p_spl == NUL
1226 || wp->w_s->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001227 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001228 emsg(_("E756: Spell checking is not enabled"));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001229 return TRUE;
1230 }
1231 return FALSE;
1232}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001233
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001234/*
1235 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001236 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
1237 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001238 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1239 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00001240 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001241 */
1242 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001243spell_move_to(
1244 win_T *wp,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001245 int dir, // FORWARD or BACKWARD
1246 int allwords, // TRUE for "[s"/"]s", FALSE for "[S"/"]S"
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001247 int curline,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001248 hlf_T *attrp) // return: attributes of bad word or NULL
1249 // (only when "dir" is FORWARD)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001250{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001251 linenr_T lnum;
1252 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001253 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001254 char_u *line;
1255 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001256 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001257 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001258 int len;
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001259#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001260 int has_syntax = syntax_present(wp);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001261#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001262 int col;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001263 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001264 char_u *buf = NULL;
1265 int buflen = 0;
1266 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001267 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001268 int found_one = FALSE;
1269 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001270
Bram Moolenaar95529562005-08-25 21:21:38 +00001271 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00001272 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001273
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001274 /*
1275 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00001276 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001277 *
1278 * When searching backwards, we continue in the line to find the last
1279 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001280 *
1281 * We concatenate the start of the next line, so that wrapped words work
1282 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1283 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001284 */
Bram Moolenaar95529562005-08-25 21:21:38 +00001285 lnum = wp->w_cursor.lnum;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001286 CLEAR_POS(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001287
1288 while (!got_int)
1289 {
Bram Moolenaar95529562005-08-25 21:21:38 +00001290 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001291
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001292 len = (int)STRLEN(line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001293 if (buflen < len + MAXWLEN + 2)
1294 {
1295 vim_free(buf);
1296 buflen = len + MAXWLEN + 2;
1297 buf = alloc(buflen);
1298 if (buf == NULL)
1299 break;
1300 }
1301
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001302 // In first line check first word for Capital.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001303 if (lnum == 1)
1304 capcol = 0;
1305
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001306 // For checking first word with a capital skip white space.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001307 if (capcol == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001308 capcol = getwhitecols(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001309 else if (curline && wp == curwin)
1310 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001311 // For spellbadword(): check if first word needs a capital.
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001312 col = getwhitecols(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001313 if (check_need_cap(lnum, col))
1314 capcol = col;
1315
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001316 // Need to get the line again, may have looked at the previous
1317 // one.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001318 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1319 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001320
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001321 // Copy the line into "buf" and append the start of the next line if
1322 // possible.
Bram Moolenaar0c405862005-06-22 22:26:26 +00001323 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00001324 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar5dd95a12006-05-13 12:09:24 +00001325 spell_cat_line(buf + STRLEN(buf),
1326 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001327
1328 p = buf + skip;
1329 endp = buf + len;
1330 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001331 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001332 // When searching backward don't search after the cursor. Unless
1333 // we wrapped around the end of the buffer.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001334 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00001335 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001336 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00001337 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001338 break;
1339
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001340 // start of word
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001341 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001342 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001343
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001344 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001345 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001346 // We found a bad word. Check the attribute.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001347 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001348 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001349 // When searching forward only accept a bad word after
1350 // the cursor.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001351 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001352 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00001353 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001354 && (wrapped
1355 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00001356 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001357 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001358 {
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001359#ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00001360 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001361 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001362 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001363 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00001364 FALSE, &can_spell, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001365 if (!can_spell)
1366 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001367 }
1368 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001369#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001370 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001371
Bram Moolenaar51485f02005-06-04 21:55:20 +00001372 if (can_spell)
1373 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00001374 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001375 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001376 found_pos.col = (int)(p - buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001377 found_pos.coladd = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001378 if (dir == FORWARD)
1379 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001380 // No need to search further.
Bram Moolenaar95529562005-08-25 21:21:38 +00001381 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001382 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001383 if (attrp != NULL)
1384 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001385 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001386 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001387 else if (curline)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001388 // Insert mode completion: put cursor after
1389 // the bad word.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001390 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001391 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001392 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001393 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00001394 else
1395 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001396 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001397 }
1398
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001399 // advance to character after the word
Bram Moolenaar51485f02005-06-04 21:55:20 +00001400 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001401 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001402 }
1403
Bram Moolenaar5195e452005-08-19 20:32:47 +00001404 if (dir == BACKWARD && found_pos.lnum != 0)
1405 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001406 // Use the last match in the line (before the cursor).
Bram Moolenaar95529562005-08-25 21:21:38 +00001407 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001408 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001409 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001410 }
1411
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001412 if (curline)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001413 break; // only check cursor line
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001414
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001415 // If we are back at the starting line and searched it again there
1416 // is no match, give up.
Bram Moolenaard3f78dc2017-02-25 14:21:10 +01001417 if (lnum == wp->w_cursor.lnum && wrapped)
1418 break;
1419
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001420 // Advance to next line.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001421 if (dir == BACKWARD)
1422 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001423 if (lnum > 1)
1424 --lnum;
1425 else if (!p_ws)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001426 break; // at first line and 'nowrapscan'
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001427 else
1428 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001429 // Wrap around to the end of the buffer. May search the
1430 // starting line again and accept the last match.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001431 lnum = wp->w_buffer->b_ml.ml_line_count;
1432 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001433 if (!shortmess(SHM_SEARCH))
1434 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001435 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001436 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001437 }
1438 else
1439 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001440 if (lnum < wp->w_buffer->b_ml.ml_line_count)
1441 ++lnum;
1442 else if (!p_ws)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001443 break; // at first line and 'nowrapscan'
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001444 else
1445 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001446 // Wrap around to the start of the buffer. May search the
1447 // starting line again and accept the first match.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001448 lnum = 1;
1449 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001450 if (!shortmess(SHM_SEARCH))
1451 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001452 }
1453
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001454 // If we are back at the starting line and there is no match then
1455 // give up.
Bram Moolenaard3f78dc2017-02-25 14:21:10 +01001456 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001457 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001458
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001459 // Skip the characters at the start of the next line that were
1460 // included in a match crossing line boundaries.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001461 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001462 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001463 else
1464 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001465
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001466 // Capcol skips over the inserted space.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001467 --capcol;
1468
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001469 // But after empty line check first word in next line
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001470 if (*skipwhite(line) == NUL)
1471 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001472 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001473
1474 line_breakcheck();
1475 }
1476
Bram Moolenaar0c405862005-06-22 22:26:26 +00001477 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001478 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001479}
1480
1481/*
1482 * For spell checking: concatenate the start of the following line "line" into
1483 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00001484 * Keep the blanks at the start of the next line, this is used in win_line()
1485 * to skip those bytes if the word was OK.
Bram Moolenaar0c405862005-06-22 22:26:26 +00001486 */
1487 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001488spell_cat_line(char_u *buf, char_u *line, int maxlen)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001489{
1490 char_u *p;
1491 int n;
1492
1493 p = skipwhite(line);
1494 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1495 p = skipwhite(p + 1);
1496
1497 if (*p != NUL)
1498 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001499 // Only worth concatenating if there is something else than spaces to
1500 // concatenate.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00001501 n = (int)(p - line) + 1;
1502 if (n < maxlen - 1)
1503 {
1504 vim_memset(buf, ' ', n);
1505 vim_strncpy(buf + n, p, maxlen - 1 - n);
1506 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00001507 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001508}
1509
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001510/*
1511 * Structure used for the cookie argument of do_in_runtimepath().
1512 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001513typedef struct spelload_S
1514{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001515 char_u sl_lang[MAXWLEN + 1]; // language name
1516 slang_T *sl_slang; // resulting slang_T struct
1517 int sl_nobreak; // NOBREAK language found
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001518} spelload_T;
1519
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001520/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001521 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001522 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001523 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001524 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001525spell_load_lang(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001526{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001527 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001528 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001529 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001530 int round;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001531
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001532 // Copy the language name to pass it to spell_load_cb() as a cookie.
1533 // It's truncated when an error is detected.
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001534 STRCPY(sl.sl_lang, lang);
1535 sl.sl_slang = NULL;
1536 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001537
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001538 // We may retry when no spell file is found for the language, an
1539 // autocommand may load it then.
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001540 for (round = 1; round <= 2; ++round)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001541 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001542 /*
1543 * Find the first spell file for "lang" in 'runtimepath' and load it.
1544 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001545 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001546#ifdef VMS
1547 "spell/%s_%s.spl",
1548#else
1549 "spell/%s.%s.spl",
1550#endif
1551 lang, spell_enc());
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001552 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001553
1554 if (r == FAIL && *sl.sl_lang != NUL)
1555 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001556 // Try loading the ASCII version.
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001557 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001558#ifdef VMS
1559 "spell/%s_ascii.spl",
1560#else
1561 "spell/%s.ascii.spl",
1562#endif
1563 lang);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001564 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001565
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001566 if (r == FAIL && *sl.sl_lang != NUL && round == 1
1567 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
1568 curbuf->b_fname, FALSE, curbuf))
1569 continue;
1570 break;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001571 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001572 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001573 }
1574
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001575 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001576 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001577 smsg(
Bram Moolenaar56f78042010-12-08 17:09:32 +01001578#ifdef VMS
1579 _("Warning: Cannot find word list \"%s_%s.spl\" or \"%s_ascii.spl\""),
1580#else
1581 _("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
1582#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00001583 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001584 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001585 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001586 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001587 // At least one file was loaded, now load ALL the additions.
Bram Moolenaarb765d632005-06-07 21:00:02 +00001588 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001589 do_in_runtimepath(fname_enc, DIP_ALL, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001590 }
1591}
1592
1593/*
1594 * Return the encoding used for spell checking: Use 'encoding', except that we
1595 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1596 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001597 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001598spell_enc(void)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001599{
1600
Bram Moolenaarb765d632005-06-07 21:00:02 +00001601 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1602 return p_enc;
Bram Moolenaarb765d632005-06-07 21:00:02 +00001603 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001604}
1605
1606/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001607 * Get the name of the .spl file for the internal wordlist into
1608 * "fname[MAXPATHL]".
1609 */
1610 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001611int_wordlist_spl(char_u *fname)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001612{
Bram Moolenaar56f78042010-12-08 17:09:32 +01001613 vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001614 int_wordlist, spell_enc());
1615}
1616
1617/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001618 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001619 * Caller must fill "sl_next".
1620 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001621 slang_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001622slang_alloc(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001623{
1624 slang_T *lp;
1625
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001626 lp = ALLOC_CLEAR_ONE(slang_T);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001627 if (lp != NULL)
1628 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001629 if (lang != NULL)
1630 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001631 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001632 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001633 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001634 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001635 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001636 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001637
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001638 return lp;
1639}
1640
1641/*
1642 * Free the contents of an slang_T and the structure itself.
1643 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001644 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001645slang_free(slang_T *lp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001646{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001647 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001648 vim_free(lp->sl_fname);
1649 slang_clear(lp);
1650 vim_free(lp);
1651}
1652
1653/*
1654 * Clear an slang_T so that the file can be reloaded.
1655 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001656 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001657slang_clear(slang_T *lp)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001658{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001659 garray_T *gap;
1660 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001661 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001662 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001663 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001664
Bram Moolenaard23a8232018-02-10 18:45:26 +01001665 VIM_CLEAR(lp->sl_fbyts);
1666 VIM_CLEAR(lp->sl_kbyts);
1667 VIM_CLEAR(lp->sl_pbyts);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001668
Bram Moolenaard23a8232018-02-10 18:45:26 +01001669 VIM_CLEAR(lp->sl_fidxs);
1670 VIM_CLEAR(lp->sl_kidxs);
1671 VIM_CLEAR(lp->sl_pidxs);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001672
Bram Moolenaar4770d092006-01-12 23:22:24 +00001673 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001674 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001675 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
1676 while (gap->ga_len > 0)
1677 {
1678 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1679 vim_free(ftp->ft_from);
1680 vim_free(ftp->ft_to);
1681 }
1682 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001683 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001684
1685 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001686 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001687 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001688 // "ga_len" is set to 1 without adding an item for latin1
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001689 if (gap->ga_data != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001690 // SOFOFROM and SOFOTO items: free lists of wide characters.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001691 for (i = 0; i < gap->ga_len; ++i)
1692 vim_free(((int **)gap->ga_data)[i]);
1693 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001694 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001695 // SAL items: free salitem_T items
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001696 while (gap->ga_len > 0)
1697 {
1698 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1699 vim_free(smp->sm_lead);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001700 // Don't free sm_oneof and sm_rules, they point into sm_lead.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001701 vim_free(smp->sm_to);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001702 vim_free(smp->sm_lead_w);
1703 vim_free(smp->sm_oneof_w);
1704 vim_free(smp->sm_to_w);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001705 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001706 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001707
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001708 for (i = 0; i < lp->sl_prefixcnt; ++i)
Bram Moolenaar473de612013-06-08 18:19:48 +02001709 vim_regfree(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001710 lp->sl_prefixcnt = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001711 VIM_CLEAR(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001712
Bram Moolenaard23a8232018-02-10 18:45:26 +01001713 VIM_CLEAR(lp->sl_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001714
Bram Moolenaard23a8232018-02-10 18:45:26 +01001715 VIM_CLEAR(lp->sl_midword);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001716
Bram Moolenaar473de612013-06-08 18:19:48 +02001717 vim_regfree(lp->sl_compprog);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001718 lp->sl_compprog = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001719 VIM_CLEAR(lp->sl_comprules);
1720 VIM_CLEAR(lp->sl_compstartflags);
1721 VIM_CLEAR(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001722
Bram Moolenaard23a8232018-02-10 18:45:26 +01001723 VIM_CLEAR(lp->sl_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001724 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001725
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001726 ga_clear_strings(&lp->sl_comppat);
1727
Bram Moolenaar4770d092006-01-12 23:22:24 +00001728 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
1729 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00001730
Bram Moolenaar4770d092006-01-12 23:22:24 +00001731 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001732
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001733 // Clear info from .sug file.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001734 slang_clear_sug(lp);
1735
Bram Moolenaar5195e452005-08-19 20:32:47 +00001736 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001737 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001738 lp->sl_compsylmax = MAXWLEN;
1739 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001740}
1741
1742/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001743 * Clear the info from the .sug file in "lp".
1744 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001745 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001746slang_clear_sug(slang_T *lp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00001747{
Bram Moolenaard23a8232018-02-10 18:45:26 +01001748 VIM_CLEAR(lp->sl_sbyts);
1749 VIM_CLEAR(lp->sl_sidxs);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001750 close_spellbuf(lp->sl_sugbuf);
1751 lp->sl_sugbuf = NULL;
1752 lp->sl_sugloaded = FALSE;
1753 lp->sl_sugtime = 0;
1754}
1755
1756/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001757 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001758 * Invoked through do_in_runtimepath().
1759 */
1760 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001761spell_load_cb(char_u *fname, void *cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001762{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001763 spelload_T *slp = (spelload_T *)cookie;
1764 slang_T *slang;
1765
1766 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
1767 if (slang != NULL)
1768 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001769 // When a previously loaded file has NOBREAK also use it for the
1770 // ".add" files.
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001771 if (slp->sl_nobreak && slang->sl_add)
1772 slang->sl_nobreak = TRUE;
1773 else if (slang->sl_nobreak)
1774 slp->sl_nobreak = TRUE;
1775
1776 slp->sl_slang = slang;
1777 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00001778}
1779
Bram Moolenaar4770d092006-01-12 23:22:24 +00001780
1781/*
1782 * Add a word to the hashtable of common words.
1783 * If it's already there then the counter is increased.
1784 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001785 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001786count_common_word(
1787 slang_T *lp,
1788 char_u *word,
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001789 int len, // word length, -1 for up to NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001790 int count) // 1 to count once, 10 to init
Bram Moolenaar4770d092006-01-12 23:22:24 +00001791{
1792 hash_T hash;
1793 hashitem_T *hi;
1794 wordcount_T *wc;
1795 char_u buf[MAXWLEN];
1796 char_u *p;
1797
1798 if (len == -1)
1799 p = word;
Bram Moolenaar5bcc5a12019-08-06 22:48:02 +02001800 else if (len >= MAXWLEN)
1801 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001802 else
1803 {
1804 vim_strncpy(buf, word, len);
1805 p = buf;
1806 }
1807
1808 hash = hash_hash(p);
1809 hi = hash_lookup(&lp->sl_wordcount, p, hash);
1810 if (HASHITEM_EMPTY(hi))
1811 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001812 wc = alloc(sizeof(wordcount_T) + STRLEN(p));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001813 if (wc == NULL)
1814 return;
1815 STRCPY(wc->wc_word, p);
1816 wc->wc_count = count;
1817 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
1818 }
1819 else
1820 {
1821 wc = HI2WC(hi);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001822 if ((wc->wc_count += count) < (unsigned)count) // check for overflow
Bram Moolenaar4770d092006-01-12 23:22:24 +00001823 wc->wc_count = MAXWORDCOUNT;
1824 }
1825}
1826
1827/*
Bram Moolenaar95529562005-08-25 21:21:38 +00001828 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00001829 * Like strchr() but independent of locale.
1830 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001831 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001832byte_in_str(char_u *str, int n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001833{
1834 char_u *p;
1835
1836 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00001837 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001838 return TRUE;
1839 return FALSE;
1840}
1841
Bram Moolenaar5195e452005-08-19 20:32:47 +00001842#define SY_MAXLEN 30
1843typedef struct syl_item_S
1844{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001845 char_u sy_chars[SY_MAXLEN]; // the sequence of chars
Bram Moolenaar5195e452005-08-19 20:32:47 +00001846 int sy_len;
1847} syl_item_T;
1848
1849/*
1850 * Truncate "slang->sl_syllable" at the first slash and put the following items
1851 * in "slang->sl_syl_items".
1852 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001853 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001854init_syl_tab(slang_T *slang)
Bram Moolenaar5195e452005-08-19 20:32:47 +00001855{
1856 char_u *p;
1857 char_u *s;
1858 int l;
1859 syl_item_T *syl;
1860
1861 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
1862 p = vim_strchr(slang->sl_syllable, '/');
1863 while (p != NULL)
1864 {
1865 *p++ = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001866 if (*p == NUL) // trailing slash
Bram Moolenaar5195e452005-08-19 20:32:47 +00001867 break;
1868 s = p;
1869 p = vim_strchr(p, '/');
1870 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001871 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001872 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001873 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001874 if (l >= SY_MAXLEN)
1875 return SP_FORMERROR;
1876 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001877 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001878 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
1879 + slang->sl_syl_items.ga_len++;
1880 vim_strncpy(syl->sy_chars, s, l);
1881 syl->sy_len = l;
1882 }
1883 return OK;
1884}
1885
1886/*
1887 * Count the number of syllables in "word".
1888 * When "word" contains spaces the syllables after the last space are counted.
1889 * Returns zero if syllables are not defines.
1890 */
1891 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001892count_syllables(slang_T *slang, char_u *word)
Bram Moolenaar5195e452005-08-19 20:32:47 +00001893{
1894 int cnt = 0;
1895 int skip = FALSE;
1896 char_u *p;
1897 int len;
1898 int i;
1899 syl_item_T *syl;
1900 int c;
1901
1902 if (slang->sl_syllable == NULL)
1903 return 0;
1904
1905 for (p = word; *p != NUL; p += len)
1906 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001907 // When running into a space reset counter.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001908 if (*p == ' ')
1909 {
1910 len = 1;
1911 cnt = 0;
1912 continue;
1913 }
1914
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001915 // Find longest match of syllable items.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001916 len = 0;
1917 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
1918 {
1919 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
1920 if (syl->sy_len > len
1921 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
1922 len = syl->sy_len;
1923 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001924 if (len != 0) // found a match, count syllable
Bram Moolenaar5195e452005-08-19 20:32:47 +00001925 {
1926 ++cnt;
1927 skip = FALSE;
1928 }
1929 else
1930 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001931 // No recognized syllable item, at least a syllable char then?
Bram Moolenaar5195e452005-08-19 20:32:47 +00001932 c = mb_ptr2char(p);
1933 len = (*mb_ptr2len)(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001934 if (vim_strchr(slang->sl_syllable, c) == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001935 skip = FALSE; // No, search for next syllable
Bram Moolenaar5195e452005-08-19 20:32:47 +00001936 else if (!skip)
1937 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001938 ++cnt; // Yes, count it
1939 skip = TRUE; // don't count following syllable chars
Bram Moolenaar5195e452005-08-19 20:32:47 +00001940 }
1941 }
1942 }
1943 return cnt;
1944}
1945
1946/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02001947 * Parse 'spelllang' and set w_s->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001948 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001949 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001950 char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001951did_set_spelllang(win_T *wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001952{
1953 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001954 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001955 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00001956 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001957 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001958 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001959 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001960 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001961 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001962 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001963 int len;
1964 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00001965 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001966 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001967 char_u *use_region = NULL;
1968 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001969 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001970 int i, j;
1971 langp_T *lp, *lp2;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001972 static int recursive = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001973 char *ret_msg = NULL;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001974 char_u *spl_copy;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001975 bufref_T bufref;
1976
1977 set_bufref(&bufref, wp->w_buffer);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001978
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001979 // We don't want to do this recursively. May happen when a language is
1980 // not available and the SpellFileMissing autocommand opens a new buffer
1981 // in which 'spell' is set.
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001982 if (recursive)
1983 return NULL;
1984 recursive = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001985
1986 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001987 clear_midword(wp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001988
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001989 // Make a copy of 'spelllang', the SpellFileMissing autocommands may change
1990 // it under our fingers.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001991 spl_copy = vim_strsave(wp->w_s->b_p_spl);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001992 if (spl_copy == NULL)
1993 goto theend;
1994
Bram Moolenaarcc63c642013-11-12 04:44:01 +01001995 wp->w_s->b_cjk = 0;
1996
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001997 // Loop over comma separated language names.
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001998 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001999 {
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002000 // Get one language name.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002001 copy_option_part(&splp, lang, MAXWLEN, ",");
Bram Moolenaar5482f332005-04-17 20:18:43 +00002002 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002003 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002004
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002005 if (!valid_spellang(lang))
2006 continue;
2007
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002008 if (STRCMP(lang, "cjk") == 0)
2009 {
2010 wp->w_s->b_cjk = 1;
2011 continue;
2012 }
2013
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002014 // If the name ends in ".spl" use it as the name of the spell file.
2015 // If there is a region name let "region" point to it and remove it
2016 // from the name.
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002017 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2018 {
2019 filename = TRUE;
2020
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002021 // Locate a region and remove it from the file name.
Bram Moolenaarb6356332005-07-18 21:40:44 +00002022 p = vim_strchr(gettail(lang), '_');
2023 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
2024 && !ASCII_ISALPHA(p[3]))
2025 {
2026 vim_strncpy(region_cp, p + 1, 2);
2027 mch_memmove(p, p + 3, len - (p - lang) - 2);
Bram Moolenaarb6356332005-07-18 21:40:44 +00002028 region = region_cp;
2029 }
2030 else
2031 dont_use_region = TRUE;
2032
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002033 // Check if we loaded this language before.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002034 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar99499b12019-05-23 21:35:48 +02002035 if (fullpathcmp(lang, slang->sl_fname, FALSE, TRUE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002036 break;
2037 }
2038 else
2039 {
2040 filename = FALSE;
2041 if (len > 3 && lang[len - 3] == '_')
2042 {
2043 region = lang + len - 2;
2044 len -= 3;
2045 lang[len] = NUL;
2046 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002047 else
2048 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002049
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002050 // Check if we loaded this language before.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002051 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002052 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002053 break;
2054 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002055
Bram Moolenaarb6356332005-07-18 21:40:44 +00002056 if (region != NULL)
2057 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002058 // If the region differs from what was used before then don't
2059 // use it for 'spellfile'.
Bram Moolenaarb6356332005-07-18 21:40:44 +00002060 if (use_region != NULL && STRCMP(region, use_region) != 0)
2061 dont_use_region = TRUE;
2062 use_region = region;
2063 }
2064
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002065 // If not found try loading the language now.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002066 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002067 {
2068 if (filename)
2069 (void)spell_load_file(lang, lang, NULL, FALSE);
2070 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002071 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002072 spell_load_lang(lang);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002073 // SpellFileMissing autocommands may do anything, including
2074 // destroying the buffer we are using...
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002075 if (!bufref_valid(&bufref))
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002076 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002077 ret_msg = N_("E797: SpellFileMissing autocommand deleted buffer");
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002078 goto theend;
2079 }
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002080 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002081 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002082
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002083 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002084 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002085 */
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002086 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar99499b12019-05-23 21:35:48 +02002087 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE, TRUE)
2088 == FPC_SAME
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002089 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002090 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002091 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002092 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002093 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002094 // find region in sl_regions
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002095 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002096 if (c == REGION_ALL)
2097 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002098 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002099 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002100 if (*slang->sl_regions != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002101 // This addition file is for other regions.
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002102 region_mask = 0;
2103 }
2104 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002105 // This is probably an error. Give a warning and
2106 // accept the words anyway.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002107 smsg(_("Warning: region %s not supported"),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002108 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002109 }
2110 else
2111 region_mask = 1 << c;
2112 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002113
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002114 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002115 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002116 if (ga_grow(&ga, 1) == FAIL)
2117 {
2118 ga_clear(&ga);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002119 ret_msg = e_outofmem;
2120 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002121 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002122 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002123 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2124 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002125 use_midword(slang, wp);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002126 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002127 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002128 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002129 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002130 }
2131
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002132 // round 0: load int_wordlist, if possible.
2133 // round 1: load first name in 'spellfile'.
2134 // round 2: load second name in 'spellfile.
2135 // etc.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002136 spf = curwin->w_s->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002137 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002138 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002139 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002140 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002141 // Internal wordlist, if there is one.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002142 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002143 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002144 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002145 }
2146 else
2147 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002148 // One entry in 'spellfile'.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002149 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
2150 STRCAT(spf_name, ".spl");
2151
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002152 // If it was already found above then skip it.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002153 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002154 {
2155 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
Bram Moolenaar99499b12019-05-23 21:35:48 +02002156 if (p != NULL && fullpathcmp(spf_name, p, FALSE, TRUE)
2157 == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002158 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002159 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002160 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002161 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002162 }
2163
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002164 // Check if it was loaded already.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002165 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar99499b12019-05-23 21:35:48 +02002166 if (fullpathcmp(spf_name, slang->sl_fname, FALSE, TRUE)
2167 == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002168 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002169 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002170 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002171 // Not loaded, try loading it now. The language name includes the
2172 // region name, the region is ignored otherwise. for int_wordlist
2173 // use an arbitrary name.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002174 if (round == 0)
2175 STRCPY(lang, "internal wordlist");
2176 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00002177 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002178 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002179 p = vim_strchr(lang, '.');
2180 if (p != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002181 *p = NUL; // truncate at ".encoding.add"
Bram Moolenaar7887d882005-07-01 22:33:52 +00002182 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002183 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002184
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002185 // If one of the languages has NOBREAK we assume the addition
2186 // files also have this.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002187 if (slang != NULL && nobreak)
2188 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002189 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002190 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002191 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002192 region_mask = REGION_ALL;
2193 if (use_region != NULL && !dont_use_region)
2194 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002195 // find region in sl_regions
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002196 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002197 if (c != REGION_ALL)
2198 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002199 else if (*slang->sl_regions != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002200 // This spell file is for other regions.
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002201 region_mask = 0;
2202 }
2203
2204 if (region_mask != 0)
2205 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002206 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
2207 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
2208 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002209 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2210 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002211 use_midword(slang, wp);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002212 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002213 }
2214 }
2215
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002216 // Everything is fine, store the new b_langp value.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002217 ga_clear(&wp->w_s->b_langp);
2218 wp->w_s->b_langp = ga;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002219
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002220 // For each language figure out what language to use for sound folding and
2221 // REP items. If the language doesn't support it itself use another one
2222 // with the same name. E.g. for "en-math" use "en".
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002223 for (i = 0; i < ga.ga_len; ++i)
2224 {
2225 lp = LANGP_ENTRY(ga, i);
2226
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002227 // sound folding
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002228 if (lp->lp_slang->sl_sal.ga_len > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002229 // language does sound folding itself
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002230 lp->lp_sallang = lp->lp_slang;
2231 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002232 // find first similar language that does sound folding
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002233 for (j = 0; j < ga.ga_len; ++j)
2234 {
2235 lp2 = LANGP_ENTRY(ga, j);
2236 if (lp2->lp_slang->sl_sal.ga_len > 0
2237 && STRNCMP(lp->lp_slang->sl_name,
2238 lp2->lp_slang->sl_name, 2) == 0)
2239 {
2240 lp->lp_sallang = lp2->lp_slang;
2241 break;
2242 }
2243 }
2244
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002245 // REP items
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002246 if (lp->lp_slang->sl_rep.ga_len > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002247 // language has REP items itself
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002248 lp->lp_replang = lp->lp_slang;
2249 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002250 // find first similar language that has REP items
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002251 for (j = 0; j < ga.ga_len; ++j)
2252 {
2253 lp2 = LANGP_ENTRY(ga, j);
2254 if (lp2->lp_slang->sl_rep.ga_len > 0
2255 && STRNCMP(lp->lp_slang->sl_name,
2256 lp2->lp_slang->sl_name, 2) == 0)
2257 {
2258 lp->lp_replang = lp2->lp_slang;
2259 break;
2260 }
2261 }
2262 }
2263
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002264theend:
2265 vim_free(spl_copy);
2266 recursive = FALSE;
Bram Moolenaarbe578ed2014-05-13 14:03:40 +02002267 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002268 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002269}
2270
2271/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002272 * Clear the midword characters for buffer "buf".
2273 */
2274 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002275clear_midword(win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002276{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002277 CLEAR_FIELD(wp->w_s->b_spell_ismw);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002278 VIM_CLEAR(wp->w_s->b_spell_ismw_mb);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002279}
2280
2281/*
2282 * Use the "sl_midword" field of language "lp" for buffer "buf".
2283 * They add up to any currently used midword characters.
2284 */
2285 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002286use_midword(slang_T *lp, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002287{
2288 char_u *p;
2289
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002290 if (lp->sl_midword == NULL) // there aren't any
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002291 return;
2292
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002293 for (p = lp->sl_midword; *p != NUL; )
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002294 if (has_mbyte)
2295 {
2296 int c, l, n;
2297 char_u *bp;
2298
2299 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002300 l = (*mb_ptr2len)(p);
2301 if (c < 256 && l <= 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002302 wp->w_s->b_spell_ismw[c] = TRUE;
2303 else if (wp->w_s->b_spell_ismw_mb == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002304 // First multi-byte char in "b_spell_ismw_mb".
Bram Moolenaar860cae12010-06-05 23:22:07 +02002305 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002306 else
2307 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002308 // Append multi-byte chars to "b_spell_ismw_mb".
Bram Moolenaar860cae12010-06-05 23:22:07 +02002309 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
2310 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002311 if (bp != NULL)
2312 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002313 vim_free(wp->w_s->b_spell_ismw_mb);
2314 wp->w_s->b_spell_ismw_mb = bp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002315 vim_strncpy(bp + n, p, l);
2316 }
2317 }
2318 p += l;
2319 }
2320 else
Bram Moolenaar860cae12010-06-05 23:22:07 +02002321 wp->w_s->b_spell_ismw[*p++] = TRUE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002322}
2323
2324/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002325 * Find the region "region[2]" in "rp" (points to "sl_regions").
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01002326 * Each region is simply stored as the two characters of its name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002327 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002328 */
2329 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002330find_region(char_u *rp, char_u *region)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002331{
2332 int i;
2333
2334 for (i = 0; ; i += 2)
2335 {
2336 if (rp[i] == NUL)
2337 return REGION_ALL;
2338 if (rp[i] == region[0] && rp[i + 1] == region[1])
2339 break;
2340 }
2341 return i / 2;
2342}
2343
2344/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002345 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002346 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002347 * Word WF_ONECAP
2348 * W WORD WF_ALLCAP
2349 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002350 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002351 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002352captype(
2353 char_u *word,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002354 char_u *end) // When NULL use up to NUL byte.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002355{
2356 char_u *p;
2357 int c;
2358 int firstcap;
2359 int allcap;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002360 int past_second = FALSE; // past second word char
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002361
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002362 // find first letter
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002363 for (p = word; !spell_iswordp_nmw(p, curwin); MB_PTR_ADV(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002364 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002365 return 0; // only non-word characters, illegal word
Bram Moolenaarb765d632005-06-07 21:00:02 +00002366 if (has_mbyte)
2367 c = mb_ptr2char_adv(&p);
2368 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00002369 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002370 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002371
2372 /*
2373 * Need to check all letters to find a word with mixed upper/lower.
2374 * But a word with an upper char only at start is a ONECAP.
2375 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002376 for ( ; end == NULL ? *p != NUL : p < end; MB_PTR_ADV(p))
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002377 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002378 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002379 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002380 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002381 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002382 // UUl -> KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002383 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002384 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002385 allcap = FALSE;
2386 }
2387 else if (!allcap)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002388 // UlU -> KEEPCAP
Bram Moolenaar51485f02005-06-04 21:55:20 +00002389 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002390 past_second = TRUE;
2391 }
2392
2393 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002394 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002395 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002396 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002397 return 0;
2398}
2399
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002400/*
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002401 * Delete the internal wordlist and its .spl file.
2402 */
2403 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002404spell_delete_wordlist(void)
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002405{
2406 char_u fname[MAXPATHL];
2407
2408 if (int_wordlist != NULL)
2409 {
2410 mch_remove(int_wordlist);
2411 int_wordlist_spl(fname);
2412 mch_remove(fname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002413 VIM_CLEAR(int_wordlist);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002414 }
2415}
2416
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002417/*
2418 * Free all languages.
2419 */
2420 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002421spell_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002422{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002423 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002424 buf_T *buf;
2425
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002426 // Go through all buffers and handle 'spelllang'. <VN>
Bram Moolenaar29323592016-07-24 22:04:11 +02002427 FOR_ALL_BUFFERS(buf)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002428 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002429
2430 while (first_lang != NULL)
2431 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002432 slang = first_lang;
2433 first_lang = slang->sl_next;
2434 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002435 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002436
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002437 spell_delete_wordlist();
Bram Moolenaar7887d882005-07-01 22:33:52 +00002438
Bram Moolenaard23a8232018-02-10 18:45:26 +01002439 VIM_CLEAR(repl_to);
2440 VIM_CLEAR(repl_from);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002441}
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002442
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002443/*
2444 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002445 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002446 */
2447 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002448spell_reload(void)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002449{
Bram Moolenaar3982c542005-06-08 21:56:31 +00002450 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002451
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002452 // Initialize the table for spell_iswordp().
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002453 init_spell_chartab();
2454
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002455 // Unload all allocated memory.
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002456 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002457
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002458 // Go through all buffers and handle 'spelllang'.
Bram Moolenaar29323592016-07-24 22:04:11 +02002459 FOR_ALL_WINDOWS(wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002460 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002461 // Only load the wordlists when 'spelllang' is set and there is a
2462 // window for this buffer in which 'spell' is set.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002463 if (*wp->w_s->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002464 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002465 if (wp->w_p_spell)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002466 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002467 (void)did_set_spelllang(wp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00002468 break;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002469 }
2470 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002471 }
2472}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002473
Bram Moolenaarb765d632005-06-07 21:00:02 +00002474/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002475 * Open a spell buffer. This is a nameless buffer that is not in the buffer
2476 * list and only contains text lines. Can use a swapfile to reduce memory
2477 * use.
2478 * Most other fields are invalid! Esp. watch out for string options being
2479 * NULL and there is no undo info.
2480 * Returns NULL when out of memory.
2481 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002482 buf_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002483open_spellbuf(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002484{
2485 buf_T *buf;
2486
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002487 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002488 if (buf != NULL)
2489 {
2490 buf->b_spell = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002491 buf->b_p_swf = TRUE; // may create a swap file
Bram Moolenaar706d2de2013-07-17 17:35:13 +02002492#ifdef FEAT_CRYPT
2493 buf->b_p_key = empty_option;
2494#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00002495 ml_open(buf);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002496 ml_open_file(buf); // create swap file now
Bram Moolenaar4770d092006-01-12 23:22:24 +00002497 }
2498 return buf;
2499}
2500
2501/*
2502 * Close the buffer used for spell info.
2503 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002504 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002505close_spellbuf(buf_T *buf)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002506{
2507 if (buf != NULL)
2508 {
2509 ml_close(buf, TRUE);
2510 vim_free(buf);
2511 }
2512}
2513
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002514/*
2515 * Init the chartab used for spelling for ASCII.
2516 * EBCDIC is not supported!
2517 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002518 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002519clear_spell_chartab(spelltab_T *sp)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002520{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002521 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002522
Bram Moolenaara80faa82020-04-12 19:37:17 +02002523 // Init everything to FALSE (zero).
2524 CLEAR_FIELD(sp->st_isw);
2525 CLEAR_FIELD(sp->st_isu);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002526 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002527 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002528 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002529 sp->st_upper[i] = i;
2530 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002531
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002532 // We include digits. A word shouldn't start with a digit, but handling
2533 // that is done separately.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002534 for (i = '0'; i <= '9'; ++i)
2535 sp->st_isw[i] = TRUE;
2536 for (i = 'A'; i <= 'Z'; ++i)
2537 {
2538 sp->st_isw[i] = TRUE;
2539 sp->st_isu[i] = TRUE;
2540 sp->st_fold[i] = i + 0x20;
2541 }
2542 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002543 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002544 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002545 sp->st_upper[i] = i - 0x20;
2546 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002547}
2548
2549/*
2550 * Init the chartab used for spelling. Only depends on 'encoding'.
2551 * Called once while starting up and when 'encoding' changes.
2552 * The default is to use isalpha(), but the spell file should define the word
2553 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002554 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002555 */
2556 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002557init_spell_chartab(void)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002558{
2559 int i;
2560
2561 did_set_spelltab = FALSE;
2562 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002563 if (enc_dbcs)
2564 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002565 // DBCS: assume double-wide characters are word characters.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002566 for (i = 128; i <= 255; ++i)
2567 if (MB_BYTE2LEN(i) == 2)
2568 spelltab.st_isw[i] = TRUE;
2569 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002570 else if (enc_utf8)
2571 {
2572 for (i = 128; i < 256; ++i)
2573 {
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02002574 int f = utf_fold(i);
2575 int u = utf_toupper(i);
2576
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002577 spelltab.st_isu[i] = utf_isupper(i);
2578 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002579 // The folded/upper-cased value is different between latin1 and
2580 // utf8 for 0xb5, causing E763 for no good reason. Use the latin1
2581 // value for utf-8 to avoid this.
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02002582 spelltab.st_fold[i] = (f < 256) ? f : i;
2583 spelltab.st_upper[i] = (u < 256) ? u : i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002584 }
2585 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002586 else
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002587 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002588 // Rough guess: use locale-dependent library functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002589 for (i = 128; i < 256; ++i)
2590 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002591 if (MB_ISUPPER(i))
2592 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002593 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002594 spelltab.st_isu[i] = TRUE;
2595 spelltab.st_fold[i] = MB_TOLOWER(i);
2596 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002597 else if (MB_ISLOWER(i))
2598 {
2599 spelltab.st_isw[i] = TRUE;
2600 spelltab.st_upper[i] = MB_TOUPPER(i);
2601 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002602 }
2603 }
2604}
2605
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002606
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002607/*
Bram Moolenaarea408852005-06-25 22:49:46 +00002608 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002609 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00002610 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002611 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00002612 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02002613 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002614spell_iswordp(
2615 char_u *p,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002616 win_T *wp) // buffer used
Bram Moolenaarea408852005-06-25 22:49:46 +00002617{
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002618 char_u *s;
2619 int l;
2620 int c;
2621
2622 if (has_mbyte)
2623 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002624 l = mb_ptr2len(p);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002625 s = p;
2626 if (l == 1)
2627 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002628 // be quick for ASCII
Bram Moolenaar860cae12010-06-05 23:22:07 +02002629 if (wp->w_s->b_spell_ismw[*p])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002630 s = p + 1; // skip a mid-word character
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002631 }
2632 else
2633 {
2634 c = mb_ptr2char(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002635 if (c < 256 ? wp->w_s->b_spell_ismw[c]
2636 : (wp->w_s->b_spell_ismw_mb != NULL
2637 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002638 s = p + l;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002639 }
2640
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002641 c = mb_ptr2char(s);
2642 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002643 return spell_mb_isword_class(mb_get_class(s), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002644 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002645 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002646
Bram Moolenaar860cae12010-06-05 23:22:07 +02002647 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002648}
2649
2650/*
2651 * Return TRUE if "p" points to a word character.
2652 * Unlike spell_iswordp() this doesn't check for "midword" characters.
2653 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02002654 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002655spell_iswordp_nmw(char_u *p, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002656{
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002657 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002658
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002659 if (has_mbyte)
2660 {
2661 c = mb_ptr2char(p);
2662 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002663 return spell_mb_isword_class(mb_get_class(p), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002664 return spelltab.st_isw[c];
2665 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002666 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00002667}
2668
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002669/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002670 * Return TRUE if word class indicates a word character.
2671 * Only for characters above 255.
2672 * Unicode subscript and superscript are not considered word characters.
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002673 * See also dbcs_class() and utf_class() in mbyte.c.
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002674 */
2675 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002676spell_mb_isword_class(int cl, win_T *wp)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002677{
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002678 if (wp->w_s->b_cjk)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002679 // East Asian characters are not considered word characters.
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002680 return cl == 2 || cl == 0x2800;
Bram Moolenaar06e63772019-07-19 23:04:34 +02002681 return cl >= 2 && cl != 0x2070 && cl != 0x2080 && cl != 3;
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002682}
2683
2684/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002685 * Return TRUE if "p" points to a word character.
2686 * Wide version of spell_iswordp().
2687 */
2688 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002689spell_iswordp_w(int *p, win_T *wp)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002690{
2691 int *s;
2692
Bram Moolenaar860cae12010-06-05 23:22:07 +02002693 if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
2694 : (wp->w_s->b_spell_ismw_mb != NULL
2695 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002696 s = p + 1;
2697 else
2698 s = p;
2699
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002700 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002701 {
2702 if (enc_utf8)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002703 return spell_mb_isword_class(utf_class(*s), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002704 if (enc_dbcs)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002705 return spell_mb_isword_class(
2706 dbcs_class((unsigned)*s >> 8, *s & 0xff), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002707 return 0;
2708 }
2709 return spelltab.st_isw[*s];
2710}
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002711
Bram Moolenaarea408852005-06-25 22:49:46 +00002712/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002713 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
2714 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002715 * When using a multi-byte 'encoding' the length may change!
2716 * Returns FAIL when something wrong.
2717 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002718 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002719spell_casefold(
2720 char_u *str,
2721 int len,
2722 char_u *buf,
2723 int buflen)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002724{
2725 int i;
2726
2727 if (len >= buflen)
2728 {
2729 buf[0] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002730 return FAIL; // result will not fit
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002731 }
2732
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002733 if (has_mbyte)
2734 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002735 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002736 char_u *p;
2737 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002738
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002739 // Fold one character at a time.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002740 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002741 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002742 if (outi + MB_MAXBYTES > buflen)
2743 {
2744 buf[outi] = NUL;
2745 return FAIL;
2746 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002747 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002748 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002749 }
2750 buf[outi] = NUL;
2751 }
2752 else
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002753 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002754 // Be quick for non-multibyte encodings.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002755 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002756 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002757 buf[i] = NUL;
2758 }
2759
2760 return OK;
2761}
2762
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002763/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002764 * Check if the word at line "lnum" column "col" is required to start with a
2765 * capital. This uses 'spellcapcheck' of the current buffer.
2766 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02002767 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002768check_need_cap(linenr_T lnum, colnr_T col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002769{
2770 int need_cap = FALSE;
2771 char_u *line;
2772 char_u *line_copy = NULL;
2773 char_u *p;
2774 colnr_T endcol;
2775 regmatch_T regmatch;
2776
Bram Moolenaar860cae12010-06-05 23:22:07 +02002777 if (curwin->w_s->b_cap_prog == NULL)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002778 return FALSE;
2779
2780 line = ml_get_curline();
2781 endcol = 0;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002782 if (getwhitecols(line) >= (int)col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002783 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002784 // At start of line, check if previous line is empty or sentence
2785 // ends there.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002786 if (lnum == 1)
2787 need_cap = TRUE;
2788 else
2789 {
2790 line = ml_get(lnum - 1);
2791 if (*skipwhite(line) == NUL)
2792 need_cap = TRUE;
2793 else
2794 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002795 // Append a space in place of the line break.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002796 line_copy = concat_str(line, (char_u *)" ");
2797 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002798 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002799 }
2800 }
2801 }
2802 else
2803 endcol = col;
2804
2805 if (endcol > 0)
2806 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002807 // Check if sentence ends before the bad word.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002808 regmatch.regprog = curwin->w_s->b_cap_prog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002809 regmatch.rm_ic = FALSE;
2810 p = line + endcol;
2811 for (;;)
2812 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002813 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002814 if (p == line || spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002815 break;
2816 if (vim_regexec(&regmatch, p, 0)
2817 && regmatch.endp[0] == line + endcol)
2818 {
2819 need_cap = TRUE;
2820 break;
2821 }
2822 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002823 curwin->w_s->b_cap_prog = regmatch.regprog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002824 }
2825
2826 vim_free(line_copy);
2827
2828 return need_cap;
2829}
2830
2831
2832/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002833 * ":spellrepall"
2834 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002835 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002836ex_spellrepall(exarg_T *eap UNUSED)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002837{
2838 pos_T pos = curwin->w_cursor;
2839 char_u *frompat;
2840 int addlen;
2841 char_u *line;
2842 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002843 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002844 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002845
2846 if (repl_from == NULL || repl_to == NULL)
2847 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002848 emsg(_("E752: No previous spell replacement"));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002849 return;
2850 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002851 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002852
Bram Moolenaar964b3742019-05-24 18:54:09 +02002853 frompat = alloc(STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002854 if (frompat == NULL)
2855 return;
2856 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
2857 p_ws = FALSE;
2858
Bram Moolenaar5195e452005-08-19 20:32:47 +00002859 sub_nsubs = 0;
2860 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002861 curwin->w_cursor.lnum = 0;
2862 while (!got_int)
2863 {
Bram Moolenaarc036e872020-02-21 21:30:52 +01002864 if (do_search(NULL, '/', '/', frompat, 1L, SEARCH_KEEP, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002865 || u_save_cursor() == FAIL)
2866 break;
2867
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002868 // Only replace when the right word isn't there yet. This happens
2869 // when changing "etc" to "etc.".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002870 line = ml_get_curline();
2871 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
2872 repl_to, STRLEN(repl_to)) != 0)
2873 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002874 p = alloc(STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002875 if (p == NULL)
2876 break;
2877 mch_memmove(p, line, curwin->w_cursor.col);
2878 STRCPY(p + curwin->w_cursor.col, repl_to);
2879 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
2880 ml_replace(curwin->w_cursor.lnum, p, FALSE);
2881 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002882
2883 if (curwin->w_cursor.lnum != prev_lnum)
2884 {
2885 ++sub_nlines;
2886 prev_lnum = curwin->w_cursor.lnum;
2887 }
2888 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002889 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002890 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002891 }
2892
2893 p_ws = save_ws;
2894 curwin->w_cursor = pos;
2895 vim_free(frompat);
2896
Bram Moolenaar5195e452005-08-19 20:32:47 +00002897 if (sub_nsubs == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002898 semsg(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002899 else
2900 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002901}
2902
2903/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002904 * Make a copy of "word", with the first letter upper or lower cased, to
2905 * "wcopy[MAXWLEN]". "word" must not be empty.
2906 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002907 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002908 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002909onecap_copy(
2910 char_u *word,
2911 char_u *wcopy,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002912 int upper) // TRUE: first letter made upper case
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002913{
2914 char_u *p;
2915 int c;
2916 int l;
2917
2918 p = word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002919 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002920 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002921 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002922 c = *p++;
2923 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002924 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002925 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002926 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002927 if (has_mbyte)
2928 l = mb_char2bytes(c, wcopy);
2929 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002930 {
2931 l = 1;
2932 wcopy[0] = c;
2933 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002934 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002935}
2936
2937/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002938 * Make a copy of "word" with all the letters upper cased into
2939 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002940 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02002941 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002942allcap_copy(char_u *word, char_u *wcopy)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002943{
2944 char_u *s;
2945 char_u *d;
2946 int c;
2947
2948 d = wcopy;
2949 for (s = word; *s != NUL; )
2950 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002951 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002952 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002953 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002954 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +00002955
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002956 // We only change 0xdf to SS when we are certain latin1 is used. It
2957 // would cause weird errors in other 8-bit encodings.
Bram Moolenaar78622822005-08-23 21:00:13 +00002958 if (enc_latin1like && c == 0xdf)
2959 {
2960 c = 'S';
2961 if (d - wcopy >= MAXWLEN - 1)
2962 break;
2963 *d++ = c;
2964 }
2965 else
Bram Moolenaar78622822005-08-23 21:00:13 +00002966 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002967
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002968 if (has_mbyte)
2969 {
2970 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
2971 break;
2972 d += mb_char2bytes(c, d);
2973 }
2974 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002975 {
2976 if (d - wcopy >= MAXWLEN - 1)
2977 break;
2978 *d++ = c;
2979 }
2980 }
2981 *d = NUL;
2982}
2983
2984/*
Bram Moolenaar53805d12005-08-01 07:08:33 +00002985 * Case-folding may change the number of bytes: Count nr of chars in
2986 * fword[flen] and return the byte length of that many chars in "word".
2987 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02002988 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002989nofold_len(char_u *fword, int flen, char_u *word)
Bram Moolenaar53805d12005-08-01 07:08:33 +00002990{
2991 char_u *p;
2992 int i = 0;
2993
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002994 for (p = fword; p < fword + flen; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00002995 ++i;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002996 for (p = word; i > 0; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00002997 --i;
2998 return (int)(p - word);
2999}
Bram Moolenaar53805d12005-08-01 07:08:33 +00003000
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003001/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003002 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003003 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02003004 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003005make_case_word(char_u *fword, char_u *cword, int flags)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003006{
3007 if (flags & WF_ALLCAP)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003008 // Make it all upper-case
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003009 allcap_copy(fword, cword);
3010 else if (flags & WF_ONECAP)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003011 // Make the first letter upper-case
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003012 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003013 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003014 // Use goodword as-is.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003015 STRCPY(cword, fword);
3016}
3017
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003018#if defined(FEAT_EVAL) || defined(PROTO)
3019/*
3020 * Soundfold a string, for soundfold().
3021 * Result is in allocated memory, NULL for an error.
3022 */
3023 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003024eval_soundfold(char_u *word)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003025{
3026 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003027 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003028 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003029
Bram Moolenaar860cae12010-06-05 23:22:07 +02003030 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003031 // Use the sound-folding of the first language that supports it.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003032 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003033 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003034 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003035 if (lp->lp_slang->sl_sal.ga_len > 0)
3036 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003037 // soundfold the word
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003038 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003039 return vim_strsave(sound);
3040 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003041 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003042
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003043 // No language with sound folding, return word as-is.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003044 return vim_strsave(word);
3045}
3046#endif
3047
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003048/*
3049 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +00003050 *
3051 * There are many ways to turn a word into a sound-a-like representation. The
3052 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
3053 * swedish name matching - survey and test of different algorithms" by Klas
3054 * Erikson.
3055 *
3056 * We support two methods:
3057 * 1. SOFOFROM/SOFOTO do a simple character mapping.
3058 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003059 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003060 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003061spell_soundfold(
3062 slang_T *slang,
3063 char_u *inword,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003064 int folded, // "inword" is already case-folded
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003065 char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003066{
3067 char_u fword[MAXWLEN];
3068 char_u *word;
3069
3070 if (slang->sl_sofo)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003071 // SOFOFROM and SOFOTO used
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003072 spell_soundfold_sofo(slang, inword, res);
3073 else
3074 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003075 // SAL items used. Requires the word to be case-folded.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003076 if (folded)
3077 word = inword;
3078 else
3079 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003080 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003081 word = fword;
3082 }
3083
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003084 if (has_mbyte)
3085 spell_soundfold_wsal(slang, word, res);
3086 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003087 spell_soundfold_sal(slang, word, res);
3088 }
3089}
3090
3091/*
3092 * Perform sound folding of "inword" into "res" according to SOFOFROM and
3093 * SOFOTO lines.
3094 */
3095 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003096spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003097{
3098 char_u *s;
3099 int ri = 0;
3100 int c;
3101
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003102 if (has_mbyte)
3103 {
3104 int prevc = 0;
3105 int *ip;
3106
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003107 // The sl_sal_first[] table contains the translation for chars up to
3108 // 255, sl_sal the rest.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003109 for (s = inword; *s != NUL; )
3110 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003111 c = mb_cptr2char_adv(&s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003112 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003113 c = ' ';
3114 else if (c < 256)
3115 c = slang->sl_sal_first[c];
3116 else
3117 {
3118 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003119 if (ip == NULL) // empty list, can't match
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003120 c = NUL;
3121 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003122 for (;;) // find "c" in the list
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003123 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003124 if (*ip == 0) // not found
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003125 {
3126 c = NUL;
3127 break;
3128 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003129 if (*ip == c) // match!
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003130 {
3131 c = ip[1];
3132 break;
3133 }
3134 ip += 2;
3135 }
3136 }
3137
3138 if (c != NUL && c != prevc)
3139 {
3140 ri += mb_char2bytes(c, res + ri);
3141 if (ri + MB_MAXBYTES > MAXWLEN)
3142 break;
3143 prevc = c;
3144 }
3145 }
3146 }
3147 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003148 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003149 // The sl_sal_first[] table contains the translation.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003150 for (s = inword; (c = *s) != NUL; ++s)
3151 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003152 if (VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003153 c = ' ';
3154 else
3155 c = slang->sl_sal_first[c];
3156 if (c != NUL && (ri == 0 || res[ri - 1] != c))
3157 res[ri++] = c;
3158 }
3159 }
3160
3161 res[ri] = NUL;
3162}
3163
3164 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003165spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003166{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003167 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003168 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003169 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003170 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003171 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003172 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003173 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003174 int n, k = 0;
3175 int z0;
3176 int k0;
3177 int n0;
3178 int c;
3179 int pri;
3180 int p0 = -333;
3181 int c0;
3182
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003183 // Remove accents, if wanted. We actually remove all non-word characters.
3184 // But keep white space. We need a copy, the word may be changed here.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003185 if (slang->sl_rem_accents)
3186 {
3187 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003188 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003189 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003190 if (VIM_ISWHITE(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003191 {
3192 *t++ = ' ';
3193 s = skipwhite(s);
3194 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003195 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003196 {
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003197 if (spell_iswordp_nmw(s, curwin))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003198 *t++ = *s;
3199 ++s;
3200 }
3201 }
3202 *t = NUL;
3203 }
3204 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003205 vim_strncpy(word, s, MAXWLEN - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003206
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003207 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003208
3209 /*
3210 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003211 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003212 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003213 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003214 while ((c = word[i]) != NUL)
3215 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003216 // Start with the first rule that has the character in the word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003217 n = slang->sl_sal_first[c];
3218 z0 = 0;
3219
3220 if (n >= 0)
3221 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003222 // check all rules for the same letter
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003223 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003224 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003225 // Quickly skip entries that don't match the word. Most
3226 // entries are less then three chars, optimize for that.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003227 k = smp[n].sm_leadlen;
3228 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003229 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003230 if (word[i + 1] != s[1])
3231 continue;
3232 if (k > 2)
3233 {
3234 for (j = 2; j < k; ++j)
3235 if (word[i + j] != s[j])
3236 break;
3237 if (j < k)
3238 continue;
3239 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003240 }
3241
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003242 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003243 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003244 // Check for match with one of the chars in "sm_oneof".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003245 while (*pf != NUL && *pf != word[i + k])
3246 ++pf;
3247 if (*pf == NUL)
3248 continue;
3249 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003250 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003251 s = smp[n].sm_rules;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003252 pri = 5; // default priority
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003253
3254 p0 = *s;
3255 k0 = k;
3256 while (*s == '-' && k > 1)
3257 {
3258 k--;
3259 s++;
3260 }
3261 if (*s == '<')
3262 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003263 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003264 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003265 // determine priority
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003266 pri = *s - '0';
3267 s++;
3268 }
3269 if (*s == '^' && *(s + 1) == '^')
3270 s++;
3271
3272 if (*s == NUL
3273 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003274 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02003275 || spell_iswordp(word + i - 1, curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003276 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003277 || (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003278 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02003279 && spell_iswordp(word + i - 1, curwin)
3280 && (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003281 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003282 // search for followup rules, if:
3283 // followup and k > 1 and NO '-' in searchstring
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003284 c0 = word[i + k - 1];
3285 n0 = slang->sl_sal_first[c0];
3286
3287 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003288 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003289 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003290 // test follow-up rule for "word[i + k]"
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003291 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003292 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003293 // Quickly skip entries that don't match the word.
3294 //
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003295 k0 = smp[n0].sm_leadlen;
3296 if (k0 > 1)
3297 {
3298 if (word[i + k] != s[1])
3299 continue;
3300 if (k0 > 2)
3301 {
3302 pf = word + i + k + 1;
3303 for (j = 2; j < k0; ++j)
3304 if (*pf++ != s[j])
3305 break;
3306 if (j < k0)
3307 continue;
3308 }
3309 }
3310 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003311
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003312 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003313 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003314 // Check for match with one of the chars in
3315 // "sm_oneof".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003316 while (*pf != NUL && *pf != word[i + k0])
3317 ++pf;
3318 if (*pf == NUL)
3319 continue;
3320 ++k0;
3321 }
3322
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003323 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003324 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003325 while (*s == '-')
3326 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003327 // "k0" gets NOT reduced because
3328 // "if (k0 == k)"
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003329 s++;
3330 }
3331 if (*s == '<')
3332 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003333 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003334 {
3335 p0 = *s - '0';
3336 s++;
3337 }
3338
3339 if (*s == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003340 // *s == '^' cuts
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003341 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003342 && !spell_iswordp(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02003343 curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003344 {
3345 if (k0 == k)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003346 // this is just a piece of the string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003347 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003348
3349 if (p0 < pri)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003350 // priority too low
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003351 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003352 // rule fits; stop search
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003353 break;
3354 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003355 }
3356
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003357 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003358 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003359 }
3360
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003361 // replace string
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003362 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003363 if (s == NULL)
3364 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003365 pf = smp[n].sm_rules;
3366 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003367 if (p0 == 1 && z == 0)
3368 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003369 // rule with '<' is used
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003370 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
3371 || res[reslen - 1] == *s))
3372 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003373 z0 = 1;
3374 z = 1;
3375 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003376 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003377 {
3378 word[i + k0] = *s;
3379 k0++;
3380 s++;
3381 }
3382 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +00003383 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003384
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003385 // new "actual letter"
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003386 c = word[i];
3387 }
3388 else
3389 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003390 // no '<' rule used
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003391 i += k - 1;
3392 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003393 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003394 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003395 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003396 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003397 s++;
3398 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003399 // new "actual letter"
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003400 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003401 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003402 {
3403 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003404 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003405 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003406 i = 0;
3407 z0 = 1;
3408 }
3409 }
3410 break;
3411 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003412 }
3413 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01003414 else if (VIM_ISWHITE(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003415 {
3416 c = ' ';
3417 k = 1;
3418 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003419
3420 if (z0 == 0)
3421 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003422 if (k && !p0 && reslen < MAXWLEN && c != NUL
3423 && (!slang->sl_collapse || reslen == 0
3424 || res[reslen - 1] != c))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003425 // condense only double letters
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003426 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003427
3428 i++;
3429 z = 0;
3430 k = 0;
3431 }
3432 }
3433
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003434 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003435}
3436
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003437/*
3438 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
3439 * Multi-byte version of spell_soundfold().
3440 */
3441 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003442spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003443{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003444 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003445 int word[MAXWLEN];
3446 int wres[MAXWLEN];
3447 int l;
3448 char_u *s;
3449 int *ws;
3450 char_u *t;
3451 int *pf;
3452 int i, j, z;
3453 int reslen;
3454 int n, k = 0;
3455 int z0;
3456 int k0;
3457 int n0;
3458 int c;
3459 int pri;
3460 int p0 = -333;
3461 int c0;
3462 int did_white = FALSE;
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003463 int wordlen;
3464
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003465
3466 /*
3467 * Convert the multi-byte string to a wide-character string.
3468 * Remove accents, if wanted. We actually remove all non-word characters.
3469 * But keep white space.
3470 */
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003471 wordlen = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003472 for (s = inword; *s != NUL; )
3473 {
3474 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003475 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003476 if (slang->sl_rem_accents)
3477 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003478 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003479 {
3480 if (did_white)
3481 continue;
3482 c = ' ';
3483 did_white = TRUE;
3484 }
3485 else
3486 {
3487 did_white = FALSE;
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003488 if (!spell_iswordp_nmw(t, curwin))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003489 continue;
3490 }
3491 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003492 word[wordlen++] = c;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003493 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003494 word[wordlen] = NUL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003495
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003496 /*
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003497 * This algorithm comes from Aspell phonet.cpp.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003498 * Converted from C++ to C. Added support for multi-byte chars.
3499 * Changed to keep spaces.
3500 */
3501 i = reslen = z = 0;
3502 while ((c = word[i]) != NUL)
3503 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003504 // Start with the first rule that has the character in the word.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003505 n = slang->sl_sal_first[c & 0xff];
3506 z0 = 0;
3507
3508 if (n >= 0)
3509 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003510 // Check all rules for the same index byte.
3511 // If c is 0x300 need extra check for the end of the array, as
3512 // (c & 0xff) is NUL.
Bram Moolenaar95e85792010-08-01 15:37:02 +02003513 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff)
3514 && ws[0] != NUL; ++n)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003515 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003516 // Quickly skip entries that don't match the word. Most
3517 // entries are less then three chars, optimize for that.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003518 if (c != ws[0])
3519 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003520 k = smp[n].sm_leadlen;
3521 if (k > 1)
3522 {
3523 if (word[i + 1] != ws[1])
3524 continue;
3525 if (k > 2)
3526 {
3527 for (j = 2; j < k; ++j)
3528 if (word[i + j] != ws[j])
3529 break;
3530 if (j < k)
3531 continue;
3532 }
3533 }
3534
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003535 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003536 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003537 // Check for match with one of the chars in "sm_oneof".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003538 while (*pf != NUL && *pf != word[i + k])
3539 ++pf;
3540 if (*pf == NUL)
3541 continue;
3542 ++k;
3543 }
3544 s = smp[n].sm_rules;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003545 pri = 5; // default priority
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003546
3547 p0 = *s;
3548 k0 = k;
3549 while (*s == '-' && k > 1)
3550 {
3551 k--;
3552 s++;
3553 }
3554 if (*s == '<')
3555 s++;
3556 if (VIM_ISDIGIT(*s))
3557 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003558 // determine priority
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003559 pri = *s - '0';
3560 s++;
3561 }
3562 if (*s == '^' && *(s + 1) == '^')
3563 s++;
3564
3565 if (*s == NUL
3566 || (*s == '^'
3567 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02003568 || spell_iswordp_w(word + i - 1, curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003569 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003570 || (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003571 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02003572 && spell_iswordp_w(word + i - 1, curwin)
3573 && (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003574 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003575 // search for followup rules, if:
3576 // followup and k > 1 and NO '-' in searchstring
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003577 c0 = word[i + k - 1];
3578 n0 = slang->sl_sal_first[c0 & 0xff];
3579
3580 if (slang->sl_followup && k > 1 && n0 >= 0
3581 && p0 != '-' && word[i + k] != NUL)
3582 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003583 // Test follow-up rule for "word[i + k]"; loop over
3584 // all entries with the same index byte.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003585 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
3586 == (c0 & 0xff); ++n0)
3587 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003588 // Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003589 if (c0 != ws[0])
3590 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003591 k0 = smp[n0].sm_leadlen;
3592 if (k0 > 1)
3593 {
3594 if (word[i + k] != ws[1])
3595 continue;
3596 if (k0 > 2)
3597 {
3598 pf = word + i + k + 1;
3599 for (j = 2; j < k0; ++j)
3600 if (*pf++ != ws[j])
3601 break;
3602 if (j < k0)
3603 continue;
3604 }
3605 }
3606 k0 += k - 1;
3607
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003608 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003609 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003610 // Check for match with one of the chars in
3611 // "sm_oneof".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003612 while (*pf != NUL && *pf != word[i + k0])
3613 ++pf;
3614 if (*pf == NUL)
3615 continue;
3616 ++k0;
3617 }
3618
3619 p0 = 5;
3620 s = smp[n0].sm_rules;
3621 while (*s == '-')
3622 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003623 // "k0" gets NOT reduced because
3624 // "if (k0 == k)"
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003625 s++;
3626 }
3627 if (*s == '<')
3628 s++;
3629 if (VIM_ISDIGIT(*s))
3630 {
3631 p0 = *s - '0';
3632 s++;
3633 }
3634
3635 if (*s == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003636 // *s == '^' cuts
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003637 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003638 && !spell_iswordp_w(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02003639 curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003640 {
3641 if (k0 == k)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003642 // this is just a piece of the string
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003643 continue;
3644
3645 if (p0 < pri)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003646 // priority too low
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003647 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003648 // rule fits; stop search
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003649 break;
3650 }
3651 }
3652
3653 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
3654 == (c0 & 0xff))
3655 continue;
3656 }
3657
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003658 // replace string
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003659 ws = smp[n].sm_to_w;
3660 s = smp[n].sm_rules;
3661 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
3662 if (p0 == 1 && z == 0)
3663 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003664 // rule with '<' is used
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003665 if (reslen > 0 && ws != NULL && *ws != NUL
3666 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003667 || wres[reslen - 1] == *ws))
3668 reslen--;
3669 z0 = 1;
3670 z = 1;
3671 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003672 if (ws != NULL)
3673 while (*ws != NUL && word[i + k0] != NUL)
3674 {
3675 word[i + k0] = *ws;
3676 k0++;
3677 ws++;
3678 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003679 if (k > k0)
3680 mch_memmove(word + i + k0, word + i + k,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003681 sizeof(int) * (wordlen - (i + k) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003682
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003683 // new "actual letter"
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003684 c = word[i];
3685 }
3686 else
3687 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003688 // no '<' rule used
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003689 i += k - 1;
3690 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003691 if (ws != NULL)
3692 while (*ws != NUL && ws[1] != NUL
3693 && reslen < MAXWLEN)
3694 {
3695 if (reslen == 0 || wres[reslen - 1] != *ws)
3696 wres[reslen++] = *ws;
3697 ws++;
3698 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003699 // new "actual letter"
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003700 if (ws == NULL)
3701 c = NUL;
3702 else
3703 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003704 if (strstr((char *)s, "^^") != NULL)
3705 {
3706 if (c != NUL)
3707 wres[reslen++] = c;
3708 mch_memmove(word, word + i + 1,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003709 sizeof(int) * (wordlen - (i + 1) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003710 i = 0;
3711 z0 = 1;
3712 }
3713 }
3714 break;
3715 }
3716 }
3717 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01003718 else if (VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003719 {
3720 c = ' ';
3721 k = 1;
3722 }
3723
3724 if (z0 == 0)
3725 {
3726 if (k && !p0 && reslen < MAXWLEN && c != NUL
3727 && (!slang->sl_collapse || reslen == 0
3728 || wres[reslen - 1] != c))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003729 // condense only double letters
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003730 wres[reslen++] = c;
3731
3732 i++;
3733 z = 0;
3734 k = 0;
3735 }
3736 }
3737
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003738 // Convert wide characters in "wres" to a multi-byte string in "res".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003739 l = 0;
3740 for (n = 0; n < reslen; ++n)
3741 {
3742 l += mb_char2bytes(wres[n], res + l);
3743 if (l + MB_MAXBYTES > MAXWLEN)
3744 break;
3745 }
3746 res[l] = NUL;
3747}
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003748
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003749/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003750 * ":spellinfo"
3751 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003752 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003753ex_spellinfo(exarg_T *eap UNUSED)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003754{
3755 int lpi;
3756 langp_T *lp;
3757 char_u *p;
3758
3759 if (no_spell_checking(curwin))
3760 return;
3761
3762 msg_start();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003763 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003764 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003765 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003766 msg_puts("file: ");
3767 msg_puts((char *)lp->lp_slang->sl_fname);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003768 msg_putchar('\n');
3769 p = lp->lp_slang->sl_info;
3770 if (p != NULL)
3771 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003772 msg_puts((char *)p);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003773 msg_putchar('\n');
3774 }
3775 }
3776 msg_end();
3777}
3778
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003779#define DUMPFLAG_KEEPCASE 1 // round 2: keep-case tree
3780#define DUMPFLAG_COUNT 2 // include word count
3781#define DUMPFLAG_ICASE 4 // ignore case when finding matches
3782#define DUMPFLAG_ONECAP 8 // pattern starts with capital
3783#define DUMPFLAG_ALLCAP 16 // pattern is all capitals
Bram Moolenaar4770d092006-01-12 23:22:24 +00003784
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003785/*
3786 * ":spelldump"
3787 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003788 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003789ex_spelldump(exarg_T *eap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003790{
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003791 char_u *spl;
3792 long dummy;
3793
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003794 if (no_spell_checking(curwin))
3795 return;
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003796 get_option_value((char_u*)"spl", &dummy, &spl, OPT_LOCAL);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003797
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003798 // Create a new empty buffer in a new window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003799 do_cmdline_cmd((char_u *)"new");
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003800
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003801 // enable spelling locally in the new window
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003802 set_option_value((char_u*)"spell", TRUE, (char_u*)"", OPT_LOCAL);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003803 set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL);
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003804 vim_free(spl);
3805
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003806 if (!BUFEMPTY())
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003807 return;
3808
Bram Moolenaar860cae12010-06-05 23:22:07 +02003809 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003810
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003811 // Delete the empty line that we started with.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003812 if (curbuf->b_ml.ml_line_count > 1)
3813 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
3814
3815 redraw_later(NOT_VALID);
3816}
3817
3818/*
3819 * Go through all possible words and:
3820 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
3821 * "ic" and "dir" are not used.
3822 * 2. When "pat" is not NULL: add matching words to insert mode completion.
3823 */
3824 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003825spell_dump_compl(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003826 char_u *pat, // leading part of the word
3827 int ic, // ignore case
3828 int *dir, // direction for adding matches
3829 int dumpflags_arg) // DUMPFLAG_*
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003830{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003831 langp_T *lp;
3832 slang_T *slang;
3833 idx_T arridx[MAXWLEN];
3834 int curi[MAXWLEN];
3835 char_u word[MAXWLEN];
3836 int c;
3837 char_u *byts;
3838 idx_T *idxs;
3839 linenr_T lnum = 0;
3840 int round;
3841 int depth;
3842 int n;
3843 int flags;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003844 char_u *region_names = NULL; // region names being used
3845 int do_region = TRUE; // dump region names and numbers
Bram Moolenaar7887d882005-07-01 22:33:52 +00003846 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003847 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003848 int dumpflags = dumpflags_arg;
3849 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003850
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003851 // When ignoring case or when the pattern starts with capital pass this on
3852 // to dump_word().
Bram Moolenaard0131a82006-03-04 21:46:13 +00003853 if (pat != NULL)
3854 {
3855 if (ic)
3856 dumpflags |= DUMPFLAG_ICASE;
3857 else
3858 {
3859 n = captype(pat, NULL);
3860 if (n == WF_ONECAP)
3861 dumpflags |= DUMPFLAG_ONECAP;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01003862 else if (n == WF_ALLCAP && (int)STRLEN(pat) > mb_ptr2len(pat))
Bram Moolenaard0131a82006-03-04 21:46:13 +00003863 dumpflags |= DUMPFLAG_ALLCAP;
3864 }
3865 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003866
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003867 // Find out if we can support regions: All languages must support the same
3868 // regions or none at all.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003869 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003870 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003871 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003872 p = lp->lp_slang->sl_regions;
3873 if (p[0] != 0)
3874 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003875 if (region_names == NULL) // first language with regions
Bram Moolenaar7887d882005-07-01 22:33:52 +00003876 region_names = p;
3877 else if (STRCMP(region_names, p) != 0)
3878 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003879 do_region = FALSE; // region names are different
Bram Moolenaar7887d882005-07-01 22:33:52 +00003880 break;
3881 }
3882 }
3883 }
3884
3885 if (do_region && region_names != NULL)
3886 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003887 if (pat == NULL)
3888 {
3889 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
3890 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
3891 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00003892 }
3893 else
3894 do_region = FALSE;
3895
3896 /*
3897 * Loop over all files loaded for the entries in 'spelllang'.
3898 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003899 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003900 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003901 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003902 slang = lp->lp_slang;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003903 if (slang->sl_fbyts == NULL) // reloading failed
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003904 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003905
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003906 if (pat == NULL)
3907 {
3908 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
3909 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
3910 }
3911
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003912 // When matching with a pattern and there are no prefixes only use
3913 // parts of the tree that match "pat".
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003914 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003915 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003916 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003917 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003918
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003919 // round 1: case-folded tree
3920 // round 2: keep-case tree
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003921 for (round = 1; round <= 2; ++round)
3922 {
3923 if (round == 1)
3924 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003925 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003926 byts = slang->sl_fbyts;
3927 idxs = slang->sl_fidxs;
3928 }
3929 else
3930 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003931 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003932 byts = slang->sl_kbyts;
3933 idxs = slang->sl_kidxs;
3934 }
3935 if (byts == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003936 continue; // array is empty
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003937
3938 depth = 0;
3939 arridx[0] = 0;
3940 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003941 while (depth >= 0 && !got_int
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003942 && (pat == NULL || !ins_compl_interrupted()))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003943 {
3944 if (curi[depth] > byts[arridx[depth]])
3945 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003946 // Done all bytes at this node, go up one level.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003947 --depth;
3948 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003949 ins_compl_check_keys(50, FALSE);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003950 }
3951 else
3952 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003953 // Do one more byte at this node.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003954 n = arridx[depth] + curi[depth];
3955 ++curi[depth];
3956 c = byts[n];
3957 if (c == 0)
3958 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003959 // End of word, deal with the word.
3960 // Don't use keep-case words in the fold-case tree,
3961 // they will appear in the keep-case tree.
3962 // Only use the word when the region matches.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003963 flags = (int)idxs[n];
3964 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003965 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +00003966 && (do_region
3967 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003968 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003969 & lp->lp_region) != 0))
3970 {
3971 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003972 if (!do_region)
3973 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003974
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003975 // Dump the basic word if there is no prefix or
3976 // when it's the first one.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003977 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003978 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003979 {
3980 dump_word(slang, word, pat, dir,
3981 dumpflags, flags, lnum);
3982 if (pat == NULL)
3983 ++lnum;
3984 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003985
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003986 // Apply the prefix, if there is one.
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003987 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003988 lnum = dump_prefixes(slang, word, pat, dir,
3989 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003990 }
3991 }
3992 else
3993 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003994 // Normal char, go one level deeper.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003995 word[depth++] = c;
3996 arridx[depth] = idxs[n];
3997 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003998
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003999 // Check if this characters matches with the pattern.
4000 // If not skip the whole tree below it.
4001 // Always ignore case here, dump_word() will check
4002 // proper case later. This isn't exactly right when
4003 // length changes for multi-byte characters with
4004 // ignore case...
Bram Moolenaard0131a82006-03-04 21:46:13 +00004005 if (depth <= patlen
4006 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004007 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004008 }
4009 }
4010 }
4011 }
4012 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004013}
4014
4015/*
4016 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004017 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004018 */
4019 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004020dump_word(
4021 slang_T *slang,
4022 char_u *word,
4023 char_u *pat,
4024 int *dir,
4025 int dumpflags,
4026 int wordflags,
4027 linenr_T lnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004028{
4029 int keepcap = FALSE;
4030 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004031 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004032 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +00004033 char_u badword[MAXWLEN + 10];
4034 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +00004035 int flags = wordflags;
4036
4037 if (dumpflags & DUMPFLAG_ONECAP)
4038 flags |= WF_ONECAP;
4039 if (dumpflags & DUMPFLAG_ALLCAP)
4040 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004041
Bram Moolenaar4770d092006-01-12 23:22:24 +00004042 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004043 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004044 // Need to fix case according to "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004045 make_case_word(word, cword, flags);
4046 p = cword;
4047 }
4048 else
4049 {
4050 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004051 if ((dumpflags & DUMPFLAG_KEEPCASE)
4052 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004053 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004054 keepcap = TRUE;
4055 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004056 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004057
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004058 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004059 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004060 // Add flags and regions after a slash.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004061 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004062 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004063 STRCPY(badword, p);
4064 STRCAT(badword, "/");
4065 if (keepcap)
4066 STRCAT(badword, "=");
4067 if (flags & WF_BANNED)
4068 STRCAT(badword, "!");
4069 else if (flags & WF_RARE)
4070 STRCAT(badword, "?");
4071 if (flags & WF_REGION)
4072 for (i = 0; i < 7; ++i)
4073 if (flags & (0x10000 << i))
4074 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
4075 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004076 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004077
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004078 if (dumpflags & DUMPFLAG_COUNT)
4079 {
4080 hashitem_T *hi;
4081
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004082 // Include the word count for ":spelldump!".
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004083 hi = hash_find(&slang->sl_wordcount, tw);
4084 if (!HASHITEM_EMPTY(hi))
4085 {
4086 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
4087 tw, HI2WC(hi)->wc_count);
4088 p = IObuff;
4089 }
4090 }
4091
4092 ml_append(lnum, p, (colnr_T)0, FALSE);
4093 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00004094 else if (((dumpflags & DUMPFLAG_ICASE)
4095 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
4096 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004097 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004098 p_ic, NULL, *dir, FALSE) == OK)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004099 // if dir was BACKWARD then honor it just once
Bram Moolenaard0131a82006-03-04 21:46:13 +00004100 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004101}
4102
4103/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004104 * For ":spelldump": Find matching prefixes for "word". Prepend each to
4105 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004106 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004107 * Return the updated line number.
4108 */
4109 static linenr_T
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004110dump_prefixes(
4111 slang_T *slang,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004112 char_u *word, // case-folded word
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004113 char_u *pat,
4114 int *dir,
4115 int dumpflags,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004116 int flags, // flags with prefix ID
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004117 linenr_T startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004118{
4119 idx_T arridx[MAXWLEN];
4120 int curi[MAXWLEN];
4121 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +00004122 char_u word_up[MAXWLEN];
4123 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004124 int c;
4125 char_u *byts;
4126 idx_T *idxs;
4127 linenr_T lnum = startlnum;
4128 int depth;
4129 int n;
4130 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004131 int i;
4132
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004133 // If the word starts with a lower-case letter make the word with an
4134 // upper-case letter in word_up[].
Bram Moolenaar53805d12005-08-01 07:08:33 +00004135 c = PTR2CHAR(word);
4136 if (SPELL_TOUPPER(c) != c)
4137 {
4138 onecap_copy(word, word_up, TRUE);
4139 has_word_up = TRUE;
4140 }
4141
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004142 byts = slang->sl_pbyts;
4143 idxs = slang->sl_pidxs;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004144 if (byts != NULL) // array not is empty
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004145 {
4146 /*
4147 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004148 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004149 */
4150 depth = 0;
4151 arridx[0] = 0;
4152 curi[0] = 1;
4153 while (depth >= 0 && !got_int)
4154 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004155 n = arridx[depth];
4156 len = byts[n];
4157 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004158 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004159 // Done all bytes at this node, go up one level.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004160 --depth;
4161 line_breakcheck();
4162 }
4163 else
4164 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004165 // Do one more byte at this node.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004166 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004167 ++curi[depth];
4168 c = byts[n];
4169 if (c == 0)
4170 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004171 // End of prefix, find out how many IDs there are.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004172 for (i = 1; i < len; ++i)
4173 if (byts[n + i] != 0)
4174 break;
4175 curi[depth] += i - 1;
4176
Bram Moolenaar53805d12005-08-01 07:08:33 +00004177 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
4178 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004179 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004180 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004181 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004182 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004183 : flags, lnum);
4184 if (lnum != 0)
4185 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004186 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00004187
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004188 // Check for prefix that matches the word when the
4189 // first letter is upper-case, but only if the prefix has
4190 // a condition.
Bram Moolenaar53805d12005-08-01 07:08:33 +00004191 if (has_word_up)
4192 {
4193 c = valid_word_prefix(i, n, flags, word_up, slang,
4194 TRUE);
4195 if (c != 0)
4196 {
4197 vim_strncpy(prefix + depth, word_up,
4198 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004199 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004200 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004201 : flags, lnum);
4202 if (lnum != 0)
4203 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004204 }
4205 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004206 }
4207 else
4208 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004209 // Normal char, go one level deeper.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004210 prefix[depth++] = c;
4211 arridx[depth] = idxs[n];
4212 curi[depth] = 1;
4213 }
4214 }
4215 }
4216 }
4217
4218 return lnum;
4219}
4220
Bram Moolenaar95529562005-08-25 21:21:38 +00004221/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004222 * Move "p" to the end of word "start".
4223 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +00004224 */
4225 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004226spell_to_word_end(char_u *start, win_T *win)
Bram Moolenaar95529562005-08-25 21:21:38 +00004227{
4228 char_u *p = start;
4229
Bram Moolenaar860cae12010-06-05 23:22:07 +02004230 while (*p != NUL && spell_iswordp(p, win))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004231 MB_PTR_ADV(p);
Bram Moolenaar95529562005-08-25 21:21:38 +00004232 return p;
4233}
4234
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004235/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004236 * For Insert mode completion CTRL-X s:
4237 * Find start of the word in front of column "startcol".
4238 * We don't check if it is badly spelled, with completion we can only change
4239 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004240 * Returns the column number of the word.
4241 */
4242 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004243spell_word_start(int startcol)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004244{
4245 char_u *line;
4246 char_u *p;
4247 int col = 0;
4248
Bram Moolenaar95529562005-08-25 21:21:38 +00004249 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004250 return startcol;
4251
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004252 // Find a word character before "startcol".
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004253 line = ml_get_curline();
4254 for (p = line + startcol; p > line; )
4255 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004256 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004257 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004258 break;
4259 }
4260
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004261 // Go back to start of the word.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004262 while (p > line)
4263 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004264 col = (int)(p - line);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004265 MB_PTR_BACK(line, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004266 if (!spell_iswordp(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004267 break;
4268 col = 0;
4269 }
4270
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004271 return col;
4272}
4273
4274/*
Bram Moolenaar4effc802005-09-30 21:12:02 +00004275 * Need to check for 'spellcapcheck' now, the word is removed before
4276 * expand_spelling() is called. Therefore the ugly global variable.
4277 */
4278static int spell_expand_need_cap;
4279
4280 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004281spell_expand_check_cap(colnr_T col)
Bram Moolenaar4effc802005-09-30 21:12:02 +00004282{
4283 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
4284}
4285
4286/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004287 * Get list of spelling suggestions.
4288 * Used for Insert mode completion CTRL-X ?.
4289 * Returns the number of matches. The matches are in "matchp[]", array of
4290 * allocated strings.
4291 */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004292 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004293expand_spelling(
4294 linenr_T lnum UNUSED,
4295 char_u *pat,
4296 char_u ***matchp)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004297{
4298 garray_T ga;
4299
Bram Moolenaar4770d092006-01-12 23:22:24 +00004300 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004301 *matchp = ga.ga_data;
4302 return ga.ga_len;
4303}
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004304
Bram Moolenaare677df82019-09-02 22:31:11 +02004305/*
4306 * Return TRUE if "val" is a valid 'spellang' value.
4307 */
4308 int
4309valid_spellang(char_u *val)
4310{
4311 return valid_name(val, ".-_,@");
4312}
4313
4314/*
4315 * Return TRUE if "val" is a valid 'spellfile' value.
4316 */
4317 int
4318valid_spellfile(char_u *val)
4319{
4320 char_u *s;
4321
4322 for (s = val; *s != NUL; ++s)
4323 if (!vim_isfilec(*s) && *s != ',')
4324 return FALSE;
4325 return TRUE;
4326}
4327
4328/*
4329 * Handle side effects of setting 'spell'.
4330 * Return an error message or NULL for success.
4331 */
4332 char *
4333did_set_spell_option(int is_spellfile)
4334{
4335 char *errmsg = NULL;
4336 win_T *wp;
4337 int l;
4338
4339 if (is_spellfile)
4340 {
4341 l = (int)STRLEN(curwin->w_s->b_p_spf);
4342 if (l > 0 && (l < 4
4343 || STRCMP(curwin->w_s->b_p_spf + l - 4, ".add") != 0))
4344 errmsg = e_invarg;
4345 }
4346
4347 if (errmsg == NULL)
4348 {
4349 FOR_ALL_WINDOWS(wp)
4350 if (wp->w_buffer == curbuf && wp->w_p_spell)
4351 {
4352 errmsg = did_set_spelllang(wp);
4353 break;
4354 }
4355 }
4356 return errmsg;
4357}
4358
4359/*
4360 * Set curbuf->b_cap_prog to the regexp program for 'spellcapcheck'.
4361 * Return error message when failed, NULL when OK.
4362 */
4363 char *
4364compile_cap_prog(synblock_T *synblock)
4365{
4366 regprog_T *rp = synblock->b_cap_prog;
4367 char_u *re;
4368
Bram Moolenaar53efb182019-10-13 19:49:26 +02004369 if (synblock->b_p_spc == NULL || *synblock->b_p_spc == NUL)
Bram Moolenaare677df82019-09-02 22:31:11 +02004370 synblock->b_cap_prog = NULL;
4371 else
4372 {
4373 // Prepend a ^ so that we only match at one column
4374 re = concat_str((char_u *)"^", synblock->b_p_spc);
4375 if (re != NULL)
4376 {
4377 synblock->b_cap_prog = vim_regcomp(re, RE_MAGIC);
4378 vim_free(re);
4379 if (synblock->b_cap_prog == NULL)
4380 {
4381 synblock->b_cap_prog = rp; // restore the previous program
4382 return e_invarg;
4383 }
4384 }
4385 }
4386
4387 vim_regfree(rp);
4388 return NULL;
4389}
4390
4391#endif // FEAT_SPELL