blob: 87256f9d7be85a6de9e085ecd4e9ecf996ad4754 [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
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02001339 smt_T behaviour, // Behaviour of the function
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
zeertzjq94b7c322024-03-12 21:50:32 +01001387 len = ml_get_buf_len(wp->w_buffer, lnum);
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.
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02001444 if (behaviour == SMT_ALL
1445 || (behaviour == SMT_BAD && attr == HLF_SPB)
1446 || (behaviour == SMT_RARE && attr == HLF_SPR))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001447 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001448 // When searching forward only accept a bad word after
1449 // the cursor.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001450 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001451 || lnum != wp->w_cursor.lnum
Bram Moolenaarfe154992022-03-22 20:42:12 +00001452 || (wrapped
1453 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00001454 : p - buf)
Bram Moolenaarfe154992022-03-22 20:42:12 +00001455 > wp->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001456 {
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001457#ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00001458 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001459 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001460 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001461 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00001462 FALSE, &can_spell, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001463 if (!can_spell)
1464 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001465 }
1466 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001467#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001468 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001469
Bram Moolenaar51485f02005-06-04 21:55:20 +00001470 if (can_spell)
1471 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00001472 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001473 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001474 found_pos.col = (int)(p - buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001475 found_pos.coladd = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001476 if (dir == FORWARD)
1477 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001478 // No need to search further.
Bram Moolenaar95529562005-08-25 21:21:38 +00001479 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001480 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001481 if (attrp != NULL)
1482 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001483 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001484 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001485 else if (curline)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001486 // Insert mode completion: put cursor after
1487 // the bad word.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001488 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001489 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001490 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001491 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00001492 else
1493 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001494 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001495 }
1496
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001497 // advance to character after the word
Bram Moolenaar51485f02005-06-04 21:55:20 +00001498 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001499 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001500 }
1501
Bram Moolenaar5195e452005-08-19 20:32:47 +00001502 if (dir == BACKWARD && found_pos.lnum != 0)
1503 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001504 // Use the last match in the line (before the cursor).
Bram Moolenaar95529562005-08-25 21:21:38 +00001505 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001506 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001507 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001508 }
1509
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001510 if (curline)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001511 break; // only check cursor line
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001512
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001513 // If we are back at the starting line and searched it again there
1514 // is no match, give up.
Bram Moolenaard3f78dc2017-02-25 14:21:10 +01001515 if (lnum == wp->w_cursor.lnum && wrapped)
1516 break;
1517
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001518 // Advance to next line.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001519 if (dir == BACKWARD)
1520 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001521 if (lnum > 1)
1522 --lnum;
1523 else if (!p_ws)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001524 break; // at first line and 'nowrapscan'
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001525 else
1526 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001527 // Wrap around to the end of the buffer. May search the
1528 // starting line again and accept the last match.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001529 lnum = wp->w_buffer->b_ml.ml_line_count;
1530 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001531 if (!shortmess(SHM_SEARCH))
1532 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001533 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001534 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001535 }
1536 else
1537 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001538 if (lnum < wp->w_buffer->b_ml.ml_line_count)
1539 ++lnum;
1540 else if (!p_ws)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001541 break; // at first line and 'nowrapscan'
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001542 else
1543 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001544 // Wrap around to the start of the buffer. May search the
1545 // starting line again and accept the first match.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001546 lnum = 1;
1547 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001548 if (!shortmess(SHM_SEARCH))
1549 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001550 }
1551
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001552 // If we are back at the starting line and there is no match then
1553 // give up.
Bram Moolenaard3f78dc2017-02-25 14:21:10 +01001554 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001555 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001556
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001557 // Skip the characters at the start of the next line that were
1558 // included in a match crossing line boundaries.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001559 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001560 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001561 else
1562 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001563
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001564 // Capcol skips over the inserted space.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001565 --capcol;
1566
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001567 // But after empty line check first word in next line
Bram Moolenaar2813f382022-06-09 19:54:24 +01001568 if (empty_line)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001569 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001570 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001571
1572 line_breakcheck();
1573 }
1574
Bram Moolenaar0c405862005-06-22 22:26:26 +00001575 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001576 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001577}
1578
1579/*
1580 * For spell checking: concatenate the start of the following line "line" into
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001581 * "buf", blanking-out special characters. Copy less than "maxlen" bytes.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00001582 * Keep the blanks at the start of the next line, this is used in win_line()
1583 * to skip those bytes if the word was OK.
Bram Moolenaar0c405862005-06-22 22:26:26 +00001584 */
1585 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001586spell_cat_line(char_u *buf, char_u *line, int maxlen)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001587{
1588 char_u *p;
1589 int n;
1590
1591 p = skipwhite(line);
1592 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1593 p = skipwhite(p + 1);
1594
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001595 if (*p == NUL)
1596 return;
1597
1598 // Only worth concatenating if there is something else than spaces to
1599 // concatenate.
1600 n = (int)(p - line) + 1;
1601 if (n < maxlen - 1)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001602 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001603 vim_memset(buf, ' ', n);
1604 vim_strncpy(buf + n, p, maxlen - 1 - n);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001605 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001606}
1607
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001608/*
1609 * Structure used for the cookie argument of do_in_runtimepath().
1610 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001611typedef struct spelload_S
1612{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001613 char_u sl_lang[MAXWLEN + 1]; // language name
1614 slang_T *sl_slang; // resulting slang_T struct
1615 int sl_nobreak; // NOBREAK language found
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001616} spelload_T;
1617
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001618/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001619 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001620 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001621 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001622 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001623spell_load_lang(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001624{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001625 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001626 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001627 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001628 int round;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001629
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001630 // Copy the language name to pass it to spell_load_cb() as a cookie.
1631 // It's truncated when an error is detected.
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001632 STRCPY(sl.sl_lang, lang);
1633 sl.sl_slang = NULL;
1634 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001635
Bram Moolenaaref976322022-09-28 11:48:30 +01001636 // Disallow deleting the current buffer. Autocommands can do weird things
1637 // and cause "lang" to be freed.
1638 ++curbuf->b_locked;
1639
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001640 // We may retry when no spell file is found for the language, an
1641 // autocommand may load it then.
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001642 for (round = 1; round <= 2; ++round)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001643 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001644 /*
1645 * Find the first spell file for "lang" in 'runtimepath' and load it.
1646 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001647 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001648#ifdef VMS
1649 "spell/%s_%s.spl",
1650#else
1651 "spell/%s.%s.spl",
1652#endif
1653 lang, spell_enc());
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001654 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001655
1656 if (r == FAIL && *sl.sl_lang != NUL)
1657 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001658 // Try loading the ASCII version.
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001659 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001660#ifdef VMS
1661 "spell/%s_ascii.spl",
1662#else
1663 "spell/%s.ascii.spl",
1664#endif
1665 lang);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001666 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001667
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001668 if (r == FAIL && *sl.sl_lang != NUL && round == 1
1669 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
1670 curbuf->b_fname, FALSE, curbuf))
1671 continue;
1672 break;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001673 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001674 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001675 }
1676
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001677 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001678 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001679 smsg(
Bram Moolenaar56f78042010-12-08 17:09:32 +01001680#ifdef VMS
1681 _("Warning: Cannot find word list \"%s_%s.spl\" or \"%s_ascii.spl\""),
1682#else
1683 _("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
1684#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00001685 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001686 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001687 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001688 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001689 // At least one file was loaded, now load ALL the additions.
Bram Moolenaarb765d632005-06-07 21:00:02 +00001690 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001691 do_in_runtimepath(fname_enc, DIP_ALL, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001692 }
Bram Moolenaaref976322022-09-28 11:48:30 +01001693
1694 --curbuf->b_locked;
Bram Moolenaarb765d632005-06-07 21:00:02 +00001695}
1696
1697/*
1698 * Return the encoding used for spell checking: Use 'encoding', except that we
1699 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1700 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001701 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001702spell_enc(void)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001703{
1704
Bram Moolenaarb765d632005-06-07 21:00:02 +00001705 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1706 return p_enc;
Bram Moolenaarb765d632005-06-07 21:00:02 +00001707 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001708}
1709
1710/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001711 * Get the name of the .spl file for the internal wordlist into
1712 * "fname[MAXPATHL]".
1713 */
1714 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001715int_wordlist_spl(char_u *fname)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001716{
Bram Moolenaar56f78042010-12-08 17:09:32 +01001717 vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001718 int_wordlist, spell_enc());
1719}
1720
1721/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001722 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001723 * Caller must fill "sl_next".
1724 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001725 slang_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001726slang_alloc(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001727{
1728 slang_T *lp;
1729
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001730 lp = ALLOC_CLEAR_ONE(slang_T);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001731 if (lp != NULL)
1732 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001733 if (lang != NULL)
1734 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001735 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001736 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001737 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001738 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001739 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001740 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001741
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001742 return lp;
1743}
1744
1745/*
1746 * Free the contents of an slang_T and the structure itself.
1747 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001748 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001749slang_free(slang_T *lp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001750{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001751 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001752 vim_free(lp->sl_fname);
1753 slang_clear(lp);
1754 vim_free(lp);
1755}
1756
1757/*
1758 * Clear an slang_T so that the file can be reloaded.
1759 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001760 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001761slang_clear(slang_T *lp)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001762{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001763 garray_T *gap;
1764 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001765 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001766 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001767 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001768
Bram Moolenaard23a8232018-02-10 18:45:26 +01001769 VIM_CLEAR(lp->sl_fbyts);
1770 VIM_CLEAR(lp->sl_kbyts);
1771 VIM_CLEAR(lp->sl_pbyts);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001772
Bram Moolenaard23a8232018-02-10 18:45:26 +01001773 VIM_CLEAR(lp->sl_fidxs);
1774 VIM_CLEAR(lp->sl_kidxs);
1775 VIM_CLEAR(lp->sl_pidxs);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001776
Bram Moolenaar4770d092006-01-12 23:22:24 +00001777 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001778 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001779 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
1780 while (gap->ga_len > 0)
1781 {
1782 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1783 vim_free(ftp->ft_from);
1784 vim_free(ftp->ft_to);
1785 }
1786 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001787 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001788
1789 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001790 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001791 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001792 // "ga_len" is set to 1 without adding an item for latin1
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001793 if (gap->ga_data != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001794 // SOFOFROM and SOFOTO items: free lists of wide characters.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001795 for (i = 0; i < gap->ga_len; ++i)
1796 vim_free(((int **)gap->ga_data)[i]);
1797 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001798 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001799 // SAL items: free salitem_T items
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001800 while (gap->ga_len > 0)
1801 {
1802 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1803 vim_free(smp->sm_lead);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001804 // Don't free sm_oneof and sm_rules, they point into sm_lead.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001805 vim_free(smp->sm_to);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001806 vim_free(smp->sm_lead_w);
1807 vim_free(smp->sm_oneof_w);
1808 vim_free(smp->sm_to_w);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001809 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001810 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001811
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001812 for (i = 0; i < lp->sl_prefixcnt; ++i)
Bram Moolenaar473de612013-06-08 18:19:48 +02001813 vim_regfree(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001814 lp->sl_prefixcnt = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001815 VIM_CLEAR(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001816
Bram Moolenaard23a8232018-02-10 18:45:26 +01001817 VIM_CLEAR(lp->sl_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001818
Bram Moolenaard23a8232018-02-10 18:45:26 +01001819 VIM_CLEAR(lp->sl_midword);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001820
Bram Moolenaar473de612013-06-08 18:19:48 +02001821 vim_regfree(lp->sl_compprog);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001822 lp->sl_compprog = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001823 VIM_CLEAR(lp->sl_comprules);
1824 VIM_CLEAR(lp->sl_compstartflags);
1825 VIM_CLEAR(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001826
Bram Moolenaard23a8232018-02-10 18:45:26 +01001827 VIM_CLEAR(lp->sl_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001828 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001829
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001830 ga_clear_strings(&lp->sl_comppat);
1831
Bram Moolenaar4770d092006-01-12 23:22:24 +00001832 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
1833 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00001834
Bram Moolenaar4770d092006-01-12 23:22:24 +00001835 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001836
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001837 // Clear info from .sug file.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001838 slang_clear_sug(lp);
1839
Bram Moolenaar5195e452005-08-19 20:32:47 +00001840 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001841 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001842 lp->sl_compsylmax = MAXWLEN;
1843 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001844}
1845
1846/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001847 * Clear the info from the .sug file in "lp".
1848 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001849 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001850slang_clear_sug(slang_T *lp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00001851{
Bram Moolenaard23a8232018-02-10 18:45:26 +01001852 VIM_CLEAR(lp->sl_sbyts);
1853 VIM_CLEAR(lp->sl_sidxs);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001854 close_spellbuf(lp->sl_sugbuf);
1855 lp->sl_sugbuf = NULL;
1856 lp->sl_sugloaded = FALSE;
1857 lp->sl_sugtime = 0;
1858}
1859
1860/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001861 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001862 * Invoked through do_in_runtimepath().
1863 */
1864 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001865spell_load_cb(char_u *fname, void *cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001866{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001867 spelload_T *slp = (spelload_T *)cookie;
1868 slang_T *slang;
1869
1870 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001871 if (slang == NULL)
1872 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001873
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001874 // When a previously loaded file has NOBREAK also use it for the
1875 // ".add" files.
1876 if (slp->sl_nobreak && slang->sl_add)
1877 slang->sl_nobreak = TRUE;
1878 else if (slang->sl_nobreak)
1879 slp->sl_nobreak = TRUE;
1880
1881 slp->sl_slang = slang;
Bram Moolenaarb765d632005-06-07 21:00:02 +00001882}
1883
Bram Moolenaar4770d092006-01-12 23:22:24 +00001884
1885/*
1886 * Add a word to the hashtable of common words.
1887 * If it's already there then the counter is increased.
1888 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001889 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001890count_common_word(
1891 slang_T *lp,
1892 char_u *word,
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001893 int len, // word length, -1 for up to NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001894 int count) // 1 to count once, 10 to init
Bram Moolenaar4770d092006-01-12 23:22:24 +00001895{
1896 hash_T hash;
1897 hashitem_T *hi;
1898 wordcount_T *wc;
1899 char_u buf[MAXWLEN];
1900 char_u *p;
1901
1902 if (len == -1)
1903 p = word;
Bram Moolenaar5bcc5a12019-08-06 22:48:02 +02001904 else if (len >= MAXWLEN)
1905 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001906 else
1907 {
1908 vim_strncpy(buf, word, len);
1909 p = buf;
1910 }
1911
1912 hash = hash_hash(p);
1913 hi = hash_lookup(&lp->sl_wordcount, p, hash);
1914 if (HASHITEM_EMPTY(hi))
1915 {
zeertzjq1b438a82023-02-01 13:11:15 +00001916 wc = alloc(offsetof(wordcount_T, wc_word) + STRLEN(p) + 1);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001917 if (wc == NULL)
1918 return;
1919 STRCPY(wc->wc_word, p);
1920 wc->wc_count = count;
1921 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
1922 }
1923 else
1924 {
1925 wc = HI2WC(hi);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001926 if ((wc->wc_count += count) < (unsigned)count) // check for overflow
Bram Moolenaar4770d092006-01-12 23:22:24 +00001927 wc->wc_count = MAXWORDCOUNT;
1928 }
1929}
1930
1931/*
Bram Moolenaar95529562005-08-25 21:21:38 +00001932 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00001933 * Like strchr() but independent of locale.
1934 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001935 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001936byte_in_str(char_u *str, int n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001937{
1938 char_u *p;
1939
1940 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00001941 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001942 return TRUE;
1943 return FALSE;
1944}
1945
Bram Moolenaar5195e452005-08-19 20:32:47 +00001946#define SY_MAXLEN 30
1947typedef struct syl_item_S
1948{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001949 char_u sy_chars[SY_MAXLEN]; // the sequence of chars
Bram Moolenaar5195e452005-08-19 20:32:47 +00001950 int sy_len;
1951} syl_item_T;
1952
1953/*
1954 * Truncate "slang->sl_syllable" at the first slash and put the following items
1955 * in "slang->sl_syl_items".
1956 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001957 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001958init_syl_tab(slang_T *slang)
Bram Moolenaar5195e452005-08-19 20:32:47 +00001959{
1960 char_u *p;
1961 char_u *s;
1962 int l;
1963 syl_item_T *syl;
1964
1965 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
1966 p = vim_strchr(slang->sl_syllable, '/');
1967 while (p != NULL)
1968 {
1969 *p++ = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001970 if (*p == NUL) // trailing slash
Bram Moolenaar5195e452005-08-19 20:32:47 +00001971 break;
1972 s = p;
1973 p = vim_strchr(p, '/');
1974 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001975 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001976 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001977 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001978 if (l >= SY_MAXLEN)
1979 return SP_FORMERROR;
1980 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001981 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001982 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
1983 + slang->sl_syl_items.ga_len++;
1984 vim_strncpy(syl->sy_chars, s, l);
1985 syl->sy_len = l;
1986 }
1987 return OK;
1988}
1989
1990/*
1991 * Count the number of syllables in "word".
1992 * When "word" contains spaces the syllables after the last space are counted.
1993 * Returns zero if syllables are not defines.
1994 */
1995 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001996count_syllables(slang_T *slang, char_u *word)
Bram Moolenaar5195e452005-08-19 20:32:47 +00001997{
1998 int cnt = 0;
1999 int skip = FALSE;
2000 char_u *p;
2001 int len;
2002 int i;
2003 syl_item_T *syl;
2004 int c;
2005
2006 if (slang->sl_syllable == NULL)
2007 return 0;
2008
2009 for (p = word; *p != NUL; p += len)
2010 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002011 // When running into a space reset counter.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002012 if (*p == ' ')
2013 {
2014 len = 1;
2015 cnt = 0;
2016 continue;
2017 }
2018
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002019 // Find longest match of syllable items.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002020 len = 0;
2021 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
2022 {
2023 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
2024 if (syl->sy_len > len
2025 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
2026 len = syl->sy_len;
2027 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002028 if (len != 0) // found a match, count syllable
Bram Moolenaar5195e452005-08-19 20:32:47 +00002029 {
2030 ++cnt;
2031 skip = FALSE;
2032 }
2033 else
2034 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002035 // No recognized syllable item, at least a syllable char then?
Bram Moolenaar5195e452005-08-19 20:32:47 +00002036 c = mb_ptr2char(p);
2037 len = (*mb_ptr2len)(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002038 if (vim_strchr(slang->sl_syllable, c) == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002039 skip = FALSE; // No, search for next syllable
Bram Moolenaar5195e452005-08-19 20:32:47 +00002040 else if (!skip)
2041 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002042 ++cnt; // Yes, count it
2043 skip = TRUE; // don't count following syllable chars
Bram Moolenaar5195e452005-08-19 20:32:47 +00002044 }
2045 }
2046 }
2047 return cnt;
2048}
2049
2050/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02002051 * Parse 'spelllang' and set w_s->b_langp accordingly.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002052 * Returns NULL if it's OK, an untranslated error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002053 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002054 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002055parse_spelllang(win_T *wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002056{
2057 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002058 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002059 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00002060 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002061 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002062 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002063 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002064 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002065 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002066 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002067 int len;
2068 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002069 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002070 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002071 char_u *use_region = NULL;
2072 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002073 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002074 int i, j;
2075 langp_T *lp, *lp2;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002076 static int recursive = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002077 char *ret_msg = NULL;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002078 char_u *spl_copy;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002079 bufref_T bufref;
2080
2081 set_bufref(&bufref, wp->w_buffer);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002082
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002083 // We don't want to do this recursively. May happen when a language is
2084 // not available and the SpellFileMissing autocommand opens a new buffer
2085 // in which 'spell' is set.
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002086 if (recursive)
2087 return NULL;
2088 recursive = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002089
2090 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002091 clear_midword(wp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002092
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002093 // Make a copy of 'spelllang', the SpellFileMissing autocommands may change
2094 // it under our fingers.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002095 spl_copy = vim_strsave(wp->w_s->b_p_spl);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002096 if (spl_copy == NULL)
2097 goto theend;
2098
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002099 wp->w_s->b_cjk = 0;
2100
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002101 // Loop over comma separated language names.
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002102 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002103 {
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002104 // Get one language name.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002105 copy_option_part(&splp, lang, MAXWLEN, ",");
Bram Moolenaar5482f332005-04-17 20:18:43 +00002106 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002107 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002108
Bram Moolenaarf154f3a2020-06-08 18:54:49 +02002109 if (!valid_spelllang(lang))
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002110 continue;
2111
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002112 if (STRCMP(lang, "cjk") == 0)
2113 {
2114 wp->w_s->b_cjk = 1;
2115 continue;
2116 }
2117
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002118 // If the name ends in ".spl" use it as the name of the spell file.
2119 // If there is a region name let "region" point to it and remove it
2120 // from the name.
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002121 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2122 {
2123 filename = TRUE;
2124
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002125 // Locate a region and remove it from the file name.
Bram Moolenaarb6356332005-07-18 21:40:44 +00002126 p = vim_strchr(gettail(lang), '_');
2127 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
2128 && !ASCII_ISALPHA(p[3]))
2129 {
2130 vim_strncpy(region_cp, p + 1, 2);
2131 mch_memmove(p, p + 3, len - (p - lang) - 2);
Bram Moolenaarb6356332005-07-18 21:40:44 +00002132 region = region_cp;
2133 }
2134 else
2135 dont_use_region = TRUE;
2136
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002137 // Check if we loaded this language before.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002138 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar99499b12019-05-23 21:35:48 +02002139 if (fullpathcmp(lang, slang->sl_fname, FALSE, TRUE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002140 break;
2141 }
2142 else
2143 {
2144 filename = FALSE;
2145 if (len > 3 && lang[len - 3] == '_')
2146 {
2147 region = lang + len - 2;
2148 len -= 3;
2149 lang[len] = NUL;
2150 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002151 else
2152 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002153
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002154 // Check if we loaded this language before.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002155 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002156 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002157 break;
2158 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002159
Bram Moolenaarb6356332005-07-18 21:40:44 +00002160 if (region != NULL)
2161 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002162 // If the region differs from what was used before then don't
2163 // use it for 'spellfile'.
Bram Moolenaarb6356332005-07-18 21:40:44 +00002164 if (use_region != NULL && STRCMP(region, use_region) != 0)
2165 dont_use_region = TRUE;
2166 use_region = region;
2167 }
2168
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002169 // If not found try loading the language now.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002170 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002171 {
2172 if (filename)
2173 (void)spell_load_file(lang, lang, NULL, FALSE);
2174 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002175 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002176 spell_load_lang(lang);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002177 // SpellFileMissing autocommands may do anything, including
Bram Moolenaarc3d27ad2022-11-14 20:52:14 +00002178 // destroying the buffer we are using or closing the window.
2179 if (!bufref_valid(&bufref) || !win_valid_any_tab(wp))
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002180 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002181 ret_msg = N_(e_spellfilemising_autocommand_deleted_buffer);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002182 goto theend;
2183 }
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002184 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002185 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002186
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002187 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002188 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002189 */
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002190 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar99499b12019-05-23 21:35:48 +02002191 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE, TRUE)
2192 == FPC_SAME
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002193 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002194 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002195 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002196 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002197 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002198 // find region in sl_regions
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002199 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002200 if (c == REGION_ALL)
2201 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002202 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002203 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002204 if (*slang->sl_regions != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002205 // This addition file is for other regions.
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002206 region_mask = 0;
2207 }
2208 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002209 // This is probably an error. Give a warning and
2210 // accept the words anyway.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002211 smsg(_("Warning: region %s not supported"),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002212 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002213 }
2214 else
2215 region_mask = 1 << c;
2216 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002217
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002218 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002219 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002220 if (ga_grow(&ga, 1) == FAIL)
2221 {
2222 ga_clear(&ga);
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002223 ret_msg = e_out_of_memory;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002224 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002225 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002226 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002227 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2228 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002229 use_midword(slang, wp);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002230 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002231 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002232 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002233 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002234 }
2235
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002236 // round 0: load int_wordlist, if possible.
2237 // round 1: load first name in 'spellfile'.
2238 // round 2: load second name in 'spellfile.
2239 // etc.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002240 spf = curwin->w_s->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002241 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002242 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002243 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002244 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002245 // Internal wordlist, if there is one.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002246 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002247 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002248 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002249 }
2250 else
2251 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002252 // One entry in 'spellfile'.
Milly322ad0c2024-10-14 20:21:48 +02002253 copy_option_part(&spf, spf_name, MAXPATHL - 4, ",");
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002254 STRCAT(spf_name, ".spl");
2255
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002256 // If it was already found above then skip it.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002257 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002258 {
2259 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
Bram Moolenaar99499b12019-05-23 21:35:48 +02002260 if (p != NULL && fullpathcmp(spf_name, p, FALSE, TRUE)
2261 == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002262 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002263 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002264 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002265 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002266 }
2267
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002268 // Check if it was loaded already.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002269 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar99499b12019-05-23 21:35:48 +02002270 if (fullpathcmp(spf_name, slang->sl_fname, FALSE, TRUE)
2271 == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002272 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002273 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002274 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002275 // Not loaded, try loading it now. The language name includes the
2276 // region name, the region is ignored otherwise. for int_wordlist
2277 // use an arbitrary name.
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002278 if (round == 0)
2279 STRCPY(lang, "internal wordlist");
2280 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00002281 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002282 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002283 p = vim_strchr(lang, '.');
2284 if (p != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002285 *p = NUL; // truncate at ".encoding.add"
Bram Moolenaar7887d882005-07-01 22:33:52 +00002286 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002287 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002288
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002289 // If one of the languages has NOBREAK we assume the addition
2290 // files also have this.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002291 if (slang != NULL && nobreak)
2292 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002293 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002294 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002295 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002296 region_mask = REGION_ALL;
2297 if (use_region != NULL && !dont_use_region)
2298 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002299 // find region in sl_regions
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002300 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002301 if (c != REGION_ALL)
2302 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002303 else if (*slang->sl_regions != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002304 // This spell file is for other regions.
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002305 region_mask = 0;
2306 }
2307
2308 if (region_mask != 0)
2309 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002310 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
2311 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
2312 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002313 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2314 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002315 use_midword(slang, wp);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002316 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002317 }
2318 }
2319
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002320 // Everything is fine, store the new b_langp value.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002321 ga_clear(&wp->w_s->b_langp);
2322 wp->w_s->b_langp = ga;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002323
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002324 // For each language figure out what language to use for sound folding and
2325 // REP items. If the language doesn't support it itself use another one
2326 // with the same name. E.g. for "en-math" use "en".
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002327 for (i = 0; i < ga.ga_len; ++i)
2328 {
2329 lp = LANGP_ENTRY(ga, i);
2330
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002331 // sound folding
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002332 if (lp->lp_slang->sl_sal.ga_len > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002333 // language does sound folding itself
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002334 lp->lp_sallang = lp->lp_slang;
2335 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002336 // find first similar language that does sound folding
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002337 for (j = 0; j < ga.ga_len; ++j)
2338 {
2339 lp2 = LANGP_ENTRY(ga, j);
2340 if (lp2->lp_slang->sl_sal.ga_len > 0
2341 && STRNCMP(lp->lp_slang->sl_name,
2342 lp2->lp_slang->sl_name, 2) == 0)
2343 {
2344 lp->lp_sallang = lp2->lp_slang;
2345 break;
2346 }
2347 }
2348
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002349 // REP items
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002350 if (lp->lp_slang->sl_rep.ga_len > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002351 // language has REP items itself
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002352 lp->lp_replang = lp->lp_slang;
2353 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002354 // find first similar language that has REP items
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002355 for (j = 0; j < ga.ga_len; ++j)
2356 {
2357 lp2 = LANGP_ENTRY(ga, j);
2358 if (lp2->lp_slang->sl_rep.ga_len > 0
2359 && STRNCMP(lp->lp_slang->sl_name,
2360 lp2->lp_slang->sl_name, 2) == 0)
2361 {
2362 lp->lp_replang = lp2->lp_slang;
2363 break;
2364 }
2365 }
2366 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002367 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002368
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002369theend:
2370 vim_free(spl_copy);
2371 recursive = FALSE;
2372 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002373}
2374
2375/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002376 * Clear the midword characters for buffer "buf".
2377 */
2378 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002379clear_midword(win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002380{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002381 CLEAR_FIELD(wp->w_s->b_spell_ismw);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002382 VIM_CLEAR(wp->w_s->b_spell_ismw_mb);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002383}
2384
2385/*
2386 * Use the "sl_midword" field of language "lp" for buffer "buf".
2387 * They add up to any currently used midword characters.
2388 */
2389 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002390use_midword(slang_T *lp, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002391{
2392 char_u *p;
2393
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002394 if (lp->sl_midword == NULL) // there aren't any
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002395 return;
2396
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002397 for (p = lp->sl_midword; *p != NUL; )
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002398 if (has_mbyte)
2399 {
2400 int c, l, n;
2401 char_u *bp;
2402
2403 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002404 l = (*mb_ptr2len)(p);
2405 if (c < 256 && l <= 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002406 wp->w_s->b_spell_ismw[c] = TRUE;
2407 else if (wp->w_s->b_spell_ismw_mb == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002408 // First multi-byte char in "b_spell_ismw_mb".
Bram Moolenaar860cae12010-06-05 23:22:07 +02002409 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002410 else
2411 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002412 // Append multi-byte chars to "b_spell_ismw_mb".
Bram Moolenaar860cae12010-06-05 23:22:07 +02002413 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
2414 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002415 if (bp != NULL)
2416 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002417 vim_free(wp->w_s->b_spell_ismw_mb);
2418 wp->w_s->b_spell_ismw_mb = bp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002419 vim_strncpy(bp + n, p, l);
2420 }
2421 }
2422 p += l;
2423 }
2424 else
Bram Moolenaar860cae12010-06-05 23:22:07 +02002425 wp->w_s->b_spell_ismw[*p++] = TRUE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002426}
2427
2428/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002429 * Find the region "region[2]" in "rp" (points to "sl_regions").
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01002430 * Each region is simply stored as the two characters of its name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002431 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002432 */
2433 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002434find_region(char_u *rp, char_u *region)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002435{
2436 int i;
2437
2438 for (i = 0; ; i += 2)
2439 {
2440 if (rp[i] == NUL)
2441 return REGION_ALL;
2442 if (rp[i] == region[0] && rp[i + 1] == region[1])
2443 break;
2444 }
2445 return i / 2;
2446}
2447
2448/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002449 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002450 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002451 * Word WF_ONECAP
2452 * W WORD WF_ALLCAP
2453 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002454 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002455 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002456captype(
2457 char_u *word,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002458 char_u *end) // When NULL use up to NUL byte.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002459{
2460 char_u *p;
2461 int c;
2462 int firstcap;
2463 int allcap;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002464 int past_second = FALSE; // past second word char
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002465
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002466 // find first letter
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002467 for (p = word; !spell_iswordp_nmw(p, curwin); MB_PTR_ADV(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002468 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002469 return 0; // only non-word characters, illegal word
Bram Moolenaarb765d632005-06-07 21:00:02 +00002470 if (has_mbyte)
2471 c = mb_ptr2char_adv(&p);
2472 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00002473 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002474 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002475
2476 /*
2477 * Need to check all letters to find a word with mixed upper/lower.
2478 * But a word with an upper char only at start is a ONECAP.
2479 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002480 for ( ; end == NULL ? *p != NUL : p < end; MB_PTR_ADV(p))
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002481 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002482 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002483 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002484 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002485 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002486 // UUl -> KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002487 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002488 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002489 allcap = FALSE;
2490 }
2491 else if (!allcap)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002492 // UlU -> KEEPCAP
Bram Moolenaar51485f02005-06-04 21:55:20 +00002493 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002494 past_second = TRUE;
2495 }
2496
2497 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002498 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002499 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002500 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002501 return 0;
2502}
2503
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002504/*
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002505 * Delete the internal wordlist and its .spl file.
2506 */
2507 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002508spell_delete_wordlist(void)
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002509{
2510 char_u fname[MAXPATHL];
2511
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002512 if (int_wordlist == NULL)
2513 return;
2514
2515 mch_remove(int_wordlist);
2516 int_wordlist_spl(fname);
2517 mch_remove(fname);
2518 VIM_CLEAR(int_wordlist);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002519}
2520
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002521/*
2522 * Free all languages.
2523 */
2524 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002525spell_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002526{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002527 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002528 buf_T *buf;
2529
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002530 // Go through all buffers and handle 'spelllang'. <VN>
Bram Moolenaar29323592016-07-24 22:04:11 +02002531 FOR_ALL_BUFFERS(buf)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002532 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002533
2534 while (first_lang != NULL)
2535 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002536 slang = first_lang;
2537 first_lang = slang->sl_next;
2538 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002539 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002540
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002541 spell_delete_wordlist();
Bram Moolenaar7887d882005-07-01 22:33:52 +00002542
Bram Moolenaard23a8232018-02-10 18:45:26 +01002543 VIM_CLEAR(repl_to);
2544 VIM_CLEAR(repl_from);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002545}
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002546
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002547/*
2548 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002549 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002550 */
2551 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002552spell_reload(void)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002553{
Bram Moolenaar3982c542005-06-08 21:56:31 +00002554 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002555
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002556 // Initialize the table for spell_iswordp().
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002557 init_spell_chartab();
2558
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002559 // Unload all allocated memory.
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002560 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002561
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002562 // Go through all buffers and handle 'spelllang'.
Bram Moolenaar29323592016-07-24 22:04:11 +02002563 FOR_ALL_WINDOWS(wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002564 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002565 // Only load the wordlists when 'spelllang' is set and there is a
2566 // window for this buffer in which 'spell' is set.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002567 if (*wp->w_s->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002568 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002569 if (wp->w_p_spell)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002570 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002571 (void)parse_spelllang(wp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00002572 break;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002573 }
2574 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002575 }
2576}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002577
Bram Moolenaarb765d632005-06-07 21:00:02 +00002578/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002579 * Open a spell buffer. This is a nameless buffer that is not in the buffer
2580 * list and only contains text lines. Can use a swapfile to reduce memory
2581 * use.
2582 * Most other fields are invalid! Esp. watch out for string options being
2583 * NULL and there is no undo info.
2584 * Returns NULL when out of memory.
2585 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002586 buf_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002587open_spellbuf(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002588{
2589 buf_T *buf;
2590
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002591 buf = ALLOC_CLEAR_ONE(buf_T);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002592 if (buf == NULL)
2593 return NULL;
2594
2595 buf->b_spell = TRUE;
2596 buf->b_p_swf = TRUE; // may create a swap file
Bram Moolenaar706d2de2013-07-17 17:35:13 +02002597#ifdef FEAT_CRYPT
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002598 buf->b_p_key = empty_option;
Bram Moolenaar706d2de2013-07-17 17:35:13 +02002599#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002600 ml_open(buf);
2601 ml_open_file(buf); // create swap file now
Bram Moolenaar4770d092006-01-12 23:22:24 +00002602 return buf;
2603}
2604
2605/*
2606 * Close the buffer used for spell info.
2607 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002608 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002609close_spellbuf(buf_T *buf)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002610{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002611 if (buf == NULL)
2612 return;
2613
2614 ml_close(buf, TRUE);
2615 vim_free(buf);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002616}
2617
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002618/*
2619 * Init the chartab used for spelling for ASCII.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002620 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002621 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002622clear_spell_chartab(spelltab_T *sp)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002623{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002624 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002625
Bram Moolenaara80faa82020-04-12 19:37:17 +02002626 // Init everything to FALSE (zero).
2627 CLEAR_FIELD(sp->st_isw);
2628 CLEAR_FIELD(sp->st_isu);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002629 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002630 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002631 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002632 sp->st_upper[i] = i;
2633 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002634
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002635 // We include digits. A word shouldn't start with a digit, but handling
2636 // that is done separately.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002637 for (i = '0'; i <= '9'; ++i)
2638 sp->st_isw[i] = TRUE;
2639 for (i = 'A'; i <= 'Z'; ++i)
2640 {
2641 sp->st_isw[i] = TRUE;
2642 sp->st_isu[i] = TRUE;
2643 sp->st_fold[i] = i + 0x20;
2644 }
2645 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002646 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002647 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002648 sp->st_upper[i] = i - 0x20;
2649 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002650}
2651
2652/*
2653 * Init the chartab used for spelling. Only depends on 'encoding'.
2654 * Called once while starting up and when 'encoding' changes.
2655 * The default is to use isalpha(), but the spell file should define the word
2656 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002657 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002658 */
2659 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002660init_spell_chartab(void)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002661{
2662 int i;
2663
2664 did_set_spelltab = FALSE;
2665 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002666 if (enc_dbcs)
2667 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002668 // DBCS: assume double-wide characters are word characters.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002669 for (i = 128; i <= 255; ++i)
2670 if (MB_BYTE2LEN(i) == 2)
2671 spelltab.st_isw[i] = TRUE;
2672 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002673 else if (enc_utf8)
2674 {
2675 for (i = 128; i < 256; ++i)
2676 {
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02002677 int f = utf_fold(i);
2678 int u = utf_toupper(i);
2679
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002680 spelltab.st_isu[i] = utf_isupper(i);
2681 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002682 // The folded/upper-cased value is different between latin1 and
2683 // utf8 for 0xb5, causing E763 for no good reason. Use the latin1
2684 // value for utf-8 to avoid this.
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02002685 spelltab.st_fold[i] = (f < 256) ? f : i;
2686 spelltab.st_upper[i] = (u < 256) ? u : i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002687 }
2688 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002689 else
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002690 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002691 // Rough guess: use locale-dependent library functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002692 for (i = 128; i < 256; ++i)
2693 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002694 if (MB_ISUPPER(i))
2695 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002696 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002697 spelltab.st_isu[i] = TRUE;
2698 spelltab.st_fold[i] = MB_TOLOWER(i);
2699 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002700 else if (MB_ISLOWER(i))
2701 {
2702 spelltab.st_isw[i] = TRUE;
2703 spelltab.st_upper[i] = MB_TOUPPER(i);
2704 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002705 }
2706 }
2707}
2708
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002709
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002710/*
Bram Moolenaarea408852005-06-25 22:49:46 +00002711 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002712 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00002713 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002714 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00002715 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02002716 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002717spell_iswordp(
2718 char_u *p,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002719 win_T *wp) // buffer used
Bram Moolenaarea408852005-06-25 22:49:46 +00002720{
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002721 char_u *s;
2722 int l;
2723 int c;
2724
2725 if (has_mbyte)
2726 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002727 l = mb_ptr2len(p);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002728 s = p;
2729 if (l == 1)
2730 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002731 // be quick for ASCII
Bram Moolenaar860cae12010-06-05 23:22:07 +02002732 if (wp->w_s->b_spell_ismw[*p])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002733 s = p + 1; // skip a mid-word character
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002734 }
2735 else
2736 {
2737 c = mb_ptr2char(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002738 if (c < 256 ? wp->w_s->b_spell_ismw[c]
2739 : (wp->w_s->b_spell_ismw_mb != NULL
2740 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002741 s = p + l;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002742 }
2743
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002744 c = mb_ptr2char(s);
2745 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002746 return spell_mb_isword_class(mb_get_class(s), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002747 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002748 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002749
Bram Moolenaar860cae12010-06-05 23:22:07 +02002750 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002751}
2752
2753/*
2754 * Return TRUE if "p" points to a word character.
2755 * Unlike spell_iswordp() this doesn't check for "midword" characters.
2756 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02002757 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002758spell_iswordp_nmw(char_u *p, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002759{
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002760 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002761
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002762 if (has_mbyte)
2763 {
2764 c = mb_ptr2char(p);
2765 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002766 return spell_mb_isword_class(mb_get_class(p), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002767 return spelltab.st_isw[c];
2768 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002769 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00002770}
2771
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002772/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002773 * Return TRUE if word class indicates a word character.
2774 * Only for characters above 255.
2775 * Unicode subscript and superscript are not considered word characters.
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002776 * See also dbcs_class() and utf_class() in mbyte.c.
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002777 */
2778 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002779spell_mb_isword_class(int cl, win_T *wp)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002780{
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002781 if (wp->w_s->b_cjk)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002782 // East Asian characters are not considered word characters.
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002783 return cl == 2 || cl == 0x2800;
Bram Moolenaar06e63772019-07-19 23:04:34 +02002784 return cl >= 2 && cl != 0x2070 && cl != 0x2080 && cl != 3;
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00002785}
2786
2787/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002788 * Return TRUE if "p" points to a word character.
2789 * Wide version of spell_iswordp().
2790 */
2791 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002792spell_iswordp_w(int *p, win_T *wp)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002793{
2794 int *s;
2795
Bram Moolenaar860cae12010-06-05 23:22:07 +02002796 if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
2797 : (wp->w_s->b_spell_ismw_mb != NULL
2798 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002799 s = p + 1;
2800 else
2801 s = p;
2802
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002803 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002804 {
2805 if (enc_utf8)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002806 return spell_mb_isword_class(utf_class(*s), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002807 if (enc_dbcs)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002808 return spell_mb_isword_class(
2809 dbcs_class((unsigned)*s >> 8, *s & 0xff), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002810 return 0;
2811 }
2812 return spelltab.st_isw[*s];
2813}
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002814
Bram Moolenaarea408852005-06-25 22:49:46 +00002815/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002816 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
2817 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002818 * When using a multi-byte 'encoding' the length may change!
2819 * Returns FAIL when something wrong.
2820 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002821 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002822spell_casefold(
Bram Moolenaar4f135272021-06-11 19:07:40 +02002823 win_T *wp,
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002824 char_u *str,
2825 int len,
2826 char_u *buf,
2827 int buflen)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002828{
2829 int i;
2830
2831 if (len >= buflen)
2832 {
2833 buf[0] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002834 return FAIL; // result will not fit
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002835 }
2836
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002837 if (has_mbyte)
2838 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002839 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002840 char_u *p;
2841 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002842
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002843 // Fold one character at a time.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002844 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002845 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002846 if (outi + MB_MAXBYTES > buflen)
2847 {
2848 buf[outi] = NUL;
2849 return FAIL;
2850 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002851 c = mb_cptr2char_adv(&p);
Bram Moolenaar4f135272021-06-11 19:07:40 +02002852
2853 // Exception: greek capital sigma 0x03A3 folds to 0x03C3, except
2854 // when it is the last character in a word, then it folds to
2855 // 0x03C2.
2856 if (c == 0x03a3 || c == 0x03c2)
2857 {
2858 if (p == str + len || !spell_iswordp(p, wp))
2859 c = 0x03c2;
2860 else
2861 c = 0x03c3;
2862 }
2863 else
2864 c = SPELL_TOFOLD(c);
2865
2866 outi += mb_char2bytes(c, buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002867 }
2868 buf[outi] = NUL;
2869 }
2870 else
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002871 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002872 // Be quick for non-multibyte encodings.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002873 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002874 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002875 buf[i] = NUL;
2876 }
2877
2878 return OK;
2879}
2880
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002881/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002882 * Check if the word at line "lnum" column "col" is required to start with a
Luuk van Baal2ac64972023-05-25 17:14:42 +01002883 * capital. This uses 'spellcapcheck' of the buffer in window "wp".
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002884 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02002885 int
Luuk van Baal2ac64972023-05-25 17:14:42 +01002886check_need_cap(win_T *wp, linenr_T lnum, colnr_T col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002887{
Luuk van Baal2ac64972023-05-25 17:14:42 +01002888 if (wp->w_s->b_cap_prog == NULL)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002889 return FALSE;
2890
Luuk van Baal2ac64972023-05-25 17:14:42 +01002891 int need_cap = FALSE;
2892 char_u *line = col ? ml_get_buf(wp->w_buffer, lnum, FALSE) : NULL;
2893 char_u *line_copy = NULL;
2894 colnr_T endcol = 0;
2895
2896 if (col == 0 || getwhitecols(line) >= col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002897 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002898 // At start of line, check if previous line is empty or sentence
2899 // ends there.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002900 if (lnum == 1)
2901 need_cap = TRUE;
2902 else
2903 {
Luuk van Baal2ac64972023-05-25 17:14:42 +01002904 line = ml_get_buf(wp->w_buffer, lnum - 1, FALSE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002905 if (*skipwhite(line) == NUL)
2906 need_cap = TRUE;
2907 else
2908 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002909 // Append a space in place of the line break.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002910 line_copy = concat_str(line, (char_u *)" ");
Luuk van Baal2ac64972023-05-25 17:14:42 +01002911 if (line_copy == NULL)
2912 return FALSE;
2913
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002914 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002915 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002916 }
2917 }
2918 }
2919 else
2920 endcol = col;
2921
2922 if (endcol > 0)
2923 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002924 // Check if sentence ends before the bad word.
Luuk van Baal2ac64972023-05-25 17:14:42 +01002925 regmatch_T regmatch;
2926 regmatch.regprog = wp->w_s->b_cap_prog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002927 regmatch.rm_ic = FALSE;
Luuk van Baal2ac64972023-05-25 17:14:42 +01002928 char_u *p = line + endcol;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002929 for (;;)
2930 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002931 MB_PTR_BACK(line, p);
Luuk van Baal2ac64972023-05-25 17:14:42 +01002932 if (p == line || spell_iswordp_nmw(p, wp))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002933 break;
2934 if (vim_regexec(&regmatch, p, 0)
2935 && regmatch.endp[0] == line + endcol)
2936 {
2937 need_cap = TRUE;
2938 break;
2939 }
2940 }
Luuk van Baal2ac64972023-05-25 17:14:42 +01002941 wp->w_s->b_cap_prog = regmatch.regprog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002942 }
2943
2944 vim_free(line_copy);
2945
2946 return need_cap;
2947}
2948
2949
2950/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002951 * ":spellrepall"
2952 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002953 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002954ex_spellrepall(exarg_T *eap UNUSED)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002955{
2956 pos_T pos = curwin->w_cursor;
2957 char_u *frompat;
John Marriott8c85a2a2024-05-20 19:18:26 +02002958 size_t frompatlen;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002959 char_u *line;
2960 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002961 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002962 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002963
2964 if (repl_from == NULL || repl_to == NULL)
2965 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002966 emsg(_(e_no_previous_spell_replacement));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002967 return;
2968 }
zeertzjq59f70382023-06-06 15:59:59 +01002969 size_t repl_from_len = STRLEN(repl_from);
2970 size_t repl_to_len = STRLEN(repl_to);
2971 int addlen = (int)(repl_to_len - repl_from_len);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002972
zeertzjq59f70382023-06-06 15:59:59 +01002973 frompat = alloc(repl_from_len + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002974 if (frompat == NULL)
2975 return;
John Marriott8c85a2a2024-05-20 19:18:26 +02002976 frompatlen = vim_snprintf((char *)frompat, repl_from_len + 7, "\\V\\<%s\\>", repl_from);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002977 p_ws = FALSE;
2978
Bram Moolenaar5195e452005-08-19 20:32:47 +00002979 sub_nsubs = 0;
2980 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002981 curwin->w_cursor.lnum = 0;
2982 while (!got_int)
2983 {
John Marriott8c85a2a2024-05-20 19:18:26 +02002984 if (do_search(NULL, '/', '/', frompat, frompatlen, 1L, SEARCH_KEEP, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002985 || u_save_cursor() == FAIL)
2986 break;
2987
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002988 // Only replace when the right word isn't there yet. This happens
2989 // when changing "etc" to "etc.".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002990 line = ml_get_curline();
2991 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
zeertzjq59f70382023-06-06 15:59:59 +01002992 repl_to, repl_to_len) != 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002993 {
zeertzjq94b7c322024-03-12 21:50:32 +01002994 p = alloc(ml_get_curline_len() + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002995 if (p == NULL)
2996 break;
2997 mch_memmove(p, line, curwin->w_cursor.col);
2998 STRCPY(p + curwin->w_cursor.col, repl_to);
zeertzjq59f70382023-06-06 15:59:59 +01002999 STRCAT(p, line + curwin->w_cursor.col + repl_from_len);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003000 ml_replace(curwin->w_cursor.lnum, p, FALSE);
3001 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Martin Tournoijba43e762022-10-13 22:12:15 +01003002#if defined(FEAT_PROP_POPUP)
LemonBoyb7a70122022-05-13 12:41:50 +01003003 if (curbuf->b_has_textprop && addlen != 0)
3004 adjust_prop_columns(curwin->w_cursor.lnum,
3005 curwin->w_cursor.col, addlen, APC_SUBSTITUTE);
Martin Tournoijba43e762022-10-13 22:12:15 +01003006#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00003007
3008 if (curwin->w_cursor.lnum != prev_lnum)
3009 {
3010 ++sub_nlines;
3011 prev_lnum = curwin->w_cursor.lnum;
3012 }
3013 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003014 }
zeertzjq59f70382023-06-06 15:59:59 +01003015 curwin->w_cursor.col += (colnr_T)repl_to_len;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003016 }
3017
3018 p_ws = save_ws;
3019 curwin->w_cursor = pos;
3020 vim_free(frompat);
3021
Bram Moolenaar5195e452005-08-19 20:32:47 +00003022 if (sub_nsubs == 0)
Bram Moolenaar677658a2022-01-05 16:09:06 +00003023 semsg(_(e_not_found_str), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003024 else
3025 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003026}
3027
3028/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003029 * Make a copy of "word", with the first letter upper or lower cased, to
3030 * "wcopy[MAXWLEN]". "word" must not be empty.
3031 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003032 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003033 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003034onecap_copy(
3035 char_u *word,
3036 char_u *wcopy,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003037 int upper) // TRUE: first letter made upper case
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003038{
3039 char_u *p;
3040 int c;
3041 int l;
3042
3043 p = word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003044 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003045 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003046 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003047 c = *p++;
3048 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003049 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003050 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003051 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003052 if (has_mbyte)
3053 l = mb_char2bytes(c, wcopy);
3054 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003055 {
3056 l = 1;
3057 wcopy[0] = c;
3058 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003059 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003060}
3061
3062/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003063 * Make a copy of "word" with all the letters upper cased into
3064 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003065 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02003066 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003067allcap_copy(char_u *word, char_u *wcopy)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003068{
3069 char_u *s;
3070 char_u *d;
3071 int c;
3072
3073 d = wcopy;
3074 for (s = word; *s != NUL; )
3075 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003076 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003077 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003078 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003079 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +00003080
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003081 // We only change 0xdf to SS when we are certain latin1 is used. It
3082 // would cause weird errors in other 8-bit encodings.
Bram Moolenaar78622822005-08-23 21:00:13 +00003083 if (enc_latin1like && c == 0xdf)
3084 {
3085 c = 'S';
3086 if (d - wcopy >= MAXWLEN - 1)
3087 break;
3088 *d++ = c;
3089 }
3090 else
Bram Moolenaar78622822005-08-23 21:00:13 +00003091 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003092
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003093 if (has_mbyte)
3094 {
3095 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
3096 break;
3097 d += mb_char2bytes(c, d);
3098 }
3099 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003100 {
3101 if (d - wcopy >= MAXWLEN - 1)
3102 break;
3103 *d++ = c;
3104 }
3105 }
3106 *d = NUL;
3107}
3108
3109/*
Bram Moolenaar53805d12005-08-01 07:08:33 +00003110 * Case-folding may change the number of bytes: Count nr of chars in
3111 * fword[flen] and return the byte length of that many chars in "word".
3112 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02003113 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003114nofold_len(char_u *fword, int flen, char_u *word)
Bram Moolenaar53805d12005-08-01 07:08:33 +00003115{
3116 char_u *p;
3117 int i = 0;
3118
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003119 for (p = fword; p < fword + flen; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00003120 ++i;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003121 for (p = word; i > 0; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00003122 --i;
3123 return (int)(p - word);
3124}
Bram Moolenaar53805d12005-08-01 07:08:33 +00003125
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003126/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003127 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003128 */
Bram Moolenaar46a426c2019-09-27 12:41:56 +02003129 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003130make_case_word(char_u *fword, char_u *cword, int flags)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003131{
3132 if (flags & WF_ALLCAP)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003133 // Make it all upper-case
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003134 allcap_copy(fword, cword);
3135 else if (flags & WF_ONECAP)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003136 // Make the first letter upper-case
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003137 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003138 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003139 // Use goodword as-is.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003140 STRCPY(cword, fword);
3141}
3142
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003143#if defined(FEAT_EVAL) || defined(PROTO)
3144/*
3145 * Soundfold a string, for soundfold().
3146 * Result is in allocated memory, NULL for an error.
3147 */
3148 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003149eval_soundfold(char_u *word)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003150{
3151 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003152 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003153 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003154
Bram Moolenaar860cae12010-06-05 23:22:07 +02003155 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003156 // Use the sound-folding of the first language that supports it.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003157 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003158 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003159 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003160 if (lp->lp_slang->sl_sal.ga_len > 0)
3161 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003162 // soundfold the word
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003163 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003164 return vim_strsave(sound);
3165 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003166 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003167
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003168 // No language with sound folding, return word as-is.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003169 return vim_strsave(word);
3170}
3171#endif
3172
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003173/*
3174 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +00003175 *
3176 * There are many ways to turn a word into a sound-a-like representation. The
3177 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
3178 * swedish name matching - survey and test of different algorithms" by Klas
3179 * Erikson.
3180 *
3181 * We support two methods:
3182 * 1. SOFOFROM/SOFOTO do a simple character mapping.
3183 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003184 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003185 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003186spell_soundfold(
3187 slang_T *slang,
3188 char_u *inword,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003189 int folded, // "inword" is already case-folded
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003190 char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003191{
3192 char_u fword[MAXWLEN];
3193 char_u *word;
3194
3195 if (slang->sl_sofo)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003196 // SOFOFROM and SOFOTO used
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003197 spell_soundfold_sofo(slang, inword, res);
3198 else
3199 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003200 // SAL items used. Requires the word to be case-folded.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003201 if (folded)
3202 word = inword;
3203 else
3204 {
Bram Moolenaar4f135272021-06-11 19:07:40 +02003205 (void)spell_casefold(curwin,
3206 inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003207 word = fword;
3208 }
3209
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003210 if (has_mbyte)
3211 spell_soundfold_wsal(slang, word, res);
3212 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003213 spell_soundfold_sal(slang, word, res);
3214 }
3215}
3216
3217/*
3218 * Perform sound folding of "inword" into "res" according to SOFOFROM and
3219 * SOFOTO lines.
3220 */
3221 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003222spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003223{
3224 char_u *s;
3225 int ri = 0;
3226 int c;
3227
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003228 if (has_mbyte)
3229 {
3230 int prevc = 0;
3231 int *ip;
3232
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003233 // The sl_sal_first[] table contains the translation for chars up to
3234 // 255, sl_sal the rest.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003235 for (s = inword; *s != NUL; )
3236 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003237 c = mb_cptr2char_adv(&s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003238 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003239 c = ' ';
3240 else if (c < 256)
3241 c = slang->sl_sal_first[c];
3242 else
3243 {
3244 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003245 if (ip == NULL) // empty list, can't match
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003246 c = NUL;
3247 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003248 for (;;) // find "c" in the list
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003249 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003250 if (*ip == 0) // not found
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003251 {
3252 c = NUL;
3253 break;
3254 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003255 if (*ip == c) // match!
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003256 {
3257 c = ip[1];
3258 break;
3259 }
3260 ip += 2;
3261 }
3262 }
3263
3264 if (c != NUL && c != prevc)
3265 {
3266 ri += mb_char2bytes(c, res + ri);
3267 if (ri + MB_MAXBYTES > MAXWLEN)
3268 break;
3269 prevc = c;
3270 }
3271 }
3272 }
3273 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003274 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003275 // The sl_sal_first[] table contains the translation.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003276 for (s = inword; (c = *s) != NUL; ++s)
3277 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003278 if (VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003279 c = ' ';
3280 else
3281 c = slang->sl_sal_first[c];
3282 if (c != NUL && (ri == 0 || res[ri - 1] != c))
3283 res[ri++] = c;
3284 }
3285 }
3286
3287 res[ri] = NUL;
3288}
3289
3290 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003291spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003292{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003293 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003294 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003295 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003296 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003297 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003298 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003299 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003300 int n, k = 0;
3301 int z0;
3302 int k0;
3303 int n0;
3304 int c;
3305 int pri;
3306 int p0 = -333;
3307 int c0;
3308
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003309 // Remove accents, if wanted. We actually remove all non-word characters.
3310 // But keep white space. We need a copy, the word may be changed here.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003311 if (slang->sl_rem_accents)
3312 {
3313 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003314 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003315 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003316 if (VIM_ISWHITE(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003317 {
3318 *t++ = ' ';
3319 s = skipwhite(s);
3320 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003321 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003322 {
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003323 if (spell_iswordp_nmw(s, curwin))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003324 *t++ = *s;
3325 ++s;
3326 }
3327 }
3328 *t = NUL;
3329 }
3330 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003331 vim_strncpy(word, s, MAXWLEN - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003332
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003333 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003334
3335 /*
3336 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003337 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003338 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003339 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003340 while ((c = word[i]) != NUL)
3341 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003342 // Start with the first rule that has the character in the word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003343 n = slang->sl_sal_first[c];
3344 z0 = 0;
3345
3346 if (n >= 0)
3347 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003348 // check all rules for the same letter
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003349 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003350 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003351 // Quickly skip entries that don't match the word. Most
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003352 // entries are less than three chars, optimize for that.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003353 k = smp[n].sm_leadlen;
3354 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003355 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003356 if (word[i + 1] != s[1])
3357 continue;
3358 if (k > 2)
3359 {
3360 for (j = 2; j < k; ++j)
3361 if (word[i + j] != s[j])
3362 break;
3363 if (j < k)
3364 continue;
3365 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003366 }
3367
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003368 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003369 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003370 // Check for match with one of the chars in "sm_oneof".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003371 while (*pf != NUL && *pf != word[i + k])
3372 ++pf;
3373 if (*pf == NUL)
3374 continue;
3375 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003376 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003377 s = smp[n].sm_rules;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003378 pri = 5; // default priority
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003379
3380 p0 = *s;
3381 k0 = k;
3382 while (*s == '-' && k > 1)
3383 {
3384 k--;
3385 s++;
3386 }
3387 if (*s == '<')
3388 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003389 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003390 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003391 // determine priority
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003392 pri = *s - '0';
3393 s++;
3394 }
3395 if (*s == '^' && *(s + 1) == '^')
3396 s++;
3397
3398 if (*s == NUL
3399 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003400 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02003401 || spell_iswordp(word + i - 1, curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003402 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003403 || (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003404 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02003405 && spell_iswordp(word + i - 1, curwin)
3406 && (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003407 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003408 // search for followup rules, if:
3409 // followup and k > 1 and NO '-' in searchstring
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003410 c0 = word[i + k - 1];
3411 n0 = slang->sl_sal_first[c0];
3412
3413 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003414 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003415 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003416 // test follow-up rule for "word[i + k]"
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003417 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003418 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003419 // Quickly skip entries that don't match the word.
3420 //
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003421 k0 = smp[n0].sm_leadlen;
3422 if (k0 > 1)
3423 {
3424 if (word[i + k] != s[1])
3425 continue;
3426 if (k0 > 2)
3427 {
3428 pf = word + i + k + 1;
3429 for (j = 2; j < k0; ++j)
3430 if (*pf++ != s[j])
3431 break;
3432 if (j < k0)
3433 continue;
3434 }
3435 }
3436 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003437
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003438 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003439 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003440 // Check for match with one of the chars in
3441 // "sm_oneof".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003442 while (*pf != NUL && *pf != word[i + k0])
3443 ++pf;
3444 if (*pf == NUL)
3445 continue;
3446 ++k0;
3447 }
3448
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003449 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003450 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003451 while (*s == '-')
3452 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003453 // "k0" gets NOT reduced because
3454 // "if (k0 == k)"
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003455 s++;
3456 }
3457 if (*s == '<')
3458 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003459 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003460 {
3461 p0 = *s - '0';
3462 s++;
3463 }
3464
3465 if (*s == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003466 // *s == '^' cuts
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003467 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003468 && !spell_iswordp(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02003469 curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003470 {
3471 if (k0 == k)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003472 // this is just a piece of the string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003473 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003474
3475 if (p0 < pri)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003476 // priority too low
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003477 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003478 // rule fits; stop search
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003479 break;
3480 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003481 }
3482
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003483 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003484 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003485 }
3486
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003487 // replace string
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003488 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003489 if (s == NULL)
3490 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003491 pf = smp[n].sm_rules;
3492 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003493 if (p0 == 1 && z == 0)
3494 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003495 // rule with '<' is used
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003496 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
3497 || res[reslen - 1] == *s))
3498 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003499 z0 = 1;
3500 z = 1;
3501 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003502 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003503 {
3504 word[i + k0] = *s;
3505 k0++;
3506 s++;
3507 }
3508 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +00003509 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003510
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003511 // new "actual letter"
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003512 c = word[i];
3513 }
3514 else
3515 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003516 // no '<' rule used
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003517 i += k - 1;
3518 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003519 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003520 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003521 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003522 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003523 s++;
3524 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003525 // new "actual letter"
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003526 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003527 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003528 {
3529 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003530 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003531 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003532 i = 0;
3533 z0 = 1;
3534 }
3535 }
3536 break;
3537 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003538 }
3539 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01003540 else if (VIM_ISWHITE(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003541 {
3542 c = ' ';
3543 k = 1;
3544 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003545
3546 if (z0 == 0)
3547 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003548 if (k && !p0 && reslen < MAXWLEN && c != NUL
3549 && (!slang->sl_collapse || reslen == 0
3550 || res[reslen - 1] != c))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003551 // condense only double letters
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003552 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003553
3554 i++;
3555 z = 0;
3556 k = 0;
3557 }
3558 }
3559
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003560 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003561}
3562
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003563/*
3564 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
3565 * Multi-byte version of spell_soundfold().
3566 */
3567 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003568spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003569{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003570 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003571 int word[MAXWLEN];
3572 int wres[MAXWLEN];
3573 int l;
3574 char_u *s;
3575 int *ws;
3576 char_u *t;
3577 int *pf;
3578 int i, j, z;
3579 int reslen;
3580 int n, k = 0;
3581 int z0;
3582 int k0;
3583 int n0;
3584 int c;
3585 int pri;
3586 int p0 = -333;
3587 int c0;
3588 int did_white = FALSE;
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003589 int wordlen;
3590
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003591
3592 /*
3593 * Convert the multi-byte string to a wide-character string.
3594 * Remove accents, if wanted. We actually remove all non-word characters.
3595 * But keep white space.
3596 */
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003597 wordlen = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003598 for (s = inword; *s != NUL; )
3599 {
3600 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003601 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003602 if (slang->sl_rem_accents)
3603 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003604 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003605 {
3606 if (did_white)
3607 continue;
3608 c = ' ';
3609 did_white = TRUE;
3610 }
3611 else
3612 {
3613 did_white = FALSE;
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003614 if (!spell_iswordp_nmw(t, curwin))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003615 continue;
3616 }
3617 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003618 word[wordlen++] = c;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003619 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003620 word[wordlen] = NUL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003621
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003622 /*
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003623 * This algorithm comes from Aspell phonet.cpp.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003624 * Converted from C++ to C. Added support for multi-byte chars.
3625 * Changed to keep spaces.
3626 */
3627 i = reslen = z = 0;
3628 while ((c = word[i]) != NUL)
3629 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003630 // Start with the first rule that has the character in the word.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003631 n = slang->sl_sal_first[c & 0xff];
3632 z0 = 0;
3633
3634 if (n >= 0)
3635 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003636 // Check all rules for the same index byte.
3637 // If c is 0x300 need extra check for the end of the array, as
3638 // (c & 0xff) is NUL.
Bram Moolenaar95e85792010-08-01 15:37:02 +02003639 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff)
3640 && ws[0] != NUL; ++n)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003641 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003642 // Quickly skip entries that don't match the word. Most
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003643 // entries are less than three chars, optimize for that.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003644 if (c != ws[0])
3645 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003646 k = smp[n].sm_leadlen;
3647 if (k > 1)
3648 {
3649 if (word[i + 1] != ws[1])
3650 continue;
3651 if (k > 2)
3652 {
3653 for (j = 2; j < k; ++j)
3654 if (word[i + j] != ws[j])
3655 break;
3656 if (j < k)
3657 continue;
3658 }
3659 }
3660
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003661 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003662 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003663 // Check for match with one of the chars in "sm_oneof".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003664 while (*pf != NUL && *pf != word[i + k])
3665 ++pf;
3666 if (*pf == NUL)
3667 continue;
3668 ++k;
3669 }
3670 s = smp[n].sm_rules;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003671 pri = 5; // default priority
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003672
3673 p0 = *s;
3674 k0 = k;
3675 while (*s == '-' && k > 1)
3676 {
3677 k--;
3678 s++;
3679 }
3680 if (*s == '<')
3681 s++;
3682 if (VIM_ISDIGIT(*s))
3683 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003684 // determine priority
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003685 pri = *s - '0';
3686 s++;
3687 }
3688 if (*s == '^' && *(s + 1) == '^')
3689 s++;
3690
3691 if (*s == NUL
3692 || (*s == '^'
3693 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02003694 || spell_iswordp_w(word + i - 1, curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003695 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003696 || (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003697 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02003698 && spell_iswordp_w(word + i - 1, curwin)
3699 && (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003700 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003701 // search for followup rules, if:
3702 // followup and k > 1 and NO '-' in searchstring
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003703 c0 = word[i + k - 1];
3704 n0 = slang->sl_sal_first[c0 & 0xff];
3705
3706 if (slang->sl_followup && k > 1 && n0 >= 0
3707 && p0 != '-' && word[i + k] != NUL)
3708 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003709 // Test follow-up rule for "word[i + k]"; loop over
3710 // all entries with the same index byte.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003711 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
3712 == (c0 & 0xff); ++n0)
3713 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003714 // Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003715 if (c0 != ws[0])
3716 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003717 k0 = smp[n0].sm_leadlen;
3718 if (k0 > 1)
3719 {
3720 if (word[i + k] != ws[1])
3721 continue;
3722 if (k0 > 2)
3723 {
3724 pf = word + i + k + 1;
3725 for (j = 2; j < k0; ++j)
3726 if (*pf++ != ws[j])
3727 break;
3728 if (j < k0)
3729 continue;
3730 }
3731 }
3732 k0 += k - 1;
3733
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003734 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003735 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003736 // Check for match with one of the chars in
3737 // "sm_oneof".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003738 while (*pf != NUL && *pf != word[i + k0])
3739 ++pf;
3740 if (*pf == NUL)
3741 continue;
3742 ++k0;
3743 }
3744
3745 p0 = 5;
3746 s = smp[n0].sm_rules;
3747 while (*s == '-')
3748 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003749 // "k0" gets NOT reduced because
3750 // "if (k0 == k)"
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003751 s++;
3752 }
3753 if (*s == '<')
3754 s++;
3755 if (VIM_ISDIGIT(*s))
3756 {
3757 p0 = *s - '0';
3758 s++;
3759 }
3760
3761 if (*s == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003762 // *s == '^' cuts
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003763 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003764 && !spell_iswordp_w(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02003765 curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003766 {
3767 if (k0 == k)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003768 // this is just a piece of the string
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003769 continue;
3770
3771 if (p0 < pri)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003772 // priority too low
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003773 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003774 // rule fits; stop search
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003775 break;
3776 }
3777 }
3778
3779 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
3780 == (c0 & 0xff))
3781 continue;
3782 }
3783
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003784 // replace string
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003785 ws = smp[n].sm_to_w;
3786 s = smp[n].sm_rules;
3787 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
3788 if (p0 == 1 && z == 0)
3789 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003790 // rule with '<' is used
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003791 if (reslen > 0 && ws != NULL && *ws != NUL
3792 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003793 || wres[reslen - 1] == *ws))
3794 reslen--;
3795 z0 = 1;
3796 z = 1;
3797 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003798 if (ws != NULL)
3799 while (*ws != NUL && word[i + k0] != NUL)
3800 {
3801 word[i + k0] = *ws;
3802 k0++;
3803 ws++;
3804 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003805 if (k > k0)
3806 mch_memmove(word + i + k0, word + i + k,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003807 sizeof(int) * (wordlen - (i + k) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003808
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003809 // new "actual letter"
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003810 c = word[i];
3811 }
3812 else
3813 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003814 // no '<' rule used
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003815 i += k - 1;
3816 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003817 if (ws != NULL)
3818 while (*ws != NUL && ws[1] != NUL
3819 && reslen < MAXWLEN)
3820 {
3821 if (reslen == 0 || wres[reslen - 1] != *ws)
3822 wres[reslen++] = *ws;
3823 ws++;
3824 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003825 // new "actual letter"
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003826 if (ws == NULL)
3827 c = NUL;
3828 else
3829 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003830 if (strstr((char *)s, "^^") != NULL)
3831 {
Zdenek Dohnal39a94d22024-12-04 20:16:17 +01003832 if (c != NUL && reslen < MAXWLEN)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003833 wres[reslen++] = c;
3834 mch_memmove(word, word + i + 1,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02003835 sizeof(int) * (wordlen - (i + 1) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003836 i = 0;
3837 z0 = 1;
3838 }
3839 }
3840 break;
3841 }
3842 }
3843 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01003844 else if (VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003845 {
3846 c = ' ';
3847 k = 1;
3848 }
3849
3850 if (z0 == 0)
3851 {
3852 if (k && !p0 && reslen < MAXWLEN && c != NUL
3853 && (!slang->sl_collapse || reslen == 0
3854 || wres[reslen - 1] != c))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003855 // condense only double letters
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003856 wres[reslen++] = c;
3857
3858 i++;
3859 z = 0;
3860 k = 0;
3861 }
3862 }
3863
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003864 // Convert wide characters in "wres" to a multi-byte string in "res".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003865 l = 0;
3866 for (n = 0; n < reslen; ++n)
3867 {
3868 l += mb_char2bytes(wres[n], res + l);
3869 if (l + MB_MAXBYTES > MAXWLEN)
3870 break;
3871 }
3872 res[l] = NUL;
3873}
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003874
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003875/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003876 * ":spellinfo"
3877 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003878 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003879ex_spellinfo(exarg_T *eap UNUSED)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003880{
3881 int lpi;
3882 langp_T *lp;
3883 char_u *p;
3884
3885 if (no_spell_checking(curwin))
3886 return;
3887
3888 msg_start();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003889 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003890 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003891 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003892 msg_puts("file: ");
3893 msg_puts((char *)lp->lp_slang->sl_fname);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003894 msg_putchar('\n');
3895 p = lp->lp_slang->sl_info;
3896 if (p != NULL)
3897 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003898 msg_puts((char *)p);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003899 msg_putchar('\n');
3900 }
3901 }
3902 msg_end();
3903}
3904
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003905#define DUMPFLAG_KEEPCASE 1 // round 2: keep-case tree
3906#define DUMPFLAG_COUNT 2 // include word count
3907#define DUMPFLAG_ICASE 4 // ignore case when finding matches
3908#define DUMPFLAG_ONECAP 8 // pattern starts with capital
3909#define DUMPFLAG_ALLCAP 16 // pattern is all capitals
Bram Moolenaar4770d092006-01-12 23:22:24 +00003910
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003911/*
3912 * ":spelldump"
3913 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003914 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003915ex_spelldump(exarg_T *eap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003916{
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003917 char_u *spl;
3918 long dummy;
3919
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003920 if (no_spell_checking(curwin))
3921 return;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003922 (void)get_option_value((char_u*)"spl", &dummy, &spl, NULL, OPT_LOCAL);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003923
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003924 // Create a new empty buffer in a new window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003925 do_cmdline_cmd((char_u *)"new");
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003926
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003927 // enable spelling locally in the new window
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003928 set_option_value_give_err((char_u*)"spell", TRUE, (char_u*)"", OPT_LOCAL);
3929 set_option_value_give_err((char_u*)"spl", dummy, spl, OPT_LOCAL);
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02003930 vim_free(spl);
3931
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003932 if (!BUFEMPTY())
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003933 return;
3934
Bram Moolenaar860cae12010-06-05 23:22:07 +02003935 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003936
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003937 // Delete the empty line that we started with.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003938 if (curbuf->b_ml.ml_line_count > 1)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003939 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003940
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003941 redraw_later(UPD_NOT_VALID);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003942}
3943
3944/*
3945 * Go through all possible words and:
3946 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
3947 * "ic" and "dir" are not used.
3948 * 2. When "pat" is not NULL: add matching words to insert mode completion.
3949 */
3950 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003951spell_dump_compl(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003952 char_u *pat, // leading part of the word
3953 int ic, // ignore case
3954 int *dir, // direction for adding matches
3955 int dumpflags_arg) // DUMPFLAG_*
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003956{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003957 langp_T *lp;
3958 slang_T *slang;
3959 idx_T arridx[MAXWLEN];
3960 int curi[MAXWLEN];
3961 char_u word[MAXWLEN];
3962 int c;
3963 char_u *byts;
3964 idx_T *idxs;
3965 linenr_T lnum = 0;
3966 int round;
3967 int depth;
3968 int n;
3969 int flags;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003970 char_u *region_names = NULL; // region names being used
3971 int do_region = TRUE; // dump region names and numbers
Bram Moolenaar7887d882005-07-01 22:33:52 +00003972 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003973 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00003974 int dumpflags = dumpflags_arg;
3975 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003976
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003977 // When ignoring case or when the pattern starts with capital pass this on
3978 // to dump_word().
Bram Moolenaard0131a82006-03-04 21:46:13 +00003979 if (pat != NULL)
3980 {
3981 if (ic)
3982 dumpflags |= DUMPFLAG_ICASE;
3983 else
3984 {
3985 n = captype(pat, NULL);
3986 if (n == WF_ONECAP)
3987 dumpflags |= DUMPFLAG_ONECAP;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01003988 else if (n == WF_ALLCAP && (int)STRLEN(pat) > mb_ptr2len(pat))
Bram Moolenaard0131a82006-03-04 21:46:13 +00003989 dumpflags |= DUMPFLAG_ALLCAP;
3990 }
3991 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003992
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003993 // Find out if we can support regions: All languages must support the same
3994 // regions or none at all.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003995 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003996 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003997 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003998 p = lp->lp_slang->sl_regions;
3999 if (p[0] != 0)
4000 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004001 if (region_names == NULL) // first language with regions
Bram Moolenaar7887d882005-07-01 22:33:52 +00004002 region_names = p;
4003 else if (STRCMP(region_names, p) != 0)
4004 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004005 do_region = FALSE; // region names are different
Bram Moolenaar7887d882005-07-01 22:33:52 +00004006 break;
4007 }
4008 }
4009 }
4010
LemonBoye98fb642023-08-15 23:07:55 +02004011 if (do_region && region_names != NULL && pat == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004012 {
LemonBoye98fb642023-08-15 23:07:55 +02004013 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
4014 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004015 }
4016 else
4017 do_region = FALSE;
4018
4019 /*
4020 * Loop over all files loaded for the entries in 'spelllang'.
4021 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004022 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004023 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004024 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004025 slang = lp->lp_slang;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004026 if (slang->sl_fbyts == NULL) // reloading failed
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004027 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004028
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004029 if (pat == NULL)
4030 {
4031 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
4032 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
4033 }
4034
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004035 // When matching with a pattern and there are no prefixes only use
4036 // parts of the tree that match "pat".
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004037 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004038 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004039 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004040 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004041
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004042 // round 1: case-folded tree
4043 // round 2: keep-case tree
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004044 for (round = 1; round <= 2; ++round)
4045 {
4046 if (round == 1)
4047 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004048 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004049 byts = slang->sl_fbyts;
4050 idxs = slang->sl_fidxs;
4051 }
4052 else
4053 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004054 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004055 byts = slang->sl_kbyts;
4056 idxs = slang->sl_kidxs;
4057 }
4058 if (byts == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004059 continue; // array is empty
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004060
4061 depth = 0;
4062 arridx[0] = 0;
4063 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004064 while (depth >= 0 && !got_int
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004065 && (pat == NULL || !ins_compl_interrupted()))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004066 {
4067 if (curi[depth] > byts[arridx[depth]])
4068 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004069 // Done all bytes at this node, go up one level.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004070 --depth;
4071 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02004072 ins_compl_check_keys(50, FALSE);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004073 }
4074 else
4075 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004076 // Do one more byte at this node.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004077 n = arridx[depth] + curi[depth];
4078 ++curi[depth];
4079 c = byts[n];
Bram Moolenaar54e5fed2022-07-04 13:37:07 +01004080 if (c == 0 || depth >= MAXWLEN - 1)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004081 {
Bram Moolenaar54e5fed2022-07-04 13:37:07 +01004082 // End of word or reached maximum length, deal with the
4083 // word.
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004084 // Don't use keep-case words in the fold-case tree,
4085 // they will appear in the keep-case tree.
4086 // Only use the word when the region matches.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004087 flags = (int)idxs[n];
4088 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004089 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +00004090 && (do_region
4091 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004092 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004093 & lp->lp_region) != 0))
4094 {
4095 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004096 if (!do_region)
4097 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004098
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004099 // Dump the basic word if there is no prefix or
4100 // when it's the first one.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004101 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004102 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004103 {
4104 dump_word(slang, word, pat, dir,
4105 dumpflags, flags, lnum);
4106 if (pat == NULL)
4107 ++lnum;
4108 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004109
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004110 // Apply the prefix, if there is one.
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004111 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004112 lnum = dump_prefixes(slang, word, pat, dir,
4113 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004114 }
4115 }
4116 else
4117 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004118 // Normal char, go one level deeper.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004119 word[depth++] = c;
4120 arridx[depth] = idxs[n];
4121 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004122
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004123 // Check if this character matches with the pattern.
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004124 // If not skip the whole tree below it.
4125 // Always ignore case here, dump_word() will check
4126 // proper case later. This isn't exactly right when
4127 // length changes for multi-byte characters with
4128 // ignore case...
Bram Moolenaard0131a82006-03-04 21:46:13 +00004129 if (depth <= patlen
4130 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004131 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004132 }
4133 }
4134 }
4135 }
4136 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004137}
4138
4139/*
4140 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004141 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004142 */
4143 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004144dump_word(
4145 slang_T *slang,
4146 char_u *word,
4147 char_u *pat,
4148 int *dir,
4149 int dumpflags,
4150 int wordflags,
4151 linenr_T lnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004152{
4153 int keepcap = FALSE;
4154 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004155 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004156 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +00004157 char_u badword[MAXWLEN + 10];
4158 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +00004159 int flags = wordflags;
4160
4161 if (dumpflags & DUMPFLAG_ONECAP)
4162 flags |= WF_ONECAP;
4163 if (dumpflags & DUMPFLAG_ALLCAP)
4164 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004165
Bram Moolenaar4770d092006-01-12 23:22:24 +00004166 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004167 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004168 // Need to fix case according to "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004169 make_case_word(word, cword, flags);
4170 p = cword;
4171 }
4172 else
4173 {
4174 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004175 if ((dumpflags & DUMPFLAG_KEEPCASE)
4176 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004177 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004178 keepcap = TRUE;
4179 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004180 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004181
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004182 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004183 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004184 // Add flags and regions after a slash.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004185 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004186 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004187 STRCPY(badword, p);
4188 STRCAT(badword, "/");
4189 if (keepcap)
4190 STRCAT(badword, "=");
4191 if (flags & WF_BANNED)
4192 STRCAT(badword, "!");
4193 else if (flags & WF_RARE)
4194 STRCAT(badword, "?");
4195 if (flags & WF_REGION)
4196 for (i = 0; i < 7; ++i)
4197 if (flags & (0x10000 << i))
4198 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
4199 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004200 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004201
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004202 if (dumpflags & DUMPFLAG_COUNT)
4203 {
4204 hashitem_T *hi;
4205
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004206 // Include the word count for ":spelldump!".
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004207 hi = hash_find(&slang->sl_wordcount, tw);
4208 if (!HASHITEM_EMPTY(hi))
4209 {
4210 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
4211 tw, HI2WC(hi)->wc_count);
4212 p = IObuff;
4213 }
4214 }
4215
4216 ml_append(lnum, p, (colnr_T)0, FALSE);
4217 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00004218 else if (((dumpflags & DUMPFLAG_ICASE)
4219 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
4220 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004221 && ins_compl_add_infercase(p, (int)STRLEN(p),
glepnirf31cfa22025-03-06 21:59:13 +01004222 p_ic, NULL, *dir, FALSE, 0) == OK)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004223 // if dir was BACKWARD then honor it just once
Bram Moolenaard0131a82006-03-04 21:46:13 +00004224 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004225}
4226
4227/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004228 * For ":spelldump": Find matching prefixes for "word". Prepend each to
4229 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004230 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004231 * Return the updated line number.
4232 */
4233 static linenr_T
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004234dump_prefixes(
4235 slang_T *slang,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004236 char_u *word, // case-folded word
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004237 char_u *pat,
4238 int *dir,
4239 int dumpflags,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004240 int flags, // flags with prefix ID
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004241 linenr_T startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004242{
4243 idx_T arridx[MAXWLEN];
4244 int curi[MAXWLEN];
4245 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +00004246 char_u word_up[MAXWLEN];
4247 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004248 int c;
4249 char_u *byts;
4250 idx_T *idxs;
4251 linenr_T lnum = startlnum;
4252 int depth;
4253 int n;
4254 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004255 int i;
4256
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004257 // If the word starts with a lower-case letter make the word with an
4258 // upper-case letter in word_up[].
Bram Moolenaar53805d12005-08-01 07:08:33 +00004259 c = PTR2CHAR(word);
4260 if (SPELL_TOUPPER(c) != c)
4261 {
4262 onecap_copy(word, word_up, TRUE);
4263 has_word_up = TRUE;
4264 }
4265
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004266 byts = slang->sl_pbyts;
4267 idxs = slang->sl_pidxs;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004268 if (byts != NULL) // array not is empty
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004269 {
4270 /*
4271 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004272 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004273 */
4274 depth = 0;
4275 arridx[0] = 0;
4276 curi[0] = 1;
4277 while (depth >= 0 && !got_int)
4278 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004279 n = arridx[depth];
4280 len = byts[n];
4281 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004282 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004283 // Done all bytes at this node, go up one level.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004284 --depth;
4285 line_breakcheck();
4286 }
4287 else
4288 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004289 // Do one more byte at this node.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004290 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004291 ++curi[depth];
4292 c = byts[n];
4293 if (c == 0)
4294 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004295 // End of prefix, find out how many IDs there are.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004296 for (i = 1; i < len; ++i)
4297 if (byts[n + i] != 0)
4298 break;
4299 curi[depth] += i - 1;
4300
Bram Moolenaar53805d12005-08-01 07:08:33 +00004301 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
4302 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004303 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004304 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004305 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004306 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004307 : flags, lnum);
4308 if (lnum != 0)
4309 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004310 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00004311
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004312 // Check for prefix that matches the word when the
4313 // first letter is upper-case, but only if the prefix has
4314 // a condition.
Bram Moolenaar53805d12005-08-01 07:08:33 +00004315 if (has_word_up)
4316 {
4317 c = valid_word_prefix(i, n, flags, word_up, slang,
4318 TRUE);
4319 if (c != 0)
4320 {
4321 vim_strncpy(prefix + depth, word_up,
4322 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004323 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004324 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004325 : flags, lnum);
4326 if (lnum != 0)
4327 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004328 }
4329 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004330 }
4331 else
4332 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004333 // Normal char, go one level deeper.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004334 prefix[depth++] = c;
4335 arridx[depth] = idxs[n];
4336 curi[depth] = 1;
4337 }
4338 }
4339 }
4340 }
4341
4342 return lnum;
4343}
4344
Bram Moolenaar95529562005-08-25 21:21:38 +00004345/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004346 * Move "p" to the end of word "start".
4347 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +00004348 */
4349 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004350spell_to_word_end(char_u *start, win_T *win)
Bram Moolenaar95529562005-08-25 21:21:38 +00004351{
4352 char_u *p = start;
4353
Bram Moolenaar860cae12010-06-05 23:22:07 +02004354 while (*p != NUL && spell_iswordp(p, win))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004355 MB_PTR_ADV(p);
Bram Moolenaar95529562005-08-25 21:21:38 +00004356 return p;
4357}
4358
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004359/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004360 * For Insert mode completion CTRL-X s:
4361 * Find start of the word in front of column "startcol".
4362 * We don't check if it is badly spelled, with completion we can only change
4363 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004364 * Returns the column number of the word.
4365 */
4366 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004367spell_word_start(int startcol)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004368{
4369 char_u *line;
4370 char_u *p;
4371 int col = 0;
4372
Bram Moolenaar95529562005-08-25 21:21:38 +00004373 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004374 return startcol;
4375
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004376 // Find a word character before "startcol".
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004377 line = ml_get_curline();
4378 for (p = line + startcol; p > line; )
4379 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004380 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004381 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004382 break;
4383 }
4384
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004385 // Go back to start of the word.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004386 while (p > line)
4387 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004388 col = (int)(p - line);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004389 MB_PTR_BACK(line, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004390 if (!spell_iswordp(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004391 break;
4392 col = 0;
4393 }
4394
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004395 return col;
4396}
4397
4398/*
Bram Moolenaar4effc802005-09-30 21:12:02 +00004399 * Need to check for 'spellcapcheck' now, the word is removed before
4400 * expand_spelling() is called. Therefore the ugly global variable.
4401 */
4402static int spell_expand_need_cap;
4403
4404 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004405spell_expand_check_cap(colnr_T col)
Bram Moolenaar4effc802005-09-30 21:12:02 +00004406{
Luuk van Baal2ac64972023-05-25 17:14:42 +01004407 spell_expand_need_cap = check_need_cap(curwin, curwin->w_cursor.lnum, col);
Bram Moolenaar4effc802005-09-30 21:12:02 +00004408}
4409
4410/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004411 * Get list of spelling suggestions.
4412 * Used for Insert mode completion CTRL-X ?.
4413 * Returns the number of matches. The matches are in "matchp[]", array of
4414 * allocated strings.
4415 */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004416 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004417expand_spelling(
4418 linenr_T lnum UNUSED,
4419 char_u *pat,
4420 char_u ***matchp)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004421{
4422 garray_T ga;
4423
Bram Moolenaar4770d092006-01-12 23:22:24 +00004424 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004425 *matchp = ga.ga_data;
4426 return ga.ga_len;
4427}
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004428
Bram Moolenaare677df82019-09-02 22:31:11 +02004429/*
Bram Moolenaarf154f3a2020-06-08 18:54:49 +02004430 * Return TRUE if "val" is a valid 'spelllang' value.
Bram Moolenaare677df82019-09-02 22:31:11 +02004431 */
4432 int
Bram Moolenaarf154f3a2020-06-08 18:54:49 +02004433valid_spelllang(char_u *val)
Bram Moolenaare677df82019-09-02 22:31:11 +02004434{
4435 return valid_name(val, ".-_,@");
4436}
4437
4438/*
4439 * Return TRUE if "val" is a valid 'spellfile' value.
4440 */
4441 int
4442valid_spellfile(char_u *val)
4443{
Milly322ad0c2024-10-14 20:21:48 +02004444 char_u spf_name[MAXPATHL];
4445 char_u *spf;
4446 char_u *s;
4447 int l;
Bram Moolenaare677df82019-09-02 22:31:11 +02004448
Milly322ad0c2024-10-14 20:21:48 +02004449 spf = val;
4450 while (*spf != NUL)
4451 {
4452 l = copy_option_part(&spf, spf_name, MAXPATHL, ",");
4453 if (l >= MAXPATHL - 4 || l < 4
4454 || STRCMP(spf_name + l - 4, ".add") != 0)
Bram Moolenaare677df82019-09-02 22:31:11 +02004455 return FALSE;
Milly322ad0c2024-10-14 20:21:48 +02004456 for (s = spf_name; *s != NUL; ++s)
4457 if (!vim_is_fname_char(*s))
4458 return FALSE;
4459 }
Bram Moolenaare677df82019-09-02 22:31:11 +02004460 return TRUE;
4461}
4462
4463/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004464 * Handle side effects of setting 'spell' or 'spellfile'
Bram Moolenaare677df82019-09-02 22:31:11 +02004465 * Return an error message or NULL for success.
4466 */
4467 char *
Milly322ad0c2024-10-14 20:21:48 +02004468did_set_spell_option(void)
Bram Moolenaare677df82019-09-02 22:31:11 +02004469{
4470 char *errmsg = NULL;
4471 win_T *wp;
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00004472
4473 FOR_ALL_WINDOWS(wp)
4474 if (wp->w_buffer == curbuf && wp->w_p_spell)
4475 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004476 errmsg = parse_spelllang(wp);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00004477 break;
4478 }
Bram Moolenaare677df82019-09-02 22:31:11 +02004479 return errmsg;
4480}
4481
4482/*
4483 * Set curbuf->b_cap_prog to the regexp program for 'spellcapcheck'.
4484 * Return error message when failed, NULL when OK.
4485 */
4486 char *
4487compile_cap_prog(synblock_T *synblock)
4488{
4489 regprog_T *rp = synblock->b_cap_prog;
4490 char_u *re;
4491
Bram Moolenaar53efb182019-10-13 19:49:26 +02004492 if (synblock->b_p_spc == NULL || *synblock->b_p_spc == NUL)
Bram Moolenaare677df82019-09-02 22:31:11 +02004493 synblock->b_cap_prog = NULL;
4494 else
4495 {
4496 // Prepend a ^ so that we only match at one column
4497 re = concat_str((char_u *)"^", synblock->b_p_spc);
4498 if (re != NULL)
4499 {
4500 synblock->b_cap_prog = vim_regcomp(re, RE_MAGIC);
4501 vim_free(re);
4502 if (synblock->b_cap_prog == NULL)
4503 {
4504 synblock->b_cap_prog = rp; // restore the previous program
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004505 return e_invalid_argument;
Bram Moolenaare677df82019-09-02 22:31:11 +02004506 }
4507 }
4508 }
4509
4510 vim_regfree(rp);
4511 return NULL;
4512}
4513
4514#endif // FEAT_SPELL