blob: da8ece03601fff88d49275c635c1fe9d5ab8949d [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaare19defe2005-03-21 08:23:33 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * spell.c: code for spell checking
Bram Moolenaarfc735152005-03-22 22:54:12 +000012 *
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020013 * See spellfile.c for the Vim spell file format.
14 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000015 * The spell checking mechanism uses a tree (aka trie). Each node in the tree
16 * has a list of bytes that can appear (siblings). For each byte there is a
17 * pointer to the node with the byte that follows in the word (child).
Bram Moolenaar9f30f502005-06-14 22:01:04 +000018 *
19 * A NUL byte is used where the word may end. The bytes are sorted, so that
20 * binary searching can be used and the NUL bytes are at the start. The
21 * number of possible bytes is stored before the list of bytes.
22 *
23 * The tree uses two arrays: "byts" stores the characters, "idxs" stores
24 * either the next index or flags. The tree starts at index 0. For example,
25 * to lookup "vi" this sequence is followed:
26 * i = 0
27 * len = byts[i]
28 * n = where "v" appears in byts[i + 1] to byts[i + len]
29 * i = idxs[n]
30 * len = byts[i]
31 * n = where "i" appears in byts[i + 1] to byts[i + len]
32 * i = idxs[n]
33 * len = byts[i]
34 * find that byts[i + 1] is 0, idxs[i + 1] has flags for "vi".
Bram Moolenaar51485f02005-06-04 21:55:20 +000035 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000036 * There are two word trees: one with case-folded words and one with words in
Bram Moolenaar51485f02005-06-04 21:55:20 +000037 * original case. The second one is only used for keep-case words and is
38 * usually small.
39 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +000040 * There is one additional tree for when not all prefixes are applied when
Bram Moolenaar1d73c882005-06-19 22:48:47 +000041 * generating the .spl file. This tree stores all the possible prefixes, as
42 * if they were words. At each word (prefix) end the prefix nr is stored, the
43 * following word must support this prefix nr. And the condition nr is
44 * stored, used to lookup the condition that the word must match with.
45 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000046 * Thanks to Olaf Seibert for providing an example implementation of this tree
47 * and the compression mechanism.
Bram Moolenaar4770d092006-01-12 23:22:24 +000048 * LZ trie ideas:
49 * http://www.irb.hr/hr/home/ristov/papers/RistovLZtrieRevision1.pdf
50 * More papers: http://www-igm.univ-mlv.fr/~laporte/publi_en.html
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000051 *
52 * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000053 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000054 * Why doesn't Vim use aspell/ispell/myspell/etc.?
55 * See ":help develop-spell".
56 */
57
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020058#define IN_SPELL_C
Bram Moolenaare19defe2005-03-21 08:23:33 +000059#include "vim.h"
60
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000061#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaare19defe2005-03-21 08:23:33 +000062
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010063#ifndef UNIX // it's in os_unix.h for Unix
64# include <time.h> // for time_t
Bram Moolenaar4770d092006-01-12 23:22:24 +000065#endif
66
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010067#define REGION_ALL 0xff // word valid in all regions
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000068
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010069// Result values. Lower number is accepted over higher one.
kylo252ae6f1d82022-02-16 19:24:07 +000070#define SP_BANNED (-1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000071#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000072#define SP_RARE 1
73#define SP_LOCAL 2
74#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000075
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000076/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000077 * Structure to store info for word matching.
78 */
79typedef struct matchinf_S
80{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010081 langp_T *mi_lp; // info for language and region
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000082
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010083 // pointers to original text to be checked
84 char_u *mi_word; // start of word being checked
85 char_u *mi_end; // end of matching word so far
86 char_u *mi_fend; // next char to be added to mi_fword
87 char_u *mi_cend; // char after what was used for
88 // mi_capflags
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000089
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010090 // case-folded text
91 char_u mi_fword[MAXWLEN + 1]; // mi_word case-folded
92 int mi_fwordlen; // nr of valid bytes in mi_fword
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000093
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010094 // for when checking word after a prefix
95 int mi_prefarridx; // index in sl_pidxs with list of
96 // affixID/condition
97 int mi_prefcnt; // number of entries at mi_prefarridx
98 int mi_prefixlen; // byte length of prefix
99 int mi_cprefixlen; // byte length of prefix in original
100 // case
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000101
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100102 // for when checking a compound word
103 int mi_compoff; // start of following word offset
104 char_u mi_compflags[MAXWLEN]; // flags for compound words used
105 int mi_complen; // nr of compound words used
106 int mi_compextra; // nr of COMPOUNDROOT words
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000107
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100108 // others
109 int mi_result; // result so far: SP_BAD, SP_OK, etc.
110 int mi_capflags; // WF_ONECAP WF_ALLCAP WF_KEEPCAP
111 win_T *mi_win; // buffer being checked
Bram Moolenaar78622822005-08-23 21:00:13 +0000112
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100113 // for NOBREAK
dundargocc57b5bc2022-11-02 13:30:51 +0000114 int mi_result2; // "mi_result" without following word
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100115 char_u *mi_end2; // "mi_end" without following word
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000116} matchinf_T;
117
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000118
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100119static int spell_mb_isword_class(int cl, win_T *wp);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000120
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100121// mode values for find_word
122#define FIND_FOLDWORD 0 // find word case-folded
123#define FIND_KEEPWORD 1 // find keep-case word
124#define FIND_PREFIX 2 // find word after prefix
125#define FIND_COMPOUND 3 // find case-folded compound word
126#define FIND_KEEPCOMPOUND 4 // find keep-case compound word
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000127
LemonBoyd0874502023-08-27 21:52:27 +0200128// type values for get_char_type
129#define CHAR_OTHER 0
130#define CHAR_UPPER 1
131#define CHAR_DIGIT 2
132
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void find_word(matchinf_T *mip, int mode);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100134static void find_prefix(matchinf_T *mip, int mode);
135static int fold_more(matchinf_T *mip);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100136static void spell_load_cb(char_u *fname, void *cookie);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static int count_syllables(slang_T *slang, char_u *word);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100138static void clear_midword(win_T *buf);
139static void use_midword(slang_T *lp, win_T *buf);
140static int find_region(char_u *rp, char_u *region);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100141static void spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res);
142static void spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static void spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100144static void dump_word(slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum);
145static linenr_T dump_prefixes(slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T startlnum);
LemonBoyd0874502023-08-27 21:52:27 +0200146static char_u *advance_camelcase_word(char_u *p, win_T *wp, int *is_camel_case);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000147
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000148/*
149 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000150 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000151 * "*attrp" is set to the highlight index for a badly spelled word. For a
152 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000153 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000154 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000155 * "capcol" is used to check for a Capitalised word after the end of a
156 * sentence. If it's zero then perform the check. Return the column where to
157 * check next, or -1 when no sentence end was found. If it's NULL then don't
158 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000159 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000160 * Returns the length of the word in bytes, also when it's OK, so that the
161 * caller can skip over the word.
162 */
163 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100164spell_check(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100165 win_T *wp, // current window
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100166 char_u *ptr,
167 hlf_T *attrp,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100168 int *capcol, // column to check for Capital
169 int docount) // count good words
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000170{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100171 matchinf_T mi; // Most things are put in "mi" so that it can
172 // be passed to functions quickly.
173 int nrlen = 0; // found a number first
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000174 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000175 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000176 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +0000177 int count_word = docount;
Bram Moolenaare0ebeda2020-06-10 22:17:58 +0200178 int use_camel_case = *wp->w_s->b_p_spo != NUL;
LemonBoyd0874502023-08-27 21:52:27 +0200179 int is_camel_case = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000180
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100181 // A word never starts at a space or a control character. Return quickly
182 // then, skipping over the character.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000183 if (*ptr <= ' ')
184 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000185
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100186 // Return here when loading language files failed.
Bram Moolenaar860cae12010-06-05 23:22:07 +0200187 if (wp->w_s->b_langp.ga_len == 0)
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000188 return 1;
189
Bram Moolenaara80faa82020-04-12 19:37:17 +0200190 CLEAR_FIELD(mi);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000191
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100192 // A number is always OK. Also skip hexadecimal numbers 0xFF99 and
193 // 0X99FF. But always do check spelling to find "3GPP" and "11
194 // julifeest".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000195 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000196 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100197 if (*ptr == '0' && (ptr[1] == 'b' || ptr[1] == 'B'))
198 mi.mi_end = skipbin(ptr + 2);
199 else if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
Bram Moolenaar3982c542005-06-08 21:56:31 +0000200 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000201 else
202 mi.mi_end = skipdigits(ptr);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000203 nrlen = (int)(mi.mi_end - ptr);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000204 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000205
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100206 // Find the normal end of the word (until the next non-word character).
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000207 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +0000208 mi.mi_fend = ptr;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200209 if (spell_iswordp(mi.mi_fend, wp))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000210 {
Bram Moolenaare0ebeda2020-06-10 22:17:58 +0200211 if (use_camel_case)
LemonBoyd0874502023-08-27 21:52:27 +0200212 mi.mi_fend = advance_camelcase_word(ptr, wp, &is_camel_case);
213 else
Bram Moolenaare0ebeda2020-06-10 22:17:58 +0200214 {
LemonBoyd0874502023-08-27 21:52:27 +0200215 do
Bram Moolenaare0ebeda2020-06-10 22:17:58 +0200216 {
LemonBoyd0874502023-08-27 21:52:27 +0200217 MB_PTR_ADV(mi.mi_fend);
218 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp));
219 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000220
Bram Moolenaar860cae12010-06-05 23:22:07 +0200221 if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000222 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100223 // Check word starting with capital letter.
Bram Moolenaar53805d12005-08-01 07:08:33 +0000224 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000225 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000226 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000227 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000228 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000229 if (capcol != NULL)
230 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000231
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100232 // We always use the characters up to the next non-word character,
233 // also for bad words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000234 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000235
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100236 // Check caps type later.
Bram Moolenaar860cae12010-06-05 23:22:07 +0200237 mi.mi_capflags = 0;
238 mi.mi_cend = NULL;
239 mi.mi_win = wp;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000240
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100241 // case-fold the word with one non-word character, so that we can check
242 // for the word end.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000243 if (*mi.mi_fend != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100244 MB_PTR_ADV(mi.mi_fend);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000245
Bram Moolenaar4f135272021-06-11 19:07:40 +0200246 (void)spell_casefold(wp, ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000247 MAXWLEN + 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000248 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000249
LemonBoyd0874502023-08-27 21:52:27 +0200250 if (is_camel_case && mi.mi_fwordlen > 0)
Bram Moolenaare0ebeda2020-06-10 22:17:58 +0200251 // Introduce a fake word end space into the folded word.
252 mi.mi_fword[mi.mi_fwordlen - 1] = ' ';
253
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100254 // The word is bad unless we recognize it.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000255 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +0000256 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000257
258 /*
259 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000260 * We check them all, because a word may be matched longer in another
261 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000262 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200263 for (lpi = 0; lpi < wp->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000264 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200265 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, lpi);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000266
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100267 // If reloading fails the language is still in the list but everything
268 // has been cleared.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000269 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
270 continue;
271
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100272 // Check for a matching word in case-folded words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000273 find_word(&mi, FIND_FOLDWORD);
274
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100275 // Check for a matching word in keep-case words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000276 find_word(&mi, FIND_KEEPWORD);
277
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100278 // Check for matching prefixes.
Bram Moolenaard12a1322005-08-21 22:08:24 +0000279 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +0000280
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100281 // For a NOBREAK language, may want to use a word without a following
282 // word as a backup.
Bram Moolenaar78622822005-08-23 21:00:13 +0000283 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
284 && mi.mi_result2 != SP_BAD)
285 {
286 mi.mi_result = mi.mi_result2;
287 mi.mi_end = mi.mi_end2;
288 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000289
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100290 // Count the word in the first language where it's found to be OK.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000291 if (count_word && mi.mi_result == SP_OK)
292 {
293 count_common_word(mi.mi_lp->lp_slang, ptr,
294 (int)(mi.mi_end - ptr), 1);
295 count_word = FALSE;
296 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000297 }
298
299 if (mi.mi_result != SP_OK)
300 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100301 // If we found a number skip over it. Allows for "42nd". Do flag
302 // rare and local words, e.g., "3GPP".
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000303 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000304 {
305 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
306 return nrlen;
307 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000308
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100309 // When we are at a non-word character there is no error, just
310 // skip over the character (try looking for a word after it).
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100311 else if (!spell_iswordp_nmw(ptr, wp))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000312 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200313 if (capcol != NULL && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000314 {
315 regmatch_T regmatch;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100316 int r;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000317
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100318 // Check for end of sentence.
Bram Moolenaar860cae12010-06-05 23:22:07 +0200319 regmatch.regprog = wp->w_s->b_cap_prog;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000320 regmatch.rm_ic = FALSE;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100321 r = vim_regexec(&regmatch, ptr, 0);
322 wp->w_s->b_cap_prog = regmatch.regprog;
323 if (r)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000324 *capcol = (int)(regmatch.endp[0] - ptr);
325 }
326
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000327 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000328 return (*mb_ptr2len)(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000329 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000330 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000331 else if (mi.mi_end == ptr)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100332 // Always include at least one character. Required for when there
333 // is a mixup in "midword".
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100334 MB_PTR_ADV(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +0000335 else if (mi.mi_result == SP_BAD
Bram Moolenaar860cae12010-06-05 23:22:07 +0200336 && LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak)
Bram Moolenaar78622822005-08-23 21:00:13 +0000337 {
338 char_u *p, *fp;
339 int save_result = mi.mi_result;
340
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100341 // First language in 'spelllang' is NOBREAK. Find first position
342 // at which any word would be valid.
Bram Moolenaar860cae12010-06-05 23:22:07 +0200343 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000344 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +0000345 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000346 p = mi.mi_word;
347 fp = mi.mi_fword;
348 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +0000349 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100350 MB_PTR_ADV(p);
351 MB_PTR_ADV(fp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000352 if (p >= mi.mi_end)
353 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000354 mi.mi_compoff = (int)(fp - mi.mi_fword);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000355 find_word(&mi, FIND_COMPOUND);
356 if (mi.mi_result != SP_BAD)
357 {
358 mi.mi_end = p;
359 break;
360 }
Bram Moolenaar78622822005-08-23 21:00:13 +0000361 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000362 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +0000363 }
Bram Moolenaar78622822005-08-23 21:00:13 +0000364 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000365
366 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000367 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000368 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000369 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000370 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000371 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000372 }
373
Bram Moolenaar5195e452005-08-19 20:32:47 +0000374 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
375 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100376 // Report SpellCap only when the word isn't badly spelled.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000377 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000378 return wrongcaplen;
379 }
380
Bram Moolenaar51485f02005-06-04 21:55:20 +0000381 return (int)(mi.mi_end - ptr);
382}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000383
Bram Moolenaar51485f02005-06-04 21:55:20 +0000384/*
LemonBoyd0874502023-08-27 21:52:27 +0200385 * Determine the type of character 'c'.
386 */
387 static int
388get_char_type(int c)
389{
390 if (VIM_ISDIGIT(c))
391 return CHAR_DIGIT;
392 if (SPELL_ISUPPER(c))
393 return CHAR_UPPER;
394 return CHAR_OTHER;
395}
396
397/*
398 * Returns a pointer to the end of the word starting at "str".
399 * Supports camelCase words.
400 */
401 static char_u *
402advance_camelcase_word(char_u *str, win_T *wp, int *is_camel_case)
403{
404 int last_type, last_last_type, this_type;
405 int c;
406 char_u *end = str;
407
408 *is_camel_case = FALSE;
409
410 if (*str == NUL)
411 return str;
412
413 c = PTR2CHAR(end);
414 MB_PTR_ADV(end);
415 // We need at most the types of the type of the last two chars.
416 last_last_type = -1;
417 last_type = get_char_type(c);
418
419 while (*end != NUL && spell_iswordp(end, wp))
420 {
421 c = PTR2CHAR(end);
422 this_type = get_char_type(c);
423
424 if (last_last_type == CHAR_UPPER && last_type == CHAR_UPPER
425 && this_type == CHAR_OTHER)
426 {
427 // Handle the following cases:
428 // UpperUpperLower
429 *is_camel_case = TRUE;
430 // Back up by one char.
431 MB_PTR_BACK(str, end);
432 break;
433 }
434 else if ((this_type == CHAR_UPPER && last_type == CHAR_OTHER)
435 || (this_type != last_type
436 && (this_type == CHAR_DIGIT || last_type == CHAR_DIGIT)))
437 {
438 // Handle the following cases:
439 // LowerUpper LowerDigit UpperDigit DigitUpper DigitLower
440 *is_camel_case = TRUE;
441 break;
442 }
443
444 last_last_type = last_type;
445 last_type = this_type;
446
447 MB_PTR_ADV(end);
448 }
449
450 return end;
451}
452
453/*
Bram Moolenaar51485f02005-06-04 21:55:20 +0000454 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000455 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
456 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
457 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
458 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000459 *
460 * For a match mip->mi_result is updated.
461 */
462 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100463find_word(matchinf_T *mip, int mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000464{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000465 idx_T arridx = 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100466 int endlen[MAXWLEN]; // length at possible word endings
467 idx_T endidx[MAXWLEN]; // possible word endings
Bram Moolenaar51485f02005-06-04 21:55:20 +0000468 int endidxcnt = 0;
469 int len;
470 int wlen = 0;
471 int flen;
472 int c;
473 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000474 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000475 char_u *s;
Bram Moolenaare52325c2005-08-22 22:54:29 +0000476 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000477 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000478 slang_T *slang = mip->mi_lp->lp_slang;
479 unsigned flags;
480 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000481 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000482 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000483 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +0000484 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000485
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000486 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000487 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100488 // Check for word with matching case in keep-case tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000489 ptr = mip->mi_word;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100490 flen = 9999; // no case folding, always enough bytes
Bram Moolenaar51485f02005-06-04 21:55:20 +0000491 byts = slang->sl_kbyts;
492 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000493
494 if (mode == FIND_KEEPCOMPOUND)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100495 // Skip over the previously found word(s).
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000496 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000497 }
498 else
499 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100500 // Check for case-folded in case-folded tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000501 ptr = mip->mi_fword;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100502 flen = mip->mi_fwordlen; // available case-folded bytes
Bram Moolenaar51485f02005-06-04 21:55:20 +0000503 byts = slang->sl_fbyts;
504 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000505
506 if (mode == FIND_PREFIX)
507 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100508 // Skip over the prefix.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000509 wlen = mip->mi_prefixlen;
510 flen -= mip->mi_prefixlen;
511 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000512 else if (mode == FIND_COMPOUND)
513 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100514 // Skip over the previously found word(s).
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000515 wlen = mip->mi_compoff;
516 flen -= mip->mi_compoff;
517 }
518
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000519 }
520
Bram Moolenaar51485f02005-06-04 21:55:20 +0000521 if (byts == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100522 return; // array is empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000523
Bram Moolenaar51485f02005-06-04 21:55:20 +0000524 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000525 * Repeat advancing in the tree until:
526 * - there is a byte that doesn't match,
527 * - we reach the end of the tree,
528 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000529 */
530 for (;;)
531 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000532 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000533 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000534
535 len = byts[arridx++];
536
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100537 // If the first possible byte is a zero the word could end here.
538 // Remember this index, we first check for the longest word.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000539 if (byts[arridx] == 0)
540 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000541 if (endidxcnt == MAXWLEN)
542 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100543 // Must be a corrupted spell file.
Bram Moolenaar677658a2022-01-05 16:09:06 +0000544 emsg(_(e_format_error_in_spell_file));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000545 return;
546 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000547 endlen[endidxcnt] = wlen;
548 endidx[endidxcnt++] = arridx++;
549 --len;
550
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100551 // Skip over the zeros, there can be several flag/region
552 // combinations.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000553 while (len > 0 && byts[arridx] == 0)
554 {
555 ++arridx;
556 --len;
557 }
558 if (len == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100559 break; // no children, word must end here
Bram Moolenaar51485f02005-06-04 21:55:20 +0000560 }
561
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100562 // Stop looking at end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000563 if (ptr[wlen] == NUL)
564 break;
565
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100566 // Perform a binary search in the list of accepted bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000567 c = ptr[wlen];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100568 if (c == TAB) // <Tab> is handled like <Space>
Bram Moolenaar0c405862005-06-22 22:26:26 +0000569 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +0000570 lo = arridx;
571 hi = arridx + len - 1;
572 while (lo < hi)
573 {
574 m = (lo + hi) / 2;
575 if (byts[m] > c)
576 hi = m - 1;
577 else if (byts[m] < c)
578 lo = m + 1;
579 else
580 {
581 lo = hi = m;
582 break;
583 }
584 }
585
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100586 // Stop if there is no matching byte.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000587 if (hi < lo || byts[lo] != c)
588 break;
589
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100590 // Continue at the child (if there is one).
Bram Moolenaar51485f02005-06-04 21:55:20 +0000591 arridx = idxs[lo];
592 ++wlen;
593 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +0000594
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100595 // One space in the good word may stand for several spaces in the
596 // checked word.
Bram Moolenaar0c405862005-06-22 22:26:26 +0000597 if (c == ' ')
598 {
599 for (;;)
600 {
601 if (flen <= 0 && *mip->mi_fend != NUL)
602 flen = fold_more(mip);
603 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
604 break;
605 ++wlen;
606 --flen;
607 }
608 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000609 }
610
611 /*
612 * Verify that one of the possible endings is valid. Try the longest
613 * first.
614 */
615 while (endidxcnt > 0)
616 {
617 --endidxcnt;
618 arridx = endidx[endidxcnt];
619 wlen = endlen[endidxcnt];
620
Bram Moolenaar51485f02005-06-04 21:55:20 +0000621 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100622 continue; // not at first byte of character
Bram Moolenaar860cae12010-06-05 23:22:07 +0200623 if (spell_iswordp(ptr + wlen, mip->mi_win))
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000624 {
Bram Moolenaar78622822005-08-23 21:00:13 +0000625 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100626 continue; // next char is a word character
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000627 word_ends = FALSE;
628 }
629 else
630 word_ends = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100631 // The prefix flag is before compound flags. Once a valid prefix flag
632 // has been found we try compound flags.
Bram Moolenaard12a1322005-08-21 22:08:24 +0000633 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000634
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000635 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000636 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100637 // Compute byte length in original word, length may change
638 // when folding case. This can be slow, take a shortcut when the
639 // case-folded word is equal to the keep-case word.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000640 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000641 if (STRNCMP(ptr, p, wlen) != 0)
642 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100643 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s))
644 MB_PTR_ADV(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000645 wlen = (int)(p - mip->mi_word);
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000646 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000647 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000648
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100649 // Check flags and region. For FIND_PREFIX check the condition and
650 // prefix ID.
651 // Repeat this if there are more flags/region alternatives until there
652 // is a match.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000653 res = SP_BAD;
654 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
655 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000656 {
657 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000658
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100659 // For the fold-case tree check that the case of the checked word
660 // matches with what the word in the tree requires.
661 // For keep-case tree the case is always right. For prefixes we
662 // don't bother to check.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000663 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000664 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000665 if (mip->mi_cend != mip->mi_word + wlen)
666 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100667 // mi_capflags was set for a different word length, need
668 // to do it again.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000669 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000670 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000671 }
672
Bram Moolenaar0c405862005-06-22 22:26:26 +0000673 if (mip->mi_capflags == WF_KEEPCAP
674 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000675 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000676 }
677
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100678 // When mode is FIND_PREFIX the word must support the prefix:
679 // check the prefix ID and the condition. Do that for the list at
680 // mip->mi_prefarridx that find_prefix() filled.
Bram Moolenaard12a1322005-08-21 22:08:24 +0000681 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000682 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000683 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000684 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +0000685 mip->mi_word + mip->mi_cprefixlen, slang,
686 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000687 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000688 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000689
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100690 // Use the WF_RARE flag for a rare prefix.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000691 if (c & WF_RAREPFX)
692 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000693 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000694 }
695
Bram Moolenaar78622822005-08-23 21:00:13 +0000696 if (slang->sl_nobreak)
697 {
698 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
699 && (flags & WF_BANNED) == 0)
700 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100701 // NOBREAK: found a valid following word. That's all we
702 // need to know, so return.
Bram Moolenaar78622822005-08-23 21:00:13 +0000703 mip->mi_result = SP_OK;
704 break;
705 }
706 }
707
708 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
709 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000710 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100711 // If there is no compound flag or the word is shorter than
712 // COMPOUNDMIN reject it quickly.
713 // Makes you wonder why someone puts a compound flag on a word
714 // that's too short... Myspell compatibility requires this
715 // anyway.
Bram Moolenaare52325c2005-08-22 22:54:29 +0000716 if (((unsigned)flags >> 24) == 0
717 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000718 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100719 // For multi-byte chars check character length against
720 // COMPOUNDMIN.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000721 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000722 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000723 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
724 wlen - mip->mi_compoff) < slang->sl_compminlen)
725 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000726
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100727 // Limit the number of compound words to COMPOUNDWORDMAX if no
728 // maximum for syllables is specified.
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000729 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
730 > slang->sl_compmax
Bram Moolenaare52325c2005-08-22 22:54:29 +0000731 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000732 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000733
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100734 // Don't allow compounding on a side where an affix was added,
735 // unless COMPOUNDPERMITFLAG was used.
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000736 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
737 continue;
738 if (!word_ends && (flags & WF_NOCOMPAFT))
739 continue;
740
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100741 // Quickly check if compounding is possible with this flag.
Bram Moolenaar6de68532005-08-24 22:08:48 +0000742 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +0000743 ? slang->sl_compstartflags
744 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +0000745 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000746 continue;
747
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100748 // If there is a match with a CHECKCOMPOUNDPATTERN rule
749 // discard the compound word.
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000750 if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat))
751 continue;
752
Bram Moolenaare52325c2005-08-22 22:54:29 +0000753 if (mode == FIND_COMPOUND)
754 {
755 int capflags;
756
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100757 // Need to check the caps type of the appended compound
758 // word.
Bram Moolenaare52325c2005-08-22 22:54:29 +0000759 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
760 mip->mi_compoff) != 0)
761 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100762 // case folding may have changed the length
Bram Moolenaare52325c2005-08-22 22:54:29 +0000763 p = mip->mi_word;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100764 for (s = ptr; s < ptr + mip->mi_compoff; MB_PTR_ADV(s))
765 MB_PTR_ADV(p);
Bram Moolenaare52325c2005-08-22 22:54:29 +0000766 }
767 else
Bram Moolenaare52325c2005-08-22 22:54:29 +0000768 p = mip->mi_word + mip->mi_compoff;
769 capflags = captype(p, mip->mi_word + wlen);
770 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
771 && (flags & WF_FIXCAP) != 0))
772 continue;
773
774 if (capflags != WF_ALLCAP)
775 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100776 // When the character before the word is a word
777 // character we do not accept a Onecap word. We do
778 // accept a no-caps word, even when the dictionary
779 // word specifies ONECAP.
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100780 MB_PTR_BACK(mip->mi_word, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100781 if (spell_iswordp_nmw(p, mip->mi_win)
Bram Moolenaare52325c2005-08-22 22:54:29 +0000782 ? capflags == WF_ONECAP
783 : (flags & WF_ONECAP) != 0
784 && capflags != WF_ONECAP)
785 continue;
786 }
787 }
788
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100789 // If the word ends the sequence of compound flags of the
790 // words must match with one of the COMPOUNDRULE items and
791 // the number of syllables must not be too large.
Bram Moolenaar5195e452005-08-19 20:32:47 +0000792 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
793 mip->mi_compflags[mip->mi_complen + 1] = NUL;
794 if (word_ends)
795 {
796 char_u fword[MAXWLEN];
797
798 if (slang->sl_compsylmax < MAXWLEN)
799 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100800 // "fword" is only needed for checking syllables.
Bram Moolenaar5195e452005-08-19 20:32:47 +0000801 if (ptr == mip->mi_word)
Bram Moolenaar4f135272021-06-11 19:07:40 +0200802 (void)spell_casefold(mip->mi_win,
803 ptr, wlen, fword, MAXWLEN);
Bram Moolenaar5195e452005-08-19 20:32:47 +0000804 else
805 vim_strncpy(fword, ptr, endlen[endidxcnt]);
806 }
807 if (!can_compound(slang, fword, mip->mi_compflags))
808 continue;
809 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000810 else if (slang->sl_comprules != NULL
811 && !match_compoundrule(slang, mip->mi_compflags))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100812 // The compound flags collected so far do not match any
813 // COMPOUNDRULE, discard the compounded word.
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000814 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000815 }
816
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100817 // Check NEEDCOMPOUND: can't use word without compounding.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000818 else if (flags & WF_NEEDCOMP)
819 continue;
820
Bram Moolenaar78622822005-08-23 21:00:13 +0000821 nobreak_result = SP_OK;
822
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000823 if (!word_ends)
824 {
Bram Moolenaar78622822005-08-23 21:00:13 +0000825 int save_result = mip->mi_result;
826 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000827 langp_T *save_lp = mip->mi_lp;
828 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +0000829
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100830 // Check that a valid word follows. If there is one and we
831 // are compounding, it will set "mi_result", thus we are
832 // always finished here. For NOBREAK we only check that a
833 // valid word follows.
834 // Recursive!
Bram Moolenaar78622822005-08-23 21:00:13 +0000835 if (slang->sl_nobreak)
836 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000837
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100838 // Find following word in case-folded tree.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000839 mip->mi_compoff = endlen[endidxcnt];
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000840 if (has_mbyte && mode == FIND_KEEPWORD)
841 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100842 // Compute byte length in case-folded word from "wlen":
843 // byte length in keep-case word. Length may change when
844 // folding case. This can be slow, take a shortcut when
845 // the case-folded word is equal to the keep-case word.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000846 p = mip->mi_fword;
847 if (STRNCMP(ptr, p, wlen) != 0)
848 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100849 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s))
850 MB_PTR_ADV(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000851 mip->mi_compoff = (int)(p - mip->mi_fword);
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000852 }
853 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100854#if 0 // Disabled, see below
Bram Moolenaard12a1322005-08-21 22:08:24 +0000855 c = mip->mi_compoff;
Bram Moolenaarba534352016-04-21 09:20:26 +0200856#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000857 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000858 if (flags & WF_COMPROOT)
859 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000860
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100861 // For NOBREAK we need to try all NOBREAK languages, at least
862 // to find the ".add" file(s).
Bram Moolenaar860cae12010-06-05 23:22:07 +0200863 for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +0000864 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000865 if (slang->sl_nobreak)
866 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200867 mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi);
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000868 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
869 || !mip->mi_lp->lp_slang->sl_nobreak)
870 continue;
871 }
Bram Moolenaard12a1322005-08-21 22:08:24 +0000872
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000873 find_word(mip, FIND_COMPOUND);
874
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100875 // When NOBREAK any word that matches is OK. Otherwise we
876 // need to find the longest match, thus try with keep-case
877 // and prefix too.
Bram Moolenaar78622822005-08-23 21:00:13 +0000878 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
879 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100880 // Find following word in keep-case tree.
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000881 mip->mi_compoff = wlen;
882 find_word(mip, FIND_KEEPCOMPOUND);
883
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100884#if 0 // Disabled, a prefix must not appear halfway a compound word,
885 // unless the COMPOUNDPERMITFLAG is used and then it can't be a
886 // postponed prefix.
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000887 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
888 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100889 // Check for following word with prefix.
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000890 mip->mi_compoff = c;
891 find_prefix(mip, FIND_COMPOUND);
892 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000893#endif
Bram Moolenaar78622822005-08-23 21:00:13 +0000894 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000895
896 if (!slang->sl_nobreak)
897 break;
Bram Moolenaar78622822005-08-23 21:00:13 +0000898 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000899 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000900 if (flags & WF_COMPROOT)
901 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000902 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000903
Bram Moolenaar78622822005-08-23 21:00:13 +0000904 if (slang->sl_nobreak)
905 {
906 nobreak_result = mip->mi_result;
907 mip->mi_result = save_result;
908 mip->mi_end = save_end;
909 }
910 else
911 {
912 if (mip->mi_result == SP_OK)
913 break;
914 continue;
915 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000916 }
917
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000918 if (flags & WF_BANNED)
919 res = SP_BANNED;
920 else if (flags & WF_REGION)
921 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100922 // Check region.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000923 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000924 res = SP_OK;
925 else
926 res = SP_LOCAL;
927 }
928 else if (flags & WF_RARE)
929 res = SP_RARE;
930 else
931 res = SP_OK;
932
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100933 // Always use the longest match and the best result. For NOBREAK
934 // we separately keep the longest match without a following good
935 // word as a fall-back.
Bram Moolenaar78622822005-08-23 21:00:13 +0000936 if (nobreak_result == SP_BAD)
937 {
938 if (mip->mi_result2 > res)
939 {
940 mip->mi_result2 = res;
941 mip->mi_end2 = mip->mi_word + wlen;
942 }
943 else if (mip->mi_result2 == res
944 && mip->mi_end2 < mip->mi_word + wlen)
945 mip->mi_end2 = mip->mi_word + wlen;
946 }
947 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000948 {
949 mip->mi_result = res;
950 mip->mi_end = mip->mi_word + wlen;
951 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000952 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000953 mip->mi_end = mip->mi_word + wlen;
954
Bram Moolenaar78622822005-08-23 21:00:13 +0000955 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000956 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000957 }
958
Bram Moolenaar78622822005-08-23 21:00:13 +0000959 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000960 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000961 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000962}
963
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000964/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000965 * Return TRUE if there is a match between the word ptr[wlen] and
966 * CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another
967 * word.
968 * A match means that the first part of CHECKCOMPOUNDPATTERN matches at the
969 * end of ptr[wlen] and the second part matches after it.
970 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +0200971 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100972match_checkcompoundpattern(
973 char_u *ptr,
974 int wlen,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100975 garray_T *gap) // &sl_comppat
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000976{
977 int i;
978 char_u *p;
979 int len;
980
981 for (i = 0; i + 1 < gap->ga_len; i += 2)
982 {
983 p = ((char_u **)gap->ga_data)[i + 1];
984 if (STRNCMP(ptr + wlen, p, STRLEN(p)) == 0)
985 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100986 // Second part matches at start of following compound word, now
987 // check if first part matches at end of previous word.
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000988 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar19c9c762008-12-09 21:34:39 +0000989 len = (int)STRLEN(p);
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000990 if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0)
991 return TRUE;
992 }
993 }
994 return FALSE;
995}
996
997/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000998 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
999 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001000 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02001001 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001002can_compound(slang_T *slang, char_u *word, char_u *flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001003{
Bram Moolenaar6de68532005-08-24 22:08:48 +00001004 char_u uflags[MAXWLEN * 2];
1005 int i;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001006 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001007
1008 if (slang->sl_compprog == NULL)
1009 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001010 if (enc_utf8)
1011 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001012 // Need to convert the single byte flags to utf8 characters.
Bram Moolenaar6de68532005-08-24 22:08:48 +00001013 p = uflags;
1014 for (i = 0; flags[i] != NUL; ++i)
Bram Moolenaarace95982017-03-29 17:30:27 +02001015 p += utf_char2bytes(flags[i], p);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001016 *p = NUL;
1017 p = uflags;
1018 }
1019 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00001020 p = flags;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001021 if (!vim_regexec_prog(&slang->sl_compprog, FALSE, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001022 return FALSE;
1023
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001024 // Count the number of syllables. This may be slow, do it last. If there
1025 // are too many syllables AND the number of compound words is above
1026 // COMPOUNDWORDMAX then compounding is not allowed.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001027 if (slang->sl_compsylmax < MAXWLEN
1028 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001029 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001030 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001031}
1032
1033/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001034 * Return TRUE if the compound flags in compflags[] match the start of any
1035 * compound rule. This is used to stop trying a compound if the flags
1036 * collected so far can't possibly match any compound rule.
1037 * Caller must check that slang->sl_comprules is not NULL.
1038 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02001039 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001040match_compoundrule(slang_T *slang, char_u *compflags)
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001041{
1042 char_u *p;
1043 int i;
1044 int c;
1045
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001046 // loop over all the COMPOUNDRULE entries
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001047 for (p = slang->sl_comprules; *p != NUL; ++p)
1048 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001049 // loop over the flags in the compound word we have made, match
1050 // them against the current rule entry
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001051 for (i = 0; ; ++i)
1052 {
1053 c = compflags[i];
1054 if (c == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001055 // found a rule that matches for the flags we have so far
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001056 return TRUE;
1057 if (*p == '/' || *p == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001058 break; // end of rule, it's too short
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001059 if (*p == '[')
1060 {
1061 int match = FALSE;
1062
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001063 // compare against all the flags in []
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001064 ++p;
1065 while (*p != ']' && *p != NUL)
1066 if (*p++ == c)
1067 match = TRUE;
1068 if (!match)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001069 break; // none matches
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001070 }
1071 else if (*p != c)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001072 break; // flag of word doesn't match flag in pattern
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001073 ++p;
1074 }
1075
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001076 // Skip to the next "/", where the next pattern starts.
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001077 p = vim_strchr(p, '/');
1078 if (p == NULL)
1079 break;
1080 }
1081
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001082 // Checked all the rules and none of them match the flags, so there
1083 // can't possibly be a compound starting with these flags.
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001084 return FALSE;
1085}
1086
1087/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001088 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1089 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001090 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001091 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02001092 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001093valid_word_prefix(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001094 int totprefcnt, // nr of prefix IDs
1095 int arridx, // idx in sl_pidxs[]
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001096 int flags,
1097 char_u *word,
1098 slang_T *slang,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001099 int cond_req) // only use prefixes with a condition
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001100{
1101 int prefcnt;
1102 int pidx;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001103 regprog_T **rp;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001104 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001105
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001106 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001107 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1108 {
1109 pidx = slang->sl_pidxs[arridx + prefcnt];
1110
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001111 // Check the prefix ID.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001112 if (prefid != (pidx & 0xff))
1113 continue;
1114
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001115 // Check if the prefix doesn't combine and the word already has a
1116 // suffix.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001117 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1118 continue;
1119
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001120 // Check the condition, if there is one. The condition index is
1121 // stored in the two bytes above the prefix ID byte.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001122 rp = &slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
1123 if (*rp != NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001124 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001125 if (!vim_regexec_prog(rp, FALSE, word, 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001126 continue;
1127 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001128 else if (cond_req)
1129 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001130
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001131 // It's a match! Return the WF_ flags.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001132 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001133 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001134 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001135}
1136
1137/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001138 * Check if the word at "mip->mi_word" has a matching prefix.
1139 * If it does, then check the following word.
1140 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001141 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1142 * prefix in a compound word.
1143 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001144 * For a match mip->mi_result is updated.
1145 */
1146 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001147find_prefix(matchinf_T *mip, int mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001148{
1149 idx_T arridx = 0;
1150 int len;
1151 int wlen = 0;
1152 int flen;
1153 int c;
1154 char_u *ptr;
1155 idx_T lo, hi, m;
1156 slang_T *slang = mip->mi_lp->lp_slang;
1157 char_u *byts;
1158 idx_T *idxs;
1159
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001160 byts = slang->sl_pbyts;
1161 if (byts == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001162 return; // array is empty
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001163
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001164 // We use the case-folded word here, since prefixes are always
1165 // case-folded.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001166 ptr = mip->mi_fword;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001167 flen = mip->mi_fwordlen; // available case-folded bytes
Bram Moolenaard12a1322005-08-21 22:08:24 +00001168 if (mode == FIND_COMPOUND)
1169 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001170 // Skip over the previously found word(s).
Bram Moolenaard12a1322005-08-21 22:08:24 +00001171 ptr += mip->mi_compoff;
1172 flen -= mip->mi_compoff;
1173 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001174 idxs = slang->sl_pidxs;
1175
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001176 /*
1177 * Repeat advancing in the tree until:
1178 * - there is a byte that doesn't match,
1179 * - we reach the end of the tree,
1180 * - or we reach the end of the line.
1181 */
1182 for (;;)
1183 {
1184 if (flen == 0 && *mip->mi_fend != NUL)
1185 flen = fold_more(mip);
1186
1187 len = byts[arridx++];
1188
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001189 // If the first possible byte is a zero the prefix could end here.
1190 // Check if the following word matches and supports the prefix.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001191 if (byts[arridx] == 0)
1192 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001193 // There can be several prefixes with different conditions. We
1194 // try them all, since we don't know which one will give the
1195 // longest match. The word is the same each time, pass the list
1196 // of possible prefixes to find_word().
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001197 mip->mi_prefarridx = arridx;
1198 mip->mi_prefcnt = len;
1199 while (len > 0 && byts[arridx] == 0)
1200 {
1201 ++arridx;
1202 --len;
1203 }
1204 mip->mi_prefcnt -= len;
1205
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001206 // Find the word that comes after the prefix.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001207 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001208 if (mode == FIND_COMPOUND)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001209 // Skip over the previously found word(s).
Bram Moolenaard12a1322005-08-21 22:08:24 +00001210 mip->mi_prefixlen += mip->mi_compoff;
1211
Bram Moolenaar53805d12005-08-01 07:08:33 +00001212 if (has_mbyte)
1213 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001214 // Case-folded length may differ from original length.
Bram Moolenaard12a1322005-08-21 22:08:24 +00001215 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1216 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001217 }
1218 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001219 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001220 find_word(mip, FIND_PREFIX);
1221
1222
1223 if (len == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001224 break; // no children, word must end here
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001225 }
1226
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001227 // Stop looking at end of the line.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001228 if (ptr[wlen] == NUL)
1229 break;
1230
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001231 // Perform a binary search in the list of accepted bytes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001232 c = ptr[wlen];
1233 lo = arridx;
1234 hi = arridx + len - 1;
1235 while (lo < hi)
1236 {
1237 m = (lo + hi) / 2;
1238 if (byts[m] > c)
1239 hi = m - 1;
1240 else if (byts[m] < c)
1241 lo = m + 1;
1242 else
1243 {
1244 lo = hi = m;
1245 break;
1246 }
1247 }
1248
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001249 // Stop if there is no matching byte.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001250 if (hi < lo || byts[lo] != c)
1251 break;
1252
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001253 // Continue at the child (if there is one).
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001254 arridx = idxs[lo];
1255 ++wlen;
1256 --flen;
1257 }
1258}
1259
1260/*
1261 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001262 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001263 * Return the length of the folded chars in bytes.
1264 */
1265 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001266fold_more(matchinf_T *mip)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001267{
1268 int flen;
1269 char_u *p;
1270
1271 p = mip->mi_fend;
1272 do
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001273 MB_PTR_ADV(mip->mi_fend);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001274 while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001275
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001276 // Include the non-word character so that we can check for the word end.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001277 if (*mip->mi_fend != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001278 MB_PTR_ADV(mip->mi_fend);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001279
Bram Moolenaar4f135272021-06-11 19:07:40 +02001280 (void)spell_casefold(mip->mi_win, p, (int)(mip->mi_fend - p),
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001281 mip->mi_fword + mip->mi_fwordlen,
1282 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001283 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001284 mip->mi_fwordlen += flen;
1285 return flen;
1286}
1287
1288/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001289 * Check case flags for a word. Return TRUE if the word has the requested
1290 * case.
1291 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02001292 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001293spell_valid_case(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001294 int wordflags, // flags for the checked word.
1295 int treeflags) // flags for the word in the spell tree
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001296{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001297 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001298 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001299 && ((treeflags & WF_ONECAP) == 0
1300 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001301}
1302
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001303/*
Bram Moolenaaree09fcc2022-09-25 20:58:30 +01001304 * Return TRUE if spell checking is enabled for "wp".
1305 */
1306 int
1307spell_check_window(win_T *wp)
1308{
1309 return wp->w_p_spell
1310 && *wp->w_s->b_p_spl != NUL
1311 && wp->w_s->b_langp.ga_len > 0
1312 && *(char **)(wp->w_s->b_langp.ga_data) != NULL;
1313}
1314
1315/*
1316 * Return TRUE and give an error if spell checking is not enabled.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001317 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +02001318 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001319no_spell_checking(win_T *wp)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001320{
Bram Moolenaaree09fcc2022-09-25 20:58:30 +01001321 if (spell_check_window(wp))
1322 return FALSE;
1323 emsg(_(e_spell_checking_is_not_possible));
1324 return TRUE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001325}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001326
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001327/*
1328 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001329 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
1330 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001331 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1332 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00001333 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001334 */
1335 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001336spell_move_to(
1337 win_T *wp,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001338 int dir, // FORWARD or BACKWARD
1339 int allwords, // TRUE for "[s"/"]s", FALSE for "[S"/"]S"
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001340 int curline,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001341 hlf_T *attrp) // return: attributes of bad word or NULL
1342 // (only when "dir" is FORWARD)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001343{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001344 linenr_T lnum;
1345 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001346 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001347 char_u *line;
1348 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001349 char_u *endp;
Bram Moolenaar2813f382022-06-09 19:54:24 +01001350 hlf_T attr = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001351 int len;
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001352#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001353 int has_syntax = syntax_present(wp);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001354#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001355 int col;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001356 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001357 char_u *buf = NULL;
1358 int buflen = 0;
1359 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001360 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001361 int found_one = FALSE;
1362 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001363
Bram Moolenaar95529562005-08-25 21:21:38 +00001364 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00001365 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001366
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001367 /*
1368 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00001369 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001370 *
1371 * When searching backwards, we continue in the line to find the last
1372 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001373 *
1374 * We concatenate the start of the next line, so that wrapped words work
1375 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1376 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001377 */
Bram Moolenaar95529562005-08-25 21:21:38 +00001378 lnum = wp->w_cursor.lnum;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001379 CLEAR_POS(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001380
1381 while (!got_int)
1382 {
Bram Moolenaar2813f382022-06-09 19:54:24 +01001383 int empty_line;
1384
Bram Moolenaar95529562005-08-25 21:21:38 +00001385 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001386
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001387 len = (int)STRLEN(line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001388 if (buflen < len + MAXWLEN + 2)
1389 {
1390 vim_free(buf);
1391 buflen = len + MAXWLEN + 2;
1392 buf = alloc(buflen);
1393 if (buf == NULL)
1394 break;
1395 }
1396
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001397 // In first line check first word for Capital.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001398 if (lnum == 1)
1399 capcol = 0;
1400
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001401 // For checking first word with a capital skip white space.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001402 if (capcol == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001403 capcol = getwhitecols(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001404 else if (curline && wp == curwin)
1405 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001406 // For spellbadword(): check if first word needs a capital.
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001407 col = getwhitecols(line);
Luuk van Baal2ac64972023-05-25 17:14:42 +01001408 if (check_need_cap(curwin, lnum, col))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001409 capcol = col;
1410
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001411 // Need to get the line again, may have looked at the previous
1412 // one.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001413 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1414 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001415
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001416 // Copy the line into "buf" and append the start of the next line if
Bram Moolenaar2813f382022-06-09 19:54:24 +01001417 // possible. Note: this ml_get_buf() may make "line" invalid, check
1418 // for empty line first.
1419 empty_line = *skipwhite(line) == NUL;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001420 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00001421 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar5dd95a12006-05-13 12:09:24 +00001422 spell_cat_line(buf + STRLEN(buf),
1423 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001424
1425 p = buf + skip;
1426 endp = buf + len;
1427 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001428 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001429 // When searching backward don't search after the cursor. Unless
1430 // we wrapped around the end of the buffer.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001431 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00001432 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001433 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00001434 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001435 break;
1436
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001437 // start of word
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001438 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001439 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001440
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001441 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001442 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001443 // We found a bad word. Check the attribute.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001444 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001445 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001446 // When searching forward only accept a bad word after
1447 // the cursor.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001448 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001449 || lnum != wp->w_cursor.lnum
Bram Moolenaarfe154992022-03-22 20:42:12 +00001450 || (wrapped
1451 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00001452 : p - buf)
Bram Moolenaarfe154992022-03-22 20:42:12 +00001453 > wp->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001454 {
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001455#ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00001456 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001457 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001458 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001459 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00001460 FALSE, &can_spell, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001461 if (!can_spell)
1462 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001463 }
1464 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001465#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001466 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001467
Bram Moolenaar51485f02005-06-04 21:55:20 +00001468 if (can_spell)
1469 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00001470 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001471 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001472 found_pos.col = (int)(p - buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001473 found_pos.coladd = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001474 if (dir == FORWARD)
1475 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001476 // No need to search further.
Bram Moolenaar95529562005-08-25 21:21:38 +00001477 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001478 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001479 if (attrp != NULL)
1480 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001481 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001482 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001483 else if (curline)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001484 // Insert mode completion: put cursor after
1485 // the bad word.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001486 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001487 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001488 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001489 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00001490 else
1491 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001492 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001493 }
1494
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001495 // advance to character after the word
Bram Moolenaar51485f02005-06-04 21:55:20 +00001496 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001497 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001498 }
1499
Bram Moolenaar5195e452005-08-19 20:32:47 +00001500 if (dir == BACKWARD && found_pos.lnum != 0)
1501 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001502 // Use the last match in the line (before the cursor).
Bram Moolenaar95529562005-08-25 21:21:38 +00001503 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001504 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001505 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001506 }
1507
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001508 if (curline)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001509 break; // only check cursor line
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001510
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001511 // If we are back at the starting line and searched it again there
1512 // is no match, give up.
Bram Moolenaard3f78dc2017-02-25 14:21:10 +01001513 if (lnum == wp->w_cursor.lnum && wrapped)
1514 break;
1515
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001516 // Advance to next line.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001517 if (dir == BACKWARD)
1518 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001519 if (lnum > 1)
1520 --lnum;
1521 else if (!p_ws)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001522 break; // at first line and 'nowrapscan'
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001523 else
1524 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001525 // Wrap around to the end of the buffer. May search the
1526 // starting line again and accept the last match.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001527 lnum = wp->w_buffer->b_ml.ml_line_count;
1528 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001529 if (!shortmess(SHM_SEARCH))
1530 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001531 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001532 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001533 }
1534 else
1535 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001536 if (lnum < wp->w_buffer->b_ml.ml_line_count)
1537 ++lnum;
1538 else if (!p_ws)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001539 break; // at first line and 'nowrapscan'
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001540 else
1541 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001542 // Wrap around to the start of the buffer. May search the
1543 // starting line again and accept the first match.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001544 lnum = 1;
1545 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001546 if (!shortmess(SHM_SEARCH))
1547 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001548 }
1549
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001550 // If we are back at the starting line and there is no match then
1551 // give up.
Bram Moolenaard3f78dc2017-02-25 14:21:10 +01001552 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001553 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001554
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001555 // Skip the characters at the start of the next line that were
1556 // included in a match crossing line boundaries.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001557 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001558 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001559 else
1560 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001561
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001562 // Capcol skips over the inserted space.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001563 --capcol;
1564
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001565 // But after empty line check first word in next line
Bram Moolenaar2813f382022-06-09 19:54:24 +01001566 if (empty_line)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001567 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001568 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001569
1570 line_breakcheck();
1571 }
1572
Bram Moolenaar0c405862005-06-22 22:26:26 +00001573 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001574 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001575}
1576
1577/*
1578 * For spell checking: concatenate the start of the following line "line" into
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001579 * "buf", blanking-out special characters. Copy less than "maxlen" bytes.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00001580 * Keep the blanks at the start of the next line, this is used in win_line()
1581 * to skip those bytes if the word was OK.
Bram Moolenaar0c405862005-06-22 22:26:26 +00001582 */
1583 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001584spell_cat_line(char_u *buf, char_u *line, int maxlen)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001585{
1586 char_u *p;
1587 int n;
1588
1589 p = skipwhite(line);
1590 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1591 p = skipwhite(p + 1);
1592
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001593 if (*p == NUL)
1594 return;
1595
1596 // Only worth concatenating if there is something else than spaces to
1597 // concatenate.
1598 n = (int)(p - line) + 1;
1599 if (n < maxlen - 1)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001600 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001601 vim_memset(buf, ' ', n);
1602 vim_strncpy(buf + n, p, maxlen - 1 - n);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001603 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001604}
1605
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001606/*
1607 * Structure used for the cookie argument of do_in_runtimepath().
1608 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001609typedef struct spelload_S
1610{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001611 char_u sl_lang[MAXWLEN + 1]; // language name
1612 slang_T *sl_slang; // resulting slang_T struct
1613 int sl_nobreak; // NOBREAK language found
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001614} spelload_T;
1615
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001616/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001617 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001618 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001619 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001620 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001621spell_load_lang(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001622{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001623 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001624 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001625 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001626 int round;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001627
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001628 // Copy the language name to pass it to spell_load_cb() as a cookie.
1629 // It's truncated when an error is detected.
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001630 STRCPY(sl.sl_lang, lang);
1631 sl.sl_slang = NULL;
1632 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001633
Bram Moolenaaref976322022-09-28 11:48:30 +01001634 // Disallow deleting the current buffer. Autocommands can do weird things
1635 // and cause "lang" to be freed.
1636 ++curbuf->b_locked;
1637
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001638 // We may retry when no spell file is found for the language, an
1639 // autocommand may load it then.
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001640 for (round = 1; round <= 2; ++round)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001641 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001642 /*
1643 * Find the first spell file for "lang" in 'runtimepath' and load it.
1644 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001645 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001646#ifdef VMS
1647 "spell/%s_%s.spl",
1648#else
1649 "spell/%s.%s.spl",
1650#endif
1651 lang, spell_enc());
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001652 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001653
1654 if (r == FAIL && *sl.sl_lang != NUL)
1655 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001656 // Try loading the ASCII version.
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001657 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001658#ifdef VMS
1659 "spell/%s_ascii.spl",
1660#else
1661 "spell/%s.ascii.spl",
1662#endif
1663 lang);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001664 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001665
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001666 if (r == FAIL && *sl.sl_lang != NUL && round == 1
1667 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
1668 curbuf->b_fname, FALSE, curbuf))
1669 continue;
1670 break;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001671 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001672 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001673 }
1674
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001675 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001676 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001677 smsg(
Bram Moolenaar56f78042010-12-08 17:09:32 +01001678#ifdef VMS
1679 _("Warning: Cannot find word list \"%s_%s.spl\" or \"%s_ascii.spl\""),
1680#else
1681 _("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
1682#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00001683 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001684 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001685 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001686 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001687 // At least one file was loaded, now load ALL the additions.
Bram Moolenaarb765d632005-06-07 21:00:02 +00001688 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001689 do_in_runtimepath(fname_enc, DIP_ALL, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001690 }
Bram Moolenaaref976322022-09-28 11:48:30 +01001691
1692 --curbuf->b_locked;
Bram Moolenaarb765d632005-06-07 21:00:02 +00001693}
1694
1695/*
1696 * Return the encoding used for spell checking: Use 'encoding', except that we
1697 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1698 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001699 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001700spell_enc(void)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001701{
1702
Bram Moolenaarb765d632005-06-07 21:00:02 +00001703 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1704 return p_enc;
Bram Moolenaarb765d632005-06-07 21:00:02 +00001705 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001706}
1707
1708/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001709 * Get the name of the .spl file for the internal wordlist into
1710 * "fname[MAXPATHL]".
1711 */
1712 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001713int_wordlist_spl(char_u *fname)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001714{
Bram Moolenaar56f78042010-12-08 17:09:32 +01001715 vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001716 int_wordlist, spell_enc());
1717}
1718
1719/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001720 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001721 * Caller must fill "sl_next".
1722 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001723 slang_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001724slang_alloc(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001725{
1726 slang_T *lp;
1727
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001728 lp = ALLOC_CLEAR_ONE(slang_T);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001729 if (lp != NULL)
1730 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001731 if (lang != NULL)
1732 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001733 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001734 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001735 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001736 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001737 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001738 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001739
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001740 return lp;
1741}
1742
1743/*
1744 * Free the contents of an slang_T and the structure itself.
1745 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001746 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001747slang_free(slang_T *lp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001748{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001749 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001750 vim_free(lp->sl_fname);
1751 slang_clear(lp);
1752 vim_free(lp);
1753}
1754
1755/*
1756 * Clear an slang_T so that the file can be reloaded.
1757 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001758 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001759slang_clear(slang_T *lp)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001760{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001761 garray_T *gap;
1762 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001763 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001764 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001765 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001766
Bram Moolenaard23a8232018-02-10 18:45:26 +01001767 VIM_CLEAR(lp->sl_fbyts);
1768 VIM_CLEAR(lp->sl_kbyts);
1769 VIM_CLEAR(lp->sl_pbyts);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001770
Bram Moolenaard23a8232018-02-10 18:45:26 +01001771 VIM_CLEAR(lp->sl_fidxs);
1772 VIM_CLEAR(lp->sl_kidxs);
1773 VIM_CLEAR(lp->sl_pidxs);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001774
Bram Moolenaar4770d092006-01-12 23:22:24 +00001775 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001776 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001777 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
1778 while (gap->ga_len > 0)
1779 {
1780 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1781 vim_free(ftp->ft_from);
1782 vim_free(ftp->ft_to);
1783 }
1784 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001785 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001786
1787 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001788 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001789 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001790 // "ga_len" is set to 1 without adding an item for latin1
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001791 if (gap->ga_data != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001792 // SOFOFROM and SOFOTO items: free lists of wide characters.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001793 for (i = 0; i < gap->ga_len; ++i)
1794 vim_free(((int **)gap->ga_data)[i]);
1795 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001796 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001797 // SAL items: free salitem_T items
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001798 while (gap->ga_len > 0)
1799 {
1800 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1801 vim_free(smp->sm_lead);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001802 // Don't free sm_oneof and sm_rules, they point into sm_lead.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001803 vim_free(smp->sm_to);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001804 vim_free(smp->sm_lead_w);
1805 vim_free(smp->sm_oneof_w);
1806 vim_free(smp->sm_to_w);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001807 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001808 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001809
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001810 for (i = 0; i < lp->sl_prefixcnt; ++i)
Bram Moolenaar473de612013-06-08 18:19:48 +02001811 vim_regfree(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001812 lp->sl_prefixcnt = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001813 VIM_CLEAR(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001814
Bram Moolenaard23a8232018-02-10 18:45:26 +01001815 VIM_CLEAR(lp->sl_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001816
Bram Moolenaard23a8232018-02-10 18:45:26 +01001817 VIM_CLEAR(lp->sl_midword);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001818
Bram Moolenaar473de612013-06-08 18:19:48 +02001819 vim_regfree(lp->sl_compprog);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001820 lp->sl_compprog = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001821 VIM_CLEAR(lp->sl_comprules);
1822 VIM_CLEAR(lp->sl_compstartflags);
1823 VIM_CLEAR(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001824
Bram Moolenaard23a8232018-02-10 18:45:26 +01001825 VIM_CLEAR(lp->sl_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001826 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001827
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001828 ga_clear_strings(&lp->sl_comppat);
1829
Bram Moolenaar4770d092006-01-12 23:22:24 +00001830 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
1831 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00001832
Bram Moolenaar4770d092006-01-12 23:22:24 +00001833 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001834
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001835 // Clear info from .sug file.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001836 slang_clear_sug(lp);
1837
Bram Moolenaar5195e452005-08-19 20:32:47 +00001838 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001839 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001840 lp->sl_compsylmax = MAXWLEN;
1841 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001842}
1843
1844/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001845 * Clear the info from the .sug file in "lp".
1846 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001847 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001848slang_clear_sug(slang_T *lp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00001849{
Bram Moolenaard23a8232018-02-10 18:45:26 +01001850 VIM_CLEAR(lp->sl_sbyts);
1851 VIM_CLEAR(lp->sl_sidxs);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001852 close_spellbuf(lp->sl_sugbuf);
1853 lp->sl_sugbuf = NULL;
1854 lp->sl_sugloaded = FALSE;
1855 lp->sl_sugtime = 0;
1856}
1857
1858/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001859 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001860 * Invoked through do_in_runtimepath().
1861 */
1862 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001863spell_load_cb(char_u *fname, void *cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001864{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001865 spelload_T *slp = (spelload_T *)cookie;
1866 slang_T *slang;
1867
1868 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001869 if (slang == NULL)
1870 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001871
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001872 // When a previously loaded file has NOBREAK also use it for the
1873 // ".add" files.
1874 if (slp->sl_nobreak && slang->sl_add)
1875 slang->sl_nobreak = TRUE;
1876 else if (slang->sl_nobreak)
1877 slp->sl_nobreak = TRUE;
1878
1879 slp->sl_slang = slang;
Bram Moolenaarb765d632005-06-07 21:00:02 +00001880}
1881
Bram Moolenaar4770d092006-01-12 23:22:24 +00001882
1883/*
1884 * Add a word to the hashtable of common words.
1885 * If it's already there then the counter is increased.
1886 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001887 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001888count_common_word(
1889 slang_T *lp,
1890 char_u *word,
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001891 int len, // word length, -1 for up to NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001892 int count) // 1 to count once, 10 to init
Bram Moolenaar4770d092006-01-12 23:22:24 +00001893{
1894 hash_T hash;
1895 hashitem_T *hi;
1896 wordcount_T *wc;
1897 char_u buf[MAXWLEN];
1898 char_u *p;
1899
1900 if (len == -1)
1901 p = word;
Bram Moolenaar5bcc5a12019-08-06 22:48:02 +02001902 else if (len >= MAXWLEN)
1903 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001904 else
1905 {
1906 vim_strncpy(buf, word, len);
1907 p = buf;
1908 }
1909
1910 hash = hash_hash(p);
1911 hi = hash_lookup(&lp->sl_wordcount, p, hash);
1912 if (HASHITEM_EMPTY(hi))
1913 {
zeertzjq1b438a82023-02-01 13:11:15 +00001914 wc = alloc(offsetof(wordcount_T, wc_word) + STRLEN(p) + 1);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001915 if (wc == NULL)
1916 return;
1917 STRCPY(wc->wc_word, p);
1918 wc->wc_count = count;
1919 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
1920 }
1921 else
1922 {
1923 wc = HI2WC(hi);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001924 if ((wc->wc_count += count) < (unsigned)count) // check for overflow
Bram Moolenaar4770d092006-01-12 23:22:24 +00001925 wc->wc_count = MAXWORDCOUNT;
1926 }
1927}
1928
1929/*
Bram Moolenaar95529562005-08-25 21:21:38 +00001930 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00001931 * Like strchr() but independent of locale.
1932 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001933 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001934byte_in_str(char_u *str, int n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001935{
1936 char_u *p;
1937
1938 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00001939 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001940 return TRUE;
1941 return FALSE;
1942}
1943
Bram Moolenaar5195e452005-08-19 20:32:47 +00001944#define SY_MAXLEN 30
1945typedef struct syl_item_S
1946{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001947 char_u sy_chars[SY_MAXLEN]; // the sequence of chars
Bram Moolenaar5195e452005-08-19 20:32:47 +00001948 int sy_len;
1949} syl_item_T;
1950
1951/*
1952 * Truncate "slang->sl_syllable" at the first slash and put the following items
1953 * in "slang->sl_syl_items".
1954 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001955 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001956init_syl_tab(slang_T *slang)
Bram Moolenaar5195e452005-08-19 20:32:47 +00001957{
1958 char_u *p;
1959 char_u *s;
1960 int l;
1961 syl_item_T *syl;
1962
1963 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
1964 p = vim_strchr(slang->sl_syllable, '/');
1965 while (p != NULL)
1966 {
1967 *p++ = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001968 if (*p == NUL) // trailing slash
Bram Moolenaar5195e452005-08-19 20:32:47 +00001969 break;
1970 s = p;
1971 p = vim_strchr(p, '/');
1972 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001973 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001974 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001975 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001976 if (l >= SY_MAXLEN)
1977 return SP_FORMERROR;
1978 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001979 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001980 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
1981 + slang->sl_syl_items.ga_len++;
1982 vim_strncpy(syl->sy_chars, s, l);
1983 syl->sy_len = l;
1984 }
1985 return OK;
1986}
1987
1988/*
1989 * Count the number of syllables in "word".
1990 * When "word" contains spaces the syllables after the last space are counted.
1991 * Returns zero if syllables are not defines.
1992 */
1993 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001994count_syllables(slang_T *slang, char_u *word)
Bram Moolenaar5195e452005-08-19 20:32:47 +00001995{
1996 int cnt = 0;
1997 int skip = FALSE;
1998 char_u *p;
1999 int len;
2000 int i;
2001 syl_item_T *syl;
2002 int c;
2003
2004 if (slang->sl_syllable == NULL)
2005 return 0;
2006
2007 for (p = word; *p != NUL; p += len)
2008 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002009 // When running into a space reset counter.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002010 if (*p == ' ')
2011 {
2012 len = 1;
2013 cnt = 0;
2014 continue;
2015 }
2016
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002017 // Find longest match of syllable items.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002018 len = 0;
2019 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
2020 {
2021 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
2022 if (syl->sy_len > len
2023 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
2024 len = syl->sy_len;
2025 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002026 if (len != 0) // found a match, count syllable
Bram Moolenaar5195e452005-08-19 20:32:47 +00002027 {
2028 ++cnt;
2029 skip = FALSE;
2030 }
2031 else
2032 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002033 // No recognized syllable item, at least a syllable char then?
Bram Moolenaar5195e452005-08-19 20:32:47 +00002034 c = mb_ptr2char(p);
2035 len = (*mb_ptr2len)(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002036 if (vim_strchr(slang->sl_syllable, c) == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002037 skip = FALSE; // No, search for next syllable
Bram Moolenaar5195e452005-08-19 20:32:47 +00002038 else if (!skip)
2039 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002040 ++cnt; // Yes, count it
2041 skip = TRUE; // don't count following syllable chars
Bram Moolenaar5195e452005-08-19 20:32:47 +00002042 }
2043 }
2044 }
2045 return cnt;
2046}
2047
2048/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02002049 * Parse 'spelllang' and set w_s->b_langp accordingly.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002050 * Returns NULL if it's OK, an untranslated error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002051 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002052 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002053parse_spelllang(win_T *wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002054{
2055 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002056 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002057 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00002058 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002059 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002060 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002061 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002062 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002063 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002064 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002065 int len;
2066 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002067 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002068 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002069 char_u *use_region = NULL;
2070 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002071 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002072 int i, j;
2073 langp_T *lp, *lp2;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002074 static int recursive = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002075 char *ret_msg = NULL;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002076 char_u *spl_copy;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002077 bufref_T bufref;
2078
2079 set_bufref(&bufref, wp->w_buffer);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002080
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002081 // We don't want to do this recursively. May happen when a language is
2082 // not available and the SpellFileMissing autocommand opens a new buffer
2083 // in which 'spell' is set.
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002084 if (recursive)
2085 return NULL;
2086 recursive = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002087
2088 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002089 clear_midword(wp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002090
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002091 // Make a copy of 'spelllang', the SpellFileMissing autocommands may change
2092 // it under our fingers.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002093 spl_copy = vim_strsave(wp->w_s->b_p_spl);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002094 if (spl_copy == NULL)
2095 goto theend;
2096
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002097 wp->w_s->b_cjk = 0;
2098
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002099 // Loop over comma separated language names.
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002100 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002101 {
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002102 // Get one language name.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002103 copy_option_part(&splp, lang, MAXWLEN, ",");
Bram Moolenaar5482f332005-04-17 20:18:43 +00002104 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002105 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002106
Bram Moolenaarf154f3a2020-06-08 18:54:49 +02002107 if (!valid_spelllang(lang))
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002108 continue;
2109
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002110 if (STRCMP(lang, "cjk") == 0)
2111 {
2112 wp->w_s->b_cjk = 1;
2113 continue;
2114 }
2115
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002116 // If the name ends in ".spl" use it as the name of the spell file.
2117 // If there is a region name let "region" point to it and remove it
2118 // from the name.
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002119 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2120 {
2121 filename = TRUE;
2122
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002123 // Locate a region and remove it from the file name.
Bram Moolenaarb6356332005-07-18 21:40:44 +00002124 p = vim_strchr(gettail(lang), '_');
2125 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
2126 && !ASCII_ISALPHA(p[3]))
2127 {
2128 vim_strncpy(region_cp, p + 1, 2);
2129 mch_memmove(p, p + 3, len - (p - lang) - 2);
Bram Moolenaarb6356332005-07-18 21:40:44 +00002130 region = region_cp;
2131 }
2132 else
2133 dont_use_region = TRUE;
2134
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002135 // Check if we loaded this language before.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002136 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar99499b12019-05-23 21:35:48 +02002137 if (fullpathcmp(lang, slang->sl_fname, FALSE, TRUE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002138 break;
2139 }
2140 else
2141 {
2142 filename = FALSE;
2143 if (len > 3 && lang[len - 3] == '_')
2144 {
2145 region = lang + len - 2;
2146 len -= 3;
2147 lang[len] = NUL;
2148 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002149 else
2150 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002151
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002152 // Check if we loaded this language before.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002153 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002154 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002155 break;
2156 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002157
Bram Moolenaarb6356332005-07-18 21:40:44 +00002158 if (region != NULL)
2159 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002160 // If the region differs from what was used before then don't
2161 // use it for 'spellfile'.
Bram Moolenaarb6356332005-07-18 21:40:44 +00002162 if (use_region != NULL && STRCMP(region, use_region) != 0)
2163 dont_use_region = TRUE;
2164 use_region = region;
2165 }
2166
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002167 // If not found try loading the language now.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002168 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002169 {
2170 if (filename)
2171 (void)spell_load_file(lang, lang, NULL, FALSE);
2172 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002173 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002174 spell_load_lang(lang);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002175 // SpellFileMissing autocommands may do anything, including
Bram Moolenaarc3d27ad2022-11-14 20:52:14 +00002176 // destroying the buffer we are using or closing the window.
2177 if (!bufref_valid(&bufref) || !win_valid_any_tab(wp))
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002178 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002179 ret_msg = N_(e_spellfilemising_autocommand_deleted_buffer);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002180 goto theend;
2181 }
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002182 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002183 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002184
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002185 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002186 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002187 */
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002188 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar99499b12019-05-23 21:35:48 +02002189 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE, TRUE)
2190 == FPC_SAME
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002191 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002192 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002193 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002194 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002195 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002196 // find region in sl_regions
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002197 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002198 if (c == REGION_ALL)
2199 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002200 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002201 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002202 if (*slang->sl_regions != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002203 // This addition file is for other regions.
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002204 region_mask = 0;
2205 }
2206 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002207 // This is probably an error. Give a warning and
2208 // accept the words anyway.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002209 smsg(_("Warning: region %s not supported"),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002210 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002211 }
2212 else
2213 region_mask = 1 << c;
2214 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002215
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002216 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002217 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002218 if (ga_grow(&ga, 1) == FAIL)
2219 {
2220 ga_clear(&ga);
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002221 ret_msg = e_out_of_memory;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002222 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002223 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002224 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002225 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2226 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002227 use_midword(slang, wp);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002228 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002229 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002230 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002231 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002232 }
2233
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002234 // round 0: load int_wordlist, if possible.
2235 // round 1: load first name in 'spellfile'.
2236 // round 2: load second name in 'spellfile.
2237 // etc.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002238 spf = curwin->w_s->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002239 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002240 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002241 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002242 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002243 // Internal wordlist, if there is one.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002244 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002245 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002246 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002247 }
2248 else
2249 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002250 // One entry in 'spellfile'.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002251 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
2252 STRCAT(spf_name, ".spl");
2253
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002254 // If it was already found above then skip it.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002255 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002256 {
2257 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
Bram Moolenaar99499b12019-05-23 21:35:48 +02002258 if (p != NULL && fullpathcmp(spf_name, p, FALSE, TRUE)
2259 == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002260 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002261 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002262 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002263 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002264 }
2265
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002266 // Check if it was loaded already.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002267 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar99499b12019-05-23 21:35:48 +02002268 if (fullpathcmp(spf_name, slang->sl_fname, FALSE, TRUE)
2269 == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002270 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002271 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002272 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002273 // Not loaded, try loading it now. The language name includes the
2274 // region name, the region is ignored otherwise. for int_wordlist
2275 // use an arbitrary name.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002276 if (round == 0)
2277 STRCPY(lang, "internal wordlist");
2278 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00002279 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002280 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002281 p = vim_strchr(lang, '.');
2282 if (p != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002283 *p = NUL; // truncate at ".encoding.add"
Bram Moolenaar7887d882005-07-01 22:33:52 +00002284 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002285 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002286
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002287 // If one of the languages has NOBREAK we assume the addition
2288 // files also have this.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002289 if (slang != NULL && nobreak)
2290 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002291 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002292 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002293 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002294 region_mask = REGION_ALL;
2295 if (use_region != NULL && !dont_use_region)
2296 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002297 // find region in sl_regions
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002298 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002299 if (c != REGION_ALL)
2300 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002301 else if (*slang->sl_regions != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002302 // This spell file is for other regions.
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002303 region_mask = 0;
2304 }
2305
2306 if (region_mask != 0)
2307 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002308 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
2309 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
2310 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002311 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2312 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002313 use_midword(slang, wp);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002314 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002315 }
2316 }
2317
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002318 // Everything is fine, store the new b_langp value.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002319 ga_clear(&wp->w_s->b_langp);
2320 wp->w_s->b_langp = ga;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002321
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002322 // For each language figure out what language to use for sound folding and
2323 // REP items. If the language doesn't support it itself use another one
2324 // with the same name. E.g. for "en-math" use "en".
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002325 for (i = 0; i < ga.ga_len; ++i)
2326 {
2327 lp = LANGP_ENTRY(ga, i);
2328
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002329 // sound folding
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002330 if (lp->lp_slang->sl_sal.ga_len > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002331 // language does sound folding itself
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002332 lp->lp_sallang = lp->lp_slang;
2333 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002334 // find first similar language that does sound folding
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002335 for (j = 0; j < ga.ga_len; ++j)
2336 {
2337 lp2 = LANGP_ENTRY(ga, j);
2338 if (lp2->lp_slang->sl_sal.ga_len > 0
2339 && STRNCMP(lp->lp_slang->sl_name,
2340 lp2->lp_slang->sl_name, 2) == 0)
2341 {
2342 lp->lp_sallang = lp2->lp_slang;
2343 break;
2344 }
2345 }
2346
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002347 // REP items
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002348 if (lp->lp_slang->sl_rep.ga_len > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002349 // language has REP items itself
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002350 lp->lp_replang = lp->lp_slang;
2351 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002352 // find first similar language that has REP items
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002353 for (j = 0; j < ga.ga_len; ++j)
2354 {
2355 lp2 = LANGP_ENTRY(ga, j);
2356 if (lp2->lp_slang->sl_rep.ga_len > 0
2357 && STRNCMP(lp->lp_slang->sl_name,
2358 lp2->lp_slang->sl_name, 2) == 0)
2359 {
2360 lp->lp_replang = lp2->lp_slang;
2361 break;
2362 }
2363 }
2364 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002365 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002366
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002367theend:
2368 vim_free(spl_copy);
2369 recursive = FALSE;
2370 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002371}
2372
2373/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002374 * Clear the midword characters for buffer "buf".
2375 */
2376 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002377clear_midword(win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002378{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002379 CLEAR_FIELD(wp->w_s->b_spell_ismw);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002380 VIM_CLEAR(wp->w_s->b_spell_ismw_mb);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002381}
2382
2383/*
2384 * Use the "sl_midword" field of language "lp" for buffer "buf".
2385 * They add up to any currently used midword characters.
2386 */
2387 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002388use_midword(slang_T *lp, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002389{
2390 char_u *p;
2391
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002392 if (lp->sl_midword == NULL) // there aren't any
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002393 return;
2394
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002395 for (p = lp->sl_midword; *p != NUL; )
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002396 if (has_mbyte)
2397 {
2398 int c, l, n;
2399 char_u *bp;
2400
2401 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002402 l = (*mb_ptr2len)(p);
2403 if (c < 256 && l <= 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002404 wp->w_s->b_spell_ismw[c] = TRUE;
2405 else if (wp->w_s->b_spell_ismw_mb == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002406 // First multi-byte char in "b_spell_ismw_mb".
Bram Moolenaar860cae12010-06-05 23:22:07 +02002407 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002408 else
2409 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002410 // Append multi-byte chars to "b_spell_ismw_mb".
Bram Moolenaar860cae12010-06-05 23:22:07 +02002411 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
2412 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002413 if (bp != NULL)
2414 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002415 vim_free(wp->w_s->b_spell_ismw_mb);
2416 wp->w_s->b_spell_ismw_mb = bp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002417 vim_strncpy(bp + n, p, l);
2418 }
2419 }
2420 p += l;
2421 }
2422 else
Bram Moolenaar860cae12010-06-05 23:22:07 +02002423 wp->w_s->b_spell_ismw[*p++] = TRUE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002424}
2425
2426/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002427 * Find the region "region[2]" in "rp" (points to "sl_regions").
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01002428 * Each region is simply stored as the two characters of its name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002429 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002430 */
2431 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002432find_region(char_u *rp, char_u *region)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002433{
2434 int i;
2435
2436 for (i = 0; ; i += 2)
2437 {
2438 if (rp[i] == NUL)
2439 return REGION_ALL;
2440 if (rp[i] == region[0] && rp[i + 1] == region[1])
2441 break;
2442 }
2443 return i / 2;
2444}
2445
2446/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002447 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002448 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002449 * Word WF_ONECAP
2450 * W WORD WF_ALLCAP
2451 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002452 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002453 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002454captype(
2455 char_u *word,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002456 char_u *end) // When NULL use up to NUL byte.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002457{
2458 char_u *p;
2459 int c;
2460 int firstcap;
2461 int allcap;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002462 int past_second = FALSE; // past second word char
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002463
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002464 // find first letter
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002465 for (p = word; !spell_iswordp_nmw(p, curwin); MB_PTR_ADV(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002466 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002467 return 0; // only non-word characters, illegal word
Bram Moolenaarb765d632005-06-07 21:00:02 +00002468 if (has_mbyte)
2469 c = mb_ptr2char_adv(&p);
2470 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00002471 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002472 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002473
2474 /*
2475 * Need to check all letters to find a word with mixed upper/lower.
2476 * But a word with an upper char only at start is a ONECAP.
2477 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002478 for ( ; end == NULL ? *p != NUL : p < end; MB_PTR_ADV(p))
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002479 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002480 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002481 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002482 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002483 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002484 // UUl -> KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002485 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002486 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002487 allcap = FALSE;
2488 }
2489 else if (!allcap)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002490 // UlU -> KEEPCAP
Bram Moolenaar51485f02005-06-04 21:55:20 +00002491 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002492 past_second = TRUE;
2493 }
2494
2495 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002496 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002497 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002498 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002499 return 0;
2500}
2501
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002502/*
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002503 * Delete the internal wordlist and its .spl file.
2504 */
2505 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002506spell_delete_wordlist(void)
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002507{
2508 char_u fname[MAXPATHL];
2509
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002510 if (int_wordlist == NULL)
2511 return;
2512
2513 mch_remove(int_wordlist);
2514 int_wordlist_spl(fname);
2515 mch_remove(fname);
2516 VIM_CLEAR(int_wordlist);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002517}
2518
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002519/*
2520 * Free all languages.
2521 */
2522 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002523spell_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002524{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002525 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002526 buf_T *buf;
2527
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002528 // Go through all buffers and handle 'spelllang'. <VN>
Bram Moolenaar29323592016-07-24 22:04:11 +02002529 FOR_ALL_BUFFERS(buf)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002530 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002531
2532 while (first_lang != NULL)
2533 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002534 slang = first_lang;
2535 first_lang = slang->sl_next;
2536 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002537 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002538
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002539 spell_delete_wordlist();
Bram Moolenaar7887d882005-07-01 22:33:52 +00002540
Bram Moolenaard23a8232018-02-10 18:45:26 +01002541 VIM_CLEAR(repl_to);
2542 VIM_CLEAR(repl_from);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002543}
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002544
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002545/*
2546 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002547 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002548 */
2549 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002550spell_reload(void)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002551{
Bram Moolenaar3982c542005-06-08 21:56:31 +00002552 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002553
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002554 // Initialize the table for spell_iswordp().
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002555 init_spell_chartab();
2556
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002557 // Unload all allocated memory.
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002558 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002559
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002560 // Go through all buffers and handle 'spelllang'.
Bram Moolenaar29323592016-07-24 22:04:11 +02002561 FOR_ALL_WINDOWS(wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002562 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002563 // Only load the wordlists when 'spelllang' is set and there is a
2564 // window for this buffer in which 'spell' is set.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002565 if (*wp->w_s->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002566 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002567 if (wp->w_p_spell)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002568 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002569 (void)parse_spelllang(wp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00002570 break;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002571 }
2572 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002573 }
2574}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002575
Bram Moolenaarb765d632005-06-07 21:00:02 +00002576/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002577 * Open a spell buffer. This is a nameless buffer that is not in the buffer
2578 * list and only contains text lines. Can use a swapfile to reduce memory
2579 * use.
2580 * Most other fields are invalid! Esp. watch out for string options being
2581 * NULL and there is no undo info.
2582 * Returns NULL when out of memory.
2583 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002584 buf_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002585open_spellbuf(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002586{
2587 buf_T *buf;
2588
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002589 buf = ALLOC_CLEAR_ONE(buf_T);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002590 if (buf == NULL)
2591 return NULL;
2592
2593 buf->b_spell = TRUE;
2594 buf->b_p_swf = TRUE; // may create a swap file
Bram Moolenaar706d2de2013-07-17 17:35:13 +02002595#ifdef FEAT_CRYPT
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002596 buf->b_p_key = empty_option;
Bram Moolenaar706d2de2013-07-17 17:35:13 +02002597#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002598 ml_open(buf);
2599 ml_open_file(buf); // create swap file now
Bram Moolenaar4770d092006-01-12 23:22:24 +00002600 return buf;
2601}
2602
2603/*
2604 * Close the buffer used for spell info.
2605 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002606 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002607close_spellbuf(buf_T *buf)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002608{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002609 if (buf == NULL)
2610 return;
2611
2612 ml_close(buf, TRUE);
2613 vim_free(buf);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002614}
2615
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002616/*
2617 * Init the chartab used for spelling for ASCII.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002618 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002619 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002620clear_spell_chartab(spelltab_T *sp)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002621{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002622 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002623
Bram Moolenaara80faa82020-04-12 19:37:17 +02002624 // Init everything to FALSE (zero).
2625 CLEAR_FIELD(sp->st_isw);
2626 CLEAR_FIELD(sp->st_isu);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002627 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002628 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002629 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002630 sp->st_upper[i] = i;
2631 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002632
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002633 // We include digits. A word shouldn't start with a digit, but handling
2634 // that is done separately.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002635 for (i = '0'; i <= '9'; ++i)
2636 sp->st_isw[i] = TRUE;
2637 for (i = 'A'; i <= 'Z'; ++i)
2638 {
2639 sp->st_isw[i] = TRUE;
2640 sp->st_isu[i] = TRUE;
2641 sp->st_fold[i] = i + 0x20;
2642 }
2643 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002644 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002645 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002646 sp->st_upper[i] = i - 0x20;
2647 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002648}
2649
2650/*
2651 * Init the chartab used for spelling. Only depends on 'encoding'.
2652 * Called once while starting up and when 'encoding' changes.
2653 * The default is to use isalpha(), but the spell file should define the word
2654 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002655 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002656 */
2657 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002658init_spell_chartab(void)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002659{
2660 int i;
2661
2662 did_set_spelltab = FALSE;
2663 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002664 if (enc_dbcs)
2665 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002666 // DBCS: assume double-wide characters are word characters.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002667 for (i = 128; i <= 255; ++i)
2668 if (MB_BYTE2LEN(i) == 2)
2669 spelltab.st_isw[i] = TRUE;
2670 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002671 else if (enc_utf8)
2672 {
2673 for (i = 128; i < 256; ++i)
2674 {
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02002675 int f = utf_fold(i);
2676 int u = utf_toupper(i);
2677
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002678 spelltab.st_isu[i] = utf_isupper(i);
2679 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002680 // The folded/upper-cased value is different between latin1 and
2681 // utf8 for 0xb5, causing E763 for no good reason. Use the latin1
2682 // value for utf-8 to avoid this.
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02002683 spelltab.st_fold[i] = (f < 256) ? f : i;
2684 spelltab.st_upper[i] = (u < 256) ? u : i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002685 }
2686 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002687 else
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002688 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002689 // Rough guess: use locale-dependent library functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002690 for (i = 128; i < 256; ++i)
2691 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002692 if (MB_ISUPPER(i))
2693 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002694 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002695 spelltab.st_isu[i] = TRUE;
2696 spelltab.st_fold[i] = MB_TOLOWER(i);
2697 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002698 else if (MB_ISLOWER(i))
2699 {
2700 spelltab.st_isw[i] = TRUE;
2701 spelltab.st_upper[i] = MB_TOUPPER(i);
2702 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002703 }
2704 }
2705}
2706
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002707
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002708/*
Bram Moolenaarea408852005-06-25 22:49:46 +00002709 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002710 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00002711 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002712 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00002713 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02002714 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002715spell_iswordp(
2716 char_u *p,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002717 win_T *wp) // buffer used
Bram Moolenaarea408852005-06-25 22:49:46 +00002718{
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002719 char_u *s;
2720 int l;
2721 int c;
2722
2723 if (has_mbyte)
2724 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002725 l = mb_ptr2len(p);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002726 s = p;
2727 if (l == 1)
2728 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002729 // be quick for ASCII
Bram Moolenaar860cae12010-06-05 23:22:07 +02002730 if (wp->w_s->b_spell_ismw[*p])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002731 s = p + 1; // skip a mid-word character
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002732 }
2733 else
2734 {
2735 c = mb_ptr2char(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002736 if (c < 256 ? wp->w_s->b_spell_ismw[c]
2737 : (wp->w_s->b_spell_ismw_mb != NULL
2738 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002739 s = p + l;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002740 }
2741
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002742 c = mb_ptr2char(s);
2743 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002744 return spell_mb_isword_class(mb_get_class(s), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002745 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002746 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002747
Bram Moolenaar860cae12010-06-05 23:22:07 +02002748 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002749}
2750
2751/*
2752 * Return TRUE if "p" points to a word character.
2753 * Unlike spell_iswordp() this doesn't check for "midword" characters.
2754 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02002755 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002756spell_iswordp_nmw(char_u *p, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002757{
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002758 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002759
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002760 if (has_mbyte)
2761 {
2762 c = mb_ptr2char(p);
2763 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002764 return spell_mb_isword_class(mb_get_class(p), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002765 return spelltab.st_isw[c];
2766 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002767 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00002768}
2769
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002770/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002771 * Return TRUE if word class indicates a word character.
2772 * Only for characters above 255.
2773 * Unicode subscript and superscript are not considered word characters.
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002774 * See also dbcs_class() and utf_class() in mbyte.c.
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002775 */
2776 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002777spell_mb_isword_class(int cl, win_T *wp)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002778{
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002779 if (wp->w_s->b_cjk)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002780 // East Asian characters are not considered word characters.
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002781 return cl == 2 || cl == 0x2800;
Bram Moolenaar06e63772019-07-19 23:04:34 +02002782 return cl >= 2 && cl != 0x2070 && cl != 0x2080 && cl != 3;
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002783}
2784
2785/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002786 * Return TRUE if "p" points to a word character.
2787 * Wide version of spell_iswordp().
2788 */
2789 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002790spell_iswordp_w(int *p, win_T *wp)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002791{
2792 int *s;
2793
Bram Moolenaar860cae12010-06-05 23:22:07 +02002794 if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
2795 : (wp->w_s->b_spell_ismw_mb != NULL
2796 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002797 s = p + 1;
2798 else
2799 s = p;
2800
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002801 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002802 {
2803 if (enc_utf8)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002804 return spell_mb_isword_class(utf_class(*s), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002805 if (enc_dbcs)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002806 return spell_mb_isword_class(
2807 dbcs_class((unsigned)*s >> 8, *s & 0xff), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002808 return 0;
2809 }
2810 return spelltab.st_isw[*s];
2811}
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002812
Bram Moolenaarea408852005-06-25 22:49:46 +00002813/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002814 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
2815 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002816 * When using a multi-byte 'encoding' the length may change!
2817 * Returns FAIL when something wrong.
2818 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002819 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002820spell_casefold(
Bram Moolenaar4f135272021-06-11 19:07:40 +02002821 win_T *wp,
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002822 char_u *str,
2823 int len,
2824 char_u *buf,
2825 int buflen)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002826{
2827 int i;
2828
2829 if (len >= buflen)
2830 {
2831 buf[0] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002832 return FAIL; // result will not fit
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002833 }
2834
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002835 if (has_mbyte)
2836 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002837 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002838 char_u *p;
2839 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002840
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002841 // Fold one character at a time.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002842 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002843 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002844 if (outi + MB_MAXBYTES > buflen)
2845 {
2846 buf[outi] = NUL;
2847 return FAIL;
2848 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002849 c = mb_cptr2char_adv(&p);
Bram Moolenaar4f135272021-06-11 19:07:40 +02002850
2851 // Exception: greek capital sigma 0x03A3 folds to 0x03C3, except
2852 // when it is the last character in a word, then it folds to
2853 // 0x03C2.
2854 if (c == 0x03a3 || c == 0x03c2)
2855 {
2856 if (p == str + len || !spell_iswordp(p, wp))
2857 c = 0x03c2;
2858 else
2859 c = 0x03c3;
2860 }
2861 else
2862 c = SPELL_TOFOLD(c);
2863
2864 outi += mb_char2bytes(c, buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002865 }
2866 buf[outi] = NUL;
2867 }
2868 else
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002869 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002870 // Be quick for non-multibyte encodings.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002871 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002872 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002873 buf[i] = NUL;
2874 }
2875
2876 return OK;
2877}
2878
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002879/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002880 * Check if the word at line "lnum" column "col" is required to start with a
Luuk van Baal2ac64972023-05-25 17:14:42 +01002881 * capital. This uses 'spellcapcheck' of the buffer in window "wp".
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002882 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02002883 int
Luuk van Baal2ac64972023-05-25 17:14:42 +01002884check_need_cap(win_T *wp, linenr_T lnum, colnr_T col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002885{
Luuk van Baal2ac64972023-05-25 17:14:42 +01002886 if (wp->w_s->b_cap_prog == NULL)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002887 return FALSE;
2888
Luuk van Baal2ac64972023-05-25 17:14:42 +01002889 int need_cap = FALSE;
2890 char_u *line = col ? ml_get_buf(wp->w_buffer, lnum, FALSE) : NULL;
2891 char_u *line_copy = NULL;
2892 colnr_T endcol = 0;
2893
2894 if (col == 0 || getwhitecols(line) >= col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002895 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002896 // At start of line, check if previous line is empty or sentence
2897 // ends there.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002898 if (lnum == 1)
2899 need_cap = TRUE;
2900 else
2901 {
Luuk van Baal2ac64972023-05-25 17:14:42 +01002902 line = ml_get_buf(wp->w_buffer, lnum - 1, FALSE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002903 if (*skipwhite(line) == NUL)
2904 need_cap = TRUE;
2905 else
2906 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002907 // Append a space in place of the line break.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002908 line_copy = concat_str(line, (char_u *)" ");
Luuk van Baal2ac64972023-05-25 17:14:42 +01002909 if (line_copy == NULL)
2910 return FALSE;
2911
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002912 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002913 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002914 }
2915 }
2916 }
2917 else
2918 endcol = col;
2919
2920 if (endcol > 0)
2921 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002922 // Check if sentence ends before the bad word.
Luuk van Baal2ac64972023-05-25 17:14:42 +01002923 regmatch_T regmatch;
2924 regmatch.regprog = wp->w_s->b_cap_prog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002925 regmatch.rm_ic = FALSE;
Luuk van Baal2ac64972023-05-25 17:14:42 +01002926 char_u *p = line + endcol;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002927 for (;;)
2928 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002929 MB_PTR_BACK(line, p);
Luuk van Baal2ac64972023-05-25 17:14:42 +01002930 if (p == line || spell_iswordp_nmw(p, wp))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002931 break;
2932 if (vim_regexec(&regmatch, p, 0)
2933 && regmatch.endp[0] == line + endcol)
2934 {
2935 need_cap = TRUE;
2936 break;
2937 }
2938 }
Luuk van Baal2ac64972023-05-25 17:14:42 +01002939 wp->w_s->b_cap_prog = regmatch.regprog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002940 }
2941
2942 vim_free(line_copy);
2943
2944 return need_cap;
2945}
2946
2947
2948/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002949 * ":spellrepall"
2950 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002951 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002952ex_spellrepall(exarg_T *eap UNUSED)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002953{
2954 pos_T pos = curwin->w_cursor;
2955 char_u *frompat;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002956 char_u *line;
2957 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002958 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002959 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002960
2961 if (repl_from == NULL || repl_to == NULL)
2962 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002963 emsg(_(e_no_previous_spell_replacement));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002964 return;
2965 }
zeertzjq59f70382023-06-06 15:59:59 +01002966 size_t repl_from_len = STRLEN(repl_from);
2967 size_t repl_to_len = STRLEN(repl_to);
2968 int addlen = (int)(repl_to_len - repl_from_len);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002969
zeertzjq59f70382023-06-06 15:59:59 +01002970 frompat = alloc(repl_from_len + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002971 if (frompat == NULL)
2972 return;
2973 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
2974 p_ws = FALSE;
2975
Bram Moolenaar5195e452005-08-19 20:32:47 +00002976 sub_nsubs = 0;
2977 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002978 curwin->w_cursor.lnum = 0;
2979 while (!got_int)
2980 {
Bram Moolenaarc036e872020-02-21 21:30:52 +01002981 if (do_search(NULL, '/', '/', frompat, 1L, SEARCH_KEEP, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002982 || u_save_cursor() == FAIL)
2983 break;
2984
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002985 // Only replace when the right word isn't there yet. This happens
2986 // when changing "etc" to "etc.".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002987 line = ml_get_curline();
2988 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
zeertzjq59f70382023-06-06 15:59:59 +01002989 repl_to, repl_to_len) != 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002990 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002991 p = alloc(STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002992 if (p == NULL)
2993 break;
2994 mch_memmove(p, line, curwin->w_cursor.col);
2995 STRCPY(p + curwin->w_cursor.col, repl_to);
zeertzjq59f70382023-06-06 15:59:59 +01002996 STRCAT(p, line + curwin->w_cursor.col + repl_from_len);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002997 ml_replace(curwin->w_cursor.lnum, p, FALSE);
2998 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Martin Tournoijba43e762022-10-13 22:12:15 +01002999#if defined(FEAT_PROP_POPUP)
LemonBoyb7a70122022-05-13 12:41:50 +01003000 if (curbuf->b_has_textprop && addlen != 0)
3001 adjust_prop_columns(curwin->w_cursor.lnum,
3002 curwin->w_cursor.col, addlen, APC_SUBSTITUTE);
Martin Tournoijba43e762022-10-13 22:12:15 +01003003#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00003004
3005 if (curwin->w_cursor.lnum != prev_lnum)
3006 {
3007 ++sub_nlines;
3008 prev_lnum = curwin->w_cursor.lnum;
3009 }
3010 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003011 }
zeertzjq59f70382023-06-06 15:59:59 +01003012 curwin->w_cursor.col += (colnr_T)repl_to_len;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003013 }
3014
3015 p_ws = save_ws;
3016 curwin->w_cursor = pos;
3017 vim_free(frompat);
3018
Bram Moolenaar5195e452005-08-19 20:32:47 +00003019 if (sub_nsubs == 0)
Bram Moolenaar677658a2022-01-05 16:09:06 +00003020 semsg(_(e_not_found_str), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003021 else
3022 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003023}
3024
3025/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003026 * Make a copy of "word", with the first letter upper or lower cased, to
3027 * "wcopy[MAXWLEN]". "word" must not be empty.
3028 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003029 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003030 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003031onecap_copy(
3032 char_u *word,
3033 char_u *wcopy,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003034 int upper) // TRUE: first letter made upper case
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003035{
3036 char_u *p;
3037 int c;
3038 int l;
3039
3040 p = word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003041 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003042 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003043 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003044 c = *p++;
3045 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003046 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003047 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003048 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003049 if (has_mbyte)
3050 l = mb_char2bytes(c, wcopy);
3051 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003052 {
3053 l = 1;
3054 wcopy[0] = c;
3055 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003056 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003057}
3058
3059/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003060 * Make a copy of "word" with all the letters upper cased into
3061 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003062 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02003063 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003064allcap_copy(char_u *word, char_u *wcopy)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003065{
3066 char_u *s;
3067 char_u *d;
3068 int c;
3069
3070 d = wcopy;
3071 for (s = word; *s != NUL; )
3072 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003073 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003074 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003075 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003076 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +00003077
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003078 // We only change 0xdf to SS when we are certain latin1 is used. It
3079 // would cause weird errors in other 8-bit encodings.
Bram Moolenaar78622822005-08-23 21:00:13 +00003080 if (enc_latin1like && c == 0xdf)
3081 {
3082 c = 'S';
3083 if (d - wcopy >= MAXWLEN - 1)
3084 break;
3085 *d++ = c;
3086 }
3087 else
Bram Moolenaar78622822005-08-23 21:00:13 +00003088 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003089
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003090 if (has_mbyte)
3091 {
3092 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
3093 break;
3094 d += mb_char2bytes(c, d);
3095 }
3096 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003097 {
3098 if (d - wcopy >= MAXWLEN - 1)
3099 break;
3100 *d++ = c;
3101 }
3102 }
3103 *d = NUL;
3104}
3105
3106/*
Bram Moolenaar53805d12005-08-01 07:08:33 +00003107 * Case-folding may change the number of bytes: Count nr of chars in
3108 * fword[flen] and return the byte length of that many chars in "word".
3109 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02003110 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003111nofold_len(char_u *fword, int flen, char_u *word)
Bram Moolenaar53805d12005-08-01 07:08:33 +00003112{
3113 char_u *p;
3114 int i = 0;
3115
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003116 for (p = fword; p < fword + flen; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00003117 ++i;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003118 for (p = word; i > 0; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00003119 --i;
3120 return (int)(p - word);
3121}
Bram Moolenaar53805d12005-08-01 07:08:33 +00003122
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003123/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003124 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003125 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02003126 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003127make_case_word(char_u *fword, char_u *cword, int flags)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003128{
3129 if (flags & WF_ALLCAP)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003130 // Make it all upper-case
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003131 allcap_copy(fword, cword);
3132 else if (flags & WF_ONECAP)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003133 // Make the first letter upper-case
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003134 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003135 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003136 // Use goodword as-is.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003137 STRCPY(cword, fword);
3138}
3139
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003140#if defined(FEAT_EVAL) || defined(PROTO)
3141/*
3142 * Soundfold a string, for soundfold().
3143 * Result is in allocated memory, NULL for an error.
3144 */
3145 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003146eval_soundfold(char_u *word)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003147{
3148 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003149 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003150 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003151
Bram Moolenaar860cae12010-06-05 23:22:07 +02003152 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003153 // Use the sound-folding of the first language that supports it.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003154 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003155 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003156 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003157 if (lp->lp_slang->sl_sal.ga_len > 0)
3158 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003159 // soundfold the word
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003160 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003161 return vim_strsave(sound);
3162 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003163 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003164
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003165 // No language with sound folding, return word as-is.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003166 return vim_strsave(word);
3167}
3168#endif
3169
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003170/*
3171 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +00003172 *
3173 * There are many ways to turn a word into a sound-a-like representation. The
3174 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
3175 * swedish name matching - survey and test of different algorithms" by Klas
3176 * Erikson.
3177 *
3178 * We support two methods:
3179 * 1. SOFOFROM/SOFOTO do a simple character mapping.
3180 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003181 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003182 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003183spell_soundfold(
3184 slang_T *slang,
3185 char_u *inword,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003186 int folded, // "inword" is already case-folded
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003187 char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003188{
3189 char_u fword[MAXWLEN];
3190 char_u *word;
3191
3192 if (slang->sl_sofo)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003193 // SOFOFROM and SOFOTO used
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003194 spell_soundfold_sofo(slang, inword, res);
3195 else
3196 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003197 // SAL items used. Requires the word to be case-folded.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003198 if (folded)
3199 word = inword;
3200 else
3201 {
Bram Moolenaar4f135272021-06-11 19:07:40 +02003202 (void)spell_casefold(curwin,
3203 inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003204 word = fword;
3205 }
3206
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003207 if (has_mbyte)
3208 spell_soundfold_wsal(slang, word, res);
3209 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003210 spell_soundfold_sal(slang, word, res);
3211 }
3212}
3213
3214/*
3215 * Perform sound folding of "inword" into "res" according to SOFOFROM and
3216 * SOFOTO lines.
3217 */
3218 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003219spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003220{
3221 char_u *s;
3222 int ri = 0;
3223 int c;
3224
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003225 if (has_mbyte)
3226 {
3227 int prevc = 0;
3228 int *ip;
3229
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003230 // The sl_sal_first[] table contains the translation for chars up to
3231 // 255, sl_sal the rest.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003232 for (s = inword; *s != NUL; )
3233 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003234 c = mb_cptr2char_adv(&s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003235 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003236 c = ' ';
3237 else if (c < 256)
3238 c = slang->sl_sal_first[c];
3239 else
3240 {
3241 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003242 if (ip == NULL) // empty list, can't match
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003243 c = NUL;
3244 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003245 for (;;) // find "c" in the list
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003246 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003247 if (*ip == 0) // not found
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003248 {
3249 c = NUL;
3250 break;
3251 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003252 if (*ip == c) // match!
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003253 {
3254 c = ip[1];
3255 break;
3256 }
3257 ip += 2;
3258 }
3259 }
3260
3261 if (c != NUL && c != prevc)
3262 {
3263 ri += mb_char2bytes(c, res + ri);
3264 if (ri + MB_MAXBYTES > MAXWLEN)
3265 break;
3266 prevc = c;
3267 }
3268 }
3269 }
3270 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003271 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003272 // The sl_sal_first[] table contains the translation.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003273 for (s = inword; (c = *s) != NUL; ++s)
3274 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003275 if (VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003276 c = ' ';
3277 else
3278 c = slang->sl_sal_first[c];
3279 if (c != NUL && (ri == 0 || res[ri - 1] != c))
3280 res[ri++] = c;
3281 }
3282 }
3283
3284 res[ri] = NUL;
3285}
3286
3287 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003288spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003289{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003290 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003291 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003292 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003293 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003294 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003295 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003296 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003297 int n, k = 0;
3298 int z0;
3299 int k0;
3300 int n0;
3301 int c;
3302 int pri;
3303 int p0 = -333;
3304 int c0;
3305
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003306 // Remove accents, if wanted. We actually remove all non-word characters.
3307 // But keep white space. We need a copy, the word may be changed here.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003308 if (slang->sl_rem_accents)
3309 {
3310 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003311 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003312 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003313 if (VIM_ISWHITE(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003314 {
3315 *t++ = ' ';
3316 s = skipwhite(s);
3317 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003318 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003319 {
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003320 if (spell_iswordp_nmw(s, curwin))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003321 *t++ = *s;
3322 ++s;
3323 }
3324 }
3325 *t = NUL;
3326 }
3327 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003328 vim_strncpy(word, s, MAXWLEN - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003329
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003330 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003331
3332 /*
3333 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003334 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003335 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003336 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003337 while ((c = word[i]) != NUL)
3338 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003339 // Start with the first rule that has the character in the word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003340 n = slang->sl_sal_first[c];
3341 z0 = 0;
3342
3343 if (n >= 0)
3344 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003345 // check all rules for the same letter
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003346 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003347 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003348 // Quickly skip entries that don't match the word. Most
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003349 // entries are less than three chars, optimize for that.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003350 k = smp[n].sm_leadlen;
3351 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003352 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003353 if (word[i + 1] != s[1])
3354 continue;
3355 if (k > 2)
3356 {
3357 for (j = 2; j < k; ++j)
3358 if (word[i + j] != s[j])
3359 break;
3360 if (j < k)
3361 continue;
3362 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003363 }
3364
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003365 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003366 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003367 // Check for match with one of the chars in "sm_oneof".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003368 while (*pf != NUL && *pf != word[i + k])
3369 ++pf;
3370 if (*pf == NUL)
3371 continue;
3372 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003373 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003374 s = smp[n].sm_rules;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003375 pri = 5; // default priority
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003376
3377 p0 = *s;
3378 k0 = k;
3379 while (*s == '-' && k > 1)
3380 {
3381 k--;
3382 s++;
3383 }
3384 if (*s == '<')
3385 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003386 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003387 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003388 // determine priority
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003389 pri = *s - '0';
3390 s++;
3391 }
3392 if (*s == '^' && *(s + 1) == '^')
3393 s++;
3394
3395 if (*s == NUL
3396 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003397 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02003398 || spell_iswordp(word + i - 1, curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003399 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003400 || (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003401 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02003402 && spell_iswordp(word + i - 1, curwin)
3403 && (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003404 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003405 // search for followup rules, if:
3406 // followup and k > 1 and NO '-' in searchstring
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003407 c0 = word[i + k - 1];
3408 n0 = slang->sl_sal_first[c0];
3409
3410 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003411 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003412 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003413 // test follow-up rule for "word[i + k]"
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003414 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003415 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003416 // Quickly skip entries that don't match the word.
3417 //
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003418 k0 = smp[n0].sm_leadlen;
3419 if (k0 > 1)
3420 {
3421 if (word[i + k] != s[1])
3422 continue;
3423 if (k0 > 2)
3424 {
3425 pf = word + i + k + 1;
3426 for (j = 2; j < k0; ++j)
3427 if (*pf++ != s[j])
3428 break;
3429 if (j < k0)
3430 continue;
3431 }
3432 }
3433 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003434
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003435 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003436 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003437 // Check for match with one of the chars in
3438 // "sm_oneof".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003439 while (*pf != NUL && *pf != word[i + k0])
3440 ++pf;
3441 if (*pf == NUL)
3442 continue;
3443 ++k0;
3444 }
3445
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003446 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003447 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003448 while (*s == '-')
3449 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003450 // "k0" gets NOT reduced because
3451 // "if (k0 == k)"
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003452 s++;
3453 }
3454 if (*s == '<')
3455 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003456 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003457 {
3458 p0 = *s - '0';
3459 s++;
3460 }
3461
3462 if (*s == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003463 // *s == '^' cuts
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003464 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003465 && !spell_iswordp(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02003466 curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003467 {
3468 if (k0 == k)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003469 // this is just a piece of the string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003470 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003471
3472 if (p0 < pri)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003473 // priority too low
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003474 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003475 // rule fits; stop search
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003476 break;
3477 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003478 }
3479
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003480 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003481 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003482 }
3483
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003484 // replace string
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003485 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003486 if (s == NULL)
3487 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003488 pf = smp[n].sm_rules;
3489 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003490 if (p0 == 1 && z == 0)
3491 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003492 // rule with '<' is used
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003493 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
3494 || res[reslen - 1] == *s))
3495 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003496 z0 = 1;
3497 z = 1;
3498 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003499 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003500 {
3501 word[i + k0] = *s;
3502 k0++;
3503 s++;
3504 }
3505 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +00003506 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003507
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003508 // new "actual letter"
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003509 c = word[i];
3510 }
3511 else
3512 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003513 // no '<' rule used
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003514 i += k - 1;
3515 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003516 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003517 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003518 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003519 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003520 s++;
3521 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003522 // new "actual letter"
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003523 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003524 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003525 {
3526 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003527 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003528 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003529 i = 0;
3530 z0 = 1;
3531 }
3532 }
3533 break;
3534 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003535 }
3536 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01003537 else if (VIM_ISWHITE(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003538 {
3539 c = ' ';
3540 k = 1;
3541 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003542
3543 if (z0 == 0)
3544 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003545 if (k && !p0 && reslen < MAXWLEN && c != NUL
3546 && (!slang->sl_collapse || reslen == 0
3547 || res[reslen - 1] != c))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003548 // condense only double letters
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003549 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003550
3551 i++;
3552 z = 0;
3553 k = 0;
3554 }
3555 }
3556
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003557 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003558}
3559
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003560/*
3561 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
3562 * Multi-byte version of spell_soundfold().
3563 */
3564 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003565spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003566{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003567 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003568 int word[MAXWLEN];
3569 int wres[MAXWLEN];
3570 int l;
3571 char_u *s;
3572 int *ws;
3573 char_u *t;
3574 int *pf;
3575 int i, j, z;
3576 int reslen;
3577 int n, k = 0;
3578 int z0;
3579 int k0;
3580 int n0;
3581 int c;
3582 int pri;
3583 int p0 = -333;
3584 int c0;
3585 int did_white = FALSE;
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003586 int wordlen;
3587
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003588
3589 /*
3590 * Convert the multi-byte string to a wide-character string.
3591 * Remove accents, if wanted. We actually remove all non-word characters.
3592 * But keep white space.
3593 */
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003594 wordlen = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003595 for (s = inword; *s != NUL; )
3596 {
3597 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003598 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003599 if (slang->sl_rem_accents)
3600 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003601 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003602 {
3603 if (did_white)
3604 continue;
3605 c = ' ';
3606 did_white = TRUE;
3607 }
3608 else
3609 {
3610 did_white = FALSE;
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003611 if (!spell_iswordp_nmw(t, curwin))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003612 continue;
3613 }
3614 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003615 word[wordlen++] = c;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003616 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003617 word[wordlen] = NUL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003618
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003619 /*
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003620 * This algorithm comes from Aspell phonet.cpp.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003621 * Converted from C++ to C. Added support for multi-byte chars.
3622 * Changed to keep spaces.
3623 */
3624 i = reslen = z = 0;
3625 while ((c = word[i]) != NUL)
3626 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003627 // Start with the first rule that has the character in the word.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003628 n = slang->sl_sal_first[c & 0xff];
3629 z0 = 0;
3630
3631 if (n >= 0)
3632 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003633 // Check all rules for the same index byte.
3634 // If c is 0x300 need extra check for the end of the array, as
3635 // (c & 0xff) is NUL.
Bram Moolenaar95e85792010-08-01 15:37:02 +02003636 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff)
3637 && ws[0] != NUL; ++n)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003638 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003639 // Quickly skip entries that don't match the word. Most
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003640 // entries are less than three chars, optimize for that.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003641 if (c != ws[0])
3642 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003643 k = smp[n].sm_leadlen;
3644 if (k > 1)
3645 {
3646 if (word[i + 1] != ws[1])
3647 continue;
3648 if (k > 2)
3649 {
3650 for (j = 2; j < k; ++j)
3651 if (word[i + j] != ws[j])
3652 break;
3653 if (j < k)
3654 continue;
3655 }
3656 }
3657
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003658 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003659 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003660 // Check for match with one of the chars in "sm_oneof".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003661 while (*pf != NUL && *pf != word[i + k])
3662 ++pf;
3663 if (*pf == NUL)
3664 continue;
3665 ++k;
3666 }
3667 s = smp[n].sm_rules;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003668 pri = 5; // default priority
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003669
3670 p0 = *s;
3671 k0 = k;
3672 while (*s == '-' && k > 1)
3673 {
3674 k--;
3675 s++;
3676 }
3677 if (*s == '<')
3678 s++;
3679 if (VIM_ISDIGIT(*s))
3680 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003681 // determine priority
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003682 pri = *s - '0';
3683 s++;
3684 }
3685 if (*s == '^' && *(s + 1) == '^')
3686 s++;
3687
3688 if (*s == NUL
3689 || (*s == '^'
3690 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02003691 || spell_iswordp_w(word + i - 1, curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003692 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003693 || (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003694 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02003695 && spell_iswordp_w(word + i - 1, curwin)
3696 && (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003697 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003698 // search for followup rules, if:
3699 // followup and k > 1 and NO '-' in searchstring
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003700 c0 = word[i + k - 1];
3701 n0 = slang->sl_sal_first[c0 & 0xff];
3702
3703 if (slang->sl_followup && k > 1 && n0 >= 0
3704 && p0 != '-' && word[i + k] != NUL)
3705 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003706 // Test follow-up rule for "word[i + k]"; loop over
3707 // all entries with the same index byte.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003708 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
3709 == (c0 & 0xff); ++n0)
3710 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003711 // Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003712 if (c0 != ws[0])
3713 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003714 k0 = smp[n0].sm_leadlen;
3715 if (k0 > 1)
3716 {
3717 if (word[i + k] != ws[1])
3718 continue;
3719 if (k0 > 2)
3720 {
3721 pf = word + i + k + 1;
3722 for (j = 2; j < k0; ++j)
3723 if (*pf++ != ws[j])
3724 break;
3725 if (j < k0)
3726 continue;
3727 }
3728 }
3729 k0 += k - 1;
3730
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003731 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003732 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003733 // Check for match with one of the chars in
3734 // "sm_oneof".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003735 while (*pf != NUL && *pf != word[i + k0])
3736 ++pf;
3737 if (*pf == NUL)
3738 continue;
3739 ++k0;
3740 }
3741
3742 p0 = 5;
3743 s = smp[n0].sm_rules;
3744 while (*s == '-')
3745 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003746 // "k0" gets NOT reduced because
3747 // "if (k0 == k)"
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003748 s++;
3749 }
3750 if (*s == '<')
3751 s++;
3752 if (VIM_ISDIGIT(*s))
3753 {
3754 p0 = *s - '0';
3755 s++;
3756 }
3757
3758 if (*s == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003759 // *s == '^' cuts
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003760 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003761 && !spell_iswordp_w(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02003762 curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003763 {
3764 if (k0 == k)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003765 // this is just a piece of the string
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003766 continue;
3767
3768 if (p0 < pri)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003769 // priority too low
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003770 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003771 // rule fits; stop search
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003772 break;
3773 }
3774 }
3775
3776 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
3777 == (c0 & 0xff))
3778 continue;
3779 }
3780
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003781 // replace string
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003782 ws = smp[n].sm_to_w;
3783 s = smp[n].sm_rules;
3784 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
3785 if (p0 == 1 && z == 0)
3786 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003787 // rule with '<' is used
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003788 if (reslen > 0 && ws != NULL && *ws != NUL
3789 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003790 || wres[reslen - 1] == *ws))
3791 reslen--;
3792 z0 = 1;
3793 z = 1;
3794 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003795 if (ws != NULL)
3796 while (*ws != NUL && word[i + k0] != NUL)
3797 {
3798 word[i + k0] = *ws;
3799 k0++;
3800 ws++;
3801 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003802 if (k > k0)
3803 mch_memmove(word + i + k0, word + i + k,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003804 sizeof(int) * (wordlen - (i + k) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003805
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003806 // new "actual letter"
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003807 c = word[i];
3808 }
3809 else
3810 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003811 // no '<' rule used
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003812 i += k - 1;
3813 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003814 if (ws != NULL)
3815 while (*ws != NUL && ws[1] != NUL
3816 && reslen < MAXWLEN)
3817 {
3818 if (reslen == 0 || wres[reslen - 1] != *ws)
3819 wres[reslen++] = *ws;
3820 ws++;
3821 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003822 // new "actual letter"
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003823 if (ws == NULL)
3824 c = NUL;
3825 else
3826 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003827 if (strstr((char *)s, "^^") != NULL)
3828 {
3829 if (c != NUL)
3830 wres[reslen++] = c;
3831 mch_memmove(word, word + i + 1,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003832 sizeof(int) * (wordlen - (i + 1) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003833 i = 0;
3834 z0 = 1;
3835 }
3836 }
3837 break;
3838 }
3839 }
3840 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01003841 else if (VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003842 {
3843 c = ' ';
3844 k = 1;
3845 }
3846
3847 if (z0 == 0)
3848 {
3849 if (k && !p0 && reslen < MAXWLEN && c != NUL
3850 && (!slang->sl_collapse || reslen == 0
3851 || wres[reslen - 1] != c))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003852 // condense only double letters
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003853 wres[reslen++] = c;
3854
3855 i++;
3856 z = 0;
3857 k = 0;
3858 }
3859 }
3860
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003861 // Convert wide characters in "wres" to a multi-byte string in "res".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003862 l = 0;
3863 for (n = 0; n < reslen; ++n)
3864 {
3865 l += mb_char2bytes(wres[n], res + l);
3866 if (l + MB_MAXBYTES > MAXWLEN)
3867 break;
3868 }
3869 res[l] = NUL;
3870}
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003871
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003872/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003873 * ":spellinfo"
3874 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003875 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003876ex_spellinfo(exarg_T *eap UNUSED)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003877{
3878 int lpi;
3879 langp_T *lp;
3880 char_u *p;
3881
3882 if (no_spell_checking(curwin))
3883 return;
3884
3885 msg_start();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003886 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003887 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003888 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003889 msg_puts("file: ");
3890 msg_puts((char *)lp->lp_slang->sl_fname);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003891 msg_putchar('\n');
3892 p = lp->lp_slang->sl_info;
3893 if (p != NULL)
3894 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003895 msg_puts((char *)p);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003896 msg_putchar('\n');
3897 }
3898 }
3899 msg_end();
3900}
3901
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003902#define DUMPFLAG_KEEPCASE 1 // round 2: keep-case tree
3903#define DUMPFLAG_COUNT 2 // include word count
3904#define DUMPFLAG_ICASE 4 // ignore case when finding matches
3905#define DUMPFLAG_ONECAP 8 // pattern starts with capital
3906#define DUMPFLAG_ALLCAP 16 // pattern is all capitals
Bram Moolenaar4770d092006-01-12 23:22:24 +00003907
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003908/*
3909 * ":spelldump"
3910 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003911 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003912ex_spelldump(exarg_T *eap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003913{
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003914 char_u *spl;
3915 long dummy;
3916
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003917 if (no_spell_checking(curwin))
3918 return;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003919 (void)get_option_value((char_u*)"spl", &dummy, &spl, NULL, OPT_LOCAL);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003920
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003921 // Create a new empty buffer in a new window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003922 do_cmdline_cmd((char_u *)"new");
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003923
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003924 // enable spelling locally in the new window
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003925 set_option_value_give_err((char_u*)"spell", TRUE, (char_u*)"", OPT_LOCAL);
3926 set_option_value_give_err((char_u*)"spl", dummy, spl, OPT_LOCAL);
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003927 vim_free(spl);
3928
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003929 if (!BUFEMPTY())
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003930 return;
3931
Bram Moolenaar860cae12010-06-05 23:22:07 +02003932 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003933
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003934 // Delete the empty line that we started with.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003935 if (curbuf->b_ml.ml_line_count > 1)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003936 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003937
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003938 redraw_later(UPD_NOT_VALID);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003939}
3940
3941/*
3942 * Go through all possible words and:
3943 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
3944 * "ic" and "dir" are not used.
3945 * 2. When "pat" is not NULL: add matching words to insert mode completion.
3946 */
3947 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003948spell_dump_compl(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003949 char_u *pat, // leading part of the word
3950 int ic, // ignore case
3951 int *dir, // direction for adding matches
3952 int dumpflags_arg) // DUMPFLAG_*
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003953{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003954 langp_T *lp;
3955 slang_T *slang;
3956 idx_T arridx[MAXWLEN];
3957 int curi[MAXWLEN];
3958 char_u word[MAXWLEN];
3959 int c;
3960 char_u *byts;
3961 idx_T *idxs;
3962 linenr_T lnum = 0;
3963 int round;
3964 int depth;
3965 int n;
3966 int flags;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003967 char_u *region_names = NULL; // region names being used
3968 int do_region = TRUE; // dump region names and numbers
Bram Moolenaar7887d882005-07-01 22:33:52 +00003969 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003970 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003971 int dumpflags = dumpflags_arg;
3972 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003973
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003974 // When ignoring case or when the pattern starts with capital pass this on
3975 // to dump_word().
Bram Moolenaard0131a82006-03-04 21:46:13 +00003976 if (pat != NULL)
3977 {
3978 if (ic)
3979 dumpflags |= DUMPFLAG_ICASE;
3980 else
3981 {
3982 n = captype(pat, NULL);
3983 if (n == WF_ONECAP)
3984 dumpflags |= DUMPFLAG_ONECAP;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01003985 else if (n == WF_ALLCAP && (int)STRLEN(pat) > mb_ptr2len(pat))
Bram Moolenaard0131a82006-03-04 21:46:13 +00003986 dumpflags |= DUMPFLAG_ALLCAP;
3987 }
3988 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003989
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003990 // Find out if we can support regions: All languages must support the same
3991 // regions or none at all.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003992 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003993 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003994 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003995 p = lp->lp_slang->sl_regions;
3996 if (p[0] != 0)
3997 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003998 if (region_names == NULL) // first language with regions
Bram Moolenaar7887d882005-07-01 22:33:52 +00003999 region_names = p;
4000 else if (STRCMP(region_names, p) != 0)
4001 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004002 do_region = FALSE; // region names are different
Bram Moolenaar7887d882005-07-01 22:33:52 +00004003 break;
4004 }
4005 }
4006 }
4007
LemonBoye98fb642023-08-15 23:07:55 +02004008 if (do_region && region_names != NULL && pat == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004009 {
LemonBoye98fb642023-08-15 23:07:55 +02004010 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
4011 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004012 }
4013 else
4014 do_region = FALSE;
4015
4016 /*
4017 * Loop over all files loaded for the entries in 'spelllang'.
4018 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004019 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004020 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004021 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004022 slang = lp->lp_slang;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004023 if (slang->sl_fbyts == NULL) // reloading failed
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004024 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004025
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004026 if (pat == NULL)
4027 {
4028 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
4029 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
4030 }
4031
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004032 // When matching with a pattern and there are no prefixes only use
4033 // parts of the tree that match "pat".
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004034 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004035 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004036 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004037 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004038
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004039 // round 1: case-folded tree
4040 // round 2: keep-case tree
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004041 for (round = 1; round <= 2; ++round)
4042 {
4043 if (round == 1)
4044 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004045 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004046 byts = slang->sl_fbyts;
4047 idxs = slang->sl_fidxs;
4048 }
4049 else
4050 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004051 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004052 byts = slang->sl_kbyts;
4053 idxs = slang->sl_kidxs;
4054 }
4055 if (byts == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004056 continue; // array is empty
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004057
4058 depth = 0;
4059 arridx[0] = 0;
4060 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004061 while (depth >= 0 && !got_int
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004062 && (pat == NULL || !ins_compl_interrupted()))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004063 {
4064 if (curi[depth] > byts[arridx[depth]])
4065 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004066 // Done all bytes at this node, go up one level.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004067 --depth;
4068 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02004069 ins_compl_check_keys(50, FALSE);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004070 }
4071 else
4072 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004073 // Do one more byte at this node.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004074 n = arridx[depth] + curi[depth];
4075 ++curi[depth];
4076 c = byts[n];
Bram Moolenaar54e5fed2022-07-04 13:37:07 +01004077 if (c == 0 || depth >= MAXWLEN - 1)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004078 {
Bram Moolenaar54e5fed2022-07-04 13:37:07 +01004079 // End of word or reached maximum length, deal with the
4080 // word.
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004081 // Don't use keep-case words in the fold-case tree,
4082 // they will appear in the keep-case tree.
4083 // Only use the word when the region matches.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004084 flags = (int)idxs[n];
4085 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004086 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +00004087 && (do_region
4088 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004089 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004090 & lp->lp_region) != 0))
4091 {
4092 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004093 if (!do_region)
4094 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004095
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004096 // Dump the basic word if there is no prefix or
4097 // when it's the first one.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004098 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004099 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004100 {
4101 dump_word(slang, word, pat, dir,
4102 dumpflags, flags, lnum);
4103 if (pat == NULL)
4104 ++lnum;
4105 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004106
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004107 // Apply the prefix, if there is one.
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004108 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004109 lnum = dump_prefixes(slang, word, pat, dir,
4110 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004111 }
4112 }
4113 else
4114 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004115 // Normal char, go one level deeper.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004116 word[depth++] = c;
4117 arridx[depth] = idxs[n];
4118 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004119
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004120 // Check if this character matches with the pattern.
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004121 // If not skip the whole tree below it.
4122 // Always ignore case here, dump_word() will check
4123 // proper case later. This isn't exactly right when
4124 // length changes for multi-byte characters with
4125 // ignore case...
Bram Moolenaard0131a82006-03-04 21:46:13 +00004126 if (depth <= patlen
4127 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004128 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004129 }
4130 }
4131 }
4132 }
4133 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004134}
4135
4136/*
4137 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004138 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004139 */
4140 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004141dump_word(
4142 slang_T *slang,
4143 char_u *word,
4144 char_u *pat,
4145 int *dir,
4146 int dumpflags,
4147 int wordflags,
4148 linenr_T lnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004149{
4150 int keepcap = FALSE;
4151 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004152 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004153 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +00004154 char_u badword[MAXWLEN + 10];
4155 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +00004156 int flags = wordflags;
4157
4158 if (dumpflags & DUMPFLAG_ONECAP)
4159 flags |= WF_ONECAP;
4160 if (dumpflags & DUMPFLAG_ALLCAP)
4161 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004162
Bram Moolenaar4770d092006-01-12 23:22:24 +00004163 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004164 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004165 // Need to fix case according to "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004166 make_case_word(word, cword, flags);
4167 p = cword;
4168 }
4169 else
4170 {
4171 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004172 if ((dumpflags & DUMPFLAG_KEEPCASE)
4173 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004174 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004175 keepcap = TRUE;
4176 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004177 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004178
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004179 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004180 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004181 // Add flags and regions after a slash.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004182 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004183 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004184 STRCPY(badword, p);
4185 STRCAT(badword, "/");
4186 if (keepcap)
4187 STRCAT(badword, "=");
4188 if (flags & WF_BANNED)
4189 STRCAT(badword, "!");
4190 else if (flags & WF_RARE)
4191 STRCAT(badword, "?");
4192 if (flags & WF_REGION)
4193 for (i = 0; i < 7; ++i)
4194 if (flags & (0x10000 << i))
4195 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
4196 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004197 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004198
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004199 if (dumpflags & DUMPFLAG_COUNT)
4200 {
4201 hashitem_T *hi;
4202
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004203 // Include the word count for ":spelldump!".
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004204 hi = hash_find(&slang->sl_wordcount, tw);
4205 if (!HASHITEM_EMPTY(hi))
4206 {
4207 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
4208 tw, HI2WC(hi)->wc_count);
4209 p = IObuff;
4210 }
4211 }
4212
4213 ml_append(lnum, p, (colnr_T)0, FALSE);
4214 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00004215 else if (((dumpflags & DUMPFLAG_ICASE)
4216 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
4217 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004218 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004219 p_ic, NULL, *dir, FALSE) == OK)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004220 // if dir was BACKWARD then honor it just once
Bram Moolenaard0131a82006-03-04 21:46:13 +00004221 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004222}
4223
4224/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004225 * For ":spelldump": Find matching prefixes for "word". Prepend each to
4226 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004227 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004228 * Return the updated line number.
4229 */
4230 static linenr_T
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004231dump_prefixes(
4232 slang_T *slang,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004233 char_u *word, // case-folded word
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004234 char_u *pat,
4235 int *dir,
4236 int dumpflags,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004237 int flags, // flags with prefix ID
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004238 linenr_T startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004239{
4240 idx_T arridx[MAXWLEN];
4241 int curi[MAXWLEN];
4242 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +00004243 char_u word_up[MAXWLEN];
4244 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004245 int c;
4246 char_u *byts;
4247 idx_T *idxs;
4248 linenr_T lnum = startlnum;
4249 int depth;
4250 int n;
4251 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004252 int i;
4253
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004254 // If the word starts with a lower-case letter make the word with an
4255 // upper-case letter in word_up[].
Bram Moolenaar53805d12005-08-01 07:08:33 +00004256 c = PTR2CHAR(word);
4257 if (SPELL_TOUPPER(c) != c)
4258 {
4259 onecap_copy(word, word_up, TRUE);
4260 has_word_up = TRUE;
4261 }
4262
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004263 byts = slang->sl_pbyts;
4264 idxs = slang->sl_pidxs;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004265 if (byts != NULL) // array not is empty
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004266 {
4267 /*
4268 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004269 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004270 */
4271 depth = 0;
4272 arridx[0] = 0;
4273 curi[0] = 1;
4274 while (depth >= 0 && !got_int)
4275 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004276 n = arridx[depth];
4277 len = byts[n];
4278 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004279 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004280 // Done all bytes at this node, go up one level.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004281 --depth;
4282 line_breakcheck();
4283 }
4284 else
4285 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004286 // Do one more byte at this node.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004287 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004288 ++curi[depth];
4289 c = byts[n];
4290 if (c == 0)
4291 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004292 // End of prefix, find out how many IDs there are.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004293 for (i = 1; i < len; ++i)
4294 if (byts[n + i] != 0)
4295 break;
4296 curi[depth] += i - 1;
4297
Bram Moolenaar53805d12005-08-01 07:08:33 +00004298 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
4299 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004300 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004301 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004302 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004303 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004304 : flags, lnum);
4305 if (lnum != 0)
4306 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004307 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00004308
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004309 // Check for prefix that matches the word when the
4310 // first letter is upper-case, but only if the prefix has
4311 // a condition.
Bram Moolenaar53805d12005-08-01 07:08:33 +00004312 if (has_word_up)
4313 {
4314 c = valid_word_prefix(i, n, flags, word_up, slang,
4315 TRUE);
4316 if (c != 0)
4317 {
4318 vim_strncpy(prefix + depth, word_up,
4319 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004320 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004321 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004322 : flags, lnum);
4323 if (lnum != 0)
4324 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004325 }
4326 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004327 }
4328 else
4329 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004330 // Normal char, go one level deeper.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004331 prefix[depth++] = c;
4332 arridx[depth] = idxs[n];
4333 curi[depth] = 1;
4334 }
4335 }
4336 }
4337 }
4338
4339 return lnum;
4340}
4341
Bram Moolenaar95529562005-08-25 21:21:38 +00004342/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004343 * Move "p" to the end of word "start".
4344 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +00004345 */
4346 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004347spell_to_word_end(char_u *start, win_T *win)
Bram Moolenaar95529562005-08-25 21:21:38 +00004348{
4349 char_u *p = start;
4350
Bram Moolenaar860cae12010-06-05 23:22:07 +02004351 while (*p != NUL && spell_iswordp(p, win))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004352 MB_PTR_ADV(p);
Bram Moolenaar95529562005-08-25 21:21:38 +00004353 return p;
4354}
4355
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004356/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004357 * For Insert mode completion CTRL-X s:
4358 * Find start of the word in front of column "startcol".
4359 * We don't check if it is badly spelled, with completion we can only change
4360 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004361 * Returns the column number of the word.
4362 */
4363 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004364spell_word_start(int startcol)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004365{
4366 char_u *line;
4367 char_u *p;
4368 int col = 0;
4369
Bram Moolenaar95529562005-08-25 21:21:38 +00004370 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004371 return startcol;
4372
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004373 // Find a word character before "startcol".
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004374 line = ml_get_curline();
4375 for (p = line + startcol; p > line; )
4376 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004377 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004378 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004379 break;
4380 }
4381
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004382 // Go back to start of the word.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004383 while (p > line)
4384 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004385 col = (int)(p - line);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004386 MB_PTR_BACK(line, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004387 if (!spell_iswordp(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004388 break;
4389 col = 0;
4390 }
4391
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004392 return col;
4393}
4394
4395/*
Bram Moolenaar4effc802005-09-30 21:12:02 +00004396 * Need to check for 'spellcapcheck' now, the word is removed before
4397 * expand_spelling() is called. Therefore the ugly global variable.
4398 */
4399static int spell_expand_need_cap;
4400
4401 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004402spell_expand_check_cap(colnr_T col)
Bram Moolenaar4effc802005-09-30 21:12:02 +00004403{
Luuk van Baal2ac64972023-05-25 17:14:42 +01004404 spell_expand_need_cap = check_need_cap(curwin, curwin->w_cursor.lnum, col);
Bram Moolenaar4effc802005-09-30 21:12:02 +00004405}
4406
4407/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004408 * Get list of spelling suggestions.
4409 * Used for Insert mode completion CTRL-X ?.
4410 * Returns the number of matches. The matches are in "matchp[]", array of
4411 * allocated strings.
4412 */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004413 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004414expand_spelling(
4415 linenr_T lnum UNUSED,
4416 char_u *pat,
4417 char_u ***matchp)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004418{
4419 garray_T ga;
4420
Bram Moolenaar4770d092006-01-12 23:22:24 +00004421 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004422 *matchp = ga.ga_data;
4423 return ga.ga_len;
4424}
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004425
Bram Moolenaare677df82019-09-02 22:31:11 +02004426/*
Bram Moolenaarf154f3a2020-06-08 18:54:49 +02004427 * Return TRUE if "val" is a valid 'spelllang' value.
Bram Moolenaare677df82019-09-02 22:31:11 +02004428 */
4429 int
Bram Moolenaarf154f3a2020-06-08 18:54:49 +02004430valid_spelllang(char_u *val)
Bram Moolenaare677df82019-09-02 22:31:11 +02004431{
4432 return valid_name(val, ".-_,@");
4433}
4434
4435/*
4436 * Return TRUE if "val" is a valid 'spellfile' value.
4437 */
4438 int
4439valid_spellfile(char_u *val)
4440{
4441 char_u *s;
4442
4443 for (s = val; *s != NUL; ++s)
Bram Moolenaarbc49c5f2022-08-04 13:01:48 +01004444 if (!vim_is_fname_char(*s))
Bram Moolenaare677df82019-09-02 22:31:11 +02004445 return FALSE;
4446 return TRUE;
4447}
4448
4449/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004450 * Handle side effects of setting 'spell' or 'spellfile'
Bram Moolenaare677df82019-09-02 22:31:11 +02004451 * Return an error message or NULL for success.
4452 */
4453 char *
4454did_set_spell_option(int is_spellfile)
4455{
4456 char *errmsg = NULL;
4457 win_T *wp;
4458 int l;
4459
4460 if (is_spellfile)
4461 {
4462 l = (int)STRLEN(curwin->w_s->b_p_spf);
4463 if (l > 0 && (l < 4
4464 || STRCMP(curwin->w_s->b_p_spf + l - 4, ".add") != 0))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004465 errmsg = e_invalid_argument;
Bram Moolenaare677df82019-09-02 22:31:11 +02004466 }
4467
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00004468 if (errmsg != NULL)
4469 return errmsg;
4470
4471 FOR_ALL_WINDOWS(wp)
4472 if (wp->w_buffer == curbuf && wp->w_p_spell)
4473 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004474 errmsg = parse_spelllang(wp);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00004475 break;
4476 }
Bram Moolenaare677df82019-09-02 22:31:11 +02004477 return errmsg;
4478}
4479
4480/*
4481 * Set curbuf->b_cap_prog to the regexp program for 'spellcapcheck'.
4482 * Return error message when failed, NULL when OK.
4483 */
4484 char *
4485compile_cap_prog(synblock_T *synblock)
4486{
4487 regprog_T *rp = synblock->b_cap_prog;
4488 char_u *re;
4489
Bram Moolenaar53efb182019-10-13 19:49:26 +02004490 if (synblock->b_p_spc == NULL || *synblock->b_p_spc == NUL)
Bram Moolenaare677df82019-09-02 22:31:11 +02004491 synblock->b_cap_prog = NULL;
4492 else
4493 {
4494 // Prepend a ^ so that we only match at one column
4495 re = concat_str((char_u *)"^", synblock->b_p_spc);
4496 if (re != NULL)
4497 {
4498 synblock->b_cap_prog = vim_regcomp(re, RE_MAGIC);
4499 vim_free(re);
4500 if (synblock->b_cap_prog == NULL)
4501 {
4502 synblock->b_cap_prog = rp; // restore the previous program
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004503 return e_invalid_argument;
Bram Moolenaare677df82019-09-02 22:31:11 +02004504 }
4505 }
4506 }
4507
4508 vim_regfree(rp);
4509 return NULL;
4510}
4511
4512#endif // FEAT_SPELL