blob: 4f4959e301332fb1cfa1e7ce19651d07ef311725 [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 Moolenaar51485f02005-06-04 21:55:20 +000058/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000059 * Use this to adjust the score after finding suggestions, based on the
60 * suggested word sounding like the bad word. This is much faster than doing
61 * it for every possible suggestion.
Bram Moolenaar4770d092006-01-12 23:22:24 +000062 * Disadvantage: When "the" is typed as "hte" it sounds quite different ("@"
63 * vs "ht") and goes down in the list.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000064 * Used when 'spellsuggest' is set to "best".
65 */
66#define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4)
67
68/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000069 * Do the opposite: based on a maximum end score and a known sound score,
Bram Moolenaar6949d1d2008-08-25 02:14:05 +000070 * compute the maximum word score that can be used.
Bram Moolenaar4770d092006-01-12 23:22:24 +000071 */
72#define MAXSCORE(word_score, sound_score) ((4 * word_score - sound_score) / 3)
73
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020074#define IN_SPELL_C
Bram Moolenaare19defe2005-03-21 08:23:33 +000075#include "vim.h"
76
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000077#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaare19defe2005-03-21 08:23:33 +000078
Bram Moolenaar4770d092006-01-12 23:22:24 +000079#ifndef UNIX /* it's in os_unix.h for Unix */
80# include <time.h> /* for time_t */
81#endif
82
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000083/* only used for su_badflags */
84#define WF_MIXCAP 0x20 /* mix of upper and lower case: macaRONI */
85
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000086#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +000087
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000088#define REGION_ALL 0xff /* word valid in all regions */
89
Bram Moolenaar4770d092006-01-12 23:22:24 +000090#define VIMSUGMAGIC "VIMsug" /* string at start of Vim .sug file */
91#define VIMSUGMAGICL 6
92#define VIMSUGVERSION 1
93
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000094/* Result values. Lower number is accepted over higher one. */
95#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000096#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000097#define SP_RARE 1
98#define SP_LOCAL 2
99#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000100
Bram Moolenaar4770d092006-01-12 23:22:24 +0000101typedef struct wordcount_S
102{
103 short_u wc_count; /* nr of times word was seen */
104 char_u wc_word[1]; /* word, actually longer */
105} wordcount_T;
106
Bram Moolenaar84026842016-07-17 20:37:43 +0200107#define WC_KEY_OFF offsetof(wordcount_T, wc_word)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000108#define HI2WC(hi) ((wordcount_T *)((hi)->hi_key - WC_KEY_OFF))
109#define MAXWORDCOUNT 0xffff
110
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000111/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000112 * Information used when looking for suggestions.
113 */
114typedef struct suginfo_S
115{
116 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000117 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000118 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000119 int su_sfmaxscore; /* idem, for when doing soundfold words */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000120 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000121 char_u *su_badptr; /* start of bad word in line */
122 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000123 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000124 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
125 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000126 char_u su_sal_badword[MAXWLEN]; /* su_badword soundfolded */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000127 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000128 slang_T *su_sallang; /* default language for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000129} suginfo_T;
130
131/* One word suggestion. Used in "si_ga". */
132typedef struct suggest_S
133{
134 char_u *st_word; /* suggested word, allocated string */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000135 int st_wordlen; /* STRLEN(st_word) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000136 int st_orglen; /* length of replaced text */
137 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000138 int st_altscore; /* used when st_score compares equal */
139 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000140 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000141 slang_T *st_slang; /* language used for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000142} suggest_T;
143
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000144#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000145
Bram Moolenaar4770d092006-01-12 23:22:24 +0000146/* TRUE if a word appears in the list of banned words. */
147#define WAS_BANNED(su, word) (!HASHITEM_EMPTY(hash_find(&su->su_banned, word)))
148
Bram Moolenaar6949d1d2008-08-25 02:14:05 +0000149/* Number of suggestions kept when cleaning up. We need to keep more than
Bram Moolenaar4770d092006-01-12 23:22:24 +0000150 * what is displayed, because when rescore_suggestions() is called the score
151 * may change and wrong suggestions may be removed later. */
152#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 130 ? 150 : (su)->su_maxcount + 20)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000153
154/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
155 * of suggestions that are not going to be displayed. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000156#define SUG_MAX_COUNT(su) (SUG_CLEAN_COUNT(su) + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000157
158/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000159#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000160#define SCORE_SPLIT_NO 249 /* split bad word with NOSPLITSUGS */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000161#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000162#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000163#define SCORE_RARE 180 /* rare word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000164#define SCORE_SWAP 75 /* swap two characters */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000165#define SCORE_SWAP3 110 /* swap two characters in three */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000166#define SCORE_REP 65 /* REP replacement */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000167#define SCORE_SUBST 93 /* substitute a character */
168#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000169#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000170#define SCORE_DEL 94 /* delete a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000171#define SCORE_DELDUP 66 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000172#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000173#define SCORE_INS 96 /* insert a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000174#define SCORE_INSDUP 67 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000175#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000176#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000177
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000178#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000179#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
180 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000181
Bram Moolenaar4770d092006-01-12 23:22:24 +0000182#define SCORE_COMMON1 30 /* subtracted for words seen before */
183#define SCORE_COMMON2 40 /* subtracted for words often seen */
184#define SCORE_COMMON3 50 /* subtracted for words very often seen */
185#define SCORE_THRES2 10 /* word count threshold for COMMON2 */
186#define SCORE_THRES3 100 /* word count threshold for COMMON3 */
187
188/* When trying changed soundfold words it becomes slow when trying more than
189 * two changes. With less then two changes it's slightly faster but we miss a
190 * few good suggestions. In rare cases we need to try three of four changes.
191 */
192#define SCORE_SFMAX1 200 /* maximum score for first try */
193#define SCORE_SFMAX2 300 /* maximum score for second try */
194#define SCORE_SFMAX3 400 /* maximum score for third try */
195
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000196#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000197#define SCORE_MAXMAX 999999 /* accept any score */
198#define SCORE_LIMITMAX 350 /* for spell_edit_score_limit() */
199
200/* for spell_edit_score_limit() we need to know the minimum value of
201 * SCORE_ICASE, SCORE_SWAP, SCORE_DEL, SCORE_SIMILAR and SCORE_INS */
202#define SCORE_EDIT_MIN SCORE_SIMILAR
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000203
204/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000205 * Structure to store info for word matching.
206 */
207typedef struct matchinf_S
208{
209 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000210
211 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000212 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000213 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000214 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000215 char_u *mi_cend; /* char after what was used for
216 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000217
218 /* case-folded text */
219 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000220 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000221
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000222 /* for when checking word after a prefix */
223 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000224 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000225 int mi_prefcnt; /* number of entries at mi_prefarridx */
226 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000227 int mi_cprefixlen; /* byte length of prefix in original
228 case */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000229
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000230 /* for when checking a compound word */
231 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000232 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
233 int mi_complen; /* nr of compound words used */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000234 int mi_compextra; /* nr of COMPOUNDROOT words */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000235
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000236 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000237 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000238 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200239 win_T *mi_win; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000240
241 /* for NOBREAK */
242 int mi_result2; /* "mi_resul" without following word */
243 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000244} matchinf_T;
245
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000246
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100247static int spell_iswordp(char_u *p, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100248static int spell_mb_isword_class(int cl, win_T *wp);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000249
250/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000251 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000252 */
253typedef enum
254{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000255 STATE_START = 0, /* At start of node check for NUL bytes (goodword
256 * ends); if badword ends there is a match, otherwise
257 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000258 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000259 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000260 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
261 STATE_PLAIN, /* Use each byte of the node. */
262 STATE_DEL, /* Delete a byte from the bad word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000263 STATE_INS_PREP, /* Prepare for inserting bytes. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000264 STATE_INS, /* Insert a byte in the bad word. */
265 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000266 STATE_UNSWAP, /* Undo swap two characters. */
267 STATE_SWAP3, /* Swap two characters over three. */
268 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000269 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000270 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000271 STATE_REP_INI, /* Prepare for using REP items. */
272 STATE_REP, /* Use matching REP items from the .aff file. */
273 STATE_REP_UNDO, /* Undo a REP item replacement. */
274 STATE_FINAL /* End of this node. */
275} state_T;
276
277/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000278 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000279 */
280typedef struct trystate_S
281{
Bram Moolenaarea424162005-06-16 21:51:00 +0000282 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000283 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000284 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000285 short ts_curi; /* index in list of child nodes */
286 char_u ts_fidx; /* index in fword[], case-folded bad word */
287 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
288 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000289 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000290 * PFD_PREFIXTREE or PFD_NOPREFIX */
291 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000292 char_u ts_tcharlen; /* number of bytes in tword character */
293 char_u ts_tcharidx; /* current byte index in tword character */
294 char_u ts_isdiff; /* DIFF_ values */
295 char_u ts_fcharstart; /* index in fword where badword char started */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000296 char_u ts_prewordlen; /* length of word in "preword[]" */
297 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000298 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000299 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000300 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000301 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000302 char_u ts_delidx; /* index in fword for char that was deleted,
303 valid when "ts_flags" has TSF_DIDDEL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000304} trystate_T;
305
Bram Moolenaarea424162005-06-16 21:51:00 +0000306/* values for ts_isdiff */
307#define DIFF_NONE 0 /* no different byte (yet) */
308#define DIFF_YES 1 /* different byte found */
309#define DIFF_INSERT 2 /* inserting character */
310
Bram Moolenaard12a1322005-08-21 22:08:24 +0000311/* values for ts_flags */
312#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
313#define TSF_DIDSPLIT 2 /* tried split at this point */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000314#define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000315
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000316/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000317#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000318#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000319#define PFD_NOTSPECIAL 0xfd /* highest value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000320
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000321/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000322#define FIND_FOLDWORD 0 /* find word case-folded */
323#define FIND_KEEPWORD 1 /* find keep-case word */
324#define FIND_PREFIX 2 /* find word after prefix */
325#define FIND_COMPOUND 3 /* find case-folded compound word */
326#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000327
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100328static void find_word(matchinf_T *mip, int mode);
329static int match_checkcompoundpattern(char_u *ptr, int wlen, garray_T *gap);
330static int can_compound(slang_T *slang, char_u *word, char_u *flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100331static int match_compoundrule(slang_T *slang, char_u *compflags);
332static int valid_word_prefix(int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang, int cond_req);
333static void find_prefix(matchinf_T *mip, int mode);
334static int fold_more(matchinf_T *mip);
335static int spell_valid_case(int wordflags, int treeflags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100336static void spell_load_cb(char_u *fname, void *cookie);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100337static int count_syllables(slang_T *slang, char_u *word);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100338static void clear_midword(win_T *buf);
339static void use_midword(slang_T *lp, win_T *buf);
340static int find_region(char_u *rp, char_u *region);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200341static int spell_iswordp_nmw(char_u *p, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100342static int check_need_cap(linenr_T lnum, colnr_T col);
343static void spell_find_suggest(char_u *badptr, int badlen, suginfo_T *su, int maxcount, int banbadword, int need_cap, int interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000344#ifdef FEAT_EVAL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100345static void spell_suggest_expr(suginfo_T *su, char_u *expr);
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000346#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100347static void spell_suggest_file(suginfo_T *su, char_u *fname);
348static void spell_suggest_intern(suginfo_T *su, int interactive);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100349static void spell_find_cleanup(suginfo_T *su);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100350static void suggest_try_special(suginfo_T *su);
351static void suggest_try_change(suginfo_T *su);
352static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, int soundfold);
353static void go_deeper(trystate_T *stack, int depth, int score_add);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100354static int nofold_len(char_u *fword, int flen, char_u *word);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100355static void find_keepcap_word(slang_T *slang, char_u *fword, char_u *kword);
356static void score_comp_sal(suginfo_T *su);
357static void score_combine(suginfo_T *su);
358static int stp_sal_score(suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound);
359static void suggest_try_soundalike_prep(void);
360static void suggest_try_soundalike(suginfo_T *su);
361static void suggest_try_soundalike_finish(void);
362static void add_sound_suggest(suginfo_T *su, char_u *goodword, int score, langp_T *lp);
363static int soundfold_find(slang_T *slang, char_u *word);
364static void make_case_word(char_u *fword, char_u *cword, int flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100365static int similar_chars(slang_T *slang, int c1, int c2);
366static void add_suggestion(suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int score, int altscore, int had_bonus, slang_T *slang, int maxsf);
367static void check_suggestions(suginfo_T *su, garray_T *gap);
368static void add_banned(suginfo_T *su, char_u *word);
369static void rescore_suggestions(suginfo_T *su);
370static void rescore_one(suginfo_T *su, suggest_T *stp);
371static int cleanup_suggestions(garray_T *gap, int maxscore, int keep);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100372static void spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res);
373static void spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100374static void spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100375static int soundalike_score(char_u *goodsound, char_u *badsound);
376static int spell_edit_score(slang_T *slang, char_u *badword, char_u *goodword);
377static int spell_edit_score_limit(slang_T *slang, char_u *badword, char_u *goodword, int limit);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100378static int spell_edit_score_limit_w(slang_T *slang, char_u *badword, char_u *goodword, int limit);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100379static void dump_word(slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum);
380static linenr_T dump_prefixes(slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T startlnum);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000381
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000382
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000383/* Remember what "z?" replaced. */
384static char_u *repl_from = NULL;
385static char_u *repl_to = NULL;
386
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000387/*
388 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000389 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000390 * "*attrp" is set to the highlight index for a badly spelled word. For a
391 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000392 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000393 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000394 * "capcol" is used to check for a Capitalised word after the end of a
395 * sentence. If it's zero then perform the check. Return the column where to
396 * check next, or -1 when no sentence end was found. If it's NULL then don't
397 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000398 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000399 * Returns the length of the word in bytes, also when it's OK, so that the
400 * caller can skip over the word.
401 */
402 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100403spell_check(
404 win_T *wp, /* current window */
405 char_u *ptr,
406 hlf_T *attrp,
407 int *capcol, /* column to check for Capital */
408 int docount) /* count good words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000409{
410 matchinf_T mi; /* Most things are put in "mi" so that it can
411 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000412 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000413 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000414 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000415 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +0000416 int count_word = docount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000417
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000418 /* A word never starts at a space or a control character. Return quickly
419 * then, skipping over the character. */
420 if (*ptr <= ' ')
421 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000422
423 /* Return here when loading language files failed. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200424 if (wp->w_s->b_langp.ga_len == 0)
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000425 return 1;
426
Bram Moolenaar5195e452005-08-19 20:32:47 +0000427 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000428
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000429 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +0000430 * 0X99FF. But always do check spelling to find "3GPP" and "11
431 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000432 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000433 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100434 if (*ptr == '0' && (ptr[1] == 'b' || ptr[1] == 'B'))
435 mi.mi_end = skipbin(ptr + 2);
436 else if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
Bram Moolenaar3982c542005-06-08 21:56:31 +0000437 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000438 else
439 mi.mi_end = skipdigits(ptr);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000440 nrlen = (int)(mi.mi_end - ptr);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000441 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000442
Bram Moolenaar0c405862005-06-22 22:26:26 +0000443 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000444 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +0000445 mi.mi_fend = ptr;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200446 if (spell_iswordp(mi.mi_fend, wp))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000447 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000448 do
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100449 MB_PTR_ADV(mi.mi_fend);
Bram Moolenaarabab0b02019-03-30 18:47:01 +0100450 while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000451
Bram Moolenaar860cae12010-06-05 23:22:07 +0200452 if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000453 {
454 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000455 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000456 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000457 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000458 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000459 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000460 if (capcol != NULL)
461 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000462
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000463 /* We always use the characters up to the next non-word character,
464 * also for bad words. */
465 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000466
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000467 /* Check caps type later. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200468 mi.mi_capflags = 0;
469 mi.mi_cend = NULL;
470 mi.mi_win = wp;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000471
Bram Moolenaar5195e452005-08-19 20:32:47 +0000472 /* case-fold the word with one non-word character, so that we can check
473 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000474 if (*mi.mi_fend != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100475 MB_PTR_ADV(mi.mi_fend);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000476
477 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
478 MAXWLEN + 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000479 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000480
481 /* The word is bad unless we recognize it. */
482 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +0000483 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000484
485 /*
486 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000487 * We check them all, because a word may be matched longer in another
488 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000489 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200490 for (lpi = 0; lpi < wp->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000491 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200492 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, lpi);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000493
494 /* If reloading fails the language is still in the list but everything
495 * has been cleared. */
496 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
497 continue;
498
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000499 /* Check for a matching word in case-folded words. */
500 find_word(&mi, FIND_FOLDWORD);
501
502 /* Check for a matching word in keep-case words. */
503 find_word(&mi, FIND_KEEPWORD);
504
505 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000506 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +0000507
508 /* For a NOBREAK language, may want to use a word without a following
509 * word as a backup. */
510 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
511 && mi.mi_result2 != SP_BAD)
512 {
513 mi.mi_result = mi.mi_result2;
514 mi.mi_end = mi.mi_end2;
515 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000516
517 /* Count the word in the first language where it's found to be OK. */
518 if (count_word && mi.mi_result == SP_OK)
519 {
520 count_common_word(mi.mi_lp->lp_slang, ptr,
521 (int)(mi.mi_end - ptr), 1);
522 count_word = FALSE;
523 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000524 }
525
526 if (mi.mi_result != SP_OK)
527 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000528 /* If we found a number skip over it. Allows for "42nd". Do flag
529 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000530 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000531 {
532 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
533 return nrlen;
534 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000535
536 /* When we are at a non-word character there is no error, just
537 * skip over the character (try looking for a word after it). */
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100538 else if (!spell_iswordp_nmw(ptr, wp))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000539 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200540 if (capcol != NULL && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000541 {
542 regmatch_T regmatch;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100543 int r;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000544
545 /* Check for end of sentence. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200546 regmatch.regprog = wp->w_s->b_cap_prog;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000547 regmatch.rm_ic = FALSE;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100548 r = vim_regexec(&regmatch, ptr, 0);
549 wp->w_s->b_cap_prog = regmatch.regprog;
550 if (r)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000551 *capcol = (int)(regmatch.endp[0] - ptr);
552 }
553
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000554 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000555 return (*mb_ptr2len)(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000556 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000557 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000558 else if (mi.mi_end == ptr)
559 /* Always include at least one character. Required for when there
560 * is a mixup in "midword". */
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100561 MB_PTR_ADV(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +0000562 else if (mi.mi_result == SP_BAD
Bram Moolenaar860cae12010-06-05 23:22:07 +0200563 && LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak)
Bram Moolenaar78622822005-08-23 21:00:13 +0000564 {
565 char_u *p, *fp;
566 int save_result = mi.mi_result;
567
568 /* First language in 'spelllang' is NOBREAK. Find first position
569 * at which any word would be valid. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200570 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000571 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +0000572 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000573 p = mi.mi_word;
574 fp = mi.mi_fword;
575 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +0000576 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100577 MB_PTR_ADV(p);
578 MB_PTR_ADV(fp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000579 if (p >= mi.mi_end)
580 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000581 mi.mi_compoff = (int)(fp - mi.mi_fword);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000582 find_word(&mi, FIND_COMPOUND);
583 if (mi.mi_result != SP_BAD)
584 {
585 mi.mi_end = p;
586 break;
587 }
Bram Moolenaar78622822005-08-23 21:00:13 +0000588 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000589 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +0000590 }
Bram Moolenaar78622822005-08-23 21:00:13 +0000591 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000592
593 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000594 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000595 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000596 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000597 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000598 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000599 }
600
Bram Moolenaar5195e452005-08-19 20:32:47 +0000601 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
602 {
603 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000604 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000605 return wrongcaplen;
606 }
607
Bram Moolenaar51485f02005-06-04 21:55:20 +0000608 return (int)(mi.mi_end - ptr);
609}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000610
Bram Moolenaar51485f02005-06-04 21:55:20 +0000611/*
612 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000613 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
614 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
615 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
616 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000617 *
618 * For a match mip->mi_result is updated.
619 */
620 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100621find_word(matchinf_T *mip, int mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000622{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000623 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000624 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000625 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000626 int endidxcnt = 0;
627 int len;
628 int wlen = 0;
629 int flen;
630 int c;
631 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000632 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000633 char_u *s;
Bram Moolenaare52325c2005-08-22 22:54:29 +0000634 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000635 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000636 slang_T *slang = mip->mi_lp->lp_slang;
637 unsigned flags;
638 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000639 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000640 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000641 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +0000642 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000643
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000644 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000645 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000646 /* Check for word with matching case in keep-case tree. */
647 ptr = mip->mi_word;
648 flen = 9999; /* no case folding, always enough bytes */
649 byts = slang->sl_kbyts;
650 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000651
652 if (mode == FIND_KEEPCOMPOUND)
653 /* Skip over the previously found word(s). */
654 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000655 }
656 else
657 {
658 /* Check for case-folded in case-folded tree. */
659 ptr = mip->mi_fword;
660 flen = mip->mi_fwordlen; /* available case-folded bytes */
661 byts = slang->sl_fbyts;
662 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000663
664 if (mode == FIND_PREFIX)
665 {
666 /* Skip over the prefix. */
667 wlen = mip->mi_prefixlen;
668 flen -= mip->mi_prefixlen;
669 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000670 else if (mode == FIND_COMPOUND)
671 {
672 /* Skip over the previously found word(s). */
673 wlen = mip->mi_compoff;
674 flen -= mip->mi_compoff;
675 }
676
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000677 }
678
Bram Moolenaar51485f02005-06-04 21:55:20 +0000679 if (byts == NULL)
680 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000681
Bram Moolenaar51485f02005-06-04 21:55:20 +0000682 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000683 * Repeat advancing in the tree until:
684 * - there is a byte that doesn't match,
685 * - we reach the end of the tree,
686 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000687 */
688 for (;;)
689 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000690 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000691 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000692
693 len = byts[arridx++];
694
695 /* If the first possible byte is a zero the word could end here.
696 * Remember this index, we first check for the longest word. */
697 if (byts[arridx] == 0)
698 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000699 if (endidxcnt == MAXWLEN)
700 {
701 /* Must be a corrupted spell file. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100702 emsg(_(e_format));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000703 return;
704 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000705 endlen[endidxcnt] = wlen;
706 endidx[endidxcnt++] = arridx++;
707 --len;
708
709 /* Skip over the zeros, there can be several flag/region
710 * combinations. */
711 while (len > 0 && byts[arridx] == 0)
712 {
713 ++arridx;
714 --len;
715 }
716 if (len == 0)
717 break; /* no children, word must end here */
718 }
719
720 /* Stop looking at end of the line. */
721 if (ptr[wlen] == NUL)
722 break;
723
724 /* Perform a binary search in the list of accepted bytes. */
725 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +0000726 if (c == TAB) /* <Tab> is handled like <Space> */
727 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +0000728 lo = arridx;
729 hi = arridx + len - 1;
730 while (lo < hi)
731 {
732 m = (lo + hi) / 2;
733 if (byts[m] > c)
734 hi = m - 1;
735 else if (byts[m] < c)
736 lo = m + 1;
737 else
738 {
739 lo = hi = m;
740 break;
741 }
742 }
743
744 /* Stop if there is no matching byte. */
745 if (hi < lo || byts[lo] != c)
746 break;
747
748 /* Continue at the child (if there is one). */
749 arridx = idxs[lo];
750 ++wlen;
751 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +0000752
753 /* One space in the good word may stand for several spaces in the
754 * checked word. */
755 if (c == ' ')
756 {
757 for (;;)
758 {
759 if (flen <= 0 && *mip->mi_fend != NUL)
760 flen = fold_more(mip);
761 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
762 break;
763 ++wlen;
764 --flen;
765 }
766 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000767 }
768
769 /*
770 * Verify that one of the possible endings is valid. Try the longest
771 * first.
772 */
773 while (endidxcnt > 0)
774 {
775 --endidxcnt;
776 arridx = endidx[endidxcnt];
777 wlen = endlen[endidxcnt];
778
Bram Moolenaar51485f02005-06-04 21:55:20 +0000779 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
780 continue; /* not at first byte of character */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200781 if (spell_iswordp(ptr + wlen, mip->mi_win))
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000782 {
Bram Moolenaar78622822005-08-23 21:00:13 +0000783 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000784 continue; /* next char is a word character */
785 word_ends = FALSE;
786 }
787 else
788 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000789 /* The prefix flag is before compound flags. Once a valid prefix flag
790 * has been found we try compound flags. */
791 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000792
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000793 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000794 {
795 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000796 * when folding case. This can be slow, take a shortcut when the
797 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000798 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000799 if (STRNCMP(ptr, p, wlen) != 0)
800 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100801 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s))
802 MB_PTR_ADV(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000803 wlen = (int)(p - mip->mi_word);
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000804 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000805 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000806
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000807 /* Check flags and region. For FIND_PREFIX check the condition and
808 * prefix ID.
809 * Repeat this if there are more flags/region alternatives until there
810 * is a match. */
811 res = SP_BAD;
812 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
813 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000814 {
815 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000816
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000817 /* For the fold-case tree check that the case of the checked word
818 * matches with what the word in the tree requires.
819 * For keep-case tree the case is always right. For prefixes we
820 * don't bother to check. */
821 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000822 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000823 if (mip->mi_cend != mip->mi_word + wlen)
824 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000825 /* mi_capflags was set for a different word length, need
826 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000827 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000828 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000829 }
830
Bram Moolenaar0c405862005-06-22 22:26:26 +0000831 if (mip->mi_capflags == WF_KEEPCAP
832 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000833 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000834 }
835
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000836 /* When mode is FIND_PREFIX the word must support the prefix:
837 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000838 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000839 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000840 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000841 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000842 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +0000843 mip->mi_word + mip->mi_cprefixlen, slang,
844 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000845 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000846 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000847
848 /* Use the WF_RARE flag for a rare prefix. */
849 if (c & WF_RAREPFX)
850 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000851 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000852 }
853
Bram Moolenaar78622822005-08-23 21:00:13 +0000854 if (slang->sl_nobreak)
855 {
856 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
857 && (flags & WF_BANNED) == 0)
858 {
859 /* NOBREAK: found a valid following word. That's all we
860 * need to know, so return. */
861 mip->mi_result = SP_OK;
862 break;
863 }
864 }
865
866 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
867 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000868 {
Bram Moolenaar2113a1d2006-09-11 19:38:08 +0000869 /* If there is no compound flag or the word is shorter than
Bram Moolenaar5195e452005-08-19 20:32:47 +0000870 * COMPOUNDMIN reject it quickly.
871 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000872 * that's too short... Myspell compatibility requires this
873 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000874 if (((unsigned)flags >> 24) == 0
875 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000876 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000877 /* For multi-byte chars check character length against
878 * COMPOUNDMIN. */
879 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000880 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000881 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
882 wlen - mip->mi_compoff) < slang->sl_compminlen)
883 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000884
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000885 /* Limit the number of compound words to COMPOUNDWORDMAX if no
Bram Moolenaare52325c2005-08-22 22:54:29 +0000886 * maximum for syllables is specified. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000887 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
888 > slang->sl_compmax
Bram Moolenaare52325c2005-08-22 22:54:29 +0000889 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000890 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000891
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000892 /* Don't allow compounding on a side where an affix was added,
893 * unless COMPOUNDPERMITFLAG was used. */
894 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
895 continue;
896 if (!word_ends && (flags & WF_NOCOMPAFT))
897 continue;
898
Bram Moolenaard12a1322005-08-21 22:08:24 +0000899 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000900 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +0000901 ? slang->sl_compstartflags
902 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +0000903 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000904 continue;
905
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000906 /* If there is a match with a CHECKCOMPOUNDPATTERN rule
907 * discard the compound word. */
908 if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat))
909 continue;
910
Bram Moolenaare52325c2005-08-22 22:54:29 +0000911 if (mode == FIND_COMPOUND)
912 {
913 int capflags;
914
915 /* Need to check the caps type of the appended compound
916 * word. */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000917 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
918 mip->mi_compoff) != 0)
919 {
920 /* case folding may have changed the length */
921 p = mip->mi_word;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100922 for (s = ptr; s < ptr + mip->mi_compoff; MB_PTR_ADV(s))
923 MB_PTR_ADV(p);
Bram Moolenaare52325c2005-08-22 22:54:29 +0000924 }
925 else
Bram Moolenaare52325c2005-08-22 22:54:29 +0000926 p = mip->mi_word + mip->mi_compoff;
927 capflags = captype(p, mip->mi_word + wlen);
928 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
929 && (flags & WF_FIXCAP) != 0))
930 continue;
931
932 if (capflags != WF_ALLCAP)
933 {
934 /* When the character before the word is a word
935 * character we do not accept a Onecap word. We do
936 * accept a no-caps word, even when the dictionary
937 * word specifies ONECAP. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100938 MB_PTR_BACK(mip->mi_word, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100939 if (spell_iswordp_nmw(p, mip->mi_win)
Bram Moolenaare52325c2005-08-22 22:54:29 +0000940 ? capflags == WF_ONECAP
941 : (flags & WF_ONECAP) != 0
942 && capflags != WF_ONECAP)
943 continue;
944 }
945 }
946
Bram Moolenaar5195e452005-08-19 20:32:47 +0000947 /* If the word ends the sequence of compound flags of the
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000948 * words must match with one of the COMPOUNDRULE items and
Bram Moolenaar5195e452005-08-19 20:32:47 +0000949 * the number of syllables must not be too large. */
950 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
951 mip->mi_compflags[mip->mi_complen + 1] = NUL;
952 if (word_ends)
953 {
954 char_u fword[MAXWLEN];
955
956 if (slang->sl_compsylmax < MAXWLEN)
957 {
958 /* "fword" is only needed for checking syllables. */
959 if (ptr == mip->mi_word)
960 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
961 else
962 vim_strncpy(fword, ptr, endlen[endidxcnt]);
963 }
964 if (!can_compound(slang, fword, mip->mi_compflags))
965 continue;
966 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000967 else if (slang->sl_comprules != NULL
968 && !match_compoundrule(slang, mip->mi_compflags))
969 /* The compound flags collected so far do not match any
970 * COMPOUNDRULE, discard the compounded word. */
971 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000972 }
973
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000974 /* Check NEEDCOMPOUND: can't use word without compounding. */
975 else if (flags & WF_NEEDCOMP)
976 continue;
977
Bram Moolenaar78622822005-08-23 21:00:13 +0000978 nobreak_result = SP_OK;
979
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000980 if (!word_ends)
981 {
Bram Moolenaar78622822005-08-23 21:00:13 +0000982 int save_result = mip->mi_result;
983 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000984 langp_T *save_lp = mip->mi_lp;
985 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +0000986
987 /* Check that a valid word follows. If there is one and we
988 * are compounding, it will set "mi_result", thus we are
989 * always finished here. For NOBREAK we only check that a
990 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000991 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +0000992 if (slang->sl_nobreak)
993 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000994
995 /* Find following word in case-folded tree. */
996 mip->mi_compoff = endlen[endidxcnt];
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000997 if (has_mbyte && mode == FIND_KEEPWORD)
998 {
999 /* Compute byte length in case-folded word from "wlen":
1000 * byte length in keep-case word. Length may change when
1001 * folding case. This can be slow, take a shortcut when
1002 * the case-folded word is equal to the keep-case word. */
1003 p = mip->mi_fword;
1004 if (STRNCMP(ptr, p, wlen) != 0)
1005 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001006 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s))
1007 MB_PTR_ADV(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001008 mip->mi_compoff = (int)(p - mip->mi_fword);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001009 }
1010 }
Bram Moolenaarba534352016-04-21 09:20:26 +02001011#if 0 /* Disabled, see below */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001012 c = mip->mi_compoff;
Bram Moolenaarba534352016-04-21 09:20:26 +02001013#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00001014 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001015 if (flags & WF_COMPROOT)
1016 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001017
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001018 /* For NOBREAK we need to try all NOBREAK languages, at least
1019 * to find the ".add" file(s). */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001020 for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001021 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001022 if (slang->sl_nobreak)
1023 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001024 mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001025 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1026 || !mip->mi_lp->lp_slang->sl_nobreak)
1027 continue;
1028 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001029
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001030 find_word(mip, FIND_COMPOUND);
1031
1032 /* When NOBREAK any word that matches is OK. Otherwise we
1033 * need to find the longest match, thus try with keep-case
1034 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001035 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1036 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001037 /* Find following word in keep-case tree. */
1038 mip->mi_compoff = wlen;
1039 find_word(mip, FIND_KEEPCOMPOUND);
1040
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001041#if 0 /* Disabled, a prefix must not appear halfway a compound word,
1042 unless the COMPOUNDPERMITFLAG is used and then it can't be a
1043 postponed prefix. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001044 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1045 {
1046 /* Check for following word with prefix. */
1047 mip->mi_compoff = c;
1048 find_prefix(mip, FIND_COMPOUND);
1049 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001050#endif
Bram Moolenaar78622822005-08-23 21:00:13 +00001051 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001052
1053 if (!slang->sl_nobreak)
1054 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001055 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001056 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001057 if (flags & WF_COMPROOT)
1058 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001059 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001060
Bram Moolenaar78622822005-08-23 21:00:13 +00001061 if (slang->sl_nobreak)
1062 {
1063 nobreak_result = mip->mi_result;
1064 mip->mi_result = save_result;
1065 mip->mi_end = save_end;
1066 }
1067 else
1068 {
1069 if (mip->mi_result == SP_OK)
1070 break;
1071 continue;
1072 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001073 }
1074
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001075 if (flags & WF_BANNED)
1076 res = SP_BANNED;
1077 else if (flags & WF_REGION)
1078 {
1079 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001080 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001081 res = SP_OK;
1082 else
1083 res = SP_LOCAL;
1084 }
1085 else if (flags & WF_RARE)
1086 res = SP_RARE;
1087 else
1088 res = SP_OK;
1089
Bram Moolenaar78622822005-08-23 21:00:13 +00001090 /* Always use the longest match and the best result. For NOBREAK
1091 * we separately keep the longest match without a following good
1092 * word as a fall-back. */
1093 if (nobreak_result == SP_BAD)
1094 {
1095 if (mip->mi_result2 > res)
1096 {
1097 mip->mi_result2 = res;
1098 mip->mi_end2 = mip->mi_word + wlen;
1099 }
1100 else if (mip->mi_result2 == res
1101 && mip->mi_end2 < mip->mi_word + wlen)
1102 mip->mi_end2 = mip->mi_word + wlen;
1103 }
1104 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001105 {
1106 mip->mi_result = res;
1107 mip->mi_end = mip->mi_word + wlen;
1108 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001109 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001110 mip->mi_end = mip->mi_word + wlen;
1111
Bram Moolenaar78622822005-08-23 21:00:13 +00001112 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001113 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001114 }
1115
Bram Moolenaar78622822005-08-23 21:00:13 +00001116 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001117 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001118 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001119}
1120
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001121/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001122 * Return TRUE if there is a match between the word ptr[wlen] and
1123 * CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another
1124 * word.
1125 * A match means that the first part of CHECKCOMPOUNDPATTERN matches at the
1126 * end of ptr[wlen] and the second part matches after it.
1127 */
1128 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001129match_checkcompoundpattern(
1130 char_u *ptr,
1131 int wlen,
1132 garray_T *gap) /* &sl_comppat */
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001133{
1134 int i;
1135 char_u *p;
1136 int len;
1137
1138 for (i = 0; i + 1 < gap->ga_len; i += 2)
1139 {
1140 p = ((char_u **)gap->ga_data)[i + 1];
1141 if (STRNCMP(ptr + wlen, p, STRLEN(p)) == 0)
1142 {
1143 /* Second part matches at start of following compound word, now
1144 * check if first part matches at end of previous word. */
1145 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar19c9c762008-12-09 21:34:39 +00001146 len = (int)STRLEN(p);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001147 if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0)
1148 return TRUE;
1149 }
1150 }
1151 return FALSE;
1152}
1153
1154/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001155 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1156 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001157 */
1158 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001159can_compound(slang_T *slang, char_u *word, char_u *flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001160{
Bram Moolenaar6de68532005-08-24 22:08:48 +00001161 char_u uflags[MAXWLEN * 2];
1162 int i;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001163 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001164
1165 if (slang->sl_compprog == NULL)
1166 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001167 if (enc_utf8)
1168 {
1169 /* Need to convert the single byte flags to utf8 characters. */
1170 p = uflags;
1171 for (i = 0; flags[i] != NUL; ++i)
Bram Moolenaarace95982017-03-29 17:30:27 +02001172 p += utf_char2bytes(flags[i], p);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001173 *p = NUL;
1174 p = uflags;
1175 }
1176 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00001177 p = flags;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001178 if (!vim_regexec_prog(&slang->sl_compprog, FALSE, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001179 return FALSE;
1180
Bram Moolenaare52325c2005-08-22 22:54:29 +00001181 /* Count the number of syllables. This may be slow, do it last. If there
1182 * are too many syllables AND the number of compound words is above
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001183 * COMPOUNDWORDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001184 if (slang->sl_compsylmax < MAXWLEN
1185 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001186 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001187 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001188}
1189
1190/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001191 * Return TRUE when the sequence of flags in "compflags" plus "flag" can
1192 * possibly form a valid compounded word. This also checks the COMPOUNDRULE
1193 * lines if they don't contain wildcards.
1194 */
1195 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001196can_be_compound(
1197 trystate_T *sp,
1198 slang_T *slang,
1199 char_u *compflags,
1200 int flag)
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001201{
1202 /* If the flag doesn't appear in sl_compstartflags or sl_compallflags
1203 * then it can't possibly compound. */
1204 if (!byte_in_str(sp->ts_complen == sp->ts_compsplit
1205 ? slang->sl_compstartflags : slang->sl_compallflags, flag))
1206 return FALSE;
1207
1208 /* If there are no wildcards, we can check if the flags collected so far
1209 * possibly can form a match with COMPOUNDRULE patterns. This only
1210 * makes sense when we have two or more words. */
1211 if (slang->sl_comprules != NULL && sp->ts_complen > sp->ts_compsplit)
1212 {
1213 int v;
1214
1215 compflags[sp->ts_complen] = flag;
1216 compflags[sp->ts_complen + 1] = NUL;
1217 v = match_compoundrule(slang, compflags + sp->ts_compsplit);
1218 compflags[sp->ts_complen] = NUL;
1219 return v;
1220 }
1221
1222 return TRUE;
1223}
1224
1225
1226/*
1227 * Return TRUE if the compound flags in compflags[] match the start of any
1228 * compound rule. This is used to stop trying a compound if the flags
1229 * collected so far can't possibly match any compound rule.
1230 * Caller must check that slang->sl_comprules is not NULL.
1231 */
1232 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001233match_compoundrule(slang_T *slang, char_u *compflags)
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001234{
1235 char_u *p;
1236 int i;
1237 int c;
1238
1239 /* loop over all the COMPOUNDRULE entries */
1240 for (p = slang->sl_comprules; *p != NUL; ++p)
1241 {
1242 /* loop over the flags in the compound word we have made, match
1243 * them against the current rule entry */
1244 for (i = 0; ; ++i)
1245 {
1246 c = compflags[i];
1247 if (c == NUL)
1248 /* found a rule that matches for the flags we have so far */
1249 return TRUE;
1250 if (*p == '/' || *p == NUL)
1251 break; /* end of rule, it's too short */
1252 if (*p == '[')
1253 {
1254 int match = FALSE;
1255
1256 /* compare against all the flags in [] */
1257 ++p;
1258 while (*p != ']' && *p != NUL)
1259 if (*p++ == c)
1260 match = TRUE;
1261 if (!match)
1262 break; /* none matches */
1263 }
1264 else if (*p != c)
1265 break; /* flag of word doesn't match flag in pattern */
1266 ++p;
1267 }
1268
1269 /* Skip to the next "/", where the next pattern starts. */
1270 p = vim_strchr(p, '/');
1271 if (p == NULL)
1272 break;
1273 }
1274
1275 /* Checked all the rules and none of them match the flags, so there
1276 * can't possibly be a compound starting with these flags. */
1277 return FALSE;
1278}
1279
1280/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001281 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1282 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001283 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001284 */
1285 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001286valid_word_prefix(
1287 int totprefcnt, /* nr of prefix IDs */
1288 int arridx, /* idx in sl_pidxs[] */
1289 int flags,
1290 char_u *word,
1291 slang_T *slang,
1292 int cond_req) /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001293{
1294 int prefcnt;
1295 int pidx;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001296 regprog_T **rp;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001297 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001298
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001299 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001300 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1301 {
1302 pidx = slang->sl_pidxs[arridx + prefcnt];
1303
1304 /* Check the prefix ID. */
1305 if (prefid != (pidx & 0xff))
1306 continue;
1307
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001308 /* Check if the prefix doesn't combine and the word already has a
1309 * suffix. */
1310 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1311 continue;
1312
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001313 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001314 * stored in the two bytes above the prefix ID byte. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001315 rp = &slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
1316 if (*rp != NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001317 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001318 if (!vim_regexec_prog(rp, FALSE, word, 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001319 continue;
1320 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001321 else if (cond_req)
1322 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001323
Bram Moolenaar53805d12005-08-01 07:08:33 +00001324 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001325 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001326 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001327 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001328}
1329
1330/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001331 * Check if the word at "mip->mi_word" has a matching prefix.
1332 * If it does, then check the following word.
1333 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001334 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1335 * prefix in a compound word.
1336 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001337 * For a match mip->mi_result is updated.
1338 */
1339 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001340find_prefix(matchinf_T *mip, int mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001341{
1342 idx_T arridx = 0;
1343 int len;
1344 int wlen = 0;
1345 int flen;
1346 int c;
1347 char_u *ptr;
1348 idx_T lo, hi, m;
1349 slang_T *slang = mip->mi_lp->lp_slang;
1350 char_u *byts;
1351 idx_T *idxs;
1352
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001353 byts = slang->sl_pbyts;
1354 if (byts == NULL)
1355 return; /* array is empty */
1356
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001357 /* We use the case-folded word here, since prefixes are always
1358 * case-folded. */
1359 ptr = mip->mi_fword;
1360 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001361 if (mode == FIND_COMPOUND)
1362 {
1363 /* Skip over the previously found word(s). */
1364 ptr += mip->mi_compoff;
1365 flen -= mip->mi_compoff;
1366 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001367 idxs = slang->sl_pidxs;
1368
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001369 /*
1370 * Repeat advancing in the tree until:
1371 * - there is a byte that doesn't match,
1372 * - we reach the end of the tree,
1373 * - or we reach the end of the line.
1374 */
1375 for (;;)
1376 {
1377 if (flen == 0 && *mip->mi_fend != NUL)
1378 flen = fold_more(mip);
1379
1380 len = byts[arridx++];
1381
1382 /* If the first possible byte is a zero the prefix could end here.
1383 * Check if the following word matches and supports the prefix. */
1384 if (byts[arridx] == 0)
1385 {
1386 /* There can be several prefixes with different conditions. We
1387 * try them all, since we don't know which one will give the
1388 * longest match. The word is the same each time, pass the list
1389 * of possible prefixes to find_word(). */
1390 mip->mi_prefarridx = arridx;
1391 mip->mi_prefcnt = len;
1392 while (len > 0 && byts[arridx] == 0)
1393 {
1394 ++arridx;
1395 --len;
1396 }
1397 mip->mi_prefcnt -= len;
1398
1399 /* Find the word that comes after the prefix. */
1400 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001401 if (mode == FIND_COMPOUND)
1402 /* Skip over the previously found word(s). */
1403 mip->mi_prefixlen += mip->mi_compoff;
1404
Bram Moolenaar53805d12005-08-01 07:08:33 +00001405 if (has_mbyte)
1406 {
1407 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001408 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1409 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001410 }
1411 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001412 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001413 find_word(mip, FIND_PREFIX);
1414
1415
1416 if (len == 0)
1417 break; /* no children, word must end here */
1418 }
1419
1420 /* Stop looking at end of the line. */
1421 if (ptr[wlen] == NUL)
1422 break;
1423
1424 /* Perform a binary search in the list of accepted bytes. */
1425 c = ptr[wlen];
1426 lo = arridx;
1427 hi = arridx + len - 1;
1428 while (lo < hi)
1429 {
1430 m = (lo + hi) / 2;
1431 if (byts[m] > c)
1432 hi = m - 1;
1433 else if (byts[m] < c)
1434 lo = m + 1;
1435 else
1436 {
1437 lo = hi = m;
1438 break;
1439 }
1440 }
1441
1442 /* Stop if there is no matching byte. */
1443 if (hi < lo || byts[lo] != c)
1444 break;
1445
1446 /* Continue at the child (if there is one). */
1447 arridx = idxs[lo];
1448 ++wlen;
1449 --flen;
1450 }
1451}
1452
1453/*
1454 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001455 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001456 * Return the length of the folded chars in bytes.
1457 */
1458 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001459fold_more(matchinf_T *mip)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001460{
1461 int flen;
1462 char_u *p;
1463
1464 p = mip->mi_fend;
1465 do
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001466 MB_PTR_ADV(mip->mi_fend);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001467 while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001468
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001469 /* Include the non-word character so that we can check for the word end. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001470 if (*mip->mi_fend != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001471 MB_PTR_ADV(mip->mi_fend);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001472
1473 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1474 mip->mi_fword + mip->mi_fwordlen,
1475 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001476 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001477 mip->mi_fwordlen += flen;
1478 return flen;
1479}
1480
1481/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001482 * Check case flags for a word. Return TRUE if the word has the requested
1483 * case.
1484 */
1485 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001486spell_valid_case(
1487 int wordflags, /* flags for the checked word. */
1488 int treeflags) /* flags for the word in the spell tree */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001489{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001490 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001491 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001492 && ((treeflags & WF_ONECAP) == 0
1493 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001494}
1495
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001496/*
1497 * Return TRUE if spell checking is not enabled.
1498 */
1499 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001500no_spell_checking(win_T *wp)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001501{
Bram Moolenaar860cae12010-06-05 23:22:07 +02001502 if (!wp->w_p_spell || *wp->w_s->b_p_spl == NUL
1503 || wp->w_s->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001504 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001505 emsg(_("E756: Spell checking is not enabled"));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001506 return TRUE;
1507 }
1508 return FALSE;
1509}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001510
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001511/*
1512 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001513 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
1514 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001515 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1516 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00001517 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001518 */
1519 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001520spell_move_to(
1521 win_T *wp,
1522 int dir, /* FORWARD or BACKWARD */
1523 int allwords, /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
1524 int curline,
1525 hlf_T *attrp) /* return: attributes of bad word or NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001526 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001527{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001528 linenr_T lnum;
1529 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001530 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001531 char_u *line;
1532 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001533 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001534 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001535 int len;
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001536#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001537 int has_syntax = syntax_present(wp);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001538#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001539 int col;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001540 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001541 char_u *buf = NULL;
1542 int buflen = 0;
1543 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001544 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001545 int found_one = FALSE;
1546 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001547
Bram Moolenaar95529562005-08-25 21:21:38 +00001548 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00001549 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001550
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001551 /*
1552 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00001553 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001554 *
1555 * When searching backwards, we continue in the line to find the last
1556 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001557 *
1558 * We concatenate the start of the next line, so that wrapped words work
1559 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1560 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001561 */
Bram Moolenaar95529562005-08-25 21:21:38 +00001562 lnum = wp->w_cursor.lnum;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001563 CLEAR_POS(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001564
1565 while (!got_int)
1566 {
Bram Moolenaar95529562005-08-25 21:21:38 +00001567 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001568
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001569 len = (int)STRLEN(line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001570 if (buflen < len + MAXWLEN + 2)
1571 {
1572 vim_free(buf);
1573 buflen = len + MAXWLEN + 2;
1574 buf = alloc(buflen);
1575 if (buf == NULL)
1576 break;
1577 }
1578
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001579 /* In first line check first word for Capital. */
1580 if (lnum == 1)
1581 capcol = 0;
1582
1583 /* For checking first word with a capital skip white space. */
1584 if (capcol == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001585 capcol = getwhitecols(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001586 else if (curline && wp == curwin)
1587 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001588 /* For spellbadword(): check if first word needs a capital. */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001589 col = getwhitecols(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001590 if (check_need_cap(lnum, col))
1591 capcol = col;
1592
1593 /* Need to get the line again, may have looked at the previous
1594 * one. */
1595 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1596 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001597
Bram Moolenaar0c405862005-06-22 22:26:26 +00001598 /* Copy the line into "buf" and append the start of the next line if
1599 * possible. */
1600 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00001601 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar5dd95a12006-05-13 12:09:24 +00001602 spell_cat_line(buf + STRLEN(buf),
1603 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001604
1605 p = buf + skip;
1606 endp = buf + len;
1607 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001608 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001609 /* When searching backward don't search after the cursor. Unless
1610 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001611 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00001612 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001613 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00001614 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001615 break;
1616
1617 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001618 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001619 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001620
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001621 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001622 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001623 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001624 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001625 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001626 /* When searching forward only accept a bad word after
1627 * the cursor. */
1628 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001629 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00001630 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001631 && (wrapped
1632 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00001633 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001634 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001635 {
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001636#ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00001637 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001638 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001639 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001640 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00001641 FALSE, &can_spell, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001642 if (!can_spell)
1643 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001644 }
1645 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001646#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001647 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001648
Bram Moolenaar51485f02005-06-04 21:55:20 +00001649 if (can_spell)
1650 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00001651 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001652 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001653 found_pos.col = (int)(p - buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001654 found_pos.coladd = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001655 if (dir == FORWARD)
1656 {
1657 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00001658 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001659 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001660 if (attrp != NULL)
1661 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001662 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001663 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001664 else if (curline)
1665 /* Insert mode completion: put cursor after
1666 * the bad word. */
1667 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001668 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001669 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001670 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00001671 else
1672 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001673 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001674 }
1675
Bram Moolenaar51485f02005-06-04 21:55:20 +00001676 /* advance to character after the word */
1677 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001678 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001679 }
1680
Bram Moolenaar5195e452005-08-19 20:32:47 +00001681 if (dir == BACKWARD && found_pos.lnum != 0)
1682 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001683 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00001684 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001685 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001686 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001687 }
1688
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001689 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001690 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001691
Bram Moolenaard3f78dc2017-02-25 14:21:10 +01001692 /* If we are back at the starting line and searched it again there
1693 * is no match, give up. */
1694 if (lnum == wp->w_cursor.lnum && wrapped)
1695 break;
1696
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001697 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001698 if (dir == BACKWARD)
1699 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001700 if (lnum > 1)
1701 --lnum;
1702 else if (!p_ws)
1703 break; /* at first line and 'nowrapscan' */
1704 else
1705 {
1706 /* Wrap around to the end of the buffer. May search the
1707 * starting line again and accept the last match. */
1708 lnum = wp->w_buffer->b_ml.ml_line_count;
1709 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001710 if (!shortmess(SHM_SEARCH))
1711 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001712 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001713 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001714 }
1715 else
1716 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001717 if (lnum < wp->w_buffer->b_ml.ml_line_count)
1718 ++lnum;
1719 else if (!p_ws)
1720 break; /* at first line and 'nowrapscan' */
1721 else
1722 {
1723 /* Wrap around to the start of the buffer. May search the
1724 * starting line again and accept the first match. */
1725 lnum = 1;
1726 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001727 if (!shortmess(SHM_SEARCH))
1728 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001729 }
1730
1731 /* If we are back at the starting line and there is no match then
1732 * give up. */
Bram Moolenaard3f78dc2017-02-25 14:21:10 +01001733 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001734 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001735
1736 /* Skip the characters at the start of the next line that were
1737 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001738 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001739 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001740 else
1741 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001742
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001743 /* Capcol skips over the inserted space. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001744 --capcol;
1745
1746 /* But after empty line check first word in next line */
1747 if (*skipwhite(line) == NUL)
1748 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001749 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001750
1751 line_breakcheck();
1752 }
1753
Bram Moolenaar0c405862005-06-22 22:26:26 +00001754 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001755 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001756}
1757
1758/*
1759 * For spell checking: concatenate the start of the following line "line" into
1760 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00001761 * Keep the blanks at the start of the next line, this is used in win_line()
1762 * to skip those bytes if the word was OK.
Bram Moolenaar0c405862005-06-22 22:26:26 +00001763 */
1764 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001765spell_cat_line(char_u *buf, char_u *line, int maxlen)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001766{
1767 char_u *p;
1768 int n;
1769
1770 p = skipwhite(line);
1771 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1772 p = skipwhite(p + 1);
1773
1774 if (*p != NUL)
1775 {
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00001776 /* Only worth concatenating if there is something else than spaces to
1777 * concatenate. */
1778 n = (int)(p - line) + 1;
1779 if (n < maxlen - 1)
1780 {
1781 vim_memset(buf, ' ', n);
1782 vim_strncpy(buf + n, p, maxlen - 1 - n);
1783 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00001784 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001785}
1786
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001787/*
1788 * Structure used for the cookie argument of do_in_runtimepath().
1789 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001790typedef struct spelload_S
1791{
1792 char_u sl_lang[MAXWLEN + 1]; /* language name */
1793 slang_T *sl_slang; /* resulting slang_T struct */
1794 int sl_nobreak; /* NOBREAK language found */
1795} spelload_T;
1796
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001797/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001798 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001799 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001800 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001801 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001802spell_load_lang(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001803{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001804 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001805 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001806 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001807 int round;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001808
Bram Moolenaarb765d632005-06-07 21:00:02 +00001809 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001810 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001811 STRCPY(sl.sl_lang, lang);
1812 sl.sl_slang = NULL;
1813 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001814
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001815 /* We may retry when no spell file is found for the language, an
1816 * autocommand may load it then. */
1817 for (round = 1; round <= 2; ++round)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001818 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001819 /*
1820 * Find the first spell file for "lang" in 'runtimepath' and load it.
1821 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001822 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001823#ifdef VMS
1824 "spell/%s_%s.spl",
1825#else
1826 "spell/%s.%s.spl",
1827#endif
1828 lang, spell_enc());
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001829 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001830
1831 if (r == FAIL && *sl.sl_lang != NUL)
1832 {
1833 /* Try loading the ASCII version. */
1834 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001835#ifdef VMS
1836 "spell/%s_ascii.spl",
1837#else
1838 "spell/%s.ascii.spl",
1839#endif
1840 lang);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001841 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001842
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001843 if (r == FAIL && *sl.sl_lang != NUL && round == 1
1844 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
1845 curbuf->b_fname, FALSE, curbuf))
1846 continue;
1847 break;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001848 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001849 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001850 }
1851
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001852 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001853 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001854 smsg(
Bram Moolenaar56f78042010-12-08 17:09:32 +01001855#ifdef VMS
1856 _("Warning: Cannot find word list \"%s_%s.spl\" or \"%s_ascii.spl\""),
1857#else
1858 _("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
1859#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00001860 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001861 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001862 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001863 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001864 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001865 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001866 do_in_runtimepath(fname_enc, DIP_ALL, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001867 }
1868}
1869
1870/*
1871 * Return the encoding used for spell checking: Use 'encoding', except that we
1872 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1873 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001874 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001875spell_enc(void)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001876{
1877
Bram Moolenaarb765d632005-06-07 21:00:02 +00001878 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1879 return p_enc;
Bram Moolenaarb765d632005-06-07 21:00:02 +00001880 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001881}
1882
1883/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001884 * Get the name of the .spl file for the internal wordlist into
1885 * "fname[MAXPATHL]".
1886 */
1887 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001888int_wordlist_spl(char_u *fname)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001889{
Bram Moolenaar56f78042010-12-08 17:09:32 +01001890 vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001891 int_wordlist, spell_enc());
1892}
1893
1894/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001895 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001896 * Caller must fill "sl_next".
1897 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001898 slang_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001899slang_alloc(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001900{
1901 slang_T *lp;
1902
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001903 lp = ALLOC_CLEAR_ONE(slang_T);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001904 if (lp != NULL)
1905 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001906 if (lang != NULL)
1907 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001908 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001909 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001910 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001911 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001912 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001913 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001914
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001915 return lp;
1916}
1917
1918/*
1919 * Free the contents of an slang_T and the structure itself.
1920 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001921 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001922slang_free(slang_T *lp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001923{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001924 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001925 vim_free(lp->sl_fname);
1926 slang_clear(lp);
1927 vim_free(lp);
1928}
1929
1930/*
1931 * Clear an slang_T so that the file can be reloaded.
1932 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001933 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001934slang_clear(slang_T *lp)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001935{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001936 garray_T *gap;
1937 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001938 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001939 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001940 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001941
Bram Moolenaard23a8232018-02-10 18:45:26 +01001942 VIM_CLEAR(lp->sl_fbyts);
1943 VIM_CLEAR(lp->sl_kbyts);
1944 VIM_CLEAR(lp->sl_pbyts);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001945
Bram Moolenaard23a8232018-02-10 18:45:26 +01001946 VIM_CLEAR(lp->sl_fidxs);
1947 VIM_CLEAR(lp->sl_kidxs);
1948 VIM_CLEAR(lp->sl_pidxs);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001949
Bram Moolenaar4770d092006-01-12 23:22:24 +00001950 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001951 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001952 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
1953 while (gap->ga_len > 0)
1954 {
1955 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1956 vim_free(ftp->ft_from);
1957 vim_free(ftp->ft_to);
1958 }
1959 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001960 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001961
1962 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001963 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001964 {
1965 /* "ga_len" is set to 1 without adding an item for latin1 */
1966 if (gap->ga_data != NULL)
1967 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
1968 for (i = 0; i < gap->ga_len; ++i)
1969 vim_free(((int **)gap->ga_data)[i]);
1970 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001971 else
1972 /* SAL items: free salitem_T items */
1973 while (gap->ga_len > 0)
1974 {
1975 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1976 vim_free(smp->sm_lead);
1977 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
1978 vim_free(smp->sm_to);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001979 vim_free(smp->sm_lead_w);
1980 vim_free(smp->sm_oneof_w);
1981 vim_free(smp->sm_to_w);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001982 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001983 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001984
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001985 for (i = 0; i < lp->sl_prefixcnt; ++i)
Bram Moolenaar473de612013-06-08 18:19:48 +02001986 vim_regfree(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001987 lp->sl_prefixcnt = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001988 VIM_CLEAR(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001989
Bram Moolenaard23a8232018-02-10 18:45:26 +01001990 VIM_CLEAR(lp->sl_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001991
Bram Moolenaard23a8232018-02-10 18:45:26 +01001992 VIM_CLEAR(lp->sl_midword);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001993
Bram Moolenaar473de612013-06-08 18:19:48 +02001994 vim_regfree(lp->sl_compprog);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001995 lp->sl_compprog = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001996 VIM_CLEAR(lp->sl_comprules);
1997 VIM_CLEAR(lp->sl_compstartflags);
1998 VIM_CLEAR(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001999
Bram Moolenaard23a8232018-02-10 18:45:26 +01002000 VIM_CLEAR(lp->sl_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002001 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002002
Bram Moolenaar899dddf2006-03-26 21:06:50 +00002003 ga_clear_strings(&lp->sl_comppat);
2004
Bram Moolenaar4770d092006-01-12 23:22:24 +00002005 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2006 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002007
Bram Moolenaar4770d092006-01-12 23:22:24 +00002008 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002009
Bram Moolenaar4770d092006-01-12 23:22:24 +00002010 /* Clear info from .sug file. */
2011 slang_clear_sug(lp);
2012
Bram Moolenaar5195e452005-08-19 20:32:47 +00002013 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002014 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002015 lp->sl_compsylmax = MAXWLEN;
2016 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002017}
2018
2019/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002020 * Clear the info from the .sug file in "lp".
2021 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002022 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002023slang_clear_sug(slang_T *lp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002024{
Bram Moolenaard23a8232018-02-10 18:45:26 +01002025 VIM_CLEAR(lp->sl_sbyts);
2026 VIM_CLEAR(lp->sl_sidxs);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002027 close_spellbuf(lp->sl_sugbuf);
2028 lp->sl_sugbuf = NULL;
2029 lp->sl_sugloaded = FALSE;
2030 lp->sl_sugtime = 0;
2031}
2032
2033/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002034 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002035 * Invoked through do_in_runtimepath().
2036 */
2037 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002038spell_load_cb(char_u *fname, void *cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002039{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002040 spelload_T *slp = (spelload_T *)cookie;
2041 slang_T *slang;
2042
2043 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2044 if (slang != NULL)
2045 {
2046 /* When a previously loaded file has NOBREAK also use it for the
2047 * ".add" files. */
2048 if (slp->sl_nobreak && slang->sl_add)
2049 slang->sl_nobreak = TRUE;
2050 else if (slang->sl_nobreak)
2051 slp->sl_nobreak = TRUE;
2052
2053 slp->sl_slang = slang;
2054 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002055}
2056
Bram Moolenaar4770d092006-01-12 23:22:24 +00002057
2058/*
2059 * Add a word to the hashtable of common words.
2060 * If it's already there then the counter is increased.
2061 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002062 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002063count_common_word(
2064 slang_T *lp,
2065 char_u *word,
2066 int len, /* word length, -1 for upto NUL */
2067 int count) /* 1 to count once, 10 to init */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002068{
2069 hash_T hash;
2070 hashitem_T *hi;
2071 wordcount_T *wc;
2072 char_u buf[MAXWLEN];
2073 char_u *p;
2074
2075 if (len == -1)
2076 p = word;
Bram Moolenaar5bcc5a12019-08-06 22:48:02 +02002077 else if (len >= MAXWLEN)
2078 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002079 else
2080 {
2081 vim_strncpy(buf, word, len);
2082 p = buf;
2083 }
2084
2085 hash = hash_hash(p);
2086 hi = hash_lookup(&lp->sl_wordcount, p, hash);
2087 if (HASHITEM_EMPTY(hi))
2088 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002089 wc = alloc(sizeof(wordcount_T) + STRLEN(p));
Bram Moolenaar4770d092006-01-12 23:22:24 +00002090 if (wc == NULL)
2091 return;
2092 STRCPY(wc->wc_word, p);
2093 wc->wc_count = count;
2094 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
2095 }
2096 else
2097 {
2098 wc = HI2WC(hi);
2099 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
2100 wc->wc_count = MAXWORDCOUNT;
2101 }
2102}
2103
2104/*
2105 * Adjust the score of common words.
2106 */
2107 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002108score_wordcount_adj(
2109 slang_T *slang,
2110 int score,
2111 char_u *word,
2112 int split) /* word was split, less bonus */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002113{
2114 hashitem_T *hi;
2115 wordcount_T *wc;
2116 int bonus;
2117 int newscore;
2118
2119 hi = hash_find(&slang->sl_wordcount, word);
2120 if (!HASHITEM_EMPTY(hi))
2121 {
2122 wc = HI2WC(hi);
2123 if (wc->wc_count < SCORE_THRES2)
2124 bonus = SCORE_COMMON1;
2125 else if (wc->wc_count < SCORE_THRES3)
2126 bonus = SCORE_COMMON2;
2127 else
2128 bonus = SCORE_COMMON3;
2129 if (split)
2130 newscore = score - bonus / 2;
2131 else
2132 newscore = score - bonus;
2133 if (newscore < 0)
2134 return 0;
2135 return newscore;
2136 }
2137 return score;
2138}
2139
Bram Moolenaar5195e452005-08-19 20:32:47 +00002140
Bram Moolenaar6de68532005-08-24 22:08:48 +00002141/*
Bram Moolenaar95529562005-08-25 21:21:38 +00002142 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00002143 * Like strchr() but independent of locale.
2144 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002145 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002146byte_in_str(char_u *str, int n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002147{
2148 char_u *p;
2149
2150 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00002151 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002152 return TRUE;
2153 return FALSE;
2154}
2155
Bram Moolenaar5195e452005-08-19 20:32:47 +00002156#define SY_MAXLEN 30
2157typedef struct syl_item_S
2158{
2159 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
2160 int sy_len;
2161} syl_item_T;
2162
2163/*
2164 * Truncate "slang->sl_syllable" at the first slash and put the following items
2165 * in "slang->sl_syl_items".
2166 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002167 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002168init_syl_tab(slang_T *slang)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002169{
2170 char_u *p;
2171 char_u *s;
2172 int l;
2173 syl_item_T *syl;
2174
2175 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
2176 p = vim_strchr(slang->sl_syllable, '/');
2177 while (p != NULL)
2178 {
2179 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002180 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002181 break;
2182 s = p;
2183 p = vim_strchr(p, '/');
2184 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002185 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002186 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002187 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002188 if (l >= SY_MAXLEN)
2189 return SP_FORMERROR;
2190 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002191 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002192 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
2193 + slang->sl_syl_items.ga_len++;
2194 vim_strncpy(syl->sy_chars, s, l);
2195 syl->sy_len = l;
2196 }
2197 return OK;
2198}
2199
2200/*
2201 * Count the number of syllables in "word".
2202 * When "word" contains spaces the syllables after the last space are counted.
2203 * Returns zero if syllables are not defines.
2204 */
2205 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002206count_syllables(slang_T *slang, char_u *word)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002207{
2208 int cnt = 0;
2209 int skip = FALSE;
2210 char_u *p;
2211 int len;
2212 int i;
2213 syl_item_T *syl;
2214 int c;
2215
2216 if (slang->sl_syllable == NULL)
2217 return 0;
2218
2219 for (p = word; *p != NUL; p += len)
2220 {
2221 /* When running into a space reset counter. */
2222 if (*p == ' ')
2223 {
2224 len = 1;
2225 cnt = 0;
2226 continue;
2227 }
2228
2229 /* Find longest match of syllable items. */
2230 len = 0;
2231 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
2232 {
2233 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
2234 if (syl->sy_len > len
2235 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
2236 len = syl->sy_len;
2237 }
2238 if (len != 0) /* found a match, count syllable */
2239 {
2240 ++cnt;
2241 skip = FALSE;
2242 }
2243 else
2244 {
2245 /* No recognized syllable item, at least a syllable char then? */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002246 c = mb_ptr2char(p);
2247 len = (*mb_ptr2len)(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002248 if (vim_strchr(slang->sl_syllable, c) == NULL)
2249 skip = FALSE; /* No, search for next syllable */
2250 else if (!skip)
2251 {
2252 ++cnt; /* Yes, count it */
2253 skip = TRUE; /* don't count following syllable chars */
2254 }
2255 }
2256 }
2257 return cnt;
2258}
2259
2260/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02002261 * Parse 'spelllang' and set w_s->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002262 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002263 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002264 char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002265did_set_spelllang(win_T *wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002266{
2267 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002268 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002269 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00002270 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002271 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002272 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002273 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002274 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002275 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002276 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002277 int len;
2278 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002279 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002280 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002281 char_u *use_region = NULL;
2282 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002283 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002284 int i, j;
2285 langp_T *lp, *lp2;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002286 static int recursive = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002287 char *ret_msg = NULL;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002288 char_u *spl_copy;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002289 bufref_T bufref;
2290
2291 set_bufref(&bufref, wp->w_buffer);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002292
2293 /* We don't want to do this recursively. May happen when a language is
2294 * not available and the SpellFileMissing autocommand opens a new buffer
2295 * in which 'spell' is set. */
2296 if (recursive)
2297 return NULL;
2298 recursive = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002299
2300 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002301 clear_midword(wp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002302
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002303 /* Make a copy of 'spelllang', the SpellFileMissing autocommands may change
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002304 * it under our fingers. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002305 spl_copy = vim_strsave(wp->w_s->b_p_spl);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002306 if (spl_copy == NULL)
2307 goto theend;
2308
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002309 wp->w_s->b_cjk = 0;
2310
2311 /* Loop over comma separated language names. */
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002312 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002313 {
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002314 // Get one language name.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002315 copy_option_part(&splp, lang, MAXWLEN, ",");
Bram Moolenaar5482f332005-04-17 20:18:43 +00002316 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002317 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002318
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002319 if (!valid_spellang(lang))
2320 continue;
2321
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002322 if (STRCMP(lang, "cjk") == 0)
2323 {
2324 wp->w_s->b_cjk = 1;
2325 continue;
2326 }
2327
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002328 /* If the name ends in ".spl" use it as the name of the spell file.
2329 * If there is a region name let "region" point to it and remove it
2330 * from the name. */
2331 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2332 {
2333 filename = TRUE;
2334
Bram Moolenaarb6356332005-07-18 21:40:44 +00002335 /* Locate a region and remove it from the file name. */
2336 p = vim_strchr(gettail(lang), '_');
2337 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
2338 && !ASCII_ISALPHA(p[3]))
2339 {
2340 vim_strncpy(region_cp, p + 1, 2);
2341 mch_memmove(p, p + 3, len - (p - lang) - 2);
Bram Moolenaarb6356332005-07-18 21:40:44 +00002342 region = region_cp;
2343 }
2344 else
2345 dont_use_region = TRUE;
2346
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002347 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002348 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaar99499b12019-05-23 21:35:48 +02002349 if (fullpathcmp(lang, slang->sl_fname, FALSE, TRUE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002350 break;
2351 }
2352 else
2353 {
2354 filename = FALSE;
2355 if (len > 3 && lang[len - 3] == '_')
2356 {
2357 region = lang + len - 2;
2358 len -= 3;
2359 lang[len] = NUL;
2360 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002361 else
2362 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002363
2364 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002365 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2366 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002367 break;
2368 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002369
Bram Moolenaarb6356332005-07-18 21:40:44 +00002370 if (region != NULL)
2371 {
2372 /* If the region differs from what was used before then don't
2373 * use it for 'spellfile'. */
2374 if (use_region != NULL && STRCMP(region, use_region) != 0)
2375 dont_use_region = TRUE;
2376 use_region = region;
2377 }
2378
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002379 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002380 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002381 {
2382 if (filename)
2383 (void)spell_load_file(lang, lang, NULL, FALSE);
2384 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002385 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002386 spell_load_lang(lang);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002387 /* SpellFileMissing autocommands may do anything, including
2388 * destroying the buffer we are using... */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002389 if (!bufref_valid(&bufref))
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002390 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002391 ret_msg = N_("E797: SpellFileMissing autocommand deleted buffer");
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002392 goto theend;
2393 }
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002394 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002395 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002396
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002397 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002398 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002399 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002400 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaar99499b12019-05-23 21:35:48 +02002401 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE, TRUE)
2402 == FPC_SAME
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002403 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002404 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002405 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002406 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002407 {
2408 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002409 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002410 if (c == REGION_ALL)
2411 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002412 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002413 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002414 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002415 /* This addition file is for other regions. */
2416 region_mask = 0;
2417 }
2418 else
2419 /* This is probably an error. Give a warning and
2420 * accept the words anyway. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002421 smsg(_("Warning: region %s not supported"),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002422 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002423 }
2424 else
2425 region_mask = 1 << c;
2426 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002427
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002428 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002429 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002430 if (ga_grow(&ga, 1) == FAIL)
2431 {
2432 ga_clear(&ga);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002433 ret_msg = e_outofmem;
2434 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002435 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002436 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002437 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2438 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002439 use_midword(slang, wp);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002440 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002441 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002442 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002443 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002444 }
2445
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002446 /* round 0: load int_wordlist, if possible.
2447 * round 1: load first name in 'spellfile'.
2448 * round 2: load second name in 'spellfile.
2449 * etc. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002450 spf = curwin->w_s->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002451 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002452 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002453 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002454 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002455 /* Internal wordlist, if there is one. */
2456 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002457 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002458 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002459 }
2460 else
2461 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002462 /* One entry in 'spellfile'. */
2463 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
2464 STRCAT(spf_name, ".spl");
2465
2466 /* If it was already found above then skip it. */
2467 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002468 {
2469 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
Bram Moolenaar99499b12019-05-23 21:35:48 +02002470 if (p != NULL && fullpathcmp(spf_name, p, FALSE, TRUE)
2471 == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002472 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002473 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002474 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002475 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002476 }
2477
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002478 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002479 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaar99499b12019-05-23 21:35:48 +02002480 if (fullpathcmp(spf_name, slang->sl_fname, FALSE, TRUE)
2481 == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002482 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002483 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002484 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002485 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002486 * region name, the region is ignored otherwise. for int_wordlist
2487 * use an arbitrary name. */
2488 if (round == 0)
2489 STRCPY(lang, "internal wordlist");
2490 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00002491 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002492 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002493 p = vim_strchr(lang, '.');
2494 if (p != NULL)
2495 *p = NUL; /* truncate at ".encoding.add" */
2496 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002497 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002498
2499 /* If one of the languages has NOBREAK we assume the addition
2500 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002501 if (slang != NULL && nobreak)
2502 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002503 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002504 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002505 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002506 region_mask = REGION_ALL;
2507 if (use_region != NULL && !dont_use_region)
2508 {
2509 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002510 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002511 if (c != REGION_ALL)
2512 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002513 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002514 /* This spell file is for other regions. */
2515 region_mask = 0;
2516 }
2517
2518 if (region_mask != 0)
2519 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002520 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
2521 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
2522 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002523 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2524 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002525 use_midword(slang, wp);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002526 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002527 }
2528 }
2529
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002530 /* Everything is fine, store the new b_langp value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002531 ga_clear(&wp->w_s->b_langp);
2532 wp->w_s->b_langp = ga;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002533
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002534 /* For each language figure out what language to use for sound folding and
2535 * REP items. If the language doesn't support it itself use another one
2536 * with the same name. E.g. for "en-math" use "en". */
2537 for (i = 0; i < ga.ga_len; ++i)
2538 {
2539 lp = LANGP_ENTRY(ga, i);
2540
2541 /* sound folding */
2542 if (lp->lp_slang->sl_sal.ga_len > 0)
2543 /* language does sound folding itself */
2544 lp->lp_sallang = lp->lp_slang;
2545 else
2546 /* find first similar language that does sound folding */
2547 for (j = 0; j < ga.ga_len; ++j)
2548 {
2549 lp2 = LANGP_ENTRY(ga, j);
2550 if (lp2->lp_slang->sl_sal.ga_len > 0
2551 && STRNCMP(lp->lp_slang->sl_name,
2552 lp2->lp_slang->sl_name, 2) == 0)
2553 {
2554 lp->lp_sallang = lp2->lp_slang;
2555 break;
2556 }
2557 }
2558
2559 /* REP items */
2560 if (lp->lp_slang->sl_rep.ga_len > 0)
2561 /* language has REP items itself */
2562 lp->lp_replang = lp->lp_slang;
2563 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00002564 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002565 for (j = 0; j < ga.ga_len; ++j)
2566 {
2567 lp2 = LANGP_ENTRY(ga, j);
2568 if (lp2->lp_slang->sl_rep.ga_len > 0
2569 && STRNCMP(lp->lp_slang->sl_name,
2570 lp2->lp_slang->sl_name, 2) == 0)
2571 {
2572 lp->lp_replang = lp2->lp_slang;
2573 break;
2574 }
2575 }
2576 }
2577
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002578theend:
2579 vim_free(spl_copy);
2580 recursive = FALSE;
Bram Moolenaarbe578ed2014-05-13 14:03:40 +02002581 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002582 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002583}
2584
2585/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002586 * Clear the midword characters for buffer "buf".
2587 */
2588 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002589clear_midword(win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002590{
Bram Moolenaar860cae12010-06-05 23:22:07 +02002591 vim_memset(wp->w_s->b_spell_ismw, 0, 256);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002592 VIM_CLEAR(wp->w_s->b_spell_ismw_mb);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002593}
2594
2595/*
2596 * Use the "sl_midword" field of language "lp" for buffer "buf".
2597 * They add up to any currently used midword characters.
2598 */
2599 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002600use_midword(slang_T *lp, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002601{
2602 char_u *p;
2603
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002604 if (lp->sl_midword == NULL) /* there aren't any */
2605 return;
2606
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002607 for (p = lp->sl_midword; *p != NUL; )
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002608 if (has_mbyte)
2609 {
2610 int c, l, n;
2611 char_u *bp;
2612
2613 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002614 l = (*mb_ptr2len)(p);
2615 if (c < 256 && l <= 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002616 wp->w_s->b_spell_ismw[c] = TRUE;
2617 else if (wp->w_s->b_spell_ismw_mb == NULL)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002618 /* First multi-byte char in "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002619 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002620 else
2621 {
2622 /* Append multi-byte chars to "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002623 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
2624 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002625 if (bp != NULL)
2626 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002627 vim_free(wp->w_s->b_spell_ismw_mb);
2628 wp->w_s->b_spell_ismw_mb = bp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002629 vim_strncpy(bp + n, p, l);
2630 }
2631 }
2632 p += l;
2633 }
2634 else
Bram Moolenaar860cae12010-06-05 23:22:07 +02002635 wp->w_s->b_spell_ismw[*p++] = TRUE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002636}
2637
2638/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002639 * Find the region "region[2]" in "rp" (points to "sl_regions").
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01002640 * Each region is simply stored as the two characters of its name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002641 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002642 */
2643 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002644find_region(char_u *rp, char_u *region)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002645{
2646 int i;
2647
2648 for (i = 0; ; i += 2)
2649 {
2650 if (rp[i] == NUL)
2651 return REGION_ALL;
2652 if (rp[i] == region[0] && rp[i + 1] == region[1])
2653 break;
2654 }
2655 return i / 2;
2656}
2657
2658/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002659 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002660 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002661 * Word WF_ONECAP
2662 * W WORD WF_ALLCAP
2663 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002664 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002665 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002666captype(
2667 char_u *word,
2668 char_u *end) /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002669{
2670 char_u *p;
2671 int c;
2672 int firstcap;
2673 int allcap;
2674 int past_second = FALSE; /* past second word char */
2675
2676 /* find first letter */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002677 for (p = word; !spell_iswordp_nmw(p, curwin); MB_PTR_ADV(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002678 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002679 return 0; /* only non-word characters, illegal word */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002680 if (has_mbyte)
2681 c = mb_ptr2char_adv(&p);
2682 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00002683 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002684 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002685
2686 /*
2687 * Need to check all letters to find a word with mixed upper/lower.
2688 * But a word with an upper char only at start is a ONECAP.
2689 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002690 for ( ; end == NULL ? *p != NUL : p < end; MB_PTR_ADV(p))
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002691 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002692 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002693 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002694 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002695 {
2696 /* UUl -> KEEPCAP */
2697 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002698 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002699 allcap = FALSE;
2700 }
2701 else if (!allcap)
2702 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002703 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002704 past_second = TRUE;
2705 }
2706
2707 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002708 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002709 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002710 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002711 return 0;
2712}
2713
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002714/*
2715 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
2716 * capital. So that make_case_word() can turn WOrd into Word.
2717 * Add ALLCAP for "WOrD".
2718 */
2719 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002720badword_captype(char_u *word, char_u *end)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002721{
2722 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002723 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002724 int l, u;
2725 int first;
2726 char_u *p;
2727
2728 if (flags & WF_KEEPCAP)
2729 {
2730 /* Count the number of UPPER and lower case letters. */
2731 l = u = 0;
2732 first = FALSE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002733 for (p = word; p < end; MB_PTR_ADV(p))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002734 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002735 c = PTR2CHAR(p);
2736 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002737 {
2738 ++u;
2739 if (p == word)
2740 first = TRUE;
2741 }
2742 else
2743 ++l;
2744 }
2745
2746 /* If there are more UPPER than lower case letters suggest an
2747 * ALLCAP word. Otherwise, if the first letter is UPPER then
2748 * suggest ONECAP. Exception: "ALl" most likely should be "All",
2749 * require three upper case letters. */
2750 if (u > l && u > 2)
2751 flags |= WF_ALLCAP;
2752 else if (first)
2753 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002754
2755 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
2756 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002757 }
2758 return flags;
2759}
2760
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002761/*
2762 * Delete the internal wordlist and its .spl file.
2763 */
2764 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002765spell_delete_wordlist(void)
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002766{
2767 char_u fname[MAXPATHL];
2768
2769 if (int_wordlist != NULL)
2770 {
2771 mch_remove(int_wordlist);
2772 int_wordlist_spl(fname);
2773 mch_remove(fname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002774 VIM_CLEAR(int_wordlist);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002775 }
2776}
2777
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002778/*
2779 * Free all languages.
2780 */
2781 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002782spell_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002783{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002784 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002785 buf_T *buf;
2786
Bram Moolenaar60bb4e12010-09-18 13:36:49 +02002787 /* Go through all buffers and handle 'spelllang'. <VN> */
Bram Moolenaar29323592016-07-24 22:04:11 +02002788 FOR_ALL_BUFFERS(buf)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002789 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002790
2791 while (first_lang != NULL)
2792 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002793 slang = first_lang;
2794 first_lang = slang->sl_next;
2795 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002796 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002797
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002798 spell_delete_wordlist();
Bram Moolenaar7887d882005-07-01 22:33:52 +00002799
Bram Moolenaard23a8232018-02-10 18:45:26 +01002800 VIM_CLEAR(repl_to);
2801 VIM_CLEAR(repl_from);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002802}
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002803
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002804/*
2805 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002806 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002807 */
2808 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002809spell_reload(void)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002810{
Bram Moolenaar3982c542005-06-08 21:56:31 +00002811 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002812
Bram Moolenaarea408852005-06-25 22:49:46 +00002813 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002814 init_spell_chartab();
2815
2816 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002817 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002818
2819 /* Go through all buffers and handle 'spelllang'. */
Bram Moolenaar29323592016-07-24 22:04:11 +02002820 FOR_ALL_WINDOWS(wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002821 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002822 /* Only load the wordlists when 'spelllang' is set and there is a
2823 * window for this buffer in which 'spell' is set. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002824 if (*wp->w_s->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002825 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002826 if (wp->w_p_spell)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002827 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002828 (void)did_set_spelllang(wp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00002829 break;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002830 }
2831 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002832 }
2833}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002834
Bram Moolenaarb765d632005-06-07 21:00:02 +00002835/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002836 * Opposite of offset2bytes().
2837 * "pp" points to the bytes and is advanced over it.
2838 * Returns the offset.
2839 */
2840 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002841bytes2offset(char_u **pp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002842{
2843 char_u *p = *pp;
2844 int nr;
2845 int c;
2846
2847 c = *p++;
2848 if ((c & 0x80) == 0x00) /* 1 byte */
2849 {
2850 nr = c - 1;
2851 }
2852 else if ((c & 0xc0) == 0x80) /* 2 bytes */
2853 {
2854 nr = (c & 0x3f) - 1;
2855 nr = nr * 255 + (*p++ - 1);
2856 }
2857 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
2858 {
2859 nr = (c & 0x1f) - 1;
2860 nr = nr * 255 + (*p++ - 1);
2861 nr = nr * 255 + (*p++ - 1);
2862 }
2863 else /* 4 bytes */
2864 {
2865 nr = (c & 0x0f) - 1;
2866 nr = nr * 255 + (*p++ - 1);
2867 nr = nr * 255 + (*p++ - 1);
2868 nr = nr * 255 + (*p++ - 1);
2869 }
2870
2871 *pp = p;
2872 return nr;
2873}
2874
Bram Moolenaar4770d092006-01-12 23:22:24 +00002875
2876/*
2877 * Open a spell buffer. This is a nameless buffer that is not in the buffer
2878 * list and only contains text lines. Can use a swapfile to reduce memory
2879 * use.
2880 * Most other fields are invalid! Esp. watch out for string options being
2881 * NULL and there is no undo info.
2882 * Returns NULL when out of memory.
2883 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002884 buf_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002885open_spellbuf(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002886{
2887 buf_T *buf;
2888
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002889 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002890 if (buf != NULL)
2891 {
2892 buf->b_spell = TRUE;
2893 buf->b_p_swf = TRUE; /* may create a swap file */
Bram Moolenaar706d2de2013-07-17 17:35:13 +02002894#ifdef FEAT_CRYPT
2895 buf->b_p_key = empty_option;
2896#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00002897 ml_open(buf);
2898 ml_open_file(buf); /* create swap file now */
2899 }
2900 return buf;
2901}
2902
2903/*
2904 * Close the buffer used for spell info.
2905 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002906 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002907close_spellbuf(buf_T *buf)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002908{
2909 if (buf != NULL)
2910 {
2911 ml_close(buf, TRUE);
2912 vim_free(buf);
2913 }
2914}
2915
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002916/*
2917 * Init the chartab used for spelling for ASCII.
2918 * EBCDIC is not supported!
2919 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002920 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002921clear_spell_chartab(spelltab_T *sp)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002922{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002923 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002924
2925 /* Init everything to FALSE. */
2926 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
2927 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
2928 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002929 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002930 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002931 sp->st_upper[i] = i;
2932 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002933
2934 /* We include digits. A word shouldn't start with a digit, but handling
2935 * that is done separately. */
2936 for (i = '0'; i <= '9'; ++i)
2937 sp->st_isw[i] = TRUE;
2938 for (i = 'A'; i <= 'Z'; ++i)
2939 {
2940 sp->st_isw[i] = TRUE;
2941 sp->st_isu[i] = TRUE;
2942 sp->st_fold[i] = i + 0x20;
2943 }
2944 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002945 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002946 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002947 sp->st_upper[i] = i - 0x20;
2948 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002949}
2950
2951/*
2952 * Init the chartab used for spelling. Only depends on 'encoding'.
2953 * Called once while starting up and when 'encoding' changes.
2954 * The default is to use isalpha(), but the spell file should define the word
2955 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002956 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002957 */
2958 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002959init_spell_chartab(void)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002960{
2961 int i;
2962
2963 did_set_spelltab = FALSE;
2964 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002965 if (enc_dbcs)
2966 {
2967 /* DBCS: assume double-wide characters are word characters. */
2968 for (i = 128; i <= 255; ++i)
2969 if (MB_BYTE2LEN(i) == 2)
2970 spelltab.st_isw[i] = TRUE;
2971 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002972 else if (enc_utf8)
2973 {
2974 for (i = 128; i < 256; ++i)
2975 {
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02002976 int f = utf_fold(i);
2977 int u = utf_toupper(i);
2978
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002979 spelltab.st_isu[i] = utf_isupper(i);
2980 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02002981 /* The folded/upper-cased value is different between latin1 and
2982 * utf8 for 0xb5, causing E763 for no good reason. Use the latin1
2983 * value for utf-8 to avoid this. */
2984 spelltab.st_fold[i] = (f < 256) ? f : i;
2985 spelltab.st_upper[i] = (u < 256) ? u : i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002986 }
2987 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002988 else
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002989 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002990 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002991 for (i = 128; i < 256; ++i)
2992 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002993 if (MB_ISUPPER(i))
2994 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002995 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002996 spelltab.st_isu[i] = TRUE;
2997 spelltab.st_fold[i] = MB_TOLOWER(i);
2998 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002999 else if (MB_ISLOWER(i))
3000 {
3001 spelltab.st_isw[i] = TRUE;
3002 spelltab.st_upper[i] = MB_TOUPPER(i);
3003 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003004 }
3005 }
3006}
3007
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003008
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003009/*
Bram Moolenaarea408852005-06-25 22:49:46 +00003010 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003011 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00003012 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003013 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00003014 */
3015 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003016spell_iswordp(
3017 char_u *p,
3018 win_T *wp) /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00003019{
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003020 char_u *s;
3021 int l;
3022 int c;
3023
3024 if (has_mbyte)
3025 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02003026 l = MB_PTR2LEN(p);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003027 s = p;
3028 if (l == 1)
3029 {
3030 /* be quick for ASCII */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003031 if (wp->w_s->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003032 s = p + 1; /* skip a mid-word character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003033 }
3034 else
3035 {
3036 c = mb_ptr2char(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003037 if (c < 256 ? wp->w_s->b_spell_ismw[c]
3038 : (wp->w_s->b_spell_ismw_mb != NULL
3039 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003040 s = p + l;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003041 }
3042
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003043 c = mb_ptr2char(s);
3044 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003045 return spell_mb_isword_class(mb_get_class(s), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003046 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003047 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003048
Bram Moolenaar860cae12010-06-05 23:22:07 +02003049 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003050}
3051
3052/*
3053 * Return TRUE if "p" points to a word character.
3054 * Unlike spell_iswordp() this doesn't check for "midword" characters.
3055 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003056 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003057spell_iswordp_nmw(char_u *p, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003058{
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003059 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003060
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003061 if (has_mbyte)
3062 {
3063 c = mb_ptr2char(p);
3064 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003065 return spell_mb_isword_class(mb_get_class(p), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003066 return spelltab.st_isw[c];
3067 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003068 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00003069}
3070
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003071/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003072 * Return TRUE if word class indicates a word character.
3073 * Only for characters above 255.
3074 * Unicode subscript and superscript are not considered word characters.
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003075 * See also dbcs_class() and utf_class() in mbyte.c.
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003076 */
3077 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003078spell_mb_isword_class(int cl, win_T *wp)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003079{
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003080 if (wp->w_s->b_cjk)
3081 /* East Asian characters are not considered word characters. */
3082 return cl == 2 || cl == 0x2800;
Bram Moolenaar06e63772019-07-19 23:04:34 +02003083 return cl >= 2 && cl != 0x2070 && cl != 0x2080 && cl != 3;
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003084}
3085
3086/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003087 * Return TRUE if "p" points to a word character.
3088 * Wide version of spell_iswordp().
3089 */
3090 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003091spell_iswordp_w(int *p, win_T *wp)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003092{
3093 int *s;
3094
Bram Moolenaar860cae12010-06-05 23:22:07 +02003095 if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
3096 : (wp->w_s->b_spell_ismw_mb != NULL
3097 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003098 s = p + 1;
3099 else
3100 s = p;
3101
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003102 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003103 {
3104 if (enc_utf8)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003105 return spell_mb_isword_class(utf_class(*s), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003106 if (enc_dbcs)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003107 return spell_mb_isword_class(
3108 dbcs_class((unsigned)*s >> 8, *s & 0xff), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003109 return 0;
3110 }
3111 return spelltab.st_isw[*s];
3112}
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003113
Bram Moolenaarea408852005-06-25 22:49:46 +00003114/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003115 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
3116 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003117 * When using a multi-byte 'encoding' the length may change!
3118 * Returns FAIL when something wrong.
3119 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003120 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003121spell_casefold(
3122 char_u *str,
3123 int len,
3124 char_u *buf,
3125 int buflen)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003126{
3127 int i;
3128
3129 if (len >= buflen)
3130 {
3131 buf[0] = NUL;
3132 return FAIL; /* result will not fit */
3133 }
3134
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003135 if (has_mbyte)
3136 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003137 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003138 char_u *p;
3139 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003140
3141 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003142 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003143 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003144 if (outi + MB_MAXBYTES > buflen)
3145 {
3146 buf[outi] = NUL;
3147 return FAIL;
3148 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003149 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003150 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003151 }
3152 buf[outi] = NUL;
3153 }
3154 else
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003155 {
3156 /* Be quick for non-multibyte encodings. */
3157 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003158 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003159 buf[i] = NUL;
3160 }
3161
3162 return OK;
3163}
3164
Bram Moolenaar4770d092006-01-12 23:22:24 +00003165/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003166#define SPS_BEST 1
3167#define SPS_FAST 2
3168#define SPS_DOUBLE 4
3169
Bram Moolenaar4770d092006-01-12 23:22:24 +00003170static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
3171static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003172
3173/*
3174 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00003175 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003176 */
3177 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003178spell_check_sps(void)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003179{
3180 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003181 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003182 char_u buf[MAXPATHL];
3183 int f;
3184
3185 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003186 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003187
3188 for (p = p_sps; *p != NUL; )
3189 {
3190 copy_option_part(&p, buf, MAXPATHL, ",");
3191
3192 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003193 if (VIM_ISDIGIT(*buf))
3194 {
3195 s = buf;
3196 sps_limit = getdigits(&s);
3197 if (*s != NUL && !VIM_ISDIGIT(*s))
3198 f = -1;
3199 }
3200 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003201 f = SPS_BEST;
3202 else if (STRCMP(buf, "fast") == 0)
3203 f = SPS_FAST;
3204 else if (STRCMP(buf, "double") == 0)
3205 f = SPS_DOUBLE;
3206 else if (STRNCMP(buf, "expr:", 5) != 0
3207 && STRNCMP(buf, "file:", 5) != 0)
3208 f = -1;
3209
3210 if (f == -1 || (sps_flags != 0 && f != 0))
3211 {
3212 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003213 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003214 return FAIL;
3215 }
3216 if (f != 0)
3217 sps_flags = f;
3218 }
3219
3220 if (sps_flags == 0)
3221 sps_flags = SPS_BEST;
3222
3223 return OK;
3224}
3225
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003226/*
Bram Moolenaar134bf072013-09-25 18:54:24 +02003227 * "z=": Find badly spelled word under or after the cursor.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003228 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003229 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00003230 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003231 */
3232 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003233spell_suggest(int count)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003234{
3235 char_u *line;
3236 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003237 char_u wcopy[MAXWLEN + 2];
3238 char_u *p;
3239 int i;
3240 int c;
3241 suginfo_T sug;
3242 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003243 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003244 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003245 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003246 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003247 int badlen = 0;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003248 int msg_scroll_save = msg_scroll;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003249
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003250 if (no_spell_checking(curwin))
3251 return;
3252
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003253 if (VIsual_active)
3254 {
3255 /* Use the Visually selected text as the bad word. But reject
3256 * a multi-line selection. */
3257 if (curwin->w_cursor.lnum != VIsual.lnum)
3258 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003259 vim_beep(BO_SPELL);
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003260 return;
3261 }
3262 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
3263 if (badlen < 0)
3264 badlen = -badlen;
3265 else
3266 curwin->w_cursor.col = VIsual.col;
3267 ++badlen;
3268 end_visual_mode();
3269 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003270 /* Find the start of the badly spelled word. */
3271 else if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +00003272 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003273 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003274 /* No bad word or it starts after the cursor: use the word under the
3275 * cursor. */
3276 curwin->w_cursor = prev_cursor;
3277 line = ml_get_curline();
3278 p = line + curwin->w_cursor.col;
3279 /* Backup to before start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003280 while (p > line && spell_iswordp_nmw(p, curwin))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003281 MB_PTR_BACK(line, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003282 /* Forward to start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003283 while (*p != NUL && !spell_iswordp_nmw(p, curwin))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003284 MB_PTR_ADV(p);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003285
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003286 if (!spell_iswordp_nmw(p, curwin)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003287 {
3288 beep_flush();
3289 return;
3290 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003291 curwin->w_cursor.col = (colnr_T)(p - line);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003292 }
3293
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003294 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003295
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003296 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003297 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003298
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01003299 /* Make a copy of current line since autocommands may free the line. */
3300 line = vim_strsave(ml_get_curline());
3301 if (line == NULL)
3302 goto skip;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003303
Bram Moolenaar5195e452005-08-19 20:32:47 +00003304 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
3305 * 'spellsuggest', whatever is smaller. */
3306 if (sps_limit > (int)Rows - 2)
3307 limit = (int)Rows - 2;
3308 else
3309 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003310 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003311 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003312
3313 if (sug.su_ga.ga_len == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003314 msg(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +00003315 else if (count > 0)
3316 {
3317 if (count > sug.su_ga.ga_len)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003318 smsg(_("Sorry, only %ld suggestions"),
Bram Moolenaard12a1322005-08-21 22:08:24 +00003319 (long)sug.su_ga.ga_len);
3320 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003321 else
3322 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01003323 VIM_CLEAR(repl_from);
3324 VIM_CLEAR(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003325
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003326#ifdef FEAT_RIGHTLEFT
3327 /* When 'rightleft' is set the list is drawn right-left. */
3328 cmdmsg_rl = curwin->w_p_rl;
3329 if (cmdmsg_rl)
3330 msg_col = Columns - 1;
3331#endif
3332
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003333 /* List the suggestions. */
3334 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +00003335 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003336 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003337 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
3338 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003339#ifdef FEAT_RIGHTLEFT
3340 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
3341 {
3342 /* And now the rabbit from the high hat: Avoid showing the
3343 * untranslated message rightleft. */
3344 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
3345 sug.su_badlen, sug.su_badptr);
3346 }
3347#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01003348 msg_puts((char *)IObuff);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003349 msg_clr_eos();
3350 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00003351
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003352 msg_scroll = TRUE;
3353 for (i = 0; i < sug.su_ga.ga_len; ++i)
3354 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003355 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003356
3357 /* The suggested word may replace only part of the bad word, add
3358 * the not replaced part. */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003359 vim_strncpy(wcopy, stp->st_word, MAXWLEN);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003360 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003361 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003362 sug.su_badptr + stp->st_orglen,
3363 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003364 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
3365#ifdef FEAT_RIGHTLEFT
3366 if (cmdmsg_rl)
3367 rl_mirror(IObuff);
3368#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01003369 msg_puts((char *)IObuff);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003370
3371 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003372 msg_puts((char *)IObuff);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003373
3374 /* The word may replace more than "su_badlen". */
3375 if (sug.su_badlen < stp->st_orglen)
3376 {
3377 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
3378 stp->st_orglen, sug.su_badptr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003379 msg_puts((char *)IObuff);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003380 }
3381
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003382 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003383 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003384 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003385 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003386 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003387 stp->st_salscore ? "s " : "",
3388 stp->st_score, stp->st_altscore);
3389 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003390 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00003391 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003392#ifdef FEAT_RIGHTLEFT
3393 if (cmdmsg_rl)
3394 /* Mirror the numbers, but keep the leading space. */
3395 rl_mirror(IObuff + 1);
3396#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00003397 msg_advance(30);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003398 msg_puts((char *)IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003399 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003400 msg_putchar('\n');
3401 }
3402
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003403#ifdef FEAT_RIGHTLEFT
3404 cmdmsg_rl = FALSE;
3405 msg_col = 0;
3406#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003407 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00003408 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003409 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +00003410 selected -= lines_left;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003411 lines_left = Rows; /* avoid more prompt */
3412 /* don't delay for 'smd' in normal_cmd() */
3413 msg_scroll = msg_scroll_save;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003414 }
3415
Bram Moolenaard12a1322005-08-21 22:08:24 +00003416 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
3417 {
3418 /* Save the from and to text for :spellrepall. */
3419 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003420 if (sug.su_badlen > stp->st_orglen)
3421 {
3422 /* Replacing less than "su_badlen", append the remainder to
3423 * repl_to. */
3424 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
3425 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
3426 sug.su_badlen - stp->st_orglen,
3427 sug.su_badptr + stp->st_orglen);
3428 repl_to = vim_strsave(IObuff);
3429 }
3430 else
3431 {
3432 /* Replacing su_badlen or more, use the whole word. */
3433 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
3434 repl_to = vim_strsave(stp->st_word);
3435 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00003436
3437 /* Replace the word. */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003438 p = alloc(STRLEN(line) - stp->st_orglen + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003439 if (p != NULL)
3440 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003441 c = (int)(sug.su_badptr - line);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003442 mch_memmove(p, line, c);
3443 STRCPY(p + c, stp->st_word);
3444 STRCAT(p, sug.su_badptr + stp->st_orglen);
3445 ml_replace(curwin->w_cursor.lnum, p, FALSE);
3446 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003447
3448 /* For redo we use a change-word command. */
3449 ResetRedobuff();
3450 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +00003451 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003452 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003453 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003454
3455 /* After this "p" may be invalid. */
3456 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003457 }
3458 }
3459 else
3460 curwin->w_cursor = prev_cursor;
3461
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003462 spell_find_cleanup(&sug);
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01003463skip:
3464 vim_free(line);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003465}
3466
3467/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003468 * Check if the word at line "lnum" column "col" is required to start with a
3469 * capital. This uses 'spellcapcheck' of the current buffer.
3470 */
3471 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003472check_need_cap(linenr_T lnum, colnr_T col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003473{
3474 int need_cap = FALSE;
3475 char_u *line;
3476 char_u *line_copy = NULL;
3477 char_u *p;
3478 colnr_T endcol;
3479 regmatch_T regmatch;
3480
Bram Moolenaar860cae12010-06-05 23:22:07 +02003481 if (curwin->w_s->b_cap_prog == NULL)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003482 return FALSE;
3483
3484 line = ml_get_curline();
3485 endcol = 0;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003486 if (getwhitecols(line) >= (int)col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003487 {
3488 /* At start of line, check if previous line is empty or sentence
3489 * ends there. */
3490 if (lnum == 1)
3491 need_cap = TRUE;
3492 else
3493 {
3494 line = ml_get(lnum - 1);
3495 if (*skipwhite(line) == NUL)
3496 need_cap = TRUE;
3497 else
3498 {
3499 /* Append a space in place of the line break. */
3500 line_copy = concat_str(line, (char_u *)" ");
3501 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003502 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003503 }
3504 }
3505 }
3506 else
3507 endcol = col;
3508
3509 if (endcol > 0)
3510 {
3511 /* Check if sentence ends before the bad word. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003512 regmatch.regprog = curwin->w_s->b_cap_prog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003513 regmatch.rm_ic = FALSE;
3514 p = line + endcol;
3515 for (;;)
3516 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003517 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003518 if (p == line || spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003519 break;
3520 if (vim_regexec(&regmatch, p, 0)
3521 && regmatch.endp[0] == line + endcol)
3522 {
3523 need_cap = TRUE;
3524 break;
3525 }
3526 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003527 curwin->w_s->b_cap_prog = regmatch.regprog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003528 }
3529
3530 vim_free(line_copy);
3531
3532 return need_cap;
3533}
3534
3535
3536/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003537 * ":spellrepall"
3538 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003539 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003540ex_spellrepall(exarg_T *eap UNUSED)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003541{
3542 pos_T pos = curwin->w_cursor;
3543 char_u *frompat;
3544 int addlen;
3545 char_u *line;
3546 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003547 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003548 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003549
3550 if (repl_from == NULL || repl_to == NULL)
3551 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003552 emsg(_("E752: No previous spell replacement"));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003553 return;
3554 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003555 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003556
Bram Moolenaar964b3742019-05-24 18:54:09 +02003557 frompat = alloc(STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003558 if (frompat == NULL)
3559 return;
3560 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
3561 p_ws = FALSE;
3562
Bram Moolenaar5195e452005-08-19 20:32:47 +00003563 sub_nsubs = 0;
3564 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003565 curwin->w_cursor.lnum = 0;
3566 while (!got_int)
3567 {
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003568 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003569 || u_save_cursor() == FAIL)
3570 break;
3571
3572 /* Only replace when the right word isn't there yet. This happens
3573 * when changing "etc" to "etc.". */
3574 line = ml_get_curline();
3575 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
3576 repl_to, STRLEN(repl_to)) != 0)
3577 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02003578 p = alloc(STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003579 if (p == NULL)
3580 break;
3581 mch_memmove(p, line, curwin->w_cursor.col);
3582 STRCPY(p + curwin->w_cursor.col, repl_to);
3583 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
3584 ml_replace(curwin->w_cursor.lnum, p, FALSE);
3585 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003586
3587 if (curwin->w_cursor.lnum != prev_lnum)
3588 {
3589 ++sub_nlines;
3590 prev_lnum = curwin->w_cursor.lnum;
3591 }
3592 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003593 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003594 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003595 }
3596
3597 p_ws = save_ws;
3598 curwin->w_cursor = pos;
3599 vim_free(frompat);
3600
Bram Moolenaar5195e452005-08-19 20:32:47 +00003601 if (sub_nsubs == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003602 semsg(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003603 else
3604 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003605}
3606
3607/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003608 * Find spell suggestions for "word". Return them in the growarray "*gap" as
3609 * a list of allocated strings.
3610 */
3611 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003612spell_suggest_list(
3613 garray_T *gap,
3614 char_u *word,
3615 int maxcount, /* maximum nr of suggestions */
3616 int need_cap, /* 'spellcapcheck' matched */
3617 int interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003618{
3619 suginfo_T sug;
3620 int i;
3621 suggest_T *stp;
3622 char_u *wcopy;
3623
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003624 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003625
3626 /* Make room in "gap". */
3627 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003628 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003629 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003630 for (i = 0; i < sug.su_ga.ga_len; ++i)
3631 {
3632 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003633
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003634 /* The suggested word may replace only part of "word", add the not
3635 * replaced part. */
3636 wcopy = alloc(stp->st_wordlen
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003637 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003638 if (wcopy == NULL)
3639 break;
3640 STRCPY(wcopy, stp->st_word);
3641 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
3642 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
3643 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003644 }
3645
3646 spell_find_cleanup(&sug);
3647}
3648
3649/*
3650 * Find spell suggestions for the word at the start of "badptr".
3651 * Return the suggestions in "su->su_ga".
3652 * The maximum number of suggestions is "maxcount".
3653 * Note: does use info for the current window.
3654 * This is based on the mechanisms of Aspell, but completely reimplemented.
3655 */
3656 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003657spell_find_suggest(
3658 char_u *badptr,
3659 int badlen, /* length of bad word or 0 if unknown */
3660 suginfo_T *su,
3661 int maxcount,
3662 int banbadword, /* don't include badword in suggestions */
3663 int need_cap, /* word should start with capital */
3664 int interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003665{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003666 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003667 char_u buf[MAXPATHL];
3668 char_u *p;
3669 int do_combine = FALSE;
3670 char_u *sps_copy;
3671#ifdef FEAT_EVAL
3672 static int expr_busy = FALSE;
3673#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003674 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003675 int i;
3676 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003677
3678 /*
3679 * Set the info in "*su".
3680 */
3681 vim_memset(su, 0, sizeof(suginfo_T));
3682 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
3683 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003684 if (*badptr == NUL)
3685 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003686 hash_init(&su->su_banned);
3687
3688 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003689 if (badlen != 0)
3690 su->su_badlen = badlen;
3691 else
3692 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003693 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003694 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003695
3696 if (su->su_badlen >= MAXWLEN)
3697 su->su_badlen = MAXWLEN - 1; /* just in case */
3698 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
3699 (void)spell_casefold(su->su_badptr, su->su_badlen,
3700 su->su_fbadword, MAXWLEN);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02003701 /* TODO: make this work if the case-folded text is longer than the original
3702 * text. Currently an illegal byte causes wrong pointer computations. */
3703 su->su_fbadword[su->su_badlen] = NUL;
3704
Bram Moolenaar0c405862005-06-22 22:26:26 +00003705 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003706 su->su_badflags = badword_captype(su->su_badptr,
3707 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003708 if (need_cap)
3709 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003710
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003711 /* Find the default language for sound folding. We simply use the first
3712 * one in 'spelllang' that supports sound folding. That's good for when
3713 * using multiple files for one language, it's not that bad when mixing
3714 * languages (e.g., "pl,en"). */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003715 for (i = 0; i < curbuf->b_s.b_langp.ga_len; ++i)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003716 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003717 lp = LANGP_ENTRY(curbuf->b_s.b_langp, i);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003718 if (lp->lp_sallang != NULL)
3719 {
3720 su->su_sallang = lp->lp_sallang;
3721 break;
3722 }
3723 }
3724
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003725 /* Soundfold the bad word with the default sound folding, so that we don't
3726 * have to do this many times. */
3727 if (su->su_sallang != NULL)
3728 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
3729 su->su_sal_badword);
3730
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003731 /* If the word is not capitalised and spell_check() doesn't consider the
3732 * word to be bad then it might need to be capitalised. Add a suggestion
3733 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003734 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003735 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003736 {
3737 make_case_word(su->su_badword, buf, WF_ONECAP);
3738 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003739 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003740 }
3741
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003742 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00003743 if (banbadword)
3744 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003745
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003746 /* Make a copy of 'spellsuggest', because the expression may change it. */
3747 sps_copy = vim_strsave(p_sps);
3748 if (sps_copy == NULL)
3749 return;
3750
3751 /* Loop over the items in 'spellsuggest'. */
3752 for (p = sps_copy; *p != NUL; )
3753 {
3754 copy_option_part(&p, buf, MAXPATHL, ",");
3755
3756 if (STRNCMP(buf, "expr:", 5) == 0)
3757 {
3758#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003759 /* Evaluate an expression. Skip this when called recursively,
3760 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003761 if (!expr_busy)
3762 {
3763 expr_busy = TRUE;
3764 spell_suggest_expr(su, buf + 5);
3765 expr_busy = FALSE;
3766 }
3767#endif
3768 }
3769 else if (STRNCMP(buf, "file:", 5) == 0)
3770 /* Use list of suggestions in a file. */
3771 spell_suggest_file(su, buf + 5);
3772 else
3773 {
3774 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003775 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003776 if (sps_flags & SPS_DOUBLE)
3777 do_combine = TRUE;
3778 }
3779 }
3780
3781 vim_free(sps_copy);
3782
3783 if (do_combine)
3784 /* Combine the two list of suggestions. This must be done last,
3785 * because sorting changes the order again. */
3786 score_combine(su);
3787}
3788
3789#ifdef FEAT_EVAL
3790/*
3791 * Find suggestions by evaluating expression "expr".
3792 */
3793 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003794spell_suggest_expr(suginfo_T *su, char_u *expr)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003795{
3796 list_T *list;
3797 listitem_T *li;
3798 int score;
3799 char_u *p;
3800
3801 /* The work is split up in a few parts to avoid having to export
3802 * suginfo_T.
3803 * First evaluate the expression and get the resulting list. */
3804 list = eval_spell_expr(su->su_badword, expr);
3805 if (list != NULL)
3806 {
3807 /* Loop over the items in the list. */
3808 for (li = list->lv_first; li != NULL; li = li->li_next)
3809 if (li->li_tv.v_type == VAR_LIST)
3810 {
3811 /* Get the word and the score from the items. */
3812 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003813 if (score >= 0 && score <= su->su_maxscore)
3814 add_suggestion(su, &su->su_ga, p, su->su_badlen,
3815 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003816 }
3817 list_unref(list);
3818 }
3819
Bram Moolenaar4770d092006-01-12 23:22:24 +00003820 /* Remove bogus suggestions, sort and truncate at "maxcount". */
3821 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003822 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
3823}
3824#endif
3825
3826/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003827 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003828 */
3829 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003830spell_suggest_file(suginfo_T *su, char_u *fname)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003831{
3832 FILE *fd;
3833 char_u line[MAXWLEN * 2];
3834 char_u *p;
3835 int len;
3836 char_u cword[MAXWLEN];
3837
3838 /* Open the file. */
3839 fd = mch_fopen((char *)fname, "r");
3840 if (fd == NULL)
3841 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003842 semsg(_(e_notopen), fname);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003843 return;
3844 }
3845
3846 /* Read it line by line. */
3847 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
3848 {
3849 line_breakcheck();
3850
3851 p = vim_strchr(line, '/');
3852 if (p == NULL)
3853 continue; /* No Tab found, just skip the line. */
3854 *p++ = NUL;
3855 if (STRICMP(su->su_badword, line) == 0)
3856 {
3857 /* Match! Isolate the good word, until CR or NL. */
3858 for (len = 0; p[len] >= ' '; ++len)
3859 ;
3860 p[len] = NUL;
3861
3862 /* If the suggestion doesn't have specific case duplicate the case
3863 * of the bad word. */
3864 if (captype(p, NULL) == 0)
3865 {
3866 make_case_word(p, cword, su->su_badflags);
3867 p = cword;
3868 }
3869
3870 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003871 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003872 }
3873 }
3874
3875 fclose(fd);
3876
Bram Moolenaar4770d092006-01-12 23:22:24 +00003877 /* Remove bogus suggestions, sort and truncate at "maxcount". */
3878 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003879 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
3880}
3881
3882/*
3883 * Find suggestions for the internal method indicated by "sps_flags".
3884 */
3885 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003886spell_suggest_intern(suginfo_T *su, int interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003887{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003888 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003889 * Load the .sug file(s) that are available and not done yet.
3890 */
3891 suggest_load_files();
3892
3893 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00003894 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003895 *
3896 * Set a maximum score to limit the combination of operations that is
3897 * tried.
3898 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003899 suggest_try_special(su);
3900
3901 /*
3902 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
3903 * from the .aff file and inserting a space (split the word).
3904 */
3905 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003906
3907 /* For the resulting top-scorers compute the sound-a-like score. */
3908 if (sps_flags & SPS_DOUBLE)
3909 score_comp_sal(su);
3910
3911 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00003912 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003913 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003914 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003915 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00003916 if (sps_flags & SPS_BEST)
3917 /* Adjust the word score for the suggestions found so far for how
3918 * they sounds like. */
3919 rescore_suggestions(su);
3920
3921 /*
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01003922 * While going through the soundfold tree "su_maxscore" is the score
Bram Moolenaar4770d092006-01-12 23:22:24 +00003923 * for the soundfold word, limits the changes that are being tried,
3924 * and "su_sfmaxscore" the rescored score, which is set by
3925 * cleanup_suggestions().
3926 * First find words with a small edit distance, because this is much
3927 * faster and often already finds the top-N suggestions. If we didn't
3928 * find many suggestions try again with a higher edit distance.
3929 * "sl_sounddone" is used to avoid doing the same word twice.
3930 */
3931 suggest_try_soundalike_prep();
3932 su->su_maxscore = SCORE_SFMAX1;
3933 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +00003934 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003935 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
3936 {
3937 /* We didn't find enough matches, try again, allowing more
3938 * changes to the soundfold word. */
3939 su->su_maxscore = SCORE_SFMAX2;
3940 suggest_try_soundalike(su);
3941 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
3942 {
3943 /* Still didn't find enough matches, try again, allowing even
3944 * more changes to the soundfold word. */
3945 su->su_maxscore = SCORE_SFMAX3;
3946 suggest_try_soundalike(su);
3947 }
3948 }
3949 su->su_maxscore = su->su_sfmaxscore;
3950 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003951 }
3952
Bram Moolenaar4770d092006-01-12 23:22:24 +00003953 /* When CTRL-C was hit while searching do show the results. Only clear
3954 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003955 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +00003956 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003957 {
3958 (void)vgetc();
3959 got_int = FALSE;
3960 }
3961
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003962 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003963 {
3964 if (sps_flags & SPS_BEST)
3965 /* Adjust the word score for how it sounds like. */
3966 rescore_suggestions(su);
3967
Bram Moolenaar4770d092006-01-12 23:22:24 +00003968 /* Remove bogus suggestions, sort and truncate at "maxcount". */
3969 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003970 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003971 }
3972}
3973
3974/*
3975 * Free the info put in "*su" by spell_find_suggest().
3976 */
3977 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003978spell_find_cleanup(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003979{
3980 int i;
3981
3982 /* Free the suggestions. */
3983 for (i = 0; i < su->su_ga.ga_len; ++i)
3984 vim_free(SUG(su->su_ga, i).st_word);
3985 ga_clear(&su->su_ga);
3986 for (i = 0; i < su->su_sga.ga_len; ++i)
3987 vim_free(SUG(su->su_sga, i).st_word);
3988 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003989
3990 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003991 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003992}
3993
3994/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003995 * Make a copy of "word", with the first letter upper or lower cased, to
3996 * "wcopy[MAXWLEN]". "word" must not be empty.
3997 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003998 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003999 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004000onecap_copy(
4001 char_u *word,
4002 char_u *wcopy,
4003 int upper) /* TRUE: first letter made upper case */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004004{
4005 char_u *p;
4006 int c;
4007 int l;
4008
4009 p = word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004010 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004011 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004012 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004013 c = *p++;
4014 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004015 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004016 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004017 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004018 if (has_mbyte)
4019 l = mb_char2bytes(c, wcopy);
4020 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004021 {
4022 l = 1;
4023 wcopy[0] = c;
4024 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004025 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004026}
4027
4028/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004029 * Make a copy of "word" with all the letters upper cased into
4030 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004031 */
4032 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004033allcap_copy(char_u *word, char_u *wcopy)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004034{
4035 char_u *s;
4036 char_u *d;
4037 int c;
4038
4039 d = wcopy;
4040 for (s = word; *s != NUL; )
4041 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004042 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004043 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004044 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004045 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +00004046
Bram Moolenaard3184b52011-09-02 14:18:20 +02004047 /* We only change 0xdf to SS when we are certain latin1 is used. It
Bram Moolenaar78622822005-08-23 21:00:13 +00004048 * would cause weird errors in other 8-bit encodings. */
4049 if (enc_latin1like && c == 0xdf)
4050 {
4051 c = 'S';
4052 if (d - wcopy >= MAXWLEN - 1)
4053 break;
4054 *d++ = c;
4055 }
4056 else
Bram Moolenaar78622822005-08-23 21:00:13 +00004057 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004058
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004059 if (has_mbyte)
4060 {
4061 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
4062 break;
4063 d += mb_char2bytes(c, d);
4064 }
4065 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004066 {
4067 if (d - wcopy >= MAXWLEN - 1)
4068 break;
4069 *d++ = c;
4070 }
4071 }
4072 *d = NUL;
4073}
4074
4075/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00004076 * Try finding suggestions by recognizing specific situations.
4077 */
4078 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004079suggest_try_special(suginfo_T *su)
Bram Moolenaar0c405862005-06-22 22:26:26 +00004080{
4081 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004082 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004083 int c;
4084 char_u word[MAXWLEN];
4085
4086 /*
4087 * Recognize a word that is repeated: "the the".
4088 */
4089 p = skiptowhite(su->su_fbadword);
4090 len = p - su->su_fbadword;
4091 p = skipwhite(p);
4092 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
4093 {
4094 /* Include badflags: if the badword is onecap or allcap
4095 * use that for the goodword too: "The the" -> "The". */
4096 c = su->su_fbadword[len];
4097 su->su_fbadword[len] = NUL;
4098 make_case_word(su->su_fbadword, word, su->su_badflags);
4099 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004100
4101 /* Give a soundalike score of 0, compute the score as if deleting one
4102 * character. */
4103 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +00004104 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004105 }
4106}
4107
4108/*
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004109 * Change the 0 to 1 to measure how much time is spent in each state.
4110 * Output is dumped in "suggestprof".
4111 */
4112#if 0
4113# define SUGGEST_PROFILE
4114proftime_T current;
4115proftime_T total;
4116proftime_T times[STATE_FINAL + 1];
4117long counts[STATE_FINAL + 1];
4118
4119 static void
4120prof_init(void)
4121{
4122 for (int i = 0; i <= STATE_FINAL; ++i)
4123 {
4124 profile_zero(&times[i]);
4125 counts[i] = 0;
4126 }
4127 profile_start(&current);
4128 profile_start(&total);
4129}
4130
4131/* call before changing state */
4132 static void
4133prof_store(state_T state)
4134{
4135 profile_end(&current);
4136 profile_add(&times[state], &current);
4137 ++counts[state];
4138 profile_start(&current);
4139}
4140# define PROF_STORE(state) prof_store(state);
4141
4142 static void
4143prof_report(char *name)
4144{
4145 FILE *fd = fopen("suggestprof", "a");
4146
4147 profile_end(&total);
4148 fprintf(fd, "-----------------------\n");
4149 fprintf(fd, "%s: %s\n", name, profile_msg(&total));
4150 for (int i = 0; i <= STATE_FINAL; ++i)
4151 fprintf(fd, "%d: %s (%ld)\n", i, profile_msg(&times[i]), counts[i]);
4152 fclose(fd);
4153}
4154#else
4155# define PROF_STORE(state)
4156#endif
4157
4158/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004159 * Try finding suggestions by adding/removing/swapping letters.
4160 */
4161 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004162suggest_try_change(suginfo_T *su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004163{
4164 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004165 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004166 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004167 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004168 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004169
4170 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00004171 * to find matches (esp. REP items). Append some more text, changing
4172 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004173 STRCPY(fword, su->su_fbadword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004174 n = (int)STRLEN(fword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004175 p = su->su_badptr + su->su_badlen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004176 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004177
Bram Moolenaar860cae12010-06-05 23:22:07 +02004178 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004179 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004180 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004181
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004182 /* If reloading a spell file fails it's still in the list but
4183 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004184 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004185 continue;
4186
Bram Moolenaar4770d092006-01-12 23:22:24 +00004187 /* Try it for this language. Will add possible suggestions. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004188#ifdef SUGGEST_PROFILE
4189 prof_init();
4190#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004191 suggest_trie_walk(su, lp, fword, FALSE);
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004192#ifdef SUGGEST_PROFILE
4193 prof_report("try_change");
4194#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004195 }
4196}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004197
Bram Moolenaar4770d092006-01-12 23:22:24 +00004198/* Check the maximum score, if we go over it we won't try this change. */
4199#define TRY_DEEPER(su, stack, depth, add) \
4200 (stack[depth].ts_score + (add) < su->su_maxscore)
4201
4202/*
4203 * Try finding suggestions by adding/removing/swapping letters.
4204 *
4205 * This uses a state machine. At each node in the tree we try various
4206 * operations. When trying if an operation works "depth" is increased and the
4207 * stack[] is used to store info. This allows combinations, thus insert one
4208 * character, replace one and delete another. The number of changes is
4209 * limited by su->su_maxscore.
4210 *
4211 * After implementing this I noticed an article by Kemal Oflazer that
4212 * describes something similar: "Error-tolerant Finite State Recognition with
4213 * Applications to Morphological Analysis and Spelling Correction" (1996).
4214 * The implementation in the article is simplified and requires a stack of
4215 * unknown depth. The implementation here only needs a stack depth equal to
4216 * the length of the word.
4217 *
4218 * This is also used for the sound-folded word, "soundfold" is TRUE then.
4219 * The mechanism is the same, but we find a match with a sound-folded word
4220 * that comes from one or more original words. Each of these words may be
4221 * added, this is done by add_sound_suggest().
4222 * Don't use:
4223 * the prefix tree or the keep-case tree
4224 * "su->su_badlen"
4225 * anything to do with upper and lower case
4226 * anything to do with word or non-word characters ("spell_iswordp()")
4227 * banned words
4228 * word flags (rare, region, compounding)
4229 * word splitting for now
4230 * "similar_chars()"
4231 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
4232 */
4233 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004234suggest_trie_walk(
4235 suginfo_T *su,
4236 langp_T *lp,
4237 char_u *fword,
4238 int soundfold)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004239{
4240 char_u tword[MAXWLEN]; /* good word collected so far */
4241 trystate_T stack[MAXWLEN];
4242 char_u preword[MAXWLEN * 3]; /* word found with proper case;
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01004243 * concatenation of prefix compound
Bram Moolenaar4770d092006-01-12 23:22:24 +00004244 * words and split word. NUL terminated
4245 * when going deeper but not when coming
4246 * back. */
4247 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
4248 trystate_T *sp;
4249 int newscore;
4250 int score;
4251 char_u *byts, *fbyts, *pbyts;
4252 idx_T *idxs, *fidxs, *pidxs;
4253 int depth;
4254 int c, c2, c3;
4255 int n = 0;
4256 int flags;
4257 garray_T *gap;
4258 idx_T arridx;
4259 int len;
4260 char_u *p;
4261 fromto_T *ftp;
4262 int fl = 0, tl;
4263 int repextra = 0; /* extra bytes in fword[] from REP item */
4264 slang_T *slang = lp->lp_slang;
4265 int fword_ends;
4266 int goodword_ends;
4267#ifdef DEBUG_TRIEWALK
4268 /* Stores the name of the change made at each level. */
4269 char_u changename[MAXWLEN][80];
4270#endif
4271 int breakcheckcount = 1000;
4272 int compound_ok;
4273
4274 /*
4275 * Go through the whole case-fold tree, try changes at each node.
4276 * "tword[]" contains the word collected from nodes in the tree.
4277 * "fword[]" the word we are trying to match with (initially the bad
4278 * word).
4279 */
4280 depth = 0;
4281 sp = &stack[0];
4282 vim_memset(sp, 0, sizeof(trystate_T));
4283 sp->ts_curi = 1;
4284
4285 if (soundfold)
4286 {
4287 /* Going through the soundfold tree. */
4288 byts = fbyts = slang->sl_sbyts;
4289 idxs = fidxs = slang->sl_sidxs;
4290 pbyts = NULL;
4291 pidxs = NULL;
4292 sp->ts_prefixdepth = PFD_NOPREFIX;
4293 sp->ts_state = STATE_START;
4294 }
4295 else
4296 {
Bram Moolenaarea424162005-06-16 21:51:00 +00004297 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004298 * When there are postponed prefixes we need to use these first. At
4299 * the end of the prefix we continue in the case-fold tree.
4300 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004301 fbyts = slang->sl_fbyts;
4302 fidxs = slang->sl_fidxs;
4303 pbyts = slang->sl_pbyts;
4304 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004305 if (pbyts != NULL)
4306 {
4307 byts = pbyts;
4308 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004309 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004310 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
4311 }
4312 else
4313 {
4314 byts = fbyts;
4315 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004316 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004317 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004318 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004319 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004320
Bram Moolenaar4770d092006-01-12 23:22:24 +00004321 /*
4322 * Loop to find all suggestions. At each round we either:
4323 * - For the current state try one operation, advance "ts_curi",
4324 * increase "depth".
4325 * - When a state is done go to the next, set "ts_state".
4326 * - When all states are tried decrease "depth".
4327 */
4328 while (depth >= 0 && !got_int)
4329 {
4330 sp = &stack[depth];
4331 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004332 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004333 case STATE_START:
4334 case STATE_NOPREFIX:
4335 /*
4336 * Start of node: Deal with NUL bytes, which means
4337 * tword[] may end here.
4338 */
4339 arridx = sp->ts_arridx; /* current node in the tree */
4340 len = byts[arridx]; /* bytes in this node */
4341 arridx += sp->ts_curi; /* index of current byte */
4342
4343 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004344 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004345 /* Skip over the NUL bytes, we use them later. */
4346 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
4347 ;
4348 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004349
Bram Moolenaar4770d092006-01-12 23:22:24 +00004350 /* Always past NUL bytes now. */
4351 n = (int)sp->ts_state;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004352 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004353 sp->ts_state = STATE_ENDNUL;
4354 sp->ts_save_badflags = su->su_badflags;
4355
4356 /* At end of a prefix or at start of prefixtree: check for
4357 * following word. */
4358 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004359 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004360 /* Set su->su_badflags to the caps type at this position.
4361 * Use the caps type until here for the prefix itself. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004362 if (has_mbyte)
4363 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
4364 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004365 n = sp->ts_fidx;
4366 flags = badword_captype(su->su_badptr, su->su_badptr + n);
4367 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004368 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004369#ifdef DEBUG_TRIEWALK
4370 sprintf(changename[depth], "prefix");
4371#endif
4372 go_deeper(stack, depth, 0);
4373 ++depth;
4374 sp = &stack[depth];
4375 sp->ts_prefixdepth = depth - 1;
4376 byts = fbyts;
4377 idxs = fidxs;
4378 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004379
Bram Moolenaar4770d092006-01-12 23:22:24 +00004380 /* Move the prefix to preword[] with the right case
4381 * and make find_keepcap_word() works. */
4382 tword[sp->ts_twordlen] = NUL;
4383 make_case_word(tword + sp->ts_splitoff,
4384 preword + sp->ts_prewordlen, flags);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004385 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004386 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004387 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004388 break;
4389 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004390
Bram Moolenaar4770d092006-01-12 23:22:24 +00004391 if (sp->ts_curi > len || byts[arridx] != 0)
4392 {
4393 /* Past bytes in node and/or past NUL bytes. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004394 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004395 sp->ts_state = STATE_ENDNUL;
4396 sp->ts_save_badflags = su->su_badflags;
4397 break;
4398 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004399
Bram Moolenaar4770d092006-01-12 23:22:24 +00004400 /*
4401 * End of word in tree.
4402 */
4403 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004404
Bram Moolenaar4770d092006-01-12 23:22:24 +00004405 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004406
4407 /* Skip words with the NOSUGGEST flag. */
4408 if (flags & WF_NOSUGGEST)
4409 break;
4410
Bram Moolenaar4770d092006-01-12 23:22:24 +00004411 fword_ends = (fword[sp->ts_fidx] == NUL
4412 || (soundfold
Bram Moolenaar1c465442017-03-12 20:10:05 +01004413 ? VIM_ISWHITE(fword[sp->ts_fidx])
Bram Moolenaar860cae12010-06-05 23:22:07 +02004414 : !spell_iswordp(fword + sp->ts_fidx, curwin)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004415 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004416
Bram Moolenaar4770d092006-01-12 23:22:24 +00004417 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +00004418 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004419 {
4420 /* There was a prefix before the word. Check that the prefix
4421 * can be used with this word. */
4422 /* Count the length of the NULs in the prefix. If there are
4423 * none this must be the first try without a prefix. */
4424 n = stack[sp->ts_prefixdepth].ts_arridx;
4425 len = pbyts[n++];
4426 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
4427 ;
4428 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004429 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004430 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00004431 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004432 if (c == 0)
4433 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004434
Bram Moolenaar4770d092006-01-12 23:22:24 +00004435 /* Use the WF_RARE flag for a rare prefix. */
4436 if (c & WF_RAREPFX)
4437 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004438
Bram Moolenaar4770d092006-01-12 23:22:24 +00004439 /* Tricky: when checking for both prefix and compounding
4440 * we run into the prefix flag first.
4441 * Remember that it's OK, so that we accept the prefix
4442 * when arriving at a compound flag. */
4443 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004444 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004445 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004446
Bram Moolenaar4770d092006-01-12 23:22:24 +00004447 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
4448 * appending another compound word below. */
4449 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004450 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004451 goodword_ends = FALSE;
4452 else
4453 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004454
Bram Moolenaar4770d092006-01-12 23:22:24 +00004455 p = NULL;
4456 compound_ok = TRUE;
4457 if (sp->ts_complen > sp->ts_compsplit)
4458 {
4459 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +00004460 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004461 /* There was a word before this word. When there was no
4462 * change in this word (it was correct) add the first word
4463 * as a suggestion. If this word was corrected too, we
4464 * need to check if a correct word follows. */
4465 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +00004466 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +00004467 && STRNCMP(fword + sp->ts_splitfidx,
4468 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +00004469 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004470 {
4471 preword[sp->ts_prewordlen] = NUL;
4472 newscore = score_wordcount_adj(slang, sp->ts_score,
4473 preword + sp->ts_prewordlen,
4474 sp->ts_prewordlen > 0);
4475 /* Add the suggestion if the score isn't too bad. */
4476 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +00004477 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004478 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +00004479 newscore, 0, FALSE,
4480 lp->lp_sallang, FALSE);
4481 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00004482 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00004483 }
Bram Moolenaare52325c2005-08-22 22:54:29 +00004484 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00004485 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004486 /* There was a compound word before this word. If this
4487 * word does not support compounding then give up
4488 * (splitting is tried for the word without compound
4489 * flag). */
4490 if (((unsigned)flags >> 24) == 0
4491 || sp->ts_twordlen - sp->ts_splitoff
4492 < slang->sl_compminlen)
4493 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004494 /* For multi-byte chars check character length against
4495 * COMPOUNDMIN. */
4496 if (has_mbyte
4497 && slang->sl_compminlen > 0
4498 && mb_charlen(tword + sp->ts_splitoff)
4499 < slang->sl_compminlen)
4500 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00004501
Bram Moolenaar4770d092006-01-12 23:22:24 +00004502 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
4503 compflags[sp->ts_complen + 1] = NUL;
4504 vim_strncpy(preword + sp->ts_prewordlen,
4505 tword + sp->ts_splitoff,
4506 sp->ts_twordlen - sp->ts_splitoff);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004507
4508 /* Verify CHECKCOMPOUNDPATTERN rules. */
4509 if (match_checkcompoundpattern(preword, sp->ts_prewordlen,
4510 &slang->sl_comppat))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004511 compound_ok = FALSE;
4512
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004513 if (compound_ok)
4514 {
4515 p = preword;
4516 while (*skiptowhite(p) != NUL)
4517 p = skipwhite(skiptowhite(p));
4518 if (fword_ends && !can_compound(slang, p,
4519 compflags + sp->ts_compsplit))
4520 /* Compound is not allowed. But it may still be
4521 * possible if we add another (short) word. */
4522 compound_ok = FALSE;
4523 }
4524
Bram Moolenaar4770d092006-01-12 23:22:24 +00004525 /* Get pointer to last char of previous word. */
4526 p = preword + sp->ts_prewordlen;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004527 MB_PTR_BACK(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004528 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004529 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004530
Bram Moolenaar4770d092006-01-12 23:22:24 +00004531 /*
4532 * Form the word with proper case in preword.
4533 * If there is a word from a previous split, append.
4534 * For the soundfold tree don't change the case, simply append.
4535 */
4536 if (soundfold)
4537 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
4538 else if (flags & WF_KEEPCAP)
4539 /* Must find the word in the keep-case tree. */
4540 find_keepcap_word(slang, tword + sp->ts_splitoff,
4541 preword + sp->ts_prewordlen);
4542 else
4543 {
4544 /* Include badflags: If the badword is onecap or allcap
4545 * use that for the goodword too. But if the badword is
4546 * allcap and it's only one char long use onecap. */
4547 c = su->su_badflags;
4548 if ((c & WF_ALLCAP)
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004549 && su->su_badlen == (*mb_ptr2len)(su->su_badptr))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004550 c = WF_ONECAP;
4551 c |= flags;
4552
4553 /* When appending a compound word after a word character don't
4554 * use Onecap. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004555 if (p != NULL && spell_iswordp_nmw(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004556 c &= ~WF_ONECAP;
4557 make_case_word(tword + sp->ts_splitoff,
4558 preword + sp->ts_prewordlen, c);
4559 }
4560
4561 if (!soundfold)
4562 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004563 /* Don't use a banned word. It may appear again as a good
4564 * word, thus remember it. */
4565 if (flags & WF_BANNED)
4566 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004567 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004568 break;
4569 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004570 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +00004571 && WAS_BANNED(su, preword + sp->ts_prewordlen))
4572 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004573 {
4574 if (slang->sl_compprog == NULL)
4575 break;
4576 /* the word so far was banned but we may try compounding */
4577 goodword_ends = FALSE;
4578 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004579 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004580
Bram Moolenaar4770d092006-01-12 23:22:24 +00004581 newscore = 0;
4582 if (!soundfold) /* soundfold words don't have flags */
4583 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004584 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004585 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004586 newscore += SCORE_REGION;
4587 if (flags & WF_RARE)
4588 newscore += SCORE_RARE;
4589
Bram Moolenaar0c405862005-06-22 22:26:26 +00004590 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00004591 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004592 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004593 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004594
Bram Moolenaar4770d092006-01-12 23:22:24 +00004595 /* TODO: how about splitting in the soundfold tree? */
4596 if (fword_ends
4597 && goodword_ends
4598 && sp->ts_fidx >= sp->ts_fidxtry
4599 && compound_ok)
4600 {
4601 /* The badword also ends: add suggestions. */
4602#ifdef DEBUG_TRIEWALK
4603 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004604 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004605 int j;
4606
4607 /* print the stack of changes that brought us here */
4608 smsg("------ %s -------", fword);
4609 for (j = 0; j < depth; ++j)
4610 smsg("%s", changename[j]);
4611 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004612#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004613 if (soundfold)
4614 {
4615 /* For soundfolded words we need to find the original
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004616 * words, the edit distance and then add them. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004617 add_sound_suggest(su, preword, sp->ts_score, lp);
4618 }
Bram Moolenaar7e88c3d2010-08-01 15:47:35 +02004619 else if (sp->ts_fidx > 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004620 {
4621 /* Give a penalty when changing non-word char to word
4622 * char, e.g., "thes," -> "these". */
4623 p = fword + sp->ts_fidx;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004624 MB_PTR_BACK(fword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004625 if (!spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004626 {
4627 p = preword + STRLEN(preword);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004628 MB_PTR_BACK(preword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004629 if (spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004630 newscore += SCORE_NONWORD;
4631 }
4632
Bram Moolenaar4770d092006-01-12 23:22:24 +00004633 /* Give a bonus to words seen before. */
4634 score = score_wordcount_adj(slang,
4635 sp->ts_score + newscore,
4636 preword + sp->ts_prewordlen,
4637 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004638
Bram Moolenaar4770d092006-01-12 23:22:24 +00004639 /* Add the suggestion if the score isn't too bad. */
4640 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004641 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004642 add_suggestion(su, &su->su_ga, preword,
4643 sp->ts_fidx - repextra,
4644 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004645
4646 if (su->su_badflags & WF_MIXCAP)
4647 {
4648 /* We really don't know if the word should be
4649 * upper or lower case, add both. */
4650 c = captype(preword, NULL);
4651 if (c == 0 || c == WF_ALLCAP)
4652 {
4653 make_case_word(tword + sp->ts_splitoff,
4654 preword + sp->ts_prewordlen,
4655 c == 0 ? WF_ALLCAP : 0);
4656
4657 add_suggestion(su, &su->su_ga, preword,
4658 sp->ts_fidx - repextra,
4659 score + SCORE_ICASE, 0, FALSE,
4660 lp->lp_sallang, FALSE);
4661 }
4662 }
4663 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004664 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004665 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004666
Bram Moolenaar4770d092006-01-12 23:22:24 +00004667 /*
4668 * Try word split and/or compounding.
4669 */
4670 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004671 /* Don't split halfway a character. */
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004672 && (!has_mbyte || sp->ts_tcharlen == 0))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004673 {
4674 int try_compound;
4675 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004676
Bram Moolenaar4770d092006-01-12 23:22:24 +00004677 /* If past the end of the bad word don't try a split.
4678 * Otherwise try changing the next word. E.g., find
4679 * suggestions for "the the" where the second "the" is
4680 * different. It's done like a split.
4681 * TODO: word split for soundfold words */
4682 try_split = (sp->ts_fidx - repextra < su->su_badlen)
4683 && !soundfold;
4684
4685 /* Get here in several situations:
4686 * 1. The word in the tree ends:
4687 * If the word allows compounding try that. Otherwise try
4688 * a split by inserting a space. For both check that a
4689 * valid words starts at fword[sp->ts_fidx].
4690 * For NOBREAK do like compounding to be able to check if
4691 * the next word is valid.
4692 * 2. The badword does end, but it was due to a change (e.g.,
4693 * a swap). No need to split, but do check that the
4694 * following word is valid.
4695 * 3. The badword and the word in the tree end. It may still
4696 * be possible to compound another (short) word.
4697 */
4698 try_compound = FALSE;
4699 if (!soundfold
Bram Moolenaar7b877b32016-01-09 13:51:34 +01004700 && !slang->sl_nocompoundsugs
Bram Moolenaar4770d092006-01-12 23:22:24 +00004701 && slang->sl_compprog != NULL
4702 && ((unsigned)flags >> 24) != 0
4703 && sp->ts_twordlen - sp->ts_splitoff
4704 >= slang->sl_compminlen
Bram Moolenaar4770d092006-01-12 23:22:24 +00004705 && (!has_mbyte
4706 || slang->sl_compminlen == 0
4707 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004708 >= slang->sl_compminlen)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004709 && (slang->sl_compsylmax < MAXWLEN
4710 || sp->ts_complen + 1 - sp->ts_compsplit
4711 < slang->sl_compmax)
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004712 && (can_be_compound(sp, slang,
4713 compflags, ((unsigned)flags >> 24))))
4714
Bram Moolenaar4770d092006-01-12 23:22:24 +00004715 {
4716 try_compound = TRUE;
4717 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
4718 compflags[sp->ts_complen + 1] = NUL;
4719 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00004720
Bram Moolenaar4770d092006-01-12 23:22:24 +00004721 /* For NOBREAK we never try splitting, it won't make any word
4722 * valid. */
Bram Moolenaar7b877b32016-01-09 13:51:34 +01004723 if (slang->sl_nobreak && !slang->sl_nocompoundsugs)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004724 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +00004725
Bram Moolenaar4770d092006-01-12 23:22:24 +00004726 /* If we could add a compound word, and it's also possible to
4727 * split at this point, do the split first and set
4728 * TSF_DIDSPLIT to avoid doing it again. */
4729 else if (!fword_ends
4730 && try_compound
4731 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
4732 {
4733 try_compound = FALSE;
4734 sp->ts_flags |= TSF_DIDSPLIT;
4735 --sp->ts_curi; /* do the same NUL again */
4736 compflags[sp->ts_complen] = NUL;
4737 }
4738 else
4739 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004740
Bram Moolenaar4770d092006-01-12 23:22:24 +00004741 if (try_split || try_compound)
4742 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004743 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +00004744 {
4745 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004746 * words so far are valid for compounding. If there
4747 * is only one word it must not have the NEEDCOMPOUND
4748 * flag. */
4749 if (sp->ts_complen == sp->ts_compsplit
4750 && (flags & WF_NEEDCOMP))
4751 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00004752 p = preword;
4753 while (*skiptowhite(p) != NUL)
4754 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +00004755 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +00004756 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00004757 compflags + sp->ts_compsplit))
4758 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004759
4760 if (slang->sl_nosplitsugs)
4761 newscore += SCORE_SPLIT_NO;
4762 else
4763 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004764
4765 /* Give a bonus to words seen before. */
4766 newscore = score_wordcount_adj(slang, newscore,
4767 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004768 }
4769
Bram Moolenaar4770d092006-01-12 23:22:24 +00004770 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004771 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004772 go_deeper(stack, depth, newscore);
4773#ifdef DEBUG_TRIEWALK
4774 if (!try_compound && !fword_ends)
4775 sprintf(changename[depth], "%.*s-%s: split",
4776 sp->ts_twordlen, tword, fword + sp->ts_fidx);
4777 else
4778 sprintf(changename[depth], "%.*s-%s: compound",
4779 sp->ts_twordlen, tword, fword + sp->ts_fidx);
4780#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004781 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004782 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004783 PROF_STORE(sp->ts_state)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004784 sp->ts_state = STATE_SPLITUNDO;
4785
4786 ++depth;
4787 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004788
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004789 /* Append a space to preword when splitting. */
4790 if (!try_compound && !fword_ends)
4791 STRCAT(preword, " ");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004792 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004793 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +00004794 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004795
4796 /* If the badword has a non-word character at this
4797 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004798 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004799 * character when the word ends. But only when the
4800 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004801 if (((!try_compound && !spell_iswordp_nmw(fword
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004802 + sp->ts_fidx,
4803 curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004804 || fword_ends)
4805 && fword[sp->ts_fidx] != NUL
4806 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004807 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004808 int l;
4809
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02004810 l = MB_PTR2LEN(fword + sp->ts_fidx);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004811 if (fword_ends)
4812 {
4813 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004814 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004815 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004816 sp->ts_prewordlen += l;
4817 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004818 }
4819 else
4820 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
4821 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004822 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00004823
Bram Moolenaard12a1322005-08-21 22:08:24 +00004824 /* When compounding include compound flag in
4825 * compflags[] (already set above). When splitting we
4826 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004827 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004828 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004829 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00004830 sp->ts_compsplit = sp->ts_complen;
4831 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004832
Bram Moolenaar53805d12005-08-01 07:08:33 +00004833 /* set su->su_badflags to the caps type at this
4834 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004835 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00004836 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004837 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00004838 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004839 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004840 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004841
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004842 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004843 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004844
4845 /* If there are postponed prefixes, try these too. */
4846 if (pbyts != NULL)
4847 {
4848 byts = pbyts;
4849 idxs = pidxs;
4850 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004851 PROF_STORE(sp->ts_state)
Bram Moolenaard12a1322005-08-21 22:08:24 +00004852 sp->ts_state = STATE_NOPREFIX;
4853 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004854 }
4855 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004856 }
4857 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004858
Bram Moolenaar4770d092006-01-12 23:22:24 +00004859 case STATE_SPLITUNDO:
4860 /* Undo the changes done for word split or compound word. */
4861 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004862
Bram Moolenaar4770d092006-01-12 23:22:24 +00004863 /* Continue looking for NUL bytes. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004864 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004865 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004866
Bram Moolenaar4770d092006-01-12 23:22:24 +00004867 /* In case we went into the prefix tree. */
4868 byts = fbyts;
4869 idxs = fidxs;
4870 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004871
Bram Moolenaar4770d092006-01-12 23:22:24 +00004872 case STATE_ENDNUL:
4873 /* Past the NUL bytes in the node. */
4874 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004875 if (fword[sp->ts_fidx] == NUL && sp->ts_tcharlen == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004876 {
4877 /* The badword ends, can't use STATE_PLAIN. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004878 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004879 sp->ts_state = STATE_DEL;
4880 break;
4881 }
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004882 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004883 sp->ts_state = STATE_PLAIN;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004884 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004885
4886 case STATE_PLAIN:
4887 /*
4888 * Go over all possible bytes at this node, add each to tword[]
4889 * and use child node. "ts_curi" is the index.
4890 */
4891 arridx = sp->ts_arridx;
4892 if (sp->ts_curi > byts[arridx])
4893 {
4894 /* Done all bytes at this node, do next state. When still at
4895 * already changed bytes skip the other tricks. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004896 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004897 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004898 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004899 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004900 sp->ts_state = STATE_FINAL;
4901 }
4902 else
4903 {
4904 arridx += sp->ts_curi++;
4905 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004906
Bram Moolenaar4770d092006-01-12 23:22:24 +00004907 /* Normal byte, go one level deeper. If it's not equal to the
4908 * byte in the bad word adjust the score. But don't even try
4909 * when the byte was already changed. And don't try when we
Bram Moolenaar4de6a212014-03-08 16:13:44 +01004910 * just deleted this byte, accepting it is always cheaper than
Bram Moolenaar4770d092006-01-12 23:22:24 +00004911 * delete + substitute. */
4912 if (c == fword[sp->ts_fidx]
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004913 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004914 newscore = 0;
4915 else
4916 newscore = SCORE_SUBST;
4917 if ((newscore == 0
4918 || (sp->ts_fidx >= sp->ts_fidxtry
4919 && ((sp->ts_flags & TSF_DIDDEL) == 0
4920 || c != fword[sp->ts_delidx])))
4921 && TRY_DEEPER(su, stack, depth, newscore))
4922 {
4923 go_deeper(stack, depth, newscore);
4924#ifdef DEBUG_TRIEWALK
4925 if (newscore > 0)
4926 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
4927 sp->ts_twordlen, tword, fword + sp->ts_fidx,
4928 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004929 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004930 sprintf(changename[depth], "%.*s-%s: accept %c",
4931 sp->ts_twordlen, tword, fword + sp->ts_fidx,
4932 fword[sp->ts_fidx]);
4933#endif
4934 ++depth;
4935 sp = &stack[depth];
4936 ++sp->ts_fidx;
4937 tword[sp->ts_twordlen++] = c;
4938 sp->ts_arridx = idxs[arridx];
Bram Moolenaar4770d092006-01-12 23:22:24 +00004939 if (newscore == SCORE_SUBST)
4940 sp->ts_isdiff = DIFF_YES;
4941 if (has_mbyte)
4942 {
4943 /* Multi-byte characters are a bit complicated to
4944 * handle: They differ when any of the bytes differ
4945 * and then their length may also differ. */
4946 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +00004947 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004948 /* First byte. */
4949 sp->ts_tcharidx = 0;
4950 sp->ts_tcharlen = MB_BYTE2LEN(c);
4951 sp->ts_fcharstart = sp->ts_fidx - 1;
4952 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +00004953 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004954 }
4955 else if (sp->ts_isdiff == DIFF_INSERT)
4956 /* When inserting trail bytes don't advance in the
4957 * bad word. */
4958 --sp->ts_fidx;
4959 if (++sp->ts_tcharidx == sp->ts_tcharlen)
4960 {
4961 /* Last byte of character. */
4962 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +00004963 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004964 /* Correct ts_fidx for the byte length of the
4965 * character (we didn't check that before). */
4966 sp->ts_fidx = sp->ts_fcharstart
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02004967 + MB_PTR2LEN(
4968 fword + sp->ts_fcharstart);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004969 /* For changing a composing character adjust
4970 * the score from SCORE_SUBST to
4971 * SCORE_SUBCOMP. */
4972 if (enc_utf8
4973 && utf_iscomposing(
Bram Moolenaarace95982017-03-29 17:30:27 +02004974 utf_ptr2char(tword
Bram Moolenaar4770d092006-01-12 23:22:24 +00004975 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00004976 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004977 && utf_iscomposing(
Bram Moolenaarace95982017-03-29 17:30:27 +02004978 utf_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00004979 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004980 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00004981 SCORE_SUBST - SCORE_SUBCOMP;
4982
Bram Moolenaar4770d092006-01-12 23:22:24 +00004983 /* For a similar character adjust score from
4984 * SCORE_SUBST to SCORE_SIMILAR. */
4985 else if (!soundfold
4986 && slang->sl_has_map
4987 && similar_chars(slang,
4988 mb_ptr2char(tword
4989 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +00004990 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +00004991 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +00004992 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004993 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +00004994 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +00004995 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004996 else if (sp->ts_isdiff == DIFF_INSERT
4997 && sp->ts_twordlen > sp->ts_tcharlen)
4998 {
4999 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
5000 c = mb_ptr2char(p);
5001 if (enc_utf8 && utf_iscomposing(c))
5002 {
5003 /* Inserting a composing char doesn't
5004 * count that much. */
5005 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
5006 }
5007 else
5008 {
5009 /* If the previous character was the same,
5010 * thus doubling a character, give a bonus
5011 * to the score. Also for the soundfold
5012 * tree (might seem illogical but does
5013 * give better scores). */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005014 MB_PTR_BACK(tword, p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005015 if (c == mb_ptr2char(p))
5016 sp->ts_score -= SCORE_INS
5017 - SCORE_INSDUP;
5018 }
5019 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005020
Bram Moolenaar4770d092006-01-12 23:22:24 +00005021 /* Starting a new char, reset the length. */
5022 sp->ts_tcharlen = 0;
5023 }
Bram Moolenaarea408852005-06-25 22:49:46 +00005024 }
Bram Moolenaarea424162005-06-16 21:51:00 +00005025 else
Bram Moolenaarea408852005-06-25 22:49:46 +00005026 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005027 /* If we found a similar char adjust the score.
5028 * We do this after calling go_deeper() because
5029 * it's slow. */
5030 if (newscore != 0
5031 && !soundfold
5032 && slang->sl_has_map
5033 && similar_chars(slang,
5034 c, fword[sp->ts_fidx - 1]))
5035 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +00005036 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005037 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005038 }
5039 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005040
Bram Moolenaar4770d092006-01-12 23:22:24 +00005041 case STATE_DEL:
Bram Moolenaar4770d092006-01-12 23:22:24 +00005042 /* When past the first byte of a multi-byte char don't try
5043 * delete/insert/swap a character. */
5044 if (has_mbyte && sp->ts_tcharlen > 0)
5045 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005046 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005047 sp->ts_state = STATE_FINAL;
5048 break;
5049 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005050 /*
5051 * Try skipping one character in the bad word (delete it).
5052 */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005053 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005054 sp->ts_state = STATE_INS_PREP;
5055 sp->ts_curi = 1;
5056 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
5057 /* Deleting a vowel at the start of a word counts less, see
5058 * soundalike_score(). */
5059 newscore = 2 * SCORE_DEL / 3;
5060 else
5061 newscore = SCORE_DEL;
5062 if (fword[sp->ts_fidx] != NUL
5063 && TRY_DEEPER(su, stack, depth, newscore))
5064 {
5065 go_deeper(stack, depth, newscore);
5066#ifdef DEBUG_TRIEWALK
5067 sprintf(changename[depth], "%.*s-%s: delete %c",
5068 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5069 fword[sp->ts_fidx]);
5070#endif
5071 ++depth;
5072
5073 /* Remember what character we deleted, so that we can avoid
5074 * inserting it again. */
5075 stack[depth].ts_flags |= TSF_DIDDEL;
5076 stack[depth].ts_delidx = sp->ts_fidx;
5077
5078 /* Advance over the character in fword[]. Give a bonus to the
5079 * score if the same character is following "nn" -> "n". It's
5080 * a bit illogical for soundfold tree but it does give better
5081 * results. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005082 if (has_mbyte)
5083 {
5084 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005085 stack[depth].ts_fidx += MB_PTR2LEN(fword + sp->ts_fidx);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005086 if (enc_utf8 && utf_iscomposing(c))
5087 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
5088 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
5089 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
5090 }
5091 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005092 {
5093 ++stack[depth].ts_fidx;
5094 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
5095 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
5096 }
5097 break;
5098 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005099 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005100
5101 case STATE_INS_PREP:
5102 if (sp->ts_flags & TSF_DIDDEL)
5103 {
5104 /* If we just deleted a byte then inserting won't make sense,
5105 * a substitute is always cheaper. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005106 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005107 sp->ts_state = STATE_SWAP;
5108 break;
5109 }
5110
5111 /* skip over NUL bytes */
5112 n = sp->ts_arridx;
5113 for (;;)
5114 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005115 if (sp->ts_curi > byts[n])
5116 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005117 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005118 PROF_STORE(sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005119 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005120 break;
5121 }
5122 if (byts[n + sp->ts_curi] != NUL)
5123 {
5124 /* Found a byte to insert. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005125 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005126 sp->ts_state = STATE_INS;
5127 break;
5128 }
5129 ++sp->ts_curi;
5130 }
5131 break;
5132
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005133 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005134
5135 case STATE_INS:
5136 /* Insert one byte. Repeat this for each possible byte at this
5137 * node. */
5138 n = sp->ts_arridx;
5139 if (sp->ts_curi > byts[n])
5140 {
5141 /* Done all bytes at this node, go to next state. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005142 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005143 sp->ts_state = STATE_SWAP;
5144 break;
5145 }
5146
5147 /* Do one more byte at this node, but:
5148 * - Skip NUL bytes.
5149 * - Skip the byte if it's equal to the byte in the word,
5150 * accepting that byte is always better.
5151 */
5152 n += sp->ts_curi++;
5153 c = byts[n];
5154 if (soundfold && sp->ts_twordlen == 0 && c == '*')
5155 /* Inserting a vowel at the start of a word counts less,
5156 * see soundalike_score(). */
5157 newscore = 2 * SCORE_INS / 3;
5158 else
5159 newscore = SCORE_INS;
5160 if (c != fword[sp->ts_fidx]
5161 && TRY_DEEPER(su, stack, depth, newscore))
5162 {
5163 go_deeper(stack, depth, newscore);
5164#ifdef DEBUG_TRIEWALK
5165 sprintf(changename[depth], "%.*s-%s: insert %c",
5166 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5167 c);
5168#endif
5169 ++depth;
5170 sp = &stack[depth];
5171 tword[sp->ts_twordlen++] = c;
5172 sp->ts_arridx = idxs[n];
Bram Moolenaar4770d092006-01-12 23:22:24 +00005173 if (has_mbyte)
5174 {
5175 fl = MB_BYTE2LEN(c);
5176 if (fl > 1)
5177 {
5178 /* There are following bytes for the same character.
5179 * We must find all bytes before trying
5180 * delete/insert/swap/etc. */
5181 sp->ts_tcharlen = fl;
5182 sp->ts_tcharidx = 1;
5183 sp->ts_isdiff = DIFF_INSERT;
5184 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005185 }
5186 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005187 fl = 1;
5188 if (fl == 1)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005189 {
5190 /* If the previous character was the same, thus doubling a
5191 * character, give a bonus to the score. Also for
5192 * soundfold words (illogical but does give a better
5193 * score). */
5194 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +00005195 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005196 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005197 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005198 }
5199 break;
5200
5201 case STATE_SWAP:
5202 /*
5203 * Swap two bytes in the bad word: "12" -> "21".
5204 * We change "fword" here, it's changed back afterwards at
5205 * STATE_UNSWAP.
5206 */
5207 p = fword + sp->ts_fidx;
5208 c = *p;
5209 if (c == NUL)
5210 {
5211 /* End of word, can't swap or replace. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005212 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005213 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005214 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005215 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005216
Bram Moolenaar4770d092006-01-12 23:22:24 +00005217 /* Don't swap if the first character is not a word character.
5218 * SWAP3 etc. also don't make sense then. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005219 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005220 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005221 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005222 sp->ts_state = STATE_REP_INI;
5223 break;
5224 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00005225
Bram Moolenaar4770d092006-01-12 23:22:24 +00005226 if (has_mbyte)
5227 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005228 n = MB_CPTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005229 c = mb_ptr2char(p);
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005230 if (p[n] == NUL)
5231 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005232 else if (!soundfold && !spell_iswordp(p + n, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005233 c2 = c; /* don't swap non-word char */
5234 else
5235 c2 = mb_ptr2char(p + n);
5236 }
5237 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005238 {
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005239 if (p[1] == NUL)
5240 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005241 else if (!soundfold && !spell_iswordp(p + 1, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005242 c2 = c; /* don't swap non-word char */
5243 else
5244 c2 = p[1];
5245 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00005246
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005247 /* When the second character is NUL we can't swap. */
5248 if (c2 == NUL)
5249 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005250 PROF_STORE(sp->ts_state)
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005251 sp->ts_state = STATE_REP_INI;
5252 break;
5253 }
5254
Bram Moolenaar4770d092006-01-12 23:22:24 +00005255 /* When characters are identical, swap won't do anything.
5256 * Also get here if the second char is not a word character. */
5257 if (c == c2)
5258 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005259 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005260 sp->ts_state = STATE_SWAP3;
5261 break;
5262 }
5263 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
5264 {
5265 go_deeper(stack, depth, SCORE_SWAP);
5266#ifdef DEBUG_TRIEWALK
5267 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
5268 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5269 c, c2);
5270#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005271 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005272 sp->ts_state = STATE_UNSWAP;
5273 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005274 if (has_mbyte)
5275 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005276 fl = mb_char2len(c2);
5277 mch_memmove(p, p + n, fl);
5278 mb_char2bytes(c, p + fl);
5279 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +00005280 }
5281 else
Bram Moolenaarbb15b652005-10-03 21:52:09 +00005282 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005283 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +00005284 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005285 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +00005286 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005287 }
5288 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005289 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005290 /* If this swap doesn't work then SWAP3 won't either. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005291 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005292 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005293 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005294 break;
Bram Moolenaarea424162005-06-16 21:51:00 +00005295
Bram Moolenaar4770d092006-01-12 23:22:24 +00005296 case STATE_UNSWAP:
5297 /* Undo the STATE_SWAP swap: "21" -> "12". */
5298 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005299 if (has_mbyte)
5300 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005301 n = MB_PTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005302 c = mb_ptr2char(p + n);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005303 mch_memmove(p + MB_PTR2LEN(p + n), p, n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005304 mb_char2bytes(c, p);
5305 }
5306 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005307 {
5308 c = *p;
5309 *p = p[1];
5310 p[1] = c;
5311 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005312 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005313
5314 case STATE_SWAP3:
5315 /* Swap two bytes, skipping one: "123" -> "321". We change
5316 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
5317 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005318 if (has_mbyte)
5319 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005320 n = MB_CPTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005321 c = mb_ptr2char(p);
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005322 fl = MB_CPTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005323 c2 = mb_ptr2char(p + n);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005324 if (!soundfold && !spell_iswordp(p + n + fl, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005325 c3 = c; /* don't swap non-word char */
5326 else
5327 c3 = mb_ptr2char(p + n + fl);
5328 }
5329 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005330 {
5331 c = *p;
5332 c2 = p[1];
Bram Moolenaar860cae12010-06-05 23:22:07 +02005333 if (!soundfold && !spell_iswordp(p + 2, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005334 c3 = c; /* don't swap non-word char */
5335 else
5336 c3 = p[2];
5337 }
5338
5339 /* When characters are identical: "121" then SWAP3 result is
5340 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
5341 * same as SWAP on next char: "112". Thus skip all swapping.
5342 * Also skip when c3 is NUL.
5343 * Also get here when the third character is not a word character.
5344 * Second character may any char: "a.b" -> "b.a" */
5345 if (c == c3 || c3 == NUL)
5346 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005347 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005348 sp->ts_state = STATE_REP_INI;
5349 break;
5350 }
5351 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
5352 {
5353 go_deeper(stack, depth, SCORE_SWAP3);
5354#ifdef DEBUG_TRIEWALK
5355 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
5356 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5357 c, c3);
5358#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005359 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005360 sp->ts_state = STATE_UNSWAP3;
5361 ++depth;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005362 if (has_mbyte)
5363 {
5364 tl = mb_char2len(c3);
5365 mch_memmove(p, p + n + fl, tl);
5366 mb_char2bytes(c2, p + tl);
5367 mb_char2bytes(c, p + fl + tl);
5368 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
5369 }
5370 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005371 {
5372 p[0] = p[2];
5373 p[2] = c;
5374 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5375 }
5376 }
5377 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005378 {
5379 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005380 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005381 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005382 break;
5383
5384 case STATE_UNSWAP3:
5385 /* Undo STATE_SWAP3: "321" -> "123" */
5386 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005387 if (has_mbyte)
5388 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005389 n = MB_PTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005390 c2 = mb_ptr2char(p + n);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005391 fl = MB_PTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005392 c = mb_ptr2char(p + n + fl);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005393 tl = MB_PTR2LEN(p + n + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005394 mch_memmove(p + fl + tl, p, n);
5395 mb_char2bytes(c, p);
5396 mb_char2bytes(c2, p + tl);
5397 p = p + tl;
5398 }
5399 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005400 {
5401 c = *p;
5402 *p = p[2];
5403 p[2] = c;
5404 ++p;
5405 }
5406
Bram Moolenaar860cae12010-06-05 23:22:07 +02005407 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005408 {
5409 /* Middle char is not a word char, skip the rotate. First and
5410 * third char were already checked at swap and swap3. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005411 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005412 sp->ts_state = STATE_REP_INI;
5413 break;
5414 }
5415
5416 /* Rotate three characters left: "123" -> "231". We change
5417 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
5418 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
5419 {
5420 go_deeper(stack, depth, SCORE_SWAP3);
5421#ifdef DEBUG_TRIEWALK
5422 p = fword + sp->ts_fidx;
5423 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
5424 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5425 p[0], p[1], p[2]);
5426#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005427 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005428 sp->ts_state = STATE_UNROT3L;
5429 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005430 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +00005431 if (has_mbyte)
5432 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005433 n = MB_CPTR2LEN(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00005434 c = mb_ptr2char(p);
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005435 fl = MB_CPTR2LEN(p + n);
5436 fl += MB_CPTR2LEN(p + n + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005437 mch_memmove(p, p + n, fl);
5438 mb_char2bytes(c, p + fl);
5439 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +00005440 }
5441 else
Bram Moolenaarea424162005-06-16 21:51:00 +00005442 {
5443 c = *p;
5444 *p = p[1];
5445 p[1] = p[2];
5446 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005447 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +00005448 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005449 }
5450 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005451 {
5452 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005453 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005454 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005455 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005456
Bram Moolenaar4770d092006-01-12 23:22:24 +00005457 case STATE_UNROT3L:
5458 /* Undo ROT3L: "231" -> "123" */
5459 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005460 if (has_mbyte)
5461 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005462 n = MB_PTR2LEN(p);
5463 n += MB_PTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005464 c = mb_ptr2char(p + n);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005465 tl = MB_PTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005466 mch_memmove(p + tl, p, n);
5467 mb_char2bytes(c, p);
5468 }
5469 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005470 {
5471 c = p[2];
5472 p[2] = p[1];
5473 p[1] = *p;
5474 *p = c;
5475 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005476
Bram Moolenaar4770d092006-01-12 23:22:24 +00005477 /* Rotate three bytes right: "123" -> "312". We change "fword"
5478 * here, it's changed back afterwards at STATE_UNROT3R. */
5479 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
5480 {
5481 go_deeper(stack, depth, SCORE_SWAP3);
5482#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005483 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005484 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
5485 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5486 p[0], p[1], p[2]);
5487#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005488 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005489 sp->ts_state = STATE_UNROT3R;
5490 ++depth;
5491 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005492 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005493 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005494 n = MB_CPTR2LEN(p);
5495 n += MB_CPTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005496 c = mb_ptr2char(p + n);
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005497 tl = MB_CPTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005498 mch_memmove(p + tl, p, n);
5499 mb_char2bytes(c, p);
5500 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005501 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005502 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005503 {
5504 c = p[2];
5505 p[2] = p[1];
5506 p[1] = *p;
5507 *p = c;
5508 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5509 }
5510 }
5511 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005512 {
5513 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005514 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005515 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005516 break;
5517
5518 case STATE_UNROT3R:
5519 /* Undo ROT3R: "312" -> "123" */
5520 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005521 if (has_mbyte)
5522 {
5523 c = mb_ptr2char(p);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005524 tl = MB_PTR2LEN(p);
5525 n = MB_PTR2LEN(p + tl);
5526 n += MB_PTR2LEN(p + tl + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005527 mch_memmove(p, p + tl, n);
5528 mb_char2bytes(c, p + n);
5529 }
5530 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005531 {
5532 c = *p;
5533 *p = p[1];
5534 p[1] = p[2];
5535 p[2] = c;
5536 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005537 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005538
5539 case STATE_REP_INI:
5540 /* Check if matching with REP items from the .aff file would work.
5541 * Quickly skip if:
5542 * - there are no REP items and we are not in the soundfold trie
5543 * - the score is going to be too high anyway
5544 * - already applied a REP item or swapped here */
5545 if ((lp->lp_replang == NULL && !soundfold)
5546 || sp->ts_score + SCORE_REP >= su->su_maxscore
5547 || sp->ts_fidx < sp->ts_fidxtry)
5548 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005549 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005550 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005551 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005552 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005553
Bram Moolenaar4770d092006-01-12 23:22:24 +00005554 /* Use the first byte to quickly find the first entry that may
5555 * match. If the index is -1 there is none. */
5556 if (soundfold)
5557 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
5558 else
5559 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005560
Bram Moolenaar4770d092006-01-12 23:22:24 +00005561 if (sp->ts_curi < 0)
5562 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005563 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005564 sp->ts_state = STATE_FINAL;
5565 break;
5566 }
5567
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005568 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005569 sp->ts_state = STATE_REP;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005570 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005571
5572 case STATE_REP:
5573 /* Try matching with REP items from the .aff file. For each match
5574 * replace the characters and check if the resulting word is
5575 * valid. */
5576 p = fword + sp->ts_fidx;
5577
5578 if (soundfold)
5579 gap = &slang->sl_repsal;
5580 else
5581 gap = &lp->lp_replang->sl_rep;
5582 while (sp->ts_curi < gap->ga_len)
5583 {
5584 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
5585 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005586 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005587 /* past possible matching entries */
5588 sp->ts_curi = gap->ga_len;
5589 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005590 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005591 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
5592 && TRY_DEEPER(su, stack, depth, SCORE_REP))
5593 {
5594 go_deeper(stack, depth, SCORE_REP);
5595#ifdef DEBUG_TRIEWALK
5596 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
5597 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5598 ftp->ft_from, ftp->ft_to);
5599#endif
5600 /* Need to undo this afterwards. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005601 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005602 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005603
Bram Moolenaar4770d092006-01-12 23:22:24 +00005604 /* Change the "from" to the "to" string. */
5605 ++depth;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005606 fl = (int)STRLEN(ftp->ft_from);
5607 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005608 if (fl != tl)
5609 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005610 STRMOVE(p + tl, p + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005611 repextra += tl - fl;
5612 }
5613 mch_memmove(p, ftp->ft_to, tl);
5614 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005615 stack[depth].ts_tcharlen = 0;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005616 break;
5617 }
5618 }
5619
5620 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005621 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005622 /* No (more) matches. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005623 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005624 sp->ts_state = STATE_FINAL;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005625 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005626
5627 break;
5628
5629 case STATE_REP_UNDO:
5630 /* Undo a REP replacement and continue with the next one. */
5631 if (soundfold)
5632 gap = &slang->sl_repsal;
5633 else
5634 gap = &lp->lp_replang->sl_rep;
5635 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005636 fl = (int)STRLEN(ftp->ft_from);
5637 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005638 p = fword + sp->ts_fidx;
5639 if (fl != tl)
5640 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005641 STRMOVE(p + fl, p + tl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005642 repextra -= tl - fl;
5643 }
5644 mch_memmove(p, ftp->ft_from, fl);
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005645 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005646 sp->ts_state = STATE_REP;
5647 break;
5648
5649 default:
5650 /* Did all possible states at this level, go up one level. */
5651 --depth;
5652
5653 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
5654 {
5655 /* Continue in or go back to the prefix tree. */
5656 byts = pbyts;
5657 idxs = pidxs;
5658 }
5659
5660 /* Don't check for CTRL-C too often, it takes time. */
5661 if (--breakcheckcount == 0)
5662 {
5663 ui_breakcheck();
5664 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005665 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005666 }
5667 }
5668}
5669
Bram Moolenaar4770d092006-01-12 23:22:24 +00005670
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005671/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00005672 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005673 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005674 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005675go_deeper(trystate_T *stack, int depth, int score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005676{
Bram Moolenaarea424162005-06-16 21:51:00 +00005677 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005678 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005679 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005680 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +00005681 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005682}
5683
Bram Moolenaar53805d12005-08-01 07:08:33 +00005684/*
5685 * Case-folding may change the number of bytes: Count nr of chars in
5686 * fword[flen] and return the byte length of that many chars in "word".
5687 */
5688 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005689nofold_len(char_u *fword, int flen, char_u *word)
Bram Moolenaar53805d12005-08-01 07:08:33 +00005690{
5691 char_u *p;
5692 int i = 0;
5693
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005694 for (p = fword; p < fword + flen; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00005695 ++i;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005696 for (p = word; i > 0; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00005697 --i;
5698 return (int)(p - word);
5699}
Bram Moolenaar53805d12005-08-01 07:08:33 +00005700
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005701/*
5702 * "fword" is a good word with case folded. Find the matching keep-case
5703 * words and put it in "kword".
5704 * Theoretically there could be several keep-case words that result in the
5705 * same case-folded word, but we only find one...
5706 */
5707 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005708find_keepcap_word(slang_T *slang, char_u *fword, char_u *kword)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005709{
5710 char_u uword[MAXWLEN]; /* "fword" in upper-case */
5711 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005712 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005713
5714 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005715 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005716 int round[MAXWLEN];
5717 int fwordidx[MAXWLEN];
5718 int uwordidx[MAXWLEN];
5719 int kwordlen[MAXWLEN];
5720
5721 int flen, ulen;
5722 int l;
5723 int len;
5724 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005725 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005726 char_u *p;
5727 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005728 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005729
5730 if (byts == NULL)
5731 {
5732 /* array is empty: "cannot happen" */
5733 *kword = NUL;
5734 return;
5735 }
5736
5737 /* Make an all-cap version of "fword". */
5738 allcap_copy(fword, uword);
5739
5740 /*
5741 * Each character needs to be tried both case-folded and upper-case.
5742 * All this gets very complicated if we keep in mind that changing case
5743 * may change the byte length of a multi-byte character...
5744 */
5745 depth = 0;
5746 arridx[0] = 0;
5747 round[0] = 0;
5748 fwordidx[0] = 0;
5749 uwordidx[0] = 0;
5750 kwordlen[0] = 0;
5751 while (depth >= 0)
5752 {
5753 if (fword[fwordidx[depth]] == NUL)
5754 {
5755 /* We are at the end of "fword". If the tree allows a word to end
5756 * here we have found a match. */
5757 if (byts[arridx[depth] + 1] == 0)
5758 {
5759 kword[kwordlen[depth]] = NUL;
5760 return;
5761 }
5762
5763 /* kword is getting too long, continue one level up */
5764 --depth;
5765 }
5766 else if (++round[depth] > 2)
5767 {
5768 /* tried both fold-case and upper-case character, continue one
5769 * level up */
5770 --depth;
5771 }
5772 else
5773 {
5774 /*
5775 * round[depth] == 1: Try using the folded-case character.
5776 * round[depth] == 2: Try using the upper-case character.
5777 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005778 if (has_mbyte)
5779 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005780 flen = MB_CPTR2LEN(fword + fwordidx[depth]);
5781 ulen = MB_CPTR2LEN(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005782 }
5783 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005784 ulen = flen = 1;
5785 if (round[depth] == 1)
5786 {
5787 p = fword + fwordidx[depth];
5788 l = flen;
5789 }
5790 else
5791 {
5792 p = uword + uwordidx[depth];
5793 l = ulen;
5794 }
5795
5796 for (tryidx = arridx[depth]; l > 0; --l)
5797 {
5798 /* Perform a binary search in the list of accepted bytes. */
5799 len = byts[tryidx++];
5800 c = *p++;
5801 lo = tryidx;
5802 hi = tryidx + len - 1;
5803 while (lo < hi)
5804 {
5805 m = (lo + hi) / 2;
5806 if (byts[m] > c)
5807 hi = m - 1;
5808 else if (byts[m] < c)
5809 lo = m + 1;
5810 else
5811 {
5812 lo = hi = m;
5813 break;
5814 }
5815 }
5816
5817 /* Stop if there is no matching byte. */
5818 if (hi < lo || byts[lo] != c)
5819 break;
5820
5821 /* Continue at the child (if there is one). */
5822 tryidx = idxs[lo];
5823 }
5824
5825 if (l == 0)
5826 {
5827 /*
5828 * Found the matching char. Copy it to "kword" and go a
5829 * level deeper.
5830 */
5831 if (round[depth] == 1)
5832 {
5833 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
5834 flen);
5835 kwordlen[depth + 1] = kwordlen[depth] + flen;
5836 }
5837 else
5838 {
5839 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
5840 ulen);
5841 kwordlen[depth + 1] = kwordlen[depth] + ulen;
5842 }
5843 fwordidx[depth + 1] = fwordidx[depth] + flen;
5844 uwordidx[depth + 1] = uwordidx[depth] + ulen;
5845
5846 ++depth;
5847 arridx[depth] = tryidx;
5848 round[depth] = 0;
5849 }
5850 }
5851 }
5852
5853 /* Didn't find it: "cannot happen". */
5854 *kword = NUL;
5855}
5856
5857/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005858 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
5859 * su->su_sga.
5860 */
5861 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005862score_comp_sal(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005863{
5864 langp_T *lp;
5865 char_u badsound[MAXWLEN];
5866 int i;
5867 suggest_T *stp;
5868 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005869 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005870 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005871
5872 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
5873 return;
5874
5875 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005876 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005877 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005878 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005879 if (lp->lp_slang->sl_sal.ga_len > 0)
5880 {
5881 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005882 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005883
5884 for (i = 0; i < su->su_ga.ga_len; ++i)
5885 {
5886 stp = &SUG(su->su_ga, i);
5887
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005888 /* Case-fold the suggested word, sound-fold it and compute the
5889 * sound-a-like score. */
5890 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005891 if (score < SCORE_MAXMAX)
5892 {
5893 /* Add the suggestion. */
5894 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
5895 sstp->st_word = vim_strsave(stp->st_word);
5896 if (sstp->st_word != NULL)
5897 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005898 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005899 sstp->st_score = score;
5900 sstp->st_altscore = 0;
5901 sstp->st_orglen = stp->st_orglen;
5902 ++su->su_sga.ga_len;
5903 }
5904 }
5905 }
5906 break;
5907 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005908 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005909}
5910
5911/*
5912 * Combine the list of suggestions in su->su_ga and su->su_sga.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005913 * They are entwined.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005914 */
5915 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005916score_combine(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005917{
5918 int i;
5919 int j;
5920 garray_T ga;
5921 garray_T *gap;
5922 langp_T *lp;
5923 suggest_T *stp;
5924 char_u *p;
5925 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005926 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005927 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005928 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005929
5930 /* Add the alternate score to su_ga. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005931 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005932 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005933 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005934 if (lp->lp_slang->sl_sal.ga_len > 0)
5935 {
5936 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005937 slang = lp->lp_slang;
5938 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005939
5940 for (i = 0; i < su->su_ga.ga_len; ++i)
5941 {
5942 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005943 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005944 if (stp->st_altscore == SCORE_MAXMAX)
5945 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
5946 else
5947 stp->st_score = (stp->st_score * 3
5948 + stp->st_altscore) / 4;
5949 stp->st_salscore = FALSE;
5950 }
5951 break;
5952 }
5953 }
5954
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005955 if (slang == NULL) /* Using "double" without sound folding. */
5956 {
5957 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
5958 su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005959 return;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005960 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005961
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005962 /* Add the alternate score to su_sga. */
5963 for (i = 0; i < su->su_sga.ga_len; ++i)
5964 {
5965 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005966 stp->st_altscore = spell_edit_score(slang,
5967 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005968 if (stp->st_score == SCORE_MAXMAX)
5969 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
5970 else
5971 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
5972 stp->st_salscore = TRUE;
5973 }
5974
Bram Moolenaar4770d092006-01-12 23:22:24 +00005975 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
5976 * for both lists. */
5977 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005978 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005979 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005980 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
5981
5982 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
5983 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
5984 return;
5985
5986 stp = &SUG(ga, 0);
5987 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
5988 {
5989 /* round 1: get a suggestion from su_ga
5990 * round 2: get a suggestion from su_sga */
5991 for (round = 1; round <= 2; ++round)
5992 {
5993 gap = round == 1 ? &su->su_ga : &su->su_sga;
5994 if (i < gap->ga_len)
5995 {
5996 /* Don't add a word if it's already there. */
5997 p = SUG(*gap, i).st_word;
5998 for (j = 0; j < ga.ga_len; ++j)
5999 if (STRCMP(stp[j].st_word, p) == 0)
6000 break;
6001 if (j == ga.ga_len)
6002 stp[ga.ga_len++] = SUG(*gap, i);
6003 else
6004 vim_free(p);
6005 }
6006 }
6007 }
6008
6009 ga_clear(&su->su_ga);
6010 ga_clear(&su->su_sga);
6011
6012 /* Truncate the list to the number of suggestions that will be displayed. */
6013 if (ga.ga_len > su->su_maxcount)
6014 {
6015 for (i = su->su_maxcount; i < ga.ga_len; ++i)
6016 vim_free(stp[i].st_word);
6017 ga.ga_len = su->su_maxcount;
6018 }
6019
6020 su->su_ga = ga;
6021}
6022
6023/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006024 * For the goodword in "stp" compute the soundalike score compared to the
6025 * badword.
6026 */
6027 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006028stp_sal_score(
6029 suggest_T *stp,
6030 suginfo_T *su,
6031 slang_T *slang,
6032 char_u *badsound) /* sound-folded badword */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006033{
6034 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006035 char_u *pbad;
6036 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006037 char_u badsound2[MAXWLEN];
6038 char_u fword[MAXWLEN];
6039 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006040 char_u goodword[MAXWLEN];
6041 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006042
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006043 lendiff = (int)(su->su_badlen - stp->st_orglen);
6044 if (lendiff >= 0)
6045 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006046 else
6047 {
6048 /* soundfold the bad word with more characters following */
6049 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
6050
6051 /* When joining two words the sound often changes a lot. E.g., "t he"
6052 * sounds like "t h" while "the" sounds like "@". Avoid that by
6053 * removing the space. Don't do it when the good word also contains a
6054 * space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006055 if (VIM_ISWHITE(su->su_badptr[su->su_badlen])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006056 && *skiptowhite(stp->st_word) == NUL)
6057 for (p = fword; *(p = skiptowhite(p)) != NUL; )
Bram Moolenaara7241f52008-06-24 20:39:31 +00006058 STRMOVE(p, p + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006059
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006060 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006061 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006062 }
6063
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006064 if (lendiff > 0 && stp->st_wordlen + lendiff < MAXWLEN)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006065 {
6066 /* Add part of the bad word to the good word, so that we soundfold
6067 * what replaces the bad word. */
6068 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006069 vim_strncpy(goodword + stp->st_wordlen,
6070 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006071 pgood = goodword;
6072 }
6073 else
6074 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006075
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006076 /* Sound-fold the word and compute the score for the difference. */
6077 spell_soundfold(slang, pgood, FALSE, goodsound);
6078
6079 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006080}
6081
Bram Moolenaar4770d092006-01-12 23:22:24 +00006082/* structure used to store soundfolded words that add_sound_suggest() has
6083 * handled already. */
6084typedef struct
6085{
6086 short sft_score; /* lowest score used */
6087 char_u sft_word[1]; /* soundfolded word, actually longer */
6088} sftword_T;
6089
6090static sftword_T dumsft;
6091#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
6092#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
6093
6094/*
6095 * Prepare for calling suggest_try_soundalike().
6096 */
6097 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006098suggest_try_soundalike_prep(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006099{
6100 langp_T *lp;
6101 int lpi;
6102 slang_T *slang;
6103
6104 /* Do this for all languages that support sound folding and for which a
6105 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006106 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006107 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006108 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006109 slang = lp->lp_slang;
6110 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
6111 /* prepare the hashtable used by add_sound_suggest() */
6112 hash_init(&slang->sl_sounddone);
6113 }
6114}
6115
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006116/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006117 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006118 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006119 */
6120 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006121suggest_try_soundalike(suginfo_T *su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006122{
6123 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006124 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006125 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006126 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006127
Bram Moolenaar4770d092006-01-12 23:22:24 +00006128 /* Do this for all languages that support sound folding and for which a
6129 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006130 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006131 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006132 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006133 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006134 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006135 {
6136 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006137 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006138
Bram Moolenaar4770d092006-01-12 23:22:24 +00006139 /* try all kinds of inserts/deletes/swaps/etc. */
6140 /* TODO: also soundfold the next words, so that we can try joining
6141 * and splitting */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01006142#ifdef SUGGEST_PROFILE
6143 prof_init();
6144#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00006145 suggest_trie_walk(su, lp, salword, TRUE);
Bram Moolenaarca1fe982016-01-07 16:22:06 +01006146#ifdef SUGGEST_PROFILE
6147 prof_report("soundalike");
6148#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00006149 }
6150 }
6151}
6152
6153/*
6154 * Finish up after calling suggest_try_soundalike().
6155 */
6156 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006157suggest_try_soundalike_finish(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006158{
6159 langp_T *lp;
6160 int lpi;
6161 slang_T *slang;
6162 int todo;
6163 hashitem_T *hi;
6164
6165 /* Do this for all languages that support sound folding and for which a
6166 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006167 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006168 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006169 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006170 slang = lp->lp_slang;
6171 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
6172 {
6173 /* Free the info about handled words. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006174 todo = (int)slang->sl_sounddone.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006175 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
6176 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006177 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006178 vim_free(HI2SFT(hi));
6179 --todo;
6180 }
Bram Moolenaar6417da62007-03-08 13:49:53 +00006181
6182 /* Clear the hashtable, it may also be used by another region. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006183 hash_clear(&slang->sl_sounddone);
Bram Moolenaar6417da62007-03-08 13:49:53 +00006184 hash_init(&slang->sl_sounddone);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006185 }
6186 }
6187}
6188
6189/*
6190 * A match with a soundfolded word is found. Add the good word(s) that
6191 * produce this soundfolded word.
6192 */
6193 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006194add_sound_suggest(
6195 suginfo_T *su,
6196 char_u *goodword,
6197 int score, /* soundfold score */
6198 langp_T *lp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006199{
6200 slang_T *slang = lp->lp_slang; /* language for sound folding */
6201 int sfwordnr;
6202 char_u *nrline;
6203 int orgnr;
6204 char_u theword[MAXWLEN];
6205 int i;
6206 int wlen;
6207 char_u *byts;
6208 idx_T *idxs;
6209 int n;
6210 int wordcount;
6211 int wc;
6212 int goodscore;
6213 hash_T hash;
6214 hashitem_T *hi;
6215 sftword_T *sft;
6216 int bc, gc;
6217 int limit;
6218
6219 /*
6220 * It's very well possible that the same soundfold word is found several
6221 * times with different scores. Since the following is quite slow only do
6222 * the words that have a better score than before. Use a hashtable to
6223 * remember the words that have been done.
6224 */
6225 hash = hash_hash(goodword);
6226 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
6227 if (HASHITEM_EMPTY(hi))
6228 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006229 sft = alloc(sizeof(sftword_T) + STRLEN(goodword));
Bram Moolenaar4770d092006-01-12 23:22:24 +00006230 if (sft != NULL)
6231 {
6232 sft->sft_score = score;
6233 STRCPY(sft->sft_word, goodword);
6234 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
6235 }
6236 }
6237 else
6238 {
6239 sft = HI2SFT(hi);
6240 if (score >= sft->sft_score)
6241 return;
6242 sft->sft_score = score;
6243 }
6244
6245 /*
6246 * Find the word nr in the soundfold tree.
6247 */
6248 sfwordnr = soundfold_find(slang, goodword);
6249 if (sfwordnr < 0)
6250 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01006251 internal_error("add_sound_suggest()");
Bram Moolenaar4770d092006-01-12 23:22:24 +00006252 return;
6253 }
6254
6255 /*
6256 * go over the list of good words that produce this soundfold word
6257 */
6258 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
6259 orgnr = 0;
6260 while (*nrline != NUL)
6261 {
6262 /* The wordnr was stored in a minimal nr of bytes as an offset to the
6263 * previous wordnr. */
6264 orgnr += bytes2offset(&nrline);
6265
6266 byts = slang->sl_fbyts;
6267 idxs = slang->sl_fidxs;
6268
6269 /* Lookup the word "orgnr" one of the two tries. */
6270 n = 0;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006271 wordcount = 0;
Bram Moolenaarace8d8e2013-11-21 17:42:31 +01006272 for (wlen = 0; wlen < MAXWLEN - 3; ++wlen)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006273 {
6274 i = 1;
6275 if (wordcount == orgnr && byts[n + 1] == NUL)
6276 break; /* found end of word */
6277
6278 if (byts[n + 1] == NUL)
6279 ++wordcount;
6280
6281 /* skip over the NUL bytes */
6282 for ( ; byts[n + i] == NUL; ++i)
6283 if (i > byts[n]) /* safety check */
6284 {
6285 STRCPY(theword + wlen, "BAD");
Bram Moolenaarace8d8e2013-11-21 17:42:31 +01006286 wlen += 3;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006287 goto badword;
6288 }
6289
6290 /* One of the siblings must have the word. */
6291 for ( ; i < byts[n]; ++i)
6292 {
6293 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
6294 if (wordcount + wc > orgnr)
6295 break;
6296 wordcount += wc;
6297 }
6298
Bram Moolenaarace8d8e2013-11-21 17:42:31 +01006299 theword[wlen] = byts[n + i];
Bram Moolenaar4770d092006-01-12 23:22:24 +00006300 n = idxs[n + i];
6301 }
6302badword:
6303 theword[wlen] = NUL;
6304
6305 /* Go over the possible flags and regions. */
6306 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
6307 {
6308 char_u cword[MAXWLEN];
6309 char_u *p;
6310 int flags = (int)idxs[n + i];
6311
Bram Moolenaare1438bb2006-03-01 22:01:55 +00006312 /* Skip words with the NOSUGGEST flag */
6313 if (flags & WF_NOSUGGEST)
6314 continue;
6315
Bram Moolenaar4770d092006-01-12 23:22:24 +00006316 if (flags & WF_KEEPCAP)
6317 {
6318 /* Must find the word in the keep-case tree. */
6319 find_keepcap_word(slang, theword, cword);
6320 p = cword;
6321 }
6322 else
6323 {
6324 flags |= su->su_badflags;
6325 if ((flags & WF_CAPMASK) != 0)
6326 {
6327 /* Need to fix case according to "flags". */
6328 make_case_word(theword, cword, flags);
6329 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006330 }
6331 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00006332 p = theword;
6333 }
6334
6335 /* Add the suggestion. */
6336 if (sps_flags & SPS_DOUBLE)
6337 {
6338 /* Add the suggestion if the score isn't too bad. */
6339 if (score <= su->su_maxscore)
6340 add_suggestion(su, &su->su_sga, p, su->su_badlen,
6341 score, 0, FALSE, slang, FALSE);
6342 }
6343 else
6344 {
6345 /* Add a penalty for words in another region. */
6346 if ((flags & WF_REGION)
6347 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
6348 goodscore = SCORE_REGION;
6349 else
6350 goodscore = 0;
6351
6352 /* Add a small penalty for changing the first letter from
6353 * lower to upper case. Helps for "tath" -> "Kath", which is
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006354 * less common than "tath" -> "path". Don't do it when the
Bram Moolenaar4770d092006-01-12 23:22:24 +00006355 * letter is the same, that has already been counted. */
6356 gc = PTR2CHAR(p);
6357 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006358 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006359 bc = PTR2CHAR(su->su_badword);
6360 if (!SPELL_ISUPPER(bc)
6361 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
6362 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006363 }
6364
Bram Moolenaar4770d092006-01-12 23:22:24 +00006365 /* Compute the score for the good word. This only does letter
6366 * insert/delete/swap/replace. REP items are not considered,
6367 * which may make the score a bit higher.
6368 * Use a limit for the score to make it work faster. Use
6369 * MAXSCORE(), because RESCORE() will change the score.
6370 * If the limit is very high then the iterative method is
6371 * inefficient, using an array is quicker. */
6372 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
6373 if (limit > SCORE_LIMITMAX)
6374 goodscore += spell_edit_score(slang, su->su_badword, p);
6375 else
6376 goodscore += spell_edit_score_limit(slang, su->su_badword,
6377 p, limit);
6378
6379 /* When going over the limit don't bother to do the rest. */
6380 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006381 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006382 /* Give a bonus to words seen before. */
6383 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006384
Bram Moolenaar4770d092006-01-12 23:22:24 +00006385 /* Add the suggestion if the score isn't too bad. */
6386 goodscore = RESCORE(goodscore, score);
6387 if (goodscore <= su->su_sfmaxscore)
6388 add_suggestion(su, &su->su_ga, p, su->su_badlen,
6389 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006390 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006391 }
6392 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006393 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006394 }
6395}
6396
6397/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00006398 * Find word "word" in fold-case tree for "slang" and return the word number.
6399 */
6400 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006401soundfold_find(slang_T *slang, char_u *word)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006402{
6403 idx_T arridx = 0;
6404 int len;
6405 int wlen = 0;
6406 int c;
6407 char_u *ptr = word;
6408 char_u *byts;
6409 idx_T *idxs;
6410 int wordnr = 0;
6411
6412 byts = slang->sl_sbyts;
6413 idxs = slang->sl_sidxs;
6414
6415 for (;;)
6416 {
6417 /* First byte is the number of possible bytes. */
6418 len = byts[arridx++];
6419
6420 /* If the first possible byte is a zero the word could end here.
6421 * If the word ends we found the word. If not skip the NUL bytes. */
6422 c = ptr[wlen];
6423 if (byts[arridx] == NUL)
6424 {
6425 if (c == NUL)
6426 break;
6427
6428 /* Skip over the zeros, there can be several. */
6429 while (len > 0 && byts[arridx] == NUL)
6430 {
6431 ++arridx;
6432 --len;
6433 }
6434 if (len == 0)
6435 return -1; /* no children, word should have ended here */
6436 ++wordnr;
6437 }
6438
6439 /* If the word ends we didn't find it. */
6440 if (c == NUL)
6441 return -1;
6442
6443 /* Perform a binary search in the list of accepted bytes. */
6444 if (c == TAB) /* <Tab> is handled like <Space> */
6445 c = ' ';
6446 while (byts[arridx] < c)
6447 {
6448 /* The word count is in the first idxs[] entry of the child. */
6449 wordnr += idxs[idxs[arridx]];
6450 ++arridx;
6451 if (--len == 0) /* end of the bytes, didn't find it */
6452 return -1;
6453 }
6454 if (byts[arridx] != c) /* didn't find the byte */
6455 return -1;
6456
6457 /* Continue at the child (if there is one). */
6458 arridx = idxs[arridx];
6459 ++wlen;
6460
6461 /* One space in the good word may stand for several spaces in the
6462 * checked word. */
6463 if (c == ' ')
6464 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
6465 ++wlen;
6466 }
6467
6468 return wordnr;
6469}
6470
6471/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006472 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006473 */
6474 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006475make_case_word(char_u *fword, char_u *cword, int flags)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006476{
6477 if (flags & WF_ALLCAP)
6478 /* Make it all upper-case */
6479 allcap_copy(fword, cword);
6480 else if (flags & WF_ONECAP)
6481 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006482 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006483 else
6484 /* Use goodword as-is. */
6485 STRCPY(cword, fword);
6486}
6487
Bram Moolenaarea424162005-06-16 21:51:00 +00006488
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006489/*
6490 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
6491 * lines in the .aff file.
6492 */
6493 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006494similar_chars(slang_T *slang, int c1, int c2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006495{
Bram Moolenaarea424162005-06-16 21:51:00 +00006496 int m1, m2;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006497 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarea424162005-06-16 21:51:00 +00006498 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006499
Bram Moolenaarea424162005-06-16 21:51:00 +00006500 if (c1 >= 256)
6501 {
6502 buf[mb_char2bytes(c1, buf)] = 0;
6503 hi = hash_find(&slang->sl_map_hash, buf);
6504 if (HASHITEM_EMPTY(hi))
6505 m1 = 0;
6506 else
6507 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
6508 }
6509 else
Bram Moolenaarea424162005-06-16 21:51:00 +00006510 m1 = slang->sl_map_array[c1];
6511 if (m1 == 0)
6512 return FALSE;
6513
6514
Bram Moolenaarea424162005-06-16 21:51:00 +00006515 if (c2 >= 256)
6516 {
6517 buf[mb_char2bytes(c2, buf)] = 0;
6518 hi = hash_find(&slang->sl_map_hash, buf);
6519 if (HASHITEM_EMPTY(hi))
6520 m2 = 0;
6521 else
6522 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
6523 }
6524 else
Bram Moolenaarea424162005-06-16 21:51:00 +00006525 m2 = slang->sl_map_array[c2];
6526
6527 return m1 == m2;
6528}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006529
6530/*
6531 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +00006532 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006533 */
6534 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006535add_suggestion(
6536 suginfo_T *su,
6537 garray_T *gap, /* either su_ga or su_sga */
6538 char_u *goodword,
6539 int badlenarg, /* len of bad word replaced with "goodword" */
6540 int score,
6541 int altscore,
6542 int had_bonus, /* value for st_had_bonus */
6543 slang_T *slang, /* language for sound folding */
6544 int maxsf) /* su_maxscore applies to soundfold score,
Bram Moolenaar4770d092006-01-12 23:22:24 +00006545 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006546{
Bram Moolenaar4770d092006-01-12 23:22:24 +00006547 int goodlen; /* len of goodword changed */
6548 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006549 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006550 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006551 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006552 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006553
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006554 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
6555 * "thee the" is added next to changing the first "the" the "thee". */
6556 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006557 pbad = su->su_badptr + badlenarg;
6558 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006559 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006560 goodlen = (int)(pgood - goodword);
6561 badlen = (int)(pbad - su->su_badptr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006562 if (goodlen <= 0 || badlen <= 0)
6563 break;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006564 MB_PTR_BACK(goodword, pgood);
6565 MB_PTR_BACK(su->su_badptr, pbad);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006566 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006567 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006568 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
6569 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006570 }
Bram Moolenaar264b74f2019-01-24 17:18:42 +01006571 else if (*pgood != *pbad)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006572 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006573 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006574
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006575 if (badlen == 0 && goodlen == 0)
6576 /* goodword doesn't change anything; may happen for "the the" changing
6577 * the first "the" to itself. */
6578 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006579
Bram Moolenaar89d40322006-08-29 15:30:07 +00006580 if (gap->ga_len == 0)
6581 i = -1;
6582 else
6583 {
6584 /* Check if the word is already there. Also check the length that is
6585 * being replaced "thes," -> "these" is a different suggestion from
6586 * "thes" -> "these". */
6587 stp = &SUG(*gap, 0);
6588 for (i = gap->ga_len; --i >= 0; ++stp)
6589 if (stp->st_wordlen == goodlen
6590 && stp->st_orglen == badlen
6591 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006592 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00006593 /*
6594 * Found it. Remember the word with the lowest score.
6595 */
6596 if (stp->st_slang == NULL)
6597 stp->st_slang = slang;
6598
6599 new_sug.st_score = score;
6600 new_sug.st_altscore = altscore;
6601 new_sug.st_had_bonus = had_bonus;
6602
6603 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006604 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00006605 /* Only one of the two had the soundalike score computed.
6606 * Need to do that for the other one now, otherwise the
6607 * scores can't be compared. This happens because
6608 * suggest_try_change() doesn't compute the soundalike
6609 * word to keep it fast, while some special methods set
6610 * the soundalike score to zero. */
6611 if (had_bonus)
6612 rescore_one(su, stp);
6613 else
6614 {
6615 new_sug.st_word = stp->st_word;
6616 new_sug.st_wordlen = stp->st_wordlen;
6617 new_sug.st_slang = stp->st_slang;
6618 new_sug.st_orglen = badlen;
6619 rescore_one(su, &new_sug);
6620 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006621 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006622
Bram Moolenaar89d40322006-08-29 15:30:07 +00006623 if (stp->st_score > new_sug.st_score)
6624 {
6625 stp->st_score = new_sug.st_score;
6626 stp->st_altscore = new_sug.st_altscore;
6627 stp->st_had_bonus = new_sug.st_had_bonus;
6628 }
6629 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006630 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00006631 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006632
Bram Moolenaar4770d092006-01-12 23:22:24 +00006633 if (i < 0 && ga_grow(gap, 1) == OK)
6634 {
6635 /* Add a suggestion. */
6636 stp = &SUG(*gap, gap->ga_len);
6637 stp->st_word = vim_strnsave(goodword, goodlen);
6638 if (stp->st_word != NULL)
6639 {
6640 stp->st_wordlen = goodlen;
6641 stp->st_score = score;
6642 stp->st_altscore = altscore;
6643 stp->st_had_bonus = had_bonus;
6644 stp->st_orglen = badlen;
6645 stp->st_slang = slang;
6646 ++gap->ga_len;
6647
6648 /* If we have too many suggestions now, sort the list and keep
6649 * the best suggestions. */
6650 if (gap->ga_len > SUG_MAX_COUNT(su))
6651 {
6652 if (maxsf)
6653 su->su_sfmaxscore = cleanup_suggestions(gap,
6654 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
6655 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00006656 su->su_maxscore = cleanup_suggestions(gap,
6657 su->su_maxscore, SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006658 }
6659 }
6660 }
6661}
6662
6663/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00006664 * Suggestions may in fact be flagged as errors. Esp. for banned words and
6665 * for split words, such as "the the". Remove these from the list here.
6666 */
6667 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006668check_suggestions(
6669 suginfo_T *su,
6670 garray_T *gap) /* either su_ga or su_sga */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006671{
6672 suggest_T *stp;
6673 int i;
6674 char_u longword[MAXWLEN + 1];
6675 int len;
6676 hlf_T attr;
6677
6678 stp = &SUG(*gap, 0);
6679 for (i = gap->ga_len - 1; i >= 0; --i)
6680 {
6681 /* Need to append what follows to check for "the the". */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006682 vim_strncpy(longword, stp[i].st_word, MAXWLEN);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006683 len = stp[i].st_wordlen;
6684 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
6685 MAXWLEN - len);
6686 attr = HLF_COUNT;
6687 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
6688 if (attr != HLF_COUNT)
6689 {
6690 /* Remove this entry. */
6691 vim_free(stp[i].st_word);
6692 --gap->ga_len;
6693 if (i < gap->ga_len)
6694 mch_memmove(stp + i, stp + i + 1,
6695 sizeof(suggest_T) * (gap->ga_len - i));
6696 }
6697 }
6698}
6699
6700
6701/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006702 * Add a word to be banned.
6703 */
6704 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006705add_banned(
6706 suginfo_T *su,
6707 char_u *word)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006708{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006709 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006710 hash_T hash;
6711 hashitem_T *hi;
6712
Bram Moolenaar4770d092006-01-12 23:22:24 +00006713 hash = hash_hash(word);
6714 hi = hash_lookup(&su->su_banned, word, hash);
6715 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006716 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006717 s = vim_strsave(word);
6718 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006719 hash_add_item(&su->su_banned, hi, s, hash);
6720 }
6721}
6722
6723/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006724 * Recompute the score for all suggestions if sound-folding is possible. This
6725 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006726 */
6727 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006728rescore_suggestions(suginfo_T *su)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006729{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006730 int i;
6731
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006732 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006733 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006734 rescore_one(su, &SUG(su->su_ga, i));
6735}
6736
6737/*
6738 * Recompute the score for one suggestion if sound-folding is possible.
6739 */
6740 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006741rescore_one(suginfo_T *su, suggest_T *stp)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006742{
6743 slang_T *slang = stp->st_slang;
6744 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +00006745 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006746
6747 /* Only rescore suggestions that have no sal score yet and do have a
6748 * language. */
6749 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
6750 {
6751 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +00006752 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006753 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006754 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006755 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +00006756 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006757 }
Bram Moolenaar4effc802005-09-30 21:12:02 +00006758
6759 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006760 if (stp->st_altscore == SCORE_MAXMAX)
6761 stp->st_altscore = SCORE_BIG;
6762 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
6763 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006764 }
6765}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006766
Bram Moolenaareae1b912019-05-09 15:12:55 +02006767static int sug_compare(const void *s1, const void *s2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006768
6769/*
6770 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +00006771 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006772 */
6773 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006774sug_compare(const void *s1, const void *s2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006775{
6776 suggest_T *p1 = (suggest_T *)s1;
6777 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006778 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006779
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006780 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +00006781 {
6782 n = p1->st_altscore - p2->st_altscore;
6783 if (n == 0)
6784 n = STRICMP(p1->st_word, p2->st_word);
6785 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006786 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006787}
6788
6789/*
6790 * Cleanup the suggestions:
6791 * - Sort on score.
6792 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006793 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006794 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006795 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006796cleanup_suggestions(
6797 garray_T *gap,
6798 int maxscore,
6799 int keep) /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006800{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006801 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006802 int i;
6803
6804 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006805 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006806
6807 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006808 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006809 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006810 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006811 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006812 gap->ga_len = keep;
6813 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006814 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006815 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006816}
6817
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006818#if defined(FEAT_EVAL) || defined(PROTO)
6819/*
6820 * Soundfold a string, for soundfold().
6821 * Result is in allocated memory, NULL for an error.
6822 */
6823 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006824eval_soundfold(char_u *word)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006825{
6826 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006827 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006828 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006829
Bram Moolenaar860cae12010-06-05 23:22:07 +02006830 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006831 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006832 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006833 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006834 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006835 if (lp->lp_slang->sl_sal.ga_len > 0)
6836 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006837 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006838 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006839 return vim_strsave(sound);
6840 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006841 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006842
6843 /* No language with sound folding, return word as-is. */
6844 return vim_strsave(word);
6845}
6846#endif
6847
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006848/*
6849 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +00006850 *
6851 * There are many ways to turn a word into a sound-a-like representation. The
6852 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
6853 * swedish name matching - survey and test of different algorithms" by Klas
6854 * Erikson.
6855 *
6856 * We support two methods:
6857 * 1. SOFOFROM/SOFOTO do a simple character mapping.
6858 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006859 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006860 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006861spell_soundfold(
6862 slang_T *slang,
6863 char_u *inword,
6864 int folded, /* "inword" is already case-folded */
6865 char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006866{
6867 char_u fword[MAXWLEN];
6868 char_u *word;
6869
6870 if (slang->sl_sofo)
6871 /* SOFOFROM and SOFOTO used */
6872 spell_soundfold_sofo(slang, inword, res);
6873 else
6874 {
6875 /* SAL items used. Requires the word to be case-folded. */
6876 if (folded)
6877 word = inword;
6878 else
6879 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006880 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006881 word = fword;
6882 }
6883
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006884 if (has_mbyte)
6885 spell_soundfold_wsal(slang, word, res);
6886 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006887 spell_soundfold_sal(slang, word, res);
6888 }
6889}
6890
6891/*
6892 * Perform sound folding of "inword" into "res" according to SOFOFROM and
6893 * SOFOTO lines.
6894 */
6895 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006896spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006897{
6898 char_u *s;
6899 int ri = 0;
6900 int c;
6901
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006902 if (has_mbyte)
6903 {
6904 int prevc = 0;
6905 int *ip;
6906
6907 /* The sl_sal_first[] table contains the translation for chars up to
6908 * 255, sl_sal the rest. */
6909 for (s = inword; *s != NUL; )
6910 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006911 c = mb_cptr2char_adv(&s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006912 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006913 c = ' ';
6914 else if (c < 256)
6915 c = slang->sl_sal_first[c];
6916 else
6917 {
6918 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
6919 if (ip == NULL) /* empty list, can't match */
6920 c = NUL;
6921 else
6922 for (;;) /* find "c" in the list */
6923 {
6924 if (*ip == 0) /* not found */
6925 {
6926 c = NUL;
6927 break;
6928 }
6929 if (*ip == c) /* match! */
6930 {
6931 c = ip[1];
6932 break;
6933 }
6934 ip += 2;
6935 }
6936 }
6937
6938 if (c != NUL && c != prevc)
6939 {
6940 ri += mb_char2bytes(c, res + ri);
6941 if (ri + MB_MAXBYTES > MAXWLEN)
6942 break;
6943 prevc = c;
6944 }
6945 }
6946 }
6947 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006948 {
6949 /* The sl_sal_first[] table contains the translation. */
6950 for (s = inword; (c = *s) != NUL; ++s)
6951 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006952 if (VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006953 c = ' ';
6954 else
6955 c = slang->sl_sal_first[c];
6956 if (c != NUL && (ri == 0 || res[ri - 1] != c))
6957 res[ri++] = c;
6958 }
6959 }
6960
6961 res[ri] = NUL;
6962}
6963
6964 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006965spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006966{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006967 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006968 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006969 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006970 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006971 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006972 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006973 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006974 int n, k = 0;
6975 int z0;
6976 int k0;
6977 int n0;
6978 int c;
6979 int pri;
6980 int p0 = -333;
6981 int c0;
6982
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006983 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006984 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006985 if (slang->sl_rem_accents)
6986 {
6987 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006988 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006989 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006990 if (VIM_ISWHITE(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006991 {
6992 *t++ = ' ';
6993 s = skipwhite(s);
6994 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006995 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006996 {
Bram Moolenaarcc63c642013-11-12 04:44:01 +01006997 if (spell_iswordp_nmw(s, curwin))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006998 *t++ = *s;
6999 ++s;
7000 }
7001 }
7002 *t = NUL;
7003 }
7004 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007005 vim_strncpy(word, s, MAXWLEN - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007006
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007007 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007008
7009 /*
7010 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007011 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007012 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007013 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007014 while ((c = word[i]) != NUL)
7015 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007016 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007017 n = slang->sl_sal_first[c];
7018 z0 = 0;
7019
7020 if (n >= 0)
7021 {
7022 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007023 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007024 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007025 /* Quickly skip entries that don't match the word. Most
7026 * entries are less then three chars, optimize for that. */
7027 k = smp[n].sm_leadlen;
7028 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007029 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007030 if (word[i + 1] != s[1])
7031 continue;
7032 if (k > 2)
7033 {
7034 for (j = 2; j < k; ++j)
7035 if (word[i + j] != s[j])
7036 break;
7037 if (j < k)
7038 continue;
7039 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007040 }
7041
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007042 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007043 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007044 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007045 while (*pf != NUL && *pf != word[i + k])
7046 ++pf;
7047 if (*pf == NUL)
7048 continue;
7049 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007050 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007051 s = smp[n].sm_rules;
7052 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007053
7054 p0 = *s;
7055 k0 = k;
7056 while (*s == '-' && k > 1)
7057 {
7058 k--;
7059 s++;
7060 }
7061 if (*s == '<')
7062 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007063 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007064 {
7065 /* determine priority */
7066 pri = *s - '0';
7067 s++;
7068 }
7069 if (*s == '^' && *(s + 1) == '^')
7070 s++;
7071
7072 if (*s == NUL
7073 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007074 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02007075 || spell_iswordp(word + i - 1, curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007076 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02007077 || (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007078 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02007079 && spell_iswordp(word + i - 1, curwin)
7080 && (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007081 {
7082 /* search for followup rules, if: */
7083 /* followup and k > 1 and NO '-' in searchstring */
7084 c0 = word[i + k - 1];
7085 n0 = slang->sl_sal_first[c0];
7086
7087 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007088 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007089 {
7090 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007091 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007092 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007093 /* Quickly skip entries that don't match the word.
7094 * */
7095 k0 = smp[n0].sm_leadlen;
7096 if (k0 > 1)
7097 {
7098 if (word[i + k] != s[1])
7099 continue;
7100 if (k0 > 2)
7101 {
7102 pf = word + i + k + 1;
7103 for (j = 2; j < k0; ++j)
7104 if (*pf++ != s[j])
7105 break;
7106 if (j < k0)
7107 continue;
7108 }
7109 }
7110 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007111
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007112 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007113 {
7114 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007115 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007116 while (*pf != NUL && *pf != word[i + k0])
7117 ++pf;
7118 if (*pf == NUL)
7119 continue;
7120 ++k0;
7121 }
7122
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007123 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007124 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007125 while (*s == '-')
7126 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007127 /* "k0" gets NOT reduced because
7128 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007129 s++;
7130 }
7131 if (*s == '<')
7132 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007133 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007134 {
7135 p0 = *s - '0';
7136 s++;
7137 }
7138
7139 if (*s == NUL
7140 /* *s == '^' cuts */
7141 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007142 && !spell_iswordp(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02007143 curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007144 {
7145 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007146 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007147 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007148
7149 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007150 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007151 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007152 /* rule fits; stop search */
7153 break;
7154 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007155 }
7156
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007157 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007158 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007159 }
7160
7161 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007162 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007163 if (s == NULL)
7164 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007165 pf = smp[n].sm_rules;
7166 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007167 if (p0 == 1 && z == 0)
7168 {
7169 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007170 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
7171 || res[reslen - 1] == *s))
7172 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007173 z0 = 1;
7174 z = 1;
7175 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007176 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007177 {
7178 word[i + k0] = *s;
7179 k0++;
7180 s++;
7181 }
7182 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +00007183 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007184
7185 /* new "actual letter" */
7186 c = word[i];
7187 }
7188 else
7189 {
7190 /* no '<' rule used */
7191 i += k - 1;
7192 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007193 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007194 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007195 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007196 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007197 s++;
7198 }
7199 /* new "actual letter" */
7200 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007201 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007202 {
7203 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007204 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +00007205 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007206 i = 0;
7207 z0 = 1;
7208 }
7209 }
7210 break;
7211 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007212 }
7213 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01007214 else if (VIM_ISWHITE(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007215 {
7216 c = ' ';
7217 k = 1;
7218 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007219
7220 if (z0 == 0)
7221 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007222 if (k && !p0 && reslen < MAXWLEN && c != NUL
7223 && (!slang->sl_collapse || reslen == 0
7224 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007225 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007226 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007227
7228 i++;
7229 z = 0;
7230 k = 0;
7231 }
7232 }
7233
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007234 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007235}
7236
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007237/*
7238 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
7239 * Multi-byte version of spell_soundfold().
7240 */
7241 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007242spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007243{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007244 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007245 int word[MAXWLEN];
7246 int wres[MAXWLEN];
7247 int l;
7248 char_u *s;
7249 int *ws;
7250 char_u *t;
7251 int *pf;
7252 int i, j, z;
7253 int reslen;
7254 int n, k = 0;
7255 int z0;
7256 int k0;
7257 int n0;
7258 int c;
7259 int pri;
7260 int p0 = -333;
7261 int c0;
7262 int did_white = FALSE;
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007263 int wordlen;
7264
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007265
7266 /*
7267 * Convert the multi-byte string to a wide-character string.
7268 * Remove accents, if wanted. We actually remove all non-word characters.
7269 * But keep white space.
7270 */
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007271 wordlen = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007272 for (s = inword; *s != NUL; )
7273 {
7274 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007275 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007276 if (slang->sl_rem_accents)
7277 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01007278 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007279 {
7280 if (did_white)
7281 continue;
7282 c = ' ';
7283 did_white = TRUE;
7284 }
7285 else
7286 {
7287 did_white = FALSE;
Bram Moolenaarcc63c642013-11-12 04:44:01 +01007288 if (!spell_iswordp_nmw(t, curwin))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007289 continue;
7290 }
7291 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007292 word[wordlen++] = c;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007293 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007294 word[wordlen] = NUL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007295
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007296 /*
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007297 * This algorithm comes from Aspell phonet.cpp.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007298 * Converted from C++ to C. Added support for multi-byte chars.
7299 * Changed to keep spaces.
7300 */
7301 i = reslen = z = 0;
7302 while ((c = word[i]) != NUL)
7303 {
7304 /* Start with the first rule that has the character in the word. */
7305 n = slang->sl_sal_first[c & 0xff];
7306 z0 = 0;
7307
7308 if (n >= 0)
7309 {
Bram Moolenaar95e85792010-08-01 15:37:02 +02007310 /* Check all rules for the same index byte.
7311 * If c is 0x300 need extra check for the end of the array, as
7312 * (c & 0xff) is NUL. */
7313 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff)
7314 && ws[0] != NUL; ++n)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007315 {
7316 /* Quickly skip entries that don't match the word. Most
7317 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007318 if (c != ws[0])
7319 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007320 k = smp[n].sm_leadlen;
7321 if (k > 1)
7322 {
7323 if (word[i + 1] != ws[1])
7324 continue;
7325 if (k > 2)
7326 {
7327 for (j = 2; j < k; ++j)
7328 if (word[i + j] != ws[j])
7329 break;
7330 if (j < k)
7331 continue;
7332 }
7333 }
7334
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007335 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007336 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007337 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007338 while (*pf != NUL && *pf != word[i + k])
7339 ++pf;
7340 if (*pf == NUL)
7341 continue;
7342 ++k;
7343 }
7344 s = smp[n].sm_rules;
7345 pri = 5; /* default priority */
7346
7347 p0 = *s;
7348 k0 = k;
7349 while (*s == '-' && k > 1)
7350 {
7351 k--;
7352 s++;
7353 }
7354 if (*s == '<')
7355 s++;
7356 if (VIM_ISDIGIT(*s))
7357 {
7358 /* determine priority */
7359 pri = *s - '0';
7360 s++;
7361 }
7362 if (*s == '^' && *(s + 1) == '^')
7363 s++;
7364
7365 if (*s == NUL
7366 || (*s == '^'
7367 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02007368 || spell_iswordp_w(word + i - 1, curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007369 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02007370 || (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007371 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02007372 && spell_iswordp_w(word + i - 1, curwin)
7373 && (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007374 {
7375 /* search for followup rules, if: */
7376 /* followup and k > 1 and NO '-' in searchstring */
7377 c0 = word[i + k - 1];
7378 n0 = slang->sl_sal_first[c0 & 0xff];
7379
7380 if (slang->sl_followup && k > 1 && n0 >= 0
7381 && p0 != '-' && word[i + k] != NUL)
7382 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007383 /* Test follow-up rule for "word[i + k]"; loop over
7384 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007385 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
7386 == (c0 & 0xff); ++n0)
7387 {
7388 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007389 */
7390 if (c0 != ws[0])
7391 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007392 k0 = smp[n0].sm_leadlen;
7393 if (k0 > 1)
7394 {
7395 if (word[i + k] != ws[1])
7396 continue;
7397 if (k0 > 2)
7398 {
7399 pf = word + i + k + 1;
7400 for (j = 2; j < k0; ++j)
7401 if (*pf++ != ws[j])
7402 break;
7403 if (j < k0)
7404 continue;
7405 }
7406 }
7407 k0 += k - 1;
7408
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007409 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007410 {
7411 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007412 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007413 while (*pf != NUL && *pf != word[i + k0])
7414 ++pf;
7415 if (*pf == NUL)
7416 continue;
7417 ++k0;
7418 }
7419
7420 p0 = 5;
7421 s = smp[n0].sm_rules;
7422 while (*s == '-')
7423 {
7424 /* "k0" gets NOT reduced because
7425 * "if (k0 == k)" */
7426 s++;
7427 }
7428 if (*s == '<')
7429 s++;
7430 if (VIM_ISDIGIT(*s))
7431 {
7432 p0 = *s - '0';
7433 s++;
7434 }
7435
7436 if (*s == NUL
7437 /* *s == '^' cuts */
7438 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007439 && !spell_iswordp_w(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02007440 curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007441 {
7442 if (k0 == k)
7443 /* this is just a piece of the string */
7444 continue;
7445
7446 if (p0 < pri)
7447 /* priority too low */
7448 continue;
7449 /* rule fits; stop search */
7450 break;
7451 }
7452 }
7453
7454 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
7455 == (c0 & 0xff))
7456 continue;
7457 }
7458
7459 /* replace string */
7460 ws = smp[n].sm_to_w;
7461 s = smp[n].sm_rules;
7462 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
7463 if (p0 == 1 && z == 0)
7464 {
7465 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007466 if (reslen > 0 && ws != NULL && *ws != NUL
7467 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007468 || wres[reslen - 1] == *ws))
7469 reslen--;
7470 z0 = 1;
7471 z = 1;
7472 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007473 if (ws != NULL)
7474 while (*ws != NUL && word[i + k0] != NUL)
7475 {
7476 word[i + k0] = *ws;
7477 k0++;
7478 ws++;
7479 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007480 if (k > k0)
7481 mch_memmove(word + i + k0, word + i + k,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007482 sizeof(int) * (wordlen - (i + k) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007483
7484 /* new "actual letter" */
7485 c = word[i];
7486 }
7487 else
7488 {
7489 /* no '<' rule used */
7490 i += k - 1;
7491 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007492 if (ws != NULL)
7493 while (*ws != NUL && ws[1] != NUL
7494 && reslen < MAXWLEN)
7495 {
7496 if (reslen == 0 || wres[reslen - 1] != *ws)
7497 wres[reslen++] = *ws;
7498 ws++;
7499 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007500 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007501 if (ws == NULL)
7502 c = NUL;
7503 else
7504 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007505 if (strstr((char *)s, "^^") != NULL)
7506 {
7507 if (c != NUL)
7508 wres[reslen++] = c;
7509 mch_memmove(word, word + i + 1,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007510 sizeof(int) * (wordlen - (i + 1) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007511 i = 0;
7512 z0 = 1;
7513 }
7514 }
7515 break;
7516 }
7517 }
7518 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01007519 else if (VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007520 {
7521 c = ' ';
7522 k = 1;
7523 }
7524
7525 if (z0 == 0)
7526 {
7527 if (k && !p0 && reslen < MAXWLEN && c != NUL
7528 && (!slang->sl_collapse || reslen == 0
7529 || wres[reslen - 1] != c))
7530 /* condense only double letters */
7531 wres[reslen++] = c;
7532
7533 i++;
7534 z = 0;
7535 k = 0;
7536 }
7537 }
7538
7539 /* Convert wide characters in "wres" to a multi-byte string in "res". */
7540 l = 0;
7541 for (n = 0; n < reslen; ++n)
7542 {
7543 l += mb_char2bytes(wres[n], res + l);
7544 if (l + MB_MAXBYTES > MAXWLEN)
7545 break;
7546 }
7547 res[l] = NUL;
7548}
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007549
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007550/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007551 * Compute a score for two sound-a-like words.
7552 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
7553 * Instead of a generic loop we write out the code. That keeps it fast by
7554 * avoiding checks that will not be possible.
7555 */
7556 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007557soundalike_score(
7558 char_u *goodstart, /* sound-folded good word */
7559 char_u *badstart) /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007560{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007561 char_u *goodsound = goodstart;
7562 char_u *badsound = badstart;
7563 int goodlen;
7564 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007565 int n;
7566 char_u *pl, *ps;
7567 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007568 int score = 0;
7569
Bram Moolenaar121d95f2010-08-01 15:11:43 +02007570 /* Adding/inserting "*" at the start (word starts with vowel) shouldn't be
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007571 * counted so much, vowels halfway the word aren't counted at all. */
7572 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
7573 {
Bram Moolenaar121d95f2010-08-01 15:11:43 +02007574 if ((badsound[0] == NUL && goodsound[1] == NUL)
7575 || (goodsound[0] == NUL && badsound[1] == NUL))
7576 /* changing word with vowel to word without a sound */
7577 return SCORE_DEL;
7578 if (badsound[0] == NUL || goodsound[0] == NUL)
7579 /* more than two changes */
7580 return SCORE_MAXMAX;
7581
Bram Moolenaar4770d092006-01-12 23:22:24 +00007582 if (badsound[1] == goodsound[1]
7583 || (badsound[1] != NUL
7584 && goodsound[1] != NUL
7585 && badsound[2] == goodsound[2]))
7586 {
7587 /* handle like a substitute */
7588 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007589 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00007590 {
7591 score = 2 * SCORE_DEL / 3;
7592 if (*badsound == '*')
7593 ++badsound;
7594 else
7595 ++goodsound;
7596 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007597 }
7598
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007599 goodlen = (int)STRLEN(goodsound);
7600 badlen = (int)STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007601
Bram Moolenaarf711faf2007-05-10 16:48:19 +00007602 /* Return quickly if the lengths are too different to be fixed by two
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007603 * changes. */
7604 n = goodlen - badlen;
7605 if (n < -2 || n > 2)
7606 return SCORE_MAXMAX;
7607
7608 if (n > 0)
7609 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007610 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007611 ps = badsound;
7612 }
7613 else
7614 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007615 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007616 ps = goodsound;
7617 }
7618
7619 /* Skip over the identical part. */
7620 while (*pl == *ps && *pl != NUL)
7621 {
7622 ++pl;
7623 ++ps;
7624 }
7625
7626 switch (n)
7627 {
7628 case -2:
7629 case 2:
7630 /*
7631 * Must delete two characters from "pl".
7632 */
7633 ++pl; /* first delete */
7634 while (*pl == *ps)
7635 {
7636 ++pl;
7637 ++ps;
7638 }
7639 /* strings must be equal after second delete */
7640 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007641 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007642
7643 /* Failed to compare. */
7644 break;
7645
7646 case -1:
7647 case 1:
7648 /*
7649 * Minimal one delete from "pl" required.
7650 */
7651
7652 /* 1: delete */
7653 pl2 = pl + 1;
7654 ps2 = ps;
7655 while (*pl2 == *ps2)
7656 {
7657 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007658 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007659 ++pl2;
7660 ++ps2;
7661 }
7662
7663 /* 2: delete then swap, then rest must be equal */
7664 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7665 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007666 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007667
7668 /* 3: delete then substitute, then the rest must be equal */
7669 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007670 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007671
7672 /* 4: first swap then delete */
7673 if (pl[0] == ps[1] && pl[1] == ps[0])
7674 {
7675 pl2 = pl + 2; /* swap, skip two chars */
7676 ps2 = ps + 2;
7677 while (*pl2 == *ps2)
7678 {
7679 ++pl2;
7680 ++ps2;
7681 }
7682 /* delete a char and then strings must be equal */
7683 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007684 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007685 }
7686
7687 /* 5: first substitute then delete */
7688 pl2 = pl + 1; /* substitute, skip one char */
7689 ps2 = ps + 1;
7690 while (*pl2 == *ps2)
7691 {
7692 ++pl2;
7693 ++ps2;
7694 }
7695 /* delete a char and then strings must be equal */
7696 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007697 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007698
7699 /* Failed to compare. */
7700 break;
7701
7702 case 0:
7703 /*
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00007704 * Lengths are equal, thus changes must result in same length: An
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007705 * insert is only possible in combination with a delete.
7706 * 1: check if for identical strings
7707 */
7708 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007709 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007710
7711 /* 2: swap */
7712 if (pl[0] == ps[1] && pl[1] == ps[0])
7713 {
7714 pl2 = pl + 2; /* swap, skip two chars */
7715 ps2 = ps + 2;
7716 while (*pl2 == *ps2)
7717 {
7718 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007719 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007720 ++pl2;
7721 ++ps2;
7722 }
7723 /* 3: swap and swap again */
7724 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7725 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007726 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007727
7728 /* 4: swap and substitute */
7729 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007730 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007731 }
7732
7733 /* 5: substitute */
7734 pl2 = pl + 1;
7735 ps2 = ps + 1;
7736 while (*pl2 == *ps2)
7737 {
7738 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007739 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007740 ++pl2;
7741 ++ps2;
7742 }
7743
7744 /* 6: substitute and swap */
7745 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7746 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007747 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007748
7749 /* 7: substitute and substitute */
7750 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007751 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007752
7753 /* 8: insert then delete */
7754 pl2 = pl;
7755 ps2 = ps + 1;
7756 while (*pl2 == *ps2)
7757 {
7758 ++pl2;
7759 ++ps2;
7760 }
7761 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007762 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007763
7764 /* 9: delete then insert */
7765 pl2 = pl + 1;
7766 ps2 = ps;
7767 while (*pl2 == *ps2)
7768 {
7769 ++pl2;
7770 ++ps2;
7771 }
7772 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007773 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007774
7775 /* Failed to compare. */
7776 break;
7777 }
7778
7779 return SCORE_MAXMAX;
7780}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007781
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007782/*
7783 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007784 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007785 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00007786 * The algorithm is described by Du and Chang, 1992.
7787 * The implementation of the algorithm comes from Aspell editdist.cpp,
7788 * edit_distance(). It has been converted from C++ to C and modified to
7789 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007790 */
7791 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007792spell_edit_score(
7793 slang_T *slang,
7794 char_u *badword,
7795 char_u *goodword)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007796{
7797 int *cnt;
Bram Moolenaarf711faf2007-05-10 16:48:19 +00007798 int badlen, goodlen; /* lengths including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007799 int j, i;
7800 int t;
7801 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007802 int pbc, pgc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007803 char_u *p;
7804 int wbadword[MAXWLEN];
7805 int wgoodword[MAXWLEN];
7806
7807 if (has_mbyte)
7808 {
7809 /* Get the characters from the multi-byte strings and put them in an
7810 * int array for easy access. */
7811 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007812 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +00007813 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007814 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007815 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +00007816 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007817 }
7818 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007819 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007820 badlen = (int)STRLEN(badword) + 1;
7821 goodlen = (int)STRLEN(goodword) + 1;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007822 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007823
7824 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
7825#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007826 cnt = ALLOC_MULT(int, (badlen + 1) * (goodlen + 1));
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007827 if (cnt == NULL)
7828 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007829
7830 CNT(0, 0) = 0;
7831 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007832 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007833
7834 for (i = 1; i <= badlen; ++i)
7835 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007836 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007837 for (j = 1; j <= goodlen; ++j)
7838 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007839 if (has_mbyte)
7840 {
7841 bc = wbadword[i - 1];
7842 gc = wgoodword[j - 1];
7843 }
7844 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007845 {
7846 bc = badword[i - 1];
7847 gc = goodword[j - 1];
7848 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007849 if (bc == gc)
7850 CNT(i, j) = CNT(i - 1, j - 1);
7851 else
7852 {
7853 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007854 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007855 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
7856 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00007857 {
7858 /* For a similar character use SCORE_SIMILAR. */
7859 if (slang != NULL
7860 && slang->sl_has_map
7861 && similar_chars(slang, gc, bc))
7862 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
7863 else
7864 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
7865 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007866
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007867 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007868 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007869 if (has_mbyte)
7870 {
7871 pbc = wbadword[i - 2];
7872 pgc = wgoodword[j - 2];
7873 }
7874 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007875 {
7876 pbc = badword[i - 2];
7877 pgc = goodword[j - 2];
7878 }
7879 if (bc == pgc && pbc == gc)
7880 {
7881 t = SCORE_SWAP + CNT(i - 2, j - 2);
7882 if (t < CNT(i, j))
7883 CNT(i, j) = t;
7884 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007885 }
7886 t = SCORE_DEL + CNT(i - 1, j);
7887 if (t < CNT(i, j))
7888 CNT(i, j) = t;
7889 t = SCORE_INS + CNT(i, j - 1);
7890 if (t < CNT(i, j))
7891 CNT(i, j) = t;
7892 }
7893 }
7894 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007895
7896 i = CNT(badlen - 1, goodlen - 1);
7897 vim_free(cnt);
7898 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007899}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007900
Bram Moolenaar4770d092006-01-12 23:22:24 +00007901typedef struct
7902{
7903 int badi;
7904 int goodi;
7905 int score;
7906} limitscore_T;
7907
7908/*
7909 * Like spell_edit_score(), but with a limit on the score to make it faster.
7910 * May return SCORE_MAXMAX when the score is higher than "limit".
7911 *
7912 * This uses a stack for the edits still to be tried.
7913 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
7914 * for multi-byte characters.
7915 */
7916 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007917spell_edit_score_limit(
7918 slang_T *slang,
7919 char_u *badword,
7920 char_u *goodword,
7921 int limit)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007922{
7923 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
7924 int stackidx;
7925 int bi, gi;
7926 int bi2, gi2;
7927 int bc, gc;
7928 int score;
7929 int score_off;
7930 int minscore;
7931 int round;
7932
Bram Moolenaar4770d092006-01-12 23:22:24 +00007933 /* Multi-byte characters require a bit more work, use a different function
7934 * to avoid testing "has_mbyte" quite often. */
7935 if (has_mbyte)
7936 return spell_edit_score_limit_w(slang, badword, goodword, limit);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007937
7938 /*
7939 * The idea is to go from start to end over the words. So long as
7940 * characters are equal just continue, this always gives the lowest score.
7941 * When there is a difference try several alternatives. Each alternative
7942 * increases "score" for the edit distance. Some of the alternatives are
7943 * pushed unto a stack and tried later, some are tried right away. At the
7944 * end of the word the score for one alternative is known. The lowest
7945 * possible score is stored in "minscore".
7946 */
7947 stackidx = 0;
7948 bi = 0;
7949 gi = 0;
7950 score = 0;
7951 minscore = limit + 1;
7952
7953 for (;;)
7954 {
7955 /* Skip over an equal part, score remains the same. */
7956 for (;;)
7957 {
7958 bc = badword[bi];
7959 gc = goodword[gi];
7960 if (bc != gc) /* stop at a char that's different */
7961 break;
7962 if (bc == NUL) /* both words end */
7963 {
7964 if (score < minscore)
7965 minscore = score;
7966 goto pop; /* do next alternative */
7967 }
7968 ++bi;
7969 ++gi;
7970 }
7971
7972 if (gc == NUL) /* goodword ends, delete badword chars */
7973 {
7974 do
7975 {
7976 if ((score += SCORE_DEL) >= minscore)
7977 goto pop; /* do next alternative */
7978 } while (badword[++bi] != NUL);
7979 minscore = score;
7980 }
7981 else if (bc == NUL) /* badword ends, insert badword chars */
7982 {
7983 do
7984 {
7985 if ((score += SCORE_INS) >= minscore)
7986 goto pop; /* do next alternative */
7987 } while (goodword[++gi] != NUL);
7988 minscore = score;
7989 }
7990 else /* both words continue */
7991 {
7992 /* If not close to the limit, perform a change. Only try changes
7993 * that may lead to a lower score than "minscore".
7994 * round 0: try deleting a char from badword
7995 * round 1: try inserting a char in badword */
7996 for (round = 0; round <= 1; ++round)
7997 {
7998 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
7999 if (score_off < minscore)
8000 {
8001 if (score_off + SCORE_EDIT_MIN >= minscore)
8002 {
8003 /* Near the limit, rest of the words must match. We
8004 * can check that right now, no need to push an item
8005 * onto the stack. */
8006 bi2 = bi + 1 - round;
8007 gi2 = gi + round;
8008 while (goodword[gi2] == badword[bi2])
8009 {
8010 if (goodword[gi2] == NUL)
8011 {
8012 minscore = score_off;
8013 break;
8014 }
8015 ++bi2;
8016 ++gi2;
8017 }
8018 }
8019 else
8020 {
8021 /* try deleting/inserting a character later */
8022 stack[stackidx].badi = bi + 1 - round;
8023 stack[stackidx].goodi = gi + round;
8024 stack[stackidx].score = score_off;
8025 ++stackidx;
8026 }
8027 }
8028 }
8029
8030 if (score + SCORE_SWAP < minscore)
8031 {
8032 /* If swapping two characters makes a match then the
8033 * substitution is more expensive, thus there is no need to
8034 * try both. */
8035 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
8036 {
8037 /* Swap two characters, that is: skip them. */
8038 gi += 2;
8039 bi += 2;
8040 score += SCORE_SWAP;
8041 continue;
8042 }
8043 }
8044
8045 /* Substitute one character for another which is the same
8046 * thing as deleting a character from both goodword and badword.
8047 * Use a better score when there is only a case difference. */
8048 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
8049 score += SCORE_ICASE;
8050 else
8051 {
8052 /* For a similar character use SCORE_SIMILAR. */
8053 if (slang != NULL
8054 && slang->sl_has_map
8055 && similar_chars(slang, gc, bc))
8056 score += SCORE_SIMILAR;
8057 else
8058 score += SCORE_SUBST;
8059 }
8060
8061 if (score < minscore)
8062 {
8063 /* Do the substitution. */
8064 ++gi;
8065 ++bi;
8066 continue;
8067 }
8068 }
8069pop:
8070 /*
8071 * Get here to try the next alternative, pop it from the stack.
8072 */
8073 if (stackidx == 0) /* stack is empty, finished */
8074 break;
8075
8076 /* pop an item from the stack */
8077 --stackidx;
8078 gi = stack[stackidx].goodi;
8079 bi = stack[stackidx].badi;
8080 score = stack[stackidx].score;
8081 }
8082
8083 /* When the score goes over "limit" it may actually be much higher.
8084 * Return a very large number to avoid going below the limit when giving a
8085 * bonus. */
8086 if (minscore > limit)
8087 return SCORE_MAXMAX;
8088 return minscore;
8089}
8090
Bram Moolenaar4770d092006-01-12 23:22:24 +00008091/*
8092 * Multi-byte version of spell_edit_score_limit().
8093 * Keep it in sync with the above!
8094 */
8095 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008096spell_edit_score_limit_w(
8097 slang_T *slang,
8098 char_u *badword,
8099 char_u *goodword,
8100 int limit)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008101{
8102 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
8103 int stackidx;
8104 int bi, gi;
8105 int bi2, gi2;
8106 int bc, gc;
8107 int score;
8108 int score_off;
8109 int minscore;
8110 int round;
8111 char_u *p;
8112 int wbadword[MAXWLEN];
8113 int wgoodword[MAXWLEN];
8114
8115 /* Get the characters from the multi-byte strings and put them in an
8116 * int array for easy access. */
8117 bi = 0;
8118 for (p = badword; *p != NUL; )
8119 wbadword[bi++] = mb_cptr2char_adv(&p);
8120 wbadword[bi++] = 0;
8121 gi = 0;
8122 for (p = goodword; *p != NUL; )
8123 wgoodword[gi++] = mb_cptr2char_adv(&p);
8124 wgoodword[gi++] = 0;
8125
8126 /*
8127 * The idea is to go from start to end over the words. So long as
8128 * characters are equal just continue, this always gives the lowest score.
8129 * When there is a difference try several alternatives. Each alternative
8130 * increases "score" for the edit distance. Some of the alternatives are
8131 * pushed unto a stack and tried later, some are tried right away. At the
8132 * end of the word the score for one alternative is known. The lowest
8133 * possible score is stored in "minscore".
8134 */
8135 stackidx = 0;
8136 bi = 0;
8137 gi = 0;
8138 score = 0;
8139 minscore = limit + 1;
8140
8141 for (;;)
8142 {
8143 /* Skip over an equal part, score remains the same. */
8144 for (;;)
8145 {
8146 bc = wbadword[bi];
8147 gc = wgoodword[gi];
8148
8149 if (bc != gc) /* stop at a char that's different */
8150 break;
8151 if (bc == NUL) /* both words end */
8152 {
8153 if (score < minscore)
8154 minscore = score;
8155 goto pop; /* do next alternative */
8156 }
8157 ++bi;
8158 ++gi;
8159 }
8160
8161 if (gc == NUL) /* goodword ends, delete badword chars */
8162 {
8163 do
8164 {
8165 if ((score += SCORE_DEL) >= minscore)
8166 goto pop; /* do next alternative */
8167 } while (wbadword[++bi] != NUL);
8168 minscore = score;
8169 }
8170 else if (bc == NUL) /* badword ends, insert badword chars */
8171 {
8172 do
8173 {
8174 if ((score += SCORE_INS) >= minscore)
8175 goto pop; /* do next alternative */
8176 } while (wgoodword[++gi] != NUL);
8177 minscore = score;
8178 }
8179 else /* both words continue */
8180 {
8181 /* If not close to the limit, perform a change. Only try changes
8182 * that may lead to a lower score than "minscore".
8183 * round 0: try deleting a char from badword
8184 * round 1: try inserting a char in badword */
8185 for (round = 0; round <= 1; ++round)
8186 {
8187 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
8188 if (score_off < minscore)
8189 {
8190 if (score_off + SCORE_EDIT_MIN >= minscore)
8191 {
8192 /* Near the limit, rest of the words must match. We
8193 * can check that right now, no need to push an item
8194 * onto the stack. */
8195 bi2 = bi + 1 - round;
8196 gi2 = gi + round;
8197 while (wgoodword[gi2] == wbadword[bi2])
8198 {
8199 if (wgoodword[gi2] == NUL)
8200 {
8201 minscore = score_off;
8202 break;
8203 }
8204 ++bi2;
8205 ++gi2;
8206 }
8207 }
8208 else
8209 {
8210 /* try deleting a character from badword later */
8211 stack[stackidx].badi = bi + 1 - round;
8212 stack[stackidx].goodi = gi + round;
8213 stack[stackidx].score = score_off;
8214 ++stackidx;
8215 }
8216 }
8217 }
8218
8219 if (score + SCORE_SWAP < minscore)
8220 {
8221 /* If swapping two characters makes a match then the
8222 * substitution is more expensive, thus there is no need to
8223 * try both. */
8224 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
8225 {
8226 /* Swap two characters, that is: skip them. */
8227 gi += 2;
8228 bi += 2;
8229 score += SCORE_SWAP;
8230 continue;
8231 }
8232 }
8233
8234 /* Substitute one character for another which is the same
8235 * thing as deleting a character from both goodword and badword.
8236 * Use a better score when there is only a case difference. */
8237 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
8238 score += SCORE_ICASE;
8239 else
8240 {
8241 /* For a similar character use SCORE_SIMILAR. */
8242 if (slang != NULL
8243 && slang->sl_has_map
8244 && similar_chars(slang, gc, bc))
8245 score += SCORE_SIMILAR;
8246 else
8247 score += SCORE_SUBST;
8248 }
8249
8250 if (score < minscore)
8251 {
8252 /* Do the substitution. */
8253 ++gi;
8254 ++bi;
8255 continue;
8256 }
8257 }
8258pop:
8259 /*
8260 * Get here to try the next alternative, pop it from the stack.
8261 */
8262 if (stackidx == 0) /* stack is empty, finished */
8263 break;
8264
8265 /* pop an item from the stack */
8266 --stackidx;
8267 gi = stack[stackidx].goodi;
8268 bi = stack[stackidx].badi;
8269 score = stack[stackidx].score;
8270 }
8271
8272 /* When the score goes over "limit" it may actually be much higher.
8273 * Return a very large number to avoid going below the limit when giving a
8274 * bonus. */
8275 if (minscore > limit)
8276 return SCORE_MAXMAX;
8277 return minscore;
8278}
Bram Moolenaar4770d092006-01-12 23:22:24 +00008279
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008280/*
8281 * ":spellinfo"
8282 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008283 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008284ex_spellinfo(exarg_T *eap UNUSED)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008285{
8286 int lpi;
8287 langp_T *lp;
8288 char_u *p;
8289
8290 if (no_spell_checking(curwin))
8291 return;
8292
8293 msg_start();
Bram Moolenaar860cae12010-06-05 23:22:07 +02008294 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008295 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008296 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008297 msg_puts("file: ");
8298 msg_puts((char *)lp->lp_slang->sl_fname);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008299 msg_putchar('\n');
8300 p = lp->lp_slang->sl_info;
8301 if (p != NULL)
8302 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01008303 msg_puts((char *)p);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008304 msg_putchar('\n');
8305 }
8306 }
8307 msg_end();
8308}
8309
Bram Moolenaar4770d092006-01-12 23:22:24 +00008310#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
8311#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008312#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +00008313#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
8314#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008315
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008316/*
8317 * ":spelldump"
8318 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008319 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008320ex_spelldump(exarg_T *eap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008321{
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008322 char_u *spl;
8323 long dummy;
8324
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008325 if (no_spell_checking(curwin))
8326 return;
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008327 get_option_value((char_u*)"spl", &dummy, &spl, OPT_LOCAL);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008328
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008329 /* Create a new empty buffer in a new window. */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008330 do_cmdline_cmd((char_u *)"new");
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008331
8332 /* enable spelling locally in the new window */
8333 set_option_value((char_u*)"spell", TRUE, (char_u*)"", OPT_LOCAL);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01008334 set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL);
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008335 vim_free(spl);
8336
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008337 if (!BUFEMPTY())
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008338 return;
8339
Bram Moolenaar860cae12010-06-05 23:22:07 +02008340 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008341
8342 /* Delete the empty line that we started with. */
8343 if (curbuf->b_ml.ml_line_count > 1)
8344 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
8345
8346 redraw_later(NOT_VALID);
8347}
8348
8349/*
8350 * Go through all possible words and:
8351 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
8352 * "ic" and "dir" are not used.
8353 * 2. When "pat" is not NULL: add matching words to insert mode completion.
8354 */
8355 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008356spell_dump_compl(
8357 char_u *pat, /* leading part of the word */
8358 int ic, /* ignore case */
8359 int *dir, /* direction for adding matches */
8360 int dumpflags_arg) /* DUMPFLAG_* */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008361{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008362 langp_T *lp;
8363 slang_T *slang;
8364 idx_T arridx[MAXWLEN];
8365 int curi[MAXWLEN];
8366 char_u word[MAXWLEN];
8367 int c;
8368 char_u *byts;
8369 idx_T *idxs;
8370 linenr_T lnum = 0;
8371 int round;
8372 int depth;
8373 int n;
8374 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +00008375 char_u *region_names = NULL; /* region names being used */
8376 int do_region = TRUE; /* dump region names and numbers */
8377 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008378 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008379 int dumpflags = dumpflags_arg;
8380 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008381
Bram Moolenaard0131a82006-03-04 21:46:13 +00008382 /* When ignoring case or when the pattern starts with capital pass this on
8383 * to dump_word(). */
8384 if (pat != NULL)
8385 {
8386 if (ic)
8387 dumpflags |= DUMPFLAG_ICASE;
8388 else
8389 {
8390 n = captype(pat, NULL);
8391 if (n == WF_ONECAP)
8392 dumpflags |= DUMPFLAG_ONECAP;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01008393 else if (n == WF_ALLCAP && (int)STRLEN(pat) > mb_ptr2len(pat))
Bram Moolenaard0131a82006-03-04 21:46:13 +00008394 dumpflags |= DUMPFLAG_ALLCAP;
8395 }
8396 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008397
Bram Moolenaar7887d882005-07-01 22:33:52 +00008398 /* Find out if we can support regions: All languages must support the same
8399 * regions or none at all. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02008400 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +00008401 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008402 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +00008403 p = lp->lp_slang->sl_regions;
8404 if (p[0] != 0)
8405 {
8406 if (region_names == NULL) /* first language with regions */
8407 region_names = p;
8408 else if (STRCMP(region_names, p) != 0)
8409 {
8410 do_region = FALSE; /* region names are different */
8411 break;
8412 }
8413 }
8414 }
8415
8416 if (do_region && region_names != NULL)
8417 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008418 if (pat == NULL)
8419 {
8420 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
8421 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
8422 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00008423 }
8424 else
8425 do_region = FALSE;
8426
8427 /*
8428 * Loop over all files loaded for the entries in 'spelllang'.
8429 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02008430 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008431 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008432 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008433 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008434 if (slang->sl_fbyts == NULL) /* reloading failed */
8435 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008436
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008437 if (pat == NULL)
8438 {
8439 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
8440 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
8441 }
8442
8443 /* When matching with a pattern and there are no prefixes only use
8444 * parts of the tree that match "pat". */
8445 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008446 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008447 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008448 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008449
8450 /* round 1: case-folded tree
8451 * round 2: keep-case tree */
8452 for (round = 1; round <= 2; ++round)
8453 {
8454 if (round == 1)
8455 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008456 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008457 byts = slang->sl_fbyts;
8458 idxs = slang->sl_fidxs;
8459 }
8460 else
8461 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008462 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008463 byts = slang->sl_kbyts;
8464 idxs = slang->sl_kidxs;
8465 }
8466 if (byts == NULL)
8467 continue; /* array is empty */
8468
8469 depth = 0;
8470 arridx[0] = 0;
8471 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008472 while (depth >= 0 && !got_int
Bram Moolenaar7591bb32019-03-30 13:53:47 +01008473 && (pat == NULL || !ins_compl_interrupted()))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008474 {
8475 if (curi[depth] > byts[arridx[depth]])
8476 {
8477 /* Done all bytes at this node, go up one level. */
8478 --depth;
8479 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02008480 ins_compl_check_keys(50, FALSE);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008481 }
8482 else
8483 {
8484 /* Do one more byte at this node. */
8485 n = arridx[depth] + curi[depth];
8486 ++curi[depth];
8487 c = byts[n];
8488 if (c == 0)
8489 {
8490 /* End of word, deal with the word.
8491 * Don't use keep-case words in the fold-case tree,
8492 * they will appear in the keep-case tree.
8493 * Only use the word when the region matches. */
8494 flags = (int)idxs[n];
8495 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008496 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +00008497 && (do_region
8498 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008499 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008500 & lp->lp_region) != 0))
8501 {
8502 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00008503 if (!do_region)
8504 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008505
8506 /* Dump the basic word if there is no prefix or
8507 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008508 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008509 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008510 {
8511 dump_word(slang, word, pat, dir,
8512 dumpflags, flags, lnum);
8513 if (pat == NULL)
8514 ++lnum;
8515 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008516
8517 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008518 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008519 lnum = dump_prefixes(slang, word, pat, dir,
8520 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008521 }
8522 }
8523 else
8524 {
8525 /* Normal char, go one level deeper. */
8526 word[depth++] = c;
8527 arridx[depth] = idxs[n];
8528 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008529
8530 /* Check if this characters matches with the pattern.
8531 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +00008532 * Always ignore case here, dump_word() will check
8533 * proper case later. This isn't exactly right when
8534 * length changes for multi-byte characters with
8535 * ignore case... */
8536 if (depth <= patlen
8537 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008538 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008539 }
8540 }
8541 }
8542 }
8543 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008544}
8545
8546/*
8547 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008548 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008549 */
8550 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008551dump_word(
8552 slang_T *slang,
8553 char_u *word,
8554 char_u *pat,
8555 int *dir,
8556 int dumpflags,
8557 int wordflags,
8558 linenr_T lnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008559{
8560 int keepcap = FALSE;
8561 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008562 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008563 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +00008564 char_u badword[MAXWLEN + 10];
8565 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +00008566 int flags = wordflags;
8567
8568 if (dumpflags & DUMPFLAG_ONECAP)
8569 flags |= WF_ONECAP;
8570 if (dumpflags & DUMPFLAG_ALLCAP)
8571 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008572
Bram Moolenaar4770d092006-01-12 23:22:24 +00008573 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008574 {
8575 /* Need to fix case according to "flags". */
8576 make_case_word(word, cword, flags);
8577 p = cword;
8578 }
8579 else
8580 {
8581 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008582 if ((dumpflags & DUMPFLAG_KEEPCASE)
8583 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008584 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008585 keepcap = TRUE;
8586 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00008587 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008588
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008589 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008590 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008591 /* Add flags and regions after a slash. */
8592 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008593 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008594 STRCPY(badword, p);
8595 STRCAT(badword, "/");
8596 if (keepcap)
8597 STRCAT(badword, "=");
8598 if (flags & WF_BANNED)
8599 STRCAT(badword, "!");
8600 else if (flags & WF_RARE)
8601 STRCAT(badword, "?");
8602 if (flags & WF_REGION)
8603 for (i = 0; i < 7; ++i)
8604 if (flags & (0x10000 << i))
8605 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
8606 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008607 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00008608
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008609 if (dumpflags & DUMPFLAG_COUNT)
8610 {
8611 hashitem_T *hi;
8612
8613 /* Include the word count for ":spelldump!". */
8614 hi = hash_find(&slang->sl_wordcount, tw);
8615 if (!HASHITEM_EMPTY(hi))
8616 {
8617 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
8618 tw, HI2WC(hi)->wc_count);
8619 p = IObuff;
8620 }
8621 }
8622
8623 ml_append(lnum, p, (colnr_T)0, FALSE);
8624 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00008625 else if (((dumpflags & DUMPFLAG_ICASE)
8626 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
8627 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008628 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaard9eefe32019-04-06 14:22:21 +02008629 p_ic, NULL, *dir, FALSE) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +00008630 /* if dir was BACKWARD then honor it just once */
8631 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008632}
8633
8634/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008635 * For ":spelldump": Find matching prefixes for "word". Prepend each to
8636 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008637 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008638 * Return the updated line number.
8639 */
8640 static linenr_T
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008641dump_prefixes(
8642 slang_T *slang,
8643 char_u *word, /* case-folded word */
8644 char_u *pat,
8645 int *dir,
8646 int dumpflags,
8647 int flags, /* flags with prefix ID */
8648 linenr_T startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008649{
8650 idx_T arridx[MAXWLEN];
8651 int curi[MAXWLEN];
8652 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +00008653 char_u word_up[MAXWLEN];
8654 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008655 int c;
8656 char_u *byts;
8657 idx_T *idxs;
8658 linenr_T lnum = startlnum;
8659 int depth;
8660 int n;
8661 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008662 int i;
8663
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008664 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +00008665 * upper-case letter in word_up[]. */
8666 c = PTR2CHAR(word);
8667 if (SPELL_TOUPPER(c) != c)
8668 {
8669 onecap_copy(word, word_up, TRUE);
8670 has_word_up = TRUE;
8671 }
8672
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008673 byts = slang->sl_pbyts;
8674 idxs = slang->sl_pidxs;
8675 if (byts != NULL) /* array not is empty */
8676 {
8677 /*
8678 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008679 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008680 */
8681 depth = 0;
8682 arridx[0] = 0;
8683 curi[0] = 1;
8684 while (depth >= 0 && !got_int)
8685 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008686 n = arridx[depth];
8687 len = byts[n];
8688 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008689 {
8690 /* Done all bytes at this node, go up one level. */
8691 --depth;
8692 line_breakcheck();
8693 }
8694 else
8695 {
8696 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008697 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008698 ++curi[depth];
8699 c = byts[n];
8700 if (c == 0)
8701 {
8702 /* End of prefix, find out how many IDs there are. */
8703 for (i = 1; i < len; ++i)
8704 if (byts[n + i] != 0)
8705 break;
8706 curi[depth] += i - 1;
8707
Bram Moolenaar53805d12005-08-01 07:08:33 +00008708 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
8709 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008710 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008711 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008712 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00008713 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008714 : flags, lnum);
8715 if (lnum != 0)
8716 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008717 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00008718
8719 /* Check for prefix that matches the word when the
8720 * first letter is upper-case, but only if the prefix has
8721 * a condition. */
8722 if (has_word_up)
8723 {
8724 c = valid_word_prefix(i, n, flags, word_up, slang,
8725 TRUE);
8726 if (c != 0)
8727 {
8728 vim_strncpy(prefix + depth, word_up,
8729 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008730 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00008731 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008732 : flags, lnum);
8733 if (lnum != 0)
8734 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00008735 }
8736 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008737 }
8738 else
8739 {
8740 /* Normal char, go one level deeper. */
8741 prefix[depth++] = c;
8742 arridx[depth] = idxs[n];
8743 curi[depth] = 1;
8744 }
8745 }
8746 }
8747 }
8748
8749 return lnum;
8750}
8751
Bram Moolenaar95529562005-08-25 21:21:38 +00008752/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008753 * Move "p" to the end of word "start".
8754 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +00008755 */
8756 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008757spell_to_word_end(char_u *start, win_T *win)
Bram Moolenaar95529562005-08-25 21:21:38 +00008758{
8759 char_u *p = start;
8760
Bram Moolenaar860cae12010-06-05 23:22:07 +02008761 while (*p != NUL && spell_iswordp(p, win))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01008762 MB_PTR_ADV(p);
Bram Moolenaar95529562005-08-25 21:21:38 +00008763 return p;
8764}
8765
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008766/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008767 * For Insert mode completion CTRL-X s:
8768 * Find start of the word in front of column "startcol".
8769 * We don't check if it is badly spelled, with completion we can only change
8770 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008771 * Returns the column number of the word.
8772 */
8773 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008774spell_word_start(int startcol)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008775{
8776 char_u *line;
8777 char_u *p;
8778 int col = 0;
8779
Bram Moolenaar95529562005-08-25 21:21:38 +00008780 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008781 return startcol;
8782
8783 /* Find a word character before "startcol". */
8784 line = ml_get_curline();
8785 for (p = line + startcol; p > line; )
8786 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01008787 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01008788 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008789 break;
8790 }
8791
8792 /* Go back to start of the word. */
8793 while (p > line)
8794 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008795 col = (int)(p - line);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01008796 MB_PTR_BACK(line, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02008797 if (!spell_iswordp(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008798 break;
8799 col = 0;
8800 }
8801
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008802 return col;
8803}
8804
8805/*
Bram Moolenaar4effc802005-09-30 21:12:02 +00008806 * Need to check for 'spellcapcheck' now, the word is removed before
8807 * expand_spelling() is called. Therefore the ugly global variable.
8808 */
8809static int spell_expand_need_cap;
8810
8811 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008812spell_expand_check_cap(colnr_T col)
Bram Moolenaar4effc802005-09-30 21:12:02 +00008813{
8814 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
8815}
8816
8817/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008818 * Get list of spelling suggestions.
8819 * Used for Insert mode completion CTRL-X ?.
8820 * Returns the number of matches. The matches are in "matchp[]", array of
8821 * allocated strings.
8822 */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008823 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008824expand_spelling(
8825 linenr_T lnum UNUSED,
8826 char_u *pat,
8827 char_u ***matchp)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008828{
8829 garray_T ga;
8830
Bram Moolenaar4770d092006-01-12 23:22:24 +00008831 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008832 *matchp = ga.ga_data;
8833 return ga.ga_len;
8834}
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008835
Bram Moolenaare677df82019-09-02 22:31:11 +02008836/*
8837 * Return TRUE if "val" is a valid 'spellang' value.
8838 */
8839 int
8840valid_spellang(char_u *val)
8841{
8842 return valid_name(val, ".-_,@");
8843}
8844
8845/*
8846 * Return TRUE if "val" is a valid 'spellfile' value.
8847 */
8848 int
8849valid_spellfile(char_u *val)
8850{
8851 char_u *s;
8852
8853 for (s = val; *s != NUL; ++s)
8854 if (!vim_isfilec(*s) && *s != ',')
8855 return FALSE;
8856 return TRUE;
8857}
8858
8859/*
8860 * Handle side effects of setting 'spell'.
8861 * Return an error message or NULL for success.
8862 */
8863 char *
8864did_set_spell_option(int is_spellfile)
8865{
8866 char *errmsg = NULL;
8867 win_T *wp;
8868 int l;
8869
8870 if (is_spellfile)
8871 {
8872 l = (int)STRLEN(curwin->w_s->b_p_spf);
8873 if (l > 0 && (l < 4
8874 || STRCMP(curwin->w_s->b_p_spf + l - 4, ".add") != 0))
8875 errmsg = e_invarg;
8876 }
8877
8878 if (errmsg == NULL)
8879 {
8880 FOR_ALL_WINDOWS(wp)
8881 if (wp->w_buffer == curbuf && wp->w_p_spell)
8882 {
8883 errmsg = did_set_spelllang(wp);
8884 break;
8885 }
8886 }
8887 return errmsg;
8888}
8889
8890/*
8891 * Set curbuf->b_cap_prog to the regexp program for 'spellcapcheck'.
8892 * Return error message when failed, NULL when OK.
8893 */
8894 char *
8895compile_cap_prog(synblock_T *synblock)
8896{
8897 regprog_T *rp = synblock->b_cap_prog;
8898 char_u *re;
8899
8900 if (*synblock->b_p_spc == NUL)
8901 synblock->b_cap_prog = NULL;
8902 else
8903 {
8904 // Prepend a ^ so that we only match at one column
8905 re = concat_str((char_u *)"^", synblock->b_p_spc);
8906 if (re != NULL)
8907 {
8908 synblock->b_cap_prog = vim_regcomp(re, RE_MAGIC);
8909 vim_free(re);
8910 if (synblock->b_cap_prog == NULL)
8911 {
8912 synblock->b_cap_prog = rp; // restore the previous program
8913 return e_invarg;
8914 }
8915 }
8916 }
8917
8918 vim_regfree(rp);
8919 return NULL;
8920}
8921
8922#endif // FEAT_SPELL